@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.
Files changed (66) hide show
  1. package/README.md +134 -30
  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.test.ts +30 -1
  10. package/src/core/text.ts +9 -6
  11. package/src/core/types.ts +4 -7
  12. package/src/index.ts +9 -11
  13. package/src/integration/config-registration.test.ts +206 -0
  14. package/src/integration/grep-rg.test.ts +285 -153
  15. package/src/integration/large-text.test.ts +11 -12
  16. package/src/integration/replace-package.test.ts +11 -7
  17. package/src/pi/action-fusion.test.ts +251 -39
  18. package/src/pi/action-fusion.ts +113 -117
  19. package/src/pi/anchor-format.test.ts +91 -24
  20. package/src/pi/anchor-format.ts +43 -9
  21. package/src/pi/budgets.ts +7 -2
  22. package/src/pi/config.test.ts +153 -40
  23. package/src/pi/config.ts +87 -41
  24. package/src/pi/diagnostic-buffer.test.ts +58 -0
  25. package/src/pi/diagnostic-buffer.ts +59 -0
  26. package/src/pi/e2e-scenarios.test.ts +13 -11
  27. package/src/pi/edit-tool.ts +21 -157
  28. package/src/pi/error-text.ts +10 -5
  29. package/src/pi/execute.test.ts +299 -175
  30. package/src/pi/failure-context.test.ts +7 -2
  31. package/src/pi/failure-context.ts +155 -8
  32. package/src/pi/failure-path.test.ts +33 -26
  33. package/src/pi/file-commit.ts +2 -1
  34. package/src/pi/fusion-card.test.ts +26 -0
  35. package/src/pi/fusion-card.ts +102 -6
  36. package/src/pi/grep-output.ts +50 -35
  37. package/src/pi/grep-render.ts +13 -15
  38. package/src/pi/grep-scope.ts +8 -46
  39. package/src/pi/grep-search.ts +90 -63
  40. package/src/pi/grep-tool.ts +109 -121
  41. package/src/pi/grep.test.ts +597 -377
  42. package/src/pi/mutation-outcome.testing.ts +22 -0
  43. package/src/pi/mutation-result.test.ts +51 -2
  44. package/src/pi/mutation-result.ts +62 -55
  45. package/src/pi/mutation-runner.ts +16 -33
  46. package/src/pi/pi.test.ts +11 -6
  47. package/src/pi/read-tool.ts +62 -34
  48. package/src/pi/registration.test.ts +7 -7
  49. package/src/pi/render.test.ts +143 -9
  50. package/src/pi/render.ts +32 -96
  51. package/src/pi/replace-regex.ts +54 -0
  52. package/src/pi/replace-tool.ts +25 -59
  53. package/src/pi/replace-worker.ts +2 -2
  54. package/src/pi/replace.test.ts +62 -58
  55. package/src/pi/result-safety.test.ts +124 -84
  56. package/src/pi/rg-line-ranges.ts +3 -2
  57. package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
  58. package/src/pi/{rg-line-filter.ts → rg-process.ts} +43 -9
  59. package/src/pi/rg-text-view.ts +25 -38
  60. package/src/pi/schema.ts +2 -10
  61. package/src/pi/text-stream.test.ts +153 -0
  62. package/src/pi/text-stream.ts +33 -13
  63. package/src/pi/write-tool.ts +3 -2
  64. package/src/pi/write.test.ts +10 -9
  65. package/src/core/index.ts +0 -14
  66. package/src/pi/state.ts +0 -26
@@ -7,14 +7,14 @@ import type { Anchor, Edit } from "./types.ts";
7
7
 
8
8
  /** Live anchor: hash the current content at the given line (1-based). */
9
9
  function at(text: string, line: number): Anchor {
10
- return { line, hash: computeLineHash(line, splitLines(text)[line - 1]) };
10
+ return { line, hash: computeLineHash(line, splitLines(text)[line - 1], 4) };
11
11
  }
12
12
 
13
13
  // --- happy paths ---
14
14
 
15
15
  test("replace single line", () => {
16
16
  const text = "a\nb\nc\n";
17
- const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }]);
17
+ const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }], 4, 15);
18
18
  assert.equal(r.ok, true);
19
19
  if (r.ok) assert.equal(r.text, "a\nB\nc\n");
20
20
  });
@@ -22,7 +22,7 @@ test("replace single line", () => {
22
22
  test("editing another line preserves standalone carriage returns at EOF", () => {
23
23
  for (const ending of ["\n", "\r\n"]) {
24
24
  const text = `first${ending}last\r`;
25
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["changed"] }]);
25
+ const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["changed"] }], 4, 15);
26
26
  assert.equal(r.ok, true);
27
27
  if (r.ok) assert.equal(r.text, `changed${ending}last\r`);
28
28
  }
@@ -30,60 +30,78 @@ test("editing another line preserves standalone carriage returns at EOF", () =>
30
30
 
31
31
  test("replace range", () => {
32
32
  const text = "a\nb\nc\nd\ne\n";
33
- const r = applyEdits(text, [
34
- { op: "replace", start: at(text, 2), end: at(text, 4), body: ["X", "Y"] },
35
- ]);
33
+ const r = applyEdits(
34
+ text,
35
+ [{ op: "replace", start: at(text, 2), end: at(text, 4), body: ["X", "Y"] }],
36
+ 4,
37
+ 15,
38
+ );
36
39
  assert.equal(r.ok, true);
37
40
  if (r.ok) assert.equal(r.text, "a\nX\nY\ne\n");
38
41
  });
39
42
 
40
43
  test("delete single line / range", () => {
41
44
  const text = "a\nb\nc\nd\n";
42
- const r = applyEdits(text, [{ op: "delete", start: at(text, 2), end: at(text, 3) }]);
45
+ const r = applyEdits(text, [{ op: "delete", start: at(text, 2), end: at(text, 3) }], 4, 15);
43
46
  assert.equal(r.ok, true);
44
47
  if (r.ok) assert.equal(r.text, "a\nd\n");
45
48
  });
46
49
 
47
50
  test("insert_after", () => {
48
51
  const text = "a\nb\n";
49
- const r = applyEdits(text, [{ op: "insert_after", anchor: at(text, 1), body: ["x"] }]);
52
+ const r = applyEdits(text, [{ op: "insert_after", anchor: at(text, 1), body: ["x"] }], 4, 15);
50
53
  assert.equal(r.ok, true);
51
54
  if (r.ok) assert.equal(r.text, "a\nx\nb\n");
52
55
  });
53
56
 
54
57
  test("insert_before", () => {
55
58
  const text = "a\nb\n";
56
- const r = applyEdits(text, [{ op: "insert_before", anchor: at(text, 2), body: ["x"] }]);
59
+ const r = applyEdits(text, [{ op: "insert_before", anchor: at(text, 2), body: ["x"] }], 4, 15);
57
60
  assert.equal(r.ok, true);
58
61
  if (r.ok) assert.equal(r.text, "a\nx\nb\n");
59
62
  });
60
63
 
61
64
  test("append / prepend", () => {
62
65
  const text = "a\nb\n";
63
- const r = applyEdits(text, [
64
- { op: "prepend", body: ["head"] },
65
- { op: "append", body: ["tail"] },
66
- ]);
66
+ const r = applyEdits(
67
+ text,
68
+ [
69
+ { op: "prepend", body: ["head"] },
70
+ { op: "append", body: ["tail"] },
71
+ ],
72
+ 4,
73
+ 15,
74
+ );
67
75
  assert.equal(r.ok, true);
68
76
  if (r.ok) assert.equal(r.text, "head\na\nb\ntail\n");
69
77
  });
70
78
 
71
79
  test("multiple out-of-order ops → applied at the right positions", () => {
72
80
  const text = "a\nb\nc\n";
73
- const r = applyEdits(text, [
74
- { op: "insert_after", anchor: at(text, 3), body: ["z"] },
75
- { op: "replace", start: at(text, 1), body: ["A"] },
76
- ]);
81
+ const r = applyEdits(
82
+ text,
83
+ [
84
+ { op: "insert_after", anchor: at(text, 3), body: ["z"] },
85
+ { op: "replace", start: at(text, 1), body: ["A"] },
86
+ ],
87
+ 4,
88
+ 15,
89
+ );
77
90
  assert.equal(r.ok, true);
78
91
  if (r.ok) assert.equal(r.text, "A\nb\nc\nz\n");
79
92
  });
80
93
 
81
94
  test("touchedLines covers exactly the lines this edit produced (new-file indices)", () => {
82
95
  const text = "a\nb\nc\n";
83
- const r = applyEdits(text, [
84
- { op: "insert_after", anchor: at(text, 3), body: ["z"] },
85
- { op: "replace", start: at(text, 1), body: ["A"] },
86
- ]);
96
+ const r = applyEdits(
97
+ text,
98
+ [
99
+ { op: "insert_after", anchor: at(text, 3), body: ["z"] },
100
+ { op: "replace", start: at(text, 1), body: ["A"] },
101
+ ],
102
+ 4,
103
+ 15,
104
+ );
87
105
  assert.equal(r.ok, true);
88
106
  if (r.ok) {
89
107
  // new file: [A, b, c, z]; this edit produced line index 0 (A) and 3 (z)
@@ -93,14 +111,14 @@ test("touchedLines covers exactly the lines this edit produced (new-file indices
93
111
 
94
112
  test("touchedLines for append", () => {
95
113
  const text = "a\n";
96
- const r = applyEdits(text, [{ op: "append", body: ["b", "c"] }]);
114
+ const r = applyEdits(text, [{ op: "append", body: ["b", "c"] }], 4, 15);
97
115
  assert.equal(r.ok, true);
98
116
  if (r.ok) assert.deepEqual([...r.touchedLines], [1, 2]);
99
117
  });
100
118
 
101
119
  test("touchedLines for delete re-anchors the line that shifted into the gap", () => {
102
120
  const text = "a\nb\nc\nd\n";
103
- const r = applyEdits(text, [{ op: "delete", start: at(text, 2) }]);
121
+ const r = applyEdits(text, [{ op: "delete", start: at(text, 2) }], 4, 15);
104
122
  assert.equal(r.ok, true);
105
123
  if (r.ok) {
106
124
  // new file: [a, c, d]; deleting line 2 (b) shifts c into index 1
@@ -112,16 +130,24 @@ test("touchedLines for delete re-anchors the line that shifted into the gap", ()
112
130
 
113
131
  test("anchor hash mismatch rejected (line changed)", () => {
114
132
  const text = "a\nb\n";
115
- const r = applyEdits(text, [{ op: "replace", start: { line: 1, hash: "WRONG" }, body: ["x"] }]);
133
+ const r = applyEdits(
134
+ text,
135
+ [{ op: "replace", start: { line: 1, hash: "WRONG" }, body: ["x"] }],
136
+ 4,
137
+ 15,
138
+ );
116
139
  assert.equal(r.ok, false);
117
140
  if (!r.ok) assert.equal(r.failure.kind, "anchor");
118
141
  });
119
142
 
120
143
  test("line out of range rejected", () => {
121
144
  const text = "a\n";
122
- const r = applyEdits(text, [
123
- { op: "replace", start: { line: 5, hash: computeLineHash(1, "a") }, body: ["x"] },
124
- ]);
145
+ const r = applyEdits(
146
+ text,
147
+ [{ op: "replace", start: { line: 5, hash: computeLineHash(1, "a", 4) }, body: ["x"] }],
148
+ 4,
149
+ 15,
150
+ );
125
151
  assert.equal(r.ok, false);
126
152
  if (!r.ok) assert.equal(r.failure.kind, "anchor");
127
153
  });
@@ -129,38 +155,54 @@ test("line out of range rejected", () => {
129
155
  test("anchor mismatch when the cited line's content differs (live verification)", () => {
130
156
  // hash for line 2's content, but cited as line 1 → must fail because line 1's live content differs
131
157
  const text = "a\nb\n";
132
- const r = applyEdits(text, [
133
- { op: "replace", start: { line: 1, hash: computeLineHash(2, "b") }, body: ["x"] },
134
- ]);
158
+ const r = applyEdits(
159
+ text,
160
+ [{ op: "replace", start: { line: 1, hash: computeLineHash(2, "b", 4) }, body: ["x"] }],
161
+ 4,
162
+ 15,
163
+ );
135
164
  assert.equal(r.ok, false);
136
165
  if (!r.ok) assert.equal(r.failure.kind, "anchor");
137
166
  });
138
167
 
139
168
  test("reverse-order range rejected", () => {
140
169
  const text = "a\nb\nc\n";
141
- const r = applyEdits(text, [
142
- { op: "replace", start: at(text, 3), end: at(text, 1), body: ["x"] },
143
- ]);
170
+ const r = applyEdits(
171
+ text,
172
+ [{ op: "replace", start: at(text, 3), end: at(text, 1), body: ["x"] }],
173
+ 4,
174
+ 15,
175
+ );
144
176
  assert.equal(r.ok, false);
145
177
  if (!r.ok) assert.equal(r.failure.kind, "range");
146
178
  });
147
179
 
148
180
  test("overlapping edits rejected", () => {
149
181
  const text = "a\nb\nc\nd\n";
150
- const r = applyEdits(text, [
151
- { op: "replace", start: at(text, 2), end: at(text, 3), body: ["x"] },
152
- { op: "replace", start: at(text, 3), body: ["y"] },
153
- ]);
182
+ const r = applyEdits(
183
+ text,
184
+ [
185
+ { op: "replace", start: at(text, 2), end: at(text, 3), body: ["x"] },
186
+ { op: "replace", start: at(text, 3), body: ["y"] },
187
+ ],
188
+ 4,
189
+ 15,
190
+ );
154
191
  assert.equal(r.ok, false);
155
192
  if (!r.ok) assert.equal(r.failure.kind, "range");
156
193
  });
157
194
 
158
195
  test("conflict at the same insertion point rejected", () => {
159
196
  const text = "a\nb\n";
160
- const r = applyEdits(text, [
161
- { op: "insert_after", anchor: at(text, 1), body: ["x"] },
162
- { op: "insert_after", anchor: at(text, 1), body: ["y"] },
163
- ]);
197
+ const r = applyEdits(
198
+ text,
199
+ [
200
+ { op: "insert_after", anchor: at(text, 1), body: ["x"] },
201
+ { op: "insert_after", anchor: at(text, 1), body: ["y"] },
202
+ ],
203
+ 4,
204
+ 15,
205
+ );
164
206
  assert.equal(r.ok, false);
165
207
  if (!r.ok) assert.equal(r.failure.kind, "range");
166
208
  });
@@ -168,9 +210,14 @@ test("conflict at the same insertion point rejected", () => {
168
210
  test("byte-identical edits succeed without updated anchors after validation", () => {
169
211
  for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
170
212
  const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
171
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: [body] }]);
213
+ const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: [body] }], 4, 15);
172
214
  assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
173
- const invalid = applyEdits(text, [{ op: "replace", start: at("wrong\nb\n", 1), body: [body] }]);
215
+ const invalid = applyEdits(
216
+ text,
217
+ [{ op: "replace", start: at("wrong\nb\n", 1), body: [body] }],
218
+ 4,
219
+ 15,
220
+ );
174
221
  assert.ok(!invalid.ok && invalid.failure.kind === "anchor");
175
222
  }
176
223
  });
@@ -179,40 +226,58 @@ test("unrelated change elsewhere does NOT block the edit (no global stale check)
179
226
  // read-time text and current text differ at line 3, but we only touch line 1 — must succeed
180
227
  const readText = "a\nb\nc\n";
181
228
  const currentText = "a\nb\nCHANGED\n";
182
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 1), body: ["A"] }]);
229
+ const r = applyEdits(
230
+ currentText,
231
+ [{ op: "replace", start: at(readText, 1), body: ["A"] }],
232
+ 4,
233
+ 15,
234
+ );
183
235
  assert.equal(r.ok, true);
184
236
  if (r.ok) assert.equal(r.text, "A\nb\nCHANGED\n");
185
237
  });
186
238
 
187
239
  test("CRLF line endings preserved", () => {
188
240
  const text = "a\r\nb\r\n";
189
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }]);
241
+ const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
190
242
  assert.equal(r.ok, true);
191
243
  if (r.ok) assert.equal(r.text, "A\r\nb\r\n");
192
244
  });
193
245
 
194
246
  test("editing a mixed-ending file preserves untouched separators", () => {
195
247
  const before = "first\r\nsecond\nthird\r\n";
196
- const result = applyEdits(before, [{ op: "replace", start: at(before, 2), body: ["SECOND"] }]);
248
+ const result = applyEdits(
249
+ before,
250
+ [{ op: "replace", start: at(before, 2), body: ["SECOND"] }],
251
+ 4,
252
+ 15,
253
+ );
197
254
  assert.equal(result.ok, true);
198
255
  if (result.ok) assert.equal(result.text, "first\r\nSECOND\nthird\r\n");
199
256
  });
200
257
 
201
258
  test("inserting and deleting in mixed-ending files preserves surviving separators", () => {
202
259
  const before = "a\r\nb\nc\r\nlast";
203
- const inserted = applyEdits(before, [{ op: "insert_after", anchor: at(before, 2), body: ["x"] }]);
260
+ const inserted = applyEdits(
261
+ before,
262
+ [{ op: "insert_after", anchor: at(before, 2), body: ["x"] }],
263
+ 4,
264
+ 15,
265
+ );
204
266
  assert.equal(inserted.ok, true);
205
267
  if (inserted.ok) assert.equal(inserted.text, "a\r\nb\nx\r\nc\r\nlast");
206
- const deleted = applyEdits(before, [{ op: "delete", start: at(before, 2) }]);
268
+ const deleted = applyEdits(before, [{ op: "delete", start: at(before, 2) }], 4, 15);
207
269
  assert.equal(deleted.ok, true);
208
270
  if (deleted.ok) assert.equal(deleted.text, "a\r\nc\r\nlast");
209
271
  });
210
272
 
211
273
  test("replacing one line with several keeps the block's trailing gap", () => {
212
274
  const before = "a\r\nb\nc\r\nd";
213
- const result = applyEdits(before, [
214
- { op: "replace", start: at(before, 2), body: ["X", "Y", "Z"] },
215
- ]);
275
+ const result = applyEdits(
276
+ before,
277
+ [{ op: "replace", start: at(before, 2), body: ["X", "Y", "Z"] }],
278
+ 4,
279
+ 15,
280
+ );
216
281
  assert.equal(result.ok, true);
217
282
  // b's gap (\n) moves to the last new line, so the boundary to c is unchanged;
218
283
  // b has no internal gap; both new internal gaps use the file style, like replace("b", "X\nY\nZ").
@@ -221,28 +286,28 @@ test("replacing one line with several keeps the block's trailing gap", () => {
221
286
 
222
287
  test("a file without a final newline keeps not having one", () => {
223
288
  const text = "a\nb";
224
- const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }]);
289
+ const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }], 4, 15);
225
290
  assert.equal(r.ok, true);
226
291
  if (r.ok) assert.equal(r.text, "a\nB");
227
292
  });
228
293
 
229
294
  test("CRLF without a final newline keeps not having one", () => {
230
295
  const text = "a\r\nb";
231
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }]);
296
+ const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
232
297
  assert.equal(r.ok, true);
233
298
  if (r.ok) assert.equal(r.text, "A\r\nb");
234
299
  });
235
300
 
236
301
  test("appending to a file without a final newline keeps it absent", () => {
237
302
  const text = "a";
238
- const r = applyEdits(text, [{ op: "append", body: ["b"] }]);
303
+ const r = applyEdits(text, [{ op: "append", body: ["b"] }], 4, 15);
239
304
  assert.equal(r.ok, true);
240
305
  if (r.ok) assert.equal(r.text, "a\nb");
241
306
  });
242
307
 
243
308
  test("noop is still detected when the file lacks a final newline", () => {
244
309
  const text = "a\nb";
245
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["a"] }]);
310
+ const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["a"] }], 4, 15);
246
311
  assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
247
312
  });
248
313
 
@@ -251,28 +316,41 @@ test("noop is still detected when the file lacks a final newline", () => {
251
316
  test("shifted recovery: content moved down → found with a fresh anchor", () => {
252
317
  const readText = "a\nb\nc\nd\ne\n";
253
318
  const currentText = "a\nX\nb\nc\nd\ne\n"; // inserted X after line 1 → "c" moved 3→4
254
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }]);
319
+ const r = applyEdits(
320
+ currentText,
321
+ [{ op: "replace", start: at(readText, 3), body: ["C"] }],
322
+ 4,
323
+ 15,
324
+ );
255
325
  assert.ok(!r.ok && r.failure.kind === "anchor");
256
326
  const f = r.failure.failures[0];
257
327
  assert.ok(f.recovery.kind === "found");
258
328
  assert.equal(f.recovery.newLine, 4);
259
329
  assert.equal(f.recovery.scope, "local");
260
330
  // The recovered anchor must verify against the current file.
261
- assert.equal(computeLineHash(4, splitLines(currentText)[3]), f.recovery.newHash);
331
+ assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
262
332
  });
263
333
 
264
334
  test("rescued anchor lets the retry succeed without a re-read", () => {
265
335
  const readText = "a\nb\nc\nd\ne\n";
266
336
  const currentText = "a\nX\nb\nc\nd\ne\n";
267
337
  // first attempt with a stale anchor → rescued
268
- const r1 = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }]);
338
+ const r1 = applyEdits(
339
+ currentText,
340
+ [{ op: "replace", start: at(readText, 3), body: ["C"] }],
341
+ 4,
342
+ 15,
343
+ );
269
344
  assert.ok(!r1.ok && r1.failure.kind === "anchor");
270
345
  const f = r1.failure.failures[0];
271
346
  assert.ok(f.recovery.kind === "found");
272
347
  // Retry with the recovered anchor; the rest of the file stays unchanged.
273
- const r2 = applyEdits(currentText, [
274
- { op: "replace", start: { line: f.recovery.newLine, hash: f.recovery.newHash }, body: ["C"] },
275
- ]);
348
+ const r2 = applyEdits(
349
+ currentText,
350
+ [{ op: "replace", start: { line: f.recovery.newLine, hash: f.recovery.newHash }, body: ["C"] }],
351
+ 4,
352
+ 15,
353
+ );
276
354
  assert.ok(r2.ok);
277
355
  assert.equal(r2.text, "a\nX\nb\nC\nd\ne\n");
278
356
  });
@@ -280,7 +358,12 @@ test("rescued anchor lets the retry succeed without a re-read", () => {
280
358
  test("shifted recovery: duplicate content → ambiguous candidates", () => {
281
359
  const readText = "a\nx\nb\nx\nc\n";
282
360
  const currentText = "a\nY\nx\nb\nx\nc\n"; // both "x" shifted
283
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 2), body: ["Z"] }]);
361
+ const r = applyEdits(
362
+ currentText,
363
+ [{ op: "replace", start: at(readText, 2), body: ["Z"] }],
364
+ 4,
365
+ 15,
366
+ );
284
367
  assert.ok(!r.ok && r.failure.kind === "anchor");
285
368
  const f = r.failure.failures[0];
286
369
  assert.ok(f.recovery.kind === "ambiguous");
@@ -294,7 +377,12 @@ test("shifted recovery: duplicate content → ambiguous candidates", () => {
294
377
  test("shifted recovery: content genuinely changed → none, with live content", () => {
295
378
  const readText = "a\nb\nc\n";
296
379
  const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
297
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 2), body: ["B"] }]);
380
+ const r = applyEdits(
381
+ currentText,
382
+ [{ op: "replace", start: at(readText, 2), body: ["B"] }],
383
+ 4,
384
+ 15,
385
+ );
298
386
  assert.ok(!r.ok && r.failure.kind === "anchor");
299
387
  const f = r.failure.failures[0];
300
388
  assert.equal(f.recovery.kind, "none");
@@ -309,15 +397,18 @@ test("full-file recovery finds distant content and anchors beyond the current EO
309
397
  ]) {
310
398
  const lines = Array.from({ length: 100 }, () => "filler");
311
399
  lines[newLine - 1] = "target";
312
- const result = applyEdits(lines.join("\n"), [
313
- { op: "delete", start: { line: oldLine, hash: computeLineHash(oldLine, "target") } },
314
- ]);
400
+ const result = applyEdits(
401
+ lines.join("\n"),
402
+ [{ op: "delete", start: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
403
+ 4,
404
+ 15,
405
+ );
315
406
  assert.ok(!result.ok && result.failure.kind === "anchor");
316
407
  assert.deepEqual(result.failure.failures[0].recovery, {
317
408
  kind: "found",
318
409
  scope: "full-file",
319
410
  newLine,
320
- newHash: computeLineHash(newLine, "target"),
411
+ newHash: computeLineHash(newLine, "target", 4),
321
412
  });
322
413
  }
323
414
  });
@@ -330,9 +421,12 @@ test("full-file recovery collects both sides while preserving local candidate pr
330
421
  ]) {
331
422
  const lines = Array.from({ length: 100 }, () => "filler");
332
423
  for (const line of positions) lines[line - 1] = "target";
333
- const result = applyEdits(lines.join("\n"), [
334
- { op: "delete", start: { line: 50, hash: computeLineHash(50, "target") } },
335
- ]);
424
+ const result = applyEdits(
425
+ lines.join("\n"),
426
+ [{ op: "delete", start: { line: 50, hash: computeLineHash(50, "target", 4) } }],
427
+ 4,
428
+ 15,
429
+ );
336
430
  assert.ok(!result.ok && result.failure.kind === "anchor");
337
431
  assert.deepEqual(
338
432
  result.failure.failures[0].recovery,
@@ -341,12 +435,15 @@ test("full-file recovery collects both sides while preserving local candidate pr
341
435
  kind: "found",
342
436
  scope,
343
437
  newLine: selected[0],
344
- newHash: computeLineHash(selected[0], "target"),
438
+ newHash: computeLineHash(selected[0], "target", 4),
345
439
  }
346
440
  : {
347
441
  kind: "ambiguous",
348
442
  scope,
349
- candidates: selected.map((line) => ({ line, hash: computeLineHash(line, "target") })),
443
+ candidates: selected.map((line) => ({
444
+ line,
445
+ hash: computeLineHash(line, "target", 4),
446
+ })),
350
447
  },
351
448
  );
352
449
  }
@@ -355,10 +452,15 @@ test("full-file recovery collects both sides while preserving local candidate pr
355
452
  test("collect-all: two stale anchors in one batch → both failures returned", () => {
356
453
  const readText = "a\nb\nc\nd\n";
357
454
  const currentText = "X\na\nb\nc\nd\n"; // inserted X at top → all shifted +1
358
- const r = applyEdits(currentText, [
359
- { op: "replace", start: at(readText, 2), body: ["B"] },
360
- { op: "replace", start: at(readText, 4), body: ["D"] },
361
- ]);
455
+ const r = applyEdits(
456
+ currentText,
457
+ [
458
+ { op: "replace", start: at(readText, 2), body: ["B"] },
459
+ { op: "replace", start: at(readText, 4), body: ["D"] },
460
+ ],
461
+ 4,
462
+ 15,
463
+ );
362
464
  assert.ok(!r.ok && r.failure.kind === "anchor");
363
465
  assert.equal(r.failure.failures.length, 2);
364
466
  const found = r.failure.failures.map((f) =>
@@ -375,20 +477,6 @@ test("shiftRadius=0 disables rescue (always none)", () => {
375
477
  assert.equal(r.failure.failures[0].recovery.kind, "none");
376
478
  });
377
479
 
378
- test("rejects CR or LF embedded in body elements", () => {
379
- for (const body of [["x\ny"], ["x\ry"]]) {
380
- const result = applyEdits("a\n", [{ op: "append", body }]);
381
- assert.deepEqual(result, {
382
- ok: false,
383
- failure: {
384
- kind: "input",
385
- message: "INVALID_BODY: each body element must contain exactly one logical line.",
386
- checks: [],
387
- },
388
- });
389
- }
390
- });
391
-
392
480
  test("BOM stays at byte zero through first-line edits while anchors retain their original hashes", () => {
393
481
  for (const text of [
394
482
  "\uFEFFfirst\nsecond\n",
@@ -413,20 +501,28 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
413
501
  [{ op: "delete", start: at(text, 1), end: at(text, 2) }, "\uFEFF"],
414
502
  ];
415
503
  for (const [edit, expected] of cases) {
416
- const result = applyEdits(text, [edit]);
504
+ const result = applyEdits(text, [edit], 4, 15);
417
505
  assert.ok(result.ok);
418
506
  assert.equal(result.text, expected, `${edit.op}: ${JSON.stringify(text)}`);
419
507
  }
420
508
  }
421
509
  for (const text of ["\uFEFF", "\uFEFFfirst"]) {
422
- const result = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["changed"] }]);
510
+ const result = applyEdits(
511
+ text,
512
+ [{ op: "replace", start: at(text, 1), body: ["changed"] }],
513
+ 4,
514
+ 15,
515
+ );
423
516
  assert.ok(result.ok);
424
517
  assert.equal(result.text, "\uFEFFchanged");
425
518
  }
426
519
  const embedded = "\uFEFFfirst\ninside\uFEFFcontent\n";
427
- const result = applyEdits(embedded, [
428
- { op: "replace", start: at(embedded, 1), body: ["changed"] },
429
- ]);
520
+ const result = applyEdits(
521
+ embedded,
522
+ [{ op: "replace", start: at(embedded, 1), body: ["changed"] }],
523
+ 4,
524
+ 15,
525
+ );
430
526
  assert.ok(result.ok);
431
527
  assert.equal(result.text, "\uFEFFchanged\ninside\uFEFFcontent\n");
432
528
  });
@@ -442,7 +538,7 @@ test("failed batches report every supplied anchor in input order from one snapsh
442
538
  { op: "append", body: ["last"] },
443
539
  { op: "prepend", body: ["first"] },
444
540
  ];
445
- const result = applyEdits(text, edits);
541
+ const result = applyEdits(text, edits, 4, 15);
446
542
  assert.ok(!result.ok && result.failure.kind === "anchor");
447
543
  assert.deepEqual(result.failure.checks, [
448
544
  { opIndex: 0, op: "replace", which: "anchor", cited: at(text, 1), status: "matched" },
@@ -452,12 +548,6 @@ test("failed batches report every supplied anchor in input order from one snapsh
452
548
  { opIndex: 2, op: "insert_after", which: "anchor", cited: at(text, 5), status: "matched" },
453
549
  { opIndex: 3, op: "insert_before", which: "anchor", cited: stale, status: "mismatched" },
454
550
  ]);
455
- const invalid = applyEdits(text, [...edits, { op: "append", body: ["bad\nline"] }]);
456
- assert.ok(!invalid.ok && invalid.failure.kind === "input");
457
- assert.deepEqual(
458
- invalid.failure.checks,
459
- result.failure.checks.map((check) => ({ ...check, status: "not_checked" })),
460
- );
461
551
  });
462
552
 
463
553
  test("matched anchor checks do not imply valid ranges", () => {
@@ -474,7 +564,7 @@ test("matched anchor checks do not imply valid ranges", () => {
474
564
  },
475
565
  ];
476
566
  for (const { edits, kind, checks } of cases) {
477
- const result = applyEdits(text, edits);
567
+ const result = applyEdits(text, edits, 4, 15);
478
568
  assert.ok(!result.ok);
479
569
  assert.equal(result.failure.kind, kind);
480
570
  assert.equal(result.failure.checks.length, checks);
@@ -489,7 +579,7 @@ test("zero-length replacement without line deletion does not produce false delet
489
579
  { op: "replace", start: at(text, 1), body: ["A"] },
490
580
  { op: "insert_after", anchor: at(text, 2), body: [] },
491
581
  ];
492
- const result = applyEdits(text, edits);
582
+ const result = applyEdits(text, edits, 4, 15);
493
583
  assert.ok(result.ok);
494
584
  if (result.ok) {
495
585
  assert.equal(result.changed, true);
package/src/core/apply.ts CHANGED
@@ -52,9 +52,6 @@ interface SpanOp {
52
52
  newLines: string[];
53
53
  }
54
54
 
55
- /** Default first-pass ±line radius before full-file recovery. */
56
- const DEFAULT_SHIFT_RADIUS = 15;
57
-
58
55
  /**
59
56
  * Verify an anchor against the live content; on mismatch, attempt shifted
60
57
  * recovery. Returns null when the anchor matches, otherwise an
@@ -139,20 +136,6 @@ function checkedAnchor(
139
136
  return { opIndex, which, op: edit.op, cited, status: failure ? "mismatched" : "matched" };
140
137
  }
141
138
 
142
- function inputAnchorChecks(edits: readonly Edit[], status: AnchorCheck["status"]): AnchorCheck[] {
143
- const checks: AnchorCheck[] = [];
144
- for (let opIndex = 0; opIndex < edits.length; opIndex++) {
145
- const edit = edits[opIndex];
146
- if (edit.op === "replace" || edit.op === "delete") {
147
- checks.push({ opIndex, which: "anchor", op: edit.op, cited: edit.start, status });
148
- if (edit.end) checks.push({ opIndex, which: "end", op: edit.op, cited: edit.end, status });
149
- } else if (edit.op === "insert_after" || edit.op === "insert_before") {
150
- checks.push({ opIndex, which: "anchor", op: edit.op, cited: edit.anchor, status });
151
- }
152
- }
153
- return checks;
154
- }
155
-
156
139
  /** Translate an Edit into a SpanOp, verifying anchors and ranges against the current lines. */
157
140
  function translateEdit(
158
141
  edit: Edit,
@@ -212,38 +195,24 @@ function translateEdit(
212
195
  }
213
196
  }
214
197
 
215
- function hasInvalidBodyLine(edits: readonly Edit[]): boolean {
216
- return edits.some((edit) => "body" in edit && edit.body.some((line) => /[\r\n]/.test(line)));
217
- }
218
-
219
198
  /**
220
199
  * Apply edits to `text`. Anchors are verified against the current content; on
221
200
  * success `touchedLines` gives the 0-based indices of the new-file lines this
222
201
  * edit produced. On any anchor mismatch, all failures (with shifted recovery)
223
202
  * are collected and returned together — nothing is written. Every failure includes
224
- * per-input anchor checks; input rejection before hashing marks them not_checked.
203
+ * per-input anchor checks.
225
204
  *
226
205
  * @param text current full file text
227
- * @param edits parsed edit operations
228
- * @param hashLen hash length used to verify anchors (default 4)
229
- * @param shiftRadius first-pass ±line radius before full-file recovery (default 15; 0 disables recovery)
206
+ * @param edits validated edit operations; each body element is one logical line
207
+ * @param hashLen hash length used to verify anchors
208
+ * @param shiftRadius first-pass ±line radius before full-file recovery; 0 disables recovery
230
209
  */
231
210
  export function applyEdits(
232
211
  text: string,
233
212
  edits: Edit[],
234
- hashLen = 4,
235
- shiftRadius = DEFAULT_SHIFT_RADIUS,
213
+ hashLen: number,
214
+ shiftRadius: number,
236
215
  ): ApplyResult {
237
- if (hasInvalidBodyLine(edits)) {
238
- return {
239
- ok: false,
240
- failure: {
241
- kind: "input",
242
- message: "INVALID_BODY: each body element must contain exactly one logical line.",
243
- checks: inputAnchorChecks(edits, "not_checked"),
244
- },
245
- };
246
- }
247
216
  const lines = splitLines(text);
248
217
  const bom = text.startsWith("\uFEFF") ? "\uFEFF" : "";
249
218
  const ending = detectLineEnding(text);