bug: chat transport `watch: true` duplicates the previous assistant message when a turn starts out-of-band, and `useChat` status never returns to ready

オープン
#4,326 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
typescript
領域
api, frontend

調査の方向性

Start in packages/trigger-sdk/src/v3/chat.ts, especially the “Re-arm per-turn events” branch, and trace how watch mode handles turn completion with useChat. Compare that behavior with the AI SDK’s single-message-per-request stream processing. Done means out-of-band turns render without duplicating or corrupting the previous assistant message, useChat returns to ready, and an idle session does not reconnect indefinitely.

索引モデルが issue の本文から書いたものです。

説明

Provide environment information
System:
  OS: macOS 26.6
  CPU: (14) arm64 Apple M4 Pro
  Memory: 294.73 MB / 48.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 24.14.0
  Yarn: 1.22.22
  npm: 11.9.0
  pnpm: 10.25.0
  bun: 1.3.5

trigger.dev CLI 4.5.4, @trigger.dev/sdk 4.5.4
ai 7.0.31, @ai-sdk/react 4.0.34, next 16.2.10
Describe the bug

I am building on chat.agent() with durable sessions, and background tasks wake a conversation by appending a user message to session.in (a data load finishing, a scheduled heartbeat). To render those wake turns live in open tabs I turned on watch: true on useTriggerChatTransport. When the wake turn streams in, the tab shows the previous assistant message twice, and the new turn's content renders inside a message that also contains the previous turn's parts. A refresh shows the conversation correctly, so persistence is fine; it is the live view that corrupts.

I dug in and I am fairly confident about the mechanism. Watch mode keeps one useChat response stream open across turns: on turn-complete the transport re-arms and continues instead of closing (packages/trigger-sdk/src/v3/chat.ts, the "Re-arm per-turn events" branch). But the AI SDK's stream processing is single-message per request: createStreamingUIMessageState seeds one in-progress message, and a start chunk only renames it (state.message.id = chunk.messageId) rather than beginning a new one. So the second turn's start renames the first turn's still-referenced message object and keeps appending parts into it. useChat's write path replaces the last message only when ids match, and after the rename they do not, so the renamed message (old parts plus new parts) is pushed alongside the stale snapshot of the first one. Two copies, and a structurally wrong message.

I verified the wire is innocent: replaying the session's .out stream from seq 0 shows each turn's chunks exactly once. The duplicate is manufactured client-side by the interaction above, which I think means any watch: true consumer rendering through useChat hits this on every out-of-band turn.

Two side effects of the same root, for completeness: because the stream never closes, useChat status never returns to "ready" after a watched turn (composers stay disabled, onFinish never fires), and once the session goes idle the subscription reconnects on its 60s stall timer forever.

Reproduction repo

The project is a hackathon submission that goes public at https://github.com/mcheemaa/rendi in the next couple of days. The three steps below reproduce on any chat.agent() project with useChat, and I am happy to put together a minimal public repro if that helps.

To reproduce
  1. Any chat.agent() task plus useTriggerChatTransport({ watch: true }) and useChat. Send a message, let the turn complete, keep the tab open.
  2. From a task or a script, append a user message to the same session out-of-band: sessions.open(chatId).in.send({ kind: "message", payload: { chatId, trigger: "submit-message", messageId, message } }).
  3. Watch the open tab as the new turn streams: the previous assistant message duplicates, the new content lands under the renamed message, and the composer stays on its streaming state indefinitely.
Additional information

Possibly related, while debugging this I noticed the dashboard's session view has its own trouble with these out-of-band messages: on first load it renders the injected user message directly under the conversation's first user message, before the previous turn's output, and toggling Raw and Rendered a couple of times re-sorts it into the correct position (easy to see on any session that has one of these injected messages). Different surface, but it looks like the same underlying edge: a session.in message that no browser sent, which rendering layers do not expect between turns. Happy to file that separately if that is more useful for triage.

The workaround I shipped, in case it helps others: drop watch: true; while idle, poll persisted messages and adopt new rows by id; when the adopted tail is a user-role message, set the session state's isStreaming back to true and call resumeStream(). reconnectToStream then attaches from the stored cursor, and because the last message is user-role the AI SDK seeds a fresh assistant message, so the wake turn streams in live and correctly.

On a fix direction: it seems like the transport is the right place, since the AI SDK's one-message-per-request model is its documented contract. If watch mode ended the useChat request at each turn-complete and surfaced the next turn as something the client attaches to (an event plus resumeStream, or an auto-reattach), each turn would get its own request and a fresh message.

主要言語
TypeScript
スター
16.5k
フォーク
1.5k
平均マージ
1日 15時間
マージ済み PR(30日)
17

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

triggerdotdev/trigger.dev のほかの issue

triggerdotdev/trigger.dev の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。