coeo91: after an update or a restart, the base URL in an agent's config
sometimes went from the LAN address back to 127.0.0.1.
Every start of magpie (and a provider added, a catalog fetched) runs
SyncCatalog, which runs each agent's Sync to bring magpie's models in
its files up to date. Each Sync writes magpie's whole provider block,
base URL and key included, from gateway.URL(), so a block the user had
pointed at a magpie on another machine (a NAS's, or this machine's LAN
address) went back to 127.0.0.1 with magpie's own key. Only ZCode
(zcodeAddress), WorkBuddy (buddyWriteAt) and Codex (#816) kept such an
address; in a sandbox HOME, 27 files of the other agents went back
(Qoder, OpenCode, Pi, Crush, Cline, Qwen Code, Kimi, Grok, Hermes, omp,
MiniMax, Snow, Hanako, VS Code, Copilot JetBrains, Goose, dsh, ...).
The fix is one place for all of them: SyncCatalog runs each Sync
through synced, which passes every file written through keptAway
(edit.Filtered, a filter on edit.WriteAtomic while it runs). In a file
without the gateway's own address, with an address on another machine
and none a WSL distro was seen at, a write that brings gateway.URL() in
place of that address keeps the address, and one that brings magpie's
own key (gateway.Token, TokenFor, the LAN key, "Bearer " too) in place
of another key keeps that key. An address on this machine's loopback
(another port, localhost) still follows the gateway, a WSL distro's
way to Windows is still magpie's to move, magpie's own files are never
filtered, and with two addresses or keys gone the write is left as it
is. A pick made in magpie still points the agent at this magpie.
Tests:
- TestSyncKeepsAnAddressOnAnotherMachine wires every agent in a sandbox
HOME, points each file at 192.168.50.10 with a key of its own, adds a
provider and runs SyncCatalog: no file is back on 127.0.0.1, the
address and key are kept, and the new model is written. With
SyncCatalog calling a.Sync() directly it fails on 27 files.
- TestKeptAwayOnlyWhatIsSure: old port, localhost, WSL and two-gone
cases follow the gateway; a NAS beside a local Ollama is kept; Qwen
Code's key written on its own is kept.
- TestKeyValuesFormats, TestFilteredWrites.
Run: gofmt -l (clean); GOOS=linux and GOOS=windows go vet -tags nogui
./internal/agent ./internal/edit; go test -tags nogui ./... in a temp HOME.
syncJSON and Pi's writeMagpie set providers.magpie to a freshly built
object, so a key magpie doesn't write there was dropped on every sync.
theirsKept now merges magpie's block into what the file holds: keys magpie
writes are its own, objects in both are merged key by key, and anything
else (compat, an options key, Crush's extra_headers) stays. The model list
is still magpie's whole, so a model gone from the catalog leaves it, and a
sync with nothing new writes nothing.
The same goes for every agent that writes the block whole: Pi and OmO,
Aside, Pencil, OpenCode (and the agents built on it), OpenChamber, Crush
and Command Code.
Verified: TestPiKeepsTheirKeysInMagpiesProvider (the reporter's
compat.sendSessionAffinityHeaders: true in Pi's models.json, through
SyncCatalog and a pick) and TestOpenCodeAndCrushKeepTheirKeysInMagpiesProvider
fail without the change ("compat.sendSessionAffinityHeaders dropped by the
sync", "opencode sync") and pass with it. go test -race ./internal/agent
and go vet ./... pass in a sandbox HOME.