Skip to content

fix(sdk): watch-mode chat subscriptions survive quiet windows - #4548

Open
kathiekiwi wants to merge 24 commits into
feat/agent-storybook-galleryfrom
fix/watch-mode-keepalive-tri-13065
Open

fix(sdk): watch-mode chat subscriptions survive quiet windows#4548
kathiekiwi wants to merge 24 commits into
feat/agent-storybook-galleryfrom
fix/watch-mode-keepalive-tri-13065

Conversation

@kathiekiwi

@kathiekiwi kathiekiwi commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

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-13070reconnectToStream derived 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 .in on unmount and could stop a turn it didn't own. Subscription lifecycle is not session ownership: reconnectToStream now takes an explicit stopOnAbort option that defaults to false, and the owning turn paths (sendMessages, sendAction) pass sendStopOnAbort: true explicitly. A read-only subscription ending never mutates the session; it still cancels its own request.

@changeset-bot

changeset-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5810c80

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@trigger.dev/sdk Patch
@trigger.dev/python Patch
@internal/dashboard-agent Patch
@internal/sdk-compat-tests Patch
@trigger.dev/build Patch
@trigger.dev/core Patch
@trigger.dev/react-hooks Patch
@trigger.dev/redis-worker Patch
@trigger.dev/rsc Patch
@trigger.dev/schema-to-json Patch
@trigger.dev/database Patch
@trigger.dev/otlp-importer Patch
@trigger.dev/rbac Patch
@trigger.dev/sso Patch
trigger.dev Patch
@internal/cache Patch
@internal/clickhouse Patch
@internal/llm-model-catalog Patch
@internal/metrics-pipeline Patch
@internal/redis Patch
@internal/replication Patch
@internal/run-engine Patch
@internal/run-store Patch
@internal/schedule-engine Patch
@internal/testcontainers Patch
@internal/tracing Patch
@internal/tsql 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

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 77b4a597-3d0f-4ed6-b4d0-873f33cb8341

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

Watch 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)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the primary watch-mode chat subscription fix.
Description check ✅ Passed The description clearly explains both fixes and their intended behavior, but it omits the template checklist and explicit testing, changelog, and screenshots sections.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/watch-mode-keepalive-tri-13065

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Aug 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trigger.dev/build

npm i https://pkg.pr.new/@trigger.dev/build@c1d851a

trigger.dev

npm i https://pkg.pr.new/trigger.dev@c1d851a

@trigger.dev/core

npm i https://pkg.pr.new/@trigger.dev/core@c1d851a

@trigger.dev/python

npm i https://pkg.pr.new/@trigger.dev/python@c1d851a

@trigger.dev/react-hooks

npm i https://pkg.pr.new/@trigger.dev/react-hooks@c1d851a

@trigger.dev/redis-worker

npm i https://pkg.pr.new/@trigger.dev/redis-worker@c1d851a

@trigger.dev/rsc

npm i https://pkg.pr.new/@trigger.dev/rsc@c1d851a

@trigger.dev/schema-to-json

npm i https://pkg.pr.new/@trigger.dev/schema-to-json@c1d851a

@trigger.dev/sdk

npm i https://pkg.pr.new/@trigger.dev/sdk@c1d851a

commit: c1d851a

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

…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.
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@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 2 new potential issues.

Open in Devin Review

Comment on lines +2063 to +2065
cancel() {
internalAbort.abort();
},

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.

🔍 Aborting an in-flight stream still deletes the successor's abort controller

subscribeToSessionStream's finally (packages/trigger-sdk/src/v3/chat.ts:2055-2059) calls this.activeStreams.delete(chatId) unconditionally, but the map may already hold a newer controller. sendMessages aborts the old stream and then synchronously registers the new one (packages/trigger-sdk/src/v3/chat.ts:867-880chat.ts:1638) with no await in between, so the aborted stream's teardown runs afterwards on a microtask and removes the live stream's entry. Consequences: a later sendMessages/stopGeneration finds no active stream to abort (two concurrent readers both writing state.lastEventId, the exact hazard called out at chat.ts:1254-1257), and reconnectToStream's activeStreams.has() guard stops protecting against a duplicate subscription. This is pre-existing, but the new cancel() hook (which aborts internalAbort from a consumer cancel) plus watch-mode subscriptions that now outlive every turn make the window much easier to hit. Guarding the delete with if (this.activeStreams.get(chatId) === internalAbort) would fix it.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +1800 to +1806
// 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

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.

🔍 In a watch-mode transport, a stream opened by sendMessages now never closes after turn-complete

resumeAfterEof no longer stops on state.isStreaming when this.watchMode is set, and that flag is a property of the transport, not of the individual subscription. A watch-mode transport that also calls sendMessages/sendAction gets a stream that skips controller.close() on turn-complete (chat.ts:2009) and now also never closes at the following EOF — previously it closed within one long-poll window. For AI SDK consumers that means status stays "streaming" forever after the reply finished. Watch mode is documented as a read-only viewer mode (chat.ts:583-590), so this may be out of scope, but it is worth confirming that no consumer both watches and sends on the same transport instance.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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.

2 participants