antigravity-plugin-context-mode 1.3.2 → 1.5.0
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/CHANGELOG.md +29 -0
- package/README.md +4 -4
- package/hooks/pre-invocation.js +84 -0
- package/hooks/pre-tool-use.js +129 -24
- package/hooks/session-start.js +75 -7
- package/hooks.json +7 -0
- package/lib/store.js +415 -66
- package/lib/tools.js +83 -68
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/rules/AGENTS.md +18 -5
- package/server.js +11 -17
- package/skills/context-mode/SKILL.md +27 -18
package/lib/store.js
CHANGED
|
@@ -54,6 +54,55 @@ export const DEFAULT_EXEMPT_EXTENSIONS = Array.from(
|
|
|
54
54
|
)
|
|
55
55
|
);
|
|
56
56
|
|
|
57
|
+
/** Format active categories and custom extensions into a human-readable summary */
|
|
58
|
+
export function formatExemptSummary({ activeCats = [], customs = [] } = {}) {
|
|
59
|
+
const categoryLabels = {
|
|
60
|
+
images: "Images (.png, .jpg, etc.)",
|
|
61
|
+
documents: "Documents & Office (.pdf, .docx, etc.)",
|
|
62
|
+
code: "Source Code (.js, .py, etc.)",
|
|
63
|
+
models3d: "3D Models (.blend, .obj, etc.)",
|
|
64
|
+
music: "Audio (.mp3, .wav, etc.)",
|
|
65
|
+
video: "Video (.mp4, .mkv, etc.)",
|
|
66
|
+
archives: "Archives & Binaries (.zip, .tar, etc.)"
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const labels = activeCats
|
|
70
|
+
.map((c) => categoryLabels[c] || (FILE_CATEGORIES[c] ? `${FILE_CATEGORIES[c].name}` : c))
|
|
71
|
+
.filter(Boolean);
|
|
72
|
+
|
|
73
|
+
const parts = [];
|
|
74
|
+
if (labels.length > 0) {
|
|
75
|
+
parts.push(labels.join(", "));
|
|
76
|
+
} else {
|
|
77
|
+
parts.push("No categories");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (customs && customs.length > 0) {
|
|
81
|
+
parts.push(`Custom (${customs.join(", ")})`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
parts.push("prompt attachments in .user_uploaded/");
|
|
85
|
+
return parts.join(", ");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Default max file size (50 MB) automatically indexed on cutoff block */
|
|
89
|
+
export const DEFAULT_MAX_AUTO_INDEX_BYTES = 50 * 1024 * 1024; // 52,428,800 bytes
|
|
90
|
+
|
|
91
|
+
/** Resolve intuitive file path: workspace-relative when inside project, normalized absolute when outside */
|
|
92
|
+
export function resolveFilePath(filePath, cwd = process.cwd()) {
|
|
93
|
+
if (!filePath) return null;
|
|
94
|
+
const absPath = path.isAbsolute(filePath) ? path.normalize(filePath) : path.resolve(cwd, filePath);
|
|
95
|
+
const normCwd = path.normalize(cwd);
|
|
96
|
+
|
|
97
|
+
const rel = path.relative(normCwd, absPath);
|
|
98
|
+
if (!rel.startsWith("..") && !path.isAbsolute(rel)) {
|
|
99
|
+
return rel.replace(/\\/g, "/");
|
|
100
|
+
}
|
|
101
|
+
return absPath.replace(/\\/g, "/");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const resolveFileSourceId = resolveFilePath;
|
|
105
|
+
|
|
57
106
|
/** Default path to context database (~/.context-mode/context.db) */
|
|
58
107
|
export function getDefaultDbPath() {
|
|
59
108
|
if (process.env.CONTEXT_MODE_DB_PATH && process.env.CONTEXT_MODE_DB_PATH.trim()) {
|
|
@@ -90,7 +139,6 @@ export class ContextModeStore {
|
|
|
90
139
|
this.dbPath = getDefaultDbPath();
|
|
91
140
|
}
|
|
92
141
|
this.db = null;
|
|
93
|
-
this.sourceCounter = 0;
|
|
94
142
|
}
|
|
95
143
|
|
|
96
144
|
/** Open or initialize SQLite database */
|
|
@@ -135,7 +183,7 @@ export class ContextModeStore {
|
|
|
135
183
|
);
|
|
136
184
|
|
|
137
185
|
CREATE TABLE IF NOT EXISTS sources (
|
|
138
|
-
|
|
186
|
+
path TEXT PRIMARY KEY,
|
|
139
187
|
session_id TEXT NOT NULL,
|
|
140
188
|
worktree TEXT NOT NULL,
|
|
141
189
|
agent TEXT NOT NULL,
|
|
@@ -145,7 +193,8 @@ export class ContextModeStore {
|
|
|
145
193
|
chunk_count INTEGER NOT NULL DEFAULT 0,
|
|
146
194
|
created_ts INTEGER NOT NULL,
|
|
147
195
|
created_at TEXT NOT NULL,
|
|
148
|
-
accessed_ts INTEGER NOT NULL
|
|
196
|
+
accessed_ts INTEGER NOT NULL,
|
|
197
|
+
mtime INTEGER DEFAULT 0
|
|
149
198
|
);
|
|
150
199
|
|
|
151
200
|
CREATE INDEX IF NOT EXISTS idx_sources_session ON sources(session_id);
|
|
@@ -178,7 +227,7 @@ export class ContextModeStore {
|
|
|
178
227
|
// Virtual FTS5 tables
|
|
179
228
|
this.db.exec(`
|
|
180
229
|
CREATE VIRTUAL TABLE IF NOT EXISTS chunks USING fts5(
|
|
181
|
-
|
|
230
|
+
path UNINDEXED,
|
|
182
231
|
session_id UNINDEXED,
|
|
183
232
|
worktree UNINDEXED,
|
|
184
233
|
agent UNINDEXED,
|
|
@@ -189,7 +238,7 @@ export class ContextModeStore {
|
|
|
189
238
|
);
|
|
190
239
|
|
|
191
240
|
CREATE VIRTUAL TABLE IF NOT EXISTS chunks_tri USING fts5(
|
|
192
|
-
|
|
241
|
+
path UNINDEXED,
|
|
193
242
|
session_id UNINDEXED,
|
|
194
243
|
ord UNINDEXED,
|
|
195
244
|
body,
|
|
@@ -197,9 +246,47 @@ export class ContextModeStore {
|
|
|
197
246
|
);
|
|
198
247
|
`);
|
|
199
248
|
|
|
249
|
+
// Schema migration check: upgrade existing v2 databases to v3
|
|
250
|
+
try {
|
|
251
|
+
const sourceCols = this.db.prepare("PRAGMA table_info(sources)").all().map((c) => c.name);
|
|
252
|
+
if (sourceCols.includes("id") && !sourceCols.includes("path")) {
|
|
253
|
+
this.db.exec("ALTER TABLE sources RENAME COLUMN id TO path;");
|
|
254
|
+
this.db.exec("DROP TABLE IF EXISTS chunks;");
|
|
255
|
+
this.db.exec("DROP TABLE IF EXISTS chunks_tri;");
|
|
256
|
+
this.db.exec(`
|
|
257
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS chunks USING fts5(
|
|
258
|
+
path UNINDEXED,
|
|
259
|
+
session_id UNINDEXED,
|
|
260
|
+
worktree UNINDEXED,
|
|
261
|
+
agent UNINDEXED,
|
|
262
|
+
ord UNINDEXED,
|
|
263
|
+
body,
|
|
264
|
+
title,
|
|
265
|
+
tokenize = 'porter unicode61 remove_diacritics 2'
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS chunks_tri USING fts5(
|
|
269
|
+
path UNINDEXED,
|
|
270
|
+
session_id UNINDEXED,
|
|
271
|
+
ord UNINDEXED,
|
|
272
|
+
body,
|
|
273
|
+
tokenize = 'trigram'
|
|
274
|
+
);
|
|
275
|
+
`);
|
|
276
|
+
this.db.exec("DELETE FROM sources;");
|
|
277
|
+
}
|
|
278
|
+
if (!sourceCols.includes("mtime")) {
|
|
279
|
+
this.db.exec("ALTER TABLE sources ADD COLUMN mtime INTEGER DEFAULT 0;");
|
|
280
|
+
}
|
|
281
|
+
} catch {
|
|
282
|
+
// Best-effort migration
|
|
283
|
+
}
|
|
284
|
+
|
|
200
285
|
const versionRow = this.db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
|
|
201
286
|
if (!versionRow) {
|
|
202
|
-
this.db.prepare("INSERT INTO meta(key, value) VALUES ('schema_version', '
|
|
287
|
+
this.db.prepare("INSERT INTO meta(key, value) VALUES ('schema_version', '3')").run();
|
|
288
|
+
} else {
|
|
289
|
+
this.db.prepare("UPDATE meta SET value = '3' WHERE key = 'schema_version'").run();
|
|
203
290
|
}
|
|
204
291
|
}
|
|
205
292
|
|
|
@@ -233,17 +320,27 @@ export class ContextModeStore {
|
|
|
233
320
|
}
|
|
234
321
|
|
|
235
322
|
/** Index a text, file output, or tool response into SQLite FTS5 */
|
|
236
|
-
indexContent({
|
|
323
|
+
indexContent({
|
|
324
|
+
sessionId = null,
|
|
325
|
+
worktree = null,
|
|
326
|
+
agent = "antigravity",
|
|
327
|
+
kind = "content",
|
|
328
|
+
label = null,
|
|
329
|
+
content,
|
|
330
|
+
path: filePath = null,
|
|
331
|
+
sourceId = null,
|
|
332
|
+
mtime = 0
|
|
333
|
+
}) {
|
|
237
334
|
this.open();
|
|
238
335
|
const sid = sessionId || "default";
|
|
239
336
|
const wt = worktree || process.cwd();
|
|
240
337
|
const ag = agent || "antigravity";
|
|
241
338
|
const k = kind || "content";
|
|
242
|
-
const lbl = label || "untitled";
|
|
243
339
|
const rawText = typeof content === "string" ? content : String(content || "");
|
|
244
340
|
const totalBytes = Buffer.byteLength(rawText, "utf8");
|
|
245
341
|
|
|
246
|
-
const
|
|
342
|
+
const targetPath = filePath || sourceId || label || "output";
|
|
343
|
+
const lbl = label || targetPath;
|
|
247
344
|
const now = Date.now();
|
|
248
345
|
const isoDate = new Date(now).toISOString();
|
|
249
346
|
|
|
@@ -252,32 +349,36 @@ export class ContextModeStore {
|
|
|
252
349
|
|
|
253
350
|
const insertSource = this.db.prepare(`
|
|
254
351
|
INSERT OR REPLACE INTO sources (
|
|
255
|
-
|
|
256
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
352
|
+
path, session_id, worktree, agent, kind, label, bytes_total, chunk_count, created_ts, created_at, accessed_ts, mtime
|
|
353
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
257
354
|
`);
|
|
258
355
|
|
|
259
356
|
const insertChunk = this.db.prepare(`
|
|
260
|
-
INSERT INTO chunks (
|
|
357
|
+
INSERT INTO chunks (path, session_id, worktree, agent, ord, body, title)
|
|
261
358
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
262
359
|
`);
|
|
263
360
|
|
|
264
361
|
const insertChunkTri = this.db.prepare(`
|
|
265
|
-
INSERT INTO chunks_tri (
|
|
362
|
+
INSERT INTO chunks_tri (path, session_id, ord, body)
|
|
266
363
|
VALUES (?, ?, ?, ?)
|
|
267
364
|
`);
|
|
268
365
|
|
|
269
|
-
insertSource.run(
|
|
366
|
+
insertSource.run(targetPath, sid, wt, ag, k, lbl, totalBytes, chunkCount, now, isoDate, now, mtime || 0);
|
|
367
|
+
|
|
368
|
+
// Clean up any existing chunks for this path to ensure idempotent re-indexing
|
|
369
|
+
this.db.prepare("DELETE FROM chunks WHERE path = ?").run(targetPath);
|
|
370
|
+
this.db.prepare("DELETE FROM chunks_tri WHERE path = ?").run(targetPath);
|
|
270
371
|
|
|
271
372
|
for (let ord = 0; ord < chunkCount; ord++) {
|
|
272
373
|
const chunkText = chunks[ord];
|
|
273
|
-
insertChunk.run(
|
|
274
|
-
insertChunkTri.run(
|
|
374
|
+
insertChunk.run(targetPath, sid, wt, ag, ord, chunkText, lbl);
|
|
375
|
+
insertChunkTri.run(targetPath, sid, ord, chunkText);
|
|
275
376
|
}
|
|
276
377
|
|
|
277
378
|
this.pruneStaleSources();
|
|
278
379
|
|
|
279
380
|
return {
|
|
280
|
-
|
|
381
|
+
path: targetPath,
|
|
281
382
|
bytesTotal: totalBytes,
|
|
282
383
|
chunkCount,
|
|
283
384
|
label: lbl,
|
|
@@ -285,13 +386,147 @@ export class ContextModeStore {
|
|
|
285
386
|
};
|
|
286
387
|
}
|
|
287
388
|
|
|
389
|
+
/**
|
|
390
|
+
* Ensures a file, directory, or URL is indexed and fresh in SQLite based on file mtime.
|
|
391
|
+
* - If URL: indexes content if not already present.
|
|
392
|
+
* - If File: checks mtime & size; if unindexed or modified, re-indexes Just-In-Time.
|
|
393
|
+
* - If Directory: scans non-exempt files, indexes any unindexed or modified files.
|
|
394
|
+
*/
|
|
395
|
+
ensurePathIndexed(targetPath, { cwd = process.cwd(), sessionId = null, agent = "antigravity" } = {}) {
|
|
396
|
+
if (!targetPath || typeof targetPath !== "string") return null;
|
|
397
|
+
this.open();
|
|
398
|
+
|
|
399
|
+
// 1. Remote URL
|
|
400
|
+
if (/^https?:\/\//i.test(targetPath)) {
|
|
401
|
+
const existing = this.db.prepare("SELECT path FROM sources WHERE path = ?").get(targetPath);
|
|
402
|
+
return { path: targetPath, isDir: false, isUrl: true, exists: Boolean(existing) };
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// 2. Local File or Directory
|
|
406
|
+
const normCwd = cwd || process.cwd();
|
|
407
|
+
const absPath = path.isAbsolute(targetPath) ? path.normalize(targetPath) : path.resolve(normCwd, targetPath);
|
|
408
|
+
if (!fs.existsSync(absPath)) {
|
|
409
|
+
return { path: targetPath, isDir: false, exists: false };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
let stat;
|
|
413
|
+
try {
|
|
414
|
+
stat = fs.statSync(absPath);
|
|
415
|
+
} catch {
|
|
416
|
+
return { path: targetPath, isDir: false, exists: false };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// 2a. Single File
|
|
420
|
+
if (stat.isFile()) {
|
|
421
|
+
const canonicalPath = this.resolveFilePath(absPath, normCwd);
|
|
422
|
+
const currentMtime = Math.round(stat.mtimeMs);
|
|
423
|
+
|
|
424
|
+
const existing = this.db.prepare("SELECT path, mtime, bytes_total FROM sources WHERE path = ?").get(canonicalPath);
|
|
425
|
+
if (existing && existing.mtime === currentMtime && existing.bytes_total === stat.size) {
|
|
426
|
+
return { path: canonicalPath, isDir: false, exists: true, wasReindexed: false };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
try {
|
|
430
|
+
const content = fs.readFileSync(absPath, "utf8");
|
|
431
|
+
this.indexContent({
|
|
432
|
+
sessionId,
|
|
433
|
+
worktree: normCwd,
|
|
434
|
+
agent,
|
|
435
|
+
kind: "file",
|
|
436
|
+
label: canonicalPath,
|
|
437
|
+
path: canonicalPath,
|
|
438
|
+
content,
|
|
439
|
+
mtime: currentMtime
|
|
440
|
+
});
|
|
441
|
+
return { path: canonicalPath, isDir: false, exists: true, wasReindexed: true };
|
|
442
|
+
} catch {
|
|
443
|
+
return { path: canonicalPath, isDir: false, exists: true, wasReindexed: false };
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// 2b. Directory
|
|
448
|
+
if (stat.isDirectory()) {
|
|
449
|
+
let canonicalDir = this.resolveFilePath(absPath, normCwd);
|
|
450
|
+
if (!canonicalDir.endsWith("/")) canonicalDir += "/";
|
|
451
|
+
|
|
452
|
+
const maxBytes = this.getMaxAutoIndexBytes(sessionId);
|
|
453
|
+
const exemptExts = this.getExemptExtensions(sessionId);
|
|
454
|
+
const files = [];
|
|
455
|
+
|
|
456
|
+
const scan = (dir, depth = 0) => {
|
|
457
|
+
if (depth > 5) return;
|
|
458
|
+
let entries = [];
|
|
459
|
+
try {
|
|
460
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
461
|
+
} catch {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
for (const ent of entries) {
|
|
465
|
+
if (ent.name.startsWith(".") || ent.name === "node_modules") continue;
|
|
466
|
+
const full = path.join(dir, ent.name);
|
|
467
|
+
if (ent.isDirectory()) {
|
|
468
|
+
scan(full, depth + 1);
|
|
469
|
+
} else if (ent.isFile()) {
|
|
470
|
+
const ext = path.extname(ent.name).toLowerCase();
|
|
471
|
+
if (exemptExts.includes(ext)) continue;
|
|
472
|
+
files.push(full);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
scan(absPath);
|
|
478
|
+
|
|
479
|
+
for (const f of files) {
|
|
480
|
+
try {
|
|
481
|
+
const fStat = fs.statSync(f);
|
|
482
|
+
if (maxBytes > 0 && fStat.size > maxBytes) continue;
|
|
483
|
+
const fCanonical = this.resolveFilePath(f, normCwd);
|
|
484
|
+
const fMtime = Math.round(fStat.mtimeMs);
|
|
485
|
+
const exist = this.db.prepare("SELECT path, mtime, bytes_total FROM sources WHERE path = ?").get(fCanonical);
|
|
486
|
+
if (!exist || exist.mtime !== fMtime || exist.bytes_total !== fStat.size) {
|
|
487
|
+
const content = fs.readFileSync(f, "utf8");
|
|
488
|
+
this.indexContent({
|
|
489
|
+
sessionId,
|
|
490
|
+
worktree: normCwd,
|
|
491
|
+
agent,
|
|
492
|
+
kind: "file",
|
|
493
|
+
label: fCanonical,
|
|
494
|
+
path: fCanonical,
|
|
495
|
+
content,
|
|
496
|
+
mtime: fMtime
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
} catch {}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
return { path: canonicalDir, isDir: true, exists: true };
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
return { path: targetPath, isDir: false, exists: false };
|
|
506
|
+
}
|
|
507
|
+
|
|
288
508
|
/** Search indexed chunks using BM25 + Trigram Reciprocal Rank Fusion */
|
|
289
|
-
search({ queries, sourceId, sessionId, worktree, scope = "session", limit = 5, offset = 0 }) {
|
|
509
|
+
search({ queries, path: searchPath, sourceId, sessionId, worktree, scope = "session", limit = 5, offset = 0, reverse = false }) {
|
|
290
510
|
this.open();
|
|
291
511
|
const queryList = Array.isArray(queries) ? queries : [queries];
|
|
292
512
|
const maxHits = Math.min(Math.max(1, limit || 5), 20);
|
|
293
513
|
const skipHits = Math.max(0, offset || 0);
|
|
294
514
|
|
|
515
|
+
const target = searchPath || sourceId || null;
|
|
516
|
+
let pathFilter = null;
|
|
517
|
+
let isDir = false;
|
|
518
|
+
|
|
519
|
+
if (target) {
|
|
520
|
+
const indexed = this.ensurePathIndexed(target, { cwd: worktree, sessionId });
|
|
521
|
+
if (indexed) {
|
|
522
|
+
pathFilter = indexed.path;
|
|
523
|
+
isDir = Boolean(indexed.isDir);
|
|
524
|
+
} else {
|
|
525
|
+
pathFilter = target;
|
|
526
|
+
isDir = target.endsWith("/");
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
295
530
|
const scores = new Map();
|
|
296
531
|
|
|
297
532
|
for (const q of queryList) {
|
|
@@ -302,15 +537,20 @@ export class ContextModeStore {
|
|
|
302
537
|
// 1. Porter FTS5 with BM25 ranking
|
|
303
538
|
if (porterQ) {
|
|
304
539
|
let sql = `
|
|
305
|
-
SELECT rowid,
|
|
540
|
+
SELECT rowid, path, ord, body, title, session_id, worktree, bm25(chunks) AS rank
|
|
306
541
|
FROM chunks
|
|
307
542
|
WHERE chunks MATCH ?
|
|
308
543
|
`;
|
|
309
544
|
const params = [porterQ];
|
|
310
545
|
|
|
311
|
-
if (
|
|
312
|
-
|
|
313
|
-
|
|
546
|
+
if (pathFilter) {
|
|
547
|
+
if (isDir) {
|
|
548
|
+
sql += " AND path LIKE ?";
|
|
549
|
+
params.push(`${pathFilter}%`);
|
|
550
|
+
} else {
|
|
551
|
+
sql += " AND path = ?";
|
|
552
|
+
params.push(pathFilter);
|
|
553
|
+
}
|
|
314
554
|
} else if (scope === "session" && sessionId) {
|
|
315
555
|
sql += " AND session_id = ?";
|
|
316
556
|
params.push(sessionId);
|
|
@@ -324,7 +564,7 @@ export class ContextModeStore {
|
|
|
324
564
|
try {
|
|
325
565
|
const rows = this.db.prepare(sql).all(...params);
|
|
326
566
|
rows.forEach((row, rankIdx) => {
|
|
327
|
-
const key = `${row.
|
|
567
|
+
const key = `${row.path}:${row.ord}`;
|
|
328
568
|
const rrf = 1.0 / (RRF_K + rankIdx + 1);
|
|
329
569
|
const existing = scores.get(key);
|
|
330
570
|
if (existing) {
|
|
@@ -333,7 +573,7 @@ export class ContextModeStore {
|
|
|
333
573
|
} else {
|
|
334
574
|
scores.set(key, {
|
|
335
575
|
rowid: row.rowid,
|
|
336
|
-
|
|
576
|
+
path: row.path,
|
|
337
577
|
ord: row.ord,
|
|
338
578
|
body: row.body,
|
|
339
579
|
title: row.title,
|
|
@@ -352,15 +592,20 @@ export class ContextModeStore {
|
|
|
352
592
|
// 2. Trigram FTS5
|
|
353
593
|
if (trigramQ) {
|
|
354
594
|
let sqlTri = `
|
|
355
|
-
SELECT rowid,
|
|
595
|
+
SELECT rowid, path, ord, body, session_id
|
|
356
596
|
FROM chunks_tri
|
|
357
597
|
WHERE chunks_tri MATCH ?
|
|
358
598
|
`;
|
|
359
599
|
const paramsTri = [trigramQ];
|
|
360
600
|
|
|
361
|
-
if (
|
|
362
|
-
|
|
363
|
-
|
|
601
|
+
if (pathFilter) {
|
|
602
|
+
if (isDir) {
|
|
603
|
+
sqlTri += " AND path LIKE ?";
|
|
604
|
+
paramsTri.push(`${pathFilter}%`);
|
|
605
|
+
} else {
|
|
606
|
+
sqlTri += " AND path = ?";
|
|
607
|
+
paramsTri.push(pathFilter);
|
|
608
|
+
}
|
|
364
609
|
} else if (scope === "session" && sessionId) {
|
|
365
610
|
sqlTri += " AND session_id = ?";
|
|
366
611
|
paramsTri.push(sessionId);
|
|
@@ -371,7 +616,7 @@ export class ContextModeStore {
|
|
|
371
616
|
try {
|
|
372
617
|
const rowsTri = this.db.prepare(sqlTri).all(...paramsTri);
|
|
373
618
|
rowsTri.forEach((row, rankIdx) => {
|
|
374
|
-
const key = `${row.
|
|
619
|
+
const key = `${row.path}:${row.ord}`;
|
|
375
620
|
const rrf = 1.0 / (RRF_K + rankIdx + 1);
|
|
376
621
|
const existing = scores.get(key);
|
|
377
622
|
if (existing) {
|
|
@@ -380,7 +625,7 @@ export class ContextModeStore {
|
|
|
380
625
|
} else {
|
|
381
626
|
scores.set(key, {
|
|
382
627
|
rowid: row.rowid,
|
|
383
|
-
|
|
628
|
+
path: row.path,
|
|
384
629
|
ord: row.ord,
|
|
385
630
|
body: row.body,
|
|
386
631
|
title: "",
|
|
@@ -397,18 +642,44 @@ export class ContextModeStore {
|
|
|
397
642
|
}
|
|
398
643
|
}
|
|
399
644
|
|
|
400
|
-
const allHits = Array.from(scores.values())
|
|
645
|
+
const allHits = Array.from(scores.values());
|
|
646
|
+
if (reverse) {
|
|
647
|
+
// Prioritize tail chunks (newest lines / latest events in logs) first
|
|
648
|
+
allHits.sort((a, b) => {
|
|
649
|
+
if (b.ord !== a.ord) {
|
|
650
|
+
return b.ord - a.ord; // Highest ord (tail of file) first
|
|
651
|
+
}
|
|
652
|
+
return b.rrfScore - a.rrfScore;
|
|
653
|
+
});
|
|
654
|
+
} else {
|
|
655
|
+
allHits.sort((a, b) => b.rrfScore - a.rrfScore);
|
|
656
|
+
}
|
|
657
|
+
|
|
401
658
|
const paginated = allHits.slice(skipHits, skipHits + maxHits);
|
|
402
659
|
|
|
403
660
|
const formatted = paginated.map((hit) => {
|
|
404
661
|
let snippet = hit.body;
|
|
405
662
|
if (Buffer.byteLength(snippet, "utf8") > SNIPPET_MAX_BYTES) {
|
|
406
|
-
|
|
663
|
+
let matchPos = -1;
|
|
664
|
+
const lowerBody = hit.body.toLowerCase();
|
|
665
|
+
for (const q of queryList) {
|
|
666
|
+
if (!q || typeof q !== "string") continue;
|
|
667
|
+
const pos = lowerBody.indexOf(q.toLowerCase());
|
|
668
|
+
if (pos !== -1 && (matchPos === -1 || pos < matchPos)) {
|
|
669
|
+
matchPos = pos;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
if (matchPos > 100) {
|
|
673
|
+
const start = Math.max(0, matchPos - 60);
|
|
674
|
+
snippet = "... " + hit.body.slice(start, start + SNIPPET_MAX_BYTES) + "\n... [truncated]";
|
|
675
|
+
} else {
|
|
676
|
+
snippet = hit.body.slice(0, SNIPPET_MAX_BYTES) + "\n... [truncated]";
|
|
677
|
+
}
|
|
407
678
|
}
|
|
408
679
|
return {
|
|
409
|
-
|
|
680
|
+
path: hit.path,
|
|
410
681
|
ord: hit.ord,
|
|
411
|
-
title: hit.title || hit.
|
|
682
|
+
title: hit.title || hit.path,
|
|
412
683
|
rrfScore: parseFloat(hit.rrfScore.toFixed(5)),
|
|
413
684
|
matches: hit.matches.length,
|
|
414
685
|
snippet
|
|
@@ -416,6 +687,9 @@ export class ContextModeStore {
|
|
|
416
687
|
});
|
|
417
688
|
|
|
418
689
|
return {
|
|
690
|
+
queries: queryList,
|
|
691
|
+
path: pathFilter || null,
|
|
692
|
+
reverse: Boolean(reverse),
|
|
419
693
|
hits: formatted,
|
|
420
694
|
totalMatches: allHits.length,
|
|
421
695
|
returned: formatted.length,
|
|
@@ -501,7 +775,7 @@ export class ContextModeStore {
|
|
|
501
775
|
} else if (sid === "app" && sinceIso) {
|
|
502
776
|
sources = this.db
|
|
503
777
|
.prepare(`
|
|
504
|
-
SELECT
|
|
778
|
+
SELECT path, label, kind, bytes_total, chunk_count, created_at
|
|
505
779
|
FROM sources
|
|
506
780
|
WHERE created_at >= ? OR created_ts >= ?
|
|
507
781
|
ORDER BY created_ts DESC
|
|
@@ -520,7 +794,7 @@ export class ContextModeStore {
|
|
|
520
794
|
} else {
|
|
521
795
|
sources = this.db
|
|
522
796
|
.prepare(`
|
|
523
|
-
SELECT
|
|
797
|
+
SELECT path, label, kind, bytes_total, chunk_count, created_at
|
|
524
798
|
FROM sources
|
|
525
799
|
WHERE session_id = ?
|
|
526
800
|
ORDER BY created_ts DESC
|
|
@@ -552,13 +826,13 @@ export class ContextModeStore {
|
|
|
552
826
|
return row || null;
|
|
553
827
|
}
|
|
554
828
|
|
|
555
|
-
/** Retrieve all chunks for a specific
|
|
556
|
-
getSourceChunks(
|
|
829
|
+
/** Retrieve all chunks for a specific path */
|
|
830
|
+
getSourceChunks(targetPath) {
|
|
557
831
|
this.open();
|
|
558
|
-
if (!
|
|
832
|
+
if (!targetPath) return [];
|
|
559
833
|
return this.db
|
|
560
|
-
.prepare("SELECT ord, body, title FROM chunks WHERE
|
|
561
|
-
.all(
|
|
834
|
+
.prepare("SELECT ord, body, title FROM chunks WHERE path = ? ORDER BY ord ASC")
|
|
835
|
+
.all(targetPath);
|
|
562
836
|
}
|
|
563
837
|
|
|
564
838
|
/** Calculate token savings & statistics */
|
|
@@ -596,8 +870,11 @@ export class ContextModeStore {
|
|
|
596
870
|
WHERE s.kind NOT IN ('execution', 'batch', 'tool_output', 'exec')
|
|
597
871
|
AND NOT EXISTS (
|
|
598
872
|
SELECT 1 FROM events e
|
|
599
|
-
WHERE
|
|
600
|
-
AND e.sandbox_bytes = s.bytes_total
|
|
873
|
+
WHERE (
|
|
874
|
+
(e.tool = 'ctx_index' AND e.sandbox_bytes = s.bytes_total)
|
|
875
|
+
OR e.path = s.path
|
|
876
|
+
OR e.path = s.label
|
|
877
|
+
)
|
|
601
878
|
)
|
|
602
879
|
`).get();
|
|
603
880
|
} else if (sid === "app" && sinceIso) {
|
|
@@ -627,8 +904,11 @@ export class ContextModeStore {
|
|
|
627
904
|
AND s.kind NOT IN ('execution', 'batch', 'tool_output', 'exec')
|
|
628
905
|
AND NOT EXISTS (
|
|
629
906
|
SELECT 1 FROM events e
|
|
630
|
-
WHERE
|
|
631
|
-
AND e.sandbox_bytes = s.bytes_total
|
|
907
|
+
WHERE (
|
|
908
|
+
(e.tool = 'ctx_index' AND e.sandbox_bytes = s.bytes_total)
|
|
909
|
+
OR e.path = s.path
|
|
910
|
+
OR e.path = s.label
|
|
911
|
+
)
|
|
632
912
|
)
|
|
633
913
|
`).get(sinceIso, sinceTs);
|
|
634
914
|
} else {
|
|
@@ -659,8 +939,11 @@ export class ContextModeStore {
|
|
|
659
939
|
AND NOT EXISTS (
|
|
660
940
|
SELECT 1 FROM events e
|
|
661
941
|
WHERE e.session_id = s.session_id
|
|
662
|
-
AND
|
|
663
|
-
|
|
942
|
+
AND (
|
|
943
|
+
(e.tool = 'ctx_index' AND e.sandbox_bytes = s.bytes_total)
|
|
944
|
+
OR e.path = s.path
|
|
945
|
+
OR e.path = s.label
|
|
946
|
+
)
|
|
664
947
|
)
|
|
665
948
|
`).get(sid);
|
|
666
949
|
}
|
|
@@ -701,14 +984,14 @@ export class ContextModeStore {
|
|
|
701
984
|
|
|
702
985
|
if (scope === "app" && sinceTs) {
|
|
703
986
|
const sinceIso = new Date(sinceTs).toISOString();
|
|
704
|
-
const
|
|
705
|
-
for (const
|
|
706
|
-
this.db.prepare("DELETE FROM chunks WHERE
|
|
707
|
-
this.db.prepare("DELETE FROM chunks_tri WHERE
|
|
987
|
+
const paths = this.db.prepare("SELECT path FROM sources WHERE created_at >= ? OR created_ts >= ?").all(sinceIso, sinceTs).map((r) => r.path);
|
|
988
|
+
for (const p of paths) {
|
|
989
|
+
this.db.prepare("DELETE FROM chunks WHERE path = ?").run(p);
|
|
990
|
+
this.db.prepare("DELETE FROM chunks_tri WHERE path = ?").run(p);
|
|
708
991
|
}
|
|
709
992
|
this.db.prepare("DELETE FROM sources WHERE created_at >= ? OR created_ts >= ?").run(sinceIso, sinceTs);
|
|
710
993
|
this.db.prepare("DELETE FROM events WHERE created_at >= ?").run(sinceIso);
|
|
711
|
-
return { ok: true, purged: "app", count:
|
|
994
|
+
return { ok: true, purged: "app", count: paths.length };
|
|
712
995
|
}
|
|
713
996
|
|
|
714
997
|
if (scope === "worktree" && worktree) {
|
|
@@ -840,6 +1123,64 @@ export class ContextModeStore {
|
|
|
840
1123
|
return numVal;
|
|
841
1124
|
}
|
|
842
1125
|
|
|
1126
|
+
/** Get active auto-indexing max file size in bytes (default 50 MB, 0 = disabled) */
|
|
1127
|
+
getMaxAutoIndexBytes(sessionId = null) {
|
|
1128
|
+
this.open();
|
|
1129
|
+
// 1. Session/Conversation override
|
|
1130
|
+
if (sessionId && sessionId !== "all" && sessionId !== "lifetime") {
|
|
1131
|
+
const sessionVal = this.getMeta(`max_auto_index:session:${sessionId}`);
|
|
1132
|
+
if (sessionVal !== null) {
|
|
1133
|
+
return parseInt(sessionVal, 10);
|
|
1134
|
+
}
|
|
1135
|
+
if (sessionId !== "app") {
|
|
1136
|
+
const appVal = this.getMeta("max_auto_index:session:app");
|
|
1137
|
+
if (appVal !== null) {
|
|
1138
|
+
return parseInt(appVal, 10);
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
// 2. Environment variable if explicitly set
|
|
1144
|
+
const envVal = process.env.CONTEXT_MODE_MAX_AUTO_INDEX_BYTES;
|
|
1145
|
+
if (envVal) {
|
|
1146
|
+
const parsed = parseInt(envVal, 10);
|
|
1147
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
// 3. Saved global setting in SQLite
|
|
1151
|
+
const globalVal = this.getMeta("max_auto_index:global");
|
|
1152
|
+
if (globalVal !== null) {
|
|
1153
|
+
return parseInt(globalVal, 10);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
// 4. Default 50 MB
|
|
1157
|
+
return DEFAULT_MAX_AUTO_INDEX_BYTES;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
/** Set auto-indexing max file size in bytes (0 = disabled) */
|
|
1161
|
+
setMaxAutoIndexBytes(bytes, sessionId = null) {
|
|
1162
|
+
this.open();
|
|
1163
|
+
const val = parseInt(bytes, 10);
|
|
1164
|
+
const numVal = Number.isNaN(val) ? DEFAULT_MAX_AUTO_INDEX_BYTES : Math.max(0, val);
|
|
1165
|
+
if (sessionId && sessionId !== "all" && sessionId !== "lifetime") {
|
|
1166
|
+
this.setMeta(`max_auto_index:session:${sessionId}`, numVal);
|
|
1167
|
+
} else {
|
|
1168
|
+
this.setMeta("max_auto_index:global", numVal);
|
|
1169
|
+
this.deleteMeta("max_auto_index:session:app");
|
|
1170
|
+
this.deleteMeta("max_auto_index:session:default");
|
|
1171
|
+
}
|
|
1172
|
+
return numVal;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
/** Resolve intuitive file path: workspace-relative when inside project, normalized absolute when outside */
|
|
1176
|
+
resolveFilePath(filePath, cwd = process.cwd()) {
|
|
1177
|
+
return resolveFilePath(filePath, cwd);
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
resolveFileSourceId(filePath, cwd = process.cwd()) {
|
|
1181
|
+
return this.resolveFilePath(filePath, cwd);
|
|
1182
|
+
}
|
|
1183
|
+
|
|
843
1184
|
/** Internal helper to recompute and save the combined exempt_ext list */
|
|
844
1185
|
_recomputeExemptExtensions(sessionId = null) {
|
|
845
1186
|
const activeCats = this.getExemptCategories(sessionId);
|
|
@@ -1038,6 +1379,14 @@ export class ContextModeStore {
|
|
|
1038
1379
|
return [...DEFAULT_EXEMPT_EXTENSIONS];
|
|
1039
1380
|
}
|
|
1040
1381
|
|
|
1382
|
+
/** Get live human-readable summary of active exempt categories and custom extensions */
|
|
1383
|
+
getExemptSummary(sessionId = null) {
|
|
1384
|
+
this.open();
|
|
1385
|
+
const activeCats = this.getExemptCategories(sessionId);
|
|
1386
|
+
const customs = this.getCustomExemptExtensions(sessionId);
|
|
1387
|
+
return formatExemptSummary({ activeCats, customs });
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1041
1390
|
/** Set list of file extensions exempt from the size cutoff */
|
|
1042
1391
|
setExemptExtensions(extensions, sessionId = null) {
|
|
1043
1392
|
this.open();
|
|
@@ -1128,9 +1477,9 @@ export class ContextModeStore {
|
|
|
1128
1477
|
// 1. Prune sources exceeding per-session cap
|
|
1129
1478
|
const excessSessionSources = this.db
|
|
1130
1479
|
.prepare(`
|
|
1131
|
-
SELECT
|
|
1132
|
-
WHERE
|
|
1133
|
-
SELECT
|
|
1480
|
+
SELECT path FROM sources
|
|
1481
|
+
WHERE path NOT IN (
|
|
1482
|
+
SELECT path FROM sources s2
|
|
1134
1483
|
WHERE s2.session_id = sources.session_id
|
|
1135
1484
|
ORDER BY s2.created_ts DESC
|
|
1136
1485
|
LIMIT ?
|
|
@@ -1139,11 +1488,11 @@ export class ContextModeStore {
|
|
|
1139
1488
|
.all(maxSourcesPerSession);
|
|
1140
1489
|
|
|
1141
1490
|
if (excessSessionSources.length > 0) {
|
|
1142
|
-
const
|
|
1143
|
-
const placeholders =
|
|
1144
|
-
this.db.prepare(`DELETE FROM chunks WHERE
|
|
1145
|
-
this.db.prepare(`DELETE FROM chunks_tri WHERE
|
|
1146
|
-
this.db.prepare(`DELETE FROM sources WHERE
|
|
1491
|
+
const paths = excessSessionSources.map((r) => r.path);
|
|
1492
|
+
const placeholders = paths.map(() => "?").join(",");
|
|
1493
|
+
this.db.prepare(`DELETE FROM chunks WHERE path IN (${placeholders})`).run(...paths);
|
|
1494
|
+
this.db.prepare(`DELETE FROM chunks_tri WHERE path IN (${placeholders})`).run(...paths);
|
|
1495
|
+
this.db.prepare(`DELETE FROM sources WHERE path IN (${placeholders})`).run(...paths);
|
|
1147
1496
|
}
|
|
1148
1497
|
|
|
1149
1498
|
// 2. Prune global total sources if over cap
|
|
@@ -1151,15 +1500,15 @@ export class ContextModeStore {
|
|
|
1151
1500
|
if (totalCountRow && totalCountRow.count > maxTotalSources) {
|
|
1152
1501
|
const deleteCount = totalCountRow.count - maxTotalSources;
|
|
1153
1502
|
const oldest = this.db
|
|
1154
|
-
.prepare("SELECT
|
|
1503
|
+
.prepare("SELECT path FROM sources ORDER BY created_ts ASC LIMIT ?")
|
|
1155
1504
|
.all(deleteCount);
|
|
1156
1505
|
|
|
1157
1506
|
if (oldest.length > 0) {
|
|
1158
|
-
const
|
|
1159
|
-
const placeholders =
|
|
1160
|
-
this.db.prepare(`DELETE FROM chunks WHERE
|
|
1161
|
-
this.db.prepare(`DELETE FROM chunks_tri WHERE
|
|
1162
|
-
this.db.prepare(`DELETE FROM sources WHERE
|
|
1507
|
+
const paths = oldest.map((r) => r.path);
|
|
1508
|
+
const placeholders = paths.map(() => "?").join(",");
|
|
1509
|
+
this.db.prepare(`DELETE FROM chunks WHERE path IN (${placeholders})`).run(...paths);
|
|
1510
|
+
this.db.prepare(`DELETE FROM chunks_tri WHERE path IN (${placeholders})`).run(...paths);
|
|
1511
|
+
this.db.prepare(`DELETE FROM sources WHERE path IN (${placeholders})`).run(...paths);
|
|
1163
1512
|
}
|
|
1164
1513
|
}
|
|
1165
1514
|
} catch {
|