Repository navigation
fix(usage): read only the default profile's own credentials on macOS - #816
Open
eric-engberg wants to merge 3 commits into
Open
eric-engberg wants to merge 3 commits into
eric-engberg wants to merge 3 commits into
Conversation
On macOS, the default profile (CLAUDE_CONFIG_DIR unset) read the plain "Claude Code-credentials" keychain item, then scanned a keychain dump for any other "Claude Code-credentials-*" item, newest first, and only then read ~/.claude/.credentials.json. Claude Code adds that suffix, -<sha256(configDir)[:8]>, only when CLAUDE_CONFIG_DIR is set, so each suffixed item is another profile's. For the default profile it reads the plain item and falls back to .credentials.json, nothing else. Whenever the plain item had no login (logged out, an API-key session, or an item holding only MCP OAuth tokens), the scan picked up another profile's login, and the usage widgets showed that account's session and weekly usage. This happened even when ~/.claude/.credentials.json held the default profile's own login, because the scan ran first. The default profile now reads the plain item, then its own .credentials.json: the rule custom profiles already follow. The keychain dump, its parsing and its tests go. The scan came from sirmalloc#236, where the suffixed item returned nothing and the working login was in ~/.claude/.credentials.json, which is still read.
The default profile still reads only its own keychain item, now with upstream sirmalloc#532's retry under the OS username after a service-only miss and upstream sirmalloc#760's stop after a timeout, then its credentials file. Upstream's scan of the other profiles' suffixed items stays removed, with its tests; the remaining tests expect the username retry in their security calls.
# Conflicts: # src/utils/__tests__/usage-fetch.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.
What
On macOS, the usage widgets in the default profile (no
CLAUDE_CONFIG_DIR) now use only that profile's own login: the plainClaude Code-credentialsKeychain item, then~/.claude/.credentials.json. They no longer search the Keychain for otherClaude Code-credentials-*items. If neither has a login, the lookup finds none instead of picking up another profile's login, and the widgets show[No credentials]unless~/.cache/ccstatusline/usage.jsonholds another login's numbers: that cache is shared by every profile and, with no login to compare against, is still served. That's the cache side of the same symptom, which this PR leaves alone (#266 scopes the usage cache per config dir).Why (evidence)
When the plain item had no login, the default-profile lookup ran
security dump-keychainand took the newestClaude Code-credentials-*item. It did this before reading.credentials.json. The plain item has no login when the user is logged out, using an API key, or has an item holding only MCP OAuth tokens (#521).Claude Code names its credential item
Claude Code-credentials-<sha256(configDir)[:8]>only whenCLAUDE_CONFIG_DIRis set (checked in Claude Code 2.1.301's service-name builder). For the default profile it reads only the plain item, then.credentials.json. So every suffixed item belongs to another profile, which is why #573 already stopped custom profiles from falling back to them.On a Mac where a second profile such as
~/.claude-workis signed in, the default profile sent that profile's token to the usage API. It then showed that account's session, weekly and extra usage, even when~/.claude/.credentials.jsonheld the default profile's own login. Reproduced with a stubsecurityand a fake usage API:find plain → dump-keychain → find -<hash>)The scan was added for #236. In that report the suffixed item returned nothing and the working login was in
~/.claude/.credentials.json, which is still read.How
getUsageCredentialsresolves one Keychain service for every profile: the config-dir service if set, else the plain name. It falls back only to that profile's.credentials.json.claudeAiOauth, the lookup still falls through to.credentials.json, as fix(usage): read the CLAUDE_CONFIG_DIR keychain credential first on macOS #573 does for custom profiles; the file is the same profile's own login.docs/USAGE.md,docs/DEVELOPMENT.md) and the no-credentials backoff comment are updated.Demo
Default profile, with
~/.claude-worksigned in to another account (fake logins, a fake usage API answering by token, a stubbed Keychain; nothing is fetched).~/.claude/.credentials.jsonwith no Keychain item: Block and Weekly come from the payload, and Overage Used should be that login's own $4.00, not $123.45.[No credentials]instead of 77% / 66% / $123.45.Powerline
Before:
After:
Plain
Before:
After:
Testing
main(6) and pass here.usage-token.test.ts: the default profile skips another profile's item after an exact miss, finds no login when the item holds only MCP tokens, and makes a single lookup on invalid data or timeouts.usage-fetch.test.ts: two probe tests in a child process with a scratch HOME, noCLAUDE_CONFIG_DIR, a stubbedsecurity, and the platform forced todarwinso they also run on Linux CI. They assert that the request carriesBearer default-access, or reportsno-credentialswith 0 requests, and that only the plain item is read.mainas of 2.2.34 (0c94df7): it merges after one test conflict next to feat: add an Extra Usage Today widget #811's probe import, resolved by keeping both.bun run lintis clean, andbun teston Linux CI gives 4402 pass, 0 fail (4403 onmain). The built CLI under Bun 1.4.4 and Node 26.13.0 agrees across runtimes and matchesmainin plain and Powerline mode, and the TUI opens and exits cleanly under both. It adds no JavaScript or Node API newer than Node 14, the build's target.usage-token.test.tsfails to load onmainand here (the existingvi.mockhas nospawnexport); inusage-fetch.test.tsthe 2 new probe tests fail with the same extensionless-import error as every probe test onmain.dist/under Node 26.10.0 and Bun 1.4.2: plain and Powerline renders agree and matchmainfor the baseline configs, and the TUI opens and exits under both. With the stubbed Keychain, both runtimes send only the default profile's own token.