diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d1a545..645d1d8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `sessionpipe-gui`. The format follows ## [Unreleased] +### Fixed + +- Search avoids repeating overlapping capped progress text and the final transcript when a running reply exceeds 20,000 characters. + ## [0.8.19] - 2026-12-04 ### Added diff --git a/server/search/sources/events.test.ts b/server/search/sources/events.test.ts index 418cba96..e535d706 100644 --- a/server/search/sources/events.test.ts +++ b/server/search/sources/events.test.ts @@ -138,6 +138,165 @@ describe("events source", () => { ]); }); + describe("a reply past sessionpipe's 20,000-character cap", () => { + // packages/core/src/privacy/filter.ts: turn.progress keeps the end (capTail), + // turn.transcript keeps the start (cap). + const MAX = 20_000; + const capTail = (v: string) => (v.length > MAX ? `…${v.slice(v.length - MAX + 1)}` : v); + const capHead = (v: string) => (v.length > MAX ? `${v.slice(0, MAX)}…` : v); + const full = Array.from({ length: 6000 }, (_, i) => `w${i}`).join(" "); + /** The reply so far at about `n` characters, cut before a space. */ + const upTo = (n: number) => (n >= full.length ? full : full.slice(0, full.lastIndexOf(" ", n))); + const progress = (n: number, turn = 1) => ev("turn.progress", { turn, user: "Go", assistant: capTail(upTo(n)) }); + const counts = (docs: { field: string; text: string }[]) => { + const c = new Map(); + for (const d of docs) + if (d.field === "reply") + for (const w of d.text.split(/\s+/)) if (/^w\d+$/.test(w)) c.set(w, (c.get(w) ?? 0) + 1); + return c; + }; + + it("progress with a kept end and the head-capped transcript never repeat a word", async () => { + expect(full.length).toBeGreaterThan(34_000); + write([ + ...[12_000, 19_000, 24_000, 30_000, 36_000].map((n) => progress(n)), + ev("turn.transcript", { + turn: 1, + user: "Go", + assistant: capHead(full), + }), + ]); + const sink = new Collect(); + const a = eventsAdapter(); + const [f] = await a.discover(testEnv(dir, dir)); + await a.read(f!, { offset: 0 }, sink); + const c = counts(sink.docs); + expect(c.size).toBe(6000); + expect([...c.values()].every((n) => n === 1)).toBe(true); + expect(sink.docs.filter((d) => d.field === "reply").length).toBe(5); + }); + + it("continues from a cursor saved before the end fingerprint existed", async () => { + write([progress(12_000)]); + const a = eventsAdapter(); + const [f] = await a.discover(testEnv(dir, dir)); + const sink = new Collect(); + const r1 = await a.read(f!, { offset: 0 }, sink); + // The cursor shape older releases saved: { n, u, a, h } only. + const st = r1.state as { s: number[]; p: { n: number; u: number; a: number; h: number } }; + const old = { + s: st.s, + p: { n: st.p.n, u: st.p.u, a: st.p.a, h: st.p.h }, + }; + // Hashes and lengths only: no word of the reply is kept in the cursor. + expect( + Object.values(st.p) + .flat() + .every((v) => typeof v === "number"), + ).toBe(true); + write([ + progress(19_000), + progress(24_000), + ev("turn.transcript", { + turn: 1, + user: "Go", + assistant: capHead(upTo(24_000)), + }), + ]); + const [g] = await a.discover(testEnv(dir, dir)); + await a.read(g!, { offset: r1.offset, state: old }, sink); + const c = counts(sink.docs); + expect(c.size).toBe(upTo(24_000).split(" ").length); + expect([...c.values()].every((n) => n === 1)).toBe(true); + }); + + it("a kept end that does not continue the last text is still indexed whole", async () => { + const other = Array.from({ length: 5000 }, (_, i) => `x${i}`).join(" "); + write([progress(12_000), ev("turn.progress", { turn: 1, user: "Go", assistant: capTail(other) })]); + const sink = new Collect(); + const a = eventsAdapter(); + const [f] = await a.discover(testEnv(dir, dir)); + await a.read(f!, { offset: 0 }, sink); + const last = sink.docs[sink.docs.length - 1]!; + expect(last.text.endsWith("x4999")).toBe(true); + expect(last.text.length).toBeGreaterThan(19_000); + }); + + it("a kept end past a gap is indexed whole, though the last end's line recurs in it", async () => { + const sep = `\n${"=".repeat(80)}\n`; + const p1 = `${full.slice(0, 8000)}${sep}`; + const p2 = `${p1}${full.slice(8000, 30_000)}${sep}${full.slice(30_000)}${sep}`; + write([ + ev("turn.progress", { turn: 1, user: "Go", assistant: p1 }), + ev("turn.progress", { turn: 1, user: "Go", assistant: capTail(p2) }), + ]); + const sink = new Collect(); + const a = eventsAdapter(); + const [f] = await a.discover(testEnv(dir, dir)); + await a.read(f!, { offset: 0 }, sink); + // Not joined at the separator in its middle: everything it holds is indexed. + const kept = capTail(p2) + .split(/\s+/) + .filter((w) => /^w\d+$/.test(w)); + const c = counts(sink.docs); + expect(kept.filter((w) => !c.has(w))).toEqual([]); + }); + + it("a kept end past a gap is indexed whole, though the last text's end is repeated in it", async () => { + // The last 1,024 characters of the first progress are printed again, once, more than + // 20,000 characters later: the place they recur is not where that progress ended. + const p1 = upTo(12_000); + const p2 = `${full} mustfindmarker ${p1.slice(-1024)} finalword`; + expect(p2.length - p1.length).toBeGreaterThan(MAX); + write([ + ev("turn.progress", { turn: 1, user: "Go", assistant: p1 }), + ev("turn.progress", { turn: 1, user: "Go", assistant: capTail(p2) }), + ]); + const sink = new Collect(); + const a = eventsAdapter(); + const [f] = await a.discover(testEnv(dir, dir)); + await a.read(f!, { offset: 0 }, sink); + const last = sink.docs[sink.docs.length - 1]!; + expect(last.text).toContain("mustfindmarker"); + expect(last.text.length).toBeGreaterThan(19_000); + }); + + it("a kept end past a gap after another kept end is indexed whole, though that end is repeated", async () => { + // As above, with a first progress that is itself past the cap, so its length is not known. + const p1 = upTo(24_000); + const p2 = `${full} ${full.slice(0, 12_000)} mustfindmarker ${p1.slice(-1024)} finalword`; + write([ + ev("turn.progress", { turn: 1, user: "Go", assistant: capTail(p1) }), + ev("turn.progress", { turn: 1, user: "Go", assistant: capTail(p2) }), + ]); + const sink = new Collect(); + const a = eventsAdapter(); + const [f] = await a.discover(testEnv(dir, dir)); + await a.read(f!, { offset: 0 }, sink); + const last = sink.docs[sink.docs.length - 1]!; + expect(last.text).toContain("mustfindmarker"); + expect(last.text.length).toBeGreaterThan(19_000); + }); + + it("a new turn's kept end is indexed whole, and its transcript is not skipped", async () => { + write([ + progress(24_000, 2), + ev("turn.transcript", { + turn: 2, + user: "Go", + assistant: capHead(upTo(24_000)), + }), + ]); + const sink = new Collect(); + const a = eventsAdapter(); + const [f] = await a.discover(testEnv(dir, dir)); + await a.read(f!, { offset: 0 }, sink); + // Nothing of the start was seen before the transcript: it is indexed, whole. + expect(counts(sink.docs).get("w0")).toBe(1); + expect(counts(sink.docs).get(upTo(24_000).split(" ").pop()!)).toBe(1); + }); + }); + it("a tier upgrade of the same seq adds only what the lower copy lacked", async () => { const p = write([ev("turn.transcript", { turn: 1, user: "hidden", assistant: "hidden" }, { tier: 0, seq: 7 })]); write([ev("turn.transcript", { turn: 1, user: "Now visible", assistant: "Yes" }, { tier: 2, seq: 7 })]); diff --git a/server/search/sources/events.ts b/server/search/sources/events.ts index 1f36be6d..27cabbba 100644 --- a/server/search/sources/events.ts +++ b/server/search/sources/events.ts @@ -44,6 +44,68 @@ function fp(s: string, upTo = s.length): number { return h >>> 0; } +/** sessionpipe's cap on a reply (core's privacy/filter.ts MAX_SIDE): a running turn's + * reply past it arrives in turn.progress as "…" and its last 19,999 characters. */ +const CAP = 20_000; +/** The end a kept-end progress is joined at: found by its last 64 characters, checked + * against its last END, so a short line that recurs is not taken for it. */ +const END = 1024; + +/** FNV-1a over `s[from, to)`. */ +function hashAt(s: string, from: number, to: number): number { + let h = 2166136261; + for (let i = from; i < to; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619); + return h >>> 0; +} + +/** The fingerprints of `s`'s end: [last 64, last END], or 0s when it is shorter than END. */ +function endFp(s: string): [number, number] { + const n = s.length; + return n < END ? [0, 0] : [hashAt(s, n - 64, n), hashAt(s, n - END, n)]; +} + +/** FNV-1a over `s` read backwards from `to`, noted after every END characters and stopping + * before `stop`: rung k is a fingerprint of the (k + 1) * END characters that end at `to`. */ +function rungs(s: string, to: number, stop: number): number[] { + const out: number[] = []; + let h = 2166136261; + for (let i = to - 1, n = 1; i >= stop; i--, n++) { + h = Math.imul(h ^ s.charCodeAt(i), 16777619); + if (n % END === 0) out.push(h >>> 0); + } + return out; +} + +/** The rungs of a whole reply text's end. A kept end's leading "…" is not the reply's. */ +function endRungs(s: string, kept: boolean): number[] { + return rungs(s, s.length, kept ? 1 : 0); +} + +/** Where in the kept end `s` a text with end fingerprints `e`/`f` ended, or -1 when `s` does + * not hold it exactly once. */ +function findEnd(s: string, e: number, f: number): number { + let at = -1; + for (let j = END; j <= s.length; j++) { + if (hashAt(s, j - 64, j) !== e || hashAt(s, j - END, j) !== f) continue; + if (at >= 0) return -1; + at = j; + } + return at; +} + +/** Whether what the kept end `s` holds before `end` is the last text's own end, by that + * text's rungs `l`: every whole END of it is compared, so a passage of the last text that + * is only repeated later in the reply is not taken for the place it ended. Fewer than END + * characters, at the start of `s`, are left unchecked. `whole` is the last text's length + * when all of it was seen, or -1. */ +function continues(s: string, end: number, l: number[], whole: number): boolean { + const before = end - 1; + // More before the join than the last text held: that text ended somewhere earlier. + if (whole >= 0 && before > whole) return false; + const k = Math.floor(before / END); + return k > 0 && k <= l.length && rungs(s, end, end - k * END)[k - 1] === l[k - 1]; +} + /** Start a continuation at a word boundary a little before `from`, so no word is split. */ function tail(s: string, from: number): string { // Already on a word boundary: nothing to repeat. @@ -109,11 +171,21 @@ export function eventsAdapter(): SourceAdapter { async read(file: SourceFile, from: ReadCursor, sink: DocSink, opts: ReadOptions = {}): Promise { const st = (obj(from.state) ? { ...(from.state as Obj) } : {}) as { [k: string]: AdapterState }; const seen: number[] = Array.isArray(st.s) ? (st.s as number[]).slice() : []; + // n: the turn; u: its prompt indexed; a: how much of the reply's start is indexed, + // h: the fingerprint of that start; e: the end fingerprint of the last reply text seen + // and f: of its last END characters, and g: set when that text's end is not at `a` (a kept + // end read with a gap before it), and l: fingerprints of that text's last END, 2 * END, + // ... characters. Hashes and lengths only, never the words; e, f, g and l are absent + // from older cursors. const pg = (obj(st.p) ? { ...(st.p as Obj) } : { n: -1, u: 0, a: 0, h: 0 }) as { n: number; u: number; a: number; h: number; + e?: number; + f?: number; + g?: number; + l?: number[]; }; const facts = new Map(); let lastTitle: [string, string] = ["", ""]; @@ -216,23 +288,48 @@ export function eventsAdapter(): SourceAdapter { const asst = str(data.assistant); const same = turn === pg.n && !parent; if (user && !(same && pg.u)) prompt(user, t); - if (asst) { - // The same turn's text grown: only the new part (a word-aligned continuation). - const cont = same && pg.a > 0 && fp(asst, pg.a) === pg.h; - if (cont && asst.length > pg.a) reply(tail(asst, pg.a), t); - else if (!cont) reply(asst, t); - } + // The same turn's text grown: only the new part (a word-aligned continuation). + const cont = !!asst && same && pg.a > 0 && fp(asst, pg.a) === pg.h; + // A reply past sessionpipe's cap arrives in turn.progress as "…" and its end: + // joined where the last text seen ended, as its start moves with every progress. + const kept = type === "turn.progress" && asst.length >= CAP && asst.startsWith("…"); + const at = kept && !cont && same && pg.e ? findEnd(asst, pg.e, pg.f ?? 0) : -1; + // All of the last text was seen when its start is the indexed start (no gap, and + // not itself a kept end, which is CAP long). + const whole = !pg.g && pg.a < CAP ? pg.a : -1; + const end = at >= 0 && continues(asst, at, Array.isArray(pg.l) ? pg.l : [], whole) ? at : -1; + if (cont) { + if (asst.length > pg.a) reply(tail(asst, pg.a), t); + } else if (end >= 0) { + if (asst.length > end) reply(tail(asst, end), t); + } else if (asst) reply(asst, t); if (!parent) { if (!same) { pg.n = turn; pg.u = 0; pg.a = 0; pg.h = 0; + pg.e = 0; + pg.f = 0; + pg.g = 0; + pg.l = []; } if (user) pg.u = 1; - if (asst && asst.length >= pg.a) { - pg.a = asst.length; - pg.h = fp(asst); + if (asst) { + if (end >= 0) { + // A kept end that continues the last text: the indexed start grows with it, + // unless that text was read after a gap. + if (!pg.g) pg.a += asst.length - end; + } else if (kept && !cont) { + // A kept end that cannot be joined: indexed whole, after a gap. + pg.g = 1; + } else if (asst.length >= pg.a) { + pg.a = asst.length; + pg.h = fp(asst); + pg.g = 0; + } else pg.g = 1; + [pg.e, pg.f] = endFp(asst); + pg.l = endRungs(asst, kept); } } } else if (type === "tool.ended" && tier >= 1) {