fix(web): a turn's media messages no longer hide behind "Worked for …"

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 <[email protected]>
This commit is contained in:
Ben DavisandClaude Fable 5 committed 2026-07-28 11:05:57 -07:00
1 parent ebe7bc4b13
commit 31113dfe7f
4 files changed
+156 -3

No files matched your search

@@ -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<string, unknown> = {},
) => ({
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: [
@@ -223,6 +223,7 @@ export function resolveAssistantMessageCopyState({
function deriveTerminalAssistantMessageIds(timelineEntries: ReadonlyArray<TimelineEntry>) {
const lastAssistantMessageIdByResponseKey = new Map<string, string>();
const attachmentMessageIds = new Set<string>();
let nullTurnResponseIndex = 0;
for (const timelineEntry of timelineEntries) {
@@ -238,13 +239,21 @@ function deriveTerminalAssistantMessageIds(timelineEntries: ReadonlyArray<Timeli
continue;
}
// A message carrying attachments is a deliverable, not commentary: a
// turn's files must stay visible even when a later text message takes
// the last-per-turn slot. Without this, turn-scoped media folded behind
// "Worked for …" and the reply appeared to arrive with no files.
if (message.attachments && message.attachments.length > 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<string>();
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);
}
}
+27
View File
@@ -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", () => {
+23 -1
View File
@@ -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`,
);
}