@criogaid/pi-hashline-edit 0.5.1 → 0.6.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 +51 -15
- package/package.json +6 -6
- package/src/core/apply.test.ts +66 -16
- package/src/core/apply.ts +36 -19
- package/src/core/lines.ts +3 -3
- package/src/index.ts +2 -0
- package/src/integration/forget-grep.test.ts +98 -0
- package/src/integration/fusion-dispatch.test.ts +343 -0
- package/src/integration/grep-rg.test.ts +269 -33
- package/src/integration/mutation-file-types.test.ts +44 -0
- package/src/pi/action-fusion.test.ts +71 -128
- package/src/pi/action-fusion.ts +83 -45
- package/src/pi/argument-validation.test.ts +136 -0
- package/src/pi/argument-validation.ts +103 -0
- package/src/pi/budgets.ts +4 -1
- package/src/pi/config.test.ts +1 -0
- package/src/pi/config.ts +4 -0
- package/src/pi/directory-sync.test.ts +1 -1
- package/src/pi/edit-tool.ts +55 -36
- package/src/pi/execute.test.ts +185 -645
- package/src/pi/failure-context.test.ts +4 -3
- package/src/pi/failure-path.test.ts +3 -3
- package/src/pi/file-commit.test.ts +24 -17
- package/src/pi/file-commit.ts +36 -9
- package/src/pi/file-snapshot.test.ts +24 -0
- package/src/pi/forget-tool.ts +268 -0
- package/src/pi/forget.test.ts +343 -0
- package/src/pi/forget.testing.ts +273 -0
- package/src/pi/grep-output.ts +57 -28
- package/src/pi/grep-search.ts +35 -12
- package/src/pi/grep-tool.ts +28 -9
- package/src/pi/grep.test.ts +173 -152
- package/src/pi/mutation-result.test.ts +23 -0
- package/src/pi/mutation-runner.ts +8 -4
- package/src/pi/path.ts +16 -2
- package/src/pi/pi.test.ts +3 -2
- package/src/pi/read-tool.ts +58 -26
- package/src/pi/read.test.ts +361 -0
- package/src/pi/registration.test.ts +115 -2
- package/src/pi/render.test.ts +45 -14
- package/src/pi/replace-tool.ts +18 -27
- package/src/pi/replace.test.ts +8 -107
- package/src/pi/result-safety.test.ts +123 -149
- package/src/pi/rg-match-bytes.ts +95 -0
- package/src/pi/rg-process.test.ts +40 -59
- package/src/pi/rg-text-view.ts +52 -5
- package/src/pi/tool-call.testing.ts +50 -0
- package/src/pi/tool-prompts.ts +15 -0
- package/src/pi/write-tool.ts +17 -26
- package/src/pi/write.test.ts +70 -61
- package/src/pi/e2e-scenarios.test.ts +0 -361
package/src/pi/edit-tool.ts
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
import {
|
|
23
|
-
type
|
|
23
|
+
type ExtensionToolContext,
|
|
24
24
|
type Theme,
|
|
25
25
|
type ToolDefinition,
|
|
26
26
|
type ToolRenderResultOptions,
|
|
@@ -31,7 +31,6 @@ import { ACTION_FUSION_GUIDELINES, withThenRunSchema, type ThenRunInput } from "
|
|
|
31
31
|
import { applyEdits } from "../core/apply.ts";
|
|
32
32
|
import { splitLines } from "../core/lines.ts";
|
|
33
33
|
import { unwritableTextReason } from "../core/text.ts";
|
|
34
|
-
import { invalidArgument } from "./error-text.ts";
|
|
35
34
|
import type { Anchor, Edit } from "../core/types.ts";
|
|
36
35
|
import type { HashlineEditConfig } from "./config.ts";
|
|
37
36
|
import {
|
|
@@ -55,6 +54,12 @@ import {
|
|
|
55
54
|
type MutationTarget,
|
|
56
55
|
type TextMutationDetails,
|
|
57
56
|
} from "./mutation-runner.ts";
|
|
57
|
+
import { MUTATION_TOOL_GUIDELINE } from "./tool-prompts.ts";
|
|
58
|
+
import {
|
|
59
|
+
argumentItems,
|
|
60
|
+
createArgumentPreparer,
|
|
61
|
+
type ReportArgumentIssue,
|
|
62
|
+
} from "./argument-validation.ts";
|
|
58
63
|
type EditDetails = TextMutationDetails;
|
|
59
64
|
type EditRenderContext = Parameters<NonNullable<ToolDefinition<EditSchema>["renderCall"]>>[2];
|
|
60
65
|
|
|
@@ -79,7 +84,8 @@ function buildEditSchema(hashLen: number) {
|
|
|
79
84
|
}),
|
|
80
85
|
);
|
|
81
86
|
const bodyLines = Type.Array(Type.String({ pattern: "^[^\\r\\n]*$" }), {
|
|
82
|
-
|
|
87
|
+
minItems: 1,
|
|
88
|
+
description: 'New lines, one per element, without CR/LF. At least one; [""] is one blank line.',
|
|
83
89
|
});
|
|
84
90
|
const editOpSchema = Type.Union([
|
|
85
91
|
Type.Object(
|
|
@@ -137,33 +143,31 @@ function buildEditSchema(hashLen: number) {
|
|
|
137
143
|
type EditSchema = ReturnType<typeof buildEditSchema>;
|
|
138
144
|
|
|
139
145
|
function createEditSchema(actionFusion: boolean, hashLen: number) {
|
|
140
|
-
return withThenRunSchema(
|
|
141
|
-
buildEditSchema(hashLen),
|
|
142
|
-
"Command to run after the edit succeeds; failure does not roll back the edit.",
|
|
143
|
-
actionFusion,
|
|
144
|
-
);
|
|
146
|
+
return withThenRunSchema(buildEditSchema(hashLen), "edit", actionFusion);
|
|
145
147
|
}
|
|
146
148
|
type EditParams = Static<EditSchema> & { then_run?: ThenRunInput };
|
|
147
149
|
|
|
148
150
|
type EditOpInput = Static<EditSchema>["edits"][number];
|
|
149
151
|
|
|
150
|
-
/**
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
* Arguments are never changed.
|
|
156
|
-
*/
|
|
157
|
-
function checkEditArguments(args: unknown, hashLen: number): void {
|
|
158
|
-
const edits = (args as { edits?: unknown } | null)?.edits;
|
|
159
|
-
if (!Array.isArray(edits)) return;
|
|
160
|
-
const mismatches: string[] = [];
|
|
152
|
+
/** Report semantic argument issues; malformed shapes remain Pi's schema responsibility. */
|
|
153
|
+
function checkEditArguments(args: unknown, hashLen: number, report: ReportArgumentIssue): void {
|
|
154
|
+
const raw = (args as { edits?: unknown } | null)?.edits;
|
|
155
|
+
const edits = argumentItems(raw);
|
|
156
|
+
let hashLengthRecoveryHint = " Read or grep the file for current anchors.";
|
|
161
157
|
edits.forEach((op, index) => {
|
|
162
158
|
const body = (op as Record<string, unknown> | null)?.body;
|
|
163
159
|
if (Array.isArray(body)) {
|
|
160
|
+
if (body.length === 0) {
|
|
161
|
+
report(
|
|
162
|
+
`edits[${index}].body`,
|
|
163
|
+
(op as Record<string, unknown>).op === "replace"
|
|
164
|
+
? 'is empty; use {"op":"delete"} to remove lines, or supply the replacement lines.'
|
|
165
|
+
: 'is empty; remove this edit or supply at least one line ([""] for a blank line).',
|
|
166
|
+
);
|
|
167
|
+
}
|
|
164
168
|
body.forEach((line, lineIndex) => {
|
|
165
169
|
const reason = typeof line === "string" ? unwritableTextReason(line) : undefined;
|
|
166
|
-
if (reason)
|
|
170
|
+
if (reason) report(`edits[${index}].body[${lineIndex}]`, reason);
|
|
167
171
|
});
|
|
168
172
|
}
|
|
169
173
|
for (const field of ["anchor", "end"] as const) {
|
|
@@ -171,23 +175,20 @@ function checkEditArguments(args: unknown, hashLen: number): void {
|
|
|
171
175
|
if (typeof value !== "string") continue;
|
|
172
176
|
const token = parseAnchorToken(value);
|
|
173
177
|
if (token && !Number.isSafeInteger(token.line)) {
|
|
174
|
-
|
|
178
|
+
report(
|
|
175
179
|
`edits[${index}].${field}`,
|
|
176
180
|
`line number in ${value} exceeds the safe integer range; copy a complete "LINE#HASH" token from the latest tool result.`,
|
|
177
181
|
);
|
|
178
182
|
}
|
|
179
183
|
if (token && token.hash.length !== hashLen) {
|
|
180
|
-
|
|
181
|
-
`edits[${index}].${field}
|
|
184
|
+
report(
|
|
185
|
+
`edits[${index}].${field}`,
|
|
186
|
+
`Anchor hash length mismatch: ${value} has ${token.hash.length} hash characters, but hashLen is ${hashLen}.${hashLengthRecoveryHint}`,
|
|
182
187
|
);
|
|
188
|
+
hashLengthRecoveryHint = "";
|
|
183
189
|
}
|
|
184
190
|
}
|
|
185
191
|
});
|
|
186
|
-
if (mismatches.length) {
|
|
187
|
-
throw new Error(
|
|
188
|
-
`Anchor hash length mismatch: ${mismatches.join("; ")}, but hashLen is ${hashLen}. Anchors from a different hashLen setting cannot be verified; read or grep the file for current anchors.`,
|
|
189
|
-
);
|
|
190
|
-
}
|
|
191
192
|
}
|
|
192
193
|
|
|
193
194
|
/** Translate validated public operations into core edits, parsing numeric anchor positions. */
|
|
@@ -235,15 +236,33 @@ function formatUpdatedAnchors(
|
|
|
235
236
|
);
|
|
236
237
|
}
|
|
237
238
|
|
|
238
|
-
/** Call
|
|
239
|
+
/** Call header with total/per-kind operation counts and the result's diff counts. */
|
|
239
240
|
function editHeader(args: EditParams, theme: Theme, counts?: DiffCounts): string {
|
|
240
241
|
let t = theme.fg("toolTitle", theme.bold("edit "));
|
|
241
242
|
t += theme.fg("accent", args.path);
|
|
242
243
|
const edits = Array.isArray(args.edits) ? args.edits : [];
|
|
243
244
|
const n = edits.length;
|
|
244
245
|
if (n) {
|
|
245
|
-
const
|
|
246
|
-
|
|
246
|
+
const opCounts: Record<EditOpInput["op"] | "unknown", number> = {
|
|
247
|
+
replace: 0,
|
|
248
|
+
delete: 0,
|
|
249
|
+
insert_before: 0,
|
|
250
|
+
insert_after: 0,
|
|
251
|
+
append: 0,
|
|
252
|
+
prepend: 0,
|
|
253
|
+
unknown: 0,
|
|
254
|
+
};
|
|
255
|
+
// Renderers receive partial arguments; fixed buckets keep one-pass counting O(1) in space.
|
|
256
|
+
for (const edit of edits) {
|
|
257
|
+
const kind = edit?.op;
|
|
258
|
+
if (typeof kind === "string" && Object.hasOwn(opCounts, kind)) opCounts[kind]++;
|
|
259
|
+
else opCounts.unknown++;
|
|
260
|
+
}
|
|
261
|
+
const summary = Object.entries(opCounts)
|
|
262
|
+
.filter(([, count]) => count > 0)
|
|
263
|
+
.map(([kind, count]) => `${kind} ×${count}`)
|
|
264
|
+
.join(", ");
|
|
265
|
+
t += theme.fg("dim", ` — ${n} op${n > 1 ? "s" : ""}: ${summary}`);
|
|
247
266
|
}
|
|
248
267
|
if (counts && (counts.added || counts.removed)) t += formatDiffCounts(counts, theme);
|
|
249
268
|
return t;
|
|
@@ -265,16 +284,16 @@ export function makeEditOverride(
|
|
|
265
284
|
"Edit file lines by LINE#HASH anchors checked against the current file. Returns fresh anchors for changed lines. On anchor failure, shows current context and recovery candidates; nothing is retried automatically.",
|
|
266
285
|
promptSnippet: "Edit file lines using verified anchors",
|
|
267
286
|
promptGuidelines: [
|
|
287
|
+
MUTATION_TOOL_GUIDELINE,
|
|
268
288
|
"Batch all edits to one file in a single edit call; all its anchors are checked against one snapshot.",
|
|
269
289
|
"Reuse anchors while their line number and content are unchanged; inserts and deletes shift later lines, so use the edit's Updated anchors or re-read shifted lines.",
|
|
270
290
|
"On edit anchor failure, inspect the recovery candidates before retrying or re-reading.",
|
|
271
291
|
...(fusion ? ACTION_FUSION_GUIDELINES : []),
|
|
272
292
|
],
|
|
273
293
|
parameters,
|
|
274
|
-
prepareArguments(args
|
|
275
|
-
checkEditArguments(args, hashLen)
|
|
276
|
-
|
|
277
|
-
},
|
|
294
|
+
prepareArguments: createArgumentPreparer("edit", parameters, (args, report) =>
|
|
295
|
+
checkEditArguments(args, hashLen, report),
|
|
296
|
+
),
|
|
278
297
|
renderShell: "default" as const,
|
|
279
298
|
|
|
280
299
|
renderCall(args: EditParams, theme: Theme, context: EditRenderContext) {
|
|
@@ -303,7 +322,7 @@ export function makeEditOverride(
|
|
|
303
322
|
params: EditParams,
|
|
304
323
|
signal: AbortSignal | undefined,
|
|
305
324
|
onUpdate: AgentToolUpdateCallback<EditDetails> | undefined,
|
|
306
|
-
ctx:
|
|
325
|
+
ctx: ExtensionToolContext,
|
|
307
326
|
) {
|
|
308
327
|
return executeMutation<Omit<EditParams, "then_run">, EditDetails>(
|
|
309
328
|
{
|