Skip to content

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

Open
kathiekiwi wants to merge 30 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 30 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: 1fa556f

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: 1a414ac5-a63b-45c0-a302-16991c300cee

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@e24e584

trigger.dev

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

@trigger.dev/core

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

@trigger.dev/python

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

@trigger.dev/react-hooks

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

@trigger.dev/redis-worker

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

@trigger.dev/rsc

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

@trigger.dev/schema-to-json

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

@trigger.dev/sdk

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

commit: e24e584

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[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 1 new potential issue.

Open in Devin Review

Comment on lines +1805 to +1811
// 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.

🟡 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).
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