@criogaid/pi-hashline-edit 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -3
- package/README.zh-CN.md +4 -3
- package/package.json +1 -1
- package/src/core/apply.test.ts +2 -2
- package/src/core/apply.ts +2 -2
- 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/types.ts +1 -1
- package/src/index.ts +8 -8
- 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 +3 -2
- 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 +4 -3
- 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 +50 -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.testing.ts +3 -3
- 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 +5 -6
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +6 -5
- 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 +218 -139
- 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 +47 -62
- package/src/pi/error-text.ts +7 -2
- package/src/pi/execute.test.ts +174 -157
- package/src/pi/failure-context.test.ts +4 -3
- 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/grep-candidates.test.ts +18 -20
- package/src/pi/grep-candidates.ts +8 -13
- package/src/pi/grep-matrix.test.ts +3 -3
- package/src/pi/grep-order.test.ts +12 -11
- package/src/pi/grep-output.ts +36 -15
- package/src/pi/grep-reread.ts +7 -2
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +4 -4
- package/src/pi/grep-search.ts +8 -11
- package/src/pi/grep-tool.ts +23 -39
- package/src/pi/grep.test.ts +102 -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 +9 -5
- 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 +22 -41
- package/src/pi/registration.test.ts +25 -19
- package/src/pi/render.test.ts +51 -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 +205 -83
- package/src/pi/report.test.ts +66 -64
- package/src/pi/report.ts +23 -17
- 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/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,32 +333,28 @@ 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
|
|
|
@@ -357,7 +365,6 @@ test("match and scan limits → recovery prioritizes the match limit", () => {
|
|
|
357
365
|
tool: "grep",
|
|
358
366
|
matches: 1,
|
|
359
367
|
matchLimit: 1,
|
|
360
|
-
scanned: "all",
|
|
361
368
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
362
369
|
});
|
|
363
370
|
assert.equal(reportOf(result, "grep")?.next, next);
|
|
@@ -375,15 +382,15 @@ for (const { name, report, fact } of [
|
|
|
375
382
|
fact: "outputLimit",
|
|
376
383
|
},
|
|
377
384
|
{
|
|
378
|
-
name: "output limit with read
|
|
385
|
+
name: "output limit with read next offset → recovery prioritizes the next offset",
|
|
379
386
|
report: {
|
|
380
387
|
tool: "read",
|
|
381
|
-
|
|
388
|
+
nextOffset: 2,
|
|
382
389
|
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.readBytes),
|
|
383
390
|
},
|
|
384
|
-
fact: "
|
|
391
|
+
fact: "nextOffset",
|
|
385
392
|
},
|
|
386
|
-
] satisfies { name: string; report: SuccessReport; fact: "outputLimit" | "
|
|
393
|
+
] satisfies { name: string; report: SuccessReport; fact: "outputLimit" | "nextOffset" }[]) {
|
|
387
394
|
test(name, () => {
|
|
388
395
|
const next = OUTCOME_NEXT.find(([key]) => key === fact)?.[1];
|
|
389
396
|
assert.ok(next);
|
|
@@ -406,20 +413,18 @@ test("invalid details report → falls back to valid text and leaves content int
|
|
|
406
413
|
const last = expected ? renderReport(expected) : JSON.stringify({ matches: "x" }, null, 2);
|
|
407
414
|
const result = { details, content: [text("payload"), text(last)] };
|
|
408
415
|
assert.deepEqual(reportOf(result, "grep"), expected);
|
|
409
|
-
assert.deepEqual(splitReport(result), { content: result.content, report: undefined });
|
|
416
|
+
assert.deepEqual(splitReport(result, "grep"), { content: result.content, report: undefined });
|
|
410
417
|
assert.equal(result.content.length, 2);
|
|
411
418
|
}
|
|
412
419
|
});
|
|
413
420
|
|
|
414
421
|
test("output byte limit → follows the scan limit and precedes the line preview limit", () => {
|
|
415
422
|
const serialized = renderReport({
|
|
416
|
-
scanned: "all",
|
|
417
423
|
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
418
424
|
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.grepBytes),
|
|
419
425
|
linePreviewLimit: DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
|
|
420
426
|
});
|
|
421
427
|
assert.deepEqual(Object.keys(JSON.parse(serialized)), [
|
|
422
|
-
"scanned",
|
|
423
428
|
"scanLimit",
|
|
424
429
|
"outputLimit",
|
|
425
430
|
"linePreviewLimit",
|
|
@@ -437,7 +442,7 @@ test("hooked details contain a cyclic report cause → preserves payload and dec
|
|
|
437
442
|
const content = [text(renderReport(fallback))];
|
|
438
443
|
const result = { details: { report: { ...fallback, cause } }, content };
|
|
439
444
|
assert.deepEqual(reportOf(result, "read"), fallback);
|
|
440
|
-
assert.deepEqual(splitReport(result), { content, report: undefined });
|
|
445
|
+
assert.deepEqual(splitReport(result, "read"), { content, report: undefined });
|
|
441
446
|
});
|
|
442
447
|
|
|
443
448
|
test("native mutation failure before commit → retains publication", () => {
|
|
@@ -472,18 +477,15 @@ test("failure classification without mutation facts → omits mutation state for
|
|
|
472
477
|
});
|
|
473
478
|
|
|
474
479
|
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) {
|
|
480
|
+
for (const freshness of ["missing", "changed", "unknown"] as const) {
|
|
480
481
|
const result = reportResult({ tool: "edit", publication: "PUBLISHED", freshness });
|
|
481
482
|
const report = reportOf(result, "edit");
|
|
482
483
|
assert.equal(report?.freshness, freshness);
|
|
483
|
-
assert.equal(report?.next,
|
|
484
|
+
if (freshness === "missing") assert.equal(report?.next, undefined);
|
|
485
|
+
else assert.match(report?.next ?? "", /read.*before.*edit|reread/i);
|
|
484
486
|
const block = result.content.at(-1);
|
|
485
487
|
assert.ok(block?.type === "text");
|
|
486
|
-
assert.deepEqual(
|
|
487
|
-
assert.equal(Object.hasOwn(JSON.parse(block.text), "next"),
|
|
488
|
+
assert.deepEqual(reportFromText(block.text, "edit"), report);
|
|
489
|
+
assert.equal(Object.hasOwn(JSON.parse(block.text), "next"), freshness !== "missing");
|
|
488
490
|
}
|
|
489
491
|
});
|
package/src/pi/report.ts
CHANGED
|
@@ -30,12 +30,14 @@ 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 ModelOmittedField,
|
|
39
41
|
type Report,
|
|
40
42
|
type ReportTool,
|
|
41
43
|
type SuccessReport,
|
|
@@ -50,12 +52,9 @@ type Content = AgentToolResult<unknown>["content"];
|
|
|
50
52
|
|
|
51
53
|
/** What a report's text states: the originating call supplies its tool and path. */
|
|
52
54
|
export type ReportText<R extends Report = Report> = R extends unknown
|
|
53
|
-
? Omit<R,
|
|
55
|
+
? Omit<R, ModelOmittedField>
|
|
54
56
|
: never;
|
|
55
57
|
|
|
56
|
-
/** Fields the originating call supplies, which a result's text never repeats. */
|
|
57
|
-
const CALL_FIELDS = new Set(["tool", "path"]);
|
|
58
|
-
|
|
59
58
|
/** The fields a report states, in schema order; both renderers list them in this order. */
|
|
60
59
|
export function reportEntries(report: ReportText): [string, unknown][] {
|
|
61
60
|
return Object.keys(REPORT_SCHEMA.properties).flatMap((key) => {
|
|
@@ -66,17 +65,24 @@ export function reportEntries(report: ReportText): [string, unknown][] {
|
|
|
66
65
|
|
|
67
66
|
/** Serialize facts in schema order; a result never repeats its call's tool and path. */
|
|
68
67
|
export function renderReport(report: ReportText): string {
|
|
69
|
-
const entries = reportEntries(report).filter(
|
|
68
|
+
const entries = reportEntries(report).filter(
|
|
69
|
+
([key]) => !MODEL_OMITTED_FIELDS.has(key as ModelOmittedField),
|
|
70
|
+
);
|
|
70
71
|
return JSON.stringify(Object.fromEntries(entries), null, 2);
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
/** Decode a text report; the text omits its tool, which the originating call supplies. */
|
|
74
|
-
|
|
75
|
+
function decodeReport(text: string, contextTool: ReportTool): Report | undefined {
|
|
75
76
|
if (!text.startsWith("{")) return undefined;
|
|
76
77
|
try {
|
|
77
78
|
let value: unknown = JSON.parse(text);
|
|
78
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)
|
|
79
|
-
|
|
79
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
80
|
+
if ("tool" in value) {
|
|
81
|
+
if (value.tool !== contextTool) return undefined;
|
|
82
|
+
} else {
|
|
83
|
+
value = { tool: contextTool, ...value };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
80
86
|
return Value.Check(WIRE_REPORT_SCHEMA, value) ? (value as Report) : undefined;
|
|
81
87
|
} catch (error) {
|
|
82
88
|
if (error instanceof SyntaxError || error instanceof RangeError || error instanceof TypeError)
|
|
@@ -89,15 +95,15 @@ export function decodeReport(text: string, contextTool: ReportTool): Report | un
|
|
|
89
95
|
* The report a result's details carry, checked once against the wire schema: details also
|
|
90
96
|
* come from saved sessions and other extensions' result hooks, so their shape is unknown.
|
|
91
97
|
*/
|
|
92
|
-
export function detailsReport(details: unknown): Report | undefined {
|
|
98
|
+
export function detailsReport(details: unknown, contextTool: ReportTool): Report | undefined {
|
|
93
99
|
const report: unknown =
|
|
94
100
|
typeof details === "object" && details !== null ? Reflect.get(details, "report") : undefined;
|
|
95
101
|
try {
|
|
96
102
|
// Hooks can introduce cycles; reject non-JSON reports before recursive schema validation.
|
|
97
103
|
JSON.stringify(report);
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
if (report === undefined || !Value.Check(WIRE_REPORT_SCHEMA, report)) return undefined;
|
|
105
|
+
const checked = report as Report;
|
|
106
|
+
return checked.tool === contextTool ? checked : undefined;
|
|
101
107
|
} catch (error) {
|
|
102
108
|
if (error instanceof TypeError || error instanceof RangeError) return undefined;
|
|
103
109
|
throw error;
|
|
@@ -109,7 +115,7 @@ export function reportOf(
|
|
|
109
115
|
result: { readonly content: Content; readonly details?: unknown },
|
|
110
116
|
tool: ReportTool,
|
|
111
117
|
): Report | undefined {
|
|
112
|
-
const report = detailsReport(result.details);
|
|
118
|
+
const report = detailsReport(result.details, tool);
|
|
113
119
|
if (report) return report;
|
|
114
120
|
const last = result.content.at(-1);
|
|
115
121
|
return last?.type === "text" ? decodeReport(last.text, tool) : undefined;
|
|
@@ -134,7 +140,9 @@ export function reportResult<TDetails extends object | undefined>(
|
|
|
134
140
|
const complete = Object.fromEntries(
|
|
135
141
|
reportEntries({ ...report, next: outcomeNext(report) }),
|
|
136
142
|
) as SuccessReport;
|
|
137
|
-
const stated = Object.keys(complete).some(
|
|
143
|
+
const stated = Object.keys(complete).some(
|
|
144
|
+
(key) => !MODEL_OMITTED_FIELDS.has(key as ModelOmittedField),
|
|
145
|
+
);
|
|
138
146
|
return {
|
|
139
147
|
content:
|
|
140
148
|
stated || payload.length === 0
|
|
@@ -145,9 +153,7 @@ export function reportResult<TDetails extends object | undefined>(
|
|
|
145
153
|
}
|
|
146
154
|
|
|
147
155
|
/** 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>> {
|
|
156
|
+
function failureResult(report: FailureReport): AgentToolResult<ReportDetails<FailureReport>> {
|
|
151
157
|
const complete = Object.fromEntries(reportEntries(report)) as FailureReport;
|
|
152
158
|
return {
|
|
153
159
|
content: [{ type: "text", text: renderReport(complete) }],
|