fix: isolate quota cache entries per credential account - #236
Open
gitslim wants to merge 4 commits into
Open
Conversation
The per-user disk quota cache keyed entries without any account identity, so concurrent opencode instances using different accounts for the same provider (e.g. different ZAI_API_KEY values) clobbered each other's snapshots and displayed the wrong account's quotas until TTL expiry. Add an irreversible fingerprint (truncated sha1) of the allowlisted credential env values to every cache key, and of custom quota-provider apiKeyEnv values to aggregate keys. Raw credential values never enter a key; when no credential env vars are set the keys stay byte-identical to legacy keys, so single-account setups keep their cache warm. Fixes slkiser#235
…cache # Conflicts: # tests/lib.google-token-cache.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The per-user disk quota cache (
~/.cache/opencode/quota-provider-state/<provider>-<sha1(key)>.json) keyed entries without any account identity, so concurrent opencode instances that use different accounts for the same provider (e.g. differentZAI_API_KEYvalues via per-account wrapper scripts) clobbered each other's snapshots: last writer wins, and every other instance displays the wrong account's quotas until the cache TTL expires.buildQuotaProviderStateCacheKeynow appends an irreversible fingerprint (truncated sha1) of:ZAI_API_KEY,ZAI_CODING_PLAN_API_KEY,ZHIPU_*,MINIMAX_*,KIMI_*,ALIBABA_*,DEEPSEEK_API_KEY,KILO_API_KEY,CHUTES_API_KEY,OLLAMA_API_KEY,SYNTHETIC_API_KEY,NANOGPT_API_KEY,NANO_GPT_API_KEY,OPENCODE_API_KEY) to every key, andapiKeyEnvvalues to aggregate keys (the env var name was already part of the identity, but two accounts reusing the same name with different values still collided).Design constraints preserved:
options.credentialEnvis injectable so tests stay hermetic.New module
src/lib/credential-env.tskeeps the allowlist in one place, mirroring the per-providerenvVarsresolver lists.Linked Issue
Fixes #235
OpenCode Validation
ZAI_API_KEYdiffering per instance) where instances alternately showed each other's five-hour/MCP quotas; the fix was validated on that setup.Quality Checklist
pnpm run typecheckpnpm run buildpnpm test(
pnpm verifygate passes locally: 181 test files / 2175 tests, Biome clean, package contents verified. Docs checklist ticked as N/A — no user-facing workflow/config surface changes; the cache key is an internal detail.)