provider: a plugin sign-in drops what a request holds at once (#2449)

TestPluginLoginInstallsItsCLIFirst went red on macOS CI (run
38798393584) with "not signed in after the install: []". cli's run()
sets catalog.Changed = agent.SyncCatalog, and a sync left going from an
earlier step holds the catalog (provider.Hold), plugins and their
sign-ins among it, for up to a second. The host's "auth" event dropped
that only through catalog.Touched, told in the background, so on a busy
runner provider.Logins right after the sign-in read the snapshot taken
before it.

plugin.OnChangeNow registers hooks run before changed() or listed()
returns, i.e. before the host's reply after the event is read; provider
registers dropHeld there. Changed() is filememo.Forget + dropHeld.

Reproduced by injecting a pause where SyncCatalog holds (patch kept
outside the tree): without the fix red 3/3 with the CI message; with it
ok -count=5. TestHeldGoesWithAPluginChange: red without the fix ("held
from before the change: built 1 times, want 2"), green with it, also
-race -count=20.

Run: -count=20 -run TestPluginLoginInstallsItsCLIFirst ./internal/cli
ok; internal/cli, internal/plugin, internal/provider whole ok; gofmt -l
clean; go vet -tags nogui for darwin, linux, windows on the three ok.
Full suite: go test -tags nogui -timeout 45m ./... at 7fefbc614 with both #2449 commits (this one and the Settle fix after it) (sandbox HOME, macOS): exit 0, 66 ok, 18 no test files, 0 FAIL.
This commit is contained in:
yetone
2026-10-16 05:29:20 +08:00
parent bbf901570e
commit 6641e1b0ac
4 changed files with 62 additions and 0 deletions
+1
View File
@@ -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 `<level>-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`).
+26
View File
@@ -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()
+10
View File
@@ -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
+25
View File
@@ -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.