@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
|
@@ -1,597 +0,0 @@
|
|
|
1
|
-
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
2
|
-
import { rejectsArgument } from "./argument-error.testing.ts";
|
|
3
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
4
|
-
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
5
|
-
import assert from "node:assert/strict";
|
|
6
|
-
import fs, { mkdtemp, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
7
|
-
import { syncBuiltinESMExports } from "node:module";
|
|
8
|
-
import { writeFileSync } from "node:fs";
|
|
9
|
-
import { tmpdir } from "node:os";
|
|
10
|
-
import { join } from "node:path";
|
|
11
|
-
import { test } from "node:test";
|
|
12
|
-
import { outputBudgetsFor } from "./budgets.ts";
|
|
13
|
-
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
14
|
-
import { makeReplaceTool } from "./tool-factories.testing.ts";
|
|
15
|
-
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
16
|
-
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
17
|
-
import { reportOf } from "./report.ts";
|
|
18
|
-
|
|
19
|
-
import { callTool, createToolContext as ctx } from "./tool-call.testing.ts";
|
|
20
|
-
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
21
|
-
|
|
22
|
-
const text = (result: Pick<AgentToolResult<unknown>, "content">): string =>
|
|
23
|
-
result.content.map((block) => (block.type === "text" ? block.text : "")).join("\n");
|
|
24
|
-
|
|
25
|
-
const anchorBudget = outputBudgetsFor(4096);
|
|
26
|
-
const anchorBatchSize: unknown = Reflect.get(
|
|
27
|
-
makeEditOverride(process.cwd(), DEFAULT_CONFIG).parameters.properties.steps,
|
|
28
|
-
"maxItems",
|
|
29
|
-
);
|
|
30
|
-
assert.ok(typeof anchorBatchSize === "number");
|
|
31
|
-
|
|
32
|
-
function assertFailureByteBudgets(message: string): void {
|
|
33
|
-
const report = reportOf({ content: [{ type: "text", text: message }] }, "edit");
|
|
34
|
-
assert.ok(report);
|
|
35
|
-
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
36
|
-
assert.ok(report.failures && report.failures.length > 0);
|
|
37
|
-
assert.ok(Buffer.byteLength(JSON.stringify(report.failures)) <= anchorBudget.blockBytes);
|
|
38
|
-
assert.equal(report.failures.length + (report.omittedFailures ?? 0), anchorBatchSize);
|
|
39
|
-
assert.ok(report.omittedFailures);
|
|
40
|
-
assert.ok(report.next?.includes("intended target"));
|
|
41
|
-
if (report.candidateNeighborhoods) {
|
|
42
|
-
const rows = report.candidateNeighborhoods.flatMap((group) => group.rows);
|
|
43
|
-
assert.ok(rows.length > 0);
|
|
44
|
-
assert.ok(Buffer.byteLength(rows.join("\n")) + 1 <= anchorBudget.blockBytes);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
test("edit and replace commands change or remove the file → withhold anchors and report freshness", async (t) => {
|
|
49
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-result-"));
|
|
50
|
-
try {
|
|
51
|
-
const context = await ctx(dir, t);
|
|
52
|
-
for (const toolName of ["edit", "replace"] as const) {
|
|
53
|
-
for (const state of ["changed", "missing", "unknown"] as const) {
|
|
54
|
-
for (const fails of [false, true]) {
|
|
55
|
-
const path = join(dir, `${toolName}-${state}-${fails}.txt`);
|
|
56
|
-
await writeFile(path, "before\n");
|
|
57
|
-
const updates: AgentToolResult<unknown>[] = [];
|
|
58
|
-
const executeTool: SessionCommand = async (_name, _input, options) => {
|
|
59
|
-
if (state === "changed") await writeFile(path, "command output\n");
|
|
60
|
-
else {
|
|
61
|
-
await rm(path);
|
|
62
|
-
if (state === "unknown") await mkdir(path);
|
|
63
|
-
}
|
|
64
|
-
options?.onUpdate?.({
|
|
65
|
-
content: [{ type: "text", text: "progress" }],
|
|
66
|
-
details: undefined,
|
|
67
|
-
});
|
|
68
|
-
if (fails)
|
|
69
|
-
return commandCompletion(
|
|
70
|
-
{ content: [{ type: "text", text: "command failed" }] },
|
|
71
|
-
true,
|
|
72
|
-
);
|
|
73
|
-
return commandCompletion({ content: [{ type: "text", text: "done" }] });
|
|
74
|
-
};
|
|
75
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
76
|
-
const tool =
|
|
77
|
-
toolName === "edit"
|
|
78
|
-
? makeEditOverride(dir, DEFAULT_CONFIG, fusion)
|
|
79
|
-
: makeReplaceTool(dir, DEFAULT_CONFIG, fusion);
|
|
80
|
-
const args =
|
|
81
|
-
toolName === "edit"
|
|
82
|
-
? {
|
|
83
|
-
steps: [
|
|
84
|
-
{
|
|
85
|
-
action: "replace",
|
|
86
|
-
anchor: `1#${computeLineHash(1, "before", DEFAULT_CONFIG.hashLen)}`,
|
|
87
|
-
lines: ["after"],
|
|
88
|
-
},
|
|
89
|
-
],
|
|
90
|
-
}
|
|
91
|
-
: { replacements: [{ literal: true, find: "before", replace: "after" }] };
|
|
92
|
-
const result = await callTool(
|
|
93
|
-
tool,
|
|
94
|
-
{ path, ...args, then_run: { command: "check" } },
|
|
95
|
-
{
|
|
96
|
-
onUpdate: (update: AgentToolResult<unknown>) => updates.push(update),
|
|
97
|
-
ctx: { ...context, executeTool },
|
|
98
|
-
},
|
|
99
|
-
);
|
|
100
|
-
assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
|
|
101
|
-
const report = reportOf(result, toolName);
|
|
102
|
-
assert.equal(report?.freshness, state);
|
|
103
|
-
assert.equal(report?.publication, "PUBLISHED");
|
|
104
|
-
if (state === "missing") assert.equal(report?.next, undefined);
|
|
105
|
-
else assert.match(report?.next ?? "", /Read the file before further edits/);
|
|
106
|
-
assert.equal(report?.then_run?.status, fails ? "failed" : "succeeded");
|
|
107
|
-
if (fails) assert.equal(report?.then_run?.output, "command failed");
|
|
108
|
-
assert.equal(result.content.length, 1);
|
|
109
|
-
assert.ok(updates.length > 0);
|
|
110
|
-
for (const update of updates)
|
|
111
|
-
assert.doesNotMatch(text(update), /Updated anchors|\d+#[0-9A-Z]+│/);
|
|
112
|
-
if (state === "changed") assert.equal(await readFile(path, "utf8"), "command output\n");
|
|
113
|
-
else if (state === "missing") await assert.rejects(stat(path), { code: "ENOENT" });
|
|
114
|
-
else assert.equal((await stat(path)).isDirectory(), true);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
} finally {
|
|
119
|
-
await rm(dir, { recursive: true, force: true });
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test("progress callback failures → preserve publication and allow the command", async (t) => {
|
|
124
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-progress-"));
|
|
125
|
-
try {
|
|
126
|
-
for (const callback of ["reporter", "update"]) {
|
|
127
|
-
let commands = 0;
|
|
128
|
-
const fail = () => {
|
|
129
|
-
throw new Error("display unavailable");
|
|
130
|
-
};
|
|
131
|
-
const executeTool: SessionCommand = async () => {
|
|
132
|
-
commands++;
|
|
133
|
-
return commandCompletion({ content: [{ type: "text", text: "done" }] });
|
|
134
|
-
};
|
|
135
|
-
const fusion = createActionFusionExecutor(callback === "reporter" ? fail : () => {});
|
|
136
|
-
const path = join(dir, callback);
|
|
137
|
-
const result = await callTool(
|
|
138
|
-
makeWriteOverride(dir, fusion),
|
|
139
|
-
{
|
|
140
|
-
path,
|
|
141
|
-
content: "saved\n",
|
|
142
|
-
mode: "create",
|
|
143
|
-
then_run: { command: "check" },
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
onUpdate: callback === "update" ? fail : undefined,
|
|
147
|
-
ctx: { ...(await ctx(dir, t)), executeTool },
|
|
148
|
-
},
|
|
149
|
-
);
|
|
150
|
-
assert.equal(commands, 1);
|
|
151
|
-
assert.equal(await readFile(path, "utf8"), "saved\n");
|
|
152
|
-
const report = reportOf(result, "write");
|
|
153
|
-
assert.equal(report?.publication, "PUBLISHED");
|
|
154
|
-
assert.equal(report?.then_run?.status, "succeeded");
|
|
155
|
-
assert.equal(report?.progressError?.message, "display unavailable");
|
|
156
|
-
assert.doesNotMatch(text(result), /display unavailable/);
|
|
157
|
-
}
|
|
158
|
-
} finally {
|
|
159
|
-
await rm(dir, { recursive: true, force: true });
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
test("aggregate anchor diagnostics → respect their byte budgets", async (t) => {
|
|
164
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-budget-"));
|
|
165
|
-
try {
|
|
166
|
-
const path = join(dir, "long.txt");
|
|
167
|
-
await writeFile(path, "current\n");
|
|
168
|
-
const failure = await callTool(
|
|
169
|
-
makeEditOverride(dir, DEFAULT_CONFIG, undefined, () => anchorBudget),
|
|
170
|
-
{
|
|
171
|
-
path,
|
|
172
|
-
steps: Array.from({ length: anchorBatchSize }, () => ({
|
|
173
|
-
action: "delete",
|
|
174
|
-
anchor: "1#XXXX",
|
|
175
|
-
})),
|
|
176
|
-
},
|
|
177
|
-
{ ctx: await ctx(dir, t) },
|
|
178
|
-
);
|
|
179
|
-
assert.equal(failure.isError, true);
|
|
180
|
-
assertFailureByteBudgets(text(failure));
|
|
181
|
-
} finally {
|
|
182
|
-
await rm(dir, { recursive: true, force: true });
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
test("unpaired surrogate arguments are rejected before Fusion", async (t) => {
|
|
187
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-unicode-"));
|
|
188
|
-
try {
|
|
189
|
-
let commands = 0;
|
|
190
|
-
const events: string[] = [];
|
|
191
|
-
const executeTool: SessionCommand = async () => {
|
|
192
|
-
commands++;
|
|
193
|
-
return commandCompletion({ content: [{ type: "text", text: "done" }] });
|
|
194
|
-
};
|
|
195
|
-
const fusion = createActionFusionExecutor((event) => events.push(event.command));
|
|
196
|
-
const path = join(dir, "file.txt");
|
|
197
|
-
await writeFile(path, "original\n");
|
|
198
|
-
const cases = [
|
|
199
|
-
{
|
|
200
|
-
tool: makeEditOverride(dir, DEFAULT_CONFIG, fusion),
|
|
201
|
-
args: {
|
|
202
|
-
path,
|
|
203
|
-
steps: [{ action: "append", lines: ["\ud800"] }],
|
|
204
|
-
then_run: { command: "check" },
|
|
205
|
-
},
|
|
206
|
-
field: "steps[0].lines[0]",
|
|
207
|
-
},
|
|
208
|
-
{
|
|
209
|
-
tool: makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
|
|
210
|
-
args: {
|
|
211
|
-
path,
|
|
212
|
-
replacements: [{ literal: true, find: "original", replace: "\udfff" }],
|
|
213
|
-
then_run: { command: "check" },
|
|
214
|
-
},
|
|
215
|
-
field: "replacements[0].replace",
|
|
216
|
-
},
|
|
217
|
-
{
|
|
218
|
-
tool: makeWriteOverride(dir, fusion),
|
|
219
|
-
args: { path, content: "\ud800", mode: "overwrite", then_run: { command: "check" } },
|
|
220
|
-
field: "content",
|
|
221
|
-
},
|
|
222
|
-
];
|
|
223
|
-
for (const { tool, args, field } of cases) {
|
|
224
|
-
await assert.rejects(
|
|
225
|
-
callTool(tool, args, { ctx: { ...(await ctx(dir, t)), executeTool } }),
|
|
226
|
-
(error: unknown) =>
|
|
227
|
-
error instanceof Error &&
|
|
228
|
-
rejectsArgument(field, /encoded losslessly/)(error) &&
|
|
229
|
-
!("publication" in error),
|
|
230
|
-
);
|
|
231
|
-
assert.equal(await readFile(path, "utf8"), "original\n");
|
|
232
|
-
}
|
|
233
|
-
assert.deepEqual(events, []);
|
|
234
|
-
assert.equal(commands, 0);
|
|
235
|
-
} finally {
|
|
236
|
-
await rm(dir, { recursive: true, force: true });
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
test("ambiguous recovery bounds candidate lists and never claims content identity", async (t) => {
|
|
241
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-candidates-"));
|
|
242
|
-
try {
|
|
243
|
-
const path = join(dir, "file.txt");
|
|
244
|
-
const lines = Array.from({ length: 100 }, () => "same");
|
|
245
|
-
lines[49] = "different";
|
|
246
|
-
await writeFile(path, lines.join("\n"));
|
|
247
|
-
const failure = await callTool(
|
|
248
|
-
makeEditOverride(dir, DEFAULT_CONFIG, undefined, () => anchorBudget),
|
|
249
|
-
{
|
|
250
|
-
path,
|
|
251
|
-
steps: Array.from({ length: anchorBatchSize }, () => ({
|
|
252
|
-
action: "delete",
|
|
253
|
-
anchor: `50#${computeLineHash(50, "same", 4)}`,
|
|
254
|
-
})),
|
|
255
|
-
},
|
|
256
|
-
{ ctx: await ctx(dir, t) },
|
|
257
|
-
);
|
|
258
|
-
assert.equal(failure.isError, true);
|
|
259
|
-
assertFailureByteBudgets(text(failure));
|
|
260
|
-
const report = reportOf(failure, "edit");
|
|
261
|
-
assert.ok(
|
|
262
|
-
report?.failures?.every((entry) => entry.result === "ambiguous" && entry.omittedCandidates),
|
|
263
|
-
);
|
|
264
|
-
assert.doesNotMatch(text(failure), /same content/);
|
|
265
|
-
} finally {
|
|
266
|
-
await rm(dir, { recursive: true, force: true });
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
test("NUL arguments are rejected before Fusion for all mutation tools", async (t) => {
|
|
271
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-nul-"));
|
|
272
|
-
try {
|
|
273
|
-
const path = join(dir, "file.txt");
|
|
274
|
-
await writeFile(path, "original\n");
|
|
275
|
-
let commands = 0;
|
|
276
|
-
const events: string[] = [];
|
|
277
|
-
const executeTool: SessionCommand = async () => {
|
|
278
|
-
commands++;
|
|
279
|
-
return commandCompletion({ content: [{ type: "text", text: "unexpected" }] });
|
|
280
|
-
};
|
|
281
|
-
const fusion = createActionFusionExecutor((event) => events.push(event.command));
|
|
282
|
-
const cases = [
|
|
283
|
-
{
|
|
284
|
-
tool: makeEditOverride(dir, DEFAULT_CONFIG, fusion),
|
|
285
|
-
args: {
|
|
286
|
-
path,
|
|
287
|
-
steps: [{ action: "append", lines: ["\0"] }],
|
|
288
|
-
then_run: { command: "check" },
|
|
289
|
-
},
|
|
290
|
-
field: "steps[0].lines[0]",
|
|
291
|
-
},
|
|
292
|
-
{
|
|
293
|
-
tool: makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
|
|
294
|
-
args: {
|
|
295
|
-
path,
|
|
296
|
-
replacements: [{ literal: true, find: "original", replace: "\0" }],
|
|
297
|
-
then_run: { command: "check" },
|
|
298
|
-
},
|
|
299
|
-
field: "replacements[0].replace",
|
|
300
|
-
},
|
|
301
|
-
{
|
|
302
|
-
tool: makeWriteOverride(dir, fusion),
|
|
303
|
-
args: { path, content: "\0", mode: "overwrite", then_run: { command: "check" } },
|
|
304
|
-
field: "content",
|
|
305
|
-
},
|
|
306
|
-
];
|
|
307
|
-
for (const { tool, args, field } of cases) {
|
|
308
|
-
await assert.rejects(
|
|
309
|
-
callTool(tool, args, { ctx: { ...(await ctx(dir, t)), executeTool } }),
|
|
310
|
-
(error: unknown) =>
|
|
311
|
-
error instanceof Error &&
|
|
312
|
-
rejectsArgument(field, /NUL/)(error) &&
|
|
313
|
-
!("publication" in error),
|
|
314
|
-
);
|
|
315
|
-
assert.equal(await readFile(path, "utf8"), "original\n");
|
|
316
|
-
}
|
|
317
|
-
assert.deepEqual(events, []);
|
|
318
|
-
assert.equal(commands, 0);
|
|
319
|
-
} finally {
|
|
320
|
-
await rm(dir, { recursive: true, force: true });
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
test("Fusion skips the command when a replacement produces unencodable text", async (t) => {
|
|
325
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-derived-unicode-"));
|
|
326
|
-
try {
|
|
327
|
-
const path = join(dir, "file.txt");
|
|
328
|
-
const before = "😀\n";
|
|
329
|
-
await writeFile(path, before);
|
|
330
|
-
let commands = 0;
|
|
331
|
-
const events: string[] = [];
|
|
332
|
-
const executeTool: SessionCommand = async () => {
|
|
333
|
-
commands++;
|
|
334
|
-
return commandCompletion({ content: [{ type: "text", text: "unexpected" }] });
|
|
335
|
-
};
|
|
336
|
-
const fusion = createActionFusionExecutor((event) => events.push(event.command));
|
|
337
|
-
const result = await callTool(
|
|
338
|
-
makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
|
|
339
|
-
{
|
|
340
|
-
path,
|
|
341
|
-
replacements: [{ find: "^.", replace: "x", literal: false }],
|
|
342
|
-
then_run: { command: "check" },
|
|
343
|
-
},
|
|
344
|
-
{ ctx: { ...(await ctx(dir, t)), executeTool } },
|
|
345
|
-
);
|
|
346
|
-
assert.equal(result.isError, true);
|
|
347
|
-
assert.equal(reportOf(result, "replace")?.error, "INVALID_UNICODE");
|
|
348
|
-
assert.equal(reportOf(result, "replace")?.publication, "NOT_PUBLISHED");
|
|
349
|
-
assert.equal(reportOf(result, "replace")?.then_run?.status, "skipped");
|
|
350
|
-
assert.deepEqual(events, ["waiting", "skipped"]);
|
|
351
|
-
assert.equal(await readFile(path, "utf8"), before);
|
|
352
|
-
assert.equal(commands, 0);
|
|
353
|
-
} finally {
|
|
354
|
-
await rm(dir, { recursive: true, force: true });
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
test("result generation fails after a real mutation → preserves publication and cause", async (t) => {
|
|
359
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-result-generation-"));
|
|
360
|
-
try {
|
|
361
|
-
for (const content of ["before\n", "after\n"]) {
|
|
362
|
-
const path = join(dir, "file.txt");
|
|
363
|
-
await writeFile(path, "before\n");
|
|
364
|
-
const cause = new Error("render failed");
|
|
365
|
-
const context = await ctx(dir, t);
|
|
366
|
-
const nativeOpen = fs.open;
|
|
367
|
-
const nativeReplace = String.prototype.replace;
|
|
368
|
-
const readsBeforeDiff = content === "before\n" ? 2 : 3;
|
|
369
|
-
let targetReads = 0;
|
|
370
|
-
let injected = false;
|
|
371
|
-
let commands = 0;
|
|
372
|
-
const executeTool: SessionCommand = async () => {
|
|
373
|
-
commands++;
|
|
374
|
-
return commandCompletion({ content: [] });
|
|
375
|
-
};
|
|
376
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
377
|
-
try {
|
|
378
|
-
t.mock.method(fs, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
379
|
-
if (String(args[0]) === path) targetReads++;
|
|
380
|
-
return nativeOpen(...args);
|
|
381
|
-
});
|
|
382
|
-
syncBuiltinESMExports();
|
|
383
|
-
// Only fail diff normalization after the commit's final target read.
|
|
384
|
-
t.mock.method(
|
|
385
|
-
String.prototype,
|
|
386
|
-
"replace",
|
|
387
|
-
function (this: string, ...args: Parameters<typeof nativeReplace>) {
|
|
388
|
-
if (!injected && targetReads === readsBeforeDiff && String(this) === content) {
|
|
389
|
-
injected = true;
|
|
390
|
-
throw cause;
|
|
391
|
-
}
|
|
392
|
-
return nativeReplace.apply(this, args);
|
|
393
|
-
},
|
|
394
|
-
);
|
|
395
|
-
const result = await callTool(
|
|
396
|
-
makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
|
|
397
|
-
{
|
|
398
|
-
path,
|
|
399
|
-
replacements: [{ literal: true, find: "before\n", replace: content }],
|
|
400
|
-
then_run: { command: "check" },
|
|
401
|
-
},
|
|
402
|
-
{ ctx: { ...context, executeTool } },
|
|
403
|
-
);
|
|
404
|
-
const report = reportOf(result, "replace");
|
|
405
|
-
assert.equal(injected, true, `diff injection after ${targetReads} target reads`);
|
|
406
|
-
assert.equal(result.isError, true);
|
|
407
|
-
assert.equal(report?.error, "POST_PROCESS_FAILED");
|
|
408
|
-
assert.ok(result.details);
|
|
409
|
-
assert.equal(Reflect.get(result.details, "diff"), undefined);
|
|
410
|
-
assert.equal(report?.publication, content === "before\n" ? "NOT_PUBLISHED" : "PUBLISHED");
|
|
411
|
-
assert.deepEqual(report?.cause, { message: cause.message });
|
|
412
|
-
assert.equal(report?.then_run?.status, "skipped");
|
|
413
|
-
assert.equal(commands, 0);
|
|
414
|
-
} finally {
|
|
415
|
-
t.mock.restoreAll();
|
|
416
|
-
syncBuiltinESMExports();
|
|
417
|
-
}
|
|
418
|
-
assert.equal(await readFile(path, "utf8"), content);
|
|
419
|
-
}
|
|
420
|
-
} finally {
|
|
421
|
-
await rm(dir, { recursive: true, force: true });
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
type Fusion = Parameters<typeof makeWriteOverride>[1];
|
|
426
|
-
const noOpCases = [
|
|
427
|
-
{
|
|
428
|
-
makeTool: (cwd: string, fusion?: Fusion) => makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
|
|
429
|
-
params: {
|
|
430
|
-
steps: [
|
|
431
|
-
{
|
|
432
|
-
action: "replace",
|
|
433
|
-
anchor: `1#${computeLineHash(1, "same", DEFAULT_CONFIG.hashLen)}`,
|
|
434
|
-
lines: ["same"],
|
|
435
|
-
},
|
|
436
|
-
],
|
|
437
|
-
},
|
|
438
|
-
},
|
|
439
|
-
{
|
|
440
|
-
makeTool: (cwd: string, fusion?: Fusion) => makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
|
|
441
|
-
params: { replacements: [{ literal: true, find: "same", replace: "same" }] },
|
|
442
|
-
},
|
|
443
|
-
{ makeTool: makeWriteOverride, params: { content: "same\n", mode: "overwrite" } },
|
|
444
|
-
];
|
|
445
|
-
|
|
446
|
-
test("all mutation tools succeed without rewriting on no-op, with and without Fusion", async (t) => {
|
|
447
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-noop-"));
|
|
448
|
-
try {
|
|
449
|
-
for (const { makeTool, params } of noOpCases) {
|
|
450
|
-
for (const mode of ["standalone", "fusion", "command"]) {
|
|
451
|
-
const path = join(dir, "same.txt");
|
|
452
|
-
await writeFile(path, "same\n");
|
|
453
|
-
const before = await stat(path);
|
|
454
|
-
let commands = 0;
|
|
455
|
-
const executeTool: SessionCommand = async () => {
|
|
456
|
-
commands++;
|
|
457
|
-
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
458
|
-
};
|
|
459
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
460
|
-
const tool = makeTool(dir, mode === "standalone" ? undefined : fusion);
|
|
461
|
-
const result = await callTool(
|
|
462
|
-
tool,
|
|
463
|
-
{ path, ...params, ...(mode === "command" ? { then_run: { command: "check" } } : {}) },
|
|
464
|
-
{ ctx: { ...(await ctx(dir, t)), executeTool } },
|
|
465
|
-
);
|
|
466
|
-
assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
|
|
467
|
-
assert.match(
|
|
468
|
-
reportOf(result, tool.name)?.next ?? "",
|
|
469
|
-
/file already has this content.*do not repeat/i,
|
|
470
|
-
);
|
|
471
|
-
assert.ok(result.details);
|
|
472
|
-
assert.equal(Reflect.get(result.details, "diff"), tool.name === "write" ? undefined : "");
|
|
473
|
-
assert.equal(reportOf(result, tool.name)?.freshness, undefined);
|
|
474
|
-
assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
|
|
475
|
-
assert.equal(commands, mode === "command" ? 1 : 0);
|
|
476
|
-
assert.equal(
|
|
477
|
-
reportOf(result, tool.name)?.then_run?.status,
|
|
478
|
-
mode === "command" ? "succeeded" : undefined,
|
|
479
|
-
);
|
|
480
|
-
const after = await stat(path);
|
|
481
|
-
assert.deepEqual(
|
|
482
|
-
[after.ino, after.mtimeMs, after.ctimeMs],
|
|
483
|
-
[before.ino, before.mtimeMs, before.ctimeMs],
|
|
484
|
-
);
|
|
485
|
-
assert.equal(await readFile(path, "utf8"), "same\n");
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
} finally {
|
|
489
|
-
await rm(dir, { recursive: true, force: true });
|
|
490
|
-
}
|
|
491
|
-
});
|
|
492
|
-
|
|
493
|
-
test("delete and append change only line endings → publishes with an empty diff and still checks Fusion freshness", async (t) => {
|
|
494
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-empty-diff-"));
|
|
495
|
-
try {
|
|
496
|
-
const context = await ctx(dir, t);
|
|
497
|
-
for (const mode of ["standalone", "fusion", "command", "stale"] as const) {
|
|
498
|
-
const path = join(dir, `${mode}.txt`);
|
|
499
|
-
await writeFile(path, "same\r\nsame\n");
|
|
500
|
-
let commands = 0;
|
|
501
|
-
const executeTool: SessionCommand = async () => {
|
|
502
|
-
commands++;
|
|
503
|
-
assert.equal(await readFile(path, "utf8"), "same\nsame\r\n");
|
|
504
|
-
return commandCompletion({ content: [] });
|
|
505
|
-
};
|
|
506
|
-
const fusion = createActionFusionExecutor((progress) => {
|
|
507
|
-
if (mode === "stale" && progress.mutationCompleted && progress.command === "waiting")
|
|
508
|
-
writeFileSync(path, "external\n");
|
|
509
|
-
});
|
|
510
|
-
const result = await callTool(
|
|
511
|
-
makeEditOverride(dir, DEFAULT_CONFIG, mode === "standalone" ? undefined : fusion),
|
|
512
|
-
{
|
|
513
|
-
path,
|
|
514
|
-
steps: [
|
|
515
|
-
{
|
|
516
|
-
action: "delete",
|
|
517
|
-
anchor: `1#${computeLineHash(1, "same", DEFAULT_CONFIG.hashLen)}`,
|
|
518
|
-
},
|
|
519
|
-
{ action: "append", lines: ["same"] },
|
|
520
|
-
],
|
|
521
|
-
...(["command", "stale"].includes(mode) ? { then_run: { command: "check" } } : {}),
|
|
522
|
-
},
|
|
523
|
-
{ ctx: { ...context, executeTool } },
|
|
524
|
-
);
|
|
525
|
-
const report = reportOf(result, "edit");
|
|
526
|
-
assert.equal(result.isError, undefined);
|
|
527
|
-
assert.equal(Reflect.get(result.details, "diff"), "");
|
|
528
|
-
assert.equal(report?.publication, "PUBLISHED");
|
|
529
|
-
assert.equal(report?.freshness, mode === "stale" ? "changed" : undefined);
|
|
530
|
-
assert.equal(
|
|
531
|
-
report?.then_run?.status,
|
|
532
|
-
mode === "command" ? "succeeded" : mode === "stale" ? "skipped" : undefined,
|
|
533
|
-
);
|
|
534
|
-
if (mode === "stale") assert.match(report?.next ?? "", /Read the file before further edits/);
|
|
535
|
-
else assert.equal(report?.next, undefined);
|
|
536
|
-
assert.equal(commands, mode === "command" ? 1 : 0);
|
|
537
|
-
assert.equal(
|
|
538
|
-
await readFile(path, "utf8"),
|
|
539
|
-
mode === "stale" ? "external\n" : "same\nsame\r\n",
|
|
540
|
-
);
|
|
541
|
-
}
|
|
542
|
-
} finally {
|
|
543
|
-
await rm(dir, { recursive: true, force: true });
|
|
544
|
-
}
|
|
545
|
-
});
|
|
546
|
-
|
|
547
|
-
test("no-op Fusion still detects external changes and reports command failures", async (t) => {
|
|
548
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-noop-fusion-"));
|
|
549
|
-
try {
|
|
550
|
-
for (const { makeTool, params } of noOpCases) {
|
|
551
|
-
for (const scenario of ["before", "during", "failed"]) {
|
|
552
|
-
const path = join(dir, "same.txt");
|
|
553
|
-
await writeFile(path, "same\n");
|
|
554
|
-
let commands = 0;
|
|
555
|
-
const executeTool: SessionCommand = async () => {
|
|
556
|
-
commands++;
|
|
557
|
-
if (scenario === "failed")
|
|
558
|
-
return commandCompletion({ content: [{ type: "text", text: "check failed" }] }, true);
|
|
559
|
-
await writeFile(path, "external\n");
|
|
560
|
-
return commandCompletion({ content: [{ type: "text", text: "done" }] });
|
|
561
|
-
};
|
|
562
|
-
const fusion = createActionFusionExecutor((progress) => {
|
|
563
|
-
if (scenario === "before" && progress.mutationCompleted && progress.command === "waiting")
|
|
564
|
-
writeFileSync(path, "external\n");
|
|
565
|
-
});
|
|
566
|
-
const tool = makeTool(dir, fusion);
|
|
567
|
-
const result = await callTool(
|
|
568
|
-
tool,
|
|
569
|
-
{ path, ...params, then_run: { command: "check" } },
|
|
570
|
-
{ ctx: { ...(await ctx(dir, t)), executeTool } },
|
|
571
|
-
);
|
|
572
|
-
assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
|
|
573
|
-
assert.equal(commands, scenario === "before" ? 0 : 1);
|
|
574
|
-
assert.equal(
|
|
575
|
-
reportOf(result, tool.name)?.then_run?.status,
|
|
576
|
-
scenario === "before" ? "skipped" : scenario === "failed" ? "failed" : "succeeded",
|
|
577
|
-
);
|
|
578
|
-
assert.equal(
|
|
579
|
-
reportOf(result, tool.name)?.freshness,
|
|
580
|
-
scenario === "failed" ? undefined : "changed",
|
|
581
|
-
);
|
|
582
|
-
if (scenario === "failed")
|
|
583
|
-
assert.match(
|
|
584
|
-
reportOf(result, tool.name)?.next ?? "",
|
|
585
|
-
/file already has this content.*do not repeat/i,
|
|
586
|
-
);
|
|
587
|
-
else
|
|
588
|
-
assert.match(
|
|
589
|
-
reportOf(result, tool.name)?.next ?? "",
|
|
590
|
-
/Read the file before further edits/,
|
|
591
|
-
);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
} finally {
|
|
595
|
-
await rm(dir, { recursive: true, force: true });
|
|
596
|
-
}
|
|
597
|
-
});
|