Files
magpie/internal/agent/applied.go
T
JoeyWu-tech cff2852ca8 agent: Claude Code's own endpoint is never asked whether it is magpie (#3480)
zhangt2333 (#3480): magpie sent GET / (Go-http-client/1.1) about every
30s to the ANTHROPIC_BASE_URL in ~/.claude/settings.json of a Claude
Code it never connected, with nothing shown and nothing to turn it off.
Every read of Claude Code the Agents page makes (/api/state, /api/drift
each 15s) goes through routed(), which asked a base URL that can be
another way to magpie (proxyBase: an address on another machine, or one
on loopback with a path of its own) whether it answers as magpie, for
#1985's reverse proxy, whatever key Claude Code sends there. One read
pass asked it about 65 times; the probe cache sends that as one GET once
its answer is older than 20s (reachAge), every 30s on the page's poll.

The address is now asked only while what Claude Code sends is for magpie
(claudeProxied, claudeKeyed): a key the gateway takes (keyHolds, as
Check already asks of ANTHROPIC_AUTH_TOKEN), or none where it keeps its
claude.ai sign-in through magpie (claude.login, on a Claude Code magpie
wired, wired_at, which Disconnect forgets, where the gateway takes any
key). With a key of the user's own, the base URL is their endpoint, and
nothing asks it: the reads (routed), a pick (gwAt), Check (its managed
settings' and settings.json's URL), and a request's stand-ins
(claudeStandInAt, now given the place, and claudeClassifierIn), a WSL
distro's Claude Code's too. Check says such a Claude Code's address as
it is, unasked (whatsOff: wiringOff's words, without its probe; an
administrator's in managed settings too): with a model named as one of
magpie's there, as a relay can name its own, "Claude Code's
ANTHROPIC_BASE_URL (settings.json) is <its endpoint>, not magpie's
gateway at <the gateway>", or "is gone" with none, which main said of
it too once the address had answered as not magpie. #1985's proxy keeps
magpie's key, so it is asked and kept as before.

The key, not a record of what magpie wrote: wired_at is from 10-08
(#1218), so a Claude Code connected before it and not picked since has
none, and a Claude Code magpie wired and then moved to a relay of the
user's own (CC Switch, its own key) would still have that relay asked.
The claude.ai sign-in can lean on it: it came with it (#1218), and its
setting, on a routed Claude Code only, stamps it.

Before and after, for a read, a pick or a request's stand-in while
ANTHROPIC_BASE_URL is not the gateway's and proxyBase takes it:
  before: GET <base>/ in the background, at most every 20s
  after: the same while Claude Code sends a key the gateway takes or
  keeps its claude.ai sign-in through magpie; otherwise never, and the
  address is not magpie's
Changed with it: a Claude Code pointed by hand at a proxy to magpie with
a key of its own, drift on main too (its key "was changed"; now its
address), is no longer taken as routed there, so a pick writes magpie's
address and key, keeping its own for Disconnect, where it kept the
proxy.

Not changed: the other agents ask an address only from magpie's own
entry in their config, or once on one of magpie's models (T3 Code's
wired, Gemini CLI's isMagpie(model), Hermes's onMagpie and
providers.magpie, dsh's usesMagpie), so none asks one of the user's own.
A Claude Code magpie wired keeping its claude.ai sign-in, then moved by
hand to an endpoint of the user's own with an ANTHROPIC_API_KEY alone
and not disconnected, is still asked, as before: magpie reads no
ANTHROPIC_API_KEY of Claude Code's.

Cost: a never-connected Claude Code with its own key at such an address,
the case this reaches: a read pass took 5.5 to 6.0ms on origin/main and
9.4 to 9.8ms with this (three runs each, taking turns, 200 passes, at
load 46 to 50; the key's read and access.Authenticate at ~65 calls a
pass), on the 15s drift poll. A connected Claude Code (the gateway's
address) and one on Anthropic's own endpoint (no base URL) never get
there.

docs/subsystems/agent-wiring.md (Drift) says it.

What was run (tree c7f75f7a, on origin/main 74dcf40a, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077:
  magpie web --no-open, built -tags nogui from origin/main and from
  this, each in a sandbox HOME of its own (env -i), with a settings.json
  on a local relay that logs what it gets
  (http://127.0.0.1:<port>/corp-relay, a key of its own) and listens
  before magpie starts, /api/drift read every 5s:
    model opus, 120s: main, GET /corp-relay/ (Go-http-client/1.1) every
    20s from the first read, 6 in all; this, none. Neither touched
    settings.json.
    model fakeco/fake-1 (a provider magpie has), 60s: main, 3 GETs;
    this, none. Both said "Claude Code's ANTHROPIC_BASE_URL
    (settings.json) is http://127.0.0.1:<port>/corp-relay, not magpie's
    gateway at http://127.0.0.1:<port>" from the first read.
    Run once more with magpie up before the relay listened: main 5 GETs
    and 2, its first ask made before then, this none, the rest the
    same.
  magpie claude fakeco/fake-1, then its ANTHROPIC_BASE_URL moved by hand
  to the relay answering GET / as magpie, magpie's key kept, 60s: both
  asked it every 20s, 3 GETs, no drift. Both wrote settings.json again
  at start with every key and value the same (a label's "·", which the
  hand edit had escaped, now as is). Run once more with magpie up
  before the relay listened: both drifted "… is <the relay>, not
  magpie's gateway …" on the first read and took
  CLAUDE_CODE_AUTO_MODE_SERVER out of settings.json, then asked it
  every 20s with no drift, the address kept; main does it the same, not
  this change.
  TestClaudeOwnEndpointNeverAsked (nginx: its own key in
  ANTHROPIC_AUTH_TOKEN; local: in ANTHROPIC_API_KEY) and
  TestWSLClaudeOwnEndpointNeverAsked: pass. Broken by hand, each red:
    claudeProxied without claudeKeyed: "never connected: asked
    [https://relay.example/anthropic https://relay.example/anthropic …]"
    (and the WSL test)
    Check's addresses asked whatever the key (keyed true): "a model of
    magpie's name: asked [https://relay.example/anthropic]"
    Check's address unsaid without magpie's key: "its own key, a model
    of magpie's name: … ANTHROPIC_AUTH_TOKEN (settings.json) was
    changed, so magpie's gateway won't take its requests" (nginx), "… is
    gone, so Claude Code sends magpie its claude.ai sign-in …" (local)
    the administrator's address asked whatever the key: "its own key, an
    administrator's proxy: … ANTHROPIC_BASE_URL (settings.json) is
    https://relay.example/anthropic, not magpie's gateway …"
    claudeStandInAt, then claudeClassifierIn, on surelyMagpie alone:
    "never connected: asked [https://relay.example/anthropic]" (and the
    WSL test)
    claudeKeyed with no claude.ai branch: "the claude.ai sign-in at the
    proxy: drift … ANTHROPIC_BASE_URL (settings.json) is <the proxy>,
    not magpie's gateway …" (and the WSL test)
    its claude.ai branch without wired_at: local only, "asked after
    Disconnect: [https://relay.example/anthropic …]"
    its claude.ai branch on this machine's stash keys: the WSL test
    alone, the same as with no claude.ai branch
  agent-wiring.md's reverse proxy checks (TestReverseProxy…,
  TestClaudeBehindLocalProxyKeepsIt, …), Drift, the sign-in's and
  TestKeptAddress: ok, but for TestWSLColdPlaceOpensNothing (e54a7d50,
  #3896: a stopped distro's rows read none of its files), red at
  origin/main and with this change alike (#3924): the same eight looks
  under .hermes/profiles, none under .claude
  gofmt -l of every .go file: nothing. GOOS=linux, windows and darwin
  go vet -tags nogui ./..., GOOS=windows go vet ./...: exit 0.
  GOOS=linux and darwin go build -tags nogui ./..., GOOS=windows go
  build ./...: exit 0. Untagged linux vet and build stop at pkg-config
  (no libsoup-3.0 or gio-unix-2.0 here)
  full suite, go test -tags nogui -count=1 -timeout 45m ./... (8m19s,
  load 42 at its start, Bun 1.4.2 on PATH): 68 packages ok, reds in 8:
    internal/agent TestInstalledVersion,
    TestCursorFolderFromTheEditorIsNotTheCLI,
    TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
    TestQoderSettingsFromAnIDEPluginIsNotTheCLI and
    TestOmpDetectedWithPiDir (#3141); the two Reasonix tests (#3641);
    TestCodexDriftReached (#3398); TestWSLColdPlaceOpensNothing (#3924,
    above)
    internal/edit TestWriteAtomicReadOnlyHardLink and internal/testenv
    TestProgram, umask 077 (#3640)
    internal/gateway TestPluginToldWhereTheAgentWorks (#3675)
    internal/gui TestGatewayHeldByAContainer and
    TestGatewayFixQuitsOnlyAMagpie (#3649)
    internal/proc TestEnvOfChild (#3906)
    internal/provider: "fatal error: close of synctest channel from
    outside bubble" in TestUnreadLoginsWriteErrorsKeepCredentials
    (#3632), which ends the package's run
    internal/tui TestTUIFetchesSubscriptionModels/agent (#3919)
  On origin/main 1a804859, where #3924's fix is in, with this commit
  cherry-picked (a scratch worktree, not this tree): go test
  ./internal/agent has the reds 1a804859 has alone, #3141's, #3398's
  and #3641's, and TestWSLColdPlaceOpensNothing passes.
  TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestInstalledVersion and
  TestZedAppPath come and go here: -count=5, 12 of 15 red with this
  and 11 of 15 at 1a804859.
Not tried: macOS, Windows, a real WSL distro (its Claude Code's stash
keys are only tested), Claude Code itself sending through the relay, a
company's real proxy.

Landed by yetone in a batch of Coffemon's patches from the issues.
What was run, go1.26.8, on origin/main cc81ad4ea, then again at
70a0c387c (gofmt, the three vets, the changed tests, umask 077):
- mote (macOS 27): gofmt -l clean; go vet, GOOS=linux go vet -tags
  nogui and GOOS=windows go vet ./...: ok. edit and testenv at umask
  077; agent, provider, library and gateway with an empty .git above
  TMPDIR; davsync, imagemcp, plugin, library, edit, testenv whole; the
  changed tests in agent, provider, gateway and gui; the timing tests
  -count=10 (gateway, davsync, imagemcp) and -count=5 (agent): all ok.
  TestCoreScriptBudget ok: core 1,536,751 of 1,537,034, app.js
  1,312,975 of 1,315,316, no budget raised.
- Red on the old code: on origin/main, TestWriteAtomicReadOnlyHardLink
  at umask 077 and two Reasonix tests under an empty .git; on Linux
  (wsl-run) TestProgram at umask 077 with the old program.go. With
  each fix's code taken back to main's, the Gemini imageConfig tests,
  TestPluginToldWhereTheAgentWorks, TestSkillRepoTraced,
  TestTaggedHooksAlone, TestMainURL/TestMainView and update-auto,
  plugin-github-hooks, plugins-more-discover, plugin-namesake were
  red. Broken by hand: claudeKeyed always true (both
  OwnEndpointNeverAsked tests red), claudeAsk a no-op
  (TestClaudeFirstWriteAsksTheProxy red), GitAt taking any .git folder
  (the three TestGitOnDisk tests red).
- GUI: 123 files (plugins*, update-*, panel/tray/start/context, i18n,
  gui-zh-tw/ja/de and every file that evals app.js parts) in WebKit on
  Linux (wsl-run). 11 files had reds (click-scroll, panel-fold,
  plugin-card-update, plugin-github, plugin-move*, segs-glide,
  tray-totals, update-*), and run again side by side they fail the
  same on origin/main d80d75f30 (79 pass, 26 fail here; 78 and 27
  there). They are click-scroll guards ("the click must not scroll
  the page"), varying by language from run to run (#4385).
  copilot-ghe's route-after-close red is #1949, seen on main 244d80e23
  too. In Chromium on mote, Chromium segfaulted partway through files
  (#4125): context-map, context-scroll-end and context-tune fail the
  same on origin/main 0911a65e7, so the batch's own files
  (plugin-namesake, update-auto, plugins-more-discover,
  plugin-github-hooks) ran in Chromium on Linux: 62 of 62.
- Full suite (go test -tags nogui -count=1 -timeout 45m ./..., mote)
  at 472a0608f (on 70a0c387c): every package ok but internal/gateway,
  whose five reds were #4362's (Antigravity turned-away and
  TestCodexSpentGrokFailingIsTold), fixed on main in d80d75f30. After
  the rebase on 244d80e23 the five pass, and gofmt, the three vets
  and the targeted runs above were run again: ok. In Linux the
  sign-in, copilot, plugin-github*, plugins-more-discover,
  plugin-namesake, plugin-gateway, update-auto, quota-refresh-order
  and i18n files ran in both engines: 475 pass, 4 fail (copilot-ghe
  #1949, plugin-github's click scroll, both red on main too).
- Rebased again on c7171a256, over its budget fix e94bf7a08: gofmt, vet of
  gui, gateway, provider and plugin, TestCoreScriptBudget (core
  1,536,265 of 1,536,586 with the batch's last commit, statusTErr and settingsErr), the Gemini,
  GitOnDisk and plugin tests, and in Linux both engines i18n-keys,
  update-mirror, plugin-namesake, plugins-more-discover,
  plugin-github-hooks and update-auto (138 of 138): ok.
Not tried: the whole GUI suite (mote's WebKit wouldn't launch and its
Chromium segfaulted, #4125); macOS WebKit; the real app's window.
2026-10-30 15:53:22 +08:00

1034 lines
36 KiB
Go

package agent
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/lastgood"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// An agent's config is a file anyone can write: another switcher, an
// installer, the agent's own setup. One that takes magpie out leaves the
// row showing a magpie model while the agent asks its own vendor for it, and
// the user blames magpie. So magpie remembers what it last set on each agent
// (applied.json, beside the stash) and says when that no longer holds —
// Drift — with the way to set it again.
var appliedMu sync.Mutex
// applied is what magpie last set on one agent, and when.
type applied struct {
At time.Time `json:"at"`
Fields map[string]string `json:"fields"` // field key → value
// Kept: the user took the agent's config as it was (Keep) while magpie
// had it set up, which still says magpie set it up (setUp)
Kept bool `json:"kept,omitempty"`
}
func appliedPath() string { return filepath.Join(filepath.Dir(provider.Path()), "applied.json") }
// appliedLoad is agent id → what magpie set on it.
func appliedLoad() map[string]applied {
out := map[string]applied{}
if b, err := os.ReadFile(appliedPath()); err == nil {
json.Unmarshal(b, &out)
}
return out
}
// appliedSave writes m as applied.json. It holds what magpie set on every
// agent, and appliedLoad reads a file that doesn't parse as empty: such a
// file is copied aside first, and one that can't be copied isn't written
// over (#2263).
func appliedSave(m map[string]applied) {
if err := lastgood.KeepUnparsed(appliedPath(), lastgood.Into[map[string]applied]); err != nil {
log.Printf("applied.json wasn't written over: %v", err)
return
}
b, _ := json.MarshalIndent(m, "", " ")
os.MkdirAll(filepath.Dir(appliedPath()), 0o755)
os.WriteFile(appliedPath(), b, 0o600)
}
func appliedOf(id string) applied {
appliedMu.Lock()
defer appliedMu.Unlock()
return appliedLoad()[id]
}
// record keeps what a field reads after magpie set it; a field put back to
// the agent's default is magpie's no longer.
func record(id, key, v string) {
appliedMu.Lock()
defer appliedMu.Unlock()
m := appliedLoad()
a := m[id]
if a.Fields == nil {
a.Fields = map[string]string{}
}
if v == "" {
delete(a.Fields, key)
} else {
a.Fields[key] = v
}
a.At = time.Now()
m[id] = a
appliedSave(m)
}
// Apply sets one of the agent's fields and remembers it as magpie's, so it
// can be told apart from what something else writes there later. Every
// setting of a field by the user goes through here. A field magpie set
// before that this one moves too is magpie's doing as well: Claude Code's
// Default takes its tiers and subagent model out with magpie's endpoint,
// which read as changed outside magpie, and the drift kept the row up
// among the connected ones while it said Not connected (#834).
func (a *Agent) Apply(key, v string) error {
if a.Native != nil {
return a.Native.Apply(key, v)
}
f := a.Field(key)
if f == nil {
return nil
}
// a found group named by its old id is written as it is listed now,
// auto/<model> (autoref.go)
if pub, ok := a.publicValue(v); ok {
v = pub
}
was := a.Values()
if err := f.Set(v); err != nil {
return err
}
now := a.Values()
for k := range appliedOf(a.ID).Fields {
if k != f.Key && now[k] != was[k] {
record(a.ID, k, now[k])
}
}
record(a.ID, f.Key, f.Get())
// magpie picked in by the user's own hand (magpie model, a pick in
// the CLI) connects it again
if a.Wired() {
forget(a.ID + disconnectedKey)
}
return nil
}
// Drift is how an agent differs from what magpie set on it.
type Drift struct {
// Kind says what is off:
// "unwired" the model is magpie's but the config no longer sends it
// through magpie (Check);
// "replaced" a magpie model magpie set was replaced by the agent's own;
// "bypassed" the config is right, yet the agent was used since and
// nothing of it reached the gateway — it runs on an old
// config, or something outside the file overrides it;
// "unreachable" the config is right, but the address off loopback
// it names the gateway at doesn't answer (#1013);
// "proxied" the config is right, but the agent sends its requests
// for the gateway through a proxy (Addr) that doesn't
// pass them on to it (codex_proxy.go).
Kind string `json:"kind"`
Field string `json:"field"` // the field it shows on
Now string `json:"now,omitempty"` // what that field says now
Want string `json:"want"` // what setting it again sets
Detail string `json:"detail"` // what exactly is off, for a tooltip
// Addr is where an unreachable agent is pointed; Move, when set, the
// address WSL reaches Windows at now, which Reapply points it at.
Addr string `json:"addr,omitempty"`
Move string `json:"move,omitempty"`
}
// started is when this process — and the gateway in it — came up: before
// then, a request the gateway missed says nothing about the agent.
var started = time.Now()
// Drift says what, if anything, keeps the agent off what magpie set: its
// config first (wiring, then each field against magpie's record), then —
// the config being right — whether its latest use actually came through.
// A field moved from one magpie model to another (the agent's own picker)
// isn't drift; one moved off magpie is.
func (a *Agent) Drift() *Drift {
if a.Native != nil {
s := a.Native.Read()
if s.Provider == "invalid" {
return &Drift{Kind: "unwired", Field: "model", Detail: s.Detail}
}
return nil
}
if len(a.Fields) == 0 || a.theInstalled() {
return nil
}
vals := a.Values()
// the field on one of magpie's models: where drift shows, and what
// setting it again sets
on, onMagpie := a.Fields[0], false
for _, f := range a.Fields {
if magpieValue(a, f, vals[f.Key], vals) {
on, onMagpie = f, true
break
}
}
// a provider named magpie the user wrote by hand is theirs to keep as
// they like: there is nothing magpie set to drift from, and Reapply
// would write magpie's over it
if onMagpie && a.handWired() {
return nil
}
if a.Check != nil {
if d := a.Check(); d != "" {
return &Drift{Kind: "unwired", Field: on.Key, Now: vals[on.Key], Want: vals[on.Key], Detail: d}
}
}
rec := appliedOf(a.ID)
// still joined, a model off magpie is one picked in the agent, beside
// magpie's in its list (dsh's /model), not a change from outside; so is
// one Codex picks beside its ChatGPT sign-in, which still goes through
// magpie (#2424: the row went red with Reconnect)
joined := a.Joined != nil && a.Joined()
picked := joined || a.Beside != nil && a.Beside()
for _, f := range a.Fields {
want, ok := rec.Fields[f.Key]
// a field that reads empty while it follows another (Claude Code's
// tiers and subagents on its main model) runs on that one's model:
// the main model moved onto the one magpie set the field to reads
// as following it, not as the field put back to the agent's own
// default (#1050)
now := vals[f.Key]
if now == "" && f.Follows != "" {
now = vals[f.Follows]
}
if !ok || picked || now == want || !magpieValue(a, f, want, vals) || magpieValue(a, f, now, vals) || sameGroup(want, now) {
continue
}
return &Drift{Kind: "replaced", Field: f.Key, Now: vals[f.Key], Want: want,
Detail: a.Name + "'s config was changed outside magpie: " + f.Label + " is " + orDefault(vals[f.Key]) + ", not " + want + " as magpie set it"}
}
if onMagpie || joined {
if d := a.unreachable(on, vals[on.Key]); d != nil {
return d
}
if d := a.proxied(on, vals[on.Key]); d != nil {
return d
}
}
if a.Reached != nil && onMagpie {
if at, to, refused := a.Reached(rec.At); !at.IsZero() {
switch {
// a request the gateway saw since, or one to where the config
// keeps magpie (Codex's kept address, a reverse proxy, #1985),
// came through
case !sameHost(to, gateway.URL()) && !sameHost(to, a.pointedAt()) && lastSeen(a.ID).Before(at.Add(-2*time.Second)):
return &Drift{Kind: "bypassed", Field: on.Key, Now: vals[on.Key], Want: vals[on.Key],
Detail: a.Name + "'s last request (" + at.Format("15:04") + ") went to " + hostOf(to) + ", not magpie — it was started before magpie set it up and still runs on its old config: quit and reopen it"}
case refused:
return &Drift{Kind: "bypassed", Field: on.Key, Now: vals[on.Key], Want: vals[on.Key],
Detail: a.Name + " couldn't reach magpie at " + at.Format("15:04") + " — magpie wasn't running then, so it has none of magpie's models: quit and reopen it"}
}
}
}
if a.LastUsed != nil && onMagpie {
if used := a.LastUsed(); bypassed(used, rec.At, lastSeen(a.ID)) {
return &Drift{Kind: "bypassed", Field: on.Key, Now: vals[on.Key], Want: vals[on.Key],
Detail: a.Name + " was used at " + used.Format("15:04") + " but none of its requests reached magpie — one started before magpie set it up still runs on its old config: restart it"}
}
}
return nil
}
// installedURL is the gateway of the magpie people install, on its own
// port, the one its Settings have; a magpie MAGPIE_ADDR puts on another
// (magpie-dev, a sandbox) is run beside it.
var installedURL = gateway.SavedURL
// elsewhere: this magpie is not on the installed one's port.
func elsewhere() bool { return !sameHost(gateway.URL(), installedURL()) }
// theInstalled: this magpie runs beside the installed one, and the agent is
// wired to that one's gateway, not this one's — the installed magpie set it
// up, so it is that one's to check, not drift here. Otherwise the two would
// take turns flagging each other's wiring.
func (a *Agent) theInstalled() bool {
if !elsewhere() || a.Path == "" {
return false
}
b, err := edit.Read(a.Path)
if err != nil {
return false
}
return strings.Contains(string(b), hostOf(installedURL())) && !strings.Contains(string(b), hostOf(gateway.URL()))
}
// bypassed: the agent was used — while this gateway was up and after magpie
// last set it — and no request of it arrived since. A request leaves within
// moments of the prompt; a little grace keeps one in flight from counting.
// lastSeen is when the agent's last request reached this process's gateway
// (usage.LastSeen). What usage.Saw records lasts as long as the process, so
// a test that stands for a request gives its own here.
var lastSeen = usage.LastSeen
func bypassed(used, applied, seen time.Time) bool {
const grace = 30 * time.Second
return !used.IsZero() && used.After(started) && used.After(applied) &&
time.Since(used) > grace && seen.Before(used.Add(-2*time.Second))
}
// magpieValue: the value is one of magpie's models as this agent spells it,
// its suffix after the model (SplitSuffix) aside; a list of models is the
// user's own.
func magpieValue(a *Agent, f Field, v string, vals map[string]string) bool {
if v == "" {
return false
}
v, _, one := a.split(v)
if !one || a.Spelled != nil && !a.Spelled(v) {
return false
}
if isMagpie(v) {
return true
}
// spelled as magpie's provider in the agent's own config (OpenCode's
// magpie/…): magpie's even when its list there doesn't have the model
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
return true
}
if f.Options == nil {
return false
}
for _, o := range f.Options(vals) {
if o.Value == v {
return o.Ref != ""
}
}
return false
}
// groupNamed is the routing group ("group/<id>") a model value reaches at
// the gateway: a group's own id, or a model's id without a provider in it
// that the gateway takes as a group's (provider.GroupFor: gpt-6.1-sol is
// group/auto-gpt-6-1-sol); "" for any other.
func groupNamed(v string) string {
// Claude Code's [1m] mark rides on the group's id as it does on a
// model's, and one of the two values here comes from the agent's own
// settings: without it off, a group read back marked is not the one
// magpie set (GroupFor takes it off; so does GroupFinder)
v = strings.TrimSuffix(strings.TrimSpace(v), "[1m]")
v = strings.TrimPrefix(v, magpieID+"/")
if provider.IsGroupRef(v) {
return provider.CanonicalRef(v) // auto/<model> and group/auto-<slug> are one group
}
g, _ := provider.GroupFor(v)
return g
}
// sameGroup: two values of a field reach the same routing group, one by
// the group's id and the other by its model's name (#750: Codex's config
// read gpt-6.1-sol where magpie had set group/auto-gpt-6-1-sol). Asked for
// either, the gateway serves the group, so the one is not the other
// replaced.
func sameGroup(a, b string) bool {
g := groupNamed(a)
return g != "" && g == groupNamed(b)
}
// setAsGroup: magpie set the agent's field to a routing group, and v names
// that group by its model's name (sameGroup) — magpie's model still, for an
// agent whose config sends v to magpie's gateway.
func setAsGroup(id, key, v string) bool {
if v == "" || isMagpie(v) {
return false
}
want, ok := appliedOf(id).Fields[key]
return ok && sameGroup(want, v)
}
func orDefault(v string) string {
if v == "" {
return "the agent's default"
}
return v
}
// Reapply sets again what magpie set on the agent: what drifted, else its
// fields as they read now — for a config taken off magpie in a way no check
// catches. A replaced field brings back the others magpie set with it.
// Either way the record is renewed, so a use before now no longer counts.
func (a *Agent) Reapply() error {
if a.Native != nil {
return a.Native.Connect()
}
d := a.Drift()
if d != nil && d.Kind == "unreachable" && d.Move != "" && a.move != nil {
return a.move(d.Addr, d.Move)
}
if d != nil && d.Kind == "replaced" {
rec := appliedOf(a.ID)
// the model first: the others (an effort) are checked against it
if err := a.Apply(d.Field, d.Want); err != nil {
return err
}
for _, f := range a.Fields {
if v, ok := rec.Fields[f.Key]; ok && f.Key != d.Field && f.Get() != v {
if err := a.Apply(f.Key, v); err != nil {
return err
}
}
}
return nil
}
vals := a.Values()
for _, f := range a.Fields {
// magpie as the agent's provider too (Claude Desktop's provider
// field, beside its tiers on magpie's models), as Wired takes it:
// left out, Reconnect set the tiers again and never the gateway
// Check found off (dumplings on Discord)
if v := vals[f.Key]; v != "" && (d != nil && f.Key == d.Field || v == magpieID || magpieValue(a, f, v, vals)) {
if err := a.Apply(f.Key, v); err != nil {
return err
}
}
}
return nil
}
// Keep takes the agent's config as it is now: what magpie set before is
// forgotten, and no longer said to have been changed. The agent stays one
// magpie set up (setUp): an entry with no fields is kept for it, so one
// whose magpie is spelled as a provider of its own config still reads as
// connected.
func (a *Agent) Keep() {
appliedMu.Lock()
defer appliedMu.Unlock()
m := appliedLoad()
if _, ok := m[a.ID]; !ok {
return
}
m[a.ID] = applied{Kept: true}
appliedSave(m)
}
// forgetSetUp is Disconnect's Keep: what magpie set is forgotten, and so is
// that magpie set the agent up at all.
func (a *Agent) forgetSetUp() {
appliedMu.Lock()
defer appliedMu.Unlock()
m := appliedLoad()
if _, ok := m[a.ID]; !ok {
return
}
delete(m, a.ID)
appliedSave(m)
}
// setUp reports whether magpie put the agent on magpie and it hasn't been
// disconnected since: a Connect, which stashes what the agent was on
// (connect.was, since 10-03, which a Keep never took away), or a field
// magpie set to one of its models or to itself (applied.json, which Keep
// keeps an entry in). A field set in magpie to a model of the agent's own,
// or its thinking level, doesn't set it up.
func (a *Agent) setUp() bool {
if stashLoad()[a.ID+".connect.was"] != "" {
return true
}
rec := appliedOf(a.ID)
if rec.Kept {
return true
}
for k, v := range rec.Fields {
// magpie/<model> as magpie wrote it, its model since gone from the
// catalog too
if f := a.Field(k); f != nil && (v == magpieID || prefixed(v) || magpieValue(a, *f, v, rec.Fields)) {
return true
}
}
return false
}
// handWired: magpie is spelled as a provider of the agent's own config
// (Spelled: Pi's magpie/<model>, OpenCode's provider at the gateway), and
// magpie never set the agent up. A provider named magpie that the user
// wrote by hand is theirs (JFunfunfun on X): the agent isn't connected,
// and magpie neither syncs its list there, renames in it, nor takes it out
// on Disconnect.
//
// An app connected natively (Aside) keeps ownership its own way: its
// provider is magpie's by the key magpie wrote in it.
func (a *Agent) handWired() bool { return a.Native == nil && a.Spelled != nil && !a.setUp() }
// Wired reports whether magpie is in the agent's config: a field on one of
// magpie's models, or on magpie itself (an app whose one setting is magpie
// as its provider). A model on a provider of the agent's own config named
// magpie counts only where magpie set the agent up (handWired).
func (a *Agent) Wired() bool {
if a.Native != nil {
return a.Native.Read().Provider == "connected"
}
// joined by a provider named magpie in the agent's own config (Hermes's
// providers.magpie, VS Code's magpie group), as a model on it is
if a.Joined != nil && a.Joined() || a.Beside != nil && a.Beside() {
return !a.handWired()
}
vals := a.Values()
for _, f := range a.Fields {
if v := vals[f.Key]; v == magpieID || magpieValue(a, f, v, vals) || a.Routed != nil && setAsGroup(a.ID, f.Key, v) && a.Routed() {
return !a.handWired()
}
}
return false
}
// Pick sets a field from the Agents page's model picker. One of magpie's
// models picked on an agent not connected yet connects it first, as its
// switch does, then starts it on that model (the owner: the switch, and a
// model picked in magpie in one click, both): the agent gets magpie's whole
// list, and Disconnect puts back what it had before.
func (a *Agent) Pick(key, v string) error {
if a.Native != nil {
value, err := a.Spell(key, v)
if err != nil {
return err
}
return a.Apply(key, value)
}
if f := a.Field(key); f != nil && !a.Wired() {
vals := a.Values()
vals[key] = v
if v == magpieID || magpieValue(a, *f, v, vals) {
if _, err := a.connect(); err != nil {
return err
}
}
}
return a.Apply(key, v)
}
// Connect puts magpie into the agent's config, the Agents page's switch
// (the owner: an agent is connected to magpie, and its models are picked in
// the agent): the agent's main field goes to one of magpie's models — the
// one serving the model it is on now, else the first — or to magpie itself
// for an app whose one setting is magpie as its provider. Setting it writes
// the provider and the whole catalog in, so the agent's own model list has
// every one of magpie's models. An agent that can keep the model it is on
// (Join) keeps it. An agent already connected is left as it is.
// NoModelsError is Connect's answer while magpie has no models to give
// the agent: no provider or subscription has been added yet. The GUI says
// it in the reader's language by its code (no_models).
type NoModelsError struct{ Agent string }
func (e *NoModelsError) Error() string {
return "Add a provider or subscription in magpie first, then connect " + e.Agent
}
// AllHiddenError is Connect's answer when magpie has models but none of
// them is left in the agent's lists: the only ones picked for it are gone
// with their provider, or every one is turned off for it (#1989). Saying
// it can't be connected sent the user looking at the agent, when its model
// list is what emptied it. The GUI says it in the reader's language by its
// code (all_hidden).
type AllHiddenError struct{ Agent string }
func (e *AllHiddenError) Error() string {
return "None of magpie's models is left in " + e.Agent + "'s model list: pick one under Model list → Pick on the Agents page, then connect " + e.Agent
}
func (a *Agent) Connect() error {
_, err := a.ConnectHow()
return err
}
// Connection is how Connect chose the model an agent starts on, for the
// Agents page to say it (#726: Codex showed its last pick, Claude Code a
// Sonnet through magpie and Antigravity some model, with nothing saying
// why). How is one of:
//
// - "kept": it was connected already, left as it was
// - "again": the models it was on when it was switched off, put back
// - "joined": the model it is on stays its own (Codex signed in with
// ChatGPT), magpie's models join its list
// - "magpie": an app whose one setting is magpie as its provider
// - "same": the model it is on now, through magpie
// - "alike": the model its alias (opus) stands for, through magpie
// - "default": on its own default with no model set: the first of its
// own models in its list (the newest, for Claude Code), through magpie
// - "first": magpie has none of the model it is on: the first on the
// account it is signed in to, else a subscription's, else the first
//
// Field and Value are the field set and what it was set to, where Connect
// picked one model ("" for kept, again and joined).
type Connection struct {
How string `json:"how"`
Field string `json:"field,omitempty"`
Value string `json:"value,omitempty"`
}
// Rewired, when set, is told of an agent that Connect or Disconnect
// changed: the library gives Codex its servers another way on magpie.
var Rewired func(id string)
func (a *Agent) rewired(err error) {
if err == nil && Rewired != nil {
Rewired(a.ID)
}
}
// ConnectHow is Connect, saying how the model was chosen.
func (a *Agent) ConnectHow() (c Connection, err error) {
defer func() { a.rewired(err) }()
if a.Native != nil {
return Connection{How: "joined"}, a.Native.Connect()
}
if len(a.Fields) == 0 || a.Wired() {
return Connection{How: "kept"}, nil
}
return a.connect()
}
// disconnectedKey, after an agent's id, marks in the stash an agent the
// user disconnected: Disconnect sets it, connecting again takes it out.
const disconnectedKey = ".disconnected"
// Disconnected reports whether the user disconnected the agent and hasn't
// connected it since.
func (a *Agent) Disconnected() bool { return stashLoad()[a.ID+disconnectedKey] == "1" }
// noModelLeft says why there is no model of magpie's to connect the agent
// to: none added yet (NoModelsError), or every one taken out of its lists
// (AllHiddenError); nil while one is left.
func (a *Agent) noModelLeft() error {
shown, hidden := provider.CatalogFor(a.ListsFor())
// nothing to connect it to yet: no provider or subscription added,
// which said only that it can't be connected (Tystem on Discord)
if len(shown)+len(hidden) == 0 {
return &NoModelsError{Agent: a.Name}
}
// magpie has models, and every one of them is taken out of the
// agent's lists (a picked model gone with its provider, one hidden by
// hand): saying the agent can't be connected sends the user looking
// at the agent, when its model list is what emptied it (#1989)
if len(shown) == 0 {
return &AllHiddenError{Agent: a.Name}
}
return nil
}
func (a *Agent) connect() (Connection, error) {
forget(a.ID + disconnectedKey)
// 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 Connection{How: "again"}, 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 {
if ok, err := a.Join(); ok || err != nil {
return Connection{How: "joined"}, err
}
}
// the field magpie is picked in: the one listing magpie's models (for
// Gemini CLI its model, its first field being how it signs in), else one
// whose value is magpie itself
vals := a.Values()
var f *Field
var opts []Option
for pass := 0; pass < 2 && f == nil; pass++ {
for i := range a.Fields {
if a.Fields[i].Options == nil {
continue
}
list := a.Fields[i].Options(vals)
for _, o := range list {
if pass == 0 && o.Ref != "" || pass == 1 && o.Value == magpieID {
f, opts = &a.Fields[i], list
break
}
}
if f != nil {
break
}
}
}
if f == nil {
if err := a.noModelLeft(); err != nil {
return Connection{}, err
}
return Connection{}, fmt.Errorf("%s can't be connected to magpie", a.Name)
}
cur := connectWas(vals[f.Key], opts)
if cur == "" && f.Key != a.Fields[0].Key {
cur = connectWas(a.Values()[a.Fields[0].Key], nil)
}
onDefault := cur == ""
if cur == "" {
// on its default: the first of its own models
for _, o := range opts {
if o.Ref == "" && o.Group == a.Name && o.Value != magpieID {
cur = connectWas(o.Value, opts)
break
}
}
}
// the model it is on now, through magpie (or of its family, for an
// alias): on the account it is signed in to (its vendor's) first, then
// on a subscription, before a key's; else its own vendor's; else a
// subscription's; else the first
var pick, same, alike, own, sub, group string
sameRank, alikeRank := -1, -1
for _, o := range opts {
if o.Value == magpieID {
if err := a.Apply(f.Key, magpieID); err != nil {
return Connection{}, err
}
// an app whose picker gets magpie's models as listed for it
// (WorkBuddy, ZCode): with none listed, setting magpie wrote
// none, and the agent reads as not connected right after a
// connect that said it was (Lumens on Discord). One connected
// by its address alone (Cursor Private Inference) is connected
// all the same.
if !a.Wired() {
if err := a.noModelLeft(); err != nil {
return Connection{}, err
}
}
return Connection{How: "magpie", Field: f.Key, Value: magpieID}, nil
}
if o.Ref != "" && o.Group == RoutingGroups && group == "" {
group = o.Value
}
if o.Ref == "" || o.Group == RoutingGroups {
continue
}
rank := 0
if o.own || o.Group == a.Name {
rank = 2
} else if o.sub {
rank = 1
}
if pick == "" {
pick = o.Value
}
id := o.Ref[strings.LastIndex(o.Ref, "/")+1:]
if cur != "" && (id == cur || strings.TrimSuffix(id, "[1m]") == cur) && rank > sameRank {
same, sameRank = o.Value, rank
}
// an alias (opus) no label read: the model of that family
if cur != "" && !strings.Contains(cur, "-") && strings.Contains(id+"-", "-"+cur+"-") && rank > alikeRank {
alike, alikeRank = o.Value, rank
}
if own == "" && (o.own || o.Group == a.Name) {
own = o.Value
}
if sub == "" && o.sub {
sub = o.Value
}
}
// the model it is on, of its own, as magpie serves it on a sign-in of
// the user's, which its field lists as its own (Codex's on a ChatGPT
// account it can't join, #940)
if same == "" && alike == "" && cur != "" && a.OwnVia != nil {
same = a.OwnVia(cur)
}
how := "first"
switch {
case same != "":
pick, how = same, "same"
if onDefault {
how = "default"
}
case alike != "":
pick, how = alike, "alike"
case own != "":
pick = own
case sub != "":
pick = sub
case pick == "" && group != "":
// only routing groups are shown it (its models hidden, or its own
// account's alone beside them): the first group (#939)
pick = group
}
if pick == "" {
return Connection{}, fmt.Errorf("magpie has no models %s can use: add a subscription or a provider first", a.Name)
}
return Connection{How: how, Field: f.Key, Value: pick}, a.Apply(f.Key, pick)
}
// connectWas is the model an agent is on before it is connected, as a
// model id: its own option's alias read through the label that names the
// model ("opus · claude-opus-5-5"), a provider's prefix taken off.
func connectWas(v string, opts []Option) string {
for _, o := range opts {
if o.Value == v && o.Ref == "" {
if _, id, ok := strings.Cut(o.Label, " · "); ok {
v = strings.TrimSpace(id)
}
break
}
}
if i := strings.LastIndex(v, "/"); i >= 0 {
v = v[i+1:]
}
return strings.TrimSpace(v)
}
// Disconnect takes magpie out of the agent's config and puts back what the
// user had, the Agents page's "Disconnect from magpie" (Fate on Discord:
// picking the agent's default did it, but nothing said so). Unwire first,
// for an agent whose default would leave it as installed; then each field
// still on magpie goes to its default, as picking it does, and so does
// each one magpie set that reads as magpie set it (an effort). What magpie
// remembered setting is forgotten.
func (a *Agent) Disconnect() (err error) {
defer func() { a.rewired(err) }()
if a.Native != nil {
plan, err := a.Native.Disconnect()
if err != nil {
return err
}
return a.Native.Execute(plan)
}
if !a.Wired() {
return nil
}
// the user's say, kept till they connect it again: nothing magpie does
// by itself (a start's Sync, Codex's account failover) puts magpie back
// in, this Disconnect's own writes included (paynezhuang on Discord)
stash(map[string]string{a.ID + disconnectedKey: "1"})
// the user's own say over a harness Kiro Crew's connect put on
kiroCrewUserLeft(a.ID)
before, rec := a.Values(), appliedOf(a.ID)
if a.Unwire != nil {
if err := a.Unwire(); err != nil {
return err
}
}
for _, f := range a.Fields {
vals := a.Values()
v := vals[f.Key]
// set by magpie: as it set it, or its group by the model's name
set := v != "" && (rec.Fields[f.Key] == v || sameGroup(rec.Fields[f.Key], v)) && before[f.Key] == v
if v == "" || !set && v != magpieID && !magpieValue(a, f, v, vals) {
continue
}
if err := f.Set(""); err != nil {
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()
// connected while on a provider named magpie it had by hand: what it
// was on then is magpie's route, its thinking level read off it too
// (dsh's), not the agent's own to put back
for _, f := range a.Fields {
if w := was[f.Key]; w == magpieID || magpieValue(a, f, w, was) {
was = nil
break
}
}
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)
}
}
}
if a.Restore != nil {
if err := a.Restore(); err != nil {
return 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.forgetSetUp()
return nil
}
// DisconnectOffline restores a native agent's saved files only after an
// explicit offline choice. Legacy callers of Disconnect keep live semantics.
func (a *Agent) DisconnectOffline() error {
if a.Native == nil || a.Native.ExecuteOffline == nil {
return fmt.Errorf("%s does not support offline disconnect", a.Name)
}
plan, err := a.Native.Disconnect()
if err != nil {
return err
}
return a.Native.ExecuteOffline(plan)
}
// 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
}
}
// joined first, where the agent can be (Codex signed in with ChatGPT),
// as the switch connects it the first time: its own models stay
// through magpie beside the one put back. Put back on a routing group
// 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)
if a.Join != nil {
if _, err := a.Join(); err != nil {
return true, err
}
}
// 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.
// Zero if there is none.
func lastJSONLTime(path, key, textKey string) time.Time {
f, err := os.Open(path)
if err != nil {
return time.Time{}
}
defer f.Close()
const tail = 64 << 10
if st, err := f.Stat(); err == nil && st.Size() > tail {
f.Seek(st.Size()-tail, 0)
}
var last int64
sc := bufio.NewScanner(f)
sc.Buffer(make([]byte, 64<<10), 4<<20)
for sc.Scan() {
var m map[string]json.RawMessage
if json.Unmarshal(sc.Bytes(), &m) != nil {
continue
}
var text string
if json.Unmarshal(m[textKey], &text) == nil && strings.HasPrefix(strings.TrimSpace(text), "/") {
continue
}
n, err := strconv.ParseInt(string(m[key]), 10, 64)
if err != nil {
continue
}
if n > 1e12 {
n /= 1000
}
last = max(last, n)
}
if last == 0 {
return time.Time{}
}
return time.Unix(last, 0)
}
// sameHost: two URLs name the same server, whatever their scheme (Codex
// asks magpie's http gateway over ws) or path.
func sameHost(a, b string) bool { return hostOf(a) == hostOf(b) }
// wiringOff checks what magpie wrote into an agent's config to reach the
// gateway — pairs of key and value, read with get — and says which no longer
// holds, "" when all do. A URL is quoted; a key's value never is. A URL
// that reaches magpie another way (reachesMagpie: a reverse proxy, #1985)
// holds. So does a key of the gateway's own the user put in place of
// magpie's (keyHolds).
func wiringOff(name, file string, get func(string) (string, bool), kvs ...string) string {
for i := 0; i+1 < len(kvs); i += 2 {
k, want := kvs[i], kvs[i+1]
v, _ := get(k)
if keyHolds(v, want) || v != "" && reachesMagpie(v, want) {
continue
}
return whatsOff(name, file, k, v, want)
}
return ""
}
// whatsOff is what wiringOff says of k in an agent's config, v where
// magpie wrote want.
func whatsOff(name, file, k, v, want string) string {
where := name + "'s " + k + " (" + filepath.Base(file) + ")"
switch {
case v == "":
return where + " is gone, so it no longer reaches magpie"
case strings.Contains(strings.ToLower(k), "url"):
return where + " is " + v + ", not magpie's gateway at " + want
default:
return where + " was changed, so magpie's gateway won't take its requests"
}
}