Merge pull request #1813 from samcharles93/fix/default-org-providers

fix(controlplane): resolve the default org's providers with its aliases
This commit is contained in:
sam authored and GitHub committed 2026-10-20 03:44:53 +11:00
commit d1b7a21b9b
4 files changed
+155 -4

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)
}
}
+6 -2
View File
@@ -67,9 +67,13 @@ type OrgModels struct {
}
// WithOrgModels is c for a run in models.Org: its aliases, and the instance
// providers with the org's own beside them.
// providers with the org's own beside them. An org that supplies no aliases
// keeps the ones c already carries, which for a task config outside the
// system org are empty -- so nothing here widens what the org may use.
func (c Config) WithOrgModels(models OrgModels) Config {
c.Models = models.Aliases
if len(models.Aliases) > 0 {
c.Models = models.Aliases
}
if len(models.Providers) > 0 {
providers := maps.Clone(c.Providers)
if providers == nil {
+29
View File
@@ -0,0 +1,29 @@
package config
import (
"maps"
"testing"
)
// TestWithOrgModelsKeepsAliasesTheOrgDoesNotOverride pins that an org's absent
// aliases are not an empty model set: a run keeps the aliases it already
// resolved, and an org that does define aliases replaces them outright.
func TestWithOrgModelsKeepsAliasesTheOrgDoesNotOverride(t *testing.T) {
instance := map[string]string{"default": "a/large", "embedding": "b/embed"}
tests := []struct {
name string
org OrgModels
want map[string]string
}{
{"an org with no aliases of its own keeps the instance's", OrgModels{}, instance},
{"an org's own aliases replace the instance's", OrgModels{Aliases: map[string]string{"fast": "c/small"}}, map[string]string{"fast": "c/small"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
models := Config{Models: instance}.WithOrgModels(tt.org).Models
if !maps.Equal(models, tt.want) {
t.Fatalf("models = %v, want %v", models, tt.want)
}
})
}
}