dsh-better-edit 0.8.1 → 1.0.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 +162 -97
- package/README.md +2 -2
- package/README.zh.md +3 -3
- package/cordis.patch.yml +1 -1
- package/lib/constants.js +19 -4
- package/lib/contract.js +136 -79
- package/lib/domain-errors.js +570 -0
- package/lib/edit-diff.js +33 -7
- package/lib/file-encoding-state.js +27 -7
- package/lib/file-view.js +305 -82
- package/lib/fs-bridge.js +23 -7
- package/lib/hash-store.js +404 -278
- package/lib/hashline/anchor-pipeline.js +586 -378
- package/lib/hashline/hash-assign.js +21 -22
- package/lib/hashline/hash.js +6 -6
- package/lib/hashline/index.js +1 -1
- package/lib/hashline/resolve.js +1 -1
- package/lib/hashline/served-guard.js +181 -0
- package/lib/index.js +2 -2
- package/lib/mutation/engine.js +392 -96
- package/lib/mutation.js +22 -130
- package/lib/noop-guard.js +20 -9
- package/lib/prompts.js +20 -18
- package/lib/read-and-serve.js +44 -20
- package/lib/sandbox.js +1 -1
- package/lib/session-view.js +57 -43
- package/lib/snapshot-store/index.js +97 -0
- package/lib/snapshot-store/lineage-store.js +400 -0
- package/lib/snapshot-store/migrate.js +80 -0
- package/lib/snapshot-store/pairing.js +412 -0
- package/lib/snapshot-store/txn.js +63 -0
- package/lib/snapshot-store/vacuum.js +243 -0
- package/lib/store-lifecycle.js +13 -1
- package/lib/store-retry.js +34 -0
- package/lib/tool-edit.js +90 -44
- package/lib/tool-read.js +29 -1
- package/lib/tool-str-replace-editor.js +59 -24
- package/lib/tool-undo.js +25 -8
- package/lib/types/constants.d.ts +5 -2
- package/lib/types/contract.d.ts +76 -71
- package/lib/types/domain-errors.d.ts +410 -0
- package/lib/types/edit-response.d.ts +1 -1
- package/lib/types/file-encoding-state.d.ts +1 -0
- package/lib/types/file-view.d.ts +35 -6
- package/lib/types/hash-store.d.ts +38 -19
- package/lib/types/hashline/anchor-pipeline.d.ts +117 -36
- package/lib/types/hashline/hash-assign.d.ts +5 -5
- package/lib/types/hashline/hash.d.ts +1 -1
- package/lib/types/hashline/index.d.ts +2 -2
- package/lib/types/hashline/served-guard.d.ts +100 -0
- package/lib/types/index.d.ts +2 -2
- package/lib/types/mutation/engine.d.ts +49 -30
- package/lib/types/mutation.d.ts +1 -39
- package/lib/types/noop-guard.d.ts +4 -4
- package/lib/types/prompts.d.ts +1 -1
- package/lib/types/read-and-serve.d.ts +6 -0
- package/lib/types/sandbox.d.ts +1 -1
- package/lib/types/session-view.d.ts +38 -9
- package/lib/types/snapshot-store/index.d.ts +28 -0
- package/lib/types/snapshot-store/lineage-store.d.ts +96 -0
- package/lib/types/snapshot-store/migrate.d.ts +16 -0
- package/lib/types/snapshot-store/pairing.d.ts +99 -0
- package/lib/types/snapshot-store/txn.d.ts +12 -0
- package/lib/types/snapshot-store/vacuum.d.ts +79 -0
- package/lib/types/store-lifecycle.d.ts +0 -1
- package/lib/types/store-retry.d.ts +6 -0
- package/lib/types/tool-edit.d.ts +2 -2
- package/lib/types/tool-str-replace-editor.d.ts +10 -4
- package/lib/types/utils.d.ts +5 -6
- package/lib/types/workspace-context.d.ts +0 -1
- package/lib/undo-edit.js +27 -11
- package/lib/utils.js +12 -12
- package/lib/workspace-context.js +4 -8
- package/lib/write-hook.js +13 -4
- package/package.json +9 -10
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v7 identity substrate: the single owner of `file_snapshots`, `line_lineage`,
|
|
3
|
+
* `line_id_counters` and the `served_leases` write path. Co-location is
|
|
4
|
+
* deliberate: a snapshot, its lineage and the leases bound to it commit as one
|
|
5
|
+
* unit, so a crash can never leave leases pointing at a snapshot that was
|
|
6
|
+
* never recorded (or lineage without its snapshot row).
|
|
7
|
+
*
|
|
8
|
+
* Transaction semantics: `commitSnapshot` runs adopt-or-insert plus the lease
|
|
9
|
+
* grant inside the store's single re-entrant transaction owner
|
|
10
|
+
* (`snapshot-store/txn.ts`). Outermost it opens ONE `BEGIN IMMEDIATE`; nested it
|
|
11
|
+
* joins the caller's unit, so a failure rolls the whole unit back and propagates.
|
|
12
|
+
* Retry policy: the owner retries only the BEGIN/COMMIT acquisitions, so every
|
|
13
|
+
* `.run` below carries its own `withBusyRetry` — matching `hash-store`'s `stmts`
|
|
14
|
+
* wrappers and `snapshot-store/index.ts`. A transient SQLITE_BUSY on any lineage
|
|
15
|
+
* statement retries instead of aborting the unit.
|
|
16
|
+
*
|
|
17
|
+
* Pairing rule (deterministic): a new snapshot's lines inherit `line_id`s from the path's
|
|
18
|
+
* latest committed snapshot via the scaled recursive patience engine in `./pairing.js` —
|
|
19
|
+
* locally unique canons pin, the minimal-displacement LIS backbone pairs, pinless rigid
|
|
20
|
+
* runs zip positionally, and a bounded leaf interval pairs only on a UNIQUE optimal LCS
|
|
21
|
+
* embedding. Anything the engine cannot prove moved — a duplicate canon, a contested
|
|
22
|
+
* reorder, an over-budget interval — pairs nothing and takes a fresh id. That is
|
|
23
|
+
* fail-closed on purpose: a guessed inheritance would rebind a lease onto a look-alike
|
|
24
|
+
* line. No previous snapshot for the path → every line fresh.
|
|
25
|
+
*
|
|
26
|
+
* @module dsh-better-edit/snapshot-store/lineage-store
|
|
27
|
+
*/
|
|
28
|
+
import { DatabaseSync } from "node:sqlite";
|
|
29
|
+
import { CANON_VERSION, canonDigest, contentChecksum } from "../hashline/hash-assign.js";
|
|
30
|
+
import { splitLines } from "../utils.js";
|
|
31
|
+
import { withBusyRetry } from "../store-retry.js";
|
|
32
|
+
import { pairSnapshots } from "./pairing.js";
|
|
33
|
+
import { withTransaction } from "./txn.js";
|
|
34
|
+
export function snapshotHashFor(content) {
|
|
35
|
+
return `${CANON_VERSION}:${contentChecksum(content)}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A snapshot row whose lineage is missing or malformed. The adopt arm *diagnoses* with this
|
|
39
|
+
* (CP2): an unusable family is discarded and re-materialized fresh, observably, instead of
|
|
40
|
+
* throwing — a permanent throw is what pinned a path into a lease-less state.
|
|
41
|
+
*/
|
|
42
|
+
export class LineageCorruptError extends Error {
|
|
43
|
+
constructor(message) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = "LineageCorruptError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Canonical DDL for the four lineage-owned tables (IF NOT EXISTS, additive). */
|
|
49
|
+
export function ensureLineageTables(db) {
|
|
50
|
+
db.exec("CREATE TABLE IF NOT EXISTS file_snapshots (" +
|
|
51
|
+
"snapshot_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
|
52
|
+
"path TEXT NOT NULL, " +
|
|
53
|
+
"snapshot_hash TEXT NOT NULL, " +
|
|
54
|
+
"line_count INTEGER NOT NULL, " +
|
|
55
|
+
"created_at INTEGER NOT NULL, " +
|
|
56
|
+
"committed INTEGER NOT NULL DEFAULT 1, " +
|
|
57
|
+
"UNIQUE (path, snapshot_hash)" +
|
|
58
|
+
")");
|
|
59
|
+
db.exec("CREATE INDEX IF NOT EXISTS idx_snapshots_created ON file_snapshots (created_at)");
|
|
60
|
+
db.exec("CREATE TABLE IF NOT EXISTS line_id_counters (" +
|
|
61
|
+
"path TEXT PRIMARY KEY, " +
|
|
62
|
+
"next_id INTEGER NOT NULL" +
|
|
63
|
+
")");
|
|
64
|
+
db.exec("CREATE TABLE IF NOT EXISTS line_lineage (" +
|
|
65
|
+
"snapshot_id INTEGER NOT NULL, " +
|
|
66
|
+
"line_number INTEGER NOT NULL, " +
|
|
67
|
+
"line_id INTEGER NOT NULL, " +
|
|
68
|
+
"canon_hash TEXT NOT NULL, " +
|
|
69
|
+
"anchor TEXT NOT NULL, " +
|
|
70
|
+
"PRIMARY KEY (snapshot_id, line_number), " +
|
|
71
|
+
"FOREIGN KEY (snapshot_id) REFERENCES file_snapshots(snapshot_id) ON DELETE CASCADE" +
|
|
72
|
+
")");
|
|
73
|
+
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_lineage_snapshot_line_id " +
|
|
74
|
+
"ON line_lineage (snapshot_id, line_id)");
|
|
75
|
+
db.exec("CREATE TABLE IF NOT EXISTS served_leases (" +
|
|
76
|
+
"session_id TEXT NOT NULL, " +
|
|
77
|
+
"file_path TEXT NOT NULL, " +
|
|
78
|
+
"anchor TEXT NOT NULL, " +
|
|
79
|
+
"line_id INTEGER NOT NULL, " +
|
|
80
|
+
"canon_hash TEXT NOT NULL, " +
|
|
81
|
+
"served_snapshot_hash TEXT NOT NULL, " +
|
|
82
|
+
"served_line_number INTEGER NOT NULL, " +
|
|
83
|
+
"updated_at INTEGER NOT NULL, " +
|
|
84
|
+
"retired_at INTEGER, " +
|
|
85
|
+
"PRIMARY KEY (session_id, file_path, anchor)" +
|
|
86
|
+
")");
|
|
87
|
+
db.exec("CREATE INDEX IF NOT EXISTS idx_leases_line " +
|
|
88
|
+
"ON served_leases (session_id, file_path, line_id)");
|
|
89
|
+
db.exec("CREATE INDEX IF NOT EXISTS idx_leases_line_num " +
|
|
90
|
+
"ON served_leases (session_id, file_path, served_line_number)");
|
|
91
|
+
db.exec("CREATE INDEX IF NOT EXISTS idx_leases_file_retired " +
|
|
92
|
+
"ON served_leases (file_path, retired_at)");
|
|
93
|
+
db.exec("CREATE INDEX IF NOT EXISTS idx_leases_session_anchor " +
|
|
94
|
+
"ON served_leases (session_id, anchor)");
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Adapter: the patience/LIS engine (`./pairing.js`) in the shape `commitSnapshot` needs.
|
|
98
|
+
* Returns the inherited `line_id` per current line, or null for a fresh id. A current line
|
|
99
|
+
* the engine left unpaired takes a fresh id, which is what retires the identity of a line
|
|
100
|
+
* it could not prove moved — the fail-closed direction.
|
|
101
|
+
*/
|
|
102
|
+
function pairLineIds(prev, curCanons) {
|
|
103
|
+
const prevById = new Map();
|
|
104
|
+
for (const line of prev)
|
|
105
|
+
prevById.set(line.lineNumber, line.lineId);
|
|
106
|
+
const pairing = pairSnapshots(prev.map((line) => ({ lineNumber: line.lineNumber, canonHash: line.canonHash })), curCanons.map((canonHash, index) => ({ lineNumber: index + 1, canonHash })));
|
|
107
|
+
const prevByCurr = new Map();
|
|
108
|
+
for (const [prevLine, currLine] of pairing)
|
|
109
|
+
prevByCurr.set(currLine, prevLine);
|
|
110
|
+
return curCanons.map((_, index) => {
|
|
111
|
+
const prevLine = prevByCurr.get(index + 1);
|
|
112
|
+
if (prevLine === undefined)
|
|
113
|
+
return null;
|
|
114
|
+
return prevById.get(prevLine) ?? null;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Classify a stored lineage family against the content being adopted. Returns the diagnosis
|
|
119
|
+
* when the family is unusable — empty, wrong row count, numbering that is not contiguous from
|
|
120
|
+
* line 1, or a `canon_hash` that disagrees with the line being adopted — or undefined when it
|
|
121
|
+
* can be adopted as-is.
|
|
122
|
+
*
|
|
123
|
+
* The canon arm cannot be version skew: `snapshotHashFor` embeds `CANON_VERSION` in the key that
|
|
124
|
+
* found this row, and `canonDigest` is deterministic for a given line, so within the adopt arm a
|
|
125
|
+
* mismatch can only be corruption. Excluding it would silently inherit the corrupt row's
|
|
126
|
+
* `line_id` into every future `pairLineIds` pairing.
|
|
127
|
+
*
|
|
128
|
+
* Unusable is no longer thrown: the adopt arm repairs it (see the `unusable` branch below).
|
|
129
|
+
* The diagnosis is still built so the repair warning carries the precise arm, and so a future
|
|
130
|
+
* caller that must refuse has one place to ask.
|
|
131
|
+
*/
|
|
132
|
+
function diagnoseUnusableLineage(stored, lineCount, path, curCanons) {
|
|
133
|
+
if (stored.length === 0) {
|
|
134
|
+
return new LineageCorruptError(`commitSnapshot(${path}): snapshot row has no lineage`);
|
|
135
|
+
}
|
|
136
|
+
if (stored.length !== lineCount) {
|
|
137
|
+
return new LineageCorruptError(`commitSnapshot(${path}): stored lineage has ${stored.length} rows for ${lineCount} lines`);
|
|
138
|
+
}
|
|
139
|
+
for (let index = 0; index < lineCount; index++) {
|
|
140
|
+
const row = stored[index];
|
|
141
|
+
if (row === undefined || row.line_number !== index + 1) {
|
|
142
|
+
return new LineageCorruptError(`commitSnapshot(${path}): lineage row out of order at index ${index}`);
|
|
143
|
+
}
|
|
144
|
+
const expected = curCanons[index];
|
|
145
|
+
if (expected === undefined || row.canon_hash !== expected) {
|
|
146
|
+
return new LineageCorruptError(`commitSnapshot(${path}): canon mismatch at line ${index + 1}: ` +
|
|
147
|
+
`stored ${row.canon_hash} expected ${expected ?? "<missing>"}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
export function createLineageStore(db) {
|
|
153
|
+
ensureLineageTables(db);
|
|
154
|
+
const getSnapshotStmt = db.prepare("SELECT snapshot_id FROM file_snapshots WHERE path = ? AND snapshot_hash = ?");
|
|
155
|
+
const latestSnapshotStmt = db.prepare("SELECT snapshot_id, snapshot_hash FROM file_snapshots WHERE path = ? ORDER BY snapshot_id DESC LIMIT 1");
|
|
156
|
+
const prevLineageStmt = db.prepare("SELECT line_number, line_id, canon_hash FROM line_lineage WHERE snapshot_id = ? ORDER BY line_number ASC");
|
|
157
|
+
const getCounterStmt = db.prepare("SELECT next_id FROM line_id_counters WHERE path = ?");
|
|
158
|
+
const upsertCounterStmt = db.prepare("INSERT INTO line_id_counters (path, next_id) VALUES (?, ?) " +
|
|
159
|
+
"ON CONFLICT(path) DO UPDATE SET next_id = excluded.next_id");
|
|
160
|
+
const insertSnapshotStmt = db.prepare("INSERT INTO file_snapshots (path, snapshot_hash, line_count, created_at, committed) " +
|
|
161
|
+
"VALUES (?, ?, ?, ?, 1)");
|
|
162
|
+
const insertLineageStmt = db.prepare("INSERT INTO line_lineage (snapshot_id, line_number, line_id, canon_hash, anchor) " +
|
|
163
|
+
"VALUES (?, ?, ?, ?, ?)");
|
|
164
|
+
const snapshotLineageStmt = db.prepare("SELECT line_number, line_id, canon_hash, anchor FROM line_lineage " +
|
|
165
|
+
"WHERE snapshot_id = ? ORDER BY line_number ASC");
|
|
166
|
+
const upsertLeaseStmt = db.prepare("INSERT INTO served_leases (session_id, file_path, anchor, line_id, canon_hash, " +
|
|
167
|
+
"served_snapshot_hash, served_line_number, updated_at, retired_at) " +
|
|
168
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, NULL) " +
|
|
169
|
+
"ON CONFLICT(session_id, file_path, anchor) DO UPDATE SET line_id = excluded.line_id, " +
|
|
170
|
+
"canon_hash = excluded.canon_hash, served_snapshot_hash = excluded.served_snapshot_hash, " +
|
|
171
|
+
"served_line_number = excluded.served_line_number, updated_at = excluded.updated_at, " +
|
|
172
|
+
"retired_at = NULL");
|
|
173
|
+
const getLeaseStmt = db.prepare("SELECT line_id, canon_hash, served_snapshot_hash, served_line_number, retired_at " +
|
|
174
|
+
"FROM served_leases WHERE session_id = ? AND file_path = ? AND anchor = ?");
|
|
175
|
+
const getLeaseHomesStmt = db.prepare("SELECT DISTINCT file_path FROM served_leases WHERE session_id = ? AND anchor = ? AND file_path != ?");
|
|
176
|
+
const updateLineageAnchorStmt = db.prepare("UPDATE line_lineage SET anchor = ? WHERE snapshot_id = ? AND line_number = ?");
|
|
177
|
+
const retireAbsentLeasesStmt = db.prepare("UPDATE served_leases SET retired_at = ? WHERE file_path = ? AND retired_at IS NULL " +
|
|
178
|
+
"AND line_id NOT IN (SELECT line_id FROM line_lineage WHERE snapshot_id = ?)");
|
|
179
|
+
const deleteSnapshotsByPathStmt = db.prepare("DELETE FROM file_snapshots WHERE path = ?");
|
|
180
|
+
const deleteLineageByPathStmt = db.prepare("DELETE FROM line_lineage WHERE snapshot_id IN (SELECT snapshot_id FROM file_snapshots WHERE path = ?)");
|
|
181
|
+
const deleteCountersByPathStmt = db.prepare("DELETE FROM line_id_counters WHERE path = ?");
|
|
182
|
+
const deleteLeasesByPathStmt = db.prepare("DELETE FROM served_leases WHERE file_path = ?");
|
|
183
|
+
// Repair-at-detection (CP2): a targeted discard of ONE unusable snapshot, never its siblings.
|
|
184
|
+
const deleteLineageBySnapshotStmt = db.prepare("DELETE FROM line_lineage WHERE snapshot_id = ?");
|
|
185
|
+
const deleteSnapshotByIdStmt = db.prepare("DELETE FROM file_snapshots WHERE snapshot_id = ?");
|
|
186
|
+
function retireAbsentLeases(snapshotId, path, now) {
|
|
187
|
+
withBusyRetry(() => {
|
|
188
|
+
retireAbsentLeasesStmt.run(now, path, snapshotId);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function grantLeases(snapshotId, snapshotHash, path, leases, now) {
|
|
192
|
+
// SAFETY: SELECT list (line_number, line_id, canon_hash, anchor) matches LineageRecord
|
|
193
|
+
// field-for-field; node:sqlite returns one record per row with exactly those columns.
|
|
194
|
+
const records = snapshotLineageStmt.all(snapshotId);
|
|
195
|
+
const byAnchor = new Map();
|
|
196
|
+
for (const record of records) {
|
|
197
|
+
if (!byAnchor.has(record.anchor)) {
|
|
198
|
+
byAnchor.set(record.anchor, { lineId: record.line_id, canonHash: record.canon_hash });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// Sequential upserts: a repeated anchor inside one batch keeps the last serve.
|
|
202
|
+
for (const row of leases.rows) {
|
|
203
|
+
if (row.hash === null)
|
|
204
|
+
continue;
|
|
205
|
+
const hit = byAnchor.get(row.hash);
|
|
206
|
+
if (!hit)
|
|
207
|
+
continue;
|
|
208
|
+
withBusyRetry(() => {
|
|
209
|
+
upsertLeaseStmt.run(leases.sessionKey, path, row.hash, hit.lineId, hit.canonHash, snapshotHash, row.position + 1, now);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
commitSnapshot(input) {
|
|
215
|
+
const lines = splitLines(input.content);
|
|
216
|
+
if (input.hashes.length !== lines.length) {
|
|
217
|
+
throw new Error(`commitSnapshot(${input.path}): ${input.hashes.length} hashes for ${lines.length} lines`);
|
|
218
|
+
}
|
|
219
|
+
const snapshotHash = snapshotHashFor(input.content);
|
|
220
|
+
const curCanons = lines.map((line) => canonDigest(line));
|
|
221
|
+
const now = Date.now();
|
|
222
|
+
withTransaction(db, () => {
|
|
223
|
+
const existing = getSnapshotStmt.get(input.path, snapshotHash);
|
|
224
|
+
let snapshotId;
|
|
225
|
+
if (existing !== undefined) {
|
|
226
|
+
// SAFETY: SELECT list matches LineageRecord field-for-field (same statement shape
|
|
227
|
+
// as grantLeases/lineageFor).
|
|
228
|
+
const stored = snapshotLineageStmt.all(existing.snapshot_id);
|
|
229
|
+
const unusable = diagnoseUnusableLineage(stored, lines.length, input.path, curCanons);
|
|
230
|
+
if (unusable === undefined) {
|
|
231
|
+
// Adopt-if-exists: allocate nothing — but refresh the anchor column to the live
|
|
232
|
+
// assignment (compare-then-update, diffs only). Anchor assignment is not
|
|
233
|
+
// content-determined (retire/reservation-dependent), so stored anchors go
|
|
234
|
+
// archaeological without this; identity (line_id, canon_hash) stays first-wins.
|
|
235
|
+
const adoptedId = existing.snapshot_id;
|
|
236
|
+
snapshotId = adoptedId;
|
|
237
|
+
for (let index = 0; index < lines.length; index++) {
|
|
238
|
+
if (stored[index].anchor !== input.hashes[index]) {
|
|
239
|
+
withBusyRetry(() => {
|
|
240
|
+
updateLineageAnchorStmt.run(input.hashes[index], adoptedId, index + 1);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
// Repair-at-detection (CP2): the (path, snapshot_hash) row exists but its lineage
|
|
247
|
+
// is unusable, so the stored identity is unrecoverable by definition. Discard THIS
|
|
248
|
+
// snapshot's lineage family and re-materialize it fresh below, inside the caller's
|
|
249
|
+
// unit so a failure rolls the repair back with everything else. Siblings are
|
|
250
|
+
// untouched and `line_id_counters` is not reset — fresh ids stay globally unique —
|
|
251
|
+
// and the leases bound to the discarded identities retire at the end of this unit
|
|
252
|
+
// (fail closed). Observable by construction: never a silent repair.
|
|
253
|
+
console.warn(`dsh-better-edit: repairing corrupt lineage for ${input.path} ` +
|
|
254
|
+
`(snapshot ${snapshotHash}): ${unusable.message} — discarded the unusable ` +
|
|
255
|
+
`family and re-materialized it fresh; leases on discarded identities retire.`);
|
|
256
|
+
withBusyRetry(() => {
|
|
257
|
+
deleteLineageBySnapshotStmt.run(existing.snapshot_id);
|
|
258
|
+
});
|
|
259
|
+
withBusyRetry(() => {
|
|
260
|
+
deleteSnapshotByIdStmt.run(existing.snapshot_id);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (snapshotId === undefined) {
|
|
265
|
+
const latest = latestSnapshotStmt.get(input.path);
|
|
266
|
+
const prev = [];
|
|
267
|
+
if (latest !== undefined) {
|
|
268
|
+
// SAFETY: prevLineageStmt's SELECT list (line_number, line_id, canon_hash) matches
|
|
269
|
+
// this inline shape field-for-field; node:sqlite returns one record per row with
|
|
270
|
+
// exactly those columns.
|
|
271
|
+
const stored = prevLineageStmt.all(latest.snapshot_id);
|
|
272
|
+
for (const row of stored) {
|
|
273
|
+
prev.push({
|
|
274
|
+
lineNumber: row.line_number,
|
|
275
|
+
lineId: row.line_id,
|
|
276
|
+
canonHash: row.canon_hash,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const inherited = pairLineIds(prev, curCanons);
|
|
281
|
+
// ONE counter upsert for the whole fresh block.
|
|
282
|
+
const counter = getCounterStmt.get(input.path);
|
|
283
|
+
let fresh = counter === undefined ? 1 : counter.next_id;
|
|
284
|
+
const freshCount = inherited.filter((id) => id === null).length;
|
|
285
|
+
withBusyRetry(() => {
|
|
286
|
+
upsertCounterStmt.run(input.path, fresh + freshCount);
|
|
287
|
+
});
|
|
288
|
+
// node:sqlite run() returns StatementResultingChanges, which declares
|
|
289
|
+
// lastInsertRowid — no cast is needed to read it.
|
|
290
|
+
const info = withBusyRetry(() => insertSnapshotStmt.run(input.path, snapshotHash, lines.length, now));
|
|
291
|
+
const insertedId = Number(info.lastInsertRowid);
|
|
292
|
+
snapshotId = insertedId;
|
|
293
|
+
for (let index = 0; index < lines.length; index++) {
|
|
294
|
+
let lineId = inherited[index];
|
|
295
|
+
if (lineId === null) {
|
|
296
|
+
lineId = fresh;
|
|
297
|
+
fresh += 1;
|
|
298
|
+
}
|
|
299
|
+
withBusyRetry(() => {
|
|
300
|
+
insertLineageStmt.run(insertedId, index + 1, lineId, curCanons[index], input.hashes[index]);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (input.leases !== undefined) {
|
|
305
|
+
grantLeases(snapshotId, snapshotHash, input.path, input.leases, now);
|
|
306
|
+
}
|
|
307
|
+
// Both paths, same transaction: leases whose line left the resolved snapshot retire;
|
|
308
|
+
// revival stays the grant path's retired_at = NULL.
|
|
309
|
+
retireAbsentLeases(snapshotId, input.path, now);
|
|
310
|
+
});
|
|
311
|
+
},
|
|
312
|
+
lineageFor(path, snapshotHash) {
|
|
313
|
+
const existing = getSnapshotStmt.get(path, snapshotHash);
|
|
314
|
+
if (existing === undefined)
|
|
315
|
+
return [];
|
|
316
|
+
// SAFETY: SELECT list matches LineageRecord field-for-field (same statement as grantLeases).
|
|
317
|
+
return snapshotLineageStmt.all(existing.snapshot_id).map((row) => ({
|
|
318
|
+
lineNumber: row.line_number,
|
|
319
|
+
lineId: row.line_id,
|
|
320
|
+
canonHash: row.canon_hash,
|
|
321
|
+
anchor: row.anchor,
|
|
322
|
+
}));
|
|
323
|
+
},
|
|
324
|
+
positionsByIdentity(path, content) {
|
|
325
|
+
const current = getSnapshotStmt.get(path, snapshotHashFor(content));
|
|
326
|
+
if (current !== undefined) {
|
|
327
|
+
const map = new Map();
|
|
328
|
+
// SAFETY: SELECT list matches LineageRecord field-for-field (same statement as grantLeases).
|
|
329
|
+
const records = snapshotLineageStmt.all(current.snapshot_id);
|
|
330
|
+
for (const row of records) {
|
|
331
|
+
map.set(row.line_id, row.line_number);
|
|
332
|
+
}
|
|
333
|
+
return map;
|
|
334
|
+
}
|
|
335
|
+
const latest = latestSnapshotStmt.get(path);
|
|
336
|
+
if (latest === undefined)
|
|
337
|
+
return new Map();
|
|
338
|
+
// SAFETY: prevLineageStmt's SELECT list (line_number, line_id, canon_hash) matches this
|
|
339
|
+
// inline shape field-for-field; node:sqlite returns one record per row with those columns.
|
|
340
|
+
const previous = prevLineageStmt.all(latest.snapshot_id).map((row) => ({
|
|
341
|
+
lineNumber: row.line_number,
|
|
342
|
+
lineId: row.line_id,
|
|
343
|
+
canonHash: row.canon_hash,
|
|
344
|
+
}));
|
|
345
|
+
if (previous.length === 0)
|
|
346
|
+
return new Map();
|
|
347
|
+
const inherited = pairLineIds(previous, splitLines(content).map((line) => canonDigest(line)));
|
|
348
|
+
const map = new Map();
|
|
349
|
+
for (let index = 0; index < inherited.length; index++) {
|
|
350
|
+
const lineId = inherited[index];
|
|
351
|
+
if (lineId !== null)
|
|
352
|
+
map.set(lineId, index + 1);
|
|
353
|
+
}
|
|
354
|
+
return map;
|
|
355
|
+
},
|
|
356
|
+
leaseFor(sessionKey, path, anchor) {
|
|
357
|
+
const row = getLeaseStmt.get(sessionKey, path, anchor);
|
|
358
|
+
if (row === undefined)
|
|
359
|
+
return undefined;
|
|
360
|
+
return {
|
|
361
|
+
lineId: row.line_id,
|
|
362
|
+
canonHash: row.canon_hash,
|
|
363
|
+
snapshotHash: row.served_snapshot_hash,
|
|
364
|
+
lineNumber: row.served_line_number,
|
|
365
|
+
retiredAt: row.retired_at,
|
|
366
|
+
};
|
|
367
|
+
},
|
|
368
|
+
leaseHomes(sessionKey, excludePath, anchor) {
|
|
369
|
+
const rows = getLeaseHomesStmt.all(sessionKey, anchor, excludePath);
|
|
370
|
+
return rows.map((row) => row.file_path);
|
|
371
|
+
},
|
|
372
|
+
snapshotIdFor(path, snapshotHash) {
|
|
373
|
+
return getSnapshotStmt.get(path, snapshotHash)
|
|
374
|
+
?.snapshot_id;
|
|
375
|
+
},
|
|
376
|
+
deleteByPath(path) {
|
|
377
|
+
// One unit: these four statements are one logical mutation, so an interrupted delete
|
|
378
|
+
// must never leave a `file_snapshots` row whose lineage family is already gone (the
|
|
379
|
+
// empty arm this module's adopt path repairs). Nested inside `pruneMissing`'s
|
|
380
|
+
// `withStore` unit this joins the outer transaction instead of raising
|
|
381
|
+
// "cannot start a transaction within a transaction".
|
|
382
|
+
withTransaction(db, () => {
|
|
383
|
+
// Explicit lineage delete first: the FK cascade covers FK-on openers, but
|
|
384
|
+
// deleteByPath must hold for any opener (e.g. :memory: test DBs).
|
|
385
|
+
withBusyRetry(() => {
|
|
386
|
+
deleteLineageByPathStmt.run(path);
|
|
387
|
+
});
|
|
388
|
+
withBusyRetry(() => {
|
|
389
|
+
deleteSnapshotsByPathStmt.run(path);
|
|
390
|
+
});
|
|
391
|
+
withBusyRetry(() => {
|
|
392
|
+
deleteCountersByPathStmt.run(path);
|
|
393
|
+
});
|
|
394
|
+
withBusyRetry(() => {
|
|
395
|
+
deleteLeasesByPathStmt.run(path);
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
},
|
|
399
|
+
};
|
|
400
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy pre-SQLite JSON file import (`hash-store.json` → `snapshots` shell).
|
|
3
|
+
* Moved verbatim from `hash-store.ts`: same validity rules, same messages,
|
|
4
|
+
* same best-effort I/O handling, same transaction shape, same `.bak` rename.
|
|
5
|
+
* The legacy path stays the caller's business: `openStore` resolves it
|
|
6
|
+
* tenant-aware via `join(dirname(storePath), "hash-store.json")` and passes
|
|
7
|
+
* it in, so this module never touches tenancy or `hash-store`.
|
|
8
|
+
* @module dsh-better-edit/snapshot-store/migrate
|
|
9
|
+
*/
|
|
10
|
+
import { DatabaseSync } from "node:sqlite";
|
|
11
|
+
import { rename, readFile } from "node:fs/promises";
|
|
12
|
+
import { contentChecksum } from "../hashline/hash-assign.js";
|
|
13
|
+
import { splitLines, errCode } from "../utils.js";
|
|
14
|
+
import { isValidHashList } from "./index.js";
|
|
15
|
+
export function isValidSnapshot(value) {
|
|
16
|
+
if (typeof value !== "object" || value === null)
|
|
17
|
+
return false;
|
|
18
|
+
const v = value;
|
|
19
|
+
if (typeof v.content !== "string")
|
|
20
|
+
return false;
|
|
21
|
+
return isValidHashList(v.hashes);
|
|
22
|
+
}
|
|
23
|
+
export async function migrateLegacyStore(db, legacyPath) {
|
|
24
|
+
let content;
|
|
25
|
+
try {
|
|
26
|
+
content = await readFile(legacyPath, "utf-8");
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (errCode(error) === "ENOENT")
|
|
30
|
+
return;
|
|
31
|
+
console.error("Failed to read legacy hash store for migration:", error);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = JSON.parse(content);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.error("Failed to parse legacy hash store, skipping migration:", error);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const raw = parsed.snapshots;
|
|
43
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
44
|
+
return;
|
|
45
|
+
const rows = [];
|
|
46
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
47
|
+
if (!isValidSnapshot(value))
|
|
48
|
+
continue;
|
|
49
|
+
if (new Set(value.hashes).size !== value.hashes.length) {
|
|
50
|
+
console.warn(`Skipped legacy snapshot with duplicate hashes for ${key}; it will be re-hashed on next read.`);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
rows.push([
|
|
54
|
+
key,
|
|
55
|
+
contentChecksum(value.content),
|
|
56
|
+
splitLines(value.content).length,
|
|
57
|
+
JSON.stringify(value.hashes),
|
|
58
|
+
Date.now(),
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
if (rows.length > 0) {
|
|
62
|
+
db.exec("BEGIN IMMEDIATE");
|
|
63
|
+
try {
|
|
64
|
+
const stmt = db.prepare("INSERT OR REPLACE INTO snapshots (path, checksum, line_count, hashes, updated_at) VALUES (?, ?, ?, ?, ?)");
|
|
65
|
+
for (const row of rows)
|
|
66
|
+
stmt.run(...row);
|
|
67
|
+
db.exec("COMMIT");
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
db.exec("ROLLBACK");
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
await rename(legacyPath, `${legacyPath}.bak`);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
console.error("Failed to rename legacy hash store after migration:", error);
|
|
79
|
+
}
|
|
80
|
+
}
|