Claude Code 2.1.302 reads CLAUDE_CODE_AUTO_MODE_MODEL for the model its client-side auto mode classifier checks each action with. It doesn't change server-side auto mode, and a classifier model Anthropic sets for the account comes first. The installed 2.1.303 binary has the variable: unset, Claude Code picks the sonnet tier's model, else Sonnet. It refuses claude-sonnet-5-5, claude-opus-5-5 and claude-opus-5 with a warning and uses its own pick instead. - Claude Code gets an "auto mode" field (key auto_mode, Quiet, follows nothing). It can be set only while Claude Code is routed through magpie, only to one of magpie's models, and is written dashed like the other model keys. Its options leave out the three models Claude Code refuses, and Set refuses them too. - A value the user had before magpie routed Claude Code is stashed with their tiers (claudeOwnEnv). It is taken out while wired in and comes back on Disconnect or Claude Code's own model, unless they have set one again since. The restore loop now skips any claudeOwnEnv key the user wrote again. - magpie's own value goes when magpie steps out. A value the user sets while wired in is never touched. - GUI: an "Auto mode" line in the opened row's Tiers, after Subagents, using the app's model picker. Default is noted as Claude Code's own pick. Strings are translated for zh, zh-TW, ja and de. - Docs: agent-wiring.md and reference.md. Run, in a worktree at origin/main33dbdcc52, sandbox HOME, load ~110-135: - gofmt -l: clean. - go vet ./... (darwin), GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: clean. - set -o pipefail; go test -timeout 45m ./...: every package ok. - Breaking by hand turned the new tests red: dropping the stash-time removal, the "set again since" check, the no-classifier refusal and the removal on step-out made TestClaudeAutoModeModel, TestClaudeAutoModeUserValue and TestClaudeDisconnect fail. - GUI, playwright 1.62.0, Chromium and WebKit: 84 files the change reaches (agent*, claude*, tier/subagent effort, codex subagent/memories, gui-de, gui-ja, gui-zh-tw, i18n-keys, header-relang, and every file with a Claude row or the model picker), concurrency 4: 1305 of 1306 pass. - The one red is agent-expand-steady "webkit 980px ... slides" ("slid open: 208"). Its fixture has no auto mode field. Alone it passed 3 of 3. Filed as #2935. - claude-auto-mode.test.cjs (new; en and zh, 1100px and 560px, both engines): 8 of 8 pass, and 4 of 4 fail in Chromium on the old app.js. - Not tried against a real Claude Code session. - After rebasing on origin/mainab7ec8c65(three commits, none touching these files): - go vet ./internal/agent and TestClaudeAutoMode*/TestClaudeDisconnect: ok. - claude-auto-mode, tier-effort, i18n-keys and agent-layout, both engines: 23 of 23 pass serially, and 19 of 19 in parallel. - A first parallel try at load ~127 had 16 reds in claude-auto-mode and tier-effort, and its output wasn't kept. It didn't come back in the next three runs.
266 lines
10 KiB
Go
266 lines
10 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Disconnect leaves Codex as it was before magpie was wired in (Fate on
|
|
// Discord asked for one action that takes out what magpie wrote): the
|
|
// provider, model and effort the user had come back, and magpie's base URL,
|
|
// catalog, subagent model and the effort it set go. Picking the agent's
|
|
// default instead leaves Codex as installed, the user's own provider gone.
|
|
func TestCodexDisconnect(t *testing.T) {
|
|
const own = "model_provider = \"mine\"\nmodel = \"gpt-5.4\"\nmodel_reasoning_effort = \"high\"\n\n" +
|
|
"[model_providers.mine]\nname = \"mine\"\nbase_url = \"https://mine.example/v1\"\n"
|
|
for _, tc := range []struct{ name, config, auth string }{
|
|
{"own provider", own, ""},
|
|
{"own provider, signed in", own, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`},
|
|
{"nothing set", "", ""},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
home, read := codexHome(t, tc.auth, tc.config)
|
|
path := filepath.Join(home, ".codex", "config.toml")
|
|
top := func() map[string]string {
|
|
m := map[string]string{}
|
|
for _, k := range []string{"model", "model_provider", "model_reasoning_effort", "openai_base_url", "model_catalog_json"} {
|
|
if v, _ := edit.GetTOMLTop(path, k); v != "" {
|
|
m[k] = v
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
was := top()
|
|
cx := codex(home)
|
|
if cx.Wired() {
|
|
t.Fatal("wired before magpie set anything")
|
|
}
|
|
if err := cx.Apply("model", "fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cx.Apply("effort", "low"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cx.Apply("subagent", "fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !cx.Wired() {
|
|
t.Fatalf("not wired on a magpie model:\n%s", read())
|
|
}
|
|
if err := cx.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := top(); !reflect.DeepEqual(got, was) {
|
|
t.Fatalf("after disconnect %v, want %v:\n%s", got, was, read())
|
|
}
|
|
if agents, _ := edit.GetTOMLTable(path, "agents"); agents["default_subagent_model"] != "" {
|
|
t.Fatalf("subagent model left:\n%s", read())
|
|
}
|
|
if mine, _ := edit.GetTOMLTable(path, "model_providers.mine"); tc.config != "" && mine["base_url"] != "https://mine.example/v1" {
|
|
t.Fatalf("own provider table changed:\n%s", read())
|
|
}
|
|
if _, err := os.Stat(filepath.Join(home, ".codex", "magpie-models.json")); err == nil {
|
|
t.Error("magpie's catalog left")
|
|
}
|
|
if cx.Wired() || cx.Drift() != nil {
|
|
t.Fatalf("still wired (drift %+v):\n%s", cx.Drift(), read())
|
|
}
|
|
if _, ok := appliedLoad()["codex"]; ok {
|
|
t.Error("magpie still remembers setting Codex")
|
|
}
|
|
for _, k := range []string{"codex.model", "codex.effort", "codex.provider", "codex.catalog"} {
|
|
if v, ok := stashLoad()[here(home).key(k)]; ok {
|
|
t.Errorf("stash keeps %s = %q", k, v)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Disconnect leaves Claude Code's settings.json as it was before magpie
|
|
// was wired in: the relay's endpoint and token and the model the user had
|
|
// come back, and every env key, tier and capability magpie wrote goes.
|
|
// The tier and subagent models the user set for their relay come back with
|
|
// it (#1050), whether magpie steps out by Disconnect or by Claude Code's
|
|
// own model. So does the main model set for the relay in the env
|
|
// (ANTHROPIC_MODEL), which magpie takes out while wired in, and the auto
|
|
// mode classifier's model (CLAUDE_CODE_AUTO_MODE_MODEL).
|
|
func TestClaudeDisconnect(t *testing.T) {
|
|
for name, before := range map[string]string{
|
|
"own relay": `{"theme":"dark","model":"opus","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","DISABLE_TELEMETRY":"1"}}`,
|
|
"own tiers": `{"model":"sonnet","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","ANTHROPIC_DEFAULT_OPUS_MODEL":"relay-opus","ANTHROPIC_DEFAULT_SONNET_MODEL":"relay-sonnet","ANTHROPIC_DEFAULT_HAIKU_MODEL":"relay-haiku","ANTHROPIC_SMALL_FAST_MODEL":"relay-haiku","CLAUDE_CODE_SUBAGENT_MODEL":"relay-sonnet"}}`,
|
|
"nothing set": `{"theme":"dark"}`,
|
|
// the auto mode classifier's model the user set themselves, which
|
|
// magpie takes out while wired in and puts back
|
|
"own auto mode": `{"env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","CLAUDE_CODE_AUTO_MODE_MODEL":"relay-haiku"}}`,
|
|
// the main model set for the relay in the env (with no settings
|
|
// model, and beside one), which magpie takes out while wired in:
|
|
// it outranks a pick in /model
|
|
"own env model": `{"env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","ANTHROPIC_MODEL":"relay-main"}}`,
|
|
"own env model, both": `{"model":"opus","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","ANTHROPIC_MODEL":"relay-main[1m]"}}`,
|
|
} {
|
|
for _, out := range []string{"disconnect", "default"} {
|
|
t.Run(name+", "+out, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(home, ".claude", "settings.json")
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(before), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parse := func() map[string]any {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// an object magpie emptied out is the same as none
|
|
for k, v := range m {
|
|
if o, ok := v.(map[string]any); ok && len(o) == 0 {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
want := parse()
|
|
a := claude(home)
|
|
if a.Wired() {
|
|
t.Fatal("wired before magpie set anything")
|
|
}
|
|
if err := a.Apply("model", "deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Apply("haiku", "deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Apply("subagent", "deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Apply("auto_mode", "deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !a.Wired() {
|
|
t.Fatal("not wired on a magpie model")
|
|
}
|
|
if out == "default" {
|
|
if err := a.Apply("model", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Claude Code's own model, set by leaving none
|
|
delete(want, "model")
|
|
} else if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := parse(); !reflect.DeepEqual(got, want) {
|
|
b, _ := os.ReadFile(path)
|
|
t.Fatalf("after disconnect:\n%s\nwant %v", b, want)
|
|
}
|
|
if a.Wired() {
|
|
t.Fatal("still wired")
|
|
}
|
|
if _, ok := appliedLoad()["claude"]; ok && out == "disconnect" {
|
|
t.Error("magpie still remembers setting Claude Code")
|
|
}
|
|
for k := range stashLoad() {
|
|
if strings.HasPrefix(k, "claude.env.") {
|
|
t.Errorf("stash keeps %s", k)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// Gemini CLI's model default forgets the sign-in it had before magpie:
|
|
// Disconnect brings back the sign-in, the model and the user's own key.
|
|
func TestGeminiDisconnect(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
dir := filepath.Join(home, ".gemini")
|
|
os.MkdirAll(dir, 0o755)
|
|
settings := `{"security":{"auth":{"selectedType":"gemini-api-key"}},"model":{"name":"gemini-2.5-pro"}}`
|
|
os.WriteFile(filepath.Join(dir, "settings.json"), []byte(settings), 0o644)
|
|
os.WriteFile(filepath.Join(dir, ".env"), []byte("GEMINI_API_KEY=mine\n"), 0o600)
|
|
g := gemini(home)
|
|
was := g.Values()
|
|
if err := g.Apply("model", "fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !g.Wired() {
|
|
t.Fatal("not wired on a magpie model")
|
|
}
|
|
if err := g.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := g.Values(); !reflect.DeepEqual(got, was) || g.Wired() {
|
|
t.Fatalf("after disconnect %v, want %v", got, was)
|
|
}
|
|
if k, _ := edit.GetEnvFile(filepath.Join(dir, ".env"), "GEMINI_API_KEY"); k != "mine" {
|
|
t.Fatalf("own key %q", k)
|
|
}
|
|
if u, _ := edit.GetEnvFile(filepath.Join(dir, ".env"), "GOOGLE_GEMINI_BASE_URL"); u != "" {
|
|
t.Fatalf("base URL left: %q", u)
|
|
}
|
|
}
|
|
|
|
// An agent magpie is not in has nothing to disconnect: its config stays
|
|
// byte for byte.
|
|
func TestDisconnectUnwiredLeavesConfig(t *testing.T) {
|
|
home, read := codexHome(t, "", "model = \"gpt-5.4\"\nmodel_reasoning_effort = \"high\"\n")
|
|
was := read()
|
|
if err := codex(home).Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := read(); got != was {
|
|
t.Fatalf("changed:\n%s\nwant\n%s", got, was)
|
|
}
|
|
}
|
|
|
|
// An auth Gemini CLI doesn't know, set on its provider field, is refused
|
|
// before magpie is taken out. `magpie gemini <a model it has no option
|
|
// for>` sets that field (it is the first), and took Gemini CLI off magpie,
|
|
// its sign-in and key put back, before saying the auth was unknown: the
|
|
// sibling of `magpie snow --help` (#1457).
|
|
func TestGeminiUnknownAuthChangesNothing(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
dir := filepath.Join(home, ".gemini")
|
|
os.MkdirAll(dir, 0o755)
|
|
os.WriteFile(filepath.Join(dir, "settings.json"), []byte(`{"security":{"auth":{"selectedType":"gemini-api-key"}},"model":{"name":"gemini-2.5-pro"}}`), 0o644)
|
|
os.WriteFile(filepath.Join(dir, ".env"), []byte("GEMINI_API_KEY=mine\n"), 0o600)
|
|
g := gemini(home)
|
|
if err := g.Apply("model", "fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
read := func() string {
|
|
s, _ := os.ReadFile(filepath.Join(dir, "settings.json"))
|
|
e, _ := os.ReadFile(filepath.Join(dir, ".env"))
|
|
return string(s) + "\n" + string(e)
|
|
}
|
|
was, vals := read(), g.Values()
|
|
for _, v := range []string{"nonsense-model", "--help"} {
|
|
if err := g.Field("provider").Set(v); err == nil {
|
|
t.Errorf("%q taken", v)
|
|
}
|
|
if got := read(); got != was || !g.Wired() || !reflect.DeepEqual(g.Values(), vals) {
|
|
t.Fatalf("%q changed Gemini CLI:\n%s\nwas\n%s", v, got, was)
|
|
}
|
|
}
|
|
}
|