@criogaid/pi-hashline-edit 0.6.1 → 0.8.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 +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { rejectsArgument } from "./argument-error.testing.ts";
|
|
1
2
|
import { computeLineHash } from "../core/hash.ts";
|
|
2
3
|
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
3
4
|
import assert from "node:assert/strict";
|
|
@@ -6,29 +7,18 @@ import { writeFileSync } from "node:fs";
|
|
|
6
7
|
import { tmpdir } from "node:os";
|
|
7
8
|
import { join } from "node:path";
|
|
8
9
|
import { test } from "node:test";
|
|
9
|
-
import {
|
|
10
|
-
import { createActionFusionExecutor
|
|
10
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
|
|
11
|
+
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
11
12
|
import { makeReplaceTool } from "./replace-tool.ts";
|
|
12
13
|
import { makeWriteOverride } from "./write-tool.ts";
|
|
13
14
|
import { makeEditOverride } from "./edit-tool.ts";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
staleTargetNotice,
|
|
20
|
-
type MutationOutcome,
|
|
21
|
-
} from "./mutation-result.ts";
|
|
15
|
+
import { commitFreshness, FileMutationError } from "./file-commit.ts";
|
|
16
|
+
import { byteRevision } from "./file-revision.ts";
|
|
17
|
+
import { finalizeMutation, postProcessMutation, type MutationOutcome } from "./mutation-result.ts";
|
|
18
|
+
import { CommandNotRun, decodeReport, reportOf } from "./report.ts";
|
|
19
|
+
import { REREAD } from "./report-schema.ts";
|
|
22
20
|
import { callTool, createToolContext as ctx } from "./tool-call.testing.ts";
|
|
23
21
|
import { DEFAULT_CONFIG } from "./config.ts";
|
|
24
|
-
import { publishedMutation } from "./mutation-outcome.testing.ts";
|
|
25
|
-
|
|
26
|
-
const KIB = formatKiB(MAX_BLOCK_BYTES);
|
|
27
|
-
const DIAGNOSTIC_TRUNCATED = new RegExp(`Diagnostic output truncated at ${KIB}`);
|
|
28
|
-
const ANCHOR_CHECK_TRUNCATED = new RegExp(
|
|
29
|
-
`Anchor-check output truncated at ${KIB}; omitted entries are not implied matched`,
|
|
30
|
-
);
|
|
31
|
-
const ANCHORS_OMITTED = new RegExp(`additional anchors omitted: ${KIB} limit`);
|
|
32
22
|
|
|
33
23
|
const text = (result: Pick<AgentToolResult<unknown>, "content">): string =>
|
|
34
24
|
result.content.map((block) => (block.type === "text" ? block.text : "")).join("\n");
|
|
@@ -43,73 +33,85 @@ const invoke = (
|
|
|
43
33
|
) => callTool(tool, args, { toolCallId, signal, onUpdate, ctx: context });
|
|
44
34
|
|
|
45
35
|
function assertFailureByteBudgets(message: string): void {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
assert.ok(
|
|
50
|
-
assert.ok(Buffer.byteLength(message.slice(0, checksAt)) <= MAX_BLOCK_BYTES);
|
|
36
|
+
const report = decodeReport(message, "edit");
|
|
37
|
+
assert.ok(report);
|
|
38
|
+
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
39
|
+
assert.ok(report.failures && report.failures.length > 0);
|
|
51
40
|
assert.ok(
|
|
52
|
-
Buffer.byteLength(
|
|
53
|
-
message.slice(checksAt + 1, neighborhoodsAt < 0 ? guidanceAt : neighborhoodsAt),
|
|
54
|
-
) <= MAX_BLOCK_BYTES,
|
|
41
|
+
Buffer.byteLength(JSON.stringify(report.failures)) <= DEFAULT_OUTPUT_BUDGETS.blockBytes,
|
|
55
42
|
);
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
assert.equal(report.failures.length + (report.omittedFailures ?? 0), 1000);
|
|
44
|
+
assert.ok(report.omittedFailures);
|
|
45
|
+
assert.equal(report.matched, undefined);
|
|
46
|
+
assert.equal(report.omittedMatched, undefined);
|
|
47
|
+
assert.ok(report.next?.includes("intended target"));
|
|
48
|
+
if (report.candidateNeighborhoods) {
|
|
49
|
+
const rows = report.candidateNeighborhoods.flatMap((group) => group.rows);
|
|
58
50
|
assert.ok(rows.length > 0);
|
|
59
|
-
assert.ok(Buffer.byteLength(rows.join("\n")) + 1 <=
|
|
51
|
+
assert.ok(Buffer.byteLength(rows.join("\n")) + 1 <= DEFAULT_OUTPUT_BUDGETS.blockBytes);
|
|
60
52
|
}
|
|
61
|
-
assert.match(message, DIAGNOSTIC_TRUNCATED);
|
|
62
|
-
assert.match(message, ANCHOR_CHECK_TRUNCATED);
|
|
63
|
-
const checks = message.match(/^op \d+ \/ anchor \/ .* \/ mismatched$/gm) ?? [];
|
|
64
|
-
assert.ok(checks.length > 40 && checks.length < 1000);
|
|
65
53
|
}
|
|
66
54
|
|
|
67
|
-
test("
|
|
55
|
+
test("edit and replace commands change or remove the file → withhold anchors and report freshness", async (t) => {
|
|
68
56
|
const dir = await mkdtemp(join(tmpdir(), "hashline-result-"));
|
|
69
57
|
try {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
58
|
+
const context = await ctx(dir, t);
|
|
59
|
+
for (const toolName of ["edit", "replace"] as const) {
|
|
60
|
+
for (const state of ["changed", "missing", "unknown"] as const) {
|
|
61
|
+
for (const fails of [false, true]) {
|
|
62
|
+
const path = join(dir, `${toolName}-${state}-${fails}.txt`);
|
|
63
|
+
await writeFile(path, "before\n");
|
|
64
|
+
const updates: AgentToolResult<unknown>[] = [];
|
|
65
|
+
const fusion = createActionFusionExecutor(
|
|
66
|
+
async (_id, _input, _signal, _ctx, onUpdate) => {
|
|
67
|
+
if (state === "changed") await writeFile(path, "command output\n");
|
|
68
|
+
else {
|
|
69
|
+
await rm(path);
|
|
70
|
+
if (state === "unknown") await mkdir(path);
|
|
71
|
+
}
|
|
72
|
+
onUpdate?.({ content: [{ type: "text", text: "progress" }], details: undefined });
|
|
73
|
+
if (fails) throw new Error("command failed");
|
|
74
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
const tool =
|
|
78
|
+
toolName === "edit"
|
|
79
|
+
? makeEditOverride(dir, DEFAULT_CONFIG, fusion)
|
|
80
|
+
: makeReplaceTool(dir, DEFAULT_CONFIG, fusion);
|
|
81
|
+
const args =
|
|
82
|
+
toolName === "edit"
|
|
83
|
+
? {
|
|
84
|
+
edits: [
|
|
85
|
+
{
|
|
86
|
+
op: "replace",
|
|
87
|
+
anchor: `1#${computeLineHash(1, "before", DEFAULT_CONFIG.hashLen)}`,
|
|
88
|
+
body: ["after"],
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
}
|
|
92
|
+
: { replacements: [{ literal: true, find: "before", replace: "after" }] };
|
|
93
|
+
const result = await callTool(
|
|
94
|
+
tool,
|
|
95
|
+
{ path, ...args, then_run: { command: "check" } },
|
|
96
|
+
{
|
|
97
|
+
onUpdate: (update: AgentToolResult<unknown>) => updates.push(update),
|
|
98
|
+
ctx: context,
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
|
|
102
|
+
const report = reportOf(result, toolName);
|
|
103
|
+
assert.equal(report?.freshness, state);
|
|
104
|
+
assert.equal(report?.publication, "PUBLISHED");
|
|
105
|
+
assert.equal(report?.next, REREAD);
|
|
106
|
+
assert.equal(report?.then_run?.status, fails ? "failed" : "succeeded");
|
|
107
|
+
assert.equal(result.content.length, 1);
|
|
108
|
+
assert.ok(updates.length > 0);
|
|
109
|
+
for (const update of updates)
|
|
110
|
+
assert.doesNotMatch(text(update), /Updated anchors|\d+#[0-9A-Z]+│/);
|
|
111
|
+
if (state === "changed") assert.equal(await readFile(path, "utf8"), "command output\n");
|
|
112
|
+
else if (state === "missing") await assert.rejects(stat(path), { code: "ENOENT" });
|
|
113
|
+
else assert.equal((await stat(path)).isDirectory(), true);
|
|
114
|
+
}
|
|
113
115
|
}
|
|
114
116
|
}
|
|
115
117
|
} finally {
|
|
@@ -128,7 +130,7 @@ test("progress callback failures preserve publication and do not prevent the com
|
|
|
128
130
|
const fusion = createActionFusionExecutor(
|
|
129
131
|
async () => {
|
|
130
132
|
commands++;
|
|
131
|
-
return { status: "succeeded",
|
|
133
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
|
|
132
134
|
},
|
|
133
135
|
callback === "reporter" ? fail : undefined,
|
|
134
136
|
);
|
|
@@ -148,9 +150,8 @@ test("progress callback failures preserve publication and do not prevent the com
|
|
|
148
150
|
);
|
|
149
151
|
assert.equal(commands, 1);
|
|
150
152
|
assert.equal(await readFile(path, "utf8"), "saved\n");
|
|
151
|
-
assert.
|
|
152
|
-
assert.equal(result
|
|
153
|
-
assert.equal(result.details.actionFusion.command, "succeeded");
|
|
153
|
+
assert.equal(reportOf(result, "write")?.publication, "PUBLISHED");
|
|
154
|
+
assert.equal(reportOf(result, "write")?.then_run?.status, "succeeded");
|
|
154
155
|
assert.match(text(result), /display unavailable/);
|
|
155
156
|
}
|
|
156
157
|
const fusion = createActionFusionExecutor(undefined, () => {
|
|
@@ -158,73 +159,102 @@ test("progress callback failures preserve publication and do not prevent the com
|
|
|
158
159
|
});
|
|
159
160
|
await assert.rejects(
|
|
160
161
|
fusion({
|
|
162
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
161
163
|
toolCallId: "failed",
|
|
162
164
|
absolutePath: join(dir, "failed"),
|
|
165
|
+
tool: "write",
|
|
166
|
+
displayPath: "failed",
|
|
163
167
|
thenRun: { command: "check" },
|
|
164
168
|
mutate: async () => {
|
|
165
|
-
throw new FileMutationError(
|
|
169
|
+
throw new FileMutationError(
|
|
170
|
+
"POST_PROCESS_FAILED",
|
|
171
|
+
"post_process",
|
|
172
|
+
"PUBLISHED",
|
|
173
|
+
"original failure",
|
|
174
|
+
);
|
|
166
175
|
},
|
|
167
176
|
signal: undefined,
|
|
168
177
|
ctx: await ctx(dir, t),
|
|
169
178
|
}),
|
|
170
|
-
(error:
|
|
179
|
+
(error: unknown) =>
|
|
180
|
+
error instanceof CommandNotRun &&
|
|
181
|
+
error.cause instanceof FileMutationError &&
|
|
182
|
+
error.cause.publication === "PUBLISHED" &&
|
|
183
|
+
/original failure/.test(error.cause.message),
|
|
171
184
|
);
|
|
172
185
|
} finally {
|
|
173
186
|
await rm(dir, { recursive: true, force: true });
|
|
174
187
|
}
|
|
175
188
|
});
|
|
176
189
|
|
|
177
|
-
test("standalone and Fusion finalizers
|
|
190
|
+
test("standalone and Fusion finalizers → take publication and freshness from commit facts, independent of diff", async (t) => {
|
|
178
191
|
const dir = await mkdtemp(join(tmpdir(), "hashline-finalizer-"));
|
|
179
192
|
try {
|
|
193
|
+
const path = join(dir, "file");
|
|
194
|
+
const context = await ctx(dir, t);
|
|
195
|
+
const publishedRevision = byteRevision(Buffer.from("published\n"));
|
|
180
196
|
for (const publication of ["PUBLISHED", "NOT_PUBLISHED"] as const) {
|
|
181
|
-
for (const
|
|
182
|
-
|
|
197
|
+
for (const content of ["published\n", "external\n"]) {
|
|
198
|
+
await writeFile(path, content);
|
|
199
|
+
for (const details of [{}, { diff: "" }]) {
|
|
200
|
+
const expectedDetails = structuredClone(details);
|
|
183
201
|
const mutation = {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
details: {
|
|
187
|
-
publication: "UNKNOWN",
|
|
188
|
-
publishedRevision: "ignored",
|
|
189
|
-
observedRevision: "ignored",
|
|
190
|
-
},
|
|
191
|
-
},
|
|
202
|
+
details,
|
|
203
|
+
facts: {},
|
|
192
204
|
commit: {
|
|
193
205
|
publication,
|
|
194
|
-
publishedRevision
|
|
195
|
-
observedRevision,
|
|
206
|
+
publishedRevision,
|
|
207
|
+
observedRevision: byteRevision(Buffer.from(content)),
|
|
196
208
|
created: false,
|
|
197
209
|
},
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
assert.equal(
|
|
210
|
+
} satisfies MutationOutcome<object>;
|
|
211
|
+
const fresh = content === "published\n";
|
|
212
|
+
const observation = commitFreshness(mutation.commit);
|
|
213
|
+
assert.equal(observation.freshness, fresh ? "unchanged" : "changed");
|
|
202
214
|
const standalone = finalizeMutation(
|
|
215
|
+
"edit",
|
|
216
|
+
path,
|
|
203
217
|
mutation,
|
|
204
|
-
|
|
218
|
+
observation,
|
|
219
|
+
undefined,
|
|
220
|
+
DEFAULT_OUTPUT_BUDGETS,
|
|
205
221
|
);
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
222
|
+
assert.equal(reportOf(standalone, "edit")?.publication, publication);
|
|
223
|
+
assert.equal(reportOf(standalone, "edit")?.freshness, fresh ? undefined : "changed");
|
|
224
|
+
for (const thenRun of [undefined, { command: "check" }]) {
|
|
225
|
+
let commands = 0;
|
|
226
|
+
const fused = await createActionFusionExecutor(async () => {
|
|
227
|
+
commands++;
|
|
228
|
+
return {
|
|
229
|
+
status: "succeeded",
|
|
230
|
+
result: { content: [{ type: "text", text: "checked" }] },
|
|
231
|
+
};
|
|
232
|
+
})({
|
|
233
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
234
|
+
tool: "edit",
|
|
235
|
+
displayPath: path,
|
|
236
|
+
toolCallId: "commit-facts",
|
|
237
|
+
absolutePath: path,
|
|
238
|
+
thenRun,
|
|
239
|
+
mutate: async () => mutation,
|
|
240
|
+
signal: undefined,
|
|
241
|
+
ctx: context,
|
|
242
|
+
});
|
|
243
|
+
if (!thenRun) assert.deepEqual(fused.content, standalone.content);
|
|
244
|
+
assert.equal(fused.content.length, 1);
|
|
245
|
+
const report = reportOf(fused, "edit");
|
|
246
|
+
assert.equal(report?.publication, publication);
|
|
247
|
+
assert.equal(report?.freshness, fresh ? undefined : "changed");
|
|
248
|
+
assert.equal(
|
|
249
|
+
report?.then_run?.status,
|
|
250
|
+
thenRun ? (fresh ? "succeeded" : "skipped") : undefined,
|
|
251
|
+
);
|
|
252
|
+
assert.equal(commands, thenRun && fresh ? 1 : 0);
|
|
253
|
+
for (const [key, value] of Object.entries(expectedDetails))
|
|
254
|
+
assert.deepEqual(Reflect.get(fused.details, key), value);
|
|
255
|
+
assert.equal(await readFile(path, "utf8"), content);
|
|
256
|
+
}
|
|
257
|
+
assert.deepEqual(mutation.details, expectedDetails);
|
|
228
258
|
}
|
|
229
259
|
}
|
|
230
260
|
}
|
|
@@ -233,91 +263,21 @@ test("standalone and Fusion finalizers use commit freshness with or without anch
|
|
|
233
263
|
}
|
|
234
264
|
});
|
|
235
265
|
|
|
236
|
-
test("
|
|
266
|
+
test("aggregate anchor diagnostics → respect their byte budgets", async (t) => {
|
|
237
267
|
const dir = await mkdtemp(join(tmpdir(), "hashline-budget-"));
|
|
238
268
|
try {
|
|
239
269
|
const path = join(dir, "long.txt");
|
|
240
|
-
const long = "界".repeat(100000);
|
|
241
|
-
const changedLines = [long, ...Array.from({ length: 79 }, (_, index) => `changed ${index}`)];
|
|
242
|
-
for (const name of ["edit", "replace"]) {
|
|
243
|
-
await writeFile(path, "before\n");
|
|
244
|
-
const result =
|
|
245
|
-
name === "edit"
|
|
246
|
-
? await invoke(
|
|
247
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
248
|
-
name,
|
|
249
|
-
{ path, edits: [{ op: "append", body: changedLines }] },
|
|
250
|
-
undefined,
|
|
251
|
-
undefined,
|
|
252
|
-
await ctx(dir, t),
|
|
253
|
-
)
|
|
254
|
-
: await invoke(
|
|
255
|
-
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
256
|
-
name,
|
|
257
|
-
{ path, replacements: [{ find: "before", replace: changedLines.join("\n") }] },
|
|
258
|
-
undefined,
|
|
259
|
-
undefined,
|
|
260
|
-
await ctx(dir, t),
|
|
261
|
-
);
|
|
262
|
-
const output = text(result);
|
|
263
|
-
const firstLine = name === "edit" ? 2 : 1;
|
|
264
|
-
assert.deepEqual(
|
|
265
|
-
output.match(/^\d+#[0-9A-Z]+$/gm),
|
|
266
|
-
changedLines.map((line, index) => {
|
|
267
|
-
const position = firstLine + index;
|
|
268
|
-
return `${position}#${computeLineHash(position, line, 4)}`;
|
|
269
|
-
}),
|
|
270
|
-
);
|
|
271
|
-
assert.doesNotMatch(output, /omitted|truncated|│/i);
|
|
272
|
-
assert.equal(
|
|
273
|
-
await readFile(path, "utf8"),
|
|
274
|
-
(name === "edit" ? "before\n" : "") + changedLines.join("\n") + "\n",
|
|
275
|
-
);
|
|
276
|
-
await writeFile(path, `remove\n${long}\n`);
|
|
277
|
-
const deleted =
|
|
278
|
-
name === "edit"
|
|
279
|
-
? await invoke(
|
|
280
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
281
|
-
name,
|
|
282
|
-
{ path, edits: [{ op: "delete", anchor: `1#${computeLineHash(1, "remove", 4)}` }] },
|
|
283
|
-
undefined,
|
|
284
|
-
undefined,
|
|
285
|
-
await ctx(dir, t),
|
|
286
|
-
)
|
|
287
|
-
: await invoke(
|
|
288
|
-
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
289
|
-
name,
|
|
290
|
-
{ path, replacements: [{ find: "remove\n", replace: "" }] },
|
|
291
|
-
undefined,
|
|
292
|
-
undefined,
|
|
293
|
-
await ctx(dir, t),
|
|
294
|
-
);
|
|
295
|
-
const anchorStart = text(deleted).indexOf("\nUpdated anchors:");
|
|
296
|
-
assert.ok(anchorStart >= 0);
|
|
297
|
-
const anchorBlock = text(deleted).slice(anchorStart);
|
|
298
|
-
assert.ok(Buffer.byteLength(anchorBlock) <= MAX_BLOCK_BYTES);
|
|
299
|
-
assert.match(text(deleted), ANCHORS_OMITTED);
|
|
300
|
-
assert.doesNotMatch(text(deleted), /^\d+#[0-9A-Z]+/m);
|
|
301
|
-
}
|
|
302
270
|
await writeFile(path, "current\n");
|
|
303
|
-
await
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
},
|
|
311
|
-
undefined,
|
|
312
|
-
undefined,
|
|
313
|
-
await ctx(dir, t),
|
|
314
|
-
),
|
|
315
|
-
(error: Error) => {
|
|
316
|
-
assertFailureByteBudgets(error.message);
|
|
317
|
-
assert.match(error.message, /omitted|truncated/i);
|
|
318
|
-
return true;
|
|
319
|
-
},
|
|
271
|
+
const failure = await invoke(
|
|
272
|
+
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
273
|
+
"errors",
|
|
274
|
+
{ path, edits: Array.from({ length: 1000 }, () => ({ op: "delete", anchor: "1#XXXX" })) },
|
|
275
|
+
undefined,
|
|
276
|
+
undefined,
|
|
277
|
+
await ctx(dir, t),
|
|
320
278
|
);
|
|
279
|
+
assert.equal(failure.isError, true);
|
|
280
|
+
assertFailureByteBudgets(text(failure));
|
|
321
281
|
} finally {
|
|
322
282
|
await rm(dir, { recursive: true, force: true });
|
|
323
283
|
}
|
|
@@ -331,7 +291,7 @@ test("unpaired surrogate arguments are rejected before Fusion", async (t) => {
|
|
|
331
291
|
const fusion = createActionFusionExecutor(
|
|
332
292
|
async () => {
|
|
333
293
|
commands++;
|
|
334
|
-
return { status: "succeeded",
|
|
294
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
|
|
335
295
|
},
|
|
336
296
|
(event) => events.push(event.command),
|
|
337
297
|
);
|
|
@@ -347,7 +307,7 @@ test("unpaired surrogate arguments are rejected before Fusion", async (t) => {
|
|
|
347
307
|
tool: makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
|
|
348
308
|
args: {
|
|
349
309
|
path,
|
|
350
|
-
replacements: [{ find: "original", replace: "\udfff" }],
|
|
310
|
+
replacements: [{ literal: true, find: "original", replace: "\udfff" }],
|
|
351
311
|
then_run: { command: "check" },
|
|
352
312
|
},
|
|
353
313
|
field: "replacements[0].replace",
|
|
@@ -363,7 +323,7 @@ test("unpaired surrogate arguments are rejected before Fusion", async (t) => {
|
|
|
363
323
|
callTool(tool, args, { ctx: await ctx(dir, t) }),
|
|
364
324
|
(error: unknown) =>
|
|
365
325
|
error instanceof Error &&
|
|
366
|
-
|
|
326
|
+
rejectsArgument(field, /encoded losslessly/)(error) &&
|
|
367
327
|
!("publication" in error),
|
|
368
328
|
);
|
|
369
329
|
assert.equal(await readFile(path, "utf8"), "original\n");
|
|
@@ -382,29 +342,27 @@ test("ambiguous recovery bounds candidate lists and never claims content identit
|
|
|
382
342
|
const lines = Array.from({ length: 100 }, () => "same");
|
|
383
343
|
lines[49] = "different";
|
|
384
344
|
await writeFile(path, lines.join("\n"));
|
|
385
|
-
await
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
})),
|
|
395
|
-
},
|
|
396
|
-
undefined,
|
|
397
|
-
undefined,
|
|
398
|
-
await ctx(dir, t),
|
|
399
|
-
),
|
|
400
|
-
(error: Error) => {
|
|
401
|
-
assertFailureByteBudgets(error.message);
|
|
402
|
-
assert.match(error.message, /ambiguous checksum matches/);
|
|
403
|
-
assert.match(error.message, /candidates omitted/);
|
|
404
|
-
assert.doesNotMatch(error.message, /same content/);
|
|
405
|
-
return true;
|
|
345
|
+
const failure = await invoke(
|
|
346
|
+
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
347
|
+
"ambiguous",
|
|
348
|
+
{
|
|
349
|
+
path,
|
|
350
|
+
edits: Array.from({ length: 1000 }, () => ({
|
|
351
|
+
op: "delete",
|
|
352
|
+
anchor: `50#${computeLineHash(50, "same", 4)}`,
|
|
353
|
+
})),
|
|
406
354
|
},
|
|
355
|
+
undefined,
|
|
356
|
+
undefined,
|
|
357
|
+
await ctx(dir, t),
|
|
358
|
+
);
|
|
359
|
+
assert.equal(failure.isError, true);
|
|
360
|
+
assertFailureByteBudgets(text(failure));
|
|
361
|
+
const report = reportOf(failure, "edit");
|
|
362
|
+
assert.ok(
|
|
363
|
+
report?.failures?.every((entry) => entry.result === "ambiguous" && entry.omittedCandidates),
|
|
407
364
|
);
|
|
365
|
+
assert.doesNotMatch(text(failure), /same content/);
|
|
408
366
|
} finally {
|
|
409
367
|
await rm(dir, { recursive: true, force: true });
|
|
410
368
|
}
|
|
@@ -420,7 +378,7 @@ test("NUL arguments are rejected before Fusion for all mutation tools", async (t
|
|
|
420
378
|
const fusion = createActionFusionExecutor(
|
|
421
379
|
async () => {
|
|
422
380
|
commands++;
|
|
423
|
-
return { status: "succeeded",
|
|
381
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "unexpected" }] } };
|
|
424
382
|
},
|
|
425
383
|
(event) => events.push(event.command),
|
|
426
384
|
);
|
|
@@ -434,7 +392,7 @@ test("NUL arguments are rejected before Fusion for all mutation tools", async (t
|
|
|
434
392
|
tool: makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
|
|
435
393
|
args: {
|
|
436
394
|
path,
|
|
437
|
-
replacements: [{ find: "original", replace: "\0" }],
|
|
395
|
+
replacements: [{ literal: true, find: "original", replace: "\0" }],
|
|
438
396
|
then_run: { command: "check" },
|
|
439
397
|
},
|
|
440
398
|
field: "replacements[0].replace",
|
|
@@ -450,7 +408,7 @@ test("NUL arguments are rejected before Fusion for all mutation tools", async (t
|
|
|
450
408
|
callTool(tool, args, { ctx: await ctx(dir, t) }),
|
|
451
409
|
(error: unknown) =>
|
|
452
410
|
error instanceof Error &&
|
|
453
|
-
|
|
411
|
+
rejectsArgument(field, /NUL/)(error) &&
|
|
454
412
|
!("publication" in error),
|
|
455
413
|
);
|
|
456
414
|
assert.equal(await readFile(path, "utf8"), "original\n");
|
|
@@ -473,25 +431,23 @@ test("Fusion skips the command when a replacement produces unencodable text", as
|
|
|
473
431
|
const fusion = createActionFusionExecutor(
|
|
474
432
|
async () => {
|
|
475
433
|
commands++;
|
|
476
|
-
return { status: "succeeded",
|
|
434
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "unexpected" }] } };
|
|
477
435
|
},
|
|
478
436
|
(event) => events.push(event.command),
|
|
479
437
|
);
|
|
480
|
-
await
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
{ ctx: await ctx(dir, t) },
|
|
489
|
-
),
|
|
490
|
-
(error: any) =>
|
|
491
|
-
error.publication === "NOT_PUBLISHED" &&
|
|
492
|
-
error.command === "skipped" &&
|
|
493
|
-
/INVALID_UNICODE/.test(error.message),
|
|
438
|
+
const result = await callTool(
|
|
439
|
+
makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
|
|
440
|
+
{
|
|
441
|
+
path,
|
|
442
|
+
replacements: [{ find: "^.", replace: "x", literal: false }],
|
|
443
|
+
then_run: { command: "check" },
|
|
444
|
+
},
|
|
445
|
+
{ ctx: await ctx(dir, t) },
|
|
494
446
|
);
|
|
447
|
+
assert.equal(result.isError, true);
|
|
448
|
+
assert.equal(reportOf(result, "replace")?.error, "INVALID_UNICODE");
|
|
449
|
+
assert.equal(reportOf(result, "replace")?.publication, "NOT_PUBLISHED");
|
|
450
|
+
assert.equal(reportOf(result, "replace")?.then_run?.status, "skipped");
|
|
495
451
|
assert.deepEqual(events, ["waiting", "skipped"]);
|
|
496
452
|
assert.equal(await readFile(path, "utf8"), before);
|
|
497
453
|
assert.equal(commands, 0);
|
|
@@ -500,12 +456,12 @@ test("Fusion skips the command when a replacement produces unencodable text", as
|
|
|
500
456
|
}
|
|
501
457
|
});
|
|
502
458
|
|
|
503
|
-
test("
|
|
459
|
+
test("result generation failure → preserves publication and cause", () => {
|
|
504
460
|
for (const publication of ["NOT_PUBLISHED", "PUBLISHED", "UNKNOWN"] as const) {
|
|
505
461
|
const cause = new Error("render failed");
|
|
506
462
|
assert.throws(
|
|
507
463
|
() =>
|
|
508
|
-
postProcessMutation(
|
|
464
|
+
postProcessMutation(publication, () => {
|
|
509
465
|
throw cause;
|
|
510
466
|
}),
|
|
511
467
|
(error: any) =>
|
|
@@ -515,23 +471,6 @@ test("shared result building preserves publication and appends anchors only to t
|
|
|
515
471
|
error.cause === cause,
|
|
516
472
|
);
|
|
517
473
|
}
|
|
518
|
-
const result = {
|
|
519
|
-
content: [
|
|
520
|
-
{ type: "text" as const, text: "summary" },
|
|
521
|
-
{ type: "text" as const, text: "command" },
|
|
522
|
-
],
|
|
523
|
-
details: {},
|
|
524
|
-
};
|
|
525
|
-
const outcome = publishedMutation("saved", result, " ANCHOR");
|
|
526
|
-
assert.deepEqual(
|
|
527
|
-
finalizeMutation(outcome, true).content.map((block: any) => block.text),
|
|
528
|
-
["summary ANCHOR", "command"],
|
|
529
|
-
);
|
|
530
|
-
assert.deepEqual(finalizeMutation(outcome, false).content, [
|
|
531
|
-
...result.content,
|
|
532
|
-
{ type: "text", text: staleTargetNotice() },
|
|
533
|
-
]);
|
|
534
|
-
assert.equal(result.content[0].text, "summary");
|
|
535
474
|
});
|
|
536
475
|
|
|
537
476
|
type Fusion = Parameters<typeof makeWriteOverride>[1];
|
|
@@ -550,7 +489,7 @@ const noOpCases = [
|
|
|
550
489
|
},
|
|
551
490
|
{
|
|
552
491
|
makeTool: (cwd: string, fusion?: Fusion) => makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
|
|
553
|
-
params: { replacements: [{ find: "same", replace: "same" }] },
|
|
492
|
+
params: { replacements: [{ literal: true, find: "same", replace: "same" }] },
|
|
554
493
|
},
|
|
555
494
|
{ makeTool: makeWriteOverride, params: { content: "same\n", mode: "overwrite" } },
|
|
556
495
|
];
|
|
@@ -566,7 +505,7 @@ test("all mutation tools succeed without rewriting on no-op, with and without Fu
|
|
|
566
505
|
let commands = 0;
|
|
567
506
|
const fusion = createActionFusionExecutor(async () => {
|
|
568
507
|
commands++;
|
|
569
|
-
return { status: "succeeded",
|
|
508
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
|
|
570
509
|
});
|
|
571
510
|
const tool: any = makeTool(dir, mode === "standalone" ? undefined : fusion);
|
|
572
511
|
const result = await invoke(
|
|
@@ -577,21 +516,14 @@ test("all mutation tools succeed without rewriting on no-op, with and without Fu
|
|
|
577
516
|
undefined,
|
|
578
517
|
await ctx(dir, t),
|
|
579
518
|
);
|
|
580
|
-
assert.
|
|
581
|
-
assert.equal(result.
|
|
582
|
-
const revision = byteRevision(Buffer.from("same\n"));
|
|
583
|
-
for (const key of ["baseRevision", "publishedRevision", "observedRevision"])
|
|
584
|
-
assert.equal(result.details[key], revision);
|
|
585
|
-
assert.equal("revision" in result.details, false);
|
|
519
|
+
assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
|
|
520
|
+
assert.equal(reportOf(result, tool.name)?.freshness, undefined);
|
|
586
521
|
assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
|
|
587
522
|
assert.equal(commands, mode === "command" ? 1 : 0);
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
);
|
|
593
|
-
assert.equal(result.details.actionFusion.freshness, "unchanged");
|
|
594
|
-
}
|
|
523
|
+
assert.equal(
|
|
524
|
+
reportOf(result, tool.name)?.then_run?.status,
|
|
525
|
+
mode === "command" ? "succeeded" : undefined,
|
|
526
|
+
);
|
|
595
527
|
const after = await stat(path);
|
|
596
528
|
assert.deepEqual(
|
|
597
529
|
[after.ino, after.mtimeMs, after.ctimeMs],
|
|
@@ -618,7 +550,7 @@ test("no-op Fusion still detects external changes and reports command failures",
|
|
|
618
550
|
commands++;
|
|
619
551
|
if (scenario === "failed") throw new Error("check failed");
|
|
620
552
|
await writeFile(path, "external\n");
|
|
621
|
-
return { status: "succeeded",
|
|
553
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
|
|
622
554
|
},
|
|
623
555
|
(progress) => {
|
|
624
556
|
if (
|
|
@@ -638,15 +570,15 @@ test("no-op Fusion still detects external changes and reports command failures",
|
|
|
638
570
|
undefined,
|
|
639
571
|
await ctx(dir, t),
|
|
640
572
|
);
|
|
641
|
-
assert.equal(result.
|
|
573
|
+
assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
|
|
642
574
|
assert.equal(commands, scenario === "before" ? 0 : 1);
|
|
643
575
|
assert.equal(
|
|
644
|
-
result.
|
|
576
|
+
reportOf(result, tool.name)?.then_run?.status,
|
|
645
577
|
scenario === "before" ? "skipped" : scenario === "failed" ? "failed" : "succeeded",
|
|
646
578
|
);
|
|
647
579
|
assert.equal(
|
|
648
|
-
result.
|
|
649
|
-
scenario === "failed" ?
|
|
580
|
+
reportOf(result, tool.name)?.freshness,
|
|
581
|
+
scenario === "failed" ? undefined : "changed",
|
|
650
582
|
);
|
|
651
583
|
}
|
|
652
584
|
}
|
|
@@ -654,200 +586,3 @@ test("no-op Fusion still detects external changes and reports command failures",
|
|
|
654
586
|
await rm(dir, { recursive: true, force: true });
|
|
655
587
|
}
|
|
656
588
|
});
|
|
657
|
-
|
|
658
|
-
test("mutation anchors omit unchanged positions across distant changes", async (t) => {
|
|
659
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-anchor-delta-"));
|
|
660
|
-
try {
|
|
661
|
-
const before = Array.from({ length: 100 }, (_, index) => `row ${index + 1}`);
|
|
662
|
-
const after = [...before];
|
|
663
|
-
after[0] = "changed first";
|
|
664
|
-
after[99] = "changed last";
|
|
665
|
-
for (const name of ["edit", "replace"]) {
|
|
666
|
-
const path = join(dir, `${name}.txt`);
|
|
667
|
-
await writeFile(path, before.join("\r\n") + "\r\n");
|
|
668
|
-
const result =
|
|
669
|
-
name === "edit"
|
|
670
|
-
? await invoke(
|
|
671
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
672
|
-
name,
|
|
673
|
-
{
|
|
674
|
-
path,
|
|
675
|
-
edits: [
|
|
676
|
-
{
|
|
677
|
-
op: "replace",
|
|
678
|
-
anchor: `1#${computeLineHash(1, before[0], 4)}`,
|
|
679
|
-
end: `100#${computeLineHash(100, before[99], 4)}`,
|
|
680
|
-
body: after,
|
|
681
|
-
},
|
|
682
|
-
],
|
|
683
|
-
},
|
|
684
|
-
undefined,
|
|
685
|
-
undefined,
|
|
686
|
-
await ctx(dir, t),
|
|
687
|
-
)
|
|
688
|
-
: await invoke(
|
|
689
|
-
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
690
|
-
name,
|
|
691
|
-
{
|
|
692
|
-
path,
|
|
693
|
-
replacements: [
|
|
694
|
-
{ find: before[0] + "\r\n", replace: after[0] + "\r\n" },
|
|
695
|
-
{ find: before[99], replace: after[99] },
|
|
696
|
-
],
|
|
697
|
-
},
|
|
698
|
-
undefined,
|
|
699
|
-
undefined,
|
|
700
|
-
await ctx(dir, t),
|
|
701
|
-
);
|
|
702
|
-
const returned = [...text(result).matchAll(/^(\d+#[0-9A-Z]+)/gm)].map((match) => match[1]);
|
|
703
|
-
assert.deepEqual(returned, [
|
|
704
|
-
`1#${computeLineHash(1, after[0], 4)}`,
|
|
705
|
-
`100#${computeLineHash(100, after[99], 4)}`,
|
|
706
|
-
]);
|
|
707
|
-
assert.doesNotMatch(text(result), /omitted/);
|
|
708
|
-
// An omitted stable row keeps its old anchor; a changed row uses the returned anchor.
|
|
709
|
-
await invoke(
|
|
710
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
711
|
-
"chain",
|
|
712
|
-
{
|
|
713
|
-
path,
|
|
714
|
-
edits: [
|
|
715
|
-
{
|
|
716
|
-
op: "replace",
|
|
717
|
-
anchor: `50#${computeLineHash(50, before[49], 4)}`,
|
|
718
|
-
body: ["stable anchor reused"],
|
|
719
|
-
},
|
|
720
|
-
{ op: "replace", anchor: returned[1], body: ["fresh anchor reused"] },
|
|
721
|
-
],
|
|
722
|
-
},
|
|
723
|
-
undefined,
|
|
724
|
-
undefined,
|
|
725
|
-
await ctx(dir, t),
|
|
726
|
-
);
|
|
727
|
-
const final = (await readFile(path, "utf8")).split("\r\n");
|
|
728
|
-
assert.equal(final[49], "stable anchor reused");
|
|
729
|
-
assert.equal(final[99], "fresh anchor reused");
|
|
730
|
-
}
|
|
731
|
-
} finally {
|
|
732
|
-
await rm(dir, { recursive: true, force: true });
|
|
733
|
-
}
|
|
734
|
-
});
|
|
735
|
-
|
|
736
|
-
test("mutation anchors retain a deletion successor but omit stable rows and deleted EOF", async (t) => {
|
|
737
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-anchor-delete-"));
|
|
738
|
-
try {
|
|
739
|
-
for (const name of ["edit", "replace"]) {
|
|
740
|
-
for (const atEnd of [false, true]) {
|
|
741
|
-
for (const ending of ["\n", "\r\n"]) {
|
|
742
|
-
const path = join(dir, `${name}.txt`);
|
|
743
|
-
const lines = atEnd ? ["a", "c", "remove"] : ["a", "remove", "c", "d"];
|
|
744
|
-
await writeFile(path, lines.join(ending) + ending);
|
|
745
|
-
const line = atEnd ? 3 : 2;
|
|
746
|
-
const result =
|
|
747
|
-
name === "edit"
|
|
748
|
-
? await invoke(
|
|
749
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
750
|
-
name,
|
|
751
|
-
{
|
|
752
|
-
path,
|
|
753
|
-
edits: [
|
|
754
|
-
{ op: "delete", anchor: `${line}#${computeLineHash(line, "remove", 4)}` },
|
|
755
|
-
],
|
|
756
|
-
},
|
|
757
|
-
undefined,
|
|
758
|
-
undefined,
|
|
759
|
-
await ctx(dir, t),
|
|
760
|
-
)
|
|
761
|
-
: await invoke(
|
|
762
|
-
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
763
|
-
name,
|
|
764
|
-
{ path, replacements: [{ find: "remove\n", replace: "" }] },
|
|
765
|
-
undefined,
|
|
766
|
-
undefined,
|
|
767
|
-
await ctx(dir, t),
|
|
768
|
-
);
|
|
769
|
-
const rows = text(result)
|
|
770
|
-
.split("\n")
|
|
771
|
-
.filter((row) => /^\d+#/.test(row));
|
|
772
|
-
assert.deepEqual(rows, atEnd ? [] : [`2#${computeLineHash(2, "c", 4)}│c`]);
|
|
773
|
-
assert.deepEqual(
|
|
774
|
-
await readFile(path),
|
|
775
|
-
Buffer.from(lines.filter((_, index) => index !== line - 1).join(ending) + ending),
|
|
776
|
-
);
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
} finally {
|
|
781
|
-
await rm(dir, { recursive: true, force: true });
|
|
782
|
-
}
|
|
783
|
-
});
|
|
784
|
-
|
|
785
|
-
test("mutation anchors retain deletion successor even when deleted line content matches the successor", async (t) => {
|
|
786
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-anchor-matching-delete-"));
|
|
787
|
-
try {
|
|
788
|
-
const path = join(dir, "edit.txt");
|
|
789
|
-
const lines = ["a", "duplicate", "duplicate", "d"];
|
|
790
|
-
await writeFile(path, lines.join("\n") + "\n");
|
|
791
|
-
const tool = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
792
|
-
const result = await invoke(
|
|
793
|
-
tool,
|
|
794
|
-
"edit",
|
|
795
|
-
{
|
|
796
|
-
path,
|
|
797
|
-
edits: [{ op: "delete", anchor: `2#${computeLineHash(2, "duplicate", 4)}` }],
|
|
798
|
-
},
|
|
799
|
-
undefined,
|
|
800
|
-
undefined,
|
|
801
|
-
await ctx(dir, t),
|
|
802
|
-
);
|
|
803
|
-
const rows = text(result)
|
|
804
|
-
.split("\n")
|
|
805
|
-
.filter((row) => /^\d+#/.test(row));
|
|
806
|
-
assert.deepEqual(rows, [`2#${computeLineHash(2, "duplicate", 4)}│duplicate`]);
|
|
807
|
-
} finally {
|
|
808
|
-
await rm(dir, { recursive: true, force: true });
|
|
809
|
-
}
|
|
810
|
-
});
|
|
811
|
-
|
|
812
|
-
test("oversized deletion successors do not suppress later editable anchors", async (t) => {
|
|
813
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-skip-long-anchor-"));
|
|
814
|
-
try {
|
|
815
|
-
const path = join(dir, "fixture.txt");
|
|
816
|
-
const long = "x".repeat(MAX_BLOCK_BYTES + 1);
|
|
817
|
-
await writeFile(path, `remove\n${long}\nold\n`);
|
|
818
|
-
const tool = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
819
|
-
const result = await invoke(
|
|
820
|
-
tool,
|
|
821
|
-
"skip",
|
|
822
|
-
{
|
|
823
|
-
path,
|
|
824
|
-
edits: [
|
|
825
|
-
{ op: "delete", anchor: `1#${computeLineHash(1, "remove", 4)}` },
|
|
826
|
-
{ op: "replace", anchor: `3#${computeLineHash(3, "old", 4)}`, body: ["new"] },
|
|
827
|
-
],
|
|
828
|
-
},
|
|
829
|
-
undefined,
|
|
830
|
-
undefined,
|
|
831
|
-
await ctx(dir, t),
|
|
832
|
-
);
|
|
833
|
-
const output = text(result);
|
|
834
|
-
assert.doesNotMatch(output, /^1#[0-9A-Z]+/m);
|
|
835
|
-
const anchor = output.match(/^2#[0-9A-Z]+$/m)?.[0];
|
|
836
|
-
assert.equal(anchor, `2#${computeLineHash(2, "new", 4)}`);
|
|
837
|
-
assert.match(output, ANCHORS_OMITTED);
|
|
838
|
-
assert.ok(
|
|
839
|
-
Buffer.byteLength(output.slice(output.indexOf("\nUpdated anchors:"))) <= MAX_BLOCK_BYTES,
|
|
840
|
-
);
|
|
841
|
-
await invoke(
|
|
842
|
-
tool,
|
|
843
|
-
"retry",
|
|
844
|
-
{ path, edits: [{ op: "replace", anchor, body: ["verified"] }] },
|
|
845
|
-
undefined,
|
|
846
|
-
undefined,
|
|
847
|
-
await ctx(dir, t),
|
|
848
|
-
);
|
|
849
|
-
assert.equal(await readFile(path, "utf8"), `${long}\nverified\n`);
|
|
850
|
-
} finally {
|
|
851
|
-
await rm(dir, { recursive: true, force: true });
|
|
852
|
-
}
|
|
853
|
-
});
|