From 1e654d8ce7410a41dc0cf312cd5ade7a6f41bfa6 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 05:28:36 +0800 Subject: [PATCH] agent: T3 Code keeps the name, auto-compact and other settings the user gave magpie's provider across a restart (#2030, Kanscape) T3 Code's sync wrote providerInstances.magpie whole on every start, so what the user changed on it in T3 Code (Display Name, Auto-compact after, its switch, colour, launch args, an env var of their own) went back to magpie's defaults when magpie restarted or updated. Sync and the field setter now merge into what is there (t3Merged): magpie's own parts are written as before (driver, customModels, the gateway's ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN and the review switch), every other key under the instance and its config is the user's and kept (mergeTheirs), a non-empty displayName and the enabled switch are theirs, and the environment list keeps the user's own variables beside magpie's. T3's own claudeAgent instance is never touched, so it and magpie's keep their settings apart. Run: - TestT3CodeKeepsTheUsersSettings: T3's claudeAgent instance beside magpie's; the user edits magpie's (name, enabled, accentColor, autoCompactWindow "300000", launchArgs, a stale model, a changed base URL, an added HTTPS_PROXY), then three syncs. Against the old t3code.go it fails on the name, the switch, the colour, the config and the env. - go test -tags nogui ./internal/agent (whole package, ok, 1555s on 4c746ab39; T3 tests again after rebasing on 27956002c), gofmt -l, go vet darwin, GOOS=linux -tags nogui, GOOS=windows. Not tried: the real T3 Code app (not installed here); the instance layout follows T3's settings schema from its source. A binaryPath or homePath carried over from T3's own Claude instance stays once T3 drops it; a value the user gives magpie's review variable is put back. --- internal/agent/t3code.go | 55 +++++++++++++++++- internal/agent/t3code_test.go | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 2 deletions(-) diff --git a/internal/agent/t3code.go b/internal/agent/t3code.go index 8367a36ac..30f0fee42 100644 --- a/internal/agent/t3code.go +++ b/internal/agent/t3code.go @@ -34,6 +34,7 @@ package agent // asks the gateway. import ( + "encoding/json" "os" "path/filepath" "runtime" @@ -66,7 +67,7 @@ func t3code(home string) *Agent { // found by its bundle. No command: `t3` is a common name. detect: func() bool { return isDir(base) || t3App(home) }, Sync: func() error { - return syncJSON(path, t3Instance, func() any { return t3InstanceJSON(path, gateway.URL()) }) + return syncJSON(path, t3Instance, func() any { return t3Merged(path, gateway.URL()) }) }, // its custom models are the models picked for it (#1508) ListsModels: true, @@ -82,7 +83,7 @@ func t3code(home string) *Agent { if v == "" { return edit.DelJSON(path, t3Instance) } - return edit.SetJSON(path, edit.KV{Path: t3Instance, Value: t3InstanceJSON(path, gateway.URL())}) + return edit.SetJSON(path, edit.KV{Path: t3Instance, Value: t3Merged(path, gateway.URL())}) }, Options: func(map[string]string) []Option { return []Option{{Value: magpieID, Label: "magpie", Icon: "magpie", Note: "every magpie model as a provider in T3 Code, on Claude Code"}} @@ -151,6 +152,56 @@ func t3InstanceJSON(path, gw string) map[string]any { } } +// t3Env is the environment variables of magpie's instance that magpie +// sets: the gateway's address and key, and auto mode's review. +var t3Env = []string{"ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN", claudeReviewEnv} + +// t3Merged is magpie's instance as t3InstanceJSON writes it, merged into +// the one at path: what the user set on it in T3 Code's own settings +// stays (#2030, Kanscape: its name and Auto-compact after were reset on +// every start). Its name, whether it is on, and every key magpie doesn't +// write (config.autoCompactWindow, config.launchArgs, accentColor) are +// theirs, as are the environment variables besides magpie's own. The +// custom models and the gateway's variables are magpie's, and so is a +// binary path or Claude home while T3's own Claude has one to carry over. +// Only magpie's instance is read: another, such as T3's own Claude, lends +// it nothing of the user's. +func t3Merged(path, gw string) any { + want := t3InstanceJSON(path, gw) + cur, ok := edit.GetJSON(path, t3Instance) + if !ok { + return want + } + var have map[string]json.RawMessage + if json.Unmarshal([]byte(cur), &have) != nil || have == nil { + return want + } + var name string + if json.Unmarshal(have["displayName"], &name) == nil && strings.TrimSpace(name) != "" { + want["displayName"] = name + } + var on bool + if json.Unmarshal(have["enabled"], &on) == nil { + want["enabled"] = on + } + var theirs []map[string]any + if json.Unmarshal(have["environment"], &theirs) == nil { + env := want["environment"].([]map[string]any) + for _, v := range theirs { + if n, _ := v["name"].(string); n != "" && !slices.Contains(t3Env, n) { + env = append(env, v) + } + } + want["environment"] = env + } + b, err := json.Marshal(want) + var w map[string]json.RawMessage + if err != nil || json.Unmarshal(b, &w) != nil { + return want + } + return mergeTheirs(have, w, []string{"environment"}) +} + // t3EffortLabels are T3 Code's names for Claude Code's levels, as its own // Claude models show them. var t3EffortLabels = map[string]string{"low": "Low", "medium": "Medium", "high": "High", "xhigh": "Extra High", "max": "Max"} diff --git a/internal/agent/t3code_test.go b/internal/agent/t3code_test.go index 1a8f80856..8b1a1d711 100644 --- a/internal/agent/t3code_test.go +++ b/internal/agent/t3code_test.go @@ -9,6 +9,7 @@ import ( "slices" "testing" + "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" ) @@ -328,3 +329,107 @@ func TestT3CodeEfforts(t *testing.T) { } } } + +// What the user set on magpie's instance in T3 Code's own settings stays +// through every sync (#2030, Kanscape): its name, Auto-compact after, +// launch arguments, a variable of their own and whether it is on, while +// the models and the gateway's variables stay magpie's. T3's own Claude, +// an instance beside it with a name and window of its own, neither changes +// nor lends magpie's instance anything. +func TestT3CodeKeepsTheUsersSettings(t *testing.T) { + home := syncHome(t) + t.Setenv("T3CODE_HOME", "") + path := filepath.Join(home, ".t3", "userdata", "settings.json") + a := t3code(home) + os.MkdirAll(filepath.Dir(path), 0o755) + const claude = `{"driver":"claudeAgent","displayName":"Claude (sub)","enabled":true,"config":{"autoCompactWindow":"200000","launchArgs":"--chrome"}}` + os.WriteFile(path, []byte(`{"providerInstances":{"claudeAgent":`+claude+`}}`), 0o600) + f := a.Field("provider") + if err := f.Set(magpieID); err != nil { + t.Fatal(err) + } + read := func() map[string]json.RawMessage { + var file struct { + ProviderInstances map[string]json.RawMessage `json:"providerInstances"` + } + if err := json.Unmarshal([]byte(readFile(path)), &file); err != nil { + t.Fatalf("%v\n%s", err, readFile(path)) + } + return file.ProviderInstances + } + var mine map[string]any + json.Unmarshal(read()[magpieID], &mine) + if mine["displayName"] != "magpie" { + t.Fatalf("first written as %v", mine["displayName"]) + } + cfg := mine["config"].(map[string]any) + if _, ok := cfg["autoCompactWindow"]; ok { + t.Fatalf("the Claude instance's window lent to magpie's: %v", cfg) + } + + // the user edits magpie's instance in T3 Code, as T3 saves it + mine["displayName"] = "Magpie GW" + mine["enabled"] = false + mine["accentColor"] = "#ff8800" + cfg["autoCompactWindow"] = "300000" + cfg["launchArgs"] = "--verbose" + cfg["customModels"] = []any{map[string]any{"slug": "gone/old", "name": "old"}} + env := mine["environment"].([]any) + for _, v := range env { + if m := v.(map[string]any); m["name"] == "ANTHROPIC_BASE_URL" { + m["value"] = "http://127.0.0.1:1" + } + } + mine["environment"] = append(env, map[string]any{"name": "HTTPS_PROXY", "value": "http://proxy:8080", "sensitive": false}) + if err := edit.SetJSON(path, edit.KV{Path: t3Instance, Value: mine}); err != nil { + t.Fatal(err) + } + + for range 3 { + if err := a.Sync(); err != nil { + t.Fatal(err) + } + } + insts := read() + if !sameJSON(string(insts["claudeAgent"]), json.RawMessage(claude)) { + t.Errorf("T3's own Claude changed: %s", insts["claudeAgent"]) + } + var got struct { + DisplayName string `json:"displayName"` + Enabled *bool `json:"enabled"` + AccentColor string `json:"accentColor"` + Environment []struct { + Name, Value string + } `json:"environment"` + Config struct { + AutoCompactWindow string `json:"autoCompactWindow"` + LaunchArgs string `json:"launchArgs"` + CustomModels []struct { + Slug string `json:"slug"` + } `json:"customModels"` + } `json:"config"` + } + if err := json.Unmarshal(insts[magpieID], &got); err != nil { + t.Fatal(err) + } + if got.DisplayName != "Magpie GW" || got.Enabled == nil || *got.Enabled || got.AccentColor != "#ff8800" { + t.Errorf("the user's name, switch or colour not kept: %s", insts[magpieID]) + } + if got.Config.AutoCompactWindow != "300000" || got.Config.LaunchArgs != "--verbose" { + t.Errorf("the user's config not kept: %+v", got.Config) + } + envs := map[string]string{} + for _, v := range got.Environment { + envs[v.Name] = v.Value + } + if envs["HTTPS_PROXY"] != "http://proxy:8080" || envs["ANTHROPIC_BASE_URL"] != gateway.URL() || len(got.Environment) != len(env)+1 { + t.Errorf("environment: %v", got.Environment) + } + if slices.ContainsFunc(got.Config.CustomModels, func(m struct { + Slug string `json:"slug"` + }) bool { + return m.Slug == "gone/old" + }) || len(got.Config.CustomModels) == 0 { + t.Errorf("custom models not magpie's: %+v", got.Config.CustomModels) + } +}