@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
|
@@ -8,6 +8,9 @@ import registerHashline from "../index.ts";
|
|
|
8
8
|
import { computeLineHash } from "../core/hash.ts";
|
|
9
9
|
import { callTool } from "../pi/tool-call.testing.ts";
|
|
10
10
|
import { DEFAULT_CONFIG, type HashlineEditConfig } from "../pi/config.ts";
|
|
11
|
+
import { reportOf } from "../pi/report.ts";
|
|
12
|
+
import { formatByteSize } from "../pi/budgets.ts";
|
|
13
|
+
import { expectToolFailure } from "../pi/report-failure.testing.ts";
|
|
11
14
|
|
|
12
15
|
test("registered tools retain their configuration when another tool set is registered", async (t) => {
|
|
13
16
|
const directory = await mkdtemp(join(tmpdir(), "hashline-config-registration-"));
|
|
@@ -38,8 +41,8 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
38
41
|
hashLen: 6,
|
|
39
42
|
shiftRadius: 1,
|
|
40
43
|
read: { defaultLimit: 1, maxKiB: 1 },
|
|
41
|
-
grep: { defaultLimit: 1, defaultContext: 1 },
|
|
42
|
-
replace: { regexTimeoutMs:
|
|
44
|
+
grep: { ...DEFAULT_CONFIG.grep, defaultLimit: 1, defaultContext: 1 },
|
|
45
|
+
replace: { regexTimeoutMs: 3_000 },
|
|
43
46
|
})),
|
|
44
47
|
expectedGrepLines: [1, 2, 3],
|
|
45
48
|
},
|
|
@@ -50,8 +53,8 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
50
53
|
hashLen: 8,
|
|
51
54
|
shiftRadius: 0,
|
|
52
55
|
read: { defaultLimit: 2, maxKiB: 2 },
|
|
53
|
-
grep: { defaultLimit: 2, defaultContext: 0 },
|
|
54
|
-
replace: { regexTimeoutMs:
|
|
56
|
+
grep: { ...DEFAULT_CONFIG.grep, defaultLimit: 2, defaultContext: 0 },
|
|
57
|
+
replace: { regexTimeoutMs: 3_500 },
|
|
55
58
|
})),
|
|
56
59
|
expectedGrepLines: [2, 6],
|
|
57
60
|
},
|
|
@@ -59,7 +62,7 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
59
62
|
|
|
60
63
|
for (const { tools, config, expectedGrepLines } of registrations) {
|
|
61
64
|
const { hashLen, shiftRadius, read, grep, replace } = config;
|
|
62
|
-
for (const name of ["read", "grep", "
|
|
65
|
+
for (const name of ["read", "grep", "edit"] as const) {
|
|
63
66
|
await t.test(`${name} retains the registered hash length ${hashLen}`, async () => {
|
|
64
67
|
const tool = tools.get(name);
|
|
65
68
|
assert.ok(tool);
|
|
@@ -68,7 +71,6 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
68
71
|
const params = {
|
|
69
72
|
read: { path },
|
|
70
73
|
grep: { path, pattern: "before", literal: true },
|
|
71
|
-
replace: { path, replacements: [{ find: "before", replace: "after" }] },
|
|
72
74
|
edit: {
|
|
73
75
|
path,
|
|
74
76
|
edits: [
|
|
@@ -81,9 +83,14 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
81
83
|
},
|
|
82
84
|
}[name];
|
|
83
85
|
const result = await callTool(tool, params);
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
86
|
+
assert.equal(result.content[0].type, "text");
|
|
87
|
+
const expected = name === "edit" ? "after" : "before";
|
|
88
|
+
if (name === "edit") {
|
|
89
|
+
assert.equal(result.isError, undefined);
|
|
90
|
+
} else {
|
|
91
|
+
const anchor = `1#${computeLineHash(1, expected, hashLen)}`;
|
|
92
|
+
assert.match(result.content[0].text, new RegExp(`^${anchor}(?:│|$)`, "m"));
|
|
93
|
+
}
|
|
87
94
|
assert.equal(await readFile(join(directory, path), "utf8"), `${expected}\n`);
|
|
88
95
|
});
|
|
89
96
|
}
|
|
@@ -94,14 +101,16 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
94
101
|
const path = `${hashLen}-recovery.txt`;
|
|
95
102
|
const text = "changed\ntarget\n";
|
|
96
103
|
await writeFile(join(directory, path), text);
|
|
97
|
-
await
|
|
104
|
+
await expectToolFailure(
|
|
98
105
|
callTool(tool, {
|
|
99
106
|
path,
|
|
100
107
|
edits: [{ op: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
|
|
101
108
|
}),
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
109
|
+
"ANCHOR_MISMATCH",
|
|
110
|
+
(report) => {
|
|
111
|
+
assert.equal(report.failures?.[0].result, shiftRadius === 0 ? "unresolved" : "shifted");
|
|
112
|
+
if (shiftRadius !== 0) assert.match(report.failures?.[0].candidate ?? "", /^2#/);
|
|
113
|
+
},
|
|
105
114
|
);
|
|
106
115
|
assert.equal(await readFile(join(directory, path), "utf8"), text);
|
|
107
116
|
});
|
|
@@ -115,6 +124,7 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
115
124
|
const lines = ["first", "second", "third"];
|
|
116
125
|
await writeFile(join(directory, path), `${lines.join("\n")}\n`);
|
|
117
126
|
const result = await callTool(tool, { path });
|
|
127
|
+
assert.equal(result.content[0].type, "text");
|
|
118
128
|
assert.deepEqual(
|
|
119
129
|
result.content[0].text.split("\n").filter((line: string) => /^\d+#/.test(line)),
|
|
120
130
|
lines
|
|
@@ -124,20 +134,25 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
124
134
|
`${index + 1}#${computeLineHash(index + 1, content, hashLen)}│${content}`,
|
|
125
135
|
),
|
|
126
136
|
);
|
|
127
|
-
assert.equal(result
|
|
137
|
+
assert.equal(reportOf(result, "read")?.pagination?.nextOffset, read.defaultLimit + 1);
|
|
128
138
|
|
|
129
139
|
const wide = "界".repeat(500);
|
|
130
140
|
await writeFile(join(directory, path), `${wide}\n`);
|
|
131
141
|
const bounded = await callTool(tool, { path });
|
|
142
|
+
assert.equal(bounded.content[0].type, "text");
|
|
132
143
|
if (read.maxKiB === 1) {
|
|
133
|
-
|
|
134
|
-
assert.
|
|
135
|
-
|
|
144
|
+
const maxBytes = read.maxKiB * 1024;
|
|
145
|
+
assert.deepEqual(reportOf(bounded, "read")?.lineTooLong, {
|
|
146
|
+
line: 1,
|
|
147
|
+
limit: formatByteSize(maxBytes),
|
|
148
|
+
});
|
|
149
|
+
assert.doesNotMatch(bounded.content[0].text, /^\d+#/m);
|
|
136
150
|
} else {
|
|
137
151
|
assert.ok(
|
|
138
152
|
bounded.content[0].text.includes(`1#${computeLineHash(1, wide, hashLen)}│${wide}`),
|
|
139
153
|
);
|
|
140
|
-
assert.equal(bounded
|
|
154
|
+
assert.equal(reportOf(bounded, "read")?.outputLimit, undefined);
|
|
155
|
+
assert.equal(reportOf(bounded, "read")?.pagination, undefined);
|
|
141
156
|
}
|
|
142
157
|
},
|
|
143
158
|
);
|
|
@@ -163,6 +178,7 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
163
178
|
];
|
|
164
179
|
await writeFile(join(directory, path), `${lines.join("\n")}\n`);
|
|
165
180
|
const result = await callTool(tool, { path, pattern: "needle", literal: true });
|
|
181
|
+
assert.equal(result.content[0].type, "text");
|
|
166
182
|
assert.deepEqual(
|
|
167
183
|
result.content[0].text.split("\n").filter((line: string) => /^\d+#/.test(line)),
|
|
168
184
|
expectedGrepLines.map(
|
|
@@ -170,10 +186,7 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
170
186
|
`${line}#${computeLineHash(line, lines[line - 1], hashLen)}│${lines[line - 1]}`,
|
|
171
187
|
),
|
|
172
188
|
);
|
|
173
|
-
assert.
|
|
174
|
-
result.content[0].text,
|
|
175
|
-
new RegExp(`${grep.defaultLimit} matches limit reached`),
|
|
176
|
-
);
|
|
189
|
+
assert.equal(reportOf(result, "grep")?.matchLimit, grep.defaultLimit);
|
|
177
190
|
},
|
|
178
191
|
);
|
|
179
192
|
|
|
@@ -185,15 +198,16 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
185
198
|
const path = `${hashLen}-replace-settings.txt`;
|
|
186
199
|
const original = `${"a".repeat(35)}!`;
|
|
187
200
|
await writeFile(join(directory, path), original);
|
|
188
|
-
await
|
|
201
|
+
await expectToolFailure(
|
|
189
202
|
callTool(tool, {
|
|
190
203
|
path,
|
|
191
|
-
replacements: [{ find: "^(a+)+$", replace: "changed",
|
|
204
|
+
replacements: [{ find: "^(a+)+$", replace: "changed", literal: false }],
|
|
192
205
|
}),
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
206
|
+
"REGEX_TIMEOUT",
|
|
207
|
+
(report) => {
|
|
208
|
+
assert.equal(report.timeoutMs, replace.regexTimeoutMs);
|
|
209
|
+
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
210
|
+
},
|
|
197
211
|
);
|
|
198
212
|
assert.equal(await readFile(join(directory, path), "utf8"), original);
|
|
199
213
|
},
|
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
-
import {
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
2
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
3
|
import { join } from "node:path";
|
|
5
4
|
import { test, type TestContext } from "node:test";
|
|
6
5
|
import { Type } from "typebox";
|
|
7
|
-
import type {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
createAgentSession,
|
|
11
|
-
DefaultResourceLoader,
|
|
12
|
-
type ExtensionAPI,
|
|
13
|
-
ModelRuntime,
|
|
14
|
-
SessionManager,
|
|
15
|
-
SettingsManager,
|
|
16
|
-
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
7
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { openTestSession, responseStream } from "../testing/session.testing.ts";
|
|
17
9
|
import {
|
|
18
10
|
createActionFusionExecutor,
|
|
19
|
-
type ActionFusionDetails,
|
|
20
11
|
type ActionFusionProgress,
|
|
21
12
|
type ThenRunInput,
|
|
22
13
|
} from "../pi/action-fusion.ts";
|
|
23
14
|
import { makeWriteOverride } from "../pi/write-tool.ts";
|
|
15
|
+
import { commandText } from "../pi/mutation-result.ts";
|
|
16
|
+
import { reportOf } from "../pi/report.ts";
|
|
17
|
+
import type { CommandStatus } from "../pi/report-schema.ts";
|
|
24
18
|
|
|
25
19
|
/** Exercise the default runner through a real parent tool call, with no provider traffic. */
|
|
26
20
|
async function openSession(
|
|
@@ -28,84 +22,26 @@ async function openSession(
|
|
|
28
22
|
configure?: (pi: ExtensionAPI) => void,
|
|
29
23
|
onProgress?: (event: ActionFusionProgress) => void,
|
|
30
24
|
) {
|
|
31
|
-
const cwd = await mkdtemp(join(tmpdir(), "hashline-fusion-session-"));
|
|
32
|
-
let dispose: (() => void) | undefined;
|
|
33
|
-
t.after(async () => {
|
|
34
|
-
dispose?.();
|
|
35
|
-
await rm(cwd, { recursive: true, force: true });
|
|
36
|
-
});
|
|
37
|
-
const modelRuntime = await ModelRuntime.create({
|
|
38
|
-
authPath: join(cwd, "auth.json"),
|
|
39
|
-
modelsPath: null,
|
|
40
|
-
modelsStorePath: join(cwd, "models.json"),
|
|
41
|
-
refreshOnCreate: false,
|
|
42
|
-
allowModelNetwork: false,
|
|
43
|
-
});
|
|
44
|
-
modelRuntime.registerProvider("fusion-test", {
|
|
45
|
-
api: "openai-completions",
|
|
46
|
-
baseUrl: "https://unused.invalid",
|
|
47
|
-
apiKey: "local-test-placeholder",
|
|
48
|
-
models: [
|
|
49
|
-
{
|
|
50
|
-
id: "test",
|
|
51
|
-
name: "Test",
|
|
52
|
-
input: ["text"],
|
|
53
|
-
reasoning: false,
|
|
54
|
-
contextWindow: 100_000,
|
|
55
|
-
maxTokens: 1000,
|
|
56
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
57
|
-
},
|
|
58
|
-
],
|
|
59
|
-
});
|
|
60
|
-
const model = modelRuntime.getModel("fusion-test", "test");
|
|
61
|
-
assert.ok(model);
|
|
62
25
|
const calls: { name: string; parent?: string }[] = [];
|
|
63
26
|
const progress: ActionFusionProgress[] = [];
|
|
64
|
-
const
|
|
65
|
-
compaction: { enabled: false },
|
|
66
|
-
retry: { enabled: false },
|
|
67
|
-
});
|
|
68
|
-
const loader = new DefaultResourceLoader({
|
|
69
|
-
cwd,
|
|
70
|
-
agentDir: cwd,
|
|
71
|
-
settingsManager,
|
|
72
|
-
noExtensions: true,
|
|
73
|
-
noSkills: true,
|
|
74
|
-
noPromptTemplates: true,
|
|
75
|
-
noThemes: true,
|
|
76
|
-
noContextFiles: true,
|
|
77
|
-
extensionFactories: [
|
|
78
|
-
(pi) => {
|
|
79
|
-
pi.on("tool_call", (event) => {
|
|
80
|
-
calls.push({ name: event.toolName, parent: event.parentToolCallId });
|
|
81
|
-
});
|
|
82
|
-
configure?.(pi);
|
|
83
|
-
pi.registerTool(
|
|
84
|
-
makeWriteOverride(
|
|
85
|
-
cwd,
|
|
86
|
-
createActionFusionExecutor(undefined, (event) => {
|
|
87
|
-
progress.push(event);
|
|
88
|
-
onProgress?.(event);
|
|
89
|
-
}),
|
|
90
|
-
),
|
|
91
|
-
);
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
});
|
|
95
|
-
await loader.reload();
|
|
96
|
-
const { session, extensionsResult } = await createAgentSession({
|
|
97
|
-
cwd,
|
|
98
|
-
agentDir: cwd,
|
|
99
|
-
modelRuntime,
|
|
100
|
-
model,
|
|
101
|
-
settingsManager,
|
|
102
|
-
resourceLoader: loader,
|
|
103
|
-
sessionManager: SessionManager.inMemory(cwd),
|
|
27
|
+
const { cwd, session, model } = await openTestSession(t, {
|
|
104
28
|
tools: ["write", "bash"],
|
|
29
|
+
configure(pi, cwd) {
|
|
30
|
+
pi.on("tool_call", (event) => {
|
|
31
|
+
calls.push({ name: event.toolName, parent: event.parentToolCallId });
|
|
32
|
+
});
|
|
33
|
+
configure?.(pi);
|
|
34
|
+
pi.registerTool(
|
|
35
|
+
makeWriteOverride(
|
|
36
|
+
cwd,
|
|
37
|
+
createActionFusionExecutor(undefined, (event) => {
|
|
38
|
+
progress.push(event);
|
|
39
|
+
onProgress?.(event);
|
|
40
|
+
}),
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
},
|
|
105
44
|
});
|
|
106
|
-
dispose = () => session.dispose();
|
|
107
|
-
assert.deepEqual(extensionsResult.errors, []);
|
|
108
|
-
await session.bindExtensions({});
|
|
109
45
|
return {
|
|
110
46
|
cwd,
|
|
111
47
|
calls,
|
|
@@ -116,8 +52,7 @@ async function openSession(
|
|
|
116
52
|
session.agent.streamFunction = () => {
|
|
117
53
|
const first = responses++ === 0;
|
|
118
54
|
assert.ok(responses <= 2, "Unexpected model request");
|
|
119
|
-
|
|
120
|
-
role: "assistant",
|
|
55
|
+
return responseStream(model, {
|
|
121
56
|
content: first
|
|
122
57
|
? [
|
|
123
58
|
{
|
|
@@ -134,23 +69,7 @@ async function openSession(
|
|
|
134
69
|
]
|
|
135
70
|
: [{ type: "text", text: "Done." }],
|
|
136
71
|
stopReason: first ? "toolUse" : "stop",
|
|
137
|
-
|
|
138
|
-
provider: model.provider,
|
|
139
|
-
model: model.id,
|
|
140
|
-
timestamp: Date.now(),
|
|
141
|
-
usage: {
|
|
142
|
-
input: 0,
|
|
143
|
-
output: 0,
|
|
144
|
-
cacheRead: 0,
|
|
145
|
-
cacheWrite: 0,
|
|
146
|
-
totalTokens: 0,
|
|
147
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
148
|
-
},
|
|
149
|
-
};
|
|
150
|
-
const stream = new AssistantMessageEventStream();
|
|
151
|
-
stream.push({ type: "start", partial: message });
|
|
152
|
-
stream.push({ type: "done", reason: first ? "toolUse" : "stop", message });
|
|
153
|
-
return stream;
|
|
72
|
+
});
|
|
154
73
|
};
|
|
155
74
|
await session.prompt("Run the requested local command after the mutation.");
|
|
156
75
|
assert.equal(session.agent.state.errorMessage, undefined);
|
|
@@ -162,7 +81,7 @@ async function openSession(
|
|
|
162
81
|
assert.equal(await readFile(join(cwd, "saved.txt"), "utf8"), "saved\n");
|
|
163
82
|
return {
|
|
164
83
|
modelRequests: responses,
|
|
165
|
-
|
|
84
|
+
report: reportOf(result, "write"),
|
|
166
85
|
text: result.content
|
|
167
86
|
.filter((block) => block.type === "text")
|
|
168
87
|
.map((block) => block.text)
|
|
@@ -176,7 +95,7 @@ const commandCases: {
|
|
|
176
95
|
name: string;
|
|
177
96
|
source: string;
|
|
178
97
|
timeout?: number;
|
|
179
|
-
command:
|
|
98
|
+
command: CommandStatus;
|
|
180
99
|
diagnostic: RegExp;
|
|
181
100
|
}[] = [
|
|
182
101
|
{
|
|
@@ -216,7 +135,9 @@ for (const { name, source, timeout, command, diagnostic } of commandCases) {
|
|
|
216
135
|
command: "node command.mjs",
|
|
217
136
|
...(timeout === undefined ? {} : { timeout }),
|
|
218
137
|
});
|
|
219
|
-
assert.
|
|
138
|
+
assert.equal(result.report?.publication, "PUBLISHED");
|
|
139
|
+
assert.equal(result.report?.then_run?.status, command);
|
|
140
|
+
assert.equal(result.report?.freshness, undefined);
|
|
220
141
|
assert.match(result.text, diagnostic);
|
|
221
142
|
assert.equal(f.progress.at(-1)?.command, command);
|
|
222
143
|
});
|
|
@@ -227,7 +148,12 @@ test("cancelling a real session Bash command → reports cancellation and keeps
|
|
|
227
148
|
}, async (t) => {
|
|
228
149
|
const started = Promise.withResolvers<void>();
|
|
229
150
|
const f = await openSession(t, undefined, (event) => {
|
|
230
|
-
if (
|
|
151
|
+
if (
|
|
152
|
+
event.command === "running" &&
|
|
153
|
+
event.result &&
|
|
154
|
+
commandText(event.result.content).includes("READY")
|
|
155
|
+
)
|
|
156
|
+
started.resolve();
|
|
231
157
|
});
|
|
232
158
|
await writeFile(
|
|
233
159
|
join(f.cwd, "command.mjs"),
|
|
@@ -237,11 +163,8 @@ test("cancelling a real session Bash command → reports cancellation and keeps
|
|
|
237
163
|
await started.promise;
|
|
238
164
|
await f.abort();
|
|
239
165
|
const result = await pending;
|
|
240
|
-
assert.
|
|
241
|
-
|
|
242
|
-
command: "cancelled",
|
|
243
|
-
freshness: "unchanged",
|
|
244
|
-
});
|
|
166
|
+
assert.equal(result.report?.publication, "PUBLISHED");
|
|
167
|
+
assert.equal(result.report?.then_run?.status, "cancelled");
|
|
245
168
|
assert.match(result.text, /Command aborted/);
|
|
246
169
|
assert.equal(f.progress.at(-1)?.command, "cancelled");
|
|
247
170
|
});
|
|
@@ -290,11 +213,8 @@ test("session Bash tool_call blocks then_run → no command side effect, mutatio
|
|
|
290
213
|
"import { writeFileSync } from 'node:fs'; writeFileSync('command-ran', 'yes');\n",
|
|
291
214
|
);
|
|
292
215
|
const result = await f.run({ command: "node command.mjs" });
|
|
293
|
-
assert.
|
|
294
|
-
|
|
295
|
-
command: "failed",
|
|
296
|
-
freshness: "unchanged",
|
|
297
|
-
});
|
|
216
|
+
assert.equal(result.report?.publication, "PUBLISHED");
|
|
217
|
+
assert.equal(result.report?.then_run?.status, "failed");
|
|
298
218
|
assert.match(result.text, /BLOCKED_BY_POLICY/);
|
|
299
219
|
assert.equal(result.modelRequests, 1, "Preserve the policy hook's request to stop the batch");
|
|
300
220
|
await assert.rejects(readFile(join(f.cwd, "command-ran")), { code: "ENOENT" });
|
|
@@ -305,21 +225,33 @@ test("session Bash override and tool_result hook → then_run uses their final r
|
|
|
305
225
|
timeout: 15_000,
|
|
306
226
|
}, async (t) => {
|
|
307
227
|
let executions = 0;
|
|
228
|
+
const partial: AgentToolResult<unknown> = {
|
|
229
|
+
content: [{ type: "text", text: "OVERRIDE_PROGRESS" }],
|
|
230
|
+
details: { fullOutputPath: "/tmp/partial-command.log" },
|
|
231
|
+
};
|
|
232
|
+
const final: AgentToolResult<unknown> = {
|
|
233
|
+
content: [
|
|
234
|
+
{ type: "text", text: "HOOK_REJECTED_OVERRIDE" },
|
|
235
|
+
{ type: "image", data: "iVBORw0KGgo=", mimeType: "image/png" },
|
|
236
|
+
],
|
|
237
|
+
details: { fullOutputPath: "/tmp/final-command.log" },
|
|
238
|
+
};
|
|
308
239
|
const f = await openSession(t, (pi) => {
|
|
309
240
|
pi.registerTool({
|
|
310
241
|
name: "bash",
|
|
311
242
|
label: "bash",
|
|
312
243
|
description: "Session Bash override",
|
|
313
244
|
parameters: Type.Object({ command: Type.String() }),
|
|
314
|
-
async execute() {
|
|
245
|
+
async execute(_id, _args, _signal, onUpdate) {
|
|
315
246
|
executions++;
|
|
247
|
+
onUpdate?.(partial);
|
|
316
248
|
return { content: [{ type: "text", text: "OVERRIDE_RESULT" }], details: {} };
|
|
317
249
|
},
|
|
318
250
|
});
|
|
319
251
|
pi.on("tool_result", (event) =>
|
|
320
252
|
event.toolName === "bash"
|
|
321
253
|
? {
|
|
322
|
-
|
|
254
|
+
...final,
|
|
323
255
|
isError: true,
|
|
324
256
|
}
|
|
325
257
|
: undefined,
|
|
@@ -331,13 +263,16 @@ test("session Bash override and tool_result hook → then_run uses their final r
|
|
|
331
263
|
);
|
|
332
264
|
const result = await f.run({ command: "node command.mjs" });
|
|
333
265
|
assert.equal(executions, 1);
|
|
334
|
-
assert.
|
|
335
|
-
publication: "PUBLISHED",
|
|
336
|
-
command: "failed",
|
|
337
|
-
freshness: "unchanged",
|
|
338
|
-
});
|
|
266
|
+
assert.equal(result.report?.then_run?.status, "failed");
|
|
339
267
|
assert.match(result.text, /HOOK_REJECTED_OVERRIDE/);
|
|
340
268
|
assert.doesNotMatch(result.text, /OVERRIDE_RESULT/);
|
|
269
|
+
assert.deepEqual(
|
|
270
|
+
f.progress.find(
|
|
271
|
+
(event) => event.result && commandText(event.result.content) === "OVERRIDE_PROGRESS",
|
|
272
|
+
)?.result,
|
|
273
|
+
partial,
|
|
274
|
+
);
|
|
275
|
+
assert.deepEqual(f.progress.at(-1)?.result, final);
|
|
341
276
|
await assert.rejects(readFile(join(f.cwd, "command-ran")), { code: "ENOENT" });
|
|
342
277
|
assert.ok(f.calls.some((call) => call.name === "bash" && call.parent === "fused"));
|
|
343
278
|
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { chmod, mkdir, mkdtemp, rm, stat, symlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { DEFAULT_CONFIG } from "../pi/config.ts";
|
|
7
|
+
import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
|
|
8
|
+
import { callTool } from "../pi/tool-call.testing.ts";
|
|
9
|
+
import { expectToolFailure } from "../pi/report-failure.testing.ts";
|
|
10
|
+
|
|
11
|
+
for (const condition of ["permission denied", "symlink loop"] as const) {
|
|
12
|
+
test(`existing search path has ${condition} → grep retains the real filesystem error`, {
|
|
13
|
+
skip:
|
|
14
|
+
process.platform === "win32" ||
|
|
15
|
+
(condition === "permission denied" && process.getuid?.() === 0)
|
|
16
|
+
? "Requires POSIX traversal permissions and symlinks; permission case requires a non-root user"
|
|
17
|
+
: false,
|
|
18
|
+
}, async (t) => {
|
|
19
|
+
const directory = await mkdtemp(join(tmpdir(), "hashline-grep-scope-"));
|
|
20
|
+
const denied = join(directory, "denied");
|
|
21
|
+
t.after(async () => {
|
|
22
|
+
if (condition === "permission denied") await chmod(denied, 0o700);
|
|
23
|
+
await rm(directory, { recursive: true, force: true });
|
|
24
|
+
});
|
|
25
|
+
let path: string;
|
|
26
|
+
let code: string;
|
|
27
|
+
if (condition === "permission denied") {
|
|
28
|
+
await mkdir(denied);
|
|
29
|
+
path = join(denied, "source.txt");
|
|
30
|
+
await writeFile(path, "needle中文\n");
|
|
31
|
+
await chmod(denied, 0);
|
|
32
|
+
code = "EACCES";
|
|
33
|
+
} else {
|
|
34
|
+
path = join(directory, "loop");
|
|
35
|
+
await symlink("loop", path);
|
|
36
|
+
code = "ELOOP";
|
|
37
|
+
}
|
|
38
|
+
await assert.rejects(stat(path), { code });
|
|
39
|
+
const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
|
|
40
|
+
await expectToolFailure(
|
|
41
|
+
callTool(tool, { path, pattern: "needle", literal: true, ignoreCase: false }),
|
|
42
|
+
"FILESYSTEM_ERROR",
|
|
43
|
+
(report) => {
|
|
44
|
+
assert.doesNotMatch(report.message ?? "", /Path not found/);
|
|
45
|
+
assert.ok(report.message?.includes(code));
|
|
46
|
+
assert.equal(report.cause, undefined);
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
}
|