@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
|
@@ -0,0 +1,97 @@
|
|
|
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 { decodeReport } from "../pi/report.ts";
|
|
9
|
+
|
|
10
|
+
test("packed 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
|
+
edits: [{ op: "replace", anchor: anchor("grep"), body: ["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" ? decodeReport(block.text, "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
|
+
});
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/** Real file-handle races at publication and Fusion observation boundaries. */
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { test, type TestContext } from "node:test";
|
|
9
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
10
|
+
import { createActionFusionExecutor } from "../pi/action-fusion.ts";
|
|
11
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "../pi/budgets.ts";
|
|
12
|
+
import { DEFAULT_CONFIG } from "../pi/config.ts";
|
|
13
|
+
import { makeEditOverride } from "../pi/edit-tool.ts";
|
|
14
|
+
import { commitFile, FileMutationError } from "../pi/file-commit.ts";
|
|
15
|
+
import { byteRevision } from "../pi/file-revision.ts";
|
|
16
|
+
import { describeFailure, reportOf } from "../pi/report.ts";
|
|
17
|
+
import { expectToolFailure } from "../pi/report-failure.testing.ts";
|
|
18
|
+
import { REREAD } from "../pi/report-schema.ts";
|
|
19
|
+
import { makeReplaceTool } from "../pi/replace-tool.ts";
|
|
20
|
+
import { callTool, createToolContext } from "../pi/tool-call.testing.ts";
|
|
21
|
+
import { makeWriteOverride } from "../pi/write-tool.ts";
|
|
22
|
+
|
|
23
|
+
const BEFORE = "before\n";
|
|
24
|
+
const PUBLISHED = "before\nafter\n";
|
|
25
|
+
const CONCURRENT_APPEND = "other writer\n";
|
|
26
|
+
const READ_FAILURE = Object.assign(new Error("observation I/O failure"), { code: "EIO" });
|
|
27
|
+
type Disturbance = "append" | "delete" | "I/O failure";
|
|
28
|
+
|
|
29
|
+
async function targetFile(t: TestContext, content?: string) {
|
|
30
|
+
const directory = await mkdtemp(join(tmpdir(), "hashline-publication-read-"));
|
|
31
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
32
|
+
const path = join(directory, "target.txt");
|
|
33
|
+
if (content !== undefined) await writeFile(path, content);
|
|
34
|
+
return { directory, path };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Disturb the real file after its bytes are read, before withFileRead accepts the observation. */
|
|
38
|
+
async function withDisturbedRead<T>(
|
|
39
|
+
t: TestContext,
|
|
40
|
+
path: string,
|
|
41
|
+
expected: string,
|
|
42
|
+
disturbance: Disturbance,
|
|
43
|
+
ready: (bytes: Buffer) => boolean,
|
|
44
|
+
run: () => Promise<T>,
|
|
45
|
+
): Promise<T> {
|
|
46
|
+
const nativeOpen = fs.promises.open;
|
|
47
|
+
let disturbed = false;
|
|
48
|
+
t.mock.method(fs.promises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
49
|
+
const handle = await nativeOpen(...args);
|
|
50
|
+
if (args[0] === path) {
|
|
51
|
+
const nativeRead = handle.readFile;
|
|
52
|
+
t.mock.method(handle, "readFile", async (...readArgs: Parameters<typeof nativeRead>) => {
|
|
53
|
+
const bytes = await nativeRead.apply(handle, readArgs);
|
|
54
|
+
assert.ok(Buffer.isBuffer(bytes));
|
|
55
|
+
if (!disturbed && ready(bytes)) {
|
|
56
|
+
disturbed = true;
|
|
57
|
+
assert.deepEqual(bytes, Buffer.from(expected));
|
|
58
|
+
if (disturbance === "I/O failure") throw READ_FAILURE;
|
|
59
|
+
if (disturbance === "delete") fs.unlinkSync(path);
|
|
60
|
+
else fs.appendFileSync(path, CONCURRENT_APPEND);
|
|
61
|
+
}
|
|
62
|
+
return bytes;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return handle;
|
|
66
|
+
});
|
|
67
|
+
syncBuiltinESMExports();
|
|
68
|
+
try {
|
|
69
|
+
return await run();
|
|
70
|
+
} finally {
|
|
71
|
+
t.mock.restoreAll();
|
|
72
|
+
syncBuiltinESMExports();
|
|
73
|
+
assert.equal(disturbed, true, "the intended observation must be disturbed");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function assertTarget(path: string, original: string, disturbance: Disturbance) {
|
|
78
|
+
if (disturbance === "delete") await assert.rejects(readFile(path), { code: "ENOENT" });
|
|
79
|
+
else {
|
|
80
|
+
const expected = original + (disturbance === "append" ? CONCURRENT_APPEND : "");
|
|
81
|
+
assert.deepEqual(await readFile(path), Buffer.from(expected));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function text(result: AgentToolResult<unknown>): string {
|
|
86
|
+
return result.content.map((block) => (block.type === "text" ? block.text : "")).join("\n");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const mutationCases = [
|
|
90
|
+
{
|
|
91
|
+
name: "edit",
|
|
92
|
+
make: (cwd: string, fusion?: ReturnType<typeof createActionFusionExecutor>) =>
|
|
93
|
+
makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
|
|
94
|
+
args: { edits: [{ op: "append", body: ["after"] }] },
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "replace",
|
|
98
|
+
make: (cwd: string, fusion?: ReturnType<typeof createActionFusionExecutor>) =>
|
|
99
|
+
makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
|
|
100
|
+
args: { replacements: [{ literal: true, find: BEFORE, replace: PUBLISHED }] },
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "write overwrite",
|
|
104
|
+
make: makeWriteOverride,
|
|
105
|
+
args: { content: PUBLISHED, mode: "overwrite" },
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: "write create",
|
|
109
|
+
make: makeWriteOverride,
|
|
110
|
+
args: { content: PUBLISHED, mode: "create" },
|
|
111
|
+
},
|
|
112
|
+
] as const;
|
|
113
|
+
|
|
114
|
+
for (const fused of [false, true]) {
|
|
115
|
+
test(`concurrent change during post-publication read without then_run (${fused ? "Fusion enabled" : "plain"}) → succeeds and withholds stale anchors`, async (t) => {
|
|
116
|
+
for (const mutation of mutationCases) {
|
|
117
|
+
for (const disturbance of ["append", "delete"] as const) {
|
|
118
|
+
await t.test(
|
|
119
|
+
`${mutation.name}: ${disturbance} → retains publication and reports changed freshness`,
|
|
120
|
+
async (t) => {
|
|
121
|
+
const { directory, path } = await targetFile(
|
|
122
|
+
t,
|
|
123
|
+
mutation.name === "write create" ? undefined : BEFORE,
|
|
124
|
+
);
|
|
125
|
+
const fusion = fused ? createActionFusionExecutor() : undefined;
|
|
126
|
+
const result = await withDisturbedRead(
|
|
127
|
+
t,
|
|
128
|
+
path,
|
|
129
|
+
PUBLISHED,
|
|
130
|
+
disturbance,
|
|
131
|
+
(bytes) => bytes.equals(Buffer.from(PUBLISHED)),
|
|
132
|
+
() => callTool(mutation.make(directory, fusion), { path, ...mutation.args }),
|
|
133
|
+
);
|
|
134
|
+
assert.equal(result.isError, undefined);
|
|
135
|
+
const report = reportOf(
|
|
136
|
+
result,
|
|
137
|
+
mutation.name === "edit" ? "edit" : mutation.name === "replace" ? "replace" : "write",
|
|
138
|
+
);
|
|
139
|
+
assert.equal(report?.publication, "PUBLISHED");
|
|
140
|
+
assert.equal(report?.next, REREAD);
|
|
141
|
+
assert.doesNotMatch(text(result), /Updated anchors|retry the tool/);
|
|
142
|
+
assert.equal(report?.then_run, undefined);
|
|
143
|
+
assert.equal(report?.freshness, "changed");
|
|
144
|
+
await assertTarget(path, PUBLISHED, disturbance);
|
|
145
|
+
},
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
for (const observation of [1, 2]) {
|
|
153
|
+
test(`disturbance during pre-command observation ${observation} → skips then_run without retry advice or rollback`, async (t) => {
|
|
154
|
+
for (const disturbance of ["append", "delete", "I/O failure"] as const) {
|
|
155
|
+
await t.test(`${disturbance} → skips the command and preserves publication`, async (t) => {
|
|
156
|
+
const { directory, path } = await targetFile(t, BEFORE);
|
|
157
|
+
let mutationCompleted = false;
|
|
158
|
+
let observations = 0;
|
|
159
|
+
let commands = 0;
|
|
160
|
+
const fusion = createActionFusionExecutor(
|
|
161
|
+
async () => {
|
|
162
|
+
commands++;
|
|
163
|
+
return {
|
|
164
|
+
status: "succeeded",
|
|
165
|
+
result: { content: [{ type: "text", text: "checked" }] },
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
(progress) => {
|
|
169
|
+
if (progress.mutationCompleted) mutationCompleted = true;
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
const ctx = await createToolContext(directory, t);
|
|
173
|
+
const result = await withDisturbedRead(
|
|
174
|
+
t,
|
|
175
|
+
path,
|
|
176
|
+
PUBLISHED,
|
|
177
|
+
disturbance,
|
|
178
|
+
() => mutationCompleted && ++observations === observation,
|
|
179
|
+
() =>
|
|
180
|
+
callTool(
|
|
181
|
+
makeWriteOverride(directory, fusion),
|
|
182
|
+
{
|
|
183
|
+
path,
|
|
184
|
+
content: PUBLISHED,
|
|
185
|
+
mode: "overwrite",
|
|
186
|
+
then_run: { command: "check" },
|
|
187
|
+
},
|
|
188
|
+
{ ctx },
|
|
189
|
+
),
|
|
190
|
+
);
|
|
191
|
+
assert.equal(commands, 0);
|
|
192
|
+
const report = reportOf(result, "write");
|
|
193
|
+
assert.equal(report?.publication, "PUBLISHED");
|
|
194
|
+
assert.equal(report?.then_run?.status, "skipped");
|
|
195
|
+
assert.equal(report?.freshness, disturbance === "I/O failure" ? "unknown" : "changed");
|
|
196
|
+
if (disturbance === "I/O failure") {
|
|
197
|
+
assert.equal(report?.freshnessError?.message, READ_FAILURE.message);
|
|
198
|
+
assert.equal(report?.freshnessError?.code, "EIO");
|
|
199
|
+
}
|
|
200
|
+
assert.equal(report?.next, REREAD);
|
|
201
|
+
assert.doesNotMatch(text(result), /retry the tool/);
|
|
202
|
+
await assertTarget(path, PUBLISHED, disturbance);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
test("disturbance during post-command freshness read → retains publication and reports stale anchors", async (t) => {
|
|
209
|
+
for (const disturbance of ["append", "delete", "I/O failure"] as const) {
|
|
210
|
+
await t.test(
|
|
211
|
+
`${disturbance} → freshness is ${disturbance === "I/O failure" ? "unknown" : "changed"} without retry advice`,
|
|
212
|
+
async (t) => {
|
|
213
|
+
const { directory, path } = await targetFile(t, BEFORE);
|
|
214
|
+
let commands = 0;
|
|
215
|
+
const fusion = createActionFusionExecutor(async () => {
|
|
216
|
+
commands++;
|
|
217
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
|
|
218
|
+
});
|
|
219
|
+
const ctx = await createToolContext(directory, t);
|
|
220
|
+
const result = await withDisturbedRead(
|
|
221
|
+
t,
|
|
222
|
+
path,
|
|
223
|
+
PUBLISHED,
|
|
224
|
+
disturbance,
|
|
225
|
+
() => commands > 0,
|
|
226
|
+
() =>
|
|
227
|
+
callTool(
|
|
228
|
+
makeWriteOverride(directory, fusion),
|
|
229
|
+
{
|
|
230
|
+
path,
|
|
231
|
+
content: PUBLISHED,
|
|
232
|
+
mode: "overwrite",
|
|
233
|
+
then_run: { command: "check" },
|
|
234
|
+
},
|
|
235
|
+
{ ctx },
|
|
236
|
+
),
|
|
237
|
+
);
|
|
238
|
+
assert.equal(commands, 1);
|
|
239
|
+
assert.equal(reportOf(result, "write")?.publication, "PUBLISHED");
|
|
240
|
+
assert.equal(reportOf(result, "write")?.then_run?.status, "succeeded");
|
|
241
|
+
assert.equal(
|
|
242
|
+
reportOf(result, "write")?.freshness,
|
|
243
|
+
disturbance === "I/O failure" ? "unknown" : "changed",
|
|
244
|
+
);
|
|
245
|
+
assert.equal(reportOf(result, "write")?.next, REREAD);
|
|
246
|
+
assert.doesNotMatch(text(result), /Updated anchors|retry the tool/);
|
|
247
|
+
await assertTarget(path, PUBLISHED, disturbance);
|
|
248
|
+
},
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("other I/O failure during post-publication read → retains the published post-process error", async (t) => {
|
|
254
|
+
const { path } = await targetFile(t, BEFORE);
|
|
255
|
+
await withDisturbedRead(
|
|
256
|
+
t,
|
|
257
|
+
path,
|
|
258
|
+
PUBLISHED,
|
|
259
|
+
"I/O failure",
|
|
260
|
+
(bytes) => bytes.equals(Buffer.from(PUBLISHED)),
|
|
261
|
+
async () => {
|
|
262
|
+
await assert.rejects(commitFile(path, PUBLISHED, { mode: "overwrite" }), (error: unknown) => {
|
|
263
|
+
assert.ok(error instanceof FileMutationError);
|
|
264
|
+
assert.equal(error.stage, "post_process");
|
|
265
|
+
assert.equal(error.publication, "PUBLISHED");
|
|
266
|
+
assert.equal(error.cause, READ_FAILURE);
|
|
267
|
+
return true;
|
|
268
|
+
});
|
|
269
|
+
},
|
|
270
|
+
);
|
|
271
|
+
await assertTarget(path, PUBLISHED, "I/O failure");
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("concurrent change during pre-publication revision read → rejects without publishing and keeps retry advice", async (t) => {
|
|
275
|
+
for (const phase of ["inspect target", "revision recheck", "no-op check"] as const) {
|
|
276
|
+
await t.test(`${phase} → NOT_PUBLISHED`, async (t) => {
|
|
277
|
+
const { path } = await targetFile(t, BEFORE);
|
|
278
|
+
const revision = byteRevision(Buffer.from(BEFORE));
|
|
279
|
+
await withDisturbedRead(
|
|
280
|
+
t,
|
|
281
|
+
path,
|
|
282
|
+
BEFORE,
|
|
283
|
+
"append",
|
|
284
|
+
() => true,
|
|
285
|
+
async () => {
|
|
286
|
+
await assert.rejects(
|
|
287
|
+
commitFile(path, phase === "no-op check" ? BEFORE : PUBLISHED, {
|
|
288
|
+
mode: "overwrite",
|
|
289
|
+
...(phase !== "inspect target" ? { baseRevision: revision } : {}),
|
|
290
|
+
}),
|
|
291
|
+
(error: unknown) => {
|
|
292
|
+
assert.ok(error instanceof FileMutationError);
|
|
293
|
+
assert.equal(error.publication, "NOT_PUBLISHED");
|
|
294
|
+
assert.equal(error.errorCode, "FILE_CHANGED");
|
|
295
|
+
assert.equal(
|
|
296
|
+
describeFailure("write", error, { budget: DEFAULT_OUTPUT_BUDGETS }).next,
|
|
297
|
+
"Retry the call.",
|
|
298
|
+
);
|
|
299
|
+
return true;
|
|
300
|
+
},
|
|
301
|
+
);
|
|
302
|
+
},
|
|
303
|
+
);
|
|
304
|
+
await assertTarget(path, BEFORE, "append");
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("file changes after the editable snapshot → rejects publication against the read bytes", async (t) => {
|
|
310
|
+
for (const mutation of mutationCases.filter(
|
|
311
|
+
({ name }) => name === "edit" || name === "replace",
|
|
312
|
+
)) {
|
|
313
|
+
await t.test(mutation.name, async (t) => {
|
|
314
|
+
const { directory, path } = await targetFile(t, BEFORE);
|
|
315
|
+
const nativeOpen = fs.promises.open;
|
|
316
|
+
let targetReads = 0;
|
|
317
|
+
t.mock.method(fs.promises, "open", async (...args: Parameters<typeof nativeOpen>) => {
|
|
318
|
+
if (args[0] === path && ++targetReads === 2) fs.appendFileSync(path, CONCURRENT_APPEND);
|
|
319
|
+
return nativeOpen(...args);
|
|
320
|
+
});
|
|
321
|
+
syncBuiltinESMExports();
|
|
322
|
+
try {
|
|
323
|
+
await expectToolFailure(
|
|
324
|
+
callTool(mutation.make(directory), { path, ...mutation.args }),
|
|
325
|
+
"FILE_CHANGED",
|
|
326
|
+
(report) => {
|
|
327
|
+
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
328
|
+
},
|
|
329
|
+
);
|
|
330
|
+
assert.equal(targetReads, 2);
|
|
331
|
+
await assertTarget(path, BEFORE, "append");
|
|
332
|
+
} finally {
|
|
333
|
+
t.mock.restoreAll();
|
|
334
|
+
syncBuiltinESMExports();
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test("concurrent change during editable snapshot read → skips Fusion with NOT_PUBLISHED and retry advice", async (t) => {
|
|
341
|
+
const { directory, path } = await targetFile(t, BEFORE);
|
|
342
|
+
let commands = 0;
|
|
343
|
+
const fusion = createActionFusionExecutor(async () => {
|
|
344
|
+
commands++;
|
|
345
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
|
|
346
|
+
});
|
|
347
|
+
const ctx = await createToolContext(directory, t);
|
|
348
|
+
await withDisturbedRead(
|
|
349
|
+
t,
|
|
350
|
+
path,
|
|
351
|
+
BEFORE,
|
|
352
|
+
"append",
|
|
353
|
+
() => true,
|
|
354
|
+
async () => {
|
|
355
|
+
await expectToolFailure(
|
|
356
|
+
callTool(
|
|
357
|
+
makeEditOverride(directory, DEFAULT_CONFIG, fusion),
|
|
358
|
+
{
|
|
359
|
+
path,
|
|
360
|
+
edits: [{ op: "append", body: ["after"] }],
|
|
361
|
+
then_run: { command: "check" },
|
|
362
|
+
},
|
|
363
|
+
{ ctx },
|
|
364
|
+
),
|
|
365
|
+
"FILE_CHANGED",
|
|
366
|
+
(report) => {
|
|
367
|
+
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
368
|
+
assert.equal(report.then_run?.status, "skipped");
|
|
369
|
+
assert.equal(report.next, "Retry the call.");
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
},
|
|
373
|
+
);
|
|
374
|
+
assert.equal(commands, 0);
|
|
375
|
+
await assertTarget(path, BEFORE, "append");
|
|
376
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { isUtf8 } from "node:buffer";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { test } from "node:test";
|
|
9
|
+
import { DEFAULT_CONFIG } from "../pi/config.ts";
|
|
10
|
+
import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
|
|
11
|
+
import { makeReadOverride } from "../pi/read-tool.ts";
|
|
12
|
+
import { callTool } from "../pi/tool-call.testing.ts";
|
|
13
|
+
import { withFileRead } from "../pi/file-read.ts";
|
|
14
|
+
import { expectToolFailure } from "../pi/report-failure.testing.ts";
|
|
15
|
+
|
|
16
|
+
const CHUNK_BYTES = 64 * 1024;
|
|
17
|
+
|
|
18
|
+
test("read or grep scan truncated across a UTF-8 sequence → reports file change without encoding blame", async (t) => {
|
|
19
|
+
const directory = await mkdtemp(join(tmpdir(), "hashline-read-change-"));
|
|
20
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
21
|
+
const path = join(directory, "race.txt");
|
|
22
|
+
const source = Buffer.from(
|
|
23
|
+
"head\r\n" +
|
|
24
|
+
"x".repeat(CHUNK_BYTES - 7) +
|
|
25
|
+
"中" +
|
|
26
|
+
"x".repeat(CHUNK_BYTES - 3) +
|
|
27
|
+
"中" +
|
|
28
|
+
"tail".repeat(CHUNK_BYTES),
|
|
29
|
+
);
|
|
30
|
+
assert.equal(isUtf8(source), true);
|
|
31
|
+
const tools = [
|
|
32
|
+
makeReadOverride(directory, DEFAULT_CONFIG),
|
|
33
|
+
makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}),
|
|
34
|
+
];
|
|
35
|
+
for (const tool of tools) {
|
|
36
|
+
await t.test(tool.name, async () => {
|
|
37
|
+
await writeFile(path, source);
|
|
38
|
+
const original = fs.createReadStream;
|
|
39
|
+
let scans = 0;
|
|
40
|
+
fs.createReadStream = (...args: Parameters<typeof original>) => {
|
|
41
|
+
const stream = original(...args);
|
|
42
|
+
if (args[0] === path && ++scans === (tool.name === "read" ? 1 : 2)) {
|
|
43
|
+
stream.once("data", () => fs.truncateSync(path, CHUNK_BYTES - 1));
|
|
44
|
+
}
|
|
45
|
+
return stream;
|
|
46
|
+
};
|
|
47
|
+
syncBuiltinESMExports();
|
|
48
|
+
try {
|
|
49
|
+
const args =
|
|
50
|
+
tool.name === "read" ? { path, limit: 1 } : { path, pattern: "head", literal: true };
|
|
51
|
+
await expectToolFailure(
|
|
52
|
+
callTool(tool, args),
|
|
53
|
+
tool.name === "read" ? "FILE_CHANGED" : "SEARCH_INCOMPLETE",
|
|
54
|
+
(report) => {
|
|
55
|
+
if (tool.name === "grep") {
|
|
56
|
+
assert.match(report.diagnostics ?? "", /race\.txt/);
|
|
57
|
+
assert.match(report.diagnostics ?? "", /File changed during read\./);
|
|
58
|
+
} else assert.match(JSON.stringify(report), /File changed/);
|
|
59
|
+
assert.doesNotMatch(JSON.stringify(report), /UNSUPPORTED_ENCODING|Invalid UTF-8/);
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
const current = await readFile(path);
|
|
63
|
+
assert.equal(isUtf8(current), true);
|
|
64
|
+
assert.deepEqual(current, source.subarray(0, CHUNK_BYTES - 1));
|
|
65
|
+
} finally {
|
|
66
|
+
fs.createReadStream = original;
|
|
67
|
+
syncBuiltinESMExports();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
for (const operation of ["delete", "replace", "atomic replace"] as const) {
|
|
74
|
+
test(`path ${operation} during a read → rejects the opened file's obsolete observation`, {
|
|
75
|
+
skip:
|
|
76
|
+
operation === "atomic replace" && process.platform === "win32"
|
|
77
|
+
? "Windows rename rejects a concurrently open target"
|
|
78
|
+
: false,
|
|
79
|
+
}, async (t) => {
|
|
80
|
+
const directory = await mkdtemp(join(tmpdir(), "hashline-read-path-"));
|
|
81
|
+
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
82
|
+
const path = join(directory, "source.txt");
|
|
83
|
+
const replacement = join(directory, "replacement.txt");
|
|
84
|
+
await writeFile(path, "中文\n");
|
|
85
|
+
await writeFile(replacement, "汉字\n");
|
|
86
|
+
await assert.rejects(
|
|
87
|
+
withFileRead(path, undefined, async (handle) => {
|
|
88
|
+
const bytes = await handle.readFile();
|
|
89
|
+
if (operation !== "atomic replace") await rm(path);
|
|
90
|
+
if (operation !== "delete") await rename(replacement, path);
|
|
91
|
+
return bytes;
|
|
92
|
+
}),
|
|
93
|
+
/File changed/,
|
|
94
|
+
);
|
|
95
|
+
if (operation === "delete") await assert.rejects(readFile(path), { code: "ENOENT" });
|
|
96
|
+
else assert.equal(await readFile(path, "utf8"), "汉字\n");
|
|
97
|
+
});
|
|
98
|
+
}
|
|
@@ -1,41 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { cp, mkdir, mkdtemp, rm } from "node:fs/promises";
|
|
3
|
-
import { join } from "node:path";
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
4
2
|
import { test } from "node:test";
|
|
5
|
-
import {
|
|
3
|
+
import { installedExtension } from "../testing/installation.testing.ts";
|
|
4
|
+
import { runTestProcess } from "../testing/process.testing.ts";
|
|
6
5
|
|
|
7
|
-
//
|
|
8
|
-
test("regex
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"core/ranges.ts",
|
|
22
|
-
"core/types.ts",
|
|
23
|
-
]) {
|
|
24
|
-
await cp(new URL(`../${path}`, import.meta.url), join(src, path));
|
|
25
|
-
}
|
|
26
|
-
const worker = new Worker(join(src, "pi", "replace-worker.mjs"), {
|
|
27
|
-
workerData: {
|
|
28
|
-
source: "before\n",
|
|
29
|
-
rules: [{ find: "(before)", replace: "$1 after", regex: true }],
|
|
30
|
-
},
|
|
6
|
+
// Workers have their own loader; an installed worker must resolve only production dependencies.
|
|
7
|
+
test("regex workers start from installed source and npm archive paths", async (t) => {
|
|
8
|
+
for (const source of ["source", "packed"] as const) {
|
|
9
|
+
await t.test(source, async (t) => {
|
|
10
|
+
const installation = await installedExtension(t, source);
|
|
11
|
+
await runTestProcess(
|
|
12
|
+
process.execPath,
|
|
13
|
+
[
|
|
14
|
+
fileURLToPath(new URL("./replace-package.testing.ts", import.meta.url)),
|
|
15
|
+
installation.packageDir,
|
|
16
|
+
],
|
|
17
|
+
installation.root,
|
|
18
|
+
installation.agentDir,
|
|
19
|
+
);
|
|
31
20
|
});
|
|
32
|
-
const result = await new Promise<unknown>((resolve, reject) => {
|
|
33
|
-
worker.once("message", resolve);
|
|
34
|
-
worker.once("error", reject);
|
|
35
|
-
worker.once("exit", (code) => reject(new Error(`Worker exited before responding: ${code}`)));
|
|
36
|
-
});
|
|
37
|
-
assert.deepEqual(result, { result: { text: "before after\n", count: 1 } });
|
|
38
|
-
} finally {
|
|
39
|
-
await rm(root, { recursive: true, force: true });
|
|
40
21
|
}
|
|
41
22
|
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { Worker } from "node:worker_threads";
|
|
4
|
+
|
|
5
|
+
for (const { entry, input, expected } of [
|
|
6
|
+
{
|
|
7
|
+
entry: "replace-worker.mjs",
|
|
8
|
+
input: {
|
|
9
|
+
source: "before\n",
|
|
10
|
+
rules: [{ find: "(before)", replace: "$1 after", literal: false }],
|
|
11
|
+
},
|
|
12
|
+
expected: { text: "before after\n", count: 1 },
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
entry: "grep-worker.mjs",
|
|
16
|
+
input: {
|
|
17
|
+
source: "😀\r\na\n",
|
|
18
|
+
query: { patterns: ["^..$"], literal: false, multiline: false, flags: "m" },
|
|
19
|
+
limit: 10,
|
|
20
|
+
},
|
|
21
|
+
expected: { lines: [{ lineNumber: 1, column: 0 }], totalLines: 2 },
|
|
22
|
+
},
|
|
23
|
+
]) {
|
|
24
|
+
const worker = new Worker(join(process.argv[2], "src", "pi", entry));
|
|
25
|
+
try {
|
|
26
|
+
const result = await new Promise<unknown>((resolve, reject) => {
|
|
27
|
+
worker.once("message", resolve);
|
|
28
|
+
worker.once("error", reject);
|
|
29
|
+
worker.once("exit", (code) => reject(new Error(`Worker exited before responding: ${code}`)));
|
|
30
|
+
worker.postMessage(input);
|
|
31
|
+
});
|
|
32
|
+
assert.deepEqual(result, { result: expected });
|
|
33
|
+
} finally {
|
|
34
|
+
await worker.terminate();
|
|
35
|
+
}
|
|
36
|
+
}
|