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