Repository navigation
fix(claude): show an API error Claude Code records as its answer as a failed request - #3749
Conversation
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
Merge passMerged head
Two reviewers at this head: Claude (Opus) CLEAR, Codex Terra CLEAR. When a Claude Code request fails (an invalid key, an overloaded API), Claude Code records the error text as an assistant entry flagged Left, LOW (both): Reports: |
…ther than DONE, with the failure alert (#3949) ## Change When the API refuses a request (a rate limit, a cooldown, an overloaded model), Claude Code ends the turn with an assistant entry marked `isApiErrorMessage: true`. #3749 shows that entry in Chat as a failed request, but the sidebar still reads the turn as a green DONE and sends a finish alert (#3749: "Not in this PR: the sidebar status of such a turn"). This applies #3733's Codex rule, which #687 set for OmO and GJC, to Claude Code: an error is a rest, not a finish. - `server/claude-transcript.ts` `claudeTurnEnd` / `claudeLastTurnEnd`: the last entry of the session's own that tells how its turn stands (last 256 KB): `failed` for an `isApiErrorMessage` answer, `answered` for an answer, `running` for a prompt, a tool call or a tool result. Subagent (`isSidechain`), meta, compaction-summary and local-command entries tell nothing. - `server/core/status.ts`: as a Claude pane this server saw work comes to rest, its transcript (the one the subagent tracker found) is read at once. An API error makes the rest a failed one: `completions.observe(…, "failed")`, the waiting finish alert called off by the status change, then `push.onFailed`. herdr's own DONE or `unknown` for the pane reads as idle until it works again (`failedRests`, shared with Codex). An answer after it (a retry that got through) ends the turn on the `answered` path, which is a finish. An end written after the rest was told is read at the next snapshot. Only a turn this server saw run is read, and an answer ends what it saw, so a pane found at rest after a restart, or a turn that began while status events were lost, tells no failure. When the failed rest is held on the turn's background work (`background_wait`), the failure alert waits until that wait ends with no turn after it, since `onFailed` skips a pane the alerts take for working. Reproduce on main: a Claude Code pane whose account is rate limited (or any 429/529); send a prompt; the transcript ends with the `isApiErrorMessage` entry (`server/fixtures/claude-api-error-2.1.321.jsonl`) and the sidebar shows DONE with a finish alert. Related: #3733 (Codex), #3749 (Claude chat part), #3760 (the Codex follow-up about an end written after herdr reports rest; the Claude path reads such a late end at the next snapshot). ## Validation - New `server/core/claude-status-hub.test.ts` (8): API error → idle and one failure alert, never DONE; a retry that answers after the failure → DONE; an error a retry answered before the rest → DONE; a normal turn → DONE with no failure alert; an error written after the rest was told → read at the next snapshot, then a later answer finishes; a failed turn held on its subagent → failure alert once the wait ends; a later turn's failure not seen run → nothing told; a pane found at rest after a restart → nothing told. The last three fail with their part of the fix removed. - `server/claude-api-error.test.ts`: `claudeTurnEnd` cases. - Each file on its own with `HERDR_TEST_MODE=unit bun test`: claude-status-hub, claude-api-error, codex-status-hub, status, letta-status-hub, completion, claude-subagents, push, conversation, server/conventions, scripts/conventions, scripts/changelog-fold: all pass. - `bun run typecheck`: pass. - Not run: `bun run check fast` (build step), the full unit suite to the end, and the integration suite (no isolated herdr/PTY host here). No contract test with a live Claude Code was added. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <[email protected]> Co-authored-by: devswha <[email protected]>
Change
When the API refuses a request (a rate limit, an overloaded model), Claude Code writes the error into its transcript as an assistant message whose text is the error, marked
isApiErrorMessage: true(witherrorandapiErrorStatus). Ovis drew it as an ordinary answer, so a turn that failed read as if Claude had answered "API Error: …" (agent chat audit, Claude row C20).server/claude-transcript.ts: the text of an entry markedisApiErrorMessagebecomes the failed request part every reader already uses ({ kind: "text", text, failed: true }, as omp, pi, OmO, Codex (fix(codex): show a turn Codex ended in an error, and read it at rest rather than DONE #3733) and dsh (fix(dsh): show the error a dsh turn ended on as a failed request, and a stopped turn as such #3742) write theirs), so Chat shows "Request failed" with Claude Code's own words, never as the answer or a final answer. Both the whole-page and incremental readers go through it.server/fixtures/claude-api-error-2.1.321.jsonl: a 429 as Claude Code 2.1.321 recorded it on this PC, the error entry kept as written (folder and branch names replaced), the prompt before it shortened.Not in this PR: the sidebar status of such a turn, which is herdr's for Claude Code.
Screenshots
Before: the same turn rendered as an answer reading "API Error: Request rejected (429) …" (the audit's finding). After:
scripts/claude-api-error-qa.ts: the fixture under an isolated HOME, a pane in an isolated herdr session reporting that Claude session, this branch's server and a browser at 1280 and 390. A live Claude run was not made: a cheap one cannot be made to fail without spending the owner's account into a rate limit.Validation
server/claude-api-error.test.ts"shows the API error Claude Code recorded as a failed request" fails on main, passes after; "keeps an answer that only quotes an error" passes on both.HERDR_TEST_MODE=unit bun test ./server/claude-api-error.test.ts ./server/claude-incremental.test.ts: pass.bun scripts/claude-api-error-qa.ts(heavy queue, browser): pass.bun run check fast(heavy queue): pass.