@criogaid/pi-hashline-edit 0.8.0 → 0.10.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/README.md +13 -10
- package/README.zh-CN.md +12 -9
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +118 -293
- package/src/core/apply.ts +63 -165
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.ts +4 -4
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +16 -8
- package/src/core/ranges.ts +10 -6
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +7 -43
- package/src/integration/config-registration.test.ts +2 -2
- package/src/integration/fusion-dispatch.test.ts +2 -5
- package/src/integration/grep-flags.test.ts +0 -14
- package/src/integration/grep-js-completeness.test.ts +34 -8
- package/src/integration/grep-line-candidates.test.ts +4 -8
- package/src/integration/grep-matrix.test.ts +3 -7
- package/src/integration/grep-path-matrix.test.ts +5 -5
- package/src/integration/grep-prefilter-coverage.test.ts +3 -7
- package/src/integration/grep-process-limits.test.ts +78 -0
- package/src/integration/grep-rg.test.ts +123 -25
- package/src/integration/grep-scan-budget.test.ts +2 -2
- package/src/integration/grep-translation.test.ts +12 -27
- package/src/integration/large-text.test.ts +1 -47
- package/src/integration/model-budget-lifecycle.test.ts +13 -3
- package/src/integration/mutation-file-types.testing.ts +1 -1
- package/src/integration/npm-package.testing.ts +1 -1
- package/src/integration/publication-read-changes.test.ts +125 -47
- package/src/integration/replace-worker-boundary.test.ts +21 -11
- package/src/integration/report-replay.test.ts +21 -23
- package/src/integration/utf8-errors.test.ts +1 -1
- package/src/pi/AGENTS.md +16 -16
- package/src/pi/action-fusion.test.ts +168 -300
- package/src/pi/action-fusion.ts +148 -111
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +52 -207
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +20 -22
- package/src/pi/argument-validation.test.ts +382 -418
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +5 -4
- package/src/pi/config.test.ts +5 -2
- package/src/pi/config.ts +4 -6
- package/src/pi/diagnostic-buffer.test.ts +12 -1
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +173 -226
- package/src/pi/error-text.ts +4 -8
- package/src/pi/execute.test.ts +165 -266
- package/src/pi/failure-context.test.ts +144 -126
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +168 -141
- package/src/pi/file-commit.test.ts +110 -53
- package/src/pi/file-commit.ts +90 -137
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +4 -1
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +2 -101
- package/src/pi/grep-candidates.ts +23 -69
- package/src/pi/grep-matrix.test.ts +2 -2
- package/src/pi/grep-order.test.ts +4 -4
- package/src/pi/grep-output.ts +62 -96
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +92 -0
- package/src/pi/grep-scope.ts +40 -42
- package/src/pi/grep-search.ts +22 -31
- package/src/pi/grep-tool.ts +55 -82
- package/src/pi/grep.test.ts +81 -46
- package/src/pi/model-budgets.test.ts +25 -23
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +51 -20
- package/src/pi/mutation-runner.ts +126 -70
- package/src/pi/output-budgets.test.ts +38 -16
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +3 -1
- package/src/pi/read-tool.ts +5 -5
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +82 -20
- package/src/pi/render.test.ts +79 -42
- package/src/pi/render.ts +69 -63
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -83
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +9 -5
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +1 -2
- package/src/pi/report-schema.ts +65 -81
- package/src/pi/report.test.ts +171 -25
- package/src/pi/report.ts +33 -87
- package/src/pi/result-safety.test.ts +177 -189
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +48 -64
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +12 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/write-tool.ts +33 -62
- package/src/pi/write.test.ts +1 -3
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fsPromises from "node:fs/promises";
|
|
2
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
1
3
|
import {
|
|
2
4
|
chmod,
|
|
3
5
|
lstat,
|
|
@@ -19,18 +21,19 @@ import { test } from "node:test";
|
|
|
19
21
|
import { spawn } from "node:child_process";
|
|
20
22
|
import {
|
|
21
23
|
commitFile,
|
|
22
|
-
commitReplacement,
|
|
23
24
|
FileMutationError,
|
|
24
|
-
fileRevision,
|
|
25
25
|
readEditableSnapshot,
|
|
26
26
|
type CommitResult,
|
|
27
27
|
type FreshnessObservation,
|
|
28
28
|
} from "./file-commit.ts";
|
|
29
29
|
import { byteRevision } from "./file-revision.ts";
|
|
30
|
+
import { HashlineError, PATH_ERROR_MESSAGES } from "../core/errors.ts";
|
|
30
31
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
31
32
|
import { makeWriteOverride } from "./write-tool.ts";
|
|
32
33
|
import { callTool } from "./tool-call.testing.ts";
|
|
34
|
+
import { describeMutationFailure } from "./mutation-result.ts";
|
|
33
35
|
import { reportOf } from "./report.ts";
|
|
36
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
|
|
34
37
|
|
|
35
38
|
// Checked by tsc; these states cannot be returned by the commit layer.
|
|
36
39
|
function invalidCommitStates() {
|
|
@@ -120,8 +123,8 @@ test("independent create commits race at the no-replace publication primitive",
|
|
|
120
123
|
const rejected = results.find(
|
|
121
124
|
(result) => result.status === "rejected",
|
|
122
125
|
) as PromiseRejectedResult;
|
|
123
|
-
assert.ok(rejected.reason instanceof
|
|
124
|
-
assert.equal(rejected.reason.
|
|
126
|
+
assert.ok(rejected.reason instanceof HashlineError);
|
|
127
|
+
assert.equal(rejected.reason.errorCode, "TARGET_EXISTS");
|
|
125
128
|
assert.match(await readFile(target, "utf8"), /^winner-[ab]\n$/);
|
|
126
129
|
}));
|
|
127
130
|
|
|
@@ -129,13 +132,12 @@ test("create never overwrites an existing target", async () =>
|
|
|
129
132
|
withTemp(async (dir) => {
|
|
130
133
|
const target = join(dir, "existing.txt");
|
|
131
134
|
await writeFile(target, "original\n");
|
|
132
|
-
const revision = await
|
|
135
|
+
const revision = byteRevision(await readFile(target));
|
|
133
136
|
await assert.rejects(
|
|
134
137
|
commitFile(target, "replacement\n", { mode: "create" }),
|
|
135
|
-
(error: unknown) =>
|
|
136
|
-
error instanceof FileMutationError && error.publication === "NOT_PUBLISHED",
|
|
138
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "TARGET_EXISTS",
|
|
137
139
|
);
|
|
138
|
-
assert.equal(await
|
|
140
|
+
assert.equal(byteRevision(await readFile(target)), revision);
|
|
139
141
|
assert.equal(await readFile(target, "utf8"), "original\n");
|
|
140
142
|
}));
|
|
141
143
|
|
|
@@ -216,7 +218,7 @@ test("replacement rejects a multi-hardlink target without changing either link",
|
|
|
216
218
|
assert.equal((await stat(target)).nlink, 2);
|
|
217
219
|
}));
|
|
218
220
|
|
|
219
|
-
test("existing
|
|
221
|
+
test("files are replaced or created → preserve existing modes and apply umask to new files", async (t) =>
|
|
220
222
|
withTemp(async (dir) => {
|
|
221
223
|
if (process.platform === "win32")
|
|
222
224
|
return t.skip("Windows mode bits are not an ACL preservation test");
|
|
@@ -227,14 +229,19 @@ test("existing private and executable permissions survive replacement; new files
|
|
|
227
229
|
assert.equal((await stat(existing)).mode & 0o777, 0o750);
|
|
228
230
|
const created = join(dir, "created.txt");
|
|
229
231
|
await commitFile(created, "new\n", { mode: "create" });
|
|
230
|
-
assert.equal((await stat(created)).mode & 0o777,
|
|
232
|
+
assert.equal((await stat(created)).mode & 0o777, 0o666 & ~process.umask());
|
|
231
233
|
}));
|
|
232
234
|
|
|
233
235
|
test("directories are rejected instead of entering regular-file publication", async () =>
|
|
234
236
|
withTemp(async (dir) => {
|
|
235
237
|
const target = join(dir, "directory");
|
|
236
238
|
await mkdir(target);
|
|
237
|
-
await assert.rejects(commitFile(target, "bad\n", { mode: "overwrite" }),
|
|
239
|
+
await assert.rejects(commitFile(target, "bad\n", { mode: "overwrite" }), (error: unknown) => {
|
|
240
|
+
assert.ok(error instanceof HashlineError);
|
|
241
|
+
assert.equal(error.errorCode, "NOT_REGULAR_FILE");
|
|
242
|
+
assert.equal(error.message, PATH_ERROR_MESSAGES.NOT_REGULAR_FILE);
|
|
243
|
+
return true;
|
|
244
|
+
});
|
|
238
245
|
}));
|
|
239
246
|
|
|
240
247
|
test("Windows shared access failure preserves the target, skips then_run, and releases the queue", async (t) =>
|
|
@@ -322,8 +329,13 @@ test("cancellation racing publication never reports a settled operation as unpub
|
|
|
322
329
|
publication = result.publication;
|
|
323
330
|
assert.equal(publication, "PUBLISHED");
|
|
324
331
|
} catch (error) {
|
|
325
|
-
assert.ok(error instanceof
|
|
326
|
-
|
|
332
|
+
assert.ok(error instanceof HashlineError);
|
|
333
|
+
if (!(error instanceof FileMutationError)) assert.equal(error.errorCode, "OPERATION_ABORTED");
|
|
334
|
+
const reported = describeMutationFailure("write", error, {
|
|
335
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
336
|
+
}).publication;
|
|
337
|
+
assert.ok(reported);
|
|
338
|
+
publication = reported;
|
|
327
339
|
}
|
|
328
340
|
const finalContent = await readFile(target, "utf8");
|
|
329
341
|
assert.ok(finalContent === oldContent || finalContent === newContent);
|
|
@@ -335,7 +347,7 @@ test("bound overwrite → reports published and observed revisions for the publi
|
|
|
335
347
|
withTemp(async (dir) => {
|
|
336
348
|
const target = join(dir, "versions.txt");
|
|
337
349
|
await writeFile(target, "before\n");
|
|
338
|
-
const baseRevision = await
|
|
350
|
+
const baseRevision = byteRevision(await readFile(target));
|
|
339
351
|
const result = await commitFile(target, "after\n", {
|
|
340
352
|
mode: "overwrite",
|
|
341
353
|
baseRevision,
|
|
@@ -356,9 +368,7 @@ test("commit rejects lossy UTF-8 output before modifying or creating files", asy
|
|
|
356
368
|
await assert.rejects(
|
|
357
369
|
commitFile(path, content, { mode: path === existing ? "overwrite" : "create" }),
|
|
358
370
|
(error: unknown) =>
|
|
359
|
-
error instanceof
|
|
360
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
361
|
-
error.errorCode === "INVALID_UNICODE",
|
|
371
|
+
error instanceof HashlineError && error.errorCode === "INVALID_UNICODE",
|
|
362
372
|
);
|
|
363
373
|
}
|
|
364
374
|
}
|
|
@@ -376,7 +386,7 @@ test("identical commits preserve the file and still enforce mode, revision, and
|
|
|
376
386
|
const content = "\uFEFFsame\r\nbytes\n";
|
|
377
387
|
await writeFile(target, content);
|
|
378
388
|
const before = await stat(target);
|
|
379
|
-
const revision = await
|
|
389
|
+
const revision = byteRevision(await readFile(target));
|
|
380
390
|
const result = await commitFile(target, content, {
|
|
381
391
|
mode: "overwrite",
|
|
382
392
|
baseRevision: revision,
|
|
@@ -396,26 +406,37 @@ test("identical commits preserve the file and still enforce mode, revision, and
|
|
|
396
406
|
await assert.rejects(
|
|
397
407
|
commitFile(target, content, { mode: "overwrite", baseRevision: "stale" }),
|
|
398
408
|
(error: unknown) =>
|
|
399
|
-
error instanceof
|
|
409
|
+
error instanceof HashlineError &&
|
|
400
410
|
error.errorCode === "FILE_CHANGED" &&
|
|
401
|
-
error.
|
|
402
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
403
|
-
/before publication/.test(error.message),
|
|
411
|
+
error.message === "File changed after it was read.",
|
|
404
412
|
);
|
|
405
413
|
await assert.rejects(
|
|
406
414
|
commitFile(target, content, { mode: "overwrite", signal: AbortSignal.abort() }),
|
|
407
|
-
(error: unknown) =>
|
|
408
|
-
error instanceof FileMutationError && error.publication === "NOT_PUBLISHED",
|
|
409
|
-
);
|
|
410
|
-
await assert.rejects(
|
|
411
|
-
commitFile(join(dir, "missing"), "", { mode: "overwrite" }),
|
|
412
|
-
/does not exist/,
|
|
415
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "OPERATION_ABORTED",
|
|
413
416
|
);
|
|
414
417
|
const created = await commitFile(join(dir, "empty"), "", { mode: "create" });
|
|
415
418
|
assert.equal(created.created, true);
|
|
416
419
|
assert.equal(created.publication, "PUBLISHED");
|
|
417
420
|
}));
|
|
418
421
|
|
|
422
|
+
test("missing overwrite target → suggests create only without a base revision", async () =>
|
|
423
|
+
withTemp(async (dir) => {
|
|
424
|
+
const missing = join(dir, "missing");
|
|
425
|
+
for (const baseRevision of [undefined, "previous-revision"]) {
|
|
426
|
+
await assert.rejects(
|
|
427
|
+
commitFile(missing, "", { mode: "overwrite", baseRevision }),
|
|
428
|
+
(error: unknown) => {
|
|
429
|
+
assert.ok(error instanceof HashlineError);
|
|
430
|
+
assert.equal(error.errorCode, "PATH_NOT_FOUND");
|
|
431
|
+
assert.equal(error.message, PATH_ERROR_MESSAGES.PATH_NOT_FOUND);
|
|
432
|
+
assert.deepEqual(error.facts, { missing: [missing] });
|
|
433
|
+
assert.equal(error.next, baseRevision === undefined ? "useCreate" : undefined);
|
|
434
|
+
return true;
|
|
435
|
+
},
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
}));
|
|
439
|
+
|
|
419
440
|
test("file changes after the read → bound commits reject changed and unchanged output", async () =>
|
|
420
441
|
withTemp(async (dir) => {
|
|
421
442
|
const target = join(dir, "target.txt");
|
|
@@ -426,25 +447,16 @@ test("file changes after the read → bound commits reject changed and unchanged
|
|
|
426
447
|
|
|
427
448
|
// External process modifies target after snapshot is read.
|
|
428
449
|
await writeFile(target, "line 1\nmodified line 2\n");
|
|
429
|
-
const externalRevision = await
|
|
450
|
+
const externalRevision = byteRevision(await readFile(target));
|
|
430
451
|
|
|
431
452
|
for (const content of [snapshot.text, "replacement\n"]) {
|
|
432
|
-
|
|
433
|
-
() => commitReplacement(target, content, snapshot.baseRevision),
|
|
453
|
+
await assert.rejects(
|
|
434
454
|
() =>
|
|
435
455
|
commitFile(target, content, { mode: "overwrite", baseRevision: snapshot.baseRevision }),
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
error instanceof FileMutationError &&
|
|
441
|
-
error.stage === "prepare" &&
|
|
442
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
443
|
-
error.errorCode === "FILE_CHANGED",
|
|
444
|
-
);
|
|
445
|
-
assert.equal(await readFile(target, "utf8"), "line 1\nmodified line 2\n");
|
|
446
|
-
assert.equal(await fileRevision(target), externalRevision);
|
|
447
|
-
}
|
|
456
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "FILE_CHANGED",
|
|
457
|
+
);
|
|
458
|
+
assert.equal(await readFile(target, "utf8"), "line 1\nmodified line 2\n");
|
|
459
|
+
assert.equal(byteRevision(await readFile(target)), externalRevision);
|
|
448
460
|
}
|
|
449
461
|
|
|
450
462
|
// Without baseRevision, overwrite hashes the current target instead of using the snapshot.
|
|
@@ -458,11 +470,10 @@ test("file changes after the read → bound commits reject changed and unchanged
|
|
|
458
470
|
|
|
459
471
|
// When target does not change externally, no-op commit succeeds and confirms observed revision.
|
|
460
472
|
const freshSnapshot = await readEditableSnapshot(target);
|
|
461
|
-
const noopResult = await
|
|
462
|
-
|
|
463
|
-
freshSnapshot.
|
|
464
|
-
|
|
465
|
-
);
|
|
473
|
+
const noopResult = await commitFile(target, freshSnapshot.text, {
|
|
474
|
+
mode: "overwrite",
|
|
475
|
+
baseRevision: freshSnapshot.baseRevision,
|
|
476
|
+
});
|
|
466
477
|
assert.equal(noopResult.publication, "NOT_PUBLISHED");
|
|
467
478
|
assert.equal(noopResult.publishedRevision, freshSnapshot.baseRevision);
|
|
468
479
|
assert.equal(noopResult.observedRevision, freshSnapshot.baseRevision);
|
|
@@ -477,10 +488,56 @@ test("no-op commit with baseRevision rejects if target was deleted externally",
|
|
|
477
488
|
await rm(target);
|
|
478
489
|
|
|
479
490
|
await assert.rejects(
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
491
|
+
commitFile(target, snapshot.text, {
|
|
492
|
+
mode: "overwrite",
|
|
493
|
+
baseRevision: snapshot.baseRevision,
|
|
494
|
+
}),
|
|
495
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "PATH_NOT_FOUND",
|
|
485
496
|
);
|
|
486
497
|
}));
|
|
498
|
+
|
|
499
|
+
test("commitFile temporary publication → handles existing and missing parents and preserves other failures", async (t) =>
|
|
500
|
+
withTemp(async (dir) => {
|
|
501
|
+
const originalMkdir = fsPromises.mkdir;
|
|
502
|
+
const originalMkdtemp = fsPromises.mkdtemp;
|
|
503
|
+
for (const scenario of ["existing", "nested", "denied"] as const) {
|
|
504
|
+
const parent = scenario === "nested" ? join(dir, "new", "nested") : dir;
|
|
505
|
+
const target = join(parent, `${scenario}.txt`);
|
|
506
|
+
let mkdirCalls = 0;
|
|
507
|
+
const denied = Object.assign(new Error("denied"), { code: "EPERM" });
|
|
508
|
+
try {
|
|
509
|
+
t.mock.method(fsPromises, "mkdir", async (...args: Parameters<typeof originalMkdir>) => {
|
|
510
|
+
mkdirCalls++;
|
|
511
|
+
if (scenario !== "nested") throw denied;
|
|
512
|
+
return originalMkdir(...args);
|
|
513
|
+
});
|
|
514
|
+
t.mock.method(
|
|
515
|
+
fsPromises,
|
|
516
|
+
"mkdtemp",
|
|
517
|
+
async (...args: Parameters<typeof originalMkdtemp>) => {
|
|
518
|
+
if (scenario === "denied") throw denied;
|
|
519
|
+
return originalMkdtemp(...args);
|
|
520
|
+
},
|
|
521
|
+
);
|
|
522
|
+
syncBuiltinESMExports();
|
|
523
|
+
if (scenario === "denied") {
|
|
524
|
+
await assert.rejects(
|
|
525
|
+
commitFile(target, "data", { mode: "create" }),
|
|
526
|
+
(error: unknown) =>
|
|
527
|
+
error instanceof HashlineError &&
|
|
528
|
+
error.errorCode === "PUBLISH_FAILED" &&
|
|
529
|
+
error.cause === denied,
|
|
530
|
+
);
|
|
531
|
+
await assert.rejects(readFile(target), { code: "ENOENT" });
|
|
532
|
+
} else {
|
|
533
|
+
const result = await commitFile(target, "data", { mode: "create" });
|
|
534
|
+
assert.equal(result.publication, "PUBLISHED");
|
|
535
|
+
assert.equal(await readFile(target, "utf8"), "data");
|
|
536
|
+
}
|
|
537
|
+
assert.equal(mkdirCalls, scenario === "nested" ? 1 : 0);
|
|
538
|
+
} finally {
|
|
539
|
+
t.mock.restoreAll();
|
|
540
|
+
syncBuiltinESMExports();
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}));
|
package/src/pi/file-commit.ts
CHANGED
|
@@ -24,20 +24,20 @@ import { decodeEditableText, unwritableTextError } from "../core/text.ts";
|
|
|
24
24
|
import {
|
|
25
25
|
errnoCode,
|
|
26
26
|
filesystemErrorCode,
|
|
27
|
+
PATH_ERROR_MESSAGES,
|
|
28
|
+
pathError,
|
|
27
29
|
HashlineError,
|
|
28
|
-
type ErrorCode,
|
|
29
|
-
type HashlineErrorArguments,
|
|
30
30
|
} from "../core/errors.ts";
|
|
31
|
-
import {
|
|
31
|
+
import { FileChangedDuringReadError, throwIfCancelled } from "./error-text.ts";
|
|
32
32
|
import { withFileRead } from "./file-read.ts";
|
|
33
|
-
import type { Freshness,
|
|
33
|
+
import type { Freshness, PublicationStatus } from "./report-schema.ts";
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* How a commit treats the target. `create` requires that it not exist. `overwrite` requires
|
|
37
37
|
* that it exist; with `baseRevision` (read-modify-write), the target must still hold the bytes
|
|
38
38
|
* that revision names, and that revision stands in for hashing the target again.
|
|
39
39
|
*/
|
|
40
|
-
|
|
40
|
+
type CommitOptions = { readonly signal?: AbortSignal } & (
|
|
41
41
|
| { readonly mode: "create" }
|
|
42
42
|
| { readonly mode: "overwrite"; readonly baseRevision?: string }
|
|
43
43
|
);
|
|
@@ -60,29 +60,27 @@ export type CommitResult = CommitRevisions &
|
|
|
60
60
|
);
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
|
-
* A mutation failure with
|
|
64
|
-
*
|
|
63
|
+
* A mutation failure with what it published. Every other failure of a mutation
|
|
64
|
+
* tool published nothing, so it stays a plain HashlineError.
|
|
65
65
|
*/
|
|
66
|
-
export class FileMutationError
|
|
67
|
-
readonly stage: MutationStage;
|
|
66
|
+
export class FileMutationError extends HashlineError<"PUBLISH_FAILED" | "POST_PROCESS_FAILED"> {
|
|
68
67
|
readonly publication: PublicationStatus;
|
|
69
68
|
|
|
70
69
|
constructor(
|
|
71
|
-
code:
|
|
72
|
-
stage: MutationStage,
|
|
70
|
+
code: "PUBLISH_FAILED" | "POST_PROCESS_FAILED",
|
|
73
71
|
publication: PublicationStatus,
|
|
74
72
|
message: string,
|
|
75
|
-
|
|
73
|
+
options?: { readonly cause?: unknown },
|
|
76
74
|
) {
|
|
77
|
-
super(code, message,
|
|
75
|
+
super(code, message, options);
|
|
78
76
|
this.name = "FileMutationError";
|
|
79
|
-
this.stage = stage;
|
|
80
77
|
this.publication = publication;
|
|
81
78
|
}
|
|
82
79
|
}
|
|
83
80
|
|
|
84
81
|
function requireRegularFile(target: Stats | BigIntStats): void {
|
|
85
|
-
if (!target.isFile())
|
|
82
|
+
if (!target.isFile())
|
|
83
|
+
throw new HashlineError("NOT_REGULAR_FILE", PATH_ERROR_MESSAGES.NOT_REGULAR_FILE);
|
|
86
84
|
}
|
|
87
85
|
|
|
88
86
|
/** Check the opened object before reading; a FIFO must not block even if the path changed. */
|
|
@@ -93,7 +91,7 @@ async function readRegularFile(path: string, signal?: AbortSignal): Promise<Buff
|
|
|
93
91
|
});
|
|
94
92
|
}
|
|
95
93
|
|
|
96
|
-
|
|
94
|
+
async function fileRevision(path: string): Promise<string> {
|
|
97
95
|
return byteRevision(await readRegularFile(path));
|
|
98
96
|
}
|
|
99
97
|
|
|
@@ -103,59 +101,40 @@ export async function readEditableSnapshot(path: string, signal?: AbortSignal) {
|
|
|
103
101
|
const bytes = await readRegularFile(path, signal);
|
|
104
102
|
return { text: decodeEditableText(bytes), baseRevision: byteRevision(bytes) };
|
|
105
103
|
} catch (error) {
|
|
106
|
-
|
|
104
|
+
throwIfCancelled(signal);
|
|
107
105
|
throw error;
|
|
108
106
|
}
|
|
109
107
|
}
|
|
110
108
|
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
code: C,
|
|
123
|
-
message: string,
|
|
124
|
-
...options: HashlineErrorArguments<NoInfer<C>>
|
|
125
|
-
): FileMutationError<C> {
|
|
126
|
-
return new FileMutationError(code, "prepare", "NOT_PUBLISHED", message, ...options);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** A classified failure before any publication step keeps its code, facts, cause, and recovery. */
|
|
130
|
-
function asPrepareError<C extends ErrorCode>(error: HashlineError<C>): FileMutationError<C> {
|
|
131
|
-
// The original constructor already enforced its code's facts; preserve that correlated tuple.
|
|
132
|
-
return prepareError(
|
|
133
|
-
error.errorCode,
|
|
134
|
-
error.message,
|
|
135
|
-
...([
|
|
136
|
-
{
|
|
137
|
-
cause: error.cause,
|
|
138
|
-
next: error.next,
|
|
139
|
-
facts: error.facts,
|
|
140
|
-
},
|
|
141
|
-
] as HashlineErrorArguments<C>),
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function throwIfCancelledBeforePublication(signal: AbortSignal | undefined): void {
|
|
146
|
-
if (signal?.aborted) throw asPrepareError(cancellationError());
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** A failed filesystem step keeps the classification of its cause. */
|
|
150
|
-
function causeCode(error: unknown, fallback: ErrorCode): ErrorCode {
|
|
151
|
-
return error instanceof HashlineError
|
|
152
|
-
? error.errorCode
|
|
153
|
-
: (filesystemErrorCode(error) ?? fallback);
|
|
109
|
+
/**
|
|
110
|
+
* A failure of a filesystem step before publication: the step is the message, the failure its
|
|
111
|
+
* cause, whose classification it keeps. A missing path or directory target keeps its own report,
|
|
112
|
+
* whichever step found it.
|
|
113
|
+
*/
|
|
114
|
+
function stepError(step: string, error: unknown): HashlineError {
|
|
115
|
+
const code =
|
|
116
|
+
error instanceof HashlineError
|
|
117
|
+
? error.errorCode
|
|
118
|
+
: (filesystemErrorCode(error) ?? "FILESYSTEM_ERROR");
|
|
119
|
+
return pathError(error) ?? new HashlineError(code, step, { cause: error });
|
|
154
120
|
}
|
|
155
121
|
|
|
156
|
-
/**
|
|
157
|
-
function
|
|
158
|
-
|
|
122
|
+
/** The target must still hold the bytes a mutation read; cancellation wins over what this read finds. */
|
|
123
|
+
async function requireBaseRevision(
|
|
124
|
+
path: string,
|
|
125
|
+
baseRevision: string,
|
|
126
|
+
signal: AbortSignal | undefined,
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
let currentRevision: string;
|
|
129
|
+
try {
|
|
130
|
+
currentRevision = await fileRevision(path);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
throwIfCancelled(signal);
|
|
133
|
+
throw stepError("Unable to recheck target revision.", error);
|
|
134
|
+
}
|
|
135
|
+
throwIfCancelled(signal);
|
|
136
|
+
if (currentRevision !== baseRevision)
|
|
137
|
+
throw new HashlineError("FILE_CHANGED", "File changed after it was read.");
|
|
159
138
|
}
|
|
160
139
|
|
|
161
140
|
type TargetInfo = { readonly publishPath: string } & (
|
|
@@ -178,7 +157,7 @@ async function inspectTarget(path: string, knownRevision?: string): Promise<Targ
|
|
|
178
157
|
try {
|
|
179
158
|
publishPath = await realpath(path);
|
|
180
159
|
} catch (error) {
|
|
181
|
-
throw
|
|
160
|
+
throw new HashlineError("SYMLINK_UNRESOLVED", "Target symlink cannot be resolved.", {
|
|
182
161
|
cause: error,
|
|
183
162
|
});
|
|
184
163
|
}
|
|
@@ -192,7 +171,7 @@ async function inspectTarget(path: string, knownRevision?: string): Promise<Targ
|
|
|
192
171
|
}
|
|
193
172
|
requireRegularFile(target);
|
|
194
173
|
if (target.nlink > 1)
|
|
195
|
-
throw
|
|
174
|
+
throw new HashlineError(
|
|
196
175
|
"MULTIPLE_HARD_LINKS",
|
|
197
176
|
"Target has multiple hard links; publishing would split the link set.",
|
|
198
177
|
);
|
|
@@ -222,7 +201,8 @@ async function writeAndSyncTemp(
|
|
|
222
201
|
signal: AbortSignal | undefined,
|
|
223
202
|
): Promise<void> {
|
|
224
203
|
throwIfCancelled(signal);
|
|
225
|
-
|
|
204
|
+
// A new file takes the umask's default mode; its private temp directory hides it until publication.
|
|
205
|
+
const handle = await open(tempPath, "w");
|
|
226
206
|
try {
|
|
227
207
|
await handle.writeFile(content);
|
|
228
208
|
await handle.sync();
|
|
@@ -243,21 +223,11 @@ async function publishCreate(
|
|
|
243
223
|
await link(tempPath, targetPath);
|
|
244
224
|
} catch (error) {
|
|
245
225
|
if (errnoCode(error) === "EEXIST" || errnoCode(error) === "ENOTEMPTY") {
|
|
246
|
-
throw new
|
|
247
|
-
"TARGET_EXISTS",
|
|
248
|
-
"commit",
|
|
249
|
-
"NOT_PUBLISHED",
|
|
250
|
-
"Target appeared during create.",
|
|
251
|
-
{ cause: error },
|
|
252
|
-
);
|
|
226
|
+
throw new HashlineError("TARGET_EXISTS", "Target appeared during create.", { cause: error });
|
|
253
227
|
}
|
|
254
|
-
throw new FileMutationError(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
"UNKNOWN",
|
|
258
|
-
"Unable to publish new target.",
|
|
259
|
-
{ cause: error },
|
|
260
|
-
);
|
|
228
|
+
throw new FileMutationError("PUBLISH_FAILED", "UNKNOWN", "Unable to publish new target.", {
|
|
229
|
+
cause: error,
|
|
230
|
+
});
|
|
261
231
|
}
|
|
262
232
|
}
|
|
263
233
|
|
|
@@ -271,13 +241,9 @@ async function publishReplace(
|
|
|
271
241
|
try {
|
|
272
242
|
await rename(tempPath, targetPath);
|
|
273
243
|
} catch (error) {
|
|
274
|
-
throw new FileMutationError(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
"UNKNOWN",
|
|
278
|
-
"Unable to publish replacement.",
|
|
279
|
-
{ cause: error },
|
|
280
|
-
);
|
|
244
|
+
throw new FileMutationError("PUBLISH_FAILED", "UNKNOWN", "Unable to publish replacement.", {
|
|
245
|
+
cause: error,
|
|
246
|
+
});
|
|
281
247
|
}
|
|
282
248
|
}
|
|
283
249
|
|
|
@@ -294,6 +260,21 @@ async function syncDirectory(path: string): Promise<void> {
|
|
|
294
260
|
}
|
|
295
261
|
}
|
|
296
262
|
|
|
263
|
+
/**
|
|
264
|
+
* A temporary directory beside the target. Recursive mkdir fails with EPERM on a Windows
|
|
265
|
+
* drive root even though it exists, so it runs only when the directory is missing.
|
|
266
|
+
*/
|
|
267
|
+
async function makeTempDirectory(directory: string): Promise<string> {
|
|
268
|
+
const prefix = join(directory, ".hashline-commit-");
|
|
269
|
+
try {
|
|
270
|
+
return await mkdtemp(prefix);
|
|
271
|
+
} catch (error) {
|
|
272
|
+
if (errnoCode(error) !== "ENOENT") throw error;
|
|
273
|
+
}
|
|
274
|
+
await mkdir(directory, { recursive: true });
|
|
275
|
+
return mkdtemp(prefix);
|
|
276
|
+
}
|
|
277
|
+
|
|
297
278
|
/** Validate the target, skip identical content, otherwise sync and publish. Callers own the file queue. */
|
|
298
279
|
export async function commitFile(
|
|
299
280
|
path: string,
|
|
@@ -303,33 +284,26 @@ export async function commitFile(
|
|
|
303
284
|
// Tools reject such input in prepareArguments, but a transformation can still produce it
|
|
304
285
|
// (a regex without `u` can split a surrogate pair), so publication keeps the final check.
|
|
305
286
|
const unwritable = unwritableTextError(content);
|
|
306
|
-
if (unwritable) throw
|
|
287
|
+
if (unwritable) throw unwritable;
|
|
307
288
|
const bytes = Buffer.from(content, "utf8");
|
|
308
289
|
const { mode } = options;
|
|
309
290
|
const baseRevision = mode === "overwrite" ? options.baseRevision : undefined;
|
|
310
291
|
const target = await inspectTarget(path, baseRevision);
|
|
311
292
|
if (mode === "create" && target.existed)
|
|
312
|
-
throw
|
|
293
|
+
throw new HashlineError("TARGET_EXISTS", "Target already exists.");
|
|
313
294
|
if (mode === "overwrite" && !target.existed)
|
|
314
|
-
throw
|
|
315
|
-
|
|
295
|
+
throw new HashlineError("PATH_NOT_FOUND", PATH_ERROR_MESSAGES.PATH_NOT_FOUND, {
|
|
296
|
+
facts: { missing: [path] },
|
|
297
|
+
// A read-modify-write target vanished after its read; those tools have no mode to change.
|
|
298
|
+
next: baseRevision === undefined ? "useCreate" : undefined,
|
|
299
|
+
});
|
|
300
|
+
throwIfCancelled(options.signal);
|
|
316
301
|
const publishedRevision = byteRevision(bytes);
|
|
317
302
|
// Mode, revision, target safety, and cancellation checks still apply to no-ops.
|
|
318
303
|
if (target.beforeRevision === publishedRevision) {
|
|
319
|
-
// A base revision was not hashed from the target here
|
|
320
|
-
if (baseRevision !== undefined)
|
|
321
|
-
|
|
322
|
-
try {
|
|
323
|
-
throwIfCancelledBeforePublication(options.signal);
|
|
324
|
-
currentRevision = await fileRevision(target.publishPath);
|
|
325
|
-
throwIfCancelledBeforePublication(options.signal);
|
|
326
|
-
} catch (error) {
|
|
327
|
-
if (options.signal?.aborted) throw asPrepareError(cancellationError());
|
|
328
|
-
throw stepError("Unable to read target revision.", error);
|
|
329
|
-
}
|
|
330
|
-
if (currentRevision !== baseRevision)
|
|
331
|
-
throw prepareError("FILE_CHANGED", "File changed after it was read.");
|
|
332
|
-
}
|
|
304
|
+
// A base revision was not hashed from the target here.
|
|
305
|
+
if (baseRevision !== undefined)
|
|
306
|
+
await requireBaseRevision(target.publishPath, baseRevision, options.signal);
|
|
333
307
|
return {
|
|
334
308
|
created: false,
|
|
335
309
|
publishedRevision,
|
|
@@ -342,31 +316,18 @@ export async function commitFile(
|
|
|
342
316
|
const publishDirectory = dirname(publishPath);
|
|
343
317
|
let tempDir: string;
|
|
344
318
|
try {
|
|
345
|
-
await
|
|
346
|
-
tempDir = await mkdtemp(join(publishDirectory, ".hashline-commit-"));
|
|
319
|
+
tempDir = await makeTempDirectory(publishDirectory);
|
|
347
320
|
} catch (error) {
|
|
348
|
-
throw new
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
"NOT_PUBLISHED",
|
|
352
|
-
"Unable to prepare temporary publication area.",
|
|
353
|
-
{ cause: error },
|
|
354
|
-
);
|
|
321
|
+
throw new HashlineError("PUBLISH_FAILED", "Unable to prepare temporary publication area.", {
|
|
322
|
+
cause: error,
|
|
323
|
+
});
|
|
355
324
|
}
|
|
356
325
|
const tempPath = join(tempDir, "content");
|
|
357
326
|
let published = false;
|
|
358
327
|
try {
|
|
359
328
|
await writeAndSyncTemp(tempPath, bytes, target.modeBits, options.signal);
|
|
360
|
-
if (baseRevision !== undefined)
|
|
361
|
-
|
|
362
|
-
try {
|
|
363
|
-
currentRevision = await fileRevision(publishPath);
|
|
364
|
-
} catch (error) {
|
|
365
|
-
throw stepError("Unable to recheck target revision.", error);
|
|
366
|
-
}
|
|
367
|
-
if (currentRevision !== baseRevision)
|
|
368
|
-
throw prepareError("FILE_CHANGED", "File changed before publication.");
|
|
369
|
-
}
|
|
329
|
+
if (baseRevision !== undefined)
|
|
330
|
+
await requireBaseRevision(publishPath, baseRevision, options.signal);
|
|
370
331
|
if (mode === "create") await publishCreate(tempPath, publishPath, options.signal);
|
|
371
332
|
else await publishReplace(tempPath, publishPath, options.signal);
|
|
372
333
|
published = true;
|
|
@@ -384,32 +345,24 @@ export async function commitFile(
|
|
|
384
345
|
if (error instanceof FileChangedDuringReadError) return committed;
|
|
385
346
|
throw new FileMutationError(
|
|
386
347
|
"POST_PROCESS_FAILED",
|
|
387
|
-
"post_process",
|
|
388
348
|
"PUBLISHED",
|
|
389
349
|
"Final revision could not be read.",
|
|
390
350
|
{ cause: error },
|
|
391
351
|
);
|
|
392
352
|
}
|
|
393
353
|
} catch (error) {
|
|
394
|
-
|
|
354
|
+
// Already classified, including cancellation and every publication failure.
|
|
355
|
+
if (error instanceof HashlineError) throw error;
|
|
395
356
|
if (published)
|
|
396
357
|
throw new FileMutationError(
|
|
397
358
|
"POST_PROCESS_FAILED",
|
|
398
|
-
"post_process",
|
|
399
359
|
"PUBLISHED",
|
|
400
360
|
"Post-publication processing failed.",
|
|
401
361
|
{ cause: error },
|
|
402
362
|
);
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
throw new FileMutationError(
|
|
407
|
-
"PUBLISH_FAILED",
|
|
408
|
-
"commit",
|
|
409
|
-
"NOT_PUBLISHED",
|
|
410
|
-
"Unable to prepare or publish target.",
|
|
411
|
-
{ cause: error },
|
|
412
|
-
);
|
|
363
|
+
throw new HashlineError("PUBLISH_FAILED", "Unable to prepare or publish target.", {
|
|
364
|
+
cause: error,
|
|
365
|
+
});
|
|
413
366
|
} finally {
|
|
414
367
|
// Best effort: a leftover temporary directory changes neither the publication nor the recovery.
|
|
415
368
|
await rm(tempDir, { recursive: true, force: true }).catch(() => undefined);
|