-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix(orchestration): show follow-up assistant replies after Cursor session resume #3642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { MessageId, TurnId } from "@t3tools/contracts"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { assistantSegmentMessageId } from "./assistantMessageIds.ts"; | ||
|
|
||
| describe("assistantMessageIds", () => { | ||
| it("scopes assistant message ids by turn so resumed sessions do not collide", () => { | ||
| const baseKey = "assistant:session-1:segment:0"; | ||
| const turnOne = TurnId.make("turn-1"); | ||
| const turnTwo = TurnId.make("turn-2"); | ||
|
|
||
| expect(assistantSegmentMessageId(baseKey, 0, turnOne)).toBe( | ||
| MessageId.make("assistant:turn-1:assistant:session-1:segment:0"), | ||
| ); | ||
| expect(assistantSegmentMessageId(baseKey, 0, turnTwo)).toBe( | ||
| MessageId.make("assistant:turn-2:assistant:session-1:segment:0"), | ||
| ); | ||
| expect(assistantSegmentMessageId(baseKey, 0, turnOne)).not.toBe( | ||
| assistantSegmentMessageId(baseKey, 0, turnTwo), | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { MessageId, type TurnId } from "@t3tools/contracts"; | ||
|
|
||
| export function assistantSegmentBaseKeyFromRuntimeItem( | ||
| itemId: string | undefined, | ||
| turnId: string | undefined, | ||
| eventId: string, | ||
| ): string { | ||
| return String(itemId ?? turnId ?? eventId); | ||
| } | ||
|
|
||
| export function assistantSegmentMessageId( | ||
| baseKey: string, | ||
| segmentIndex: number, | ||
| turnId?: TurnId, | ||
| ): MessageId { | ||
| const scopedBase = turnId ? `${turnId}:${baseKey}` : baseKey; | ||
| return MessageId.make( | ||
| segmentIndex === 0 | ||
| ? `assistant:${scopedBase}` | ||
| : `assistant:${scopedBase}:segment:${segmentIndex}`, | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { TurnId } from "@t3tools/contracts"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { assistantMessageTurnChanged, mergeThreadMessageProjection } from "./threadMessageProjection.ts"; | ||
|
|
||
| describe("threadMessageProjection", () => { | ||
| it("detects turn changes on reused assistant message ids", () => { | ||
| expect( | ||
| assistantMessageTurnChanged( | ||
| { text: "old", turnId: TurnId.make("turn-1"), createdAt: "2026-07-01T21:00:00.000Z" }, | ||
| TurnId.make("turn-2"), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("resets text and createdAt when a reused message id starts a new turn", () => { | ||
| const merged = mergeThreadMessageProjection( | ||
| { | ||
| text: "first turn reply", | ||
| turnId: TurnId.make("turn-1"), | ||
| createdAt: "2026-07-01T21:00:00.000Z", | ||
| }, | ||
| { | ||
| text: "second turn reply", | ||
| turnId: TurnId.make("turn-2"), | ||
| streaming: false, | ||
| createdAt: "2026-07-02T01:44:30.000Z", | ||
| updatedAt: "2026-07-02T01:44:30.000Z", | ||
| }, | ||
| ); | ||
|
|
||
| expect(merged.text).toBe("second turn reply"); | ||
| expect(merged.createdAt).toBe("2026-07-02T01:44:30.000Z"); | ||
| expect(merged.turnId).toBe(TurnId.make("turn-2")); | ||
| }); | ||
|
|
||
| it("appends streaming deltas within the same turn", () => { | ||
| const merged = mergeThreadMessageProjection( | ||
| { | ||
| text: "hello", | ||
| turnId: TurnId.make("turn-1"), | ||
| createdAt: "2026-07-01T21:00:00.000Z", | ||
| }, | ||
| { | ||
| text: " world", | ||
| turnId: TurnId.make("turn-1"), | ||
| streaming: true, | ||
| createdAt: "2026-07-01T21:00:01.000Z", | ||
| updatedAt: "2026-07-01T21:00:01.000Z", | ||
| }, | ||
| ); | ||
|
|
||
| expect(merged.text).toBe("hello world"); | ||
| expect(merged.createdAt).toBe("2026-07-01T21:00:00.000Z"); | ||
| }); | ||
|
|
||
| it("starts fresh streaming text when the turn changes", () => { | ||
| const merged = mergeThreadMessageProjection( | ||
| { | ||
| text: "first turn reply", | ||
| turnId: TurnId.make("turn-1"), | ||
| createdAt: "2026-07-01T21:00:00.000Z", | ||
| }, | ||
| { | ||
| text: "Checking", | ||
| turnId: TurnId.make("turn-2"), | ||
| streaming: true, | ||
| createdAt: "2026-07-02T01:44:30.000Z", | ||
| updatedAt: "2026-07-02T01:44:30.000Z", | ||
| }, | ||
| ); | ||
|
|
||
| expect(merged.text).toBe("Checking"); | ||
| expect(merged.createdAt).toBe("2026-07-02T01:44:30.000Z"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import type { TurnId } from "@t3tools/contracts"; | ||
|
|
||
| export interface ThreadMessageProjectionSlice { | ||
| readonly text: string; | ||
| readonly turnId: TurnId | null; | ||
| readonly createdAt: string; | ||
| } | ||
|
|
||
| export interface ThreadMessageProjectionIncoming { | ||
| readonly text: string; | ||
| readonly turnId: TurnId | null; | ||
| readonly streaming: boolean; | ||
| readonly createdAt: string; | ||
| readonly updatedAt: string; | ||
| } | ||
|
|
||
| export function assistantMessageTurnChanged( | ||
| previousMessage: ThreadMessageProjectionSlice | undefined, | ||
| nextTurnId: TurnId | null, | ||
| ): boolean { | ||
| if (!previousMessage) { | ||
| return false; | ||
| } | ||
| return previousMessage.turnId !== nextTurnId; | ||
| } | ||
|
|
||
| export function mergeThreadMessageProjection( | ||
| previousMessage: ThreadMessageProjectionSlice | undefined, | ||
| incoming: ThreadMessageProjectionIncoming, | ||
| ): ThreadMessageProjectionSlice & { readonly updatedAt: string } { | ||
| const turnChanged = assistantMessageTurnChanged(previousMessage, incoming.turnId); | ||
|
|
||
| const nextText = (() => { | ||
| if (!previousMessage || turnChanged) { | ||
| if (incoming.streaming || incoming.text.length > 0) { | ||
| return incoming.text; | ||
| } | ||
| return turnChanged ? "" : previousMessage.text; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge crashes first empty upsertMedium Severity When Reviewed by Cursor Bugbot for commit 3a401bf. Configure here. |
||
| } | ||
|
|
||
| if (incoming.streaming) { | ||
| return `${previousMessage.text}${incoming.text}`; | ||
| } | ||
| if (incoming.text.length === 0) { | ||
| return previousMessage.text; | ||
| } | ||
| return incoming.text; | ||
| })(); | ||
|
|
||
| return { | ||
| text: nextText, | ||
| turnId: incoming.turnId, | ||
| createdAt: turnChanged ? incoming.createdAt : (previousMessage?.createdAt ?? incoming.createdAt), | ||
| updatedAt: incoming.updatedAt, | ||
| }; | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
t3code/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
Line 27 in 3a401bf
When the first
thread.message-sentevent for an attachments-only message arrives (empty text,streaming: false, no existing row),mergeThreadMessageProjectionevaluatespreviousMessage.texton the!previousMessagebranch because the conditionincoming.streaming || incoming.text.length > 0is false, sonextTextfalls through toturnChanged ? "" : previousMessage.text, which dereferencesundefined. This throws during projection instead of persisting the message. The guard should also cover the case where there is no previous message and the incoming text is empty.🤖 Copy this AI Prompt to have your agent fix this: