Skip to content

Chat: the turn definition writes stream chunks in batches again, at once at tool events and pauses - #3357

Merged
mattzcarey merged 2 commits into
mainfrom
slim/chat-fn-batch
Nov 27, 2026
Merged

mattzcarey merged 2 commits into
mainfrom
slim/chat-fn-batch

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Since H2 (#3295), ThinkHarness's turns run through chatTurnDefinition, which writes each stream chunk as its own row write. The release batched them. This puts the batching back in the definition, as the hosts' reply streams do: 10 chunks or 512 KB per write, into the stream's newest block while it stays under 256 KB.

A batch is written at once when:

  • a tool's call or outcome arrives (tool-input-available, tool-approval-request, tool-output-available/-error/-denied, but not progress);
  • a step ends;
  • a text or reasoning part gets its first delta;
  • the model pauses (the end of a burst of chunks).

So a reset while a tool runs keeps the call. That's verify's F18 case: without the flush, the tool ran twice. A reset while the model is quiet keeps what it sent. A reader flushes first. A chunk over the capability's maxChunkBytes goes through the writer, which refuses it as before.

It also fixes recovery taking a tool's progress for its result: a tool cut off after only progress output (preliminary: true) is reported interrupted. This mattered on main too, since main stores progress.

This is the batching part of #3298 (6d), split out per arch's ruling. The hunk is the same as on #3298 at 96c0d3f. The chat functions stay in #3298.

Streams.open now marks a stream it creates as born in this isolate, so a new reply's buffered appends start at seq 0. Before, they reserved seqs as if an earlier isolate had buffered into it, which left a false hole at seq 0. A part id that comes back in a later step has its first delta written at once too.

Tests (workers chat-host, harness-think 142/142), each failing without the piece it names:

  • a server tool's call is stored while the tool runs;
  • a part's first delta is stored at once, and so is a finished step;
  • a part id used again in a later step gets the same treatment;
  • what the model sent is stored once it pauses, while the turn is live;
  • a new reply's stream starts at seq 0;
  • a burst's other chunks are batched;
  • a tool cut off after only progress is reported interrupted (changeset line in chat-recovery.md: the release has this bug too).

An oversized chunk is still refused.

Cost (statements / rows written per prompt, local workerd, perf's harness probes/perf, a ThinkHarness on a Durable Object, the object's second prompt). Release is agents 0.34.0 and think 0.24.0, main is 9492a1a, head is this PR:

prompt release main head
hello (a short text reply) 74 / 24 75 / 25 59 / 21
tool (one server tool) 117 / 38 97 / 33 66 / 26
20 chunks, 50 ms apart 115 / 36 132 / 44 78 / 40
2,000 chunks in one burst 674 / 224 6072 / 2024 259 / 221

Statements are below the release in every row. Rows written are below it except the paced reply: 40 against 36, because a model that pauses between chunks is written at each pause, so a reset loses nothing it sent. Think's and AIChatAgent's own scenarios are unchanged; they don't run the definition yet.

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 7a0958c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes changesets to release 4 packages
Name Type
agents Patch
@cloudflare/ai-chat Patch
@cloudflare/think Patch
@cloudflare/agent-think Patch

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@3357

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@3357

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@3357

hono-agents

npm i https://pkg.pr.new/hono-agents@3357

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@3357

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@3357

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@3357

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@3357

commit: 7a0958c

@agent-think

agent-think Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

✅ agents import sizes: no significant changes (c14144d4 → 7a0958ca, workflow run)

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

… event, a part's first delta, a step's end and when the model pauses; a tool cut off after only progress is reported interrupted
…again has its first delta written at once; tests that fail without each flush; a changeset line for the progress fix (rv-1, Devin)
@mattzcarey
mattzcarey merged commit da0ab69 into main Nov 27, 2026
26 checks passed
@mattzcarey
mattzcarey deleted the slim/chat-fn-batch branch November 27, 2026 04:03
mattzcarey added a commit that referenced this pull request Nov 27, 2026
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