perf: cache the keychain token until expiry to cut authorization prompts - #2
Merged
Merged
Conversation
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.
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
Claude Code-credentialsitem on every token refresh (~hourly, confirmed: the item'smdatmoves whilecdatstays 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).AppState.cached_credentials; re-read the Keychain only when the token nearsexpires_at(60s margin) or after a 401 invalidates the cache. Reads drop from every 180s to ~once per token lifetime.claude setup-token(that token 403s on the usage endpoint). Recorded as a future slice inkeychain/CONTEXT.md.Test plan
cargo test— newtoken_expiredtest covers the expiry margin, past/future, and unknown-expiry (treated as expired)bun tauri dev: app reads the Keychain via the new cache path, caches, fetches, and showsstatus=Normalwith real gauges--types+--lintclean; diff is Rust + CONTEXT.md only (TS untouched)--buildon a clean environment (the local vite build hits an unrelated rolldown/bun crash)