@criogaid/pi-hashline-edit 0.4.0 → 0.5.1
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 +134 -30
- package/package.json +3 -3
- package/src/core/apply.test.ts +194 -104
- package/src/core/apply.ts +6 -37
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +8 -8
- package/src/core/hash.ts +5 -5
- package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
- package/src/core/text.test.ts +30 -1
- package/src/core/text.ts +9 -6
- package/src/core/types.ts +4 -7
- package/src/index.ts +9 -11
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +285 -153
- package/src/integration/large-text.test.ts +11 -12
- package/src/integration/replace-package.test.ts +11 -7
- package/src/pi/action-fusion.test.ts +251 -39
- package/src/pi/action-fusion.ts +113 -117
- package/src/pi/anchor-format.test.ts +91 -24
- package/src/pi/anchor-format.ts +43 -9
- package/src/pi/budgets.ts +7 -2
- package/src/pi/config.test.ts +153 -40
- package/src/pi/config.ts +87 -41
- package/src/pi/diagnostic-buffer.test.ts +58 -0
- package/src/pi/diagnostic-buffer.ts +59 -0
- package/src/pi/e2e-scenarios.test.ts +13 -11
- package/src/pi/edit-tool.ts +21 -157
- package/src/pi/error-text.ts +10 -5
- package/src/pi/execute.test.ts +299 -175
- package/src/pi/failure-context.test.ts +7 -2
- package/src/pi/failure-context.ts +155 -8
- package/src/pi/failure-path.test.ts +33 -26
- package/src/pi/file-commit.ts +2 -1
- package/src/pi/fusion-card.test.ts +26 -0
- package/src/pi/fusion-card.ts +102 -6
- package/src/pi/grep-output.ts +50 -35
- package/src/pi/grep-render.ts +13 -15
- package/src/pi/grep-scope.ts +8 -46
- package/src/pi/grep-search.ts +90 -63
- package/src/pi/grep-tool.ts +109 -121
- package/src/pi/grep.test.ts +597 -377
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +51 -2
- package/src/pi/mutation-result.ts +62 -55
- package/src/pi/mutation-runner.ts +16 -33
- package/src/pi/pi.test.ts +11 -6
- package/src/pi/read-tool.ts +62 -34
- package/src/pi/registration.test.ts +7 -7
- package/src/pi/render.test.ts +143 -9
- package/src/pi/render.ts +32 -96
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +25 -59
- package/src/pi/replace-worker.ts +2 -2
- package/src/pi/replace.test.ts +62 -58
- package/src/pi/result-safety.test.ts +124 -84
- package/src/pi/rg-line-ranges.ts +3 -2
- package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +43 -9
- package/src/pi/rg-text-view.ts +25 -38
- package/src/pi/schema.ts +2 -10
- package/src/pi/text-stream.test.ts +153 -0
- package/src/pi/text-stream.ts +33 -13
- package/src/pi/write-tool.ts +3 -2
- package/src/pi/write.test.ts +10 -9
- package/src/core/index.ts +0 -14
- package/src/pi/state.ts +0 -26
package/src/pi/edit-tool.ts
CHANGED
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
import {
|
|
23
|
-
truncateHead,
|
|
24
23
|
type ExtensionContext,
|
|
25
24
|
type Theme,
|
|
26
25
|
type ToolDefinition,
|
|
@@ -29,12 +28,12 @@ import {
|
|
|
29
28
|
import type { AgentToolResult, AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
|
|
30
29
|
import { Type, type Static } from "typebox";
|
|
31
30
|
import { ACTION_FUSION_GUIDELINES, withThenRunSchema, type ThenRunInput } from "./action-fusion.ts";
|
|
32
|
-
import { applyEdits } from "../core/
|
|
31
|
+
import { applyEdits } from "../core/apply.ts";
|
|
33
32
|
import { splitLines } from "../core/lines.ts";
|
|
34
33
|
import { unwritableTextReason } from "../core/text.ts";
|
|
35
34
|
import { invalidArgument } from "./error-text.ts";
|
|
36
|
-
import type { Anchor,
|
|
37
|
-
import {
|
|
35
|
+
import type { Anchor, Edit } from "../core/types.ts";
|
|
36
|
+
import type { HashlineEditConfig } from "./config.ts";
|
|
38
37
|
import {
|
|
39
38
|
anchorPattern,
|
|
40
39
|
createAnchorFormatter,
|
|
@@ -47,11 +46,7 @@ import {
|
|
|
47
46
|
renderMutationResult,
|
|
48
47
|
type DiffCounts,
|
|
49
48
|
} from "./render.ts";
|
|
50
|
-
import {
|
|
51
|
-
formatAmbiguousCandidateNeighborhoods,
|
|
52
|
-
MAX_AMBIGUOUS_CANDIDATES,
|
|
53
|
-
} from "./failure-context.ts";
|
|
54
|
-
import { formatKiB, MAX_BLOCK_BYTES, MAX_RECOVERY_CANDIDATE_BYTES } from "./budgets.ts";
|
|
49
|
+
import { formatFailure } from "./failure-context.ts";
|
|
55
50
|
import { formatMutationAnchors } from "./mutation-result.ts";
|
|
56
51
|
import {
|
|
57
52
|
executeMutation,
|
|
@@ -63,12 +58,6 @@ import {
|
|
|
63
58
|
type EditDetails = TextMutationDetails;
|
|
64
59
|
type EditRenderContext = Parameters<NonNullable<ToolDefinition<EditSchema>["renderCall"]>>[2];
|
|
65
60
|
|
|
66
|
-
/** Keep independent byte budgets for failure details and input-anchor checks. */
|
|
67
|
-
function boundDiagnostic(message: string, notice: string): string {
|
|
68
|
-
const bounded = truncateHead(message, { maxBytes: MAX_BLOCK_BYTES - Buffer.byteLength(notice) });
|
|
69
|
-
return bounded.content + (bounded.truncated ? notice : "");
|
|
70
|
-
}
|
|
71
|
-
|
|
72
61
|
/** Split a validated anchor token; prepareArguments and the schema already checked it. */
|
|
73
62
|
function parseAnchor(value: string): Anchor;
|
|
74
63
|
function parseAnchor(value: string | undefined): Anchor | undefined;
|
|
@@ -201,139 +190,6 @@ function checkEditArguments(args: unknown, hashLen: number): void {
|
|
|
201
190
|
}
|
|
202
191
|
}
|
|
203
192
|
|
|
204
|
-
/** Format failure mappings and complete unique-candidate rows within the detail budget. */
|
|
205
|
-
function formatFailureDetails(
|
|
206
|
-
failure: ApplyFailure,
|
|
207
|
-
snapshot: Readonly<{ currentText: string; anchors: AnchorFormatter }>,
|
|
208
|
-
candidateLines: ReadonlySet<number>,
|
|
209
|
-
): string {
|
|
210
|
-
if (failure.kind !== "anchor") return failure.message;
|
|
211
|
-
|
|
212
|
-
const lines: string[] = [];
|
|
213
|
-
const currentLines = splitLines(snapshot.currentText);
|
|
214
|
-
const shownCandidates = new Set(candidateLines);
|
|
215
|
-
let found = 0;
|
|
216
|
-
let ambiguous = 0;
|
|
217
|
-
let none = 0;
|
|
218
|
-
for (const f of failure.failures) {
|
|
219
|
-
if (f.recovery.kind === "found") found++;
|
|
220
|
-
else if (f.recovery.kind === "ambiguous") ambiguous++;
|
|
221
|
-
else none++;
|
|
222
|
-
const where = `op #${f.opIndex} ${f.op} ${f.which} (line ${f.cited.line})`;
|
|
223
|
-
const search =
|
|
224
|
-
f.recovery.kind === "none"
|
|
225
|
-
? ""
|
|
226
|
-
: f.recovery.scope === "local"
|
|
227
|
-
? "Search: local; matches outside the window were not checked."
|
|
228
|
-
: "Search: full file.";
|
|
229
|
-
switch (f.recovery.kind) {
|
|
230
|
-
case "found": {
|
|
231
|
-
const content = currentLines[f.recovery.newLine - 1];
|
|
232
|
-
const candidate = snapshot.anchors.reference(f.recovery.newLine, f.recovery.newHash);
|
|
233
|
-
const row =
|
|
234
|
-
content === undefined ? candidate : snapshot.anchors.row(f.recovery.newLine, content);
|
|
235
|
-
let detail = `• ${where}: checksum-matching candidate ${candidate}. ${search}`;
|
|
236
|
-
if (!shownCandidates.has(f.recovery.newLine)) {
|
|
237
|
-
if (
|
|
238
|
-
content !== undefined &&
|
|
239
|
-
Buffer.byteLength(row, "utf8") <= MAX_RECOVERY_CANDIDATE_BYTES
|
|
240
|
-
) {
|
|
241
|
-
detail += `\n${row}`;
|
|
242
|
-
shownCandidates.add(f.recovery.newLine);
|
|
243
|
-
} else {
|
|
244
|
-
detail += ` Candidate content exceeds ${MAX_RECOVERY_CANDIDATE_BYTES} bytes.`;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
lines.push(detail);
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
case "ambiguous": {
|
|
251
|
-
const list = f.recovery.candidates
|
|
252
|
-
.slice(0, MAX_AMBIGUOUS_CANDIDATES)
|
|
253
|
-
.map((candidate) => `"${snapshot.anchors.reference(candidate.line, candidate.hash)}"`)
|
|
254
|
-
.join(" / ");
|
|
255
|
-
const more =
|
|
256
|
-
f.recovery.candidates.length > MAX_AMBIGUOUS_CANDIDATES
|
|
257
|
-
? ` (${f.recovery.candidates.length - MAX_AMBIGUOUS_CANDIDATES} more candidates omitted)`
|
|
258
|
-
: "";
|
|
259
|
-
lines.push(`• ${where}: ambiguous checksum matches: ${list}${more}. ${search}`);
|
|
260
|
-
break;
|
|
261
|
-
}
|
|
262
|
-
case "none": {
|
|
263
|
-
const row =
|
|
264
|
-
f.current === null ? null : snapshot.anchors.row(f.cited.line, f.current.content);
|
|
265
|
-
if (row !== null && Buffer.byteLength(row, "utf8") <= MAX_RECOVERY_CANDIDATE_BYTES) {
|
|
266
|
-
lines.push(
|
|
267
|
-
`• ${where}: no checksum-matching candidate found. Current cited line (validation snapshot; observation only):\n${row}\nConfirm this is the intended target before reusing its anchor directly; retries revalidate.`,
|
|
268
|
-
);
|
|
269
|
-
} else {
|
|
270
|
-
lines.push(
|
|
271
|
-
`• ${where}: no checksum-matching candidate found. Use read or grep to inspect the current file before retrying.` +
|
|
272
|
-
(f.current === null
|
|
273
|
-
? " Cited line is out of range."
|
|
274
|
-
: ` Current row exceeds ${MAX_RECOVERY_CANDIDATE_BYTES} bytes.`),
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
const parts: string[] = [];
|
|
282
|
-
if (found) parts.push(`${found} shifted`);
|
|
283
|
-
if (ambiguous) parts.push(`${ambiguous} ambiguous`);
|
|
284
|
-
if (none) parts.push(`${none} unresolved`);
|
|
285
|
-
const message = [
|
|
286
|
-
`Anchor mismatch: ${parts.join(", ")}.`,
|
|
287
|
-
"No changes written by this edit batch.",
|
|
288
|
-
...lines,
|
|
289
|
-
].join("\n");
|
|
290
|
-
return boundDiagnostic(
|
|
291
|
-
message,
|
|
292
|
-
`\nDiagnostic output truncated at ${formatKiB(MAX_BLOCK_BYTES)}.`,
|
|
293
|
-
);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function formatAnchorChecks(failure: ApplyFailure, anchors: AnchorFormatter): string {
|
|
297
|
-
const rows = failure.checks.map(
|
|
298
|
-
(check) =>
|
|
299
|
-
`op ${check.opIndex} / ${check.which} / ${anchors.reference(check.cited.line, check.cited.hash)} / ${check.status}`,
|
|
300
|
-
);
|
|
301
|
-
const message = [
|
|
302
|
-
"Input-anchor checks (this snapshot):",
|
|
303
|
-
...rows,
|
|
304
|
-
"Anchor checks only; retries revalidate.",
|
|
305
|
-
].join("\n");
|
|
306
|
-
return boundDiagnostic(
|
|
307
|
-
message,
|
|
308
|
-
`\nAnchor-check output truncated at ${formatKiB(MAX_BLOCK_BYTES)}; omitted entries are not implied matched.`,
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/** Keep validation status and observation context visible even when failure details are truncated. */
|
|
313
|
-
function formatFailure(
|
|
314
|
-
failure: ApplyFailure,
|
|
315
|
-
snapshot: Readonly<{ currentText: string; anchors: AnchorFormatter }>,
|
|
316
|
-
isBatch: boolean,
|
|
317
|
-
): string {
|
|
318
|
-
const candidateNeighborhoods =
|
|
319
|
-
failure.kind === "anchor"
|
|
320
|
-
? formatAmbiguousCandidateNeighborhoods(
|
|
321
|
-
snapshot.currentText,
|
|
322
|
-
failure.failures,
|
|
323
|
-
snapshot.anchors,
|
|
324
|
-
)
|
|
325
|
-
: { text: "", shownLines: new Set<number>() };
|
|
326
|
-
const guidance =
|
|
327
|
-
failure.kind === "anchor"
|
|
328
|
-
? "\nCheck the intended target before retrying; use read or grep for omitted or additional context."
|
|
329
|
-
: "";
|
|
330
|
-
const anchorChecks =
|
|
331
|
-
isBatch && failure.checks.length > 0
|
|
332
|
-
? `\n${formatAnchorChecks(failure, snapshot.anchors)}`
|
|
333
|
-
: "";
|
|
334
|
-
return `${formatFailureDetails(failure, snapshot, candidateNeighborhoods.shownLines)}${anchorChecks}${candidateNeighborhoods.text}${guidance}`;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
193
|
/** Translate validated public operations into core edits, parsing numeric anchor positions. */
|
|
338
194
|
function toCoreEdits(ops: readonly EditOpInput[]): Edit[] {
|
|
339
195
|
return ops.map((op) => {
|
|
@@ -393,9 +249,13 @@ function editHeader(args: EditParams, theme: Theme, counts?: DiffCounts): string
|
|
|
393
249
|
return t;
|
|
394
250
|
}
|
|
395
251
|
|
|
396
|
-
export function makeEditOverride(
|
|
397
|
-
|
|
398
|
-
|
|
252
|
+
export function makeEditOverride(
|
|
253
|
+
cwd: string,
|
|
254
|
+
config: HashlineEditConfig,
|
|
255
|
+
fusion?: ActionFusionExecutor,
|
|
256
|
+
) {
|
|
257
|
+
// Schema, verification, recovery, and returned anchors use the registered configuration.
|
|
258
|
+
const { hashLen, shiftRadius } = config;
|
|
399
259
|
const parameters = createEditSchema(fusion !== undefined, hashLen);
|
|
400
260
|
|
|
401
261
|
return {
|
|
@@ -405,7 +265,7 @@ export function makeEditOverride(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
405
265
|
"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.",
|
|
406
266
|
promptSnippet: "Edit file lines using verified anchors",
|
|
407
267
|
promptGuidelines: [
|
|
408
|
-
"Batch
|
|
268
|
+
"Batch all edits to one file in a single edit call; all its anchors are checked against one snapshot.",
|
|
409
269
|
"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.",
|
|
410
270
|
"On edit anchor failure, inspect the recovery candidates before retrying or re-reading.",
|
|
411
271
|
...(fusion ? ACTION_FUSION_GUIDELINES : []),
|
|
@@ -449,8 +309,8 @@ export function makeEditOverride(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
449
309
|
{
|
|
450
310
|
cwd,
|
|
451
311
|
fusion,
|
|
452
|
-
|
|
453
|
-
|
|
312
|
+
run: (mutationParams, target) =>
|
|
313
|
+
runHashline(target, mutationParams.edits, hashLen, shiftRadius),
|
|
454
314
|
},
|
|
455
315
|
{ toolCallId, params, signal, onUpdate, ctx },
|
|
456
316
|
);
|
|
@@ -458,9 +318,13 @@ export function makeEditOverride(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
458
318
|
};
|
|
459
319
|
}
|
|
460
320
|
|
|
461
|
-
function runHashline(
|
|
321
|
+
function runHashline(
|
|
322
|
+
target: MutationTarget,
|
|
323
|
+
editOps: readonly EditOpInput[],
|
|
324
|
+
hashLen: number,
|
|
325
|
+
shiftRadius: number,
|
|
326
|
+
) {
|
|
462
327
|
const anchorFormatter = createAnchorFormatter(hashLen);
|
|
463
|
-
const { shiftRadius } = getState().config;
|
|
464
328
|
|
|
465
329
|
return runTextMutation("edit", target, (currentText) => {
|
|
466
330
|
const translated = toCoreEdits(editOps);
|
|
@@ -468,7 +332,7 @@ function runHashline(target: MutationTarget, editOps: readonly EditOpInput[], ha
|
|
|
468
332
|
// Recovery reports checksum candidates from nearby lines, then the whole file
|
|
469
333
|
// if needed. Every failed anchor still rejects the batch; callers inspect
|
|
470
334
|
// candidates and resubmit with fresh anchors.
|
|
471
|
-
const result = applyEdits(currentText, translated,
|
|
335
|
+
const result = applyEdits(currentText, translated, hashLen, shiftRadius);
|
|
472
336
|
if (!result.ok) {
|
|
473
337
|
throw new Error(
|
|
474
338
|
formatFailure(
|
package/src/pi/error-text.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/** Message text for a caught value; non-Error throws are stringified. */
|
|
2
|
-
export function errorMessage(error: unknown): string {
|
|
3
|
-
return error instanceof Error ? error.message : String(error);
|
|
4
|
-
}
|
|
5
|
-
|
|
6
1
|
/** Every cancellation starts with this text, matching Pi's built-in tools. */
|
|
7
2
|
export const OPERATION_ABORTED = "Operation aborted";
|
|
8
3
|
|
|
@@ -15,6 +10,16 @@ export function throwIfCancelled(signal: AbortSignal | undefined, detail?: strin
|
|
|
15
10
|
if (signal?.aborted) throw cancellationError(detail);
|
|
16
11
|
}
|
|
17
12
|
|
|
13
|
+
/**
|
|
14
|
+
* A searched file no longer matches what ripgrep reported. grep binds matches to
|
|
15
|
+
* one snapshot per file: rg-line-ranges checks line counts, the CRLF text view
|
|
16
|
+
* checks the snapshot copy, and grep-output checks matched text and the
|
|
17
|
+
* revision recorded by grep-search. All of them report this one error.
|
|
18
|
+
*/
|
|
19
|
+
export function searchChangedError(): Error {
|
|
20
|
+
return new Error("File changed during search; rerun the query.");
|
|
21
|
+
}
|
|
22
|
+
|
|
18
23
|
/** Rejection from a tool's prepareArguments for a check the schema cannot express. */
|
|
19
24
|
export function invalidArgument(path: string, reason: string): Error {
|
|
20
25
|
return new Error(`Invalid argument ${path}: ${reason}`);
|