@criogaid/pi-hashline-edit 0.6.1 → 0.8.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 +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { searchLines, searchText } from "./search.ts";
|
|
4
|
+
import { unsupportedByHost } from "../testing/regex-host.testing.ts";
|
|
5
|
+
|
|
6
|
+
const cases = [
|
|
7
|
+
{ name: "dot counts UTF-16 units", pattern: "^..$", source: "😀\na\nab\n", lines: [1, 3] },
|
|
8
|
+
{
|
|
9
|
+
name: "negated class counts UTF-16 units",
|
|
10
|
+
pattern: "^[^x]{2}$",
|
|
11
|
+
source: "😀\nab\nx😀\n",
|
|
12
|
+
lines: [1, 2],
|
|
13
|
+
},
|
|
14
|
+
{ name: "surrogate half in a class", pattern: "[😀]", source: "😀\nx\n", lines: [1] },
|
|
15
|
+
{
|
|
16
|
+
name: "repeated capture resets",
|
|
17
|
+
pattern: "^(?:(a)|b)+\\1$",
|
|
18
|
+
source: "ab\naba\naa\n",
|
|
19
|
+
lines: [1, 3],
|
|
20
|
+
},
|
|
21
|
+
{ name: "unbounded lookbehind", pattern: "(?<=a+)b", source: "aab\nb\n", lines: [1] },
|
|
22
|
+
{
|
|
23
|
+
name: "backwards capture evaluation",
|
|
24
|
+
pattern: "(?<=(a)\\1)b",
|
|
25
|
+
source: "ab\naab\n",
|
|
26
|
+
lines: [1, 2],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "case folding in a referenced capture",
|
|
30
|
+
pattern: "^(é)\\1$",
|
|
31
|
+
source: "éÉ\nÉé\nee\n",
|
|
32
|
+
lines: [1, 2],
|
|
33
|
+
ignoreCase: true,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "CRLF normalized before backreference",
|
|
37
|
+
pattern: "(a\\n)\\1",
|
|
38
|
+
source: "a\r\na\nb\n",
|
|
39
|
+
lines: [1, 2],
|
|
40
|
+
multiline: true,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "empty matches on consecutive empty lines",
|
|
44
|
+
pattern: "(?=^$)",
|
|
45
|
+
source: "\n\r\n\n",
|
|
46
|
+
lines: [1, 2, 3],
|
|
47
|
+
multiline: true,
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: "match ends before next physical line",
|
|
51
|
+
pattern: "a\\n",
|
|
52
|
+
source: "a\r\nb\n",
|
|
53
|
+
lines: [1],
|
|
54
|
+
multiline: true,
|
|
55
|
+
},
|
|
56
|
+
{ name: "line mode excludes separators", pattern: "\\n", source: "a\nb\n", lines: [] },
|
|
57
|
+
{ name: "empty file has no physical rows", pattern: "^$", source: "", lines: [] },
|
|
58
|
+
{
|
|
59
|
+
name: "terminal empty match stays on the unterminated physical line",
|
|
60
|
+
pattern: "(?=$)",
|
|
61
|
+
source: "ab",
|
|
62
|
+
lines: [1],
|
|
63
|
+
multiline: true,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: "scoped flags stay native",
|
|
67
|
+
pattern: "^(?i:a)(?-i:b)$",
|
|
68
|
+
source: "Ab\nAB\nab\n",
|
|
69
|
+
lines: [1, 3],
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
for (const scenario of cases) {
|
|
73
|
+
test(`JS search: ${scenario.name}`, { skip: unsupportedByHost([scenario.pattern], "m") }, () => {
|
|
74
|
+
const result = searchText({
|
|
75
|
+
source: scenario.source,
|
|
76
|
+
query: {
|
|
77
|
+
patterns: [scenario.pattern],
|
|
78
|
+
literal: false,
|
|
79
|
+
flags: scenario.ignoreCase ? "mi" : "m",
|
|
80
|
+
multiline: scenario.multiline ?? false,
|
|
81
|
+
},
|
|
82
|
+
limit: 100,
|
|
83
|
+
});
|
|
84
|
+
assert.deepEqual(
|
|
85
|
+
result.lines.map(({ lineNumber }) => lineNumber),
|
|
86
|
+
scenario.lines,
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
test("JS search: alternatives merge before the physical row limit", () => {
|
|
92
|
+
const result = searchText({
|
|
93
|
+
source: "zero\na\nb\nc\n",
|
|
94
|
+
query: {
|
|
95
|
+
patterns: ["c", "a\\nb", "(?=b)"],
|
|
96
|
+
literal: false,
|
|
97
|
+
flags: "m",
|
|
98
|
+
multiline: true,
|
|
99
|
+
},
|
|
100
|
+
limit: 2,
|
|
101
|
+
});
|
|
102
|
+
assert.deepEqual(result.lines, [
|
|
103
|
+
{ lineNumber: 2, column: 0 },
|
|
104
|
+
{ lineNumber: 3, column: 0 },
|
|
105
|
+
]);
|
|
106
|
+
assert.equal(result.totalLines, 4);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("JS search: columns retain UTF-16 positions inside supplementary characters", () => {
|
|
110
|
+
const result = searchText({
|
|
111
|
+
source: "😀x\n",
|
|
112
|
+
query: { patterns: ["\\uDE00"], literal: false, flags: "m", multiline: false },
|
|
113
|
+
limit: 1,
|
|
114
|
+
});
|
|
115
|
+
assert.deepEqual(result.lines, [{ lineNumber: 1, column: 1 }]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("selected lines ending in CR → matching preserves the CR and uses 1-based positions", () => {
|
|
119
|
+
const query = { patterns: ["\\r$"], literal: false, flags: "", multiline: false };
|
|
120
|
+
assert.deepEqual(searchLines({ lines: "skip\na\r\nb\r\n", query, limit: 10 }), {
|
|
121
|
+
lines: [
|
|
122
|
+
{ lineNumber: 2, column: 1 },
|
|
123
|
+
{ lineNumber: 3, column: 1 },
|
|
124
|
+
],
|
|
125
|
+
totalLines: 3,
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("selected lines exceed limit → rows truncate while totalLines counts all lines", () => {
|
|
130
|
+
const query = { patterns: ["a", "b"], literal: true, flags: "", multiline: false };
|
|
131
|
+
assert.deepEqual(searchLines({ lines: "skip\nxa\nb\na\n", query, limit: 1 }), {
|
|
132
|
+
lines: [{ lineNumber: 2, column: 1 }],
|
|
133
|
+
totalLines: 4,
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("empty selected input or one empty line → physical totals are zero or one", () => {
|
|
138
|
+
const query = { patterns: ["^$"], literal: false, flags: "", multiline: false };
|
|
139
|
+
for (const [lines, expected] of [
|
|
140
|
+
["", { lines: [], totalLines: 0 }],
|
|
141
|
+
["\n", { lines: [{ lineNumber: 1, column: 0 }], totalLines: 1 }],
|
|
142
|
+
] as const) {
|
|
143
|
+
assert.deepEqual(searchLines({ lines, query, limit: 1 }), expected);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/** JavaScript matching on the LF view, returning bounded physical rows rather than match objects. */
|
|
2
|
+
import { countLines, normalizeLineEndings } from "./lines.ts";
|
|
3
|
+
import { buildRegex } from "./regex.ts";
|
|
4
|
+
import type { SearchResult } from "./search-schema.ts";
|
|
5
|
+
|
|
6
|
+
/** README's grep flags default links here; explicit flags replace this default. */
|
|
7
|
+
export const DEFAULT_GREP_FLAGS = "m";
|
|
8
|
+
|
|
9
|
+
export interface GrepQuery {
|
|
10
|
+
readonly patterns: readonly string[];
|
|
11
|
+
readonly literal: boolean;
|
|
12
|
+
readonly flags: string;
|
|
13
|
+
readonly multiline: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface SearchInput {
|
|
16
|
+
readonly source: string;
|
|
17
|
+
readonly query: GrepQuery;
|
|
18
|
+
readonly limit: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Selected physical lines of an LF view, each followed by a line feed. Unlike a source, they
|
|
22
|
+
* are not normalized: a line keeps a CR that ends it.
|
|
23
|
+
*/
|
|
24
|
+
export interface LineSearchInput {
|
|
25
|
+
readonly lines: string;
|
|
26
|
+
readonly query: GrepQuery;
|
|
27
|
+
readonly limit: number;
|
|
28
|
+
}
|
|
29
|
+
type Rows = SearchResult["lines"];
|
|
30
|
+
|
|
31
|
+
function mergeRows(left: Rows, right: Rows, limit: number): Rows {
|
|
32
|
+
const result: Rows = [];
|
|
33
|
+
let a = 0;
|
|
34
|
+
let b = 0;
|
|
35
|
+
while (result.length < limit && (a < left.length || b < right.length)) {
|
|
36
|
+
if (b === right.length || (a < left.length && left[a].lineNumber < right[b].lineNumber))
|
|
37
|
+
result.push(left[a++]);
|
|
38
|
+
else if (a === left.length || right[b].lineNumber < left[a].lineNumber) result.push(right[b++]);
|
|
39
|
+
else {
|
|
40
|
+
result.push({
|
|
41
|
+
lineNumber: left[a].lineNumber,
|
|
42
|
+
column: Math.min(left[a++].column, right[b++].column),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function multilineRows(source: string, regex: RegExp, limit: number): Rows {
|
|
50
|
+
const rows: Rows = [];
|
|
51
|
+
let lineNumber = 1;
|
|
52
|
+
let lineStart = 0;
|
|
53
|
+
let nextBreak = source.indexOf("\n");
|
|
54
|
+
for (const match of source.matchAll(regex)) {
|
|
55
|
+
// A trailing separator terminates the last physical line; it does not create another.
|
|
56
|
+
if (match.index === source.length && (source.length === 0 || source.endsWith("\n"))) break;
|
|
57
|
+
const last = match.index + Math.max(0, match[0].length - 1);
|
|
58
|
+
while (nextBreak !== -1 && nextBreak < match.index) {
|
|
59
|
+
lineStart = nextBreak + 1;
|
|
60
|
+
lineNumber++;
|
|
61
|
+
nextBreak = source.indexOf("\n", lineStart);
|
|
62
|
+
}
|
|
63
|
+
for (;;) {
|
|
64
|
+
if (rows.at(-1)?.lineNumber !== lineNumber) {
|
|
65
|
+
rows.push({ lineNumber, column: Math.max(0, match.index - lineStart) });
|
|
66
|
+
if (rows.length === limit) return rows;
|
|
67
|
+
}
|
|
68
|
+
if (nextBreak === -1 || nextBreak >= last) break;
|
|
69
|
+
lineStart = nextBreak + 1;
|
|
70
|
+
lineNumber++;
|
|
71
|
+
nextBreak = source.indexOf("\n", lineStart);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return rows;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const queryRegexes = (query: GrepQuery) =>
|
|
78
|
+
query.patterns.map((pattern) => buildRegex(pattern, query.literal, query.flags));
|
|
79
|
+
|
|
80
|
+
/** The first match column of one physical line, or undefined when no pattern matches it. */
|
|
81
|
+
function lineColumn(regexes: readonly RegExp[], text: string): number | undefined {
|
|
82
|
+
let column = Infinity;
|
|
83
|
+
for (const regex of regexes) {
|
|
84
|
+
regex.lastIndex = 0;
|
|
85
|
+
const match = regex.exec(text);
|
|
86
|
+
if (match) column = Math.min(column, match.index);
|
|
87
|
+
}
|
|
88
|
+
return column === Infinity ? undefined : column;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Each line of source, as line feeds separate it, matched alone. */
|
|
92
|
+
function lineRows(source: string, regexes: readonly RegExp[], limit: number): Rows {
|
|
93
|
+
const lines: Rows = [];
|
|
94
|
+
for (let start = 0, lineNumber = 1; start < source.length && lines.length < limit; lineNumber++) {
|
|
95
|
+
const separator = source.indexOf("\n", start);
|
|
96
|
+
const end = separator === -1 ? source.length : separator;
|
|
97
|
+
const column = lineColumn(regexes, source.slice(start, end));
|
|
98
|
+
if (column !== undefined) lines.push({ lineNumber, column });
|
|
99
|
+
start = end + 1;
|
|
100
|
+
}
|
|
101
|
+
return lines;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Inputs have passed the tool boundary; RegExp owns all syntax and matching semantics. */
|
|
105
|
+
export function searchText({ source: raw, query, limit }: SearchInput): SearchResult {
|
|
106
|
+
const source = normalizeLineEndings(raw);
|
|
107
|
+
const regexes = queryRegexes(query);
|
|
108
|
+
let lines: Rows = [];
|
|
109
|
+
if (query.multiline) {
|
|
110
|
+
for (const regex of regexes)
|
|
111
|
+
lines = mergeRows(lines, multilineRows(source, regex, limit), limit);
|
|
112
|
+
} else lines = lineRows(source, regexes, limit);
|
|
113
|
+
return { lines, totalLines: countLines(source) };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Line-mode matching of selected lines; a row's lineNumber is the line's 1-based position. */
|
|
117
|
+
export function searchLines({ lines, query, limit }: LineSearchInput): SearchResult {
|
|
118
|
+
return { lines: lineRows(lines, queryRegexes(query), limit), totalLines: countLines(lines) };
|
|
119
|
+
}
|
package/src/core/text.test.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { test } from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import { createUtf8Decoder, decodeEditableText, decodeUtf8 } from "./text.ts";
|
|
4
|
-
import {
|
|
3
|
+
import { createUtf8Decoder, decodeEditableText, decodeUtf8, Utf8DecodingError } from "./text.ts";
|
|
4
|
+
import { HashlineError, TEXT_ERROR_MESSAGES } from "./errors.ts";
|
|
5
5
|
|
|
6
6
|
test("decodeUtf8 rejects malformed input instead of replacing bytes", () => {
|
|
7
|
-
assert.throws(
|
|
7
|
+
assert.throws(
|
|
8
|
+
() => decodeUtf8(Uint8Array.from([0xc3, 0x28])),
|
|
9
|
+
(error: unknown) =>
|
|
10
|
+
error instanceof HashlineError && error.errorCode === "UNSUPPORTED_ENCODING",
|
|
11
|
+
);
|
|
8
12
|
});
|
|
9
13
|
|
|
10
14
|
test("decodeUtf8 preserves a leading BOM", () => {
|
|
@@ -12,7 +16,10 @@ test("decodeUtf8 preserves a leading BOM", () => {
|
|
|
12
16
|
});
|
|
13
17
|
|
|
14
18
|
test("decodeEditableText rejects NUL bytes", () => {
|
|
15
|
-
assert.throws(
|
|
19
|
+
assert.throws(
|
|
20
|
+
() => decodeEditableText(Uint8Array.from([0x61, 0, 0x62])),
|
|
21
|
+
(error: unknown) => error instanceof HashlineError && error.errorCode === "UNSUPPORTED_TEXT",
|
|
22
|
+
);
|
|
16
23
|
});
|
|
17
24
|
|
|
18
25
|
test("streaming UTF-8 decoders preserve a split BOM in strict and lossy modes", () => {
|
|
@@ -32,11 +39,12 @@ test("lossy decoding replaces malformed bytes and flushes incomplete sequences a
|
|
|
32
39
|
assert.equal(decode(), "�");
|
|
33
40
|
});
|
|
34
41
|
|
|
35
|
-
test("strict decoding
|
|
42
|
+
test("strict decoding rejects malformed sequences and incomplete EOF → one encoding classification", () => {
|
|
36
43
|
const isEncodingError = (error: unknown) =>
|
|
37
|
-
error instanceof
|
|
38
|
-
error.
|
|
39
|
-
error.
|
|
44
|
+
error instanceof Utf8DecodingError &&
|
|
45
|
+
error.errorCode === "UNSUPPORTED_ENCODING" &&
|
|
46
|
+
error.message === TEXT_ERROR_MESSAGES.UNSUPPORTED_ENCODING &&
|
|
47
|
+
error.cause === undefined;
|
|
40
48
|
assert.throws(() => createUtf8Decoder("strict")(Uint8Array.from([0xff])), isEncodingError);
|
|
41
49
|
const decode = createUtf8Decoder("strict");
|
|
42
50
|
assert.equal(decode(Uint8Array.from([0xf0, 0x9f]), true), "");
|
package/src/core/text.ts
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
import { TextDecoder } from "node:util";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { HashlineError, TEXT_ERROR_MESSAGES } from "./errors.ts";
|
|
4
4
|
|
|
5
5
|
/** Why text cannot be written as editable UTF-8 (NUL or unpaired surrogates); undefined when it can. */
|
|
6
|
-
export function
|
|
7
|
-
if (text.includes("\0"))
|
|
8
|
-
|
|
6
|
+
export function unwritableTextError(text: string): HashlineError | undefined {
|
|
7
|
+
if (text.includes("\0"))
|
|
8
|
+
return new HashlineError("UNSUPPORTED_TEXT", TEXT_ERROR_MESSAGES.UNSUPPORTED_TEXT);
|
|
9
|
+
if (!text.isWellFormed())
|
|
10
|
+
return new HashlineError("INVALID_UNICODE", TEXT_ERROR_MESSAGES.INVALID_UNICODE);
|
|
9
11
|
return undefined;
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export type Utf8Decoding = "strict" | "lossy";
|
|
13
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Confirmed malformed UTF-8, distinct from decoder input or resource failures.
|
|
18
|
+
* The decoder's own error states the same fact, so it is not reported as a cause.
|
|
19
|
+
*/
|
|
20
|
+
export class Utf8DecodingError extends HashlineError<"UNSUPPORTED_ENCODING"> {
|
|
21
|
+
constructor() {
|
|
22
|
+
super("UNSUPPORTED_ENCODING", TEXT_ERROR_MESSAGES.UNSUPPORTED_ENCODING);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
14
26
|
/** Streaming UTF-8 decoding preserves BOM; lossy mode replaces malformed bytes with U+FFFD. */
|
|
15
27
|
export function createUtf8Decoder(mode: Utf8Decoding = "strict") {
|
|
16
28
|
const decoder = new TextDecoder("utf-8", { fatal: mode === "strict", ignoreBOM: true });
|
|
@@ -21,7 +33,14 @@ export function createUtf8Decoder(mode: Utf8Decoding = "strict") {
|
|
|
21
33
|
try {
|
|
22
34
|
return decoder.decode(bytes, { stream });
|
|
23
35
|
} catch (error) {
|
|
24
|
-
|
|
36
|
+
if (
|
|
37
|
+
error instanceof TypeError &&
|
|
38
|
+
"code" in error &&
|
|
39
|
+
error.code === "ERR_ENCODING_INVALID_ENCODED_DATA"
|
|
40
|
+
) {
|
|
41
|
+
throw new Utf8DecodingError();
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
25
44
|
}
|
|
26
45
|
};
|
|
27
46
|
}
|
|
@@ -35,7 +54,8 @@ export function decodeUtf8(bytes: Uint8Array): string {
|
|
|
35
54
|
|
|
36
55
|
/** Reject byte-oriented/binary content before entering a text mutation pipeline. */
|
|
37
56
|
export function decodeEditableText(bytes: Uint8Array): string {
|
|
38
|
-
if (bytes.includes(0))
|
|
57
|
+
if (bytes.includes(0))
|
|
58
|
+
throw new HashlineError("UNSUPPORTED_TEXT", TEXT_ERROR_MESSAGES.UNSUPPORTED_TEXT);
|
|
39
59
|
return decodeUtf8(bytes);
|
|
40
60
|
}
|
|
41
61
|
|
package/src/core/types.ts
CHANGED
|
@@ -10,6 +10,10 @@ export interface Anchor {
|
|
|
10
10
|
readonly hash: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** Public anchor field names shared by preparation, verification, and recovery. */
|
|
14
|
+
export const EDIT_ANCHOR_FIELDS = ["anchor", "end", "before", "after"] as const;
|
|
15
|
+
type AnchorField = (typeof EDIT_ANCHOR_FIELDS)[number];
|
|
16
|
+
|
|
13
17
|
/**
|
|
14
18
|
* Edit operation. Every line-numbered op references a line via {@link Anchor} —
|
|
15
19
|
* the line number is the address, the hash a checksum that the line at that
|
|
@@ -18,11 +22,15 @@ export interface Anchor {
|
|
|
18
22
|
export type Edit =
|
|
19
23
|
| {
|
|
20
24
|
readonly op: "replace";
|
|
21
|
-
readonly
|
|
25
|
+
readonly anchor: Anchor;
|
|
22
26
|
readonly end?: Anchor;
|
|
23
27
|
readonly body: string[];
|
|
24
28
|
}
|
|
25
|
-
| { readonly op: "delete"; readonly
|
|
29
|
+
| { readonly op: "delete"; readonly anchor: Anchor; readonly end?: Anchor }
|
|
30
|
+
| ({ readonly op: "copy" | "move"; readonly anchor: Anchor; readonly end?: Anchor } & (
|
|
31
|
+
| { readonly before: Anchor; readonly after?: never }
|
|
32
|
+
| { readonly after: Anchor; readonly before?: never }
|
|
33
|
+
))
|
|
26
34
|
| { readonly op: "insert_after"; readonly anchor: Anchor; readonly body: string[] }
|
|
27
35
|
| { readonly op: "insert_before"; readonly anchor: Anchor; readonly body: string[] }
|
|
28
36
|
| { readonly op: "append"; readonly body: string[] }
|
|
@@ -43,7 +51,8 @@ export type LineEnding = "lf" | "crlf";
|
|
|
43
51
|
* checks its content before resending with the provided anchor.
|
|
44
52
|
* - `ambiguous` — several lines match; the caller inspects the candidates
|
|
45
53
|
* and chooses the intended target.
|
|
46
|
-
* - `none` — no candidate found, or recovery disabled;
|
|
54
|
+
* - `none` — no candidate found, or recovery disabled; the caller checks the
|
|
55
|
+
* current cited line, if any, or re-reads.
|
|
47
56
|
*/
|
|
48
57
|
export type AnchorRecovery =
|
|
49
58
|
| {
|
|
@@ -63,25 +72,23 @@ export type AnchorRecovery =
|
|
|
63
72
|
* A single anchor that failed verification, with its recovery attempt.
|
|
64
73
|
*
|
|
65
74
|
* `opIndex` is the 0-based position in the input `edits[]`; `which` names the
|
|
66
|
-
* op's
|
|
67
|
-
* `current` is the cited line's live content
|
|
75
|
+
* op's source/insertion anchor, range end, or before/after destination; `op` is the op kind.
|
|
76
|
+
* `current` is the cited line's live content (null if the line number is
|
|
68
77
|
* out of range) — surfaced when recovery is `none` so the model can self-diagnose.
|
|
69
78
|
*/
|
|
70
79
|
export interface AnchorFailure {
|
|
71
80
|
readonly opIndex: number;
|
|
72
|
-
readonly which:
|
|
81
|
+
readonly which: AnchorField;
|
|
73
82
|
readonly op: Edit["op"];
|
|
74
83
|
readonly cited: Anchor;
|
|
75
84
|
readonly recovery: AnchorRecovery;
|
|
76
|
-
readonly current: { readonly
|
|
85
|
+
readonly current: { readonly content: string } | null;
|
|
77
86
|
}
|
|
78
87
|
|
|
79
88
|
/** Record for one supplied anchor in the immutable apply snapshot. */
|
|
80
89
|
export interface AnchorCheck {
|
|
81
90
|
readonly opIndex: number;
|
|
82
|
-
readonly which:
|
|
83
|
-
readonly op: Edit["op"];
|
|
84
|
-
readonly cited: Anchor;
|
|
91
|
+
readonly which: AnchorField;
|
|
85
92
|
readonly status: "matched" | "mismatched";
|
|
86
93
|
}
|
|
87
94
|
|
|
@@ -94,15 +101,14 @@ export type ApplyFailure =
|
|
|
94
101
|
}
|
|
95
102
|
| {
|
|
96
103
|
readonly kind: "range";
|
|
104
|
+
/** INVALID_RANGE for a reversed range or a move into itself; OVERLAPPING_EDITS for overlap. */
|
|
105
|
+
readonly code: "INVALID_RANGE" | "OVERLAPPING_EDITS";
|
|
97
106
|
readonly message: string;
|
|
98
107
|
readonly checks: readonly AnchorCheck[];
|
|
99
108
|
};
|
|
100
109
|
|
|
101
110
|
/**
|
|
102
|
-
* Apply result.
|
|
103
|
-
* re-anchor. `contextLines` identifies deletion successors among those lines;
|
|
104
|
-
* callers retain their content while compacting anchors for caller-supplied rows.
|
|
105
|
-
* Byte-identical output succeeds with changed=false and empty anchor lists.
|
|
111
|
+
* Apply result. Byte-identical output is a success; the commit layer detects the no-op.
|
|
106
112
|
* On failure, `failure` is either the collected set of anchor failures
|
|
107
113
|
* (each with recovery) or a range error, plus per-input anchor checks.
|
|
108
114
|
* Nothing is written on failure.
|
|
@@ -111,8 +117,5 @@ export type ApplyResult =
|
|
|
111
117
|
| {
|
|
112
118
|
readonly ok: true;
|
|
113
119
|
readonly text: string;
|
|
114
|
-
readonly changed: boolean;
|
|
115
|
-
readonly touchedLines: readonly number[];
|
|
116
|
-
readonly contextLines: readonly number[];
|
|
117
120
|
}
|
|
118
121
|
| { readonly ok: false; readonly failure: ApplyFailure };
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* pi-hashline-edit extension entry.
|
|
3
3
|
*
|
|
4
|
-
* Overrides
|
|
5
|
-
* edit
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* full JS regex with capture groups) for renames/pattern transforms that
|
|
10
|
-
* would need many individual edits. Each tool carries its own renderer.
|
|
4
|
+
* Overrides Pi's built-in read, grep, edit, and write: read and grep output
|
|
5
|
+
* `LINE#HASH│content` anchors, edit applies structured ops on those anchors, and
|
|
6
|
+
* write publishes whole files through the same commit layer. Adds `replace`
|
|
7
|
+
* (literal or JavaScript-regex replacement across a file). Each tool carries its
|
|
8
|
+
* own renderer; src/pi/AGENTS.md maps the modules.
|
|
11
9
|
*
|
|
12
10
|
* @module pi-hashline-edit
|
|
13
11
|
*/
|
|
@@ -15,14 +13,15 @@
|
|
|
15
13
|
import type { ExtensionAPI, ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
16
14
|
import type { TSchema } from "typebox";
|
|
17
15
|
import { loadConfig } from "./pi/config.ts";
|
|
16
|
+
import { registerModelBudgets } from "./pi/model-budgets.ts";
|
|
18
17
|
import { makeEditOverride } from "./pi/edit-tool.ts";
|
|
19
18
|
import { makeReadOverride } from "./pi/read-tool.ts";
|
|
20
19
|
import { makeGrepOverride } from "./pi/grep-tool.ts";
|
|
21
|
-
import { registerForgetTool } from "./pi/forget-tool.ts";
|
|
22
20
|
import { makeReplaceTool } from "./pi/replace-tool.ts";
|
|
23
21
|
import { makeWriteOverride } from "./pi/write-tool.ts";
|
|
24
22
|
import { createActionFusionExecutor } from "./pi/action-fusion.ts";
|
|
25
23
|
import { registerFusionCards, withMutationStatus } from "./pi/fusion-card.ts";
|
|
24
|
+
import { NEXT_GUIDELINE } from "./pi/report-schema.ts";
|
|
26
25
|
|
|
27
26
|
export default function (pi: ExtensionAPI) {
|
|
28
27
|
const cwd = process.cwd();
|
|
@@ -31,21 +30,30 @@ export default function (pi: ExtensionAPI) {
|
|
|
31
30
|
const config = loadConfig(cwd);
|
|
32
31
|
|
|
33
32
|
// `enabled: false` leaves the extension fully inert — pi's built-in
|
|
34
|
-
// read/
|
|
33
|
+
// read/grep/edit/write stay in place, as if this package were not installed.
|
|
35
34
|
if (config.enabled) {
|
|
35
|
+
const budgets = registerModelBudgets(pi);
|
|
36
|
+
// Saved then_run cards render even when Action Fusion is now off.
|
|
36
37
|
const reportProgress = registerFusionCards(pi);
|
|
37
38
|
const fusion = config.actionFusion
|
|
38
39
|
? createActionFusionExecutor(undefined, reportProgress)
|
|
39
40
|
: undefined;
|
|
41
|
+
// Every tool returns a report; Pi prints this shared guideline once.
|
|
42
|
+
const register = <TParams extends TSchema, TDetails, TState>(
|
|
43
|
+
tool: ToolDefinition<TParams, TDetails, TState>,
|
|
44
|
+
) =>
|
|
45
|
+
pi.registerTool({
|
|
46
|
+
...tool,
|
|
47
|
+
promptGuidelines: [...(tool.promptGuidelines ?? []), NEXT_GUIDELINE],
|
|
48
|
+
});
|
|
40
49
|
const registerMutation = <TParams extends TSchema, TDetails>(
|
|
41
50
|
tool: ToolDefinition<TParams, TDetails>,
|
|
42
|
-
) =>
|
|
43
|
-
registerMutation(makeWriteOverride(cwd, fusion));
|
|
44
|
-
registerMutation(makeEditOverride(cwd, config, fusion));
|
|
45
|
-
registerMutation(makeReplaceTool(cwd, config, fusion));
|
|
51
|
+
) => register(fusion ? withMutationStatus(tool) : tool);
|
|
52
|
+
registerMutation(makeWriteOverride(cwd, fusion, budgets));
|
|
53
|
+
registerMutation(makeEditOverride(cwd, config, fusion, budgets));
|
|
54
|
+
registerMutation(makeReplaceTool(cwd, config, fusion, budgets));
|
|
46
55
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (config.forget) registerForgetTool(pi);
|
|
56
|
+
register(makeReadOverride(cwd, config, budgets));
|
|
57
|
+
register(makeGrepOverride(cwd, config, budgets));
|
|
50
58
|
}
|
|
51
59
|
}
|