@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/hash.ts
CHANGED
|
@@ -28,25 +28,54 @@ const BASE32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
|
28
28
|
* distributed, non-cryptographic. Uses `Math.imul` for correct 32-bit integer
|
|
29
29
|
* multiplication under JS.
|
|
30
30
|
*/
|
|
31
|
-
function fnv1a32(str: string): number {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
function fnv1a32(str: string, seed = 0x811c9dc5): number {
|
|
32
|
+
let h = seed;
|
|
33
|
+
for (let i = 0; i < str.length; i++) {
|
|
34
|
+
h ^= str.charCodeAt(i);
|
|
35
|
+
h = Math.imul(h, 0x01000193);
|
|
36
|
+
}
|
|
37
|
+
return h >>> 0;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Compute the FNV-1a 32-bit hash of `${line}\n${content}` without allocating
|
|
42
|
+
* the intermediate template string.
|
|
43
|
+
*/
|
|
44
|
+
function fnv1a32Line(line: number, content: string): number {
|
|
45
|
+
let h = 0x811c9dc5;
|
|
46
|
+
// Feed digit characters of the line number without String(line) allocation.
|
|
47
|
+
let n = line;
|
|
48
|
+
let divisor = 1;
|
|
49
|
+
while (divisor * 10 <= n) divisor *= 10;
|
|
50
|
+
while (divisor >= 1) {
|
|
51
|
+
h ^= (((n / divisor) | 0) % 10) + 48;
|
|
52
|
+
h = Math.imul(h, 0x01000193);
|
|
53
|
+
divisor = (divisor / 10) | 0;
|
|
54
|
+
}
|
|
55
|
+
h ^= 10;
|
|
56
|
+
h = Math.imul(h, 0x01000193);
|
|
57
|
+
return fnv1a32(content, h);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Encode a 32-bit integer into a base32 string of the given length.
|
|
62
|
+
*
|
|
63
|
+
* Seven characters expose all 32 bits; the leading character contains only
|
|
64
|
+
* two bits. An eighth character is a leading '0', adding no entropy.
|
|
65
|
+
*/
|
|
41
66
|
function toBase32(n: number, len: number): string {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
67
|
+
let encoded = "";
|
|
68
|
+
for (let i = 0; i < len; i++) {
|
|
69
|
+
encoded = BASE32[n & 31] + encoded;
|
|
70
|
+
n >>>= 5;
|
|
71
|
+
}
|
|
72
|
+
return encoded;
|
|
48
73
|
}
|
|
49
74
|
|
|
75
|
+
/** Supported hash lengths: two characters minimum; eight is the widest useful encoding. */
|
|
76
|
+
export const HASH_LEN_MIN = 2;
|
|
77
|
+
export const HASH_LEN_MAX = 8;
|
|
78
|
+
|
|
50
79
|
/**
|
|
51
80
|
* Compute the hash of a single line from its 1-based line number and content.
|
|
52
81
|
*
|
|
@@ -55,10 +84,10 @@ function toBase32(n: number, len: number): string {
|
|
|
55
84
|
* @param len hash length (default 4, 20 bits ≈ 1M values)
|
|
56
85
|
*/
|
|
57
86
|
export function computeLineHash(line: number, content: string, len = 4): string {
|
|
58
|
-
|
|
87
|
+
return toBase32(fnv1a32Line(line, content), len);
|
|
59
88
|
}
|
|
60
89
|
|
|
61
90
|
/** Compute compact per-line checksums; callers must treat collisions as possible. */
|
|
62
91
|
export function hashFileLines(lines: readonly string[], len = 4): string[] {
|
|
63
|
-
|
|
92
|
+
return lines.map((content, i) => computeLineHash(i + 1, content, len));
|
|
64
93
|
}
|
package/src/core/lines.test.ts
CHANGED
|
@@ -1,44 +1,63 @@
|
|
|
1
1
|
import { test } from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
createLfTextView,
|
|
5
|
+
normalizeLineEndings,
|
|
6
|
+
splitLines,
|
|
7
|
+
detectLineEnding,
|
|
8
|
+
hasFinalNewline,
|
|
9
|
+
} from "./lines.ts";
|
|
4
10
|
|
|
5
11
|
test("splitLines edge cases", () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
assert.deepEqual(splitLines(""), []);
|
|
13
|
+
assert.deepEqual(splitLines("a"), ["a"]);
|
|
14
|
+
assert.deepEqual(splitLines("a\nb"), ["a", "b"]);
|
|
15
|
+
assert.deepEqual(splitLines("a\nb\n"), ["a", "b"]);
|
|
16
|
+
assert.deepEqual(splitLines("a\n\n"), ["a", ""]);
|
|
17
|
+
assert.deepEqual(splitLines("\n"), [""]);
|
|
12
18
|
});
|
|
13
19
|
|
|
14
20
|
test("splitLines strips CRLF \\r", () => {
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
assert.deepEqual(splitLines("a\r\nb\r\n"), ["a", "b"]);
|
|
22
|
+
assert.deepEqual(splitLines("a\r\nb"), ["a", "b"]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("splitLines handles mixed line endings and standalone CR correctly", () => {
|
|
26
|
+
assert.deepEqual(splitLines("a\r\nb\nc\r\n"), ["a", "b", "c"]);
|
|
27
|
+
assert.deepEqual(splitLines("a\rb\r\n"), ["a\rb"]);
|
|
28
|
+
assert.deepEqual(splitLines("a\rb\nc"), ["a\rb", "c"]);
|
|
29
|
+
assert.deepEqual(splitLines("\r\n"), [""]);
|
|
30
|
+
assert.deepEqual(splitLines("\r\n\r\n"), ["", ""]);
|
|
31
|
+
assert.deepEqual(splitLines("a\r\n\r\nb"), ["a", "", "b"]);
|
|
32
|
+
assert.deepEqual(splitLines("a\r\n\r\n"), ["a", ""]);
|
|
17
33
|
});
|
|
18
34
|
|
|
19
35
|
test("hasFinalNewline", () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
assert.equal(hasFinalNewline("a\nb"), false);
|
|
37
|
+
assert.equal(hasFinalNewline("a\nb\n"), true);
|
|
38
|
+
assert.equal(hasFinalNewline("a\r\nb"), false);
|
|
39
|
+
assert.equal(hasFinalNewline("a\r\nb\r\n"), true);
|
|
40
|
+
assert.equal(hasFinalNewline("a"), false);
|
|
41
|
+
// no lines, no terminator — rejoining [] yields "" either way
|
|
42
|
+
assert.equal(hasFinalNewline(""), true);
|
|
27
43
|
});
|
|
28
44
|
|
|
29
45
|
test("detectLineEnding", () => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
assert.equal(detectLineEnding("a\nb\n"), "lf");
|
|
47
|
+
assert.equal(detectLineEnding("a\r\nb\r\n"), "crlf");
|
|
48
|
+
assert.equal(detectLineEnding("a\nb\r\nc\n"), "crlf");
|
|
33
49
|
});
|
|
34
50
|
|
|
35
51
|
test("LF views map every UTF-16 boundary back to original CRLF bytes", () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
52
|
+
for (const source of ["", "a\nb", "\uFEFF😀\r\nb\nc\r\n", "\r\n\r\n", "a\rb\r\r\nc"]) {
|
|
53
|
+
const view = createLfTextView(source);
|
|
54
|
+
assert.equal(view.text, normalizeLineEndings(source));
|
|
55
|
+
assert.equal(view.sourceOffset(view.text.length), source.length);
|
|
56
|
+
for (let offset = 0; offset <= view.text.length; offset++) {
|
|
57
|
+
assert.equal(
|
|
58
|
+
normalizeLineEndings(source.slice(0, view.sourceOffset(offset))),
|
|
59
|
+
view.text.slice(0, offset),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
44
63
|
});
|
package/src/core/lines.ts
CHANGED
|
@@ -20,44 +20,48 @@ import type { LineEnding } from "./types.ts";
|
|
|
20
20
|
|
|
21
21
|
/** Normalize CRLF boundaries to LF; standalone CR remains content. */
|
|
22
22
|
export function normalizeLineEndings(text: string): string {
|
|
23
|
-
|
|
23
|
+
return text.replace(/\r\n/g, "\n");
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/** Raw separators in logical-line order, shared by both mutation applicators. */
|
|
27
27
|
export function lineSeparators(text: string): string[] {
|
|
28
|
-
|
|
28
|
+
return text.match(/\r?\n/g) ?? [];
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/** New internal gaps reuse the corresponding old gap, then the last gap or file style. */
|
|
32
|
-
export function replacementSeparator(
|
|
33
|
-
|
|
32
|
+
export function replacementSeparator(
|
|
33
|
+
separators: readonly string[],
|
|
34
|
+
index: number,
|
|
35
|
+
fallback: string,
|
|
36
|
+
): string {
|
|
37
|
+
return separators[index] || separators[separators.length - 1] || fallback;
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
/** Restore a logical replacement without normalizing it a second time. */
|
|
37
41
|
export function restoreLineEndings(logical: string, original: string, fallback: string): string {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
const separators = lineSeparators(original);
|
|
43
|
+
let index = 0;
|
|
44
|
+
return logical.replace(/\n/g, () => replacementSeparator(separators, index++, fallback));
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
/** Build an LF matching view with UTF-16 boundary offsets back into the original text. */
|
|
44
48
|
export function createLfTextView(source: string) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
const removed: number[] = [];
|
|
50
|
+
for (const match of source.matchAll(/\r\n/g)) removed.push(match.index! - removed.length);
|
|
51
|
+
return {
|
|
52
|
+
text: normalizeLineEndings(source),
|
|
53
|
+
sourceOffset(offset: number): number {
|
|
54
|
+
// Count removed CRs strictly before this boundary: a newline starts at its original CR.
|
|
55
|
+
let lo = 0;
|
|
56
|
+
let hi = removed.length;
|
|
57
|
+
while (lo < hi) {
|
|
58
|
+
const mid = Math.floor((lo + hi) / 2);
|
|
59
|
+
if (removed[mid] < offset) lo = mid + 1;
|
|
60
|
+
else hi = mid;
|
|
61
|
+
}
|
|
62
|
+
return offset + lo;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
/**
|
|
@@ -71,14 +75,18 @@ export function createLfTextView(source: string) {
|
|
|
71
75
|
* - `""` → `[]`
|
|
72
76
|
*/
|
|
73
77
|
export function splitLines(text: string): string[] {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
if (text === "") return [];
|
|
79
|
+
if (!text.includes("\r")) {
|
|
80
|
+
return (text.endsWith("\n") ? text.slice(0, -1) : text).split("\n");
|
|
81
|
+
}
|
|
82
|
+
const normalized = text.replaceAll("\r\n", "\n");
|
|
83
|
+
const lines = (normalized.endsWith("\n") ? normalized.slice(0, -1) : normalized).split("\n");
|
|
84
|
+
return lines;
|
|
77
85
|
}
|
|
78
86
|
|
|
79
87
|
/** Whether the text uses CRLF at all (any `\r\n` counts; mixed files report "crlf"). */
|
|
80
88
|
export function detectLineEnding(text: string): LineEnding {
|
|
81
|
-
|
|
89
|
+
return text.includes("\r\n") ? "crlf" : "lf";
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
/**
|
|
@@ -90,5 +98,5 @@ export function detectLineEnding(text: string): LineEnding {
|
|
|
90
98
|
* round-trips.
|
|
91
99
|
*/
|
|
92
100
|
export function hasFinalNewline(text: string): boolean {
|
|
93
|
-
|
|
101
|
+
return text === "" || text.endsWith("\n");
|
|
94
102
|
}
|
package/src/core/ranges.test.ts
CHANGED
|
@@ -3,26 +3,80 @@ import { test } from "node:test";
|
|
|
3
3
|
import { findSortedRangeConflict, mergeRanges, type HalfOpenRange } from "./ranges.ts";
|
|
4
4
|
|
|
5
5
|
test("conflicts distinguish adjacent ranges from insertions at starts and interiors", () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
const cases: [HalfOpenRange[], number | undefined][] = [
|
|
7
|
+
[[], undefined],
|
|
8
|
+
[
|
|
9
|
+
[
|
|
10
|
+
[0, 2],
|
|
11
|
+
[2, 4],
|
|
12
|
+
],
|
|
13
|
+
undefined,
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
[
|
|
17
|
+
[0, 2],
|
|
18
|
+
[2, 2],
|
|
19
|
+
],
|
|
20
|
+
undefined,
|
|
21
|
+
],
|
|
22
|
+
[
|
|
23
|
+
[
|
|
24
|
+
[0, 0],
|
|
25
|
+
[0, 0],
|
|
26
|
+
],
|
|
27
|
+
1,
|
|
28
|
+
],
|
|
29
|
+
[
|
|
30
|
+
[
|
|
31
|
+
[0, 0],
|
|
32
|
+
[0, 2],
|
|
33
|
+
],
|
|
34
|
+
1,
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
[
|
|
38
|
+
[0, 3],
|
|
39
|
+
[1, 1],
|
|
40
|
+
],
|
|
41
|
+
1,
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
[
|
|
45
|
+
[0, 3],
|
|
46
|
+
[1, 2],
|
|
47
|
+
],
|
|
48
|
+
1,
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
[
|
|
52
|
+
[0, 1],
|
|
53
|
+
[1, 2],
|
|
54
|
+
[1, 3],
|
|
55
|
+
],
|
|
56
|
+
2,
|
|
57
|
+
],
|
|
58
|
+
];
|
|
59
|
+
for (const [ranges, expected] of cases) {
|
|
60
|
+
assert.equal(findSortedRangeConflict(ranges), expected);
|
|
61
|
+
assert.equal(
|
|
62
|
+
findSortedRangeConflict(ranges.map(([start, end]) => [start + 10, end + 10])),
|
|
63
|
+
expected,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
20
66
|
});
|
|
21
67
|
|
|
22
68
|
test("range merging preserves input and coalesces overlap, containment, and adjacency", () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
69
|
+
const ranges: HalfOpenRange[] = [
|
|
70
|
+
[7, 9],
|
|
71
|
+
[1, 3],
|
|
72
|
+
[2, 3],
|
|
73
|
+
[3, 5],
|
|
74
|
+
];
|
|
75
|
+
const before = structuredClone(ranges);
|
|
76
|
+
assert.deepEqual(mergeRanges(ranges), [
|
|
77
|
+
[1, 5],
|
|
78
|
+
[7, 9],
|
|
79
|
+
]);
|
|
80
|
+
assert.deepEqual(ranges, before);
|
|
81
|
+
assert.deepEqual(mergeRanges([]), []);
|
|
28
82
|
});
|
package/src/core/ranges.ts
CHANGED
|
@@ -3,23 +3,23 @@ export type HalfOpenRange = readonly [start: number, end: number];
|
|
|
3
3
|
|
|
4
4
|
/** Find the first conflicting pair's second index. Input must be sorted by start, then end. */
|
|
5
5
|
export function findSortedRangeConflict(ranges: readonly HalfOpenRange[]): number | undefined {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
7
|
+
const previous = ranges[i - 1];
|
|
8
|
+
const current = ranges[i];
|
|
9
|
+
// Insertions conflict at a shared start or inside a range, but not at its end.
|
|
10
|
+
if (current[0] < previous[1] || current[0] === previous[0]) return i;
|
|
11
|
+
}
|
|
12
|
+
return undefined;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/** Merge overlapping or adjacent ranges; callers validate their own units and bounds. */
|
|
16
16
|
export function mergeRanges(ranges: readonly HalfOpenRange[]): HalfOpenRange[] {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
const sorted = [...ranges].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
18
|
+
const result: HalfOpenRange[] = [];
|
|
19
|
+
for (const range of sorted) {
|
|
20
|
+
const previous = result.at(-1);
|
|
21
|
+
if (!previous || range[0] > previous[1]) result.push(range);
|
|
22
|
+
else if (range[1] > previous[1]) result[result.length - 1] = [previous[0], range[1]];
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
25
|
}
|
package/src/core/text.test.ts
CHANGED
|
@@ -3,13 +3,13 @@ import assert from "node:assert/strict";
|
|
|
3
3
|
import { decodeEditableText, decodeUtf8 } from "./text.ts";
|
|
4
4
|
|
|
5
5
|
test("decodeUtf8 rejects malformed input instead of replacing bytes", () => {
|
|
6
|
-
|
|
6
|
+
assert.throws(() => decodeUtf8(Uint8Array.from([0xc3, 0x28])), /UNSUPPORTED_ENCODING/);
|
|
7
7
|
});
|
|
8
8
|
|
|
9
9
|
test("decodeUtf8 preserves a leading BOM", () => {
|
|
10
|
-
|
|
10
|
+
assert.equal(decodeUtf8(Uint8Array.from([0xef, 0xbb, 0xbf, 0x61])), "\ufeffa");
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
test("decodeEditableText rejects NUL bytes", () => {
|
|
14
|
-
|
|
14
|
+
assert.throws(() => decodeEditableText(Uint8Array.from([0x61, 0, 0x62])), /UNSUPPORTED_TEXT/);
|
|
15
15
|
});
|
package/src/core/text.ts
CHANGED
|
@@ -1,23 +1,42 @@
|
|
|
1
1
|
import { TextDecoder } from "node:util";
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
export const UNSUPPORTED_ENCODING = "UNSUPPORTED_ENCODING: expected valid UTF-8.";
|
|
4
|
+
export const UNSUPPORTED_TEXT_NUL = "UNSUPPORTED_TEXT: NUL bytes are not editable.";
|
|
5
|
+
export const INVALID_UNICODE = "INVALID_UNICODE: content cannot be encoded losslessly as UTF-8.";
|
|
6
|
+
|
|
7
|
+
/** Why text cannot be written as editable UTF-8 (NUL or unpaired surrogates); undefined when it can. */
|
|
8
|
+
export function unwritableTextReason(text: string): string | undefined {
|
|
9
|
+
if (text.includes("\0")) return UNSUPPORTED_TEXT_NUL;
|
|
10
|
+
if (!text.isWellFormed()) return INVALID_UNICODE;
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Create an independent UTF-8 decoder; streaming calls preserve split code points and the BOM. */
|
|
15
|
+
export function createUtf8Decoder() {
|
|
16
|
+
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
17
|
+
return (bytes?: Uint8Array, stream = false): string => {
|
|
18
|
+
try {
|
|
19
|
+
return decoder.decode(bytes, { stream });
|
|
20
|
+
} catch (error) {
|
|
21
|
+
throw new Error(UNSUPPORTED_ENCODING, { cause: error });
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const decode = createUtf8Decoder();
|
|
4
27
|
|
|
5
28
|
/** Decode UTF-8 without replacing malformed bytes; preserve a leading BOM for byte-stable rewrites. */
|
|
6
29
|
export function decodeUtf8(bytes: Uint8Array): string {
|
|
7
|
-
|
|
8
|
-
return UTF8_DECODER.decode(bytes);
|
|
9
|
-
} catch (error) {
|
|
10
|
-
throw new Error("UNSUPPORTED_ENCODING: expected valid UTF-8.", { cause: error });
|
|
11
|
-
}
|
|
30
|
+
return decode(bytes);
|
|
12
31
|
}
|
|
13
32
|
|
|
14
33
|
/** Reject byte-oriented/binary content before entering a text mutation pipeline. */
|
|
15
34
|
export function decodeEditableText(bytes: Uint8Array): string {
|
|
16
|
-
|
|
17
|
-
|
|
35
|
+
if (bytes.includes(0)) throw new Error(UNSUPPORTED_TEXT_NUL);
|
|
36
|
+
return decodeUtf8(bytes);
|
|
18
37
|
}
|
|
19
38
|
|
|
20
39
|
/** Escape regex metacharacters for a standalone literal pattern in JS or ripgrep. */
|
|
21
40
|
export function escapeRegex(text: string): string {
|
|
22
|
-
|
|
41
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
23
42
|
}
|
package/src/core/types.ts
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
/** Line anchor: dual reference of line number (1-based) + content hash. */
|
|
8
8
|
export interface Anchor {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
readonly line: number;
|
|
10
|
+
readonly hash: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -16,12 +16,17 @@ export interface Anchor {
|
|
|
16
16
|
* address is still what was read; both must match at apply time.
|
|
17
17
|
*/
|
|
18
18
|
export type Edit =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
| {
|
|
20
|
+
readonly op: "replace";
|
|
21
|
+
readonly start: Anchor;
|
|
22
|
+
readonly end?: Anchor;
|
|
23
|
+
readonly body: string[];
|
|
24
|
+
}
|
|
25
|
+
| { readonly op: "delete"; readonly start: Anchor; readonly end?: Anchor }
|
|
26
|
+
| { readonly op: "insert_after"; readonly anchor: Anchor; readonly body: string[] }
|
|
27
|
+
| { readonly op: "insert_before"; readonly anchor: Anchor; readonly body: string[] }
|
|
28
|
+
| { readonly op: "append"; readonly body: string[] }
|
|
29
|
+
| { readonly op: "prepend"; readonly body: string[] };
|
|
25
30
|
|
|
26
31
|
export type LineEnding = "lf" | "crlf";
|
|
27
32
|
|
|
@@ -41,13 +46,18 @@ export type LineEnding = "lf" | "crlf";
|
|
|
41
46
|
* - `none` — no candidate found, or recovery disabled; re-read.
|
|
42
47
|
*/
|
|
43
48
|
export type AnchorRecovery =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
| {
|
|
50
|
+
readonly kind: "found";
|
|
51
|
+
readonly scope: "local" | "full-file";
|
|
52
|
+
readonly newLine: number;
|
|
53
|
+
readonly newHash: string;
|
|
54
|
+
}
|
|
55
|
+
| {
|
|
56
|
+
readonly kind: "ambiguous";
|
|
57
|
+
readonly scope: "local" | "full-file";
|
|
58
|
+
readonly candidates: ReadonlyArray<{ readonly line: number; readonly hash: string }>;
|
|
59
|
+
}
|
|
60
|
+
| { readonly kind: "none" };
|
|
51
61
|
|
|
52
62
|
/**
|
|
53
63
|
* A single anchor that failed verification, with its recovery attempt.
|
|
@@ -58,12 +68,12 @@ export type AnchorRecovery =
|
|
|
58
68
|
* out of range) — surfaced when recovery is `none` so the model can self-diagnose.
|
|
59
69
|
*/
|
|
60
70
|
export interface AnchorFailure {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
readonly opIndex: number;
|
|
72
|
+
readonly which: "anchor" | "end";
|
|
73
|
+
readonly op: Edit["op"];
|
|
74
|
+
readonly cited: Anchor;
|
|
75
|
+
readonly recovery: AnchorRecovery;
|
|
76
|
+
readonly current: { readonly hash: string; readonly content: string } | null;
|
|
67
77
|
}
|
|
68
78
|
|
|
69
79
|
/**
|
|
@@ -71,17 +81,25 @@ export interface AnchorFailure {
|
|
|
71
81
|
* not_checked means input validation rejected the batch before hashing.
|
|
72
82
|
*/
|
|
73
83
|
export interface AnchorCheck {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
84
|
+
readonly opIndex: number;
|
|
85
|
+
readonly which: "anchor" | "end";
|
|
86
|
+
readonly op: Edit["op"];
|
|
87
|
+
readonly cited: Anchor;
|
|
88
|
+
readonly status: "matched" | "mismatched" | "not_checked";
|
|
79
89
|
}
|
|
80
90
|
|
|
81
91
|
/** Batch-level failure. Anchor checks describe only checksum validation in this snapshot. */
|
|
82
92
|
export type ApplyFailure =
|
|
83
|
-
|
|
84
|
-
|
|
93
|
+
| {
|
|
94
|
+
readonly kind: "anchor";
|
|
95
|
+
readonly failures: readonly AnchorFailure[];
|
|
96
|
+
readonly checks: readonly AnchorCheck[];
|
|
97
|
+
}
|
|
98
|
+
| {
|
|
99
|
+
readonly kind: "input" | "range";
|
|
100
|
+
readonly message: string;
|
|
101
|
+
readonly checks: readonly AnchorCheck[];
|
|
102
|
+
};
|
|
85
103
|
|
|
86
104
|
/**
|
|
87
105
|
* Apply result. On success, `touchedLines` lists 0-based NEW-file indices to
|
|
@@ -93,11 +111,11 @@ export type ApplyFailure =
|
|
|
93
111
|
* Nothing is written on failure.
|
|
94
112
|
*/
|
|
95
113
|
export type ApplyResult =
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
114
|
+
| {
|
|
115
|
+
readonly ok: true;
|
|
116
|
+
readonly text: string;
|
|
117
|
+
readonly changed: boolean;
|
|
118
|
+
readonly touchedLines: readonly number[];
|
|
119
|
+
readonly contextLines: readonly number[];
|
|
120
|
+
}
|
|
121
|
+
| { readonly ok: false; readonly failure: ApplyFailure };
|