@qmediat.io/gemini-code-context-mcp 1.12.2 → 1.13.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +88 -0
  2. package/dist/cache/cache-manager.d.ts +20 -0
  3. package/dist/cache/cache-manager.d.ts.map +1 -1
  4. package/dist/cache/cache-manager.js +96 -16
  5. package/dist/cache/cache-manager.js.map +1 -1
  6. package/dist/cache/files-uploader.d.ts.map +1 -1
  7. package/dist/cache/files-uploader.js +57 -30
  8. package/dist/cache/files-uploader.js.map +1 -1
  9. package/dist/config.d.ts +11 -0
  10. package/dist/config.d.ts.map +1 -1
  11. package/dist/config.js +1 -0
  12. package/dist/config.js.map +1 -1
  13. package/dist/indexer/workspace-scanner.d.ts +63 -0
  14. package/dist/indexer/workspace-scanner.d.ts.map +1 -1
  15. package/dist/indexer/workspace-scanner.js +66 -7
  16. package/dist/indexer/workspace-scanner.js.map +1 -1
  17. package/dist/manifest/db.d.ts +101 -0
  18. package/dist/manifest/db.d.ts.map +1 -1
  19. package/dist/manifest/db.js +231 -10
  20. package/dist/manifest/db.js.map +1 -1
  21. package/dist/tools/ask.tool.d.ts +10 -0
  22. package/dist/tools/ask.tool.d.ts.map +1 -1
  23. package/dist/tools/ask.tool.js +40 -1
  24. package/dist/tools/ask.tool.js.map +1 -1
  25. package/dist/tools/code.tool.d.ts +10 -0
  26. package/dist/tools/code.tool.d.ts.map +1 -1
  27. package/dist/tools/code.tool.js +39 -1
  28. package/dist/tools/code.tool.js.map +1 -1
  29. package/dist/tools/reindex.tool.d.ts.map +1 -1
  30. package/dist/tools/reindex.tool.js +4 -0
  31. package/dist/tools/reindex.tool.js.map +1 -1
  32. package/dist/tools/status.tool.d.ts.map +1 -1
  33. package/dist/tools/status.tool.js +46 -0
  34. package/dist/tools/status.tool.js.map +1 -1
  35. package/dist/types.d.ts +36 -0
  36. package/dist/types.d.ts.map +1 -1
  37. package/dist/utils/run-pool.d.ts +15 -0
  38. package/dist/utils/run-pool.d.ts.map +1 -0
  39. package/dist/utils/run-pool.js +36 -0
  40. package/dist/utils/run-pool.js.map +1 -0
  41. package/package.json +1 -1
@@ -3,18 +3,73 @@
3
3
  *
4
4
  * Respects default include extensions + exclude directories, plus user overrides
5
5
  * passed via `MatchConfig`. Enforces a soft file-count cap and a per-file size cap.
6
+ *
7
+ * v1.13.0+: when `manifestMemo` is supplied, files whose `(mtime_ms, size)`
8
+ * match the previously-stored values reuse the stored content hash and skip
9
+ * re-hashing — typically ~95% of files on a warm rescan, cutting scan
10
+ * wall-clock by ≥10× on large workspaces.
11
+ */
12
+ import type { FileRow } from '../types.js';
13
+ /**
14
+ * v1.13.0+: build the per-file fingerprint map the scanner consults to skip
15
+ * re-hashing. Rows lacking `mtimeMs` or `size` (pre-1.13 manifests, or rows
16
+ * the uploader hasn't refreshed yet) are dropped — those files always
17
+ * re-hash on the next scan.
6
18
  */
19
+ export declare function buildScanMemo(rows: readonly FileRow[]): Map<string, ScanMemoEntry>;
20
+ /**
21
+ * v1.13.0+: per-file fingerprint stored on the previous scan, used by the
22
+ * scan memo to skip re-hashing when nothing material has changed.
23
+ *
24
+ * `contentHash` is the SHA256 we'd have computed; `mtimeMs` is the file's
25
+ * mtime in ms; `size` is the byte count. ALL THREE must match for the memo
26
+ * to fire — `mtime` alone would mis-cache the (rare) case where two writes
27
+ * within a 1-second resolution window leave the same `mtime` but different
28
+ * content. Size adds a second gate that catches that.
29
+ */
30
+ export interface ScanMemoEntry {
31
+ contentHash: string;
32
+ mtimeMs: number;
33
+ size: number;
34
+ }
7
35
  export interface ScanOptions {
8
36
  includeGlobs?: readonly string[];
9
37
  excludeGlobs?: readonly string[];
10
38
  maxFiles: number;
11
39
  maxFileSizeBytes: number;
40
+ /**
41
+ * Concurrency for the per-file `stat` + `hashFile` loop. v1.13.0 default
42
+ * (20) measured ~6× faster than the serial loop on a 670k-token workspace.
43
+ * Capped low enough that small workspaces don't pay setup overhead.
44
+ */
45
+ hashConcurrency?: number;
46
+ /**
47
+ * v1.13.0+: scan-memo lookup. Map keyed by relpath (POSIX-style, matches
48
+ * what the scanner produces). When `mtimeMs` and `size` from `stat()`
49
+ * match the entry, the scanner reuses `contentHash` and skips reading the
50
+ * file from disk. Pass empty (or `undefined`) to force a fresh hash for
51
+ * every file — equivalent to `forceRescan`.
52
+ */
53
+ manifestMemo?: ReadonlyMap<string, ScanMemoEntry>;
54
+ /**
55
+ * v1.13.0+: bypass the scan memo and re-hash every file. Useful when the
56
+ * caller suspects the manifest is stale (e.g. after a manual filesystem
57
+ * mutation outside the dev workflow) and wants a forced verification scan.
58
+ */
59
+ forceRescan?: boolean;
12
60
  }
13
61
  export interface ScannedFile {
14
62
  relpath: string;
15
63
  absolutePath: string;
16
64
  size: number;
17
65
  contentHash: string;
66
+ /** v1.13.0+: file mtime in ms, used by the scan memo on subsequent scans. */
67
+ mtimeMs: number;
68
+ /**
69
+ * v1.13.0+: did the scan memo fire for this file? Operators can sum these
70
+ * to see how often the warm path is exercised.
71
+ */
72
+ memoHit: boolean;
18
73
  }
19
74
  export interface ScanResult {
20
75
  workspaceRoot: string;
@@ -22,6 +77,14 @@ export interface ScanResult {
22
77
  filesHash: string;
23
78
  skippedTooLarge: number;
24
79
  truncated: boolean;
80
+ /**
81
+ * v1.13.0+: count of files that hit the scan memo (mtime+size unchanged
82
+ * since last scan, content hash reused without reading the file). Useful
83
+ * for status/observability — when this is near zero on what should be a
84
+ * warm scan, something is invalidating the memo (e.g. a build step
85
+ * touching every file).
86
+ */
87
+ memoHitCount: number;
25
88
  }
26
89
  export declare function scanWorkspace(workspaceRoot: string, options: ScanOptions): Promise<ScanResult>;
27
90
  //# sourceMappingURL=workspace-scanner.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"workspace-scanner.d.ts","sourceRoot":"","sources":["../../src/indexer/workspace-scanner.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAmBH,MAAM,WAAW,WAAW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;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAClD;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,6EAA6E;IAC7E,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IACzB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,OAAO,CAAC;IACnB;;;;;;OAMG;IACH,YAAY,EAAE,MAAM,CAAC;CACtB;AA6BD,wBAAsB,aAAa,CACjC,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,WAAW,GACnB,OAAO,CAAC,UAAU,CAAC,CA8ErB"}
@@ -3,11 +3,36 @@
3
3
  *
4
4
  * Respects default include extensions + exclude directories, plus user overrides
5
5
  * passed via `MatchConfig`. Enforces a soft file-count cap and a per-file size cap.
6
+ *
7
+ * v1.13.0+: when `manifestMemo` is supplied, files whose `(mtime_ms, size)`
8
+ * match the previously-stored values reuse the stored content hash and skip
9
+ * re-hashing — typically ~95% of files on a warm rescan, cutting scan
10
+ * wall-clock by ≥10× on large workspaces.
6
11
  */
7
12
  import { readdir, stat } from 'node:fs/promises';
8
13
  import { join, sep as pathSep, relative } from 'node:path';
14
+ import { runPool } from '../utils/run-pool.js';
9
15
  import { defaultMatchConfig, isFileIncluded, isPathExcluded } from './globs.js';
10
16
  import { hashFile, mergeHashes } from './hasher.js';
17
+ /**
18
+ * v1.13.0+: build the per-file fingerprint map the scanner consults to skip
19
+ * re-hashing. Rows lacking `mtimeMs` or `size` (pre-1.13 manifests, or rows
20
+ * the uploader hasn't refreshed yet) are dropped — those files always
21
+ * re-hash on the next scan.
22
+ */
23
+ export function buildScanMemo(rows) {
24
+ const memo = new Map();
25
+ for (const row of rows) {
26
+ if (typeof row.mtimeMs !== 'number' || typeof row.size !== 'number')
27
+ continue;
28
+ memo.set(row.relpath, {
29
+ contentHash: row.contentHash,
30
+ mtimeMs: row.mtimeMs,
31
+ size: row.size,
32
+ });
33
+ }
34
+ return memo;
35
+ }
11
36
  /**
12
37
  * Normalise a path separator to POSIX-style `/`.
13
38
  *
@@ -52,18 +77,51 @@ export async function scanWorkspace(workspaceRoot, options) {
52
77
  absolutes.sort();
53
78
  const truncated = absolutes.length > options.maxFiles;
54
79
  const picked = truncated ? absolutes.slice(0, options.maxFiles) : absolutes;
55
- const files = [];
56
- let skippedTooLarge = 0;
57
- for (const absolutePath of picked) {
80
+ const memo = options.forceRescan ? undefined : options.manifestMemo;
81
+ const concurrency = Math.max(1, options.hashConcurrency ?? 20);
82
+ const settled = await runPool(picked, concurrency, async (absolutePath) => {
58
83
  const stats = await stat(absolutePath);
59
84
  if (stats.size > options.maxFileSizeBytes) {
85
+ return { skipped: true };
86
+ }
87
+ const rel = toPosix(relative(workspaceRoot, absolutePath));
88
+ const memoEntry = memo?.get(rel);
89
+ const memoHit = memoEntry !== undefined &&
90
+ memoEntry.mtimeMs === stats.mtimeMs &&
91
+ memoEntry.size === stats.size;
92
+ const hash = memoHit
93
+ ? memoEntry.contentHash
94
+ : await hashFile(absolutePath, stats);
95
+ return {
96
+ file: {
97
+ relpath: rel,
98
+ absolutePath,
99
+ size: stats.size,
100
+ contentHash: hash,
101
+ mtimeMs: stats.mtimeMs,
102
+ memoHit,
103
+ },
104
+ };
105
+ });
106
+ // Drain the settled array preserving input order so `filesHash` stays
107
+ // deterministic. Any rejection (a `stat`/`hashFile` throw) bubbles —
108
+ // workspace scanning has no per-file recovery story; an unreadable file
109
+ // means the rescan is broken and the caller needs to surface that.
110
+ const files = [];
111
+ let skippedTooLarge = 0;
112
+ let memoHitCount = 0;
113
+ for (const result of settled) {
114
+ if (result.status === 'rejected') {
115
+ throw result.reason instanceof Error ? result.reason : new Error(String(result.reason));
116
+ }
117
+ const entry = result.value;
118
+ if ('skipped' in entry) {
60
119
  skippedTooLarge += 1;
61
120
  continue;
62
121
  }
63
- const rel = toPosix(relative(workspaceRoot, absolutePath));
64
- // Pass the already-fetched stats to avoid a second stat() inside hashFile.
65
- const hash = await hashFile(absolutePath, stats);
66
- files.push({ relpath: rel, absolutePath, size: stats.size, contentHash: hash });
122
+ files.push(entry.file);
123
+ if (entry.file.memoHit)
124
+ memoHitCount += 1;
67
125
  }
68
126
  const filesHash = mergeHashes(files.map((f) => ({ relpath: f.relpath, hash: f.contentHash })));
69
127
  return {
@@ -72,6 +130,7 @@ export async function scanWorkspace(workspaceRoot, options) {
72
130
  filesHash,
73
131
  skippedTooLarge,
74
132
  truncated,
133
+ memoHitCount,
75
134
  };
76
135
  }
77
136
  //# sourceMappingURL=workspace-scanner.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"workspace-scanner.js","sourceRoot":"","sources":["../../src/indexer/workspace-scanner.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,IAAI,EAAE,GAAG,IAAI,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAoB,kBAAkB,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAClG,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;;;GAOG;AACH,SAAS,OAAO,CAAC,CAAS;IACxB,OAAO,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAA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AG,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC;IAE/F,OAAO;QACL,aAAa;QACb,KAAK;QACL,SAAS;QACT,eAAe;QACf,SAAS;QACT,YAAY;KACb,CAAC;AACJ,CAAC"}
@@ -13,6 +13,46 @@ export declare class ManifestDb {
13
13
  upsertWorkspace(ws: WorkspaceRow): void;
14
14
  deleteWorkspace(workspaceRoot: string): void;
15
15
  getFiles(workspaceRoot: string): FileRow[];
16
+ /**
17
+ * v1.13.0+ memo-seeder. Used by `prepareContext`'s inline / implicit /
18
+ * small-workspace branches — those paths skip the uploader entirely (which
19
+ * is the only writer of `upsertFile`), so without this the scan memo would
20
+ * silently degrade to cold every call (every file re-hashes — defeating
21
+ * the v1.13.0 perf headline).
22
+ *
23
+ * Conditional preservation of `file_id` / `uploaded_at` / `expires_at`
24
+ * (round-3 fix for the v1.13.0 silent-corruption HIGH found by Gemini /
25
+ * GPT / Copilot in the round-2 review):
26
+ *
27
+ * - **content_hash UNCHANGED** → preserve the upload metadata. A prior
28
+ * explicit-cache run uploaded these bytes to Files API; a switch back
29
+ * to `cachingMode: 'explicit'` should hit dedup and skip re-upload.
30
+ * - **content_hash CHANGED** → null out `file_id` / `uploaded_at` /
31
+ * `expires_at`. The previous fileId points to an upload of OLD bytes
32
+ * on Google's servers; preserving it would let `findFileRowByHash`
33
+ * return that stale fileId for the NEW content — silently feeding
34
+ * OLD bytes into Gemini under the file's NEW relpath.
35
+ *
36
+ * The pre-fix version preserved unconditionally, locking in a stale-fileId
37
+ * row whenever an edit landed between an explicit run and an implicit run.
38
+ * Two corruption scenarios were reachable:
39
+ * (A) same-relpath self-corruption: file edited mid-session, subsequent
40
+ * explicit run reuses the stale fileId for the changed file.
41
+ * (B) cross-file leak: a different file with the same NEW content_hash
42
+ * hits the dedup query and gets routed through the stale fileId.
43
+ *
44
+ * The CASE-based clear closes both. Confirmed by /6step round-3 analysis:
45
+ * `findFileRowByHash` returns rows where `content_hash = ?` and `file_id IS
46
+ * NOT NULL` — the conditional null-out ensures any stale fileId is gone
47
+ * BEFORE the dedup query could return it.
48
+ */
49
+ refreshFileFingerprints(rows: ReadonlyArray<{
50
+ workspaceRoot: string;
51
+ relpath: string;
52
+ contentHash: string;
53
+ mtimeMs: number;
54
+ size: number;
55
+ }>): void;
16
56
  upsertFile(file: FileRow): void;
17
57
  deleteFile(workspaceRoot: string, relpath: string): void;
18
58
  /** Find a file ID for a content hash already uploaded (dedupe helper). */
@@ -68,6 +108,16 @@ export declare class ManifestDb {
68
108
  uncachedTokens: number;
69
109
  costUsdMicro: number;
70
110
  durationMs: number;
111
+ /**
112
+ * v1.13.0+: caching mode used for the call. `'inline'` is the
113
+ * forced-inline outcome (e.g. `code({ codeExecution: true })`), added in
114
+ * the v1.13.0 round-2 review fix (FN2) so telemetry distinguishes
115
+ * "user asked for explicit and got it" from "user asked for explicit but
116
+ * the runtime forced inline".
117
+ */
118
+ cachingMode?: 'explicit' | 'implicit' | 'inline' | null;
119
+ /** v1.13.0+: `usage_metadata.cachedContentTokenCount` from the Gemini response. */
120
+ cachedContentTokenCount?: number | null;
71
121
  }): void;
72
122
  /**
73
123
  * Drop a reservation row when the tool call failed before producing any
@@ -108,5 +158,56 @@ export declare class ManifestDb {
108
158
  inFlightReservedMicros: number;
109
159
  last24hCostMicros: number;
110
160
  };
161
+ /**
162
+ * v1.13.0+: caching telemetry aggregation for the `status` tool.
163
+ *
164
+ * Surfaces caching mode adoption, implicit-cache hit rate (when applicable),
165
+ * and explicit-cache rebuild count over the last 24 h. Hit rate is computed
166
+ * as `sum(cached_content_token_count) / (sum(cached_content_token_count) +
167
+ * sum(uncached_tokens))` across implicit-mode calls — the ratio of input
168
+ * tokens that Gemini's automatic implicit cache served vs the ones we paid
169
+ * full input rate for. Settled rows only (`duration_ms > 0`) so in-flight
170
+ * reservations don't skew the hit rate downward.
171
+ *
172
+ * `mode` is the dominant caching mode in the window:
173
+ * - `'explicit'` if all metric rows used explicit (or no caching mode set)
174
+ * - `'implicit'` if all metric rows used implicit
175
+ * - `'inline'` if all metric rows used the forced-inline path (e.g.
176
+ * `code({ codeExecution: true })` — the user requested
177
+ * explicit but the runtime forbade `cachedContent` + tools
178
+ * simultaneously). Distinguished from `'explicit'` so the
179
+ * v1.14.0 default-flip telemetry isn't biased toward
180
+ * "explicit adoption" by codeExecution traffic.
181
+ * - `'mixed'` if 2+ of the above modes appear (operator changed
182
+ * defaults mid-day, codeExecution + non-codeExecution
183
+ * traffic, etc.)
184
+ * - `null` if no calls in the window
185
+ *
186
+ * Rows older than v1.13.0 lack `caching_mode` (NULL) and are treated as
187
+ * `'explicit'` for the dominant-mode tally.
188
+ *
189
+ * Upgrade-day note (v1.12.x → v1.13.0): if pre-upgrade calls landed inside
190
+ * the 24h window AND post-upgrade traffic is exclusively forced-inline
191
+ * (e.g. `code({ codeExecution: true })`), `mode` returns `'mixed'` because
192
+ * the legacy NULL rows COALESCE into the explicit tally. This self-corrects
193
+ * once the legacy rows fall out of the 24h window. Operators surprised by
194
+ * `'mixed'` post-upgrade should look at `inlineCallCount` and
195
+ * `explicitRebuildCount` to confirm the actual post-upgrade traffic shape.
196
+ */
197
+ cacheStatsLast24h(nowMs: number): {
198
+ mode: 'explicit' | 'implicit' | 'inline' | 'mixed' | null;
199
+ callCount: number;
200
+ implicitCallsTotal: number;
201
+ implicitCallsWithHit: number;
202
+ implicitCachedTokens: number;
203
+ implicitUncachedTokens: number;
204
+ implicitHitRate: number;
205
+ explicitRebuildCount: number;
206
+ /** v1.13.0 round-2 (FN2 fix): forced-inline call count. Useful for
207
+ * understanding why explicit-rebuild count differs from explicit-call
208
+ * count (codeExecution calls request explicit, get inline, never trigger
209
+ * caches.create). */
210
+ inlineCallCount: number;
211
+ };
111
212
  }
112
213
  //# sourceMappingURL=db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AAe,EAAE,MAAM,CAAC;QACxB,oBAAoB,EAAE,MAAM,CAAC;QAC7B;;;8BAGsB;QACtB,eAAe,EAAE,MAAM,CAAC;KACzB;CAuEF"}
@@ -31,6 +31,13 @@ CREATE TABLE IF NOT EXISTS files (
31
31
  file_id TEXT,
32
32
  uploaded_at INTEGER,
33
33
  expires_at INTEGER,
34
+ -- v1.13.0+: scan-memo columns. Allow re-using a stored content_hash when
35
+ -- the file's mtime + size haven't changed since the last scan, skipping
36
+ -- the read+SHA256 cost for ~95% of files on a typical warm code-review.
37
+ -- Nullable for rows written before v1.13.0; the scanner forces a fresh
38
+ -- hash whenever either column is null.
39
+ mtime_ms INTEGER,
40
+ size INTEGER,
34
41
  PRIMARY KEY (workspace_root, relpath),
35
42
  FOREIGN KEY (workspace_root) REFERENCES workspaces(workspace_root) ON DELETE CASCADE
36
43
  );
@@ -46,7 +53,15 @@ CREATE TABLE IF NOT EXISTS usage_metrics (
46
53
  uncached_tokens INTEGER,
47
54
  cost_usd_micro INTEGER,
48
55
  duration_ms INTEGER NOT NULL,
49
- occurred_at INTEGER NOT NULL
56
+ occurred_at INTEGER NOT NULL,
57
+ -- v1.13.0+: caching-mode telemetry. caching_mode records whether this
58
+ -- call used "explicit" (caches.create) or "implicit" (Gemini's automatic
59
+ -- cache for 2.5+/3 Pro). cached_content_token_count mirrors
60
+ -- usage_metadata.cachedContentTokenCount from the Gemini response and
61
+ -- is used by the status tool to compute cache-hit rate. Nullable for
62
+ -- rows written before v1.13.0 (treated as "explicit" for aggregations).
63
+ caching_mode TEXT,
64
+ cached_content_token_count INTEGER
50
65
  );
51
66
 
52
67
  CREATE INDEX IF NOT EXISTS idx_usage_occurred_at ON usage_metrics(occurred_at);
@@ -92,8 +107,30 @@ function rowToFile(row) {
92
107
  fileId: row.file_id ?? null,
93
108
  uploadedAt: row.uploaded_at ?? null,
94
109
  expiresAt: row.expires_at ?? null,
110
+ mtimeMs: row.mtime_ms ?? null,
111
+ size: row.size ?? null,
95
112
  };
96
113
  }
114
+ /**
115
+ * v1.13.0+ additive column migrations. Each invocation runs `ALTER TABLE …
116
+ * ADD COLUMN` for one new column and swallows the "duplicate column name"
117
+ * error so the call is idempotent — re-running the binary on an already-
118
+ * migrated DB is a no-op. Every other SQLite error rethrows so genuine
119
+ * schema corruption surfaces.
120
+ *
121
+ * SQLite has no `ADD COLUMN IF NOT EXISTS`, hence the catch dance.
122
+ */
123
+ function addColumnIfMissing(db, table, column, type) {
124
+ try {
125
+ db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${type}`);
126
+ }
127
+ catch (err) {
128
+ const msg = err instanceof Error ? err.message : String(err);
129
+ if (msg.includes('duplicate column name'))
130
+ return;
131
+ throw err;
132
+ }
133
+ }
97
134
  export class ManifestDb {
98
135
  db;
99
136
  constructor(pathOverride) {
@@ -105,6 +142,15 @@ export class ManifestDb {
105
142
  this.db.pragma('synchronous = NORMAL');
106
143
  this.db.pragma('foreign_keys = ON');
107
144
  this.db.exec(SCHEMA_SQL);
145
+ // v1.13.0 additive migrations. `CREATE TABLE IF NOT EXISTS` above defines
146
+ // the post-1.13 schema for fresh DBs; for DBs already created by v1.12.x
147
+ // these `ADD COLUMN` calls bring the existing tables up to spec. Both
148
+ // calls are no-ops on a fresh DB (the columns already exist) thanks to
149
+ // `addColumnIfMissing`'s duplicate-column swallow.
150
+ addColumnIfMissing(this.db, 'files', 'mtime_ms', 'INTEGER');
151
+ addColumnIfMissing(this.db, 'files', 'size', 'INTEGER');
152
+ addColumnIfMissing(this.db, 'usage_metrics', 'caching_mode', 'TEXT');
153
+ addColumnIfMissing(this.db, 'usage_metrics', 'cached_content_token_count', 'INTEGER');
108
154
  const meta = this.db.prepare('SELECT value FROM schema_meta WHERE key = ?').get('version');
109
155
  if (!meta) {
110
156
  this.db
@@ -149,17 +195,94 @@ export class ManifestDb {
149
195
  .all(workspaceRoot);
150
196
  return rows.map(rowToFile);
151
197
  }
198
+ /**
199
+ * v1.13.0+ memo-seeder. Used by `prepareContext`'s inline / implicit /
200
+ * small-workspace branches — those paths skip the uploader entirely (which
201
+ * is the only writer of `upsertFile`), so without this the scan memo would
202
+ * silently degrade to cold every call (every file re-hashes — defeating
203
+ * the v1.13.0 perf headline).
204
+ *
205
+ * Conditional preservation of `file_id` / `uploaded_at` / `expires_at`
206
+ * (round-3 fix for the v1.13.0 silent-corruption HIGH found by Gemini /
207
+ * GPT / Copilot in the round-2 review):
208
+ *
209
+ * - **content_hash UNCHANGED** → preserve the upload metadata. A prior
210
+ * explicit-cache run uploaded these bytes to Files API; a switch back
211
+ * to `cachingMode: 'explicit'` should hit dedup and skip re-upload.
212
+ * - **content_hash CHANGED** → null out `file_id` / `uploaded_at` /
213
+ * `expires_at`. The previous fileId points to an upload of OLD bytes
214
+ * on Google's servers; preserving it would let `findFileRowByHash`
215
+ * return that stale fileId for the NEW content — silently feeding
216
+ * OLD bytes into Gemini under the file's NEW relpath.
217
+ *
218
+ * The pre-fix version preserved unconditionally, locking in a stale-fileId
219
+ * row whenever an edit landed between an explicit run and an implicit run.
220
+ * Two corruption scenarios were reachable:
221
+ * (A) same-relpath self-corruption: file edited mid-session, subsequent
222
+ * explicit run reuses the stale fileId for the changed file.
223
+ * (B) cross-file leak: a different file with the same NEW content_hash
224
+ * hits the dedup query and gets routed through the stale fileId.
225
+ *
226
+ * The CASE-based clear closes both. Confirmed by /6step round-3 analysis:
227
+ * `findFileRowByHash` returns rows where `content_hash = ?` and `file_id IS
228
+ * NOT NULL` — the conditional null-out ensures any stale fileId is gone
229
+ * BEFORE the dedup query could return it.
230
+ */
231
+ refreshFileFingerprints(rows) {
232
+ if (rows.length === 0)
233
+ return;
234
+ // SQL semantics anchored by an empirical better-sqlite3 probe (round-3
235
+ // verification, /tmp/coderev/.../round3-verify-sixstep.md finding #2):
236
+ //
237
+ // - `files.X` in the SET clause references the OLD (pre-conflict) row
238
+ // value, so `files.content_hash <> excluded.content_hash` correctly
239
+ // compares OLD vs NEW. Confirmed by probe.
240
+ // - The conditional CASE columns are listed BEFORE the unconditional
241
+ // `content_hash = excluded.content_hash` overwrite — but per SQLite
242
+ // UPDATE semantics, all RHS expressions are evaluated using OLD row
243
+ // values regardless of column order, so order is purely cosmetic.
244
+ //
245
+ // Defensive `COALESCE(files.content_hash, '')` guard: SQL `NULL <> 'x'`
246
+ // is NULL (falsy), which would let the CASE fall into the ELSE branch
247
+ // and PRESERVE a stale fileId paired with the new content_hash — exactly
248
+ // the corruption shape this fix closes. Schema declares `content_hash
249
+ // TEXT NOT NULL` so today the NULL case is unreachable, but if a future
250
+ // migration ever relaxes the constraint or a botched manual DB edit
251
+ // produces a NULL content_hash, the COALESCE forces the comparison to
252
+ // succeed (`'' <> 'new-hash'` → TRUE) and the stale fileId is cleared.
253
+ // Defense-in-depth, zero runtime cost.
254
+ const stmt = this.db.prepare(`INSERT INTO files(
255
+ workspace_root, relpath, content_hash, file_id, uploaded_at, expires_at,
256
+ mtime_ms, size
257
+ ) VALUES (?, ?, ?, NULL, NULL, NULL, ?, ?)
258
+ ON CONFLICT(workspace_root, relpath) DO UPDATE SET
259
+ file_id = CASE WHEN COALESCE(files.content_hash, '') <> excluded.content_hash THEN NULL ELSE files.file_id END,
260
+ uploaded_at = CASE WHEN COALESCE(files.content_hash, '') <> excluded.content_hash THEN NULL ELSE files.uploaded_at END,
261
+ expires_at = CASE WHEN COALESCE(files.content_hash, '') <> excluded.content_hash THEN NULL ELSE files.expires_at END,
262
+ content_hash = excluded.content_hash,
263
+ mtime_ms = excluded.mtime_ms,
264
+ size = excluded.size`);
265
+ const tx = this.db.transaction((batch) => {
266
+ for (const r of batch) {
267
+ stmt.run(r.workspaceRoot, r.relpath, r.contentHash, r.mtimeMs, r.size);
268
+ }
269
+ });
270
+ tx(rows);
271
+ }
152
272
  upsertFile(file) {
153
273
  this.db
154
274
  .prepare(`INSERT INTO files(
155
- workspace_root, relpath, content_hash, file_id, uploaded_at, expires_at
156
- ) VALUES (?, ?, ?, ?, ?, ?)
275
+ workspace_root, relpath, content_hash, file_id, uploaded_at, expires_at,
276
+ mtime_ms, size
277
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
157
278
  ON CONFLICT(workspace_root, relpath) DO UPDATE SET
158
279
  content_hash = excluded.content_hash,
159
280
  file_id = excluded.file_id,
160
281
  uploaded_at = excluded.uploaded_at,
161
- expires_at = excluded.expires_at`)
162
- .run(file.workspaceRoot, file.relpath, file.contentHash, file.fileId, file.uploadedAt, file.expiresAt);
282
+ expires_at = excluded.expires_at,
283
+ mtime_ms = excluded.mtime_ms,
284
+ size = excluded.size`)
285
+ .run(file.workspaceRoot, file.relpath, file.contentHash, file.fileId, file.uploadedAt, file.expiresAt, file.mtimeMs ?? null, file.size ?? null);
163
286
  }
164
287
  deleteFile(workspaceRoot, relpath) {
165
288
  this.db
@@ -189,9 +312,10 @@ export class ManifestDb {
189
312
  this.db
190
313
  .prepare(`INSERT INTO usage_metrics(
191
314
  workspace_root, tool_name, model, cached_tokens, uncached_tokens,
192
- cost_usd_micro, duration_ms, occurred_at
193
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
194
- .run(metric.workspaceRoot, metric.toolName, metric.model, metric.cachedTokens, metric.uncachedTokens, metric.costUsdMicro, metric.durationMs, metric.occurredAt);
315
+ cost_usd_micro, duration_ms, occurred_at,
316
+ caching_mode, cached_content_token_count
317
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
318
+ .run(metric.workspaceRoot, metric.toolName, metric.model, metric.cachedTokens, metric.uncachedTokens, metric.costUsdMicro, metric.durationMs, metric.occurredAt, metric.cachingMode ?? null, metric.cachedContentTokenCount ?? null);
195
319
  }
196
320
  /**
197
321
  * Atomically check the daily budget and — if there's headroom — reserve
@@ -260,9 +384,10 @@ export class ManifestDb {
260
384
  const safeDurationMs = Math.max(1, data.durationMs);
261
385
  this.db
262
386
  .prepare(`UPDATE usage_metrics
263
- SET cached_tokens = ?, uncached_tokens = ?, cost_usd_micro = ?, duration_ms = ?
387
+ SET cached_tokens = ?, uncached_tokens = ?, cost_usd_micro = ?, duration_ms = ?,
388
+ caching_mode = ?, cached_content_token_count = ?
264
389
  WHERE id = ?`)
265
- .run(data.cachedTokens, data.uncachedTokens, data.costUsdMicro, safeDurationMs, reservationId);
390
+ .run(data.cachedTokens, data.uncachedTokens, data.costUsdMicro, safeDurationMs, data.cachingMode ?? null, data.cachedContentTokenCount ?? null, reservationId);
266
391
  }
267
392
  /**
268
393
  * Drop a reservation row when the tool call failed before producing any
@@ -326,5 +451,101 @@ export class ManifestDb {
326
451
  last24hCostMicros: last24h.total,
327
452
  };
328
453
  }
454
+ /**
455
+ * v1.13.0+: caching telemetry aggregation for the `status` tool.
456
+ *
457
+ * Surfaces caching mode adoption, implicit-cache hit rate (when applicable),
458
+ * and explicit-cache rebuild count over the last 24 h. Hit rate is computed
459
+ * as `sum(cached_content_token_count) / (sum(cached_content_token_count) +
460
+ * sum(uncached_tokens))` across implicit-mode calls — the ratio of input
461
+ * tokens that Gemini's automatic implicit cache served vs the ones we paid
462
+ * full input rate for. Settled rows only (`duration_ms > 0`) so in-flight
463
+ * reservations don't skew the hit rate downward.
464
+ *
465
+ * `mode` is the dominant caching mode in the window:
466
+ * - `'explicit'` if all metric rows used explicit (or no caching mode set)
467
+ * - `'implicit'` if all metric rows used implicit
468
+ * - `'inline'` if all metric rows used the forced-inline path (e.g.
469
+ * `code({ codeExecution: true })` — the user requested
470
+ * explicit but the runtime forbade `cachedContent` + tools
471
+ * simultaneously). Distinguished from `'explicit'` so the
472
+ * v1.14.0 default-flip telemetry isn't biased toward
473
+ * "explicit adoption" by codeExecution traffic.
474
+ * - `'mixed'` if 2+ of the above modes appear (operator changed
475
+ * defaults mid-day, codeExecution + non-codeExecution
476
+ * traffic, etc.)
477
+ * - `null` if no calls in the window
478
+ *
479
+ * Rows older than v1.13.0 lack `caching_mode` (NULL) and are treated as
480
+ * `'explicit'` for the dominant-mode tally.
481
+ *
482
+ * Upgrade-day note (v1.12.x → v1.13.0): if pre-upgrade calls landed inside
483
+ * the 24h window AND post-upgrade traffic is exclusively forced-inline
484
+ * (e.g. `code({ codeExecution: true })`), `mode` returns `'mixed'` because
485
+ * the legacy NULL rows COALESCE into the explicit tally. This self-corrects
486
+ * once the legacy rows fall out of the 24h window. Operators surprised by
487
+ * `'mixed'` post-upgrade should look at `inlineCallCount` and
488
+ * `explicitRebuildCount` to confirm the actual post-upgrade traffic shape.
489
+ */
490
+ cacheStatsLast24h(nowMs) {
491
+ const since = nowMs - 24 * 3600 * 1000;
492
+ // Restricted to `ask` + `code` rows: those are the calls that flow through
493
+ // `prepareContext` and have a meaningful caching mode. `ask_agentic` (NULL
494
+ // caching_mode by design — no workspace cache) and `cache.create` (infra
495
+ // rebuild marker — counted separately below) are excluded so they don't
496
+ // skew the dominant-mode tally toward 'explicit'.
497
+ const row = this.db
498
+ .prepare(`SELECT
499
+ COUNT(*) AS call_count,
500
+ COALESCE(SUM(CASE WHEN COALESCE(caching_mode, 'explicit') = 'explicit' THEN 1 ELSE 0 END), 0) AS explicit_count,
501
+ COALESCE(SUM(CASE WHEN caching_mode = 'implicit' THEN 1 ELSE 0 END), 0) AS implicit_count,
502
+ COALESCE(SUM(CASE WHEN caching_mode = 'inline' THEN 1 ELSE 0 END), 0) AS inline_count,
503
+ COALESCE(SUM(CASE WHEN caching_mode = 'implicit' AND COALESCE(cached_content_token_count, 0) > 0 THEN 1 ELSE 0 END), 0) AS implicit_hit_count,
504
+ COALESCE(SUM(CASE WHEN caching_mode = 'implicit' THEN cached_content_token_count ELSE 0 END), 0) AS implicit_cached_tokens,
505
+ COALESCE(SUM(CASE WHEN caching_mode = 'implicit' THEN uncached_tokens ELSE 0 END), 0) AS implicit_uncached_tokens
506
+ FROM usage_metrics
507
+ WHERE occurred_at >= ? AND duration_ms > 0
508
+ AND tool_name IN ('ask', 'code')`)
509
+ .get(since);
510
+ // `tool_name = 'cache.create'` rows are emitted by cache-manager when a
511
+ // fresh `caches.create` call lands on the explicit path. Counting them
512
+ // gives the operator a direct rebuild tally — the v1.13.0 motivation for
513
+ // pivoting toward implicit caching is to drive this number toward zero.
514
+ const rebuilds = this.db
515
+ .prepare(`SELECT COUNT(*) AS n FROM usage_metrics
516
+ WHERE occurred_at >= ? AND duration_ms > 0 AND tool_name = 'cache.create'`)
517
+ .get(since);
518
+ // Dominant mode: 'mixed' if 2+ of the three actual modes appear; else the
519
+ // one mode that did. v1.13.0 round-2 (FN2 fix) widened this to include
520
+ // 'inline' alongside 'explicit' / 'implicit'.
521
+ const explicitN = row.explicit_count;
522
+ const implicitN = row.implicit_count;
523
+ const inlineN = row.inline_count;
524
+ const presentModes = [explicitN > 0, implicitN > 0, inlineN > 0].filter(Boolean).length;
525
+ let mode;
526
+ if (row.call_count === 0)
527
+ mode = null;
528
+ else if (presentModes >= 2)
529
+ mode = 'mixed';
530
+ else if (implicitN > 0)
531
+ mode = 'implicit';
532
+ else if (inlineN > 0)
533
+ mode = 'inline';
534
+ else
535
+ mode = 'explicit';
536
+ const denom = row.implicit_cached_tokens + row.implicit_uncached_tokens;
537
+ const implicitHitRate = denom > 0 ? row.implicit_cached_tokens / denom : 0;
538
+ return {
539
+ mode,
540
+ callCount: row.call_count,
541
+ implicitCallsTotal: row.implicit_count,
542
+ implicitCallsWithHit: row.implicit_hit_count,
543
+ implicitCachedTokens: row.implicit_cached_tokens,
544
+ implicitUncachedTokens: row.implicit_uncached_tokens,
545
+ implicitHitRate,
546
+ explicitRebuildCount: rebuilds.n,
547
+ inlineCallCount: row.inline_count,
548
+ };
549
+ }
329
550
  }
330
551
  //# sourceMappingURL=db.js.map