From 31113dfe7fb4d0b4be3ed0335d7dec3fa8efcb14 Mon Sep 17 00:00:00 2001 From: Ben Davis <45952064+bmdavis419@users.noreply.github.com> Date: Tue, 28 Jul 2026 11:05:57 -0700 Subject: [PATCH] =?UTF-8?q?fix(web):=20a=20turn's=20media=20messages=20no?= =?UTF-8?q?=20longer=20hide=20behind=20"Worked=20for=20=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Live repro: Hermes sent two files plus a closing caption in one turn, and the thread rendered only the caption — the file cards sat inside the collapsed turn fold until the user expanded "Worked for 32s". Two causes: - Fold eligibility kept exactly one visible message per turn (the last assistant message), built for streamed commentary before media existed. A message carrying attachments is a deliverable, not commentary: it is now always terminal, and the fold's hiding loop spares every terminal message rather than only the group's anchor. Intermediate text still folds exactly as before. - Ordering: the server stamps milliseconds while the plugin stamps microseconds, and the raw string sort puts ".546Z" after ".546238Z" ("Z" > "2") — so the caption rendered below the files it introduced. Timeline sorting now normalizes fractional seconds before comparing; still a pure string compare, all inputs are UTC Z-suffixed. Co-Authored-By: Claude Fable 5 --- .../chat/MessagesTimeline.logic.test.ts | 90 +++++++++++++++++++ .../components/chat/MessagesTimeline.logic.ts | 18 +++- apps/web/src/session-logic.test.ts | 27 ++++++ apps/web/src/session-logic.ts | 24 ++++- 4 files changed, 156 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index 3a4ba2250c..181ed27c9f 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -261,6 +261,96 @@ describe("resolveAssistantMessageCopyState", () => { }); describe("deriveMessagesTimelineRows", () => { + it("never folds a turn's attachment messages behind Worked-for", () => { + // Live repro (2026-07-28, thread d9f634bd): a Hermes reply of two media + // messages plus a closing text message rendered as ONLY the text — the + // files sat hidden inside the collapsed turn fold, because a single + // last-message-per-turn slot decided visibility. Attachments are output, + // not commentary; they must survive the fold un-expanded. + const makeMessage = ( + id: string, + createdAt: string, + overrides: Record = {}, + ) => ({ + id: `${id}-entry`, + kind: "message" as const, + createdAt, + message: { + id: id as never, + role: "assistant" as const, + text: "", + turnId: "turn-media" as never, + createdAt, + updatedAt: createdAt, + streaming: false, + ...overrides, + }, + }); + + const rows = deriveMessagesTimelineRows({ + timelineEntries: [ + { + id: "user-media-entry", + kind: "message", + createdAt: "2026-07-28T17:54:36.789Z", + message: { + id: "user-media" as never, + role: "user", + text: "send me a couple files", + turnId: null, + createdAt: "2026-07-28T17:54:36.789Z", + updatedAt: "2026-07-28T17:54:36.789Z", + streaming: false, + }, + }, + makeMessage("assistant-commentary", "2026-07-28T17:55:01.000Z", { + text: "Let me grab those.", + }), + makeMessage("media-1", "2026-07-28T17:55:08.546238Z", { + attachments: [ + { + type: "file", + id: "att-1", + name: "keybindings.md", + mimeType: "text/markdown", + sizeBytes: 4096, + }, + ], + }), + makeMessage("media-2", "2026-07-28T17:55:08.546889Z", { + attachments: [ + { + type: "file", + id: "att-2", + name: "workspace-layout.md", + mimeType: "text/markdown", + sizeBytes: 727, + }, + ], + }), + makeMessage("assistant-caption", "2026-07-28T17:55:09.000Z", { + text: "Here are two harmless docs.", + }), + ], + isWorking: false, + activeTurnStartedAt: null, + turnDiffSummaryByAssistantMessageId: new Map(), + revertTurnCountByUserMessageId: new Map(), + }); + + const visibleAssistantIds = rows + .filter( + (row): row is Extract<(typeof rows)[number], { kind: "message" }> => + row.kind === "message" && row.message.role === "assistant", + ) + .map((row) => row.message.id as string); + + // The fold still collapses the commentary, but both media messages and + // the closing caption render without expanding anything. + expect(visibleAssistantIds).toEqual(["media-1", "media-2", "assistant-caption"]); + expect(rows.some((row) => row.kind === "turn-fold")).toBe(true); + }); + it("only enables assistant copy for the terminal assistant message in a turn", () => { const rows = deriveMessagesTimelineRows({ timelineEntries: [ diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index 96faad0a1e..3cf6865fbd 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -223,6 +223,7 @@ export function resolveAssistantMessageCopyState({ function deriveTerminalAssistantMessageIds(timelineEntries: ReadonlyArray) { const lastAssistantMessageIdByResponseKey = new Map(); + const attachmentMessageIds = new Set(); let nullTurnResponseIndex = 0; for (const timelineEntry of timelineEntries) { @@ -238,13 +239,21 @@ function deriveTerminalAssistantMessageIds(timelineEntries: ReadonlyArray 0) { + attachmentMessageIds.add(message.id); + } + const responseKey = message.turnId ? `turn:${message.turnId}` : `unkeyed:${nullTurnResponseIndex}`; lastAssistantMessageIdByResponseKey.set(responseKey, message.id); } - return new Set(lastAssistantMessageIdByResponseKey.values()); + return new Set([...lastAssistantMessageIdByResponseKey.values(), ...attachmentMessageIds]); } interface TurnFold { @@ -456,7 +465,12 @@ function deriveTurnFolds(input: { } const hiddenEntryIds = new Set(); for (const entry of group.entries) { - if (entry.id !== group.terminalEntry?.id) { + // Every terminal message stays visible, not just the group's anchor: + // a turn can end with attachment messages AND a text message, and the + // files are output, not work-log noise to collapse. + const isTerminalMessage = + entry.kind === "message" && input.terminalAssistantMessageIds.has(entry.message.id); + if (entry.id !== group.terminalEntry?.id && !isTerminalMessage) { hiddenEntryIds.add(entry.id); } } diff --git a/apps/web/src/session-logic.test.ts b/apps/web/src/session-logic.test.ts index 0f8059c7f5..b9485b22f5 100644 --- a/apps/web/src/session-logic.test.ts +++ b/apps/web/src/session-logic.test.ts @@ -1574,6 +1574,33 @@ describe("deriveTimelineEntries", () => { }, }); }); + + it("orders mixed-precision timestamps by instant, not by string", () => { + // The server stamps milliseconds while the Hermes plugin stamps + // microseconds. Raw string compare puts ".546Z" after ".546238Z" + // ("Z" > "2"), which rendered a reply's caption below the files it + // introduced. + const makeMessage = (id: string, createdAt: string) => ({ + id: MessageId.make(id), + role: "assistant" as const, + text: id, + createdAt, + turnId: null, + updatedAt: createdAt, + streaming: false, + }); + + const entries = deriveTimelineEntries( + [ + makeMessage("media-micro", "2026-07-28T17:55:08.546238Z"), + makeMessage("text-milli", "2026-07-28T17:55:08.546Z"), + ], + [], + [], + ); + + expect(entries.map((entry) => entry.id)).toEqual(["text-milli", "media-micro"]); + }); }); describe("deriveWorkLogEntries context window handling", () => { diff --git a/apps/web/src/session-logic.ts b/apps/web/src/session-logic.ts index 8abfdb0c1a..c9c5ee2ee8 100644 --- a/apps/web/src/session-logic.ts +++ b/apps/web/src/session-logic.ts @@ -1365,7 +1365,29 @@ export function deriveTimelineEntries( entry, })); return [...messageRows, ...proposedPlanRows, ...workRows].toSorted((a, b) => - a.createdAt.localeCompare(b.createdAt), + compareIsoTimestamps(a.createdAt, b.createdAt), + ); +} + +/** + * Lexicographic ISO compare, robust to mixed fractional-second precision. + * + * The timeline mixes producers: this server stamps milliseconds (`.546Z`) + * while the Hermes plugin stamps microseconds (`.546238Z`). A raw string + * compare puts `.546Z` AFTER `.546238Z` (`Z` > `2`), which rendered a + * reply's lead-in caption below the files it introduced. Padding every + * fraction to nine digits restores instant order while staying a pure + * string comparison — all timestamps in this pipeline are UTC `Z`-suffixed + * IsoDateTime values, so no offset handling is needed. + */ +export function compareIsoTimestamps(a: string, b: string): number { + return normalizeIsoFraction(a).localeCompare(normalizeIsoFraction(b)); +} + +function normalizeIsoFraction(iso: string): string { + return iso.replace( + /(?:\.(\d+))?Z$/, + (_match, fraction: string | undefined) => `.${(fraction ?? "").padEnd(9, "0")}Z`, ); }