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
- Any
chat.agent() task plus useTriggerChatTransport({ watch: true }) and useChat. Send a message, let the turn complete, keep the tab open.
- 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 } }).
- 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.
Provide environment information
Describe the bug
I am building on
chat.agent()with durable sessions, and background tasks wake a conversation by appending a user message tosession.in(a data load finishing, a scheduled heartbeat). To render those wake turns live in open tabs I turned onwatch: trueonuseTriggerChatTransport. 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
useChatresponse stream open across turns: on turn-complete the transport re-arms andcontinues 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:createStreamingUIMessageStateseeds one in-progress message, and astartchunk only renames it (state.message.id = chunk.messageId) rather than beginning a new one. So the second turn'sstartrenames 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
.outstream from seq 0 shows each turn's chunks exactly once. The duplicate is manufactured client-side by the interaction above, which I think means anywatch: trueconsumer rendering throughuseChathits this on every out-of-band turn.Two side effects of the same root, for completeness: because the stream never closes,
useChatstatus never returns to "ready" after a watched turn (composers stay disabled,onFinishnever 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
chat.agent()task plususeTriggerChatTransport({ watch: true })anduseChat. Send a message, let the turn complete, keep the tab open.sessions.open(chatId).in.send({ kind: "message", payload: { chatId, trigger: "submit-message", messageId, message } }).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.inmessage 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'sisStreamingback to true and callresumeStream().reconnectToStreamthen 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
useChatrequest at each turn-complete and surfaced the next turn as something the client attaches to (an event plusresumeStream, or an auto-reattach), each turn would get its own request and a fresh message.