fix(providers): scope ChatGPT usage cache by credentials - #3594
Open
GautamSharma99 wants to merge 1 commit into
Open
fix(providers): scope ChatGPT usage cache by credentials#3594GautamSharma99 wants to merge 1 commit into
GautamSharma99 wants to merge 1 commit into
Conversation
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
Fixes #3584.
Successful ChatGPT usage responses were cached for 30 seconds under keys derived only from the target, base URL, and provider name. The cache lookup happened before credential resolution, so a long-lived process could return one account usage data after switching credentials or local storage profiles.
This change resolves the effective authentication context first and scopes every cache lookup to an opaque fingerprint of that context.
What changed
Credential-scoped cache keys
Added a small
CredentialScopedCacheowner that derives keys with HMAC-SHA256 using a random, process-local 256-bit key.Cloud/API usage
Cloud credentials are now resolved, including any required token refresh, before cache access. The identity includes:
Changing credentials under the same URL and provider therefore cannot reuse the previous account cache. Removing credentials returns
unauthorizedbefore cache access instead of returning stale usage.Local OAuth usage
Local OAuth credentials and the effective ChatGPT account ID are now resolved before cache access. The identity includes:
This isolates identical provider names across storage directories, distinguishes account replacement even if a token value is unchanged, and invalidates reuse when OAuth refresh replaces the credential.
Invalidation behavior
Explicit cross-layer invalidation hooks are not required for correctness:
This avoids introducing a dependency from backend credential storage into the provider usage service.
Tests
Added regression coverage proving:
Validation completed:
bun test src/providers/chatgpt-usage-service.test.ts— 13 passed, 0 failedbun run check— all 12 checks passedThe oversized
chatgpt-usage-service.tsbaseline is also ratcheted down from 1,112 to 1,111 lines.