diff --git a/internal/runtime/executor/antigravity_executor_buildrequest_test.go b/internal/runtime/executor/antigravity_executor_buildrequest_test.go index 7faecb1fa4..78e9a80d69 100644 --- a/internal/runtime/executor/antigravity_executor_buildrequest_test.go +++ b/internal/runtime/executor/antigravity_executor_buildrequest_test.go @@ -1493,6 +1493,37 @@ func TestAntigravityBuildRequest_CapsThinkingBudgetWithMaxOutputTokens(t *testin } } +func TestAntigravityBuildRequest_SnakeCaseMaxOutputTokensAboveCatalogLimit(t *testing.T) { + t.Parallel() + + payload := []byte(`{ + "request": { + "contents": [{"role": "user", "parts": [{"text": "hello"}]}], + "generationConfig": { + "max_output_tokens": 70000, + "thinkingConfig": {"thinkingBudget": 64000} + } + } + }`) + + executor := &AntigravityExecutor{} + auth := &cliproxyauth.Auth{Metadata: map[string]any{"project_id": "project-1"}} + doc := ojson.NewDoc(payload) + _, finalPayload, errBuild := executor.buildRequestDoc(context.Background(), auth, "token", "claude-sonnet-4-6", doc, false, "", "https://example.com") + if errBuild != nil { + t.Fatalf("buildRequestDoc() error: %v", errBuild) + } + if got := ojson.GetBytes(finalPayload, "request.generationConfig.max_output_tokens"); got.Exists() { + t.Fatalf("max_output_tokens alias remains in final payload: %s", finalPayload) + } + if got := ojson.GetBytes(finalPayload, "request.generationConfig.maxOutputTokens").Int(); got != 64000 { + t.Fatalf("maxOutputTokens = %d, want 64000; body=%s", got, finalPayload) + } + if got := ojson.GetBytes(finalPayload, "request.generationConfig.thinkingConfig.thinkingBudget").Int(); got != 63999 { + t.Fatalf("thinkingBudget = %d, want 63999; body=%s", got, finalPayload) + } +} + func TestAntigravityBuildRequest_CaseInsensitiveGeminiProAndImageModels(t *testing.T) { t.Parallel() diff --git a/internal/runtime/executor/antigravity_executor_request.go b/internal/runtime/executor/antigravity_executor_request.go index ff303f3bd8..18ddb5964c 100644 --- a/internal/runtime/executor/antigravity_executor_request.go +++ b/internal/runtime/executor/antigravity_executor_request.go @@ -181,15 +181,13 @@ func (e *AntigravityExecutor) buildRequestForModelDoc(ctx context.Context, auth // A matched variant limits output using the same catalog as its ID and enum. if selection.MaxOutputTokens > 0 { helps.CapAntigravityMaxOutputTokensDoc(doc, selection.MaxOutputTokens, isClaudeModel) - } else if maxOut := doc.Get("request.generationConfig.maxOutputTokens"); maxOut.Exists() && maxOut.Type == ojson.Number { + } else if maxOut, ok := antigravityRequestedMaxOutputTokensDoc(doc); ok { modelInfo := registry.LookupModelInfo(modelName, "antigravity") if modelInfo == nil && lowerModel != modelName { modelInfo = registry.LookupModelInfo(lowerModel, "antigravity") } - if modelInfo != nil && modelInfo.MaxCompletionTokens > 0 { - if int(maxOut.Int()) > modelInfo.MaxCompletionTokens { - helps.CapAntigravityMaxOutputTokensDoc(doc, modelInfo.MaxCompletionTokens, isClaudeModel) - } + if modelInfo != nil && modelInfo.MaxCompletionTokens > 0 && maxOut > modelInfo.MaxCompletionTokens { + helps.CapAntigravityMaxOutputTokensDoc(doc, modelInfo.MaxCompletionTokens, isClaudeModel) } } if antigravityRequestNeedsSchemaSanitizationDoc(doc) { @@ -1105,3 +1103,25 @@ func generateStableSessionIDFromContentsForAuth(contents ojson.Result, authID st } return generateSessionID() } + +func antigravityRequestedMaxOutputTokensDoc(doc *ojson.Doc) (int, bool) { + if doc == nil { + return 0, false + } + maxOut := 0 + found := false + for _, path := range []string{ + "request.generationConfig.maxOutputTokens", + "request.generationConfig.max_output_tokens", + } { + val := doc.Get(path) + if val.Exists() && val.Type == ojson.Number { + n := int(val.Int()) + if !found || n > maxOut { + maxOut = n + found = true + } + } + } + return maxOut, found +} diff --git a/internal/runtime/executor/helps/antigravity_thinking.go b/internal/runtime/executor/helps/antigravity_thinking.go index 4f812ce817..4627ef0ed7 100644 --- a/internal/runtime/executor/helps/antigravity_thinking.go +++ b/internal/runtime/executor/helps/antigravity_thinking.go @@ -47,6 +47,8 @@ func (model AntigravityRequestModel) TranslationModelInfo(req cliproxyexecutor.R return ®istry.ModelInfo{ID: model.ID, NativeCapabilities: ®istry.NativeCapabilities{WebSearch: &model.WebSearch}} } +const minClaudeThinkingBudget int64 = 1024 + // CapAntigravityMaxOutputTokensDoc preserves a smaller caller limit and keeps // Claude thinking budgets below the effective output limit. func CapAntigravityMaxOutputTokensDoc(doc *ojson.Doc, limit int, isClaudeModel bool) { @@ -71,10 +73,27 @@ func CapAntigravityMaxOutputTokensDoc(doc *ojson.Doc, limit int, isClaudeModel b for _, budgetPath := range []string{ "request.generationConfig.thinkingConfig.thinkingBudget", "request.generationConfig.thinkingConfig.thinking_budget", + "request.generationConfig.thinking_config.thinkingBudget", + "request.generationConfig.thinking_config.thinking_budget", } { budget := doc.Get(budgetPath) - if budget.Exists() && budget.Int() >= effectiveMaxOutputTokens { - _ = doc.Set(budgetPath, effectiveMaxOutputTokens-1) + if !budget.Exists() || budget.Type != ojson.Number { + continue + } + budgetVal := budget.Int() + if budgetVal < 0 { + continue + } + if budgetVal >= effectiveMaxOutputTokens { + budgetVal = effectiveMaxOutputTokens - 1 + } + if budgetVal < minClaudeThinkingBudget { + _ = doc.Delete("request.generationConfig.thinkingConfig") + _ = doc.Delete("request.generationConfig.thinking_config") + return + } + if budgetVal != budget.Int() { + _ = doc.Set(budgetPath, budgetVal) } } } diff --git a/internal/runtime/executor/helps/antigravity_thinking_test.go b/internal/runtime/executor/helps/antigravity_thinking_test.go new file mode 100644 index 0000000000..956f7d21f9 --- /dev/null +++ b/internal/runtime/executor/helps/antigravity_thinking_test.go @@ -0,0 +1,124 @@ +package helps + +import ( + "testing" + + "github.com/MoeForge/moego/ojson" +) + +func TestCapAntigravityMaxOutputTokensDoc_ClaudeMinBudget(t *testing.T) { + t.Run("strips_thinkingConfig_when_clamped_below_1024", func(t *testing.T) { + tests := []struct { + name string + payload string + limit int + wantMax int64 + }{ + { + name: "camelCase_maxOutputTokens_1024", + payload: `{"request":{"generationConfig":{"maxOutputTokens":1024,"thinkingConfig":{"thinkingBudget":2048,"includeThoughts":true}}}}`, + limit: 64000, + wantMax: 1024, + }, + { + name: "snake_case_max_output_tokens_512", + payload: `{"request":{"generationConfig":{"max_output_tokens":512,"thinkingConfig":{"thinkingBudget":2048,"includeThoughts":true}}}}`, + limit: 64000, + wantMax: 512, + }, + { + name: "limit_1_with_snake_case_thinking_budget", + payload: `{"request":{"generationConfig":{"thinkingConfig":{"thinking_budget":2048,"include_thoughts":true}}}}`, + limit: 1, + wantMax: 1, + }, + { + name: "limit_1024_with_snake_case_thinking_config", + payload: `{"request":{"generationConfig":{"thinking_config":{"thinking_budget":2048,"include_thoughts":true}}}}`, + limit: 1024, + wantMax: 1024, + }, + { + name: "existing_budget_already_below_1024", + payload: `{"request":{"generationConfig":{"maxOutputTokens":1024,"thinkingConfig":{"thinkingBudget":512,"includeThoughts":true}}}}`, + limit: 64000, + wantMax: 1024, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + doc := ojson.NewDocString(tc.payload) + CapAntigravityMaxOutputTokensDoc(doc, tc.limit, true) + + if got := doc.Get("request.generationConfig.maxOutputTokens").Int(); got != tc.wantMax { + t.Fatalf("maxOutputTokens = %d, want %d; body=%s", got, tc.wantMax, doc.String()) + } + if doc.Get("request.generationConfig.max_output_tokens").Exists() { + t.Fatalf("max_output_tokens should be deleted; body=%s", doc.String()) + } + if tcNode := doc.Get("request.generationConfig.thinkingConfig"); tcNode.Exists() { + t.Fatalf("expected thinkingConfig to be deleted when Claude budget < 1024, got %s; body=%s", tcNode.Raw, doc.String()) + } + if tcSnake := doc.Get("request.generationConfig.thinking_config"); tcSnake.Exists() { + t.Fatalf("expected thinking_config to be deleted when Claude budget < 1024, got %s; body=%s", tcSnake.Raw, doc.String()) + } + }) + } + }) + + t.Run("boundary_1025_clamps_to_1024", func(t *testing.T) { + doc := ojson.NewDocString(`{"request":{"generationConfig":{"maxOutputTokens":1025,"thinkingConfig":{"thinkingBudget":2048,"includeThoughts":true}}}}`) + CapAntigravityMaxOutputTokensDoc(doc, 64000, true) + + if got := doc.Get("request.generationConfig.maxOutputTokens").Int(); got != 1025 { + t.Fatalf("maxOutputTokens = %d, want 1025; body=%s", got, doc.String()) + } + if got := doc.Get("request.generationConfig.thinkingConfig.thinkingBudget").Int(); got != 1024 { + t.Fatalf("thinkingBudget = %d, want 1024; body=%s", got, doc.String()) + } + if include := doc.Get("request.generationConfig.thinkingConfig.includeThoughts"); !include.Exists() || !include.Bool() { + t.Fatalf("includeThoughts = %s, want true; body=%s", include.Raw, doc.String()) + } + }) + + t.Run("preserves_non_numeric_dynamic_level_and_non_claude", func(t *testing.T) { + t.Run("non_numeric_budget_untouched", func(t *testing.T) { + doc := ojson.NewDocString(`{"request":{"generationConfig":{"maxOutputTokens":512,"thinkingConfig":{"thinkingBudget":"2048","includeThoughts":true}}}}`) + CapAntigravityMaxOutputTokensDoc(doc, 64000, true) + + got := doc.Get("request.generationConfig.thinkingConfig.thinkingBudget") + if !got.Exists() || got.Type != ojson.String || got.String() != "2048" { + t.Fatalf("expected string thinkingBudget to remain untouched, got type=%v raw=%s; body=%s", got.Type, got.Raw, doc.String()) + } + }) + + t.Run("dynamic_budget_negative_one_preserved", func(t *testing.T) { + doc := ojson.NewDocString(`{"request":{"generationConfig":{"maxOutputTokens":512,"thinkingConfig":{"thinkingBudget":-1,"includeThoughts":true}}}}`) + CapAntigravityMaxOutputTokensDoc(doc, 64000, true) + + got := doc.Get("request.generationConfig.thinkingConfig.thinkingBudget") + if !got.Exists() || got.Type != ojson.Number || got.Int() != -1 { + t.Fatalf("expected dynamic thinkingBudget -1 to remain preserved, got %s; body=%s", got.Raw, doc.String()) + } + }) + + t.Run("thinking_level_without_budget_preserved", func(t *testing.T) { + doc := ojson.NewDocString(`{"request":{"generationConfig":{"maxOutputTokens":512,"thinkingConfig":{"thinkingLevel":"LOW","includeThoughts":true}}}}`) + CapAntigravityMaxOutputTokensDoc(doc, 64000, true) + + if got := doc.Get("request.generationConfig.thinkingConfig.thinkingLevel").String(); got != "LOW" { + t.Fatalf("thinkingLevel = %q, want LOW; body=%s", got, doc.String()) + } + }) + + t.Run("non_claude_model_untouched", func(t *testing.T) { + doc := ojson.NewDocString(`{"request":{"generationConfig":{"maxOutputTokens":512,"thinkingConfig":{"thinkingBudget":2048,"includeThoughts":true}}}}`) + CapAntigravityMaxOutputTokensDoc(doc, 64000, false) + + if got := doc.Get("request.generationConfig.thinkingConfig.thinkingBudget").Int(); got != 2048 { + t.Fatalf("thinkingBudget = %d, want 2048; body=%s", got, doc.String()) + } + }) + }) +} diff --git a/internal/thinking/provider/antigravity/apply.go b/internal/thinking/provider/antigravity/apply.go index 8044c588cd..7a513cbec4 100644 --- a/internal/thinking/provider/antigravity/apply.go +++ b/internal/thinking/provider/antigravity/apply.go @@ -215,8 +215,26 @@ func (a *Applier) normalizeClaudeBudgetDoc(budget int, doc *ojson.Doc, modelInfo } func (a *Applier) effectiveMaxTokensDoc(doc *ojson.Doc, modelInfo *registry.ModelInfo) (max int, fromModel bool) { - if maxTok := doc.Get("request.generationConfig.maxOutputTokens"); maxTok.Exists() && maxTok.Int() > 0 { - return int(maxTok.Int()), false + callerMax := 0 + for _, path := range []string{ + "request.generationConfig.maxOutputTokens", + "request.generationConfig.max_output_tokens", + "request.generation_config.maxOutputTokens", + "request.generation_config.max_output_tokens", + } { + maxTok := doc.Get(path) + if maxTok.Exists() && maxTok.Type == ojson.Number && maxTok.Int() > 0 { + val := int(maxTok.Int()) + if callerMax == 0 || val < callerMax { + callerMax = val + } + } + } + if callerMax > 0 { + if modelInfo != nil && modelInfo.MaxCompletionTokens > 0 && callerMax > modelInfo.MaxCompletionTokens { + return modelInfo.MaxCompletionTokens, false + } + return callerMax, false } if modelInfo != nil && modelInfo.MaxCompletionTokens > 0 { return modelInfo.MaxCompletionTokens, true diff --git a/internal/thinking/provider/antigravity/apply_test.go b/internal/thinking/provider/antigravity/apply_test.go index 75c2e6ccc1..8f71364974 100644 --- a/internal/thinking/provider/antigravity/apply_test.go +++ b/internal/thinking/provider/antigravity/apply_test.go @@ -307,3 +307,88 @@ func TestApplier_EmitsUppercaseThinkingLevelEnumNames(t *testing.T) { } }) } + +func TestApplier_ClaudeEffectiveMaxTokensSnakeCase(t *testing.T) { + applier := thinking.GetProviderApplier("antigravity") + if applier == nil { + t.Fatal("antigravity provider applier is not registered") + } + modelInfo := claudeSonnet46AntigravityModel() + + t.Run("strips_thinking_for_snake_case_max_output_tokens_at_or_below_1024", func(t *testing.T) { + tests := []struct { + name string + body string + }{ + { + name: "generationConfig_max_output_tokens_1024", + body: `{"request":{"generationConfig":{"max_output_tokens":1024,"thinkingConfig":{"includeThoughts":true,"thinkingBudget":2048}}}}`, + }, + { + name: "generationConfig_max_output_tokens_512", + body: `{"request":{"generationConfig":{"max_output_tokens":512,"thinkingConfig":{"includeThoughts":true,"thinkingBudget":2048}}}}`, + }, + { + name: "generation_config_max_output_tokens_512", + body: `{"request":{"generation_config":{"max_output_tokens":512,"thinking_config":{"include_thoughts":true,"thinking_budget":2048}}}}`, + }, + { + name: "non_numeric_maxOutputTokens_falls_back_to_numeric_max_output_tokens", + body: `{"request":{"generationConfig":{"maxOutputTokens":"4096","max_output_tokens":512,"thinkingConfig":{"includeThoughts":true,"thinkingBudget":2048}}}}`, + }, + { + name: "both_camel_and_snake_uses_smaller_limit", + body: `{"request":{"generationConfig":{"maxOutputTokens":4096,"max_output_tokens":1024,"thinkingConfig":{"includeThoughts":true,"thinkingBudget":2048}}}}`, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + out, errApply := applier.Apply([]byte(tc.body), thinking.ThinkingConfig{Mode: thinking.ModeBudget, Budget: 2048}, modelInfo) + if errApply != nil { + t.Fatalf("Apply() error = %v", errApply) + } + if tcNode := ojson.GetBytes(out, "request.generationConfig.thinkingConfig"); tcNode.Exists() { + t.Fatalf("expected thinkingConfig to be stripped, got %s; body=%s", tcNode.Raw, out) + } + if tcSnake := ojson.GetBytes(out, "request.generationConfig.thinking_config"); tcSnake.Exists() { + t.Fatalf("expected thinking_config to be stripped, got %s; body=%s", tcSnake.Raw, out) + } + if got := ojson.GetBytes(out, "request.generationConfig.maxOutputTokens"); got.Exists() && got.Type == ojson.Number && got.Int() == int64(modelInfo.MaxCompletionTokens) { + t.Fatalf("unexpected default maxOutputTokens=%d injected when caller provided max_output_tokens; body=%s", got.Int(), out) + } + }) + } + }) + + t.Run("clamps_budget_using_snake_case_max_output_tokens_2048", func(t *testing.T) { + body := []byte(`{"request":{"generationConfig":{"max_output_tokens":2048,"thinkingConfig":{"includeThoughts":true,"thinkingBudget":4096}}}}`) + out, errApply := applier.Apply(body, thinking.ThinkingConfig{Mode: thinking.ModeBudget, Budget: 4096}, modelInfo) + if errApply != nil { + t.Fatalf("Apply() error = %v", errApply) + } + if got := ojson.GetBytes(out, "request.generationConfig.thinkingConfig.thinkingBudget").Int(); got != 2047 { + t.Fatalf("thinkingBudget = %d, want 2047; body=%s", got, out) + } + if include := ojson.GetBytes(out, "request.generationConfig.thinkingConfig.includeThoughts"); !include.Exists() || !include.Bool() { + t.Fatalf("includeThoughts = %s, want true; body=%s", include.Raw, out) + } + if got := ojson.GetBytes(out, "request.generationConfig.maxOutputTokens"); got.Exists() { + t.Fatalf("expected default maxOutputTokens not to be injected when max_output_tokens is set, got %s; body=%s", got.Raw, out) + } + }) + + t.Run("caps_caller_max_output_tokens_above_model_limit", func(t *testing.T) { + body := []byte(`{"request":{"generationConfig":{"max_output_tokens":70000,"thinkingConfig":{"includeThoughts":true,"thinkingBudget":64000}}}}`) + out, errApply := applier.Apply(body, thinking.ThinkingConfig{Mode: thinking.ModeBudget, Budget: 64000}, modelInfo) + if errApply != nil { + t.Fatalf("Apply() error = %v", errApply) + } + if got := ojson.GetBytes(out, "request.generationConfig.thinkingConfig.thinkingBudget").Int(); got != 63999 { + t.Fatalf("thinkingBudget = %d, want 63999; body=%s", got, out) + } + if include := ojson.GetBytes(out, "request.generationConfig.thinkingConfig.includeThoughts"); !include.Exists() || !include.Bool() { + t.Fatalf("includeThoughts = %s, want true; body=%s", include.Raw, out) + } + }) +}