@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
package/src/pi/report.ts
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
* one model-facing serialization: one JSON object in schema order, without the
|
|
10
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
|
+
* Scripts receive a success as one object with the same fields, and the facts
|
|
13
|
+
* the text omits because the payload or the call already shows them; a failure
|
|
14
|
+
* carries none, so their call rejects with the report text.
|
|
12
15
|
*
|
|
13
16
|
* Throw sites classify failures as `HashlineError` (core/errors.ts); a failure
|
|
14
17
|
* beneath the failing step becomes structured `cause`, never part of `message`.
|
|
@@ -17,6 +20,7 @@
|
|
|
17
20
|
*/
|
|
18
21
|
|
|
19
22
|
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
23
|
+
import { contentText } from "@earendil-works/pi-ai";
|
|
20
24
|
import { Value } from "typebox/value";
|
|
21
25
|
import {
|
|
22
26
|
errorMessage,
|
|
@@ -26,14 +30,17 @@ import {
|
|
|
26
30
|
pathError,
|
|
27
31
|
} from "../core/errors.ts";
|
|
28
32
|
import { boundedText, causeOf } from "./report-cause.ts";
|
|
29
|
-
import type
|
|
33
|
+
import { leadingEntriesWithin, type OutputBudgets } from "./budgets.ts";
|
|
30
34
|
import { cancellationError } from "./error-text.ts";
|
|
35
|
+
import { serializeAbsolutePath } from "./path.ts";
|
|
31
36
|
import {
|
|
37
|
+
BOUNDED_LISTS,
|
|
32
38
|
ERROR_DEFINITIONS,
|
|
33
39
|
MODEL_OMITTED_FIELDS,
|
|
34
40
|
OUTCOME_NEXT,
|
|
35
41
|
PUBLICATION_NEXT,
|
|
36
42
|
REPORT_SCHEMA,
|
|
43
|
+
SCRIPT_OUTPUT_SCHEMAS,
|
|
37
44
|
WIRE_REPORT_SCHEMA,
|
|
38
45
|
type ErrorCode,
|
|
39
46
|
type FailureReport,
|
|
@@ -41,12 +48,15 @@ import {
|
|
|
41
48
|
type ModelOmittedField,
|
|
42
49
|
type Report,
|
|
43
50
|
type ReportTool,
|
|
51
|
+
type SuccessOutcome,
|
|
44
52
|
type SuccessReport,
|
|
45
53
|
} from "./report-schema.ts";
|
|
46
54
|
|
|
47
55
|
/** Every result's details carry its report for the TUI; Pi does not send details to the model. */
|
|
48
56
|
export interface ReportDetails<TReport extends Report = Report> {
|
|
49
57
|
readonly report: TReport;
|
|
58
|
+
/** Zero-based index of the report block appended to a success result, if any. */
|
|
59
|
+
readonly reportBlockIndex?: number;
|
|
50
60
|
}
|
|
51
61
|
|
|
52
62
|
type Content = AgentToolResult<unknown>["content"];
|
|
@@ -64,27 +74,32 @@ export function reportEntries(report: ReportText): [string, unknown][] {
|
|
|
64
74
|
});
|
|
65
75
|
}
|
|
66
76
|
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
([key]) => !MODEL_OMITTED_FIELDS.has(key as ModelOmittedField),
|
|
77
|
+
/** The fields a report's text states, in schema order: a result never repeats its call's tool and path. */
|
|
78
|
+
function statedFields(report: ReportText): Record<string, unknown> {
|
|
79
|
+
return Object.fromEntries(
|
|
80
|
+
reportEntries(report).filter(([key]) => !MODEL_OMITTED_FIELDS.has(key as ModelOmittedField)),
|
|
71
81
|
);
|
|
72
|
-
return JSON.stringify(Object.fromEntries(entries), null, 2);
|
|
73
82
|
}
|
|
74
83
|
|
|
75
|
-
/**
|
|
84
|
+
/** Serialize the stated fields as the one JSON object a result's text carries. */
|
|
85
|
+
export function renderReport(report: ReportText): string {
|
|
86
|
+
return JSON.stringify(statedFields(report), null, 2);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function checkWireReport(value: unknown): Report | undefined {
|
|
90
|
+
// Report retains the checked variants that TypeBox cannot infer from the assembled union.
|
|
91
|
+
return Value.Check(WIRE_REPORT_SCHEMA, value) ? (value as Report) : undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Only text reports may omit their tool; the originating call supplies it. */
|
|
76
95
|
function decodeReport(text: string, contextTool: ReportTool): Report | undefined {
|
|
77
96
|
if (!text.startsWith("{")) return undefined;
|
|
78
97
|
try {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
value = { tool: contextTool, ...value };
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return Value.Check(WIRE_REPORT_SCHEMA, value) ? (value as Report) : undefined;
|
|
98
|
+
// A successful JSON parse starting with '{' produces an object.
|
|
99
|
+
const value = JSON.parse(text) as Record<string, unknown>;
|
|
100
|
+
const hasTool = "tool" in value;
|
|
101
|
+
if (hasTool && value.tool !== contextTool) return undefined;
|
|
102
|
+
return checkWireReport(hasTool ? value : { tool: contextTool, ...value });
|
|
88
103
|
} catch (error) {
|
|
89
104
|
if (error instanceof SyntaxError || error instanceof RangeError || error instanceof TypeError)
|
|
90
105
|
return undefined;
|
|
@@ -102,9 +117,8 @@ export function detailsReport(details: unknown, contextTool: ReportTool): Report
|
|
|
102
117
|
try {
|
|
103
118
|
// Hooks can introduce cycles; reject non-JSON reports before recursive schema validation.
|
|
104
119
|
JSON.stringify(report);
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return checked.tool === contextTool ? checked : undefined;
|
|
120
|
+
const checked = report === undefined ? undefined : checkWireReport(report);
|
|
121
|
+
return checked?.tool === contextTool ? checked : undefined;
|
|
108
122
|
} catch (error) {
|
|
109
123
|
if (error instanceof TypeError || error instanceof RangeError) return undefined;
|
|
110
124
|
throw error;
|
|
@@ -123,39 +137,81 @@ export function reportOf(
|
|
|
123
137
|
}
|
|
124
138
|
|
|
125
139
|
/**
|
|
126
|
-
* The recovery a successful result's facts call for: the first
|
|
127
|
-
* producer's selected alternative when that rule names it.
|
|
140
|
+
* The recovery a successful result's facts call for: the rule of the first fact present in
|
|
141
|
+
* OUTCOME_NEXT order, in the producer's selected alternative when that rule names it. The value
|
|
142
|
+
* the rule excepts calls for none, and no later rule applies.
|
|
128
143
|
*/
|
|
129
144
|
function outcomeNext(report: Report, variant: OutcomeVariant | undefined): string | undefined {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
145
|
+
const rule = OUTCOME_NEXT.find(([fact]) => report[fact] !== undefined);
|
|
146
|
+
if (!rule || report[rule[0]] === rule[2]) return undefined;
|
|
147
|
+
const next = rule[1];
|
|
133
148
|
return typeof next === "object" ? ((variant && next[variant]) ?? next.default) : next;
|
|
134
149
|
}
|
|
135
150
|
|
|
136
151
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
152
|
+
* The facts every report states as producers gave them, made fit for output: resolved paths
|
|
153
|
+
* spelled as headers spell paths, then each bounded list cut to whole entries within the
|
|
154
|
+
* diagnostic block, counting what it omits.
|
|
155
|
+
*/
|
|
156
|
+
function boundFacts<R extends Partial<Report>>(report: R, budget: OutputBudgets): R {
|
|
157
|
+
const bounded: Record<string, unknown> = report.missing
|
|
158
|
+
? { ...report, missing: report.missing.map(serializeAbsolutePath) }
|
|
159
|
+
: { ...report };
|
|
160
|
+
for (const [list, omittedField] of BOUNDED_LISTS) {
|
|
161
|
+
const entries = bounded[list] as readonly unknown[] | undefined;
|
|
162
|
+
if (!entries) continue;
|
|
163
|
+
const kept = leadingEntriesWithin(entries, budget.blockBytes);
|
|
164
|
+
if (kept.length === entries.length) continue;
|
|
165
|
+
bounded[list] = kept;
|
|
166
|
+
bounded[omittedField] = entries.length - kept.length;
|
|
167
|
+
}
|
|
168
|
+
return bounded as R;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Project one complete outcome into the script fields and report. The report follows the
|
|
173
|
+
* supplied payload and omits facts already shown by the call or payload.
|
|
140
174
|
*/
|
|
141
175
|
export function reportResult<TDetails extends object | undefined>(
|
|
142
|
-
|
|
143
|
-
payload: Content
|
|
144
|
-
|
|
145
|
-
|
|
176
|
+
outcome: SuccessOutcome,
|
|
177
|
+
payload: Content,
|
|
178
|
+
budget: OutputBudgets,
|
|
179
|
+
{
|
|
180
|
+
details,
|
|
181
|
+
variant,
|
|
182
|
+
}: {
|
|
183
|
+
readonly details?: TDetails;
|
|
184
|
+
readonly variant?: OutcomeVariant;
|
|
185
|
+
} = {},
|
|
146
186
|
): AgentToolResult<TDetails & ReportDetails<SuccessReport>> {
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
) as SuccessReport;
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
187
|
+
const bounded = boundFacts(outcome, budget);
|
|
188
|
+
const command = bounded.then_run;
|
|
189
|
+
const complete = Object.fromEntries(reportEntries(bounded)) as SuccessReport;
|
|
190
|
+
if (bounded.callShowsNextOffset) delete complete.nextOffset;
|
|
191
|
+
if (bounded.payloadShowsCounts) {
|
|
192
|
+
delete complete.matches;
|
|
193
|
+
delete complete.files;
|
|
194
|
+
}
|
|
195
|
+
if (command) complete.then_run = { status: command.status };
|
|
196
|
+
const next = outcomeNext(complete, variant);
|
|
197
|
+
if (next !== undefined) complete.next = next;
|
|
198
|
+
const appendReport =
|
|
199
|
+
reportEntries(complete).some(([key]) => !MODEL_OMITTED_FIELDS.has(key as ModelOmittedField)) ||
|
|
200
|
+
payload.length === 0;
|
|
153
201
|
return {
|
|
154
|
-
content:
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
202
|
+
content: appendReport ? [...payload, { type: "text", text: renderReport(complete) }] : payload,
|
|
203
|
+
details: {
|
|
204
|
+
...details,
|
|
205
|
+
report: complete,
|
|
206
|
+
...(appendReport ? { reportBlockIndex: payload.length } : {}),
|
|
207
|
+
} as TDetails & ReportDetails<SuccessReport>,
|
|
208
|
+
// The schema-derived outcome carries only JSON facts. The codec projects them in wire order.
|
|
209
|
+
structuredContent: {
|
|
210
|
+
...("text" in SCRIPT_OUTPUT_SCHEMAS[bounded.tool].properties
|
|
211
|
+
? { text: contentText(payload) }
|
|
212
|
+
: {}),
|
|
213
|
+
...statedFields({ ...bounded, next }),
|
|
214
|
+
} as NonNullable<AgentToolResult<unknown>["structuredContent"]>,
|
|
159
215
|
};
|
|
160
216
|
}
|
|
161
217
|
|
|
@@ -173,17 +229,17 @@ function failureResult(report: FailureReport): AgentToolResult<ReportDetails<Fai
|
|
|
173
229
|
export interface FailureContext {
|
|
174
230
|
/** The path or paths the call names, as supplied. */
|
|
175
231
|
readonly path?: string | readonly string[];
|
|
176
|
-
readonly signal
|
|
232
|
+
readonly signal: AbortSignal;
|
|
177
233
|
readonly budget: OutputBudgets;
|
|
178
234
|
}
|
|
179
235
|
|
|
180
236
|
/** What a mutation tool states about its failure beside the classified error. */
|
|
181
237
|
type MutationFailureFacts = Pick<FailureReport, "publication" | "then_run" | "progressError">;
|
|
182
238
|
|
|
183
|
-
function classify(error: unknown, signal: AbortSignal
|
|
239
|
+
function classify(error: unknown, signal: AbortSignal): HashlineError {
|
|
184
240
|
if (error instanceof HashlineError) return error;
|
|
185
241
|
// Pi's native tools and Node report cancellation with their own errors.
|
|
186
|
-
if (signal
|
|
242
|
+
if (signal.aborted) return cancellationError();
|
|
187
243
|
const path = pathError(error);
|
|
188
244
|
if (path) return path;
|
|
189
245
|
const code: ErrorCode = filesystemErrorCode(error) ?? "UNCLASSIFIED";
|
|
@@ -196,37 +252,50 @@ function classify(error: unknown, signal: AbortSignal | undefined): HashlineErro
|
|
|
196
252
|
|
|
197
253
|
/** The recovery of a failure: publication first, then the throw site's alternative or the code's default. */
|
|
198
254
|
function failureNext(
|
|
199
|
-
failure: HashlineError,
|
|
255
|
+
failure: HashlineError | undefined,
|
|
200
256
|
publication: Report["publication"],
|
|
201
257
|
): string | undefined {
|
|
202
258
|
if (publication === "PUBLISHED" || publication === "UNKNOWN")
|
|
203
259
|
return PUBLICATION_NEXT[publication];
|
|
260
|
+
if (!failure) return undefined;
|
|
204
261
|
const next = (ERROR_DEFINITIONS[failure.errorCode] as { next?: Record<string, string> }).next;
|
|
205
262
|
return next?.[(failure.next as string | undefined) ?? "default"];
|
|
206
263
|
}
|
|
207
264
|
|
|
208
|
-
/** Classify a caught failure
|
|
265
|
+
/** Classify a caught failure, retaining the caller's settled facts if inspection fails. */
|
|
209
266
|
export function describeFailure(
|
|
210
267
|
tool: ReportTool,
|
|
211
|
-
|
|
268
|
+
error: unknown,
|
|
212
269
|
context: FailureContext,
|
|
213
|
-
|
|
270
|
+
facts: MutationFailureFacts = {},
|
|
214
271
|
): FailureReport {
|
|
215
|
-
const
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
272
|
+
const { budget, signal } = context;
|
|
273
|
+
const envelope = { tool, path: context.path as Report["path"] };
|
|
274
|
+
try {
|
|
275
|
+
const failure = classify(error, signal);
|
|
276
|
+
return boundFacts(
|
|
277
|
+
// The constructor ties the facts to the code; the union cannot carry that link.
|
|
278
|
+
{
|
|
279
|
+
...failure.facts,
|
|
280
|
+
...envelope,
|
|
281
|
+
...facts,
|
|
282
|
+
error: failure.errorCode,
|
|
283
|
+
message: boundedText(failure.message, budget.fieldBytes),
|
|
284
|
+
cause: causeOf(failure.cause, budget.fieldBytes),
|
|
285
|
+
next: failureNext(failure, facts.publication),
|
|
286
|
+
} as FailureReport,
|
|
287
|
+
budget,
|
|
288
|
+
);
|
|
289
|
+
} catch {
|
|
290
|
+
// Preserve the original diagnostic and the mutation facts known before inspection failed.
|
|
291
|
+
return {
|
|
292
|
+
...envelope,
|
|
293
|
+
...facts,
|
|
294
|
+
error: "UNCLASSIFIED",
|
|
295
|
+
message: boundedText(errorMessage(error), budget.fieldBytes),
|
|
296
|
+
next: failureNext(undefined, facts.publication),
|
|
297
|
+
} as FailureReport;
|
|
298
|
+
}
|
|
230
299
|
}
|
|
231
300
|
|
|
232
301
|
/** Run one tool execution, returning any failure as its report. */
|
package/src/pi/rg-process.ts
CHANGED
|
@@ -33,6 +33,8 @@ export function rgPatternArgUnits(pattern: string): number {
|
|
|
33
33
|
|
|
34
34
|
const MAX_RG_RECORD_BYTES = 16 * 1024 * 1024;
|
|
35
35
|
const RG_RECORD_LIMIT_MESSAGE = `ripgrep output record exceeds ${formatByteSize(MAX_RG_RECORD_BYTES)}.`;
|
|
36
|
+
const NUL = 0x00;
|
|
37
|
+
const LF = 0x0a;
|
|
36
38
|
|
|
37
39
|
/** Bounded ripgrep stderr retains the beginning and end with an omission notice. */
|
|
38
40
|
const MAX_RG_STDERR_BYTES = 64 * 1024;
|
|
@@ -79,11 +81,13 @@ export function isRgArgumentLimit(error: unknown): boolean {
|
|
|
79
81
|
return code === "ENAMETOOLONG" || code === "E2BIG";
|
|
80
82
|
}
|
|
81
83
|
|
|
82
|
-
|
|
84
|
+
/** rg resolves `--glob` patterns against its working directory: the tool's workspace, where Pi's bash also runs. */
|
|
85
|
+
function startRg(args: readonly string[], cwd: string, signal: AbortSignal): RunningProcess {
|
|
83
86
|
throwIfCancelled(signal);
|
|
84
87
|
let child: ChildProcessByStdio<null, Readable, Readable>;
|
|
85
88
|
try {
|
|
86
89
|
child = spawn(rgPath, [...args], {
|
|
90
|
+
cwd,
|
|
87
91
|
stdio: ["ignore", "pipe", "pipe"],
|
|
88
92
|
shell: false,
|
|
89
93
|
});
|
|
@@ -99,7 +103,7 @@ function startRg(args: readonly string[], signal?: AbortSignal): RunningProcess
|
|
|
99
103
|
if (closed) return;
|
|
100
104
|
child.kill("SIGTERM");
|
|
101
105
|
timer ??= setTimeout(() => {
|
|
102
|
-
|
|
106
|
+
child.kill("SIGKILL");
|
|
103
107
|
}, 1000);
|
|
104
108
|
timer.unref();
|
|
105
109
|
};
|
|
@@ -114,32 +118,35 @@ function startRg(args: readonly string[], signal?: AbortSignal): RunningProcess
|
|
|
114
118
|
child.on("close", (code) => {
|
|
115
119
|
closed = true;
|
|
116
120
|
if (timer) clearTimeout(timer);
|
|
117
|
-
signal
|
|
121
|
+
signal.removeEventListener("abort", kill);
|
|
118
122
|
stderr.append(stderrDecoder.end());
|
|
119
123
|
resolve({ code, stderr: stderr.toString(), error });
|
|
120
124
|
});
|
|
121
125
|
});
|
|
122
|
-
signal
|
|
126
|
+
signal.addEventListener("abort", kill, { once: true });
|
|
123
127
|
return { child, done, kill };
|
|
124
128
|
}
|
|
125
129
|
|
|
130
|
+
function checkRecordSize(bytes: number): void {
|
|
131
|
+
if (bytes > MAX_RG_RECORD_BYTES)
|
|
132
|
+
throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
|
|
133
|
+
}
|
|
134
|
+
|
|
126
135
|
async function* nulDelimitedRecords(stream: Readable): AsyncGenerator<Buffer> {
|
|
127
136
|
let pending: Buffer = Buffer.alloc(0);
|
|
128
137
|
for await (const value of stream) {
|
|
129
138
|
const chunk = value as Buffer;
|
|
130
|
-
|
|
131
|
-
let
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
buf = buf.subarray(at + 1);
|
|
139
|
+
const buf = pending.length ? Buffer.concat([pending, chunk]) : chunk;
|
|
140
|
+
let at = 0;
|
|
141
|
+
for (;;) {
|
|
142
|
+
const end = buf.indexOf(NUL, at);
|
|
143
|
+
if (end === -1) break;
|
|
144
|
+
checkRecordSize(end - at);
|
|
145
|
+
yield buf.subarray(at, end);
|
|
146
|
+
at = end + 1;
|
|
139
147
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
pending = buf;
|
|
148
|
+
pending = buf.subarray(at);
|
|
149
|
+
checkRecordSize(pending.length);
|
|
143
150
|
}
|
|
144
151
|
if (pending.length) yield pending;
|
|
145
152
|
}
|
|
@@ -147,10 +154,11 @@ async function* nulDelimitedRecords(stream: Readable): AsyncGenerator<Buffer> {
|
|
|
147
154
|
/** Run rg while read consumes its stdout; read returns false to stop rg early. */
|
|
148
155
|
async function runRg(
|
|
149
156
|
args: readonly string[],
|
|
150
|
-
|
|
157
|
+
cwd: string,
|
|
158
|
+
signal: AbortSignal,
|
|
151
159
|
read: (stdout: Readable) => Promise<boolean>,
|
|
152
160
|
): Promise<RgRunResult> {
|
|
153
|
-
const rg = startRg(args, signal);
|
|
161
|
+
const rg = startRg(args, cwd, signal);
|
|
154
162
|
try {
|
|
155
163
|
const stopped = !(await read(rg.child.stdout));
|
|
156
164
|
if (stopped) rg.kill();
|
|
@@ -167,10 +175,11 @@ async function runRg(
|
|
|
167
175
|
/** Read NUL-delimited UTF-8 paths from an owned rg process. */
|
|
168
176
|
export function runRgPaths(
|
|
169
177
|
args: string[],
|
|
170
|
-
|
|
178
|
+
cwd: string,
|
|
179
|
+
signal: AbortSignal,
|
|
171
180
|
onPath: (path: string) => boolean | Promise<boolean>,
|
|
172
181
|
): Promise<RgRunResult> {
|
|
173
|
-
return runRg(args, signal, async (stdout) => {
|
|
182
|
+
return runRg(args, cwd, signal, async (stdout) => {
|
|
174
183
|
for await (const record of nulDelimitedRecords(stdout)) {
|
|
175
184
|
throwIfCancelled(signal);
|
|
176
185
|
if (!(await onPath(decodePath(record)))) return false;
|
|
@@ -185,10 +194,12 @@ export function runRgPaths(
|
|
|
185
194
|
*/
|
|
186
195
|
export async function rgRejectsPatterns(
|
|
187
196
|
patternArgs: readonly string[],
|
|
188
|
-
|
|
197
|
+
cwd: string,
|
|
198
|
+
signal: AbortSignal,
|
|
189
199
|
): Promise<boolean> {
|
|
190
200
|
const result = await runRg(
|
|
191
201
|
[...COMMON_RG_ARGS, ...patternArgs, "--", "-"],
|
|
202
|
+
cwd,
|
|
192
203
|
signal,
|
|
193
204
|
async (stdout) => {
|
|
194
205
|
for await (const _ of stdout);
|
|
@@ -216,29 +227,40 @@ export const LINE_LISTING_ARGS = [
|
|
|
216
227
|
"--max-columns=1",
|
|
217
228
|
];
|
|
218
229
|
|
|
230
|
+
/** A file's candidate lines: ascending line numbers, or every line. */
|
|
231
|
+
export type CandidateLines = readonly number[] | "all";
|
|
232
|
+
|
|
233
|
+
interface CandidateFile {
|
|
234
|
+
path: string;
|
|
235
|
+
lines: number[] | "all";
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function collectCandidateLine(file: CandidateFile, record: Buffer, maxLines: number): void {
|
|
239
|
+
if (file.lines === "all") return;
|
|
240
|
+
const colon = record.indexOf(58);
|
|
241
|
+
const line = colon > 0 ? Number(record.subarray(0, colon).toString("latin1")) : NaN;
|
|
242
|
+
if (!Number.isSafeInteger(line) || line < 1)
|
|
243
|
+
throw new HashlineError("RIPGREP_FAILED", "ripgrep printed an unexpected line record.");
|
|
244
|
+
if (file.lines.length < maxLines) file.lines.push(line);
|
|
245
|
+
else file.lines = "all";
|
|
246
|
+
}
|
|
247
|
+
|
|
219
248
|
/**
|
|
220
249
|
* Read a {@link LINE_LISTING_ARGS} listing. Each file's group is a NUL-terminated
|
|
221
250
|
* path, then one `N:` record per matching line ending in a line feed (its content is at most
|
|
222
251
|
* one byte or an omission notice), then an empty line. onFile receives a file's ascending line
|
|
223
|
-
* numbers, or
|
|
252
|
+
* numbers, or "all" once there are more than maxLines.
|
|
224
253
|
*/
|
|
225
254
|
export function runRgLines(
|
|
226
255
|
args: string[],
|
|
227
|
-
|
|
256
|
+
cwd: string,
|
|
257
|
+
signal: AbortSignal,
|
|
228
258
|
maxLines: number,
|
|
229
|
-
onFile: (path: string, lines:
|
|
259
|
+
onFile: (path: string, lines: CandidateLines) => boolean | Promise<boolean>,
|
|
230
260
|
): Promise<RgRunResult> {
|
|
231
|
-
return runRg(args, signal, async (stdout) => {
|
|
232
|
-
let
|
|
233
|
-
let lines: number[] | undefined = [];
|
|
261
|
+
return runRg(args, cwd, signal, async (stdout) => {
|
|
262
|
+
let file: CandidateFile | undefined;
|
|
234
263
|
let pending: Buffer = Buffer.alloc(0);
|
|
235
|
-
const flush = () => {
|
|
236
|
-
const found = lines;
|
|
237
|
-
const file = path!;
|
|
238
|
-
path = undefined;
|
|
239
|
-
lines = [];
|
|
240
|
-
return onFile(file, found);
|
|
241
|
-
};
|
|
242
264
|
// Records are parsed synchronously; only a completed file waits for its consumer.
|
|
243
265
|
for await (const value of stdout) {
|
|
244
266
|
throwIfCancelled(signal);
|
|
@@ -246,28 +268,26 @@ export function runRgLines(
|
|
|
246
268
|
const buf = pending.length ? Buffer.concat([pending, chunk]) : chunk;
|
|
247
269
|
let at = 0;
|
|
248
270
|
for (;;) {
|
|
249
|
-
const end = buf.indexOf(
|
|
271
|
+
const end = buf.indexOf(file ? LF : NUL, at);
|
|
250
272
|
if (end === -1) break;
|
|
251
273
|
const record = buf.subarray(at, end);
|
|
252
274
|
at = end + 1;
|
|
253
|
-
if (
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
throw new HashlineError("RIPGREP_FAILED", "ripgrep printed an unexpected line record.");
|
|
261
|
-
if (lines.length < maxLines) lines.push(line);
|
|
262
|
-
else lines = undefined;
|
|
275
|
+
if (!file) {
|
|
276
|
+
file = { path: decodePath(record), lines: [] };
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (record.length) {
|
|
280
|
+
collectCandidateLine(file, record, maxLines);
|
|
281
|
+
continue;
|
|
263
282
|
}
|
|
283
|
+
if (!(await onFile(file.path, file.lines))) return false;
|
|
284
|
+
file = undefined;
|
|
264
285
|
}
|
|
265
286
|
pending = buf.subarray(at);
|
|
266
|
-
|
|
267
|
-
throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
|
|
287
|
+
checkRecordSize(pending.length);
|
|
268
288
|
}
|
|
269
289
|
if (pending.length)
|
|
270
290
|
throw new HashlineError("RIPGREP_FAILED", "ripgrep printed an incomplete line record.");
|
|
271
|
-
return
|
|
291
|
+
return !file || onFile(file.path, file.lines);
|
|
272
292
|
});
|
|
273
293
|
}
|
package/src/pi/schema.ts
CHANGED
|
@@ -7,9 +7,13 @@
|
|
|
7
7
|
* @module pi-hashline-edit/pi
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
10
11
|
import type { ConstrainedSamplingConfig } from "@earendil-works/pi-ai";
|
|
11
12
|
import { Type, type TLiteral, type TSchemaOptions } from "typebox";
|
|
12
13
|
|
|
14
|
+
/** Hashline tool factories supply a guideline array, including when it is empty. */
|
|
15
|
+
export type ToolGuidelines = Required<Pick<ToolDefinition, "promptGuidelines">>;
|
|
16
|
+
|
|
13
17
|
/**
|
|
14
18
|
* Every tool's sampling request: providers that support it enforce the parameter schema while
|
|
15
19
|
* sampling, and the rest sample as usual. Pi's strict converter rejects object and array
|