dsh-better-edit 0.1.8 → 0.2.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 +28 -6
- package/README.md +27 -7
- package/README.zh.md +24 -7
- package/assets/logo.svg +183 -18
- package/lib/contract.js +95 -0
- package/lib/drift.js +1 -1
- package/lib/edit-diff.js +2 -2
- package/lib/edit-engine.js +456 -0
- package/lib/edit-pipeline.js +41 -116
- package/lib/file-reader.js +1 -2
- package/lib/fs-bridge.js +2 -2
- package/lib/fs-write.js +3 -46
- package/lib/hash-store.js +246 -13
- package/lib/hashline/apply.js +2 -2
- package/lib/hashline/hash.js +16 -206
- package/lib/hashline/index.js +4 -3
- package/lib/hashline/parse.js +1 -1
- package/lib/hashline/pure.js +213 -0
- package/lib/hashline/resolve.js +1 -5
- package/lib/hashline/served.js +2 -2
- package/lib/paths.js +55 -1
- package/lib/read-and-serve.js +46 -0
- package/lib/schema.js +1 -1
- package/lib/served-store.js +144 -183
- package/lib/tool-batch-edit.js +28 -280
- package/lib/tool-edit.js +37 -32
- package/lib/tool-read.js +8 -31
- package/lib/tool-undo.js +4 -13
- package/lib/types/contract.d.ts +39 -0
- package/lib/types/edit-engine.d.ts +179 -0
- package/lib/types/edit-pipeline.d.ts +10 -17
- package/lib/types/fs-write.d.ts +0 -1
- package/lib/types/hash-store.d.ts +73 -25
- package/lib/types/hashline/hash.d.ts +8 -13
- package/lib/types/hashline/index.d.ts +4 -3
- package/lib/types/hashline/pure.d.ts +26 -0
- package/lib/types/hashline/resolve.d.ts +0 -1
- package/lib/types/paths.d.ts +9 -0
- package/lib/types/read-and-serve.d.ts +39 -0
- package/lib/types/schema.d.ts +2 -16
- package/lib/types/served-store.d.ts +63 -32
- package/lib/types/tool-batch-edit.d.ts +4 -1
- package/lib/types/tool-undo.d.ts +1 -1
- package/lib/undo-edit.js +16 -1
- package/lib/write-hook.js +30 -50
- package/package.json +4 -3
- package/lib/served-state.js +0 -34
- package/lib/snapshot-store.js +0 -108
- package/lib/types/served-state.d.ts +0 -3
- package/lib/types/snapshot-store.d.ts +0 -14
- package/lib/types/undo-store.d.ts +0 -14
- package/lib/undo-store.js +0 -43
package/lib/fs-write.js
CHANGED
|
@@ -1,51 +1,8 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
import { dirname, join
|
|
2
|
+
import { mkdir, open, readdir, rename, rm, stat, writeFile, } from "fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
4
|
import { errCode } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
const absolutePath = resolve(path);
|
|
7
|
-
const { root } = parse(absolutePath);
|
|
8
|
-
const parts = absolutePath
|
|
9
|
-
.slice(root.length)
|
|
10
|
-
.split(sep)
|
|
11
|
-
.filter((part) => part.length > 0);
|
|
12
|
-
const visitedSymlinks = new Set();
|
|
13
|
-
async function resParts(currentPath, remainingParts) {
|
|
14
|
-
if (remainingParts.length === 0) {
|
|
15
|
-
return currentPath;
|
|
16
|
-
}
|
|
17
|
-
const [nextPart, ...tail] = remainingParts;
|
|
18
|
-
const candidatePath = join(currentPath, nextPart);
|
|
19
|
-
try {
|
|
20
|
-
const candidateStats = await lstat(candidatePath);
|
|
21
|
-
if (!candidateStats.isSymbolicLink()) {
|
|
22
|
-
return resParts(candidatePath, tail);
|
|
23
|
-
}
|
|
24
|
-
if (visitedSymlinks.has(candidatePath)) {
|
|
25
|
-
const error = new Error(`Too many symbolic links while resolving ${path}`);
|
|
26
|
-
error.code = "ELOOP";
|
|
27
|
-
throw error;
|
|
28
|
-
}
|
|
29
|
-
visitedSymlinks.add(candidatePath);
|
|
30
|
-
const linkTargetPath = resolve(dirname(candidatePath), await readlink(candidatePath));
|
|
31
|
-
const targetParts = linkTargetPath
|
|
32
|
-
.slice(parse(linkTargetPath).root.length)
|
|
33
|
-
.split(sep)
|
|
34
|
-
.filter((part) => part.length > 0);
|
|
35
|
-
return resParts(parse(linkTargetPath).root, [
|
|
36
|
-
...targetParts,
|
|
37
|
-
...tail,
|
|
38
|
-
]);
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
if (errCode(error) === "ENOENT") {
|
|
42
|
-
return join(candidatePath, ...tail);
|
|
43
|
-
}
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return resParts(root, parts);
|
|
48
|
-
}
|
|
5
|
+
import { resolveTarget } from "./paths.js";
|
|
49
6
|
const TEMP_PREFIX = ".tmp-";
|
|
50
7
|
const TEMP_UUID_RE = /^\.tmp-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
51
8
|
const STALE_TEMP_MS = 60 * 60 * 1000;
|
package/lib/hash-store.js
CHANGED
|
@@ -1,13 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hash store — ONE deep persistence module for the hashline domain.
|
|
3
|
+
*
|
|
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
|
|
8
|
+
* use domain methods, never SQL.
|
|
9
|
+
*
|
|
10
|
+
* Corrupt-row handling (parse the JSON column → validate against the hash
|
|
11
|
+
* alphabet → delete the corrupt row) lives here, once, for every row family.
|
|
12
|
+
* Cross-table cleanup (pruneMissing) lives here too — a sibling module never
|
|
13
|
+
* reaches into another family's rows.
|
|
14
|
+
* @module dsh-better-edit/hash-store
|
|
15
|
+
*/
|
|
1
16
|
import { existsSync } from "node:fs";
|
|
2
|
-
import { readFile, rename, mkdir } from "node:fs/promises";
|
|
17
|
+
import { readFile, rename, mkdir, stat } from "node:fs/promises";
|
|
3
18
|
import { dirname, join } from "node:path";
|
|
4
19
|
import { DatabaseSync } from "node:sqlite";
|
|
5
20
|
import { hashStorePath } from "./paths.js";
|
|
6
21
|
import { workspaceCwd } from "./workspace.js";
|
|
7
22
|
import { errCode, splitLines } from "./utils.js";
|
|
8
23
|
import { initHasher, contentChecksum } from "./hashline/hasher.js";
|
|
24
|
+
import { HASH_RE } from "./hashline/alphabet.js";
|
|
9
25
|
import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS } from "./constants.js";
|
|
10
|
-
|
|
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
|
+
}
|
|
43
|
+
/** A served-row array: per-position hash, or null for never-served slots. */
|
|
44
|
+
export function isValidServedList(value) {
|
|
45
|
+
if (!Array.isArray(value))
|
|
46
|
+
return false;
|
|
47
|
+
for (const entry of value) {
|
|
48
|
+
if (entry === null)
|
|
49
|
+
continue;
|
|
50
|
+
if (typeof entry !== "string" || !HASH_RE.test(entry))
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
// ---- db plumbing (private) --------------------------------------------------
|
|
11
56
|
export function isCorruptionError(error) {
|
|
12
57
|
if (error && typeof error === "object") {
|
|
13
58
|
const errcode = error.errcode;
|
|
@@ -68,7 +113,9 @@ function openDb(storePath) {
|
|
|
68
113
|
try {
|
|
69
114
|
db.close();
|
|
70
115
|
}
|
|
71
|
-
catch {
|
|
116
|
+
catch {
|
|
117
|
+
// best-effort close when the store build fails
|
|
118
|
+
}
|
|
72
119
|
throw error;
|
|
73
120
|
}
|
|
74
121
|
}
|
|
@@ -203,6 +250,157 @@ function buildStore(db) {
|
|
|
203
250
|
};
|
|
204
251
|
return { db, stmts };
|
|
205
252
|
}
|
|
253
|
+
/** Wire the domain methods over the prepared statements. */
|
|
254
|
+
function makeDomainStore(stmts) {
|
|
255
|
+
return {
|
|
256
|
+
engine: "node:sqlite",
|
|
257
|
+
getSnapshot(path, content, deleteCorrupt = true) {
|
|
258
|
+
const checksum = contentChecksum(content);
|
|
259
|
+
const lineCount = splitLines(content).length;
|
|
260
|
+
const row = stmts.get(path, checksum, lineCount);
|
|
261
|
+
if (!row)
|
|
262
|
+
return undefined;
|
|
263
|
+
try {
|
|
264
|
+
const parsed = JSON.parse(row.hashes);
|
|
265
|
+
if (isValidHashList(parsed))
|
|
266
|
+
return parsed;
|
|
267
|
+
if (deleteCorrupt)
|
|
268
|
+
stmts.deleteOne(path);
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
if (deleteCorrupt)
|
|
273
|
+
stmts.deleteOne(path);
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
upsertSnapshot(path, checksum, lineCount, hashes) {
|
|
278
|
+
stmts.upsert(path, checksum, lineCount, JSON.stringify(hashes), Date.now());
|
|
279
|
+
},
|
|
280
|
+
allKnownPaths() {
|
|
281
|
+
return stmts.allPaths();
|
|
282
|
+
},
|
|
283
|
+
allSnapshotHashes() {
|
|
284
|
+
return stmts.allHashes();
|
|
285
|
+
},
|
|
286
|
+
deleteSnapshot(path) {
|
|
287
|
+
stmts.deleteOne(path);
|
|
288
|
+
},
|
|
289
|
+
findSnapshotPaths(hashes) {
|
|
290
|
+
const rows = stmts.allHashes();
|
|
291
|
+
const matches = [];
|
|
292
|
+
for (const row of rows) {
|
|
293
|
+
try {
|
|
294
|
+
const parsed = JSON.parse(row.hashes);
|
|
295
|
+
if (!isValidHashList(parsed))
|
|
296
|
+
continue;
|
|
297
|
+
if (hashes.every((h) => parsed.includes(h)))
|
|
298
|
+
matches.push(row.path);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
// unparseable row → skip it
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return matches;
|
|
305
|
+
},
|
|
306
|
+
getUndo(path) {
|
|
307
|
+
const row = stmts.undoGet(path);
|
|
308
|
+
if (!row)
|
|
309
|
+
return undefined;
|
|
310
|
+
try {
|
|
311
|
+
const parsed = JSON.parse(row.hashes);
|
|
312
|
+
if (!isValidHashList(parsed)) {
|
|
313
|
+
stmts.undoDelete(path);
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
content: row.content,
|
|
318
|
+
bom: row.bom,
|
|
319
|
+
ending: row.ending,
|
|
320
|
+
hashes: parsed,
|
|
321
|
+
resultContent: row.result_content,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
stmts.undoDelete(path);
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
upsertUndo(path, entry) {
|
|
330
|
+
stmts.undoUpsert(path, entry.content, entry.bom, entry.ending, JSON.stringify(entry.hashes), entry.resultContent, Date.now());
|
|
331
|
+
},
|
|
332
|
+
deleteUndo(path) {
|
|
333
|
+
stmts.undoDelete(path);
|
|
334
|
+
},
|
|
335
|
+
getServed(sessionKey, path) {
|
|
336
|
+
const row = stmts.servedGet(sessionKey, path);
|
|
337
|
+
if (!row)
|
|
338
|
+
return [];
|
|
339
|
+
try {
|
|
340
|
+
const parsed = JSON.parse(row.hashes);
|
|
341
|
+
if (isValidServedList(parsed))
|
|
342
|
+
return parsed;
|
|
343
|
+
stmts.servedDelete(sessionKey, path);
|
|
344
|
+
return [];
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
stmts.servedDelete(sessionKey, path);
|
|
348
|
+
return [];
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
getServedReported(sessionKey, path) {
|
|
352
|
+
const row = stmts.servedGet(sessionKey, path);
|
|
353
|
+
if (!row)
|
|
354
|
+
return new Set();
|
|
355
|
+
const raw = row.reported;
|
|
356
|
+
if (typeof raw !== "string" || raw.length === 0)
|
|
357
|
+
return new Set();
|
|
358
|
+
try {
|
|
359
|
+
const parsed = JSON.parse(raw);
|
|
360
|
+
if (!Array.isArray(parsed))
|
|
361
|
+
return new Set();
|
|
362
|
+
return new Set(parsed.filter((h) => typeof h === "string" && HASH_RE.test(h)));
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
return new Set();
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
upsertServed(sessionKey, path, hashesJson) {
|
|
369
|
+
stmts.servedUpsert(sessionKey, path, hashesJson, Date.now());
|
|
370
|
+
},
|
|
371
|
+
upsertServedReported(sessionKey, path, reportedJson) {
|
|
372
|
+
stmts.servedReportedUpsert(sessionKey, path, reportedJson, Date.now());
|
|
373
|
+
},
|
|
374
|
+
clearServedReported(sessionKey, path) {
|
|
375
|
+
stmts.servedReportedClear(sessionKey, Date.now(), path);
|
|
376
|
+
},
|
|
377
|
+
deleteServed(sessionKey, path) {
|
|
378
|
+
stmts.servedDelete(sessionKey, path);
|
|
379
|
+
},
|
|
380
|
+
deleteServedByPath(path) {
|
|
381
|
+
stmts.servedDeletePath(path);
|
|
382
|
+
},
|
|
383
|
+
wipeServed(sessionKey) {
|
|
384
|
+
stmts.servedWipe(sessionKey);
|
|
385
|
+
},
|
|
386
|
+
pruneServedOlderThan(ts) {
|
|
387
|
+
stmts.servedPruneOlderThan(ts);
|
|
388
|
+
},
|
|
389
|
+
async pruneMissing() {
|
|
390
|
+
const rows = stmts.allPaths();
|
|
391
|
+
const missing = await statMissing(rows);
|
|
392
|
+
if (missing.length === 0)
|
|
393
|
+
return;
|
|
394
|
+
withStore(() => {
|
|
395
|
+
for (const path of missing) {
|
|
396
|
+
stmts.deleteOne(path);
|
|
397
|
+
stmts.undoDelete(path);
|
|
398
|
+
stmts.servedDeletePath(path);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
}
|
|
206
404
|
function isHealthy(db) {
|
|
207
405
|
try {
|
|
208
406
|
const row = db.prepare("PRAGMA quick_check").get();
|
|
@@ -231,9 +429,32 @@ function shutdownDb(db) {
|
|
|
231
429
|
try {
|
|
232
430
|
db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
233
431
|
}
|
|
234
|
-
catch {
|
|
432
|
+
catch {
|
|
433
|
+
// best-effort checkpoint before close
|
|
434
|
+
}
|
|
235
435
|
db.close();
|
|
236
436
|
}
|
|
437
|
+
const STAT_BATCH = 64;
|
|
438
|
+
async function statMissing(rows) {
|
|
439
|
+
const missing = [];
|
|
440
|
+
for (let i = 0; i < rows.length; i += STAT_BATCH) {
|
|
441
|
+
const batch = rows.slice(i, i + STAT_BATCH);
|
|
442
|
+
const results = await Promise.all(batch.map(async (row) => {
|
|
443
|
+
try {
|
|
444
|
+
await stat(row.path);
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
return row.path;
|
|
449
|
+
}
|
|
450
|
+
}));
|
|
451
|
+
for (const path of results) {
|
|
452
|
+
if (path !== undefined)
|
|
453
|
+
missing.push(path);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return missing;
|
|
457
|
+
}
|
|
237
458
|
async function openStore(storePath) {
|
|
238
459
|
// Multi-store: never close another workspace's store when opening this one.
|
|
239
460
|
await initHasher();
|
|
@@ -264,7 +485,8 @@ async function openStore(storePath) {
|
|
|
264
485
|
withBusyRetry(() => {
|
|
265
486
|
stmts.servedPruneOlderThan(Date.now() - SERVED_TTL_MS);
|
|
266
487
|
});
|
|
267
|
-
|
|
488
|
+
const store = makeDomainStore(stmts);
|
|
489
|
+
stores.set(storePath, { path: storePath, db, stmts, store });
|
|
268
490
|
if (!exitHandlerRegistered) {
|
|
269
491
|
exitHandlerRegistered = true;
|
|
270
492
|
process.once("exit", () => shutdownHashStore());
|
|
@@ -275,7 +497,7 @@ async function openStore(storePath) {
|
|
|
275
497
|
});
|
|
276
498
|
}
|
|
277
499
|
}
|
|
278
|
-
return
|
|
500
|
+
return store;
|
|
279
501
|
}
|
|
280
502
|
/** Resolve the store path for this call: explicit cwd, the active workspace, or the shared-home fallback. */
|
|
281
503
|
function storePathFor(cwd) {
|
|
@@ -291,7 +513,7 @@ export function loadHashStore(cwd) {
|
|
|
291
513
|
const storePath = storePathFor(cwd);
|
|
292
514
|
const cached = stores.get(storePath);
|
|
293
515
|
if (cached && cached.db.isOpen) {
|
|
294
|
-
return Promise.resolve(
|
|
516
|
+
return Promise.resolve(cached.store);
|
|
295
517
|
}
|
|
296
518
|
const existing = openings.get(storePath);
|
|
297
519
|
if (existing)
|
|
@@ -302,9 +524,10 @@ export function loadHashStore(cwd) {
|
|
|
302
524
|
openings.set(storePath, promise);
|
|
303
525
|
return promise;
|
|
304
526
|
}
|
|
305
|
-
/** The cached store for the active workspace (or the shared-home fallback), if open. */
|
|
527
|
+
/** The cached store entry for the active workspace (or the shared-home fallback), if open. */
|
|
306
528
|
function currentStore() {
|
|
307
|
-
|
|
529
|
+
const entry = stores.get(storePathFor());
|
|
530
|
+
return entry?.db.isOpen ? entry : undefined;
|
|
308
531
|
}
|
|
309
532
|
/** Close every open store (process exit, HMR, tests). */
|
|
310
533
|
export function shutdownHashStore() {
|
|
@@ -332,7 +555,9 @@ export function withStore(fn) {
|
|
|
332
555
|
try {
|
|
333
556
|
store.db.exec("ROLLBACK");
|
|
334
557
|
}
|
|
335
|
-
catch {
|
|
558
|
+
catch {
|
|
559
|
+
// best-effort rollback; the original error propagates
|
|
560
|
+
}
|
|
336
561
|
throw e;
|
|
337
562
|
}
|
|
338
563
|
});
|
|
@@ -400,6 +625,14 @@ async function migrateLegacy(db, storePath) {
|
|
|
400
625
|
console.error("Failed to rename legacy hash store after migration:", error);
|
|
401
626
|
}
|
|
402
627
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
export
|
|
628
|
+
// ---- async convenience helpers (load the active store, then delegate) ------
|
|
629
|
+
/** Find files whose stored snapshot hashes contain every given anchor. */
|
|
630
|
+
export async function findSnapshotPathsByHashes(hashes) {
|
|
631
|
+
const store = await loadHashStore();
|
|
632
|
+
return store.findSnapshotPaths(hashes);
|
|
633
|
+
}
|
|
634
|
+
/** Persist a hash snapshot for one path (async over the active store). */
|
|
635
|
+
export async function upsertSnapshotFor(path, checksum, lineCount, hashes) {
|
|
636
|
+
const store = await loadHashStore();
|
|
637
|
+
store.upsertSnapshot(path, checksum, lineCount, hashes);
|
|
638
|
+
}
|
package/lib/hashline/apply.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { abortIf, splitLines } from "../utils.js";
|
|
2
|
-
import {
|
|
2
|
+
import { lineHashesPure, HASH_SEP } from "./pure.js";
|
|
3
3
|
import { AnchorMismatchError, verifyServedRange, } from "./served.js";
|
|
4
4
|
import { valEdit, stripBarePrefixes, stripDiffPrefixes, swapReversedRanges, warnUnicodeEsc, fmtMismatchWithServes, } from "./resolve.js";
|
|
5
5
|
export function buildIdx(content) {
|
|
@@ -85,7 +85,7 @@ function assemble(content, span, signal) {
|
|
|
85
85
|
export function applyEdit(content, edit, signal, precomputedHashes, filePath, served) {
|
|
86
86
|
abortIf(signal);
|
|
87
87
|
const lineIndex = buildIdx(content);
|
|
88
|
-
const fileHashes = precomputedHashes ??
|
|
88
|
+
const fileHashes = precomputedHashes ?? lineHashesPure(content);
|
|
89
89
|
const warnings = [];
|
|
90
90
|
const rangeFixed = swapReversedRanges(edit, fileHashes, warnings);
|
|
91
91
|
const prefixFixed = stripDiffPrefixes(stripBarePrefixes(rangeFixed, fileHashes, warnings), warnings);
|
package/lib/hashline/hash.js
CHANGED
|
@@ -1,89 +1,26 @@
|
|
|
1
1
|
import { splitLines } from "../utils.js";
|
|
2
|
-
import { loadHashStore
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
for (let j = 0; j < HASH_LEN; j++) {
|
|
14
|
-
out = ALPH[idx % ALPH.length] + out;
|
|
15
|
-
idx = Math.floor(idx / ALPH.length);
|
|
16
|
-
}
|
|
17
|
-
return out;
|
|
18
|
-
}
|
|
19
|
-
const hashCache = new Map();
|
|
20
|
-
function hashAt(idx) {
|
|
21
|
-
let hash = hashCache.get(idx);
|
|
22
|
-
if (hash === undefined) {
|
|
23
|
-
hash = idxToHash(idx);
|
|
24
|
-
hashCache.set(idx, hash);
|
|
25
|
-
}
|
|
26
|
-
return hash;
|
|
27
|
-
}
|
|
28
|
-
export const HL_PREFIX_PLUS_RE = new RegExp(`^\\+${HASH_CLASS}│`);
|
|
29
|
-
export const HL_PREFIX_MINUS_RE = new RegExp(`^-(?:${HASH_CLASS}│| {${ANCHOR_LEN}}│)`);
|
|
30
|
-
export const HL_BARE_PREFIX_RE = new RegExp(`^\\s*(${HASH_CLASS})│`);
|
|
31
|
-
export function canon(line) {
|
|
32
|
-
return line.replace(/\r/g, "").trimEnd();
|
|
33
|
-
}
|
|
34
|
-
const BITSET_WORDS = Math.ceil(HASH_SPACE / 32);
|
|
35
|
-
function getBit(bits, idx) {
|
|
36
|
-
return (bits[idx >>> 5] >>> (idx & 31) & 1) !== 0;
|
|
37
|
-
}
|
|
38
|
-
function setBit(bits, idx) {
|
|
39
|
-
bits[idx >>> 5] |= 1 << (idx & 31);
|
|
40
|
-
}
|
|
41
|
-
function nextZeroBit(bits, start) {
|
|
42
|
-
const totalBits = HASH_SPACE;
|
|
43
|
-
let idx = start % totalBits;
|
|
44
|
-
for (let i = 0; i < totalBits; i++) {
|
|
45
|
-
if (!getBit(bits, idx))
|
|
46
|
-
return idx;
|
|
47
|
-
idx += HASH_PROBE_STRIDE;
|
|
48
|
-
if (idx >= totalBits)
|
|
49
|
-
idx -= totalBits;
|
|
50
|
-
}
|
|
51
|
-
throw new Error(`[E_FILE_TOO_LARGE] Cannot allocate a unique hash anchor: the file exceeds the ${HASH_SPACE}-line limit for ${HASH_LEN}-char hashline anchors. For very large files use write or a non-line-based approach.`);
|
|
52
|
-
}
|
|
53
|
-
function assignHash(used, baseIdx, hint) {
|
|
54
|
-
if (!getBit(used, baseIdx)) {
|
|
55
|
-
setBit(used, baseIdx);
|
|
56
|
-
hint.value = baseIdx + HASH_PROBE_STRIDE;
|
|
57
|
-
return hashAt(baseIdx);
|
|
58
|
-
}
|
|
59
|
-
const nextIdx = nextZeroBit(used, hint.value);
|
|
60
|
-
setBit(used, nextIdx);
|
|
61
|
-
hint.value = nextIdx + HASH_PROBE_STRIDE;
|
|
62
|
-
return hashAt(nextIdx);
|
|
63
|
-
}
|
|
64
|
-
export function _lineHashesPure(content) {
|
|
65
|
-
const lines = splitLines(content);
|
|
66
|
-
const hashes = new Array(lines.length);
|
|
67
|
-
const used = new Uint32Array(BITSET_WORDS);
|
|
68
|
-
const hint = { value: 0 };
|
|
69
|
-
for (let i = 0; i < lines.length; i++) {
|
|
70
|
-
const c = canon(lines[i]);
|
|
71
|
-
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
72
|
-
hashes[i] = assignHash(used, baseIdx, hint);
|
|
73
|
-
}
|
|
74
|
-
return hashes;
|
|
75
|
-
}
|
|
2
|
+
import { loadHashStore } from "../hash-store.js";
|
|
3
|
+
import { contentChecksum, initHasher } from "./hasher.js";
|
|
4
|
+
import { lineHashesPure, mapStableHashes } from "./pure.js";
|
|
5
|
+
/**
|
|
6
|
+
* Persistence-aware hash-anchor assignment: the pure hasher
|
|
7
|
+
* ({@link lineHashesPure} / {@link mapStableHashes} in ./pure.js) composed
|
|
8
|
+
* with snapshot load/upsert in the hash store, keyed by
|
|
9
|
+
* (path, content checksum, line count). Without a `path` this never touches
|
|
10
|
+
* the store and is exactly the pure path. With `previous` the stable re-hash
|
|
11
|
+
* mapping keeps surviving anchors stable across edits.
|
|
12
|
+
*/
|
|
76
13
|
export async function lineHashes(content, path, previous, store, persist) {
|
|
77
14
|
await initHasher();
|
|
78
15
|
if (!path) {
|
|
79
|
-
return
|
|
16
|
+
return lineHashesPure(content);
|
|
80
17
|
}
|
|
81
18
|
const hashStore = store ?? await loadHashStore();
|
|
82
19
|
if (previous) {
|
|
83
20
|
const newHashes = mapStableHashes(previous.content, previous.hashes, content, previous.removedHashes);
|
|
84
21
|
if (persist !== false) {
|
|
85
22
|
try {
|
|
86
|
-
upsertSnapshot(
|
|
23
|
+
hashStore.upsertSnapshot(path, contentChecksum(content), splitLines(content).length, newHashes);
|
|
87
24
|
}
|
|
88
25
|
catch (error) {
|
|
89
26
|
console.error("Failed to persist hash snapshot:", error);
|
|
@@ -93,7 +30,7 @@ export async function lineHashes(content, path, previous, store, persist) {
|
|
|
93
30
|
}
|
|
94
31
|
let cached;
|
|
95
32
|
try {
|
|
96
|
-
cached = getSnapshot(
|
|
33
|
+
cached = hashStore.getSnapshot(path, content, persist !== false);
|
|
97
34
|
}
|
|
98
35
|
catch (error) {
|
|
99
36
|
console.error("Failed to read hash store snapshot:", error);
|
|
@@ -101,10 +38,10 @@ export async function lineHashes(content, path, previous, store, persist) {
|
|
|
101
38
|
if (cached) {
|
|
102
39
|
return cached;
|
|
103
40
|
}
|
|
104
|
-
const newHashes =
|
|
41
|
+
const newHashes = lineHashesPure(content);
|
|
105
42
|
if (persist !== false) {
|
|
106
43
|
try {
|
|
107
|
-
upsertSnapshot(
|
|
44
|
+
hashStore.upsertSnapshot(path, contentChecksum(content), splitLines(content).length, newHashes);
|
|
108
45
|
}
|
|
109
46
|
catch (error) {
|
|
110
47
|
console.error("Failed to persist hash snapshot:", error);
|
|
@@ -112,130 +49,3 @@ export async function lineHashes(content, path, previous, store, persist) {
|
|
|
112
49
|
}
|
|
113
50
|
return newHashes;
|
|
114
51
|
}
|
|
115
|
-
function hashToIndex(hash) {
|
|
116
|
-
let idx = 0;
|
|
117
|
-
for (let j = 0; j < HASH_LEN; j++) {
|
|
118
|
-
const charIdx = ALPH.indexOf(hash[j]);
|
|
119
|
-
if (charIdx < 0)
|
|
120
|
-
return -1;
|
|
121
|
-
idx = idx * ALPH.length + charIdx;
|
|
122
|
-
}
|
|
123
|
-
return idx;
|
|
124
|
-
}
|
|
125
|
-
function nearestNew(candidates, target) {
|
|
126
|
-
let lo = 0;
|
|
127
|
-
let hi = candidates.length;
|
|
128
|
-
while (lo < hi) {
|
|
129
|
-
const mid = (lo + hi) >>> 1;
|
|
130
|
-
if (candidates[mid] < target)
|
|
131
|
-
lo = mid + 1;
|
|
132
|
-
else
|
|
133
|
-
hi = mid;
|
|
134
|
-
}
|
|
135
|
-
const left = lo - 1;
|
|
136
|
-
const right = lo;
|
|
137
|
-
if (left >= 0 &&
|
|
138
|
-
(right >= candidates.length ||
|
|
139
|
-
target - candidates[left] <= candidates[right] - target)) {
|
|
140
|
-
return left;
|
|
141
|
-
}
|
|
142
|
-
return right < candidates.length ? right : -1;
|
|
143
|
-
}
|
|
144
|
-
function mapStableHashes(oldContent, oldHashes, newContent, removedHashes) {
|
|
145
|
-
const oldLines = splitLines(oldContent);
|
|
146
|
-
const newLines = splitLines(newContent);
|
|
147
|
-
const newHashes = new Array(newLines.length);
|
|
148
|
-
const used = new Uint32Array(BITSET_WORDS);
|
|
149
|
-
const hint = { value: 0 };
|
|
150
|
-
const removed = removedHashes ?? new Set();
|
|
151
|
-
const oldHashIndex = new Map();
|
|
152
|
-
for (let i = 0; i < oldHashes.length; i++) {
|
|
153
|
-
const hash = oldHashes[i];
|
|
154
|
-
oldHashIndex.set(hash, i);
|
|
155
|
-
const idx = hashToIndex(hash);
|
|
156
|
-
if (idx >= 0)
|
|
157
|
-
setBit(used, idx);
|
|
158
|
-
}
|
|
159
|
-
const removedIndexes = new Set();
|
|
160
|
-
for (const hash of removed) {
|
|
161
|
-
const idx = oldHashIndex.get(hash);
|
|
162
|
-
if (idx !== undefined)
|
|
163
|
-
removedIndexes.add(idx);
|
|
164
|
-
}
|
|
165
|
-
let spanStart = oldLines.length;
|
|
166
|
-
let spanEnd = -1;
|
|
167
|
-
for (const idx of removedIndexes) {
|
|
168
|
-
if (idx < spanStart)
|
|
169
|
-
spanStart = idx;
|
|
170
|
-
if (idx > spanEnd)
|
|
171
|
-
spanEnd = idx;
|
|
172
|
-
}
|
|
173
|
-
const spanLen = spanEnd >= spanStart ? spanEnd - spanStart + 1 : 0;
|
|
174
|
-
const replacementLen = newLines.length - oldLines.length + spanLen;
|
|
175
|
-
const shiftAfterSpan = spanEnd >= spanStart ? replacementLen - spanLen : 0;
|
|
176
|
-
const survivors = [];
|
|
177
|
-
const removedEntries = [];
|
|
178
|
-
for (let i = 0; i < oldLines.length; i++) {
|
|
179
|
-
const entry = { index: i, hash: oldHashes[i] };
|
|
180
|
-
if (removedIndexes.has(i))
|
|
181
|
-
removedEntries.push(entry);
|
|
182
|
-
else
|
|
183
|
-
survivors.push(entry);
|
|
184
|
-
}
|
|
185
|
-
const newByContent = new Map();
|
|
186
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
187
|
-
const key = canon(newLines[i]);
|
|
188
|
-
const list = newByContent.get(key);
|
|
189
|
-
if (list)
|
|
190
|
-
list.push(i);
|
|
191
|
-
else
|
|
192
|
-
newByContent.set(key, [i]);
|
|
193
|
-
}
|
|
194
|
-
const markUsed = (hash) => {
|
|
195
|
-
const idx = hashToIndex(hash);
|
|
196
|
-
if (idx >= 0) {
|
|
197
|
-
setBit(used, idx);
|
|
198
|
-
if (idx + HASH_PROBE_STRIDE > hint.value)
|
|
199
|
-
hint.value = idx + HASH_PROBE_STRIDE;
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
for (const entry of survivors) {
|
|
203
|
-
const candidates = newByContent.get(canon(oldLines[entry.index]));
|
|
204
|
-
if (!candidates || candidates.length === 0)
|
|
205
|
-
continue;
|
|
206
|
-
const target = entry.index > spanEnd ? entry.index + shiftAfterSpan : entry.index;
|
|
207
|
-
const pos = nearestNew(candidates, target);
|
|
208
|
-
if (pos < 0)
|
|
209
|
-
continue;
|
|
210
|
-
const newIdx = candidates.splice(pos, 1)[0];
|
|
211
|
-
newHashes[newIdx] = entry.hash;
|
|
212
|
-
markUsed(entry.hash);
|
|
213
|
-
}
|
|
214
|
-
const removedByContent = new Map();
|
|
215
|
-
for (const entry of removedEntries) {
|
|
216
|
-
const key = canon(oldLines[entry.index]);
|
|
217
|
-
let queue = removedByContent.get(key);
|
|
218
|
-
if (!queue) {
|
|
219
|
-
queue = { hashes: [], pos: 0 };
|
|
220
|
-
removedByContent.set(key, queue);
|
|
221
|
-
}
|
|
222
|
-
queue.hashes.push(entry.hash);
|
|
223
|
-
}
|
|
224
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
225
|
-
if (newHashes[i])
|
|
226
|
-
continue;
|
|
227
|
-
const queue = removedByContent.get(canon(newLines[i]));
|
|
228
|
-
if (!queue || queue.pos >= queue.hashes.length)
|
|
229
|
-
continue;
|
|
230
|
-
newHashes[i] = queue.hashes[queue.pos];
|
|
231
|
-
queue.pos += 1;
|
|
232
|
-
}
|
|
233
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
234
|
-
if (newHashes[i])
|
|
235
|
-
continue;
|
|
236
|
-
const c = canon(newLines[i]);
|
|
237
|
-
const baseIdx = (xxh32(c) >>> 14) % HASH_SPACE;
|
|
238
|
-
newHashes[i] = assignHash(used, baseIdx, hint);
|
|
239
|
-
}
|
|
240
|
-
return newHashes;
|
|
241
|
-
}
|
package/lib/hashline/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ANCHOR_LEN, HASH_SEP, HASH_SPACE, HASH_PROBE_STRIDE, MAX_HASH_LINES, lineHashesPure, initHasher, canon, } from "./pure.js";
|
|
2
|
+
export { lineHashes } from "./hash.js";
|
|
2
3
|
export { parseHashRef, parseText, } from "./parse.js";
|
|
3
|
-
export { resEdit,
|
|
4
|
-
export {
|
|
4
|
+
export { resEdit, } from "./resolve.js";
|
|
5
|
+
export { applyEdit, fmtRegion, changedRange, } from "./apply.js";
|
package/lib/hashline/parse.js
CHANGED