From 31675a5f93f70b15dcb128413ad57c05ddf4a91c Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Fri, 4 Dec 2026 13:07:47 +0700 Subject: [PATCH 1/3] search: a running reply past 20,000 characters is indexed once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sessionpipe sends a running turn's reply in turn.progress with its end kept ("…" and the last 19,999 characters) once it passes 20,000 characters. The continuation check hashes the text's start, which moves with every progress, so each later progress was indexed whole again and so was the head-capped turn.transcript: a 34,889-character reply came to 98,997 indexed characters. The cursor now also keeps two fingerprints of the end of the last reply text seen (its last 64 and last 1,024 characters). A kept-end progress whose start does not continue is searched for that end, and when it holds it exactly once only what follows is indexed; how much of the reply's start is indexed grows with it, so the head-capped transcript adds nothing already indexed. A kept end that cannot be joined is indexed whole, as before, and stops the start from growing. Signed-off-by: Connor Burgess --- server/search/sources/events.test.ts | 119 +++++++++++++++++++++++++++ server/search/sources/events.ts | 77 +++++++++++++++-- 2 files changed, 187 insertions(+), 9 deletions(-) diff --git a/server/search/sources/events.test.ts b/server/search/sources/events.test.ts index 418cba96..30860021 100644 --- a/server/search/sources/events.test.ts +++ b/server/search/sources/events.test.ts @@ -138,6 +138,125 @@ 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).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 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..17c4f84b 100644 --- a/server/search/sources/events.ts +++ b/server/search/sources/events.ts @@ -44,6 +44,38 @@ 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)]; +} + +/** Where in `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; +} + /** 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 +141,19 @@ 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). Hashes and lengths only, never the words; e, f and g + // 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; }; const facts = new Map(); let lastTitle: [string, string] = ["", ""]; @@ -216,23 +256,42 @@ 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 end = kept && !cont && same && pg.e ? findEnd(asst, pg.e, pg.f ?? 0) : -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; } 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); } } } else if (type === "tool.ended" && tier >= 1) { From 26ca736d36e6d06875a7113306ee97068d30dde7 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Sat, 5 Dec 2026 07:19:28 +0700 Subject: [PATCH 2/3] docs: record the fix under Unreleased Signed-off-by: Connor Burgess --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5299e266..170362a5 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.18] - 2026-12-04 ### Fixed From aaa302b962f02fe24a9a9d8393fbc48d9a8aaaae Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Sat, 5 Dec 2026 15:00:06 +0700 Subject: [PATCH 3/3] search: a capped progress joins only where the last text's whole end is found A progress more than 20,000 characters past the last one no longer holds that text's end. When the reply then repeated its last 1,024 characters once, the join was made at the copy and the text kept before it was not indexed. The cursor now also keeps fingerprints of the last text's final 1,024, 2,048, ... characters. A join is made only when every whole 1,024 characters the progress keeps before it match, and never when it keeps more before the join than the last text held. Otherwise the progress is indexed whole, as before. Signed-off-by: Connor Burgess --- server/search/sources/events.test.ts | 42 +++++++++++++++++++++++- server/search/sources/events.ts | 48 +++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/server/search/sources/events.test.ts b/server/search/sources/events.test.ts index 30860021..e535d706 100644 --- a/server/search/sources/events.test.ts +++ b/server/search/sources/events.test.ts @@ -189,7 +189,11 @@ describe("events source", () => { 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).every((v) => typeof v === "number")).toBe(true); + expect( + Object.values(st.p) + .flat() + .every((v) => typeof v === "number"), + ).toBe(true); write([ progress(19_000), progress(24_000), @@ -238,6 +242,42 @@ describe("events source", () => { 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), diff --git a/server/search/sources/events.ts b/server/search/sources/events.ts index 17c4f84b..27cabbba 100644 --- a/server/search/sources/events.ts +++ b/server/search/sources/events.ts @@ -64,8 +64,25 @@ function endFp(s: string): [number, number] { return n < END ? [0, 0] : [hashAt(s, n - 64, n), hashAt(s, n - END, n)]; } -/** Where in `s` a text with end fingerprints `e`/`f` ended, or -1 when `s` does not hold it - * exactly once. */ +/** 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++) { @@ -76,6 +93,19 @@ function findEnd(s: string, e: number, f: number): number { 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. @@ -144,8 +174,9 @@ export function eventsAdapter(): SourceAdapter { // 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). Hashes and lengths only, never the words; e, f and g - // are absent from older cursors. + // 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; @@ -154,6 +185,7 @@ export function eventsAdapter(): SourceAdapter { e?: number; f?: number; g?: number; + l?: number[]; }; const facts = new Map(); let lastTitle: [string, string] = ["", ""]; @@ -261,7 +293,11 @@ export function eventsAdapter(): SourceAdapter { // 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 end = kept && !cont && same && pg.e ? findEnd(asst, pg.e, pg.f ?? 0) : -1; + 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) { @@ -276,6 +312,7 @@ export function eventsAdapter(): SourceAdapter { pg.e = 0; pg.f = 0; pg.g = 0; + pg.l = []; } if (user) pg.u = 1; if (asst) { @@ -292,6 +329,7 @@ export function eventsAdapter(): SourceAdapter { 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) {