Skip to content

fix(vault): keep a newer quota reading when an older check finishes last - #78

Open
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:fix/vault-quota-newer-guard
Open

iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:fix/vault-quota-newer-guard

Conversation

@iceteaSA

@iceteaSA iceteaSA commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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

  • isOlderQuotaReading uses 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, quotaUpdate gives no update. Thus the commit result is unchanged while the commit holds the provider-state lock. The stored quota and the stored rate-limit data do not change.
  • When checkVaultQuota stops an older value, it counts the account as checked. The caller gets no error. The local recordQuota does 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 last
    • lets a newer reading replace an older one
    • keeps a newer reading on the live row when the file has none
    • accepts 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 from older to newer. 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.12 version of vault-request-credentials.ts: 3 fail (Expected: 2000, Received: 1000, and Expected: 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

  • quotaUpdate and checkVaultQuota in vault-request-credentials.ts now 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.

Written for commit 7252331. Summary will update on new commits.

View guided diff

The 3.1.11 guard covered only the local account store. On the vault
path a manual quota check runs outside the poller lock, so a late
older reading replaced the poller's newer one in the state file and on
the live row. Refuse a dated reading older than the stored one, as the
local store does; the check still reports the account as checked.
@agauth-alfonso

Copy link
Copy Markdown

Thanks @iceteaSA, this is a real bug and a clean fix. We reproduced it on 3.1.13: with your new tests and the unpatched code, the stored and live-row checks both read 1000 where 2000 was expected, and both pass with your change.

We've taken it into our next release branch as one commit, with you as co-author. We added two small things:

  • the new test file is registered in test/support/disk-test-files.ts, because it uses real provider-state locks and atomic writes, and the repository guard requires that;
  • the checkVaultQuota comment now says that a stored newer reading and a newer live-row reading are two separate checks.

We'll close this pull request with a link once the release is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant