@criogaid/pi-hashline-edit 0.4.0 → 0.5.1
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 +134 -30
- package/package.json +3 -3
- package/src/core/apply.test.ts +194 -104
- package/src/core/apply.ts +6 -37
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +8 -8
- package/src/core/hash.ts +5 -5
- package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
- package/src/core/text.test.ts +30 -1
- package/src/core/text.ts +9 -6
- package/src/core/types.ts +4 -7
- package/src/index.ts +9 -11
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +285 -153
- package/src/integration/large-text.test.ts +11 -12
- package/src/integration/replace-package.test.ts +11 -7
- package/src/pi/action-fusion.test.ts +251 -39
- package/src/pi/action-fusion.ts +113 -117
- package/src/pi/anchor-format.test.ts +91 -24
- package/src/pi/anchor-format.ts +43 -9
- package/src/pi/budgets.ts +7 -2
- package/src/pi/config.test.ts +153 -40
- package/src/pi/config.ts +87 -41
- package/src/pi/diagnostic-buffer.test.ts +58 -0
- package/src/pi/diagnostic-buffer.ts +59 -0
- package/src/pi/e2e-scenarios.test.ts +13 -11
- package/src/pi/edit-tool.ts +21 -157
- package/src/pi/error-text.ts +10 -5
- package/src/pi/execute.test.ts +299 -175
- package/src/pi/failure-context.test.ts +7 -2
- package/src/pi/failure-context.ts +155 -8
- package/src/pi/failure-path.test.ts +33 -26
- package/src/pi/file-commit.ts +2 -1
- package/src/pi/fusion-card.test.ts +26 -0
- package/src/pi/fusion-card.ts +102 -6
- package/src/pi/grep-output.ts +50 -35
- package/src/pi/grep-render.ts +13 -15
- package/src/pi/grep-scope.ts +8 -46
- package/src/pi/grep-search.ts +90 -63
- package/src/pi/grep-tool.ts +109 -121
- package/src/pi/grep.test.ts +597 -377
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +51 -2
- package/src/pi/mutation-result.ts +62 -55
- package/src/pi/mutation-runner.ts +16 -33
- package/src/pi/pi.test.ts +11 -6
- package/src/pi/read-tool.ts +62 -34
- package/src/pi/registration.test.ts +7 -7
- package/src/pi/render.test.ts +143 -9
- package/src/pi/render.ts +32 -96
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +25 -59
- package/src/pi/replace-worker.ts +2 -2
- package/src/pi/replace.test.ts +62 -58
- package/src/pi/result-safety.test.ts +124 -84
- package/src/pi/rg-line-ranges.ts +3 -2
- package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +43 -9
- package/src/pi/rg-text-view.ts +25 -38
- package/src/pi/schema.ts +2 -10
- package/src/pi/text-stream.test.ts +153 -0
- package/src/pi/text-stream.ts +33 -13
- package/src/pi/write-tool.ts +3 -2
- package/src/pi/write.test.ts +10 -9
- package/src/core/index.ts +0 -14
- package/src/pi/state.ts +0 -26
package/src/pi/rg-text-view.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DiagnosticBuffer } from "./diagnostic-buffer.ts";
|
|
1
2
|
import { mkdtemp, open, rm } from "node:fs/promises";
|
|
2
3
|
import { tmpdir } from "node:os";
|
|
3
4
|
import { join, resolve, sep } from "node:path";
|
|
@@ -10,8 +11,10 @@ import {
|
|
|
10
11
|
runRg,
|
|
11
12
|
runRgPaths,
|
|
12
13
|
type RgRunResult,
|
|
13
|
-
} from "./rg-
|
|
14
|
-
import {
|
|
14
|
+
} from "./rg-process.ts";
|
|
15
|
+
import { scopeArgs, type SearchRunner } from "./grep-search.ts";
|
|
16
|
+
import { errorMessage } from "../core/errors.ts";
|
|
17
|
+
import { searchChangedError, throwIfCancelled } from "./error-text.ts";
|
|
15
18
|
|
|
16
19
|
/** CRLF snapshot batches flush after this many files or source bytes (README: 64 files / 8 MiB). */
|
|
17
20
|
const SNAPSHOT_BATCH_FILES = 64;
|
|
@@ -30,7 +33,7 @@ async function writeLfSnapshot(source: string, destination: string, signal?: Abo
|
|
|
30
33
|
},
|
|
31
34
|
signal,
|
|
32
35
|
);
|
|
33
|
-
if (stats.hasNul) throw
|
|
36
|
+
if (stats.hasNul) throw searchChangedError();
|
|
34
37
|
if (pendingCr) await handle.writeFile(pendingCr);
|
|
35
38
|
} finally {
|
|
36
39
|
await handle.close();
|
|
@@ -38,40 +41,24 @@ async function writeLfSnapshot(source: string, destination: string, signal?: Abo
|
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
/** Search LF views in batches while reporting original paths and logical match offsets. */
|
|
41
|
-
export const runRgTextView:
|
|
42
|
-
let boundary = args.length;
|
|
43
|
-
const scope: string[] = [];
|
|
44
|
-
const matcher: string[] = [];
|
|
45
|
-
for (let i = 0; i < args.length; i++) {
|
|
46
|
-
const arg = args[i];
|
|
47
|
-
if (arg === "--") {
|
|
48
|
-
boundary = i;
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
if (arg === "--glob") scope.push(arg, args[++i]);
|
|
52
|
-
else if (["--hidden", "--no-ignore", "--follow"].includes(arg)) scope.push(arg);
|
|
53
|
-
else {
|
|
54
|
-
matcher.push(arg);
|
|
55
|
-
if (arg === "-e") matcher.push(args[++i]);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (boundary === args.length || args[boundary + 1] === "-")
|
|
59
|
-
return runRg(rgPath, args, signal, onLine);
|
|
44
|
+
export const runRgTextView: SearchRunner = async (rgPath, { matcher, scope }, signal, onLine) => {
|
|
60
45
|
let directory: string | undefined;
|
|
61
46
|
const result: RgRunResult = { code: 1, stderr: "", stopped: false };
|
|
62
|
-
|
|
47
|
+
const diagnostics = new DiagnosticBuffer(MAX_RG_STDERR_BYTES);
|
|
48
|
+
// Only valid UTF-8 CRLF files use LF snapshots; malformed UTF-8 keeps its raw search view.
|
|
63
49
|
const snapshots = new Map<string, string>();
|
|
64
|
-
const
|
|
50
|
+
const batchPaths: string[] = [];
|
|
65
51
|
let batchBytes = 0;
|
|
66
52
|
const record = (run: RgRunResult) => {
|
|
67
|
-
|
|
53
|
+
diagnostics.append(run.stderr);
|
|
54
|
+
result.stderr = diagnostics.toString();
|
|
68
55
|
if (run.code !== 0 && run.code !== 1 && !run.stopped) result.code = run.code;
|
|
69
56
|
else if (run.code === 0 && result.code === 1) result.code = 0;
|
|
70
57
|
result.stopped ||= run.stopped;
|
|
71
58
|
};
|
|
72
59
|
|
|
73
60
|
const searchBatch = async (
|
|
74
|
-
|
|
61
|
+
batchPaths: readonly string[],
|
|
75
62
|
rewritePaths: ReadonlyMap<string, string>,
|
|
76
63
|
) => {
|
|
77
64
|
const searchArgs = [
|
|
@@ -80,7 +67,7 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
|
|
|
80
67
|
"--no-ignore",
|
|
81
68
|
"--hidden",
|
|
82
69
|
"--",
|
|
83
|
-
...
|
|
70
|
+
...batchPaths,
|
|
84
71
|
];
|
|
85
72
|
const run = await runRg(rgPath, searchArgs, signal, async (line) => {
|
|
86
73
|
const event = JSON.parse(line);
|
|
@@ -96,12 +83,12 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
|
|
|
96
83
|
};
|
|
97
84
|
|
|
98
85
|
const flush = async () => {
|
|
99
|
-
if (!
|
|
86
|
+
if (!batchPaths.length) return !result.stopped;
|
|
100
87
|
// Scope filtering belongs to the original paths. Snapshot names have no ignore/glob semantics.
|
|
101
|
-
await searchBatch(
|
|
88
|
+
await searchBatch(batchPaths, snapshots);
|
|
102
89
|
for (const path of snapshots.keys()) await rm(path);
|
|
103
90
|
snapshots.clear();
|
|
104
|
-
|
|
91
|
+
batchPaths.length = 0;
|
|
105
92
|
batchBytes = 0;
|
|
106
93
|
return !result.stopped;
|
|
107
94
|
};
|
|
@@ -114,27 +101,27 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
|
|
|
114
101
|
try {
|
|
115
102
|
const listArgs = [
|
|
116
103
|
...COMMON_RG_ARGS,
|
|
117
|
-
...scope,
|
|
104
|
+
...scopeArgs(scope),
|
|
118
105
|
"--files",
|
|
119
106
|
"--null",
|
|
120
107
|
"--",
|
|
121
|
-
...
|
|
108
|
+
...scope.searchPaths,
|
|
122
109
|
];
|
|
123
110
|
const listed = await runRgPaths(rgPath, listArgs, signal, async (path) => {
|
|
124
111
|
throwIfCancelled(signal);
|
|
125
112
|
const original = resolve(path);
|
|
126
113
|
if (directory && original.startsWith(directory + sep)) return true;
|
|
127
114
|
try {
|
|
128
|
-
const info = await scanTextFile(original, undefined, signal);
|
|
129
|
-
if (info.
|
|
115
|
+
const info = await scanTextFile(original, undefined, signal, undefined, "lossy");
|
|
116
|
+
if (info.hasNul) return true;
|
|
117
|
+
if (info.hasCrLf && info.validUtf8) {
|
|
130
118
|
const dir = await ensureDirectory();
|
|
131
119
|
const snapshot = join(dir, String(snapshots.size));
|
|
132
120
|
await writeLfSnapshot(original, snapshot, signal);
|
|
133
121
|
snapshots.set(snapshot, original);
|
|
134
|
-
|
|
122
|
+
batchPaths.push(snapshot);
|
|
135
123
|
} else {
|
|
136
|
-
|
|
137
|
-
searchPaths.push(original);
|
|
124
|
+
batchPaths.push(original);
|
|
138
125
|
}
|
|
139
126
|
batchBytes += info.byteLength;
|
|
140
127
|
} catch (error) {
|
|
@@ -144,7 +131,7 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
|
|
|
144
131
|
return true;
|
|
145
132
|
}
|
|
146
133
|
return (
|
|
147
|
-
(
|
|
134
|
+
(batchPaths.length < SNAPSHOT_BATCH_FILES && batchBytes < SNAPSHOT_BATCH_BYTES) ||
|
|
148
135
|
(await flush())
|
|
149
136
|
);
|
|
150
137
|
});
|
package/src/pi/schema.ts
CHANGED
|
@@ -15,13 +15,5 @@ export function integerRange(minimum: number, maximum: number) {
|
|
|
15
15
|
/** Positive line numbers, line counts, and match limits. */
|
|
16
16
|
export const POSITIVE_SAFE_INTEGER = integerRange(1, Number.MAX_SAFE_INTEGER);
|
|
17
17
|
|
|
18
|
-
/**
|
|
19
|
-
export
|
|
20
|
-
value: unknown,
|
|
21
|
-
minimum: number,
|
|
22
|
-
maximum: number,
|
|
23
|
-
): value is number {
|
|
24
|
-
return (
|
|
25
|
-
typeof value === "number" && Number.isInteger(value) && value >= minimum && value <= maximum
|
|
26
|
-
);
|
|
27
|
-
}
|
|
18
|
+
/** grep `context` parameter and its configured default. */
|
|
19
|
+
export const GREP_CONTEXT_RANGE = integerRange(0, 20);
|
|
@@ -116,3 +116,156 @@ test("streamed reads propagate cancellation", async () =>
|
|
|
116
116
|
{ message: "Operation aborted" },
|
|
117
117
|
);
|
|
118
118
|
}));
|
|
119
|
+
|
|
120
|
+
test("lossy decoding reports invalid UTF-8 across chunk boundaries and preserves raw revisions", async () =>
|
|
121
|
+
withFile(async (path) => {
|
|
122
|
+
const source = Buffer.concat([
|
|
123
|
+
Buffer.from("\uFEFF" + "a".repeat(65532)),
|
|
124
|
+
Buffer.from([0xf0, 0x9f]),
|
|
125
|
+
Buffer.from("\r\nneedle "),
|
|
126
|
+
Buffer.from([0xff]),
|
|
127
|
+
Buffer.from("\r\nlast"),
|
|
128
|
+
]);
|
|
129
|
+
await writeFile(path, source);
|
|
130
|
+
const hash = createHash("sha256");
|
|
131
|
+
const lines: string[] = [];
|
|
132
|
+
const stats = await scanTextLines(
|
|
133
|
+
path,
|
|
134
|
+
() => true,
|
|
135
|
+
(line) => lines.push(line.text ?? ""),
|
|
136
|
+
{
|
|
137
|
+
decoding: "lossy",
|
|
138
|
+
onBytes: (bytes) => hash.update(bytes),
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
assert.equal(stats.validUtf8, false);
|
|
142
|
+
assert.equal(stats.hasNul, false);
|
|
143
|
+
assert.equal(stats.hasCrLf, true);
|
|
144
|
+
assert.deepEqual(lines, splitLines(source.toString("utf8")));
|
|
145
|
+
assert.equal(hash.digest("hex"), byteRevision(source));
|
|
146
|
+
await assert.rejects(scanTextFile(path), /UNSUPPORTED_ENCODING/);
|
|
147
|
+
}));
|
|
148
|
+
|
|
149
|
+
test("strict scans read all bytes but stop emitting text after a decoding error", async () =>
|
|
150
|
+
withFile(async (path) => {
|
|
151
|
+
const chunkBytes = 64 * 1024;
|
|
152
|
+
const prefix = Buffer.alloc(chunkBytes, "a");
|
|
153
|
+
const invalid = Buffer.alloc(chunkBytes, "b");
|
|
154
|
+
invalid[0] = 0xff;
|
|
155
|
+
const source = Buffer.concat([prefix, invalid, Buffer.from("later text\n")]);
|
|
156
|
+
await writeFile(path, source);
|
|
157
|
+
const emitted: string[] = [];
|
|
158
|
+
const scanned: Buffer[] = [];
|
|
159
|
+
await assert.rejects(
|
|
160
|
+
scanTextFile(
|
|
161
|
+
path,
|
|
162
|
+
(text) => {
|
|
163
|
+
emitted.push(text);
|
|
164
|
+
},
|
|
165
|
+
undefined,
|
|
166
|
+
(bytes) => scanned.push(bytes),
|
|
167
|
+
),
|
|
168
|
+
/UNSUPPORTED_ENCODING/,
|
|
169
|
+
);
|
|
170
|
+
assert.equal(emitted.join(""), prefix.toString("utf8"));
|
|
171
|
+
assert.deepEqual(Buffer.concat(scanned), source);
|
|
172
|
+
}));
|
|
173
|
+
|
|
174
|
+
test("lossy scans flush a multibyte sequence split across chunks and incomplete at EOF", async () =>
|
|
175
|
+
withFile(async (path) => {
|
|
176
|
+
const chunkBytes = 64 * 1024;
|
|
177
|
+
const prefix = Buffer.alloc(chunkBytes - 1, "a");
|
|
178
|
+
const source = Buffer.concat([prefix, Buffer.from([0xf0, 0x9f])]);
|
|
179
|
+
await writeFile(path, source);
|
|
180
|
+
const emitted: string[] = [];
|
|
181
|
+
const stats = await scanTextFile(
|
|
182
|
+
path,
|
|
183
|
+
(text) => {
|
|
184
|
+
emitted.push(text);
|
|
185
|
+
},
|
|
186
|
+
undefined,
|
|
187
|
+
undefined,
|
|
188
|
+
"lossy",
|
|
189
|
+
);
|
|
190
|
+
assert.equal(emitted.join(""), prefix.toString("utf8") + "�");
|
|
191
|
+
assert.equal(emitted.at(-1), "�");
|
|
192
|
+
assert.equal(stats.validUtf8, false);
|
|
193
|
+
assert.equal(stats.hasNul, false);
|
|
194
|
+
assert.equal(stats.byteLength, source.length);
|
|
195
|
+
}));
|
|
196
|
+
|
|
197
|
+
test("lossy scans without a text consumer report validity and retain all raw bytes", async () =>
|
|
198
|
+
withFile(async (path) => {
|
|
199
|
+
const source = Buffer.concat([
|
|
200
|
+
Buffer.from("\ufeffbefore\r\n"),
|
|
201
|
+
Buffer.from([0xff, 0xf0, 0x9f]),
|
|
202
|
+
]);
|
|
203
|
+
await writeFile(path, source);
|
|
204
|
+
const scanned: Buffer[] = [];
|
|
205
|
+
const stats = await scanTextFile(
|
|
206
|
+
path,
|
|
207
|
+
undefined,
|
|
208
|
+
undefined,
|
|
209
|
+
(bytes) => scanned.push(bytes),
|
|
210
|
+
"lossy",
|
|
211
|
+
);
|
|
212
|
+
assert.deepEqual(Buffer.concat(scanned), source);
|
|
213
|
+
assert.equal(stats.validUtf8, false);
|
|
214
|
+
assert.equal(stats.hasNul, false);
|
|
215
|
+
assert.equal(stats.hasCrLf, true);
|
|
216
|
+
assert.equal(stats.totalLines, 2);
|
|
217
|
+
assert.equal(stats.finalNewline, false);
|
|
218
|
+
assert.equal(stats.byteLength, source.length);
|
|
219
|
+
}));
|
|
220
|
+
|
|
221
|
+
test("NUL suppresses its chunk and all later text without flushing a pending sequence", async () =>
|
|
222
|
+
withFile(async (path) => {
|
|
223
|
+
const chunkBytes = 64 * 1024;
|
|
224
|
+
const prefix = Buffer.alloc(chunkBytes - 1, "a");
|
|
225
|
+
const source = Buffer.concat([
|
|
226
|
+
prefix,
|
|
227
|
+
Buffer.from([0xf0, 0, 0x9f]),
|
|
228
|
+
Buffer.alloc(chunkBytes, "b"),
|
|
229
|
+
]);
|
|
230
|
+
await writeFile(path, source);
|
|
231
|
+
for (const decoding of ["strict", "lossy"] as const) {
|
|
232
|
+
const emitted: string[] = [];
|
|
233
|
+
const scanned: Buffer[] = [];
|
|
234
|
+
const stats = await scanTextFile(
|
|
235
|
+
path,
|
|
236
|
+
(text) => {
|
|
237
|
+
emitted.push(text);
|
|
238
|
+
},
|
|
239
|
+
undefined,
|
|
240
|
+
(bytes) => scanned.push(bytes),
|
|
241
|
+
decoding,
|
|
242
|
+
);
|
|
243
|
+
assert.equal(stats.hasNul, true);
|
|
244
|
+
assert.equal(emitted.join(""), prefix.toString("utf8"));
|
|
245
|
+
assert.deepEqual(Buffer.concat(scanned), source);
|
|
246
|
+
}
|
|
247
|
+
}));
|
|
248
|
+
|
|
249
|
+
test("NUL takes precedence over earlier malformed UTF-8 in strict and lossy scans", async () =>
|
|
250
|
+
withFile(async (path) => {
|
|
251
|
+
const chunkBytes = 64 * 1024;
|
|
252
|
+
const invalid = Buffer.alloc(chunkBytes, "a");
|
|
253
|
+
invalid[0] = 0xff;
|
|
254
|
+
const source = Buffer.concat([invalid, Buffer.from("\0later\n")]);
|
|
255
|
+
await writeFile(path, source);
|
|
256
|
+
for (const decoding of ["strict", "lossy"] as const) {
|
|
257
|
+
const emitted: string[] = [];
|
|
258
|
+
const stats = await scanTextFile(
|
|
259
|
+
path,
|
|
260
|
+
(text) => {
|
|
261
|
+
emitted.push(text);
|
|
262
|
+
},
|
|
263
|
+
undefined,
|
|
264
|
+
undefined,
|
|
265
|
+
decoding,
|
|
266
|
+
);
|
|
267
|
+
assert.equal(stats.hasNul, true);
|
|
268
|
+
assert.equal(stats.validUtf8, false);
|
|
269
|
+
assert.equal(emitted.join(""), decoding === "strict" ? "" : invalid.toString("utf8"));
|
|
270
|
+
}
|
|
271
|
+
}));
|
package/src/pi/text-stream.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { createReadStream } from "node:fs";
|
|
2
|
-
import { createUtf8Decoder } from "../core/text.ts";
|
|
2
|
+
import { createUtf8Decoder, type Utf8Decoding } from "../core/text.ts";
|
|
3
3
|
import { throwIfCancelled } from "./error-text.ts";
|
|
4
4
|
|
|
5
|
-
/** Scan
|
|
5
|
+
/** Scan whole-file bytes; lossy decoding reports validity without rejecting malformed UTF-8. */
|
|
6
6
|
export async function scanTextFile(
|
|
7
7
|
path: string,
|
|
8
8
|
onChunk?: (text: string) => void | Promise<void>,
|
|
9
9
|
signal?: AbortSignal,
|
|
10
10
|
onBytes?: (bytes: Buffer) => void,
|
|
11
|
+
decoding: Utf8Decoding = "strict",
|
|
11
12
|
) {
|
|
12
13
|
const decode = createUtf8Decoder();
|
|
14
|
+
// Strict decoding always decides validity; lossy text is decoded only for a chunk consumer.
|
|
15
|
+
const lossy = decoding === "lossy" && onChunk ? createUtf8Decoder("lossy") : undefined;
|
|
13
16
|
let byteLength = 0;
|
|
14
17
|
let lineFeeds = 0;
|
|
15
18
|
let lastByte = -1;
|
|
@@ -31,15 +34,17 @@ export async function scanTextFile(
|
|
|
31
34
|
offset++;
|
|
32
35
|
}
|
|
33
36
|
// NUL takes precedence even when an earlier chunk contained malformed UTF-8.
|
|
34
|
-
if (hasNul
|
|
35
|
-
let text: string;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
if (hasNul) continue;
|
|
38
|
+
let text: string | undefined;
|
|
39
|
+
if (!decodingError) {
|
|
40
|
+
try {
|
|
41
|
+
text = decode(bytes, true);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
decodingError = error;
|
|
44
|
+
}
|
|
41
45
|
}
|
|
42
|
-
|
|
46
|
+
if (lossy) text = lossy(bytes, true);
|
|
47
|
+
if (text !== undefined) await onChunk?.(text);
|
|
43
48
|
}
|
|
44
49
|
} catch (error) {
|
|
45
50
|
// Node's stream abort raises its own AbortError; report the shared cancellation text.
|
|
@@ -48,8 +53,16 @@ export async function scanTextFile(
|
|
|
48
53
|
}
|
|
49
54
|
throwIfCancelled(signal);
|
|
50
55
|
if (!hasNul) {
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
let tail: string | undefined;
|
|
57
|
+
if (!decodingError) {
|
|
58
|
+
try {
|
|
59
|
+
tail = decode();
|
|
60
|
+
} catch (error) {
|
|
61
|
+
decodingError = error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (lossy) tail = lossy();
|
|
65
|
+
else if (decodingError && decoding === "strict") throw decodingError;
|
|
53
66
|
if (tail) await onChunk?.(tail);
|
|
54
67
|
}
|
|
55
68
|
return {
|
|
@@ -58,6 +71,7 @@ export async function scanTextFile(
|
|
|
58
71
|
finalNewline: lastByte === -1 || lastByte === 10,
|
|
59
72
|
hasNul,
|
|
60
73
|
hasCrLf,
|
|
74
|
+
validUtf8: !decodingError,
|
|
61
75
|
};
|
|
62
76
|
}
|
|
63
77
|
|
|
@@ -74,7 +88,12 @@ export async function scanTextLines(
|
|
|
74
88
|
path: string,
|
|
75
89
|
select: (number: number) => boolean,
|
|
76
90
|
onLine: (line: ScannedLine) => void,
|
|
77
|
-
options: {
|
|
91
|
+
options: {
|
|
92
|
+
signal?: AbortSignal;
|
|
93
|
+
maxLineBytes?: number;
|
|
94
|
+
onBytes?: (bytes: Buffer) => void;
|
|
95
|
+
decoding?: Utf8Decoding;
|
|
96
|
+
} = {},
|
|
78
97
|
) {
|
|
79
98
|
const maxBytes = options.maxLineBytes ?? Infinity;
|
|
80
99
|
let number = 1;
|
|
@@ -130,6 +149,7 @@ export async function scanTextLines(
|
|
|
130
149
|
},
|
|
131
150
|
options.signal,
|
|
132
151
|
options.onBytes,
|
|
152
|
+
options.decoding,
|
|
133
153
|
);
|
|
134
154
|
if (!stats.hasNul && byteLength > 0) finish(false);
|
|
135
155
|
return stats;
|
package/src/pi/write-tool.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { postProcessMutation } from "./mutation-result.ts";
|
|
|
17
17
|
import { executeMutation, type ActionFusionExecutor } from "./mutation-runner.ts";
|
|
18
18
|
import { invalidArgument, throwIfCancelled } from "./error-text.ts";
|
|
19
19
|
import { unwritableTextReason } from "../core/text.ts";
|
|
20
|
+
import { renderToolError } from "./render.ts";
|
|
20
21
|
|
|
21
22
|
const writeSchema = Type.Object(
|
|
22
23
|
{
|
|
@@ -78,6 +79,7 @@ export function makeWriteOverride(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
78
79
|
theme: Theme,
|
|
79
80
|
context: WriteRenderContext,
|
|
80
81
|
) {
|
|
82
|
+
if (context.isError) return renderToolError(result, theme, options.expanded);
|
|
81
83
|
return builtin.renderResult!({ ...result, details: undefined }, options, theme, context);
|
|
82
84
|
},
|
|
83
85
|
async execute(
|
|
@@ -91,8 +93,6 @@ export function makeWriteOverride(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
91
93
|
{
|
|
92
94
|
cwd,
|
|
93
95
|
fusion,
|
|
94
|
-
// Write results carry no anchors; stale revisions are reported by then_run.
|
|
95
|
-
reportsAnchors: false,
|
|
96
96
|
async run(mutationParams, { absolutePath, displayPath, signal }) {
|
|
97
97
|
throwIfCancelled(signal, `before write; ${displayPath} was not changed.`);
|
|
98
98
|
const result = await commitFile(absolutePath, mutationParams.content, {
|
|
@@ -100,6 +100,7 @@ export function makeWriteOverride(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
100
100
|
signal,
|
|
101
101
|
});
|
|
102
102
|
return {
|
|
103
|
+
commit: result,
|
|
103
104
|
result: postProcessMutation("write", result.publication, () => ({
|
|
104
105
|
content: [
|
|
105
106
|
{
|
package/src/pi/write.test.ts
CHANGED
|
@@ -4,9 +4,10 @@ import { join } from "node:path";
|
|
|
4
4
|
import assert from "node:assert/strict";
|
|
5
5
|
import { test } from "node:test";
|
|
6
6
|
import { makeWriteOverride } from "./write-tool.ts";
|
|
7
|
-
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
7
|
+
import { createActionFusionExecutor, THEN_RUN_STALE, THEN_RUN_SUCCEEDED } from "./action-fusion.ts";
|
|
8
8
|
import { fileRevision } from "./file-commit.ts";
|
|
9
9
|
import { callTool } from "./tool-call.testing.ts";
|
|
10
|
+
import { staleTargetNotice } from "./mutation-result.ts";
|
|
10
11
|
|
|
11
12
|
const context = (cwd: string) => ({ cwd }) as any;
|
|
12
13
|
|
|
@@ -193,18 +194,18 @@ test("write reports changed freshness when then_run changes the target", async (
|
|
|
193
194
|
return "checked";
|
|
194
195
|
});
|
|
195
196
|
const write = makeWriteOverride(dir, fusion) as any;
|
|
196
|
-
const result = await
|
|
197
|
-
|
|
197
|
+
const result = await callTool(
|
|
198
|
+
write,
|
|
198
199
|
{ path: "changed.txt", content: "mutation\n", then_run: { command: "check" } },
|
|
199
|
-
|
|
200
|
-
undefined,
|
|
201
|
-
context(dir),
|
|
200
|
+
{ toolCallId: "changed", ctx: context(dir) },
|
|
202
201
|
);
|
|
203
202
|
const text = result.content.map((block: any) => block.text).join("\n");
|
|
204
203
|
assert.doesNotMatch(text, /revision:|[0-9a-f]{64}/i);
|
|
205
|
-
assert.
|
|
206
|
-
|
|
207
|
-
|
|
204
|
+
assert.deepEqual(result.content, [
|
|
205
|
+
{ type: "text", text: "Created changed.txt." },
|
|
206
|
+
{ type: "text", text: staleTargetNotice(THEN_RUN_STALE) },
|
|
207
|
+
{ type: "text", text: `${THEN_RUN_SUCCEEDED}\nchecked` },
|
|
208
|
+
]);
|
|
208
209
|
assert.doesNotMatch(text, /Fresh anchors:|\b1#[0-9A-Z]+\b/);
|
|
209
210
|
assert.equal(result.details.actionFusion.freshness, "changed");
|
|
210
211
|
assert.deepEqual(
|
package/src/core/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Public API of the pi-hashline-edit core library.
|
|
3
|
-
*
|
|
4
|
-
* The pure hashline engine, with zero pi dependencies, runnable standalone via
|
|
5
|
-
* `node --test`. The pi integration layer lives under `../pi/`.
|
|
6
|
-
*
|
|
7
|
-
* @module pi-hashline-edit/core
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export * from "./types.ts";
|
|
11
|
-
export { computeLineHash, hashFileLines } from "./hash.ts";
|
|
12
|
-
export { splitLines, detectLineEnding, hasFinalNewline } from "./lines.ts";
|
|
13
|
-
export { decodeUtf8, decodeEditableText } from "./text.ts";
|
|
14
|
-
export { applyEdits } from "./apply.ts";
|
package/src/pi/state.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session-level config holder.
|
|
3
|
-
*
|
|
4
|
-
* globalThis singleton (consistent with the repo's module-identity guidance).
|
|
5
|
-
* Config is loaded when the extension registers and read by the tool
|
|
6
|
-
* overrides.
|
|
7
|
-
*
|
|
8
|
-
* @module pi-hashline-edit/pi
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { DEFAULT_CONFIG, type HashlineEditConfig } from "./config.ts";
|
|
12
|
-
|
|
13
|
-
const GLOBAL_KEY = "__piHashlineEdit";
|
|
14
|
-
|
|
15
|
-
export interface HashlineEditState {
|
|
16
|
-
config: HashlineEditConfig;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function getState(): HashlineEditState {
|
|
20
|
-
const g = globalThis as Record<string, unknown>;
|
|
21
|
-
const existing = g[GLOBAL_KEY];
|
|
22
|
-
if (existing) return existing as HashlineEditState;
|
|
23
|
-
const state: HashlineEditState = { config: { ...DEFAULT_CONFIG } };
|
|
24
|
-
g[GLOBAL_KEY] = state;
|
|
25
|
-
return state;
|
|
26
|
-
}
|