Repository navigation
Description
Summary
After a Claude thread receives a terminal authentication_failed response, authenticating Claude successfully in the terminal does not recover that thread. New Claude threads use the refreshed credentials, but the failed thread keeps its existing Claude CLI child and continues with the stale authentication state; the next turn should instead launch a replacement process and resume the same provider conversation.
Versions and environment
- bb 0.39.0
- macOS 26.5.1 (25F80), Node 22.23.1
- Claude Code 2.1.237
- Worktree environment on an enrolled host, accessed through bb Connect
- Verified against
mainat09d72cd22d738a05613c108f4978d8bdb404690d
Steps to reproduce
- Open an existing Claude thread while its terminal authentication is expired.
- Send a turn and observe Claude emit an assistant message with
error: "authentication_failed"followed by anerror_during_executionresult. - Authenticate Claude in the environment terminal and confirm a newly created Claude thread can complete a turn.
- Send another turn in the original failed thread.
Smallest faithful automated reproduction: feed the Claude bridge the synthetic authentication_failed assistant message and failed result, then start another turn on the same bb thread and provider thread ID.
Expected vs actual
Actual:
Failed to authenticate: OAuth session expired and could not be refreshed
The original thread retains the same live Claude SDK query/CLI child after the failed turn. Its next turn is written to that old process, so terminal reauthentication is not picked up.
Expected:
The failed turn remains visible. Before the next new turn, bb closes the stale per-thread Claude session, launches a replacement, and resumes the same Claude provider conversation ID with the refreshed terminal credentials.
Evidence
- Reported thread: https://ymichael.getbb.app/projects/proj_qrv2s45kmq/threads/thr_ughdhvdcdz
- Project ID:
proj_qrv2s45kmq - Thread ID:
thr_ughdhvdcdz - The Claude CLI child for the affected thread started before terminal reauthentication, and its SDK stream remained open afterward. New threads launched after reauthentication succeeded.
- On main, the Claude bridge only replaces a writable session after
streamEnded; an open stream is returned unchanged:bb/plugins/provider-claude-code/src/bridge/bridge.ts
Lines 1190 to 1230 in 09d72cd
const providerThreadId = args.threadSession.providerThreadId ?? args.threadSession.session.getSessionId(); if (!providerThreadId) { return undefined; } const replacementSession = createThreadSession({ liveSettings: args.threadSession.liveSettings, permissionEscalation: args.threadSession.permissionEscalation, // Carries the live mode, so a session replaced after an approved plan // keeps the restored preset instead of dropping back into Plan mode. permissionMode: args.threadSession.permissionMode, approvedPlanPermissionMode: args.threadSession.approvedPlanPermissionMode, providerThreadId, sessionConstructionConfig: args.threadSession.sessionConstructionConfig, sessionOptions: args.threadSession.sessionOptions, sessionPermissionGrants: args.threadSession.sessionPermissionGrants, threadIdRef: args.threadSession.threadIdRef, }); replaceThreadSession({ providerThreadId, replacementSession, reason: "Thread session replaced after Claude SDK stream ended", threadId: args.threadId, threadSession: args.threadSession, }); return replacementSession; } function getWritableThreadSession(threadId: string): ThreadSession | undefined { const threadSession = sessions.get(threadId); if (!threadSession || threadSession.closing) { return undefined; } if (!threadSession.streamEnded) { return threadSession; } return replaceEndedThreadSession({ threadId, threadSession }); } - The generic Codex runtime already treats terminal
unauthorizedprovider errors as account-restart signals:bb/packages/agent-runtime/src/runtime.ts
Lines 266 to 279 in 09d72cd
/** * Codex-shaped recovery signals. These survive graduation deliberately: the * codex bridge passes provider error text and `errorInfo.category` through * verbatim precisely so these runtime tolerances keep matching (see the * comments beside SESSION_NOT_RESTORABLE and the rename path in * codex/bridge/bridge.ts). Do not delete them as "legacy adapter" residue. */ const CODEX_PROVIDER_ID = "codex"; const CODEX_THREAD_PROCESS_KEY_PREFIX = `${CODEX_PROVIDER_ID}\0thread:`; const THREAD_CREATION_REQUEST_TIMEOUT_MS = 2 * 60_000; const CODEX_ACCOUNT_RESTART_PROVIDER_ERROR_CATEGORIES = new Set<ProviderErrorCategory>(["rate-limit", "unauthorized"]); const CODEX_ACCOUNT_RESTART_PROVIDER_ERROR_TEXT_PATTERN = /\b(?:40[19]|429|auth(?:entication|orization)?|credits?|quota|rate[-\s]?limit(?:ed)?|unauthori[sz]ed|usage limit)\b/i; - A bridge regression test at the exact SDK-message path fails before the fix because no replacement query is created; it passes once the auth-failed session is marked for replacement before the next turn.
What you ruled out
- Not a duplicate of claude login doesn't apply to exiting thread and requires restart #1654: that reproduction starts with a rate limit and switches subscriptions; this report starts with a terminal authorization failure and scopes recovery to the
unauthorizederror category. - Refreshed credentials are valid because newly launched Claude threads work.
- The provider conversation is still resumable; restarting bb recovers the thread.
- This is not an SDK stream-end recovery gap: the authentication failure leaves the old SDK stream open, which is why the existing
streamEndedreplacement path does not run.
Suggested priority and effort
Medium — affected users can work around it by restarting bb or starting a new thread, but either interrupts the existing conversation; the bridge-side fix and regression coverage are small.
AGENT GENERATED: by GPT-5
Activity
Restart Claude sessions after authentication failures (#2098)