@criogaid/pi-hashline-edit 0.12.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 +59 -16
- package/README.zh-CN.md +59 -16
- package/package.json +2 -2
- package/src/core/AGENTS.md +2 -2
- package/src/core/apply.test.ts +13 -0
- package/src/core/apply.ts +5 -9
- 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 -36
- 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 +27 -2
- 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 +3 -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 +15 -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 +63 -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.testing.ts +2 -3
- package/src/integration/replace-package.testing.ts +2 -2
- package/src/pi/AGENTS.md +21 -13
- 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 +61 -27
- package/src/pi/argument-validation.ts +34 -0
- 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 +1 -1
- package/src/pi/failure-context.test.ts +23 -2
- 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 +3 -2
- package/src/pi/grep-candidates.test.ts +5 -5
- package/src/pi/grep-candidates.ts +81 -50
- package/src/pi/grep-matrix.test.ts +14 -29
- package/src/pi/grep-order.test.ts +132 -9
- package/src/pi/grep-output.ts +169 -51
- 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 +13 -22
- package/src/pi/grep.test.ts +310 -84
- 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 +3 -4
- 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 +306 -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 +54 -0
- package/src/pi/render.ts +9 -9
- package/src/pi/replace-tool.ts +5 -2
- package/src/pi/report-schema.ts +52 -31
- package/src/pi/report.test.ts +60 -10
- package/src/pi/rg-process.ts +41 -5
- package/src/pi/rg-regex.test.ts +12 -52
- package/src/pi/rg-regex.ts +29 -77
- 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
package/src/pi/budgets.ts
CHANGED
|
@@ -3,25 +3,33 @@
|
|
|
3
3
|
* The context window is a capacity hint, not a tokenizer or remaining-context counter.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
// A read is a chosen window of one file; a grep is a probe whose size the caller cannot predict.
|
|
7
|
+
// At four bytes per token a full read spends about a sixteenth of the window, a full grep a
|
|
8
|
+
// thirty-second.
|
|
9
|
+
const READ_SHARE = 4;
|
|
10
|
+
const GREP_SHARE = 8;
|
|
11
|
+
// A preview grows with the payload: one UTF-16 unit per this many payload bytes.
|
|
12
|
+
const GREP_PREVIEW_SHARE = 100;
|
|
13
|
+
// Pi's built-in grep keeps this many characters of a line. A narrower preview cuts a row in
|
|
14
|
+
// far more calls and keeps almost none of them within the payload budget.
|
|
15
|
+
const MIN_GREP_PREVIEW_UNITS = 500;
|
|
6
16
|
const BLOCK_SHARE = 4;
|
|
7
17
|
const FIELD_SHARE = 4;
|
|
8
|
-
const UTF16_PREVIEW_BYTES = 4;
|
|
9
18
|
// Keep JSON envelopes and diagnostic omission markers usable for tiny declared windows.
|
|
10
19
|
const MIN_BLOCK_BYTES = 512;
|
|
11
20
|
const MIN_FIELD_BYTES = 128;
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
// not surpass what a user could configure, keeping one absolute bound per call.
|
|
21
|
+
// Capacity counted toward the budgets stops here; config.ts reuses this constant for the
|
|
22
|
+
// read.maxKiB range, so automatic sizing never surpasses what a user could configure.
|
|
15
23
|
export const MAX_CONTENT_KIB = 4096;
|
|
16
24
|
const MAX_CONTENT_BYTES = MAX_CONTENT_KIB * 1024;
|
|
17
25
|
|
|
18
26
|
// Declared apart so Object.freeze keeps these as numbers rather than literal types.
|
|
19
27
|
const fallbackBudgets = {
|
|
20
|
-
readBytes:
|
|
28
|
+
readBytes: 64 * 1024,
|
|
21
29
|
grepBytes: 50 * 1024,
|
|
22
30
|
blockBytes: 16 * 1024,
|
|
23
31
|
fieldBytes: 4 * 1024,
|
|
24
|
-
grepPreviewUnits:
|
|
32
|
+
grepPreviewUnits: MIN_GREP_PREVIEW_UNITS,
|
|
25
33
|
};
|
|
26
34
|
const DEFAULT_OUTPUT_BUDGETS = Object.freeze(fallbackBudgets);
|
|
27
35
|
export type OutputBudgets = typeof DEFAULT_OUTPUT_BUDGETS;
|
|
@@ -32,16 +40,17 @@ export function outputBudgetsFor(contextWindow: number | undefined): OutputBudge
|
|
|
32
40
|
if (contextWindow === undefined || !Number.isSafeInteger(contextWindow) || contextWindow <= 0) {
|
|
33
41
|
return DEFAULT_OUTPUT_BUDGETS;
|
|
34
42
|
}
|
|
35
|
-
// One byte per declared context token.
|
|
43
|
+
// One byte per declared context token, shared out below.
|
|
36
44
|
const contentBytes = Math.min(contextWindow, MAX_CONTENT_BYTES);
|
|
45
|
+
const grepBytes = Math.max(MIN_BLOCK_BYTES, Math.floor(contentBytes / GREP_SHARE));
|
|
37
46
|
const blockBytes = Math.max(MIN_BLOCK_BYTES, Math.floor(contentBytes / BLOCK_SHARE));
|
|
38
47
|
const fieldBytes = Math.max(MIN_FIELD_BYTES, Math.floor(blockBytes / FIELD_SHARE));
|
|
39
48
|
return Object.freeze({
|
|
40
|
-
readBytes: contentBytes,
|
|
41
|
-
grepBytes
|
|
49
|
+
readBytes: Math.max(MIN_BLOCK_BYTES, Math.floor(contentBytes / READ_SHARE)),
|
|
50
|
+
grepBytes,
|
|
42
51
|
blockBytes,
|
|
43
52
|
fieldBytes,
|
|
44
|
-
grepPreviewUnits: Math.floor(
|
|
53
|
+
grepPreviewUnits: Math.max(MIN_GREP_PREVIEW_UNITS, Math.floor(grepBytes / GREP_PREVIEW_SHARE)),
|
|
45
54
|
});
|
|
46
55
|
}
|
|
47
56
|
|
package/src/pi/config.test.ts
CHANGED
|
@@ -37,7 +37,7 @@ test("defaults come from the settings schema", () => {
|
|
|
37
37
|
hashLen: 4,
|
|
38
38
|
shiftRadius: 15,
|
|
39
39
|
read: { defaultLimit: DEFAULT_MAX_LINES, maxKiB: undefined },
|
|
40
|
-
grep: {
|
|
40
|
+
grep: { defaultLimit: 100, defaultContext: 0, regexTimeoutMs: 5_000 },
|
|
41
41
|
replace: { regexTimeoutMs: 5_000 },
|
|
42
42
|
});
|
|
43
43
|
});
|
|
@@ -52,6 +52,7 @@ test("search warning aggregation retains the final cause and labels truncation",
|
|
|
52
52
|
const { facts, payload } = await grepOutput({
|
|
53
53
|
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
54
54
|
cwd: process.cwd(),
|
|
55
|
+
missing: [],
|
|
55
56
|
anchors: createAnchorFormatter(4),
|
|
56
57
|
context: 0,
|
|
57
58
|
search: {
|
package/src/pi/edit-tool.ts
CHANGED
|
@@ -283,7 +283,7 @@ export function makeEditOverride(env: MutationToolEnvironment) {
|
|
|
283
283
|
"Edit whole lines using LINE#HASH anchors checked against the current file. Use replace instead when the same change repeats in several places, or when only a fragment of a long line changes.",
|
|
284
284
|
promptSnippet: "Edit file lines using verified anchors",
|
|
285
285
|
promptGuidelines: [
|
|
286
|
-
"Batch all steps for one file into a single edit call.",
|
|
286
|
+
"Batch all steps for one file into a single edit call, and send changes to different files in the same response; they do not interfere.",
|
|
287
287
|
"Before citing a line after write, edit, or replace, get its anchor from a new read or grep. Only after edit, lines above the earliest changed line keep their anchors.",
|
|
288
288
|
],
|
|
289
289
|
check: (args, report) =>
|
|
@@ -113,7 +113,10 @@ test("more than eight ambiguous candidates → context uses the same candidate p
|
|
|
113
113
|
});
|
|
114
114
|
|
|
115
115
|
test("neighborhood exceeds byte budget → whole rows and explicit omission counts", () => {
|
|
116
|
-
const
|
|
116
|
+
const empty = [{ lines: "1-1", rows: ["1#XXXX│"] }];
|
|
117
|
+
const exact = "x".repeat(
|
|
118
|
+
DEFAULT_OUTPUT_BUDGETS.blockBytes - Buffer.byteLength(JSON.stringify(empty)),
|
|
119
|
+
);
|
|
117
120
|
const cases = [
|
|
118
121
|
{ lines: [exact, "target", "last"], shown: [1], omitted: 2 },
|
|
119
122
|
{ lines: ["界".repeat(6000), "target", "last"], shown: [2, 3], omitted: 1 },
|
|
@@ -140,7 +143,7 @@ test("neighborhood exceeds byte budget → whole rows and explicit omission coun
|
|
|
140
143
|
);
|
|
141
144
|
assert.equal(result.omittedNeighborhoodRows, omitted);
|
|
142
145
|
assert.ok(
|
|
143
|
-
Buffer.byteLength(
|
|
146
|
+
Buffer.byteLength(JSON.stringify(result.candidateNeighborhoods)) <=
|
|
144
147
|
DEFAULT_OUTPUT_BUDGETS.blockBytes,
|
|
145
148
|
);
|
|
146
149
|
}
|
|
@@ -347,3 +350,21 @@ test("unresolved row out of range or oversized → omission facts contain no rec
|
|
|
347
350
|
);
|
|
348
351
|
}
|
|
349
352
|
});
|
|
353
|
+
|
|
354
|
+
test("escaped neighborhood rows → count their JSON bytes before reporting complete rows", () => {
|
|
355
|
+
const budget = outputBudgetsFor(1);
|
|
356
|
+
const report = rejectedEdit(
|
|
357
|
+
`current\n${"\\".repeat(Math.floor(budget.blockBytes * 0.75))}\nold\nold\n`,
|
|
358
|
+
[{ op: "delete", anchor: oldAnchor }],
|
|
359
|
+
4,
|
|
360
|
+
budget,
|
|
361
|
+
);
|
|
362
|
+
assert.ok(report.candidateNeighborhoods);
|
|
363
|
+
assert.ok(Buffer.byteLength(JSON.stringify(report.candidateNeighborhoods)) <= budget.blockBytes);
|
|
364
|
+
const shown = report.candidateNeighborhoods.flatMap((group) => group.rows);
|
|
365
|
+
assert.deepEqual(
|
|
366
|
+
shown.map((row) => Number(row.split("#")[0])),
|
|
367
|
+
[1, 3, 4],
|
|
368
|
+
);
|
|
369
|
+
assert.equal(report.omittedNeighborhoodRows, 1);
|
|
370
|
+
});
|
|
@@ -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
|
|