@criogaid/pi-hashline-edit 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { test } from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import { createHash } from "node:crypto";
|
|
4
3
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
5
4
|
import { tmpdir } from "node:os";
|
|
6
|
-
import { byteRevision } from "./file-
|
|
5
|
+
import { byteRevision } from "./file-revision.ts";
|
|
7
6
|
import { join } from "node:path";
|
|
8
7
|
import { scanTextFile, scanTextLines } from "./text-stream.ts";
|
|
9
8
|
import { splitLines } from "../core/lines.ts";
|
|
9
|
+
import { isUtf8 } from "node:buffer";
|
|
10
|
+
import { readFile } from "node:fs/promises";
|
|
11
|
+
import { truncateSync } from "node:fs";
|
|
12
|
+
import { Utf8DecodingError } from "../core/text.ts";
|
|
13
|
+
import { HashlineError } from "../core/errors.ts";
|
|
10
14
|
|
|
11
15
|
async function withFile(run: (path: string) => Promise<void>) {
|
|
12
16
|
const directory = await mkdtemp(join(tmpdir(), "hashline-stream-"));
|
|
@@ -43,29 +47,40 @@ test("streamed lines preserve BOM, standalone CR, and boundaries split across ch
|
|
|
43
47
|
assert.deepEqual(lines, splitLines(text));
|
|
44
48
|
assert.equal(stats.totalLines, lines.length);
|
|
45
49
|
assert.equal(stats.finalNewline, text === "" || text.endsWith("\n"));
|
|
46
|
-
assert.equal(stats.hasCrLf, text.includes("\r\n"));
|
|
47
50
|
assert.equal(stats.byteLength, Buffer.byteLength(text));
|
|
48
51
|
}
|
|
49
52
|
}));
|
|
50
53
|
|
|
51
|
-
test("streamed
|
|
54
|
+
test("streamed scans → return the revision of every original byte and byte-based chunk progress", async () =>
|
|
52
55
|
withFile(async (path) => {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
const chunkBytes = 64 * 1024;
|
|
57
|
+
for (const source of [
|
|
58
|
+
Buffer.alloc(0),
|
|
59
|
+
Buffer.from("\uFEFF"),
|
|
60
|
+
Buffer.from("first\r\nlast\r\n"),
|
|
61
|
+
Buffer.from("\uFEFF" + "a".repeat(chunkBytes - 4) + "😀\r\nlast"),
|
|
62
|
+
]) {
|
|
63
|
+
await writeFile(path, source);
|
|
64
|
+
const chunks: string[] = [];
|
|
65
|
+
const progress: number[] = [];
|
|
66
|
+
const stats = await scanTextFile(path, {
|
|
67
|
+
onChunk(text, bytesRead) {
|
|
68
|
+
chunks.push(text);
|
|
69
|
+
progress.push(bytesRead);
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
assert.equal(chunks.join(""), source.toString("utf8"));
|
|
73
|
+
assert.equal(stats.revision, byteRevision(source));
|
|
74
|
+
assert.equal(stats.byteLength, source.length);
|
|
75
|
+
assert.equal(progress.at(-1) ?? 0, source.length);
|
|
76
|
+
if (source.length > chunkBytes) assert.equal(progress[0], chunkBytes);
|
|
77
|
+
const selected = await scanTextLines(
|
|
78
|
+
path,
|
|
79
|
+
(number) => number === 1,
|
|
80
|
+
() => {},
|
|
81
|
+
);
|
|
82
|
+
assert.equal(selected.revision, stats.revision);
|
|
83
|
+
}
|
|
69
84
|
}));
|
|
70
85
|
|
|
71
86
|
test("streamed selection discards oversized content and still counts and reads subsequent lines", async () =>
|
|
@@ -98,7 +113,7 @@ test("streamed validation checks the entire file and gives NUL precedence over m
|
|
|
98
113
|
(number) => number === 1,
|
|
99
114
|
() => {},
|
|
100
115
|
),
|
|
101
|
-
|
|
116
|
+
(error: unknown) => error instanceof Utf8DecodingError,
|
|
102
117
|
);
|
|
103
118
|
await writeFile(
|
|
104
119
|
path,
|
|
@@ -112,8 +127,8 @@ test("streamed reads propagate cancellation", async () =>
|
|
|
112
127
|
await writeFile(path, "a".repeat(2 * 65536));
|
|
113
128
|
const controller = new AbortController();
|
|
114
129
|
await assert.rejects(
|
|
115
|
-
scanTextFile(path, () => controller.abort(), controller.signal),
|
|
116
|
-
|
|
130
|
+
scanTextFile(path, { onChunk: () => controller.abort(), signal: controller.signal }),
|
|
131
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "OPERATION_ABORTED",
|
|
117
132
|
);
|
|
118
133
|
}));
|
|
119
134
|
|
|
@@ -127,7 +142,6 @@ test("lossy decoding reports invalid UTF-8 across chunk boundaries and preserves
|
|
|
127
142
|
Buffer.from("\r\nlast"),
|
|
128
143
|
]);
|
|
129
144
|
await writeFile(path, source);
|
|
130
|
-
const hash = createHash("sha256");
|
|
131
145
|
const lines: string[] = [];
|
|
132
146
|
const stats = await scanTextLines(
|
|
133
147
|
path,
|
|
@@ -135,18 +149,19 @@ test("lossy decoding reports invalid UTF-8 across chunk boundaries and preserves
|
|
|
135
149
|
(line) => lines.push(line.text ?? ""),
|
|
136
150
|
{
|
|
137
151
|
decoding: "lossy",
|
|
138
|
-
onBytes: (bytes) => hash.update(bytes),
|
|
139
152
|
},
|
|
140
153
|
);
|
|
141
154
|
assert.equal(stats.validUtf8, false);
|
|
142
155
|
assert.equal(stats.hasNul, false);
|
|
143
|
-
assert.equal(stats.hasCrLf, true);
|
|
144
156
|
assert.deepEqual(lines, splitLines(source.toString("utf8")));
|
|
145
|
-
assert.equal(
|
|
146
|
-
await assert.rejects(
|
|
157
|
+
assert.equal(stats.revision, byteRevision(source));
|
|
158
|
+
await assert.rejects(
|
|
159
|
+
scanTextFile(path),
|
|
160
|
+
(error: unknown) => error instanceof Utf8DecodingError,
|
|
161
|
+
);
|
|
147
162
|
}));
|
|
148
163
|
|
|
149
|
-
test("strict scans
|
|
164
|
+
test("strict scans encounter invalid UTF-8 → stop emitting text and reject decoding", async () =>
|
|
150
165
|
withFile(async (path) => {
|
|
151
166
|
const chunkBytes = 64 * 1024;
|
|
152
167
|
const prefix = Buffer.alloc(chunkBytes, "a");
|
|
@@ -155,20 +170,15 @@ test("strict scans read all bytes but stop emitting text after a decoding error"
|
|
|
155
170
|
const source = Buffer.concat([prefix, invalid, Buffer.from("later text\n")]);
|
|
156
171
|
await writeFile(path, source);
|
|
157
172
|
const emitted: string[] = [];
|
|
158
|
-
const scanned: Buffer[] = [];
|
|
159
173
|
await assert.rejects(
|
|
160
|
-
scanTextFile(
|
|
161
|
-
|
|
162
|
-
(text) => {
|
|
174
|
+
scanTextFile(path, {
|
|
175
|
+
onChunk: (text) => {
|
|
163
176
|
emitted.push(text);
|
|
164
177
|
},
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
),
|
|
168
|
-
/UNSUPPORTED_ENCODING/,
|
|
178
|
+
}),
|
|
179
|
+
(error: unknown) => error instanceof Utf8DecodingError,
|
|
169
180
|
);
|
|
170
181
|
assert.equal(emitted.join(""), prefix.toString("utf8"));
|
|
171
|
-
assert.deepEqual(Buffer.concat(scanned), source);
|
|
172
182
|
}));
|
|
173
183
|
|
|
174
184
|
test("lossy scans flush a multibyte sequence split across chunks and incomplete at EOF", async () =>
|
|
@@ -178,15 +188,12 @@ test("lossy scans flush a multibyte sequence split across chunks and incomplete
|
|
|
178
188
|
const source = Buffer.concat([prefix, Buffer.from([0xf0, 0x9f])]);
|
|
179
189
|
await writeFile(path, source);
|
|
180
190
|
const emitted: string[] = [];
|
|
181
|
-
const stats = await scanTextFile(
|
|
182
|
-
|
|
183
|
-
(text) => {
|
|
191
|
+
const stats = await scanTextFile(path, {
|
|
192
|
+
onChunk: (text) => {
|
|
184
193
|
emitted.push(text);
|
|
185
194
|
},
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
"lossy",
|
|
189
|
-
);
|
|
195
|
+
decoding: "lossy",
|
|
196
|
+
});
|
|
190
197
|
assert.equal(emitted.join(""), prefix.toString("utf8") + "�");
|
|
191
198
|
assert.equal(emitted.at(-1), "�");
|
|
192
199
|
assert.equal(stats.validUtf8, false);
|
|
@@ -201,18 +208,12 @@ test("lossy scans without a text consumer report validity and retain all raw byt
|
|
|
201
208
|
Buffer.from([0xff, 0xf0, 0x9f]),
|
|
202
209
|
]);
|
|
203
210
|
await writeFile(path, source);
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
undefined,
|
|
209
|
-
(bytes) => scanned.push(bytes),
|
|
210
|
-
"lossy",
|
|
211
|
-
);
|
|
212
|
-
assert.deepEqual(Buffer.concat(scanned), source);
|
|
211
|
+
const stats = await scanTextFile(path, {
|
|
212
|
+
decoding: "lossy",
|
|
213
|
+
});
|
|
214
|
+
assert.equal(stats.revision, byteRevision(source));
|
|
213
215
|
assert.equal(stats.validUtf8, false);
|
|
214
216
|
assert.equal(stats.hasNul, false);
|
|
215
|
-
assert.equal(stats.hasCrLf, true);
|
|
216
217
|
assert.equal(stats.totalLines, 2);
|
|
217
218
|
assert.equal(stats.finalNewline, false);
|
|
218
219
|
assert.equal(stats.byteLength, source.length);
|
|
@@ -230,19 +231,15 @@ test("NUL suppresses its chunk and all later text without flushing a pending seq
|
|
|
230
231
|
await writeFile(path, source);
|
|
231
232
|
for (const decoding of ["strict", "lossy"] as const) {
|
|
232
233
|
const emitted: string[] = [];
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
path,
|
|
236
|
-
(text) => {
|
|
234
|
+
const stats = await scanTextFile(path, {
|
|
235
|
+
onChunk: (text) => {
|
|
237
236
|
emitted.push(text);
|
|
238
237
|
},
|
|
239
|
-
undefined,
|
|
240
|
-
(bytes) => scanned.push(bytes),
|
|
241
238
|
decoding,
|
|
242
|
-
);
|
|
239
|
+
});
|
|
243
240
|
assert.equal(stats.hasNul, true);
|
|
244
241
|
assert.equal(emitted.join(""), prefix.toString("utf8"));
|
|
245
|
-
assert.
|
|
242
|
+
assert.equal(stats.revision, byteRevision(source));
|
|
246
243
|
}
|
|
247
244
|
}));
|
|
248
245
|
|
|
@@ -255,17 +252,48 @@ test("NUL takes precedence over earlier malformed UTF-8 in strict and lossy scan
|
|
|
255
252
|
await writeFile(path, source);
|
|
256
253
|
for (const decoding of ["strict", "lossy"] as const) {
|
|
257
254
|
const emitted: string[] = [];
|
|
258
|
-
const stats = await scanTextFile(
|
|
259
|
-
|
|
260
|
-
(text) => {
|
|
255
|
+
const stats = await scanTextFile(path, {
|
|
256
|
+
onChunk: (text) => {
|
|
261
257
|
emitted.push(text);
|
|
262
258
|
},
|
|
263
|
-
undefined,
|
|
264
|
-
undefined,
|
|
265
259
|
decoding,
|
|
266
|
-
);
|
|
260
|
+
});
|
|
267
261
|
assert.equal(stats.hasNul, true);
|
|
268
262
|
assert.equal(stats.validUtf8, false);
|
|
269
263
|
assert.equal(emitted.join(""), decoding === "strict" ? "" : invalid.toString("utf8"));
|
|
270
264
|
}
|
|
271
265
|
}));
|
|
266
|
+
|
|
267
|
+
test("valid file truncated across a pending UTF-8 sequence → scans report a changed file", async () =>
|
|
268
|
+
withFile(async (path) => {
|
|
269
|
+
const chunkBytes = 64 * 1024;
|
|
270
|
+
const source = Buffer.from(
|
|
271
|
+
"x".repeat(chunkBytes - 1) +
|
|
272
|
+
"中" +
|
|
273
|
+
"x".repeat(chunkBytes - 3) +
|
|
274
|
+
"中" +
|
|
275
|
+
"tail".repeat(chunkBytes),
|
|
276
|
+
);
|
|
277
|
+
assert.equal(isUtf8(source), true);
|
|
278
|
+
for (const decoding of ["strict", "lossy"] as const) {
|
|
279
|
+
await writeFile(path, source);
|
|
280
|
+
let changed = false;
|
|
281
|
+
await assert.rejects(
|
|
282
|
+
scanTextFile(path, {
|
|
283
|
+
onChunk: () => {
|
|
284
|
+
if (changed) return;
|
|
285
|
+
changed = true;
|
|
286
|
+
truncateSync(path, chunkBytes - 1);
|
|
287
|
+
},
|
|
288
|
+
decoding,
|
|
289
|
+
}),
|
|
290
|
+
(error: unknown) => {
|
|
291
|
+
assert.ok(error instanceof Error);
|
|
292
|
+
assert.match(error.message, /File changed/);
|
|
293
|
+
assert.ok(!(error instanceof Utf8DecodingError));
|
|
294
|
+
return true;
|
|
295
|
+
},
|
|
296
|
+
);
|
|
297
|
+
assert.equal(isUtf8(await readFile(path)), true);
|
|
298
|
+
}
|
|
299
|
+
}));
|
package/src/pi/text-stream.ts
CHANGED
|
@@ -1,86 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming text scans: one read of a file yields its revision, its NUL and UTF-8 validity, line
|
|
3
|
+
* statistics, and the decoded text or selected lines.
|
|
4
|
+
*
|
|
5
|
+
* @module pi-hashline-edit/pi
|
|
6
|
+
*/
|
|
1
7
|
import { createReadStream } from "node:fs";
|
|
2
|
-
import { createUtf8Decoder, type Utf8Decoding } from "../core/text.ts";
|
|
8
|
+
import { createUtf8Decoder, Utf8DecodingError, type Utf8Decoding } from "../core/text.ts";
|
|
3
9
|
import { throwIfCancelled } from "./error-text.ts";
|
|
10
|
+
import { withFileRead } from "./file-read.ts";
|
|
11
|
+
import { createRevisionHasher } from "./file-revision.ts";
|
|
4
12
|
|
|
5
|
-
|
|
13
|
+
const TEXT_READ_CHUNK_BYTES = 64 * 1024;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Scan whole-file bytes; lossy decoding reports validity without rejecting malformed UTF-8.
|
|
17
|
+
* The revision covers exactly the bytes this scan read; onChunk also receives the bytes read so far.
|
|
18
|
+
*/
|
|
6
19
|
export async function scanTextFile(
|
|
7
20
|
path: string,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
21
|
+
options: {
|
|
22
|
+
readonly onChunk?: (text: string, bytesRead: number) => void | Promise<void>;
|
|
23
|
+
readonly signal?: AbortSignal;
|
|
24
|
+
readonly decoding?: Utf8Decoding;
|
|
25
|
+
} = {},
|
|
12
26
|
) {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
const { onChunk, signal, decoding = "strict" } = options;
|
|
28
|
+
return withFileRead(path, signal, async (handle) => {
|
|
29
|
+
const decode = createUtf8Decoder();
|
|
30
|
+
// Strict decoding always decides validity; lossy text is decoded only for a chunk consumer.
|
|
31
|
+
const lossy = decoding === "lossy" && onChunk ? createUtf8Decoder("lossy") : undefined;
|
|
32
|
+
const revision = createRevisionHasher();
|
|
33
|
+
let byteLength = 0;
|
|
34
|
+
let lineFeeds = 0;
|
|
35
|
+
let lastByte = -1;
|
|
36
|
+
let hasNul = false;
|
|
37
|
+
let decodingError: unknown;
|
|
38
|
+
const stream = createReadStream(path, {
|
|
39
|
+
fd: handle,
|
|
40
|
+
autoClose: false,
|
|
41
|
+
highWaterMark: TEXT_READ_CHUNK_BYTES,
|
|
42
|
+
signal,
|
|
43
|
+
});
|
|
44
|
+
try {
|
|
45
|
+
for await (const chunk of stream) {
|
|
46
|
+
const bytes = chunk as Buffer;
|
|
47
|
+
revision.update(bytes);
|
|
48
|
+
byteLength += bytes.length;
|
|
49
|
+
hasNul ||= bytes.includes(0);
|
|
50
|
+
lastByte = bytes[bytes.length - 1];
|
|
51
|
+
let offset = 0;
|
|
52
|
+
while ((offset = bytes.indexOf(10, offset)) !== -1) {
|
|
53
|
+
lineFeeds++;
|
|
54
|
+
offset++;
|
|
55
|
+
}
|
|
56
|
+
// NUL takes precedence even when an earlier chunk contained malformed UTF-8.
|
|
57
|
+
if (hasNul) continue;
|
|
58
|
+
let text: string | undefined;
|
|
59
|
+
if (!decodingError) {
|
|
60
|
+
try {
|
|
61
|
+
text = decode(bytes, true);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (!(error instanceof Utf8DecodingError)) throw error;
|
|
64
|
+
decodingError = error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (lossy) text = lossy(bytes, true);
|
|
68
|
+
if (text !== undefined) await onChunk?.(text, byteLength);
|
|
35
69
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
70
|
+
} catch (error) {
|
|
71
|
+
// Node's stream abort raises its own AbortError; report the shared cancellation text.
|
|
72
|
+
throwIfCancelled(signal);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
throwIfCancelled(signal);
|
|
76
|
+
if (!hasNul) {
|
|
77
|
+
let tail: string | undefined;
|
|
39
78
|
if (!decodingError) {
|
|
40
79
|
try {
|
|
41
|
-
|
|
80
|
+
tail = decode();
|
|
42
81
|
} catch (error) {
|
|
82
|
+
if (!(error instanceof Utf8DecodingError)) throw error;
|
|
43
83
|
decodingError = error;
|
|
44
84
|
}
|
|
45
85
|
}
|
|
46
|
-
if (lossy)
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
} catch (error) {
|
|
50
|
-
// Node's stream abort raises its own AbortError; report the shared cancellation text.
|
|
51
|
-
throwIfCancelled(signal);
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
54
|
-
throwIfCancelled(signal);
|
|
55
|
-
if (!hasNul) {
|
|
56
|
-
let tail: string | undefined;
|
|
57
|
-
if (!decodingError) {
|
|
58
|
-
try {
|
|
59
|
-
tail = decode();
|
|
60
|
-
} catch (error) {
|
|
61
|
-
decodingError = error;
|
|
62
|
-
}
|
|
86
|
+
if (lossy) tail = lossy();
|
|
87
|
+
else if (decodingError && decoding === "strict") throw decodingError;
|
|
88
|
+
if (tail) await onChunk?.(tail, byteLength);
|
|
63
89
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
hasCrLf,
|
|
74
|
-
validUtf8: !decodingError,
|
|
75
|
-
};
|
|
90
|
+
return {
|
|
91
|
+
revision: revision.digest(),
|
|
92
|
+
byteLength,
|
|
93
|
+
totalLines: lineFeeds + (lastByte !== -1 && lastByte !== 10 ? 1 : 0),
|
|
94
|
+
finalNewline: lastByte === -1 || lastByte === 10,
|
|
95
|
+
hasNul,
|
|
96
|
+
validUtf8: !decodingError,
|
|
97
|
+
};
|
|
98
|
+
});
|
|
76
99
|
}
|
|
77
100
|
|
|
78
101
|
interface ScannedLine {
|
|
79
|
-
number: number;
|
|
80
|
-
byteLength: number;
|
|
81
|
-
carriageReturns: number;
|
|
102
|
+
readonly number: number;
|
|
103
|
+
readonly byteLength: number;
|
|
104
|
+
readonly carriageReturns: number;
|
|
82
105
|
/** Absent when the selected line exceeds maxLineBytes. */
|
|
83
|
-
text: string | undefined;
|
|
106
|
+
readonly text: string | undefined;
|
|
84
107
|
}
|
|
85
108
|
|
|
86
109
|
/** Visit selected logical lines, retaining at most maxLineBytes of content per line. */
|
|
@@ -89,10 +112,9 @@ export async function scanTextLines(
|
|
|
89
112
|
select: (number: number) => boolean,
|
|
90
113
|
onLine: (line: ScannedLine) => void,
|
|
91
114
|
options: {
|
|
92
|
-
signal?: AbortSignal;
|
|
93
|
-
maxLineBytes?: number;
|
|
94
|
-
|
|
95
|
-
decoding?: Utf8Decoding;
|
|
115
|
+
readonly signal?: AbortSignal;
|
|
116
|
+
readonly maxLineBytes?: number;
|
|
117
|
+
readonly decoding?: Utf8Decoding;
|
|
96
118
|
} = {},
|
|
97
119
|
) {
|
|
98
120
|
const maxBytes = options.maxLineBytes ?? Infinity;
|
|
@@ -122,9 +144,11 @@ export async function scanTextLines(
|
|
|
122
144
|
number++;
|
|
123
145
|
selected = select(number);
|
|
124
146
|
};
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
147
|
+
const { signal, decoding } = options;
|
|
148
|
+
const stats = await scanTextFile(path, {
|
|
149
|
+
signal,
|
|
150
|
+
decoding,
|
|
151
|
+
onChunk: (text) => {
|
|
128
152
|
let start = 0;
|
|
129
153
|
while (start < text.length) {
|
|
130
154
|
const lf = text.indexOf("\n", start);
|
|
@@ -147,10 +171,7 @@ export async function scanTextLines(
|
|
|
147
171
|
start = lf + 1;
|
|
148
172
|
}
|
|
149
173
|
},
|
|
150
|
-
|
|
151
|
-
options.onBytes,
|
|
152
|
-
options.decoding,
|
|
153
|
-
);
|
|
174
|
+
});
|
|
154
175
|
if (!stats.hasNul && byteLength > 0) finish(false);
|
|
155
176
|
return stats;
|
|
156
177
|
}
|
|
@@ -7,23 +7,20 @@
|
|
|
7
7
|
* unusual arguments go through the same funnel instead of calling `execute`.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
11
|
-
import { join } from "node:path";
|
|
10
|
+
import { validateToolArguments, type Tool, type ToolCall } from "@earendil-works/pi-ai";
|
|
12
11
|
import type { TestContext } from "node:test";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
DefaultResourceLoader,
|
|
16
|
-
type ExtensionToolContext,
|
|
17
|
-
ModelRuntime,
|
|
18
|
-
SessionManager,
|
|
19
|
-
SettingsManager,
|
|
20
|
-
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import type { ExtensionToolContext } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { openTestSession } from "../testing/session.testing.ts";
|
|
21
14
|
|
|
22
|
-
interface CallableTool {
|
|
23
|
-
readonly name: string;
|
|
24
|
-
readonly parameters: unknown;
|
|
15
|
+
interface CallableTool extends Tool {
|
|
25
16
|
prepareArguments?(args: unknown): unknown;
|
|
26
|
-
execute(
|
|
17
|
+
execute(
|
|
18
|
+
toolCallId: string,
|
|
19
|
+
params: unknown,
|
|
20
|
+
signal: AbortSignal | undefined,
|
|
21
|
+
onUpdate: unknown,
|
|
22
|
+
ctx: unknown,
|
|
23
|
+
): Promise<unknown>;
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
export interface ToolCallOptions {
|
|
@@ -34,14 +31,20 @@ export interface ToolCallOptions {
|
|
|
34
31
|
}
|
|
35
32
|
|
|
36
33
|
/** prepareArguments → schema validation → execute, failing the way Pi reports invalid calls. */
|
|
34
|
+
export function callTool<T extends CallableTool>(
|
|
35
|
+
tool: T,
|
|
36
|
+
args: unknown,
|
|
37
|
+
options?: ToolCallOptions,
|
|
38
|
+
): ReturnType<T["execute"]>;
|
|
37
39
|
export async function callTool(tool: CallableTool, args: unknown, options: ToolCallOptions = {}) {
|
|
38
40
|
const toolCallId = options.toolCallId ?? "test";
|
|
39
41
|
const prepared = tool.prepareArguments ? tool.prepareArguments(args) : args;
|
|
40
|
-
const validated = validateToolArguments(tool
|
|
42
|
+
const validated: unknown = validateToolArguments(tool, {
|
|
41
43
|
type: "toolCall",
|
|
42
44
|
id: toolCallId,
|
|
43
45
|
name: tool.name,
|
|
44
|
-
|
|
46
|
+
// Pi owns shape checking and coercion at this external boundary.
|
|
47
|
+
arguments: prepared as ToolCall["arguments"],
|
|
45
48
|
});
|
|
46
49
|
return tool.execute(toolCallId, validated, options.signal, options.onUpdate, options.ctx);
|
|
47
50
|
}
|
|
@@ -52,36 +55,7 @@ export async function createToolContext(
|
|
|
52
55
|
t: TestContext,
|
|
53
56
|
mode: ExtensionToolContext["mode"] = "json",
|
|
54
57
|
) {
|
|
55
|
-
const
|
|
56
|
-
authPath: join(cwd, "test-auth.json"),
|
|
57
|
-
modelsPath: null,
|
|
58
|
-
modelsStorePath: join(cwd, "test-models.json"),
|
|
59
|
-
refreshOnCreate: false,
|
|
60
|
-
allowModelNetwork: false,
|
|
61
|
-
});
|
|
62
|
-
const settingsManager = SettingsManager.inMemory();
|
|
63
|
-
const loader = new DefaultResourceLoader({
|
|
64
|
-
cwd,
|
|
65
|
-
agentDir: cwd,
|
|
66
|
-
settingsManager,
|
|
67
|
-
noExtensions: true,
|
|
68
|
-
noSkills: true,
|
|
69
|
-
noPromptTemplates: true,
|
|
70
|
-
noThemes: true,
|
|
71
|
-
noContextFiles: true,
|
|
72
|
-
});
|
|
73
|
-
await loader.reload();
|
|
74
|
-
const { session } = await createAgentSession({
|
|
75
|
-
cwd,
|
|
76
|
-
agentDir: cwd,
|
|
77
|
-
modelRuntime: runtime,
|
|
78
|
-
settingsManager,
|
|
79
|
-
resourceLoader: loader,
|
|
80
|
-
sessionManager: SessionManager.inMemory(cwd),
|
|
81
|
-
tools: [],
|
|
82
|
-
});
|
|
83
|
-
t.after(() => session.dispose());
|
|
84
|
-
await session.bindExtensions({});
|
|
58
|
+
const { session } = await openTestSession(t, { cwd, tools: [] });
|
|
85
59
|
session.extensionRunner.setUIContext(undefined, mode);
|
|
86
60
|
return session.extensionRunner.createToolContext("test", undefined);
|
|
87
61
|
}
|