@criogaid/pi-hashline-edit 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -0
- package/README.md +73 -17
- package/README.zh-CN.md +74 -18
- package/package.json +8 -7
- package/src/core/AGENTS.md +2 -2
- package/src/core/apply.test.ts +15 -2
- package/src/core/apply.ts +6 -10
- package/src/core/case-fold.test.ts +16 -25
- package/src/core/case-fold.ts +4 -4
- package/src/core/character-set.test.ts +1 -6
- package/src/core/character-set.ts +7 -61
- package/src/core/regex.ts +2 -32
- package/src/core/replace.test.ts +51 -0
- package/src/core/replace.ts +10 -4
- package/src/core/search-analysis.test.ts +66 -61
- package/src/core/search-analysis.ts +24 -39
- package/src/core/search-matrix.test.ts +2 -2
- package/src/core/search-matrix.testing.ts +6 -16
- package/src/core/search-schema.ts +0 -1
- package/src/core/search.test.ts +6 -19
- package/src/core/search.ts +10 -8
- package/src/index.ts +28 -3
- package/src/integration/config-registration.test.ts +77 -4
- package/src/integration/fusion-dispatch.test.ts +47 -2
- package/src/integration/grep-errors.test.ts +31 -0
- package/src/integration/grep-flags.test.ts +13 -196
- package/src/integration/grep-js-completeness.test.ts +41 -8
- package/src/integration/grep-line-candidates.test.ts +4 -33
- package/src/integration/grep-matrix.test.ts +1 -1
- package/src/integration/grep-path-matrix.test.ts +59 -65
- package/src/integration/grep-prefilter-coverage.test.ts +26 -8
- package/src/integration/grep-process-limits.test.ts +46 -0
- package/src/integration/grep-regex.test.ts +7 -2
- package/src/integration/grep-rg.test.ts +106 -12
- package/src/integration/grep-translation.test.ts +14 -7
- package/src/integration/model-budget-lifecycle.test.ts +41 -2
- package/src/integration/npm-package.test.ts +12 -10
- package/src/integration/npm-package.testing.ts +147 -1
- package/src/integration/replace-package.testing.ts +2 -2
- package/src/integration/report-replay.test.ts +4 -0
- package/src/pi/AGENTS.md +23 -15
- package/src/pi/action-fusion.test.ts +38 -4
- package/src/pi/action-fusion.ts +23 -19
- package/src/pi/argument-diagnostics.ts +28 -48
- package/src/pi/argument-validation.test.ts +103 -6
- package/src/pi/argument-validation.ts +55 -31
- package/src/pi/budgets.ts +19 -10
- package/src/pi/config.test.ts +1 -1
- package/src/pi/diagnostic-buffer.test.ts +1 -0
- package/src/pi/edit-tool.ts +3 -3
- package/src/pi/execute.test.ts +4 -0
- package/src/pi/failure-context.test.ts +24 -3
- package/src/pi/failure-context.ts +36 -42
- package/src/pi/failure-path.test.ts +8 -3
- package/src/pi/file-commit.test.ts +109 -52
- package/src/pi/file-commit.ts +62 -56
- package/src/pi/fusion-card.ts +12 -7
- package/src/pi/grep-candidates.test.ts +5 -5
- package/src/pi/grep-candidates.ts +80 -48
- package/src/pi/grep-matrix.test.ts +14 -29
- package/src/pi/grep-order.test.ts +132 -9
- package/src/pi/grep-output.ts +173 -48
- package/src/pi/grep-render.ts +40 -34
- package/src/pi/grep-rg.testing.ts +9 -6
- package/src/pi/grep-scope.ts +51 -29
- package/src/pi/grep-search.ts +96 -71
- package/src/pi/grep-tool.ts +28 -34
- package/src/pi/grep.test.ts +348 -83
- package/src/pi/model-budgets.test.ts +56 -6
- package/src/pi/model-budgets.ts +7 -4
- package/src/pi/mutation-result.test.ts +25 -1
- package/src/pi/mutation-result.ts +7 -2
- package/src/pi/mutation-runner.ts +4 -5
- package/src/pi/output-budgets.test.ts +3 -1
- package/src/pi/path.ts +18 -4
- package/src/pi/pi.test.ts +22 -0
- package/src/pi/read-delegation.test.ts +119 -0
- package/src/pi/read-tool.ts +109 -61
- package/src/pi/read.test.ts +351 -17
- package/src/pi/regex-worker.test.ts +42 -0
- package/src/pi/regex-worker.ts +17 -11
- package/src/pi/registration.test.ts +157 -102
- package/src/pi/render.test.ts +97 -0
- package/src/pi/render.ts +9 -9
- package/src/pi/replace-tool.ts +5 -2
- package/src/pi/report-schema.ts +75 -37
- package/src/pi/report.test.ts +73 -31
- package/src/pi/report.ts +12 -5
- package/src/pi/rg-process.ts +41 -5
- package/src/pi/rg-regex.test.ts +12 -39
- package/src/pi/rg-regex.ts +31 -79
- package/src/pi/text-stream.test.ts +3 -0
- package/src/pi/text-stream.ts +1 -1
- package/src/testing/regex-translation-corpus.ts +39 -96
|
@@ -29,9 +29,8 @@ function selectAmbiguousCandidates(candidates: readonly Anchor[]): readonly Anch
|
|
|
29
29
|
return candidates.slice(0, MAX_AMBIGUOUS_CANDIDATES);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
function collectContextRows(
|
|
32
|
+
/** Keep complete rows in contiguous neighborhoods, counting compact JSON bytes. */
|
|
33
|
+
function collectNeighborhoods(
|
|
35
34
|
lines: readonly string[],
|
|
36
35
|
centers: readonly number[],
|
|
37
36
|
anchors: AnchorFormatter,
|
|
@@ -39,53 +38,48 @@ function collectContextRows(
|
|
|
39
38
|
budget: OutputBudgets,
|
|
40
39
|
) {
|
|
41
40
|
const windows = mergeRanges(
|
|
42
|
-
centers.map((
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
Math.min(lines.length, line + CONTEXT_RADIUS) + 1,
|
|
47
|
-
];
|
|
48
|
-
}),
|
|
41
|
+
centers.map((line) => [
|
|
42
|
+
Math.max(1, line - CONTEXT_RADIUS),
|
|
43
|
+
Math.min(lines.length, line + CONTEXT_RADIUS) + 1,
|
|
44
|
+
]),
|
|
49
45
|
);
|
|
50
46
|
const total = windows.reduce((sum, [start, end]) => sum + end - start, 0);
|
|
51
|
-
const rows:
|
|
52
|
-
let
|
|
47
|
+
const groups: { lines: string; rows: string[] }[] = [];
|
|
48
|
+
let first = 0;
|
|
49
|
+
let previous = -1;
|
|
50
|
+
let shown = 0;
|
|
51
|
+
let bytes = Buffer.byteLength("[]");
|
|
53
52
|
|
|
54
53
|
for (const [start, end] of windows) {
|
|
55
54
|
for (let line = start; line < end; line++) {
|
|
56
55
|
const text = anchors.row(line, lines[line - 1]);
|
|
57
|
-
const rowBytes = Buffer.byteLength(text, "utf8");
|
|
58
56
|
// Neighborhoods must not bypass the standalone candidate's complete-row limit.
|
|
59
|
-
if (candidateLines.has(line) &&
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
57
|
+
if (candidateLines.has(line) && Buffer.byteLength(text) > budget.fieldBytes) continue;
|
|
58
|
+
const group = line === previous + 1 ? groups.at(-1) : undefined;
|
|
59
|
+
const range = `${group ? first : line}-${line}`;
|
|
60
|
+
const overhead = group
|
|
61
|
+
? 1 + range.length - group.lines.length // Row comma and change in the ASCII range.
|
|
62
|
+
: Number(groups.length > 0) + Buffer.byteLength(JSON.stringify({ lines: range, rows: [] }));
|
|
63
|
+
const added = Buffer.byteLength(JSON.stringify(text)) + overhead;
|
|
64
|
+
if (bytes + added > budget.blockBytes) continue;
|
|
65
|
+
if (group) {
|
|
66
|
+
group.rows.push(text);
|
|
67
|
+
group.lines = range;
|
|
68
|
+
} else {
|
|
69
|
+
groups.push({ lines: range, rows: [text] });
|
|
70
|
+
first = line;
|
|
71
|
+
}
|
|
72
|
+
previous = line;
|
|
73
|
+
shown++;
|
|
74
|
+
bytes += added;
|
|
77
75
|
}
|
|
78
|
-
const group = groups[groups.length - 1];
|
|
79
|
-
group.rows.push(row.text);
|
|
80
|
-
group.lines = `${first}-${row.line}`;
|
|
81
|
-
previous = row;
|
|
82
76
|
}
|
|
83
|
-
return groups;
|
|
77
|
+
return { groups, omitted: total - shown };
|
|
84
78
|
}
|
|
85
79
|
|
|
86
80
|
/** Observation rows around listed ambiguous candidates, lowest lines first. */
|
|
87
81
|
function ambiguousCandidateNeighborhoods(
|
|
88
|
-
|
|
82
|
+
currentLines: readonly string[],
|
|
89
83
|
failures: readonly AnchorFailure[],
|
|
90
84
|
anchors: AnchorFormatter,
|
|
91
85
|
budget: OutputBudgets,
|
|
@@ -101,15 +95,15 @@ function ambiguousCandidateNeighborhoods(
|
|
|
101
95
|
for (const failure of failures) {
|
|
102
96
|
if (failure.recovery.kind === "found") candidateLines.add(failure.recovery.newLine);
|
|
103
97
|
}
|
|
104
|
-
const {
|
|
105
|
-
|
|
98
|
+
const { groups, omitted } = collectNeighborhoods(
|
|
99
|
+
currentLines,
|
|
106
100
|
centers,
|
|
107
101
|
anchors,
|
|
108
102
|
candidateLines,
|
|
109
103
|
budget,
|
|
110
104
|
);
|
|
111
105
|
return {
|
|
112
|
-
candidateNeighborhoods:
|
|
106
|
+
candidateNeighborhoods: groups,
|
|
113
107
|
...(omitted ? { omittedNeighborhoodRows: omitted } : {}),
|
|
114
108
|
};
|
|
115
109
|
}
|
|
@@ -177,7 +171,7 @@ export function describeEditFailure(
|
|
|
177
171
|
const currentLines = splitLines(currentText);
|
|
178
172
|
// Leading entries whose compact JSON fits the block; whole entries are omitted, never cut.
|
|
179
173
|
const kept: AnchorFailureFact[] = [];
|
|
180
|
-
let bytes =
|
|
174
|
+
let bytes = 1; // Brackets less the comma charged to the first entry.
|
|
181
175
|
for (const failure of failures) {
|
|
182
176
|
const entry = failureEntry(failure, anchors, currentLines, budget);
|
|
183
177
|
bytes += Buffer.byteLength(JSON.stringify(entry)) + 1;
|
|
@@ -189,7 +183,7 @@ export function describeEditFailure(
|
|
|
189
183
|
facts: {
|
|
190
184
|
failures: kept,
|
|
191
185
|
...(omitted ? { omittedFailures: omitted } : {}),
|
|
192
|
-
...ambiguousCandidateNeighborhoods(
|
|
186
|
+
...ambiguousCandidateNeighborhoods(currentLines, failures, anchors, budget),
|
|
193
187
|
},
|
|
194
188
|
});
|
|
195
189
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
2
|
-
import fs, { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import fs, { mkdtemp, readdir, readFile, realpath, rm, writeFile } from "node:fs/promises";
|
|
3
3
|
import { syncBuiltinESMExports } from "node:module";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
@@ -15,7 +15,7 @@ import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
|
15
15
|
async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
|
|
16
16
|
const dir = await mkdtemp(join(tmpdir(), "hashline-failure-"));
|
|
17
17
|
try {
|
|
18
|
-
return await run(dir);
|
|
18
|
+
return await run(await realpath(dir));
|
|
19
19
|
} finally {
|
|
20
20
|
await rm(dir, { recursive: true, force: true });
|
|
21
21
|
}
|
|
@@ -28,6 +28,7 @@ test("temporary preparation and cleanup failures → preserve the commit failure
|
|
|
28
28
|
const nativeOpen = fs.open;
|
|
29
29
|
const original = Object.assign(new Error("temporary open failed"), { code: "EIO" });
|
|
30
30
|
let failCleanup = false;
|
|
31
|
+
let cleanupFailed = false;
|
|
31
32
|
try {
|
|
32
33
|
t.mock.method(fs, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
33
34
|
if (String(args[0]).includes(".hashline-commit-") && args[1] === "w") throw original;
|
|
@@ -35,8 +36,10 @@ test("temporary preparation and cleanup failures → preserve the commit failure
|
|
|
35
36
|
});
|
|
36
37
|
const nativeRm = fs.rm;
|
|
37
38
|
t.mock.method(fs, "rm", async (...args: Parameters<typeof nativeRm>) => {
|
|
38
|
-
if (failCleanup && String(args[0]).includes(".hashline-commit-"))
|
|
39
|
+
if (failCleanup && String(args[0]).includes(".hashline-commit-")) {
|
|
40
|
+
cleanupFailed = true;
|
|
39
41
|
throw Object.assign(new Error("temporary cleanup failed"), { code: "EACCES" });
|
|
42
|
+
}
|
|
40
43
|
return nativeRm(...args);
|
|
41
44
|
});
|
|
42
45
|
syncBuiltinESMExports();
|
|
@@ -52,6 +55,8 @@ test("temporary preparation and cleanup failures → preserve the commit failure
|
|
|
52
55
|
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
53
56
|
assert.deepEqual(report.cause, { code: original.code, message: original.message });
|
|
54
57
|
assert.equal(await readFile(target, "utf8"), "before\n");
|
|
58
|
+
assert.equal(cleanupFailed, failCleanup);
|
|
59
|
+
if (!failCleanup) assert.deepEqual(await readdir(dir), ["unpublished.txt"]);
|
|
55
60
|
}
|
|
56
61
|
} finally {
|
|
57
62
|
t.mock.restoreAll();
|
|
@@ -21,7 +21,6 @@ import { test } from "node:test";
|
|
|
21
21
|
import { spawn } from "node:child_process";
|
|
22
22
|
import {
|
|
23
23
|
commitFile,
|
|
24
|
-
FileMutationError,
|
|
25
24
|
readEditableSnapshot,
|
|
26
25
|
type CommitResult,
|
|
27
26
|
type FreshnessObservation,
|
|
@@ -32,9 +31,7 @@ import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
|
32
31
|
import { commandCompletion } from "./action-fusion.testing.ts";
|
|
33
32
|
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
34
33
|
import { callTool } from "./tool-call.testing.ts";
|
|
35
|
-
import { describeMutationFailure } from "./mutation-result.ts";
|
|
36
34
|
import { reportOf } from "./report.ts";
|
|
37
|
-
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
38
35
|
|
|
39
36
|
// Checked by tsc; this state cannot be returned by the commit layer.
|
|
40
37
|
function invalidCommitStates() {
|
|
@@ -105,21 +102,28 @@ async function holdWindowsExclusive(
|
|
|
105
102
|
};
|
|
106
103
|
}
|
|
107
104
|
|
|
108
|
-
test("
|
|
105
|
+
test("another create commits immediately before publication → preserves the winner's bytes", async (t) =>
|
|
109
106
|
withTemp(async (dir) => {
|
|
110
107
|
const target = join(dir, "race.txt");
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
108
|
+
const nativeLink = fsPromises.link;
|
|
109
|
+
t.mock.method(fsPromises, "link", async (...args: Parameters<typeof nativeLink>) => {
|
|
110
|
+
t.mock.restoreAll();
|
|
111
|
+
syncBuiltinESMExports();
|
|
112
|
+
const winner = await commitFile(target, "winner\n", { mode: "create" });
|
|
113
|
+
assert.equal(winner.publication, "PUBLISHED");
|
|
114
|
+
return nativeLink(...args);
|
|
115
|
+
});
|
|
116
|
+
syncBuiltinESMExports();
|
|
117
|
+
try {
|
|
118
|
+
await assert.rejects(
|
|
119
|
+
commitFile(target, "loser\n", { mode: "create" }),
|
|
120
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "TARGET_EXISTS",
|
|
121
|
+
);
|
|
122
|
+
assert.deepEqual(await readFile(target), Buffer.from("winner\n"));
|
|
123
|
+
} finally {
|
|
124
|
+
t.mock.restoreAll();
|
|
125
|
+
syncBuiltinESMExports();
|
|
126
|
+
}
|
|
123
127
|
}));
|
|
124
128
|
|
|
125
129
|
test("create never overwrites an existing target", async () =>
|
|
@@ -138,29 +142,26 @@ test("create never overwrites an existing target", async () =>
|
|
|
138
142
|
test("short low-level reads → complete mutation snapshots before revision binding", async (t) =>
|
|
139
143
|
withTemp(async (dir) => {
|
|
140
144
|
const target = join(dir, "short-read.txt");
|
|
141
|
-
const expected = Buffer.from("
|
|
142
|
-
const shortRead = 65_512;
|
|
145
|
+
const expected = Buffer.from("abcDEF");
|
|
143
146
|
await writeFile(target, expected);
|
|
144
147
|
const nativeOpen = fsPromises.open;
|
|
145
|
-
let lowLevelReads = 0;
|
|
146
148
|
t.mock.method(fsPromises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
147
149
|
const handle = await nativeOpen(...args);
|
|
148
150
|
if (args[0] !== target) return handle;
|
|
149
151
|
const nativeRead = handle.read.bind(handle);
|
|
150
|
-
t.mock.method(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
:
|
|
155
|
-
|
|
152
|
+
t.mock.method(
|
|
153
|
+
handle,
|
|
154
|
+
"read",
|
|
155
|
+
(options: { buffer: Buffer; length: number; position: number }) =>
|
|
156
|
+
nativeRead({ ...options, length: Math.min(options.length, 3) }),
|
|
157
|
+
);
|
|
156
158
|
return handle;
|
|
157
159
|
});
|
|
158
160
|
syncBuiltinESMExports();
|
|
159
161
|
try {
|
|
160
|
-
const snapshot = await readEditableSnapshot(target);
|
|
162
|
+
const snapshot = await readEditableSnapshot(target, undefined);
|
|
161
163
|
assert.equal(snapshot.text, expected.toString("utf8"));
|
|
162
164
|
assert.equal(snapshot.baseRevision, byteRevision(expected));
|
|
163
|
-
assert.ok(lowLevelReads >= 2);
|
|
164
165
|
} finally {
|
|
165
166
|
t.mock.restoreAll();
|
|
166
167
|
syncBuiltinESMExports();
|
|
@@ -344,35 +345,91 @@ test("Windows shared access failure preserves the target, skips then_run, and re
|
|
|
344
345
|
assert.equal(await readFile(target, "utf8"), "replacement\n");
|
|
345
346
|
}));
|
|
346
347
|
|
|
347
|
-
test("cancellation
|
|
348
|
+
test("cancellation during rename → successful publication reports the new bytes as published", async (t) =>
|
|
348
349
|
withTemp(async (dir) => {
|
|
349
350
|
const target = join(dir, "cancel.txt");
|
|
350
|
-
|
|
351
|
-
const newContent = "new-".repeat(100_000);
|
|
352
|
-
await writeFile(target, oldContent);
|
|
351
|
+
await writeFile(target, "old\n");
|
|
353
352
|
const controller = new AbortController();
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
|
|
353
|
+
const rename = fsPromises.rename;
|
|
354
|
+
t.mock.method(fsPromises, "rename", (...args: Parameters<typeof rename>) => {
|
|
355
|
+
const publishing = rename(...args);
|
|
356
|
+
controller.abort();
|
|
357
|
+
return publishing;
|
|
358
|
+
});
|
|
359
|
+
syncBuiltinESMExports();
|
|
357
360
|
try {
|
|
358
|
-
const result = await
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
assert.
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
publication = reported;
|
|
361
|
+
const result = await commitFile(target, "new\n", {
|
|
362
|
+
mode: "overwrite",
|
|
363
|
+
signal: controller.signal,
|
|
364
|
+
});
|
|
365
|
+
assert.equal(controller.signal.aborted, true);
|
|
366
|
+
assert.equal(result.publication, "PUBLISHED");
|
|
367
|
+
assert.equal(await readFile(target, "utf8"), "new\n");
|
|
368
|
+
} finally {
|
|
369
|
+
t.mock.restoreAll();
|
|
370
|
+
syncBuiltinESMExports();
|
|
369
371
|
}
|
|
370
|
-
const finalContent = await readFile(target, "utf8");
|
|
371
|
-
assert.ok(finalContent === oldContent || finalContent === newContent);
|
|
372
|
-
if (publication === "NOT_PUBLISHED") assert.equal(finalContent, oldContent);
|
|
373
|
-
if (publication === "PUBLISHED") assert.equal(finalContent, newContent);
|
|
374
372
|
}));
|
|
375
373
|
|
|
374
|
+
test("overwrite target changes during temporary staging → rejects before publishing", async (t) => {
|
|
375
|
+
const cases = [
|
|
376
|
+
{ change: "remove", code: "PATH_NOT_FOUND" },
|
|
377
|
+
{ change: "hard link", code: "MULTIPLE_HARD_LINKS" },
|
|
378
|
+
{ change: "symlink retarget", code: "FILE_CHANGED" },
|
|
379
|
+
] as const;
|
|
380
|
+
for (const { change, code } of cases) {
|
|
381
|
+
await t.test(change, () =>
|
|
382
|
+
withTemp(async (dir) => {
|
|
383
|
+
const firstDir = join(dir, "first");
|
|
384
|
+
const secondDir = join(dir, "second");
|
|
385
|
+
const alias = join(dir, "alias");
|
|
386
|
+
const original = join(firstDir, "file.txt");
|
|
387
|
+
const other = join(secondDir, "file.txt");
|
|
388
|
+
const retargets = change === "symlink retarget";
|
|
389
|
+
const target = retargets ? join(alias, "file.txt") : original;
|
|
390
|
+
const linkType = process.platform === "win32" ? "junction" : "dir";
|
|
391
|
+
await mkdir(firstDir);
|
|
392
|
+
await mkdir(secondDir);
|
|
393
|
+
await writeFile(original, "old\n");
|
|
394
|
+
if (retargets) {
|
|
395
|
+
await writeFile(other, "old\n");
|
|
396
|
+
await symlink(firstDir, alias, linkType);
|
|
397
|
+
}
|
|
398
|
+
const baseRevision = change === "remove" ? undefined : byteRevision(await readFile(target));
|
|
399
|
+
let staged = false;
|
|
400
|
+
const makeTemp = fsPromises.mkdtemp;
|
|
401
|
+
t.mock.method(fsPromises, "mkdtemp", async (...args: Parameters<typeof makeTemp>) => {
|
|
402
|
+
const temp = await makeTemp(...args);
|
|
403
|
+
staged = true;
|
|
404
|
+
if (change === "remove") await rm(original);
|
|
405
|
+
else if (change === "hard link") await createHardLink(original, other);
|
|
406
|
+
else {
|
|
407
|
+
await rm(alias);
|
|
408
|
+
await symlink(secondDir, alias, linkType);
|
|
409
|
+
}
|
|
410
|
+
return temp;
|
|
411
|
+
});
|
|
412
|
+
syncBuiltinESMExports();
|
|
413
|
+
try {
|
|
414
|
+
await assert.rejects(
|
|
415
|
+
commitFile(target, "new\n", { mode: "overwrite", baseRevision }),
|
|
416
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === code,
|
|
417
|
+
);
|
|
418
|
+
assert.equal(staged, true);
|
|
419
|
+
if (change === "remove") await assert.rejects(readFile(original), { code: "ENOENT" });
|
|
420
|
+
else {
|
|
421
|
+
assert.equal(await readFile(original, "utf8"), "old\n");
|
|
422
|
+
assert.equal(await readFile(other, "utf8"), "old\n");
|
|
423
|
+
}
|
|
424
|
+
} finally {
|
|
425
|
+
t.mock.restoreAll();
|
|
426
|
+
syncBuiltinESMExports();
|
|
427
|
+
}
|
|
428
|
+
}),
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
|
|
376
433
|
test("bound overwrite → reports published and observed revisions for the published bytes", async () =>
|
|
377
434
|
withTemp(async (dir) => {
|
|
378
435
|
const target = join(dir, "versions.txt");
|
|
@@ -470,7 +527,7 @@ test("file changes after the read → bound commits reject changed and unchanged
|
|
|
470
527
|
const target = join(dir, "target.txt");
|
|
471
528
|
const initialContent = "line 1\nline 2\n";
|
|
472
529
|
await writeFile(target, initialContent);
|
|
473
|
-
const snapshot = await readEditableSnapshot(target);
|
|
530
|
+
const snapshot = await readEditableSnapshot(target, undefined);
|
|
474
531
|
assert.equal(snapshot.text, initialContent);
|
|
475
532
|
|
|
476
533
|
// External process modifies target after snapshot is read.
|
|
@@ -497,7 +554,7 @@ test("file changes after the read → bound commits reject changed and unchanged
|
|
|
497
554
|
assert.equal(await readFile(target, "utf8"), initialContent);
|
|
498
555
|
|
|
499
556
|
// When target does not change externally, no-op commit succeeds and confirms observed revision.
|
|
500
|
-
const freshSnapshot = await readEditableSnapshot(target);
|
|
557
|
+
const freshSnapshot = await readEditableSnapshot(target, undefined);
|
|
501
558
|
const noopResult = await commitFile(target, freshSnapshot.text, {
|
|
502
559
|
mode: "overwrite",
|
|
503
560
|
baseRevision: freshSnapshot.baseRevision,
|
|
@@ -511,7 +568,7 @@ test("no-op commit with baseRevision rejects if target was deleted externally",
|
|
|
511
568
|
withTemp(async (dir) => {
|
|
512
569
|
const target = join(dir, "deleted.txt");
|
|
513
570
|
await writeFile(target, "content\n");
|
|
514
|
-
const snapshot = await readEditableSnapshot(target);
|
|
571
|
+
const snapshot = await readEditableSnapshot(target, undefined);
|
|
515
572
|
|
|
516
573
|
await rm(target);
|
|
517
574
|
|
package/src/pi/file-commit.ts
CHANGED
|
@@ -120,12 +120,12 @@ async function readRegularFile(path: string, signal?: AbortSignal): Promise<Buff
|
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
async function fileRevision(path: string): Promise<string> {
|
|
124
|
-
return byteRevision(await readRegularFile(path));
|
|
123
|
+
async function fileRevision(path: string, signal?: AbortSignal): Promise<string> {
|
|
124
|
+
return byteRevision(await readRegularFile(path, signal));
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
/** Decode and bind a mutation snapshot to the exact bytes read. */
|
|
128
|
-
export async function readEditableSnapshot(path: string, signal
|
|
128
|
+
export async function readEditableSnapshot(path: string, signal: AbortSignal | undefined) {
|
|
129
129
|
try {
|
|
130
130
|
const bytes = await readRegularFile(path, signal);
|
|
131
131
|
return { text: decodeEditableText(bytes), baseRevision: byteRevision(bytes) };
|
|
@@ -156,7 +156,7 @@ async function requireBaseRevision(
|
|
|
156
156
|
): Promise<void> {
|
|
157
157
|
let currentRevision: string;
|
|
158
158
|
try {
|
|
159
|
-
currentRevision = await fileRevision(path);
|
|
159
|
+
currentRevision = await fileRevision(path, signal);
|
|
160
160
|
} catch (error) {
|
|
161
161
|
throwIfCancelled(signal);
|
|
162
162
|
throw stepError("Unable to recheck target revision.", error);
|
|
@@ -166,30 +166,29 @@ async function requireBaseRevision(
|
|
|
166
166
|
throw new HashlineError("FILE_CHANGED", "File changed after it was read.");
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
169
|
+
interface TargetInfo {
|
|
170
|
+
readonly publishPath: string;
|
|
171
|
+
readonly modeBits: number;
|
|
172
|
+
}
|
|
173
173
|
|
|
174
|
-
/**
|
|
175
|
-
async function inspectTarget(path: string
|
|
174
|
+
/** Resolve existing targets so staging and cleanup stay beside the inspected file. */
|
|
175
|
+
async function inspectTarget(path: string): Promise<TargetInfo | undefined> {
|
|
176
176
|
let entry;
|
|
177
177
|
try {
|
|
178
178
|
entry = await lstat(path);
|
|
179
179
|
} catch (error) {
|
|
180
|
-
if (errnoCode(error) === "ENOENT") return
|
|
180
|
+
if (errnoCode(error) === "ENOENT") return undefined;
|
|
181
181
|
throw stepError("Unable to inspect target.", error);
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
let publishPath
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
184
|
+
let publishPath: string;
|
|
185
|
+
try {
|
|
186
|
+
publishPath = await realpath(path);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (!entry.isSymbolicLink()) throw stepError("Unable to resolve target.", error);
|
|
189
|
+
throw new HashlineError("SYMLINK_UNRESOLVED", "Target symlink cannot be resolved.", {
|
|
190
|
+
cause: error,
|
|
191
|
+
});
|
|
193
192
|
}
|
|
194
193
|
|
|
195
194
|
let target;
|
|
@@ -205,22 +204,18 @@ async function inspectTarget(path: string, knownRevision?: string): Promise<Targ
|
|
|
205
204
|
"Target has multiple hard links; publishing would split the link set.",
|
|
206
205
|
);
|
|
207
206
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
publishPath,
|
|
221
|
-
beforeRevision,
|
|
222
|
-
modeBits: target.mode,
|
|
223
|
-
};
|
|
207
|
+
return { publishPath, modeBits: target.mode };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function requireOverwriteTarget(path: string, baseRevision: string | undefined) {
|
|
211
|
+
const target = await inspectTarget(path);
|
|
212
|
+
if (!target)
|
|
213
|
+
throw new HashlineError("PATH_NOT_FOUND", PATH_ERROR_MESSAGES.PATH_NOT_FOUND, {
|
|
214
|
+
facts: { missing: [path] },
|
|
215
|
+
// Read-modify-write tools have no mode to change.
|
|
216
|
+
next: baseRevision === undefined ? "useCreate" : undefined,
|
|
217
|
+
});
|
|
218
|
+
return target;
|
|
224
219
|
}
|
|
225
220
|
|
|
226
221
|
async function writeAndSyncTemp(
|
|
@@ -317,22 +312,31 @@ export async function commitFile(
|
|
|
317
312
|
const bytes = Buffer.from(content, "utf8");
|
|
318
313
|
const { mode } = options;
|
|
319
314
|
const baseRevision = mode === "overwrite" ? options.baseRevision : undefined;
|
|
320
|
-
const target =
|
|
321
|
-
|
|
315
|
+
const target =
|
|
316
|
+
mode === "overwrite" ? await requireOverwriteTarget(path, baseRevision) : undefined;
|
|
317
|
+
if (mode === "create" && (await inspectTarget(path)))
|
|
322
318
|
throw new HashlineError("TARGET_EXISTS", "Target already exists.");
|
|
323
|
-
if (mode === "overwrite" && !target.existed)
|
|
324
|
-
throw new HashlineError("PATH_NOT_FOUND", PATH_ERROR_MESSAGES.PATH_NOT_FOUND, {
|
|
325
|
-
facts: { missing: [path] },
|
|
326
|
-
// A read-modify-write target vanished after its read; those tools have no mode to change.
|
|
327
|
-
next: baseRevision === undefined ? "useCreate" : undefined,
|
|
328
|
-
});
|
|
329
319
|
throwIfCancelled(options.signal);
|
|
330
320
|
const publishedRevision = byteRevision(bytes);
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
321
|
+
const verifyOverwrite = async (initial: TargetInfo) => {
|
|
322
|
+
if (baseRevision !== undefined) await requireBaseRevision(path, baseRevision, options.signal);
|
|
323
|
+
const current = await requireOverwriteTarget(path, baseRevision);
|
|
324
|
+
throwIfCancelled(options.signal);
|
|
325
|
+
if (current.publishPath !== initial.publishPath)
|
|
326
|
+
throw new HashlineError("FILE_CHANGED", "Target path resolves to a different file.");
|
|
327
|
+
};
|
|
328
|
+
let beforeRevision = baseRevision;
|
|
329
|
+
if (target && beforeRevision === undefined) {
|
|
330
|
+
try {
|
|
331
|
+
beforeRevision = await fileRevision(path, options.signal);
|
|
332
|
+
} catch (error) {
|
|
333
|
+
throwIfCancelled(options.signal);
|
|
334
|
+
throw stepError("Unable to read target revision.", error);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
// Check a bound snapshot even when the transformation produced no change.
|
|
338
|
+
if (target && beforeRevision === publishedRevision) {
|
|
339
|
+
await verifyOverwrite(target);
|
|
336
340
|
return {
|
|
337
341
|
publishedRevision,
|
|
338
342
|
observedRevision: publishedRevision,
|
|
@@ -340,7 +344,7 @@ export async function commitFile(
|
|
|
340
344
|
};
|
|
341
345
|
}
|
|
342
346
|
|
|
343
|
-
const publishPath = target
|
|
347
|
+
const publishPath = target?.publishPath ?? path;
|
|
344
348
|
const publishDirectory = dirname(publishPath);
|
|
345
349
|
let tempDir: string;
|
|
346
350
|
try {
|
|
@@ -353,11 +357,13 @@ export async function commitFile(
|
|
|
353
357
|
const tempPath = join(tempDir, "content");
|
|
354
358
|
let published = false;
|
|
355
359
|
try {
|
|
356
|
-
await writeAndSyncTemp(tempPath, bytes, target
|
|
357
|
-
if (
|
|
358
|
-
await
|
|
359
|
-
|
|
360
|
-
else
|
|
360
|
+
await writeAndSyncTemp(tempPath, bytes, target?.modeBits, options.signal);
|
|
361
|
+
if (target) {
|
|
362
|
+
await verifyOverwrite(target);
|
|
363
|
+
await publishReplace(tempPath, publishPath, options.signal);
|
|
364
|
+
} else {
|
|
365
|
+
await publishCreate(tempPath, publishPath, options.signal);
|
|
366
|
+
}
|
|
361
367
|
published = true;
|
|
362
368
|
await syncDirectory(publishDirectory);
|
|
363
369
|
const committed: CommitResult = {
|
|
@@ -365,7 +371,7 @@ export async function commitFile(
|
|
|
365
371
|
publication: "PUBLISHED",
|
|
366
372
|
};
|
|
367
373
|
try {
|
|
368
|
-
return { ...committed, observedRevision: await fileRevision(
|
|
374
|
+
return { ...committed, observedRevision: await fileRevision(path) };
|
|
369
375
|
} catch (error) {
|
|
370
376
|
// Another writer is replacing the published bytes: report a changed target, not a failed
|
|
371
377
|
// mutation whose retry would apply it again.
|