4f9e8331a wrote served.json 2s behind the answer. In a test binary that
timer outlived the test that answered, and wrote into its temp HOME as
testing removed it: BenchmarkGroupOverhead's first case failed 1 run in
6 with "TempDir RemoveAll cleanup: unlinkat …: directory not empty", and
TestOTelSessionDedupOnlyUnkeyedLoopback failed the same way in a package
run. servedFlushAfter is now 0 in a test binary (testing.Testing(), as
partners.go already does), and NoteServed writes at once, after it lets
go of servedAt, as it did before 4f9e8331a. TestNoteServedWritesBehind
sets the 2s itself.
Test: TestNoteServedInATestWritesAtOnce. Broken by hand (writeBehind
returning d in a test binary too): red, "not written as the answer was
noted: … served.json: no such file or directory".
Run: go test ./internal/provider -run 'TestNoteServed|LastServed'
-count 1: ok. BenchmarkGroupOverhead's 12x20 p0/m1 case, 6 runs of 100
requests before: 1 failed on the cleanup; see the next commits' records
for the runs after.
NoteServed ran on every answered request (servedCandidate): it read
served.json, merged, and wrote it through steady.WriteFile (a synced temp
file renamed over it), all under servedAt's lock, so concurrent answers
queued behind each other's fsync.
Now NoteServed only notes in memory and arms one write for
servedFlushAfter (2s) later; everything noted meanwhile goes in that one
write, merged with what the file holds (another gateway's) and pruned past
servedKeep as before. The file's format is unchanged. This process's
LastServed has a note at once (it merges memory over the file, as it
did); the CLI, a process of its own, sees it within 2s. FlushServed writes
what is pending: the gateway calls it as Serve ends, and the CLI's signal
handler before it dies of the signal. A crash loses at most the last 2s
of notes. A note made under one config path (a test's HOME) is never
written into another's: a path change drops the pending write.
Numbers (Apple M4 Pro, machine loaded by other sessions):
BenchmarkNoteServed before 5.10-5.33 ms/op 7.8 KB/op 82 allocs/op
after 0.64 us/op 352 B/op 5 allocs/op
Test: TestNoteServedWritesBehind notes 200 answers and checks the file is
untouched, LastServed has the latest at once, FlushServed writes it once
(a second flush writes nothing), merged with another gateway's entry, and
that the timer writes it by itself. Broken by hand (origin/main's
served.go with stubs for the new names): red, "written on a request
answered", and the 200 notes took 1.04s.
Run: go test ./internal/provider -run 'TestNoteServedWritesBehind|TestQuotasLastServed|TestCardsLastServed';
./internal/gateway -run TestQuotasLastServedAt; ./internal/cli -run
'TestQuotaJSONLastServedAt|Signal'; ./internal/gui -run
TestServedMovedWakesTheMenuBar: ok. The full suite run is in the last
commit of this series.
f5c062677 made the menu bar's account-in-use card ("codex|*") follow the
account that answered through the gateway within the last hour. Two gaps
were left:
- The tray read its cards again only every few minutes, so after the
gateway moved to another account, or the user switched, the menu bar
kept the old account's numbers until the next tick. NoteServed now calls
OnServedMoved when a provider's answers move to another account or key
(and on the first one this process sees), and SwitchLogin calls it after
a switch; the tray loop is woken by it, as by OnSubscriptionUsage.
- A plugin's accounts (moved Kiro, Devin, ...) are no sign-in InUseLogin
knows, so with nothing served in the last hour the first card was shown
even when the gateway had last gone to another. trayInUseCard now takes,
after InUseLogin, the one that answered last at any age, before the first.
Run: TestTrayInUsePluginFollowsLastServed fails without the fallback
("plugin: ... User:k1@x.y"); TestServedMovedWakesTheMenuBar fails with
the hook's firing removed ("the first answer told nobody", "b@ answering
after a@ told nobody"); both pass now, with TestTrayInUse and
TestTrayInUseFollowsWhoAnswered. go test -tags nogui ./... (-timeout 40m)
passes; gofmt -l, go vet (gui with and without nogui), GOOS=linux and
GOOS=windows go vet -tags nogui on gui, provider, autostart pass. No GUI asset changed.
Not tried with the real menu bar and two real accounts.
okingkee on X: three Codex accounts, the menu bar set to follow the account
in use. A used its allowance up and the gateway went on to B, but the menu
bar stayed on A; changing the first account moved it, the gateway moving
on did not. trayInUseCard asked InUseLogin, which reckons from the account
the agent is signed in to, and that lags the gateway (and under Smart it
picks any account).
- provider: SubscriptionQuota gains lastServedAt, set by Quotas
(cardsServed) from served.json the way /v1/magpie/quotas already was,
on this computer's own cards only and on copies, the caches untouched.
withServed's matching moved into servedBy.at, shared by both.
This also makes the Usage page and tray panel's "show the account that
answered last in full" (ae6b4c93) work: it read q.lastServedAt, which the
window's API never sent, so it always showed the first.
- tray: a "<provider>|*" card takes the provider's account that answered
within the last hour (trayServedFresh), else InUseLogin's as before.
Run:
- TestTrayInUseFollowsWhoAnswered (A at 100% answered 40 min ago, B 1 min
ago → B; both stale → InUseLogin's A; one account → it) fails with the
served pick disabled and passes; TestCardsLastServed (subscription by
user, balance with no user, a remote card left alone, inputs unwritten)
passes; TestTrayInUse and TestQuotasLastServed unchanged and pass.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./... pass.
- go test ./... passes except internal/gateway, which hit the 10m default
timeout under this machine's load; run alone with -timeout 20m it passes
(726s). No JS changed, so no GUI test run.