@criogaid/pi-hashline-edit 0.7.0 → 0.9.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 +76 -220
- package/README.zh-CN.md +166 -0
- package/package.json +2 -1
- package/src/core/AGENTS.md +1 -1
- package/src/core/apply.test.ts +171 -133
- package/src/core/apply.ts +34 -31
- package/src/core/case-fold.test.ts +11 -7
- package/src/core/case-fold.ts +2 -6
- package/src/core/character-set.ts +14 -0
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +25 -7
- package/src/core/ranges.test.ts +49 -9
- package/src/core/ranges.ts +15 -4
- package/src/core/regex.ts +36 -6
- package/src/core/replace-schema.ts +18 -11
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +6 -5
- package/src/core/search-analysis.test.ts +9 -0
- package/src/core/search-analysis.ts +35 -31
- package/src/core/search.ts +1 -1
- package/src/core/types.ts +6 -8
- package/src/index.ts +2 -4
- package/src/integration/config-registration.test.ts +18 -10
- package/src/integration/fusion-dispatch.test.ts +41 -22
- package/src/integration/grep-flags.test.ts +6 -13
- package/src/integration/grep-js-completeness.test.ts +41 -31
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +4 -3
- package/src/integration/grep-regex.test.ts +13 -16
- package/src/integration/grep-rg.test.ts +132 -92
- package/src/integration/grep-scan-budget.test.ts +27 -5
- package/src/integration/large-text.test.ts +4 -5
- package/src/integration/model-budget-lifecycle.test.ts +26 -8
- package/src/integration/mutation-file-types.test.ts +0 -1
- package/src/integration/mutation-file-types.testing.ts +2 -2
- package/src/integration/npm-package.testing.ts +7 -33
- package/src/integration/publication-read-changes.test.ts +60 -28
- package/src/integration/replace-package.testing.ts +4 -1
- package/src/integration/replace-worker-boundary.test.ts +34 -24
- package/src/integration/report-replay.test.ts +3 -3
- package/src/integration/utf8-errors.test.ts +3 -3
- package/src/pi/AGENTS.md +13 -14
- package/src/pi/action-fusion.test.ts +75 -220
- package/src/pi/action-fusion.ts +37 -74
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +7 -3
- package/src/pi/argument-diagnostics.ts +35 -186
- package/src/pi/argument-error.testing.ts +7 -4
- package/src/pi/argument-error.ts +5 -7
- package/src/pi/argument-format.test.ts +25 -38
- package/src/pi/argument-validation.test.ts +446 -420
- package/src/pi/argument-validation.ts +4 -5
- package/src/pi/budgets.ts +5 -7
- package/src/pi/config.test.ts +15 -5
- package/src/pi/config.ts +6 -6
- package/src/pi/diagnostic-buffer.test.ts +2 -3
- package/src/pi/diagnostic-buffer.ts +2 -2
- package/src/pi/edit-tool.ts +175 -121
- package/src/pi/execute.test.ts +170 -227
- package/src/pi/failure-context.test.ts +61 -16
- package/src/pi/failure-context.ts +13 -10
- package/src/pi/failure-path.test.ts +115 -50
- package/src/pi/file-commit.test.ts +30 -46
- package/src/pi/file-commit.ts +11 -40
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +193 -37
- package/src/pi/fusion-card.ts +186 -116
- package/src/pi/grep-candidates.ts +25 -27
- package/src/pi/grep-matrix.test.ts +1 -1
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +62 -69
- package/src/pi/grep-render.ts +4 -1
- package/src/pi/grep-scope.ts +1 -1
- package/src/pi/grep-search.ts +85 -56
- package/src/pi/grep-tool.ts +17 -48
- package/src/pi/grep.test.ts +271 -170
- package/src/pi/mutation-outcome.testing.ts +2 -3
- package/src/pi/mutation-result.test.ts +26 -19
- package/src/pi/mutation-result.ts +25 -40
- package/src/pi/mutation-runner.ts +9 -17
- package/src/pi/output-budgets.test.ts +27 -18
- package/src/pi/path.ts +3 -1
- package/src/pi/pi.test.ts +7 -6
- package/src/pi/read-tool.ts +32 -80
- package/src/pi/read.test.ts +145 -92
- package/src/pi/regex-worker-contract.ts +5 -23
- package/src/pi/regex-worker.ts +4 -10
- package/src/pi/registration.test.ts +106 -67
- package/src/pi/render.test.ts +239 -109
- package/src/pi/render.ts +42 -22
- package/src/pi/replace-regex.ts +3 -5
- package/src/pi/replace-tool.ts +29 -27
- package/src/pi/replace-worker-contract.ts +3 -12
- package/src/pi/replace.test.ts +144 -226
- package/src/pi/report-cause.ts +23 -19
- package/src/pi/report-failure.testing.ts +2 -2
- package/src/pi/report-schema.ts +60 -45
- package/src/pi/report.test.ts +138 -71
- package/src/pi/report.ts +40 -43
- package/src/pi/result-safety.test.ts +186 -131
- package/src/pi/rg-process.test.ts +36 -10
- package/src/pi/rg-process.ts +9 -3
- package/src/pi/rg-regex.test.ts +17 -0
- package/src/pi/rg-regex.ts +28 -22
- package/src/pi/text-stream.test.ts +35 -36
- package/src/pi/text-stream.ts +14 -14
- package/src/pi/tool-call.testing.ts +17 -7
- package/src/pi/write-tool.ts +9 -11
- package/src/pi/write.test.ts +43 -75
- package/src/testing/regex-translation-corpus.ts +16 -0
- package/src/testing/rg-line-listing.fixture.mjs +1 -1
- package/src/integration/forget-grep.test.ts +0 -148
- package/src/pi/forget-tool.ts +0 -367
- package/src/pi/forget.test.ts +0 -630
- package/src/pi/forget.testing.ts +0 -201
package/src/pi/report.test.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import { stripVTControlCharacters } from "node:util";
|
|
4
|
-
import {
|
|
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
6
|
import { formatArgumentError } from "./argument-error.ts";
|
|
7
|
-
import { renderToolError,
|
|
7
|
+
import { renderToolError, splitReport } from "./render.ts";
|
|
8
8
|
import {
|
|
9
9
|
causeOf,
|
|
10
10
|
decodeReport,
|
|
@@ -14,9 +14,9 @@ import {
|
|
|
14
14
|
describeFailure,
|
|
15
15
|
renderReport,
|
|
16
16
|
} from "./report.ts";
|
|
17
|
-
import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
|
|
17
|
+
import { DEFAULT_OUTPUT_BUDGETS, formatByteSize } from "./budgets.ts";
|
|
18
18
|
import { MAX_UNNARROWED_SCAN_BYTES } from "./grep-candidates.ts";
|
|
19
|
-
import { OUTCOME_NEXT, type Report, type SuccessReport } from "./report-schema.ts";
|
|
19
|
+
import { OUTCOME_NEXT, type Report, type ReportTool, type SuccessReport } from "./report-schema.ts";
|
|
20
20
|
|
|
21
21
|
// npm run typecheck checks these invalid callers; they must never run.
|
|
22
22
|
function invalidReportConstruction(report: NonNullable<ReturnType<typeof decodeReport>>) {
|
|
@@ -32,20 +32,36 @@ function invalidReportConstruction(report: NonNullable<ReturnType<typeof decodeR
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
const text = (value: string) => ({ type: "text" as const, text: value });
|
|
35
|
-
function displayed(result: Parameters<typeof renderToolError>[0]) {
|
|
35
|
+
function displayed(result: Parameters<typeof renderToolError>[0], tool: ReportTool) {
|
|
36
36
|
initTheme("dark");
|
|
37
|
-
return stripVTControlCharacters(
|
|
37
|
+
return stripVTControlCharacters(
|
|
38
|
+
renderToolError(result, tool, theme, true).render(200).join("\n"),
|
|
39
|
+
);
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
test("structured failure →
|
|
42
|
+
test("structured failure → omits call fields from text and renders the complete details once", () => {
|
|
41
43
|
const result = failureResult({
|
|
42
44
|
tool: "write",
|
|
45
|
+
path: "target.txt",
|
|
43
46
|
error: "TARGET_EXISTS",
|
|
44
47
|
message: "Target already exists.",
|
|
45
48
|
});
|
|
46
49
|
const before = structuredClone(result);
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
const block = result.content[0];
|
|
51
|
+
assert.ok(block.type === "text");
|
|
52
|
+
const serialized: unknown = JSON.parse(block.text);
|
|
53
|
+
assert.ok(typeof serialized === "object" && serialized !== null);
|
|
54
|
+
assert.equal(Object.hasOwn(serialized, "tool"), false);
|
|
55
|
+
assert.equal(Object.hasOwn(serialized, "path"), false);
|
|
56
|
+
assert.equal(result.details.report.tool, "write");
|
|
57
|
+
assert.equal(result.details.report.path, "target.txt");
|
|
58
|
+
assert.equal(reportOf(result, "write"), result.details.report);
|
|
59
|
+
assert.deepEqual(decodeReport(block.text, "write"), {
|
|
60
|
+
tool: "write",
|
|
61
|
+
error: "TARGET_EXISTS",
|
|
62
|
+
message: "Target already exists.",
|
|
63
|
+
});
|
|
64
|
+
const shown = displayed(result, "write");
|
|
49
65
|
assert.match(shown, /TARGET_EXISTS/);
|
|
50
66
|
assert.equal(shown.split("Target already exists.").length - 1, 1);
|
|
51
67
|
assert.doesNotMatch(shown, /"error":/);
|
|
@@ -54,40 +70,34 @@ test("structured failure → renders its report once from details", () => {
|
|
|
54
70
|
|
|
55
71
|
test("Pi argument failure transported as text → renders field facts after serialization", () => {
|
|
56
72
|
const diagnostic = formatArgumentError(
|
|
57
|
-
"
|
|
58
|
-
[{ field: "edits[0].body", reason: "is empty" }],
|
|
73
|
+
[{ field: "pattern", reason: "missing" }],
|
|
59
74
|
false,
|
|
75
|
+
DEFAULT_OUTPUT_BUDGETS,
|
|
60
76
|
);
|
|
61
77
|
const result = JSON.parse(JSON.stringify({ content: [text(diagnostic)] }));
|
|
62
|
-
assert.equal(
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
assert.equal(Object.hasOwn(JSON.parse(diagnostic), "tool"), false);
|
|
79
|
+
assert.equal(Object.hasOwn(JSON.parse(diagnostic), "path"), false);
|
|
80
|
+
const report = reportOf(result, "grep");
|
|
81
|
+
assert.equal(report?.error, "INVALID_ARGUMENTS");
|
|
82
|
+
assert.equal(report?.tool, "grep");
|
|
83
|
+
const shown = displayed(result, "grep");
|
|
84
|
+
assert.match(shown, /pattern: missing/);
|
|
85
|
+
assert.match(shown, /grep not executed/);
|
|
65
86
|
assert.doesNotMatch(shown, /"issues":/);
|
|
66
87
|
});
|
|
67
88
|
|
|
68
|
-
test("argument reports with obsolete fields → remain raw on replay", () => {
|
|
69
|
-
for (const facts of [{ arguments: { path: "saved.txt" } }, { argumentsOmitted: true }]) {
|
|
70
|
-
const report = {
|
|
71
|
-
tool: "edit",
|
|
72
|
-
error: "INVALID_ARGUMENTS",
|
|
73
|
-
issues: [{ field: "edits[0].body", reason: "is empty" }],
|
|
74
|
-
...facts,
|
|
75
|
-
};
|
|
76
|
-
const diagnostic = JSON.stringify(report);
|
|
77
|
-
const result = { content: [text(diagnostic)], details: { report } };
|
|
78
|
-
assert.equal(decodeReport(diagnostic), undefined);
|
|
79
|
-
assert.equal(reportOf(result), undefined);
|
|
80
|
-
assert.equal(displayed(result).trimEnd(), diagnostic);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
|
|
84
89
|
test("invalid serialized report → retains the raw diagnostic", () => {
|
|
85
|
-
const valid = formatArgumentError(
|
|
90
|
+
const valid = formatArgumentError(
|
|
91
|
+
[{ field: "path", reason: "missing" }],
|
|
92
|
+
false,
|
|
93
|
+
DEFAULT_OUTPUT_BUDGETS,
|
|
94
|
+
);
|
|
86
95
|
for (const invalid of [
|
|
96
|
+
JSON.stringify({ ...JSON.parse(valid), foreign: true }),
|
|
87
97
|
'{"tool":"edit","arbitrary":"user file"}',
|
|
88
98
|
'{"tool":"replace","error":"NO_MATCH","message":"missing rule"}',
|
|
89
99
|
'{"tool":"write","error":"TARGET_EXISTS","rule":1}',
|
|
90
|
-
|
|
100
|
+
JSON.stringify({ ...JSON.parse(valid), tool: "foreign" }),
|
|
91
101
|
JSON.stringify({
|
|
92
102
|
...JSON.parse(valid),
|
|
93
103
|
issues: [{ field: "path", reason: "missing", foreign: true }],
|
|
@@ -99,14 +109,19 @@ test("invalid serialized report → retains the raw diagnostic", () => {
|
|
|
99
109
|
cause: { message: "native", foreign: true },
|
|
100
110
|
}),
|
|
101
111
|
]) {
|
|
102
|
-
assert.equal(decodeReport(invalid), undefined, invalid);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
assert.equal(decodeReport(invalid, "edit"), undefined, invalid);
|
|
113
|
+
const report: unknown = JSON.parse(invalid);
|
|
114
|
+
for (const details of [undefined, { report }]) {
|
|
115
|
+
const result = { content: [text(invalid)], details };
|
|
116
|
+
assert.equal(reportOf(result, "edit"), undefined);
|
|
117
|
+
assert.equal(
|
|
118
|
+
displayed(result, "edit")
|
|
119
|
+
.split("\n")
|
|
120
|
+
.map((line) => line.trimEnd())
|
|
121
|
+
.join("\n"),
|
|
122
|
+
invalid,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
110
125
|
}
|
|
111
126
|
});
|
|
112
127
|
|
|
@@ -115,11 +130,11 @@ test("native failure with deeply nested cause → retains the terminal diagnosti
|
|
|
115
130
|
let error: Error = terminal;
|
|
116
131
|
for (let index = 0; index < 12; index++)
|
|
117
132
|
error = Object.assign(new Error(`layer ${index}`, { cause: error }), { code: "EIO" });
|
|
118
|
-
const report = describeFailure("grep", error, { path: "source" });
|
|
133
|
+
const report = describeFailure("grep", error, { path: "source", budget: DEFAULT_OUTPUT_BUDGETS });
|
|
119
134
|
const serialized = renderReport(report);
|
|
120
|
-
const decoded = decodeReport(serialized);
|
|
135
|
+
const decoded = decodeReport(serialized, "grep");
|
|
121
136
|
assert.ok(decoded);
|
|
122
|
-
assert.deepEqual(decoded, JSON.parse(serialized));
|
|
137
|
+
assert.deepEqual(decoded, { tool: "grep", ...JSON.parse(serialized) });
|
|
123
138
|
let cause = decoded.cause;
|
|
124
139
|
while (cause?.cause) cause = cause.cause;
|
|
125
140
|
assert.equal(cause?.code, terminal.code);
|
|
@@ -132,7 +147,7 @@ test("wide and repeated cause graph with a cycle → retains every distinct fail
|
|
|
132
147
|
const errors = Array.from({ length: 24 }, (_, index) => new Error(`failure ${index}`));
|
|
133
148
|
const aggregate = new AggregateError([repeated, repeated, ...errors], "root");
|
|
134
149
|
aggregate.cause = aggregate;
|
|
135
|
-
const cause = causeOf(aggregate);
|
|
150
|
+
const cause = causeOf(aggregate, DEFAULT_OUTPUT_BUDGETS.fieldBytes);
|
|
136
151
|
assert.ok(cause);
|
|
137
152
|
assert.equal(cause.cause, undefined);
|
|
138
153
|
assert.deepEqual(
|
|
@@ -145,6 +160,7 @@ test("wide and repeated cause graph with a cycle → retains every distinct fail
|
|
|
145
160
|
test("oversized cause message → retains opening and terminal text within the message budget", () => {
|
|
146
161
|
const cause = causeOf(
|
|
147
162
|
new Error(`opening${"\u0000".repeat(DEFAULT_OUTPUT_BUDGETS.fieldBytes * 2)}terminal`),
|
|
163
|
+
DEFAULT_OUTPUT_BUDGETS.fieldBytes,
|
|
148
164
|
);
|
|
149
165
|
assert.ok(cause);
|
|
150
166
|
assert.match(cause.message, /^opening/);
|
|
@@ -157,7 +173,10 @@ test("native filesystem failure → reports its diagnostic once without a synthe
|
|
|
157
173
|
for (const code of ["EACCES", "ELOOP", "ENOTDIR"]) {
|
|
158
174
|
for (const message of [`${code}: cannot inspect source`, "cannot inspect source"]) {
|
|
159
175
|
const native = Object.assign(new Error(message), { code });
|
|
160
|
-
const report = describeFailure("grep", native, {
|
|
176
|
+
const report = describeFailure("grep", native, {
|
|
177
|
+
path: "source",
|
|
178
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
179
|
+
});
|
|
161
180
|
assert.equal(report.error, "FILESYSTEM_ERROR");
|
|
162
181
|
assert.equal(report.cause, undefined);
|
|
163
182
|
assert.match(report.message ?? "", new RegExp(code));
|
|
@@ -180,19 +199,19 @@ test("success reports → omit repeated call fields while retaining details and
|
|
|
180
199
|
assert.ok(last?.type === "text");
|
|
181
200
|
assert.deepEqual(JSON.parse(last.text), { publication: "PUBLISHED", created: true });
|
|
182
201
|
assert.deepEqual(result.details.report, report);
|
|
183
|
-
assert.deepEqual(
|
|
202
|
+
assert.deepEqual(splitReport(result), { content: payload, report });
|
|
184
203
|
assert.equal(result.content.length, 2);
|
|
185
204
|
});
|
|
186
205
|
|
|
187
|
-
test("success facts without a tool →
|
|
206
|
+
test("success facts without a tool → use call context and enforce tool-specific fields", () => {
|
|
188
207
|
const report: Report = { tool: "write", publication: "PUBLISHED", created: true };
|
|
189
208
|
const serialized = renderReport(report);
|
|
190
|
-
assert.equal(decodeReport(serialized), undefined);
|
|
191
209
|
assert.deepEqual(decodeReport(serialized, "write"), report);
|
|
192
|
-
for (const tool of ["read", "
|
|
210
|
+
for (const tool of ["read", "grep", "edit", "replace"] as const)
|
|
211
|
+
assert.equal(decodeReport(serialized, tool), undefined);
|
|
193
212
|
for (const invalid of [
|
|
194
213
|
'{"created":true,"foreign":true}',
|
|
195
|
-
'{"error":"
|
|
214
|
+
'{"error":"INVALID_ARGUMENTS","message":"missing issues"}',
|
|
196
215
|
'{"tool":"foreign","created":true}',
|
|
197
216
|
"[]",
|
|
198
217
|
"null",
|
|
@@ -200,20 +219,28 @@ test("success facts without a tool → require call context and still enforce to
|
|
|
200
219
|
assert.equal(decodeReport(invalid, "write"), undefined);
|
|
201
220
|
});
|
|
202
221
|
|
|
203
|
-
test("
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
222
|
+
test("checked report with a different trailing block → preserves the entire payload", () => {
|
|
223
|
+
const report: SuccessReport = { tool: "write", path: "saved.txt", publication: "PUBLISHED" };
|
|
224
|
+
for (const last of [
|
|
225
|
+
text(JSON.stringify(report, null, 2)),
|
|
226
|
+
text(JSON.stringify({ publication: "NOT_PUBLISHED" })),
|
|
227
|
+
text(renderReport(report) + "\n"),
|
|
228
|
+
{ type: "image" as const, data: "iVBORw0KGgo=", mimeType: "image/png" },
|
|
229
|
+
]) {
|
|
230
|
+
const content = [text("saved payload"), last];
|
|
231
|
+
const result: Parameters<typeof splitReport>[0] = { content, details: { report } };
|
|
232
|
+
assert.deepEqual(splitReport(result), { content, report });
|
|
233
|
+
assert.equal(splitReport(result).content, content);
|
|
234
|
+
}
|
|
211
235
|
});
|
|
212
236
|
|
|
213
237
|
test("report-contract: native failure with an independent cause → retains the underlying diagnostic", () => {
|
|
214
238
|
const underlying = Object.assign(new Error("disk read failed"), { code: "EIO" });
|
|
215
239
|
const native = new Error("cannot inspect source", { cause: underlying });
|
|
216
|
-
const report = describeFailure("read", native, {
|
|
240
|
+
const report = describeFailure("read", native, {
|
|
241
|
+
path: "source",
|
|
242
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
243
|
+
});
|
|
217
244
|
assert.equal(report.message, native.message);
|
|
218
245
|
assert.equal(report.cause?.message, underlying.message);
|
|
219
246
|
assert.equal(report.cause?.code, "EIO");
|
|
@@ -227,7 +254,7 @@ test("grep success and incomplete candidate facts → survive report serializati
|
|
|
227
254
|
tool: "grep",
|
|
228
255
|
matches: 0,
|
|
229
256
|
candidates: "all",
|
|
230
|
-
scanLimit:
|
|
257
|
+
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
231
258
|
},
|
|
232
259
|
{
|
|
233
260
|
tool: "grep",
|
|
@@ -235,12 +262,12 @@ test("grep success and incomplete candidate facts → survive report serializati
|
|
|
235
262
|
message: "Search incomplete.",
|
|
236
263
|
diagnostics: "unreadable file",
|
|
237
264
|
candidates: "all",
|
|
238
|
-
scanLimit:
|
|
265
|
+
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
239
266
|
},
|
|
240
267
|
] satisfies Report[]) {
|
|
241
268
|
const serialized = renderReport(report);
|
|
242
269
|
assert.deepEqual(decodeReport(serialized, report.tool), report);
|
|
243
|
-
assert.deepEqual(reportOf({ content: [text(serialized)],
|
|
270
|
+
assert.deepEqual(reportOf({ content: [text(serialized)] }, report.tool), report);
|
|
244
271
|
}
|
|
245
272
|
});
|
|
246
273
|
|
|
@@ -252,15 +279,16 @@ test("match and scan limits → recovery prioritizes the match limit", () => {
|
|
|
252
279
|
matches: 1,
|
|
253
280
|
matchLimit: 1,
|
|
254
281
|
candidates: "all",
|
|
255
|
-
scanLimit:
|
|
282
|
+
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
256
283
|
});
|
|
257
|
-
assert.equal(reportOf(result)?.next, next);
|
|
284
|
+
assert.equal(reportOf(result, "grep")?.next, next);
|
|
258
285
|
});
|
|
259
286
|
|
|
260
|
-
test("text-only error
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
assert.equal(
|
|
287
|
+
test("text-only error contains facts invalid for the originating tool → preserves the raw diagnostic", () => {
|
|
288
|
+
const diagnostic = '{"publication":"PUBLISHED"}';
|
|
289
|
+
const result = { content: [text(diagnostic)] };
|
|
290
|
+
assert.equal(reportOf(result, "read"), undefined);
|
|
291
|
+
assert.equal(displayed(result, "read").trimEnd(), diagnostic);
|
|
264
292
|
});
|
|
265
293
|
|
|
266
294
|
test("invalid details report → falls back to valid text and leaves content intact", () => {
|
|
@@ -268,9 +296,48 @@ test("invalid details report → falls back to valid text and leaves content int
|
|
|
268
296
|
const fallback: SuccessReport = { tool: "grep", matches: 1, files: 1 };
|
|
269
297
|
for (const expected of [undefined, fallback]) {
|
|
270
298
|
const last = expected ? renderReport(expected) : JSON.stringify({ matches: "x" }, null, 2);
|
|
271
|
-
const result = {
|
|
272
|
-
assert.deepEqual(reportOf(result), expected);
|
|
273
|
-
assert.deepEqual(
|
|
299
|
+
const result = { details, content: [text("payload"), text(last)] };
|
|
300
|
+
assert.deepEqual(reportOf(result, "grep"), expected);
|
|
301
|
+
assert.deepEqual(splitReport(result), { content: result.content, report: undefined });
|
|
274
302
|
assert.equal(result.content.length, 2);
|
|
275
303
|
}
|
|
276
304
|
});
|
|
305
|
+
|
|
306
|
+
test("output byte limit → follows the scan limit and precedes the line preview limit", () => {
|
|
307
|
+
const serialized = renderReport({
|
|
308
|
+
candidates: "all",
|
|
309
|
+
scanLimit: formatByteSize(MAX_UNNARROWED_SCAN_BYTES),
|
|
310
|
+
outputLimit: formatByteSize(DEFAULT_OUTPUT_BUDGETS.grepBytes),
|
|
311
|
+
linePreviewLimit: DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits,
|
|
312
|
+
});
|
|
313
|
+
assert.deepEqual(Object.keys(JSON.parse(serialized)), [
|
|
314
|
+
"candidates",
|
|
315
|
+
"scanLimit",
|
|
316
|
+
"outputLimit",
|
|
317
|
+
"linePreviewLimit",
|
|
318
|
+
]);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("hooked details contain a cyclic report cause → preserves payload and decodes the text report", () => {
|
|
322
|
+
const cause: { message: string; cause?: unknown } = { message: "loop" };
|
|
323
|
+
cause.cause = cause;
|
|
324
|
+
const fallback = {
|
|
325
|
+
tool: "read" as const,
|
|
326
|
+
error: "UNCLASSIFIED" as const,
|
|
327
|
+
message: "raw diagnostic",
|
|
328
|
+
};
|
|
329
|
+
const content = [text(renderReport(fallback))];
|
|
330
|
+
const result = { details: { report: { ...fallback, cause } }, content };
|
|
331
|
+
assert.deepEqual(reportOf(result, "read"), fallback);
|
|
332
|
+
assert.deepEqual(splitReport(result), { content, report: undefined });
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test("native mutation failure before commit → retains publication and stage", () => {
|
|
336
|
+
for (const tool of ["edit", "replace", "write"] as const) {
|
|
337
|
+
const report = describeFailure(tool, new Error("native failure"), {
|
|
338
|
+
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
339
|
+
});
|
|
340
|
+
assert.equal(report.publication, "NOT_PUBLISHED", tool);
|
|
341
|
+
assert.equal(report.stage, "prepare", tool);
|
|
342
|
+
}
|
|
343
|
+
});
|
package/src/pi/report.ts
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
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
6
|
* owns publication, stage, and freshness; each tool owns its outcome facts;
|
|
7
|
-
* Action Fusion owns `then_run
|
|
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
|
-
* one model-facing serialization: one JSON object in schema order,
|
|
10
|
-
*
|
|
9
|
+
* one model-facing serialization: one JSON object in schema order, without the
|
|
10
|
+
* call's own tool and path. A success puts it after the content payload;
|
|
11
11
|
* a failure is the report alone. The TUI receives the full report in details.
|
|
12
12
|
*
|
|
13
13
|
* Throw sites classify failures as `HashlineError` (core/errors.ts); a failure
|
|
@@ -20,12 +20,13 @@ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
|
20
20
|
import { Value } from "typebox/value";
|
|
21
21
|
import { errorMessage, errnoCode, filesystemErrorCode, HashlineError } from "../core/errors.ts";
|
|
22
22
|
import { boundedText, causeOf } from "./report-cause.ts";
|
|
23
|
-
import {
|
|
23
|
+
import type { OutputBudgets } from "./budgets.ts";
|
|
24
24
|
export { boundedText, causeOf } from "./report-cause.ts";
|
|
25
25
|
import { cancellationError } from "./error-text.ts";
|
|
26
26
|
import { FileMutationError } from "./file-commit.ts";
|
|
27
27
|
import {
|
|
28
28
|
ERROR_DEFINITIONS,
|
|
29
|
+
MUTATION_TOOLS,
|
|
29
30
|
OUTCOME_NEXT,
|
|
30
31
|
PUBLICATION_NEXT,
|
|
31
32
|
REPORT_SCHEMA,
|
|
@@ -48,37 +49,32 @@ export interface ReportDetails<TReport extends Report = Report> {
|
|
|
48
49
|
|
|
49
50
|
type Content = AgentToolResult<unknown>["content"];
|
|
50
51
|
|
|
52
|
+
/** What a report's text states: the originating call supplies its tool and path. */
|
|
53
|
+
export type ReportText<R extends Report = Report> = R extends unknown
|
|
54
|
+
? Omit<R, "tool" | "path">
|
|
55
|
+
: never;
|
|
56
|
+
|
|
51
57
|
/** The fields a report states, in schema order; both renderers list them in this order. */
|
|
52
|
-
export function reportEntries(report:
|
|
58
|
+
export function reportEntries(report: ReportText): [string, unknown][] {
|
|
53
59
|
return Object.keys(REPORT_SCHEMA.properties).flatMap((key) => {
|
|
54
60
|
const value = (report as Record<string, unknown>)[key];
|
|
55
61
|
return value === undefined ? [] : [[key, value] as [string, unknown]];
|
|
56
62
|
});
|
|
57
63
|
}
|
|
58
64
|
|
|
59
|
-
/** Serialize facts in schema order;
|
|
60
|
-
export function renderReport(report:
|
|
61
|
-
const entries = reportEntries(report).filter(
|
|
62
|
-
([key]) => report.error !== undefined || !ENVELOPE.has(key),
|
|
63
|
-
);
|
|
65
|
+
/** Serialize facts in schema order; a result never repeats its call's tool and path. */
|
|
66
|
+
export function renderReport(report: ReportText): string {
|
|
67
|
+
const entries = reportEntries(report).filter(([key]) => !ENVELOPE.has(key));
|
|
64
68
|
return JSON.stringify(Object.fromEntries(entries), null, 2);
|
|
65
69
|
}
|
|
66
70
|
|
|
67
|
-
/** Decode a text report;
|
|
68
|
-
export function decodeReport(text: string, contextTool
|
|
71
|
+
/** Decode a text report; the text omits its tool, which the originating call supplies. */
|
|
72
|
+
export function decodeReport(text: string, contextTool: ReportTool): Report | undefined {
|
|
69
73
|
if (!text.startsWith("{")) return undefined;
|
|
70
74
|
try {
|
|
71
75
|
let value: unknown = JSON.parse(text);
|
|
72
|
-
if (
|
|
73
|
-
contextTool !== undefined &&
|
|
74
|
-
typeof value === "object" &&
|
|
75
|
-
value !== null &&
|
|
76
|
-
!Array.isArray(value) &&
|
|
77
|
-
!("tool" in value) &&
|
|
78
|
-
!("error" in value)
|
|
79
|
-
) {
|
|
76
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value) && !("tool" in value))
|
|
80
77
|
value = { tool: contextTool, ...value };
|
|
81
|
-
}
|
|
82
78
|
return Value.Check(WIRE_REPORT_SCHEMA, value) ? (value as Report) : undefined;
|
|
83
79
|
} catch (error) {
|
|
84
80
|
if (error instanceof SyntaxError || error instanceof RangeError || error instanceof TypeError)
|
|
@@ -94,24 +90,27 @@ export function decodeReport(text: string, contextTool?: string): Report | undef
|
|
|
94
90
|
export function detailsReport(details: unknown): Report | undefined {
|
|
95
91
|
const report: unknown =
|
|
96
92
|
typeof details === "object" && details !== null ? Reflect.get(details, "report") : undefined;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
try {
|
|
94
|
+
// Hooks can introduce cycles; reject non-JSON reports before recursive schema validation.
|
|
95
|
+
JSON.stringify(report);
|
|
96
|
+
return report !== undefined && Value.Check(WIRE_REPORT_SCHEMA, report)
|
|
97
|
+
? (report as Report)
|
|
98
|
+
: undefined;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (error instanceof TypeError || error instanceof RangeError) return undefined;
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
/** Tool results carry their report in details; Pi argument failures carry it as their final text block. */
|
|
103
|
-
export function reportOf(
|
|
104
|
-
readonly content?: Content;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
readonly isError?: boolean;
|
|
108
|
-
}): Report | undefined {
|
|
106
|
+
export function reportOf(
|
|
107
|
+
result: { readonly content?: Content; readonly details?: unknown },
|
|
108
|
+
tool: ReportTool,
|
|
109
|
+
): Report | undefined {
|
|
109
110
|
const report = detailsReport(result.details);
|
|
110
111
|
if (report) return report;
|
|
111
112
|
const last = result.content?.at(-1);
|
|
112
|
-
return last?.type === "text"
|
|
113
|
-
? decodeReport(last.text, result.isError ? undefined : result.toolName)
|
|
114
|
-
: undefined;
|
|
113
|
+
return last?.type === "text" ? decodeReport(last.text, tool) : undefined;
|
|
115
114
|
}
|
|
116
115
|
|
|
117
116
|
const ENVELOPE = new Set(["tool", "path"]);
|
|
@@ -158,13 +157,11 @@ export function failureResult(
|
|
|
158
157
|
}
|
|
159
158
|
|
|
160
159
|
/** Where a failure happened, as the caller knows it. */
|
|
161
|
-
|
|
160
|
+
interface FailureContext {
|
|
162
161
|
/** The path or paths the call names, as supplied. */
|
|
163
162
|
readonly path?: string | readonly string[];
|
|
164
|
-
/** Mutation tools report `publication` and `stage` on every failure. */
|
|
165
|
-
readonly mutation?: boolean;
|
|
166
163
|
readonly signal?: AbortSignal;
|
|
167
|
-
readonly budget
|
|
164
|
+
readonly budget: OutputBudgets;
|
|
168
165
|
}
|
|
169
166
|
|
|
170
167
|
/**
|
|
@@ -174,7 +171,6 @@ export interface FailureContext {
|
|
|
174
171
|
* imports this module.
|
|
175
172
|
*/
|
|
176
173
|
export class CommandNotRun extends Error {
|
|
177
|
-
readonly error: unknown;
|
|
178
174
|
readonly status: Extract<CommandStatus, "skipped" | "cancelled">;
|
|
179
175
|
readonly progressError: Cause | undefined;
|
|
180
176
|
|
|
@@ -185,7 +181,6 @@ export class CommandNotRun extends Error {
|
|
|
185
181
|
) {
|
|
186
182
|
super(errorMessage(error), { cause: error });
|
|
187
183
|
this.name = "CommandNotRun";
|
|
188
|
-
this.error = error;
|
|
189
184
|
this.status = status;
|
|
190
185
|
this.progressError = progressError;
|
|
191
186
|
}
|
|
@@ -225,16 +220,18 @@ export function describeFailure(
|
|
|
225
220
|
let command: CommandNotRun | undefined;
|
|
226
221
|
if (error instanceof CommandNotRun) {
|
|
227
222
|
command = error;
|
|
228
|
-
|
|
223
|
+
// The mutation's own failure, unchanged.
|
|
224
|
+
error = error.cause;
|
|
229
225
|
}
|
|
230
226
|
const failure = classify(error, context.signal);
|
|
231
|
-
const budget = context.budget
|
|
232
|
-
const
|
|
227
|
+
const budget = context.budget;
|
|
228
|
+
const mutation = MUTATION_TOOLS.some((name) => name === tool);
|
|
229
|
+
const publication = mutation
|
|
233
230
|
? failure instanceof FileMutationError
|
|
234
231
|
? failure.publication
|
|
235
232
|
: ("NOT_PUBLISHED" as const)
|
|
236
233
|
: undefined;
|
|
237
|
-
const stage =
|
|
234
|
+
const stage = mutation
|
|
238
235
|
? failure instanceof FileMutationError
|
|
239
236
|
? failure.stage
|
|
240
237
|
: ("prepare" as const)
|