@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,243 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { readFile, writeFile } from "node:fs/promises";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { test } from "node:test";
|
|
5
|
-
import type { ToolCall } from "@earendil-works/pi-ai";
|
|
6
|
-
import { loadInstalledExtension } from "../testing/extension-loader.testing.ts";
|
|
7
|
-
import { openTestSession, responseStream, SESSION_TIMEOUT_MS } from "../testing/session.testing.ts";
|
|
8
|
-
import { reportOf } from "../pi/report.ts";
|
|
9
|
-
|
|
10
|
-
test("installed extension completes file, worker, and command operations", {
|
|
11
|
-
timeout: SESSION_TIMEOUT_MS,
|
|
12
|
-
}, async (t) => {
|
|
13
|
-
const cwd = process.cwd();
|
|
14
|
-
await writeFile(
|
|
15
|
-
join(cwd, "check.mjs"),
|
|
16
|
-
[
|
|
17
|
-
'import assert from "node:assert/strict";',
|
|
18
|
-
'import { readFileSync } from "node:fs";',
|
|
19
|
-
'assert.equal(readFileSync("fixture.txt", "utf8"), "before\\nkeep\\n");',
|
|
20
|
-
'console.log("PACKAGE_COMMAND_OK");',
|
|
21
|
-
].join("\n"),
|
|
22
|
-
);
|
|
23
|
-
const factory = await loadInstalledExtension(process.argv[2]);
|
|
24
|
-
const { session, model } = await openTestSession(t, {
|
|
25
|
-
cwd,
|
|
26
|
-
tools: ["write", "read", "grep", "edit", "replace", "bash"],
|
|
27
|
-
configure: (pi) => factory(pi),
|
|
28
|
-
});
|
|
29
|
-
const result = (id: string) => {
|
|
30
|
-
const message = session.agent.state.messages.find(
|
|
31
|
-
(message) => message.role === "toolResult" && message.toolCallId === id,
|
|
32
|
-
);
|
|
33
|
-
assert.ok(message?.role === "toolResult", `Missing tool result ${id}`);
|
|
34
|
-
assert.equal(message.isError, false);
|
|
35
|
-
return message;
|
|
36
|
-
};
|
|
37
|
-
const text = (id: string) =>
|
|
38
|
-
result(id)
|
|
39
|
-
.content.filter((block) => block.type === "text")
|
|
40
|
-
.map((block) => block.text)
|
|
41
|
-
.join("\n");
|
|
42
|
-
const anchor = (id: string) => {
|
|
43
|
-
const match = /^(1#[0-9A-Z]+)│before$/m.exec(text(id));
|
|
44
|
-
assert.ok(match, `Missing copied anchor in ${id}`);
|
|
45
|
-
return match[1];
|
|
46
|
-
};
|
|
47
|
-
const call = (id: string, name: string, args: ToolCall["arguments"]): ToolCall => ({
|
|
48
|
-
type: "toolCall",
|
|
49
|
-
id,
|
|
50
|
-
name,
|
|
51
|
-
arguments: args,
|
|
52
|
-
});
|
|
53
|
-
const actions = [
|
|
54
|
-
() =>
|
|
55
|
-
call("write", "write", {
|
|
56
|
-
path: "fixture.txt",
|
|
57
|
-
content: "before\nkeep\n",
|
|
58
|
-
mode: "create",
|
|
59
|
-
then_run: { command: "node check.mjs", timeout: 5 },
|
|
60
|
-
}),
|
|
61
|
-
() => call("read", "read", { path: "fixture.txt" }),
|
|
62
|
-
() => call("grep", "grep", { path: "fixture.txt", pattern: "before", literal: true }),
|
|
63
|
-
() => {
|
|
64
|
-
assert.equal(anchor("grep"), anchor("read"));
|
|
65
|
-
return call("edit", "edit", {
|
|
66
|
-
path: "fixture.txt",
|
|
67
|
-
steps: [{ action: "replace", anchor: anchor("grep"), lines: ["edited"] }],
|
|
68
|
-
});
|
|
69
|
-
},
|
|
70
|
-
() =>
|
|
71
|
-
call("replace", "replace", {
|
|
72
|
-
path: "fixture.txt",
|
|
73
|
-
replacements: [{ find: "(edited)", replace: "$1 after", literal: false }],
|
|
74
|
-
}),
|
|
75
|
-
];
|
|
76
|
-
let next = 0;
|
|
77
|
-
session.agent.streamFunction = () => {
|
|
78
|
-
const action = actions[next++];
|
|
79
|
-
if (action) return responseStream(model, { content: [action()], stopReason: "toolUse" });
|
|
80
|
-
return responseStream(model, {
|
|
81
|
-
content: [{ type: "text", text: "Done." }],
|
|
82
|
-
stopReason: "stop",
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
await session.prompt("Complete the packaged tool workflow.");
|
|
86
|
-
assert.equal(session.agent.state.errorMessage, undefined);
|
|
87
|
-
assert.equal(next, actions.length + 1);
|
|
88
|
-
for (const id of ["write", "read", "grep", "edit", "replace"]) result(id);
|
|
89
|
-
const command = result("write")
|
|
90
|
-
.content.map((block) =>
|
|
91
|
-
block.type === "text" ? reportOf({ content: [block] }, "write")?.then_run : undefined,
|
|
92
|
-
)
|
|
93
|
-
.find((facts) => facts !== undefined);
|
|
94
|
-
assert.equal(command?.status, "succeeded");
|
|
95
|
-
assert.match(command?.output ?? "", /PACKAGE_COMMAND_OK/);
|
|
96
|
-
assert.equal(await readFile(join(cwd, "fixture.txt"), "utf8"), "edited after\nkeep\n");
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("installed extension argument failures → precise independent issues and host coercion without input mutation", {
|
|
100
|
-
timeout: SESSION_TIMEOUT_MS,
|
|
101
|
-
}, async (t) => {
|
|
102
|
-
const cwd = process.cwd();
|
|
103
|
-
const factory = await loadInstalledExtension(process.argv[2]);
|
|
104
|
-
const { session, model } = await openTestSession(t, {
|
|
105
|
-
cwd,
|
|
106
|
-
tools: ["write", "read", "grep", "edit", "replace"],
|
|
107
|
-
configure: (pi) => factory(pi),
|
|
108
|
-
});
|
|
109
|
-
const cases = [
|
|
110
|
-
{
|
|
111
|
-
name: "grep",
|
|
112
|
-
args: { pattern: "text", literal: true, outputMode: "rows" },
|
|
113
|
-
fields: ["outputMode"],
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
name: "replace",
|
|
117
|
-
args: {
|
|
118
|
-
path: "diagnostic.txt",
|
|
119
|
-
mode: "regex",
|
|
120
|
-
replacements: [{ find: "text", replace: "value", literal: true }],
|
|
121
|
-
},
|
|
122
|
-
fields: ["mode"],
|
|
123
|
-
},
|
|
124
|
-
{
|
|
125
|
-
name: "replace",
|
|
126
|
-
args: {
|
|
127
|
-
path: "diagnostic.txt",
|
|
128
|
-
replacements: [{ find: "text", replace: "value", literal: true, mode: "regex" }],
|
|
129
|
-
},
|
|
130
|
-
fields: ["replacements[0].mode"],
|
|
131
|
-
},
|
|
132
|
-
{
|
|
133
|
-
name: "grep",
|
|
134
|
-
args: { pattern: "(", literal: false, outputMode: "rows", context: null },
|
|
135
|
-
fields: ["pattern[0]", "outputMode"],
|
|
136
|
-
},
|
|
137
|
-
{
|
|
138
|
-
name: "replace",
|
|
139
|
-
args: {
|
|
140
|
-
path: "diagnostic.txt",
|
|
141
|
-
replacements: [{ find: "(", replace: "value", literal: false, flags: "x", mode: "regex" }],
|
|
142
|
-
},
|
|
143
|
-
fields: ["replacements[0].flags", "replacements[0].mode"],
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
name: "read",
|
|
147
|
-
args: { path: "diagnostic.txt", offset: -1, limit: 0 },
|
|
148
|
-
fields: ["offset", "limit"],
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
name: "write",
|
|
152
|
-
args: { path: "diagnostic.txt", content: "text", mode: "invalid" },
|
|
153
|
-
fields: ["mode"],
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
name: "edit",
|
|
157
|
-
args: { path: "diagnostic.txt", steps: { action: "append", lines: [] } },
|
|
158
|
-
fields: ["steps[0].lines"],
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
name: "write",
|
|
162
|
-
args: { path: "diagnostic.txt", content: 42, mode: "create", then_run: null },
|
|
163
|
-
fields: [],
|
|
164
|
-
},
|
|
165
|
-
{ name: "read", args: { path: "diagnostic.txt", offset: "1", limit: null }, fields: [] },
|
|
166
|
-
{
|
|
167
|
-
name: "grep",
|
|
168
|
-
args: {
|
|
169
|
-
path: "diagnostic.txt",
|
|
170
|
-
pattern: "42",
|
|
171
|
-
literal: "true",
|
|
172
|
-
outputMode: "count",
|
|
173
|
-
},
|
|
174
|
-
fields: [],
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
name: "replace",
|
|
178
|
-
args: {
|
|
179
|
-
path: "diagnostic.txt",
|
|
180
|
-
replacements: { find: 42, replace: 43, literal: "true", flags: null },
|
|
181
|
-
},
|
|
182
|
-
fields: ["replacements[0].flags"],
|
|
183
|
-
},
|
|
184
|
-
{
|
|
185
|
-
name: "replace",
|
|
186
|
-
args: {
|
|
187
|
-
path: "diagnostic.txt",
|
|
188
|
-
replacements: [{ find: 42, replace: 42, literal: "true", flags: null }],
|
|
189
|
-
},
|
|
190
|
-
fields: [],
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
name: "replace",
|
|
194
|
-
args: {
|
|
195
|
-
path: "diagnostic.txt",
|
|
196
|
-
replacements: { find: 42, replace: 43, literal: "true" },
|
|
197
|
-
},
|
|
198
|
-
fields: [],
|
|
199
|
-
},
|
|
200
|
-
] as const;
|
|
201
|
-
const original = structuredClone(cases);
|
|
202
|
-
let next = 0;
|
|
203
|
-
session.agent.streamFunction = () => {
|
|
204
|
-
const scenario = cases[next++];
|
|
205
|
-
return responseStream(
|
|
206
|
-
model,
|
|
207
|
-
scenario
|
|
208
|
-
? {
|
|
209
|
-
content: [
|
|
210
|
-
{
|
|
211
|
-
type: "toolCall",
|
|
212
|
-
id: `diagnostic-${next}`,
|
|
213
|
-
name: scenario.name,
|
|
214
|
-
arguments: scenario.args,
|
|
215
|
-
},
|
|
216
|
-
],
|
|
217
|
-
stopReason: "toolUse",
|
|
218
|
-
}
|
|
219
|
-
: { content: [{ type: "text", text: "Done." }], stopReason: "stop" },
|
|
220
|
-
);
|
|
221
|
-
};
|
|
222
|
-
await session.prompt("Exercise installed argument validation.");
|
|
223
|
-
assert.equal(session.agent.state.errorMessage, undefined);
|
|
224
|
-
assert.equal(next, cases.length + 1);
|
|
225
|
-
for (const [index, scenario] of cases.entries()) {
|
|
226
|
-
const result = session.agent.state.messages.find(
|
|
227
|
-
(message) =>
|
|
228
|
-
message.role === "toolResult" && message.toolCallId === `diagnostic-${index + 1}`,
|
|
229
|
-
);
|
|
230
|
-
assert.ok(result?.role === "toolResult");
|
|
231
|
-
assert.equal(result.isError, scenario.fields.length > 0, JSON.stringify(result));
|
|
232
|
-
if (scenario.fields.length) {
|
|
233
|
-
const report = reportOf(result, scenario.name);
|
|
234
|
-
assert.equal(report?.error, "INVALID_ARGUMENTS");
|
|
235
|
-
assert.deepEqual(
|
|
236
|
-
report?.issues?.map((issue) => issue.field).sort(),
|
|
237
|
-
[...scenario.fields].sort(),
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
assert.deepEqual(cases, original);
|
|
242
|
-
assert.equal(await readFile(join(cwd, "diagnostic.txt"), "utf8"), "43");
|
|
243
|
-
});
|
|
@@ -1,454 +0,0 @@
|
|
|
1
|
-
import { commandCompletion, type SessionCommand } from "../pi/action-fusion.testing.ts";
|
|
2
|
-
/** Real file-handle races at publication and Fusion observation boundaries. */
|
|
3
|
-
import assert from "node:assert/strict";
|
|
4
|
-
import fs from "node:fs";
|
|
5
|
-
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
6
|
-
import { syncBuiltinESMExports } from "node:module";
|
|
7
|
-
import { tmpdir } from "node:os";
|
|
8
|
-
import { join } from "node:path";
|
|
9
|
-
import { test, type TestContext } from "node:test";
|
|
10
|
-
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
11
|
-
import { createActionFusionExecutor } from "../pi/action-fusion.ts";
|
|
12
|
-
import { DEFAULT_CONFIG } from "../pi/tool-factories.testing.ts";
|
|
13
|
-
import { makeEditOverride } from "../pi/tool-factories.testing.ts";
|
|
14
|
-
import { reportOf } from "../pi/report.ts";
|
|
15
|
-
import { expectToolFailure } from "../pi/report-failure.testing.ts";
|
|
16
|
-
import { PUBLICATION_NEXT } from "../pi/report-schema.ts";
|
|
17
|
-
import { makeReplaceTool } from "../pi/tool-factories.testing.ts";
|
|
18
|
-
import { callTool, createToolContext } from "../pi/tool-call.testing.ts";
|
|
19
|
-
import { makeWriteOverride } from "../pi/tool-factories.testing.ts";
|
|
20
|
-
|
|
21
|
-
const BEFORE = "before\n";
|
|
22
|
-
const PUBLISHED = "before\nafter\n";
|
|
23
|
-
const CONCURRENT_APPEND = "other writer\n";
|
|
24
|
-
const READ_FAILURE = Object.assign(new Error("observation I/O failure"), { code: "EIO" });
|
|
25
|
-
type Disturbance = "append" | "delete" | "I/O failure";
|
|
26
|
-
|
|
27
|
-
async function targetFile(t: TestContext, content?: string) {
|
|
28
|
-
const directory = await mkdtemp(join(tmpdir(), "hashline-publication-read-"));
|
|
29
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
30
|
-
const path = join(directory, "target.txt");
|
|
31
|
-
if (content !== undefined) await writeFile(path, content);
|
|
32
|
-
return { directory, path };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Disturb the real file after its bytes are read, before withFileRead accepts the observation. */
|
|
36
|
-
async function withDisturbedRead<T>(
|
|
37
|
-
t: TestContext,
|
|
38
|
-
path: string,
|
|
39
|
-
expected: string,
|
|
40
|
-
disturbance: Disturbance,
|
|
41
|
-
ready: (bytes: Buffer) => boolean,
|
|
42
|
-
run: () => Promise<T>,
|
|
43
|
-
): Promise<T> {
|
|
44
|
-
const nativeOpen = fs.promises.open;
|
|
45
|
-
const expectedBytes = Buffer.from(expected);
|
|
46
|
-
let disturbed = false;
|
|
47
|
-
t.mock.method(fs.promises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
48
|
-
const handle = await nativeOpen(...args);
|
|
49
|
-
if (args[0] === path) {
|
|
50
|
-
const nativeRead = handle.read.bind(handle);
|
|
51
|
-
const chunks: Buffer[] = [];
|
|
52
|
-
t.mock.method(handle, "read", async (...readArgs: Parameters<typeof nativeRead>) => {
|
|
53
|
-
const result = await nativeRead(...readArgs);
|
|
54
|
-
chunks.push(Buffer.from(result.buffer as Uint8Array).subarray(0, result.bytesRead));
|
|
55
|
-
const bytes = Buffer.concat(chunks);
|
|
56
|
-
if (!disturbed && bytes.length >= expectedBytes.length && ready(bytes)) {
|
|
57
|
-
disturbed = true;
|
|
58
|
-
assert.deepEqual(bytes, expectedBytes);
|
|
59
|
-
if (disturbance === "I/O failure") throw READ_FAILURE;
|
|
60
|
-
if (disturbance === "delete") fs.unlinkSync(path);
|
|
61
|
-
else fs.appendFileSync(path, CONCURRENT_APPEND);
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
return handle;
|
|
67
|
-
});
|
|
68
|
-
syncBuiltinESMExports();
|
|
69
|
-
try {
|
|
70
|
-
return await run();
|
|
71
|
-
} finally {
|
|
72
|
-
t.mock.restoreAll();
|
|
73
|
-
syncBuiltinESMExports();
|
|
74
|
-
assert.equal(disturbed, true, "the intended observation must be disturbed");
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async function assertTarget(path: string, original: string, disturbance: Disturbance) {
|
|
79
|
-
if (disturbance === "delete") await assert.rejects(readFile(path), { code: "ENOENT" });
|
|
80
|
-
else {
|
|
81
|
-
const expected = original + (disturbance === "append" ? CONCURRENT_APPEND : "");
|
|
82
|
-
assert.deepEqual(await readFile(path), Buffer.from(expected));
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function text(result: AgentToolResult<unknown>): string {
|
|
87
|
-
return result.content.map((block) => (block.type === "text" ? block.text : "")).join("\n");
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const mutationCases = [
|
|
91
|
-
{
|
|
92
|
-
name: "edit",
|
|
93
|
-
make: (cwd: string, fusion?: ReturnType<typeof createActionFusionExecutor>) =>
|
|
94
|
-
makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
|
|
95
|
-
args: { steps: [{ action: "append", lines: ["after"] }] },
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
name: "replace",
|
|
99
|
-
make: (cwd: string, fusion?: ReturnType<typeof createActionFusionExecutor>) =>
|
|
100
|
-
makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
|
|
101
|
-
args: { replacements: [{ literal: true, find: BEFORE, replace: PUBLISHED }] },
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
name: "write overwrite",
|
|
105
|
-
make: makeWriteOverride,
|
|
106
|
-
args: { content: PUBLISHED, mode: "overwrite" },
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
name: "write create",
|
|
110
|
-
make: makeWriteOverride,
|
|
111
|
-
args: { content: PUBLISHED, mode: "create" },
|
|
112
|
-
},
|
|
113
|
-
] as const;
|
|
114
|
-
|
|
115
|
-
const prePublicationCases = [
|
|
116
|
-
{ phase: "inspect target", mutation: mutationCases[2], observation: 1 },
|
|
117
|
-
{ phase: "revision recheck", mutation: mutationCases[0], observation: 2 },
|
|
118
|
-
{
|
|
119
|
-
phase: "no-op check",
|
|
120
|
-
mutation: {
|
|
121
|
-
...mutationCases[1],
|
|
122
|
-
args: { replacements: [{ literal: true, find: BEFORE, replace: BEFORE }] },
|
|
123
|
-
},
|
|
124
|
-
observation: 2,
|
|
125
|
-
},
|
|
126
|
-
] as const;
|
|
127
|
-
|
|
128
|
-
for (const fused of [false, true]) {
|
|
129
|
-
test(`concurrent change during post-publication read without then_run (${fused ? "Fusion enabled" : "plain"}) → succeeds and withholds stale anchors`, async (t) => {
|
|
130
|
-
for (const mutation of mutationCases) {
|
|
131
|
-
for (const disturbance of ["append", "delete"] as const) {
|
|
132
|
-
await t.test(
|
|
133
|
-
`${mutation.name}: ${disturbance} → retains publication and reports changed freshness`,
|
|
134
|
-
async (t) => {
|
|
135
|
-
const { directory, path } = await targetFile(
|
|
136
|
-
t,
|
|
137
|
-
mutation.name === "write create" ? undefined : BEFORE,
|
|
138
|
-
);
|
|
139
|
-
const executeTool: SessionCommand = async () =>
|
|
140
|
-
assert.fail("command must not run without then_run");
|
|
141
|
-
const fusion = fused ? createActionFusionExecutor(() => {}) : undefined;
|
|
142
|
-
const result = await withDisturbedRead(
|
|
143
|
-
t,
|
|
144
|
-
path,
|
|
145
|
-
PUBLISHED,
|
|
146
|
-
disturbance,
|
|
147
|
-
(bytes) => bytes.equals(Buffer.from(PUBLISHED)),
|
|
148
|
-
() =>
|
|
149
|
-
callTool(
|
|
150
|
-
mutation.make(directory, fusion),
|
|
151
|
-
{ path, ...mutation.args },
|
|
152
|
-
{ ctx: { cwd: directory, executeTool } },
|
|
153
|
-
),
|
|
154
|
-
);
|
|
155
|
-
assert.equal(result.isError, undefined);
|
|
156
|
-
const report = reportOf(
|
|
157
|
-
result,
|
|
158
|
-
mutation.name === "edit" ? "edit" : mutation.name === "replace" ? "replace" : "write",
|
|
159
|
-
);
|
|
160
|
-
assert.equal(report?.publication, "PUBLISHED");
|
|
161
|
-
assert.match(report?.next ?? "", /Read the file before further edits/);
|
|
162
|
-
assert.doesNotMatch(text(result), /Updated anchors|retry the tool/);
|
|
163
|
-
assert.equal(report?.then_run, undefined);
|
|
164
|
-
assert.equal(report?.freshness, "changed");
|
|
165
|
-
await assertTarget(path, PUBLISHED, disturbance);
|
|
166
|
-
},
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
for (const observation of [1, 2]) {
|
|
174
|
-
test(`disturbance during pre-command observation ${observation} → skips then_run without retry advice or rollback`, async (t) => {
|
|
175
|
-
for (const disturbance of ["append", "delete", "I/O failure"] as const) {
|
|
176
|
-
await t.test(`${disturbance} → skips the command and preserves publication`, async (t) => {
|
|
177
|
-
const { directory, path } = await targetFile(t, BEFORE);
|
|
178
|
-
let mutationCompleted = false;
|
|
179
|
-
let observations = 0;
|
|
180
|
-
let commands = 0;
|
|
181
|
-
const executeTool: SessionCommand = async () => {
|
|
182
|
-
commands++;
|
|
183
|
-
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
184
|
-
};
|
|
185
|
-
const fusion = createActionFusionExecutor((progress) => {
|
|
186
|
-
if (progress.mutationCompleted) mutationCompleted = true;
|
|
187
|
-
});
|
|
188
|
-
const ctx = await createToolContext(directory, t);
|
|
189
|
-
const result = await withDisturbedRead(
|
|
190
|
-
t,
|
|
191
|
-
path,
|
|
192
|
-
PUBLISHED,
|
|
193
|
-
disturbance,
|
|
194
|
-
() => mutationCompleted && ++observations === observation,
|
|
195
|
-
() =>
|
|
196
|
-
callTool(
|
|
197
|
-
makeWriteOverride(directory, fusion),
|
|
198
|
-
{
|
|
199
|
-
path,
|
|
200
|
-
content: PUBLISHED,
|
|
201
|
-
mode: "overwrite",
|
|
202
|
-
then_run: { command: "check" },
|
|
203
|
-
},
|
|
204
|
-
{ ctx: { ...ctx, executeTool } },
|
|
205
|
-
),
|
|
206
|
-
);
|
|
207
|
-
assert.equal(commands, 0);
|
|
208
|
-
const report = reportOf(result, "write");
|
|
209
|
-
assert.equal(report?.publication, "PUBLISHED");
|
|
210
|
-
assert.equal(report?.then_run?.status, "skipped");
|
|
211
|
-
assert.equal(report?.freshness, disturbance === "I/O failure" ? "unknown" : "changed");
|
|
212
|
-
if (disturbance === "I/O failure") {
|
|
213
|
-
assert.equal(report?.freshnessError?.message, READ_FAILURE.message);
|
|
214
|
-
assert.equal(report?.freshnessError?.code, "EIO");
|
|
215
|
-
}
|
|
216
|
-
assert.match(report?.next ?? "", /Read the file before further edits/);
|
|
217
|
-
assert.doesNotMatch(text(result), /retry the tool/);
|
|
218
|
-
await assertTarget(path, PUBLISHED, disturbance);
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
test("disturbance during post-command freshness read → retains publication and reports stale anchors", async (t) => {
|
|
225
|
-
for (const disturbance of ["append", "delete", "I/O failure"] as const) {
|
|
226
|
-
await t.test(
|
|
227
|
-
`${disturbance} → freshness is ${disturbance === "I/O failure" ? "unknown" : "changed"} without retry advice`,
|
|
228
|
-
async (t) => {
|
|
229
|
-
const { directory, path } = await targetFile(t, BEFORE);
|
|
230
|
-
let commands = 0;
|
|
231
|
-
const executeTool: SessionCommand = async () => {
|
|
232
|
-
commands++;
|
|
233
|
-
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
234
|
-
};
|
|
235
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
236
|
-
const ctx = await createToolContext(directory, t);
|
|
237
|
-
const result = await withDisturbedRead(
|
|
238
|
-
t,
|
|
239
|
-
path,
|
|
240
|
-
PUBLISHED,
|
|
241
|
-
disturbance,
|
|
242
|
-
() => commands > 0,
|
|
243
|
-
() =>
|
|
244
|
-
callTool(
|
|
245
|
-
makeWriteOverride(directory, fusion),
|
|
246
|
-
{
|
|
247
|
-
path,
|
|
248
|
-
content: PUBLISHED,
|
|
249
|
-
mode: "overwrite",
|
|
250
|
-
then_run: { command: "check" },
|
|
251
|
-
},
|
|
252
|
-
{ ctx: { ...ctx, executeTool } },
|
|
253
|
-
),
|
|
254
|
-
);
|
|
255
|
-
assert.equal(commands, 1);
|
|
256
|
-
assert.equal(reportOf(result, "write")?.publication, "PUBLISHED");
|
|
257
|
-
assert.equal(reportOf(result, "write")?.then_run?.status, "succeeded");
|
|
258
|
-
assert.equal(
|
|
259
|
-
reportOf(result, "write")?.freshness,
|
|
260
|
-
disturbance === "I/O failure" ? "unknown" : "changed",
|
|
261
|
-
);
|
|
262
|
-
assert.match(reportOf(result, "write")?.next ?? "", /Read the file before further edits/);
|
|
263
|
-
assert.doesNotMatch(text(result), /Updated anchors|retry the tool/);
|
|
264
|
-
await assertTarget(path, PUBLISHED, disturbance);
|
|
265
|
-
},
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
test("other I/O failure during post-publication read → retains the published post-process error", async (t) => {
|
|
271
|
-
for (const fused of [false, true]) {
|
|
272
|
-
await t.test(fused ? "Fusion enabled" : "plain", async (t) => {
|
|
273
|
-
const { directory, path } = await targetFile(t, BEFORE);
|
|
274
|
-
let commands = 0;
|
|
275
|
-
const executeTool: SessionCommand = async () => {
|
|
276
|
-
commands++;
|
|
277
|
-
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
278
|
-
};
|
|
279
|
-
const fusion = fused ? createActionFusionExecutor(() => {}) : undefined;
|
|
280
|
-
const ctx = await createToolContext(directory, t);
|
|
281
|
-
await withDisturbedRead(
|
|
282
|
-
t,
|
|
283
|
-
path,
|
|
284
|
-
PUBLISHED,
|
|
285
|
-
"I/O failure",
|
|
286
|
-
(bytes) => bytes.equals(Buffer.from(PUBLISHED)),
|
|
287
|
-
() =>
|
|
288
|
-
expectToolFailure(
|
|
289
|
-
callTool(
|
|
290
|
-
makeWriteOverride(directory, fusion),
|
|
291
|
-
{
|
|
292
|
-
path,
|
|
293
|
-
content: PUBLISHED,
|
|
294
|
-
mode: "overwrite",
|
|
295
|
-
...(fused ? { then_run: { command: "check" } } : {}),
|
|
296
|
-
},
|
|
297
|
-
{ ctx: { ...ctx, executeTool } },
|
|
298
|
-
),
|
|
299
|
-
"POST_PROCESS_FAILED",
|
|
300
|
-
(report) => {
|
|
301
|
-
assert.equal(report.publication, "PUBLISHED");
|
|
302
|
-
assert.equal(report.cause?.message, READ_FAILURE.message);
|
|
303
|
-
assert.equal(report.cause?.code, "EIO");
|
|
304
|
-
assert.equal(report.then_run?.status, fused ? "skipped" : undefined);
|
|
305
|
-
assert.equal(report.next, PUBLICATION_NEXT.PUBLISHED);
|
|
306
|
-
},
|
|
307
|
-
),
|
|
308
|
-
);
|
|
309
|
-
assert.equal(commands, 0);
|
|
310
|
-
await assertTarget(path, PUBLISHED, "I/O failure");
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
test("concurrent change during pre-publication revision read → rejects without publishing and keeps retry advice", async (t) => {
|
|
316
|
-
for (const { phase, mutation, observation } of prePublicationCases) {
|
|
317
|
-
await t.test(`${phase} → NOT_PUBLISHED`, async (t) => {
|
|
318
|
-
const { directory, path } = await targetFile(t, BEFORE);
|
|
319
|
-
let targetReads = 0;
|
|
320
|
-
await withDisturbedRead(
|
|
321
|
-
t,
|
|
322
|
-
path,
|
|
323
|
-
BEFORE,
|
|
324
|
-
"append",
|
|
325
|
-
() => ++targetReads === observation,
|
|
326
|
-
() =>
|
|
327
|
-
expectToolFailure(
|
|
328
|
-
callTool(mutation.make(directory), { path, ...mutation.args }),
|
|
329
|
-
"FILE_CHANGED",
|
|
330
|
-
(report) => {
|
|
331
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
332
|
-
assert.equal(report.next, "Retry the call.");
|
|
333
|
-
},
|
|
334
|
-
),
|
|
335
|
-
);
|
|
336
|
-
assert.equal(targetReads, observation);
|
|
337
|
-
await assertTarget(path, BEFORE, "append");
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
test("target disappears or becomes a directory before publication → keeps the path error without publishing", async (t) => {
|
|
343
|
-
for (const { phase, mutation, observation } of prePublicationCases) {
|
|
344
|
-
for (const disturbance of ["missing", "directory"] as const) {
|
|
345
|
-
await t.test(`${phase}: ${disturbance}`, async (t) => {
|
|
346
|
-
const { directory, path } = await targetFile(t, BEFORE);
|
|
347
|
-
const originalPath = join(directory, "original.txt");
|
|
348
|
-
const nativeOpen = fs.promises.open;
|
|
349
|
-
let targetReads = 0;
|
|
350
|
-
t.mock.method(fs.promises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
351
|
-
if (args[0] === path && ++targetReads === observation) {
|
|
352
|
-
// Keep the displaced inode to prove the commit never changed its bytes.
|
|
353
|
-
fs.renameSync(path, originalPath);
|
|
354
|
-
if (disturbance === "directory") fs.mkdirSync(path);
|
|
355
|
-
}
|
|
356
|
-
return nativeOpen(...args);
|
|
357
|
-
});
|
|
358
|
-
syncBuiltinESMExports();
|
|
359
|
-
try {
|
|
360
|
-
await expectToolFailure(
|
|
361
|
-
callTool(mutation.make(directory), { path, ...mutation.args }),
|
|
362
|
-
disturbance === "missing" ? "PATH_NOT_FOUND" : "NOT_REGULAR_FILE",
|
|
363
|
-
(report) => {
|
|
364
|
-
assert.equal(
|
|
365
|
-
report.message,
|
|
366
|
-
disturbance === "missing" ? "Path not found." : "Path is not a regular file.",
|
|
367
|
-
);
|
|
368
|
-
assert.deepEqual(report.missing, disturbance === "missing" ? [path] : undefined);
|
|
369
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
370
|
-
assert.equal(report.cause, undefined);
|
|
371
|
-
},
|
|
372
|
-
);
|
|
373
|
-
assert.equal(targetReads, observation, "the intended target read must be disturbed");
|
|
374
|
-
assert.deepEqual(await readFile(originalPath), Buffer.from(BEFORE));
|
|
375
|
-
if (disturbance === "directory") assert.deepEqual(fs.readdirSync(path), []);
|
|
376
|
-
else await assertTarget(path, BEFORE, "delete");
|
|
377
|
-
} finally {
|
|
378
|
-
t.mock.restoreAll();
|
|
379
|
-
syncBuiltinESMExports();
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
test("file changes after the editable snapshot → rejects publication against the read bytes", async (t) => {
|
|
387
|
-
for (const mutation of mutationCases.filter(
|
|
388
|
-
({ name }) => name === "edit" || name === "replace",
|
|
389
|
-
)) {
|
|
390
|
-
await t.test(mutation.name, async (t) => {
|
|
391
|
-
const { directory, path } = await targetFile(t, BEFORE);
|
|
392
|
-
const nativeOpen = fs.promises.open;
|
|
393
|
-
let targetReads = 0;
|
|
394
|
-
t.mock.method(fs.promises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
395
|
-
if (args[0] === path && ++targetReads === 2) fs.appendFileSync(path, CONCURRENT_APPEND);
|
|
396
|
-
return nativeOpen(...args);
|
|
397
|
-
});
|
|
398
|
-
syncBuiltinESMExports();
|
|
399
|
-
try {
|
|
400
|
-
await expectToolFailure(
|
|
401
|
-
callTool(mutation.make(directory), { path, ...mutation.args }),
|
|
402
|
-
"FILE_CHANGED",
|
|
403
|
-
(report) => {
|
|
404
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
405
|
-
},
|
|
406
|
-
);
|
|
407
|
-
assert.equal(targetReads, 2);
|
|
408
|
-
await assertTarget(path, BEFORE, "append");
|
|
409
|
-
} finally {
|
|
410
|
-
t.mock.restoreAll();
|
|
411
|
-
syncBuiltinESMExports();
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
test("concurrent change during editable snapshot read → skips Fusion with NOT_PUBLISHED and retry advice", async (t) => {
|
|
418
|
-
const { directory, path } = await targetFile(t, BEFORE);
|
|
419
|
-
let commands = 0;
|
|
420
|
-
const executeTool: SessionCommand = async () => {
|
|
421
|
-
commands++;
|
|
422
|
-
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
423
|
-
};
|
|
424
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
425
|
-
const ctx = await createToolContext(directory, t);
|
|
426
|
-
await withDisturbedRead(
|
|
427
|
-
t,
|
|
428
|
-
path,
|
|
429
|
-
BEFORE,
|
|
430
|
-
"append",
|
|
431
|
-
() => true,
|
|
432
|
-
async () => {
|
|
433
|
-
await expectToolFailure(
|
|
434
|
-
callTool(
|
|
435
|
-
makeEditOverride(directory, DEFAULT_CONFIG, fusion),
|
|
436
|
-
{
|
|
437
|
-
path,
|
|
438
|
-
steps: [{ action: "append", lines: ["after"] }],
|
|
439
|
-
then_run: { command: "check" },
|
|
440
|
-
},
|
|
441
|
-
{ ctx: { ...ctx, executeTool } },
|
|
442
|
-
),
|
|
443
|
-
"FILE_CHANGED",
|
|
444
|
-
(report) => {
|
|
445
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
446
|
-
assert.equal(report.then_run?.status, "skipped");
|
|
447
|
-
assert.equal(report.next, "Retry the call.");
|
|
448
|
-
},
|
|
449
|
-
);
|
|
450
|
-
},
|
|
451
|
-
);
|
|
452
|
-
assert.equal(commands, 0);
|
|
453
|
-
await assertTarget(path, BEFORE, "append");
|
|
454
|
-
});
|