fix(vault): keep a newer quota reading when an older check finishes last
Problem
Release 3.1.11 added a guard. When an older quota check stops after a newer quota check, the older check does not replace the newer quota value. This guard is in the local account store only (StaleQuotaObservationError in packages/core/src/account-repository-codecs.ts).
On the OpenCode 2 vault path, quotaUpdate and checkVaultQuota in packages/opencode/src/plugin/shared/vault-request-credentials.ts write a quota value and do not do this check. The "Check quota now" menu item does not use the lock of the background quota check. If the manual check stops last, its older value replaces the newer value of the background check. This occurs in the shared state file and in the live pool row. The next background check corrects the value. Thus the effect is small, but the vault path and the local path do not agree.
Change
isOlderQuotaReadinguses the same rule as the local store. It stops a new value only when the two values have a date and the date of the new value is earlier. It accepts a value with the same date or with no date.- For an older value,
quotaUpdategives no update. Thus the commit result isunchangedwhile the commit holds the provider-state lock. The stored quota and the stored rate-limit data do not change. - When
checkVaultQuotastops an older value, it counts the account as checked. The caller gets no error. The localrecordQuotadoes the same. - The live pool row does not use a value that is older than its own value.
Tests
- New file
packages/opencode/src/plugin/shared/vault-quota-newer-reading.test.ts, with a test clock, a temporary state file and no timed waits:keeps the newer stored reading when an older check commits lastlets a newer reading replace an older onekeeps a newer reading on the live row when the file has noneaccepts an undated reading over a dated one, as the local store does
- One assertion in
vault-request-credentials.test.ts(keeps a newer local reading and limit when stored quota is older) changes fromoldertonewer. The previous assertion agreed with the defect: in the file, the older value replaced the newer value.
Results on a branch from v3.1.12, with Bun 1.4.4:
- The new test file and the eight vault test files nearest to the change:
168 pass, 0 fail. - With the
v3.1.12version ofvault-request-credentials.ts:3 fail(Expected: 2000,Received: 1000, andExpected: 1100,Received: 1000). - Biome check of the changed files: no errors.
tsc -p tsconfig.ga.json: no errors.
The full bun run test and bun run typecheck did not run, because embed:check and the TUI type build must have Bun 1.4.2. The branch merges into the current main with no conflicts. The commits after v3.1.12 do not change these two files.
Summary by cubic
Fixes vault quota so an older check finishing last can no longer replace a newer reading, matching the guard the local account store already has.
A manual "Check quota now" runs outside the background poller’s lock, so its older value could commit after a newer background check and overwrite it in the shared state file and on the live pool row. The next background check corrected it, but the vault path and the local path disagreed.
Bug Fixes
quotaUpdateandcheckVaultQuotainvault-request-credentials.tsnow refuse a dated reading older than the stored one; the account still counts as checked.- The live pool row keeps its own newer reading when an older one arrives.
- One test assertion now expects the newer reading — it previously asserted the buggy behavior.