Skip to content

perf: cache the keychain token until expiry to cut authorization prompts - #2

Merged
kvnwolf merged 1 commit into
mainfrom
worktree-cache-token-until-expiry
Aug 1, 2026
Merged

perf: cache the keychain token until expiry to cut authorization prompts#2
kvnwolf merged 1 commit into
mainfrom
worktree-cache-token-until-expiry

Conversation

@kvnwolf

@kvnwolf kvnwolf commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • Cuts the recurring macOS Keychain prompt on the installed app. Claude Code rewrites its Claude Code-credentials item on every token refresh (~hourly, confirmed: the item's mdat moves while cdat stays fixed), which resets the item's ACL and evicts mad-eye from "Always Allow". Since the poll re-read the item every 180s, the next poll after any refresh re-prompted — regardless of how mad-eye is signed (the item belongs to Claude Code).
  • Fix: cache the read in AppState.cached_credentials; re-read the Keychain only when the token nears expires_at (60s margin) or after a 401 invalidates the cache. Reads drop from every 180s to ~once per token lifetime.
  • Honest scope: this mitigates, it does not eliminate. Fully removing the prompt needs mad-eye to own its own OAuth token — verified out of reach via claude setup-token (that token 403s on the usage endpoint). Recorded as a future slice in keychain/CONTEXT.md.

Test plan

  • cargo test — new token_expired test covers the expiry margin, past/future, and unknown-expiry (treated as expired)
  • Live bun tauri dev: app reads the Keychain via the new cache path, caches, fetches, and shows status=Normal with real gauges
  • --types + --lint clean; diff is Rust + CONTEXT.md only (TS untouched)
  • CI runs --build on a clean environment (the local vite build hits an unrelated rolldown/bun crash)

Claude Code rewrites its "Claude Code-credentials" Keychain item on every
token refresh (~hourly), which resets the item's ACL and evicts mad-eye
from the "Always Allow" grant. Because the poll re-read the item every
180s, the next poll after any refresh re-triggered the macOS authorization
prompt — so the installed, notarized app still prompted intermittently, no
matter how it was signed (the item is Claude Code's, not ours).

Cache the read in AppState.cached_credentials and only re-read the Keychain
when the token nears its expires_at (60s margin), or after a 401 clears the
cache. Reads drop from every 180s to ~once per token lifetime, cutting the
prompts to roughly one per token cycle. An unknown expiry is treated as
expired, so we never keep a token we can't reason about.

This mitigates but does not eliminate the prompt: fully removing it needs
mad-eye to own its own OAuth token (verified out of reach via
`claude setup-token` — that token 403s on the usage endpoint). Documented
as a future slice in keychain/CONTEXT.md.

Verified: cargo test (new token_expired margin test) + live `bun tauri dev`
reads/caches/fetches to status=Normal. The vite --build step is skipped
locally (a rolldown/bun crash unrelated to this Rust-only change); CI runs
it on a clean environment.
@kvnwolf
kvnwolf merged commit a20b9ca into main Aug 1, 2026
2 checks passed
@kvnwolf
kvnwolf deleted the worktree-cache-token-until-expiry branch August 1, 2026 06:21
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