happier icon indicating copy to clipboard operation
happier copied to clipboard

[bug] Remote session silently swallows messages on claude provider 401 (credentials.json OAuth) — older CLI surfaced it

Open danljungstrom opened this issue 5 months ago • 3 comments

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:

  1. Key mismatch in the classifier. claudeRemoteAgentSdk.ts → classifyClaudeConnectedServiceRuntimeAuthFailure (isClaudeRuntimeAuthFailureEvidence) only inspects 401 under keys api_error_status / status / statusCode / status_code. The SDK's api_error system event carries the code under key error_status (plus a short error string like "Connection error."). Neither matches → classifier returns null → onRuntimeAuthFailureEvent never fires.
  2. Connected-service gate. Even if it fired, surfaceClaudeConnectedServiceRuntimeAuthFailure returns early when there is no connected-service selection. Installs that auth via shared ~/.claude/.credentials.json OAuth have connectedServiceSelection: 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

  1. Remote session on the current CLI, claude authed via credentials.json OAuth (no connected-service selection).
  2. Make claude return 401 (expire the OAuth token with a failed refresh, or invalidate it).
  3. Send a message.
  4. 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

  1. Add error_status to the key set in isClaudeRuntimeAuthFailureEvidence (the SDK api_error event uses it).
  2. Surface provider auth failures regardless of connected-service selection — surfaceClaudeConnectedServiceRuntimeAuthFailure should not early-return for credentials.json-OAuth sessions; route to surfacePrimarySessionRuntimeIssue so 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; inbound system event with keys attempt/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 manual claude /login recovered it.

danljungstrom avatar Jun 20 '26 15:06 danljungstrom

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.

happier-bot avatar Oct 11 '26 12:10 happier-bot

Thanks. Details from the original incident:

  • CLI: 0.2.8-dev.11 (the dev build auto-installed on the host that day; possibly 0.2.8-dev.10 if the incident preceded that morning's update). A second session on the same host running 0.2.1 surfaced the 401 correctly.
  • Relay: self-hosted server-v0.2.7-dev.10 (SQLite)
  • Auth: ~/.claude/.credentials.json OAuth, 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.

danljungstrom avatar Oct 12 '26 13:10 danljungstrom

Perfect, thank you!

leeroybrun avatar Oct 12 '26 16:10 leeroybrun