From 1d907c08fae4466f7e6693ac3e38e3d6f5db40f3 Mon Sep 17 00:00:00 2001 From: yuez Date: Fri, 30 Oct 2026 15:15:14 +0800 Subject: [PATCH] fix(web): merge history and live entries in transcript order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- web/src/lib/entry-projection.ts | 33 +++++++++++++++++++++++++++------ web/tests/chat.history.test.tsx | 24 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/web/src/lib/entry-projection.ts b/web/src/lib/entry-projection.ts index 9da1e8f..693dd08 100644 --- a/web/src/lib/entry-projection.ts +++ b/web/src/lib/entry-projection.ts @@ -355,6 +355,15 @@ function matchIndex(index: PoolIndex, candidate: Entry, isTaken: (at: number) => * then by `(role, text)` when the ids differ (the snapshot carries synthetic * `m` ids while the pages carry pi's raw entry ids). The historical position * is kept so the transcript never re-orders, and each message renders once. + * + * The two inputs are windows over the same transcript, not a page plus a tail: + * the plugin's snapshot carries the whole (bounded) session, while the first + * REST page is only the newest entries. A live entry without a counterpart is + * therefore inserted where its neighbours put it — before the history entry of + * the next match, or appended only when it follows every match. Appending all + * unmatched entries instead would move messages older than the page to the end + * of the stream, so the transcript would stop on an old message and the newest + * ones (already spliced into the page) would sit far above. */ export function mergeEntries(history: Entry[], live: Entry[]): Entry[] { if (!history.length) return live; @@ -365,18 +374,30 @@ export function mergeEntries(history: Entry[], live: Entry[]): Entry[] { const historyIndex = indexPool(history); const claimed = new Set(); const replaces = new Map(); + /** Unmatched live entries keyed by the history index they precede (0..history.length). */ + const inserts = new Map(); + let anchor = 0; for (const entry of live) { const match = matchIndex(historyIndex, entry, (at) => claimed.has(at)); - if (match < 0) continue; + if (match < 0) { + const bucket = inserts.get(anchor); + if (bucket) bucket.push(entry); + else inserts.set(anchor, [entry]); + continue; + } claimed.add(match); replaces.set(match, entry); + anchor = Math.max(anchor, match + 1); } - const merged: Entry[] = history.map((entry, index) => replaces.get(index) ?? entry); - const rendered = new Set(replaces.values()); - for (const entry of live) { - if (!rendered.has(entry)) merged.push(entry); - } + const merged: Entry[] = []; + history.forEach((entry, index) => { + const bucket = inserts.get(index); + if (bucket) merged.push(...bucket); + merged.push(replaces.get(index) ?? entry); + }); + const trailing = inserts.get(history.length); + if (trailing) merged.push(...trailing); return merged; } diff --git a/web/tests/chat.history.test.tsx b/web/tests/chat.history.test.tsx index f9484fc..b9b5df5 100644 --- a/web/tests/chat.history.test.tsx +++ b/web/tests/chat.history.test.tsx @@ -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" }] }),