diff --git a/backend/internal/handler/openai_codex_models_handler.go b/backend/internal/handler/openai_codex_models_handler.go index 2daac343a9..a4f24e9045 100644 --- a/backend/internal/handler/openai_codex_models_handler.go +++ b/backend/internal/handler/openai_codex_models_handler.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "go.uber.org/zap" infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware" @@ -109,7 +110,7 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) { return } if lastUpstreamErr != nil { - if h.writeLocalCodexModelsFallback(c, apiKey.Group) { + if h.writeLocalCodexModelsFallback(c, apiKey.Group, lastUpstreamErr) { return } h.errorResponse(c, infraerrors.Code(lastUpstreamErr), "upstream_error", infraerrors.Message(lastUpstreamErr)) @@ -139,7 +140,7 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) { // upstream without a usable /models endpoint must not break the // client model picker while the group can describe its models // locally (#5252). - if h.writeLocalCodexModelsFallback(c, apiKey.Group) { + if h.writeLocalCodexModelsFallback(c, apiKey.Group, err) { return } } @@ -148,7 +149,7 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) { } // A 2xx manifest with no models is as unusable as a failed request for // Codex clients. Prefer the local catalog when one exists. - if service.CodexModelsManifestIsEmpty(manifest.Body) && h.writeLocalCodexModelsFallback(c, apiKey.Group) { + if service.CodexModelsManifestIsEmpty(manifest.Body) && h.writeLocalCodexModelsFallback(c, apiKey.Group, errors.New("upstream returned an empty models manifest")) { return } if err := h.gatewayService.CompleteAPIKeyCodexModelsManifestForClient(manifest, account); err != nil { @@ -175,18 +176,26 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) { // writeLocalCodexModelsFallback writes a locally generated Codex manifest when // the selected upstreams cannot serve /models. It reports false when the local // model list is empty so the caller can preserve the original upstream error. -func (h *OpenAIGatewayHandler) writeLocalCodexModelsFallback(c *gin.Context, group *service.Group) bool { +func (h *OpenAIGatewayHandler) writeLocalCodexModelsFallback(c *gin.Context, group *service.Group, upstreamErr error) bool { if h == nil || h.gatewayService == nil { return false } + reqLog := requestLogger(c, "handler.openai_gateway.codex_models") manifest, configured, err := h.gatewayService.BuildLocalCodexModelsFallbackManifest( c.Request.Context(), group, c.GetHeader("If-None-Match"), ) - if err != nil || !configured || manifest == nil { + if err != nil { + reqLog.Warn("openai.codex_models_local_fallback_failed", zap.Error(err), zap.NamedError("upstream_error", upstreamErr)) return false } + if !configured || manifest == nil { + return false + } + // The client gets a 200, so this is the only signal that the upstream + // /models endpoint is unusable for the group. + reqLog.Warn("openai.codex_models_local_fallback_served", zap.NamedError("upstream_error", upstreamErr)) h.writeCodexModelsResponse(c, group, manifest) return true } diff --git a/backend/internal/service/openai_codex_models_fallback.go b/backend/internal/service/openai_codex_models_fallback.go index 3cc2163774..1d8760e2db 100644 --- a/backend/internal/service/openai_codex_models_fallback.go +++ b/backend/internal/service/openai_codex_models_fallback.go @@ -71,10 +71,19 @@ func (s *OpenAIGatewayService) BuildLocalCodexModelsFallbackManifest( // localCodexFallbackModelIDs resolves the local model list in the same priority // order the fallback documents. An enabled group list wins over account // mappings, and mappings win over the default catalog. +// +// The result always passes through FilterCodexModelIDsForGroup, the same +// listing filter /v1/models uses, so the degraded catalog can never advertise a +// model the group allowlist would reject. A list made only of wildcard entries +// has no concrete names to offer, so its patterns are expanded against the +// local candidates (account mappings and the default catalog). func localCodexFallbackModelIDs(group *Group, accounts []Account) []string { var modelIDs []string if group != nil && group.ModelAllowlistEnabled() { modelIDs = concreteCodexModelIDs(group.ModelAllowlist.Models) + if len(modelIDs) == 0 && groupModelAllowlistHasWildcard(group.ModelAllowlist.Models) { + modelIDs = append(openAIConfiguredCodexModelIDs(accounts), openai.DefaultModelIDs()...) + } } if len(modelIDs) == 0 { modelIDs = openAIConfiguredCodexModelIDs(accounts) @@ -82,7 +91,16 @@ func localCodexFallbackModelIDs(group *Group, accounts []Account) []string { if len(modelIDs) == 0 { modelIDs = openai.DefaultModelIDs() } - return filterRetiredOpenAIModelIDs(accounts, modelIDs) + return FilterCodexModelIDsForGroup(filterRetiredOpenAIModelIDs(accounts, modelIDs), group) +} + +func groupModelAllowlistHasWildcard(models []string) bool { + for _, modelID := range models { + if strings.Contains(modelID, "*") { + return true + } + } + return false } // concreteCodexModelIDs keeps concrete model names. Wildcard patterns are diff --git a/backend/internal/service/openai_codex_models_fallback_test.go b/backend/internal/service/openai_codex_models_fallback_test.go index c65804e963..832e054cc1 100644 --- a/backend/internal/service/openai_codex_models_fallback_test.go +++ b/backend/internal/service/openai_codex_models_fallback_test.go @@ -86,6 +86,41 @@ func TestBuildLocalCodexModelsFallbackManifestPrefersGroupModelsListConfig(t *te require.Equal(t, []string{"gpt-5.6-luna", "gpt-6.2-sol"}, codexFallbackManifestSlugs(t, manifest)) } +func TestBuildLocalCodexModelsFallbackManifestWildcardOnlyAllowlistStaysWithinAllowlist(t *testing.T) { + accounts := []Account{ + newCodexFallbackAPIKeyAccount(map[string]any{"gpt-5.5-custom": "upstream-model", "other-alias": "upstream-model"}), + } + group := &Group{ID: 7, Platform: PlatformOpenAI} + group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"gpt-5.5*"}} + + manifest, configured, err := newCodexFallbackTestService(accounts). + BuildLocalCodexModelsFallbackManifest(context.Background(), group, "") + + require.NoError(t, err) + require.True(t, configured) + slugs := codexFallbackManifestSlugs(t, manifest) + require.Contains(t, slugs, "gpt-5.5-custom") + require.Contains(t, slugs, "gpt-5.5") + for _, slug := range slugs { + require.True(t, group.ModelAllowlist.Allows(slug), "fallback must not advertise %q outside the allowlist", slug) + } + require.NotContains(t, slugs, "other-alias") + require.NotContains(t, slugs, "gpt-5.6-sol") +} + +func TestBuildLocalCodexModelsFallbackManifestWildcardOnlyAllowlistWithoutMatchKeepsUpstreamError(t *testing.T) { + accounts := []Account{newCodexFallbackAPIKeyAccount(map[string]any{"alias-a": "target-a"})} + group := &Group{ID: 7, Platform: PlatformOpenAI} + group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"no-such-family-*"}} + + manifest, configured, err := newCodexFallbackTestService(accounts). + BuildLocalCodexModelsFallbackManifest(context.Background(), group, "") + + require.NoError(t, err) + require.False(t, configured) + require.Nil(t, manifest) +} + func TestBuildLocalCodexModelsFallbackManifestUsesAccountModelMapping(t *testing.T) { accounts := []Account{ newCodexFallbackAPIKeyAccount(map[string]any{"alias-b": "target-b", "alias-a": "target-a"}),