Skip to content

fix: prevent 401 errors from CLI-refreshed tokens being clobbered - #1286

Merged
Jinwoo-H merged 1 commit into
mainfrom
Jinwoo-H/bug-claude-codex-accounts-intermittently-return
May 1, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
Jinwoo-H/bug-claude-codex-accounts-intermittently-return

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Both CodexRuntimeHomeService and ClaudeRuntimeAuthService performed one-directional auth sync — copying managed credentials to the runtime path on every PTY launch/rate-limit fetch, but never reading back tokens that the CLI refreshed
  • When OAuth tokens expired and the CLI silently refreshed them, the next sync overwrote the fresh tokens with the stale managed copy → intermittent 401 Unauthorized / token_expired errors
  • Adds readBackRefreshedTokens() to both services: before overwriting runtime auth, compare the file to what Orca last wrote; if they differ, the CLI refreshed the token — write it back to managed storage first
  • Adds detectExternalLoginAndUpdateSnapshot() to Codex (already existed on Claude side) for detecting external codex login during managed→system-default transitions
  • Adds clearLastWritten*() methods to prevent re-auth/add-account flows from having fresh tokens clobbered by stale read-back

Test plan

  • TypeScript typecheck passes
  • All 33 existing + new tests pass
  • New tests cover: token read-back, skip on restart, skip on account switch, external login detection, re-auth safety
  • Manual: authenticate a Codex account, wait for token expiry (~2-4 hours), open a new terminal → should not get 401
  • Manual: authenticate a Claude account, same flow
  • Manual: re-authenticate an account → should not lose fresh tokens
  • Manual: run codex login externally while managed account active, then deselect → external login should persist

Fixes #1284

Made with Orca 🐋

…stale managed auth

Both CodexRuntimeHomeService and ClaudeRuntimeAuthService performed one-directional auth sync —
copying managed credentials to the runtime path on every PTY launch and rate-limit fetch, but
never reading back tokens that the CLI refreshed. When OAuth tokens expired and the CLI
silently refreshed them, the next sync would overwrite the fresh tokens with the stale
managed copy, causing intermittent 401 Unauthorized errors.

Adds readBackRefreshedTokens() to both services: before overwriting runtime auth, compare the
file to what Orca last wrote. If they differ, the CLI refreshed the token — write it back to
managed storage first. Also adds detectExternalLoginAndUpdateSnapshot() to Codex (already
existed on Claude side) and clearLastWritten*() methods to prevent re-auth flows from having
their fresh tokens clobbered by stale read-back.

Fixes #1284

Co-authored-by: Orca <[email protected]>
@Jinwoo-H
Jinwoo-H force-pushed the Jinwoo-H/bug-claude-codex-accounts-intermittently-return branch from ba1e5d4 to 2c80621 Compare May 1, 2026 05:16
@Jinwoo-H
Jinwoo-H merged commit 03e7284 into main May 1, 2026
2 checks passed
mralexsaavedra pushed a commit to mralexsaavedra/orca that referenced this pull request May 2, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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.

[Bug]: Claude & Codex accounts intermittently return 401 Unauthorized

1 participant