Repository navigation
Stream Anthropic replies: per-call state, every block kept, pause_turn - #634
Conversation
Anthropic's ChatStream kept its parse state in unit globals, never set StatusCode, dropped every block but text and tool_use, and did not continue a pause_turn. #633 made the tool loop stream whenever a provider says SupportsStreaming -- which Anthropic always did -- so under `pasclaw acp` every Anthropic turn went through it: two sessions streaming at once got each other's text, a reply could be taken for a provider failure (StatusCode left unset) and shown as the error, and in the e2e run a tool call and a pause_turn were lost. ChatStream now reads the stream with a TAnthropicStreamAcc, one per call: - text pieces passed on as they arrive, text blocks (and pause_turn rounds) joined with a line break as Chat joins them; - tool calls from content_block_start's id/name and input_json_delta fragments, the input re-serialised as Chat reports it; - thinking (with its signature), citations, server_tool_use and server tool result blocks kept block for block, so a pause_turn is continued by re-sending the round's content exactly as Anthropic sent it, as Chat does from the JSON body (up to the same cap); - usage from message_start and message_delta (input, output, cache read and creation), summed across rounds; - StatusCode set; a non-2xx reply is the error with the server's body; an `error` event inside a 200 stream fails the turn, overloaded_error as 529 so the loop retries it like any 5xx; - a reply with no events (something in between answered with one JSON body) read as Chat would. Thinking is kept but not shown, as Chat drops it from the text. PostStreaming and its plain-procedure callback had no other users and are gone; PostSSE is the one streaming POST. ContinuePausedTurn shares AppendAssistantContent with the streamed continuation. Tests: streaming_tests pins ParseAnthropicStream (text blocks, thinking and signature, citations, a tool call from fragments, usage, the content array for a continuation, an error event). acp_e2e_test gains a fake Messages API: text shown while the model is still writing, a tool call from fragments, a pause_turn continuation re-sending the server-tool blocks as they came, an overloaded error event, and two sessions streaming at once, piece by piece interleaved, each getting only its own text. Against the old code 6 of those 12 checks fail. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7c8a02c4f6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| PostSSE(URL, Body, Headers, 120, Acc.OnEvent, Status, ErrMsg, Raw, Events); | ||
| Result.StatusCode := Status; | ||
|
|
||
| if (Status < 200) or (Status >= 300) then |
There was a problem hiding this comment.
Treat transport failures after HTTP headers as stream errors
If an Anthropic stream times out or loses its connection after receiving HTTP 200, the Indy backend's PostJSONToStream exception handler returns Http.ResponseCode (still 200) together with a nonempty ErrMsg. This path ignores that error because it only checks the status and explicit SSE error events, so the accumulated partial response is returned as successful. The tool loop can consequently save a truncated answer or execute tool calls from an interrupted turn instead of triggering its provider-error handling. Check transport errors before accepting the streamed response, and return a retryable failure status when one occurred.
Useful? React with 👍 / 👎.
Anthropic replies now stream token by token, under
pasclaw acpand anywhere elseChatStreamis used. This also fixes a bug #633 introduced for Anthropic users of ACP.The bug on
main#633 said Anthropic stayed unstreamed, and that was wrong. The tool loop streams whenever a provider reports
SupportsStreaming, and Anthropic always reportedTrue. So since #633, every Anthropic turn under ACP has gone through the oldChatStream, which:StatusCode;tool_use;pause_turn.Against that old code, 6 of the 12 new e2e checks fail:
StatusCodewas left unset.pause_turncontinuation were lost.Not everything #633 said was wrong: Anthropic's
Chatdrops thinking blocks too, so streaming doesn't lose anythingChatkeeps.The fix
ChatStreamreads the stream with aTAnthropicStreamAcc, one per call, so concurrent streams share nothing.pause_turnrounds, are joined with a line break, the same wayChatjoins them.content_block_start's id and name plus theinput_json_deltafragments. The input is re-serialised the wayChatreports it.server_tool_useand server-tool result blocks are kept block for block. Apause_turnis continued by re-sending the round's content exactly as Anthropic sent it, asChatdoes from the JSON body, up to the same cap.message_startandmessage_delta(input, output, cache read and creation), summed across rounds.StatusCodeis set.errorevent inside a 200 stream fails the turn.overloaded_errormaps to 529, which the loop retries like any 5xx.Chatreads it.Smaller changes:
PostStreamingand its plain-procedure callback had no other users and are removed.PostSSEis the one streaming POST.ContinuePausedTurnnow sharesAppendAssistantContentwith the streamed continuation.docs/acp.md: Anthropic streams. Gemini and the relay provider still send each round whole.Tests
make test-streaming:ParseAnthropicStreamon a canned stream. Covers text blocks joined, thinking kept with its signature and not shown, a citation, a tool call frominput_jsonfragments, usage from both events, the content array rebuilt for a continuation, and an error event.make test-acp-e2e, against a new fake Messages API that streams real Anthropic SSE:pause_turncontinuation re-sends theserver_tool_use, the search result (with itsencrypted_content) and the text as they came, and the answer reads on;tool_usewas rebuilt from 6-character fragments and ran;make testpasses on FPC 3.2.2. Delphi (dcc64) isn't available here.Not in this PR
Anthropic thinking is kept for the conversation but not shown. ACP has an
agent_thought_chunkupdate for exactly this, so streaming thinking to the editor would be a small follow-up.Generated by Claude Code