@criogaid/pi-hashline-edit 0.7.0 → 0.9.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 +76 -220
  2. package/README.zh-CN.md +166 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +171 -133
  6. package/src/core/apply.ts +34 -31
  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 +49 -9
  14. package/src/core/ranges.ts +15 -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 +6 -8
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +18 -10
  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 +132 -92
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +4 -5
  34. package/src/integration/model-budget-lifecycle.test.ts +26 -8
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +2 -2
  37. package/src/integration/npm-package.testing.ts +7 -33
  38. package/src/integration/publication-read-changes.test.ts +60 -28
  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 +3 -3
  42. package/src/integration/utf8-errors.test.ts +3 -3
  43. package/src/pi/AGENTS.md +13 -14
  44. package/src/pi/action-fusion.test.ts +75 -220
  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 +35 -186
  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 +25 -38
  52. package/src/pi/argument-validation.test.ts +446 -420
  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 +175 -121
  60. package/src/pi/execute.test.ts +170 -227
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +13 -10
  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 +271 -170
  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 +27 -18
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +7 -6
  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 +106 -67
  92. package/src/pi/render.test.ts +239 -109
  93. package/src/pi/render.ts +42 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +29 -27
  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 +60 -45
  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 +186 -131
  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
@@ -7,7 +7,7 @@ Internal pure engine: no Pi imports and no public API. It trusts input already f
7
7
  - `hash.ts` computes position-dependent line checksums (line number + content → `LINE#HASH`). `lines.ts` handles splitting, line-ending detection, and physical line counts. `text.ts` does strict UTF-8 decoding.
8
8
  - `apply.ts` `applyEdits` validates a whole batch of `Edit` ops (see `types.ts`) against a single snapshot: anchor checks, overlap and conflict detection, and shifted-anchor recovery (`AnchorRecovery`: local `shiftRadius` search, then full file). It returns new text or structured failures and never touches the filesystem.
9
9
  - `regex.ts` builds JavaScript regexes on the LF view (`regexSource`, `buildRegex`) for both grep and replace; neither engine imports the other.
10
- - `replace.ts` is replace's engine: literal and JavaScript-regex matching on the LF view, separator restoration, and overlap rejection. `replace-schema.ts` defines the tool's replacement schema and the engine's derived rule type.
10
+ - `replace.ts` is replace's engine: literal and JavaScript-regex matching on the LF view, separator restoration, and overlap rejection. `replace-schema.ts` defines the tool's replacement schema and the engine's result schema, with their derived types.
11
11
  - `search.ts` evaluates native JavaScript queries on the LF view and returns bounded physical rows; `search-schema.ts` defines its result.
12
12
  - `search-analysis.ts` parses patterns with regexpp (V8 has already accepted them at tool entry), decides smart case, and derives the literals every match must contain; `case-fold.ts` supplies JavaScript's case equivalence; `character-set.ts` owns character-class sets and shared AST surrogate-pair decoding for both candidate strategies.
13
13
  - `errors.ts` holds the classified `HashlineError`, typed by code so its facts and `next` alternative must match the code's schema, and errno classification.
@@ -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,90 @@ 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
+ assert.match(result.failure.message, /overlap/);
149
+ }
158
150
  });
159
151
 
160
- test("conflict at the same insertion point rejected", () => {
161
- const text = "a\nb\n";
162
- const r = applyEdits(
163
- text,
152
+ test("insertion at a replaced or deleted range start → insert before the range in either batch order", () => {
153
+ const text = "a\nb\nc\nd\n";
154
+ const insertion: Edit = { op: "insert_after", anchor: at(text, 1), body: ["x"] };
155
+ for (const [edit, expected] of [
156
+ [{ op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["B"] }, "a\nx\nB\nd\n"],
157
+ [{ op: "delete", anchor: at(text, 2), end: at(text, 3) }, "a\nx\nd\n"],
158
+ ] satisfies [Edit, string][]) {
159
+ for (const edits of [
160
+ [insertion, edit],
161
+ [edit, insertion],
162
+ ]) {
163
+ const result = applyEdits(text, edits, 4, 0);
164
+ assert(result.ok);
165
+ assert.equal(result.text, expected);
166
+ }
167
+ }
168
+ });
169
+
170
+ test("insertions sharing a gap → reject the batch", () => {
171
+ const text = "a\nb\nc\nd\n";
172
+ const cases: Edit[][] = [
164
173
  [
165
- { op: "insert_after", anchor: at(text, 1), body: ["x"] },
166
- { op: "insert_after", anchor: at(text, 1), body: ["y"] },
174
+ { op: "insert_after", anchor: at(text, 2), body: ["x"] },
175
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
167
176
  ],
168
- 4,
169
- 15,
170
- );
171
- assert.equal(r.ok, false);
172
- if (!r.ok) assert.equal(r.failure.kind, "range");
177
+ [
178
+ { op: "insert_before", anchor: at(text, 3), body: ["x"] },
179
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
180
+ ],
181
+ [
182
+ { op: "copy", anchor: at(text, 1), after: at(text, 2) },
183
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
184
+ ],
185
+ [
186
+ { op: "move", anchor: at(text, 1), after: at(text, 2) },
187
+ { op: "insert_before", anchor: at(text, 3), body: ["y"] },
188
+ ],
189
+ [
190
+ { op: "append", body: ["x"] },
191
+ { op: "append", body: ["y"] },
192
+ ],
193
+ [
194
+ { op: "prepend", body: ["x"] },
195
+ { op: "insert_before", anchor: at(text, 1), body: ["y"] },
196
+ ],
197
+ ];
198
+ for (const edits of cases) {
199
+ for (const batch of [edits, [...edits].reverse()]) {
200
+ const result = applyEdits(text, batch, 4, 0);
201
+ assert(!result.ok && result.failure.kind === "range");
202
+ assert.equal(result.failure.code, "OVERLAPPING_EDITS");
203
+ assert.match(result.failure.message, /insert more than once/);
204
+ }
205
+ }
173
206
  });
174
207
 
175
208
  test("byte-identical edits → validate anchors and preserve the source bytes", () => {
176
209
  for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
177
210
  const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
178
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: [body] }], 4, 15);
211
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: [body] }], 4, 15);
179
212
  assert.ok(r.ok);
180
213
  assert.equal(r.text, text);
181
214
  const invalid = applyEdits(
182
215
  text,
183
- [{ op: "replace", start: at("wrong\nb\n", 1), body: [body] }],
216
+ [{ op: "replace", anchor: at("wrong\nb\n", 1), body: [body] }],
184
217
  4,
185
218
  15,
186
219
  );
@@ -194,7 +227,7 @@ test("unrelated change elsewhere does NOT block the edit (no global stale check)
194
227
  const currentText = "a\nb\nCHANGED\n";
195
228
  const r = applyEdits(
196
229
  currentText,
197
- [{ op: "replace", start: at(readText, 1), body: ["A"] }],
230
+ [{ op: "replace", anchor: at(readText, 1), body: ["A"] }],
198
231
  4,
199
232
  15,
200
233
  );
@@ -204,7 +237,7 @@ test("unrelated change elsewhere does NOT block the edit (no global stale check)
204
237
 
205
238
  test("CRLF line endings preserved", () => {
206
239
  const text = "a\r\nb\r\n";
207
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
240
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
208
241
  assert.equal(r.ok, true);
209
242
  if (r.ok) assert.equal(r.text, "A\r\nb\r\n");
210
243
  });
@@ -213,7 +246,7 @@ test("editing a mixed-ending file preserves untouched separators", () => {
213
246
  const before = "first\r\nsecond\nthird\r\n";
214
247
  const result = applyEdits(
215
248
  before,
216
- [{ op: "replace", start: at(before, 2), body: ["SECOND"] }],
249
+ [{ op: "replace", anchor: at(before, 2), body: ["SECOND"] }],
217
250
  4,
218
251
  15,
219
252
  );
@@ -231,7 +264,7 @@ test("inserting and deleting in mixed-ending files preserves surviving separator
231
264
  );
232
265
  assert.equal(inserted.ok, true);
233
266
  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);
267
+ const deleted = applyEdits(before, [{ op: "delete", anchor: at(before, 2) }], 4, 15);
235
268
  assert.equal(deleted.ok, true);
236
269
  if (deleted.ok) assert.equal(deleted.text, "a\r\nc\r\nlast");
237
270
  });
@@ -240,7 +273,7 @@ test("replacing one line with several keeps the block's trailing gap", () => {
240
273
  const before = "a\r\nb\nc\r\nd";
241
274
  const result = applyEdits(
242
275
  before,
243
- [{ op: "replace", start: at(before, 2), body: ["X", "Y", "Z"] }],
276
+ [{ op: "replace", anchor: at(before, 2), body: ["X", "Y", "Z"] }],
244
277
  4,
245
278
  15,
246
279
  );
@@ -252,14 +285,14 @@ test("replacing one line with several keeps the block's trailing gap", () => {
252
285
 
253
286
  test("a file without a final newline keeps not having one", () => {
254
287
  const text = "a\nb";
255
- const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }], 4, 15);
288
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body: ["B"] }], 4, 15);
256
289
  assert.equal(r.ok, true);
257
290
  if (r.ok) assert.equal(r.text, "a\nB");
258
291
  });
259
292
 
260
293
  test("CRLF without a final newline keeps not having one", () => {
261
294
  const text = "a\r\nb";
262
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
295
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
263
296
  assert.equal(r.ok, true);
264
297
  if (r.ok) assert.equal(r.text, "A\r\nb");
265
298
  });
@@ -277,9 +310,9 @@ test("edits retain empty final logical lines even without an original final newl
277
310
  const ending = text.includes("\r\n") ? "\r\n" : "\n";
278
311
  const prefix = text.slice(0, text.lastIndexOf("\n") + 1);
279
312
  const cases: [Edit, string][] = [
280
- [{ op: "replace", start: at(text, line), body: [""] }, prefix + ending],
313
+ [{ op: "replace", anchor: at(text, line), body: [""] }, prefix + ending],
281
314
  [
282
- { op: "replace", start: at(text, line), body: ["new", ""] },
315
+ { op: "replace", anchor: at(text, line), body: ["new", ""] },
283
316
  `${prefix}new${ending}${ending}`,
284
317
  ],
285
318
  [{ op: "append", body: [""] }, text + ending + ending],
@@ -295,7 +328,7 @@ test("edits retain empty final logical lines even without an original final newl
295
328
  for (const text of ["\nlast", "head\r\n\r\nlast"]) {
296
329
  const result = applyEdits(
297
330
  text,
298
- [{ op: "delete", start: at(text, splitLines(text).length) }],
331
+ [{ op: "delete", anchor: at(text, splitLines(text).length) }],
299
332
  4,
300
333
  15,
301
334
  );
@@ -306,11 +339,11 @@ test("edits retain empty final logical lines even without an original final newl
306
339
 
307
340
  test("a BOM represents an empty first line without a terminator, but not an empty later line", () => {
308
341
  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"],
342
+ ["\uFEFFold", [{ op: "replace", anchor: at("\uFEFFold", 1), body: [""] }], "\uFEFF"],
343
+ ["\uFEFF", [{ op: "replace", anchor: at("\uFEFF", 1), body: [""] }], "\uFEFF"],
311
344
  ["\uFEFF", [{ op: "append", body: [""] }], "\uFEFF\n\n"],
312
345
  ["\uFEFF", [{ op: "prepend", body: ["new"] }], "\uFEFFnew\n\n"],
313
- ["\uFEFF\nlast", [{ op: "delete", start: at("\uFEFF\nlast", 2) }], "\uFEFF"],
346
+ ["\uFEFF\nlast", [{ op: "delete", anchor: at("\uFEFF\nlast", 2) }], "\uFEFF"],
314
347
  ];
315
348
  for (const [text, edits, expected] of cases) {
316
349
  const result = applyEdits(text, edits, 4, 15);
@@ -322,55 +355,46 @@ test("a BOM represents an empty first line without a terminator, but not an empt
322
355
  test("large replacement bodies retain snapshot boundaries without a function argument limit", () => {
323
356
  const text = "head\r\nold\ntail";
324
357
  const body = Array<string>(150_000).fill("x");
325
- const result = applyEdits(text, [{ op: "replace", start: at(text, 2), body }], 4, 15);
358
+ const result = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body }], 4, 15);
326
359
  assert.ok(result.ok);
327
360
  assert.equal(result.text, `head\r\n${"x\r\n".repeat(body.length - 1)}x\ntail`);
328
361
  });
329
362
 
330
363
  test("noop is still detected when the file lacks a final newline", () => {
331
364
  const text = "a\nb";
332
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["a"] }], 4, 15);
365
+ const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["a"] }], 4, 15);
333
366
  assert.ok(r.ok);
334
367
  assert.equal(r.text, text);
335
368
  });
336
369
 
337
370
  // --- shifted-anchor recovery ---
338
371
 
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
372
  test("rescued anchor lets the retry succeed without a re-read", () => {
358
373
  const readText = "a\nb\nc\nd\ne\n";
359
374
  const currentText = "a\nX\nb\nc\nd\ne\n";
360
375
  // first attempt with a stale anchor → rescued
361
376
  const r1 = applyEdits(
362
377
  currentText,
363
- [{ op: "replace", start: at(readText, 3), body: ["C"] }],
378
+ [{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
364
379
  4,
365
380
  15,
366
381
  );
367
382
  assert.ok(!r1.ok && r1.failure.kind === "anchor");
368
383
  const f = r1.failure.failures[0];
369
384
  assert.ok(f.recovery.kind === "found");
385
+ assert.equal(f.recovery.newLine, 4);
386
+ assert.equal(f.recovery.scope, "local");
387
+ assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
370
388
  // Retry with the recovered anchor; the rest of the file stays unchanged.
371
389
  const r2 = applyEdits(
372
390
  currentText,
373
- [{ op: "replace", start: { line: f.recovery.newLine, hash: f.recovery.newHash }, body: ["C"] }],
391
+ [
392
+ {
393
+ op: "replace",
394
+ anchor: { line: f.recovery.newLine, hash: f.recovery.newHash },
395
+ body: ["C"],
396
+ },
397
+ ],
374
398
  4,
375
399
  15,
376
400
  );
@@ -383,7 +407,7 @@ test("shifted recovery: duplicate content → ambiguous candidates", () => {
383
407
  const currentText = "a\nY\nx\nb\nx\nc\n"; // both "x" shifted
384
408
  const r = applyEdits(
385
409
  currentText,
386
- [{ op: "replace", start: at(readText, 2), body: ["Z"] }],
410
+ [{ op: "replace", anchor: at(readText, 2), body: ["Z"] }],
387
411
  4,
388
412
  15,
389
413
  );
@@ -402,7 +426,7 @@ test("shifted recovery: content genuinely changed → none, with live content",
402
426
  const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
403
427
  const r = applyEdits(
404
428
  currentText,
405
- [{ op: "replace", start: at(readText, 2), body: ["B"] }],
429
+ [{ op: "replace", anchor: at(readText, 2), body: ["B"] }],
406
430
  4,
407
431
  15,
408
432
  );
@@ -422,7 +446,7 @@ test("full-file recovery finds distant content and anchors beyond the current EO
422
446
  lines[newLine - 1] = "target";
423
447
  const result = applyEdits(
424
448
  lines.join("\n"),
425
- [{ op: "delete", start: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
449
+ [{ op: "delete", anchor: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
426
450
  4,
427
451
  15,
428
452
  );
@@ -446,7 +470,7 @@ test("full-file recovery collects both sides while preserving local candidate pr
446
470
  for (const line of positions) lines[line - 1] = "target";
447
471
  const result = applyEdits(
448
472
  lines.join("\n"),
449
- [{ op: "delete", start: { line: 50, hash: computeLineHash(50, "target", 4) } }],
473
+ [{ op: "delete", anchor: { line: 50, hash: computeLineHash(50, "target", 4) } }],
450
474
  4,
451
475
  15,
452
476
  );
@@ -478,8 +502,8 @@ test("collect-all: two stale anchors in one batch → both failures returned", (
478
502
  const r = applyEdits(
479
503
  currentText,
480
504
  [
481
- { op: "replace", start: at(readText, 2), body: ["B"] },
482
- { op: "replace", start: at(readText, 4), body: ["D"] },
505
+ { op: "replace", anchor: at(readText, 2), body: ["B"] },
506
+ { op: "replace", anchor: at(readText, 4), body: ["D"] },
483
507
  ],
484
508
  4,
485
509
  15,
@@ -495,7 +519,12 @@ test("collect-all: two stale anchors in one batch → both failures returned", (
495
519
  test("shiftRadius=0 disables rescue (always none)", () => {
496
520
  const readText = "a\nb\nc\nd\ne\n";
497
521
  const currentText = "a\nX\nb\nc\nd\ne\n";
498
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }], 4, 0);
522
+ const r = applyEdits(
523
+ currentText,
524
+ [{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
525
+ 4,
526
+ 0,
527
+ );
499
528
  assert.ok(!r.ok && r.failure.kind === "anchor");
500
529
  assert.equal(r.failure.failures[0].recovery.kind, "none");
501
530
  });
@@ -510,18 +539,18 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
510
539
  const ending = text.includes("\r\n") ? "\r\n" : "\n";
511
540
  const rest = text.slice(text.indexOf("\n") + 1);
512
541
  const cases: [Edit, string][] = [
513
- [{ op: "replace", start: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
542
+ [{ op: "replace", anchor: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
514
543
  [
515
- { op: "replace", start: at(text, 1), body: ["\uFEFFchanged"] },
544
+ { op: "replace", anchor: at(text, 1), body: ["\uFEFFchanged"] },
516
545
  `\uFEFFchanged${ending}${rest}`,
517
546
  ],
518
- [{ op: "delete", start: at(text, 1) }, `\uFEFF${rest}`],
547
+ [{ op: "delete", anchor: at(text, 1) }, `\uFEFF${rest}`],
519
548
  [{ op: "prepend", body: ["new"] }, `\uFEFFnew${ending}${text.slice(1)}`],
520
549
  [
521
550
  { op: "insert_before", anchor: at(text, 1), body: ["new"] },
522
551
  `\uFEFFnew${ending}${text.slice(1)}`,
523
552
  ],
524
- [{ op: "delete", start: at(text, 1), end: at(text, 2) }, "\uFEFF"],
553
+ [{ op: "delete", anchor: at(text, 1), end: at(text, 2) }, "\uFEFF"],
525
554
  ];
526
555
  for (const [edit, expected] of cases) {
527
556
  const result = applyEdits(text, [edit], 4, 15);
@@ -532,7 +561,7 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
532
561
  for (const text of ["\uFEFF", "\uFEFFfirst"]) {
533
562
  const result = applyEdits(
534
563
  text,
535
- [{ op: "replace", start: at(text, 1), body: ["changed"] }],
564
+ [{ op: "replace", anchor: at(text, 1), body: ["changed"] }],
536
565
  4,
537
566
  15,
538
567
  );
@@ -542,7 +571,7 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
542
571
  const embedded = "\uFEFFfirst\ninside\uFEFFcontent\n";
543
572
  const result = applyEdits(
544
573
  embedded,
545
- [{ op: "replace", start: at(embedded, 1), body: ["changed"] }],
574
+ [{ op: "replace", anchor: at(embedded, 1), body: ["changed"] }],
546
575
  4,
547
576
  15,
548
577
  );
@@ -554,8 +583,8 @@ test("failed batches report every supplied anchor in input order from one snapsh
554
583
  const text = "a\nb\nc\nd\ne\nf\n";
555
584
  const stale = { line: 2, hash: "ZZ" };
556
585
  const edits: Edit[] = [
557
- { op: "replace", start: at(text, 1), end: stale, body: ["A"] },
558
- { op: "delete", start: stale, end: at(text, 4) },
586
+ { op: "replace", anchor: at(text, 1), end: stale, body: ["A"] },
587
+ { op: "delete", anchor: stale, end: at(text, 4) },
559
588
  { op: "insert_after", anchor: at(text, 5), body: ["E"] },
560
589
  { op: "insert_before", anchor: stale, body: ["B"] },
561
590
  { op: "append", body: ["last"] },
@@ -564,23 +593,23 @@ test("failed batches report every supplied anchor in input order from one snapsh
564
593
  const result = applyEdits(text, edits, 4, 15);
565
594
  assert.ok(!result.ok && result.failure.kind === "anchor");
566
595
  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" },
596
+ { opIndex: 0, which: "anchor", status: "matched" },
597
+ { opIndex: 0, which: "end", status: "mismatched" },
598
+ { opIndex: 1, which: "anchor", status: "mismatched" },
599
+ { opIndex: 1, which: "end", status: "matched" },
600
+ { opIndex: 2, which: "anchor", status: "matched" },
601
+ { opIndex: 3, which: "anchor", status: "mismatched" },
573
602
  ]);
574
603
  });
575
604
 
576
605
  test("matched anchor checks do not imply valid ranges", () => {
577
606
  const text = "a\nb\nc\n";
578
607
  const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
579
- { edits: [{ op: "delete", start: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
608
+ { edits: [{ op: "delete", anchor: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
580
609
  {
581
610
  edits: [
582
- { op: "replace", start: at(text, 1), end: at(text, 3), body: ["x"] },
583
- { op: "delete", start: at(text, 2) },
611
+ { op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["x"] },
612
+ { op: "delete", anchor: at(text, 2) },
584
613
  ],
585
614
  kind: "range",
586
615
  checks: 3,
@@ -599,19 +628,19 @@ test("copy/move original ranges → retain captured text and order", () => {
599
628
  const text = "A\nB\nC\nD\n";
600
629
  const cases: { edit: Edit; expected: string }[] = [
601
630
  {
602
- edit: { op: "copy", start: at(text, 2), end: at(text, 3), after: at(text, 4) },
631
+ edit: { op: "copy", anchor: at(text, 2), end: at(text, 3), after: at(text, 4) },
603
632
  expected: "A\nB\nC\nD\nB\nC\n",
604
633
  },
605
634
  {
606
- edit: { op: "move", start: at(text, 1), end: at(text, 2), after: at(text, 4) },
635
+ edit: { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) },
607
636
  expected: "C\nD\nA\nB\n",
608
637
  },
609
638
  {
610
- edit: { op: "move", start: at(text, 3), end: at(text, 4), before: at(text, 1) },
639
+ edit: { op: "move", anchor: at(text, 3), end: at(text, 4), before: at(text, 1) },
611
640
  expected: "C\nD\nA\nB\n",
612
641
  },
613
642
  {
614
- edit: { op: "copy", start: at(text, 1), end: at(text, 3), after: at(text, 1) },
643
+ edit: { op: "copy", anchor: at(text, 1), end: at(text, 3), after: at(text, 1) },
615
644
  expected: "A\nA\nB\nC\nB\nC\nD\n",
616
645
  },
617
646
  ];
@@ -624,10 +653,10 @@ test("copy/move original ranges → retain captured text and order", () => {
624
653
 
625
654
  test("copy source also replaced or deleted → capture original text regardless of batch order", () => {
626
655
  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) };
656
+ const copy: Edit = { op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) };
628
657
  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) },
658
+ { op: "replace", anchor: at(text, 1), end: at(text, 2), body: ["NEW"] },
659
+ { op: "delete", anchor: at(text, 1), end: at(text, 2) },
631
660
  ] satisfies Edit[]) {
632
661
  for (const edits of [
633
662
  [copy, edit],
@@ -645,7 +674,7 @@ test("copy/move first-line mixed-ending range → preserve source gaps and keep
645
674
  for (const op of ["copy", "move"] as const) {
646
675
  const result = applyEdits(
647
676
  text,
648
- [{ op, start: at(text, 1), end: at(text, 3), after: at(text, 4) }],
677
+ [{ op, anchor: at(text, 1), end: at(text, 3), after: at(text, 4) }],
649
678
  4,
650
679
  0,
651
680
  );
@@ -658,7 +687,7 @@ test("copy/move unterminated final line → add interior connectors and preserve
658
687
  for (const ending of ["\n", "\r\n"]) {
659
688
  const text = `A${ending}B`;
660
689
  for (const op of ["copy", "move"] as const) {
661
- const result = applyEdits(text, [{ op, start: at(text, 2), before: at(text, 1) }], 4, 0);
690
+ const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
662
691
  assert(result.ok);
663
692
  assert.equal(result.text, op === "copy" ? `B${ending}A${ending}B` : `B${ending}A`);
664
693
  }
@@ -668,7 +697,7 @@ test("copy/move unterminated final line → add interior connectors and preserve
668
697
  test("copy/move line ending in standalone CR → connector retains the CR as line content", () => {
669
698
  const text = "A\nB\r";
670
699
  for (const op of ["copy", "move"] as const) {
671
- const result = applyEdits(text, [{ op, start: at(text, 2), before: at(text, 1) }], 4, 0);
700
+ const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
672
701
  assert(result.ok);
673
702
  assert.equal(result.text, op === "copy" ? "B\r\r\nA\nB\r" : "B\r\r\nA");
674
703
  assert.equal(splitLines(result.text)[0], "B\r");
@@ -679,7 +708,7 @@ test("copy Unicode and standalone-CR content → preserve every source code unit
679
708
  const text = "🙂中文 e\u0301\nkeep\rcontent\n";
680
709
  const result = applyEdits(
681
710
  text,
682
- [{ op: "copy", start: at(text, 1), end: at(text, 2), after: at(text, 2) }],
711
+ [{ op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 2) }],
683
712
  4,
684
713
  0,
685
714
  );
@@ -694,7 +723,7 @@ test("copy blank or BOM-only line → retain the added logical blank line", () =
694
723
  ] as const) {
695
724
  const result = applyEdits(
696
725
  text,
697
- [{ op: "copy", start: at(text, line), after: at(text, line) }],
726
+ [{ op: "copy", anchor: at(text, line), after: at(text, line) }],
698
727
  4,
699
728
  0,
700
729
  );
@@ -709,7 +738,7 @@ test("move to either adjacent gap → byte-identical no-op", () => {
709
738
  for (const destination of [{ before: at(text, 2) }, { after: at(text, 3) }]) {
710
739
  const result = applyEdits(
711
740
  text,
712
- [{ op: "move", start: at(text, 2), end: at(text, 3), ...destination }],
741
+ [{ op: "move", anchor: at(text, 2), end: at(text, 3), ...destination }],
713
742
  4,
714
743
  0,
715
744
  );
@@ -729,7 +758,7 @@ test("copy/move bad source and destination anchors → aggregate every supplied
729
758
  [
730
759
  {
731
760
  op: "copy",
732
- start: { line: 1, hash: "XXXX" },
761
+ anchor: { line: 1, hash: "XXXX" },
733
762
  end: { line: 2, hash: "XXXX" },
734
763
  ...destination,
735
764
  },
@@ -746,23 +775,32 @@ test("copy/move bad source and destination anchors → aggregate every supplied
746
775
  }
747
776
  });
748
777
 
749
- test("move interior destination, overlapping moves, or duplicate copy gaps → reject the complete batch", () => {
778
+ test("move interior destination or reversed capture range → reject invalid ranges", () => {
750
779
  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);
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
+ });