Skip to content

fix(usage): read the Claude sign-in that expires later, keychain item or credentials file - #277

Merged
devswha merged 3 commits into
devswha:mainfrom
ddotz:fix/usage-claude-fresher-sign-in
Oct 1, 2026
Merged

devswha merged 3 commits into
devswha:mainfrom
ddotz:fix/usage-claude-fresher-sign-in

Conversation

@ddotz

@ddotz ddotz commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Change

On a Mac, Claude's usage takes the Claude Code-credentials keychain item whenever it can read it, and uses ~/.claude/.credentials.json only when the item is missing or locked (server/usage.ts, claude provider).

Claude Code started outside the desktop session (over SSH, by a detached multiplexer or a launchd service) cannot write the login keychain, so it refreshes only the credentials file. The item keeps an access token that expired hours ago, and Claude Code keeps refreshing only the file, so the item never catches up. A server that can read the item then shows Claude as expired while Claude Code is signed in and working.

Now both stores are read and the sign-in that expires later wins; on a tie the keychain item still wins, as before. Cursor's usage already uses this rule for its CLI and app sign-ins. Both stores are still only read: nothing is refreshed, written or run.

Merge order with #255: this should land before or together with #255. #255 makes the item readable from a server outside the desktop session. Without this change, a server and Claude Code that both run outside the desktop session would go from working (the file is used because the item reads as locked) to expired (the stale item wins).

Validation

  • Reproduced on a fresh clone of main (ef90055), unchanged, on macOS with Claude Code running in herdr panes started outside the desktop session:
    • Expiry only (tokens not printed): keychain item expiresAt 2026-10-01T12:27Z (in the past); credentials file expiresAt 2026-10-01T23:25Z (valid).
    • new UsageService(systemUsageContext(), [claude]).report(true) run in the desktop session (a one-shot launchctl bootstrap gui/<uid> job): problem: "expired", no windows. Run outside it, security exits 36, the file is used and the session and week windows show.
    • The same desktop-session run on this branch: problem: null, with the session and week windows.
  • New tests: a stale item with a fresher file reads the file (fails on main); a fresher item with an older file still reads the item. The existing equal-expiry "prefers the Claude keychain item" test is unchanged and passes.
  • bun run generate:types --check, bun run typecheck, bun run build: pass.
  • bun run test:unit: 727 pass, 1 skip, 0 fail (main: 725 pass, 1 skip, 0 fail).

@coderabbitai

coderabbitai Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 08819030-0fb5-4ee6-9af7-ed2415494dac

📥 Commits

Reviewing files that changed from the base of the PR and between 243677f and c187295.

📒 Files selected for processing (1)
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed Claude usage incorrectly appearing expired on macOS when Claude Code is signed in. The app now checks both the keychain and credentials file, choosing the credential that expires later when both expiration dates are known. If either date is unknown, it prefers the keychain credential; if no usable keychain sign-in is available, it can use the credentials file. Credential sources remain read-only.

Walkthrough

Claude sign-in discovery checks both the macOS keychain and .credentials.json. When both credentials have known expiries, it selects the one that expires later. Tests cover credential selection and fallback cases.

Changes

Claude Credential Freshness

Layer / File(s) Summary
Credential selection and validation
server/usage.ts, server/usage.test.ts, CHANGELOG.md
Discovery checks both credential sources and selects the file credential only when both expiries are known and the file credential expires later. Tests cover expiry orderings and a keychain credential with no expiry. The changelog records the fix and states that both sources remain read-only.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to c1872

The change selects fresher Claude credentials while preserving keychain preference when expiry cannot be compared. No actionable merge-blocking risk remains, subject to normal checks.

Architecture Summary

Architecture risk: 🔵 Low · up to 24367

The change affects 2 systems.

Changed systems: server, CHANGELOG.md

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — server (service) was modified; 2 changed files map to changed impact.
  • observed — CHANGELOG.md (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in CHANGELOG.md: Adds an Unreleased fix entry for Claude credentials on macOS: the app now uses the keychain item or credentials file with the later expiration, rather than preferring a readable keychain item that may be stale. The entry states that both sources remain read-only.
  • observed — Modified behavior in server/usage.test.ts: Added a test expecting Claude usage to authenticate with the credentials-file token when the keychain token has expired, with no reported problem.
  • observed — Modified behavior in server/usage.test.ts: Added a test expecting the unexpired keychain token to be used when the credentials-file token has expired.
  • observed — Modified behavior in server/usage.test.ts: Added tests expecting the later-expiring token to be selected when both Claude sign-ins are unexpired, and an unexpired keychain token with no expiry to be selected over an expired file token; the latter report is expected to have no problem.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the Claude usage fix and the later-expiry credential selection behavior.
Description check ✅ Passed The description includes the required Change and Validation sections. It explains the problem, resulting behavior, merge-order dependency, reproduction steps, tests, and command results.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Comment @coderabbitai help to get the list of available commands.

@devswha
devswha merged commit 03fb8b0 into devswha:main Oct 1, 2026
11 checks passed
devswha added a commit that referenced this pull request Oct 1, 2026
Prepares v0.3.41.

- `package.json` and `herdr-plugin.toml` go to 0.3.41.
- The `[Unreleased]` notes move under `[0.3.41] - 2026-10-02`; compare
links added.
- 19 entries that had no PR link get one, with `by @login` for outside
contributors (#262 by @diegolhambi; #255, #277, #278, #279 by @ddotz).

After the merge: `gh workflow run release.yml --ref main -f
version=0.3.41`.
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.

2 participants