Skip to content

fix(chat): continue a tool result only while its message is the latest - #2865

Open
mattzcarey wants to merge 4 commits into
mainfrom
fv/chat-continuation-core
Open

mattzcarey wants to merge 4 commits into
mainfrom
fv/chat-continuation-core

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

An auto-continuation now runs only for a tool result that settled its call, and only while the conversation has not moved past that call's message. The rule lives in agents/chat (AutoContinuationController.afterInteraction, continuationTargetIsLatest), and AIChatAgent and Think both use it.

Why:

  • a late tool result for reply 1 continued reply 2, which had already answered a newer question
  • a repeated tool result (a second tab running the same tool) started a second continuation
  • Think had neither guard, and its _applyToolUpdateToMessages did not report whether it changed anything

The conversation has moved past a message once a user message follows it, or once a later turn stored a reply after it. Both hosts record the id of the last reply a turn stored (lastTurnReplyId, durable). A message appended without a turn, such as a narrated milestone or a status message an app stores with persistMessages, does not count, so the tool result is still answered. In AIChatAgent the answer is a new reply after the appended message.

Follows #2860, whose expected-fail replay is now a passing test. Tests ran: agents/chat, ai-chat workers and the full Think suite, plus pnpm run check.

@changeset-bot

changeset-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6ebf34a

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[bot]

This comment was marked as resolved.

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 6ebf34a

@agent-think

agent-think Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

🟡 agents import sizes: 1 entry point grew

Entry point Exports Largest gzip change Size now
🟡 agents/chat 105 resized, 1 new up to +177 B (+7.08%) 2.6 KiB
Changed exports (106)
Import Gzip change Size now
🟡 agents/chat#aiSdkRecoveryCodec +177 B (+7.08%) 2.6 KiB
🟡 agents/chat#AutoContinuationController +177 B (+7.08%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERY_ALARM_DEBOUNCE_MS +177 B (+7.08%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERY_INCIDENT_TTL_MS +177 B (+7.08%) 2.6 KiB
🟡 agents/chat#DEFAULT_CHAT_RECOVERY_NO_PROGRESS_TIMEOUT_MS +177 B (+7.08%) 2.6 KiB
🟡 agents/chat#partialHasSettledToolResults +177 B (+7.08%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERY_STABLE_RETRY_DELAY_SECONDS +177 B (+7.07%) 2.6 KiB
🟡 agents/chat#DEFAULT_CHAT_RECOVERY_MAX_ATTEMPTS +177 B (+7.07%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERY_CANCELLED_REASON +177 B (+7.05%) 2.6 KiB
🟡 agents/chat#CHAT_LAST_TERMINAL_KEY +177 B (+7.03%) 2.6 KiB
🟡 agents/chat#clearChatTerminal +177 B (+6.97%) 2.7 KiB
🟡 agents/chat#TurnQueue +177 B (+6.27%) 2.9 KiB
🟡 agents/chat#ContinuationState +177 B (+6.2%) 3 KiB
🟡 agents/chat#SubmitConcurrencyController +177 B (+5.74%) 3.2 KiB
🟡 agents/chat#applyChunkToParts +176 B (+7.04%) 2.6 KiB
🟡 agents/chat#applyLateToolInput +176 B (+7.04%) 2.6 KiB
🟡 agents/chat#CHAT_MESSAGE_TYPES +176 B (+7.04%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERING_FLAG_TTL_MS +176 B (+7.04%) 2.6 KiB
🟡 agents/chat#isLateToolInputChunk +176 B (+7.04%) 2.6 KiB
🟡 agents/chat#normalizeToolInput +176 B (+7.04%) 2.6 KiB
🟡 agents/chat#DEFAULT_CHAT_RECOVERY_MAX_INTERRUPTED_ATTEMPTS +176 B (+7.03%) 2.6 KiB
🟡 agents/chat#AGENT_TOOL_STREAM_PROGRESS_BUMP_THROTTLE_MS +176 B (+7.02%) 2.6 KiB
🟡 agents/chat#CHAT_STREAM_PROGRESS_CREDIT_THROTTLE_MS +176 B (+7.02%) 2.6 KiB
🟡 agents/chat#DEFAULT_CHAT_RECOVERY_MAX_WORK +176 B (+7.02%) 2.6 KiB
🟡 agents/chat#DEFAULT_CHAT_RECOVERY_STABLE_TIMEOUT_MS +176 B (+7.02%) 2.6 KiB
🟡 agents/chat#KV_DELETE_MAX_KEYS +176 B (+7.02%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERY_TASK_NAME +176 B (+6.99%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERY_PROGRESS_KEY +176 B (+6.99%) 2.6 KiB
🟡 agents/chat#TIMED_OUT +176 B (+6.99%) 2.6 KiB
🟡 agents/chat#STREAM_RESUME_NONE_REASONS +176 B (+6.93%) 2.7 KiB
🟡 agents/chat#chatRecoveryBackoffSeconds +176 B (+6.91%) 2.7 KiB
🟡 agents/chat#createAgentToolEventState +176 B (+6.91%) 2.7 KiB
🟡 agents/chat#DEFAULT_CHAT_RECOVERY_TERMINAL_MESSAGE +176 B (+6.9%) 2.7 KiB
🟡 agents/chat#bumpChatRecoveryProgress +176 B (+6.89%) 2.7 KiB
🟡 agents/chat#AgentToolProgressEmitter +176 B (+6.08%) 3 KiB
🟡 agents/chat#MAX_BOUND_PARAMS +175 B (+6.98%) 2.6 KiB
🟡 agents/chat#recordChatTerminal +175 B (+6.82%) 2.7 KiB
🟡 agents/chat#byteLength +175 B (+6.53%) 2.8 KiB
🟡 agents/chat#ROW_MAX_BYTES +174 B (+6.93%) 2.6 KiB
🟡 agents/chat#CHAT_RECOVERING_KEY +174 B (+6.93%) 2.6 KiB
🟡 agents/chat#createChatTurnTaskDefinition +174 B (+6.09%) 3 KiB
🟡 agents/chat#interceptAgentToolBroadcast +174 B (+6.06%) 3 KiB
🟡 agents/chat#CHAT_RECOVERY_INCIDENT_KEY_PREFIX +173 B (+6.86%) 2.6 KiB
🟡 agents/chat#toolPartHasSettledResult +173 B (+6.84%) 2.6 KiB
🟡 agents/chat#clientResolvableToolNames +173 B (+6.84%) 2.6 KiB
🟡 agents/chat#readChatRecoveryProgress +173 B (+6.82%) 2.6 KiB
🟡 agents/chat#pendingChatTerminal +173 B (+6.82%) 2.6 KiB
🟡 agents/chat#AgentToolStreamProgressThrottle +173 B (+6.79%) 2.7 KiB
🟡 agents/chat#StreamProgressCreditThrottle +173 B (+6.79%) 2.7 KiB
🟡 agents/chat#ChatStreamStalledError +173 B (+6.78%) 2.7 KiB

…and 56 more exports in the workflow artifact.

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 81726f7d → 6ebf34ad · workflow run · reported by agent-think[bot]

devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the fv/chat-continuation-core branch from a793006 to 356fdeb Compare November 11, 2026 14:39
@mattzcarey
mattzcarey added this pull request to stack #2869 November 11, 2026 17:23
@mattzcarey
mattzcarey force-pushed the fv/chat-continuation-core branch 2 times, most recently from 3c32d14 to c318378 Compare November 18, 2026 16:44
The decision moves into agents/chat so AIChatAgent and Think share it:

- AutoContinuationController.afterInteraction schedules a continuation only
  for a result that settled its tool part, targeted at that part's message
- ContinuationState carries targetMessageId; continuationTargetIsLatest
  skips a continuation once a newer message follows the target (a newer
  question or reply, a regenerate, a clear), like pi's continue() settling
  nothing_to_answer

Think's _applyToolUpdateToMessages now reports the outcome, and Think gets
the changed-only scheduling ai-chat got in the previous commit. Tests that
built the old order on purpose now keep the tool call's message latest.
A later result for an older reply used to replace the target of a pending
continuation, so the latest-message check then skipped the newer reply's
continuation. Pending and deferred continuations now keep every message
their coalesced results target; the continuation runs while any of them is
the last message. A result without a target turns the check off. Also fix
a test's type narrowing that broke the typecheck.
Scheduling now happens once the result is applied, so idle and stable checks
count an apply in flight as a pending continuation decision. The check moves
into AutoContinuationController.hasPendingDecision, shared by both hosts.
Think keeps the live reply as the changed message when a stored row also
holds the call.
@mattzcarey
mattzcarey force-pushed the fv/chat-continuation-core branch from c318378 to 6ebf34a Compare November 19, 2026 13:53

@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

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