From 206dc085be3d44fc1161da3ab5e2d417807b236e Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 12 Oct 2026 05:20:26 +0800 Subject: [PATCH] provider: a magpie run from a terminal that only lists accounts no longer writes logins.json, and every change to it is made under a lock the gateway holds too, so a token the gateway just refreshed is never written back over (#1674, FaintGhost) Every CLI command that reached Logins() (magpie ls, providers, sessions, usage) ran rememberLogins, whose 30-second throttle (loginsSeenAt) lives in the process, so in a new process it never held: each run moved Seen on for the signed-in Codex and Claude accounts and wrote logins.json back. loginsMu was an in-process mutex, so that read-modify-write could interleave with the gateway's renewSavedLogin: the CLI reads, the gateway writes a refreshed sign-in, the CLI writes what it read. Codex/OpenAI refresh tokens are single use, so the account would be signed out. - rememberLogins writes nothing when the only change is Seen and the saved Seen is less than seenEvery (10 minutes) old (onlySeenMoved, comparing with what was read, IDs and order normalised as writeLogins writes them; sortLogins is split out of writeLogins for that). - internal/filelock (new): davsync's TryLock/Unlock (flock; LockFileEx on Windows) moved here and davsync uses it; Lock(path, wait) and filelock.Mutex, a sync.Mutex that also holds the OS lock on a lock file. A lock another magpie holds is waited for up to filelock.Wait (10s), then gone on without, as before, so a stuck magpie never hangs others. - loginsMu is a filelock.Mutex on logins.json.lock, so all 107 existing Lock sites (every writeLogins is under one) are cross-process with no change at the call sites. Taking it calls filememo.Look (new), so the read under it isn't a parse filememo.Hold kept from before another magpie's write. savedTokenMu holds logins.json.renew.lock the same way: two magpies never refresh one saved account at once. - Siblings: caller-keys.json (access.mu: magpie keys from the CLI vs the window; a lost revoke would bring a key back) and mcp-signins.json (mcpauth.fileMu, plus mcp-signins.json.renew.lock around renew, since library's MCP check renews from the CLI too) get the same lock. plugin-auth.json already has one (plugin-auth.json.lock, shared with host.js). Not covered: providers.json (33 store sites with no in-process lock either; user-entered keys, nothing refreshed there), migrations.json/settings.json (no credentials), and the agents' own files (~/.codex/auth.json etc.: the agent itself writes them without any lock of ours, so a magpie-only lock can't make them safe). Tests: - TestReadOnlyRunLeavesLogins: Codex and Claude signed in, Seen a minute old, file time set back; three "new processes" of Logins("")/codex/ claude/All() leave logins.json's bytes and mtime as they were. Seen an hour old is written; Codex's own refresh is written at once. - TestLoginsKeepAnotherMagpiesWrite: between this process's read and write (beforeLoginsWrite hook) a second test process (TestLoginsOtherMagpie, os.Args[0] -test.run) renews the saved work@ account's refresh token; both the renewed token and this process's change are in the file. - TestLoginsReadAfreshUnderLock: with filememo.Hold held, a write filememo never heard of is seen by a read under loginsMu. - TestLockAcrossProcesses (filelock): a holder subprocess makes Lock wait and give up after its wait; killed, the lock is free; a Mutex holds it. Broken by hand: onlySeenMoved bypassed -> TestReadOnlyRunLeavesLogins FAIL "a run that only listed the accounts wrote logins.json"; the file lock taken out of filelock.Mutex.Lock -> TestLoginsKeepAnotherMagpiesWrite FAIL "the other magpie's renewed sign-in was written over"; Locked hook emptied -> TestLoginsReadAfreshUnderLock FAIL. Each restored, green. Ran: gofmt -l (nothing); go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...; go build, GOOS=linux go build -tags nogui, GOOS=windows go build; go test -tags nogui -count=1 ./... under a temp HOME with pinned caches: all ok but internal/provider, whose TestReadOnlyRunLeavesLogins failed at that point from its own fixture (two rewrites of one size given the same mtime read as unchanged by filememo); fixed in the test, then internal/provider, access, mcpauth, filememo, filelock and davsync -count=1 all ok, and the three new provider tests -race -count=10 ok. Not tried: the reporter's Linux arm64 box with a real systemd gateway and real Codex accounts; Windows (LockFileEx path) only vetted and built, not run on the Windows box. --- docs/subsystems/providers-accounts.md | 1 + internal/access/access.go | 6 +- internal/davsync/lock.go | 5 +- internal/davsync/lock_unix.go | 20 -- internal/filelock/filelock.go | 82 +++++++ internal/filelock/filelock_test.go | 69 ++++++ internal/filelock/lock_unix.go | 22 ++ .../{davsync => filelock}/lock_windows.go | 10 +- internal/filelock/main_test.go | 10 + internal/filememo/filememo.go | 15 ++ internal/mcpauth/mcpauth.go | 10 +- internal/provider/logins.go | 108 +++++++-- internal/provider/logins_lock_test.go | 222 ++++++++++++++++++ internal/provider/logins_on.go | 9 +- 14 files changed, 534 insertions(+), 55 deletions(-) delete mode 100644 internal/davsync/lock_unix.go create mode 100644 internal/filelock/filelock.go create mode 100644 internal/filelock/filelock_test.go create mode 100644 internal/filelock/lock_unix.go rename internal/{davsync => filelock}/lock_windows.go (65%) create mode 100644 internal/filelock/main_test.go create mode 100644 internal/provider/logins_lock_test.go diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 5d5be190a..ba463dbfe 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -118,6 +118,7 @@ Account discovery can be turned off (#1535): Settings → Privacy → *Subscript - An unreadable `providers.json` is an error, never an empty list. Saving over an empty list would lose every provider. - When `logins.json` cannot be decoded as saved accounts, reads keep the accounts read before. Before saving a change, `lastgood.Keep` ([lastgood.go](../../internal/lastgood/lastgood.go)) keeps the file there now: as `logins.json.bak`, its last good generation, when it reads, or copied aside as `logins.json.bad-