@nandoblanco/opencode-local-search 0.0.0-stage → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/search.ts ADDED
@@ -0,0 +1,1277 @@
1
+ "use strict";
2
+
3
+ import { existsSync } from "node:fs";
4
+ import { homedir } from "node:os";
5
+ import { join } from "node:path";
6
+
7
+ export type SearchMode = "smart" | "fuzzy" | "regex";
8
+ export type RoleFilter = "all" | "user" | "assistant" | "shell";
9
+ export type ScopeFilter = "all" | "project";
10
+ export type DatePreset = "all" | "today" | "yesterday" | "7d" | "30d";
11
+
12
+ export interface SearchEntry {
13
+ id: string;
14
+ messageId?: string;
15
+ role: "user" | "assistant" | "shell";
16
+ kind: "message" | "tool" | "filepath";
17
+ text: string;
18
+ textLower: string;
19
+ toolName?: string;
20
+ filePath?: string;
21
+ timeCreated: number;
22
+ }
23
+
24
+ export interface IndexedSession {
25
+ id: string;
26
+ title: string;
27
+ titleLower: string;
28
+ directory: string;
29
+ directoryNorm: string;
30
+ timeCreated: number;
31
+ timeUpdated: number;
32
+ tools: string[];
33
+ files: string[];
34
+ entries: SearchEntry[];
35
+ }
36
+
37
+ export interface SessionIndex {
38
+ dbPath: string;
39
+ loadedAt: number;
40
+ sessions: IndexedSession[];
41
+ }
42
+
43
+ export interface SearchEntryMatch {
44
+ messageId?: string;
45
+ role: "user" | "assistant" | "shell" | "title";
46
+ kind: "title" | "message" | "tool" | "filepath" | "fuzzy";
47
+ toolName?: string;
48
+ filePath?: string;
49
+ timeCreated: number;
50
+ snippet: string;
51
+ matchedTerm: string;
52
+ }
53
+
54
+ export interface SearchSessionMatch {
55
+ session: IndexedSession;
56
+ score: number;
57
+ matchBadge: string;
58
+ bestSnippet: string;
59
+ bestRole: string;
60
+ bestMatch?: SearchEntryMatch;
61
+ totalMatches: number;
62
+ matches: SearchEntryMatch[];
63
+ }
64
+
65
+ export interface SearchOptions {
66
+ scope?: ScopeFilter;
67
+ currentDirectory?: string;
68
+ role?: RoleFilter;
69
+ datePreset?: DatePreset;
70
+ mode?: SearchMode;
71
+ limit?: number;
72
+ }
73
+
74
+ export interface ParsedQuery {
75
+ raw: string;
76
+ terms: string[];
77
+ fuzzyTerms: string[];
78
+ regex?: RegExp;
79
+ regexError?: string;
80
+ role: RoleFilter;
81
+ scope?: ScopeFilter;
82
+ fileFilter?: string;
83
+ toolFilter?: string;
84
+ inFilter?: "title" | "message" | "tool" | "file";
85
+ dateRange?: { start?: number; end?: number; label: string };
86
+ effectiveMode: SearchMode;
87
+ }
88
+
89
+ interface SqliteRowsDriver {
90
+ all(sql: string, ...params: unknown[]): Record<string, unknown>[];
91
+ close(): void;
92
+ }
93
+
94
+ let cachedIndex: SessionIndex | null = null;
95
+ let cachedIndexEvictTimer: ReturnType<typeof setTimeout> | null = null;
96
+ const INDEX_TTL_MS = 6000; // Refresh at most every 6s unless forced
97
+ const INDEX_IDLE_EVICT_MS = 120_000; // Free cached index after 2m idle
98
+ const MAX_INDEXED_TEXT_CHARS = 4000;
99
+
100
+ function capIndexedText(text: string): string {
101
+ return text.length > MAX_INDEXED_TEXT_CHARS ? text.slice(0, MAX_INDEXED_TEXT_CHARS) : text;
102
+ }
103
+
104
+ function scheduleIndexIdleEviction(): void {
105
+ if (cachedIndexEvictTimer) {
106
+ clearTimeout(cachedIndexEvictTimer);
107
+ }
108
+ cachedIndexEvictTimer = setTimeout(() => {
109
+ cachedIndex = null;
110
+ cachedIndexEvictTimer = null;
111
+ }, INDEX_IDLE_EVICT_MS);
112
+ cachedIndexEvictTimer.unref?.();
113
+ }
114
+
115
+ export function resolveDbPath(customPath?: string): string {
116
+ if (customPath && existsSync(customPath)) {
117
+ return customPath;
118
+ }
119
+ if (process.env.OPENCODE_DB_PATH && existsSync(process.env.OPENCODE_DB_PATH)) {
120
+ return process.env.OPENCODE_DB_PATH;
121
+ }
122
+ const home = homedir();
123
+ const candidates: string[] = [];
124
+ if (process.env.XDG_DATA_HOME) {
125
+ candidates.push(join(process.env.XDG_DATA_HOME, "opencode", "opencode.db"));
126
+ }
127
+ candidates.push(join(home, ".local", "share", "opencode", "opencode.db"));
128
+ if (process.env.LOCALAPPDATA) {
129
+ candidates.push(join(process.env.LOCALAPPDATA, "opencode", "opencode.db"));
130
+ }
131
+ for (const candidate of candidates) {
132
+ if (existsSync(candidate)) {
133
+ return candidate;
134
+ }
135
+ }
136
+ return candidates[0];
137
+ }
138
+
139
+ async function openBunSqlite(dbPath: string): Promise<SqliteRowsDriver> {
140
+ const bunSqlite = await import("bun:sqlite");
141
+ let db: InstanceType<typeof bunSqlite.Database>;
142
+ try {
143
+ db = new bunSqlite.Database(dbPath, { readonly: true });
144
+ } catch {
145
+ db = new bunSqlite.Database(dbPath);
146
+ }
147
+ return {
148
+ all(sql: string, ...params: unknown[]) {
149
+ return db.query(sql).all(...(params as never[])) as Record<string, unknown>[];
150
+ },
151
+ close() {
152
+ db.close();
153
+ },
154
+ };
155
+ }
156
+
157
+ async function openNodeSqlite(dbPath: string): Promise<SqliteRowsDriver> {
158
+ const nodeSqlite = await import("node:sqlite");
159
+ let db: InstanceType<typeof nodeSqlite.DatabaseSync>;
160
+ try {
161
+ db = new nodeSqlite.DatabaseSync(dbPath, { readOnly: true });
162
+ } catch {
163
+ db = new nodeSqlite.DatabaseSync(dbPath);
164
+ }
165
+ return {
166
+ all(sql: string, ...params: unknown[]) {
167
+ return db.prepare(sql).all(...(params as never[])) as Record<string, unknown>[];
168
+ },
169
+ close() {
170
+ db.close();
171
+ },
172
+ };
173
+ }
174
+
175
+ async function openReadOnlyDb(dbPath: string): Promise<SqliteRowsDriver> {
176
+ const isBun = typeof (globalThis as { Bun?: unknown }).Bun !== "undefined";
177
+ if (isBun) {
178
+ try {
179
+ return await openBunSqlite(dbPath);
180
+ } catch {
181
+ return await openNodeSqlite(dbPath);
182
+ }
183
+ }
184
+ try {
185
+ return await openNodeSqlite(dbPath);
186
+ } catch {
187
+ return await openBunSqlite(dbPath);
188
+ }
189
+ }
190
+
191
+ export function normalizePath(p: string | undefined | null): string {
192
+ if (!p) return "";
193
+ return p.replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase();
194
+ }
195
+
196
+ function extractFilePathsFromInput(input: unknown, out: Set<string>): void {
197
+ if (!input) return;
198
+ if (typeof input === "string") {
199
+ try {
200
+ const parsed = JSON.parse(input);
201
+ extractFilePathsFromInput(parsed, out);
202
+ } catch {
203
+ return;
204
+ }
205
+ return;
206
+ }
207
+ if (typeof input !== "object") return;
208
+ const record = input as Record<string, unknown>;
209
+ for (const key of ["path", "filePath", "file", "file_path", "targetFile", "workdir", "directory"]) {
210
+ const val = record[key];
211
+ if (typeof val === "string" && val.trim()) {
212
+ out.add(val.trim());
213
+ }
214
+ }
215
+ if (Array.isArray(record.files)) {
216
+ for (const item of record.files) {
217
+ if (typeof item === "string" && item.trim()) {
218
+ out.add(item.trim());
219
+ }
220
+ }
221
+ }
222
+ }
223
+
224
+ function stringifyToolInput(input: unknown): string {
225
+ if (!input) return "";
226
+ if (typeof input === "string") return input;
227
+ if (typeof input !== "object") return String(input);
228
+ const record = input as Record<string, unknown>;
229
+ const parts: string[] = [];
230
+ for (const [k, v] of Object.entries(record)) {
231
+ if (typeof v === "string" && v.trim()) {
232
+ parts.push(`${k}: ${v.trim()}`);
233
+ } else if (typeof v === "number" || typeof v === "boolean") {
234
+ parts.push(`${k}: ${v}`);
235
+ }
236
+ }
237
+ return parts.join(" · ");
238
+ }
239
+
240
+ export async function loadSessionIndex(force = false, customDbPath?: string): Promise<SessionIndex> {
241
+ const dbPath = resolveDbPath(customDbPath);
242
+ if (!force && cachedIndex && cachedIndex.dbPath === dbPath && Date.now() - cachedIndex.loadedAt < INDEX_TTL_MS) {
243
+ return cachedIndex;
244
+ }
245
+ if (!existsSync(dbPath)) {
246
+ return { dbPath, loadedAt: Date.now(), sessions: [] };
247
+ }
248
+
249
+ const db = await openReadOnlyDb(dbPath);
250
+ const sessionsMap = new Map<string, IndexedSession>();
251
+ const sessionFiles = new Map<string, Set<string>>();
252
+ const sessionTools = new Map<string, Set<string>>();
253
+
254
+ try {
255
+ const tables = new Set(
256
+ db
257
+ .all("SELECT name FROM sqlite_master WHERE type = 'table'")
258
+ .map((row) => String(row.name || ""))
259
+ );
260
+
261
+ // 1. OpenCode V2 Schema (session_v2 + session_message)
262
+ if (tables.has("session_v2")) {
263
+ const v2Cols = new Set(
264
+ db.all("PRAGMA table_info(session_v2)").map((col) => String(col.name || ""))
265
+ );
266
+ const dirExpr = v2Cols.has("directory")
267
+ ? "directory"
268
+ : v2Cols.has("workspace_directory")
269
+ ? "COALESCE(workspace_directory, root_directory, '')"
270
+ : "''";
271
+ const whereClause = v2Cols.has("time_archived") ? "WHERE time_archived IS NULL" : "";
272
+
273
+ const v2Sessions = db.all(`
274
+ SELECT id, title, ${dirExpr} AS directory, time_created, time_updated
275
+ FROM session_v2
276
+ ${whereClause}
277
+ ORDER BY time_updated DESC
278
+ LIMIT 800
279
+ `);
280
+
281
+ for (const row of v2Sessions) {
282
+ const id = String(row.id || "");
283
+ if (!id) continue;
284
+ const title = String(row.title || "(untitled session)");
285
+ const directory = String(row.directory || "");
286
+ sessionsMap.set(id, {
287
+ id,
288
+ title,
289
+ titleLower: title.toLowerCase(),
290
+ directory,
291
+ directoryNorm: normalizePath(directory),
292
+ timeCreated: Number(row.time_created || 0),
293
+ timeUpdated: Number(row.time_updated || 0),
294
+ tools: [],
295
+ files: [],
296
+ entries: [],
297
+ });
298
+ sessionFiles.set(id, new Set());
299
+ sessionTools.set(id, new Set());
300
+ }
301
+
302
+ if (tables.has("session_message") && sessionsMap.size > 0) {
303
+ const msgWhere = v2Cols.has("time_archived")
304
+ ? "WHERE s.time_archived IS NULL AND m.type IN ('user', 'assistant', 'shell')"
305
+ : "WHERE m.type IN ('user', 'assistant', 'shell')";
306
+ let v2Messages: Record<string, unknown>[];
307
+ try {
308
+ v2Messages = db.all(`
309
+ SELECT id, session_id, type, time_created, data
310
+ FROM (
311
+ SELECT m.id, m.session_id, m.type, m.time_created, m.data,
312
+ ROW_NUMBER() OVER (PARTITION BY m.session_id ORDER BY m.time_created DESC) AS rn
313
+ FROM session_message m
314
+ JOIN session_v2 s ON s.id = m.session_id
315
+ ${msgWhere}
316
+ )
317
+ WHERE rn <= 250
318
+ ORDER BY time_created DESC
319
+ LIMIT 30000
320
+ `);
321
+ } catch {
322
+ v2Messages = db.all(`
323
+ SELECT m.id, m.session_id, m.type, m.time_created, m.data
324
+ FROM session_message m
325
+ JOIN session_v2 s ON s.id = m.session_id
326
+ ${msgWhere}
327
+ ORDER BY m.time_created DESC
328
+ LIMIT 18000
329
+ `);
330
+ }
331
+
332
+ for (const row of v2Messages) {
333
+ const sessionId = String(row.session_id || "");
334
+ const session = sessionsMap.get(sessionId);
335
+ if (!session) continue;
336
+ const msgId = String(row.id || "");
337
+ const timeCreated = Number(row.time_created || session.timeUpdated);
338
+ const rawData = String(row.data || "");
339
+ if (!rawData) continue;
340
+
341
+ let parsed: Record<string, unknown>;
342
+ try {
343
+ parsed = JSON.parse(rawData);
344
+ } catch {
345
+ continue;
346
+ }
347
+
348
+ const type = String(row.type || parsed.type || "");
349
+ const filesSet = sessionFiles.get(sessionId)!;
350
+ const toolsSet = sessionTools.get(sessionId)!;
351
+
352
+ if (type === "user") {
353
+ const rawText = typeof parsed.text === "string" ? parsed.text.trim() : "";
354
+ const text = capIndexedText(rawText);
355
+ if (text) {
356
+ session.entries.push({
357
+ id: msgId,
358
+ messageId: msgId,
359
+ role: "user",
360
+ kind: "message",
361
+ text,
362
+ textLower: text.toLowerCase(),
363
+ timeCreated,
364
+ });
365
+ }
366
+ if (Array.isArray(parsed.files)) {
367
+ for (const f of parsed.files) {
368
+ if (f && typeof f === "object") {
369
+ const fileObj = f as Record<string, unknown>;
370
+ const source = fileObj.source as Record<string, unknown> | undefined;
371
+ const filePath =
372
+ (typeof fileObj.name === "string" && fileObj.name) ||
373
+ (source && typeof source.uri === "string" && source.uri) ||
374
+ "";
375
+ if (filePath) {
376
+ filesSet.add(filePath);
377
+ session.entries.push({
378
+ id: `${msgId}:file:${filePath}`,
379
+ messageId: msgId,
380
+ role: "user",
381
+ kind: "filepath",
382
+ text: filePath,
383
+ textLower: filePath.toLowerCase(),
384
+ filePath,
385
+ timeCreated,
386
+ });
387
+ }
388
+ }
389
+ }
390
+ }
391
+ } else if (type === "assistant" && Array.isArray(parsed.content)) {
392
+ let partIdx = 0;
393
+ for (const item of parsed.content) {
394
+ if (!item || typeof item !== "object") continue;
395
+ const part = item as Record<string, unknown>;
396
+ if (part.type === "text" && typeof part.text === "string") {
397
+ const text = capIndexedText(part.text.trim());
398
+ if (text) {
399
+ session.entries.push({
400
+ id: partIdx === 0 ? msgId : `${msgId}:part:${partIdx}`,
401
+ messageId: msgId,
402
+ role: "assistant",
403
+ kind: "message",
404
+ text,
405
+ textLower: text.toLowerCase(),
406
+ timeCreated,
407
+ });
408
+ partIdx++;
409
+ }
410
+ } else if (part.type === "tool" && typeof part.name === "string") {
411
+ const toolName = part.name;
412
+ toolsSet.add(toolName);
413
+ const state = (part.state as Record<string, unknown>) || {};
414
+ const localFiles = new Set<string>();
415
+ extractFilePathsFromInput(state.input, localFiles);
416
+ for (const fp of localFiles) {
417
+ filesSet.add(fp);
418
+ session.entries.push({
419
+ id: `${msgId}:file:${fp}`,
420
+ messageId: msgId,
421
+ role: "assistant",
422
+ kind: "filepath",
423
+ text: `${toolName} -> ${fp}`,
424
+ textLower: `${toolName} ${fp}`.toLowerCase(),
425
+ toolName,
426
+ filePath: fp,
427
+ timeCreated,
428
+ });
429
+ }
430
+ const inputSummary = stringifyToolInput(state.input);
431
+ if (inputSummary) {
432
+ const toolText = capIndexedText(`[${toolName}] ${inputSummary}`);
433
+ session.entries.push({
434
+ id: `${msgId}:tool:${toolName}:${partIdx}`,
435
+ messageId: msgId,
436
+ role: "assistant",
437
+ kind: "tool",
438
+ text: toolText,
439
+ textLower: toolText.toLowerCase(),
440
+ toolName,
441
+ timeCreated,
442
+ });
443
+ partIdx++;
444
+ }
445
+ }
446
+ }
447
+ } else if (type === "shell") {
448
+ const cmd = typeof parsed.command === "string" ? parsed.command.trim() : "";
449
+ const outObj = parsed.output as Record<string, unknown> | undefined;
450
+ const outText = outObj && typeof outObj.output === "string" ? outObj.output.trim() : "";
451
+ const combined = capIndexedText([cmd ? `$ ${cmd}` : "", outText].filter(Boolean).join("\n"));
452
+ if (combined) {
453
+ toolsSet.add("shell");
454
+ session.entries.push({
455
+ id: msgId,
456
+ messageId: msgId,
457
+ role: "shell",
458
+ kind: "tool",
459
+ text: combined,
460
+ textLower: combined.toLowerCase(),
461
+ toolName: "shell",
462
+ timeCreated,
463
+ });
464
+ }
465
+ }
466
+ }
467
+ }
468
+ }
469
+
470
+ // 2. OpenCode V1 Fallback Schema (session + message + part)
471
+ if (tables.has("session") && tables.has("message") && tables.has("part")) {
472
+ const v1Sessions = db.all(`
473
+ SELECT id, title, directory, time_created, time_updated
474
+ FROM session
475
+ ORDER BY time_updated DESC
476
+ LIMIT 500
477
+ `);
478
+
479
+ for (const row of v1Sessions) {
480
+ const id = String(row.id || "");
481
+ if (!id || sessionsMap.has(id)) continue;
482
+ const title = String(row.title || "(untitled session)");
483
+ const directory = String(row.directory || "");
484
+ sessionsMap.set(id, {
485
+ id,
486
+ title,
487
+ titleLower: title.toLowerCase(),
488
+ directory,
489
+ directoryNorm: normalizePath(directory),
490
+ timeCreated: Number(row.time_created || 0),
491
+ timeUpdated: Number(row.time_updated || 0),
492
+ tools: [],
493
+ files: [],
494
+ entries: [],
495
+ });
496
+ sessionFiles.set(id, new Set());
497
+ sessionTools.set(id, new Set());
498
+ }
499
+
500
+ let v1Parts: Record<string, unknown>[];
501
+ try {
502
+ v1Parts = db.all(`
503
+ SELECT id, message_id, session_id, time_created, part_data, role
504
+ FROM (
505
+ SELECT p.id, p.message_id, p.session_id, p.time_created, p.data AS part_data,
506
+ json_extract(m.data, '$.role') AS role,
507
+ ROW_NUMBER() OVER (PARTITION BY p.session_id ORDER BY p.time_created DESC) AS rn
508
+ FROM part p
509
+ JOIN message m ON m.id = p.message_id
510
+ )
511
+ WHERE rn <= 250
512
+ ORDER BY time_created DESC
513
+ LIMIT 25000
514
+ `);
515
+ } catch {
516
+ v1Parts = db.all(`
517
+ SELECT p.id, p.message_id, p.session_id, p.time_created, p.data AS part_data,
518
+ json_extract(m.data, '$.role') AS role
519
+ FROM part p
520
+ JOIN message m ON m.id = p.message_id
521
+ ORDER BY p.time_created DESC
522
+ LIMIT 12000
523
+ `);
524
+ }
525
+
526
+ for (const row of v1Parts) {
527
+ const sessionId = String(row.session_id || "");
528
+ const session = sessionsMap.get(sessionId);
529
+ if (!session || session.entries.length > 0 && tables.has("session_v2")) continue;
530
+ const role = row.role === "user" ? "user" : "assistant";
531
+ const timeCreated = Number(row.time_created || session.timeUpdated);
532
+ let part: Record<string, unknown>;
533
+ try {
534
+ part = JSON.parse(String(row.part_data || ""));
535
+ } catch {
536
+ continue;
537
+ }
538
+ const filesSet = sessionFiles.get(sessionId)!;
539
+ const toolsSet = sessionTools.get(sessionId)!;
540
+ const v1MsgId = String(row.message_id || row.id || "");
541
+
542
+ if (part.type === "text" && typeof part.text === "string" && part.text.trim()) {
543
+ const text = part.text.trim();
544
+ session.entries.push({
545
+ id: String(row.id || ""),
546
+ messageId: v1MsgId,
547
+ role,
548
+ kind: "message",
549
+ text,
550
+ textLower: text.toLowerCase(),
551
+ timeCreated,
552
+ });
553
+ } else if (part.type === "tool" && typeof part.tool === "string") {
554
+ const toolName = part.tool;
555
+ toolsSet.add(toolName);
556
+ const state = (part.state as Record<string, unknown>) || {};
557
+ const localFiles = new Set<string>();
558
+ extractFilePathsFromInput(state.input, localFiles);
559
+ for (const fp of localFiles) {
560
+ filesSet.add(fp);
561
+ }
562
+ const summary = stringifyToolInput(state.input);
563
+ if (summary) {
564
+ const toolText = `[${toolName}] ${summary}`;
565
+ session.entries.push({
566
+ id: String(row.id || ""),
567
+ messageId: v1MsgId,
568
+ role,
569
+ kind: "tool",
570
+ text: toolText,
571
+ textLower: toolText.toLowerCase(),
572
+ toolName,
573
+ timeCreated,
574
+ });
575
+ }
576
+ } else if (part.type === "patch" && Array.isArray(part.files)) {
577
+ for (const fp of part.files) {
578
+ if (typeof fp === "string" && fp.trim()) {
579
+ filesSet.add(fp.trim());
580
+ session.entries.push({
581
+ id: `${row.id}:patch:${fp}`,
582
+ messageId: v1MsgId,
583
+ role,
584
+ kind: "filepath",
585
+ text: fp.trim(),
586
+ textLower: fp.trim().toLowerCase(),
587
+ filePath: fp.trim(),
588
+ timeCreated,
589
+ });
590
+ }
591
+ }
592
+ }
593
+ }
594
+ }
595
+ } finally {
596
+ db.close();
597
+ }
598
+
599
+ for (const [id, session] of sessionsMap) {
600
+ session.files = [...(sessionFiles.get(id) || [])];
601
+ session.tools = [...(sessionTools.get(id) || [])];
602
+ }
603
+
604
+ const sessions = [...sessionsMap.values()].sort((a, b) => b.timeUpdated - a.timeUpdated);
605
+ cachedIndex = {
606
+ dbPath,
607
+ loadedAt: Date.now(),
608
+ sessions,
609
+ };
610
+ scheduleIndexIdleEviction();
611
+ return cachedIndex;
612
+ }
613
+
614
+ function parseDateToken(value: string): { start?: number; end?: number; label: string } | undefined {
615
+ const v = value.trim().toLowerCase();
616
+ const now = new Date();
617
+ const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
618
+ const dayMs = 86_400_000;
619
+
620
+ if (v === "today" || v === "1d") {
621
+ return { start: startOfToday, label: "today" };
622
+ }
623
+ if (v === "yesterday") {
624
+ return { start: startOfToday - dayMs, end: startOfToday, label: "yesterday" };
625
+ }
626
+ const daysMatch = v.match(/^(\d+)d$/);
627
+ if (daysMatch) {
628
+ const days = Number(daysMatch[1]);
629
+ return { start: Date.now() - days * dayMs, label: `${days}d` };
630
+ }
631
+ const weeksMatch = v.match(/^(\d+)w$/);
632
+ if (weeksMatch) {
633
+ const weeks = Number(weeksMatch[1]);
634
+ return { start: Date.now() - weeks * 7 * dayMs, label: `${weeks}w` };
635
+ }
636
+ if (v.includes("..")) {
637
+ const [fromStr, toStr] = v.split("..");
638
+ const start = fromStr ? Date.parse(fromStr) : undefined;
639
+ const endRaw = toStr ? Date.parse(toStr) : undefined;
640
+ const end = endRaw && !Number.isNaN(endRaw) ? endRaw + dayMs : undefined;
641
+ if ((start && !Number.isNaN(start)) || end) {
642
+ return {
643
+ start: start && !Number.isNaN(start) ? start : undefined,
644
+ end,
645
+ label: v,
646
+ };
647
+ }
648
+ }
649
+ if (/^\d{4}-\d{2}$/.test(v)) {
650
+ const [year, month] = v.split("-").map(Number);
651
+ const start = new Date(year, month - 1, 1).getTime();
652
+ const end = new Date(year, month, 1).getTime();
653
+ return { start, end, label: v };
654
+ }
655
+ if (/^\d{4}-\d{2}-\d{2}$/.test(v)) {
656
+ const start = Date.parse(v);
657
+ if (!Number.isNaN(start)) {
658
+ return { start, end: start + dayMs, label: v };
659
+ }
660
+ }
661
+ return undefined;
662
+ }
663
+
664
+ function isSafeRegexPattern(pattern: string): boolean {
665
+ if (!pattern || pattern.length > 256) return false;
666
+ // Reject nested quantifiers such as (a+)+, (.*)+, (a|aa)+ that cause catastrophic backtracking
667
+ if (/(\([^)]*[+*][^)]*\))[+*?]/.test(pattern)) return false;
668
+ if (/(\.\*){3,}/.test(pattern)) return false;
669
+ return true;
670
+ }
671
+
672
+ function compileSafeRegex(pattern: string, flags = "i"): { regex?: RegExp; error?: string } {
673
+ if (!isSafeRegexPattern(pattern)) {
674
+ return { error: "Unsafe or overly complex regex pattern" };
675
+ }
676
+ try {
677
+ return { regex: new RegExp(pattern, flags) };
678
+ } catch (err) {
679
+ return { error: err instanceof Error ? err.message : "Invalid regex" };
680
+ }
681
+ }
682
+
683
+ export function parseSearchQuery(rawQuery: string, options: SearchOptions = {}): ParsedQuery {
684
+ const raw = rawQuery.trim();
685
+ const terms: string[] = [];
686
+ const fuzzyTerms: string[] = [];
687
+ let role: RoleFilter = options.role ?? "all";
688
+ let scope: ScopeFilter | undefined = options.scope;
689
+ let fileFilter: string | undefined;
690
+ let toolFilter: string | undefined;
691
+ let inFilter: "title" | "message" | "tool" | "file" | undefined;
692
+ let dateRange =
693
+ options.datePreset && options.datePreset !== "all"
694
+ ? parseDateToken(options.datePreset)
695
+ : undefined;
696
+ let effectiveMode: SearchMode = options.mode ?? "smart";
697
+ let regex: RegExp | undefined;
698
+ let regexError: string | undefined;
699
+
700
+ // Check if entire query is a /regex/flags literal
701
+ const slashRegex = raw.match(/^\/(.+)\/([gimsuy]*)$/);
702
+ if (slashRegex) {
703
+ effectiveMode = "regex";
704
+ const compiled = compileSafeRegex(slashRegex[1], slashRegex[2] || "i");
705
+ regex = compiled.regex;
706
+ regexError = compiled.error;
707
+ return {
708
+ raw,
709
+ terms,
710
+ fuzzyTerms,
711
+ regex,
712
+ regexError,
713
+ role,
714
+ scope,
715
+ fileFilter,
716
+ toolFilter,
717
+ inFilter,
718
+ dateRange,
719
+ effectiveMode,
720
+ };
721
+ }
722
+
723
+ // Tokenize respecting /regex/flags, key:"quoted value", "quoted phrase", or words
724
+ const tokenPattern = /(\/[^/]+\/[gimsuy]*|[a-zA-Z]+:"[^"]+"|"[^"]+"|\S+)/g;
725
+ const rawTokens = raw.match(tokenPattern) || [];
726
+
727
+ for (const token of rawTokens) {
728
+ const tokenSlashRegex = token.match(/^\/(.+)\/([gimsuy]*)$/);
729
+ if (tokenSlashRegex) {
730
+ effectiveMode = "regex";
731
+ const compiled = compileSafeRegex(tokenSlashRegex[1], tokenSlashRegex[2] || "i");
732
+ regex = compiled.regex;
733
+ regexError = compiled.error;
734
+ continue;
735
+ }
736
+ const lower = token.toLowerCase();
737
+ if (lower.startsWith("role:") || lower.startsWith("from:")) {
738
+ const val = lower.slice(lower.indexOf(":") + 1).replace(/^"|"$/g, "");
739
+ if (val === "user" || val === "me" || val === "human") role = "user";
740
+ else if (val === "assistant" || val === "ai" || val === "bot" || val === "model") role = "assistant";
741
+ else if (val === "shell" || val === "cmd") role = "shell";
742
+ else if (val === "all" || val === "any") role = "all";
743
+ continue;
744
+ }
745
+ if (lower.startsWith("scope:")) {
746
+ const val = lower.slice(6).replace(/^"|"$/g, "");
747
+ if (val === "project" || val === "workspace" || val === "local" || val === "here") scope = "project";
748
+ else if (val === "all" || val === "global") scope = "all";
749
+ continue;
750
+ }
751
+ if (lower.startsWith("date:") || lower.startsWith("since:") || lower.startsWith("after:")) {
752
+ const val = lower.slice(lower.indexOf(":") + 1).replace(/^"|"$/g, "");
753
+ const parsedDate = parseDateToken(val);
754
+ if (parsedDate) {
755
+ dateRange = parsedDate;
756
+ continue;
757
+ }
758
+ }
759
+ if (lower.startsWith("file:") || lower.startsWith("path:")) {
760
+ fileFilter = token.slice(token.indexOf(":") + 1).replace(/^"|"$/g, "").toLowerCase();
761
+ continue;
762
+ }
763
+ if (lower.startsWith("tool:")) {
764
+ toolFilter = token.slice(5).replace(/^"|"$/g, "").toLowerCase();
765
+ continue;
766
+ }
767
+ if (lower.startsWith("in:")) {
768
+ const val = lower.slice(3).replace(/^"|"$/g, "");
769
+ if (val === "title" || val === "message" || val === "tool" || val === "file") {
770
+ inFilter = val;
771
+ continue;
772
+ }
773
+ }
774
+ if (lower.startsWith("re:") || lower.startsWith("regex:")) {
775
+ const pattern = token.slice(token.indexOf(":") + 1).replace(/^"|"$/g, "");
776
+ effectiveMode = "regex";
777
+ const compiled = compileSafeRegex(pattern, "i");
778
+ regex = compiled.regex;
779
+ regexError = compiled.error;
780
+ continue;
781
+ }
782
+ if (token.startsWith("~") && token.length > 1) {
783
+ fuzzyTerms.push(token.slice(1).replace(/^"|"$/g, "").toLowerCase());
784
+ continue;
785
+ }
786
+
787
+ const cleaned = token.replace(/^"|"$/g, "").toLowerCase();
788
+ if (cleaned) {
789
+ terms.push(cleaned);
790
+ }
791
+ }
792
+
793
+ if (effectiveMode === "regex" && !regex && !regexError && terms.length > 0) {
794
+ const joinedPattern =
795
+ terms.length === 1
796
+ ? terms[0]
797
+ : terms.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*");
798
+ const compiled = compileSafeRegex(joinedPattern, "i");
799
+ regex = compiled.regex;
800
+ regexError = compiled.error;
801
+ }
802
+
803
+ return {
804
+ raw,
805
+ terms,
806
+ fuzzyTerms,
807
+ regex,
808
+ regexError,
809
+ role,
810
+ scope,
811
+ fileFilter,
812
+ toolFilter,
813
+ inFilter,
814
+ dateRange,
815
+ effectiveMode,
816
+ };
817
+ }
818
+
819
+ // Bounded Levenshtein distance for fast zero-dependency typo tolerance
820
+ export function editDistanceBounded(a: string, b: string, maxDist: number): number {
821
+ const lenA = a.length;
822
+ const lenB = b.length;
823
+ if (Math.abs(lenA - lenB) > maxDist) return maxDist + 1;
824
+ if (a === b) return 0;
825
+
826
+ const prev = new Array<number>(lenB + 1);
827
+ const curr = new Array<number>(lenB + 1);
828
+ for (let j = 0; j <= lenB; j++) prev[j] = j;
829
+
830
+ for (let i = 1; i <= lenA; i++) {
831
+ curr[0] = i;
832
+ let minInRow = curr[0];
833
+ const charA = a.charCodeAt(i - 1);
834
+ for (let j = 1; j <= lenB; j++) {
835
+ const cost = charA === b.charCodeAt(j - 1) ? 0 : 1;
836
+ const val = Math.min(prev[j] + 1, curr[j - 1] + 1, prev[j - 1] + cost);
837
+ curr[j] = val;
838
+ if (val < minInRow) minInRow = val;
839
+ }
840
+ if (minInRow > maxDist) return maxDist + 1;
841
+ for (let j = 0; j <= lenB; j++) prev[j] = curr[j];
842
+ }
843
+ return prev[lenB];
844
+ }
845
+
846
+ const WORD_TOKEN_RE = /[a-z0-9_.-]{3,}/g;
847
+
848
+ export function findFuzzyWordMatch(textLower: string, term: string): { matchedWord: string; distance: number } | null {
849
+ if (term.length < 3) return null;
850
+ if (textLower.includes(term)) {
851
+ return { matchedWord: term, distance: 0 };
852
+ }
853
+ const maxDist = term.length <= 5 ? 1 : 2;
854
+ const prefix2 = term.slice(0, 2);
855
+ let bestWord = "";
856
+ let bestDist = maxDist + 1;
857
+
858
+ WORD_TOKEN_RE.lastIndex = 0;
859
+ let match: RegExpExecArray | null;
860
+ while ((match = WORD_TOKEN_RE.exec(textLower)) !== null) {
861
+ const w = match[0];
862
+ if (w.startsWith(prefix2) || Math.abs(w.length - term.length) <= maxDist) {
863
+ const dist = editDistanceBounded(term, w, maxDist);
864
+ if (dist < bestDist) {
865
+ bestDist = dist;
866
+ bestWord = w;
867
+ if (dist === 1) break;
868
+ }
869
+ }
870
+ }
871
+ return bestDist <= maxDist ? { matchedWord: bestWord, distance: bestDist } : null;
872
+ }
873
+
874
+ export function buildSnippet(text: string, matchedTerm: string, maxLen = 110): string {
875
+ const clean = text.replace(/\s+/g, " ").trim();
876
+ if (!matchedTerm) return clean.slice(0, maxLen);
877
+ const lower = clean.toLowerCase();
878
+ const idx = lower.indexOf(matchedTerm.toLowerCase());
879
+ if (idx === -1) return clean.slice(0, maxLen);
880
+ const start = Math.max(0, idx - 32);
881
+ const end = Math.min(clean.length, idx + matchedTerm.length + (maxLen - 32));
882
+ return (start > 0 ? "..." : "") + clean.slice(start, end) + (end < clean.length ? "..." : "");
883
+ }
884
+
885
+ export function formatRelativeTime(ms: number): string {
886
+ if (!ms) return "";
887
+ const diff = Date.now() - ms;
888
+ const mins = Math.floor(diff / 60_000);
889
+ if (mins < 1) return "just now";
890
+ if (mins < 60) return `${mins}m ago`;
891
+ const hours = Math.floor(mins / 60);
892
+ if (hours < 24) return `${hours}h ago`;
893
+ const days = Math.floor(hours / 24);
894
+ if (days < 30) return `${days}d ago`;
895
+ return new Date(ms).toISOString().slice(0, 10);
896
+ }
897
+
898
+ export function searchSessionIndex(
899
+ index: SessionIndex,
900
+ rawQuery: string,
901
+ options: SearchOptions = {}
902
+ ): SearchSessionMatch[] {
903
+ const parsed = parseSearchQuery(rawQuery, options);
904
+ const limit = options.limit ?? 50;
905
+ const scope = parsed.scope ?? options.scope ?? "all";
906
+ const currentDirNorm = normalizePath(options.currentDirectory);
907
+
908
+ const hasSearchTerms =
909
+ parsed.terms.length > 0 ||
910
+ parsed.fuzzyTerms.length > 0 ||
911
+ Boolean(parsed.regex) ||
912
+ Boolean(parsed.fileFilter) ||
913
+ Boolean(parsed.toolFilter);
914
+
915
+ const results: SearchSessionMatch[] = [];
916
+
917
+ for (const session of index.sessions) {
918
+ // 1. Project scope filter
919
+ if (scope === "project" && currentDirNorm) {
920
+ if (
921
+ !session.directoryNorm ||
922
+ (session.directoryNorm !== currentDirNorm &&
923
+ !session.directoryNorm.startsWith(currentDirNorm + "/") &&
924
+ !currentDirNorm.startsWith(session.directoryNorm + "/"))
925
+ ) {
926
+ continue;
927
+ }
928
+ }
929
+
930
+ // 2. Date filter (session updated or entry created within range)
931
+ if (parsed.dateRange) {
932
+ const { start, end } = parsed.dateRange;
933
+ if (start && session.timeUpdated < start) continue;
934
+ if (end && session.timeCreated > end) continue;
935
+ }
936
+
937
+ // 3. File filter
938
+ if (parsed.fileFilter) {
939
+ const matchesFile = session.files.some((f) => f.toLowerCase().includes(parsed.fileFilter!));
940
+ if (!matchesFile) continue;
941
+ }
942
+
943
+ // 4. Tool filter
944
+ if (parsed.toolFilter) {
945
+ const matchesTool = session.tools.some((t) => t.toLowerCase().includes(parsed.toolFilter!));
946
+ if (!matchesTool) continue;
947
+ }
948
+
949
+ // If browsing with no search keywords (only filters or empty query)
950
+ if (!hasSearchTerms) {
951
+ const previewMatches: SearchEntryMatch[] = [];
952
+ for (const entry of session.entries) {
953
+ if (parsed.role !== "all" && entry.role !== parsed.role) continue;
954
+ if (previewMatches.length < 6 && entry.kind === "message") {
955
+ previewMatches.push({
956
+ messageId: entry.messageId ?? entry.id.split(":")[0],
957
+ role: entry.role,
958
+ kind: "message",
959
+ timeCreated: entry.timeCreated,
960
+ snippet: buildSnippet(entry.text, "", 140),
961
+ matchedTerm: "",
962
+ });
963
+ }
964
+ }
965
+ const firstEntry = previewMatches[0];
966
+ results.push({
967
+ session,
968
+ score: session.timeUpdated / 1e12,
969
+ matchBadge: session.tools.length ? `${session.entries.length} msgs` : "recent",
970
+ bestSnippet: firstEntry ? firstEntry.snippet : session.directory || "No messages",
971
+ bestRole: firstEntry ? firstEntry.role : "title",
972
+ bestMatch: firstEntry,
973
+ totalMatches: previewMatches.length,
974
+ matches: previewMatches,
975
+ });
976
+ continue;
977
+ }
978
+
979
+ // Active search matching across title and entries
980
+ const matches: SearchEntryMatch[] = [];
981
+ const matchedTermsInSession = new Set<string>();
982
+ let score = 0;
983
+ let totalMatches = 0;
984
+
985
+ // A. Check Title
986
+ if (!parsed.inFilter || parsed.inFilter === "title") {
987
+ if (parsed.effectiveMode === "regex" && parsed.regex) {
988
+ const m = session.title.match(parsed.regex);
989
+ if (m && m[0]) {
990
+ score += 120;
991
+ totalMatches++;
992
+ matches.push({
993
+ role: "title",
994
+ kind: "title",
995
+ timeCreated: session.timeUpdated,
996
+ snippet: session.title,
997
+ matchedTerm: m[0],
998
+ });
999
+ }
1000
+ } else {
1001
+ for (const term of parsed.terms) {
1002
+ if (session.titleLower.includes(term)) {
1003
+ matchedTermsInSession.add(term);
1004
+ score += 90;
1005
+ totalMatches++;
1006
+ if (matches.length < 8) {
1007
+ matches.push({
1008
+ role: "title",
1009
+ kind: "title",
1010
+ timeCreated: session.timeUpdated,
1011
+ snippet: session.title,
1012
+ matchedTerm: term,
1013
+ });
1014
+ }
1015
+ } else if (parsed.effectiveMode === "fuzzy") {
1016
+ const fm = findFuzzyWordMatch(session.titleLower, term);
1017
+ if (fm) {
1018
+ matchedTermsInSession.add(term);
1019
+ score += 55 - fm.distance * 10;
1020
+ totalMatches++;
1021
+ if (matches.length < 8) {
1022
+ matches.push({
1023
+ role: "title",
1024
+ kind: "fuzzy",
1025
+ timeCreated: session.timeUpdated,
1026
+ snippet: session.title,
1027
+ matchedTerm: fm.matchedWord,
1028
+ });
1029
+ }
1030
+ }
1031
+ }
1032
+ }
1033
+ for (const fTerm of parsed.fuzzyTerms) {
1034
+ const fm = findFuzzyWordMatch(session.titleLower, fTerm);
1035
+ if (fm) {
1036
+ matchedTermsInSession.add(`~${fTerm}`);
1037
+ score += 65 - fm.distance * 10;
1038
+ totalMatches++;
1039
+ if (matches.length < 8) {
1040
+ matches.push({
1041
+ role: "title",
1042
+ kind: "fuzzy",
1043
+ timeCreated: session.timeUpdated,
1044
+ snippet: session.title,
1045
+ matchedTerm: fm.matchedWord,
1046
+ });
1047
+ }
1048
+ }
1049
+ }
1050
+ }
1051
+ }
1052
+
1053
+ const entryPassesFilters = (entry: SearchEntry): boolean => {
1054
+ if (parsed.role !== "all" && entry.role !== parsed.role) return false;
1055
+ if (parsed.inFilter === "title") return false;
1056
+ if (parsed.inFilter === "message" && entry.kind !== "message") return false;
1057
+ if (parsed.inFilter === "tool" && entry.kind !== "tool") return false;
1058
+ if (parsed.inFilter === "file" && entry.kind !== "filepath") return false;
1059
+ if (
1060
+ parsed.toolFilter &&
1061
+ entry.kind === "tool" &&
1062
+ (!entry.toolName || !entry.toolName.toLowerCase().includes(parsed.toolFilter))
1063
+ ) {
1064
+ return false;
1065
+ }
1066
+ if (
1067
+ parsed.fileFilter &&
1068
+ entry.kind === "filepath" &&
1069
+ (!entry.filePath || !entry.filePath.toLowerCase().includes(parsed.fileFilter))
1070
+ ) {
1071
+ return false;
1072
+ }
1073
+ if (parsed.dateRange) {
1074
+ const { start, end } = parsed.dateRange;
1075
+ if (start && entry.timeCreated < start) return false;
1076
+ if (end && entry.timeCreated > end) return false;
1077
+ }
1078
+ return true;
1079
+ };
1080
+
1081
+ // B. Check Entries (messages, tool calls, file paths)
1082
+ for (const entry of session.entries) {
1083
+ if (!entryPassesFilters(entry)) continue;
1084
+
1085
+ const matchesFilterTarget =
1086
+ Boolean(
1087
+ parsed.toolFilter &&
1088
+ entry.toolName &&
1089
+ entry.toolName.toLowerCase().includes(parsed.toolFilter)
1090
+ ) ||
1091
+ Boolean(
1092
+ parsed.fileFilter &&
1093
+ ((entry.filePath && entry.filePath.toLowerCase().includes(parsed.fileFilter)) ||
1094
+ (entry.kind === "tool" && entry.textLower.includes(parsed.fileFilter)))
1095
+ );
1096
+
1097
+ if (parsed.effectiveMode === "regex" && parsed.regex) {
1098
+ const m = entry.text.match(parsed.regex);
1099
+ if (m && m[0]) {
1100
+ score += entry.kind === "message" ? 50 : 35;
1101
+ totalMatches++;
1102
+ if (matches.length < 8) {
1103
+ matches.push({
1104
+ messageId: entry.messageId ?? entry.id.split(":")[0],
1105
+ role: entry.role,
1106
+ kind: entry.kind,
1107
+ toolName: entry.toolName,
1108
+ filePath: entry.filePath,
1109
+ timeCreated: entry.timeCreated,
1110
+ snippet: buildSnippet(entry.text, m[0], 130),
1111
+ matchedTerm: m[0],
1112
+ });
1113
+ }
1114
+ }
1115
+ continue;
1116
+ }
1117
+
1118
+ // Smart / Fuzzy term checks
1119
+ let entryMatchedTerms = 0;
1120
+ let firstMatchedSubstring = "";
1121
+ let isFuzzyEntryMatch = false;
1122
+
1123
+ for (const term of parsed.terms) {
1124
+ if (entry.textLower.includes(term)) {
1125
+ matchedTermsInSession.add(term);
1126
+ entryMatchedTerms++;
1127
+ if (!firstMatchedSubstring) firstMatchedSubstring = term;
1128
+ } else if (parsed.effectiveMode === "fuzzy") {
1129
+ const fm = findFuzzyWordMatch(entry.textLower, term);
1130
+ if (fm) {
1131
+ matchedTermsInSession.add(term);
1132
+ entryMatchedTerms++;
1133
+ isFuzzyEntryMatch = true;
1134
+ if (!firstMatchedSubstring) firstMatchedSubstring = fm.matchedWord;
1135
+ }
1136
+ }
1137
+ }
1138
+
1139
+ for (const fTerm of parsed.fuzzyTerms) {
1140
+ const fm = findFuzzyWordMatch(entry.textLower, fTerm);
1141
+ if (fm) {
1142
+ matchedTermsInSession.add(`~${fTerm}`);
1143
+ entryMatchedTerms++;
1144
+ isFuzzyEntryMatch = true;
1145
+ if (!firstMatchedSubstring) firstMatchedSubstring = fm.matchedWord;
1146
+ }
1147
+ }
1148
+
1149
+ // Filter-only match (e.g. file:tui.ts or tool:edit with no extra keywords)
1150
+ if (
1151
+ parsed.terms.length === 0 &&
1152
+ parsed.fuzzyTerms.length === 0 &&
1153
+ matchesFilterTarget
1154
+ ) {
1155
+ entryMatchedTerms = 1;
1156
+ firstMatchedSubstring = parsed.fileFilter || parsed.toolFilter || "";
1157
+ }
1158
+
1159
+ if (entryMatchedTerms > 0) {
1160
+ totalMatches++;
1161
+ // Bonus when a single message contains ALL query terms or directly matches active file:/tool: filter
1162
+ const allInSingleEntry = parsed.terms.length > 1 && entryMatchedTerms >= parsed.terms.length;
1163
+ const filterBonus = matchesFilterTarget ? 25 : 0;
1164
+ score +=
1165
+ (isFuzzyEntryMatch ? 20 : 35) * entryMatchedTerms +
1166
+ (allInSingleEntry ? 45 : 0) +
1167
+ filterBonus;
1168
+
1169
+ if (matches.length < 8) {
1170
+ matches.push({
1171
+ messageId: entry.messageId ?? entry.id.split(":")[0],
1172
+ role: entry.role,
1173
+ kind: isFuzzyEntryMatch ? "fuzzy" : entry.kind,
1174
+ toolName: entry.toolName,
1175
+ filePath: entry.filePath,
1176
+ timeCreated: entry.timeCreated,
1177
+ snippet: buildSnippet(entry.text, firstMatchedSubstring, 130),
1178
+ matchedTerm: firstMatchedSubstring,
1179
+ });
1180
+ }
1181
+ }
1182
+ }
1183
+
1184
+ // Verify Session-Level Multi-Term AND requirement:
1185
+ // Every required term in parsed.terms and parsed.fuzzyTerms must appear somewhere in the session
1186
+ const requiredCount = parsed.terms.length + parsed.fuzzyTerms.length;
1187
+ if (parsed.effectiveMode !== "regex" && requiredCount > 0) {
1188
+ if (matchedTermsInSession.size < requiredCount) {
1189
+ // Automatic Fuzzy Fallback in "smart" mode if exact terms didn't all match!
1190
+ if (parsed.effectiveMode === "smart" && parsed.terms.length > 0) {
1191
+ for (const term of parsed.terms) {
1192
+ if (matchedTermsInSession.has(term)) continue;
1193
+ if (!parsed.inFilter || parsed.inFilter === "title") {
1194
+ const titleFuzzy = findFuzzyWordMatch(session.titleLower, term);
1195
+ if (titleFuzzy) {
1196
+ matchedTermsInSession.add(term);
1197
+ score += 30;
1198
+ totalMatches++;
1199
+ if (matches.length < 8) {
1200
+ matches.push({
1201
+ role: "title",
1202
+ kind: "fuzzy",
1203
+ timeCreated: session.timeUpdated,
1204
+ snippet: session.title,
1205
+ matchedTerm: titleFuzzy.matchedWord,
1206
+ });
1207
+ }
1208
+ continue;
1209
+ }
1210
+ }
1211
+ for (const entry of session.entries) {
1212
+ if (!entryPassesFilters(entry)) continue;
1213
+ const fm = findFuzzyWordMatch(entry.textLower, term);
1214
+ if (fm) {
1215
+ matchedTermsInSession.add(term);
1216
+ score += 22;
1217
+ totalMatches++;
1218
+ if (matches.length < 8) {
1219
+ matches.push({
1220
+ messageId: entry.messageId ?? entry.id.split(":")[0],
1221
+ role: entry.role,
1222
+ kind: "fuzzy",
1223
+ toolName: entry.toolName,
1224
+ filePath: entry.filePath,
1225
+ timeCreated: entry.timeCreated,
1226
+ snippet: buildSnippet(entry.text, fm.matchedWord, 130),
1227
+ matchedTerm: fm.matchedWord,
1228
+ });
1229
+ }
1230
+ break;
1231
+ }
1232
+ }
1233
+ }
1234
+ }
1235
+ }
1236
+
1237
+ if (matchedTermsInSession.size < requiredCount) {
1238
+ continue;
1239
+ }
1240
+ }
1241
+
1242
+ if (totalMatches === 0) continue;
1243
+
1244
+ // Slight recency boost so recent relevant sessions surface above stale ones
1245
+ const recencyDays = Math.max(0, (Date.now() - session.timeUpdated) / 86_400_000);
1246
+ const recencyBoost = Math.max(0, 15 - recencyDays * 0.4);
1247
+ score += recencyBoost;
1248
+
1249
+ const primary = matches[0];
1250
+ const badge =
1251
+ primary.kind === "title"
1252
+ ? "title"
1253
+ : primary.kind === "fuzzy"
1254
+ ? "fuzzy"
1255
+ : primary.kind === "filepath"
1256
+ ? "file"
1257
+ : primary.kind === "tool"
1258
+ ? `tool:${primary.toolName || "call"}`
1259
+ : primary.role === "user"
1260
+ ? "user"
1261
+ : "ai";
1262
+
1263
+ results.push({
1264
+ session,
1265
+ score,
1266
+ matchBadge: badge,
1267
+ bestSnippet: primary.snippet,
1268
+ bestRole: primary.role,
1269
+ bestMatch: primary,
1270
+ totalMatches,
1271
+ matches,
1272
+ });
1273
+ }
1274
+
1275
+ results.sort((a, b) => b.score - a.score || b.session.timeUpdated - a.session.timeUpdated);
1276
+ return results.slice(0, limit);
1277
+ }