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:
1 parent
17fcf931c0
commit
1d907c08fa
2 files changed
+51
-6
No files matched your search
@@ -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
|
* then by `(role, text)` when the ids differ (the snapshot carries synthetic
|
||||||
* `m<N>` ids while the pages carry pi's raw entry ids). The historical position
|
* `m<N>` 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.
|
* 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[] {
|
export function mergeEntries(history: Entry[], live: Entry[]): Entry[] {
|
||||||
if (!history.length) return live;
|
if (!history.length) return live;
|
||||||
@@ -365,18 +374,30 @@ export function mergeEntries(history: Entry[], live: Entry[]): Entry[] {
|
|||||||
const historyIndex = indexPool(history);
|
const historyIndex = indexPool(history);
|
||||||
const claimed = new Set<number>();
|
const claimed = new Set<number>();
|
||||||
const replaces = new Map<number, Entry>();
|
const replaces = new Map<number, Entry>();
|
||||||
|
/** Unmatched live entries keyed by the history index they precede (0..history.length). */
|
||||||
|
const inserts = new Map<number, Entry[]>();
|
||||||
|
let anchor = 0;
|
||||||
for (const entry of live) {
|
for (const entry of live) {
|
||||||
const match = matchIndex(historyIndex, entry, (at) => claimed.has(at));
|
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);
|
claimed.add(match);
|
||||||
replaces.set(match, entry);
|
replaces.set(match, entry);
|
||||||
|
anchor = Math.max(anchor, match + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const merged: Entry[] = history.map((entry, index) => replaces.get(index) ?? entry);
|
const merged: Entry[] = [];
|
||||||
const rendered = new Set<Entry>(replaces.values());
|
history.forEach((entry, index) => {
|
||||||
for (const entry of live) {
|
const bucket = inserts.get(index);
|
||||||
if (!rendered.has(entry)) merged.push(entry);
|
if (bucket) merged.push(...bucket);
|
||||||
}
|
merged.push(replaces.get(index) ?? entry);
|
||||||
|
});
|
||||||
|
const trailing = inserts.get(history.length);
|
||||||
|
if (trailing) merged.push(...trailing);
|
||||||
return merged;
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -148,6 +148,30 @@ describe("history entry projection", () => {
|
|||||||
expect(merged[0]?.ts).toBe(TS);
|
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", () => {
|
it("keeps live-only entries and drops raw duplicates when prepending a page", () => {
|
||||||
const live = [
|
const live = [
|
||||||
entryFixture({ id: "m1", role: "user", ts: TS, blocks: [{ type: "text", text: "hello" }] }),
|
entryFixture({ id: "m1", role: "user", ts: TS, blocks: [{ type: "text", text: "hello" }] }),
|
||||||
|
|||||||
Reference in new issue
Block a user