SIGN IN SIGN UP

fix(runtime): preserve pin/gen on routine saves and remove hot-path spin-wait (#474)

P0-1: AccountManager.buildStorageSnapshot was dropping pinnedAccountIndex
and affinityGeneration on every save. The proxy calls saveToDiskDebounced
on every routine state change (rate-limit hits, cooldowns, near-quota
refunds, refresh persistence), so each such save silently wiped any pin
the user set via switch and reset the affinity counter. The persistRuntime
ActiveAccount isPinned guard only covered the successful upstream path;
many other call sites were unguarded.

Fix: hydrate pin/gen onto AccountManager at construction time, refresh
from disk just before snapshotting (race protection — a CLI switch/unpin
between proxy startup and a routine save would otherwise be clobbered),
and emit them in the snapshot. The pin is treated as part of the same
atomic write that bumps the generation, so we only adopt the disk pin
when the disk gen is strictly ahead of memory.

P0-2: readStorageMetaFromDisk was busy-waiting up to 15ms per request
between retries on transient FS errors. Replaced with a single read +
mtime-cache short-circuit; on any failure (transient or otherwise) we
fall back to the cached snapshot for that path and only return defaults
on a true cache miss. Removes the no-op spin loop that blocked the
event loop on the proxy hot path.

Tests: extends issue-474-pin-safety with round-trip pin/gen save, the
CLI-bumped-mid-save race, no-pin omission, and a first-ever-read EBUSY
defaults case.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
N
Neil Daquioag committed
7307ed083ddc6ada11cb2ca8a9e1429adeef3ddb
Parent: afbb97a
Committed by Neil Daquioag <neil@zeian.cc> on 5/10/2026, 7:05:53 AM