Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions server/search/sources/antigravity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
51 changes: 38 additions & 13 deletions server/search/sources/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Owned by: search sources. Gemini CLI history (sources.md §5): `~/.gemini/tmp/<slug>/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";
Expand All @@ -29,6 +32,9 @@ const CHAT = /^session-.*\.jsonl?$/;
type Obj = Record<string, unknown>;
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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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,
});
}
}
};
Expand Down