@criogaid/pi-hashline-edit 0.6.0 → 0.7.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 (148) hide show
  1. package/README.md +161 -304
  2. package/THIRD_PARTY_NOTICES.md +80 -0
  3. package/package.json +10 -6
  4. package/src/core/AGENTS.md +20 -0
  5. package/src/core/apply.test.ts +189 -60
  6. package/src/core/apply.ts +110 -74
  7. package/src/core/case-fold.test.ts +40 -0
  8. package/src/core/case-fold.ts +81 -0
  9. package/src/core/character-set.test.ts +103 -0
  10. package/src/core/character-set.ts +170 -0
  11. package/src/core/errors.test.ts +23 -0
  12. package/src/core/errors.ts +70 -4
  13. package/src/core/lines.ts +7 -0
  14. package/src/core/regex.ts +21 -0
  15. package/src/core/replace-schema.ts +34 -0
  16. package/src/core/replace.ts +37 -54
  17. package/src/core/search-analysis.test.ts +199 -0
  18. package/src/core/search-analysis.ts +375 -0
  19. package/src/core/search-matrix.test.ts +64 -0
  20. package/src/core/search-matrix.testing.ts +183 -0
  21. package/src/core/search-schema.ts +18 -0
  22. package/src/core/search.test.ts +145 -0
  23. package/src/core/search.ts +119 -0
  24. package/src/core/text.test.ts +16 -8
  25. package/src/core/text.ts +26 -6
  26. package/src/core/types.ts +16 -11
  27. package/src/index.ts +25 -15
  28. package/src/integration/config-registration.test.ts +30 -24
  29. package/src/integration/forget-grep.test.ts +52 -2
  30. package/src/integration/fusion-dispatch.test.ts +21 -105
  31. package/src/integration/grep-errors.test.ts +50 -0
  32. package/src/integration/grep-flags.test.ts +280 -0
  33. package/src/integration/grep-js-completeness.test.ts +208 -0
  34. package/src/integration/grep-line-candidates.test.ts +233 -0
  35. package/src/integration/grep-matrix.test.ts +121 -0
  36. package/src/integration/grep-path-matrix.test.ts +132 -0
  37. package/src/integration/grep-prefilter-coverage.test.ts +189 -0
  38. package/src/integration/grep-regex.test.ts +410 -0
  39. package/src/integration/grep-rg.test.ts +220 -402
  40. package/src/integration/grep-scan-budget.test.ts +89 -0
  41. package/src/integration/grep-translation.test.ts +206 -0
  42. package/src/integration/large-text.test.ts +57 -1
  43. package/src/integration/model-budget-lifecycle.test.ts +128 -0
  44. package/src/integration/mutation-file-types.test.ts +12 -25
  45. package/src/integration/mutation-file-types.testing.ts +33 -0
  46. package/src/integration/npm-package.test.ts +17 -0
  47. package/src/integration/npm-package.testing.ts +123 -0
  48. package/src/integration/publication-read-changes.test.ts +344 -0
  49. package/src/integration/read-changes.test.ts +98 -0
  50. package/src/integration/replace-package.test.ts +17 -36
  51. package/src/integration/replace-package.testing.ts +33 -0
  52. package/src/integration/replace-worker-boundary.test.ts +107 -0
  53. package/src/integration/report-replay.test.ts +90 -0
  54. package/src/integration/utf8-errors.test.ts +46 -0
  55. package/src/pi/AGENTS.md +55 -0
  56. package/src/pi/action-fusion.test.ts +253 -103
  57. package/src/pi/action-fusion.ts +116 -290
  58. package/src/pi/anchor-format.ts +9 -0
  59. package/src/pi/argument-diagnostics.ts +478 -0
  60. package/src/pi/argument-error.testing.ts +43 -0
  61. package/src/pi/argument-error.ts +78 -0
  62. package/src/pi/argument-format.test.ts +208 -0
  63. package/src/pi/argument-validation.test.ts +742 -45
  64. package/src/pi/argument-validation.ts +56 -59
  65. package/src/pi/budgets.ts +45 -16
  66. package/src/pi/codemode.test.ts +96 -0
  67. package/src/pi/config.test.ts +14 -10
  68. package/src/pi/config.ts +21 -7
  69. package/src/pi/diagnostic-buffer.test.ts +52 -16
  70. package/src/pi/diagnostic-buffer.ts +11 -14
  71. package/src/pi/directory-sync.test.ts +4 -1
  72. package/src/pi/edit-tool.ts +100 -93
  73. package/src/pi/error-text.ts +39 -16
  74. package/src/pi/execute.test.ts +362 -315
  75. package/src/pi/extension-loading.test.ts +14 -0
  76. package/src/pi/extension-loading.testing.ts +3 -0
  77. package/src/pi/failure-context.test.ts +220 -88
  78. package/src/pi/failure-context.ts +161 -180
  79. package/src/pi/failure-path.test.ts +85 -88
  80. package/src/pi/file-commit.test.ts +78 -45
  81. package/src/pi/file-commit.ts +218 -151
  82. package/src/pi/file-read.test.ts +29 -0
  83. package/src/pi/file-read.ts +60 -0
  84. package/src/pi/file-snapshot.test.ts +1 -1
  85. package/src/pi/forget-tool.ts +189 -90
  86. package/src/pi/forget.test.ts +309 -22
  87. package/src/pi/forget.testing.ts +41 -113
  88. package/src/pi/fusion-card.test.ts +40 -14
  89. package/src/pi/fusion-card.ts +84 -50
  90. package/src/pi/grep-candidates.test.ts +127 -0
  91. package/src/pi/grep-candidates.ts +193 -0
  92. package/src/pi/grep-matrix.test.ts +83 -0
  93. package/src/pi/grep-output.ts +181 -138
  94. package/src/pi/grep-render.ts +28 -25
  95. package/src/pi/grep-scope.ts +61 -53
  96. package/src/pi/grep-search.ts +299 -242
  97. package/src/pi/grep-tool.ts +150 -140
  98. package/src/pi/grep-worker.mjs +4 -0
  99. package/src/pi/grep-worker.ts +14 -0
  100. package/src/pi/grep.test.ts +631 -632
  101. package/src/pi/model-budgets.test.ts +43 -0
  102. package/src/pi/model-budgets.ts +44 -0
  103. package/src/pi/mutation-outcome.testing.ts +5 -5
  104. package/src/pi/mutation-result.test.ts +21 -74
  105. package/src/pi/mutation-result.ts +74 -87
  106. package/src/pi/mutation-runner.ts +83 -75
  107. package/src/pi/output-budgets.test.ts +166 -0
  108. package/src/pi/path.ts +7 -0
  109. package/src/pi/read-tool.ts +151 -137
  110. package/src/pi/read.test.ts +39 -33
  111. package/src/pi/regex-worker-contract.ts +73 -0
  112. package/src/pi/regex-worker.ts +101 -0
  113. package/src/pi/registration.test.ts +111 -92
  114. package/src/pi/render.test.ts +193 -19
  115. package/src/pi/render.ts +137 -29
  116. package/src/pi/replace-regex.ts +11 -44
  117. package/src/pi/replace-tool.ts +38 -145
  118. package/src/pi/replace-worker-contract.ts +19 -0
  119. package/src/pi/replace-worker.ts +12 -8
  120. package/src/pi/replace.test.ts +93 -160
  121. package/src/pi/report-cause.ts +99 -0
  122. package/src/pi/report-failure.testing.ts +20 -0
  123. package/src/pi/report-schema.ts +512 -0
  124. package/src/pi/report.test.ts +276 -0
  125. package/src/pi/report.ts +288 -0
  126. package/src/pi/result-safety.test.ts +122 -427
  127. package/src/pi/rg-process.test.ts +78 -79
  128. package/src/pi/rg-process.ts +124 -177
  129. package/src/pi/rg-regex.test.ts +50 -0
  130. package/src/pi/rg-regex.ts +709 -0
  131. package/src/pi/text-stream.test.ts +67 -38
  132. package/src/pi/text-stream.ts +96 -75
  133. package/src/pi/tool-call.testing.ts +3 -39
  134. package/src/pi/write-tool.ts +45 -34
  135. package/src/pi/write.test.ts +45 -37
  136. package/src/testing/extension-loader.testing.ts +24 -0
  137. package/src/testing/installation.testing.ts +77 -0
  138. package/src/testing/process.testing.ts +33 -0
  139. package/src/testing/regex-host.testing.ts +20 -0
  140. package/src/testing/regex-translation-corpus.ts +308 -0
  141. package/src/testing/rg-line-listing.fixture.mjs +7 -0
  142. package/src/testing/run-tests.ts +47 -0
  143. package/src/testing/session.testing.ts +127 -0
  144. package/src/pi/rg-line-ranges.test.ts +0 -66
  145. package/src/pi/rg-line-ranges.ts +0 -75
  146. package/src/pi/rg-match-bytes.ts +0 -95
  147. package/src/pi/rg-text-view.ts +0 -192
  148. package/src/pi/tool-prompts.ts +0 -15
@@ -0,0 +1,80 @@
1
+ # Regex translation sources and notices
2
+
3
+ The JS-to-ripgrep candidate translator adapts the following analysis techniques to this project's byte-preserving reads and JavaScript final verification. These projects are references, not runtime dependencies. Unicode membership comes from the running JavaScript engine rather than copied Unicode tables.
4
+
5
+ | Project | Technique used here | Source license |
6
+ | --- | --- | --- |
7
+ | [regexpu-core](https://github.com/mathiasbynens/regexpu-core) | Separate scalar sets from class strings; apply scoped flags before lowering a set | MIT |
8
+ | [refa](https://github.com/RunDevelopment/refa/tree/8d20d94232b1adb043c29810046c060ca1664bf1) | Normalized character intervals and bounded finite languages | MIT |
9
+ | [regexp-ast-analysis](https://github.com/RunDevelopment/regexp-ast-analysis/tree/6f0dbfc42ccc4a3fd22da6e3917f4e8cef07bee8) | Track exactness and fixed-width neighbouring characters across assertions | MIT |
10
+ | [eslint-plugin-regexp](https://github.com/ota-meshi/eslint-plugin-regexp/tree/4700804dba99bed531e727f09c72a69a1dc939a4) | Character constraints for lookarounds and word boundaries | MIT |
11
+ | [regex-syntax](https://github.com/rust-lang/regex/blob/master/regex-syntax/src/hir/literal.rs) | Budget literal expansion; retain necessary prefixes and suffixes without dropping alternatives | MIT OR Apache-2.0 |
12
+ | [RE2](https://github.com/google/re2/blob/main/re2/prefilter.cc) | Necessary AND/OR literal conditions for file prefiltering | BSD-3-Clause |
13
+ | [Vectorscan](https://github.com/VectorCamp/vectorscan/blob/develop/src/parser/prefilter.cpp) | Widen unsupported assertions and backreferences while retaining regular constraints | BSD-3-Clause |
14
+
15
+ Production implementations live in `src/core/character-set.ts`, `src/core/search-analysis.ts`, `src/core/case-fold.ts`, `src/pi/rg-regex.ts`, and `src/pi/rg-process.ts`. In particular, this project does not copy an automaton engine, a Unicode version table, or upstream match semantics into its final verifier.
16
+
17
+ ## MIT reference projects
18
+
19
+ regexpu-core: Copyright Mathias Bynens <https://mathiasbynens.be/>
20
+
21
+ refa: Copyright (c) 2020 Michael Schmidt
22
+
23
+ regexp-ast-analysis: Copyright (c) 2021 Michael Schmidt
24
+
25
+ eslint-plugin-regexp: Copyright (c) 2020 Yosuke Ota
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining a copy
28
+ of this software and associated documentation files (the "Software"), to deal
29
+ in the Software without restriction, including without limitation the rights
30
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31
+ copies of the Software, and to permit persons to whom the Software is
32
+ furnished to do so, subject to the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be included in all
35
+ copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
+ SOFTWARE.
44
+
45
+ ## Test262 regression witnesses
46
+
47
+ `src/testing/regex-translation-corpus.ts` adapts selected patterns and witnesses from [Test262](https://github.com/tc39/test262). Each fixture identifies its upstream test file. The local assertions check candidate-line inclusion and final JS results rather than reproducing the Test262 harness.
48
+
49
+ Copyright 2022 Igalia S.L. All rights reserved.
50
+
51
+ Copyright (C) 2017 the V8 project authors. All rights reserved.
52
+
53
+ Copyright 2025 Mathias Bynens. All rights reserved.
54
+
55
+ Test262: ECMAScript Test Suite ("Software") is protected by copyright and is being
56
+ made available under the "BSD License", included below. This Software may be subject to third party rights (rights
57
+ from parties other than Ecma International), including patent rights, and no licenses under such third party rights
58
+ are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA
59
+ CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://www.ecma-international.org/ipr FOR
60
+ INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS*.
61
+
62
+ Copyright (C) 2012 Ecma International
63
+ All rights reserved.
64
+
65
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
66
+ following conditions are met:
67
+
68
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
69
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
70
+ 3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
71
+
72
+ THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
73
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
74
+ SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
75
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
77
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
78
+ DAMAGE.
79
+
80
+ * Ecma International Standards hereafter means Ecma International Standards as well as Ecma Technical Reports
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@criogaid/pi-hashline-edit",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "description": "Hashline-style file editing for pi — line-anchored edits verified by content hash, replacing oldText/newText matching",
6
6
  "homepage": "https://github.com/Criogaid/pi-hashline-edit#readme",
@@ -15,23 +15,27 @@
15
15
  "files": [
16
16
  "src",
17
17
  "README.md",
18
- "LICENSE"
18
+ "LICENSE",
19
+ "THIRD_PARTY_NOTICES.md"
19
20
  ],
20
21
  "scripts": {
21
22
  "format": "biome format --write src/ bench/",
22
23
  "format:check": "biome format src/ bench/",
23
24
  "typecheck": "tsc --noEmit",
24
- "test": "node --test src/core/*.test.ts src/pi/*.test.ts",
25
- "test:integration": "node --test src/integration/*.test.ts",
25
+ "test": "node src/testing/run-tests.ts unit",
26
+ "test:integration": "node src/testing/run-tests.ts integration",
26
27
  "test:all": "npm test && npm run test:integration",
28
+ "test:cartesian": "node src/testing/run-tests.ts cartesian",
27
29
  "bench": "node bench/throughput.bench.ts"
28
30
  },
29
31
  "dependencies": {
32
+ "@eslint-community/regexpp": "4.12.2",
30
33
  "@vscode/ripgrep": "1.18.0",
31
34
  "jiti": "2.7.0"
32
35
  },
33
36
  "peerDependencies": {
34
- "@earendil-works/pi-coding-agent": ">=0.99.1"
37
+ "@earendil-works/pi-coding-agent": ">=0.99.1",
38
+ "typebox": "*"
35
39
  },
36
40
  "devDependencies": {
37
41
  "@biomejs/biome": "2.5.14",
@@ -39,7 +43,7 @@
39
43
  "@earendil-works/pi-ai": "0.99.1",
40
44
  "@earendil-works/pi-coding-agent": "0.99.1",
41
45
  "@earendil-works/pi-tui": "0.99.1",
42
- "typebox": "^1.1.38",
46
+ "typebox": "1.3.27",
43
47
  "typescript": "^7.0.2"
44
48
  },
45
49
  "pi": {
@@ -0,0 +1,20 @@
1
+ # src/core
2
+
3
+ Internal pure engine: no Pi imports and no public API. It trusts input already filtered by the tool layer and does not re-validate it. Core's report references are type-only and erased at runtime.
4
+
5
+ ## Modules
6
+
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
+ - `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
+ - `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.
11
+ - `search.ts` evaluates native JavaScript queries on the LF view and returns bounded physical rows; `search-schema.ts` defines its result.
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
+ - `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
+ - `ranges.ts` provides overlap detection for both engines and range merging for grep and failure neighborhoods.
15
+
16
+ ## Constraints
17
+
18
+ - Plan `copy/move` in `apply.ts` against original line ranges; capture text and separators without moving or duplicating the file-header BOM. Treat source reads separately from mutation conflicts, keep destinations in original coordinates, reject interior move destinations, and make adjacent moves no-ops. Reuse the shared commit and Fusion pipeline.
19
+ - Own interval algebra, bounded membership caching, and explicit class-string enumeration in `character-set.ts`; derive Unicode properties, `v` sets, and case closure from the running V8 rather than separate Unicode tables. Intersections may enumerate either string operand, subtractions only the left, and unions require every operand. Keep original scoped case flags for membership even when translating a backreference; apply repetition case widening afterward through the existing byte translator. Unknown string properties remain widened.
20
+ - Derive necessary file conditions in `search-analysis.ts` as bounded AND/OR literal clauses. Preserve every OR alternative, retain necessary prefixes or suffixes when expansion is too large, and cut literals where bytes can differ from the LF view. Unknown constructs widen candidates, never narrow them.
@@ -91,41 +91,6 @@ test("multiple out-of-order ops → applied at the right positions", () => {
91
91
  if (r.ok) assert.equal(r.text, "A\nb\nc\nz\n");
92
92
  });
93
93
 
94
- test("touchedLines covers exactly the lines this edit produced (new-file indices)", () => {
95
- const text = "a\nb\nc\n";
96
- const r = applyEdits(
97
- text,
98
- [
99
- { op: "insert_after", anchor: at(text, 3), body: ["z"] },
100
- { op: "replace", start: at(text, 1), body: ["A"] },
101
- ],
102
- 4,
103
- 15,
104
- );
105
- assert.equal(r.ok, true);
106
- if (r.ok) {
107
- // new file: [A, b, c, z]; this edit produced line index 0 (A) and 3 (z)
108
- assert.deepEqual([...r.touchedLines], [0, 3]);
109
- }
110
- });
111
-
112
- test("touchedLines for append", () => {
113
- const text = "a\n";
114
- const r = applyEdits(text, [{ op: "append", body: ["b", "c"] }], 4, 15);
115
- assert.equal(r.ok, true);
116
- if (r.ok) assert.deepEqual([...r.touchedLines], [1, 2]);
117
- });
118
-
119
- test("touchedLines for delete re-anchors the line that shifted into the gap", () => {
120
- const text = "a\nb\nc\nd\n";
121
- const r = applyEdits(text, [{ op: "delete", start: at(text, 2) }], 4, 15);
122
- assert.equal(r.ok, true);
123
- if (r.ok) {
124
- // new file: [a, c, d]; deleting line 2 (b) shifts c into index 1
125
- assert.deepEqual([...r.touchedLines], [1]);
126
- }
127
- });
128
-
129
94
  // --- error paths ---
130
95
 
131
96
  test("anchor hash mismatch rejected (line changed)", () => {
@@ -207,11 +172,12 @@ test("conflict at the same insertion point rejected", () => {
207
172
  if (!r.ok) assert.equal(r.failure.kind, "range");
208
173
  });
209
174
 
210
- test("byte-identical edits succeed without updated anchors after validation", () => {
175
+ test("byte-identical edits → validate anchors and preserve the source bytes", () => {
211
176
  for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
212
177
  const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
213
178
  const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: [body] }], 4, 15);
214
- assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
179
+ assert.ok(r.ok);
180
+ assert.equal(r.text, text);
215
181
  const invalid = applyEdits(
216
182
  text,
217
183
  [{ op: "replace", start: at("wrong\nb\n", 1), body: [body] }],
@@ -310,23 +276,19 @@ test("edits retain empty final logical lines even without an original final newl
310
276
  const line = splitLines(text).length;
311
277
  const ending = text.includes("\r\n") ? "\r\n" : "\n";
312
278
  const prefix = text.slice(0, text.lastIndexOf("\n") + 1);
313
- const cases: [Edit, string, number[]][] = [
314
- [{ op: "replace", start: at(text, line), body: [""] }, prefix + ending, [line - 1]],
279
+ const cases: [Edit, string][] = [
280
+ [{ op: "replace", start: at(text, line), body: [""] }, prefix + ending],
315
281
  [
316
282
  { op: "replace", start: at(text, line), body: ["new", ""] },
317
283
  `${prefix}new${ending}${ending}`,
318
- [line - 1, line],
319
284
  ],
320
- [{ op: "append", body: [""] }, text + ending + ending, [line]],
321
- [{ op: "insert_after", anchor: at(text, line), body: [""] }, text + ending + ending, [line]],
285
+ [{ op: "append", body: [""] }, text + ending + ending],
286
+ [{ op: "insert_after", anchor: at(text, line), body: [""] }, text + ending + ending],
322
287
  ];
323
- for (const [edit, expected, touched] of cases) {
288
+ for (const [edit, expected] of cases) {
324
289
  const result = applyEdits(text, [edit], 4, 15);
325
290
  assert.ok(result.ok);
326
291
  assert.equal(result.text, expected, `${edit.op}: ${JSON.stringify(text)}`);
327
- assert.deepEqual(result.touchedLines, touched);
328
- const newLines = splitLines(result.text);
329
- assert.ok(result.touchedLines.every((index) => index < newLines.length));
330
292
  }
331
293
  }
332
294
  // Deleting an unterminated suffix must also retain an existing blank predecessor.
@@ -339,23 +301,21 @@ test("edits retain empty final logical lines even without an original final newl
339
301
  );
340
302
  assert.ok(result.ok);
341
303
  assert.equal(result.text, text.slice(0, -"last".length));
342
- assert.deepEqual(result.touchedLines, []);
343
304
  }
344
305
  });
345
306
 
346
307
  test("a BOM represents an empty first line without a terminator, but not an empty later line", () => {
347
- const cases: [string, Edit[], string, number[]][] = [
348
- ["\uFEFFold", [{ op: "replace", start: at("\uFEFFold", 1), body: [""] }], "\uFEFF", [0]],
349
- ["\uFEFF", [{ op: "replace", start: at("\uFEFF", 1), body: [""] }], "\uFEFF", []],
350
- ["\uFEFF", [{ op: "append", body: [""] }], "\uFEFF\n\n", [1]],
351
- ["\uFEFF", [{ op: "prepend", body: ["new"] }], "\uFEFFnew\n\n", [0]],
352
- ["\uFEFF\nlast", [{ op: "delete", start: at("\uFEFF\nlast", 2) }], "\uFEFF", []],
308
+ 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"],
311
+ ["\uFEFF", [{ op: "append", body: [""] }], "\uFEFF\n\n"],
312
+ ["\uFEFF", [{ op: "prepend", body: ["new"] }], "\uFEFFnew\n\n"],
313
+ ["\uFEFF\nlast", [{ op: "delete", start: at("\uFEFF\nlast", 2) }], "\uFEFF"],
353
314
  ];
354
- for (const [text, edits, expected, touched] of cases) {
315
+ for (const [text, edits, expected] of cases) {
355
316
  const result = applyEdits(text, edits, 4, 15);
356
317
  assert.ok(result.ok);
357
318
  assert.equal(result.text, expected, JSON.stringify({ text, edits }));
358
- assert.deepEqual(result.touchedLines, touched);
359
319
  }
360
320
  });
361
321
 
@@ -365,16 +325,13 @@ test("large replacement bodies retain snapshot boundaries without a function arg
365
325
  const result = applyEdits(text, [{ op: "replace", start: at(text, 2), body }], 4, 15);
366
326
  assert.ok(result.ok);
367
327
  assert.equal(result.text, `head\r\n${"x\r\n".repeat(body.length - 1)}x\ntail`);
368
- assert.equal(result.touchedLines.length, body.length);
369
- assert.equal(result.touchedLines[0], 1);
370
- assert.equal(result.touchedLines.at(-1), body.length);
371
- assert.deepEqual(result.contextLines, []);
372
328
  });
373
329
 
374
330
  test("noop is still detected when the file lacks a final newline", () => {
375
331
  const text = "a\nb";
376
332
  const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["a"] }], 4, 15);
377
- assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
333
+ assert.ok(r.ok);
334
+ assert.equal(r.text, text);
378
335
  });
379
336
 
380
337
  // --- shifted-anchor recovery ---
@@ -637,3 +594,175 @@ test("matched anchor checks do not imply valid ranges", () => {
637
594
  assert.ok(result.failure.checks.every((check) => check.status === "matched"));
638
595
  }
639
596
  });
597
+
598
+ test("copy/move original ranges → retain captured text and order", () => {
599
+ const text = "A\nB\nC\nD\n";
600
+ const cases: { edit: Edit; expected: string }[] = [
601
+ {
602
+ edit: { op: "copy", start: at(text, 2), end: at(text, 3), after: at(text, 4) },
603
+ expected: "A\nB\nC\nD\nB\nC\n",
604
+ },
605
+ {
606
+ edit: { op: "move", start: at(text, 1), end: at(text, 2), after: at(text, 4) },
607
+ expected: "C\nD\nA\nB\n",
608
+ },
609
+ {
610
+ edit: { op: "move", start: at(text, 3), end: at(text, 4), before: at(text, 1) },
611
+ expected: "C\nD\nA\nB\n",
612
+ },
613
+ {
614
+ edit: { op: "copy", start: at(text, 1), end: at(text, 3), after: at(text, 1) },
615
+ expected: "A\nA\nB\nC\nB\nC\nD\n",
616
+ },
617
+ ];
618
+ for (const { edit, expected } of cases) {
619
+ const result = applyEdits(text, [edit], 4, 0);
620
+ assert(result.ok);
621
+ assert.equal(result.text, expected);
622
+ }
623
+ });
624
+
625
+ test("copy source also replaced or deleted → capture original text regardless of batch order", () => {
626
+ 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) };
628
+ 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) },
631
+ ] satisfies Edit[]) {
632
+ for (const edits of [
633
+ [copy, edit],
634
+ [edit, copy],
635
+ ]) {
636
+ const result = applyEdits(text, edits, 4, 0);
637
+ assert(result.ok);
638
+ assert.equal(result.text, edit.op === "replace" ? "NEW\nC\nD\nA\nB\n" : "C\nD\nA\nB\n");
639
+ }
640
+ }
641
+ });
642
+
643
+ test("copy/move first-line mixed-ending range → preserve source gaps and keep one file BOM", () => {
644
+ const text = "\uFEFFA\r\nB\nC\r\nD\n";
645
+ for (const op of ["copy", "move"] as const) {
646
+ const result = applyEdits(
647
+ text,
648
+ [{ op, start: at(text, 1), end: at(text, 3), after: at(text, 4) }],
649
+ 4,
650
+ 0,
651
+ );
652
+ assert(result.ok);
653
+ assert.equal(result.text, op === "copy" ? text + "A\r\nB\nC\r\n" : "\uFEFFD\nA\r\nB\nC\r\n");
654
+ }
655
+ });
656
+
657
+ test("copy/move unterminated final line → add interior connectors and preserve final-newline state", () => {
658
+ for (const ending of ["\n", "\r\n"]) {
659
+ const text = `A${ending}B`;
660
+ for (const op of ["copy", "move"] as const) {
661
+ const result = applyEdits(text, [{ op, start: at(text, 2), before: at(text, 1) }], 4, 0);
662
+ assert(result.ok);
663
+ assert.equal(result.text, op === "copy" ? `B${ending}A${ending}B` : `B${ending}A`);
664
+ }
665
+ }
666
+ });
667
+
668
+ test("copy/move line ending in standalone CR → connector retains the CR as line content", () => {
669
+ const text = "A\nB\r";
670
+ for (const op of ["copy", "move"] as const) {
671
+ const result = applyEdits(text, [{ op, start: at(text, 2), before: at(text, 1) }], 4, 0);
672
+ assert(result.ok);
673
+ assert.equal(result.text, op === "copy" ? "B\r\r\nA\nB\r" : "B\r\r\nA");
674
+ assert.equal(splitLines(result.text)[0], "B\r");
675
+ }
676
+ });
677
+
678
+ test("copy Unicode and standalone-CR content → preserve every source code unit", () => {
679
+ const text = "🙂中文 e\u0301\nkeep\rcontent\n";
680
+ const result = applyEdits(
681
+ text,
682
+ [{ op: "copy", start: at(text, 1), end: at(text, 2), after: at(text, 2) }],
683
+ 4,
684
+ 0,
685
+ );
686
+ assert(result.ok);
687
+ assert.equal(result.text, text + text);
688
+ });
689
+
690
+ test("copy blank or BOM-only line → retain the added logical blank line", () => {
691
+ for (const [text, line, expected] of [
692
+ ["A\n\n", 2, "A\n\n\n"],
693
+ ["\uFEFF", 1, "\uFEFF\n\n"],
694
+ ] as const) {
695
+ const result = applyEdits(
696
+ text,
697
+ [{ op: "copy", start: at(text, line), after: at(text, line) }],
698
+ 4,
699
+ 0,
700
+ );
701
+ assert(result.ok);
702
+ assert.equal(result.text, expected);
703
+ assert.equal(splitLines(result.text).length, splitLines(text).length + 1);
704
+ }
705
+ });
706
+
707
+ test("move to either adjacent gap → byte-identical no-op", () => {
708
+ const text = "A\r\nB\nC\r\nD";
709
+ for (const destination of [{ before: at(text, 2) }, { after: at(text, 3) }]) {
710
+ const result = applyEdits(
711
+ text,
712
+ [{ op: "move", start: at(text, 2), end: at(text, 3), ...destination }],
713
+ 4,
714
+ 0,
715
+ );
716
+ assert(result.ok);
717
+ assert.equal(result.text, text);
718
+ }
719
+ });
720
+
721
+ test("copy/move bad source and destination anchors → aggregate every supplied field", () => {
722
+ const text = "A\nB\nC\nD\n";
723
+ for (const destination of [
724
+ { before: { line: 4, hash: "XXXX" } },
725
+ { after: { line: 4, hash: "XXXX" } },
726
+ ]) {
727
+ const result = applyEdits(
728
+ text,
729
+ [
730
+ {
731
+ op: "copy",
732
+ start: { line: 1, hash: "XXXX" },
733
+ end: { line: 2, hash: "XXXX" },
734
+ ...destination,
735
+ },
736
+ ],
737
+ 4,
738
+ 0,
739
+ );
740
+ assert(!result.ok && result.failure.kind === "anchor");
741
+ assert.deepEqual(
742
+ result.failure.failures.map((failure) => failure.which),
743
+ ["anchor", "end", Object.keys(destination)[0]],
744
+ );
745
+ assert(result.failure.checks.every((check) => check.status === "mismatched"));
746
+ }
747
+ });
748
+
749
+ test("move interior destination, overlapping moves, or duplicate copy gaps → reject the complete batch", () => {
750
+ 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);
765
+ assert(!result.ok && result.failure.kind === "range");
766
+ assert(result.failure.checks.every((check) => check.status === "matched"));
767
+ }
768
+ });