@criogaid/pi-hashline-edit 0.7.0 → 0.8.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 (118) hide show
  1. package/README.md +74 -222
  2. package/README.zh-CN.md +162 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +172 -134
  6. package/src/core/apply.ts +28 -30
  7. package/src/core/case-fold.test.ts +11 -7
  8. package/src/core/case-fold.ts +2 -6
  9. package/src/core/character-set.ts +14 -0
  10. package/src/core/hash.test.ts +1 -25
  11. package/src/core/hash.ts +0 -5
  12. package/src/core/lines.ts +25 -7
  13. package/src/core/ranges.test.ts +41 -9
  14. package/src/core/ranges.ts +11 -4
  15. package/src/core/regex.ts +36 -6
  16. package/src/core/replace-schema.ts +18 -11
  17. package/src/core/replace.test.ts +90 -0
  18. package/src/core/replace.ts +6 -5
  19. package/src/core/search-analysis.test.ts +9 -0
  20. package/src/core/search-analysis.ts +35 -31
  21. package/src/core/search.ts +1 -1
  22. package/src/core/types.ts +5 -7
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +16 -8
  25. package/src/integration/fusion-dispatch.test.ts +41 -22
  26. package/src/integration/grep-flags.test.ts +6 -13
  27. package/src/integration/grep-js-completeness.test.ts +41 -31
  28. package/src/integration/grep-order.test.ts +78 -0
  29. package/src/integration/grep-path-matrix.test.ts +4 -3
  30. package/src/integration/grep-regex.test.ts +13 -16
  31. package/src/integration/grep-rg.test.ts +127 -87
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +3 -4
  34. package/src/integration/model-budget-lifecycle.test.ts +15 -6
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +1 -1
  37. package/src/integration/npm-package.testing.ts +6 -32
  38. package/src/integration/publication-read-changes.test.ts +58 -26
  39. package/src/integration/replace-package.testing.ts +4 -1
  40. package/src/integration/replace-worker-boundary.test.ts +34 -24
  41. package/src/integration/report-replay.test.ts +2 -2
  42. package/src/integration/utf8-errors.test.ts +2 -2
  43. package/src/pi/AGENTS.md +12 -13
  44. package/src/pi/action-fusion.test.ts +71 -216
  45. package/src/pi/action-fusion.ts +37 -74
  46. package/src/pi/anchor-format.test.ts +16 -0
  47. package/src/pi/anchor-format.ts +7 -3
  48. package/src/pi/argument-diagnostics.ts +1 -1
  49. package/src/pi/argument-error.testing.ts +7 -4
  50. package/src/pi/argument-error.ts +5 -7
  51. package/src/pi/argument-format.test.ts +5 -16
  52. package/src/pi/argument-validation.test.ts +59 -25
  53. package/src/pi/argument-validation.ts +4 -5
  54. package/src/pi/budgets.ts +5 -7
  55. package/src/pi/config.test.ts +15 -5
  56. package/src/pi/config.ts +6 -6
  57. package/src/pi/diagnostic-buffer.test.ts +2 -3
  58. package/src/pi/diagnostic-buffer.ts +2 -2
  59. package/src/pi/edit-tool.ts +9 -12
  60. package/src/pi/execute.test.ts +80 -149
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +12 -9
  63. package/src/pi/failure-path.test.ts +115 -50
  64. package/src/pi/file-commit.test.ts +30 -46
  65. package/src/pi/file-commit.ts +11 -40
  66. package/src/pi/file-revision.ts +25 -0
  67. package/src/pi/fusion-card-interaction.test.ts +143 -0
  68. package/src/pi/fusion-card.test.ts +193 -37
  69. package/src/pi/fusion-card.ts +186 -116
  70. package/src/pi/grep-candidates.ts +25 -27
  71. package/src/pi/grep-matrix.test.ts +1 -1
  72. package/src/pi/grep-order.test.ts +323 -0
  73. package/src/pi/grep-output.testing.ts +24 -0
  74. package/src/pi/grep-output.ts +62 -69
  75. package/src/pi/grep-render.ts +4 -1
  76. package/src/pi/grep-scope.ts +1 -1
  77. package/src/pi/grep-search.ts +85 -56
  78. package/src/pi/grep-tool.ts +17 -48
  79. package/src/pi/grep.test.ts +270 -169
  80. package/src/pi/mutation-outcome.testing.ts +2 -3
  81. package/src/pi/mutation-result.test.ts +26 -19
  82. package/src/pi/mutation-result.ts +25 -40
  83. package/src/pi/mutation-runner.ts +9 -17
  84. package/src/pi/output-budgets.test.ts +25 -16
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +4 -5
  87. package/src/pi/read-tool.ts +32 -80
  88. package/src/pi/read.test.ts +145 -92
  89. package/src/pi/regex-worker-contract.ts +5 -23
  90. package/src/pi/regex-worker.ts +4 -10
  91. package/src/pi/registration.test.ts +54 -49
  92. package/src/pi/render.test.ts +196 -93
  93. package/src/pi/render.ts +32 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +27 -26
  96. package/src/pi/replace-worker-contract.ts +3 -12
  97. package/src/pi/replace.test.ts +144 -226
  98. package/src/pi/report-cause.ts +23 -19
  99. package/src/pi/report-failure.testing.ts +2 -2
  100. package/src/pi/report-schema.ts +55 -43
  101. package/src/pi/report.test.ts +138 -71
  102. package/src/pi/report.ts +40 -43
  103. package/src/pi/result-safety.test.ts +157 -117
  104. package/src/pi/rg-process.test.ts +36 -10
  105. package/src/pi/rg-process.ts +9 -3
  106. package/src/pi/rg-regex.test.ts +17 -0
  107. package/src/pi/rg-regex.ts +28 -22
  108. package/src/pi/text-stream.test.ts +35 -36
  109. package/src/pi/text-stream.ts +14 -14
  110. package/src/pi/tool-call.testing.ts +17 -7
  111. package/src/pi/write-tool.ts +9 -11
  112. package/src/pi/write.test.ts +43 -75
  113. package/src/testing/regex-translation-corpus.ts +16 -0
  114. package/src/testing/rg-line-listing.fixture.mjs +1 -1
  115. package/src/integration/forget-grep.test.ts +0 -148
  116. package/src/pi/forget-tool.ts +0 -367
  117. package/src/pi/forget.test.ts +0 -630
  118. package/src/pi/forget.testing.ts +0 -201
@@ -14,7 +14,7 @@ function at(text: string, line: number): Anchor {
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"] }], 4, 15);
17
+ const r = applyEdits(text, [{ op: "replace", anchor: 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"] }], 4, 15);
25
+ const r = applyEdits(text, [{ op: "replace", anchor: 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
  }
@@ -32,7 +32,7 @@ test("replace range", () => {
32
32
  const text = "a\nb\nc\nd\ne\n";
33
33
  const r = applyEdits(
34
34
  text,
35
- [{ op: "replace", start: at(text, 2), end: at(text, 4), body: ["X", "Y"] }],
35
+ [{ op: "replace", anchor: at(text, 2), end: at(text, 4), body: ["X", "Y"] }],
36
36
  4,
37
37
  15,
38
38
  );
@@ -42,7 +42,7 @@ test("replace range", () => {
42
42
 
43
43
  test("delete single line / range", () => {
44
44
  const text = "a\nb\nc\nd\n";
45
- const r = applyEdits(text, [{ op: "delete", start: at(text, 2), end: at(text, 3) }], 4, 15);
45
+ const r = applyEdits(text, [{ op: "delete", anchor: at(text, 2), end: at(text, 3) }], 4, 15);
46
46
  assert.equal(r.ok, true);
47
47
  if (r.ok) assert.equal(r.text, "a\nd\n");
48
48
  });
@@ -82,7 +82,7 @@ test("multiple out-of-order ops → applied at the right positions", () => {
82
82
  text,
83
83
  [
84
84
  { op: "insert_after", anchor: at(text, 3), body: ["z"] },
85
- { op: "replace", start: at(text, 1), body: ["A"] },
85
+ { op: "replace", anchor: at(text, 1), body: ["A"] },
86
86
  ],
87
87
  4,
88
88
  15,
@@ -93,23 +93,11 @@ test("multiple out-of-order ops → applied at the right positions", () => {
93
93
 
94
94
  // --- error paths ---
95
95
 
96
- test("anchor hash mismatch rejected (line changed)", () => {
97
- const text = "a\nb\n";
98
- const r = applyEdits(
99
- text,
100
- [{ op: "replace", start: { line: 1, hash: "WRONG" }, body: ["x"] }],
101
- 4,
102
- 15,
103
- );
104
- assert.equal(r.ok, false);
105
- if (!r.ok) assert.equal(r.failure.kind, "anchor");
106
- });
107
-
108
96
  test("line out of range rejected", () => {
109
97
  const text = "a\n";
110
98
  const r = applyEdits(
111
99
  text,
112
- [{ op: "replace", start: { line: 5, hash: computeLineHash(1, "a", 4) }, body: ["x"] }],
100
+ [{ op: "replace", anchor: { line: 5, hash: computeLineHash(1, "a", 4) }, body: ["x"] }],
113
101
  4,
114
102
  15,
115
103
  );
@@ -122,7 +110,7 @@ test("anchor mismatch when the cited line's content differs (live verification)"
122
110
  const text = "a\nb\n";
123
111
  const r = applyEdits(
124
112
  text,
125
- [{ op: "replace", start: { line: 1, hash: computeLineHash(2, "b", 4) }, body: ["x"] }],
113
+ [{ op: "replace", anchor: { line: 1, hash: computeLineHash(2, "b", 4) }, body: ["x"] }],
126
114
  4,
127
115
  15,
128
116
  );
@@ -134,7 +122,7 @@ test("reverse-order range rejected", () => {
134
122
  const text = "a\nb\nc\n";
135
123
  const r = applyEdits(
136
124
  text,
137
- [{ op: "replace", start: at(text, 3), end: at(text, 1), body: ["x"] }],
125
+ [{ op: "replace", anchor: at(text, 3), end: at(text, 1), body: ["x"] }],
138
126
  4,
139
127
  15,
140
128
  );
@@ -142,45 +130,67 @@ test("reverse-order range rejected", () => {
142
130
  if (!r.ok) assert.equal(r.failure.kind, "range");
143
131
  });
144
132
 
145
- test("overlapping edits rejected", () => {
133
+ test("range starts inside a range or shares its start → reject overlapping edits", () => {
146
134
  const text = "a\nb\nc\nd\n";
147
- const r = applyEdits(
148
- text,
149
- [
150
- { op: "replace", start: at(text, 2), end: at(text, 3), body: ["x"] },
151
- { op: "replace", start: at(text, 3), body: ["y"] },
152
- ],
153
- 4,
154
- 15,
155
- );
156
- assert.equal(r.ok, false);
157
- if (!r.ok) assert.equal(r.failure.kind, "range");
135
+ for (const edit of [
136
+ { op: "replace", anchor: at(text, 3), body: ["y"] },
137
+ { op: "delete", anchor: at(text, 2) },
138
+ { op: "insert_after", anchor: at(text, 2), body: ["y"] },
139
+ ] satisfies Edit[]) {
140
+ const result = applyEdits(
141
+ text,
142
+ [{ op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["x"] }, edit],
143
+ 4,
144
+ 0,
145
+ );
146
+ assert(!result.ok && result.failure.kind === "range");
147
+ assert.equal(result.failure.code, "OVERLAPPING_EDITS");
148
+ }
158
149
  });
159
150
 
160
- test("conflict at the same insertion point rejected", () => {
151
+ test("insertion at a replaced or deleted range start → insert before the range in either batch order", () => {
152
+ const text = "a\nb\nc\nd\n";
153
+ const insertion: Edit = { op: "insert_after", anchor: at(text, 1), body: ["x"] };
154
+ for (const [edit, expected] of [
155
+ [{ op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["B"] }, "a\nx\nB\nd\n"],
156
+ [{ op: "delete", anchor: at(text, 2), end: at(text, 3) }, "a\nx\nd\n"],
157
+ ] satisfies [Edit, string][]) {
158
+ for (const edits of [
159
+ [insertion, edit],
160
+ [edit, insertion],
161
+ ]) {
162
+ const result = applyEdits(text, edits, 4, 0);
163
+ assert(result.ok);
164
+ assert.equal(result.text, expected);
165
+ }
166
+ }
167
+ });
168
+
169
+ test("insertions sharing a gap → preserve array order", () => {
161
170
  const text = "a\nb\n";
162
- const r = applyEdits(
163
- text,
164
- [
165
- { op: "insert_after", anchor: at(text, 1), body: ["x"] },
166
- { op: "insert_after", anchor: at(text, 1), body: ["y"] },
167
- ],
168
- 4,
169
- 15,
170
- );
171
- assert.equal(r.ok, false);
172
- if (!r.ok) assert.equal(r.failure.kind, "range");
171
+ const edits: Edit[] = [
172
+ { op: "insert_after", anchor: at(text, 1), body: ["x"] },
173
+ { op: "insert_before", anchor: at(text, 2), body: ["y"] },
174
+ ];
175
+ for (const [batch, expected] of [
176
+ [edits, "a\nx\ny\nb\n"],
177
+ [[...edits].reverse(), "a\ny\nx\nb\n"],
178
+ ] satisfies [Edit[], string][]) {
179
+ const result = applyEdits(text, batch, 4, 0);
180
+ assert(result.ok);
181
+ assert.equal(result.text, expected);
182
+ }
173
183
  });
174
184
 
175
185
  test("byte-identical edits → validate anchors and preserve the source bytes", () => {
176
186
  for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
177
187
  const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
178
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: [body] }], 4, 15);
188
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: [body] }], 4, 15);
179
189
  assert.ok(r.ok);
180
190
  assert.equal(r.text, text);
181
191
  const invalid = applyEdits(
182
192
  text,
183
- [{ op: "replace", start: at("wrong\nb\n", 1), body: [body] }],
193
+ [{ op: "replace", anchor: at("wrong\nb\n", 1), body: [body] }],
184
194
  4,
185
195
  15,
186
196
  );
@@ -194,7 +204,7 @@ test("unrelated change elsewhere does NOT block the edit (no global stale check)
194
204
  const currentText = "a\nb\nCHANGED\n";
195
205
  const r = applyEdits(
196
206
  currentText,
197
- [{ op: "replace", start: at(readText, 1), body: ["A"] }],
207
+ [{ op: "replace", anchor: at(readText, 1), body: ["A"] }],
198
208
  4,
199
209
  15,
200
210
  );
@@ -204,7 +214,7 @@ test("unrelated change elsewhere does NOT block the edit (no global stale check)
204
214
 
205
215
  test("CRLF line endings preserved", () => {
206
216
  const text = "a\r\nb\r\n";
207
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
217
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
208
218
  assert.equal(r.ok, true);
209
219
  if (r.ok) assert.equal(r.text, "A\r\nb\r\n");
210
220
  });
@@ -213,7 +223,7 @@ test("editing a mixed-ending file preserves untouched separators", () => {
213
223
  const before = "first\r\nsecond\nthird\r\n";
214
224
  const result = applyEdits(
215
225
  before,
216
- [{ op: "replace", start: at(before, 2), body: ["SECOND"] }],
226
+ [{ op: "replace", anchor: at(before, 2), body: ["SECOND"] }],
217
227
  4,
218
228
  15,
219
229
  );
@@ -231,7 +241,7 @@ test("inserting and deleting in mixed-ending files preserves surviving separator
231
241
  );
232
242
  assert.equal(inserted.ok, true);
233
243
  if (inserted.ok) assert.equal(inserted.text, "a\r\nb\nx\r\nc\r\nlast");
234
- const deleted = applyEdits(before, [{ op: "delete", start: at(before, 2) }], 4, 15);
244
+ const deleted = applyEdits(before, [{ op: "delete", anchor: at(before, 2) }], 4, 15);
235
245
  assert.equal(deleted.ok, true);
236
246
  if (deleted.ok) assert.equal(deleted.text, "a\r\nc\r\nlast");
237
247
  });
@@ -240,7 +250,7 @@ test("replacing one line with several keeps the block's trailing gap", () => {
240
250
  const before = "a\r\nb\nc\r\nd";
241
251
  const result = applyEdits(
242
252
  before,
243
- [{ op: "replace", start: at(before, 2), body: ["X", "Y", "Z"] }],
253
+ [{ op: "replace", anchor: at(before, 2), body: ["X", "Y", "Z"] }],
244
254
  4,
245
255
  15,
246
256
  );
@@ -252,14 +262,14 @@ test("replacing one line with several keeps the block's trailing gap", () => {
252
262
 
253
263
  test("a file without a final newline keeps not having one", () => {
254
264
  const text = "a\nb";
255
- const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }], 4, 15);
265
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body: ["B"] }], 4, 15);
256
266
  assert.equal(r.ok, true);
257
267
  if (r.ok) assert.equal(r.text, "a\nB");
258
268
  });
259
269
 
260
270
  test("CRLF without a final newline keeps not having one", () => {
261
271
  const text = "a\r\nb";
262
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
272
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
263
273
  assert.equal(r.ok, true);
264
274
  if (r.ok) assert.equal(r.text, "A\r\nb");
265
275
  });
@@ -277,9 +287,9 @@ test("edits retain empty final logical lines even without an original final newl
277
287
  const ending = text.includes("\r\n") ? "\r\n" : "\n";
278
288
  const prefix = text.slice(0, text.lastIndexOf("\n") + 1);
279
289
  const cases: [Edit, string][] = [
280
- [{ op: "replace", start: at(text, line), body: [""] }, prefix + ending],
290
+ [{ op: "replace", anchor: at(text, line), body: [""] }, prefix + ending],
281
291
  [
282
- { op: "replace", start: at(text, line), body: ["new", ""] },
292
+ { op: "replace", anchor: at(text, line), body: ["new", ""] },
283
293
  `${prefix}new${ending}${ending}`,
284
294
  ],
285
295
  [{ op: "append", body: [""] }, text + ending + ending],
@@ -295,7 +305,7 @@ test("edits retain empty final logical lines even without an original final newl
295
305
  for (const text of ["\nlast", "head\r\n\r\nlast"]) {
296
306
  const result = applyEdits(
297
307
  text,
298
- [{ op: "delete", start: at(text, splitLines(text).length) }],
308
+ [{ op: "delete", anchor: at(text, splitLines(text).length) }],
299
309
  4,
300
310
  15,
301
311
  );
@@ -306,11 +316,11 @@ test("edits retain empty final logical lines even without an original final newl
306
316
 
307
317
  test("a BOM represents an empty first line without a terminator, but not an empty later line", () => {
308
318
  const cases: [string, Edit[], string][] = [
309
- ["\uFEFFold", [{ op: "replace", start: at("\uFEFFold", 1), body: [""] }], "\uFEFF"],
310
- ["\uFEFF", [{ op: "replace", start: at("\uFEFF", 1), body: [""] }], "\uFEFF"],
319
+ ["\uFEFFold", [{ op: "replace", anchor: at("\uFEFFold", 1), body: [""] }], "\uFEFF"],
320
+ ["\uFEFF", [{ op: "replace", anchor: at("\uFEFF", 1), body: [""] }], "\uFEFF"],
311
321
  ["\uFEFF", [{ op: "append", body: [""] }], "\uFEFF\n\n"],
312
322
  ["\uFEFF", [{ op: "prepend", body: ["new"] }], "\uFEFFnew\n\n"],
313
- ["\uFEFF\nlast", [{ op: "delete", start: at("\uFEFF\nlast", 2) }], "\uFEFF"],
323
+ ["\uFEFF\nlast", [{ op: "delete", anchor: at("\uFEFF\nlast", 2) }], "\uFEFF"],
314
324
  ];
315
325
  for (const [text, edits, expected] of cases) {
316
326
  const result = applyEdits(text, edits, 4, 15);
@@ -322,55 +332,46 @@ test("a BOM represents an empty first line without a terminator, but not an empt
322
332
  test("large replacement bodies retain snapshot boundaries without a function argument limit", () => {
323
333
  const text = "head\r\nold\ntail";
324
334
  const body = Array<string>(150_000).fill("x");
325
- const result = applyEdits(text, [{ op: "replace", start: at(text, 2), body }], 4, 15);
335
+ const result = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body }], 4, 15);
326
336
  assert.ok(result.ok);
327
337
  assert.equal(result.text, `head\r\n${"x\r\n".repeat(body.length - 1)}x\ntail`);
328
338
  });
329
339
 
330
340
  test("noop is still detected when the file lacks a final newline", () => {
331
341
  const text = "a\nb";
332
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["a"] }], 4, 15);
342
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["a"] }], 4, 15);
333
343
  assert.ok(r.ok);
334
344
  assert.equal(r.text, text);
335
345
  });
336
346
 
337
347
  // --- shifted-anchor recovery ---
338
348
 
339
- test("shifted recovery: content moved down → found with a fresh anchor", () => {
340
- const readText = "a\nb\nc\nd\ne\n";
341
- const currentText = "a\nX\nb\nc\nd\ne\n"; // inserted X after line 1 → "c" moved 3→4
342
- const r = applyEdits(
343
- currentText,
344
- [{ op: "replace", start: at(readText, 3), body: ["C"] }],
345
- 4,
346
- 15,
347
- );
348
- assert.ok(!r.ok && r.failure.kind === "anchor");
349
- const f = r.failure.failures[0];
350
- assert.ok(f.recovery.kind === "found");
351
- assert.equal(f.recovery.newLine, 4);
352
- assert.equal(f.recovery.scope, "local");
353
- // The recovered anchor must verify against the current file.
354
- assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
355
- });
356
-
357
349
  test("rescued anchor lets the retry succeed without a re-read", () => {
358
350
  const readText = "a\nb\nc\nd\ne\n";
359
351
  const currentText = "a\nX\nb\nc\nd\ne\n";
360
352
  // first attempt with a stale anchor → rescued
361
353
  const r1 = applyEdits(
362
354
  currentText,
363
- [{ op: "replace", start: at(readText, 3), body: ["C"] }],
355
+ [{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
364
356
  4,
365
357
  15,
366
358
  );
367
359
  assert.ok(!r1.ok && r1.failure.kind === "anchor");
368
360
  const f = r1.failure.failures[0];
369
361
  assert.ok(f.recovery.kind === "found");
362
+ assert.equal(f.recovery.newLine, 4);
363
+ assert.equal(f.recovery.scope, "local");
364
+ assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
370
365
  // Retry with the recovered anchor; the rest of the file stays unchanged.
371
366
  const r2 = applyEdits(
372
367
  currentText,
373
- [{ op: "replace", start: { line: f.recovery.newLine, hash: f.recovery.newHash }, body: ["C"] }],
368
+ [
369
+ {
370
+ op: "replace",
371
+ anchor: { line: f.recovery.newLine, hash: f.recovery.newHash },
372
+ body: ["C"],
373
+ },
374
+ ],
374
375
  4,
375
376
  15,
376
377
  );
@@ -383,7 +384,7 @@ test("shifted recovery: duplicate content → ambiguous candidates", () => {
383
384
  const currentText = "a\nY\nx\nb\nx\nc\n"; // both "x" shifted
384
385
  const r = applyEdits(
385
386
  currentText,
386
- [{ op: "replace", start: at(readText, 2), body: ["Z"] }],
387
+ [{ op: "replace", anchor: at(readText, 2), body: ["Z"] }],
387
388
  4,
388
389
  15,
389
390
  );
@@ -402,7 +403,7 @@ test("shifted recovery: content genuinely changed → none, with live content",
402
403
  const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
403
404
  const r = applyEdits(
404
405
  currentText,
405
- [{ op: "replace", start: at(readText, 2), body: ["B"] }],
406
+ [{ op: "replace", anchor: at(readText, 2), body: ["B"] }],
406
407
  4,
407
408
  15,
408
409
  );
@@ -422,7 +423,7 @@ test("full-file recovery finds distant content and anchors beyond the current EO
422
423
  lines[newLine - 1] = "target";
423
424
  const result = applyEdits(
424
425
  lines.join("\n"),
425
- [{ op: "delete", start: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
426
+ [{ op: "delete", anchor: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
426
427
  4,
427
428
  15,
428
429
  );
@@ -446,7 +447,7 @@ test("full-file recovery collects both sides while preserving local candidate pr
446
447
  for (const line of positions) lines[line - 1] = "target";
447
448
  const result = applyEdits(
448
449
  lines.join("\n"),
449
- [{ op: "delete", start: { line: 50, hash: computeLineHash(50, "target", 4) } }],
450
+ [{ op: "delete", anchor: { line: 50, hash: computeLineHash(50, "target", 4) } }],
450
451
  4,
451
452
  15,
452
453
  );
@@ -478,8 +479,8 @@ test("collect-all: two stale anchors in one batch → both failures returned", (
478
479
  const r = applyEdits(
479
480
  currentText,
480
481
  [
481
- { op: "replace", start: at(readText, 2), body: ["B"] },
482
- { op: "replace", start: at(readText, 4), body: ["D"] },
482
+ { op: "replace", anchor: at(readText, 2), body: ["B"] },
483
+ { op: "replace", anchor: at(readText, 4), body: ["D"] },
483
484
  ],
484
485
  4,
485
486
  15,
@@ -495,7 +496,12 @@ test("collect-all: two stale anchors in one batch → both failures returned", (
495
496
  test("shiftRadius=0 disables rescue (always none)", () => {
496
497
  const readText = "a\nb\nc\nd\ne\n";
497
498
  const currentText = "a\nX\nb\nc\nd\ne\n";
498
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }], 4, 0);
499
+ const r = applyEdits(
500
+ currentText,
501
+ [{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
502
+ 4,
503
+ 0,
504
+ );
499
505
  assert.ok(!r.ok && r.failure.kind === "anchor");
500
506
  assert.equal(r.failure.failures[0].recovery.kind, "none");
501
507
  });
@@ -510,18 +516,18 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
510
516
  const ending = text.includes("\r\n") ? "\r\n" : "\n";
511
517
  const rest = text.slice(text.indexOf("\n") + 1);
512
518
  const cases: [Edit, string][] = [
513
- [{ op: "replace", start: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
519
+ [{ op: "replace", anchor: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
514
520
  [
515
- { op: "replace", start: at(text, 1), body: ["\uFEFFchanged"] },
521
+ { op: "replace", anchor: at(text, 1), body: ["\uFEFFchanged"] },
516
522
  `\uFEFFchanged${ending}${rest}`,
517
523
  ],
518
- [{ op: "delete", start: at(text, 1) }, `\uFEFF${rest}`],
524
+ [{ op: "delete", anchor: at(text, 1) }, `\uFEFF${rest}`],
519
525
  [{ op: "prepend", body: ["new"] }, `\uFEFFnew${ending}${text.slice(1)}`],
520
526
  [
521
527
  { op: "insert_before", anchor: at(text, 1), body: ["new"] },
522
528
  `\uFEFFnew${ending}${text.slice(1)}`,
523
529
  ],
524
- [{ op: "delete", start: at(text, 1), end: at(text, 2) }, "\uFEFF"],
530
+ [{ op: "delete", anchor: at(text, 1), end: at(text, 2) }, "\uFEFF"],
525
531
  ];
526
532
  for (const [edit, expected] of cases) {
527
533
  const result = applyEdits(text, [edit], 4, 15);
@@ -532,7 +538,7 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
532
538
  for (const text of ["\uFEFF", "\uFEFFfirst"]) {
533
539
  const result = applyEdits(
534
540
  text,
535
- [{ op: "replace", start: at(text, 1), body: ["changed"] }],
541
+ [{ op: "replace", anchor: at(text, 1), body: ["changed"] }],
536
542
  4,
537
543
  15,
538
544
  );
@@ -542,7 +548,7 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
542
548
  const embedded = "\uFEFFfirst\ninside\uFEFFcontent\n";
543
549
  const result = applyEdits(
544
550
  embedded,
545
- [{ op: "replace", start: at(embedded, 1), body: ["changed"] }],
551
+ [{ op: "replace", anchor: at(embedded, 1), body: ["changed"] }],
546
552
  4,
547
553
  15,
548
554
  );
@@ -554,8 +560,8 @@ test("failed batches report every supplied anchor in input order from one snapsh
554
560
  const text = "a\nb\nc\nd\ne\nf\n";
555
561
  const stale = { line: 2, hash: "ZZ" };
556
562
  const edits: Edit[] = [
557
- { op: "replace", start: at(text, 1), end: stale, body: ["A"] },
558
- { op: "delete", start: stale, end: at(text, 4) },
563
+ { op: "replace", anchor: at(text, 1), end: stale, body: ["A"] },
564
+ { op: "delete", anchor: stale, end: at(text, 4) },
559
565
  { op: "insert_after", anchor: at(text, 5), body: ["E"] },
560
566
  { op: "insert_before", anchor: stale, body: ["B"] },
561
567
  { op: "append", body: ["last"] },
@@ -564,23 +570,23 @@ test("failed batches report every supplied anchor in input order from one snapsh
564
570
  const result = applyEdits(text, edits, 4, 15);
565
571
  assert.ok(!result.ok && result.failure.kind === "anchor");
566
572
  assert.deepEqual(result.failure.checks, [
567
- { opIndex: 0, op: "replace", which: "anchor", cited: at(text, 1), status: "matched" },
568
- { opIndex: 0, op: "replace", which: "end", cited: stale, status: "mismatched" },
569
- { opIndex: 1, op: "delete", which: "anchor", cited: stale, status: "mismatched" },
570
- { opIndex: 1, op: "delete", which: "end", cited: at(text, 4), status: "matched" },
571
- { opIndex: 2, op: "insert_after", which: "anchor", cited: at(text, 5), status: "matched" },
572
- { opIndex: 3, op: "insert_before", which: "anchor", cited: stale, status: "mismatched" },
573
+ { opIndex: 0, which: "anchor", status: "matched" },
574
+ { opIndex: 0, which: "end", status: "mismatched" },
575
+ { opIndex: 1, which: "anchor", status: "mismatched" },
576
+ { opIndex: 1, which: "end", status: "matched" },
577
+ { opIndex: 2, which: "anchor", status: "matched" },
578
+ { opIndex: 3, which: "anchor", status: "mismatched" },
573
579
  ]);
574
580
  });
575
581
 
576
582
  test("matched anchor checks do not imply valid ranges", () => {
577
583
  const text = "a\nb\nc\n";
578
584
  const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
579
- { edits: [{ op: "delete", start: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
585
+ { edits: [{ op: "delete", anchor: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
580
586
  {
581
587
  edits: [
582
- { op: "replace", start: at(text, 1), end: at(text, 3), body: ["x"] },
583
- { op: "delete", start: at(text, 2) },
588
+ { op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["x"] },
589
+ { op: "delete", anchor: at(text, 2) },
584
590
  ],
585
591
  kind: "range",
586
592
  checks: 3,
@@ -599,19 +605,19 @@ test("copy/move original ranges → retain captured text and order", () => {
599
605
  const text = "A\nB\nC\nD\n";
600
606
  const cases: { edit: Edit; expected: string }[] = [
601
607
  {
602
- edit: { op: "copy", start: at(text, 2), end: at(text, 3), after: at(text, 4) },
608
+ edit: { op: "copy", anchor: at(text, 2), end: at(text, 3), after: at(text, 4) },
603
609
  expected: "A\nB\nC\nD\nB\nC\n",
604
610
  },
605
611
  {
606
- edit: { op: "move", start: at(text, 1), end: at(text, 2), after: at(text, 4) },
612
+ edit: { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) },
607
613
  expected: "C\nD\nA\nB\n",
608
614
  },
609
615
  {
610
- edit: { op: "move", start: at(text, 3), end: at(text, 4), before: at(text, 1) },
616
+ edit: { op: "move", anchor: at(text, 3), end: at(text, 4), before: at(text, 1) },
611
617
  expected: "C\nD\nA\nB\n",
612
618
  },
613
619
  {
614
- edit: { op: "copy", start: at(text, 1), end: at(text, 3), after: at(text, 1) },
620
+ edit: { op: "copy", anchor: at(text, 1), end: at(text, 3), after: at(text, 1) },
615
621
  expected: "A\nA\nB\nC\nB\nC\nD\n",
616
622
  },
617
623
  ];
@@ -624,10 +630,10 @@ test("copy/move original ranges → retain captured text and order", () => {
624
630
 
625
631
  test("copy source also replaced or deleted → capture original text regardless of batch order", () => {
626
632
  const text = "A\nB\nC\nD\n";
627
- const copy: Edit = { op: "copy", start: at(text, 1), end: at(text, 2), after: at(text, 4) };
633
+ const copy: Edit = { op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) };
628
634
  for (const edit of [
629
- { op: "replace", start: at(text, 1), end: at(text, 2), body: ["NEW"] },
630
- { op: "delete", start: at(text, 1), end: at(text, 2) },
635
+ { op: "replace", anchor: at(text, 1), end: at(text, 2), body: ["NEW"] },
636
+ { op: "delete", anchor: at(text, 1), end: at(text, 2) },
631
637
  ] satisfies Edit[]) {
632
638
  for (const edits of [
633
639
  [copy, edit],
@@ -645,7 +651,7 @@ test("copy/move first-line mixed-ending range → preserve source gaps and keep
645
651
  for (const op of ["copy", "move"] as const) {
646
652
  const result = applyEdits(
647
653
  text,
648
- [{ op, start: at(text, 1), end: at(text, 3), after: at(text, 4) }],
654
+ [{ op, anchor: at(text, 1), end: at(text, 3), after: at(text, 4) }],
649
655
  4,
650
656
  0,
651
657
  );
@@ -658,7 +664,7 @@ test("copy/move unterminated final line → add interior connectors and preserve
658
664
  for (const ending of ["\n", "\r\n"]) {
659
665
  const text = `A${ending}B`;
660
666
  for (const op of ["copy", "move"] as const) {
661
- const result = applyEdits(text, [{ op, start: at(text, 2), before: at(text, 1) }], 4, 0);
667
+ const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
662
668
  assert(result.ok);
663
669
  assert.equal(result.text, op === "copy" ? `B${ending}A${ending}B` : `B${ending}A`);
664
670
  }
@@ -668,7 +674,7 @@ test("copy/move unterminated final line → add interior connectors and preserve
668
674
  test("copy/move line ending in standalone CR → connector retains the CR as line content", () => {
669
675
  const text = "A\nB\r";
670
676
  for (const op of ["copy", "move"] as const) {
671
- const result = applyEdits(text, [{ op, start: at(text, 2), before: at(text, 1) }], 4, 0);
677
+ const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
672
678
  assert(result.ok);
673
679
  assert.equal(result.text, op === "copy" ? "B\r\r\nA\nB\r" : "B\r\r\nA");
674
680
  assert.equal(splitLines(result.text)[0], "B\r");
@@ -679,7 +685,7 @@ test("copy Unicode and standalone-CR content → preserve every source code unit
679
685
  const text = "🙂中文 e\u0301\nkeep\rcontent\n";
680
686
  const result = applyEdits(
681
687
  text,
682
- [{ op: "copy", start: at(text, 1), end: at(text, 2), after: at(text, 2) }],
688
+ [{ op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 2) }],
683
689
  4,
684
690
  0,
685
691
  );
@@ -694,7 +700,7 @@ test("copy blank or BOM-only line → retain the added logical blank line", () =
694
700
  ] as const) {
695
701
  const result = applyEdits(
696
702
  text,
697
- [{ op: "copy", start: at(text, line), after: at(text, line) }],
703
+ [{ op: "copy", anchor: at(text, line), after: at(text, line) }],
698
704
  4,
699
705
  0,
700
706
  );
@@ -709,7 +715,7 @@ test("move to either adjacent gap → byte-identical no-op", () => {
709
715
  for (const destination of [{ before: at(text, 2) }, { after: at(text, 3) }]) {
710
716
  const result = applyEdits(
711
717
  text,
712
- [{ op: "move", start: at(text, 2), end: at(text, 3), ...destination }],
718
+ [{ op: "move", anchor: at(text, 2), end: at(text, 3), ...destination }],
713
719
  4,
714
720
  0,
715
721
  );
@@ -729,7 +735,7 @@ test("copy/move bad source and destination anchors → aggregate every supplied
729
735
  [
730
736
  {
731
737
  op: "copy",
732
- start: { line: 1, hash: "XXXX" },
738
+ anchor: { line: 1, hash: "XXXX" },
733
739
  end: { line: 2, hash: "XXXX" },
734
740
  ...destination,
735
741
  },
@@ -746,23 +752,55 @@ test("copy/move bad source and destination anchors → aggregate every supplied
746
752
  }
747
753
  });
748
754
 
749
- test("move interior destination, overlapping moves, or duplicate copy gaps → reject the complete batch", () => {
755
+ test("copy/move captures sharing a gap → preserve original captures in array order", () => {
750
756
  const text = "A\nB\nC\nD\nE\n";
751
- const cases: Edit[][] = [
752
- [{ op: "move", start: at(text, 1), end: at(text, 3), after: at(text, 2) }],
753
- [
754
- { op: "move", start: at(text, 1), end: at(text, 2), after: at(text, 5) },
755
- { op: "move", start: at(text, 2), end: at(text, 3), before: at(text, 1) },
756
- ],
757
- [
758
- { op: "copy", start: at(text, 1), after: at(text, 5) },
759
- { op: "copy", start: at(text, 2), after: at(text, 5) },
760
- ],
761
- [{ op: "copy", start: at(text, 3), end: at(text, 2), after: at(text, 5) }],
762
- ];
763
- for (const edits of cases) {
764
- const result = applyEdits(text, edits, 4, 0);
757
+ for (const [first, second, remaining] of [
758
+ ["copy", "copy", text],
759
+ ["copy", "move", "A\nC\nD\nE\n"],
760
+ ["move", "copy", "B\nC\nD\nE\n"],
761
+ ["move", "move", "C\nD\nE\n"],
762
+ ] as const) {
763
+ const edits: Edit[] = [
764
+ { op: first, anchor: at(text, 1), after: at(text, 5) },
765
+ { op: second, anchor: at(text, 2), after: at(text, 5) },
766
+ ];
767
+ for (const [batch, suffix] of [
768
+ [edits, "A\nB\n"],
769
+ [[...edits].reverse(), "B\nA\n"],
770
+ ] satisfies [Edit[], string][]) {
771
+ const result = applyEdits(text, batch, 4, 0);
772
+ assert(result.ok);
773
+ assert.equal(result.text, remaining + suffix);
774
+ }
775
+ }
776
+ });
777
+
778
+ test("move interior destination or reversed capture range → reject invalid ranges", () => {
779
+ const text = "A\nB\nC\nD\nE\n";
780
+ for (const edit of [
781
+ { op: "move", anchor: at(text, 1), end: at(text, 3), after: at(text, 2) },
782
+ { op: "move", anchor: at(text, 1), end: at(text, 3), before: at(text, 2) },
783
+ { op: "copy", anchor: at(text, 3), end: at(text, 2), after: at(text, 5) },
784
+ ] satisfies Edit[]) {
785
+ const result = applyEdits(text, [edit], 4, 0);
765
786
  assert(!result.ok && result.failure.kind === "range");
787
+ assert.equal(result.failure.code, "INVALID_RANGE");
766
788
  assert(result.failure.checks.every((check) => check.status === "matched"));
767
789
  }
768
790
  });
791
+
792
+ test("overlapping move sources → reject the complete batch", () => {
793
+ const text = "A\nB\nC\nD\nE\n";
794
+ const result = applyEdits(
795
+ text,
796
+ [
797
+ { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 5) },
798
+ { op: "move", anchor: at(text, 2), end: at(text, 3), before: at(text, 1) },
799
+ ],
800
+ 4,
801
+ 0,
802
+ );
803
+ assert(!result.ok && result.failure.kind === "range");
804
+ assert.equal(result.failure.code, "OVERLAPPING_EDITS");
805
+ assert(result.failure.checks.every((check) => check.status === "matched"));
806
+ });