diff --git a/internal/api/handlers/management/model_definitions_test.go b/internal/api/handlers/management/model_definitions_test.go index 78646e8ca0..53f8b4f0a1 100644 --- a/internal/api/handlers/management/model_definitions_test.go +++ b/internal/api/handlers/management/model_definitions_test.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "slices" + "strings" "sync/atomic" "testing" @@ -274,3 +275,105 @@ func TestRefreshModelDefinitions_UpdatesCatalogAndReportsChangedProviders(t *tes t.Fatalf("status = %d, want %d; body = %s", recFail.Code, http.StatusBadGateway, recFail.Body.String()) } } + +type oauthCatalogPlugin struct { + provider string + models []pluginapi.ModelInfo +} + +func (p oauthCatalogPlugin) Identifier() string { return p.provider } + +func (p oauthCatalogPlugin) ParseAuth(context.Context, pluginapi.AuthParseRequest) (pluginapi.AuthParseResponse, error) { + return pluginapi.AuthParseResponse{}, nil +} + +func (p oauthCatalogPlugin) StartLogin(context.Context, pluginapi.AuthLoginStartRequest) (pluginapi.AuthLoginStartResponse, error) { + return pluginapi.AuthLoginStartResponse{}, nil +} + +func (p oauthCatalogPlugin) PollLogin(context.Context, pluginapi.AuthLoginPollRequest) (pluginapi.AuthLoginPollResponse, error) { + return pluginapi.AuthLoginPollResponse{}, nil +} + +func (p oauthCatalogPlugin) RefreshAuth(context.Context, pluginapi.AuthRefreshRequest) (pluginapi.AuthRefreshResponse, error) { + return pluginapi.AuthRefreshResponse{}, nil +} + +func (p oauthCatalogPlugin) StaticModels(context.Context, pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{Provider: p.provider, Models: p.models}, nil +} + +func (p oauthCatalogPlugin) ModelsForAuth(context.Context, pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{Provider: p.provider, Models: p.models}, nil +} + +func (p oauthCatalogPlugin) Execute(context.Context, pluginapi.ExecutorRequest) (pluginapi.ExecutorResponse, error) { + return pluginapi.ExecutorResponse{}, nil +} + +func (p oauthCatalogPlugin) ExecuteStream(context.Context, pluginapi.ExecutorRequest) (pluginapi.ExecutorStreamResponse, error) { + return pluginapi.ExecutorStreamResponse{}, nil +} + +func (p oauthCatalogPlugin) CountTokens(context.Context, pluginapi.ExecutorRequest) (pluginapi.ExecutorResponse, error) { + return pluginapi.ExecutorResponse{}, nil +} + +func (p oauthCatalogPlugin) HttpRequest(context.Context, pluginapi.ExecutorHTTPRequest) (pluginapi.ExecutorHTTPResponse, error) { + return pluginapi.ExecutorHTTPResponse{}, nil +} + +func TestGetStaticModelDefinitions_OAuthPluginCatalogDecoupledFromRegistry(t *testing.T) { + for _, tc := range []struct { + name string + scope pluginapi.ExecutorModelScope + }{ + {name: "unset scope with auth provider", scope: ""}, + {name: "explicit oauth scope", scope: pluginapi.ExecutorModelScopeOAuth}, + {name: "explicit both scope with auth provider", scope: pluginapi.ExecutorModelScopeBoth}, + } { + t.Run(tc.name, func(t *testing.T) { + provider := "demo-oauth-7512-" + strings.ReplaceAll(tc.name, " ", "-") + reg := registry.GetGlobalRegistry() + pluginID := provider + "-plugin" + staticClientID := "plugin:" + pluginID + ":" + provider + ":executor" + t.Cleanup(func() { + reg.UnregisterClient(staticClientID) + }) + + plugin := oauthCatalogPlugin{ + provider: provider, + models: []pluginapi.ModelInfo{ + {ID: "demo-a", DisplayName: "Demo A"}, + {ID: "demo-blocked", DisplayName: "Demo Blocked"}, + }, + } + host := pluginhost.New() + host.RegisterPluginForTest(pluginID, pluginapi.Plugin{ + Capabilities: pluginapi.Capabilities{ + AuthProvider: plugin, + ModelProvider: plugin, + Executor: plugin, + ExecutorModelScope: tc.scope, + }, + }) + host.RegisterModels(t.Context(), reg) + host.RegisterExecutors(coreauth.NewManager(nil, nil, nil), reg) + + if available := reg.GetAvailableModelsByProvider(provider); len(available) != 0 { + t.Fatalf("GetAvailableModelsByProvider(%q) = %#v, want empty without active credentials", provider, available) + } + + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, nil) + h.SetPluginHost(host) + + rec, body := invokeGetStaticModelDefinitions(t, h, provider) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d; body = %s", rec.Code, http.StatusOK, rec.Body.String()) + } + if len(body.Models) != 2 || body.Models[0].ID != "demo-a" || body.Models[1].ID != "demo-blocked" { + t.Fatalf("models = %#v, want [demo-a demo-blocked]", body.Models) + } + }) + } +} diff --git a/internal/pluginhost/adapters.go b/internal/pluginhost/adapters.go index eec740d232..b449f297e1 100644 --- a/internal/pluginhost/adapters.go +++ b/internal/pluginhost/adapters.go @@ -26,11 +26,13 @@ type modelProviderRegistry interface { } type pluginModelRegistration struct { - pluginID string - provider string - priority int - models []*registry.ModelInfo - hasExecutor bool + pluginID string + provider string + priority int + models []*registry.ModelInfo + hasExecutor bool + allowsStaticClient bool + hasModelProvider bool } func normalizedExecutorModelScope(caps pluginapi.Capabilities) pluginapi.ExecutorModelScope { @@ -41,6 +43,9 @@ func normalizedExecutorModelScope(caps pluginapi.Capabilities) pluginapi.Executo case pluginapi.ExecutorModelScopeStatic, pluginapi.ExecutorModelScopeOAuth, pluginapi.ExecutorModelScopeBoth: return caps.ExecutorModelScope default: + if hasAuthProviderCap(caps) { + return pluginapi.ExecutorModelScopeOAuth + } return pluginapi.ExecutorModelScopeBoth } } @@ -50,6 +55,9 @@ func executorScopeAllowsStaticModels(caps pluginapi.Capabilities) bool { return true } scope := normalizedExecutorModelScope(caps) + if hasAuthProviderCap(caps) { + return scope == pluginapi.ExecutorModelScopeStatic + } return scope == pluginapi.ExecutorModelScopeStatic || scope == pluginapi.ExecutorModelScopeBoth } @@ -256,9 +264,6 @@ func (h *Host) RegisterModels(ctx context.Context, modelRegistry modelRegistry) if modelProvider == nil && registrar == nil { continue } - if !executorScopeAllowsStaticModels(record.plugin.Capabilities) { - continue - } var resp pluginapi.ModelRegistrationResponse var errRegisterModels error if modelProvider != nil { @@ -313,11 +318,13 @@ func (h *Host) RegisterModels(ctx context.Context, modelRegistry modelRegistry) } regIndexByProvider[provider] = len(regsForPlugin) regsForPlugin = append(regsForPlugin, pluginModelRegistration{ - pluginID: record.id, - provider: provider, - priority: record.priority, - models: cloneRegistryModels(models), - hasExecutor: record.plugin.Capabilities.Executor != nil, + pluginID: record.id, + provider: provider, + priority: record.priority, + models: cloneRegistryModels(models), + hasExecutor: record.plugin.Capabilities.Executor != nil, + allowsStaticClient: record.plugin.Capabilities.Executor != nil && executorScopeAllowsStaticModels(record.plugin.Capabilities), + hasModelProvider: modelProvider != nil, }) } if len(regsForPlugin) == 0 { @@ -373,6 +380,17 @@ func (h *Host) authModelProviderMatches(record capabilityRecord, provider string return true } } + for _, prov := range recordAuthProviders(record) { + credIDs, okCredIDs := h.callAuthProviderCredentialProviders(record.id, prov) + if !okCredIDs { + return false + } + for _, credID := range credIDs { + if normalizeProviderID(credID) == provider { + return true + } + } + } } candidates := h.modelProvidersForPlugin(record.id) if len(candidates) > 0 { @@ -381,6 +399,11 @@ func (h *Host) authModelProviderMatches(record capabilityRecord, provider string return true } } + if caps.Executor != nil { + if candidate, okExec := h.executorProvider(record, caps.Executor); okExec && candidate == provider { + return true + } + } return false } if caps.Executor != nil { diff --git a/internal/pluginhost/adapters_executors.go b/internal/pluginhost/adapters_executors.go index 48771f958d..6ad6191081 100644 --- a/internal/pluginhost/adapters_executors.go +++ b/internal/pluginhost/adapters_executors.go @@ -43,6 +43,7 @@ func (h *Host) RegisterExecutors(manager executorManager, modelRegistry modelPro registrations := h.snapshotModelRegistrations() selectedModels := make(map[string][]*registry.ModelInfo) providerModels := make(map[string][]*registry.ModelInfo) + staticAppendProviderModels := make(map[string][]*registry.ModelInfo) claimedModels := make(map[string]struct{}) claimedProviders := make(map[string]string) for _, registration := range registrations { @@ -50,6 +51,7 @@ func (h *Host) RegisterExecutors(manager executorManager, modelRegistry modelPro continue } appendModelsForProvider(providerModels, registration.provider, registration.models) + appendModelsForProvider(staticAppendProviderModels, registration.provider, registration.models) // Reserve native catalog models before plugin arbitration, independently of // credential publication or exclusions. This preserves native priority without // publishing availability, which remains credential-bound. @@ -72,9 +74,13 @@ func (h *Host) RegisterExecutors(manager executorManager, modelRegistry modelPro if !okProvider { continue } + if !executorScopeAllowsStaticModels(record.plugin.Capabilities) { + continue + } registration := h.modelRegistration(record.id) if h.providerHasNativeExecutor(manager, provider) { appendModelsForProvider(providerModels, provider, registration.models) + appendModelsForProvider(staticAppendProviderModels, provider, registration.models) continue } if len(registration.models) == 0 { @@ -101,6 +107,7 @@ func (h *Host) RegisterExecutors(manager executorManager, modelRegistry modelPro } seenProviders := make(map[string]struct{}) + seenCatalogProviders := make(map[string]struct{}) nextProviders := make(map[string]struct{}) nextModelClients := make(map[string]struct{}) executorRegistrations := make([]executorRegistration, 0) @@ -111,61 +118,105 @@ func (h *Host) RegisterExecutors(manager executorManager, modelRegistry modelPro continue } - provider, okProvider := h.executorProvider(record, executor) - if !okProvider { - continue - } + allowsStatic := executorScopeAllowsStaticModels(record.plugin.Capabilities) registration := h.modelRegistration(record.id) - if len(registration.models) > 0 && len(selectedModels[record.id]) == 0 { - continue - } - if _, seenProvider := seenProviders[provider]; seenProvider { - continue - } - seenProviders[provider] = struct{}{} - if h.providerHasNativeExecutor(manager, provider) { + if allowsStatic && len(registration.models) > 0 && len(selectedModels[record.id]) == 0 { continue } - nextProviders[provider] = struct{}{} - executorRegistrations = append(executorRegistrations, newExecutorAdapterRegistration(h, record, provider, executor)) - appendModelsForProvider(providerModels, provider, selectedModels[record.id]) - if len(selectedModels[record.id]) > 0 { - clientID := pluginExecutorModelClientID(record.id, provider) - modelClientRegistrations = append(modelClientRegistrations, modelClientRegistration{ - clientID: clientID, - provider: provider, - models: selectedModels[record.id], - }) - nextModelClients[clientID] = struct{}{} + provider, okProvider := h.executorProvider(record, executor) + if h.isPluginFused(record.id) { + continue } - if executorScopeAllowsOAuthModels(record.plugin.Capabilities) && hasRecordAuthProvider(record) { - if authIDs, okAuthIDs := h.callRecordAuthProviderIdentifiers(record); okAuthIDs { - for _, authID := range authIDs { - if authID == "" || authID == provider { - continue + if okProvider { + if _, seenProvider := seenProviders[provider]; !seenProvider { + seenProviders[provider] = struct{}{} + if !h.providerHasNativeExecutor(manager, provider) { + nextProviders[provider] = struct{}{} + executorRegistrations = append(executorRegistrations, newExecutorAdapterRegistration(h, record, provider, executor)) + if allowsStatic { + appendModelsForProvider(providerModels, provider, selectedModels[record.id]) + appendModelsForProvider(staticAppendProviderModels, provider, selectedModels[record.id]) + if len(selectedModels[record.id]) > 0 { + clientID := pluginExecutorModelClientID(record.id, provider) + modelClientRegistrations = append(modelClientRegistrations, modelClientRegistration{ + clientID: clientID, + provider: provider, + models: selectedModels[record.id], + }) + nextModelClients[clientID] = struct{}{} + } } - if _, seenAuthProvider := seenProviders[authID]; seenAuthProvider { - continue - } - seenProviders[authID] = struct{}{} - if h.providerHasNativeExecutor(manager, authID) { - continue - } - nextProviders[authID] = struct{}{} - executorRegistrations = append(executorRegistrations, newExecutorAdapterRegistration(h, record, authID, executor)) } } } + if !allowsStatic { + for _, reg := range h.pluginRegistrations(record.id) { + if _, seenCatalog := seenCatalogProviders[reg.provider]; seenCatalog { + continue + } + seenCatalogProviders[reg.provider] = struct{}{} + appendModelsForProvider(providerModels, reg.provider, reg.models) + if !reg.hasModelProvider { + appendModelsForProvider(staticAppendProviderModels, reg.provider, reg.models) + } + } + } + for _, authID := range h.recordOAuthExecutorProviders(record) { + if authID == "" || authID == provider { + continue + } + if _, seenAuthProvider := seenProviders[authID]; seenAuthProvider { + continue + } + seenProviders[authID] = struct{}{} + if h.providerHasNativeExecutor(manager, authID) { + continue + } + nextProviders[authID] = struct{}{} + executorRegistrations = append(executorRegistrations, newExecutorAdapterRegistration(h, record, authID, executor)) + } } - h.commitExecutorState(snap, manager, modelRegistry, providerModels, executorRegistrations, nextProviders, modelClientRegistrations, nextModelClients) + h.commitExecutorState(snap, manager, modelRegistry, providerModels, staticAppendProviderModels, executorRegistrations, nextProviders, modelClientRegistrations, nextModelClients) +} + +func (h *Host) recordOAuthExecutorProviders(record capabilityRecord) []string { + if h == nil || !executorScopeAllowsOAuthModels(record.plugin.Capabilities) || !hasRecordAuthProvider(record) { + return nil + } + seen := make(map[string]struct{}) + var out []string + appendProvider := func(raw string) { + id := normalizeProviderID(raw) + if id == "" { + return + } + if _, exists := seen[id]; exists { + return + } + seen[id] = struct{}{} + out = append(out, id) + } + if authIDs, okAuthIDs := h.callRecordAuthProviderIdentifiers(record); okAuthIDs { + for _, authID := range authIDs { + appendProvider(authID) + } + } + for _, prov := range recordAuthProviders(record) { + if credIDs, okCredIDs := h.callAuthProviderCredentialProviders(record.id, prov); okCredIDs { + for _, credID := range credIDs { + appendProvider(credID) + } + } + } + return out } func pluginExecutorModelClientID(pluginID, provider string) string { return "plugin:" + pluginID + ":" + provider + ":executor" } -func (h *Host) commitExecutorState(snap *Snapshot, manager executorManager, modelRegistry modelRegistry, providerModels map[string][]*registry.ModelInfo, registrations []executorRegistration, nextProviders map[string]struct{}, modelClientRegistrations []modelClientRegistration, nextModelClients map[string]struct{}) { +func (h *Host) commitExecutorState(snap *Snapshot, manager executorManager, modelRegistry modelRegistry, providerModels, staticAppendProviderModels map[string][]*registry.ModelInfo, registrations []executorRegistration, nextProviders map[string]struct{}, modelClientRegistrations []modelClientRegistration, nextModelClients map[string]struct{}) { if h == nil || manager == nil { return } @@ -180,6 +231,10 @@ func (h *Host) commitExecutorState(snap *Snapshot, manager executorManager, mode for provider, models := range providerModels { h.providerModels[provider] = cloneRegistryModels(models) } + h.staticAppendProviderModels = make(map[string][]*registryModelInfo, len(staticAppendProviderModels)) + for provider, models := range staticAppendProviderModels { + h.staticAppendProviderModels[provider] = cloneRegistryModels(models) + } staleProviders := make([]string, 0) for provider := range h.executorProviders { @@ -315,11 +370,23 @@ func (h *Host) modelRegistration(pluginID string) pluginModelRegistration { return registration } +func (h *Host) pluginRegistrations(pluginID string) []pluginModelRegistration { + if h == nil { + return nil + } + h.mu.Lock() + defer h.mu.Unlock() + return h.pluginRegistrationsLocked(pluginID) +} + func (h *Host) executorProvider(record capabilityRecord, executor pluginapi.ProviderExecutor) (string, bool) { if h == nil || !h.recordCurrent(record) { return "", false } - provider := h.modelProvider(record.id) + var provider string + if executorScopeAllowsStaticModels(record.plugin.Capabilities) { + provider = h.modelProvider(record.id) + } if provider == "" { identifier, okIdentifier := h.callExecutorIdentifier(record.id, executor) if !okIdentifier { @@ -327,6 +394,9 @@ func (h *Host) executorProvider(record capabilityRecord, executor pluginapi.Prov } provider = identifier } + if provider == "" { + provider = h.modelProvider(record.id) + } provider = strings.ToLower(strings.TrimSpace(provider)) return provider, provider != "" } @@ -405,6 +475,23 @@ func (h *Host) ModelsForProvider(provider string) []*registry.ModelInfo { return cloneRegistryModels(h.providerModels[provider]) } +// StaticAppendModelsForProvider returns static plugin models eligible to be +// appended to credential registrations when per-auth ModelProvider did not handle them. +// OAuth-scoped ModelProvider executors keep their StaticModels catalog-only so +// unfiltered or unaliased static models are never appended onto credentials. +func (h *Host) StaticAppendModelsForProvider(provider string) []*registry.ModelInfo { + if h == nil { + return nil + } + provider = strings.ToLower(strings.TrimSpace(provider)) + if provider == "" { + return nil + } + h.mu.Lock() + defer h.mu.Unlock() + return cloneRegistryModels(h.staticAppendProviderModels[provider]) +} + func (h *Host) HasExecutorCandidateProvider(provider string) bool { if h == nil { return false @@ -422,13 +509,9 @@ func (h *Host) HasExecutorCandidateProvider(provider string) bool { if okCandidate && candidate == provider { return true } - if executorScopeAllowsOAuthModels(record.plugin.Capabilities) && hasRecordAuthProvider(record) { - if authIDs, okAuthIDs := h.callRecordAuthProviderIdentifiers(record); okAuthIDs { - for _, authID := range authIDs { - if authID == provider { - return true - } - } + for _, authID := range h.recordOAuthExecutorProviders(record) { + if authID == provider { + return true } } } @@ -622,9 +705,11 @@ func (a *executorAdapter) lookupPluginModelInfo(baseModel string) *registry.Mode return model } } - for _, model := range a.host.modelRegistration(a.pluginID).models { - if model != nil && strings.EqualFold(strings.TrimSpace(model.ID), baseModel) { - return model + for _, reg := range a.host.pluginRegistrations(a.pluginID) { + for _, model := range reg.models { + if model != nil && strings.EqualFold(strings.TrimSpace(model.ID), baseModel) { + return model + } } } return nil diff --git a/internal/pluginhost/adapters_test.go b/internal/pluginhost/adapters_test.go index 9a80469391..7ac57c6393 100644 --- a/internal/pluginhost/adapters_test.go +++ b/internal/pluginhost/adapters_test.go @@ -668,8 +668,8 @@ func TestRegisterExecutorsOAuthScopeSkipsStaticModelClientButRegistersExecutor(t host.RegisterModels(context.Background(), modelRegistry) host.RegisterExecutors(manager, modelRegistry) - if staticCalled { - t.Fatal("StaticModels was called for an OAuth-only executor") + if !staticCalled { + t.Fatal("StaticModels was not called to populate catalog metadata for OAuth executor") } if _, okExecutor := manager.executors["sample-provider"]; !okExecutor { t.Fatal("OAuth-only executor was not registered") @@ -677,8 +677,11 @@ func TestRegisterExecutorsOAuthScopeSkipsStaticModelClientButRegistersExecutor(t if _, okClient := modelRegistry.clients[pluginExecutorModelClientID("sample-provider", "sample-provider")]; okClient { t.Fatal("OAuth-only executor registered a static model client") } - if got := host.ModelsForProvider("sample-provider"); len(got) != 0 { - t.Fatalf("OAuth-only provider models = %#v, want none", got) + if got := host.ModelsForProvider("sample-provider"); len(got) != 1 || got[0].ID != "static-model" { + t.Fatalf("OAuth-only provider catalog models = %#v, want [static-model]", got) + } + if got := host.StaticAppendModelsForProvider("sample-provider"); len(got) != 0 { + t.Fatalf("OAuth-only StaticAppendModelsForProvider = %#v, want none", got) } result := host.ModelsForAuth(context.Background(), &coreauth.Auth{ @@ -766,45 +769,377 @@ func TestRegisterExecutorsStaticScopeSkipsModelsForAuth(t *testing.T) { } func TestRegisterExecutorsBothScopeKeepsStaticAndOAuthModels(t *testing.T) { + t.Run("without auth provider keeps static model client and oauth models", func(t *testing.T) { + modelRegistry := newFakeModelRegistry() + manager := newFakeExecutorManager() + host := newHostWithRecords(capabilityRecord{ + id: "alpha", + plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + ModelProvider: modelProviderFunc{ + staticModels: func(ctx context.Context, req pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "plugin-provider", + Models: []pluginapi.ModelInfo{{ID: "static-model"}}, + }, nil + }, + modelsForAuth: func(ctx context.Context, req pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "plugin-provider", + Models: []pluginapi.ModelInfo{{ID: "oauth-model"}}, + }, nil + }, + }, + Executor: &fakeExecutor{identifier: "plugin-provider"}, + ExecutorModelScope: pluginapi.ExecutorModelScopeBoth, + }}, + }) + + host.RegisterModels(context.Background(), modelRegistry) + host.RegisterExecutors(manager, modelRegistry) + + clientID := pluginExecutorModelClientID("alpha", "plugin-provider") + reg := modelRegistry.clients[clientID] + if reg == nil || reg.provider != "plugin-provider" || len(reg.models) != 1 || reg.models[0].ID != "static-model" { + t.Fatalf("both-scope static model client = %#v, want static-model", reg) + } + result := host.ModelsForAuth(context.Background(), &coreauth.Auth{ + ID: "plugin-auth", + Provider: "plugin-provider", + }) + if !result.Handled || result.Provider != "plugin-provider" || len(result.Models) != 1 || result.Models[0].ID != "oauth-model" { + t.Fatalf("both-scope OAuth model result = %#v, want oauth-model", result) + } + }) + + t.Run("with auth provider skips static model client and keeps catalog and oauth models", func(t *testing.T) { + modelRegistry := newFakeModelRegistry() + manager := newFakeExecutorManager() + host := newHostWithRecords(capabilityRecord{ + id: "alpha", + plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + AuthProvider: fakeAuthProvider{identifier: "plugin-provider"}, + ModelProvider: modelProviderFunc{ + staticModels: func(ctx context.Context, req pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "plugin-provider", + Models: []pluginapi.ModelInfo{{ID: "static-model"}}, + }, nil + }, + modelsForAuth: func(ctx context.Context, req pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "plugin-provider", + Models: []pluginapi.ModelInfo{{ID: "oauth-model"}}, + }, nil + }, + }, + Executor: &fakeExecutor{identifier: "plugin-provider"}, + ExecutorModelScope: pluginapi.ExecutorModelScopeBoth, + }}, + }) + + host.RegisterModels(context.Background(), modelRegistry) + host.RegisterExecutors(manager, modelRegistry) + + clientID := pluginExecutorModelClientID("alpha", "plugin-provider") + if _, okClient := modelRegistry.clients[clientID]; okClient { + t.Fatal("credential-bound both-scope executor registered a static model client") + } + if _, okExecutor := manager.executors["plugin-provider"]; !okExecutor { + t.Fatal("credential-bound both-scope executor was not registered") + } + if host.executorPluginReady("alpha", pluginapi.ModelRouteRequest{RequestedModel: "static-model", SourceFormat: "openai"}) { + t.Fatal("executorPluginReady returned true for credential-bound both-scope plugin") + } + if catalog := host.ModelsForProvider("plugin-provider"); len(catalog) != 1 || catalog[0].ID != "static-model" { + t.Fatalf("ModelsForProvider = %#v, want [static-model]", catalog) + } + result := host.ModelsForAuth(context.Background(), &coreauth.Auth{ + ID: "plugin-auth", + Provider: "plugin-provider", + }) + if !result.Handled || result.Provider != "plugin-provider" || len(result.Models) != 1 || result.Models[0].ID != "oauth-model" { + t.Fatalf("both-scope OAuth model result = %#v, want oauth-model", result) + } + }) +} + +func TestRegisterExecutorsAuthProviderUnsetScopeSkipsStaticModelClientAndPreservesCatalog(t *testing.T) { + modelRegistry := newFakeModelRegistry() + modelRegistry.RegisterClient("native-client", "claude", []*registry.ModelInfo{{ID: "native-overlap-model"}}) + manager := newFakeExecutorManager() + manager.RegisterExecutor(&fakeProviderExecutor{provider: "claude"}) + + plugin := pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + AuthProvider: fakeAuthProvider{identifier: "demo"}, + ModelProvider: modelProviderFunc{ + staticModels: func(ctx context.Context, req pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "demo", + Models: []pluginapi.ModelInfo{ + {ID: "demo-a"}, + {ID: "demo-blocked"}, + {ID: "native-overlap-model"}, + }, + }, nil + }, + modelsForAuth: func(ctx context.Context, req pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "demo", + Models: []pluginapi.ModelInfo{ + {ID: "demo-a"}, + {ID: "demo-blocked"}, + }, + }, nil + }, + }, + Executor: &fakeExecutor{identifier: "demo"}, + }} + host := newHostWithRecords(capabilityRecord{id: "demo-oauth-plugin", plugin: plugin}) + + if rpcCaps := rpcCapabilitiesFromPlugin(plugin); rpcCaps.ExecutorModelScope != pluginapi.ExecutorModelScopeOAuth { + t.Fatalf("rpcCapabilitiesFromPlugin ExecutorModelScope = %q, want %q", rpcCaps.ExecutorModelScope, pluginapi.ExecutorModelScopeOAuth) + } + + host.RegisterModels(context.Background(), modelRegistry) + host.RegisterExecutors(manager, modelRegistry) + + if _, okClient := modelRegistry.clients[pluginExecutorModelClientID("demo-oauth-plugin", "demo")]; okClient { + t.Fatal("credential-bound plugin with unset ExecutorModelScope leaked synthetic static model client into ModelRegistry") + } + if _, okExecutor := manager.executors["demo"]; !okExecutor { + t.Fatal("credential-bound plugin executor was not registered") + } + if got := host.ModelsForProvider("demo"); len(got) != 3 { + t.Fatalf("ModelsForProvider(demo) = %#v, want 3 static catalog models", got) + } + if got := host.StaticAppendModelsForProvider("demo"); len(got) != 0 { + t.Fatalf("StaticAppendModelsForProvider(demo) = %#v, want none", got) + } + if host.executorPluginReady("demo-oauth-plugin", pluginapi.ModelRouteRequest{RequestedModel: "demo-a", SourceFormat: "openai"}) { + t.Fatal("executorPluginReady returned true for credential-bound plugin with unset ExecutorModelScope") + } + result := host.ModelsForAuth(context.Background(), &coreauth.Auth{ID: "demo-auth", Provider: "demo"}) + if !result.Handled || len(result.Models) != 2 { + t.Fatalf("ModelsForAuth(demo) = %#v, want 2 models", result) + } +} + +func TestRegisterExecutorsAuthProviderCredentialProviderMatchesModelsForAuthWithStaticCatalog(t *testing.T) { modelRegistry := newFakeModelRegistry() manager := newFakeExecutorManager() + host := newHostWithRecords(capabilityRecord{ - id: "alpha", + id: "multi-portal-oauth", plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ - AuthProvider: fakeAuthProvider{identifier: "plugin-provider"}, + AuthProvider: fakeAuthProvider{ + identifier: "portal-a", + credentialProviders: []string{"cred-a"}, + }, ModelProvider: modelProviderFunc{ staticModels: func(ctx context.Context, req pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { return pluginapi.ModelResponse{ - Provider: "plugin-provider", - Models: []pluginapi.ModelInfo{{ID: "static-model"}}, + Provider: "portal-a", + Models: []pluginapi.ModelInfo{{ID: "portal-model"}}, }, nil }, modelsForAuth: func(ctx context.Context, req pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { return pluginapi.ModelResponse{ - Provider: "plugin-provider", - Models: []pluginapi.ModelInfo{{ID: "oauth-model"}}, + Provider: "cred-a", + Models: []pluginapi.ModelInfo{{ID: "cred-model"}}, }, nil }, }, - Executor: &fakeExecutor{identifier: "plugin-provider"}, - ExecutorModelScope: pluginapi.ExecutorModelScopeBoth, + Executor: &fakeExecutor{identifier: "cred-a"}, + ExecutorModelScope: pluginapi.ExecutorModelScopeOAuth, }}, }) host.RegisterModels(context.Background(), modelRegistry) host.RegisterExecutors(manager, modelRegistry) - clientID := pluginExecutorModelClientID("alpha", "plugin-provider") - reg := modelRegistry.clients[clientID] - if reg == nil || reg.provider != "plugin-provider" || len(reg.models) != 1 || reg.models[0].ID != "static-model" { - t.Fatalf("both-scope static model client = %#v, want static-model", reg) + if _, okExec := manager.executors["cred-a"]; !okExec { + t.Fatal("executor for credential provider cred-a was not registered") } - result := host.ModelsForAuth(context.Background(), &coreauth.Auth{ - ID: "plugin-auth", - Provider: "plugin-provider", + if _, okPortalExec := manager.executors["portal-a"]; !okPortalExec { + t.Fatal("executor for auth portal portal-a was not registered") + } + if !host.HasExecutorCandidateProvider("cred-a") || !host.HasExecutorCandidateProvider("portal-a") { + t.Fatal("HasExecutorCandidateProvider failed for cred-a or portal-a") + } + if !host.HasAuthModelProvider("cred-a") { + t.Fatal("HasAuthModelProvider(cred-a) = false, want true") + } + result := host.ModelsForAuth(context.Background(), &coreauth.Auth{ID: "cred-auth", Provider: "cred-a"}) + if !result.Handled || result.Provider != "cred-a" || len(result.Models) != 1 || result.Models[0].ID != "cred-model" { + t.Fatalf("ModelsForAuth(cred-a) = %#v, want cred-model", result) + } +} + +func TestShutdownAllContextClearsStaticAppendProviderModels(t *testing.T) { + modelRegistry := newFakeModelRegistry() + manager := newFakeExecutorManager() + host := newHostWithRecords(capabilityRecord{ + id: "catalog-only", + plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + ModelRegistrar: staticModelRegistrar("kimi", "catalog-model"), + }}, }) - if !result.Handled || result.Provider != "plugin-provider" || len(result.Models) != 1 || result.Models[0].ID != "oauth-model" { - t.Fatalf("both-scope OAuth model result = %#v, want oauth-model", result) + + host.RegisterModels(context.Background(), modelRegistry) + host.RegisterExecutors(manager, modelRegistry) + + if got := host.ModelsForProvider("kimi"); len(got) != 1 || got[0].ID != "catalog-model" { + t.Fatalf("ModelsForProvider(kimi) = %#v, want [catalog-model]", got) + } + if got := host.StaticAppendModelsForProvider("kimi"); len(got) != 1 || got[0].ID != "catalog-model" { + t.Fatalf("StaticAppendModelsForProvider(kimi) = %#v, want [catalog-model]", got) + } + + host.ShutdownAllContext(context.Background()) + + if got := host.ModelsForProvider("kimi"); len(got) != 0 { + t.Fatalf("ModelsForProvider(kimi) after shutdown = %#v, want empty", got) + } + if got := host.StaticAppendModelsForProvider("kimi"); len(got) != 0 { + t.Fatalf("StaticAppendModelsForProvider(kimi) after shutdown = %#v, want empty", got) + } +} + +func TestModelsForAuthOAuthScopeWithoutAuthProviderMatchesExecutorIdentifierWithStaticCatalog(t *testing.T) { + modelRegistry := newFakeModelRegistry() + manager := newFakeExecutorManager() + + host := newHostWithRecords(capabilityRecord{ + id: "oauth-no-auth-provider", + plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + ModelProvider: modelProviderFunc{ + staticModels: func(ctx context.Context, req pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "catalog-a", + Models: []pluginapi.ModelInfo{{ID: "catalog-model"}}, + }, nil + }, + modelsForAuth: func(ctx context.Context, req pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "cred-a", + Models: []pluginapi.ModelInfo{{ID: "cred-model"}}, + }, nil + }, + }, + Executor: &fakeExecutor{identifier: "cred-a"}, + ExecutorModelScope: pluginapi.ExecutorModelScopeOAuth, + }}, + }) + + host.RegisterModels(context.Background(), modelRegistry) + host.RegisterExecutors(manager, modelRegistry) + + if _, okExec := manager.executors["cred-a"]; !okExec { + t.Fatal("OAuth-only executor cred-a was not registered") + } + if !host.HasAuthModelProvider("cred-a") { + t.Fatal("HasAuthModelProvider(cred-a) = false, want true") + } + result := host.ModelsForAuth(context.Background(), &coreauth.Auth{ID: "cred-auth", Provider: "cred-a"}) + if !result.Handled || result.Provider != "cred-a" || len(result.Models) != 1 || result.Models[0].ID != "cred-model" { + t.Fatalf("ModelsForAuth(cred-a) = %#v, want cred-model", result) + } + if catalog := host.ModelsForProvider("catalog-a"); len(catalog) != 1 || catalog[0].ID != "catalog-model" { + t.Fatalf("ModelsForProvider(catalog-a) = %#v, want [catalog-model]", catalog) + } +} + +func TestRegisterExecutorsNativePrimaryProviderStillRegistersOAuthAuthProviders(t *testing.T) { + modelRegistry := newFakeModelRegistry() + manager := newFakeExecutorManager() + nativeClaude := &fakeProviderExecutor{provider: "claude"} + manager.RegisterExecutor(nativeClaude) + + host := newHostWithRecords(capabilityRecord{ + id: "oauth-native-primary-conflict", + plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + AuthProvider: fakeAuthProvider{ + identifier: "portal-a", + credentialProviders: []string{"cred-a"}, + }, + ModelProvider: modelProviderFunc{ + staticModels: func(ctx context.Context, req pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "catalog-a", + Models: []pluginapi.ModelInfo{{ID: "catalog-model"}}, + }, nil + }, + modelsForAuth: func(ctx context.Context, req pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "cred-a", + Models: []pluginapi.ModelInfo{{ID: "cred-model"}}, + }, nil + }, + }, + Executor: &fakeExecutor{identifier: "claude"}, + }}, + }) + + host.RegisterModels(context.Background(), modelRegistry) + host.RegisterExecutors(manager, modelRegistry) + + if manager.executors["claude"] != nativeClaude { + t.Fatalf("native claude executor was overwritten: %#v", manager.executors["claude"]) + } + if _, okPortal := manager.executors["portal-a"]; !okPortal { + t.Fatal("auth portal executor portal-a was skipped when primary provider conflicted with native claude") + } + if _, okCred := manager.executors["cred-a"]; !okCred { + t.Fatal("credential provider executor cred-a was skipped when primary provider conflicted with native claude") + } + if catalog := host.ModelsForProvider("catalog-a"); len(catalog) != 1 || catalog[0].ID != "catalog-model" { + t.Fatalf("ModelsForProvider(catalog-a) = %#v, want [catalog-model]", catalog) + } +} + +func TestRegisterExecutorsOAuthScopePreservesStaticCatalogAndRegistersExecutorWithNativeOverlap(t *testing.T) { + modelRegistry := newFakeModelRegistry() + modelRegistry.RegisterClient("native-client", "claude", []*registry.ModelInfo{{ID: "native-overlap-model"}}) + manager := newFakeExecutorManager() + manager.RegisterExecutor(&fakeProviderExecutor{provider: "claude"}) + + host := newHostWithRecords(capabilityRecord{ + id: "oauth-catalog-plugin", + plugin: pluginapi.Plugin{Capabilities: pluginapi.Capabilities{ + AuthProvider: fakeAuthProvider{identifier: "demo-oauth"}, + ModelProvider: modelProviderFunc{ + staticModels: func(ctx context.Context, req pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "demo-oauth", + Models: []pluginapi.ModelInfo{ + {ID: "native-overlap-model"}, + }, + }, nil + }, + modelsForAuth: func(ctx context.Context, req pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{ + Provider: "demo-oauth", + Models: []pluginapi.ModelInfo{{ID: "demo-oauth-model"}}, + }, nil + }, + }, + Executor: &fakeExecutor{identifier: "demo-oauth"}, + ExecutorModelScope: pluginapi.ExecutorModelScopeOAuth, + }}, + }) + + host.RegisterModels(context.Background(), modelRegistry) + host.RegisterExecutors(manager, modelRegistry) + + if _, okClient := modelRegistry.clients[pluginExecutorModelClientID("oauth-catalog-plugin", "demo-oauth")]; okClient { + t.Fatal("OAuth-scoped executor leaked synthetic static model client into ModelRegistry") + } + if _, okExecutor := manager.executors["demo-oauth"]; !okExecutor { + t.Fatal("OAuth-scoped executor was suppressed due to native catalog overlap") + } + got := host.ModelsForProvider("demo-oauth") + if len(got) != 1 || got[0].ID != "native-overlap-model" { + t.Fatalf("ModelsForProvider(demo-oauth) = %#v, want [native-overlap-model]", got) } } diff --git a/internal/pluginhost/host.go b/internal/pluginhost/host.go index f95b69b5e6..87b332f396 100644 --- a/internal/pluginhost/host.go +++ b/internal/pluginhost/host.go @@ -62,42 +62,43 @@ type pluginLoadResult struct { } type Host struct { - applyMu chan struct{} - mu sync.Mutex - loader pluginLoader - loaded map[string]*loadedPlugin - retired map[string][]*loadedPlugin - loading map[string]*pluginLoadRequest - fused map[string]string - pluginFileVersions map[string]string - activePluginVersions map[string]string - activePluginPaths map[string]string - cleanupFilesPending bool - runtimeConfig *config.Config - authManager *coreauth.Manager - modelExecutor modelExecutor - modelClientIDs map[string]struct{} // Reserved for future use; catalog-only plugins do not populate it. - executorModelClientIDs map[string]struct{} - modelProviders map[string]string - modelProviderSets map[string][]string - authCredentialProviders map[string]map[string]string - modelRegistrations map[string]pluginModelRegistration - pluginModelRegistrations map[string][]pluginModelRegistration - providerModels map[string][]*registryModelInfo - executorProviders map[string]struct{} - accessProviderKeys map[string]struct{} - commandLineFlags map[string]commandLineFlagRecord - commandLineHits map[string]struct{} - managementRoutes map[string]managementRouteRecord - resourceRoutes map[string]resourceRouteRecord - streams *streamBridge - httpStreams *hostHTTPStreamBridge - httpOperations *hostHTTPOperationBridge - modelStreams *modelStreamBridge - callbackContexts *callbackContextRegistry - usageStates map[string]*pluginUsageState - maxPendingUsageRecords int - snapshot atomic.Value + applyMu chan struct{} + mu sync.Mutex + loader pluginLoader + loaded map[string]*loadedPlugin + retired map[string][]*loadedPlugin + loading map[string]*pluginLoadRequest + fused map[string]string + pluginFileVersions map[string]string + activePluginVersions map[string]string + activePluginPaths map[string]string + cleanupFilesPending bool + runtimeConfig *config.Config + authManager *coreauth.Manager + modelExecutor modelExecutor + modelClientIDs map[string]struct{} // Reserved for future use; catalog-only plugins do not populate it. + executorModelClientIDs map[string]struct{} + modelProviders map[string]string + modelProviderSets map[string][]string + authCredentialProviders map[string]map[string]string + modelRegistrations map[string]pluginModelRegistration + pluginModelRegistrations map[string][]pluginModelRegistration + providerModels map[string][]*registryModelInfo + staticAppendProviderModels map[string][]*registryModelInfo + executorProviders map[string]struct{} + accessProviderKeys map[string]struct{} + commandLineFlags map[string]commandLineFlagRecord + commandLineHits map[string]struct{} + managementRoutes map[string]managementRouteRecord + resourceRoutes map[string]resourceRouteRecord + streams *streamBridge + httpStreams *hostHTTPStreamBridge + httpOperations *hostHTTPOperationBridge + modelStreams *modelStreamBridge + callbackContexts *callbackContextRegistry + usageStates map[string]*pluginUsageState + maxPendingUsageRecords int + snapshot atomic.Value // loadCleanupDoneHook, set by tests, runs after a late load cleanup dropped its load token. loadCleanupDoneHook func() // usageDrainWaitHook, set by tests, runs when beginUsageReplacement waits for admitted usage calls to drain. @@ -108,36 +109,37 @@ type Host struct { func New() *Host { h := &Host{ - applyMu: make(chan struct{}, 1), - loader: defaultPluginLoader(), - loaded: make(map[string]*loadedPlugin), - retired: make(map[string][]*loadedPlugin), - loading: make(map[string]*pluginLoadRequest), - fused: make(map[string]string), - pluginFileVersions: make(map[string]string), - activePluginVersions: make(map[string]string), - activePluginPaths: make(map[string]string), - cleanupFilesPending: true, - modelClientIDs: make(map[string]struct{}), - executorModelClientIDs: make(map[string]struct{}), - modelProviders: make(map[string]string), - modelProviderSets: make(map[string][]string), - authCredentialProviders: make(map[string]map[string]string), - modelRegistrations: make(map[string]pluginModelRegistration), - pluginModelRegistrations: make(map[string][]pluginModelRegistration), - providerModels: make(map[string][]*registryModelInfo), - executorProviders: make(map[string]struct{}), - accessProviderKeys: make(map[string]struct{}), - commandLineFlags: make(map[string]commandLineFlagRecord), - commandLineHits: make(map[string]struct{}), - managementRoutes: make(map[string]managementRouteRecord), - resourceRoutes: make(map[string]resourceRouteRecord), - streams: newStreamBridge(), - httpStreams: newHostHTTPStreamBridge(), - httpOperations: newHostHTTPOperationBridge(), - modelStreams: newModelStreamBridge(), - callbackContexts: newCallbackContextRegistry(), - usageStates: make(map[string]*pluginUsageState), + applyMu: make(chan struct{}, 1), + loader: defaultPluginLoader(), + loaded: make(map[string]*loadedPlugin), + retired: make(map[string][]*loadedPlugin), + loading: make(map[string]*pluginLoadRequest), + fused: make(map[string]string), + pluginFileVersions: make(map[string]string), + activePluginVersions: make(map[string]string), + activePluginPaths: make(map[string]string), + cleanupFilesPending: true, + modelClientIDs: make(map[string]struct{}), + executorModelClientIDs: make(map[string]struct{}), + modelProviders: make(map[string]string), + modelProviderSets: make(map[string][]string), + authCredentialProviders: make(map[string]map[string]string), + modelRegistrations: make(map[string]pluginModelRegistration), + pluginModelRegistrations: make(map[string][]pluginModelRegistration), + providerModels: make(map[string][]*registryModelInfo), + staticAppendProviderModels: make(map[string][]*registryModelInfo), + executorProviders: make(map[string]struct{}), + accessProviderKeys: make(map[string]struct{}), + commandLineFlags: make(map[string]commandLineFlagRecord), + commandLineHits: make(map[string]struct{}), + managementRoutes: make(map[string]managementRouteRecord), + resourceRoutes: make(map[string]resourceRouteRecord), + streams: newStreamBridge(), + httpStreams: newHostHTTPStreamBridge(), + httpOperations: newHostHTTPOperationBridge(), + modelStreams: newModelStreamBridge(), + callbackContexts: newCallbackContextRegistry(), + usageStates: make(map[string]*pluginUsageState), } h.snapshot.Store(emptySnapshot()) return h @@ -901,6 +903,7 @@ func (h *Host) ShutdownAllContext(ctx context.Context) { h.modelRegistrations = make(map[string]pluginModelRegistration) h.pluginModelRegistrations = make(map[string][]pluginModelRegistration) h.providerModels = make(map[string][]*registryModelInfo) + h.staticAppendProviderModels = make(map[string][]*registryModelInfo) h.executorProviders = make(map[string]struct{}) h.commandLineFlags = make(map[string]commandLineFlagRecord) h.commandLineHits = make(map[string]struct{}) @@ -1081,14 +1084,24 @@ func (h *Host) removePluginRuntimeStateLocked(id string) { delete(h.pluginModelRegistrations, id) for provider := range affectedProviders { delete(h.providerModels, provider) + delete(h.staticAppendProviderModels, provider) } if len(affectedProviders) > 0 { for _, registration := range h.snapshotModelRegistrationsLocked() { - if registration.hasExecutor { + if registration.hasExecutor && registration.allowsStaticClient { continue } if _, ok := affectedProviders[registration.provider]; ok { + if h.providerModels == nil { + h.providerModels = make(map[string][]*registryModelInfo) + } appendModelsForProvider(h.providerModels, registration.provider, registration.models) + if !registration.hasExecutor || !registration.hasModelProvider { + if h.staticAppendProviderModels == nil { + h.staticAppendProviderModels = make(map[string][]*registryModelInfo) + } + appendModelsForProvider(h.staticAppendProviderModels, registration.provider, registration.models) + } } } } diff --git a/sdk/cliproxy/model_registration_issue7512_test.go b/sdk/cliproxy/model_registration_issue7512_test.go new file mode 100644 index 0000000000..a472eaf055 --- /dev/null +++ b/sdk/cliproxy/model_registration_issue7512_test.go @@ -0,0 +1,182 @@ +package cliproxy + +import ( + "context" + "strings" + "testing" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/pluginhost" + "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" + coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" + "github.com/router-for-me/CLIProxyAPI/v8/sdk/config" + "github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi" +) + +type issue7512Plugin struct { + provider string + models []pluginapi.ModelInfo +} + +func (p issue7512Plugin) Identifier() string { return p.provider } + +func (p issue7512Plugin) ParseAuth(context.Context, pluginapi.AuthParseRequest) (pluginapi.AuthParseResponse, error) { + return pluginapi.AuthParseResponse{}, nil +} + +func (p issue7512Plugin) StartLogin(context.Context, pluginapi.AuthLoginStartRequest) (pluginapi.AuthLoginStartResponse, error) { + return pluginapi.AuthLoginStartResponse{}, nil +} + +func (p issue7512Plugin) PollLogin(context.Context, pluginapi.AuthLoginPollRequest) (pluginapi.AuthLoginPollResponse, error) { + return pluginapi.AuthLoginPollResponse{}, nil +} + +func (p issue7512Plugin) RefreshAuth(context.Context, pluginapi.AuthRefreshRequest) (pluginapi.AuthRefreshResponse, error) { + return pluginapi.AuthRefreshResponse{}, nil +} + +func (p issue7512Plugin) StaticModels(context.Context, pluginapi.StaticModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{Provider: p.provider, Models: p.models}, nil +} + +func (p issue7512Plugin) ModelsForAuth(context.Context, pluginapi.AuthModelRequest) (pluginapi.ModelResponse, error) { + return pluginapi.ModelResponse{Provider: p.provider, Models: p.models}, nil +} + +func (p issue7512Plugin) Execute(context.Context, pluginapi.ExecutorRequest) (pluginapi.ExecutorResponse, error) { + return pluginapi.ExecutorResponse{}, nil +} + +func (p issue7512Plugin) ExecuteStream(context.Context, pluginapi.ExecutorRequest) (pluginapi.ExecutorStreamResponse, error) { + return pluginapi.ExecutorStreamResponse{}, nil +} + +func (p issue7512Plugin) CountTokens(context.Context, pluginapi.ExecutorRequest) (pluginapi.ExecutorResponse, error) { + return pluginapi.ExecutorResponse{}, nil +} + +func (p issue7512Plugin) HttpRequest(context.Context, pluginapi.ExecutorHTTPRequest) (pluginapi.ExecutorHTTPResponse, error) { + return pluginapi.ExecutorHTTPResponse{}, nil +} + +func TestIssue7512_AuthPluginEnforcesExclusionsAliasesDisabledAndKeepsCatalog(t *testing.T) { + for _, tc := range []struct { + name string + scope pluginapi.ExecutorModelScope + }{ + {name: "unset scope with auth provider", scope: ""}, + {name: "explicit oauth scope", scope: pluginapi.ExecutorModelScopeOAuth}, + {name: "explicit both scope with auth provider", scope: pluginapi.ExecutorModelScopeBoth}, + } { + t.Run(tc.name, func(t *testing.T) { + provider := "demo-issue7512-" + strings.ReplaceAll(tc.name, " ", "-") + pluginID := provider + "-plugin" + reg := registry.GetGlobalRegistry() + staticClientID := "plugin:" + pluginID + ":" + provider + ":executor" + reg.UnregisterClient(staticClientID) + + plugin := issue7512Plugin{ + provider: provider, + models: []pluginapi.ModelInfo{ + {ID: "demo-a"}, + {ID: "demo-blocked"}, + {ID: "demo-per-auth-blocked"}, + }, + } + + host := pluginhost.New() + host.RegisterPluginForTest(pluginID, pluginapi.Plugin{ + Capabilities: pluginapi.Capabilities{ + AuthProvider: plugin, + ModelProvider: plugin, + Executor: plugin, + ExecutorModelScope: tc.scope, + }, + }) + + manager := coreauth.NewManager(nil, &coreauth.RoundRobinSelector{}, nil) + cfg := &config.Config{ + OAuthExcludedModels: map[string][]string{ + provider: {"demo-blocked"}, + }, + OAuthModelAlias: map[string][]config.OAuthModelAlias{ + provider: {{Name: "demo-a", Alias: "demo-a-high", Fork: false}}, + }, + } + service := &Service{cfg: cfg, coreManager: manager, pluginHost: host} + + host.RegisterModels(t.Context(), reg) + host.RegisterExecutors(manager, reg) + t.Cleanup(func() { + reg.UnregisterClient(staticClientID) + }) + + // Phase 1: zero credentials must publish no models in ModelRegistry, + // while still keeping static catalog definitions available for #6736. + if available := reg.GetAvailableModelsByProvider(provider); len(available) != 0 { + t.Fatalf("with zero credentials, GetAvailableModelsByProvider(%q) = %#v, want empty", provider, available) + } + if catalog := host.ModelsForProvider(provider); len(catalog) != 3 { + t.Fatalf("with zero credentials, ModelsForProvider(%q) = %#v, want 3 catalog models", provider, catalog) + } + + // Phase 2: an active credential must enforce oauth-excluded-models, + // per-credential excluded_models, and oauth-model-alias (fork=false). + authID := provider + "-auth-1" + auth := &coreauth.Auth{ + ID: authID, + Provider: provider, + Status: coreauth.StatusActive, + Attributes: map[string]string{ + "auth_kind": "oauth", + "excluded_models": "demo-blocked,demo-per-auth-blocked", + }, + } + registered, errRegister := manager.Register(t.Context(), auth) + if errRegister != nil { + t.Fatal(errRegister) + } + t.Cleanup(func() { + reg.UnregisterClient(authID) + }) + + service.registerModelsForAuth(t.Context(), registered) + + availableIDs := make(map[string]bool) + for _, item := range reg.GetAvailableModelsByProvider(provider) { + if item != nil && item.ID != "" { + availableIDs[item.ID] = true + } + } + if availableIDs["demo-blocked"] { + t.Fatal("oauth-excluded model demo-blocked leaked into ModelRegistry") + } + if availableIDs["demo-per-auth-blocked"] { + t.Fatal("per-credential excluded model demo-per-auth-blocked leaked into ModelRegistry") + } + if availableIDs["demo-a"] { + t.Fatal("unaliased model demo-a leaked despite fork=false oauth-model-alias") + } + if !availableIDs["demo-a-high"] { + t.Fatalf("aliased model demo-a-high missing from ModelRegistry: %#v", availableIDs) + } + + // Phase 3: disabling the credential must remove all published models for the provider. + disabledAuth := registered.Clone() + disabledAuth.Disabled = true + disabledAuth.Status = coreauth.StatusDisabled + updated, errUpdate := manager.Update(t.Context(), disabledAuth) + if errUpdate != nil { + t.Fatal(errUpdate) + } + service.registerModelsForAuth(t.Context(), updated) + + if available := reg.GetAvailableModelsByProvider(provider); len(available) != 0 { + t.Fatalf("with disabled credential, GetAvailableModelsByProvider(%q) = %#v, want empty", provider, available) + } + if catalog := host.ModelsForProvider(provider); len(catalog) != 3 { + t.Fatalf("with disabled credential, ModelsForProvider(%q) = %#v, want 3 catalog models", provider, catalog) + } + }) + } +} diff --git a/sdk/cliproxy/service_executors.go b/sdk/cliproxy/service_executors.go index dcfcbe933d..0ad196e297 100644 --- a/sdk/cliproxy/service_executors.go +++ b/sdk/cliproxy/service_executors.go @@ -508,7 +508,7 @@ func (s *Service) pluginModelsForProvider(providerKey string) []*ModelInfo { if s == nil || s.pluginHost == nil { return nil } - return s.pluginHost.ModelsForProvider(providerKey) + return s.pluginHost.StaticAppendModelsForProvider(providerKey) } // appendPluginModels appends the provider's plugin static models that are not excluded. diff --git a/sdk/pluginapi/types.go b/sdk/pluginapi/types.go index 9e84fa29f3..b69cb8b6c3 100644 --- a/sdk/pluginapi/types.go +++ b/sdk/pluginapi/types.go @@ -105,7 +105,10 @@ type Capabilities struct { // Executor sends requests to an upstream provider or local backend. Executor ProviderExecutor // ExecutorModelScope declares whether Executor serves static models, OAuth auth models, or both. - // Empty defaults to ExecutorModelScopeBoth for backward compatibility. + // Empty defaults to ExecutorModelScopeOAuth when AuthProvider/AuthProviders is declared, + // and ExecutorModelScopeBoth otherwise for backward compatibility. + // When AuthProvider/AuthProviders is declared, only ExecutorModelScopeStatic registers an + // unauthenticated static model client in ModelRegistry. ExecutorModelScope ExecutorModelScope // ExecutorInputFormats lists request protocols accepted directly by Executor. Executors must declare at least one. ExecutorInputFormats []string @@ -169,7 +172,8 @@ type Capabilities struct { type ExecutorModelScope string const ( - // ExecutorModelScopeBoth means the executor supports static and OAuth auth-bound models. + // ExecutorModelScopeBoth means the executor supports static and OAuth auth-bound models + // when no AuthProvider/AuthProviders is declared, or OAuth auth-bound models when credential-bound. ExecutorModelScopeBoth ExecutorModelScope = "both" // ExecutorModelScopeStatic means the executor supports only non-OAuth static models. ExecutorModelScopeStatic ExecutorModelScope = "static"