Skip to content

fix(usage): read only the default profile's own credentials on macOS - #816

Open
eric-engberg wants to merge 3 commits into
sirmalloc:mainfrom
eric-engberg:fix/usage-default-profile-keychain
Open

eric-engberg wants to merge 3 commits into
sirmalloc:mainfrom
eric-engberg:fix/usage-default-profile-keychain

Conversation

@eric-engberg

Copy link
Copy Markdown
Contributor

What

On macOS, the usage widgets in the default profile (no CLAUDE_CONFIG_DIR) now use only that profile's own login: the plain Claude Code-credentials Keychain item, then ~/.claude/.credentials.json. They no longer search the Keychain for other Claude 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.json holds 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-keychain and took the newest Claude 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 when CLAUDE_CONFIG_DIR is 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-work is 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.json held the default profile's own login. Reproduced with a stub security and a fake usage API:

  • before: the other login's token was sent (find plain → dump-keychain → find -<hash>)
  • after: one plain lookup, then the file

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

  • getUsageCredentials resolves 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.
  • If the plain item exists but has no 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.
  • The Keychain dump and its parsing (candidate list, timestamp decoding, sort) are removed, along with their tests.
  • Docs (docs/USAGE.md, docs/DEVELOPMENT.md) and the no-credentials backoff comment are updated.

Demo

Default profile, with ~/.claude-work signed in to another account (fake logins, a fake usage API answering by token, a stubbed Keychain; nothing is fetched).

  1. Default login in ~/.claude/.credentials.json with no Keychain item: Block and Weekly come from the payload, and Overage Used should be that login's own $4.00, not $123.45.
  2. API-key session whose Keychain item holds only MCP tokens: [No credentials] instead of 77% / 66% / $123.45.

Powerline

Before:

Default profile usage before the fix, Powerline

After:

Default profile usage after the fix, Powerline

Plain

Before:

Default profile usage before the fix, plain

After:

Default profile usage after the fix, plain

Testing

  • New and changed tests fail on 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, no CLAUDE_CONFIG_DIR, a stubbed security, and the platform forced to darwin so they also run on Linux CI. They assert that the request carries Bearer default-access, or reports no-credentials with 0 requests, and that only the plain item is read.
  • On main as 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 lint is clean, and bun test on Linux CI gives 4402 pass, 0 fail (4403 on main). The built CLI under Bun 1.4.4 and Node 26.13.0 agrees across runtimes and matches main in 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.
  • 3 tests for the removed dump parsing go, and 3 are added.
  • Under Node (vitest): usage-token.test.ts fails to load on main and here (the existing vi.mock has no spawn export); in usage-fetch.test.ts the 2 new probe tests fail with the same extensionless-import error as every probe test on main.
  • Built dist/ under Node 26.10.0 and Bun 1.4.2: plain and Powerline renders agree and match main for 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.

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
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