@criogaid/pi-hashline-edit 0.10.0 → 0.12.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 +21 -7
- package/README.zh-CN.md +22 -8
- package/package.json +7 -6
- package/src/core/AGENTS.md +2 -2
- package/src/core/apply.test.ts +4 -4
- package/src/core/apply.ts +3 -3
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +1 -1
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +6 -2
- package/src/core/replace.test.ts +51 -0
- package/src/core/replace.ts +10 -4
- package/src/core/types.ts +1 -1
- package/src/index.ts +9 -9
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +2 -2
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -5
- package/src/integration/grep-js-completeness.test.ts +12 -11
- package/src/integration/grep-line-candidates.test.ts +5 -3
- package/src/integration/grep-matrix.test.ts +5 -4
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +5 -4
- package/src/integration/grep-prefilter-coverage.test.ts +17 -5
- package/src/integration/grep-process-limits.test.ts +2 -4
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +93 -48
- package/src/integration/grep-scan-budget.test.ts +8 -8
- package/src/integration/grep-translation.test.ts +8 -7
- package/src/integration/large-text.test.ts +5 -5
- package/src/integration/model-budget-lifecycle.test.ts +9 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.test.ts +12 -10
- package/src/integration/npm-package.testing.ts +151 -4
- package/src/integration/publication-read-changes.test.ts +54 -54
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +1 -1
- package/src/integration/report-replay.test.ts +9 -6
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +13 -12
- package/src/pi/action-fusion.test.ts +125 -105
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +9 -21
- package/src/pi/argument-diagnostics.ts +4 -3
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +281 -139
- package/src/pi/argument-validation.ts +21 -31
- package/src/pi/budgets.ts +1 -4
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +2 -1
- package/src/pi/config.ts +8 -3
- package/src/pi/diagnostic-buffer.test.ts +23 -13
- package/src/pi/edit-tool.ts +48 -63
- package/src/pi/error-text.ts +7 -2
- package/src/pi/execute.test.ts +178 -157
- package/src/pi/failure-context.test.ts +5 -4
- package/src/pi/failure-context.ts +1 -1
- package/src/pi/failure-path.test.ts +37 -37
- package/src/pi/file-commit.test.ts +47 -19
- package/src/pi/file-commit.ts +36 -9
- package/src/pi/fusion-card.test.ts +22 -21
- package/src/pi/fusion-card.ts +9 -5
- package/src/pi/grep-candidates.test.ts +18 -20
- package/src/pi/grep-candidates.ts +13 -17
- package/src/pi/grep-matrix.test.ts +3 -3
- package/src/pi/grep-order.test.ts +12 -11
- package/src/pi/grep-output.ts +45 -17
- package/src/pi/grep-reread.ts +7 -2
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +10 -10
- package/src/pi/grep-search.ts +8 -11
- package/src/pi/grep-tool.ts +40 -53
- package/src/pi/grep.test.ts +141 -116
- package/src/pi/model-budgets.test.ts +2 -1
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +8 -8
- package/src/pi/mutation-result.ts +1 -1
- package/src/pi/mutation-runner.ts +10 -6
- package/src/pi/output-budgets.test.ts +53 -31
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +13 -18
- package/src/pi/read.test.ts +67 -41
- package/src/pi/registration.test.ts +25 -19
- package/src/pi/render.test.ts +94 -32
- package/src/pi/render.ts +7 -4
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +6 -16
- package/src/pi/replace.test.ts +45 -34
- package/src/pi/report-failure.testing.ts +9 -1
- package/src/pi/report-schema.ts +225 -86
- package/src/pi/report.test.ts +81 -87
- package/src/pi/report.ts +35 -22
- package/src/pi/result-safety.test.ts +121 -100
- package/src/pi/rg-process.test.ts +2 -2
- package/src/pi/rg-process.ts +9 -15
- package/src/pi/rg-regex.test.ts +13 -0
- package/src/pi/rg-regex.ts +2 -2
- package/src/pi/schema.ts +23 -0
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +9 -15
- package/src/pi/write.test.ts +30 -31
- package/src/testing/run-tests.ts +1 -1
package/src/pi/report.test.ts
CHANGED
|
@@ -9,30 +9,28 @@ import { describeMutationFailure } from "./mutation-result.ts";
|
|
|
9
9
|
import { formatArgumentError } from "./argument-error.ts";
|
|
10
10
|
import { renderToolError, splitReport } from "./render.ts";
|
|
11
11
|
import { causeOf } from "./report-cause.ts";
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
reportOf,
|
|
16
|
-
reportResult,
|
|
17
|
-
describeFailure,
|
|
18
|
-
renderReport,
|
|
19
|
-
} from "./report.ts";
|
|
20
|
-
import { DEFAULT_OUTPUT_BUDGETS, formatByteSize } from "./budgets.ts";
|
|
12
|
+
import { reportOf, reportResult, describeFailure, renderReport, runTool } from "./report.ts";
|
|
13
|
+
import { formatByteSize } from "./budgets.ts";
|
|
14
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
21
15
|
import { MAX_UNNARROWED_SCAN_BYTES } from "./grep-candidates.ts";
|
|
22
16
|
import {
|
|
17
|
+
ERROR_DEFINITIONS,
|
|
23
18
|
OUTCOME_NEXT,
|
|
24
|
-
REREAD,
|
|
25
19
|
type Report,
|
|
26
20
|
type ReportTool,
|
|
27
21
|
type SuccessReport,
|
|
28
22
|
} from "./report-schema.ts";
|
|
29
23
|
|
|
30
24
|
// npm run typecheck checks these invalid callers; they must never run.
|
|
31
|
-
function invalidReportConstruction(report:
|
|
32
|
-
// @ts-expect-error
|
|
33
|
-
|
|
25
|
+
function invalidReportConstruction(report: Report) {
|
|
26
|
+
// @ts-expect-error Removed facts cannot be supplied to a report.
|
|
27
|
+
const removedFact: Report = { tool: "grep", created: true };
|
|
34
28
|
// @ts-expect-error NO_MATCH requires its unmatched facts.
|
|
35
|
-
|
|
29
|
+
const failure: import("./report-schema.ts").FailureReport = {
|
|
30
|
+
tool: "replace",
|
|
31
|
+
error: "NO_MATCH",
|
|
32
|
+
message: "No replacement matched.",
|
|
33
|
+
};
|
|
36
34
|
if (report.error === "NO_MATCH") {
|
|
37
35
|
const unmatched: string[] = report.unmatched;
|
|
38
36
|
// @ts-expect-error NO_MATCH no longer carries a single rule.
|
|
@@ -43,6 +41,8 @@ function invalidReportConstruction(report: NonNullable<ReturnType<typeof decodeR
|
|
|
43
41
|
}
|
|
44
42
|
|
|
45
43
|
const text = (value: string) => ({ type: "text" as const, text: value });
|
|
44
|
+
const reportFromText = (value: string, tool: ReportTool) =>
|
|
45
|
+
reportOf({ content: [text(value)] }, tool);
|
|
46
46
|
function displayed(result: Parameters<typeof renderToolError>[0], tool: ReportTool) {
|
|
47
47
|
initTheme("dark");
|
|
48
48
|
return stripVTControlCharacters(
|
|
@@ -50,13 +50,14 @@ function displayed(result: Parameters<typeof renderToolError>[0], tool: ReportTo
|
|
|
50
50
|
);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
test("structured failure → omits call fields from text and renders the complete details once", () => {
|
|
54
|
-
const result =
|
|
55
|
-
|
|
56
|
-
path: "target.txt",
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
test("structured failure → omits call fields from text and renders the complete details once", async () => {
|
|
54
|
+
const result = await runTool(
|
|
55
|
+
"write",
|
|
56
|
+
{ path: "target.txt", budget: DEFAULT_OUTPUT_BUDGETS },
|
|
57
|
+
async () => {
|
|
58
|
+
throw new HashlineError("TARGET_EXISTS", "Target already exists.");
|
|
59
|
+
},
|
|
60
|
+
);
|
|
60
61
|
const before = structuredClone(result);
|
|
61
62
|
const block = result.content[0];
|
|
62
63
|
assert.ok(block.type === "text");
|
|
@@ -67,10 +68,11 @@ test("structured failure → omits call fields from text and renders the complet
|
|
|
67
68
|
assert.equal(result.details.report.tool, "write");
|
|
68
69
|
assert.equal(result.details.report.path, "target.txt");
|
|
69
70
|
assert.equal(reportOf(result, "write"), result.details.report);
|
|
70
|
-
assert.deepEqual(
|
|
71
|
+
assert.deepEqual(reportFromText(block.text, "write"), {
|
|
71
72
|
tool: "write",
|
|
72
73
|
error: "TARGET_EXISTS",
|
|
73
74
|
message: "Target already exists.",
|
|
75
|
+
next: ERROR_DEFINITIONS.TARGET_EXISTS.next.default,
|
|
74
76
|
});
|
|
75
77
|
const shown = displayed(result, "write");
|
|
76
78
|
assert.match(shown, /TARGET_EXISTS/);
|
|
@@ -120,7 +122,7 @@ test("invalid serialized report → retains the raw diagnostic", () => {
|
|
|
120
122
|
cause: { message: "native", foreign: true },
|
|
121
123
|
}),
|
|
122
124
|
]) {
|
|
123
|
-
assert.equal(
|
|
125
|
+
assert.equal(reportFromText(invalid, "edit"), undefined, invalid);
|
|
124
126
|
const report: unknown = JSON.parse(invalid);
|
|
125
127
|
for (const details of [undefined, { report }]) {
|
|
126
128
|
const result = { content: [text(invalid)], details };
|
|
@@ -143,7 +145,7 @@ test("native failure with deeply nested cause → retains the terminal diagnosti
|
|
|
143
145
|
error = Object.assign(new Error(`layer ${index}`, { cause: error }), { code: "EIO" });
|
|
144
146
|
const report = describeFailure("grep", error, { path: "source", budget: DEFAULT_OUTPUT_BUDGETS });
|
|
145
147
|
const serialized = renderReport(report);
|
|
146
|
-
const decoded =
|
|
148
|
+
const decoded = reportFromText(serialized, "grep");
|
|
147
149
|
assert.ok(decoded);
|
|
148
150
|
assert.deepEqual(decoded, { tool: "grep", ...JSON.parse(serialized) });
|
|
149
151
|
let cause = decoded.cause;
|
|
@@ -241,7 +243,7 @@ test("mutation publication → recovery reflects whether the change was saved",
|
|
|
241
243
|
const report = describeMutationFailure("edit", error, { budget: DEFAULT_OUTPUT_BUDGETS });
|
|
242
244
|
assert.equal(report.publication, publication);
|
|
243
245
|
assert.equal(report.next, expected);
|
|
244
|
-
assert.equal(
|
|
246
|
+
assert.equal(reportFromText(renderReport(report), "edit")?.next, expected);
|
|
245
247
|
}
|
|
246
248
|
});
|
|
247
249
|
|
|
@@ -250,24 +252,28 @@ test("success reports → omit repeated call fields while retaining details and
|
|
|
250
252
|
tool: "write",
|
|
251
253
|
path: "target.txt",
|
|
252
254
|
publication: "PUBLISHED",
|
|
253
|
-
created: true,
|
|
254
255
|
};
|
|
255
256
|
const payload = [text("payload")];
|
|
256
257
|
const result = reportResult(report, payload);
|
|
257
258
|
const last = result.content.at(-1);
|
|
258
259
|
assert.ok(last?.type === "text");
|
|
259
|
-
assert.deepEqual(JSON.parse(last.text), { publication: "PUBLISHED"
|
|
260
|
+
assert.deepEqual(JSON.parse(last.text), { publication: "PUBLISHED" });
|
|
260
261
|
assert.deepEqual(result.details.report, report);
|
|
261
|
-
assert.deepEqual(splitReport(result), { content: payload, report });
|
|
262
|
+
assert.deepEqual(splitReport(result, "write"), { content: payload, report });
|
|
262
263
|
assert.equal(result.content.length, 2);
|
|
263
264
|
});
|
|
264
265
|
|
|
265
266
|
test("success facts without a tool → use call context and enforce tool-specific fields", () => {
|
|
266
|
-
const report: Report = { tool: "write", publication: "PUBLISHED"
|
|
267
|
+
const report: Report = { tool: "write", publication: "PUBLISHED" };
|
|
267
268
|
const serialized = renderReport(report);
|
|
268
|
-
assert.deepEqual(
|
|
269
|
-
|
|
270
|
-
|
|
269
|
+
assert.deepEqual(reportFromText(serialized, "write"), {
|
|
270
|
+
tool: "write",
|
|
271
|
+
publication: "PUBLISHED",
|
|
272
|
+
});
|
|
273
|
+
for (const tool of ["read", "grep"] as const)
|
|
274
|
+
assert.equal(reportFromText(serialized, tool), undefined);
|
|
275
|
+
for (const tool of ["edit", "replace"] as const)
|
|
276
|
+
assert.deepEqual(reportFromText(serialized, tool), { tool, publication: "PUBLISHED" });
|
|
271
277
|
for (const invalid of [
|
|
272
278
|
'{"created":true,"foreign":true}',
|
|
273
279
|
'{"error":"INVALID_ARGUMENTS","message":"missing issues"}',
|
|
@@ -275,7 +281,13 @@ test("success facts without a tool → use call context and enforce tool-specifi
|
|
|
275
281
|
"[]",
|
|
276
282
|
"null",
|
|
277
283
|
])
|
|
278
|
-
assert.equal(
|
|
284
|
+
assert.equal(reportFromText(invalid, "write"), undefined);
|
|
285
|
+
const foreign: Report = { tool: "grep", matches: 1 };
|
|
286
|
+
const foreignText = JSON.stringify(foreign);
|
|
287
|
+
const hooked = { content: [text(foreignText)], details: { report: foreign } };
|
|
288
|
+
assert.equal(reportFromText(foreignText, "read"), undefined);
|
|
289
|
+
assert.equal(reportOf(hooked, "read"), undefined);
|
|
290
|
+
assert.deepEqual(splitReport(hooked, "read"), { content: hooked.content, report: undefined });
|
|
279
291
|
});
|
|
280
292
|
|
|
281
293
|
test("checked report with a different trailing block → preserves the entire payload", () => {
|
|
@@ -288,8 +300,8 @@ test("checked report with a different trailing block → preserves the entire pa
|
|
|
288
300
|
]) {
|
|
289
301
|
const content = [text("saved payload"), last];
|
|
290
302
|
const result: Parameters<typeof splitReport>[0] = { content, details: { report } };
|
|
291
|
-
assert.deepEqual(splitReport(result), { content, report });
|
|
292
|
-
assert.equal(splitReport(result).content, content);
|
|
303
|
+
assert.deepEqual(splitReport(result, "write"), { content, report });
|
|
304
|
+
assert.equal(splitReport(result, "write").content, content);
|
|
293
305
|
}
|
|
294
306
|
});
|
|
295
307
|
|
|
@@ -321,35 +333,41 @@ test("replacement failure facts → survive report serialization", () => {
|
|
|
321
333
|
line: 2,
|
|
322
334
|
},
|
|
323
335
|
] satisfies Report[]) {
|
|
324
|
-
assert.deepEqual(
|
|
336
|
+
assert.deepEqual(reportFromText(renderReport(report), "replace"), report);
|
|
325
337
|
}
|
|
326
338
|
});
|
|
327
339
|
|
|
328
|
-
test("
|
|
340
|
+
test("success facts survive details and model serialization", () => {
|
|
329
341
|
for (const report of [
|
|
330
342
|
{ tool: "grep", matches: 1 },
|
|
331
|
-
{ tool: "grep", matches:
|
|
332
|
-
{
|
|
333
|
-
tool: "grep",
|
|
334
|
-
matches: 0,
|
|
335
|
-
scanned: "all",
|
|
336
|
-
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
337
|
-
},
|
|
343
|
+
{ tool: "grep", matches: 0, scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES) },
|
|
338
344
|
{
|
|
339
345
|
tool: "grep",
|
|
340
346
|
error: "SEARCH_INCOMPLETE",
|
|
341
347
|
message: "Search incomplete.",
|
|
342
348
|
diagnostics: "unreadable file",
|
|
343
|
-
scanned: "all",
|
|
344
349
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
345
350
|
},
|
|
346
351
|
] satisfies Report[]) {
|
|
347
352
|
const serialized = renderReport(report);
|
|
348
|
-
assert.deepEqual(
|
|
349
|
-
assert.deepEqual(
|
|
353
|
+
assert.deepEqual(reportFromText(serialized, report.tool), report);
|
|
354
|
+
assert.deepEqual(
|
|
355
|
+
reportOf({ content: [text(serialized)], details: { report } }, report.tool),
|
|
356
|
+
report,
|
|
357
|
+
);
|
|
350
358
|
}
|
|
351
359
|
});
|
|
352
360
|
|
|
361
|
+
test("grep preview from invalid UTF-8 → recovery selects bash for the listed paths", () => {
|
|
362
|
+
const result = reportResult({
|
|
363
|
+
tool: "grep",
|
|
364
|
+
linePreviewLimit: DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
|
|
365
|
+
invalidUtf8: ["invalid.txt"],
|
|
366
|
+
});
|
|
367
|
+
const next = reportOf(result, "grep")?.next ?? "";
|
|
368
|
+
assert.match(next, /invalidUtf8.*bash/);
|
|
369
|
+
});
|
|
370
|
+
|
|
353
371
|
test("match and scan limits → recovery prioritizes the match limit", () => {
|
|
354
372
|
const next = OUTCOME_NEXT.find(([fact]) => fact === "matchLimit")?.[1];
|
|
355
373
|
assert.ok(next);
|
|
@@ -357,40 +375,21 @@ test("match and scan limits → recovery prioritizes the match limit", () => {
|
|
|
357
375
|
tool: "grep",
|
|
358
376
|
matches: 1,
|
|
359
377
|
matchLimit: 1,
|
|
360
|
-
scanned: "all",
|
|
361
378
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
362
379
|
});
|
|
363
380
|
assert.equal(reportOf(result, "grep")?.next, next);
|
|
364
381
|
});
|
|
365
382
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
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);
|
|
383
|
+
test("output limit with read next offset → recovery prioritizes the next offset", () => {
|
|
384
|
+
const next = OUTCOME_NEXT.find(([key]) => key === "nextOffset")?.[1];
|
|
385
|
+
assert.ok(next);
|
|
386
|
+
const result = reportResult({
|
|
387
|
+
tool: "read",
|
|
388
|
+
nextOffset: 2,
|
|
389
|
+
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.readBytes),
|
|
392
390
|
});
|
|
393
|
-
|
|
391
|
+
assert.equal(reportOf(result, "read")?.next, next);
|
|
392
|
+
});
|
|
394
393
|
|
|
395
394
|
test("text-only error contains facts invalid for the originating tool → preserves the raw diagnostic", () => {
|
|
396
395
|
const diagnostic = '{"publication":"PUBLISHED"}';
|
|
@@ -406,20 +405,18 @@ test("invalid details report → falls back to valid text and leaves content int
|
|
|
406
405
|
const last = expected ? renderReport(expected) : JSON.stringify({ matches: "x" }, null, 2);
|
|
407
406
|
const result = { details, content: [text("payload"), text(last)] };
|
|
408
407
|
assert.deepEqual(reportOf(result, "grep"), expected);
|
|
409
|
-
assert.deepEqual(splitReport(result), { content: result.content, report: undefined });
|
|
408
|
+
assert.deepEqual(splitReport(result, "grep"), { content: result.content, report: undefined });
|
|
410
409
|
assert.equal(result.content.length, 2);
|
|
411
410
|
}
|
|
412
411
|
});
|
|
413
412
|
|
|
414
413
|
test("output byte limit → follows the scan limit and precedes the line preview limit", () => {
|
|
415
414
|
const serialized = renderReport({
|
|
416
|
-
scanned: "all",
|
|
417
415
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
418
416
|
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.grepBytes),
|
|
419
417
|
linePreviewLimit: DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
|
|
420
418
|
});
|
|
421
419
|
assert.deepEqual(Object.keys(JSON.parse(serialized)), [
|
|
422
|
-
"scanned",
|
|
423
420
|
"scanLimit",
|
|
424
421
|
"outputLimit",
|
|
425
422
|
"linePreviewLimit",
|
|
@@ -437,7 +434,7 @@ test("hooked details contain a cyclic report cause → preserves payload and dec
|
|
|
437
434
|
const content = [text(renderReport(fallback))];
|
|
438
435
|
const result = { details: { report: { ...fallback, cause } }, content };
|
|
439
436
|
assert.deepEqual(reportOf(result, "read"), fallback);
|
|
440
|
-
assert.deepEqual(splitReport(result), { content, report: undefined });
|
|
437
|
+
assert.deepEqual(splitReport(result, "read"), { content, report: undefined });
|
|
441
438
|
});
|
|
442
439
|
|
|
443
440
|
test("native mutation failure before commit → retains publication", () => {
|
|
@@ -472,18 +469,15 @@ test("failure classification without mutation facts → omits mutation state for
|
|
|
472
469
|
});
|
|
473
470
|
|
|
474
471
|
test("mutation freshness → retains the fact and requests reread only for an existing or unknown target", () => {
|
|
475
|
-
for (const [
|
|
476
|
-
["missing", undefined],
|
|
477
|
-
["changed", REREAD],
|
|
478
|
-
["unknown", REREAD],
|
|
479
|
-
] as const) {
|
|
472
|
+
for (const freshness of ["missing", "changed", "unknown"] as const) {
|
|
480
473
|
const result = reportResult({ tool: "edit", publication: "PUBLISHED", freshness });
|
|
481
474
|
const report = reportOf(result, "edit");
|
|
482
475
|
assert.equal(report?.freshness, freshness);
|
|
483
|
-
assert.equal(report?.next,
|
|
476
|
+
if (freshness === "missing") assert.equal(report?.next, undefined);
|
|
477
|
+
else assert.match(report?.next ?? "", /read.*before.*edit|reread/i);
|
|
484
478
|
const block = result.content.at(-1);
|
|
485
479
|
assert.ok(block?.type === "text");
|
|
486
|
-
assert.deepEqual(
|
|
487
|
-
assert.equal(Object.hasOwn(JSON.parse(block.text), "next"),
|
|
480
|
+
assert.deepEqual(reportFromText(block.text, "edit"), report);
|
|
481
|
+
assert.equal(Object.hasOwn(JSON.parse(block.text), "next"), freshness !== "missing");
|
|
488
482
|
}
|
|
489
483
|
});
|
package/src/pi/report.ts
CHANGED
|
@@ -30,12 +30,15 @@ import type { OutputBudgets } from "./budgets.ts";
|
|
|
30
30
|
import { cancellationError } from "./error-text.ts";
|
|
31
31
|
import {
|
|
32
32
|
ERROR_DEFINITIONS,
|
|
33
|
+
MODEL_OMITTED_FIELDS,
|
|
33
34
|
OUTCOME_NEXT,
|
|
34
35
|
PUBLICATION_NEXT,
|
|
35
36
|
REPORT_SCHEMA,
|
|
36
37
|
WIRE_REPORT_SCHEMA,
|
|
37
38
|
type ErrorCode,
|
|
38
39
|
type FailureReport,
|
|
40
|
+
type OutcomeVariant,
|
|
41
|
+
type ModelOmittedField,
|
|
39
42
|
type Report,
|
|
40
43
|
type ReportTool,
|
|
41
44
|
type SuccessReport,
|
|
@@ -50,12 +53,9 @@ type Content = AgentToolResult<unknown>["content"];
|
|
|
50
53
|
|
|
51
54
|
/** What a report's text states: the originating call supplies its tool and path. */
|
|
52
55
|
export type ReportText<R extends Report = Report> = R extends unknown
|
|
53
|
-
? Omit<R,
|
|
56
|
+
? Omit<R, ModelOmittedField>
|
|
54
57
|
: never;
|
|
55
58
|
|
|
56
|
-
/** Fields the originating call supplies, which a result's text never repeats. */
|
|
57
|
-
const CALL_FIELDS = new Set(["tool", "path"]);
|
|
58
|
-
|
|
59
59
|
/** The fields a report states, in schema order; both renderers list them in this order. */
|
|
60
60
|
export function reportEntries(report: ReportText): [string, unknown][] {
|
|
61
61
|
return Object.keys(REPORT_SCHEMA.properties).flatMap((key) => {
|
|
@@ -66,17 +66,24 @@ export function reportEntries(report: ReportText): [string, unknown][] {
|
|
|
66
66
|
|
|
67
67
|
/** Serialize facts in schema order; a result never repeats its call's tool and path. */
|
|
68
68
|
export function renderReport(report: ReportText): string {
|
|
69
|
-
const entries = reportEntries(report).filter(
|
|
69
|
+
const entries = reportEntries(report).filter(
|
|
70
|
+
([key]) => !MODEL_OMITTED_FIELDS.has(key as ModelOmittedField),
|
|
71
|
+
);
|
|
70
72
|
return JSON.stringify(Object.fromEntries(entries), null, 2);
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
/** Decode a text report; the text omits its tool, which the originating call supplies. */
|
|
74
|
-
|
|
76
|
+
function decodeReport(text: string, contextTool: ReportTool): Report | undefined {
|
|
75
77
|
if (!text.startsWith("{")) return undefined;
|
|
76
78
|
try {
|
|
77
79
|
let value: unknown = JSON.parse(text);
|
|
78
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)
|
|
79
|
-
|
|
80
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
81
|
+
if ("tool" in value) {
|
|
82
|
+
if (value.tool !== contextTool) return undefined;
|
|
83
|
+
} else {
|
|
84
|
+
value = { tool: contextTool, ...value };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
80
87
|
return Value.Check(WIRE_REPORT_SCHEMA, value) ? (value as Report) : undefined;
|
|
81
88
|
} catch (error) {
|
|
82
89
|
if (error instanceof SyntaxError || error instanceof RangeError || error instanceof TypeError)
|
|
@@ -89,15 +96,15 @@ export function decodeReport(text: string, contextTool: ReportTool): Report | un
|
|
|
89
96
|
* The report a result's details carry, checked once against the wire schema: details also
|
|
90
97
|
* come from saved sessions and other extensions' result hooks, so their shape is unknown.
|
|
91
98
|
*/
|
|
92
|
-
export function detailsReport(details: unknown): Report | undefined {
|
|
99
|
+
export function detailsReport(details: unknown, contextTool: ReportTool): Report | undefined {
|
|
93
100
|
const report: unknown =
|
|
94
101
|
typeof details === "object" && details !== null ? Reflect.get(details, "report") : undefined;
|
|
95
102
|
try {
|
|
96
103
|
// Hooks can introduce cycles; reject non-JSON reports before recursive schema validation.
|
|
97
104
|
JSON.stringify(report);
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
105
|
+
if (report === undefined || !Value.Check(WIRE_REPORT_SCHEMA, report)) return undefined;
|
|
106
|
+
const checked = report as Report;
|
|
107
|
+
return checked.tool === contextTool ? checked : undefined;
|
|
101
108
|
} catch (error) {
|
|
102
109
|
if (error instanceof TypeError || error instanceof RangeError) return undefined;
|
|
103
110
|
throw error;
|
|
@@ -109,32 +116,40 @@ export function reportOf(
|
|
|
109
116
|
result: { readonly content: Content; readonly details?: unknown },
|
|
110
117
|
tool: ReportTool,
|
|
111
118
|
): Report | undefined {
|
|
112
|
-
const report = detailsReport(result.details);
|
|
119
|
+
const report = detailsReport(result.details, tool);
|
|
113
120
|
if (report) return report;
|
|
114
121
|
const last = result.content.at(-1);
|
|
115
122
|
return last?.type === "text" ? decodeReport(last.text, tool) : undefined;
|
|
116
123
|
}
|
|
117
124
|
|
|
118
|
-
/**
|
|
119
|
-
|
|
120
|
-
|
|
125
|
+
/**
|
|
126
|
+
* The recovery a successful result's facts call for: the first in OUTCOME_NEXT order, in the
|
|
127
|
+
* producer's selected alternative when that rule names it.
|
|
128
|
+
*/
|
|
129
|
+
function outcomeNext(report: Report, variant: OutcomeVariant | undefined): string | undefined {
|
|
130
|
+
const next = OUTCOME_NEXT.find(
|
|
121
131
|
([fact, , except]) => report[fact] !== undefined && report[fact] !== except,
|
|
122
132
|
)?.[1];
|
|
133
|
+
return typeof next === "object" ? ((variant && next[variant]) ?? next.default) : next;
|
|
123
134
|
}
|
|
124
135
|
|
|
125
136
|
/**
|
|
126
137
|
* A successful result: the tool's payload, then its report when it states
|
|
127
|
-
* anything beyond tool and path or when there is no payload.
|
|
138
|
+
* anything beyond tool and path or when there is no payload. `variant` is the recovery
|
|
139
|
+
* alternative the producing layer selected.
|
|
128
140
|
*/
|
|
129
141
|
export function reportResult<TDetails extends object | undefined>(
|
|
130
142
|
report: SuccessReport,
|
|
131
143
|
payload: Content = [],
|
|
132
144
|
details?: TDetails,
|
|
145
|
+
variant?: OutcomeVariant,
|
|
133
146
|
): AgentToolResult<TDetails & ReportDetails<SuccessReport>> {
|
|
134
147
|
const complete = Object.fromEntries(
|
|
135
|
-
reportEntries({ ...report, next: outcomeNext(report) }),
|
|
148
|
+
reportEntries({ ...report, next: outcomeNext(report, variant) }),
|
|
136
149
|
) as SuccessReport;
|
|
137
|
-
const stated = Object.keys(complete).some(
|
|
150
|
+
const stated = Object.keys(complete).some(
|
|
151
|
+
(key) => !MODEL_OMITTED_FIELDS.has(key as ModelOmittedField),
|
|
152
|
+
);
|
|
138
153
|
return {
|
|
139
154
|
content:
|
|
140
155
|
stated || payload.length === 0
|
|
@@ -145,9 +160,7 @@ export function reportResult<TDetails extends object | undefined>(
|
|
|
145
160
|
}
|
|
146
161
|
|
|
147
162
|
/** A failed result: the report is its entire content, flagged so Pi reports it as an error. */
|
|
148
|
-
|
|
149
|
-
report: FailureReport,
|
|
150
|
-
): AgentToolResult<ReportDetails<FailureReport>> {
|
|
163
|
+
function failureResult(report: FailureReport): AgentToolResult<ReportDetails<FailureReport>> {
|
|
151
164
|
const complete = Object.fromEntries(reportEntries(report)) as FailureReport;
|
|
152
165
|
return {
|
|
153
166
|
content: [{ type: "text", text: renderReport(complete) }],
|