From 42a2b1113a7845144d4337c4dbb7a4e72e2196c6 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 3 Oct 2026 22:27:17 +0800 Subject: [PATCH] =?UTF-8?q?agent:=20switched=20off=20and=20on=20again,=20a?= =?UTF-8?q?n=20agent=20is=20back=20on=20the=20magpie=20models=20it=20was?= =?UTF-8?q?=20on,=20and=20Goose=20keeps=20its=20own=20model=20through=20a?= =?UTF-8?q?=20round=20trip=20=E2=80=94=20found=20running=20connect/disconn?= =?UTF-8?q?ect=20twice=20for=20every=20agent=20against=20real=20configs=20?= =?UTF-8?q?in=20a=20sandbox=20HOME:=20turning=20one=20on=20again=20picked?= =?UTF-8?q?=20magpie's=20default=20instead=20of=20what=20the=20user=20had?= =?UTF-8?q?=20picked=20(OpenCode's=20model=20and=20small=5Fmodel,=20Codex'?= =?UTF-8?q?s=20model=20and=20effort,=20Pi,=20dsh,=20Hermes,=20Grok),=20and?= =?UTF-8?q?=20Goose=20lost=20GOOSE=5FPROVIDER/GOOSE=5FMODEL=20(its=20field?= =?UTF-8?q?=20clears=20both=20keys;=20on=20main=20too).=20Disconnect=20now?= =?UTF-8?q?=20stashes=20what=20it=20took=20off=20(.reconnect:=20the=20?= =?UTF-8?q?values,=20which=20were=20routed=20through=20magpie,=20and=20wha?= =?UTF-8?q?t=20it=20left),=20and=20Connect=20puts=20them=20back=20when=20t?= =?UTF-8?q?he=20agent=20is=20still=20as=20Disconnect=20left=20it=20?= =?UTF-8?q?=E2=80=94=20models=20on=20magpie=20first,=20so=20Gemini's=20mod?= =?UTF-8?q?el=20signs=20it=20in=20before=20its=20provider,=20and=20a=20val?= =?UTF-8?q?ue=20the=20model=20brought=20along=20(Codex's=20effort)=20is=20?= =?UTF-8?q?recorded=20as=20magpie's;=20changed=20since,=20Connect=20picks?= =?UTF-8?q?=20as=20before.=20Connect=20stashes=20what=20the=20agent=20was?= =?UTF-8?q?=20on=20(.connect.was),=20and=20Disconnect=20sets=20back=20?= =?UTF-8?q?a=20field=20it=20left=20empty=20when=20that=20was=20the=20user'?= =?UTF-8?q?s=20own=20(Goose's=20anthropic/claude-opus-5-5).=20TestReconnec?= =?UTF-8?q?tKeepsTheLastPick,=20TestGooseRoundTripKeepsItsOwnModel=20and?= =?UTF-8?q?=20the=20new=20reconnect=20check=20in=20TestGeminiConnectRoundT?= =?UTF-8?q?rip=20fail=20on=20the=20old=20code=20and=20pass.=20Sandbox=20ma?= =?UTF-8?q?trix:=20all=2020=20agents=20with=20a=20switch=20connect,=20disc?= =?UTF-8?q?onnect=20and=20reconnect=20twice=20with=20the=20right=20state,?= =?UTF-8?q?=20every=20disconnect=20preview=20line=20is=20in=20the=20real?= =?UTF-8?q?=20change;=2013=20CLIs=20(claude,=20codex,=20gemini,=20opencode?= =?UTF-8?q?,=20pi,=20goose,=20crush,=20droid,=20grok,=20cline,=20omp,=20ag?= =?UTF-8?q?y,=20commandcode)=20answer=20through=20the=20sandbox=20magpie.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/applied.go | 91 +++++++++++++++++++++++++++++ internal/agent/connect_pick_test.go | 80 +++++++++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/internal/agent/applied.go b/internal/agent/applied.go index 34e9cc7f4..8614ae601 100644 --- a/internal/agent/applied.go +++ b/internal/agent/applied.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strconv" "strings" "sync" @@ -304,6 +305,15 @@ func (a *Agent) Connect() error { if len(a.Fields) == 0 || a.Wired() { return nil } + // what it is on now, for Disconnect to put back what it can't + // otherwise (Goose's own model) + now := a.Values() + b, _ := json.Marshal(now) + stash(map[string]string{a.ID + ".connect.was": string(b)}) + // switched off and on again: the models it was on, as they were + if ok, err := a.reconnect(now); ok || err != nil { + return err + } // the model it is on stays, where the agent can have magpie's models // beside it (the owner: Codex keeps its own last pick) if a.Join != nil { @@ -449,10 +459,91 @@ func (a *Agent) Disconnect() error { return fmt.Errorf("%s: %w", f.Label, err) } } + // a field its own Set left empty goes back to what it was on before + // Connect (Goose's own provider and model) + var was map[string]string + json.Unmarshal([]byte(unstash(a.ID+".connect.was")), &was) + after := a.Values() + for _, f := range a.Fields { + if w := was[f.Key]; w != "" && after[f.Key] == "" && w != magpieID && !magpieValue(a, f, w, was) { + if err := f.Set(w); err != nil { + return fmt.Errorf("%s: %w", f.Label, err) + } + } + } + // what it was on through magpie, for switching it on again + back := reconnection{Magpie: map[string]string{}, Left: a.Values()} + for _, f := range a.Fields { + if v := before[f.Key]; v != back.Left[f.Key] { + back.Magpie[f.Key] = v + if v == magpieID || magpieValue(a, f, v, before) { + back.Routed = append(back.Routed, f.Key) + } + } + } + b, _ := json.Marshal(back) + stash(map[string]string{a.ID + ".reconnect": string(b)}) a.Keep() return nil } +// reconnection is what Disconnect took an agent off: the values of its +// fields it changed (Routed: those that were magpie's) and what it left +// every field at. +type reconnection struct { + Magpie map[string]string `json:"magpie"` + Routed []string `json:"routed,omitempty"` + Left map[string]string `json:"left"` +} + +// reconnect puts back the models an agent was on when it was disconnected +// (the owner: an agent remembers its last pick), if nothing has changed +// it since and magpie still routes them; false when it doesn't apply. +func (a *Agent) reconnect(now map[string]string) (bool, error) { + var rec reconnection + if json.Unmarshal([]byte(unstash(a.ID+".reconnect")), &rec) != nil || len(rec.Routed) == 0 { + return false, nil + } + for k, v := range rec.Left { + if now[k] != v { + return false, nil + } + } + for _, k := range rec.Routed { + f := a.Field(k) + if f == nil || rec.Magpie[k] != magpieID && !magpieValue(a, *f, rec.Magpie[k], rec.Magpie) { + return false, nil + } + } + // the models on magpie first, as Connect sets them (setting Gemini + // CLI's model signs it in through magpie, which its sign-in field + // can't), then the rest that aren't so by then + var keys []string + for _, k := range rec.Routed { + if rec.Magpie[k] != magpieID { + keys = append(keys, k) + } + } + for _, f := range a.Fields { + if _, ok := rec.Magpie[f.Key]; ok && !slices.Contains(keys, f.Key) { + keys = append(keys, f.Key) + } + } + for _, k := range keys { + v := rec.Magpie[k] + if v != a.Values()[k] { + if err := a.Apply(k, v); err != nil { + return true, err + } + } else { + // set along with the model (Codex's effort): magpie's as before, + // for Disconnect to take out again + record(a.ID, k, v) + } + } + return a.Wired(), nil +} + // lastJSONLTime reads the newest Unix timestamp (seconds or milliseconds) // under key in the last lines of a prompt log, passing over the lines whose // text (under textKey) is a slash command: those an agent answers itself. diff --git a/internal/agent/connect_pick_test.go b/internal/agent/connect_pick_test.go index 4d181d385..0b89937ae 100644 --- a/internal/agent/connect_pick_test.go +++ b/internal/agent/connect_pick_test.go @@ -61,6 +61,10 @@ func TestGeminiConnectRoundTrip(t *testing.T) { t.Fatalf("magpie's key left: %q", k) } } + // and on again, on the model it was on (its sign-in follows the model) + if err := g.Connect(); err != nil || !g.Wired() { + t.Fatalf("connected again: %v\n%s", err, readFile(filepath.Join(dir, "settings.json"))) + } } // Of the providers serving the model the agent is on, Connect takes the @@ -154,3 +158,79 @@ func TestCodexConnectKeepsItsPick(t *testing.T) { t.Fatalf("again (%v):\n%s", err, read()) } } + +// The 「接入」 switch off and on again leaves the agent on the models it was +// on (the owner: an agent remembers its last pick): Claude Code on a second +// magpie model and its effort, not the one Connect would pick. One the user +// changed while it was off is theirs, and Connect picks as it does at first. +func TestReconnectKeepsTheLastPick(t *testing.T) { + home, _ := codexHome(t, "", "") + if err := provider.Save(provider.Provider{ID: "aaa", Name: "AAA", Chat: "https://a.example/v1", Key: "k", Models: []string{"first", "claude-opus-5-5", "second"}}); err != nil { + t.Fatal(err) + } + path := filepath.Join(home, ".claude", "settings.json") + writeFile(t, path, `{"model": "opus"}`) + c := claude(home) + if err := c.Connect(); err != nil { + t.Fatal(err) + } + if err := c.Apply("model", "aaa/second"); err != nil { + t.Fatal(err) + } + if err := c.Apply("effort", "low"); err != nil { + t.Fatal(err) + } + if err := c.Disconnect(); err != nil { + t.Fatal(err) + } + if m, _ := edit.GetJSON(path, "model"); m != "opus" { + t.Fatalf("disconnected on %q:\n%s", m, readFile(path)) + } + if err := c.Connect(); err != nil { + t.Fatal(err) + } + if v := c.Values(); v["model"] != "aaa/second" || v["effort"] != "low" { + t.Fatalf("connected again on %v:\n%s", v, readFile(path)) + } + if err := c.Disconnect(); err != nil { + t.Fatal(err) + } + writeFile(t, path, `{"model": "sonnet"}`) + if err := c.Connect(); err != nil { + t.Fatal(err) + } + if v := c.Values(); v["model"] == "aaa/second" || v["effort"] == "low" { + t.Fatalf("a model picked while off was replaced by the last pick: %v", v) + } +} + +// Goose's own provider and model come back when it is disconnected: its +// field's empty value takes both keys out, and it was on anthropic before. +func TestGooseRoundTripKeepsItsOwnModel(t *testing.T) { + home := syncHome(t) + t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming")) + if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5"}}); err != nil { + t.Fatal(err) + } + a := goose(home, filepath.Join(home, ".config")) + writeFile(t, a.Path, "GOOSE_PROVIDER: anthropic\nGOOSE_MODEL: claude-opus-5-5\nGOOSE_MODE: auto\n") + for i := 0; i < 2; i++ { // and again, through its last pick + if err := a.Connect(); err != nil { + t.Fatal(err) + } + if !a.Wired() { + t.Fatalf("not connected:\n%s", readFile(a.Path)) + } + if err := a.Disconnect(); err != nil { + t.Fatal(err) + } + } + for k, want := range map[string]string{"GOOSE_PROVIDER": "anthropic", "GOOSE_MODEL": "claude-opus-5-5", "GOOSE_MODE": "auto"} { + if v, _ := edit.GetYAMLTop(a.Path, k); v != want { + t.Fatalf("%s = %q after the round trip:\n%s", k, v, readFile(a.Path)) + } + } + if _, err := os.Stat(gooseProviderPath(a.Path)); err == nil { + t.Fatal("magpie's custom provider left") + } +}