fix(providers): carry a turn's stats onto the message the timeline merges from text chunks

The previous change put the carry-over in the reasoning merge. The assistant message merge dropped the stats of its last chunk, so an imported reply of two text blocks lost its Turn stats button.
This commit is contained in:
Mohamed Boudra
2026-11-22 08:33:12 +01:00
parent 8aa0ec66d5
commit c09ff1edb4
2 changed files with 18 additions and 10 deletions
@@ -340,16 +340,24 @@ describe("turn metrics", () => {
it("keeps the stats on a message whose chunks the projection merges", () => {
const store = new InMemoryAgentTimelineStore();
store.initialize("a");
store.append("a", { type: "user_message", text: "go" }, { turnId: "turn-1" });
store.append("a", { type: "assistant_message", text: "Do" }, { turnId: "turn-1" });
store.append("a", { type: "assistant_message", text: "ne" }, { turnId: "turn-1" });
store.stampTurnMetrics("a", metrics, { turnId: "turn-1" });
const at = "2026-11-22T00:00:00.000Z";
store.initialize("a", {
rows: [
{ seq: 1, timestamp: at, item: { type: "user_message", text: "go" } },
{ seq: 2, timestamp: at, item: { type: "assistant_message", text: "Do" } },
{
seq: 3,
timestamp: at,
item: { type: "assistant_message", text: "ne" },
turnMetrics: metrics,
},
],
});
const projected = store.fetch("a").rows;
const rows = store.fetch("a").rows;
expect(projected).toHaveLength(2);
expect(projected[1]).toMatchObject({ item: { text: "Done" }, turnMetrics: metrics });
expect(rows).toHaveLength(2);
expect(rows[1]).toMatchObject({ item: { text: "Done" }, turnMetrics: metrics });
});
it("restores stamped rows when a timeline is seeded from stored rows", () => {
@@ -237,8 +237,6 @@ function mergeReasoningChunks(entries: readonly WorkingEntry[]): WorkingEntry[]
seqEnd: entry.seqEnd,
sourceSeqRanges: mergeSeqRanges(previous.sourceSeqRanges, entry.sourceSeqRanges),
collapsed: Array.from(collapsedKinds),
// The turn's stats ride on its last row, which is the later chunk of a merged message.
...(entry.turnMetrics ? { turnMetrics: entry.turnMetrics } : {}),
};
}
@@ -299,6 +297,8 @@ function mergeAssistantChunks(entries: readonly WorkingEntry[]): WorkingEntry[]
seqEnd: entry.seqEnd,
sourceSeqRanges: mergeSeqRanges(previous.sourceSeqRanges, entry.sourceSeqRanges),
collapsed: Array.from(collapsedKinds),
// The turn's stats ride on its last row, which is the later chunk of a merged message.
...(entry.turnMetrics ? { turnMetrics: entry.turnMetrics } : {}),
};
}