Agent Provider Authentication Errors#
Provider authentication errors occur when an LLM provider rejects the agent's API key with a 401 Unauthorized response — most commonly after key rotation installs a stale or invalid credential, or when a Daytona sandbox delivers a placeholder secret before substitution completes (a "credential race"). Prior to the fixes described here, these failures silently surfaced as ordinary assistant replies — the turn returned HTTP 200 with a normal stream of frames, an empty errors array, and the raw rejection text embedded in the assistant message — making them invisible to retries, monitors, and release-gate assertions.
Detection: conciseError and AUTH_REFUSAL#
Auth detection lives in services/runner/src/engines/sandbox_agent/errors.ts. The central predicate is:
AUTH_REFUSAL = /authentication required|invalid api key|unauthorized|(?<!\d)401(?!\d)/i
The digit-adjacency guard (?<!\d)401(?!\d) prevents false positives when 401 appears inside a larger number (e.g., a timestamp in a file path). This was a real bug: any message containing 401 as a substring was being misclassified as a model auth failure.
When AUTH_REFUSAL matches, conciseError emits a human-readable message:
{harness}: model authentication failed — add {keyHint} to the project vault, or log in (OAuth).
Credential-Race Classification#
On Daytona sandboxes, a fresh sandbox injects model secrets at creation time. Due to propagation latency, a direct-provider call made within roughly 60 seconds of sandbox creation may receive a dtn_secret_… placeholder instead of a real key, causing a provider 401.
The classifier in errors.ts (introduced in PR #6408) distinguishes this from genuine bad credentials using three tiers of evidence:
| Evidence | Condition |
|---|---|
| Echoed placeholder | Provider response body contains an unmasked dtn_secret_… string |
Masked-echo + AUTH_REFUSAL | Body echoes a masked form of the placeholder corroborated by an auth refusal |
| Fresh Daytona secret | daytonaCredentialFresh() returns true (secret delivered within 60 s window) |
A qualifying event is classified as credential_delivery_failed and emits retry guidance. To avoid an infinite retry loop for genuinely bad credentials, this classification fires at most once per session. Subsequent identical failures fall through to the ordinary add-a-key advice. The once-per-session counter is held in session continuity state.
The preflight helper deliversModelSecretOnCreate confirms that delivery-on-create evidence applies only when: isDaytona=true, sandboxMode="create", and hasModelSecretCandidate=true — explicitly excluding reconnects and local/plaintext-env runs.
A follow-up fix (PR #6422) further narrowed the race path to exclude known runner-originated 401s (tool calls, attachment operations, session-record operations). Explicit dtn_ placeholder evidence remains outside the once-per-session budget — repeated echoes continue to classify as delivery failures regardless.
Surfacing Silent Turns (Pi Transcript Recovery)#
A separate gap existed on Daytona: the Pi harness could record a provider error in its transcript while the ACP bridge ended the turn cleanly with no content. The runner then showed "The agent produced no output." — another silent failure.
PR #6137 adds recovery logic in pi-error.ts and run-turn.ts:
- Before prompting, the runner captures the current Pi transcript cursor.
- If a turn ends with
end_turn, has no output, and emitted no tool call, the runner probes for a newly appended assistant error record since that cursor. - The transcript is read through the sandbox daemon file API while the sandbox is still alive. Session ID and workspace
cwdare verified; unreadable or partial data does not trigger a stale-error fallback. - A recovered provider error fails the run and is emitted as an error event and trace entry before the terminal
doneframe.
Key Source Files#
| File | Purpose |
|---|---|
errors.ts | conciseError, AUTH_REFUSAL pattern, credential-race classifier |
credential-preflight.ts | deliversModelSecretOnCreate predicate |
pi-error.ts | Pi transcript recovery for silent turns |
run-turn.ts | Turn orchestration; wires recovery and classifier into both normal and Pi paths |
session-continuity.ts | Holds the once-per-session credential-race report counter |
Related Issues and PRs#
- Issue #6022 — root bug report: auth failure surfaced as ordinary agent reply
- PR #5303 — fix
401false-positive substring match - PR #6137 — surface Pi transcript errors on Daytona
- PR #6408 — credential-race classifier, once-per-session budget
- PR #6422 — narrow race path, exclude runner-originated 401s