diff --git a/packages/client-runtime/src/state/threadReducer.test.ts b/packages/client-runtime/src/state/threadReducer.test.ts index c181af4220..acc2940068 100644 --- a/packages/client-runtime/src/state/threadReducer.test.ts +++ b/packages/client-runtime/src/state/threadReducer.test.ts @@ -490,6 +490,46 @@ describe("applyThreadDetailEvent", () => { } }); + it("carries turn-scoped media's attachments and turn id into the live message", () => { + // The live repro: turn-scoped media streamed in without its file (this + // reducer dropped attachments/turnId), while a reload — which reads the + // projection row — showed it. The two views must agree. + const mediaEvent = { + ...deliveryEvent, + payload: { + ...deliveryEvent.payload, + messageId: MessageId.make("msg-media"), + text: "", + turnId: TurnId.make("turn-media"), + attachments: [ + { + type: "image" as const, + id: "thread-1-attachment-1", + name: "chart.png", + mimeType: "image/png", + sizeBytes: 1024, + }, + ], + notification: { + kind: "message" as const, + label: "Hermes", + deliveryId: "delivery-media-1", + }, + }, + } as const; + + const result = applyThreadDetailEvent(baseThread, mediaEvent); + + expect(result.kind).toBe("updated"); + if (result.kind === "updated") { + expect(result.thread.messages[0]).toMatchObject({ + id: "msg-media", + turnId: "turn-media", + attachments: [{ id: "thread-1-attachment-1", name: "chart.png" }], + }); + } + }); + it("leaves an unrelated live turn untouched", () => { // A delivery can land mid-turn; attributing it to that turn would fold // it away behind the turn's summary and mis-settle the turn state. diff --git a/packages/client-runtime/src/state/threadReducer.ts b/packages/client-runtime/src/state/threadReducer.ts index 160c5cf2fb..b5d5ce531e 100644 --- a/packages/client-runtime/src/state/threadReducer.ts +++ b/packages/client-runtime/src/state/threadReducer.ts @@ -327,15 +327,21 @@ export function applyThreadDetailEvent( case "thread.notification-delivered": { // A proactive delivery is an ordinary transcript message carrying - // provenance. It binds to no turn, so latestTurn and the checkpoint - // rebinding that `thread.message-sent` performs are deliberately - // untouched — attributing it to the live turn would fold it away - // behind that turn's summary. + // provenance. latestTurn and the checkpoint rebinding that + // `thread.message-sent` performs are deliberately untouched — + // attributing a delivery to the live turn would fold it away behind + // that turn's summary. Turn-scoped media is the one exception: its + // payload names the turn it rode in on (sequencing only), and dropping + // it here made the live-streamed delivery render without its file + // while a reload — which reads the projection row — showed it. const message: OrchestrationMessage = { id: event.payload.messageId, role: "assistant", text: event.payload.text, - turnId: null, + ...(event.payload.attachments !== undefined + ? { attachments: event.payload.attachments } + : {}), + turnId: event.payload.turnId ?? null, streaming: false, notification: event.payload.notification, createdAt: event.payload.createdAt,