dsh-better-edit 0.8.2 → 1.1.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 (79) hide show
  1. package/CHANGELOG.md +173 -99
  2. package/README.md +61 -33
  3. package/README.zh.md +20 -17
  4. package/cordis.patch.yml +1 -1
  5. package/lib/constants.js +19 -4
  6. package/lib/contract.js +136 -79
  7. package/lib/domain-errors.js +570 -0
  8. package/lib/edit-diff.js +33 -7
  9. package/lib/file-encoding-state.js +27 -7
  10. package/lib/file-view.js +305 -82
  11. package/lib/fs-bridge.js +23 -7
  12. package/lib/guidance/materialize.js +7 -7
  13. package/lib/guidance/resolve.js +13 -8
  14. package/lib/hash-store.js +404 -278
  15. package/lib/hashline/anchor-pipeline.js +586 -378
  16. package/lib/hashline/hash-assign.js +21 -22
  17. package/lib/hashline/hash.js +6 -6
  18. package/lib/hashline/index.js +1 -1
  19. package/lib/hashline/resolve.js +1 -1
  20. package/lib/hashline/served-guard.js +181 -0
  21. package/lib/index.js +37 -11
  22. package/lib/mutation/engine.js +392 -96
  23. package/lib/mutation.js +22 -130
  24. package/lib/noop-guard.js +20 -9
  25. package/lib/prompts.js +20 -18
  26. package/lib/read-and-serve.js +44 -20
  27. package/lib/sandbox.js +1 -1
  28. package/lib/session-view.js +57 -43
  29. package/lib/snapshot-store/index.js +97 -0
  30. package/lib/snapshot-store/lineage-store.js +400 -0
  31. package/lib/snapshot-store/migrate.js +80 -0
  32. package/lib/snapshot-store/pairing.js +412 -0
  33. package/lib/snapshot-store/txn.js +63 -0
  34. package/lib/snapshot-store/vacuum.js +243 -0
  35. package/lib/store-lifecycle.js +13 -1
  36. package/lib/store-retry.js +34 -0
  37. package/lib/tool-edit.js +90 -44
  38. package/lib/tool-read.js +29 -1
  39. package/lib/tool-str-replace-editor.js +26 -18
  40. package/lib/tool-undo.js +25 -8
  41. package/lib/types/constants.d.ts +5 -2
  42. package/lib/types/contract.d.ts +76 -71
  43. package/lib/types/domain-errors.d.ts +410 -0
  44. package/lib/types/edit-response.d.ts +1 -1
  45. package/lib/types/file-encoding-state.d.ts +1 -0
  46. package/lib/types/file-view.d.ts +35 -6
  47. package/lib/types/guidance/materialize.d.ts +2 -2
  48. package/lib/types/guidance/resolve.d.ts +14 -2
  49. package/lib/types/hash-store.d.ts +38 -19
  50. package/lib/types/hashline/anchor-pipeline.d.ts +117 -36
  51. package/lib/types/hashline/hash-assign.d.ts +5 -5
  52. package/lib/types/hashline/hash.d.ts +1 -1
  53. package/lib/types/hashline/index.d.ts +2 -2
  54. package/lib/types/hashline/served-guard.d.ts +100 -0
  55. package/lib/types/index.d.ts +3 -3
  56. package/lib/types/mutation/engine.d.ts +49 -30
  57. package/lib/types/mutation.d.ts +1 -39
  58. package/lib/types/noop-guard.d.ts +4 -4
  59. package/lib/types/prompts.d.ts +1 -1
  60. package/lib/types/read-and-serve.d.ts +6 -0
  61. package/lib/types/sandbox.d.ts +1 -1
  62. package/lib/types/session-view.d.ts +38 -9
  63. package/lib/types/snapshot-store/index.d.ts +28 -0
  64. package/lib/types/snapshot-store/lineage-store.d.ts +96 -0
  65. package/lib/types/snapshot-store/migrate.d.ts +16 -0
  66. package/lib/types/snapshot-store/pairing.d.ts +99 -0
  67. package/lib/types/snapshot-store/txn.d.ts +12 -0
  68. package/lib/types/snapshot-store/vacuum.d.ts +79 -0
  69. package/lib/types/store-lifecycle.d.ts +0 -1
  70. package/lib/types/store-retry.d.ts +6 -0
  71. package/lib/types/tool-edit.d.ts +2 -2
  72. package/lib/types/tool-str-replace-editor.d.ts +2 -2
  73. package/lib/types/utils.d.ts +5 -6
  74. package/lib/types/workspace-context.d.ts +10 -1
  75. package/lib/undo-edit.js +27 -11
  76. package/lib/utils.js +12 -12
  77. package/lib/workspace-context.js +27 -9
  78. package/lib/write-hook.js +13 -4
  79. package/package.json +28 -30
package/lib/hash-store.js CHANGED
@@ -2,9 +2,10 @@
2
2
  * The hash store — ONE deep persistence module for the hashline domain.
3
3
  *
4
4
  * Owns the sqlite db, the schema and migrations, corruption quarantine,
5
- * busy-retry, WAL, the legacy-JSON migration, AND the three narrow row APIs
6
- * the rest of the plugin needs: hash snapshots, undo entries, and served
7
- * rows. The prepared statements are a private implementation detail — callers
5
+ * Owns the sqlite db, the schema and migrations, corruption quarantine, WAL,
6
+ * the legacy-JSON migration, and the undo/served row APIs. Hash snapshots
7
+ * live in snapshot-store (shared busy-retry policy in store-retry), adapted
8
+ * here so the HashStore surface stays stable for its duck-typed callers.
8
9
  * use domain methods, never SQL.
9
10
  *
10
11
  * Corrupt-row handling (parse the JSON column → validate against the hash
@@ -14,32 +15,22 @@
14
15
  * @module dsh-better-edit/hash-store
15
16
  */
16
17
  import { appendFileSync, existsSync, readFileSync } from "node:fs";
17
- import { readFile, readdir, rename, rm, mkdir, stat } from "node:fs/promises";
18
+ import { readdir, rename, rm, mkdir, stat } from "node:fs/promises";
18
19
  import { dirname, join } from "node:path";
19
20
  import { DatabaseSync } from "node:sqlite";
20
21
  import { hashStorePath } from "./store-tenancy.js";
21
22
  import { onStoreOpen, setStoresGetter } from "./store-lifecycle.js";
22
23
  import { workspaceCwd } from "./workspace-context.js";
23
24
  import { errCode, splitLines } from "./utils.js";
24
- import { initHasher, contentChecksum, HASH_RE, CANON_VERSION } from "./hashline/hash-assign.js";
25
+ import { initHasher, HASH_RE } from "./hashline/hash-assign.js";
25
26
  import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS } from "./constants.js";
26
- export function isValidHashList(value) {
27
- if (!Array.isArray(value))
28
- return false;
29
- for (const hash of value) {
30
- if (typeof hash !== "string" || !HASH_RE.test(hash))
31
- return false;
32
- }
33
- return true;
34
- }
35
- export function isValidSnapshot(value) {
36
- if (typeof value !== "object" || value === null)
37
- return false;
38
- const v = value;
39
- if (typeof v.content !== "string")
40
- return false;
41
- return isValidHashList(v.hashes);
42
- }
27
+ import { DomainError } from "./domain-errors.js";
28
+ import { createSnapshotStore, isValidHashList, reportVacuum, vacuumSnapshots as runVacuumSnapshots, } from "./snapshot-store/index.js";
29
+ import { withBusyRetry } from "./store-retry.js";
30
+ import { migrateLegacyStore } from "./snapshot-store/migrate.js";
31
+ import { createLineageStore, ensureLineageTables, snapshotHashFor, } from "./snapshot-store/lineage-store.js";
32
+ import { withTransaction } from "./snapshot-store/txn.js";
33
+ // ---- validators (owned here; the store's corruption handling uses them) ----
43
34
  /** A served-row array: per-position hash, or null for never-served slots. */
44
35
  export function isValidCanonsList(value) {
45
36
  if (!Array.isArray(value))
@@ -63,8 +54,38 @@ export function isValidServedList(value) {
63
54
  }
64
55
  return true;
65
56
  }
66
- function cacheKey(checksum) {
67
- return `${CANON_VERSION}:${checksum}`;
57
+ /**
58
+ * Map a raw `file_undo` SELECT row to `FileUndoRow`, validating every column at the
59
+ * boundary. Replaces an `as unknown as FileUndoRow` double cast that the compiler cannot
60
+ * check (node:sqlite returns `Record<string, unknown>`): a SELECT/mapper drift used to
61
+ * surface as a silently `undefined` field instead of an error.
62
+ */
63
+ function mapFileUndoRow(row) {
64
+ const content = row["content"];
65
+ const bom = row["bom"];
66
+ const ending = row["ending"];
67
+ const hashes = row["hashes"];
68
+ const resultContent = row["result_content"];
69
+ const snapshotHash = row["snapshot_hash"];
70
+ const updatedAt = row["updated_at"];
71
+ if (typeof content !== "string" ||
72
+ typeof bom !== "string" ||
73
+ typeof ending !== "string" ||
74
+ typeof hashes !== "string" ||
75
+ typeof resultContent !== "string" ||
76
+ !(snapshotHash === null || typeof snapshotHash === "string") ||
77
+ typeof updatedAt !== "number") {
78
+ throw new TypeError("invalid file_undo row shape");
79
+ }
80
+ return {
81
+ content,
82
+ bom,
83
+ ending,
84
+ hashes,
85
+ result_content: resultContent,
86
+ snapshot_hash: snapshotHash,
87
+ updated_at: updatedAt,
88
+ };
68
89
  }
69
90
  export function isValidRetiredEntry(value) {
70
91
  if (typeof value !== "object" || value === null)
@@ -121,12 +142,14 @@ function parseRetiredJson(raw) {
121
142
  }
122
143
  }
123
144
  /** Load the store as served persistence — internal, for SessionView only. */
124
- // SAFETY: loadHashStore returns InternalHashStore (HashStore & ServedPersistence) — cast narrows to served view, validated via ServedPersistence interface; safe because InternalHashStore extends both.
145
+ // SAFETY: loadHashStore returns InternalHashStore (HashStore & ServedPersistence & LineageStore) — cast narrows to served view, validated via ServedPersistence interface; safe because InternalHashStore extends all three.
125
146
  export function loadServedStore(cwd) {
126
147
  return loadHashStore(cwd);
127
148
  }
128
149
  // ---- db plumbing (private) --------------------------------------------------
129
150
  export function isCorruptionError(error) {
151
+ if (error instanceof DomainError)
152
+ return false;
130
153
  if (error && typeof error === "object") {
131
154
  const errcode = error.errcode;
132
155
  if (typeof errcode === "number") {
@@ -139,35 +162,6 @@ export function isCorruptionError(error) {
139
162
  return (error instanceof Error &&
140
163
  /corrupt|not a database|malformed|database disk image/i.test(error.message));
141
164
  }
142
- function isBusyError(error) {
143
- if (error && typeof error === "object") {
144
- const errcode = error.errcode;
145
- if (typeof errcode === "number")
146
- return errcode === 5 || errcode === 6;
147
- }
148
- return error instanceof Error && /busy|locked/i.test(error.message);
149
- }
150
- function sleepSync(ms) {
151
- const sab = new Int32Array(new SharedArrayBuffer(4));
152
- Atomics.wait(sab, 0, 0, ms);
153
- }
154
- const BUSY_RETRIES = 3;
155
- const BUSY_RETRY_DELAY_MS = 100;
156
- function withBusyRetry(fn) {
157
- let lastError;
158
- for (let attempt = 0; attempt <= BUSY_RETRIES; attempt++) {
159
- try {
160
- return fn();
161
- }
162
- catch (error) {
163
- lastError = error;
164
- if (!isBusyError(error) || attempt === BUSY_RETRIES)
165
- throw error;
166
- sleepSync(BUSY_RETRY_DELAY_MS);
167
- }
168
- }
169
- throw lastError;
170
- }
171
165
  function openDbWithBusyRetry(storePath) {
172
166
  return withBusyRetry(() => openDb(storePath));
173
167
  }
@@ -180,7 +174,7 @@ function openDb(storePath) {
180
174
  timeout: HASH_STORE_BUSY_TIMEOUT,
181
175
  });
182
176
  try {
183
- return buildStore(db);
177
+ return buildStore(db, storePath);
184
178
  }
185
179
  catch (error) {
186
180
  try {
@@ -192,9 +186,81 @@ function openDb(storePath) {
192
186
  throw error;
193
187
  }
194
188
  }
195
- function buildStore(db) {
196
- db.exec("PRAGMA journal_mode = WAL");
197
- db.exec("PRAGMA synchronous = NORMAL");
189
+ /**
190
+ * Read-only probe of the store's schema stamp. Safe on a store whose schema
191
+ * we do not own: two SELECTs, no DDL, no PRAGMA, no run(). Returns undefined
192
+ * when the meta table or the version row is absent, or when the value does
193
+ * not parse as a base-10 integer. Probe errors propagate — a malformed meta
194
+ * is genuine corruption and must keep flowing to the corruption path.
195
+ */
196
+ function storedVersion(db) {
197
+ const metaRow = db
198
+ .prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'meta'")
199
+ .get();
200
+ if (metaRow === undefined)
201
+ return undefined;
202
+ const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'version'").get();
203
+ if (versionRow?.value === undefined)
204
+ return undefined;
205
+ const parsed = Number.parseInt(versionRow.value, 10);
206
+ return Number.isInteger(parsed) ? parsed : undefined;
207
+ }
208
+ /**
209
+ * Idempotent column backfill: ALTER TABLE only when the column is missing.
210
+ * Table/column/type ride an identifier allowlist (SQLite has no bind
211
+ * parameters for DDL identifiers); every current caller passes constants.
212
+ *
213
+ * pi-lens's `sql-injection` rule (`inline_tier: blocking`) flags any `db.exec`/`db.prepare`
214
+ * that receives a template literal with a substitution, including the two DDL statements
215
+ * below. That finding is a false positive here: SCHEMA_IDENTIFIER_RE rejects anything that
216
+ * is not `[A-Za-z_][A-Za-z0-9_]*` before the SQL text is built, and the rule cannot see
217
+ * that guard. Trigger: re-open if a caller stops passing a literal, SCHEMA_IDENTIFIER_RE is
218
+ * relaxed or removed, or a non-constant reaches table/column/type.
219
+ */
220
+ const SCHEMA_IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
221
+ function addColumnIfMissing(db, table, column, type) {
222
+ for (const identifier of [table, column, type]) {
223
+ if (!SCHEMA_IDENTIFIER_RE.test(identifier)) {
224
+ throw new Error(`Refused schema backfill for non-identifier: ${identifier}`);
225
+ }
226
+ }
227
+ const columns = db.prepare(`PRAGMA table_info(${table})`).all();
228
+ if (!columns.some((entry) => entry.name === column)) {
229
+ db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${type}`);
230
+ }
231
+ }
232
+ /**
233
+ * v7 state tables. All additive (IF NOT EXISTS), owned by this build: a v6
234
+ * process never names them, so a version flap cannot cost v7 anything.
235
+ */
236
+ function ensureV7Tables(db) {
237
+ ensureLineageTables(db);
238
+ db.exec("CREATE TABLE IF NOT EXISTS file_undo (" +
239
+ "path TEXT PRIMARY KEY, " +
240
+ "content TEXT NOT NULL, " +
241
+ "bom TEXT NOT NULL, " +
242
+ "ending TEXT NOT NULL, " +
243
+ "hashes TEXT NOT NULL, " +
244
+ "result_content TEXT NOT NULL, " +
245
+ "snapshot_hash TEXT, " +
246
+ "updated_at INTEGER NOT NULL" +
247
+ ")");
248
+ }
249
+ /**
250
+ * Non-destructive, idempotent schema build. Runs on every open: CREATE TABLE
251
+ * IF NOT EXISTS for the current shapes, backfill of the newer served columns,
252
+ * and DROP TABLE served only for the pre-session-keyed shell (no session_id —
253
+ * unusable by either version). No DELETE FROM anywhere on this path.
254
+ */
255
+ function ensureSchema(db) {
256
+ db.exec("CREATE TABLE IF NOT EXISTS meta (" + "key TEXT PRIMARY KEY, " + "value TEXT NOT NULL" + ")");
257
+ ensureV7Tables(db);
258
+ // WHY the v6 shells stay whole: an un-restarted v6 session — or a v6
259
+ // process in a concurrent worktree — prepares statements against these
260
+ // exact tables, so they are created complete on every open and never
261
+ // dropped or reshaped here. v7 state stays isolated in the v7 tables
262
+ // above. `cards` is kept because the released v6 build names it at store
263
+ // open; porting upstream's column list would break it.
198
264
  db.exec("CREATE TABLE IF NOT EXISTS snapshots (" +
199
265
  "path TEXT PRIMARY KEY, " +
200
266
  "checksum TEXT NOT NULL, " +
@@ -202,7 +268,6 @@ function buildStore(db) {
202
268
  "hashes TEXT NOT NULL, " +
203
269
  "updated_at INTEGER NOT NULL" +
204
270
  ")");
205
- db.exec("CREATE TABLE IF NOT EXISTS meta (" + "key TEXT PRIMARY KEY, " + "value TEXT NOT NULL" + ")");
206
271
  db.exec("CREATE TABLE IF NOT EXISTS undo (" +
207
272
  "path TEXT PRIMARY KEY, " +
208
273
  "content TEXT NOT NULL, " +
@@ -212,14 +277,8 @@ function buildStore(db) {
212
277
  "result_content TEXT NOT NULL, " +
213
278
  "updated_at INTEGER NOT NULL" +
214
279
  ")");
215
- const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'version'").get();
216
- const versionChanged = versionRow !== undefined && versionRow.value !== String(HASH_STORE_VERSION);
217
- if (versionChanged) {
218
- db.exec("DELETE FROM snapshots");
219
- db.exec("DELETE FROM undo");
220
- }
221
280
  const servedColumns = db.prepare("PRAGMA table_info(served)").all();
222
- if (versionChanged || !servedColumns.some((column) => column.name === "session_id")) {
281
+ if (!servedColumns.some((column) => column.name === "session_id")) {
223
282
  db.exec("DROP TABLE IF EXISTS served");
224
283
  }
225
284
  db.exec("CREATE TABLE IF NOT EXISTS served (" +
@@ -229,67 +288,97 @@ function buildStore(db) {
229
288
  "reported TEXT, " +
230
289
  "retired TEXT, " +
231
290
  "canons TEXT, " +
232
- "snapshotId TEXT, " +
233
291
  "cards TEXT, " +
234
292
  "updated_at INTEGER NOT NULL, " +
235
293
  "PRIMARY KEY (session_id, path)" +
236
294
  ")");
237
- const currentServedColumns = db.prepare("PRAGMA table_info(served)").all();
238
- if (!currentServedColumns.some((column) => column.name === "retired")) {
239
- let migrationOpen = false;
240
- try {
241
- db.exec("BEGIN IMMEDIATE");
242
- migrationOpen = true;
243
- const migrationColumns = db.prepare("PRAGMA table_info(served)").all();
244
- if (!migrationColumns.some((column) => column.name === "retired")) {
245
- db.exec("ALTER TABLE served ADD COLUMN retired TEXT");
246
- // Pre-fix snapshots and undo entries may already bind a remembered
247
- // anchor to the wrong position. Preserve served rows, but rebuild
248
- // every source that could restore the rebound anchor.
249
- db.exec("DELETE FROM snapshots");
250
- db.exec("DELETE FROM undo");
295
+ addColumnIfMissing(db, "served", "canons", "TEXT");
296
+ addColumnIfMissing(db, "served", "cards", "TEXT");
297
+ }
298
+ /**
299
+ * Fail-closed refusal shared by the pre-write fast path and the
300
+ * in-transaction re-check: a stamp newer than this build is never touched.
301
+ */
302
+ function assertNotNewer(stored, storePath) {
303
+ if (stored !== undefined && stored > HASH_STORE_VERSION) {
304
+ throw new DomainError("E_STORE_NEWER_VERSION", {
305
+ path: storePath,
306
+ storedVersion: stored,
307
+ supportedVersion: HASH_STORE_VERSION,
308
+ });
309
+ }
310
+ }
311
+ /**
312
+ * The single atomic forward migration. Called only when the stamp differs
313
+ * from HASH_STORE_VERSION (including an absent or non-integer stamp on a
314
+ * pre-versioning store). The sole writer of meta.version. CP1 has no data
315
+ * steps — later tickets add ordered, guarded data statements beside the stamp
316
+ * write inside the same transaction. The stamp is re-checked under the
317
+ * RESERVED lock: a newer writer that committed between the probe and
318
+ * BEGIN IMMEDIATE is refused here, so the stamp write can never silently
319
+ * downgrade a newer store.
320
+ */
321
+ function migrateForward(db, storePath) {
322
+ let migrationOpen = false;
323
+ try {
324
+ db.exec("BEGIN IMMEDIATE");
325
+ migrationOpen = true;
326
+ assertNotNewer(storedVersion(db), storePath);
327
+ db.prepare("INSERT INTO meta (key, value) VALUES ('version', ?) " +
328
+ "ON CONFLICT(key) DO UPDATE SET value = excluded.value").run(String(HASH_STORE_VERSION));
329
+ db.exec("COMMIT");
330
+ migrationOpen = false;
331
+ }
332
+ catch (error) {
333
+ if (migrationOpen) {
334
+ try {
335
+ db.exec("ROLLBACK");
251
336
  }
252
- db.exec("COMMIT");
253
- migrationOpen = false;
254
- }
255
- catch (error) {
256
- if (migrationOpen) {
257
- try {
258
- db.exec("ROLLBACK");
259
- }
260
- catch (rollbackError) {
261
- console.warn(rollbackError);
262
- }
337
+ catch (rollbackError) {
338
+ console.warn(rollbackError);
263
339
  }
264
- throw error;
265
340
  }
341
+ throw error;
266
342
  }
267
- const canonsColumns = db.prepare("PRAGMA table_info(served)").all();
268
- if (!canonsColumns.some((column) => column.name === "canons")) {
269
- db.exec("ALTER TABLE served ADD COLUMN canons TEXT");
270
- }
271
- const snapshotColumns = db.prepare("PRAGMA table_info(served)").all();
272
- if (!snapshotColumns.some((column) => column.name === "snapshotId")) {
273
- db.exec("ALTER TABLE served ADD COLUMN snapshotId TEXT");
274
- }
275
- const cardsColumns = db.prepare("PRAGMA table_info(served)").all();
276
- if (!cardsColumns.some((column) => column.name === "cards")) {
277
- db.exec("ALTER TABLE served ADD COLUMN cards TEXT");
278
- }
279
- db.prepare("INSERT INTO meta (key, value) VALUES ('version', ?) " +
280
- "ON CONFLICT(key) DO UPDATE SET value = excluded.value").run(String(HASH_STORE_VERSION));
281
- const getStmt = db.prepare("SELECT hashes FROM snapshots WHERE path = ? AND checksum = ? AND line_count = ?");
282
- const allStmt = db.prepare("SELECT path FROM snapshots UNION SELECT path FROM undo UNION SELECT path FROM served");
283
- const allHashesStmt = db.prepare("SELECT path, hashes FROM snapshots");
284
- const delStmt = db.prepare("DELETE FROM snapshots WHERE path = ?");
285
- const upsertStmt = db.prepare("INSERT INTO snapshots (path, checksum, line_count, hashes, updated_at) VALUES (?, ?, ?, ?, ?) " +
286
- "ON CONFLICT(path) DO UPDATE SET checksum = excluded.checksum, line_count = excluded.line_count, hashes = excluded.hashes, updated_at = excluded.updated_at");
343
+ }
344
+ function buildStore(db, storePath) {
345
+ const stored = storedVersion(db);
346
+ assertNotNewer(stored, storePath);
347
+ db.exec("PRAGMA journal_mode = WAL");
348
+ db.exec("PRAGMA synchronous = NORMAL");
349
+ // Explicit intent: node:sqlite already enables FK constraints by default.
350
+ db.exec("PRAGMA foreign_keys = ON");
351
+ ensureSchema(db);
352
+ if (stored !== HASH_STORE_VERSION)
353
+ migrateForward(db, storePath);
354
+ const currentServedColumns = db.prepare("PRAGMA table_info(served)").all();
355
+ if (!currentServedColumns.some((column) => column.name === "retired")) {
356
+ // Single idempotent ALTER needs no transaction: ALTER TABLE is atomic, and an
357
+ // upgrade must never destroy snapshot/undo rows (ADR-0017 retires the wipe).
358
+ db.exec("ALTER TABLE served ADD COLUMN retired TEXT");
359
+ }
360
+ const allStmt = db.prepare("SELECT path FROM snapshots UNION SELECT path FROM undo UNION SELECT path FROM served UNION SELECT path FROM file_undo UNION SELECT path FROM file_snapshots UNION SELECT path FROM line_id_counters UNION SELECT file_path FROM served_leases");
287
361
  const undoUpsertStmt = db.prepare("INSERT INTO undo (path, content, bom, ending, hashes, result_content, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?) " +
288
362
  "ON CONFLICT(path) DO UPDATE SET content = excluded.content, bom = excluded.bom, ending = excluded.ending, hashes = excluded.hashes, result_content = excluded.result_content, updated_at = excluded.updated_at");
289
363
  const undoGetStmt = db.prepare("SELECT content, bom, ending, hashes, result_content FROM undo WHERE path = ?");
290
364
  const undoDelStmt = db.prepare("DELETE FROM undo WHERE path = ?");
291
- const undoPruneOlderThanStmt = db.prepare("DELETE FROM undo WHERE updated_at < ?");
292
- const servedGetStmt = db.prepare("SELECT hashes, reported, retired, canons, snapshotId, cards FROM served WHERE session_id = ? AND path = ?");
365
+ // Pair-atomic age prune: a path's pair is pruned only when its NEWEST side is older
366
+ // than the cutoff — a recently written side keeps the pair alive. Decide per path,
367
+ // never one side alone (independent per-table deletes split the pair, F1 class).
368
+ // Each statement is a complete SQL literal — the predicate text is duplicated on purpose:
369
+ // a shared constant would be interpolated back into the SQL, so no future edit can turn
370
+ // this into an interpolation hole.
371
+ const undoPrunePairStmt = db.prepare("DELETE FROM undo WHERE path IN (SELECT path FROM (SELECT path, MAX(updated_at) AS newest FROM (" +
372
+ "SELECT path, updated_at FROM undo UNION ALL SELECT path, updated_at FROM file_undo" +
373
+ ") GROUP BY path) WHERE newest < ?)");
374
+ const fileUndoPrunePairStmt = db.prepare("DELETE FROM file_undo WHERE path IN (SELECT path FROM (SELECT path, MAX(updated_at) AS newest FROM (" +
375
+ "SELECT path, updated_at FROM undo UNION ALL SELECT path, updated_at FROM file_undo" +
376
+ ") GROUP BY path) WHERE newest < ?)");
377
+ const fileUndoUpsertStmt = db.prepare("INSERT INTO file_undo (path, content, bom, ending, hashes, result_content, snapshot_hash, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) " +
378
+ "ON CONFLICT(path) DO UPDATE SET content = excluded.content, bom = excluded.bom, ending = excluded.ending, hashes = excluded.hashes, result_content = excluded.result_content, snapshot_hash = excluded.snapshot_hash, updated_at = excluded.updated_at");
379
+ const fileUndoGetStmt = db.prepare("SELECT content, bom, ending, hashes, result_content, snapshot_hash, updated_at FROM file_undo WHERE path = ?");
380
+ const fileUndoDelStmt = db.prepare("DELETE FROM file_undo WHERE path = ?");
381
+ const servedGetStmt = db.prepare("SELECT hashes, reported, retired, canons, cards FROM served WHERE session_id = ? AND path = ?");
293
382
  const servedAllForPathStmt = db.prepare("SELECT session_id, hashes, retired FROM served WHERE path = ?");
294
383
  const servedUpsertStmt = db.prepare("INSERT INTO served (session_id, path, hashes, updated_at) VALUES (?, ?, ?, ?) " +
295
384
  "ON CONFLICT(session_id, path) DO UPDATE SET hashes = excluded.hashes, updated_at = excluded.updated_at");
@@ -302,9 +391,6 @@ function buildStore(db) {
302
391
  const servedCanonsUpsertStmt = db.prepare("INSERT INTO served (session_id, path, hashes, canons, updated_at) VALUES (?, ?, '[]', ?, ?) " +
303
392
  "ON CONFLICT(session_id, path) DO UPDATE SET canons = excluded.canons, updated_at = excluded.updated_at");
304
393
  const servedCanonsClearStmt = db.prepare("UPDATE served SET canons = NULL, updated_at = ? WHERE session_id = ? AND path = ?");
305
- const servedSnapshotUpsertStmt = db.prepare("INSERT INTO served (session_id, path, hashes, snapshotId, updated_at) VALUES (?, ?, '[]', ?, ?) " +
306
- "ON CONFLICT(session_id, path) DO UPDATE SET snapshotId = excluded.snapshotId, updated_at = excluded.updated_at");
307
- const servedSnapshotClearStmt = db.prepare("UPDATE served SET snapshotId = NULL, updated_at = ? WHERE session_id = ? AND path = ?");
308
394
  const servedCardsUpsertStmt = db.prepare("INSERT INTO served (session_id, path, hashes, cards, updated_at) VALUES (?, ?, '[]', ?, ?) " +
309
395
  "ON CONFLICT(session_id, path) DO UPDATE SET cards = excluded.cards, updated_at = excluded.updated_at");
310
396
  const servedCardsClearStmt = db.prepare("UPDATE served SET cards = NULL, updated_at = ? WHERE session_id = ? AND path = ?");
@@ -313,33 +399,40 @@ function buildStore(db) {
313
399
  const servedWipeStmt = db.prepare("DELETE FROM served WHERE session_id = ?");
314
400
  const servedPruneOlderThanStmt = db.prepare("DELETE FROM served WHERE updated_at < ?");
315
401
  const stmts = {
316
- get: (...params) => getStmt.get(...params),
317
402
  allPaths: (...params) => allStmt.all(...params),
318
- allHashes: (...params) => allHashesStmt.all(...params),
319
- deleteOne: (...params) => {
403
+ undoUpsert: (...params) => {
320
404
  withBusyRetry(() => {
321
- delStmt.run(...params);
405
+ undoUpsertStmt.run(...params);
322
406
  });
323
407
  },
324
- upsert: (...params) => {
408
+ undoGet: (...params) => undoGetStmt.get(...params),
409
+ undoDelete: (...params) => {
325
410
  withBusyRetry(() => {
326
- upsertStmt.run(...params);
411
+ undoDelStmt.run(...params);
327
412
  });
328
413
  },
329
- undoUpsert: (...params) => {
414
+ undoPrunePair: (...params) => {
330
415
  withBusyRetry(() => {
331
- undoUpsertStmt.run(...params);
416
+ undoPrunePairStmt.run(...params);
332
417
  });
333
418
  },
334
- undoGet: (...params) => undoGetStmt.get(...params),
335
- undoDelete: (...params) => {
419
+ fileUndoPrunePair: (...params) => {
336
420
  withBusyRetry(() => {
337
- undoDelStmt.run(...params);
421
+ fileUndoPrunePairStmt.run(...params);
422
+ });
423
+ },
424
+ fileUndoUpsert: (...params) => {
425
+ withBusyRetry(() => {
426
+ fileUndoUpsertStmt.run(...params);
338
427
  });
339
428
  },
340
- undoPruneOlderThan: (...params) => {
429
+ fileUndoGet: (...params) => {
430
+ const row = fileUndoGetStmt.get(...params);
431
+ return row === undefined ? undefined : mapFileUndoRow(row);
432
+ },
433
+ fileUndoDelete: (...params) => {
341
434
  withBusyRetry(() => {
342
- undoPruneOlderThanStmt.run(...params);
435
+ fileUndoDelStmt.run(...params);
343
436
  });
344
437
  },
345
438
  servedGet: (...params) => servedGetStmt.get(...params),
@@ -379,16 +472,6 @@ function buildStore(db) {
379
472
  servedCanonsClearStmt.run(params[1], params[0], params[2]);
380
473
  });
381
474
  },
382
- servedSnapshotUpsert: (...params) => {
383
- withBusyRetry(() => {
384
- servedSnapshotUpsertStmt.run(...params);
385
- });
386
- },
387
- servedSnapshotClear: (...params) => {
388
- withBusyRetry(() => {
389
- servedSnapshotClearStmt.run(params[1], params[0], params[2]);
390
- });
391
- },
392
475
  servedCardsUpsert: (...params) => {
393
476
  withBusyRetry(() => {
394
477
  servedCardsUpsertStmt.run(...params);
@@ -423,57 +506,135 @@ function buildStore(db) {
423
506
  return { db, stmts };
424
507
  }
425
508
  /** Wire the domain methods over the prepared statements. */
426
- function makeDomainStore(stmts) {
509
+ function makeDomainStore(stmts, snapshotStore, lineageStore, db) {
510
+ /**
511
+ * The post-materialization retention trigger. Runs the sweep after a v7 materialization has
512
+ * committed — outside that materialization's own `withTransaction` block — and reports the result
513
+ * through the sweep's single report owner. The deferral itself lives in `vacuumSnapshots` (TM
514
+ * probe M1: a joined sweep can be partially committed by its caller), so this site is safe from
515
+ * any caller-owned unit by construction and only decides whether there is a v7 row to protect:
516
+ * `content === undefined` means the legacy row alone, with no `file_snapshots` row to keep.
517
+ *
518
+ * Best-effort but never silent: a retention fault can never fail the read or edit that already
519
+ * committed, and it is reported observably (spec §3.6.1; the storage-error-transparency rule).
520
+ * The just-committed row is protected because its lease does not exist yet.
521
+ */
522
+ function vacuumAfterMaterialization(path, content) {
523
+ if (content === undefined)
524
+ return;
525
+ try {
526
+ const protectId = lineageStore.snapshotIdFor(path, snapshotHashFor(content));
527
+ const result = runVacuumSnapshots(db, protectId === undefined ? {} : { protectSnapshotIds: [protectId] });
528
+ reportVacuum(result, `materialize ${path}`);
529
+ }
530
+ catch (error) {
531
+ console.warn(`dsh-better-edit: snapshot vacuum failed after materializing ${path}: ${error instanceof Error ? error.message : String(error)}`);
532
+ }
533
+ }
534
+ /**
535
+ * Transaction owner for the undo pair: the store's own `db` handle (opened in
536
+ * buildStore). Delegates to the single re-entrant owner so an undo-pair write
537
+ * joins an outer unit (e.g. pruneMissing) instead of nesting a BEGIN.
538
+ */
539
+ function withUndoPairTxn(fn) {
540
+ withTransaction(db, fn);
541
+ }
542
+ function writeUndoPairImpl(path, legacy, v7) {
543
+ // Contract: at least one side must be present (both-absent is deleteUndoPair's job).
544
+ if (!legacy && !v7) {
545
+ throw new TypeError("writeUndoPair requires at least one side; use deleteUndoPair to clear the pair");
546
+ }
547
+ // One stamp for both rows: the pair's age is a pair property. The old code stamped
548
+ // legacy from the store clock and v7 from the caller's updatedAt, so a straddled
549
+ // millisecond split the pair at prune time. The impl is the single clock now; the
550
+ // caller's updatedAt is advisory and intentionally unused.
551
+ const stamp = Date.now();
552
+ withUndoPairTxn(() => {
553
+ if (legacy) {
554
+ stmts.undoUpsert(path, legacy.content, legacy.bom, legacy.ending, JSON.stringify(legacy.hashes), legacy.resultContent, stamp);
555
+ }
556
+ else {
557
+ stmts.undoDelete(path);
558
+ }
559
+ if (v7) {
560
+ stmts.fileUndoUpsert(path, v7.content, v7.bom, v7.ending, JSON.stringify(v7.hashes), v7.resultContent, v7.snapshotHash, stamp);
561
+ }
562
+ else {
563
+ stmts.fileUndoDelete(path);
564
+ }
565
+ });
566
+ }
567
+ function undoPairDeleteImpl(path) {
568
+ withUndoPairTxn(() => {
569
+ stmts.undoDelete(path);
570
+ stmts.fileUndoDelete(path);
571
+ });
572
+ }
427
573
  return {
428
574
  engine: "node:sqlite",
429
575
  getSnapshot(path, content, deleteCorrupt = true) {
430
- const checksum = cacheKey(contentChecksum(content));
431
- const lineCount = splitLines(content).length;
432
- const row = stmts.get(path, checksum, lineCount);
433
- if (!row)
434
- return undefined;
435
- try {
436
- const parsed = JSON.parse(row.hashes);
437
- if (isValidHashList(parsed))
438
- return parsed;
439
- if (deleteCorrupt)
440
- stmts.deleteOne(path);
441
- return undefined;
442
- }
443
- catch (error) {
444
- if (deleteCorrupt)
445
- stmts.deleteOne(path);
446
- return undefined;
576
+ // Lineage first: adopt refreshes stored anchors to the live assignment, so the
577
+ // lineage tracks the current snapshot and the legacy row stays the upgrade fallback.
578
+ const lineage = lineageStore.lineageFor(path, snapshotHashFor(content));
579
+ // A lineage hit shields a corrupt legacy row from healing by design —
580
+ // fallback-only healing is fail-closed (ADR-0017).
581
+ if (lineage.length === splitLines(content).length &&
582
+ lineage.every((row, index) => row.lineNumber === index + 1 && HASH_RE.test(row.anchor))) {
583
+ return lineage.map((row) => row.anchor);
447
584
  }
585
+ return snapshotStore.get(path, content, deleteCorrupt);
586
+ },
587
+ upsertSnapshot(path, checksum, lineCount, hashes, content) {
588
+ // Pair #5: the legacy `snapshots` row and the v7 family are ONE unit. A v7
589
+ // fault (or a crash) must not leave the legacy row written and v7 absent.
590
+ withTransaction(db, () => {
591
+ snapshotStore.upsert(path, checksum, lineCount, hashes);
592
+ if (content !== undefined) {
593
+ lineageStore.commitSnapshot({ path, content, hashes });
594
+ }
595
+ });
596
+ vacuumAfterMaterialization(path, content);
597
+ },
598
+ commitSnapshot(input) {
599
+ lineageStore.commitSnapshot(input);
600
+ vacuumAfterMaterialization(input.path, input.content);
601
+ },
602
+ lineageFor(path, snapshotHash) {
603
+ return lineageStore.lineageFor(path, snapshotHash);
448
604
  },
449
- upsertSnapshot(path, checksum, lineCount, hashes) {
450
- stmts.upsert(path, cacheKey(checksum), lineCount, JSON.stringify(hashes), Date.now());
605
+ positionsByIdentity(path, content) {
606
+ return lineageStore.positionsByIdentity(path, content);
607
+ },
608
+ leaseFor(sessionKey, path, anchor) {
609
+ return lineageStore.leaseFor(sessionKey, path, anchor);
610
+ },
611
+ leaseHomes(sessionKey, excludePath, anchor) {
612
+ return lineageStore.leaseHomes(sessionKey, excludePath, anchor);
613
+ },
614
+ snapshotIdFor(path, snapshotHash) {
615
+ return lineageStore.snapshotIdFor(path, snapshotHash);
616
+ },
617
+ /**
618
+ * The `LineageStore` seam member — LINEAGE FAMILY ONLY (file_snapshots, line_lineage,
619
+ * line_id_counters, served_leases). It is deliberately narrower than `pruneMissing`,
620
+ * which is the sole owner of whole-path deletion and calls the sub-stores directly.
621
+ * The name reads as "delete everything for this path"; it does not, and widening it
622
+ * here would duplicate pruneMissing's ownership.
623
+ */
624
+ deleteByPath(path) {
625
+ lineageStore.deleteByPath(path);
451
626
  },
452
627
  allKnownPaths() {
453
628
  return stmts.allPaths();
454
629
  },
455
630
  allSnapshotHashes() {
456
- return stmts.allHashes();
631
+ return snapshotStore.allHashes();
457
632
  },
458
633
  deleteSnapshot(path) {
459
- stmts.deleteOne(path);
634
+ snapshotStore.deleteByPath(path);
460
635
  },
461
636
  findSnapshotPaths(hashes) {
462
- const rows = stmts.allHashes();
463
- const matches = [];
464
- for (const row of rows) {
465
- try {
466
- const parsed = JSON.parse(row.hashes);
467
- if (!isValidHashList(parsed))
468
- continue;
469
- if (hashes.every((h) => parsed.includes(h)))
470
- matches.push(row.path);
471
- }
472
- catch (error) {
473
- console.warn(error); // unparseable row → skip it
474
- }
475
- }
476
- return matches;
637
+ return snapshotStore.findPathsContaining(hashes);
477
638
  },
478
639
  getUndo(path) {
479
640
  const row = stmts.undoGet(path);
@@ -482,7 +643,7 @@ function makeDomainStore(stmts) {
482
643
  try {
483
644
  const parsed = JSON.parse(row.hashes);
484
645
  if (!isValidHashList(parsed)) {
485
- stmts.undoDelete(path);
646
+ undoPairDeleteImpl(path);
486
647
  return undefined;
487
648
  }
488
649
  return {
@@ -494,15 +655,51 @@ function makeDomainStore(stmts) {
494
655
  };
495
656
  }
496
657
  catch (error) {
497
- stmts.undoDelete(path);
658
+ undoPairDeleteImpl(path);
659
+ return undefined;
660
+ }
661
+ },
662
+ getFileUndo(path) {
663
+ // The SELECT is inside the try: `stmts.fileUndoGet` validates the raw row
664
+ // (`mapFileUndoRow`) and throws on a shape-corrupt row, which must take the
665
+ // same healing path as `getUndo`'s parse failure below.
666
+ try {
667
+ const row = stmts.fileUndoGet(path);
668
+ if (!row)
669
+ return undefined;
670
+ const parsed = JSON.parse(row.hashes);
671
+ if (!isValidHashList(parsed)) {
672
+ undoPairDeleteImpl(path);
673
+ return undefined;
674
+ }
675
+ return {
676
+ content: row.content,
677
+ bom: row.bom,
678
+ ending: row.ending,
679
+ hashes: parsed,
680
+ resultContent: row.result_content,
681
+ snapshotHash: row.snapshot_hash ?? null,
682
+ updatedAt: row.updated_at,
683
+ };
684
+ }
685
+ catch (error) {
686
+ // Shape corruption (`mapFileUndoRow` throws `TypeError`) and payload corruption
687
+ // (`JSON.parse` throws `SyntaxError`) heal; infrastructure failures (BUSY, IO)
688
+ // must propagate, because swallowing one would delete a live pair and let
689
+ // `saveUndo` report `persisted: true` — undo silently destroyed, caller told it
690
+ // succeeded. Fail loud, never fail silent.
691
+ const healable = error instanceof TypeError || error instanceof SyntaxError;
692
+ if (!healable)
693
+ throw error;
694
+ undoPairDeleteImpl(path);
498
695
  return undefined;
499
696
  }
500
697
  },
501
- upsertUndo(path, entry) {
502
- stmts.undoUpsert(path, entry.content, entry.bom, entry.ending, JSON.stringify(entry.hashes), entry.resultContent, Date.now());
698
+ writeUndoPair(path, legacy, v7) {
699
+ writeUndoPairImpl(path, legacy, v7);
503
700
  },
504
- deleteUndo(path) {
505
- stmts.undoDelete(path);
701
+ deleteUndoPair(path) {
702
+ undoPairDeleteImpl(path);
506
703
  },
507
704
  getServed(sessionKey, path) {
508
705
  const row = stmts.servedGet(sessionKey, path);
@@ -619,24 +816,12 @@ function makeDomainStore(stmts) {
619
816
  return [];
620
817
  }
621
818
  },
622
- getEpochSnapshotId(sessionKey, path) {
623
- const row = stmts.servedGet(sessionKey, path);
624
- if (!row || row.snapshotId === null || row.snapshotId === undefined)
625
- return undefined;
626
- return row.snapshotId;
627
- },
628
819
  upsertServedCanons(sessionKey, path, canonsJson) {
629
820
  stmts.servedCanonsUpsert(sessionKey, path, canonsJson, Date.now());
630
821
  },
631
822
  clearServedCanons(sessionKey, path) {
632
823
  stmts.servedCanonsClear(sessionKey, Date.now(), path);
633
824
  },
634
- upsertEpochSnapshotId(sessionKey, path, snapshotId) {
635
- stmts.servedSnapshotUpsert(sessionKey, path, snapshotId, Date.now());
636
- },
637
- clearEpochSnapshotId(sessionKey, path) {
638
- stmts.servedSnapshotClear(sessionKey, Date.now(), path);
639
- },
640
825
  getCards(sessionKey, path) {
641
826
  const row = stmts.servedGet(sessionKey, path);
642
827
  if (!row || row.cards === null || row.cards === undefined)
@@ -670,21 +855,33 @@ function makeDomainStore(stmts) {
670
855
  stmts.servedPruneOlderThan(ts);
671
856
  },
672
857
  pruneUndoOlderThan(ts) {
673
- stmts.undoPruneOlderThan(ts);
858
+ withUndoPairTxn(() => {
859
+ stmts.undoPrunePair(ts);
860
+ stmts.fileUndoPrunePair(ts);
861
+ });
674
862
  },
675
863
  async pruneMissing() {
676
864
  const rows = stmts.allPaths();
677
865
  const missing = await statMissing(rows);
678
866
  if (missing.length === 0)
679
867
  return;
868
+ // Transaction owner: withStore — one unit for the legacy rows and the v7 family.
680
869
  withStore(() => {
681
870
  for (const path of missing) {
682
- stmts.deleteOne(path);
871
+ snapshotStore.deleteByPath(path);
872
+ lineageStore.deleteByPath(path);
683
873
  stmts.undoDelete(path);
874
+ stmts.fileUndoDelete(path);
684
875
  stmts.servedDeletePath(path);
685
876
  }
686
877
  });
687
878
  },
879
+ vacuumSnapshots() {
880
+ // WHY: the store-open boundary (spec §3.6.1) — on this store the open path is the only place
881
+ // that can reclaim a store that crashed over budget. The result goes back to the caller,
882
+ // which is the report owner; the sweep itself never writes to the console.
883
+ return runVacuumSnapshots(db);
884
+ },
688
885
  };
689
886
  }
690
887
  function isHealthy(db) {
@@ -766,9 +963,11 @@ async function openStore(storePath) {
766
963
  }
767
964
  const { db, stmts } = opened;
768
965
  if (!existed) {
769
- await migrateLegacy(db, storePath);
966
+ await migrateLegacyStore(db, join(dirname(storePath), "hash-store.json"));
770
967
  }
771
- const store = makeDomainStore(stmts);
968
+ const snapshotStore = createSnapshotStore(db);
969
+ const lineageStore = createLineageStore(db);
970
+ const store = makeDomainStore(stmts, snapshotStore, lineageStore, db);
772
971
  stores.set(storePath, { path: storePath, db, stmts, store });
773
972
  await onStoreOpen(storePath, stmts, store);
774
973
  return store;
@@ -814,91 +1013,18 @@ export function shutdownHashStore() {
814
1013
  /**
815
1014
  * Run `fn` inside one BEGIN IMMEDIATE transaction on the active workspace's
816
1015
  * store. Without an open store for this context the call runs bare (the
817
- * caller has already loaded the store in every in-process path).
1016
+ * caller has already loaded the store in every in-process path). Re-entrant:
1017
+ * when a store already has a transaction open, `fn` joins it.
818
1018
  */
819
1019
  export function withStore(fn) {
820
1020
  const store = currentStore();
821
1021
  if (store) {
822
- withBusyRetry(() => {
823
- store.db.exec("BEGIN IMMEDIATE");
824
- try {
825
- fn();
826
- store.db.exec("COMMIT");
827
- }
828
- catch (e) {
829
- try {
830
- store.db.exec("ROLLBACK");
831
- }
832
- catch (error) {
833
- console.warn(error); // best-effort rollback; the original error propagates
834
- }
835
- throw e;
836
- }
837
- });
1022
+ withTransaction(store.db, fn);
838
1023
  }
839
1024
  else {
840
1025
  fn();
841
1026
  }
842
1027
  }
843
- async function migrateLegacy(db, storePath) {
844
- const legacyPath = join(dirname(storePath), "hash-store.json");
845
- let content;
846
- try {
847
- content = await readFile(legacyPath, "utf-8");
848
- }
849
- catch (error) {
850
- if (errCode(error) === "ENOENT")
851
- return;
852
- console.error("Failed to read legacy hash store for migration:", error);
853
- return;
854
- }
855
- let parsed;
856
- try {
857
- parsed = JSON.parse(content);
858
- }
859
- catch (error) {
860
- console.error("Failed to parse legacy hash store, skipping migration:", error);
861
- return;
862
- }
863
- const raw = parsed.snapshots;
864
- if (!raw || typeof raw !== "object" || Array.isArray(raw))
865
- return;
866
- const rows = [];
867
- for (const [key, value] of Object.entries(raw)) {
868
- if (!isValidSnapshot(value))
869
- continue;
870
- if (new Set(value.hashes).size !== value.hashes.length) {
871
- console.warn(`Skipped legacy snapshot with duplicate hashes for ${key}; it will be re-hashed on next read.`);
872
- continue;
873
- }
874
- rows.push([
875
- key,
876
- contentChecksum(value.content),
877
- splitLines(value.content).length,
878
- JSON.stringify(value.hashes),
879
- Date.now(),
880
- ]);
881
- }
882
- if (rows.length > 0) {
883
- db.exec("BEGIN IMMEDIATE");
884
- try {
885
- const stmt = db.prepare("INSERT OR REPLACE INTO snapshots (path, checksum, line_count, hashes, updated_at) VALUES (?, ?, ?, ?, ?)");
886
- for (const row of rows)
887
- stmt.run(...row);
888
- db.exec("COMMIT");
889
- }
890
- catch (e) {
891
- db.exec("ROLLBACK");
892
- throw e;
893
- }
894
- }
895
- try {
896
- await rename(legacyPath, `${legacyPath}.bak`);
897
- }
898
- catch (error) {
899
- console.error("Failed to rename legacy hash store after migration:", error);
900
- }
901
- }
902
1028
  // ---- async convenience helpers (load the active store, then delegate) ------
903
1029
  /** Find files whose stored snapshot hashes contain every given anchor. */
904
1030
  export async function findSnapshotPathsByHashes(hashes) {
@@ -906,7 +1032,7 @@ export async function findSnapshotPathsByHashes(hashes) {
906
1032
  return store.findSnapshotPaths(hashes);
907
1033
  }
908
1034
  /** Persist a hash snapshot for one path (async over the active store). */
909
- export async function upsertSnapshotFor(path, checksum, lineCount, hashes) {
1035
+ export async function upsertSnapshotFor(path, checksum, lineCount, hashes, content) {
910
1036
  const store = await loadHashStore();
911
- store.upsertSnapshot(path, checksum, lineCount, hashes);
1037
+ store.upsertSnapshot(path, checksum, lineCount, hashes, content);
912
1038
  }