diff --git a/internal/provider/importapps.go b/internal/provider/importapps.go index 98edc60f6..11fcf0d8c 100644 --- a/internal/provider/importapps.go +++ b/internal/provider/importapps.go @@ -528,7 +528,16 @@ func readClaudeSettings(path string) ([]AppImport, error) { if b, err := os.ReadFile(filepath.Join(filepath.Dir(Path()), "stash.json")); err == nil { json.Unmarshal(b, &st) } + // what routing put aside is what stepping out would put back: + // the endpoint, token and main model, and with them the + // models of the user's own the tiers and subagents ran on + // (claude.env., internal/agent/claude.go) env = map[string]any{"ANTHROPIC_BASE_URL": st["claude.base_url"], "ANTHROPIC_AUTH_TOKEN": st["claude.auth_token"], "ANTHROPIC_MODEL": st["claude.model"]} + for k, v := range st { + if name, ok := strings.CutPrefix(k, "claude.env."); ok && v != "" { + env[name] = v + } + } } } base, _ := env["ANTHROPIC_BASE_URL"].(string) diff --git a/internal/provider/importapps_test.go b/internal/provider/importapps_test.go index b656c2093..c7623846a 100644 --- a/internal/provider/importapps_test.go +++ b/internal/provider/importapps_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "os" "path/filepath" + "slices" "strings" "testing" ) @@ -509,3 +510,49 @@ func TestGeminiURLIsItsHost(t *testing.T) { t.Fatalf("the relay's import entry: %+v", it) } } + +// While magpie routes Claude Code, its settings.json points at the +// gateway and what the user had there stands by in the stash. The import +// reads the stash back as magpie stepping out would put it back: the +// tier and subagent models beside the endpoint, token and main model. +func TestClaudeImportWhileRoutedTakesTheStashedEnv(t *testing.T) { + isolate(t) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming")) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + t.Setenv("MAGPIE_ADDR", "127.0.0.1:49999") + path := claudeSettingsPath() + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + t.Fatal(err) + } + // routed: magpie wrote its own address and key over the user's relay + if err := os.WriteFile(path, []byte(`{"env":{"ANTHROPIC_BASE_URL":"http://127.0.0.1:49999","ANTHROPIC_AUTH_TOKEN":"sk-magpie"}}`), 0o600); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil { + t.Fatal(err) + } + stash := map[string]string{ + "claude.base_url": "https://relay.example.com/anthropic", + "claude.auth_token": "sk-claude", + "claude.model": "claude-sonnet-5", + "claude.env.ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-5-x", + "claude.env.ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-x", + } + b, _ := json.Marshal(stash) + if err := os.WriteFile(filepath.Join(filepath.Dir(Path()), "stash.json"), b, 0o600); err != nil { + t.Fatal(err) + } + it := itemsOf(t, "claude-code")["settings"] + if it.Skip != "" || it.Provider.Key != "sk-claude" { + t.Fatalf("the relay in the stash: %+v", it) + } + for _, m := range []string{"claude-sonnet-5", "claude-sonnet-5-x", "claude-haiku-x"} { + if !slices.Contains(it.Provider.Models, m) { + t.Fatalf("models %v miss %s", it.Provider.Models, m) + } + } +}