Skip to content

fix(llm): catch the stream edge cases that still lost answers or hung turns - #393

Merged
mrnasil merged 2 commits into
mainfrom
fix/llm-stream-edges
Nov 2, 2026
Merged

mrnasil merged 2 commits into
mainfrom
fix/llm-stream-edges

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 2, 2026

Copy link
Copy Markdown
Member

Fixes (remaining findings from the LLM-stream audit)

  • Anthropic: cut-off stream. AnthropicSse tracks whether any event arrived and whether message_stop was seen. A clean close in between is now a retryable unexpected EOF before message_stop, the same check ChatSse already has. Before, a truncated answer, and its half-written tool call, were accepted.
  • Anthropic: error type kept. An error event now keeps its type, e.g. (api_error) Internal server error. The classifier also recognises a bare api_error and the phrase "internal server error" as server errors, so these are retried.
  • Chat completions: error chunk. A data: {"error":{…}} chunk becomes StreamEvent::Error, which the turn's retry path classifies. Nothing after it is read. Before, it was ignored, and a later finish_reason passed the partial reply off as an answer.
  • Stalled stream. Agent::stream_idle (300 s) is raced against each next(). A provider that stops sending without closing yields a Timeout error, retried like a drop. Before, the turn hung until Esc.
  • Still cut off after every continuation. The text is kept as the answer and the tool calls are dropped, with a status line. Before, they ran with possibly half their arguments.
  • Unclassified errors. They show readable_reason: the provider's own reason, with the prefix kept and capped at 300 chars. Before, the raw JSON body was shown.

Left as is: Codex and Code Assist still accept a clean close without their final event. Their parsers and tests document that the backends do this.

Tests

  • Agent:
    • The repeated-cut-off test fails on main because the write runs; it now passes.
    • The stalled-stream test fails on main because the turn hangs; it is now retried and finishes.
  • LLM:
    • Anthropic: EOF before message_stop is a retryable error; a stop and an empty stream close cleanly; the error type is kept and classified as retryable.
    • Chat completions: the error chunk is reported and the finish after it is not read; stream_error_message variants.
    • readable_reason.
  • Locally:
    • llm (532), agent (727), fmt, clippy and budgets are green.
    • The CLI tests show only the 4 known Windows-host failures.
    • cargo llvm-cov on llm and agent shows no uncovered added line.

🤖 Generated with Claude Code

mrnasil and others added 2 commits November 3, 2026 01:46
… turns

- Anthropic: a stream that closed before message_stop was taken as a
  finished answer (and a half-written tool call ran). It is now a
  retryable drop, as the chat-completions parser already does.
- Anthropic error events kept only their text; the type (api_error,
  overloaded_error) that tells a retryable failure from a fatal one was
  dropped. It is kept, and the classifier reads it bare as well.
- Chat completions: a `data: {"error":{…}}` chunk was ignored, so the
  real cause was lost and a following finish_reason passed the partial
  reply as an answer. It now ends the stream as a provider error event.
- A stream that stopped sending without closing held the turn until Esc.
  Five minutes of silence now counts as a dropped stream and is retried.
- A response still cut off at the output limit after every continuation
  ran its tool calls, the last possibly with half its arguments. The
  text is kept as the answer and the calls are dropped.
- An unclassified error showed the provider's raw JSON body; it now
  shows the provider's reason, capped.

Codex and Code Assist keep accepting a clean close without their final
event: their parsers document that the backends do send that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 212c5ce into main Nov 2, 2026
13 of 14 checks passed
@mrnasil
mrnasil deleted the fix/llm-stream-edges branch November 2, 2026 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant