Skip to content

acp: token streaming, docker project mount, replay status, leak fixes - #633

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

FMXExpress merged 4 commits into
mainfrom
claude/pasclaw-desktop-plan-uapzcp

Conversation

@FMXExpress

@FMXExpress FMXExpress commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

ACP follow-ups after the pre-release check, plus token-by-token streaming.

Token streaming

An editor under pasclaw acp now shows the model's reply as it is written, not once per model round.

  • A real streaming transport (PasClaw.Providers.Stream). The old SSE parser read the whole buffered response after the request had finished, so every "stream" arrived at once.
    • TSSEParseStream parses SSE as bytes are written into it, and both HTTP backends (Indy, and System.Net on Delphi) write into it as data arrives. Events fire as the model produces them.
    • All parse state is per request, so concurrent streams share nothing.
    • Anthropic's PostStreaming runs on it too.
    • Indy's HTTP error body (E.ErrorMessage) is now passed through as the error text.
  • OpenAI-compatible providers really stream. ChatStream used to call Chat and hand over the whole reply as one chunk. This covers OpenAI, OpenRouter, Groq, DeepSeek, Mistral, Ollama, LM Studio, vLLM and the rest of the openai kind.
    • It sends the same request as Chat, plus stream and stream_options.include_usage.
    • Text pieces are passed on as they arrive.
    • Tool calls are put back together from their id/name and argument fragments, without doubling a name the server repeats.
    • Finish reason and usage (including cached tokens) come through, and an error sent inside the stream fails the turn.
    • A server that refuses the streaming fields (a 400 or 422 naming them) is asked again without them. One that ignores stream and answers with a single JSON body is read the way Chat reads it.
    • SupportsStreaming is now True. This also makes the gateway's streamed /v1/responses path stream for these providers, and it no longer streams a failed call's error text as output.
  • Tool loop (TToolLoopConfig.OnTextDelta). When it's set and the provider streams, each round's first request goes through ChatStream, and every text piece is passed on as it comes, with the round's first piece marked.
    • That round isn't passed to OnText as well, unless it ended with different text (a retry or fallback answered), which then goes to OnText as before.
    • Retries and fallbacks aren't streamed. When OnTextDelta isn't set, nothing changes.
  • ACP. TACPTurn.TextDelta sends each piece as an agent_message_chunk, with the paragraph break between rounds at a round's first piece.

Not streamed yet: Anthropic, Gemini and the relay provider, whose text still arrives once per round. Anthropic's SSE handler keeps its state in unit globals and drops thinking blocks, which a tool-using turn with thinking needs passed back, so the loop doesn't use it. docs/acp.md says which providers stream.

Docker, replay, leaks

  • Docker backend mounts the editor's project. pasclaw acp runs shell_exec with -w set to the editor's project, but the container only mounted PasClaw's workspace, so every command in a project outside it failed.
    • The ACP host records each session's project (SetShellSessionDir in PasClaw.Shell.Backend).
    • BuildRunArgv mounts it in that session's container at the same path, when it's outside the workspace and exists.
    • POSIX only, like the worktree mount.
    • Reopening a session in another project restarts its container with that project.
  • Replay shows each call's final status. session/load announces each tool call with the status it ended with. A call with no recorded result replays as failed, with the same note it got live.
  • Delphi threadvar leak. Background jobs clear their thread's shell session id before the thread ends; Delphi doesn't finalize a string threadvar at thread exit. FPC does finalize it, so this can't be observed here.
  • JSON wrapper leak (separate commit). The ACP layer read params, answers, prompt blocks, MCP server entries and permission outcomes through ChildObject, ChildArray and ItemObject wrappers without freeing them, so a long editor session leaked a few small objects per message. Under FPC's heap tracer the ACP unit tests went from 80 unfreed blocks to 6, and none of the remaining 6 comes from these paths.
  • Flaky backup test (separate commit). backup_tests named its temp directory by PID and never removed it, so a reused PID inherited an earlier run's archives and the rotation check failed. It now starts from an empty directory and removes it after a pass.

Tests

  • make test-streaming (new):
    • the SSE parser fed a byte at a time: CRLF lines, a UTF-8 character split across writes, comments, a final event with no blank line, the raw-body cap;
    • a Chat Completions stream put back together: text pieces, tool-call fragments, a repeated name, a null content, finish reason, usage with cached tokens, an error inside the stream;
    • the loop's OnTextDelta contract: a streamed round, no delta callback, a provider that doesn't stream, a mismatch, text after what was streamed, two rounds.
  • make test-acp: TextDelta pieces go out one chunk each, with a paragraph per new round; replay statuses.
  • make test-shell-backend: the project mount.
  • make test-acp-e2e:
    • The fake model now streams chunked SSE. It holds its stream after the first piece until the editor has shown it, which only happens if PasClaw passes text on as it arrives.
    • Also covered: tool-call arguments in fragments, two rounds as two paragraphs, a server refusing stream_options, and the Docker run line carrying the project mount.
  • Live, through the official ACP SDK client, with me as the model behind an SSE bridge pacing words about 150 ms apart: the editor received each word about 150 ms apart. A tool call's arguments came back together from 7-character fragments, the second round opened a new paragraph, all 49 messages validated against the ACP schema, and the saved session holds each reply once.
  • Revert checks: each change was reverted and a test failed every time (7 for streaming, 4 for the rest). The exceptions are the two leak fixes: the threadvar one can't be observed under FPC, and the wrapper one was measured with the heap tracer instead.
  • Full make test passes on FPC 3.2.2. Delphi (dcc64) isn't available here.

Still not supported

Images and audio in prompts (TMessage carries text only), and the editor's file system and terminals (the file and shell tools don't go through the editor).

🤖 Generated with Claude Code

https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U

… no threadvar leak

- Docker backend: a session's project outside the workspace is now
  mounted in that session's container, at the same path. `pasclaw acp`
  runs shell_exec with -w set to the editor's project, and unmounted
  every command there failed in the container. The ACP host records the
  project per session (new SetShellSessionDir in PasClaw.Shell.Backend);
  BuildRunArgv adds it when it is outside the workspace and exists.
  POSIX only, like the worktree mount. Reopening a session in another
  project stops its container so the next command starts one with that
  project.
- Replay: session/load announces each tool call with the status it ended
  with, so a refused call is no longer shown completed and then failed.
  A call with no recorded result (its turn ended first) replays as
  failed with the same note it got live.
- Background jobs clear their thread's shell session id before the
  thread ends: Delphi does not finalize a string threadvar at thread
  exit, so it leaked one string per job. Not observable under FPC,
  which does.

Tests: acp_tests (replay statuses), shell_backend_tests (the mount, only
for that session, not for a project under the workspace or a missing
directory), acp_e2e_test (the fake docker CLI's run line carries the
project). Each was reverted to confirm a test fails without it; the
threadvar change cannot be checked under FPC.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
The directory is named by pid and was never removed, so a later run
that got the same pid found an earlier run's archives in backups/ and
the rotation check failed ("10 pruned"). It failed once in a full
`make test` today; 49 such directories had piled up in /tmp.

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-12T22:33:50.112410Z a70d0c6 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.

…s, ACP

An editor under `pasclaw acp` now shows the model's reply as it is
written instead of once per round.

- PasClaw.Providers.Stream: TSSEParseStream parses SSE as the bytes are
  written into it. Both HTTP backends write a response into the stream
  they are given as it arrives, so events fire as the model produces
  them; the old parser read the whole buffered body after the request
  had finished, which made every "stream" arrive at once. All parse
  state is per request. PostStreaming (Anthropic) runs on it too.
  Indy's error body (E.ErrorMessage) is now the transport's ErrMsg.
- PasClaw.Providers.OpenAI: ChatStream really streams (it used to call
  Chat and hand the whole reply over as one chunk). Same request as Chat
  plus stream and stream_options.include_usage; text pieces passed on as
  they arrive; tool calls put together from their id/name and argument
  fragments; finish_reason; usage with cached tokens; an error sent
  inside the stream fails the turn. A server that refuses the streaming
  fields (a 400/422 naming them) is asked again without them, and one
  that ignores `stream` and answers with one JSON body is read as Chat
  would. SupportsStreaming is now True. This also makes the gateway's
  streamed /v1/responses path stream for these providers, and it no
  longer streams a failed call's error text as output.
- Tool loop: TToolLoopConfig.OnTextDelta. When set and the provider
  streams, a round's first request goes through ChatStream and each
  piece is passed on as it comes, its first piece marked. That round is
  not passed to OnText as well, unless it ended with other text (a retry
  or fallback answered), which then goes to OnText as before. Retries and
  fallbacks are not streamed. Unset, nothing changes.
- ACP: TACPTurn.TextDelta sends each piece as an agent_message_chunk,
  with the paragraph break between rounds at a round's first piece.

Anthropic stays unstreamed in the loop: its SSE handler keeps state in
unit globals and drops thinking blocks, which a tool-using turn with
thinking needs back. Gemini and the relay do not stream.

Tests: streaming_tests (new, make test-streaming) pins the parser fed a
byte at a time, a Chat Completions stream put back together, and the
loop's OnTextDelta contract. acp_tests pins TextDelta. The e2e fake
model now streams chunked SSE: it holds its stream after the first piece
until the editor has shown it, which only happens if PasClaw passes text
on as it arrives; tool-call arguments in fragments; two rounds as two
paragraphs; a server refusing stream_options.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
ChildObject, ChildArray and ItemObject return wrappers the caller owns.
The ACP layer read params, the editor's answers, prompt blocks, MCP
server entries and permission outcomes through them without freeing
them, so a long editor session leaked a few small objects per message.
Under FPC's heap tracer the ACP unit tests went from 80 unfreed blocks
to 6, and none of the 6 comes from these paths (they carry no trace at
all).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
@FMXExpress FMXExpress changed the title acp: mount the editor's project for docker, replay final call status, no threadvar leak acp: token streaming, docker project mount, replay status, leak fixes Nov 12, 2026
@FMXExpress
FMXExpress merged commit 01e0191 into main Nov 12, 2026
FMXExpress pushed a commit that referenced this pull request Nov 12, 2026
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
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