@criogaid/pi-hashline-edit 0.8.0 → 0.10.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 +13 -10
- package/README.zh-CN.md +12 -9
- 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 +118 -293
- package/src/core/apply.ts +63 -165
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.ts +4 -4
- 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 +16 -8
- package/src/core/ranges.ts +10 -6
- 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 +7 -43
- package/src/integration/config-registration.test.ts +2 -2
- package/src/integration/fusion-dispatch.test.ts +2 -5
- package/src/integration/grep-flags.test.ts +0 -14
- package/src/integration/grep-js-completeness.test.ts +34 -8
- package/src/integration/grep-line-candidates.test.ts +4 -8
- package/src/integration/grep-matrix.test.ts +3 -7
- package/src/integration/grep-path-matrix.test.ts +5 -5
- package/src/integration/grep-prefilter-coverage.test.ts +3 -7
- package/src/integration/grep-process-limits.test.ts +78 -0
- package/src/integration/grep-rg.test.ts +123 -25
- package/src/integration/grep-scan-budget.test.ts +2 -2
- package/src/integration/grep-translation.test.ts +12 -27
- package/src/integration/large-text.test.ts +1 -47
- package/src/integration/model-budget-lifecycle.test.ts +13 -3
- package/src/integration/mutation-file-types.testing.ts +1 -1
- package/src/integration/npm-package.testing.ts +1 -1
- package/src/integration/publication-read-changes.test.ts +125 -47
- package/src/integration/replace-worker-boundary.test.ts +21 -11
- package/src/integration/report-replay.test.ts +21 -23
- package/src/integration/utf8-errors.test.ts +1 -1
- package/src/pi/AGENTS.md +16 -16
- package/src/pi/action-fusion.test.ts +168 -300
- package/src/pi/action-fusion.ts +148 -111
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +52 -207
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +20 -22
- package/src/pi/argument-validation.test.ts +382 -418
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +5 -4
- package/src/pi/config.test.ts +5 -2
- package/src/pi/config.ts +4 -6
- package/src/pi/diagnostic-buffer.test.ts +12 -1
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +173 -226
- package/src/pi/error-text.ts +4 -8
- package/src/pi/execute.test.ts +165 -266
- package/src/pi/failure-context.test.ts +144 -126
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +168 -141
- package/src/pi/file-commit.test.ts +110 -53
- package/src/pi/file-commit.ts +90 -137
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +4 -1
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +2 -101
- package/src/pi/grep-candidates.ts +23 -69
- package/src/pi/grep-matrix.test.ts +2 -2
- package/src/pi/grep-order.test.ts +4 -4
- package/src/pi/grep-output.ts +62 -96
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +92 -0
- package/src/pi/grep-scope.ts +40 -42
- package/src/pi/grep-search.ts +22 -31
- package/src/pi/grep-tool.ts +55 -82
- package/src/pi/grep.test.ts +81 -46
- package/src/pi/model-budgets.test.ts +25 -23
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +51 -20
- package/src/pi/mutation-runner.ts +126 -70
- package/src/pi/output-budgets.test.ts +38 -16
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +3 -1
- package/src/pi/read-tool.ts +5 -5
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +82 -20
- package/src/pi/render.test.ts +79 -42
- package/src/pi/render.ts +69 -63
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -83
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +9 -5
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +1 -2
- package/src/pi/report-schema.ts +65 -81
- package/src/pi/report.test.ts +171 -25
- package/src/pi/report.ts +33 -87
- package/src/pi/result-safety.test.ts +177 -189
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +48 -64
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +12 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/write-tool.ts +33 -62
- package/src/pi/write.test.ts +1 -3
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/pi/replace-worker-contract.ts +0 -10
package/src/pi/report.test.ts
CHANGED
|
@@ -3,10 +3,13 @@ import { test } from "node:test";
|
|
|
3
3
|
import { stripVTControlCharacters } from "node:util";
|
|
4
4
|
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
5
5
|
import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
|
|
6
|
+
import { HashlineError, PATH_ERROR_MESSAGES } from "../core/errors.ts";
|
|
7
|
+
import { FileMutationError } from "./file-commit.ts";
|
|
8
|
+
import { describeMutationFailure } from "./mutation-result.ts";
|
|
6
9
|
import { formatArgumentError } from "./argument-error.ts";
|
|
7
10
|
import { renderToolError, splitReport } from "./render.ts";
|
|
11
|
+
import { causeOf } from "./report-cause.ts";
|
|
8
12
|
import {
|
|
9
|
-
causeOf,
|
|
10
13
|
decodeReport,
|
|
11
14
|
failureResult,
|
|
12
15
|
reportOf,
|
|
@@ -16,15 +19,23 @@ import {
|
|
|
16
19
|
} from "./report.ts";
|
|
17
20
|
import { DEFAULT_OUTPUT_BUDGETS, formatByteSize } from "./budgets.ts";
|
|
18
21
|
import { MAX_UNNARROWED_SCAN_BYTES } from "./grep-candidates.ts";
|
|
19
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
OUTCOME_NEXT,
|
|
24
|
+
REREAD,
|
|
25
|
+
type Report,
|
|
26
|
+
type ReportTool,
|
|
27
|
+
type SuccessReport,
|
|
28
|
+
} from "./report-schema.ts";
|
|
20
29
|
|
|
21
30
|
// npm run typecheck checks these invalid callers; they must never run.
|
|
22
31
|
function invalidReportConstruction(report: NonNullable<ReturnType<typeof decodeReport>>) {
|
|
23
32
|
// @ts-expect-error Grep successes cannot contain write-only facts.
|
|
24
33
|
reportResult({ tool: "grep", created: true });
|
|
25
|
-
// @ts-expect-error NO_MATCH requires its
|
|
34
|
+
// @ts-expect-error NO_MATCH requires its unmatched facts.
|
|
26
35
|
failureResult({ tool: "replace", error: "NO_MATCH", message: "No replacement matched." });
|
|
27
36
|
if (report.error === "NO_MATCH") {
|
|
37
|
+
const unmatched: string[] = report.unmatched;
|
|
38
|
+
// @ts-expect-error NO_MATCH no longer carries a single rule.
|
|
28
39
|
const rule: number = report.rule;
|
|
29
40
|
// @ts-expect-error NO_MATCH cannot carry a search-total fact.
|
|
30
41
|
const matches: number = report.matches;
|
|
@@ -142,19 +153,23 @@ test("native failure with deeply nested cause → retains the terminal diagnosti
|
|
|
142
153
|
assert.doesNotMatch(serialized, /"omitted"/);
|
|
143
154
|
});
|
|
144
155
|
|
|
145
|
-
test("
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
test("aggregate and cyclic causes → preserve only the linear cause chain", () => {
|
|
157
|
+
const terminal = new Error("terminal");
|
|
158
|
+
const aggregate = new AggregateError([terminal, terminal, new Error("ignored")], "aggregate", {
|
|
159
|
+
cause: terminal,
|
|
160
|
+
});
|
|
161
|
+
const self = new Error("self");
|
|
162
|
+
self.cause = self;
|
|
163
|
+
const first = new Error("first");
|
|
164
|
+
const second = new Error("second", { cause: first });
|
|
165
|
+
first.cause = second;
|
|
166
|
+
for (const [input, expected] of [
|
|
167
|
+
[aggregate, { message: "aggregate", cause: { message: "terminal" } }],
|
|
168
|
+
[self, { message: "self" }],
|
|
169
|
+
[first, { message: "first", cause: { message: "second" } }],
|
|
170
|
+
] as const) {
|
|
171
|
+
assert.deepEqual(causeOf(input, DEFAULT_OUTPUT_BUDGETS.fieldBytes), expected);
|
|
172
|
+
}
|
|
158
173
|
});
|
|
159
174
|
|
|
160
175
|
test("oversized cause message → retains opening and terminal text within the message budget", () => {
|
|
@@ -186,6 +201,50 @@ test("native filesystem failure → reports its diagnostic once without a synthe
|
|
|
186
201
|
}
|
|
187
202
|
});
|
|
188
203
|
|
|
204
|
+
test("native path errors → classify fixed messages and retain the missing path", () => {
|
|
205
|
+
for (const [code, expected, missing] of [
|
|
206
|
+
["ENOENT", "PATH_NOT_FOUND", ["resolved/source"]],
|
|
207
|
+
["EISDIR", "NOT_REGULAR_FILE", undefined],
|
|
208
|
+
] as const) {
|
|
209
|
+
const native = Object.assign(new Error(`${code}: native diagnostic`), {
|
|
210
|
+
code,
|
|
211
|
+
path: "resolved/source",
|
|
212
|
+
});
|
|
213
|
+
const report = describeFailure("read", native, {
|
|
214
|
+
path: "source",
|
|
215
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
216
|
+
});
|
|
217
|
+
assert.equal(report.error, expected);
|
|
218
|
+
assert.equal(report.message, PATH_ERROR_MESSAGES[expected]);
|
|
219
|
+
assert.deepEqual(report.missing, missing);
|
|
220
|
+
assert.equal(report.cause, undefined);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("mutation publication → recovery reflects whether the change was saved", () => {
|
|
225
|
+
for (const [publication, expected] of [
|
|
226
|
+
["NOT_PUBLISHED", undefined],
|
|
227
|
+
["PUBLISHED", "The change is saved; do not repeat it. Read the file before further edits."],
|
|
228
|
+
[
|
|
229
|
+
"UNKNOWN",
|
|
230
|
+
"The change may or may not be saved. Read the file, and repeat the change only if it is missing.",
|
|
231
|
+
],
|
|
232
|
+
] as const) {
|
|
233
|
+
const error =
|
|
234
|
+
publication === "NOT_PUBLISHED"
|
|
235
|
+
? new HashlineError("PUBLISH_FAILED", "failed")
|
|
236
|
+
: new FileMutationError(
|
|
237
|
+
publication === "PUBLISHED" ? "POST_PROCESS_FAILED" : "PUBLISH_FAILED",
|
|
238
|
+
publication,
|
|
239
|
+
"failed",
|
|
240
|
+
);
|
|
241
|
+
const report = describeMutationFailure("edit", error, { budget: DEFAULT_OUTPUT_BUDGETS });
|
|
242
|
+
assert.equal(report.publication, publication);
|
|
243
|
+
assert.equal(report.next, expected);
|
|
244
|
+
assert.equal(decodeReport(renderReport(report), "edit")?.next, expected);
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
189
248
|
test("success reports → omit repeated call fields while retaining details and payload", () => {
|
|
190
249
|
const report: SuccessReport = {
|
|
191
250
|
tool: "write",
|
|
@@ -246,14 +305,34 @@ test("report-contract: native failure with an independent cause → retains the
|
|
|
246
305
|
assert.equal(report.cause?.code, "EIO");
|
|
247
306
|
});
|
|
248
307
|
|
|
308
|
+
test("replacement failure facts → survive report serialization", () => {
|
|
309
|
+
for (const report of [
|
|
310
|
+
{
|
|
311
|
+
tool: "replace",
|
|
312
|
+
error: "NO_MATCH",
|
|
313
|
+
message: "No match",
|
|
314
|
+
unmatched: ["replacements[0]", "replacements[2]"],
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
tool: "replace",
|
|
318
|
+
error: "OVERLAPPING_MATCHES",
|
|
319
|
+
message: "Overlap",
|
|
320
|
+
conflict: ["replacements[0]", "replacements[1]"],
|
|
321
|
+
line: 2,
|
|
322
|
+
},
|
|
323
|
+
] satisfies Report[]) {
|
|
324
|
+
assert.deepEqual(decodeReport(renderReport(report), "replace"), report);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
|
|
249
328
|
test("grep success and incomplete candidate facts → survive report serialization", () => {
|
|
250
329
|
for (const report of [
|
|
251
330
|
{ tool: "grep", matches: 1 },
|
|
252
|
-
{ tool: "grep", matches: 1,
|
|
331
|
+
{ tool: "grep", matches: 1, scanned: "files" },
|
|
253
332
|
{
|
|
254
333
|
tool: "grep",
|
|
255
334
|
matches: 0,
|
|
256
|
-
|
|
335
|
+
scanned: "all",
|
|
257
336
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
258
337
|
},
|
|
259
338
|
{
|
|
@@ -261,7 +340,7 @@ test("grep success and incomplete candidate facts → survive report serializati
|
|
|
261
340
|
error: "SEARCH_INCOMPLETE",
|
|
262
341
|
message: "Search incomplete.",
|
|
263
342
|
diagnostics: "unreadable file",
|
|
264
|
-
|
|
343
|
+
scanned: "all",
|
|
265
344
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
266
345
|
},
|
|
267
346
|
] satisfies Report[]) {
|
|
@@ -278,12 +357,41 @@ test("match and scan limits → recovery prioritizes the match limit", () => {
|
|
|
278
357
|
tool: "grep",
|
|
279
358
|
matches: 1,
|
|
280
359
|
matchLimit: 1,
|
|
281
|
-
|
|
360
|
+
scanned: "all",
|
|
282
361
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
283
362
|
});
|
|
284
363
|
assert.equal(reportOf(result, "grep")?.next, next);
|
|
285
364
|
});
|
|
286
365
|
|
|
366
|
+
for (const { name, report, fact } of [
|
|
367
|
+
{
|
|
368
|
+
name: "output limit with grep match limit → recovery prioritizes the output limit",
|
|
369
|
+
report: {
|
|
370
|
+
tool: "grep",
|
|
371
|
+
matches: 1,
|
|
372
|
+
matchLimit: 1,
|
|
373
|
+
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.grepBytes),
|
|
374
|
+
},
|
|
375
|
+
fact: "outputLimit",
|
|
376
|
+
},
|
|
377
|
+
{
|
|
378
|
+
name: "output limit with read pagination → recovery prioritizes pagination",
|
|
379
|
+
report: {
|
|
380
|
+
tool: "read",
|
|
381
|
+
pagination: { start: 1, end: 1, totalLines: 2, nextOffset: 2 },
|
|
382
|
+
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.readBytes),
|
|
383
|
+
},
|
|
384
|
+
fact: "pagination",
|
|
385
|
+
},
|
|
386
|
+
] satisfies { name: string; report: SuccessReport; fact: "outputLimit" | "pagination" }[]) {
|
|
387
|
+
test(name, () => {
|
|
388
|
+
const next = OUTCOME_NEXT.find(([key]) => key === fact)?.[1];
|
|
389
|
+
assert.ok(next);
|
|
390
|
+
const result = reportResult(report);
|
|
391
|
+
assert.equal(reportOf(result, report.tool)?.next, next);
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
287
395
|
test("text-only error contains facts invalid for the originating tool → preserves the raw diagnostic", () => {
|
|
288
396
|
const diagnostic = '{"publication":"PUBLISHED"}';
|
|
289
397
|
const result = { content: [text(diagnostic)] };
|
|
@@ -305,13 +413,13 @@ test("invalid details report → falls back to valid text and leaves content int
|
|
|
305
413
|
|
|
306
414
|
test("output byte limit → follows the scan limit and precedes the line preview limit", () => {
|
|
307
415
|
const serialized = renderReport({
|
|
308
|
-
|
|
416
|
+
scanned: "all",
|
|
309
417
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
310
418
|
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.grepBytes),
|
|
311
419
|
linePreviewLimit: DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
|
|
312
420
|
});
|
|
313
421
|
assert.deepEqual(Object.keys(JSON.parse(serialized)), [
|
|
314
|
-
"
|
|
422
|
+
"scanned",
|
|
315
423
|
"scanLimit",
|
|
316
424
|
"outputLimit",
|
|
317
425
|
"linePreviewLimit",
|
|
@@ -332,12 +440,50 @@ test("hooked details contain a cyclic report cause → preserves payload and dec
|
|
|
332
440
|
assert.deepEqual(splitReport(result), { content, report: undefined });
|
|
333
441
|
});
|
|
334
442
|
|
|
335
|
-
test("native mutation failure before commit → retains publication
|
|
443
|
+
test("native mutation failure before commit → retains publication", () => {
|
|
336
444
|
for (const tool of ["edit", "replace", "write"] as const) {
|
|
337
|
-
const report =
|
|
445
|
+
const report = describeMutationFailure(tool, new Error("native failure"), {
|
|
338
446
|
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
339
447
|
});
|
|
340
448
|
assert.equal(report.publication, "NOT_PUBLISHED", tool);
|
|
341
|
-
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
test("failure classification without mutation facts → omits mutation state for every tool", () => {
|
|
453
|
+
for (const tool of ["read", "grep", "edit", "replace", "write"] as const) {
|
|
454
|
+
const error =
|
|
455
|
+
tool === "read" || tool === "grep"
|
|
456
|
+
? new Error("native failure")
|
|
457
|
+
: new FileMutationError("POST_PROCESS_FAILED", "PUBLISHED", "result generation failed");
|
|
458
|
+
const report = describeFailure(tool, error, { budget: DEFAULT_OUTPUT_BUDGETS });
|
|
459
|
+
assert.equal(
|
|
460
|
+
report.error,
|
|
461
|
+
error instanceof FileMutationError ? "POST_PROCESS_FAILED" : "UNCLASSIFIED",
|
|
462
|
+
);
|
|
463
|
+
assert.equal(report.publication, undefined);
|
|
464
|
+
assert.equal(report.then_run, undefined);
|
|
465
|
+
assert.equal(report.progressError, undefined);
|
|
466
|
+
assert.equal(report.next, undefined);
|
|
467
|
+
const serialized: unknown = JSON.parse(renderReport(report));
|
|
468
|
+
assert.ok(typeof serialized === "object" && serialized !== null);
|
|
469
|
+
for (const field of ["publication", "then_run", "progressError", "next"])
|
|
470
|
+
assert.equal(Object.hasOwn(serialized, field), false);
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
test("mutation freshness → retains the fact and requests reread only for an existing or unknown target", () => {
|
|
475
|
+
for (const [freshness, next] of [
|
|
476
|
+
["missing", undefined],
|
|
477
|
+
["changed", REREAD],
|
|
478
|
+
["unknown", REREAD],
|
|
479
|
+
] as const) {
|
|
480
|
+
const result = reportResult({ tool: "edit", publication: "PUBLISHED", freshness });
|
|
481
|
+
const report = reportOf(result, "edit");
|
|
482
|
+
assert.equal(report?.freshness, freshness);
|
|
483
|
+
assert.equal(report?.next, next);
|
|
484
|
+
const block = result.content.at(-1);
|
|
485
|
+
assert.ok(block?.type === "text");
|
|
486
|
+
assert.deepEqual(decodeReport(block.text, "edit"), report);
|
|
487
|
+
assert.equal(Object.hasOwn(JSON.parse(block.text), "next"), next !== undefined);
|
|
342
488
|
}
|
|
343
489
|
});
|
package/src/pi/report.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Every result of every tool states its facts in one report (report-schema.ts).
|
|
5
5
|
* Layers return structured facts and never append sentences: the commit layer
|
|
6
|
-
* owns publication
|
|
6
|
+
* owns publication and freshness; each tool owns its outcome facts;
|
|
7
7
|
* Action Fusion owns `then_run`. This module builds the
|
|
8
8
|
* report of a success or a failure, derives its single `next`, and owns the
|
|
9
9
|
* one model-facing serialization: one JSON object in schema order, without the
|
|
@@ -18,21 +18,22 @@
|
|
|
18
18
|
|
|
19
19
|
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
20
20
|
import { Value } from "typebox/value";
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
errorMessage,
|
|
23
|
+
errnoCode,
|
|
24
|
+
filesystemErrorCode,
|
|
25
|
+
HashlineError,
|
|
26
|
+
pathError,
|
|
27
|
+
} from "../core/errors.ts";
|
|
22
28
|
import { boundedText, causeOf } from "./report-cause.ts";
|
|
23
29
|
import type { OutputBudgets } from "./budgets.ts";
|
|
24
|
-
export { boundedText, causeOf } from "./report-cause.ts";
|
|
25
30
|
import { cancellationError } from "./error-text.ts";
|
|
26
|
-
import { FileMutationError } from "./file-commit.ts";
|
|
27
31
|
import {
|
|
28
32
|
ERROR_DEFINITIONS,
|
|
29
|
-
MUTATION_TOOLS,
|
|
30
33
|
OUTCOME_NEXT,
|
|
31
34
|
PUBLICATION_NEXT,
|
|
32
35
|
REPORT_SCHEMA,
|
|
33
36
|
WIRE_REPORT_SCHEMA,
|
|
34
|
-
type Cause,
|
|
35
|
-
type CommandStatus,
|
|
36
37
|
type ErrorCode,
|
|
37
38
|
type FailureReport,
|
|
38
39
|
type Report,
|
|
@@ -40,8 +41,6 @@ import {
|
|
|
40
41
|
type SuccessReport,
|
|
41
42
|
} from "./report-schema.ts";
|
|
42
43
|
|
|
43
|
-
export type { Report } from "./report-schema.ts";
|
|
44
|
-
|
|
45
44
|
/** Every result's details carry its report for the TUI; Pi does not send details to the model. */
|
|
46
45
|
export interface ReportDetails<TReport extends Report = Report> {
|
|
47
46
|
readonly report: TReport;
|
|
@@ -54,6 +53,9 @@ export type ReportText<R extends Report = Report> = R extends unknown
|
|
|
54
53
|
? Omit<R, "tool" | "path">
|
|
55
54
|
: never;
|
|
56
55
|
|
|
56
|
+
/** Fields the originating call supplies, which a result's text never repeats. */
|
|
57
|
+
const CALL_FIELDS = new Set(["tool", "path"]);
|
|
58
|
+
|
|
57
59
|
/** The fields a report states, in schema order; both renderers list them in this order. */
|
|
58
60
|
export function reportEntries(report: ReportText): [string, unknown][] {
|
|
59
61
|
return Object.keys(REPORT_SCHEMA.properties).flatMap((key) => {
|
|
@@ -64,7 +66,7 @@ export function reportEntries(report: ReportText): [string, unknown][] {
|
|
|
64
66
|
|
|
65
67
|
/** Serialize facts in schema order; a result never repeats its call's tool and path. */
|
|
66
68
|
export function renderReport(report: ReportText): string {
|
|
67
|
-
const entries = reportEntries(report).filter(([key]) => !
|
|
69
|
+
const entries = reportEntries(report).filter(([key]) => !CALL_FIELDS.has(key));
|
|
68
70
|
return JSON.stringify(Object.fromEntries(entries), null, 2);
|
|
69
71
|
}
|
|
70
72
|
|
|
@@ -104,20 +106,20 @@ export function detailsReport(details: unknown): Report | undefined {
|
|
|
104
106
|
|
|
105
107
|
/** Tool results carry their report in details; Pi argument failures carry it as their final text block. */
|
|
106
108
|
export function reportOf(
|
|
107
|
-
result: { readonly content
|
|
109
|
+
result: { readonly content: Content; readonly details?: unknown },
|
|
108
110
|
tool: ReportTool,
|
|
109
111
|
): Report | undefined {
|
|
110
112
|
const report = detailsReport(result.details);
|
|
111
113
|
if (report) return report;
|
|
112
|
-
const last = result.content
|
|
114
|
+
const last = result.content.at(-1);
|
|
113
115
|
return last?.type === "text" ? decodeReport(last.text, tool) : undefined;
|
|
114
116
|
}
|
|
115
117
|
|
|
116
|
-
const ENVELOPE = new Set(["tool", "path"]);
|
|
117
|
-
|
|
118
118
|
/** The recovery a successful result's facts call for: the first in OUTCOME_NEXT order. */
|
|
119
119
|
function outcomeNext(report: Report): string | undefined {
|
|
120
|
-
return OUTCOME_NEXT.find(
|
|
120
|
+
return OUTCOME_NEXT.find(
|
|
121
|
+
([fact, , except]) => report[fact] !== undefined && report[fact] !== except,
|
|
122
|
+
)?.[1];
|
|
121
123
|
}
|
|
122
124
|
|
|
123
125
|
/**
|
|
@@ -132,9 +134,7 @@ export function reportResult<TDetails extends object | undefined>(
|
|
|
132
134
|
const complete = Object.fromEntries(
|
|
133
135
|
reportEntries({ ...report, next: outcomeNext(report) }),
|
|
134
136
|
) as SuccessReport;
|
|
135
|
-
const stated = Object.
|
|
136
|
-
([key, value]) => value !== undefined && !ENVELOPE.has(key),
|
|
137
|
-
);
|
|
137
|
+
const stated = Object.keys(complete).some((key) => !CALL_FIELDS.has(key));
|
|
138
138
|
return {
|
|
139
139
|
content:
|
|
140
140
|
stated || payload.length === 0
|
|
@@ -157,46 +157,28 @@ export function failureResult(
|
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
/** Where a failure happened, as the caller knows it. */
|
|
160
|
-
interface FailureContext {
|
|
160
|
+
export interface FailureContext {
|
|
161
161
|
/** The path or paths the call names, as supplied. */
|
|
162
162
|
readonly path?: string | readonly string[];
|
|
163
163
|
readonly signal?: AbortSignal;
|
|
164
164
|
readonly budget: OutputBudgets;
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
/**
|
|
168
|
-
|
|
169
|
-
* Fusion owns the command status; the mutation's own failure stays unchanged.
|
|
170
|
-
* It lives here because describeFailure unwraps it, and action-fusion.ts already
|
|
171
|
-
* imports this module.
|
|
172
|
-
*/
|
|
173
|
-
export class CommandNotRun extends Error {
|
|
174
|
-
readonly status: Extract<CommandStatus, "skipped" | "cancelled">;
|
|
175
|
-
readonly progressError: Cause | undefined;
|
|
176
|
-
|
|
177
|
-
constructor(
|
|
178
|
-
error: unknown,
|
|
179
|
-
status: Extract<CommandStatus, "skipped" | "cancelled">,
|
|
180
|
-
progressError?: Cause,
|
|
181
|
-
) {
|
|
182
|
-
super(errorMessage(error), { cause: error });
|
|
183
|
-
this.name = "CommandNotRun";
|
|
184
|
-
this.status = status;
|
|
185
|
-
this.progressError = progressError;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
167
|
+
/** What a mutation tool states about its failure beside the classified error. */
|
|
168
|
+
type MutationFailureFacts = Pick<FailureReport, "publication" | "then_run" | "progressError">;
|
|
188
169
|
|
|
189
170
|
function classify(error: unknown, signal: AbortSignal | undefined): HashlineError {
|
|
190
171
|
if (error instanceof HashlineError) return error;
|
|
191
172
|
// Pi's native tools and Node report cancellation with their own errors.
|
|
192
173
|
if (signal?.aborted) return cancellationError();
|
|
174
|
+
const path = pathError(error);
|
|
175
|
+
if (path) return path;
|
|
193
176
|
const code: ErrorCode = filesystemErrorCode(error) ?? "UNCLASSIFIED";
|
|
177
|
+
const cause = error instanceof Error ? error.cause : undefined;
|
|
194
178
|
const diagnostic = errorMessage(error);
|
|
195
179
|
const errno = errnoCode(error);
|
|
196
180
|
const message = errno && !diagnostic.includes(errno) ? `${errno}: ${diagnostic}` : diagnostic;
|
|
197
|
-
return new HashlineError(code, message, {
|
|
198
|
-
cause: error instanceof Error ? error.cause : undefined,
|
|
199
|
-
});
|
|
181
|
+
return new HashlineError(code, message, { cause });
|
|
200
182
|
}
|
|
201
183
|
|
|
202
184
|
/** The recovery of a failure: publication first, then the throw site's alternative or the code's default. */
|
|
@@ -215,40 +197,22 @@ export function describeFailure(
|
|
|
215
197
|
tool: ReportTool,
|
|
216
198
|
caught: unknown,
|
|
217
199
|
context: FailureContext,
|
|
200
|
+
mutation?: MutationFailureFacts,
|
|
218
201
|
): FailureReport {
|
|
219
|
-
|
|
220
|
-
let command: CommandNotRun | undefined;
|
|
221
|
-
if (error instanceof CommandNotRun) {
|
|
222
|
-
command = error;
|
|
223
|
-
// The mutation's own failure, unchanged.
|
|
224
|
-
error = error.cause;
|
|
225
|
-
}
|
|
226
|
-
const failure = classify(error, context.signal);
|
|
202
|
+
const failure = classify(caught, context.signal);
|
|
227
203
|
const budget = context.budget;
|
|
228
|
-
const mutation = MUTATION_TOOLS.some((name) => name === tool);
|
|
229
|
-
const publication = mutation
|
|
230
|
-
? failure instanceof FileMutationError
|
|
231
|
-
? failure.publication
|
|
232
|
-
: ("NOT_PUBLISHED" as const)
|
|
233
|
-
: undefined;
|
|
234
|
-
const stage = mutation
|
|
235
|
-
? failure instanceof FileMutationError
|
|
236
|
-
? failure.stage
|
|
237
|
-
: ("prepare" as const)
|
|
238
|
-
: undefined;
|
|
239
204
|
// HashlineError's constructor ties its facts to its code; the union cannot carry that link.
|
|
240
205
|
return {
|
|
241
206
|
error: failure.errorCode,
|
|
242
207
|
tool,
|
|
243
208
|
path: context.path as Report["path"],
|
|
244
|
-
publication,
|
|
245
|
-
stage,
|
|
209
|
+
publication: mutation?.publication,
|
|
246
210
|
message: boundedText(failure.message, budget.fieldBytes),
|
|
247
211
|
cause: causeOf(failure.cause, budget.fieldBytes),
|
|
248
212
|
...failure.facts,
|
|
249
|
-
then_run:
|
|
250
|
-
progressError:
|
|
251
|
-
next: failureNext(failure, publication),
|
|
213
|
+
then_run: mutation?.then_run,
|
|
214
|
+
progressError: mutation?.progressError,
|
|
215
|
+
next: failureNext(failure, mutation?.publication),
|
|
252
216
|
} as FailureReport;
|
|
253
217
|
}
|
|
254
218
|
|
|
@@ -257,29 +221,11 @@ export async function runTool<T>(
|
|
|
257
221
|
tool: ReportTool,
|
|
258
222
|
context: FailureContext,
|
|
259
223
|
run: () => Promise<T>,
|
|
224
|
+
describe: typeof describeFailure = describeFailure,
|
|
260
225
|
): Promise<T | AgentToolResult<ReportDetails<FailureReport>>> {
|
|
261
226
|
try {
|
|
262
227
|
return await run();
|
|
263
228
|
} catch (error) {
|
|
264
|
-
return failureResult(
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Keep the leading entries of a fact list whose compact JSON fits `maxBytes`;
|
|
270
|
-
* report how many were omitted. Whole entries are omitted, never cut.
|
|
271
|
-
*/
|
|
272
|
-
export function boundedFacts<T>(
|
|
273
|
-
entries: readonly T[],
|
|
274
|
-
maxBytes: number,
|
|
275
|
-
): { kept: T[]; omitted: number } {
|
|
276
|
-
const kept: T[] = [];
|
|
277
|
-
let bytes = 0;
|
|
278
|
-
for (const entry of entries) {
|
|
279
|
-
const entryBytes = Buffer.byteLength(JSON.stringify(entry)) + 1;
|
|
280
|
-
if (bytes + entryBytes > maxBytes) break;
|
|
281
|
-
kept.push(entry);
|
|
282
|
-
bytes += entryBytes;
|
|
229
|
+
return failureResult(describe(tool, error, context));
|
|
283
230
|
}
|
|
284
|
-
return { kept, omitted: entries.length - kept.length };
|
|
285
231
|
}
|