feat(cli): auto-refresh on expiry + retry-once on 401#34
Merged
Conversation
Make the CLI self-heal an expired/stale access token instead of forcing `mirrorstack login`, symmetric with the web client's reactive refresh. - refresh_and_save(refresh_token): exchange the stored refresh token for a rotated access+refresh pair and persist it (expires_at re-derived locally to now+15min, matching the platform's TokenConfig; save is best-effort). - with_refresh_retry(creds, call): run the authed call; on ApiError:: Unauthenticated do exactly ONE refresh_and_save + ONE retry, writing the rotated creds back through &mut Credentials. Non-401 errors pass through; if the refresh itself 401s (refresh token gone) the original Unauthenticated is returned so the caller's terminal session_expired()/login-hint still fires. At most one retry — no loop. - load_or_login_hint() now also refreshes proactively when the loaded token is past expiry (main's version did not check expiry at all). Best-effort: refresh failure falls back to the stale creds so with_refresh_retry retries. Wired through the 6 authed call sites (whoami, module me/get/create/refetch, dev tunnel-token — replacing dev's hand-rolled refresh-and-retry). 67 tests pass (incl. 2 new), clippy clean, cargo fmt clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
5 tasks
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.
Makes the CLI self-heal an expired/stale access token instead of forcing
mirrorstack login— symmetric with the web client's reactive refresh, and the follow-up flagged when we restored the 15-min access-token TTL (api-platform #180).What
refresh_and_save(refresh_token)— exchange the stored refresh token for a rotated pair, persist it (expires_atre-derived to now+15min, matching the platformTokenConfig; save is best-effort/non-fatal).with_refresh_retry(creds, call)— run the authed call; onApiError::Unauthenticateddo exactly onerefresh_and_save+ one retry, writing rotated creds back via&mut. Non-401 passes through; if the refresh itself 401s, the originalUnauthenticatedreturns so the terminalsession_expired()still fires. At most one retry — no loop.load_or_login_hint()now also refreshes proactively on expiry —main's version didn't check expiry at all (that fix lived only on thefeat/monorepo-clibranch), so this also closes that gap onmain.Wired through all 6 authed call sites (whoami; module me/get/create/refetch; dev tunnel-token — replacing its hand-rolled refresh-and-retry).
Verification
cargo build+cargo test(67 pass, incl. 2 new) +cargo clippy+cargo fmt --checkall clean.session_expired, non-401 unchanged), borrow soundness, no token logging.Branches off
main(clean — no backlog stacking).🤖 Generated with Claude Code