fix(sdk): watch-mode chat subscriptions survive quiet windows - #4548
fix(sdk): watch-mode chat subscriptions survive quiet windows#4548kathiekiwi wants to merge 30 commits into
Conversation
🦋 Changeset detectedLatest commit: 1fa556f The changes in this PR will be included in the next version bump. This PR includes changesets to release 27 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 |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughWatch mode now reconnects after completed turns and idle-window EOFs. It continues beyond the normal EOF resubscribe limit. The subscription stops when the session is settled or the operation is aborted. Tests cover reconnect behavior, settled-session termination, aborts during backoff, and updated SSE fixtures. A patch changeset documents the SDK change. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
… side effect (TRI-13070)
@trigger.dev/build
trigger.dev
@trigger.dev/core
@trigger.dev/python
@trigger.dev/react-hooks
@trigger.dev/redis-worker
@trigger.dev/rsc
@trigger.dev/schema-to-json
@trigger.dev/sdk
commit: |
…to fix/watch-mode-keepalive-tri-13065
…to fix/watch-mode-keepalive-tri-13065
…ate on give-up - reconnect no longer peek-settles in watch mode, so a settled peek between turns can't close the standing subscription before the next turn. - the returned stream now aborts its resubscribe loop when the reader is cancelled, instead of leaking it. - clear and persist isStreaming before the budget-exhaustion throw so a reload doesn't reopen a doomed subscription.
…stream-error A consumer cancelling the watch stream aborts the resubscribe loop, which reaches controller.close() on an already-closed controller. The resulting 'Invalid state' throw was surfaced as a bogus stream-error on every clean watch-viewer unmount. Wrap the remaining bare close sites to match the existing pattern.
…at/agent-storybook-gallery
| // Watch mode is a standing subscription: it outlives turn-complete | ||
| // (which clears `isStreaming`) and idle windows EOF by design, so the | ||
| // give-up budget doesn't apply. Only abort or a settled session ends it. | ||
| while ( | ||
| state.isStreaming && | ||
| (this.watchMode || (state.isStreaming && eofResubscribes < MAX_EOF_RESUBSCRIBES)) && | ||
| !currentSubscription?.sessionSettled && | ||
| !combinedSignal.aborted && | ||
| eofResubscribes < MAX_EOF_RESUBSCRIBES | ||
| !combinedSignal.aborted |
There was a problem hiding this comment.
🟡 A viewer closing a chat view mid-reply can mark the reply as finished for every tab
A watching viewer that stops listening while waiting to re-open its connection has the chat recorded as no longer generating (state.isStreaming = false + notifySessionChange at packages/trigger-sdk/src/v3/chat.ts:1855-1857) even though the reply is still being produced, so returning to the chat shows a frozen half-written reply that never resumes.
Impact: A user who navigates away and back during an in-progress reply sees it stuck mid-sentence until the next message is sent.
How the abort-during-backoff path clears the persisted streaming flag
Before this PR the reconnect loop in resumeAfterEof only ran while state.isStreaming && eofResubscribes < MAX_EOF_RESUBSCRIBES, so watch-mode readers spent little time inside it. With the new condition (this.watchMode || ...) a watch-mode subscription now sits in this loop indefinitely across quiet windows, which makes the abort-during-backoff exit path the common one: combinedSignal.aborted breaks the loop, the new budget-exhausted throw is skipped (it requires !combinedSignal.aborted), and control falls into the pre-existing trailing block at packages/trigger-sdk/src/v3/chat.ts:1854-1858 which sets state.isStreaming = false and persists it via notifySessionChange.
This contradicts the TRI-13070 goal that "a read-only subscription ending never mutates the session": reconnectToStream deliberately no longer posts a stop chunk (stopOnAbort defaults to false at packages/trigger-sdk/src/v3/chat.ts:1177), yet the same passive abort still writes the session state consumers persist. Because reconnectToStream returns null when state.isStreaming === false (packages/trigger-sdk/src/v3/chat.ts:1165), a later remount/reload will refuse to resume the still-running turn.
Prompt for agents
In `subscribeToSessionStream`'s `resumeAfterEof` (packages/trigger-sdk/src/v3/chat.ts), the trailing block after the reconnect loop unconditionally clears and persists `state.isStreaming` when the loop exits. With the new watch-mode condition, the most common exit for a passive/watch subscriber is `combinedSignal.aborted` (the consumer unmounted or cancelled), and in that case the turn may still be running server-side. Clearing + persisting `isStreaming` there makes `reconnectToStream` refuse to resume on a later mount (it returns null when isStreaming is false), so the viewer is stuck on a truncated reply. Consider only clearing the flag when the exit is a genuine end-of-turn (settled session, or budget exhaustion in non-watch mode) and leaving session state untouched when the exit was caused by an abort of a subscription that does not own the turn (i.e. the same `sendStopOnAbort`/ownership signal already threaded through this function).
Was this helpful? React with 👍 or 👎 to provide feedback.
Two related fixes to the chat transport's watch/read-only subscription lifecycle.
TRI-13065 — In watch mode the chat stream died at the first long-poll window boundary after a turn completed: the EOF-reconnect path was gated on
isStreaming, which turn-complete clears, so a watcher stopped hearing later turns. Watch mode now keeps reconnecting across quiet windows and only stops on abort or a settled session. The bounded give-up budget still applies to normal (mid-turn) streams, but not to watch mode, where empty windows are expected.TRI-13070 —
reconnectToStreamderived mutation rights from mere signal presence (sendStopOnAbort: !!options.abortSignal), so a passive/read-only subscriber that passed an abortSignal would append a{kind:"stop"}to.inon unmount and could stop a turn it didn't own. Subscription lifecycle is not session ownership:reconnectToStreamnow takes an explicitstopOnAbortoption that defaults tofalse, and the owning turn paths (sendMessages,sendAction) passsendStopOnAbort: trueexplicitly. A read-only subscription ending never mutates the session; it still cancels its own request.