Repository navigation
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
Changes from all commits
19148e8
f3ce3fe
d02f77c
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,90 @@ | ||
| 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("persists first attachments-only messages with empty text", () => { | ||
| const merged = mergeThreadMessageProjection(undefined, { | ||
| text: "", | ||
| turnId: TurnId.make("turn-1"), | ||
| streaming: false, | ||
| createdAt: "2026-07-02T01:44:30.000Z", | ||
| updatedAt: "2026-07-02T01:44:30.000Z", | ||
| }); | ||
|
|
||
| expect(merged.text).toBe(""); | ||
| expect(merged.createdAt).toBe("2026-07-02T01:44:30.000Z"); | ||
| expect(merged.turnId).toBe(TurnId.make("turn-1")); | ||
| }); | ||
|
|
||
| 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( | ||
|
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. 🟡 Medium When an assistant message with attachments from an earlier turn is reused for a new turn, 🤖 Copy this AI Prompt to have your agent fix this: |
||
| 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 ?? ""); | ||
| } | ||
|
|
||
| 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
https://github.com/pingdotgg/t3code/blob/3a401bf5b3d4372bb794ba942c9631451f4b19f5/apps/server/src/orchestration/Layers/ProjectionPipeline.ts#L27
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:
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.
Thanks — same issue flagged by Bugbot, and I agree it was worth fixing.
The failure mode is: no existing projection row, incoming
thread.message-senthas empty text andstreaming: false(attachments-only), and we incorrectly readpreviousMessage.textbecauseturnChangedis false whenpreviousMessageis undefined.Addressed in 8c2d2149d with optional chaining plus a unit test. The core Cursor resume behavior is unchanged.
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.
Sorry, I'm unable to act on this request because you do not have permissions within this repository.