agent: switched off and on again, an agent is back on the magpie models it was on, and Goose keeps its own model through a round trip — found running connect/disconnect twice for every agent against real configs in a sandbox HOME: turning one on again picked magpie's default instead of what the user had picked (OpenCode's model and small_model, Codex's model and effort, Pi, dsh, Hermes, Grok), and Goose lost GOOSE_PROVIDER/GOOSE_MODEL (its field clears both keys; on main too). Disconnect now stashes what it took off (<id>.reconnect: the values, which were routed through magpie, and what it left), and Connect puts them back when the agent is still as Disconnect left it — models on magpie first, so Gemini's model signs it in before its provider, and a value the model brought along (Codex's effort) is recorded as magpie's; changed since, Connect picks as before. Connect stashes what the agent was on (<id>.connect.was), and Disconnect sets back a field it left empty when that was the user's own (Goose's anthropic/claude-opus-5-5). TestReconnectKeepsTheLastPick, TestGooseRoundTripKeepsItsOwnModel and the new reconnect check in TestGeminiConnectRoundTrip fail on the old code and pass. Sandbox matrix: all 20 agents with a switch connect, disconnect and reconnect twice with the right state, every disconnect preview line is in the real change; 13 CLIs (claude, codex, gemini, opencode, pi, goose, crush, droid, grok, cline, omp, agy, commandcode) answer through the sandbox magpie.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user