Skip to content

refactor(chat): the auto-continuation turn runs and settles in the turn core - #2876

Open
mattzcarey wants to merge 5 commits into
refactor/turn-core-programmaticfrom
refactor/turn-core-continuation
Open

mattzcarey wants to merge 5 commits into
refactor/turn-core-programmaticfrom
refactor/turn-core-continuation

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

On top of #2870, the auto-continuation turn moves into the turn core. ChatTurns.runContinuationTurn owns it from the moment a tool result fires it to the moment it settles, and both classes run it through the turn body #2870 gave every other turn (_runServerTurnReply, _runModelTurn). Think's connection-less continuation is the same turn with no pending continuation.

AIChatAgent and Think each wrote this lifecycle and disagreed at the edges. Where they differed both now do this, and apps can observe it:

  • A continuation stopped while it waits in the queue never calls the model.
  • A continuation does not run on a reply that already went on to its answer (the last step has text and no tool call whose result follows). A reply with the tool call and the text in one step still gets its continuation, which AIChatAgent used to drop on a stop finish.
  • A turn settles only the continuation it owns. A tool result that arrives while a continuation fails, or while its stream errors, still gets its own continuation after the error frame.
  • A turn that fails before its stream, request or continuation, records the terminal status and then sends the error frame. onChatResponse for a failed continuation runs after the turn left the queue, so it can call saveMessages. AIChatAgent emits message:error for it.
  • A dropped continuation emits the new chat:continuation:dropped event with the reason (answered, moved_on, stopped).
  • A Think continuation gets context-overflow recovery, because it runs _runModelTurn.
  • Every continuation answers a message, and runs only while the conversation has not moved past it and it has no answer yet. That now includes a Think approval of a paused execution: one that arrives after the conversation moved on is stored, starts no turn, and emits chat:continuation:dropped with moved_on. The next turn's model call reads it. An approval that leaves the execution paused starts no turn either.
  • waitForIdle and waitUntilStable do not resolve between a continuation firing and its turn, whatever the host's enqueue awaits first.
  • A stopped continuation that throws is not logged as a failed continuation.
  • A tab that watches a Think tool continuation draws it as the message it is stored in. It used to draw the text twice. A continuation's chunk frames carry messageId for this.

Validation:

  • agents chat tests, ai-chat workers and the think suite ran, with pnpm run check. Each behavior change has a test that failed before it.
  • The ChatTurns TLA+ model was changed first and merged with the models of the PRs below, with a broken variant for each guard in continuation-turn.ts. The merged models pass all their cases.
  • Mutation testing found rules no test pinned (the cleanup of a turn that never started, the target merge, the failure report's choice of reply). Each has a test that fails with the mutant.

Read packages/agents/src/chat/continuation-turn.ts first, then turn-failure.ts. The two _fireAutoContinuations are what is left in the hosts.

@changeset-bot

changeset-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a68db39

The changes in this PR will be included in the next version bump.

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

Not sure what this means? Click here to learn what changesets are.

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

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: a68db39

@agent-think

agent-think Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

🔴 agents import sizes: 1 entry point over threshold

Entry point Exports Largest gzip change Size now
🔴 agents/chat 1 resized, 8 new +1.3 KiB (+20.73%) 7.8 KiB
Changed exports (9)
Import Gzip change Size now
🔴 agents/chat#ChatTurns +1.3 KiB (+20.73%) 7.8 KiB
🆕 agents/chat#runContinuationTurn — 4 KiB
🆕 agents/chat#reportTurnFailure — 2.8 KiB
🆕 agents/chat#settleContinuationTurn — 2.7 KiB
🆕 agents/chat#transcriptEndsInAnswer — 2.7 KiB
🆕 agents/chat#continuationStreamStarted — 2.7 KiB
🆕 agents/chat#continuationStreamEnded — 2.6 KiB
🆕 agents/chat#replyFinalized — 2.6 KiB
🆕 agents/chat#addContinuationTarget — 2.6 KiB
How this works

Each runtime export is bundled on its own, minified, and gzipped. Changes smaller than 100 B, or smaller than 1% and 1 KiB, are ignored. Growth over 10% or 5 KiB is marked 🔴. This report is informational and does not fail CI. The workflow artifact contains every measurement.

Compared d60ba149 → a68db392 · workflow run · reported by agent-think[bot]

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the refactor/turn-core-continuation branch from 811cd7b to 13cc169 Compare November 17, 2026 16:11
@mattzcarey
mattzcarey changed the base branch from refactor/chat-turn-core to refactor/turn-core-programmatic November 17, 2026 16:11

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

Devin Review

Comment thread packages/think/src/think.ts Resolved
@mattzcarey
mattzcarey force-pushed the refactor/turn-core-continuation branch from 13cc169 to b87bafb Compare November 17, 2026 16:15
@mattzcarey
mattzcarey added this pull request to stack #2869 November 17, 2026 18:50
@mattzcarey
mattzcarey force-pushed the refactor/turn-core-continuation branch from b87bafb to 7d5cf0e Compare November 17, 2026 21:54
@mattzcarey
mattzcarey force-pushed the refactor/turn-core-continuation branch from 7d5cf0e to 133c082 Compare November 18, 2026 17:42
…ontinuation's fire to its turn, and a tab draws a watched continuation as the message it is stored in
… check and failure report; drop the started flag
… appended without a turn does not drop it, and the answer check reads the reply it answers
@mattzcarey
mattzcarey force-pushed the refactor/turn-core-continuation branch from 133c082 to a68db39 Compare November 19, 2026 17:09
@mattzcarey
mattzcarey removed this pull request from stack #2869 November 20, 2026 12:09
@mattzcarey
mattzcarey added this pull request to stack #2923 November 20, 2026 12:12
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