Repository navigation
refactor(chat): one recovery attempt sequence for AIChatAgent and Think - #2873
Open
mattzcarey wants to merge 18 commits into
Open
mattzcarey wants to merge 18 commits into
mattzcarey wants to merge 18 commits into
Conversation
🦋 Changeset detectedLatest commit: f2bbc4b The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
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: |
Contributor
🔴 agents import sizes: 1 entry point over threshold
Changed exports (11)
How this worksEach 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 |
mattzcarey
force-pushed
the
refactor/turn-core-recovery
branch
from
November 17, 2026 19:12
c44b1f1 to
0a80b7c
Compare
mattzcarey
force-pushed
the
refactor/turn-core-recovery
branch
from
November 18, 2026 16:54
0a80b7c to
a1d92e0
Compare
…ontinued; a stop by any id ends the incident as stopped
… attempt plumbing and incident bookkeeping for both hosts
…covery inside its durable fiber
… its reply, and knows its own turn by async context
…try-After is honored in both hosts, and the engine dispatches recovery callbacks
…, and a continuation supersedes only the recovery of the reply it continues
…t tracks reaches the attempt, and only the attempt's own turn runs in its scope
… continue the reply a second time
…d its stream before recovery ran
…9, G12, G13, G14, step orders), and wait for the recovered turn in the #1781 test
…dingClientInteraction while it waits
mattzcarey
force-pushed
the
refactor/turn-core-recovery
branch
from
November 19, 2026 14:44
a1d92e0 to
f2bbc4b
Compare
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.
AIChatAgentandThinknow run chat recovery through the sharedChatRecoveryEngine. It owns three sequences both classes used to write out themselves: a recovery attempt (recovery-attempt.ts), the routing of a live stall or transient error into recovery (recovery-route.ts), and the dispatch of a recovery callback. Each host binds its turn machinery once in the adapter and passes hooks for what differs, which is its leaf check, how the turn runs, how a partial reply is stored, andThink's submission and messenger steps.The two copies had drifted, and an order bug in one of them (the stop check sat after the timeout branch) was only found in review. Moving them into one place changes what an app can observe in these cases:
skippedwithcontinued_by_interaction. Before, both turns called the model.AIChatAgent. It now ends withonExhausted(recovery_error) and the terminal message, asThinkdid. Before, the incident stayedscheduledand the tab showed "recovering".Think. The incident records the stop. Before, it gave up withrecovery_error.skippedwithuser_cancelled. Before, it wasfailed.AIChatAgent. The incident staysexhausted. Before, it was then markedfailed.onChatRecoverydeclines a live interruption (continue: false) inThink. The turn ends as the stream error it hit, so the client,onChatResponseand achat()caller'sonErrorall get that error. Before,Thinksent a generic "declined" message and reported a clean interruption.retry-afterorretry-after-msasks, up to 60 seconds.Thinknow does this for any error classified transient. ForAIChatAgentit is nominal, because a provider 429 arrives as an SSEerrorchunk and never reaches recovery.scheduled, withdeferredError, until the same attempt runs again, so a stop or a client's answer in between still reaches it. Before, it readfailedfor that window and emittedchat:recovery:failed.skippedwithuser_cancelled. A recovered turn that returnsabortedends the same way.chat:recovery:skippedevent. Before, afailedfollowed it.onChatResponse, the turns it starts and a continuation timer armed during a recovered turn no longer run as part of the recovery attempt.scheduleCountandstartedDelivery, and recovery payloads gaindelivery.Think. The turn is still continued. Before, the tab's resume closed the stream, recovery read it as finished, the incident endedskippedwithstream_terminal, and the turn's submission was markedcompleted.Retry-Afteris also read from inside the AI SDK'sRetryError, so it survives the SDK's own retries.AIChatAgent. A tab that connects afterwards still gets the terminal message. Before, the stalled turn ending cleared it.To review, start with the file comment in
recovery-attempt.ts, which lists the attempt's steps in order, thenrunRecoveredTurnbelow it.recovery-route.tsis the second sequence. In the hosts,_chatRecoveryContinueDetachedand_routeStreamInterruptionshow what each one still supplies. The persisted incident gains one optional field,scheduled.The order of the checks was modelled in TLA+ before the code changed, with a variant per check that must break a named rule. Tests ran in
agents,ai-chatandthink.recovery-attempt.test.tsandrecovery-route.test.tscover the two sequences, and each change in the list has a host test that fails without it, except theAIChatAgenthalf ofretry-after, which is covered only for a platform-transient error.