@criogaid/pi-hashline-edit 0.3.1 → 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.
- package/README.md +58 -49
- package/package.json +8 -3
- package/src/core/apply.test.ts +366 -310
- package/src/core/apply.ts +269 -202
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +45 -16
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +28 -9
- package/src/core/types.ts +54 -36
- package/src/index.ts +22 -16
- package/src/integration/grep-rg.test.ts +834 -219
- package/src/integration/large-text.test.ts +62 -0
- package/src/integration/replace-package.test.ts +37 -0
- package/src/pi/action-fusion.test.ts +535 -206
- package/src/pi/action-fusion.ts +469 -244
- package/src/pi/anchor-format.test.ts +30 -19
- package/src/pi/anchor-format.ts +27 -14
- package/src/pi/budgets.ts +20 -0
- package/src/pi/config.test.ts +60 -30
- package/src/pi/config.ts +45 -32
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +359 -0
- package/src/pi/edit-tool.ts +426 -262
- package/src/pi/error-text.ts +21 -0
- package/src/pi/execute.test.ts +1593 -712
- package/src/pi/failure-context.test.ts +105 -83
- package/src/pi/failure-context.ts +83 -71
- package/src/pi/failure-path.test.ts +171 -64
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +347 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +120 -85
- package/src/pi/grep-output.ts +208 -0
- package/src/pi/grep-render.ts +58 -0
- package/src/pi/grep-scope.ts +152 -0
- package/src/pi/grep-search.ts +217 -0
- package/src/pi/grep-tool.ts +191 -766
- package/src/pi/grep.test.ts +562 -150
- package/src/pi/mutation-result.test.ts +37 -0
- package/src/pi/mutation-result.ts +107 -59
- package/src/pi/mutation-runner.ts +168 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +95 -52
- package/src/pi/read-tool.ts +215 -132
- package/src/pi/registration.test.ts +274 -113
- package/src/pi/render.test.ts +218 -83
- package/src/pi/render.ts +152 -95
- package/src/pi/replace-apply.ts +109 -0
- package/src/pi/replace-tool.ts +280 -240
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +773 -389
- package/src/pi/result-safety.test.ts +779 -342
- package/src/pi/rg-line-filter.test.ts +64 -36
- package/src/pi/rg-line-filter.ts +40 -124
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +44 -82
- package/src/pi/rg-text-view.ts +106 -36
- package/src/pi/schema.ts +27 -0
- package/src/pi/state.ts +7 -7
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +115 -52
- package/src/pi/write.test.ts +248 -96
package/src/core/apply.test.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
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
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
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
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
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
|
});
|