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 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
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 小时
30 天内合并 PR
17

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

triggerdotdev/trigger.dev 的其他 Issue

查看 triggerdotdev/trigger.dev 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。