diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 4c7bd1a38..6a6d95f08 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -36,7 +36,7 @@ something else shows up as drift. ## Runtime path -1. **Connect.** The Agents page's switch calls `ConnectHow` (`POST /api/agents/{action}/{id}` in [`internal/gui/api.go`](../../internal/gui/api.go)). It stashes the current values (`.connect.was`). It brings back the models used before a switch-off (`reconnect`), or calls `Join` where the agent keeps its own model (Codex signed in with ChatGPT). `reconnect` calls `Join` first too, so a Codex switched off on a routing group comes back joined, as the switch first connected it: put back on the group alone it was beside its sign-in only, and an own model picked on the Agents page took it off magpie (NancyTom18 on X, `TestCodexReconnectStaysJoined`). Otherwise it picks a model by the order `ConnectHow` documents and sets it through `Apply`. +1. **Connect.** The Agents page's switch calls `ConnectHow` (`POST /api/agents/{action}/{id}` in [`internal/gui/api.go`](../../internal/gui/api.go)). It stashes the current values (`.connect.was`). It brings back the models used before a switch-off (`reconnect`), or calls `Join` where the agent keeps its own model (Codex signed in with ChatGPT). `reconnect` calls `Join` first too, so a Codex switched off on a routing group comes back joined, as the switch first connected it: put back on the group alone it was beside its sign-in only, and an own model picked on the Agents page took it off magpie (NancyTom18 on X, `TestCodexReconnectStaysJoined`). One that can't join then (a provider of the user's own, magpie API) is connected as at first unless its own model was one of magpie's: its memories' or subagents' model put back alone read as connected while its own model stayed off magpie (#3868, `TestCodexOffAndOnJoinsAgainWithItsHelpersOnMagpie`). Otherwise it picks a model by the order `ConnectHow` documents and sets it through `Apply`. 2. **Pick.** `Pick` sets a field from the model picker. Picking one of magpie's models on an agent that isn't connected connects it first. Each row of a model picker that a provider served with a timed reply this week says how fast: tokens a second, or the first token's wait when only that was timed (`speedTag` in app.js, from `GET /api/model-speeds`, `modelSpeeds` in [`internal/gui/speeds.go`](../../internal/gui/speeds.go): the usage ledger's mean TTFT and decode speed by `provider/model`, the catalog ref an option carries). Under a search, rows that match equally go fastest first by about how long a 200-token reply takes (`speedRank`), the untimed last, so a model several providers serve lists the fastest one first (axuhere on X). Without a search the rows keep catalog order, grouped by provider. A routing group that always asks the fastest provider is the Speed first strategy ([gateway routing](gateway-routing.md)). 3. **Notice.** After a change the page shows `Notice` when the agent reads its config only at start-up and needs a restart. A field that is `Live` (`Field.Live`: magpie's own setting, which the gateway reads on each request, Claude Desktop's `subagent_model` and `subagent_effort`) needs no restart, so `POST /api/set` and `magpie ` leave the Notice out after it, unless the pick connected the agent too (`Pick` on an agent not connected). A stopped WSL distro's copy of a field keeps the flag (`asleep`). 4. **Drift.** As the Agents page is drawn, `Drift` compares the config with `applied.json`. It runs `Check` first, then each field. An agent pointed at magpie through a reverse proxy is still on magpie (#1985, davisjiahao: a company's nginx at `https://ai.corp.example/magpie`). `wiringOff`, where most agents' `Check` compares a URL, and the checks of their own (Claude Code's managed settings, Reasonix's provider) take a URL that isn't magpie's when it ends with magpie's path, the rest is an http or https address on another machine (`onAnotherMachine`) or one on this machine's loopback with a path of its own (`loopbackWithPath`: the reporter's compliance proxy at `http://127.0.0.1:8788/claude-code-anthropic/`; `proxyBase`), and `GET /` there answers as the gateway's own `GET /` does (`{"name":"magpie"}`, `magpieAt` in [`reach.go`](../../internal/agent/reach.go), tried in the background like `reachProbe`). Something else there (a relay CC Switch put in) is `unwired`, as is an address on this machine's loopback without a path, which is magpie's on an older port and is never tried. Until the answer is known it isn't drift, so Reconnect isn't offered and doesn't write the proxy away; port moves (`OnGateway`, `Rewire`) re-set only `unwired` agents, so they leave it too. Codex keeps such an address by magpie's key, as for #816 (`codexKeptGateway`: http or https, any port, a path before magpie's, loopback with a path). A model picked in Claude Code keeps it too once magpie has answered there (`surelyMagpie`; `routed` and `gwAt` in `claude.go`), so a proxy every request must go through isn't bypassed; at a relay, a pick points Claude Code at this magpie. Every write to Claude Code (a field's `Set`, `Sync`, `Unwire`) asks such an address first where magpie hasn't answered yet, and waits (`claudeAsk`, `magpieAsked`, as `codexProxyOK` does): unasked, it read as a relay, and the first write in a magpie just started (the CLI's: a pick, the `Sync` a provider added runs, Disconnect) wrote the proxy away, turned the auto mode review back on, or left magpie in and forgot the endpoint the user had. `Follow` writes nothing until the answer is in, and reads never wait. Claude Code's address is asked only while what it sends is for magpie (`claudeProxied`, `claudeKeyed`: a key the gateway takes, or none where it keeps its claude.ai sign-in through magpie): with a key of the user's own it is their endpoint, never asked, and the drift a model of magpie's name there shows says that address, or an administrator's in managed settings, as it is (`whatsOff`; zhangt2333, #3480: magpie sent `GET /` about every 30 s to the `ANTHROPIC_BASE_URL` of a Claude Code it never connected). Tests stub `magpieAt` in `TestMain` as known and not magpie. When the config is right and points at an address off loopback (a WSL twin under NAT, or this machine's Codex at an address the user kept, #816), it tries that address ([`reach.go`](../../internal/agent/reach.go)): in the background, at most every 20 s, never waited for. When nothing answers there while the gateway answers on loopback, the drift is `unreachable` (#1013), with what has to listen there: Share on local network, the user's portproxy, Windows' firewall. Then it checks whether the agent's latest use (`LastUsed`, `Reached`) reached the gateway: a request Codex's log says went to the host its config keeps magpie at, or one the gateway saw from it after (`lastSeen`), reached it. diff --git a/internal/agent/applied.go b/internal/agent/applied.go index 65b938f29..0b8fad061 100644 --- a/internal/agent/applied.go +++ b/internal/agent/applied.go @@ -950,11 +950,19 @@ func (a *Agent) reconnect(now map[string]string) (bool, error) { // alone, Codex was beside its sign-in and not joined, so picking one // of its own on the Agents page took it off magpie, every OpenAI model // then said direct, and after a group again they said via magpie - // though a pick still went straight to OpenAI (NancyTom18 on X) + // though a pick still went straight to OpenAI (NancyTom18 on X). One + // that can't join now (a provider of the user's own, magpie API) is + // connected as at first unless its model was one of magpie's, which + // routes it: its subagents' or memories' model put back alone read as + // connected while its own model stayed off magpie (#3868) if a.Join != nil { - if _, err := a.Join(); err != nil { + ok, err := a.Join() + if err != nil { return true, err } + if !ok && !slices.Contains(rec.Routed, a.Fields[0].Key) { + 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 diff --git a/internal/agent/connect_pick_test.go b/internal/agent/connect_pick_test.go index 9cf234aa5..f6a55bc5c 100644 --- a/internal/agent/connect_pick_test.go +++ b/internal/agent/connect_pick_test.go @@ -160,6 +160,86 @@ func TestCodexConnectKeepsItsPick(t *testing.T) { } } +// Codex with its subagents or memories on one of magpie's, switched off and +// on again, is as it was: joined again on its own model, its config and +// those fields back (#3868, why8023). Put back alone, the subagents' model +// read as connected with no openai_base_url, and the memories and [agents] +// model couldn't be set at all. A Codex that switch-on left so gets out of +// it by switching off and on again. On one of magpie's models it is joined +// again too, beside its sign-in; on magpie API, which can't join, that model +// takes it back. +func TestCodexOffAndOnJoinsAgainWithItsHelpersOnMagpie(t *testing.T) { + for _, c := range []struct { + name string + login string // its sign-in field: "" ChatGPT, "api" magpie API + model string // one of magpie's it is on, "" for its own gpt-5.5 + fields []string + left bool // as the switch-on before #3868 left it + }{ + {"subagent model", "", "", []string{"subagent_model"}, false}, + {"memories", "", "", []string{"memories"}, false}, + {"subagents", "", "", []string{"subagent"}, false}, + {"subagents' model and memories", "", "", []string{"subagent_model", "memories"}, false}, + {"left unjoined", "", "", []string{"subagent_model"}, true}, + {"on one of magpie's", "", "fake/m1", []string{"subagent_model", "memories"}, false}, + {"magpie API", "api", "fake/m1", []string{"subagent_model", "memories"}, false}, + } { + t.Run(c.name, func(t *testing.T) { + home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, + "model = \"gpt-5.5\"\nmodel_reasoning_effort = \"xhigh\"\n") + cx := codex(home) + if c.login != "" { + if err := cx.Apply("login", c.login); err != nil { + t.Fatal(err) + } + } + if err := cx.Connect(); err != nil || cx.Joined() != (c.login == "") { + t.Fatalf("connect (%v), joined %v:\n%s", err, cx.Joined(), read()) + } + if c.model != "" { + if err := cx.Apply("model", c.model); err != nil { + t.Fatal(err) + } + } + for _, k := range c.fields { + if err := cx.Apply(k, "fake/m1"); err != nil || cx.Values()[k] != "fake/m1" { + t.Fatalf("%s on %q (%v)", k, cx.Values()[k], err) + } + } + cfg, joined, beside := read(), cx.Joined(), cx.Beside() + if err := cx.Disconnect(); err != nil { + t.Fatal(err) + } + if c.left { + // that switch-on: the record taken, the subagents' model put + // back, nothing else + forget(cx.ID+".reconnect", cx.ID+disconnectedKey) + if err := cx.Apply("subagent_model", "fake/m1"); err != nil { + t.Fatal(err) + } + if !cx.Wired() || cx.Joined() || strings.Contains(read(), "openai_base_url") { + t.Fatalf("not as #3868 found it (wired %v, joined %v):\n%s", cx.Wired(), cx.Joined(), read()) + } + if err := cx.Disconnect(); err != nil { + t.Fatal(err) + } + } + how, err := cx.ConnectHow() + if err != nil || how.How != "again" || read() != cfg || cx.Joined() != joined || cx.Beside() != beside { + t.Fatalf("on again (%+v, %v), joined %v and beside %v, were %v and %v:\n%s\nwas\n%s", how, err, cx.Joined(), cx.Beside(), joined, beside, read(), cfg) + } + for _, k := range c.fields { + if v := cx.Values()[k]; v != "fake/m1" { + t.Errorf("%s back on %q", k, v) + } + } + if d := cx.Drift(); d != nil { + t.Errorf("drift: %+v", d) + } + }) + } +} + // 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 @@ -359,3 +439,40 @@ func TestConnectSaysHow(t *testing.T) { t.Fatalf("on a model magpie lacks: %+v %v", how, err) } } + +// A Codex on a provider of the user's own, back on its own model with only +// its subagents' model on magpie, switched off and on again can't be +// joined: its own model goes to its provider. Put back alone, the +// subagents' model read as connected (「已接入」) while Codex's own turns +// never reached magpie (#3868, the case fae438b4a left). It is connected +// as at first instead, on one of magpie's models. +func TestCodexOwnProviderOffAndOnIsNotConnectedByItsHelpers(t *testing.T) { + home, read := codexHome(t, `{"OPENAI_API_KEY":"sk-relay"}`, + "model = \"gpt-5.5\"\nmodel_provider = \"relay\"\n\n[model_providers.relay]\nname = \"relay\"\nbase_url = \"https://relay.example/v1\"\nwire_api = \"responses\"\n") + cx := codex(home) + if err := cx.Connect(); err != nil { + t.Fatal(err) + } + if err := cx.Apply("subagent_model", "fake/m1"); err != nil { + t.Fatal(err) + } + if err := cx.Apply("model", "gpt-5.5"); err != nil { + t.Fatal(err) + } + if cx.Routed() || cx.Values()["subagent_model"] != "fake/m1" { + t.Skipf("not the reporter's state (routed %v, subagents on %q):\n%s", cx.Routed(), cx.Values()["subagent_model"], read()) + } + if err := cx.Disconnect(); err != nil { + t.Fatal(err) + } + how, err := cx.ConnectHow() + if err != nil { + t.Fatal(err) + } + if how.How == "again" && !cx.Routed() { + t.Fatalf("on again (%+v) with its own model off magpie:\n%s", how, read()) + } + if !cx.Routed() || !isMagpie(cx.Values()["model"]) { + t.Fatalf("connected (%+v) on %q, routed %v:\n%s", how, cx.Values()["model"], cx.Routed(), read()) + } +}