From 2b94e522e3569634e6e1c3204b0919504bbcd9e1 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Fri, 4 Dec 2026 08:07:26 +0700 Subject: [PATCH 1/2] search: a Gemini CLI reply re-appended under the same id is indexed once Symptom: a Gemini CLI reply that ran tools shows up several times in search: "Let me read the config." three times and its read_file line twice for one reply, so hit and match counts for the session are inflated. Cause: Gemini CLI's chat recorder appends a message's whole record again each time it updates it (tokens, tool calls scheduled, tool calls done), under the same id; its own loader keeps the last record per id. The search adapter turned every record into docs. Fix: the adapter state keeps the last 8 message ids, whether each one's text is indexed and the tool calls indexed for it (it survives read slices and is copied, never changed in place). A record under a known id adds only what was not indexed yet: its text if none was, and tool calls by id (a call without an id by its place in the list). Signed-off-by: Connor Burgess --- server/search/sources/antigravity.test.ts | 97 +++++++++++++++++++++++ server/search/sources/gemini.ts | 51 +++++++++--- 2 files changed, 135 insertions(+), 13 deletions(-) diff --git a/server/search/sources/antigravity.test.ts b/server/search/sources/antigravity.test.ts index 00be55c7..38f52191 100644 --- a/server/search/sources/antigravity.test.ts +++ b/server/search/sources/antigravity.test.ts @@ -192,6 +192,103 @@ describe("gemini cli", () => { expect(whole?.[1].offset).toBe(2); }); + it("a reply re-appended under the same id (tokens, tool calls scheduled, then done) is indexed once", async () => { + // The shape Gemini CLI 0.70's ChatRecordingService writes: pushMessage appends the whole + // record again on every update; its own loader keeps the last record per id. + const dir = path.join(fx.home, ".gemini/tmp/rec/chats"); + mkdirSync(dir, { recursive: true }); + const file = path.join(dir, "session-2026-12-04T01-04-e0000000.jsonl"); + const at = "2026-12-04T01:04:12.194Z"; + const reply = { id: "m1", timestamp: at, type: "gemini", content: "Let me read the config.", thoughts: [] }; + const tokens = { ...reply, tokens: { input: 10, output: 5, total: 15 }, model: "gemini-3-pro" }; + const call = (status: string, extra = {}) => ({ + id: "c1", + name: "read_file", + args: { absolute_path: "/w/tsconfig.json" }, + status, + timestamp: at, + ...extra, + }); + const lines = (...l: unknown[]) => l.map((x) => `${JSON.stringify(x)}\n`).join(""); + const head = lines( + { sessionId: "e0000000-0000-4000-8000-000000000041", startTime: at, lastUpdated: at, kind: "main" }, + { id: "u1", timestamp: at, type: "user", content: [{ text: "Why does the build fail on Windows?" }] }, + { $set: { lastUpdated: at } }, + reply, + tokens, + { $set: { lastUpdated: at } }, + { ...tokens, toolCalls: [call("scheduled")] }, + ); + const tail = lines( + { ...tokens, toolCalls: [call("success", { result: [{ text: "RESULT_never_indexed" }] })] }, + { ...tokens, toolCalls: [call("success"), { name: "list_directory", args: { dir_path: "/w" } }] }, + { ...tokens, toolCalls: [call("success"), { name: "list_directory", args: { dir_path: "/w" } }] }, + { id: "m2", timestamp: at, type: "gemini", content: "The path uses a backslash." }, + { $set: { lastUpdated: at } }, + ); + const want = [ + ["prompt", "Why does the build fail on Windows?"], + ["reply", "Let me read the config."], + ["tool", "read_file /w/tsconfig.json"], + ["tool", "list_directory /w"], + ["reply", "The path uses a backslash."], + ]; + const key = "gemini-cli:e0000000-0000-4000-8000-000000000041"; + const of = (s: Collect) => s.docs.filter((d) => d.key === key).map((d) => [d.field, d.text]); + + // Read whole, and in small slices (the state carries the last id across reads). + writeFileSync(file, head + tail); + for (const opts of [{}, { maxBytes: 64 }]) { + const sink = new Collect(); + await readAll(geminiAdapter(), testEnv(fx.home, fx.state), sink, new Map(), opts); + expect(of(sink)).toEqual(want); + } + + // Read as it grows: the later records arrive in the next pass. + writeFileSync(file, head); + const sink = new Collect(); + const cursors = await readAll(geminiAdapter(), testEnv(fx.home, fx.state), sink); + writeFileSync(file, head + tail); + await readAll(geminiAdapter(), testEnv(fx.home, fx.state), sink, cursors); + expect(of(sink)).toEqual(want); + }); + + it("re-appends: an earlier message, text that comes later, calls without ids, the state left as given", async () => { + const dir = path.join(fx.home, ".gemini/tmp/rec/chats"); + mkdirSync(dir, { recursive: true }); + const file = path.join(dir, "session-2026-12-04T02-00-e0000000.jsonl"); + const at = "2026-12-04T02:00:00Z"; + const ls = { name: "list_directory", args: { dir_path: "/w" } }; + const lines = (...l: unknown[]) => l.map((x) => `${JSON.stringify(x)}\n`).join(""); + const head = lines( + { sessionId: "e0000000-0000-4000-8000-000000000042", startTime: at, lastUpdated: at, kind: "main" }, + { id: "m1", timestamp: at, type: "gemini", content: "", toolCalls: [ls, ls] }, + { id: "m1", timestamp: at, type: "gemini", content: "Two listings.", toolCalls: [ls, ls] }, + { id: "u2", timestamp: at, type: "user", content: "stop" }, + { id: "i1", timestamp: at, type: "info", content: "INFO_never_indexed" }, + ); + const tail = lines( + { id: "m1", timestamp: at, type: "gemini", content: "Two listings.", toolCalls: [ls, ls, { id: "c9", ...ls }] }, + { id: "u2", timestamp: at, type: "user", content: "stop" }, + ); + const key = "gemini-cli:e0000000-0000-4000-8000-000000000042"; + const of = (s: Collect) => s.docs.filter((d) => d.key === key).map((d) => [d.field, d.text]); + writeFileSync(file, head); + const sink = new Collect(); + const cursors = await readAll(geminiAdapter(), testEnv(fx.home, fx.state), sink); + const before = JSON.stringify([...cursors.values()].map((c) => c.state)); + writeFileSync(file, head + tail); + await readAll(geminiAdapter(), testEnv(fx.home, fx.state), sink, cursors); + expect(JSON.stringify([...cursors.values()].map((c) => c.state))).toBe(before); + expect(of(sink)).toEqual([ + ["tool", "list_directory /w"], + ["tool", "list_directory /w"], + ["reply", "Two listings."], + ["prompt", "stop"], + ["tool", "list_directory /w"], + ]); + }); + it("GEMINI_CLI_HOME is the home that holds .gemini, as Gemini CLI reads it", async () => { // Gemini CLI writes to $GEMINI_CLI_HOME/.gemini/tmp; nothing lives at $GEMINI_CLI_HOME/tmp. const job = path.join(fx.home, "..", "job"); diff --git a/server/search/sources/gemini.ts b/server/search/sources/gemini.ts index 4566a241..479a4843 100644 --- a/server/search/sources/gemini.ts +++ b/server/search/sources/gemini.ts @@ -2,8 +2,11 @@ // Owned by: search sources. Gemini CLI history (sources.md §5): `~/.gemini/tmp//chats/ // session-*.jsonl` (current: a header line, message records, `$set` patches) and the older // whole-file `session-*.json` (`{sessionId, messages: […]}`, read whole when it changes). -// User text → prompt, `gemini` text → reply, `toolCalls[]` → tool lines; thoughts, tool -// results, info/error/warning never. No resume line (not verified; SEARCH-SPEC.md D10). +// The .jsonl appends a message's whole record again each time it changes (tokens, tool +// calls scheduled, then done), under the same `id`; the CLI keeps the last one per id, so +// a repeat adds only what is not indexed yet. User text → prompt, `gemini` text → reply, +// `toolCalls[]` → tool lines; thoughts, tool results, info/error/warning never. No resume +// line (not verified; SEARCH-SPEC.md D10). import { readFileSync, statSync } from "node:fs"; import path from "node:path"; import { sessionKey } from "../../../shared/api.js"; @@ -29,6 +32,9 @@ const CHAT = /^session-.*\.jsonl?$/; type Obj = Record; const obj = (v: unknown): Obj | undefined => (v && typeof v === "object" && !Array.isArray(v) ? (v as Obj) : undefined); const str = (v: unknown): string => (typeof v === "string" ? v : ""); +/** A recent message: its id, whether its text is indexed, the tool call keys indexed. */ +type Rec = [string, boolean, string[]]; +const RECENT = 8; /** A message's text: a string, a part list `[{text}]`, or a single part. */ function textOf(c: unknown): string { @@ -129,10 +135,21 @@ export function geminiAdapter(): SourceAdapter { const key = x.key; const t = ms(m.timestamp); const type = m.type; + if (type !== "user" && type !== "gemini" && type !== "model") return; + // Gemini CLI appends a message's whole record again under the same id each time it + // changes. `rec` keeps the last few ids, whether their text is indexed and the tool + // calls already indexed; it is copied, never changed in place (a read may be retried). + const id = str(m.id); + const rec = (Array.isArray(st.rec) ? st.rec : []) as Rec[]; + const was = id ? rec.find((e) => e[0] === id) : undefined; + const r: Rec = was ? [id, was[1], [...was[2]]] : [id, false, []]; + if (id) st.rec = [...rec.filter((e) => e !== was), r].slice(-RECENT); if (type === "user") { time(t); + if (r[1]) return; const text = finish(cleanPromptBlock(textOf(m.content ?? m.message ?? m.text)).text); if (!text) return; + r[1] = true; sink.doc({ key, field: "prompt", at: t, text, offset: at }); if (!st.fa) { st.fa = true; @@ -141,19 +158,27 @@ export function geminiAdapter(): SourceAdapter { } else if (type === "gemini" || type === "model") { time(t); const text = finish(textOf(m.content ?? m.message ?? m.text)); - if (text) sink.doc({ key, field: "reply", at: t, text, offset: at }); - for (const c of Array.isArray(m.toolCalls) ? m.toolCalls : []) { + if (text && !r[1]) { + r[1] = true; + sink.doc({ key, field: "reply", at: t, text, offset: at }); + } + const calls = Array.isArray(m.toolCalls) ? m.toolCalls : []; + for (const [n, c] of calls.entries()) { const o = obj(c); const name = str(o?.name); - if (name) - sink.doc({ - key, - field: "tool", - at: t, - text: toolLine(name, toolArg(o?.args, ["absolute_path", "dir_path", "file_paths"])), - offset: at, - tool: name, - }); + if (!name) continue; + // A call without an id is keyed by its place in the list (the recorder keeps the order). + const tk = str(o?.id) || `#${n} ${name}`; + if (r[2].includes(tk)) continue; + r[2].push(tk); + sink.doc({ + key, + field: "tool", + at: t, + text: toolLine(name, toolArg(o?.args, ["absolute_path", "dir_path", "file_paths"])), + offset: at, + tool: name, + }); } } }; From 0ad5bfdc1da87812265eb67922412d84062b10c2 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Sat, 5 Dec 2026 07:19:27 +0700 Subject: [PATCH 2/2] 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..bc450516 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `sessionpipe-gui`. The format follows ## [Unreleased] +### Fixed + +- Gemini CLI search indexes repeated records for the same recent message and tool calls once. + ## [0.8.18] - 2026-12-04 ### Fixed