fix(controlplane): resolve the default org's providers with its aliases

This commit is contained in:
sam committed 2026-10-20 03:44:21 +11:00
1 parent 4847fb0b77
commit 6194a51ed0
2 files changed
+120 -2

No files matched your search

+27 -2
View File
@@ -64,8 +64,17 @@ func orgModelPolicyDefinition() Definition {
// whose model the org may not use is left out, so it fails as unknown.
func (s *Server) ModelsFor(ctx context.Context, orgID string) (config.OrgModels, error) {
instance, err := s.modelAliases(ctx, storecontract.DefaultOrgID)
if err != nil || orgID == "" || orgID == storecontract.DefaultOrgID {
return config.OrgModels{Org: orgID, Aliases: instance}, err
if err != nil {
return config.OrgModels{}, err
}
if orgID == "" || orgID == storecontract.DefaultOrgID {
// The instance's aliases name the instance's providers, so both
// resolve together: an alias without its provider builds no runtime.
providers, err := s.instanceProviders(ctx)
if err != nil {
return config.OrgModels{}, err
}
return config.OrgModels{Org: orgID, Aliases: instance, Providers: providers}, nil
}
policy, err := s.orgPolicy(ctx, orgID)
if err != nil {
@@ -115,6 +124,22 @@ func (s *Server) orgPolicy(ctx context.Context, orgID string) (*orgModelPolicy,
return &policy, nil
}
// instanceProviders is the providers the instance configured, which are the
// ones the system org's aliases name. The credential travels with the
// provider: the document holds it as api_key_ref, and without it every call
// that provider serves is unauthenticated.
func (s *Server) instanceProviders(ctx context.Context) (map[string]config.Provider, error) {
var docs map[string]providerDocument
if err := s.readResource(ctx, storecontract.DefaultOrgID, ProviderSettingsKind, &docs); err != nil {
return nil, err
}
out := make(map[string]config.Provider, len(docs))
for name, doc := range docs {
out[name] = config.Provider{Class: doc.Class, BaseURL: doc.BaseURL, APIKeyEnv: doc.APIKeyEnv, APIKey: doc.APIKey}
}
return out, nil
}
// orgProviders is orgID's own providers, empty unless its policy allows them.
func (s *Server) orgProviders(ctx context.Context, orgID string, policy *orgModelPolicy) (map[string]config.Provider, error) {
out := map[string]config.Provider{}
@@ -0,0 +1,93 @@
package controlplane
import (
"context"
"maps"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// resourceStore answers one document per org and kind and reports every other
// key as absent, the shape a settings read sees on an instance that has never
// written it.
type resourceStore map[string]map[string]string
func (s resourceStore) Resource(_ context.Context, orgID, kind string) (storecontract.Resource, error) {
value, ok := s[orgID][kind]
if !ok {
return storecontract.Resource{}, storecontract.ErrResourceNotFound
}
return storecontract.Resource{OrgID: orgID, Kind: kind, Value: []byte(value), Version: 1}, nil
}
func (resourceStore) ListResources(context.Context) ([]storecontract.Resource, error) {
return nil, nil
}
func (resourceStore) ResourceHistory(context.Context, string, string, int) ([]storecontract.Resource, error) {
return nil, nil
}
func (resourceStore) Audit(context.Context, string, []string, int) ([]storecontract.AuditEntry, error) {
return nil, nil
}
func (resourceStore) PutResource(context.Context, storecontract.ResourceWrite) (storecontract.Resource, error) {
return storecontract.Resource{}, nil
}
// TestModelsForResolvesTheProvidersItsAliasesName pins what a task builds its
// model runtime from: an alias is a "provider/model" ref, so a provider the
// instance configured has to come back beside the alias that names it --
// otherwise the ref resolves to nothing and no runtime is ever built. The
// instance's credentials are part of the provider: without them every call is
// unauthenticated.
func TestModelsForResolvesTheProvidersItsAliasesName(t *testing.T) {
store := resourceStore{storecontract.DefaultOrgID: {
ProviderSettingsKind: `{
"remote": {"class": "openai-compatible", "base_url": "https://models.example.com/v1", "api_key_ref": {"engine": "vault", "key": "providers/remote"}},
"local": {"class": "ollama", "api_key_env": "LOCAL_KEY"}
}`,
ModelAliasesKind: `{"default": "remote/large", "embedding": "local/embed"}`,
}}
tests := []struct {
name string
orgID string
wantAliases []string
wantProviders []string
}{
{name: "the system org holds the instance's aliases and providers", orgID: storecontract.DefaultOrgID, wantAliases: []string{"default", "embedding"}, wantProviders: []string{"local", "remote"}},
{name: "no org named means the system org", orgID: "", wantAliases: []string{"default", "embedding"}, wantProviders: []string{"local", "remote"}},
{name: "an org with no policy takes neither the instance's aliases nor its providers", orgID: "acme"},
}
server := &Server{store: store}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
models, err := server.ModelsFor(t.Context(), tt.orgID)
if err != nil {
t.Fatalf("models for %q: %v", tt.orgID, err)
}
if got := slices.Sorted(maps.Keys(models.Aliases)); !slices.Equal(got, tt.wantAliases) {
t.Fatalf("aliases = %v, want %v", got, tt.wantAliases)
}
if got := slices.Sorted(maps.Keys(models.Providers)); !slices.Equal(got, tt.wantProviders) {
t.Fatalf("providers = %v, want %v", got, tt.wantProviders)
}
})
}
models, err := server.ModelsFor(t.Context(), storecontract.DefaultOrgID)
if err != nil {
t.Fatalf("models: %v", err)
}
want := config.Provider{Class: "openai-compatible", BaseURL: "https://models.example.com/v1", APIKey: config.SecretRef{Engine: "vault", Key: "providers/remote"}}
if got := models.Providers["remote"]; got != want {
t.Fatalf("remote provider = %+v, want %+v (the instance's api_key_ref is the only credential it has)", got, want)
}
if got := models.Providers["local"]; got.APIKeyEnv != "LOCAL_KEY" || got.Class != "ollama" {
t.Fatalf("local provider = %+v, want its class and api_key_env", got)
}
}