fix(web): merge history and live entries in transcript order

Opening a long session from the recent-activity list showed the transcript
ending at an old `edit` tool call. The plugin's snapshot carries the whole
transcript (`state.entries`), while the first REST page holds only the newest
entries; mergeEntries appended every unmatched live entry after the page, so
the older messages landed past the newest ones and scrolling to the bottom
stopped on a message from the middle of the session.

Unmatched live entries are now inserted where their live neighbours put them —
before the history entry of the next match — and appended only when they follow
every match, so both windows stay one chronologically ordered, deduped stream.
The regression test covers a live window that starts before the history page.
This commit is contained in:
yuez committed 2026-10-30 15:15:14 +08:00
1 parent 17fcf931c0
commit 1d907c08fa
2 files changed
+51 -6

No files matched your search

+24
View File
@@ -148,6 +148,30 @@ describe("history entry projection", () => {
expect(merged[0]?.ts).toBe(TS);
});
it("inserts live entries older than the history window before it, never after the newest page", () => {
// The plugin's snapshot carries the whole transcript; the first REST page is
// only the newest window. The live entries that precede that window are
// older than every history entry, so appending them would end the
// transcript on an old message and hide everything after it.
const history = projectEntries([
{ type: "message", id: "h3", message: { role: "user", content: "third", timestamp: TS + 4 } },
{
type: "message",
id: "h4",
message: { role: "assistant", timestamp: TS + 5, content: [{ type: "text", text: "fourth" }] },
},
]);
const live = [
entryFixture({ id: "m1", role: "user", ts: TS, blocks: [{ type: "text", text: "first" }] }),
entryFixture({ id: "m2", role: "assistant", ts: TS + 1, blocks: [{ type: "text", text: "second" }] }),
entryFixture({ id: "m3", role: "user", ts: TS + 4, blocks: [{ type: "text", text: "third" }] }),
entryFixture({ id: "m4", role: "assistant", ts: TS + 5, blocks: [{ type: "text", text: "fourth" }] }),
];
const merged = mergeEntries(history, live);
expect(merged.map((entry) => entry.id)).toEqual(["m1", "m2", "m3", "m4"]);
});
it("keeps live-only entries and drops raw duplicates when prepending a page", () => {
const live = [
entryFixture({ id: "m1", role: "user", ts: TS, blocks: [{ type: "text", text: "hello" }] }),