Skip to content

Stream Anthropic replies: per-call state, every block kept, pause_turn - #634

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pasclaw-desktop-plan-uapzcp
Nov 12, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pasclaw-desktop-plan-uapzcp

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Anthropic replies now stream token by token, under pasclaw acp and anywhere else ChatStream is 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 reported True. So since #633, every Anthropic turn under ACP has gone through the old ChatStream, which:

  • kept its parse state in unit globals;
  • never set StatusCode;
  • dropped every block except text and tool_use;
  • didn't continue a pause_turn.

Against that old code, 6 of the 12 new e2e checks fail:

  • Two sessions streaming at once got each other's text ("Alpha Beta one one alpha beta two two…").
  • A normal reply could be taken for a provider failure and shown as the error, because StatusCode was left unset.
  • A tool call and a pause_turn continuation were lost.

Not everything #633 said was wrong: Anthropic's Chat drops thinking blocks too, so streaming doesn't lose anything Chat keeps.

The fix

ChatStream reads the stream with a TAnthropicStreamAcc, one per call, so concurrent streams share nothing.

  • Text: pieces are passed on as they arrive. Text blocks, and pause_turn rounds, are joined with a line break, the same way Chat joins them.
  • Tool calls: rebuilt from content_block_start's id and name plus the input_json_delta fragments. The input is re-serialised the way Chat reports it.
  • Every block kept: thinking (with its signature), citations, server_tool_use and server-tool result blocks are kept block for block. 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: read from message_start and message_delta (input, output, cache read and creation), summed across rounds.
  • Status and errors:
    • StatusCode is set.
    • A non-2xx reply becomes the error, with the server's body.
    • An error event inside a 200 stream fails the turn. overloaded_error maps to 529, which the loop retries like any 5xx.
    • A reply with no events at all (something in between answered with one JSON body) is read the way Chat reads it.
  • Thinking display: thinking is kept for the conversation but not shown, as without streaming.

Smaller changes:

  • PostStreaming and its plain-procedure callback had no other users and are removed. PostSSE is the one streaming POST.
  • ContinuePausedTurn now shares AppendAssistantContent with the streamed continuation.
  • docs/acp.md: Anthropic streams. Gemini and the relay provider still send each round whole.

Tests

  • make test-streaming: ParseAnthropicStream on a canned stream. Covers text blocks joined, thinking kept with its signature and not shown, a citation, a tool call from input_json fragments, 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:
    • text reaches the editor while the model is still writing (the fake holds its stream after the first piece until the editor has shown it);
    • a tool call from fragments runs;
    • a pause_turn continuation re-sends the server_tool_use, the search result (with its encrypted_content) and the text as they came, and the answer reads on;
    • an overloaded error event mid-stream becomes the turn's error and isn't saved;
    • two sessions streaming at once, interleaved piece by piece, each get and save only their own text.
  • Revert check: with the old Anthropic code swapped back in, 6 of the 12 Anthropic e2e checks fail.
  • Live, through the official ACP SDK client, with me as the model behind an Anthropic-format bridge sending words about 150 ms apart:
    • the editor got each word about 150 ms apart;
    • the tool_use was rebuilt from 6-character fragments and ran;
    • the second round opened a new paragraph;
    • all 28 messages validated against the ACP schema.
  • Full make test passes 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_chunk update for exactly this, so streaming thinking to the editor would be a small follow-up.


Generated by Claude Code

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T23:32:44.964148Z 7c8a02c PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@FMXExpress
FMXExpress merged commit 368962b into main Nov 12, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +1122 to +1125
PostSSE(URL, Body, Headers, 120, Acc.OnEvent, Status, ErrMsg, Raw, Events);
Result.StatusCode := Status;

if (Status < 200) or (Status >= 300) then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

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.

2 participants