@criogaid/pi-hashline-edit 0.9.0 → 0.11.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 +12 -12
- package/README.zh-CN.md +12 -12
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Worker } from "node:worker_threads";
|
|
2
|
+
import { runRegexReplacements } from "./replace-regex.ts";
|
|
1
3
|
import assert from "node:assert/strict";
|
|
2
4
|
import { test } from "node:test";
|
|
3
5
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
@@ -7,17 +9,17 @@ import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
|
7
9
|
import { applyEdits } from "../core/apply.ts";
|
|
8
10
|
import { HashlineError } from "../core/errors.ts";
|
|
9
11
|
import { computeLineHash } from "../core/hash.ts";
|
|
12
|
+
import { openTestSession } from "../testing/session.testing.ts";
|
|
10
13
|
import { createAnchorFormatter } from "./anchor-format.ts";
|
|
11
14
|
import { formatArgumentError } from "./argument-error.ts";
|
|
12
15
|
import { formatByteSize, outputBudgetsFor } from "./budgets.ts";
|
|
13
|
-
import { DEFAULT_CONFIG } from "./
|
|
14
|
-
import { makeEditOverride } from "./
|
|
16
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
17
|
+
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
15
18
|
import { describeEditFailure } from "./failure-context.ts";
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import { restoreWorkerError } from "./regex-worker-contract.ts";
|
|
19
|
+
import { grepOutput } from "./grep-output.ts";
|
|
20
|
+
import { registerModelBudgets } from "./model-budgets.ts";
|
|
21
|
+
import { describeMutationFailure } from "./mutation-result.ts";
|
|
22
|
+
import { reportOf } from "./report.ts";
|
|
21
23
|
import { workerFailure } from "./report-cause.ts";
|
|
22
24
|
import { callTool } from "./tool-call.testing.ts";
|
|
23
25
|
|
|
@@ -29,8 +31,15 @@ function wrongHash(text: string): string {
|
|
|
29
31
|
return (hash[0] === "0" ? "1" : "0") + hash.slice(1);
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
test("byte budget
|
|
34
|
+
test("byte budget sizes include fractional KiB → round to one decimal and preserve whole units", () => {
|
|
33
35
|
for (const [bytes, expected] of [
|
|
36
|
+
[outputBudgetsFor(200_000).readBytes, "195.3 KiB"],
|
|
37
|
+
[outputBudgetsFor(200_000).blockBytes, "48.8 KiB"],
|
|
38
|
+
[outputBudgetsFor(200_000).fieldBytes, "12.2 KiB"],
|
|
39
|
+
[16 * 1024, "16 KiB"],
|
|
40
|
+
[50 * 1024, "50 KiB"],
|
|
41
|
+
[256 * 1024, "256 KiB"],
|
|
42
|
+
[64 * 1024 * 1024, "64 MiB"],
|
|
34
43
|
[1024, "1 KiB"],
|
|
35
44
|
[1023 * 1024, "1023 KiB"],
|
|
36
45
|
[1024 * 1024, "1 MiB"],
|
|
@@ -43,14 +52,14 @@ test("byte budget size → uses MiB for whole MiB and KiB otherwise", () => {
|
|
|
43
52
|
|
|
44
53
|
test("model budget grows → argument issues remain complete JSON with counted omissions", () => {
|
|
45
54
|
const issues = Array.from({ length: 100 }, (_, index) => ({
|
|
46
|
-
field: `
|
|
55
|
+
field: `steps[${index}].lines`,
|
|
47
56
|
reason: "中文🙂".repeat(100),
|
|
48
57
|
fix: "Supply a line.",
|
|
49
58
|
}));
|
|
50
59
|
const reports = [outputBudgetsFor(1), small, large].map((budget) => {
|
|
51
60
|
const text = formatArgumentError(issues, false, budget);
|
|
52
61
|
assert.ok(Buffer.byteLength(text) <= budget.blockBytes);
|
|
53
|
-
const report =
|
|
62
|
+
const report = reportOf({ content: [{ type: "text", text }] }, "edit");
|
|
54
63
|
assert.ok(report);
|
|
55
64
|
assert.equal((report.issues?.length ?? 0) + (report.omittedIssues ?? 0), issues.length);
|
|
56
65
|
return report;
|
|
@@ -70,40 +79,52 @@ test("model row allowance grows → anchor failures return complete observations
|
|
|
70
79
|
if (applied.ok) return;
|
|
71
80
|
const snapshot = { currentText: text, anchors: createAnchorFormatter(DEFAULT_CONFIG.hashLen) };
|
|
72
81
|
const reports = [small, large].map((budget) => {
|
|
73
|
-
const report =
|
|
82
|
+
const report = describeMutationFailure(
|
|
74
83
|
"edit",
|
|
75
|
-
describeEditFailure(applied.
|
|
76
|
-
{
|
|
84
|
+
describeEditFailure(snapshot.currentText, applied.failures, snapshot.anchors, budget),
|
|
85
|
+
{
|
|
86
|
+
budget,
|
|
87
|
+
},
|
|
77
88
|
);
|
|
78
89
|
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
79
90
|
return report;
|
|
80
91
|
});
|
|
81
|
-
assert.ok(reports[0].failures?.[0].
|
|
92
|
+
assert.ok(reports[0].failures?.[0].notShown);
|
|
82
93
|
assert.equal(reports[0].failures?.[0].observed, undefined);
|
|
83
94
|
assert.equal(reports[1].failures?.[0].observed, snapshot.anchors.row(1, text));
|
|
84
|
-
assert.equal(reports[1].failures?.[0].
|
|
95
|
+
assert.equal(reports[1].failures?.[0].notShown, undefined);
|
|
85
96
|
for (const report of reports) {
|
|
86
97
|
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
87
98
|
assert.ok(report.next?.includes("intended target"));
|
|
88
99
|
}
|
|
89
100
|
});
|
|
90
101
|
|
|
91
|
-
test("model budget grows → grep payload and diagnostics use that call's allowances", () => {
|
|
92
|
-
const
|
|
102
|
+
test("model budget grows → grep payload and diagnostics use that call's allowances", async () => {
|
|
103
|
+
const paths = Array.from({ length: 3_000 }, () => "中文🙂".repeat(3));
|
|
104
|
+
const payload = paths.join("\n");
|
|
93
105
|
const warning = "opening\n" + "中文🙂".repeat(800) + "\nfinal cause";
|
|
94
|
-
const outputs =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
const outputs = await Promise.all(
|
|
107
|
+
[small, large].map((budget) =>
|
|
108
|
+
grepOutput({
|
|
109
|
+
cwd: process.cwd(),
|
|
110
|
+
anchors: createAnchorFormatter(DEFAULT_CONFIG.hashLen),
|
|
111
|
+
context: 0,
|
|
112
|
+
outputMode: "files",
|
|
113
|
+
search: {
|
|
114
|
+
files: paths.map((path) => ({
|
|
115
|
+
filePath: join(process.cwd(), path),
|
|
116
|
+
rows: [],
|
|
117
|
+
snapshot: { revision: "", validUtf8: true },
|
|
118
|
+
})),
|
|
119
|
+
scanLimitReached: false,
|
|
120
|
+
matchLimitReached: false,
|
|
121
|
+
},
|
|
122
|
+
warnings: [{ reason: warning }],
|
|
123
|
+
effectiveLimit: 3_000,
|
|
124
|
+
literalPipe: false,
|
|
125
|
+
budget,
|
|
126
|
+
}),
|
|
127
|
+
),
|
|
107
128
|
);
|
|
108
129
|
assert.equal(outputs[0].facts.outputLimit, formatByteSize(small.grepBytes));
|
|
109
130
|
assert.equal(outputs[1].payload, payload);
|
|
@@ -117,14 +138,29 @@ test("model budget grows → grep payload and diagnostics use that call's allowa
|
|
|
117
138
|
}
|
|
118
139
|
});
|
|
119
140
|
|
|
120
|
-
test("worker error reaches the tool → text is bounded once using the selected model", () => {
|
|
141
|
+
test("worker error reaches the tool → text is bounded once using the selected model", async (t) => {
|
|
121
142
|
const text = "中文🙂".repeat(800);
|
|
122
|
-
const reply =
|
|
123
|
-
|
|
143
|
+
const reply = workerFailure(
|
|
144
|
+
new HashlineError("REGEX_WORKER_FAILED", text, { cause: new Error(text) }),
|
|
145
|
+
);
|
|
146
|
+
t.mock.method(Worker.prototype, "postMessage", function (this: Worker) {
|
|
147
|
+
queueMicrotask(() => this.emit("message", reply));
|
|
148
|
+
});
|
|
149
|
+
let error: unknown;
|
|
150
|
+
try {
|
|
151
|
+
await runRegexReplacements(
|
|
152
|
+
"source",
|
|
153
|
+
[{ find: "source", replace: "target", literal: false }],
|
|
154
|
+
5_000,
|
|
155
|
+
undefined,
|
|
156
|
+
);
|
|
157
|
+
assert.fail("Worker failure must reject");
|
|
158
|
+
} catch (failure) {
|
|
159
|
+
error = failure;
|
|
160
|
+
}
|
|
161
|
+
const reports = [small, large].map((budget) =>
|
|
162
|
+
describeMutationFailure("replace", error, { budget }),
|
|
124
163
|
);
|
|
125
|
-
assert.ok(reply && "error" in reply);
|
|
126
|
-
const error = restoreWorkerError(reply.error);
|
|
127
|
-
const reports = [small, large].map((budget) => describeFailure("replace", error, { budget }));
|
|
128
164
|
assert.ok(Buffer.byteLength(reports[0].message!) <= small.fieldBytes);
|
|
129
165
|
assert.ok(Buffer.byteLength(reports[0].cause!.message) <= small.fieldBytes);
|
|
130
166
|
assert.equal(reports[1].message, text);
|
|
@@ -137,8 +173,16 @@ test("model switches while a mutation waits → failure uses its starting budget
|
|
|
137
173
|
const path = join(dir, "queued.txt");
|
|
138
174
|
const text = "中文🙂".repeat(800);
|
|
139
175
|
await writeFile(path, text);
|
|
140
|
-
let
|
|
141
|
-
const
|
|
176
|
+
let budgets: ReturnType<typeof registerModelBudgets> | undefined;
|
|
177
|
+
const { session, model } = await openTestSession(t, {
|
|
178
|
+
cwd: dir,
|
|
179
|
+
tools: [],
|
|
180
|
+
configure(pi) {
|
|
181
|
+
budgets = registerModelBudgets(pi);
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
assert.ok(budgets);
|
|
185
|
+
await session.setModel({ ...model, id: "small", contextWindow: 65_536 });
|
|
142
186
|
const entered = Promise.withResolvers<void>();
|
|
143
187
|
const release = Promise.withResolvers<void>();
|
|
144
188
|
const holding = withFileMutationQueue(path, async () => {
|
|
@@ -149,18 +193,18 @@ test("model switches while a mutation waits → failure uses its starting budget
|
|
|
149
193
|
try {
|
|
150
194
|
const running = callTool(makeEditOverride(dir, DEFAULT_CONFIG, undefined, budgets), {
|
|
151
195
|
path,
|
|
152
|
-
|
|
196
|
+
steps: [{ action: "delete", anchor: `1#${wrongHash(text)}` }],
|
|
153
197
|
});
|
|
154
|
-
|
|
198
|
+
await session.setModel({ ...model, id: "large", contextWindow: 1_048_576 });
|
|
155
199
|
assert.equal(budgets().readBytes, large.readBytes);
|
|
156
200
|
release.resolve();
|
|
157
201
|
const report = reportOf(await running, "edit");
|
|
158
|
-
assert.ok(report?.failures?.[0].
|
|
202
|
+
assert.ok(report?.failures?.[0].notShown);
|
|
159
203
|
assert.equal(report.failures[0].observed, undefined);
|
|
160
204
|
const next = reportOf(
|
|
161
205
|
await callTool(makeEditOverride(dir, DEFAULT_CONFIG, undefined, budgets), {
|
|
162
206
|
path,
|
|
163
|
-
|
|
207
|
+
steps: [{ action: "delete", anchor: `1#${wrongHash(text)}` }],
|
|
164
208
|
}),
|
|
165
209
|
"edit",
|
|
166
210
|
);
|
package/src/pi/path.ts
CHANGED
|
@@ -23,7 +23,7 @@ export function canonicalPath(cwd: string, path: string): string {
|
|
|
23
23
|
!normalized.includes("\\")
|
|
24
24
|
) {
|
|
25
25
|
const match = WINDOWS_SHELL_DRIVE.exec(normalized);
|
|
26
|
-
if (match) normalized = `${match[1]
|
|
26
|
+
if (match) normalized = `${match[1].toUpperCase()}:\\${match[2]?.replaceAll("/", "\\") ?? ""}`;
|
|
27
27
|
}
|
|
28
28
|
if (normalized === "~") normalized = homedir();
|
|
29
29
|
else if (
|
package/src/pi/pi.test.ts
CHANGED
|
@@ -4,16 +4,17 @@ import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
|
4
4
|
import { tmpdir, homedir } from "node:os";
|
|
5
5
|
import { computeLineHash } from "../core/hash.ts";
|
|
6
6
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
7
|
+
import { commandCompletion } from "./action-fusion.testing.ts";
|
|
8
|
+
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
9
|
+
import { makeGrepOverride } from "./tool-factories.testing.ts";
|
|
10
|
+
import { makeReadOverride } from "./tool-factories.testing.ts";
|
|
11
|
+
import { makeReplaceTool } from "./tool-factories.testing.ts";
|
|
12
|
+
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
12
13
|
import { canonicalPath } from "./path.ts";
|
|
13
14
|
import { join, resolve } from "node:path";
|
|
14
15
|
import { pathToFileURL } from "node:url";
|
|
15
16
|
import { callTool } from "./tool-call.testing.ts";
|
|
16
|
-
import { DEFAULT_CONFIG } from "./
|
|
17
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
17
18
|
|
|
18
19
|
test("canonicalPath resolves relative and absolute", () => {
|
|
19
20
|
const cwd = resolve("/cwd");
|
|
@@ -95,8 +96,8 @@ test("file tools share Pi-style URL and @ path resolution", async () => {
|
|
|
95
96
|
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
96
97
|
{
|
|
97
98
|
path: `@${url}`,
|
|
98
|
-
|
|
99
|
-
{ action: "replace", anchor: `1#${computeLineHash(1, "after", 4)}`,
|
|
99
|
+
steps: [
|
|
100
|
+
{ action: "replace", anchor: `1#${computeLineHash(1, "after", 4)}`, lines: ["edited"] },
|
|
100
101
|
],
|
|
101
102
|
},
|
|
102
103
|
{ toolCallId: "edit", ctx },
|
|
@@ -110,10 +111,14 @@ test("file tools share Pi-style URL and @ path resolution", async () => {
|
|
|
110
111
|
assert.match(matches.content[0].text, /edited/);
|
|
111
112
|
assert.equal(await readFile(file, "utf8"), "edited\n");
|
|
112
113
|
let commands = 0;
|
|
113
|
-
const fusion = createActionFusionExecutor(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
115
|
+
const fusionContext = {
|
|
116
|
+
cwd: dir,
|
|
117
|
+
executeTool: async () => {
|
|
118
|
+
commands++;
|
|
119
|
+
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
120
|
+
},
|
|
121
|
+
};
|
|
117
122
|
await callTool(
|
|
118
123
|
makeWriteOverride(dir, fusion),
|
|
119
124
|
{
|
|
@@ -122,7 +127,7 @@ test("file tools share Pi-style URL and @ path resolution", async () => {
|
|
|
122
127
|
mode: "overwrite",
|
|
123
128
|
then_run: { command: "check" },
|
|
124
129
|
},
|
|
125
|
-
{ toolCallId: "fused", ctx },
|
|
130
|
+
{ toolCallId: "fused", ctx: fusionContext },
|
|
126
131
|
);
|
|
127
132
|
assert.equal(commands, 1);
|
|
128
133
|
} finally {
|
package/src/pi/read-tool.ts
CHANGED
|
@@ -27,12 +27,12 @@ import { scanTextLines } from "./text-stream.ts";
|
|
|
27
27
|
import { createAnchorFormatter, displayCarriageReturns, parseHashline } from "./anchor-format.ts";
|
|
28
28
|
import { canonicalPath, serializePath } from "./path.ts";
|
|
29
29
|
import { renderToolError, reportLines, splitReport } from "./render.ts";
|
|
30
|
-
import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
|
|
30
|
+
import { POSITIVE_SAFE_INTEGER, STRICT_SAMPLING } from "./schema.ts";
|
|
31
31
|
import { errnoCode } from "../core/errors.ts";
|
|
32
32
|
import { throwIfCancelled } from "./error-text.ts";
|
|
33
33
|
import { reportResult, runTool, type ReportDetails } from "./report.ts";
|
|
34
|
-
import {
|
|
35
|
-
import type {
|
|
34
|
+
import { formatByteSize } from "./budgets.ts";
|
|
35
|
+
import type { ToolEnvironment } from "./config.ts";
|
|
36
36
|
import { createArgumentPreparer } from "./argument-validation.ts";
|
|
37
37
|
|
|
38
38
|
const DEFAULT_OFFSET = 1;
|
|
@@ -44,7 +44,7 @@ type ReadDetails = (ReadToolDetails & { nativeRead?: true }) & Partial<ReportDet
|
|
|
44
44
|
* First result line: `<path> · <N> lines`, optionally ` (from line <offset>)` and
|
|
45
45
|
* ` · no trailing newline`. A missing final newline is a byte-level fact the
|
|
46
46
|
* numbered rows cannot show, so the header, which the model never copies into an
|
|
47
|
-
* edit `
|
|
47
|
+
* edit `lines`, states it. READ_HEADER is the TUI's parser for this exact line.
|
|
48
48
|
*/
|
|
49
49
|
function formatReadHeader(
|
|
50
50
|
path: string,
|
|
@@ -66,7 +66,6 @@ const READ_HEADER = /^(.+?) · (\d+ lines(?: \(from line \d+\))?(?: · no traili
|
|
|
66
66
|
*/
|
|
67
67
|
function renderReadBody(raw: string, path: string, theme: Theme): string {
|
|
68
68
|
const lines = raw.split("\n");
|
|
69
|
-
if (lines.length === 0) return "";
|
|
70
69
|
const out: string[] = [];
|
|
71
70
|
|
|
72
71
|
// Header: "<path> · <N> lines", optionally followed by " (from line <offset>)"
|
|
@@ -99,7 +98,7 @@ function renderReadBody(raw: string, path: string, theme: Theme): string {
|
|
|
99
98
|
} else {
|
|
100
99
|
rendered = detabbed.map((l) => theme.fg("toolOutput", l));
|
|
101
100
|
}
|
|
102
|
-
for (let i = 0; i < rendered.length
|
|
101
|
+
for (let i = 0; i < rendered.length; i++) {
|
|
103
102
|
out.push(theme.fg("dim", ` ${lineNos[i]}: `) + rendered[i]);
|
|
104
103
|
}
|
|
105
104
|
|
|
@@ -107,11 +106,11 @@ function renderReadBody(raw: string, path: string, theme: Theme): string {
|
|
|
107
106
|
}
|
|
108
107
|
|
|
109
108
|
/** Build the read override (a ToolDefinition fragment for registerTool). */
|
|
110
|
-
export function makeReadOverride(
|
|
111
|
-
cwd
|
|
112
|
-
config
|
|
113
|
-
budgets
|
|
114
|
-
): ToolDefinition<
|
|
109
|
+
export function makeReadOverride({
|
|
110
|
+
cwd,
|
|
111
|
+
config,
|
|
112
|
+
budgets,
|
|
113
|
+
}: ToolEnvironment): ToolDefinition<
|
|
115
114
|
ReturnType<typeof createReadToolDefinition>["parameters"],
|
|
116
115
|
ReadDetails | undefined
|
|
117
116
|
> {
|
|
@@ -147,6 +146,7 @@ export function makeReadOverride(
|
|
|
147
146
|
promptSnippet: "Read files with editable line anchors",
|
|
148
147
|
parameters: parameters as typeof builtin.parameters,
|
|
149
148
|
prepareArguments: createArgumentPreparer("read", parameters, undefined, budgets),
|
|
149
|
+
constrainedSampling: STRICT_SAMPLING,
|
|
150
150
|
renderShell: "default" as const,
|
|
151
151
|
|
|
152
152
|
renderCall: builtin.renderCall,
|
|
@@ -154,8 +154,8 @@ export function makeReadOverride(
|
|
|
154
154
|
renderResult(result, options, theme, context) {
|
|
155
155
|
const { isPartial, expanded } = options;
|
|
156
156
|
if (isPartial) return new Text(theme.fg("warning", "Reading…"), 0, 0);
|
|
157
|
-
if (context
|
|
158
|
-
const { content, report } = splitReport(result);
|
|
157
|
+
if (context.isError) return renderToolError(result, "read", theme, expanded);
|
|
158
|
+
const { content, report } = splitReport(result, "read");
|
|
159
159
|
if (result.details?.nativeRead)
|
|
160
160
|
return builtin.renderResult!({ ...result, content }, options, theme, context);
|
|
161
161
|
// Collapsed (not expanded): show nothing — the call line carries the
|
|
@@ -164,7 +164,7 @@ export function makeReadOverride(
|
|
|
164
164
|
const raw = content[0]?.type === "text" ? content[0].text : "";
|
|
165
165
|
return new Text(
|
|
166
166
|
[
|
|
167
|
-
renderReadBody(raw, String(context
|
|
167
|
+
renderReadBody(raw, String(context.args?.path ?? ""), theme),
|
|
168
168
|
...(report ? reportLines(report, theme) : []),
|
|
169
169
|
].join("\n"),
|
|
170
170
|
0,
|
|
@@ -243,16 +243,11 @@ export function makeReadOverride(
|
|
|
243
243
|
// Rows were returned and more remain. A caller's own limit is a chosen window: its header
|
|
244
244
|
// and rows already show where it ends, so only a window the extension cut (default limit
|
|
245
245
|
// or byte budget) gets a report.
|
|
246
|
-
const
|
|
246
|
+
const nextOffset =
|
|
247
247
|
(params.limit === undefined || truncated) &&
|
|
248
248
|
rows.length > 0 &&
|
|
249
249
|
start + rows.length <= stats.totalLines
|
|
250
|
-
?
|
|
251
|
-
start,
|
|
252
|
-
end: start + (rows.length - 1),
|
|
253
|
-
totalLines: stats.totalLines,
|
|
254
|
-
nextOffset: start + rows.length,
|
|
255
|
-
}
|
|
250
|
+
? start + rows.length
|
|
256
251
|
: undefined;
|
|
257
252
|
const header = `${formatReadHeader(serializePath(cwd, absPath), stats.totalLines, start, stats.finalNewline)}\n`;
|
|
258
253
|
const limitText = formatByteSize(maxBytes);
|
|
@@ -260,9 +255,9 @@ export function makeReadOverride(
|
|
|
260
255
|
return reportResult(
|
|
261
256
|
{
|
|
262
257
|
...report,
|
|
263
|
-
|
|
258
|
+
nextOffset,
|
|
264
259
|
outputLimit: truncated && !firstLineExceedsLimit ? limitText : undefined,
|
|
265
|
-
lineTooLong: firstLineExceedsLimit ?
|
|
260
|
+
lineTooLong: firstLineExceedsLimit ? limitText : undefined,
|
|
266
261
|
},
|
|
267
262
|
[{ type: "text" as const, text: header + rows.join("\n") }],
|
|
268
263
|
);
|
package/src/pi/read.test.ts
CHANGED
|
@@ -7,11 +7,12 @@ import { join } from "node:path";
|
|
|
7
7
|
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
9
9
|
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
10
|
-
import { makeReadOverride } from "./
|
|
10
|
+
import { makeReadOverride } from "./tool-factories.testing.ts";
|
|
11
11
|
import { computeLineHash } from "../core/hash.ts";
|
|
12
12
|
import { callTool } from "./tool-call.testing.ts";
|
|
13
|
-
import { DEFAULT_CONFIG } from "./
|
|
14
|
-
import {
|
|
13
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
14
|
+
import { formatByteSize } from "./budgets.ts";
|
|
15
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
15
16
|
import { reportOf } from "./report.ts";
|
|
16
17
|
import { createAnchorFormatter } from "./anchor-format.ts";
|
|
17
18
|
|
|
@@ -146,10 +147,7 @@ test("read reports an oversized first row without suggesting an ineffective retr
|
|
|
146
147
|
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "long.txt" });
|
|
147
148
|
assert.doesNotMatch(result.content[0].text, /^\d+#/m);
|
|
148
149
|
assert.doesNotMatch(result.content[0].text, /use offset\/limit/);
|
|
149
|
-
assert.
|
|
150
|
-
line: 1,
|
|
151
|
-
limit: formatByteSize(DEFAULT_READ_MAX_BYTES),
|
|
152
|
-
});
|
|
150
|
+
assert.equal(reportOf(result, "read")?.lineTooLong, formatByteSize(DEFAULT_READ_MAX_BYTES));
|
|
153
151
|
assert.match(reportOf(result, "read")?.next ?? "", /chunks.*bash.*replace/);
|
|
154
152
|
}));
|
|
155
153
|
|
|
@@ -158,14 +156,14 @@ test("read preserves empty files and explicit limits above the native default",
|
|
|
158
156
|
await writeFile(join(dir, "empty.txt"), "");
|
|
159
157
|
const empty = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "empty.txt" });
|
|
160
158
|
assert.match(empty.content[0].text, /0 lines/);
|
|
161
|
-
assert.equal(reportOf(empty, "read")?.
|
|
159
|
+
assert.equal(reportOf(empty, "read")?.nextOffset, undefined);
|
|
162
160
|
await writeFile(join(dir, "many.txt"), "x\n".repeat(2001));
|
|
163
161
|
const many = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
164
162
|
path: "many.txt",
|
|
165
163
|
limit: 2001,
|
|
166
164
|
});
|
|
167
165
|
assert.match(many.content[0].text, /\n2001#[0-9A-Z]+│x/);
|
|
168
|
-
assert.equal(reportOf(many, "read")?.
|
|
166
|
+
assert.equal(reportOf(many, "read")?.nextOffset, undefined);
|
|
169
167
|
}));
|
|
170
168
|
|
|
171
169
|
test("read window end chosen by caller or default → only the default offers optional continuation", async () =>
|
|
@@ -185,14 +183,14 @@ test("read window end chosen by caller or default → only the default offers op
|
|
|
185
183
|
assert.match(result.content[0].text, new RegExp(`large\\.txt · ${totalLines} lines`));
|
|
186
184
|
assert.match(result.content[0].text, new RegExp(`\\n${end}#[0-9A-Z]+│line${end}`));
|
|
187
185
|
assert.doesNotMatch(result.content[0].text, new RegExp(`\\n${end + 1}#[0-9A-Z]+│`));
|
|
188
|
-
const
|
|
186
|
+
const nextOffset = end + 1;
|
|
189
187
|
const report = reportOf(result, "read");
|
|
190
188
|
assert.ok(report);
|
|
191
189
|
if (limit === undefined) {
|
|
192
|
-
assert.
|
|
193
|
-
assert.match(report.next ?? "", /if you need more lines.*offset.*
|
|
190
|
+
assert.equal(report.nextOffset, nextOffset);
|
|
191
|
+
assert.match(report.next ?? "", /if you need more lines.*offset.*nextOffset/i);
|
|
194
192
|
} else {
|
|
195
|
-
assert.equal(Object.hasOwn(report, "
|
|
193
|
+
assert.equal(Object.hasOwn(report, "nextOffset"), false);
|
|
196
194
|
assert.equal(Object.hasOwn(report, "next"), false);
|
|
197
195
|
}
|
|
198
196
|
}
|
|
@@ -209,26 +207,20 @@ test("read pagination supports offset windows and stops suggesting continuation
|
|
|
209
207
|
);
|
|
210
208
|
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
211
209
|
const page = await call(read, { path: "pages.txt", offset: start });
|
|
212
|
-
assert.match(reportOf(page, "read")?.next ?? "", /offset.*
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
end,
|
|
216
|
-
totalLines,
|
|
217
|
-
nextOffset: end + 1,
|
|
218
|
-
});
|
|
219
|
-
const pagination = reportOf(page, "read")?.pagination;
|
|
220
|
-
assert.ok(pagination);
|
|
210
|
+
assert.match(reportOf(page, "read")?.next ?? "", /offset.*nextOffset/);
|
|
211
|
+
const nextOffset = reportOf(page, "read")?.nextOffset;
|
|
212
|
+
assert.ok(nextOffset);
|
|
221
213
|
const next = await call(read, {
|
|
222
214
|
path: "pages.txt",
|
|
223
|
-
offset:
|
|
215
|
+
offset: nextOffset,
|
|
224
216
|
});
|
|
225
217
|
assert.match(next.content[0].text, new RegExp(`\\n${end + 1}#[0-9A-Z]+│line${end + 1}`));
|
|
226
218
|
assert.match(next.content[0].text, new RegExp(`\\n${totalLines}#[0-9A-Z]+│line${totalLines}`));
|
|
227
|
-
assert.equal(reportOf(next, "read")?.
|
|
219
|
+
assert.equal(reportOf(next, "read")?.nextOffset, undefined);
|
|
228
220
|
assert.equal(reportOf(next, "read")?.next, undefined);
|
|
229
221
|
for (const offset of [totalLines - DEFAULT_CONFIG.read.defaultLimit + 1, totalLines + 1]) {
|
|
230
222
|
const result = await call(read, { path: "pages.txt", offset });
|
|
231
|
-
assert.equal(reportOf(result, "read")?.
|
|
223
|
+
assert.equal(reportOf(result, "read")?.nextOffset, undefined);
|
|
232
224
|
assert.equal(reportOf(result, "read")?.next, undefined);
|
|
233
225
|
}
|
|
234
226
|
}));
|
|
@@ -288,14 +280,9 @@ test("read byte truncation retains its budget and a continuation after complete
|
|
|
288
280
|
path: "large.txt",
|
|
289
281
|
limit: 2,
|
|
290
282
|
});
|
|
291
|
-
assert.
|
|
292
|
-
start: 1,
|
|
293
|
-
end: 1,
|
|
294
|
-
totalLines: 3,
|
|
295
|
-
nextOffset: 2,
|
|
296
|
-
});
|
|
283
|
+
assert.equal(reportOf(result, "read")?.nextOffset, 2);
|
|
297
284
|
assert.equal(reportOf(result, "read")?.outputLimit, formatByteSize(DEFAULT_READ_MAX_BYTES));
|
|
298
|
-
assert.match(reportOf(result, "read")?.next ?? "", /offset.*
|
|
285
|
+
assert.match(reportOf(result, "read")?.next ?? "", /offset.*nextOffset/);
|
|
299
286
|
assert.doesNotMatch(result.content[0].text, /showing lines|to continue/);
|
|
300
287
|
}));
|
|
301
288
|
|
|
@@ -310,10 +297,7 @@ test("read bounds oversized selected lines while reporting limits and preserving
|
|
|
310
297
|
assert.equal(reportOf(result, "read")?.lineTooLong, undefined);
|
|
311
298
|
assert.equal(reportOf(result, "read")?.outputLimit, formatByteSize(DEFAULT_READ_MAX_BYTES));
|
|
312
299
|
const oversized = await call(read, { path: "long.txt", offset: 2, limit: 1 });
|
|
313
|
-
assert.
|
|
314
|
-
line: 2,
|
|
315
|
-
limit: formatByteSize(DEFAULT_READ_MAX_BYTES),
|
|
316
|
-
});
|
|
300
|
+
assert.equal(reportOf(oversized, "read")?.lineTooLong, formatByteSize(DEFAULT_READ_MAX_BYTES));
|
|
317
301
|
assert.doesNotMatch(oversized.content[0].text, /^\d+#/m);
|
|
318
302
|
const last = await call(read, { path: "long.txt", offset: 3, limit: 1 });
|
|
319
303
|
assert.match(last.content[0].text, /3#[0-9A-Z]+│last/);
|
|
@@ -325,10 +309,7 @@ test("read budgets visible standalone CR characters using rendered UTF-8 bytes",
|
|
|
325
309
|
const content = "\r".repeat(90_000);
|
|
326
310
|
await writeFile(join(dir, "cr.txt"), content);
|
|
327
311
|
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "cr.txt", limit: 1 });
|
|
328
|
-
assert.
|
|
329
|
-
line: 1,
|
|
330
|
-
limit: formatByteSize(DEFAULT_READ_MAX_BYTES),
|
|
331
|
-
});
|
|
312
|
+
assert.equal(reportOf(result, "read")?.lineTooLong, formatByteSize(DEFAULT_READ_MAX_BYTES));
|
|
332
313
|
assert.doesNotMatch(result.content[0].text, /^\d+#/m);
|
|
333
314
|
}));
|
|
334
315
|
|
|
@@ -363,7 +344,7 @@ test("read accepts safe offsets and limits whose sum exceeds the safe integer ra
|
|
|
363
344
|
offset,
|
|
364
345
|
limit: Number.MAX_SAFE_INTEGER,
|
|
365
346
|
});
|
|
366
|
-
assert.equal(reportOf(result, "read")?.
|
|
347
|
+
assert.equal(reportOf(result, "read")?.nextOffset, undefined);
|
|
367
348
|
assert.doesNotMatch(result.content[0].text, /to continue|│first/);
|
|
368
349
|
if (offset <= 3) assert.match(result.content[0].text, /│third/);
|
|
369
350
|
else assert.doesNotMatch(result.content[0].text, /│/);
|
|
@@ -30,19 +30,9 @@ export function workerReplyDecoder<T extends TSchema>(result: T) {
|
|
|
30
30
|
Type.Object({ result }, { additionalProperties: false }),
|
|
31
31
|
Type.Object({ error: Type.Union(ERROR_SCHEMAS) }, { additionalProperties: false }),
|
|
32
32
|
]);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const error: unknown = Reflect.get(value, "error");
|
|
37
|
-
// Structured cloning permits cycles and BigInt; reports must remain JSON-serializable.
|
|
38
|
-
if (typeof error === "object" && error !== null) JSON.stringify(error);
|
|
39
|
-
// The dynamic error union is validated against the same code-specific schemas.
|
|
40
|
-
return Value.Check(schema, value) ? (value as RegexWorkerReply<Static<T>>) : undefined;
|
|
41
|
-
} catch (error) {
|
|
42
|
-
if (error instanceof TypeError || error instanceof RangeError) return undefined;
|
|
43
|
-
throw error;
|
|
44
|
-
}
|
|
45
|
-
};
|
|
33
|
+
// The dynamic error union is validated against the same code-specific schemas.
|
|
34
|
+
return (value: unknown): RegexWorkerReply<Static<T>> | undefined =>
|
|
35
|
+
Value.Check(schema, value) ? (value as RegexWorkerReply<Static<T>>) : undefined;
|
|
46
36
|
}
|
|
47
37
|
|
|
48
38
|
/** Restore an error workerReplyDecoder checked as the same HashlineError the worker threw. */
|
package/src/pi/regex-worker.ts
CHANGED
|
@@ -73,19 +73,10 @@ export async function withRegexWorker<Input, Result, Output>(
|
|
|
73
73
|
),
|
|
74
74
|
timeoutMs,
|
|
75
75
|
);
|
|
76
|
-
|
|
77
|
-
worker.postMessage(input);
|
|
78
|
-
} catch (error) {
|
|
79
|
-
fail(
|
|
80
|
-
new HashlineError("REGEX_WORKER_FAILED", "Could not send the regex request.", {
|
|
81
|
-
cause: error,
|
|
82
|
-
}),
|
|
83
|
-
);
|
|
84
|
-
}
|
|
76
|
+
worker.postMessage(input);
|
|
85
77
|
});
|
|
86
78
|
});
|
|
87
79
|
try {
|
|
88
|
-
if (signal?.aborted) abort();
|
|
89
80
|
return await execute(run);
|
|
90
81
|
} finally {
|
|
91
82
|
clear();
|