@criogaid/pi-hashline-edit 0.4.0 → 0.5.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.
Files changed (60) hide show
  1. package/README.md +116 -20
  2. package/package.json +3 -3
  3. package/src/core/apply.test.ts +194 -104
  4. package/src/core/apply.ts +6 -37
  5. package/src/core/errors.ts +9 -0
  6. package/src/core/hash.test.ts +8 -8
  7. package/src/core/hash.ts +5 -5
  8. package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
  9. package/src/core/text.ts +1 -3
  10. package/src/core/types.ts +4 -7
  11. package/src/index.ts +9 -11
  12. package/src/integration/config-registration.test.ts +206 -0
  13. package/src/integration/grep-rg.test.ts +73 -60
  14. package/src/integration/large-text.test.ts +9 -8
  15. package/src/integration/replace-package.test.ts +11 -7
  16. package/src/pi/action-fusion.test.ts +110 -34
  17. package/src/pi/action-fusion.ts +77 -114
  18. package/src/pi/anchor-format.test.ts +43 -22
  19. package/src/pi/anchor-format.ts +23 -8
  20. package/src/pi/budgets.ts +7 -2
  21. package/src/pi/config.test.ts +153 -40
  22. package/src/pi/config.ts +87 -41
  23. package/src/pi/e2e-scenarios.test.ts +12 -11
  24. package/src/pi/edit-tool.ts +20 -156
  25. package/src/pi/error-text.ts +10 -5
  26. package/src/pi/execute.test.ts +299 -175
  27. package/src/pi/failure-context.test.ts +7 -2
  28. package/src/pi/failure-context.ts +155 -8
  29. package/src/pi/failure-path.test.ts +33 -26
  30. package/src/pi/file-commit.ts +2 -1
  31. package/src/pi/fusion-card.ts +83 -2
  32. package/src/pi/grep-output.ts +26 -9
  33. package/src/pi/grep-render.ts +6 -5
  34. package/src/pi/grep-scope.ts +2 -15
  35. package/src/pi/grep-search.ts +29 -15
  36. package/src/pi/grep-tool.ts +94 -78
  37. package/src/pi/grep.test.ts +357 -320
  38. package/src/pi/mutation-outcome.testing.ts +22 -0
  39. package/src/pi/mutation-result.test.ts +51 -2
  40. package/src/pi/mutation-result.ts +62 -55
  41. package/src/pi/mutation-runner.ts +16 -33
  42. package/src/pi/pi.test.ts +10 -5
  43. package/src/pi/read-tool.ts +61 -33
  44. package/src/pi/registration.test.ts +1 -5
  45. package/src/pi/render.test.ts +16 -8
  46. package/src/pi/render.ts +4 -91
  47. package/src/pi/replace-regex.ts +54 -0
  48. package/src/pi/replace-tool.ts +24 -58
  49. package/src/pi/replace-worker.ts +2 -2
  50. package/src/pi/replace.test.ts +62 -58
  51. package/src/pi/result-safety.test.ts +124 -84
  52. package/src/pi/rg-line-ranges.ts +3 -2
  53. package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
  54. package/src/pi/{rg-line-filter.ts → rg-process.ts} +35 -4
  55. package/src/pi/rg-text-view.ts +17 -33
  56. package/src/pi/schema.ts +2 -10
  57. package/src/pi/write-tool.ts +1 -2
  58. package/src/pi/write.test.ts +10 -9
  59. package/src/core/index.ts +0 -14
  60. package/src/pi/state.ts +0 -26
@@ -17,12 +17,13 @@ import { makeReadOverride } from "./read-tool.ts";
17
17
  import { makeWriteOverride } from "./write-tool.ts";
18
18
  import { makeReplaceTool } from "./replace-tool.ts";
19
19
  import { createActionFusionExecutor } from "./action-fusion.ts";
20
- import { getState } from "./state.ts";
20
+ import { loadConfig } from "./config.ts";
21
21
  import { computeLineHash } from "../core/hash.ts";
22
22
  import { splitLines } from "../core/lines.ts";
23
23
  import { byteRevision } from "./file-commit.ts";
24
24
  import { generateMutationDetails } from "./mutation-result.ts";
25
25
  import { callTool } from "./tool-call.testing.ts";
26
+ import { DEFAULT_CONFIG } from "./config.ts";
26
27
 
27
28
  async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
28
29
  const dir = await mkdtemp(join(tmpdir(), "hl-"));
@@ -38,7 +39,7 @@ const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0
38
39
 
39
40
  /** Anchor a model would copy from read output for `line` of `text` (1-based). */
40
41
  function h(text: string, line: number) {
41
- return `${line}#${computeLineHash(line, splitLines(text)[line - 1])}`;
42
+ return `${line}#${computeLineHash(line, splitLines(text)[line - 1], 4)}`;
42
43
  }
43
44
 
44
45
  /** Extract a `LINE#HASH` anchor from a read/edit result text block. */
@@ -49,7 +50,7 @@ function anchorLine(block: string, line: number) {
49
50
  }
50
51
 
51
52
  test("edit guidance keeps insert anchors and warns about shifted lines", () => {
52
- const tool = makeEditOverride(process.cwd());
53
+ const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
53
54
  const insertOp = tool.parameters.properties.edits.items.anyOf.find((variant) => {
54
55
  const op = variant.properties.op;
55
56
  return "anyOf" in op && op.anyOf.some((choice) => choice.const === "insert_after");
@@ -69,7 +70,7 @@ test("edit guidance keeps insert anchors and warns about shifted lines", () => {
69
70
  test("read execute: text outputs LINE#HASH│content", async () => {
70
71
  await withDir(async (dir) => {
71
72
  await writeFile(join(dir, "f.txt"), "line1\nline2\n");
72
- const r: any = await call(makeReadOverride(dir), { path: "f.txt" });
73
+ const r: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
73
74
  const text = r.content[0];
74
75
  assert.equal(text.type, "text");
75
76
  assert.match(text.text, /1#[0-9A-Z]+│line1/);
@@ -85,7 +86,7 @@ test("read execute: Pi-supported images without NUL bypass text decoding", async
85
86
  ["picture.gif", Buffer.from("GIF89a"), "image/gif"],
86
87
  ] as const) {
87
88
  await writeFile(join(dir, name), bytes);
88
- const result: any = await call(makeReadOverride(dir), { path: name });
89
+ const result: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: name });
89
90
  assert.ok(result.content[0].text.startsWith(`Read image file [${mime}]`));
90
91
  assert.deepEqual(await readFile(join(dir, name)), bytes);
91
92
  }
@@ -94,11 +95,11 @@ test("read execute: Pi-supported images without NUL bypass text decoding", async
94
95
  test("read execute: a missing final newline is stated in the header", async () => {
95
96
  await withDir(async (dir) => {
96
97
  await writeFile(join(dir, "f.txt"), "line1\nline2");
97
- const bare: any = await call(makeReadOverride(dir), { path: "f.txt" });
98
+ const bare: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
98
99
  assert.match(bare.content[0].text, /f\.txt · 2 lines · no trailing newline/);
99
100
 
100
101
  await writeFile(join(dir, "g.txt"), "line1\nline2\n");
101
- const terminated: any = await call(makeReadOverride(dir), { path: "g.txt" });
102
+ const terminated: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "g.txt" });
102
103
  assert.doesNotMatch(terminated.content[0].text, /no trailing newline/);
103
104
  });
104
105
  });
@@ -111,8 +112,8 @@ test("edit execute: a file without a final newline stays byte-exact", async () =
111
112
  // terminator must not turn into a new byte.
112
113
  const text = "guard\nold value\u2060";
113
114
  await writeFile(f, text);
114
- await call(makeReadOverride(dir), { path: "f.txt" });
115
- const r: any = await call(makeEditOverride(dir), {
115
+ await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
116
+ const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
116
117
  path: "f.txt",
117
118
  edits: [{ op: "replace", anchor: h(text, 2), body: ["new value\u2060"] }],
118
119
  });
@@ -126,8 +127,8 @@ test("edit execute: hashline round-trip (read → edit → file changed)", async
126
127
  const f = join(dir, "f.txt");
127
128
  const text = "a\nb\nc\n";
128
129
  await writeFile(f, text);
129
- await call(makeReadOverride(dir), { path: "f.txt" });
130
- const r: any = await call(makeEditOverride(dir), {
130
+ await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
131
+ const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
131
132
  path: "f.txt",
132
133
  edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
133
134
  });
@@ -141,8 +142,8 @@ test("edit execute: multiple ops in one call", async () => {
141
142
  const f = join(dir, "f.txt");
142
143
  const text = "a\nb\nc\n";
143
144
  await writeFile(f, text);
144
- await call(makeReadOverride(dir), { path: "f.txt" });
145
- const r: any = await call(makeEditOverride(dir), {
145
+ await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
146
+ const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
146
147
  path: "f.txt",
147
148
  edits: [
148
149
  { op: "insert_after", anchor: h(text, 3), body: ["z"] },
@@ -159,7 +160,7 @@ test("edit result returns Updated anchors that chain the next edit without a re-
159
160
  const f = join(dir, "f.txt");
160
161
  const text = "a\nb\nc\n";
161
162
  await writeFile(f, text);
162
- const edit = makeEditOverride(dir);
163
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
163
164
  // first edit (model cites the read anchor for line 1)
164
165
  const r1: any = await call(edit, {
165
166
  path: "f.txt",
@@ -184,7 +185,7 @@ test("edit result anchors cover an inserted block (chain an edit inside it)", as
184
185
  const f = join(dir, "f.txt");
185
186
  const text = "a\nb\n";
186
187
  await writeFile(f, text);
187
- const edit = makeEditOverride(dir);
188
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
188
189
  const r1: any = await call(edit, {
189
190
  path: "f.txt",
190
191
  edits: [{ op: "insert_after", anchor: h(text, 2), body: ["c", "d", "e"] }],
@@ -209,7 +210,7 @@ test("unrelated external change does NOT block an edit on a stable line", async
209
210
  await writeFile(f, text);
210
211
  // simulate an external change at line 3 between read and edit
211
212
  await writeFile(f, "a\nb\nCHANGED\n");
212
- const r: any = await call(makeEditOverride(dir), {
213
+ const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
213
214
  path: "f.txt",
214
215
  edits: [{ op: "replace", anchor: h(text, 1), body: ["A"] }],
215
216
  });
@@ -225,7 +226,7 @@ test("edit on a line that changed externally → anchor mismatch", async () => {
225
226
  await writeFile(f, text);
226
227
  await writeFile(f, "a\nBCHANGED\nc\n"); // line 2 changed
227
228
  await assert.rejects(
228
- call(makeEditOverride(dir), {
229
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
229
230
  path: "f.txt",
230
231
  edits: [{ op: "replace", anchor: h(text, 2), body: ["x"] }],
231
232
  }),
@@ -243,7 +244,7 @@ test("unresolved observations require target confirmation before retrying a move
243
244
  file,
244
245
  ["one", "two", "three", "four", "changed", "six", "new marker", "eight"].join("\n") + "\n",
245
246
  );
246
- const edit = makeEditOverride(dir);
247
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
247
248
  await assert.rejects(
248
249
  call(edit, {
249
250
  path: "recover.txt",
@@ -260,7 +261,7 @@ test("unresolved observations require target confirmation before retrying a move
260
261
  return true;
261
262
  },
262
263
  );
263
- const read = await call(makeReadOverride(dir), { path: "recover.txt" });
264
+ const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "recover.txt" });
264
265
  const retryAnchor = anchorLine(read.content[0].text, 7);
265
266
  await call(edit, {
266
267
  path: "recover.txt",
@@ -277,7 +278,7 @@ test("edit execute: no read before edit → anchor verification fails", async ()
277
278
  await withDir(async (dir) => {
278
279
  await writeFile(join(dir, "f.txt"), "a\nb\n");
279
280
  await assert.rejects(
280
- call(makeEditOverride(dir), {
281
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
281
282
  path: "f.txt",
282
283
  edits: [{ op: "replace", anchor: "1#XXXX", body: ["A"] }],
283
284
  }),
@@ -290,7 +291,7 @@ test("edit execute: empty edits → throws", async () => {
290
291
  await withDir(async (dir) => {
291
292
  await writeFile(join(dir, "f.txt"), "a\n");
292
293
  await assert.rejects(
293
- call(makeEditOverride(dir), { path: "f.txt", edits: [] }),
294
+ call(makeEditOverride(dir, DEFAULT_CONFIG), { path: "f.txt", edits: [] }),
294
295
  /Validation failed for tool "edit"/,
295
296
  );
296
297
  });
@@ -300,7 +301,7 @@ test("edit execute: malformed op (replace without body) → throws", async () =>
300
301
  await withDir(async (dir) => {
301
302
  await writeFile(join(dir, "f.txt"), "a\n");
302
303
  await assert.rejects(
303
- call(makeEditOverride(dir), {
304
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
304
305
  path: "f.txt",
305
306
  edits: [{ op: "replace", anchor: "1#XXXX" }],
306
307
  }),
@@ -314,8 +315,8 @@ test("edit execute: delete op", async () => {
314
315
  const f = join(dir, "f.txt");
315
316
  const text = "a\nb\nc\n";
316
317
  await writeFile(f, text);
317
- await call(makeReadOverride(dir), { path: "f.txt" });
318
- const r: any = await call(makeEditOverride(dir), {
318
+ await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
319
+ const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
319
320
  path: "f.txt",
320
321
  edits: [{ op: "delete", anchor: h(text, 2) }],
321
322
  });
@@ -327,8 +328,6 @@ test("edit execute: delete op", async () => {
327
328
  test("disabled config registers no tools — built-ins remain", async () => {
328
329
  await withDir(async (dir) => {
329
330
  const oldCwd = process.cwd();
330
- const state = getState();
331
- const previous = state.config;
332
331
  try {
333
332
  await mkdir(join(dir, ".pi"));
334
333
  await writeFile(
@@ -354,7 +353,6 @@ test("disabled config registers no tools — built-ins remain", async () => {
354
353
  assert.equal(await readFile(join(dir, "f.txt"), "utf-8"), "new value\n");
355
354
  } finally {
356
355
  process.chdir(oldCwd);
357
- state.config = previous;
358
356
  }
359
357
  });
360
358
  });
@@ -372,8 +370,8 @@ test("edit success: details.diff is a string (not the generateDiffString object)
372
370
  const f = join(dir, "f.txt");
373
371
  const text = "a\nb\nc\n";
374
372
  await writeFile(f, text);
375
- await call(makeReadOverride(dir), { path: "f.txt" });
376
- const r: any = await call(makeEditOverride(dir), {
373
+ await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
374
+ const r: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
377
375
  path: "f.txt",
378
376
  edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
379
377
  });
@@ -388,7 +386,7 @@ test("edit success: renderResult renders the diff without throwing", async () =>
388
386
  const f = join(dir, "f.txt");
389
387
  const text = "a\nb\nc\n";
390
388
  await writeFile(f, text);
391
- const edit = makeEditOverride(dir);
389
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
392
390
  const r: any = await call(edit, {
393
391
  path: "f.txt",
394
392
  edits: [{ op: "replace", anchor: h(text, 2), body: ["B"] }],
@@ -413,8 +411,8 @@ test("edit header: renderResult refreshes the call header in place — no invali
413
411
  const f = join(dir, "f.txt");
414
412
  const text = "a\nb\nc\nd\ne\n";
415
413
  await writeFile(f, text);
416
- await call(makeReadOverride(dir), { path: "f.txt" });
417
- const edit = makeEditOverride(dir);
414
+ await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
415
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
418
416
  const r: any = await call(edit, {
419
417
  path: "f.txt",
420
418
  edits: [
@@ -463,7 +461,7 @@ test("edit header: renderResult refreshes the call header in place — no invali
463
461
  test("edit error: renderResult renders the error line without throwing", async () => {
464
462
  await withDir(async (dir) => {
465
463
  await writeFile(join(dir, "f.txt"), "a\n");
466
- const edit = makeEditOverride(dir);
464
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
467
465
  let thrown: any;
468
466
  await call(edit, {
469
467
  path: "f.txt",
@@ -497,7 +495,7 @@ test("hash length stays 4 even for runs of identical lines (no explosion)", asyn
497
495
  await withDir(async (dir) => {
498
496
  const f = join(dir, "f.txt");
499
497
  await writeFile(f, "\n\n\n\ncode\n");
500
- const r: any = await call(makeReadOverride(dir), { path: "f.txt" });
498
+ const r: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
501
499
  const text: string = r.content[0].text;
502
500
  for (const m of text.matchAll(/\d+#([0-9A-Z]+)│/g)) {
503
501
  assert.equal(m[1].length, 4, `anchor ${m[0]} hash is not 4 chars`);
@@ -508,27 +506,31 @@ test("hash length stays 4 even for runs of identical lines (no explosion)", asyn
508
506
  test("read byte truncation counts UTF-8 and separators without cutting anchors", async () =>
509
507
  withDir(async (dir) => {
510
508
  const maxBytes = 256 * 1024;
511
- const hashLen = getState().config.hashLen;
509
+ const hashLen = DEFAULT_CONFIG.hashLen;
512
510
  const prefixBytes = Buffer.byteLength(`1#${"X".repeat(hashLen)}│`);
513
511
  const first = "界".repeat(40000);
514
512
  const second = "x".repeat(maxBytes - Buffer.byteLength(first) - 2 * prefixBytes);
515
513
  await writeFile(join(dir, "large.txt"), `${first}\n${second}\n`);
516
- const result = await call(makeReadOverride(dir), { path: "large.txt" });
514
+ const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "large.txt" });
517
515
  assert.equal(result.details.truncation.outputLines, 1);
518
516
  assert.equal(result.details.truncation.truncatedBy, "bytes");
519
- assert.match(result.content[0].text, /truncated at 256KB/);
517
+ assert.match(result.content[0].text, /truncated at 256 KiB/);
520
518
  assert.ok(
521
519
  result.content[0].text.includes(`1#${computeLineHash(1, first, hashLen)}│${first}\n`),
522
520
  );
523
521
  assert.doesNotMatch(result.content[0].text, /\n2#/);
524
- const next = await call(makeReadOverride(dir), { path: "large.txt", offset: 2, limit: 1 });
522
+ const next = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
523
+ path: "large.txt",
524
+ offset: 2,
525
+ limit: 1,
526
+ });
525
527
  assert.ok(next.content[0].text.includes(`2#${computeLineHash(2, second, hashLen)}│${second}`));
526
528
  }));
527
529
 
528
530
  test("read reports an oversized first row without suggesting an ineffective retry", async () =>
529
531
  withDir(async (dir) => {
530
532
  await writeFile(join(dir, "long.txt"), "x".repeat(256 * 1024));
531
- const result = await call(makeReadOverride(dir), { path: "long.txt" });
533
+ const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "long.txt" });
532
534
  assert.equal(result.details.truncation.firstLineExceedsLimit, true);
533
535
  assert.equal(result.details.truncation.outputLines, 0);
534
536
  assert.match(result.content[0].text, /cannot return a complete anchor row/);
@@ -540,11 +542,14 @@ test("read reports an oversized first row without suggesting an ineffective retr
540
542
  test("read preserves empty files and explicit limits above the native default", async () =>
541
543
  withDir(async (dir) => {
542
544
  await writeFile(join(dir, "empty.txt"), "");
543
- const empty = await call(makeReadOverride(dir), { path: "empty.txt" });
545
+ const empty = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "empty.txt" });
544
546
  assert.match(empty.content[0].text, /0 lines/);
545
547
  assert.equal(empty.details, undefined);
546
548
  await writeFile(join(dir, "many.txt"), "x\n".repeat(2001));
547
- const many = await call(makeReadOverride(dir), { path: "many.txt", limit: 2001 });
549
+ const many = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
550
+ path: "many.txt",
551
+ limit: 2001,
552
+ });
548
553
  assert.match(many.content[0].text, /\n2001#[0-9A-Z]+│x/);
549
554
  assert.equal(many.details, undefined);
550
555
  }));
@@ -555,7 +560,7 @@ test("read defaults to 500 lines when limit is omitted and respects explicit lim
555
560
  join(dir, "large.txt"),
556
561
  Array.from({ length: 600 }, (_, i) => `line${i + 1}\n`).join(""),
557
562
  );
558
- const def = await call(makeReadOverride(dir), { path: "large.txt" });
563
+ const def = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "large.txt" });
559
564
  assert.match(def.content[0].text, /large\.txt · 600 lines/);
560
565
  assert.match(def.content[0].text, /\n500#[0-9A-Z]+│line500/);
561
566
  assert.doesNotMatch(def.content[0].text, /\n501#[0-9A-Z]+│/);
@@ -564,7 +569,10 @@ test("read defaults to 500 lines when limit is omitted and respects explicit lim
564
569
  pagination: { start: 1, end: 500, totalLines: 600, nextOffset: 501 },
565
570
  });
566
571
 
567
- const custom = await call(makeReadOverride(dir), { path: "large.txt", limit: 550 });
572
+ const custom = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
573
+ path: "large.txt",
574
+ limit: 550,
575
+ });
568
576
  assert.match(custom.content[0].text, /\n550#[0-9A-Z]+│line550/);
569
577
  assert.doesNotMatch(custom.content[0].text, /\n551#[0-9A-Z]+│/);
570
578
  assert.match(custom.content[0].text, /showing lines 1-550 of 600; use offset 551 to continue/);
@@ -579,7 +587,7 @@ test("read pagination supports offset windows and stops suggesting continuation
579
587
  join(dir, "pages.txt"),
580
588
  Array.from({ length: 600 }, (_, i) => `line${i + 1}\n`).join(""),
581
589
  );
582
- const read = makeReadOverride(dir);
590
+ const read = makeReadOverride(dir, DEFAULT_CONFIG);
583
591
  const page = await call(read, { path: "pages.txt", offset: 20 });
584
592
  assert.match(page.content[0].text, /showing lines 20-519 of 600; use offset 520 to continue/);
585
593
  assert.deepEqual(page.details, {
@@ -603,7 +611,7 @@ test("read pagination supports offset windows and stops suggesting continuation
603
611
  test("read rejects noninteger and nonpositive offsets and limits before reading", async () =>
604
612
  withDir(async (dir) => {
605
613
  await writeFile(join(dir, "pages.txt"), "first\nsecond\nthird\n");
606
- const read = makeReadOverride(dir);
614
+ const read = makeReadOverride(dir, DEFAULT_CONFIG);
607
615
  for (const key of ["offset", "limit"] as const) {
608
616
  const schema = read.parameters.properties[key];
609
617
  assert.equal(schema.type, "number");
@@ -621,17 +629,6 @@ test("read rejects noninteger and nonpositive offsets and limits before reading"
621
629
  Number.MAX_SAFE_INTEGER + 1,
622
630
  ]) {
623
631
  const params = { path: "pages.txt", [key]: invalid };
624
- assert.throws(
625
- () =>
626
- validateToolArguments(read as any, {
627
- type: "toolCall",
628
- id: "0",
629
- name: "read",
630
- arguments: params,
631
- }),
632
- /Validation failed/,
633
- `schema must reject ${key} ${invalid}`,
634
- );
635
632
  await assert.rejects(
636
633
  call(read, params),
637
634
  new RegExp(`Validation failed for tool "read":\\n {2}- ${key}: `),
@@ -659,27 +656,17 @@ test("read rejects noninteger and nonpositive offsets and limits before reading"
659
656
  });
660
657
  await writeFile(join(dir, "long.txt"), "x".repeat(300 * 1024));
661
658
  const long = await call(read, { path: "long.txt", offset: 1, limit: 1 });
662
- assert.match(long.content[0].text, /line 1 exceeds 256KB/);
659
+ assert.match(long.content[0].text, /line 1 exceeds 256 KiB/);
663
660
  }));
664
661
 
665
662
  test("read schema rejects empty paths and unknown fields before file access", async () =>
666
663
  withDir(async (dir) => {
667
- const read = makeReadOverride(dir);
664
+ const read = makeReadOverride(dir, DEFAULT_CONFIG);
668
665
  const invalidArgs: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
669
666
  { path: "" },
670
667
  { path: "missing.txt", offest: 3 },
671
668
  ];
672
669
  for (const args of invalidArgs) {
673
- assert.throws(
674
- () =>
675
- validateToolArguments(read as any, {
676
- type: "toolCall",
677
- id: "invalid",
678
- name: "read",
679
- arguments: args,
680
- }),
681
- /Validation failed/,
682
- );
683
670
  await assert.rejects(call(read, args), /Validation failed for tool "read"/);
684
671
  }
685
672
  }));
@@ -687,10 +674,13 @@ test("read schema rejects empty paths and unknown fields before file access", as
687
674
  test("read byte truncation takes precedence over line pagination", async () =>
688
675
  withDir(async (dir) => {
689
676
  await writeFile(join(dir, "large.txt"), `first\n${"x".repeat(256 * 1024)}\ntail\n`);
690
- const result = await call(makeReadOverride(dir), { path: "large.txt", limit: 2 });
677
+ const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
678
+ path: "large.txt",
679
+ limit: 2,
680
+ });
691
681
  assert.equal(result.details.truncation.truncatedBy, "bytes");
692
682
  assert.equal(result.details.pagination, undefined);
693
- assert.match(result.content[0].text, /truncated at 256KB/);
683
+ assert.match(result.content[0].text, /truncated at 256 KiB/);
694
684
  assert.doesNotMatch(result.content[0].text, /showing lines|to continue/);
695
685
  }));
696
686
 
@@ -704,7 +694,7 @@ test("native read and write renderers preserve resource titles, previews, and fu
704
694
  isPartial: false,
705
695
  lastComponent: undefined,
706
696
  };
707
- const read = makeReadOverride(dir);
697
+ const read = makeReadOverride(dir, DEFAULT_CONFIG);
708
698
  const readCall = read.renderCall!({ path: "SKILL.md", offset: 2, limit: 3 }, stubTheme, {
709
699
  ...context,
710
700
  args: { path: "SKILL.md" },
@@ -739,20 +729,12 @@ test("edit schema rejects misspelled range fields and invalid operation shapes",
739
729
  withDir(async (dir) => {
740
730
  const original = "a\nb\nc\n";
741
731
  await writeFile(join(dir, "range.txt"), original);
742
- const edit = makeEditOverride(dir);
732
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
743
733
  const anchor = h(original, 2);
744
734
  const callArgs = {
745
735
  path: "range.txt",
746
736
  edits: [{ op: "replace", anchor, endd: h(original, 3), body: ["merged"] }],
747
737
  };
748
- const check = (args: Parameters<typeof validateToolArguments>[1]["arguments"]) =>
749
- validateToolArguments(edit as any, {
750
- type: "toolCall",
751
- id: "schema-check",
752
- name: "edit",
753
- arguments: args,
754
- });
755
- assert.throws(() => check(callArgs), /Validation failed/);
756
738
  await assert.rejects(call(edit, callArgs), /edits\.0\.endd: schema is false/);
757
739
  assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
758
740
  for (const edits of [
@@ -762,13 +744,13 @@ test("edit schema rejects misspelled range fields and invalid operation shapes",
762
744
  [{ op: "insert_after", body: ["bad"] }],
763
745
  [{ op: "append", anchor, body: ["bad"] }],
764
746
  ]) {
765
- assert.throws(() => check({ path: "range.txt", edits }), /Validation failed/);
747
+ await assert.rejects(call(edit, { path: "range.txt", edits }), /Validation failed/);
748
+ assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
766
749
  }
767
750
  const alternates: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
768
751
  { path: "range.txt", op: "replace", anchor, body: ["ok"] },
769
752
  { path: "range.txt", edits: JSON.stringify([{ op: "replace", anchor, body: ["ok"] }]) },
770
753
  ];
771
- for (const alternate of alternates) assert.throws(() => check(alternate), /Validation failed/);
772
754
  for (const alternate of alternates) {
773
755
  await assert.rejects(call(edit, alternate), /Validation failed/);
774
756
  assert.equal(await readFile(join(dir, "range.txt"), "utf8"), original);
@@ -779,8 +761,8 @@ test("copied string anchors validate and replace an inclusive range", async () =
779
761
  withDir(async (dir) => {
780
762
  const original = "a\nb\nc\nd\n";
781
763
  await writeFile(join(dir, "range.txt"), original);
782
- const read = await call(makeReadOverride(dir), { path: "range.txt" });
783
- const edit = makeEditOverride(dir);
764
+ const read = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "range.txt" });
765
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
784
766
  const args = validateToolArguments(edit as any, {
785
767
  type: "toolCall",
786
768
  id: "range",
@@ -805,7 +787,7 @@ test("invalid anchors and conflicting fields fail before changing the file", asy
805
787
  withDir(async (dir) => {
806
788
  const original = "a\nb\n";
807
789
  await writeFile(join(dir, "invalid.txt"), original);
808
- const edit = makeEditOverride(dir);
790
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
809
791
  const anchor = h(original, 1);
810
792
  const invalid = [
811
793
  { op: "replace", anchor: { line: 1, hash: anchor.split("#")[1] }, body: ["changed"] },
@@ -823,21 +805,13 @@ test("invalid anchors and conflicting fields fail before changing the file", asy
823
805
  );
824
806
  assert.equal(await readFile(join(dir, "invalid.txt"), "utf8"), original);
825
807
  }
826
- assert.throws(() =>
827
- validateToolArguments(edit as any, {
828
- type: "toolCall",
829
- id: "invalid",
830
- name: "edit",
831
- arguments: { path: "invalid.txt", edits: [invalid[0]] } as any,
832
- }),
833
- );
834
808
  }));
835
809
 
836
810
  test("edit execute accepts empty insertion bodies and empty replacements delete the anchored range", async () =>
837
811
  withDir(async (dir) => {
838
812
  const original = "a\nb\nc\n";
839
813
  await writeFile(join(dir, "empty_body.txt"), original);
840
- const edit = makeEditOverride(dir);
814
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
841
815
  const anchor = h(original, 1);
842
816
  for (const op of ["insert_after", "insert_before", "append", "prepend"] as const) {
843
817
  const editOp =
@@ -856,7 +830,7 @@ test("edit execute accepts empty insertion bodies and empty replacements delete
856
830
  test("local and full-file recovery return copyable anchors without changing the rejected batch", async () =>
857
831
  withDir(async (dir) => {
858
832
  const original = "a\nb\n";
859
- const edit = makeEditOverride(dir);
833
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
860
834
  for (const prefixLines of [1, 40]) {
861
835
  const prefix = "prefix\n".repeat(prefixLines);
862
836
  await writeFile(join(dir, "shift.txt"), prefix + original);
@@ -899,14 +873,14 @@ test("shifted-anchor recovery keeps the read fallback for oversized candidates",
899
873
  const oversized = "x".repeat(4 * 1024);
900
874
  const original = `a\n${oversized}\n`;
901
875
  await writeFile(join(dir, "shift-large.txt"), `prefix\n${original}`);
902
- const edit = makeEditOverride(dir);
876
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
903
877
  await assert.rejects(
904
878
  call(edit, {
905
879
  path: "shift-large.txt",
906
880
  edits: [{ op: "replace", anchor: h(original, 2), body: ["B"] }],
907
881
  }),
908
882
  (error: Error) => {
909
- assert.match(error.message, /Candidate content exceeds 4096 bytes/);
883
+ assert.match(error.message, /Candidate content exceeds 4 KiB/);
910
884
  assert.match(error.message, /use read or grep/);
911
885
  assert.doesNotMatch(error.message, new RegExp(`│x{${oversized.length}}`));
912
886
  return true;
@@ -927,7 +901,7 @@ test("failed commands preserve mutation results and stay out of all main card re
927
901
  {
928
902
  path: "edit.txt",
929
903
  run: async () => {
930
- const tool = makeEditOverride(dir, fusion);
904
+ const tool = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
931
905
  const args = { path: "edit.txt", edits: [{ op: "append" as const, body: ["after"] }] };
932
906
  const result = await callTool(
933
907
  tool,
@@ -957,7 +931,7 @@ test("failed commands preserve mutation results and stay out of all main card re
957
931
  {
958
932
  path: "replace.txt",
959
933
  run: async () => {
960
- const tool = makeReplaceTool(dir, fusion);
934
+ const tool = makeReplaceTool(dir, DEFAULT_CONFIG, fusion);
961
935
  const args = {
962
936
  path: "replace.txt",
963
937
  replacements: [{ find: "before", replace: "after" }],
@@ -1043,18 +1017,18 @@ test("text tools reject malformed UTF-8 and NUL bytes without rewriting source b
1043
1017
  const original = Buffer.from([0x61, 0x0a, 0xc3, 0x28, 0x0a]);
1044
1018
  await writeFile(target, original);
1045
1019
  await assert.rejects(
1046
- call(makeReadOverride(dir), { path: "invalid-utf8.txt" }),
1020
+ call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "invalid-utf8.txt" }),
1047
1021
  /UNSUPPORTED_ENCODING/,
1048
1022
  );
1049
1023
  await assert.rejects(
1050
- call(makeEditOverride(dir), {
1024
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
1051
1025
  path: "invalid-utf8.txt",
1052
1026
  edits: [{ op: "append", body: ["x"] }],
1053
1027
  }),
1054
1028
  /UNSUPPORTED_ENCODING/,
1055
1029
  );
1056
1030
  await assert.rejects(
1057
- call(makeReplaceTool(dir), {
1031
+ call(makeReplaceTool(dir, DEFAULT_CONFIG), {
1058
1032
  path: "invalid-utf8.txt",
1059
1033
  replacements: [{ find: "a", replace: "b" }],
1060
1034
  }),
@@ -1066,22 +1040,28 @@ test("text tools reject malformed UTF-8 and NUL bytes without rewriting source b
1066
1040
  const nulOriginal = Buffer.from([0x61, 0x00, 0x62]);
1067
1041
  await writeFile(nulTarget, nulOriginal);
1068
1042
  await assert.rejects(
1069
- call(makeEditOverride(dir), { path: "nul.txt", edits: [{ op: "append", body: ["x"] }] }),
1043
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
1044
+ path: "nul.txt",
1045
+ edits: [{ op: "append", body: ["x"] }],
1046
+ }),
1070
1047
  /UNSUPPORTED_TEXT/,
1071
1048
  );
1072
1049
  await assert.rejects(
1073
- call(makeReplaceTool(dir), { path: "nul.txt", replacements: [{ find: "a", replace: "b" }] }),
1050
+ call(makeReplaceTool(dir, DEFAULT_CONFIG), {
1051
+ path: "nul.txt",
1052
+ replacements: [{ find: "a", replace: "b" }],
1053
+ }),
1074
1054
  /UNSUPPORTED_TEXT/,
1075
1055
  );
1076
1056
  assert.deepEqual(await readFile(nulTarget), nulOriginal);
1077
1057
 
1078
- const nativeRead: any = await call(makeReadOverride(dir), { path: "nul.txt" });
1058
+ const nativeRead: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "nul.txt" });
1079
1059
  assert.equal(nativeRead.content[0].text, "a\0b");
1080
1060
 
1081
1061
  const utf16Target = join(dir, "utf16.txt");
1082
1062
  await writeFile(utf16Target, Buffer.from([0xff, 0xfe, 0x49, 0x6c]));
1083
1063
  await assert.rejects(
1084
- call(makeReadOverride(dir), { path: "utf16.txt" }),
1064
+ call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "utf16.txt" }),
1085
1065
  /UNSUPPORTED_ENCODING/,
1086
1066
  );
1087
1067
  }));
@@ -1090,25 +1070,22 @@ test("edit rejects embedded line terminators at its schema boundary", async () =
1090
1070
  withDir(async (dir) => {
1091
1071
  const target = join(dir, "body.txt");
1092
1072
  await writeFile(target, "a\n");
1093
- await assert.rejects(
1094
- call(makeEditOverride(dir), { path: "body.txt", edits: [{ op: "append", body: ["x\ny"] }] }),
1095
- /Validation failed for tool "edit"/,
1096
- );
1097
- assert.equal(await readFile(target, "utf8"), "a\n");
1098
- assert.throws(() =>
1099
- validateToolArguments(makeEditOverride(dir) as any, {
1100
- type: "toolCall",
1101
- id: "body",
1102
- name: "edit",
1103
- arguments: { path: "body.txt", edits: [{ op: "append", body: ["x\ny"] }] },
1104
- }),
1105
- );
1073
+ for (const line of ["x\ny", "x\ry"]) {
1074
+ await assert.rejects(
1075
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
1076
+ path: "body.txt",
1077
+ edits: [{ op: "append", body: [line] }],
1078
+ }),
1079
+ /Validation failed for tool "edit"/,
1080
+ );
1081
+ assert.equal(await readFile(target, "utf8"), "a\n");
1082
+ }
1106
1083
  }));
1107
1084
 
1108
1085
  test("edit rejects unwritable body lines before reading a missing target", async () =>
1109
1086
  withDir(async (dir) => {
1110
1087
  const target = join(dir, "missing.txt");
1111
- const edit = makeEditOverride(dir);
1088
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1112
1089
  const valid = { path: target, edits: [{ op: "append" as const, body: ["ok"] }] };
1113
1090
  assert.equal(edit.prepareArguments(valid), valid);
1114
1091
  for (const [line, expected] of [
@@ -1128,7 +1105,7 @@ test("edit preserves a UTF-8 BOM and reports bound mutation revisions", async ()
1128
1105
  const target = join(dir, "bom.txt");
1129
1106
  const original = Buffer.from("\ufeffguard\nold\n", "utf8");
1130
1107
  await writeFile(target, original);
1131
- const result: any = await call(makeEditOverride(dir), {
1108
+ const result: any = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
1132
1109
  path: "bom.txt",
1133
1110
  edits: [{ op: "replace", anchor: h("\ufeffguard\nold\n", 2), body: ["new"] }],
1134
1111
  });
@@ -1146,7 +1123,7 @@ test("edit and replace reject NUL arguments without rewriting source bytes", asy
1146
1123
  const original = Buffer.from("\ufeffbefore\r\n", "utf8");
1147
1124
  await writeFile(target, original);
1148
1125
  await assert.rejects(
1149
- call(makeEditOverride(dir), {
1126
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
1150
1127
  path: "output.txt",
1151
1128
  edits: [{ op: "append", body: ["bad\0text"] }],
1152
1129
  }),
@@ -1154,7 +1131,7 @@ test("edit and replace reject NUL arguments without rewriting source bytes", asy
1154
1131
  );
1155
1132
  assert.deepEqual(await readFile(target), original);
1156
1133
  await assert.rejects(
1157
- call(makeReplaceTool(dir), {
1134
+ call(makeReplaceTool(dir, DEFAULT_CONFIG), {
1158
1135
  path: "output.txt",
1159
1136
  replacements: [{ find: "before", replace: "bad\0text" }],
1160
1137
  }),
@@ -1176,7 +1153,7 @@ test("failed edits expose input status and candidate code for a verified fused r
1176
1153
  commands++;
1177
1154
  return "checked";
1178
1155
  });
1179
- const edit = makeEditOverride(dir, fusion);
1156
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusion);
1180
1157
  const stale = h(original.join("\n"), 5);
1181
1158
  const stable = h(before, 12);
1182
1159
  const candidate = h(before, 6);
@@ -1238,7 +1215,7 @@ test("ambiguous candidates include distinguishing neighborhoods for a verified r
1238
1215
  const before = lines.join("\n") + "\n";
1239
1216
  const file = join(dir, "ambiguous.txt");
1240
1217
  await writeFile(file, before);
1241
- const edit = makeEditOverride(dir);
1218
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1242
1219
  let context = "";
1243
1220
  await assert.rejects(
1244
1221
  call(edit, {
@@ -1275,7 +1252,7 @@ test("schema-invalid bodies omit anchor checks; subsequent retries revalidate",
1275
1252
  const before = "a\nb\nc\n";
1276
1253
  const file = join(dir, "checks.txt");
1277
1254
  await writeFile(file, before);
1278
- const edit = makeEditOverride(dir);
1255
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1279
1256
  const stable = h(before, 1);
1280
1257
  await assert.rejects(
1281
1258
  call(edit, {
@@ -1287,7 +1264,7 @@ test("schema-invalid bodies omit anchor checks; subsequent retries revalidate",
1287
1264
  }),
1288
1265
  (error: Error) => {
1289
1266
  assert.match(error.message, /Validation failed for tool "edit"/);
1290
- assert.doesNotMatch(error.message, /Input-anchor checks|not_checked|\/ matched/);
1267
+ assert.doesNotMatch(error.message, /Input-anchor checks|\/ matched|\/ mismatched/);
1291
1268
  return true;
1292
1269
  },
1293
1270
  );
@@ -1328,7 +1305,7 @@ test("single-operation edit failures omit redundant Input-anchor checks table",
1328
1305
  withDir(async (dir) => {
1329
1306
  const file = join(dir, "single.txt");
1330
1307
  await writeFile(file, "line1\nline2\n");
1331
- const edit = makeEditOverride(dir);
1308
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1332
1309
  await assert.rejects(
1333
1310
  call(edit, { path: file, edits: [{ op: "replace", anchor: "1#XXXX", body: ["new"] }] }),
1334
1311
  (error: Error) => {
@@ -1344,7 +1321,7 @@ test("single-op range edits with two anchors omit Input-anchor checks table on f
1344
1321
  withDir(async (dir) => {
1345
1322
  const file = join(dir, "range.txt");
1346
1323
  await writeFile(file, "line1\nline2\nline3\n");
1347
- const edit = makeEditOverride(dir);
1324
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1348
1325
  await assert.rejects(
1349
1326
  call(edit, {
1350
1327
  path: file,
@@ -1361,7 +1338,7 @@ test("multi-op schema failures omit anchor checks even when later ops have ancho
1361
1338
  withDir(async (dir) => {
1362
1339
  const file = join(dir, "batch-append.txt");
1363
1340
  await writeFile(file, "line1\nline2\n");
1364
- const edit = makeEditOverride(dir);
1341
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1365
1342
  const stable = h("line1\nline2\n", 1);
1366
1343
  // Static body validation occurs before snapshot-based anchor verification.
1367
1344
  await assert.rejects(
@@ -1374,7 +1351,7 @@ test("multi-op schema failures omit anchor checks even when later ops have ancho
1374
1351
  }),
1375
1352
  (error: Error) => {
1376
1353
  assert.match(error.message, /Validation failed for tool "edit"/);
1377
- assert.doesNotMatch(error.message, /Input-anchor checks|not_checked/);
1354
+ assert.doesNotMatch(error.message, /Input-anchor checks/);
1378
1355
  return true;
1379
1356
  },
1380
1357
  );
@@ -1392,20 +1369,23 @@ test("failed batches return more than forty checks and mappings when byte budget
1392
1369
  anchor: h(original, 2),
1393
1370
  body: ["changed"],
1394
1371
  }));
1395
- await assert.rejects(call(makeEditOverride(dir), { path: file, edits }), (error: Error) => {
1396
- assert.doesNotMatch(
1397
- error.message,
1398
- /truncated|failure details omitted|Anchor checks: \d+\/\d+/,
1399
- );
1400
- assert.equal(
1401
- (error.message.match(/^op \d+ \/ anchor \/ .* \/ mismatched$/gm) ?? []).length,
1402
- 45,
1403
- );
1404
- assert.equal((error.message.match(/checksum-matching candidate/g) ?? []).length, 45);
1405
- assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
1406
- assert.doesNotMatch(error.message, /\/ matched/);
1407
- return true;
1408
- });
1372
+ await assert.rejects(
1373
+ call(makeEditOverride(dir, DEFAULT_CONFIG), { path: file, edits }),
1374
+ (error: Error) => {
1375
+ assert.doesNotMatch(
1376
+ error.message,
1377
+ /truncated|failure details omitted|Anchor checks: \d+\/\d+/,
1378
+ );
1379
+ assert.equal(
1380
+ (error.message.match(/^op \d+ \/ anchor \/ .* \/ mismatched$/gm) ?? []).length,
1381
+ 45,
1382
+ );
1383
+ assert.equal((error.message.match(/checksum-matching candidate/g) ?? []).length, 45);
1384
+ assert.equal((error.message.match(/^3#[0-9A-Z]+│target$/gm) ?? []).length, 1);
1385
+ assert.doesNotMatch(error.message, /\/ matched/);
1386
+ return true;
1387
+ },
1388
+ );
1409
1389
  assert.equal(await readFile(file, "utf8"), before);
1410
1390
  }));
1411
1391
 
@@ -1414,7 +1394,7 @@ test("compact edit anchors retain only untouched deletion successors in mixed ba
1414
1394
  const before = "a\nb\nc\nd\ne\nf\ng\n";
1415
1395
  const file = join(dir, "mixed.txt");
1416
1396
  await writeFile(file, before);
1417
- const edit = makeEditOverride(dir);
1397
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1418
1398
  const result = await call(edit, {
1419
1399
  path: file,
1420
1400
  edits: [
@@ -1446,7 +1426,7 @@ test("unique candidate content is independent of oversized neighboring rows", as
1446
1426
  await writeFile(file, before);
1447
1427
  let candidate = "";
1448
1428
  await assert.rejects(
1449
- call(makeEditOverride(dir), {
1429
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
1450
1430
  path: file,
1451
1431
  edits: [{ op: "replace", anchor: h(original, 2), body: ["updated"] }],
1452
1432
  }),
@@ -1459,7 +1439,7 @@ test("unique candidate content is independent of oversized neighboring rows", as
1459
1439
  },
1460
1440
  );
1461
1441
  assert.equal(await readFile(file, "utf8"), before);
1462
- await call(makeEditOverride(dir), {
1442
+ await call(makeEditOverride(dir, DEFAULT_CONFIG), {
1463
1443
  path: file,
1464
1444
  edits: [{ op: "replace", anchor: candidate, body: ["updated"] }],
1465
1445
  });
@@ -1474,7 +1454,7 @@ test("standalone CR replacements remain visible in diffs and exact in patches",
1474
1454
  ["a\rb\n", "\r", "␍"],
1475
1455
  ]) {
1476
1456
  await writeFile(file, before);
1477
- const result = await call(makeReplaceTool(dir), {
1457
+ const result = await call(makeReplaceTool(dir, DEFAULT_CONFIG), {
1478
1458
  path: file,
1479
1459
  replacements: [{ find, replace: replacement, regex: true }],
1480
1460
  });
@@ -1493,14 +1473,17 @@ test("deletion successors and unique candidate rows display CR without altering
1493
1473
  const file = join(dir, "cr.txt");
1494
1474
  const before = "remove\na\rb\n";
1495
1475
  await writeFile(file, before);
1496
- const result = await call(makeEditOverride(dir), {
1476
+ const result = await call(makeEditOverride(dir, DEFAULT_CONFIG), {
1497
1477
  path: file,
1498
1478
  edits: [{ op: "delete", anchor: h(before, 1) }],
1499
1479
  });
1500
1480
  assert.ok(result.content[0].text.includes(`${h("a\rb\n", 1)}│a␍b`));
1501
1481
  assert.equal(await readFile(file, "utf8"), "a\rb\n");
1502
1482
  await assert.rejects(
1503
- call(makeEditOverride(dir), { path: file, edits: [{ op: "delete", anchor: h(before, 2) }] }),
1483
+ call(makeEditOverride(dir, DEFAULT_CONFIG), {
1484
+ path: file,
1485
+ edits: [{ op: "delete", anchor: h(before, 2) }],
1486
+ }),
1504
1487
  (error: Error) => {
1505
1488
  assert.ok(error.message.includes(`${h("a\rb\n", 1)}│a␍b`));
1506
1489
  assert.ok(!error.message.includes("\r"));
@@ -1513,14 +1496,14 @@ test("read bounds oversized selected lines while preserving truncation metadata
1513
1496
  withDir(async (dir) => {
1514
1497
  const long = "界".repeat(200_000);
1515
1498
  await writeFile(join(dir, "long.txt"), `first\r\n${long}\r\nlast`);
1516
- const read = makeReadOverride(dir);
1499
+ const read = makeReadOverride(dir, DEFAULT_CONFIG);
1517
1500
  const result = await call(read, { path: "long.txt" });
1518
1501
  assert.match(result.content[0].text, /1#[0-9A-Z]+│first/);
1519
1502
  assert.doesNotMatch(result.content[0].text, /2#[0-9A-Z]+│/);
1520
1503
  assert.equal(result.details.truncation.firstLineExceedsLimit, false);
1521
1504
  assert.equal(result.details.truncation.outputLines, 1);
1522
1505
  const expectedRows = ["first", long, "last"].map(
1523
- (text, index) => `${index + 1}#${computeLineHash(index + 1, text)}│${text}`,
1506
+ (text, index) => `${index + 1}#${computeLineHash(index + 1, text, 4)}│${text}`,
1524
1507
  );
1525
1508
  assert.equal(result.details.truncation.totalBytes, Buffer.byteLength(expectedRows.join("\n")));
1526
1509
  const oversized = await call(read, { path: "long.txt", offset: 2, limit: 1 });
@@ -1535,12 +1518,12 @@ test("read budgets visible standalone CR characters using rendered UTF-8 bytes",
1535
1518
  withDir(async (dir) => {
1536
1519
  const content = "\r".repeat(90_000);
1537
1520
  await writeFile(join(dir, "cr.txt"), content);
1538
- const result = await call(makeReadOverride(dir), { path: "cr.txt", limit: 1 });
1521
+ const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "cr.txt", limit: 1 });
1539
1522
  assert.equal(result.details.truncation.firstLineExceedsLimit, true);
1540
1523
  assert.equal(result.details.truncation.outputBytes, 0);
1541
1524
  assert.equal(
1542
1525
  result.details.truncation.totalBytes,
1543
- Buffer.byteLength(`1#${computeLineHash(1, content)}│${"␍".repeat(content.length)}`),
1526
+ Buffer.byteLength(`1#${computeLineHash(1, content, 4)}│${"␍".repeat(content.length)}`),
1544
1527
  );
1545
1528
  }));
1546
1529
 
@@ -1548,7 +1531,7 @@ test("edit tool rejects non-array formats Pi cannot convert", async () =>
1548
1531
  withDir(async (dir) => {
1549
1532
  const file = join(dir, "f.txt");
1550
1533
  await writeFile(file, "first\nsecond\n");
1551
- const edit = makeEditOverride(dir);
1534
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1552
1535
  const anchor = h("first\nsecond\n", 2);
1553
1536
  for (const alternate of [
1554
1537
  { path: "f.txt", edits: JSON.stringify([{ op: "replace", anchor, body: ["SECOND"] }]) },
@@ -1568,29 +1551,18 @@ test("edit names anchors whose hash length differs from the registered hashLen",
1568
1551
  const text = "first\nsecond\n";
1569
1552
  const file = join(dir, "f.txt");
1570
1553
  await writeFile(file, text);
1571
- const { hashLen } = getState().config;
1572
- const edit = makeEditOverride(dir);
1554
+ const { hashLen } = DEFAULT_CONFIG;
1555
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1573
1556
  const anchor = h(text, 1);
1574
1557
  const short = h(text, 2).slice(0, -2);
1575
1558
  const args = { path: "f.txt", edits: [{ op: "replace", anchor, end: short, body: ["x"] }] };
1576
1559
 
1577
- assert.throws(
1578
- () => edit.prepareArguments(args),
1560
+ await assert.rejects(
1561
+ call(edit, args),
1579
1562
  new RegExp(
1580
1563
  `^Error: Anchor hash length mismatch: edits\\[0\\]\\.end ${short} has ${hashLen - 2} hash characters, but hashLen is ${hashLen}\\.`,
1581
1564
  ),
1582
1565
  );
1583
- // The schema itself also admits only the registered length.
1584
- assert.throws(
1585
- () =>
1586
- validateToolArguments(edit as any, {
1587
- type: "toolCall",
1588
- id: "short-anchor",
1589
- name: "edit",
1590
- arguments: args as any,
1591
- }),
1592
- /Validation failed/,
1593
- );
1594
1566
  assert.equal(await readFile(file, "utf8"), text);
1595
1567
  }));
1596
1568
 
@@ -1599,7 +1571,7 @@ test("Pi-converted single-object edits execute as a canonical array", async () =
1599
1571
  const file = join(dir, "converted.txt");
1600
1572
  const original = "first\nsecond\n";
1601
1573
  await writeFile(file, original);
1602
- const edit = makeEditOverride(dir);
1574
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1603
1575
  const operation = { op: "replace", anchor: h(original, 2), body: ["SECOND"] };
1604
1576
  const args = validateToolArguments(edit as any, {
1605
1577
  type: "toolCall",
@@ -1616,7 +1588,7 @@ test("edit execute rejects legacy oldText/newText without op", async () =>
1616
1588
  withDir(async (dir) => {
1617
1589
  const file = join(dir, "f.txt");
1618
1590
  await writeFile(file, "first\nsecond\n");
1619
- const edit = makeEditOverride(dir);
1591
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1620
1592
  await assert.rejects(
1621
1593
  call(edit, {
1622
1594
  path: "f.txt",
@@ -1641,7 +1613,7 @@ test("unresolved snapshot rows support direct retry and revalidate after further
1641
1613
  const observed = "\uFEFFguard\r\ntarget = old\r\n中文 literal \\n\\0\r\n";
1642
1614
  const current = observed.replace("old", "pending");
1643
1615
  await writeFile(file, current);
1644
- const edit = makeEditOverride(dir);
1616
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1645
1617
  let diagnostic = "";
1646
1618
  await assert.rejects(
1647
1619
  call(edit, {
@@ -1679,7 +1651,7 @@ test("unresolved oversized and out-of-range rows require more context without pa
1679
1651
  const file = join(dir, "recover.txt");
1680
1652
  const current = "界".repeat(1400) + "\n";
1681
1653
  await writeFile(file, current);
1682
- const edit = makeEditOverride(dir);
1654
+ const edit = makeEditOverride(dir, DEFAULT_CONFIG);
1683
1655
  for (const anchor of [h("old\n", 1), "99#XXXX"]) {
1684
1656
  await assert.rejects(
1685
1657
  call(edit, { path: "recover.txt", edits: [{ op: "replace", anchor, body: ["new"] }] }),
@@ -1693,3 +1665,155 @@ test("unresolved oversized and out-of-range rows require more context without pa
1693
1665
  }
1694
1666
  });
1695
1667
  });
1668
+
1669
+ test("edit rejects impossible checksum characters before accessing the file", async () =>
1670
+ withDir(async (dir) => {
1671
+ const tool = makeEditOverride(dir, DEFAULT_CONFIG);
1672
+ for (const char of ["I", "L", "O", "U"]) {
1673
+ await assert.rejects(
1674
+ call(tool, {
1675
+ path: "missing.txt",
1676
+ edits: [{ op: "delete", anchor: `1#${char.repeat(DEFAULT_CONFIG.hashLen)}` }],
1677
+ }),
1678
+ /Validation failed/,
1679
+ );
1680
+ }
1681
+ }));
1682
+
1683
+ test("read renders native content without interpreting anchor-like prefixes", async () =>
1684
+ withDir(async (dir) => {
1685
+ const tool = makeReadOverride(dir, DEFAULT_CONFIG);
1686
+ for (const content of ["12#abc│ordinary content", "12#ABCD│ordinary content"]) {
1687
+ for (const suffix of ["\n", "\n\0tail"]) {
1688
+ await writeFile(join(dir, "prefix.txt"), content + suffix);
1689
+ const result = await call(tool, { path: "prefix.txt" });
1690
+ // Round-trip details as persisted session results do before rendering.
1691
+ const rendered = tool.renderResult!(
1692
+ JSON.parse(JSON.stringify(result)),
1693
+ { expanded: true, isPartial: false },
1694
+ stubTheme as Theme,
1695
+ { args: { path: "prefix.txt" }, state: {}, cwd: dir, isError: false } as any,
1696
+ )
1697
+ .render(120)
1698
+ .join("\n");
1699
+ assert.ok(rendered.includes(content), rendered);
1700
+ }
1701
+ }
1702
+ }));
1703
+
1704
+ test("read accepts safe offsets and limits whose sum exceeds the safe integer range", async () =>
1705
+ withDir(async (dir) => {
1706
+ await writeFile(join(dir, "range.txt"), "first\nsecond\nthird\n");
1707
+ const tool = makeReadOverride(dir, DEFAULT_CONFIG);
1708
+ for (const offset of [2, 3, Number.MAX_SAFE_INTEGER]) {
1709
+ const result = await call(tool, {
1710
+ path: "range.txt",
1711
+ offset,
1712
+ limit: Number.MAX_SAFE_INTEGER,
1713
+ });
1714
+ assert.equal(result.details, undefined);
1715
+ assert.doesNotMatch(result.content[0].text, /to continue|│first/);
1716
+ if (offset <= 3) assert.match(result.content[0].text, /│third/);
1717
+ else assert.doesNotMatch(result.content[0].text, /│/);
1718
+ }
1719
+ }));
1720
+
1721
+ test("ambiguous failure lists and neighborhoods select the same first eight candidates", async () =>
1722
+ withDir(async (dir) => {
1723
+ const positions = [4, 14, 24, 34, 44, 54, 64, 74, 84];
1724
+ const lines = Array.from({ length: 90 }, (_, index) => `line-${index + 1}`);
1725
+ for (const position of positions) lines[position - 1] = "target";
1726
+ const text = lines.join("\n");
1727
+ await writeFile(join(dir, "candidates.txt"), text);
1728
+ const tool = makeEditOverride(dir, DEFAULT_CONFIG);
1729
+ await assert.rejects(
1730
+ call(tool, {
1731
+ path: "candidates.txt",
1732
+ edits: [{ op: "delete", anchor: `200#${computeLineHash(200, "target", 4)}` }],
1733
+ }),
1734
+ (error: Error) => {
1735
+ const list = error.message
1736
+ .split("\n")
1737
+ .find((line) => line.includes("ambiguous checksum matches:"));
1738
+ assert.ok(list);
1739
+ assert.match(list, /1 more candidates omitted/);
1740
+ for (const position of positions.slice(0, 8)) {
1741
+ const anchor = h(text, position);
1742
+ assert.ok(list.includes(`"${anchor}"`));
1743
+ assert.ok(error.message.includes(`${anchor}│target`));
1744
+ }
1745
+ assert.ok(!error.message.includes(h(text, 84)));
1746
+ return true;
1747
+ },
1748
+ );
1749
+ assert.equal(await readFile(join(dir, "candidates.txt"), "utf8"), text);
1750
+ }));
1751
+
1752
+ test("loaded configuration controls hash length and recovery radius", async () =>
1753
+ withDir(async (dir) => {
1754
+ const text = "changed\ntarget\npadding\ntarget\n";
1755
+ await mkdir(join(dir, ".pi"));
1756
+ await writeFile(join(dir, "configured.txt"), text);
1757
+ for (const [hashLen, shiftRadius, expected] of [
1758
+ [6, 0, /no checksum-matching candidate found/],
1759
+ [6, 1, /checksum-matching candidate 2#/],
1760
+ [8, 3, /ambiguous checksum matches/],
1761
+ ] as const) {
1762
+ await writeFile(
1763
+ join(dir, ".pi", "settings.json"),
1764
+ JSON.stringify({ hashlineEdit: { hashLen, shiftRadius } }),
1765
+ );
1766
+ const config = loadConfig(dir);
1767
+ const read = await call(makeReadOverride(dir, config), { path: "configured.txt" });
1768
+ assert.equal(anchorLine(read.content[0].text, 1).split("#")[1].length, hashLen);
1769
+ await assert.rejects(
1770
+ call(makeEditOverride(dir, config), {
1771
+ path: "configured.txt",
1772
+ edits: [{ op: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
1773
+ }),
1774
+ expected,
1775
+ );
1776
+ assert.equal(await readFile(join(dir, "configured.txt"), "utf8"), text);
1777
+ }
1778
+ }));
1779
+
1780
+ test("configured read defaults bound omitted limits and returned bytes", async () =>
1781
+ withDir(async (dir) => {
1782
+ const read = makeReadOverride(dir, {
1783
+ ...DEFAULT_CONFIG,
1784
+ read: { defaultLimit: 2, maxKiB: 1 },
1785
+ });
1786
+ const description: unknown = Reflect.get(read.parameters.properties.limit, "description");
1787
+ assert.ok(typeof description === "string");
1788
+ assert.match(description, /default 2\)/);
1789
+ await writeFile(join(dir, "short.txt"), "a\nb\nc\n");
1790
+ const paged = await call(read, { path: "short.txt" });
1791
+ assert.match(paged.content[0].text, /showing lines 1-2 of 3; use offset 3 to continue/);
1792
+ assert.match(paged.content[0].text, /^2#[0-9A-Z]+│b$/m);
1793
+ assert.doesNotMatch(paged.content[0].text, /^3#/m);
1794
+ const explicit = await call(read, { path: "short.txt", limit: 3 });
1795
+ assert.match(explicit.content[0].text, /^3#[0-9A-Z]+│c$/m);
1796
+ await writeFile(join(dir, "wide.txt"), `${"x".repeat(2048)}\n`);
1797
+ const wide = await call(read, { path: "wide.txt" });
1798
+ assert.match(wide.content[0].text, /line 1 exceeds 1 KiB/);
1799
+ assert.equal(wide.details.truncation.maxBytes, 1024);
1800
+ assert.equal(wide.details.truncation.outputBytes, 0);
1801
+ }));
1802
+
1803
+ test("edit verification and returned anchors use the supplied hash length", async () =>
1804
+ withDir(async (dir) => {
1805
+ const edit = makeEditOverride(dir, { ...DEFAULT_CONFIG, hashLen: 6 });
1806
+ await writeFile(join(dir, "registered.txt"), "before\n");
1807
+ const result = await call(edit, {
1808
+ path: "registered.txt",
1809
+ edits: [
1810
+ {
1811
+ op: "replace",
1812
+ anchor: `1#${computeLineHash(1, "before", 6)}`,
1813
+ body: ["after"],
1814
+ },
1815
+ ],
1816
+ });
1817
+ assert.equal(anchorLine(result.content[0].text, 1), `1#${computeLineHash(1, "after", 6)}`);
1818
+ assert.equal(await readFile(join(dir, "registered.txt"), "utf8"), "after\n");
1819
+ }));