Commit c476265
* fix(chat): keep AI SDK status correct when reconnect races the pre-stream window (#1784)
A chat turn spends a window between "request accepted" and "first chunk
produced" in neither the active-stream nor terminal-replay state: it is queued,
debouncing, waiting on waitForMcpConnections, or running async setup inside
onChatMessage before a stream object exists. A client that reconnected or
re-mounted in that window was answered with cf_agent_stream_resume_none and gave
up, so the turn the server went on to stream never drove the client's AI SDK
`status` — the UI stayed stuck at "ready" until a full remount.
Server (agents/chat):
- New shared PreStreamTurns tracker (pre-stream-turns.ts) for accepted-but-not-
yet-streamed turns and the connections parked waiting on them, mirroring
ContinuationState (pure data + send-through-callback).
- New server->client cf_agent_stream_pending frame (protocol + wire-types +
golden builder).
- ResumeHandshake now parks resume requests that arrive during the pre-stream
window and emits STREAM_PENDING ("keep waiting") instead of RESUME_NONE, then
flushes parked connections into the normal STREAM_RESUMING handshake on stream
start. Continuation affinity is relaxed via an optional isConnectionPresent
host hook so a transparent reconnect (whose connection id changed) can resume
a continuation whose original owner connection is gone.
Client:
- ws-chat-transport: handleStreamPending() extends the resume probe from the 5s
fast path to a 60s backstop so the probe stays open across the gap.
- useAgentChat re-probes the stream on a transparent socket reopen (e.g. a 1006
reconnect that does not remount the component) so status recovers.
Hosts:
- Wired the begin/park/flush/settle lifecycle into both AIChatAgent and
@cloudflare/think.
- Skipped turns (supersede / queue generation change) settle WITHOUT releasing
parked connections (releaseParked: false), so a client parked during the
window survives onto the successor turn instead of being cut loose by a
premature RESUME_NONE in the supersede/settle microtask race.
Hibernation: PreStreamTurns is in-memory only and is safe because the pre-stream
window cannot overlap hibernation — a turn between begin() and stream start is an
unresolved message-handler promise that pins the DO in memory, so eviction only
happens once a durable stream exists (resumed via ResumableStream) or the turn
finished. Documented as an invariant on each _preStream field.
Tests:
- Unit: PreStreamTurns (incl. skip-path contract), handshake park/pending/flush/
affinity, golden STREAM_PENDING frame.
- Transport: STREAM_PENDING keep-waiting timeout extension (incl. tool
continuation path).
- Integration (workers runtime, real AIChatAgent): onConnect/resume-request park
-> pending -> resuming, and parked-client-survives-overlapping-submits.
- React hook: transparent reconnect re-probe + STREAM_PENDING keepalive.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(ai-chat): guarantee pre-stream turn settles if a pre-turn-body step throws (#1784)
AIChatAgent.begin()'d the pre-stream turn before persistMessages,
_mergeQueuedUserMessages, and the queued mcp.waitForConnections /
_setRequestContext steps. A throw in any of those never reached the
settle in chatTurnBody's finally, so the request id stayed stuck in
_preStream (hasInFlight() true forever) and every later client was
parked on STREAM_PENDING (60s) instead of getting an immediate
STREAM_RESUME_NONE — until chat clear or DO eviction.
Wrap the whole post-begin() scope in a top-level try/finally that always
calls _settlePreStreamTurn, mirroring @cloudflare/think. Idempotent with
the existing happy-path settle.
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Sunil Pai <18808+threepointone@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent c58b401 commit c476265
17 files changed
Lines changed: 1506 additions & 160 deletions
File tree
- .changeset
- packages
- agents/src/chat
- __tests__
- ai-chat/src
- react-tests
- tests
- think/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
Lines changed: 176 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
57 | 68 | | |
58 | 69 | | |
59 | 70 | | |
| |||
82 | 93 | | |
83 | 94 | | |
84 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
85 | 99 | | |
86 | 100 | | |
87 | 101 | | |
| |||
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
57 | 71 | | |
58 | 72 | | |
59 | 73 | | |
| |||
0 commit comments