[bug] Remote session silently swallows messages on claude provider 401 (credentials.json OAuth) — older CLI surfaced it
Summary
On a self-hosted remote session, when the claude provider returns 401 (expired/unrefreshed OAuth token), the current CLI (0.2.8-dev.x) silently swallows the user's message — the UI accepts it, it's dropped, the turn finalizes as completed, and no error surfaces. An older version (0.2.1) surfaced it: Failed to authenticate. API Error: 401 The socket connection was closed unexpectedly. For more information, pass verbose: true in the second argument to fetch().
Two concurrent sessions on the same machine (sharing ~/.claude/.credentials.json): the 0.2.1 session showed the 401; the new-version session went silent. claude /login (re-mint token) fixed both.
Root cause (traced)
The message is delivered to the agent (deliveredMaterializedMessage:true); it dies inside the claude Agent-SDK turn. The 401 arrives as an in-band system/api_error event (SDK retries internally, "attempt 1/11") rather than a thrown error. Two independent gates then fail open, so nothing surfaces:
- Key mismatch in the classifier.
claudeRemoteAgentSdk.ts→classifyClaudeConnectedServiceRuntimeAuthFailure(isClaudeRuntimeAuthFailureEvidence) only inspects 401 under keysapi_error_status/status/statusCode/status_code. The SDK's api_error system event carries the code under keyerror_status(plus a shorterrorstring like "Connection error."). Neither matches → classifier returnsnull→onRuntimeAuthFailureEventnever fires. - Connected-service gate. Even if it fired,
surfaceClaudeConnectedServiceRuntimeAuthFailurereturns early when there is no connected-service selection. Installs that auth via shared~/.claude/.credentials.jsonOAuth haveconnectedServiceSelection: null, so this surface path is also dead.
Net: the turn finalizes completed, no sendSessionEvent error is emitted, the message is lost.
Why 0.2.1 surfaced it
0.2.1's SDK path threw on the 401 (isClaudeAgentSdkAuthenticationError matched the thrown message: API Error: 401, Failed to authenticate, OAuth token has expired); the throw propagated to the launcher catch → exit-code-1 branch → sendSessionEvent({type:'message', ...}) via the UI error formatter → surfaced. The move to in-band structured classification (+ internal retry + connected-service gate) is what introduced the silent swallow.
Environment
Self-hosted, remote sessions, claude auth via ~/.claude/.credentials.json OAuth (no connected-service injection). New-version session affected; 0.2.1 session on the same host/credentials surfaced correctly.
Repro
- Remote session on the current CLI, claude authed via credentials.json OAuth (no connected-service selection).
- Make claude return 401 (expire the OAuth token with a failed refresh, or invalidate it).
- Send a message.
- Observe: message accepted by UI, dropped, turn ends
completed, no error shown.
Expected
Provider auth failures (401) surface to the user (as 0.2.1 did) so they know to re-auth — including on credentials.json-OAuth installs with no connected-service selection.
Suggested fixes
- Add
error_statusto the key set inisClaudeRuntimeAuthFailureEvidence(the SDK api_error event uses it). - Surface provider auth failures regardless of connected-service selection —
surfaceClaudeConnectedServiceRuntimeAuthFailureshould not early-return for credentials.json-OAuth sessions; route tosurfacePrimarySessionRuntimeIssueso it's not a silent no-op.
Evidence (logs, anonymized)
- Client 401:
[ERROR] API error (attempt 1/11): 401 401 The socket connection was closed unexpectedly. For more information, pass verbose: true ...(and an earlier{"type":"authentication_error","message":"Invalid authentication credentials"}). - Runtime:
[claudeRemoteAgentSdk] Claude runtime auth diagnostic ... connectedServiceSelection:null; inboundsystemevent with keysattempt/error/error_status/max_retries/retry_delay_ms;[pendingQueue] ... deliveredMaterializedMessage:true;[pendingQueue] turn-end drain trigger ... terminalStatus:"completed"→task_complete; no error event emitted. Two consecutive turns dropped this way before a manualclaude /loginrecovered it.
Thanks for tracing the Claude authentication-error path. We’re investigating why the error is not shown and the prompt appears lost. Please share the CLI version and a small redacted log excerpt showing the HTTP status and final turn outcome, without credentials or prompt contents.
Posted on behalf of @leeroybrun.
Thanks. Details from the original incident:
- CLI:
0.2.8-dev.11(the dev build auto-installed on the host that day; possibly0.2.8-dev.10if the incident preceded that morning's update). A second session on the same host running0.2.1surfaced the 401 correctly. - Relay: self-hosted
server-v0.2.7-dev.10(SQLite) - Auth:
~/.claude/.credentials.jsonOAuth, no connected-service selection - Redacted log excerpt:
[ERROR] API error (attempt 1/11): 401 ... socket connection was closed unexpectedly
[claudeRemoteAgentSdk] Claude runtime auth diagnostic ... connectedServiceSelection:null
inbound system event keys: attempt/error/error_status/max_retries/retry_delay_ms
[pendingQueue] ... deliveredMaterializedMessage:true
[pendingQueue] turn-end drain trigger ... terminalStatus:"completed"
Re-checked against current dev: 1f1a46df6a (first shipped in 0.2.10-dev.1 / 0.2.11) fixes the reported path. Every inbound SDK message, including the final result, now goes through the auth classifier. The SDK's terminal auth evidence (an authentication_failed assistant error, or api_error_status: 401 on the result) trips it, and credentials.json-OAuth sessions with no connected-service selection now fail the turn with auth_error instead of returning early. The related tests pass.
One narrow case I could only infer from the code: if a 401 retry at attempt < max is followed by a result whose only auth signal is free text in result (no api_error_status, no authentication_failed message), the turn still finalizes completed. claudeRemoteAgentSdk.workState.test.ts:291-315 treats that as intended. The API error text still reaches the transcript, so it's no longer silent. I haven't seen the real CLI produce that shape. Unless you think that case is worth covering, I'm fine closing this as fixed.
Perfect, thank you!