diff --git a/internal/clients/claudecode/claudecode.go b/internal/clients/claudecode/claudecode.go index 1fbb226..12f70ed 100644 --- a/internal/clients/claudecode/claudecode.go +++ b/internal/clients/claudecode/claudecode.go @@ -221,7 +221,11 @@ func (c *Client) resolve(g *config.Global, provider, model string) (config.Provi if model == "" && sameProvider && slices.Contains(models, last.LastModel) { model = last.LastModel } - if model == "" && len(models) > 0 { + // Claude Code picks its own default when the provider offers Claude. The + // first model sorts alphabetically and may not be Claude, and then auto + // mode's server-side classifier has nothing to answer it. + isClaude := func(m string) bool { return strings.Contains(strings.ToLower(m), "claude") } + if model == "" && len(models) > 0 && !slices.ContainsFunc(models, isClaude) { model = models[0] } return p, b, model, nil diff --git a/internal/clients/claudecode/claudecode_test.go b/internal/clients/claudecode/claudecode_test.go index 6ddf791..beb25da 100644 --- a/internal/clients/claudecode/claudecode_test.go +++ b/internal/clients/claudecode/claudecode_test.go @@ -397,13 +397,13 @@ func TestSpec(t *testing.T) { } c := &Client{} - // Defaults: the first compatible provider, its first backend, its first model. + // Defaults: the first compatible provider, its first backend, Claude Code's own model. spec, err := c.Spec(newGlobal(), "", "", nil) if err != nil { t.Fatalf("Spec: %v", err) } - if spec.Env["ANTHROPIC_BASE_URL"] != testHost || spec.Env["ANTHROPIC_MODEL"] != "claude-opus" { - t.Errorf("default env = %v, want the Anthropic backend on its first model", spec.Env) + if _, ok := spec.Env["ANTHROPIC_MODEL"]; spec.Env["ANTHROPIC_BASE_URL"] != testHost || ok { + t.Errorf("default env = %v, want the Anthropic backend with no model set", spec.Env) } // Flags win, and passthrough args land after aperture's own. @@ -490,3 +490,38 @@ func TestSpec(t *testing.T) { t.Errorf("conflicting settings: %v, want the conflict named", err) } } + +func TestSpec_DefaultModel(t *testing.T) { + tests := []struct { + name string + models []string + want string + }{ + {name: "claude offered after a non-claude model", models: []string{"alibaba/qwen3.8-max", "anthropic/claude-haiku-4.5"}, want: ""}, + {name: "claude offered last", models: []string{"deepseek/deepseek-v4-pro", "moonshotai/kimi-k3", "anthropic/claude-sonnet-5.5"}, want: ""}, + {name: "bare claude ids", models: []string{"claude-opus-5-5", "claude-sonnet-5-5"}, want: ""}, + {name: "claude fast variant", models: []string{"anthropic/claude-opus-5-5-fast"}, want: ""}, + {name: "bedrock inference profile", models: []string{"meta.llama4-maverick-17b-instruct-v1:0", "us.anthropic.claude-opus-4-1-20250805-v1:0"}, want: ""}, + {name: "vertex versioned id", models: []string{"gemini-3-pro", "claude-opus-4@20250514"}, want: ""}, + {name: "mixed case claude id", models: []string{"openai/gpt-5", "Anthropic/Claude-Opus-5-5"}, want: ""}, + {name: "no claude offered", models: []string{"glm-5.1", "glm-5-turbo"}, want: "glm-5.1"}, + {name: "no claude offered, prefixed ids", models: []string{"alibaba/qwen3.8-max", "deepseek/deepseek-v4-pro"}, want: "alibaba/qwen3.8-max"}, + {name: "no models", models: nil, want: ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + p := config.ProviderInfo{ID: "gw", Models: tt.models, SupportedEndpoints: map[string]bool{config.EndpointAnthropicMessages: true}} + g := &config.Global{ApertureHost: testHost, Providers: []config.ProviderInfo{p}} + + spec, err := (&Client{}).Spec(g, "", "", nil) + + if err != nil { + t.Fatalf("Spec: %v", err) + } + if got := spec.Env["ANTHROPIC_MODEL"]; got != tt.want { + t.Errorf("ANTHROPIC_MODEL = %q, want %q", got, tt.want) + } + }) + } +}