Fix agent-tool recovery finalization#1476
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: ee5d1e6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
aron-cf
approved these changes
May 8, 2026
| "agents": patch | ||
| --- | ||
|
|
||
| Ensure recovered agent-tool runs go through the same terminal lifecycle path as live runs. Parent recovery reconciliation now replays stored child chunks, broadcasts terminal agent-tool events, and invokes `onAgentToolFinish` after updating the parent run registry. |
There was a problem hiding this comment.
This doesn't clearly describe to the reader what actually changed from their perspective. I think it's something like "Fixed bug causing client state to drift from internal Durable Object state when tool calls spanned a Durable Object restart."
Contributor
Author
There was a problem hiding this comment.
Good point, fixed
Merged
threepointone
added a commit
that referenced
this pull request
May 11, 2026
* fix(voice): harden Workers AI STT turn handling Follow up on PR #1458 by preserving Flux turn transcripts across lifecycle events and using model-detected speech start for low-latency barge-in. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(chat): harden stream resume negotiation close races Follow-up to PR #1463: route stream-resume negotiation sends through close-safe helpers so WebSocket close races do not crash resume handling in think and ai-chat. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(voice): parse raw NDJSON text streams Follow-up to PR #1462: make the voice text stream parser honor its documented NDJSON support while preserving SSE parsing for AI text streams. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(agents): defer recovered agent-tool finish hooks (#1476) Co-authored-by: Cursor <cursoragent@cursor.com> * test(voice): cover useVoiceAgent enabled lifecycle (#1478) Co-authored-by: Cursor <cursoragent@cursor.com> * fix(ai-chat): close resumed streams on disconnect (#1487) Co-authored-by: Cursor <cursoragent@cursor.com> * fix(voice): invalidate playback on client interrupt (#1458) Co-authored-by: Cursor <cursoragent@cursor.com> * fix(voice): invalidate playback when ending calls (#1458) Co-authored-by: Cursor <cursoragent@cursor.com> * Run deferred finish hooks after successful startup Ensure recovered agent-tool finish hooks are only executed after a successful user onStart. Await _runDeferredAgentToolFinishHooks inside the onStart flow so deferred finishes are skipped when startup fails. Add a test and helper (reconcileCompletedChildWithFailedStartupForTest) to verify finish hooks are not run on failed startup and to cover lifecycle ordering and event emission. --------- Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1475.
This PR makes recovered agent-tool runs go through the same terminal finalization path as live
runAgentTool()executions.Previously,
_reconcileAgentToolRuns()updatedcf_agent_tool_runswhen a parent Agent woke up and found a child run had reached a terminal state, but it skipped two observable parts of the framework lifecycle:onAgentToolFinish(...)agent-tool-eventbroadcasts via_broadcastAgentToolTerminal(...)That meant application state mirrors, dashboards, structured logs, and chat UI state could silently drift from the framework’s durable run registry after parent DO eviction / hibernation.
What changed
Shared terminal finalization path
Added a private
_finishAgentToolRun(...)helper that centralizes the terminal lifecycle invariant:cf_agent_tool_runsonAgentToolFinish(...)Live
runAgentTool()terminal paths now use this helper too, so recovery and live execution share the same behavior.Reconciliation now restores observable lifecycle
_reconcileAgentToolRuns()now:run_id/agent_typeAgentToolRunInfofrom durable stateagent_type_finishAgentToolRun(...)Stored chunk replay helper
Added
_broadcastAgentToolStoredChunks(...)to keep chunk replay behavior local and shared between:This keeps sequencing behavior consistent: terminal events are emitted after any replayed stored chunks.
Why this architecture
The bug came from terminal finalization being spread across call sites.
runAgentTool()knew to update durable state, broadcast, and call the lifecycle hook, while_reconcileAgentToolRuns()only updated durable state.This PR deepens the internal finalization module: callers provide a run and terminal result, and the implementation owns the lifecycle ordering. That improves locality and makes it less likely future recovery paths drift from live execution.
Tests
Added regression coverage for parent recovery reconciliation using
AIChatAgentToolParent/AIChatAgentToolChild.The test simulates:
runningonAgentToolFinish(...)firesfinishedevent is broadcastVerification
Ran: