diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index b57b49829..656c1e11d 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -39,6 +39,7 @@ something else shows up as drift. - magpie only removes what it wrote. Anything else in an agent's config stays as it was. - A sync brings magpie's models up to date, not where the agent reaches magpie. Every `Sync` that `SyncCatalog` runs (each start and update of magpie, a provider added, and a plugin signed in to, out of or renewed, or listing other models than the last time: `plugin.OnChange` and `plugin.OnListed` call `catalog.Touched`, #2373) goes through `synced`, which passes each file written through `kept.away` (`edit.Filtered`, [`keptaway.go`](../../internal/agent/keptaway.go)). The filter sees every file written through `edit` while it is on, from any goroutine and under whatever lock the writer holds, so it takes none a writer may hold: the gateway's address and the LAN key are read before it is on (`keeping`), and magpie's own files (`magpieFile`: its folder as named or where its links lead, such as a `~/.config` linked into a dotfiles repo) are left as they are (ylorn on Discord: `settings.Save` under the settings' lock reached a filter that read the settings, and the app hung ~10s after every start; `TestOwnWritesDuringASyncWithALinkedConfig`). In a file without the gateway's own address, whose magpie the user pointed at an address on another machine (a NAS's, or this machine's LAN address), and with no address a WSL distro was seen at, that address stays in place of `gateway.URL()`, with the path before magpie's every URL at it had (`awayPath`: a reverse proxy's `/magpie`; on loopback, a local proxy's route where magpie answered or hasn't been asked), and so does the key that went with it in place of magpie's own (coeo91 on Discord: the base URL went back to 127.0.0.1 after an update or restart). An address on this machine's loopback follows the gateway; with more than one address or key gone, the write is left as it is. Picking a model in magpie still points the agent at this magpie, but for Claude Code at a proxy to it (above). ZCode and WorkBuddy keep such an address in their own writers too (`zcodeAddress`, `buddyWriteAt`). +- `SyncCatalog` holds the catalog it builds (`provider.Hold`), the plugins' providers and sign-ins among it, for up to a second. Those `catalog.Touched` drops are told in the background, so a plugin change also drops what is held at once (`plugin.OnChangeNow`, [`hold.go`](../../internal/provider/hold.go)): the host's "auth" event is read before its reply to the sign-in that saved it, and a look at the accounts once the sign-in returns sees it (#2449: `magpie plugin login` found no account while a sync ran; `TestHeldGoesWithAPluginChange`). - JetBrains Air's `magpie-opencode.json` keeps user-added fields in `provider.magpie`, including nested options, on sync and model picks (`air` in [`air.go`](../../internal/agent/air.go), using `theirsKept` in [`sync.go`](../../internal/agent/sync.go)). The gateway fields and complete model list are still replaced, so removed models leave the list. - In every OpenCode-shaped config (OpenCode, DevEco, Air, OpenChamber; `openCodeKept` in [`agents.go`](../../internal/agent/agents.go)), a variant the user put on one of magpie's models stays through a sync, and so does one they wrote over under a name of magpie's. A variant exactly as magpie writes it under its name (`openCodeVariant`) is magpie's, so one the model no longer has goes (#2379, `TestOpenCodeCodexFastVariants`). - OpenCode has no `/fast`. A ChatGPT account's own `gpt-*` model, not a routing group (`catalog.Model.CodexFast`, set in `magpieModels`), gets each reasoning level again as `-fast` (or `fast` alone with no levels), asking `service_tier: "priority"` as Codex's `/fast` does. The tier is written twice: as an option, which OpenCode 1's `@ai-sdk/openai-compatible` sends as it is, and in `extraBody`, which OpenCode 2 sends while it drops an option it doesn't know (checked with OpenCode 2.0.25 against a fake server). The gateway drops OpenCode 1's stray `extraBody` field when it rebuilds the request as Responses for the ChatGPT backend (`TestOpenCodeFastVariantReachesCodex`). diff --git a/internal/plugin/host.go b/internal/plugin/host.go index 57139caac..761e4666e 100644 --- a/internal/plugin/host.go +++ b/internal/plugin/host.go @@ -554,6 +554,8 @@ var ( // onChange are told a sign-in or the plugins changed. onChange []func() onChangeMu sync.Mutex + // onChangeNow are run before changed or listed returns (OnChangeNow) + onChangeNow []func() ) // telling counts the hooks told of a change or of a sign-in (OnChange, @@ -575,6 +577,19 @@ func OnChange(f func()) { onChangeMu.Unlock() } +// OnChangeNow registers f to be run at once when a plugin sign-in, the +// plugins or their listing change, before anything after it is answered: +// an "auth" event is read before the reply to the sign-in that saved it, +// so a look made once the sign-in returns doesn't get what was built +// before it (#2449). OnChange's are told in the background, where a busy +// machine may run them after that look. f must be quick: the host's +// replies wait for it. +func OnChangeNow(f func()) { + onChangeMu.Lock() + onChangeNow = append(onChangeNow, f) + onChangeMu.Unlock() +} + var ( onSignInMu sync.Mutex onSignIn func(provider, account, said string) @@ -605,6 +620,7 @@ func OnListed(f func()) { } func listed() { + changedNow() onListedMu.Lock() fs := append([]func(){}, onListed...) onListedMu.Unlock() @@ -615,6 +631,7 @@ func listed() { func changed() { forgetProviders() + changedNow() onChangeMu.Lock() fs := append([]func(){}, onChange...) onChangeMu.Unlock() @@ -623,6 +640,15 @@ func changed() { } } +func changedNow() { + onChangeMu.Lock() + fs := append([]func(){}, onChangeNow...) + onChangeMu.Unlock() + for _, f := range fs { + f() + } +} + // tell runs a hook in the background, counted in telling until it ends. func tell(f func()) { telling.Lock() diff --git a/internal/provider/hold.go b/internal/provider/hold.go index 2e5d93f90..e5ec66347 100644 --- a/internal/provider/hold.go +++ b/internal/provider/hold.go @@ -39,6 +39,10 @@ func init() { // after it was written (#2373). plugin.OnChange(catalog.Touched) plugin.OnListed(catalog.Touched) + // and what a request holds goes at once: a SyncCatalog running holds + // the plugins as they were before a sign-in, and a look at the + // accounts right after it was given those for a second (#2449) + plugin.OnChangeNow(dropHeld) } // heldFor is how long a held catalog is used before it is built again, @@ -64,6 +68,12 @@ func Hold() (release func()) { // setting was written. func Changed() { filememo.Forget() + dropHeld() +} + +// dropHeld is Changed for what is built on the plugins alone, which +// read their files afresh each time. +func dropHeld() { held.Lock() held.gen++ held.built = nil diff --git a/internal/provider/hold_test.go b/internal/provider/hold_test.go index 4b2a9ac42..a49a74824 100644 --- a/internal/provider/hold_test.go +++ b/internal/provider/hold_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/yetone/magpie/internal/catalog" "github.com/yetone/magpie/internal/plugin" "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/testenv" @@ -44,6 +45,30 @@ func TestHoldBuildsOnce(t *testing.T) { } } +// A change the plugin host tells (a sign-in saved, a plugin added) drops +// what a request holds before it returns: a SyncCatalog running held the +// plugins as they were before `magpie plugin login` saved its account, +// and the look at the accounts right after it found none (#2449). The +// hooks told in the background are left out, so only what goes at once +// is looked at. +func TestHeldGoesWithAPluginChange(t *testing.T) { + plugin.Told() + forget, changed := catalog.Forget, catalog.Changed + catalog.Forget, catalog.Changed = nil, nil + t.Cleanup(func() { + plugin.Told() + catalog.Forget, catalog.Changed = forget, changed + }) + defer Hold()() + n := 0 + build := func() int { n++; return n } + heldOf("t-2449", build) + plugin.Restart() // as the host's "auth" event and a plugin added do + if heldOf("t-2449", build); n != 2 { + t.Fatalf("held from before the change: built %d times, want 2", n) + } +} + // The settings are read once while a request holds the catalog (a look at // the agents read settings.json for every model of every agent), and a // setting saved meanwhile is seen at once.