@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.
- package/README.md +161 -304
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +10 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +189 -60
- package/src/core/apply.ts +110 -74
- package/src/core/case-fold.test.ts +40 -0
- package/src/core/case-fold.ts +81 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +170 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/lines.ts +7 -0
- package/src/core/regex.ts +21 -0
- package/src/core/replace-schema.ts +34 -0
- package/src/core/replace.ts +37 -54
- package/src/core/search-analysis.test.ts +199 -0
- package/src/core/search-analysis.ts +375 -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 +16 -11
- package/src/index.ts +25 -15
- package/src/integration/config-registration.test.ts +30 -24
- package/src/integration/forget-grep.test.ts +52 -2
- package/src/integration/fusion-dispatch.test.ts +21 -105
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +280 -0
- package/src/integration/grep-js-completeness.test.ts +208 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-path-matrix.test.ts +132 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +410 -0
- package/src/integration/grep-rg.test.ts +220 -402
- package/src/integration/grep-scan-budget.test.ts +89 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +57 -1
- package/src/integration/model-budget-lifecycle.test.ts +128 -0
- package/src/integration/mutation-file-types.test.ts +12 -25
- 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 +123 -0
- package/src/integration/publication-read-changes.test.ts +344 -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 +33 -0
- package/src/integration/replace-worker-boundary.test.ts +107 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +55 -0
- package/src/pi/action-fusion.test.ts +253 -103
- package/src/pi/action-fusion.ts +116 -290
- package/src/pi/anchor-format.ts +9 -0
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +43 -0
- package/src/pi/argument-error.ts +78 -0
- package/src/pi/argument-format.test.ts +208 -0
- package/src/pi/argument-validation.test.ts +742 -45
- package/src/pi/argument-validation.ts +56 -59
- package/src/pi/budgets.ts +45 -16
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +14 -10
- package/src/pi/config.ts +21 -7
- package/src/pi/diagnostic-buffer.test.ts +52 -16
- package/src/pi/diagnostic-buffer.ts +11 -14
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +100 -93
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +362 -315
- package/src/pi/extension-loading.test.ts +14 -0
- package/src/pi/extension-loading.testing.ts +3 -0
- package/src/pi/failure-context.test.ts +220 -88
- package/src/pi/failure-context.ts +161 -180
- package/src/pi/failure-path.test.ts +85 -88
- package/src/pi/file-commit.test.ts +78 -45
- package/src/pi/file-commit.ts +218 -151
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/forget-tool.ts +189 -90
- package/src/pi/forget.test.ts +309 -22
- package/src/pi/forget.testing.ts +41 -113
- package/src/pi/fusion-card.test.ts +40 -14
- package/src/pi/fusion-card.ts +84 -50
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +193 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-output.ts +181 -138
- package/src/pi/grep-render.ts +28 -25
- package/src/pi/grep-scope.ts +61 -53
- package/src/pi/grep-search.ts +299 -242
- package/src/pi/grep-tool.ts +150 -140
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +631 -632
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +5 -5
- package/src/pi/mutation-result.test.ts +21 -74
- package/src/pi/mutation-result.ts +74 -87
- package/src/pi/mutation-runner.ts +83 -75
- package/src/pi/output-budgets.test.ts +166 -0
- package/src/pi/path.ts +7 -0
- package/src/pi/read-tool.ts +151 -137
- package/src/pi/read.test.ts +39 -33
- package/src/pi/regex-worker-contract.ts +73 -0
- package/src/pi/regex-worker.ts +101 -0
- package/src/pi/registration.test.ts +111 -92
- package/src/pi/render.test.ts +193 -19
- package/src/pi/render.ts +137 -29
- package/src/pi/replace-regex.ts +11 -44
- package/src/pi/replace-tool.ts +38 -145
- package/src/pi/replace-worker-contract.ts +19 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +93 -160
- package/src/pi/report-cause.ts +99 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +512 -0
- package/src/pi/report.test.ts +276 -0
- package/src/pi/report.ts +288 -0
- package/src/pi/result-safety.test.ts +122 -427
- package/src/pi/rg-process.test.ts +78 -79
- package/src/pi/rg-process.ts +124 -177
- package/src/pi/rg-regex.test.ts +50 -0
- package/src/pi/rg-regex.ts +709 -0
- package/src/pi/text-stream.test.ts +67 -38
- package/src/pi/text-stream.ts +96 -75
- package/src/pi/tool-call.testing.ts +3 -39
- package/src/pi/write-tool.ts +45 -34
- package/src/pi/write.test.ts +45 -37
- 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 +308 -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/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
|
|
@@ -23,6 +27,10 @@ export type Edit =
|
|
|
23
27
|
readonly body: string[];
|
|
24
28
|
}
|
|
25
29
|
| { readonly op: "delete"; readonly start: Anchor; readonly end?: Anchor }
|
|
30
|
+
| ({ readonly op: "copy" | "move"; readonly start: 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,13 +72,13 @@ 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
|
|
75
|
+
* op's source/insertion anchor, range end, or before/after destination; `op` is the op kind.
|
|
67
76
|
* `current` is the cited line's live content + hash (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;
|
|
@@ -79,7 +88,7 @@ export interface AnchorFailure {
|
|
|
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:
|
|
91
|
+
readonly which: AnchorField;
|
|
83
92
|
readonly op: Edit["op"];
|
|
84
93
|
readonly cited: Anchor;
|
|
85
94
|
readonly status: "matched" | "mismatched";
|
|
@@ -94,15 +103,14 @@ export type ApplyFailure =
|
|
|
94
103
|
}
|
|
95
104
|
| {
|
|
96
105
|
readonly kind: "range";
|
|
106
|
+
/** INVALID_RANGE for a reversed range or a move into itself; OVERLAPPING_EDITS for overlap. */
|
|
107
|
+
readonly code: "INVALID_RANGE" | "OVERLAPPING_EDITS";
|
|
97
108
|
readonly message: string;
|
|
98
109
|
readonly checks: readonly AnchorCheck[];
|
|
99
110
|
};
|
|
100
111
|
|
|
101
112
|
/**
|
|
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.
|
|
113
|
+
* Apply result. Byte-identical output is a success; the commit layer detects the no-op.
|
|
106
114
|
* On failure, `failure` is either the collected set of anchor failures
|
|
107
115
|
* (each with recovery) or a range error, plus per-input anchor checks.
|
|
108
116
|
* Nothing is written on failure.
|
|
@@ -111,8 +119,5 @@ export type ApplyResult =
|
|
|
111
119
|
| {
|
|
112
120
|
readonly ok: true;
|
|
113
121
|
readonly text: string;
|
|
114
|
-
readonly changed: boolean;
|
|
115
|
-
readonly touchedLines: readonly number[];
|
|
116
|
-
readonly contextLines: readonly number[];
|
|
117
122
|
}
|
|
118
123
|
| { 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) and, when enabled,
|
|
8
|
+
* `forget`. Each tool carries its own renderer; src/pi/AGENTS.md maps the modules.
|
|
11
9
|
*
|
|
12
10
|
* @module pi-hashline-edit
|
|
13
11
|
*/
|
|
@@ -15,6 +13,7 @@
|
|
|
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";
|
|
@@ -23,6 +22,7 @@ import { makeReplaceTool } from "./pi/replace-tool.ts";
|
|
|
23
22
|
import { makeWriteOverride } from "./pi/write-tool.ts";
|
|
24
23
|
import { createActionFusionExecutor } from "./pi/action-fusion.ts";
|
|
25
24
|
import { registerFusionCards, withMutationStatus } from "./pi/fusion-card.ts";
|
|
25
|
+
import { NEXT_GUIDELINE } from "./pi/report-schema.ts";
|
|
26
26
|
|
|
27
27
|
export default function (pi: ExtensionAPI) {
|
|
28
28
|
const cwd = process.cwd();
|
|
@@ -31,21 +31,31 @@ export default function (pi: ExtensionAPI) {
|
|
|
31
31
|
const config = loadConfig(cwd);
|
|
32
32
|
|
|
33
33
|
// `enabled: false` leaves the extension fully inert — pi's built-in
|
|
34
|
-
// read/
|
|
34
|
+
// read/grep/edit/write stay in place, as if this package were not installed.
|
|
35
35
|
if (config.enabled) {
|
|
36
|
+
const budgets = registerModelBudgets(pi);
|
|
37
|
+
// Saved then_run cards render even when Action Fusion is now off.
|
|
36
38
|
const reportProgress = registerFusionCards(pi);
|
|
37
39
|
const fusion = config.actionFusion
|
|
38
40
|
? createActionFusionExecutor(undefined, reportProgress)
|
|
39
41
|
: undefined;
|
|
42
|
+
// Every tool returns a report; Pi prints this shared guideline once.
|
|
43
|
+
const register = <TParams extends TSchema, TDetails, TState>(
|
|
44
|
+
tool: ToolDefinition<TParams, TDetails, TState>,
|
|
45
|
+
) =>
|
|
46
|
+
pi.registerTool({
|
|
47
|
+
...tool,
|
|
48
|
+
promptGuidelines: [...(tool.promptGuidelines ?? []), NEXT_GUIDELINE],
|
|
49
|
+
});
|
|
40
50
|
const registerMutation = <TParams extends TSchema, TDetails>(
|
|
41
51
|
tool: ToolDefinition<TParams, TDetails>,
|
|
42
|
-
) =>
|
|
43
|
-
registerMutation(makeWriteOverride(cwd, fusion));
|
|
44
|
-
registerMutation(makeEditOverride(cwd, config, fusion));
|
|
45
|
-
registerMutation(makeReplaceTool(cwd, config, fusion));
|
|
52
|
+
) => register(fusion ? withMutationStatus(tool) : tool);
|
|
53
|
+
registerMutation(makeWriteOverride(cwd, fusion, budgets));
|
|
54
|
+
registerMutation(makeEditOverride(cwd, config, fusion, budgets));
|
|
55
|
+
registerMutation(makeReplaceTool(cwd, config, fusion, budgets));
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (config.forget) registerForgetTool(pi);
|
|
57
|
+
register(makeReadOverride(cwd, config, budgets));
|
|
58
|
+
register(makeGrepOverride(cwd, config, budgets));
|
|
59
|
+
if (config.forget) registerForgetTool(pi, budgets);
|
|
50
60
|
}
|
|
51
61
|
}
|
|
@@ -8,6 +8,8 @@ import registerHashline from "../index.ts";
|
|
|
8
8
|
import { computeLineHash } from "../core/hash.ts";
|
|
9
9
|
import { callTool } from "../pi/tool-call.testing.ts";
|
|
10
10
|
import { DEFAULT_CONFIG, type HashlineEditConfig } from "../pi/config.ts";
|
|
11
|
+
import { reportOf } from "../pi/report.ts";
|
|
12
|
+
import { expectToolFailure } from "../pi/report-failure.testing.ts";
|
|
11
13
|
|
|
12
14
|
test("registered tools retain their configuration when another tool set is registered", async (t) => {
|
|
13
15
|
const directory = await mkdtemp(join(tmpdir(), "hashline-config-registration-"));
|
|
@@ -38,8 +40,8 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
38
40
|
hashLen: 6,
|
|
39
41
|
shiftRadius: 1,
|
|
40
42
|
read: { defaultLimit: 1, maxKiB: 1 },
|
|
41
|
-
grep: { defaultLimit: 1, defaultContext: 1 },
|
|
42
|
-
replace: { regexTimeoutMs:
|
|
43
|
+
grep: { ...DEFAULT_CONFIG.grep, defaultLimit: 1, defaultContext: 1 },
|
|
44
|
+
replace: { regexTimeoutMs: 3_000 },
|
|
43
45
|
})),
|
|
44
46
|
expectedGrepLines: [1, 2, 3],
|
|
45
47
|
},
|
|
@@ -50,8 +52,8 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
50
52
|
hashLen: 8,
|
|
51
53
|
shiftRadius: 0,
|
|
52
54
|
read: { defaultLimit: 2, maxKiB: 2 },
|
|
53
|
-
grep: { defaultLimit: 2, defaultContext: 0 },
|
|
54
|
-
replace: { regexTimeoutMs:
|
|
55
|
+
grep: { ...DEFAULT_CONFIG.grep, defaultLimit: 2, defaultContext: 0 },
|
|
56
|
+
replace: { regexTimeoutMs: 3_500 },
|
|
55
57
|
})),
|
|
56
58
|
expectedGrepLines: [2, 6],
|
|
57
59
|
},
|
|
@@ -59,7 +61,7 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
59
61
|
|
|
60
62
|
for (const { tools, config, expectedGrepLines } of registrations) {
|
|
61
63
|
const { hashLen, shiftRadius, read, grep, replace } = config;
|
|
62
|
-
for (const name of ["read", "grep", "
|
|
64
|
+
for (const name of ["read", "grep", "edit"] as const) {
|
|
63
65
|
await t.test(`${name} retains the registered hash length ${hashLen}`, async () => {
|
|
64
66
|
const tool = tools.get(name);
|
|
65
67
|
assert.ok(tool);
|
|
@@ -68,7 +70,6 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
68
70
|
const params = {
|
|
69
71
|
read: { path },
|
|
70
72
|
grep: { path, pattern: "before", literal: true },
|
|
71
|
-
replace: { path, replacements: [{ find: "before", replace: "after" }] },
|
|
72
73
|
edit: {
|
|
73
74
|
path,
|
|
74
75
|
edits: [
|
|
@@ -81,9 +82,13 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
81
82
|
},
|
|
82
83
|
}[name];
|
|
83
84
|
const result = await callTool(tool, params);
|
|
84
|
-
const expected = name === "
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
const expected = name === "edit" ? "after" : "before";
|
|
86
|
+
if (name === "edit") {
|
|
87
|
+
assert.equal(result.isError, undefined);
|
|
88
|
+
} else {
|
|
89
|
+
const anchor = `1#${computeLineHash(1, expected, hashLen)}`;
|
|
90
|
+
assert.match(result.content[0].text, new RegExp(`^${anchor}(?:│|$)`, "m"));
|
|
91
|
+
}
|
|
87
92
|
assert.equal(await readFile(join(directory, path), "utf8"), `${expected}\n`);
|
|
88
93
|
});
|
|
89
94
|
}
|
|
@@ -94,14 +99,16 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
94
99
|
const path = `${hashLen}-recovery.txt`;
|
|
95
100
|
const text = "changed\ntarget\n";
|
|
96
101
|
await writeFile(join(directory, path), text);
|
|
97
|
-
await
|
|
102
|
+
await expectToolFailure(
|
|
98
103
|
callTool(tool, {
|
|
99
104
|
path,
|
|
100
105
|
edits: [{ op: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
|
|
101
106
|
}),
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
"ANCHOR_MISMATCH",
|
|
108
|
+
(report) => {
|
|
109
|
+
assert.equal(report.failures?.[0].result, shiftRadius === 0 ? "unresolved" : "shifted");
|
|
110
|
+
if (shiftRadius !== 0) assert.match(report.failures?.[0].candidate ?? "", /^2#/);
|
|
111
|
+
},
|
|
105
112
|
);
|
|
106
113
|
assert.equal(await readFile(join(directory, path), "utf8"), text);
|
|
107
114
|
});
|
|
@@ -130,14 +137,15 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
130
137
|
await writeFile(join(directory, path), `${wide}\n`);
|
|
131
138
|
const bounded = await callTool(tool, { path });
|
|
132
139
|
if (read.maxKiB === 1) {
|
|
133
|
-
assert.
|
|
140
|
+
assert.deepEqual(reportOf(bounded)?.lineTooLong, { line: 1, limit: "1 KiB" });
|
|
134
141
|
assert.equal(bounded.details.truncation.maxBytes, 1024);
|
|
135
142
|
assert.equal(bounded.details.truncation.outputBytes, 0);
|
|
136
143
|
} else {
|
|
137
144
|
assert.ok(
|
|
138
145
|
bounded.content[0].text.includes(`1#${computeLineHash(1, wide, hashLen)}│${wide}`),
|
|
139
146
|
);
|
|
140
|
-
assert.equal(bounded
|
|
147
|
+
assert.equal(reportOf(bounded)?.truncatedAt, undefined);
|
|
148
|
+
assert.equal(reportOf(bounded)?.pagination, undefined);
|
|
141
149
|
}
|
|
142
150
|
},
|
|
143
151
|
);
|
|
@@ -170,10 +178,7 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
170
178
|
`${line}#${computeLineHash(line, lines[line - 1], hashLen)}│${lines[line - 1]}`,
|
|
171
179
|
),
|
|
172
180
|
);
|
|
173
|
-
assert.
|
|
174
|
-
result.content[0].text,
|
|
175
|
-
new RegExp(`${grep.defaultLimit} matches limit reached`),
|
|
176
|
-
);
|
|
181
|
+
assert.equal(reportOf(result)?.matchLimit, grep.defaultLimit);
|
|
177
182
|
},
|
|
178
183
|
);
|
|
179
184
|
|
|
@@ -185,15 +190,16 @@ test("registered tools retain their configuration when another tool set is regis
|
|
|
185
190
|
const path = `${hashLen}-replace-settings.txt`;
|
|
186
191
|
const original = `${"a".repeat(35)}!`;
|
|
187
192
|
await writeFile(join(directory, path), original);
|
|
188
|
-
await
|
|
193
|
+
await expectToolFailure(
|
|
189
194
|
callTool(tool, {
|
|
190
195
|
path,
|
|
191
196
|
replacements: [{ find: "^(a+)+$", replace: "changed", regex: true }],
|
|
192
197
|
}),
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
198
|
+
"REGEX_TIMEOUT",
|
|
199
|
+
(report) => {
|
|
200
|
+
assert.equal(report.timeoutMs, replace.regexTimeoutMs);
|
|
201
|
+
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
202
|
+
},
|
|
197
203
|
);
|
|
198
204
|
assert.equal(await readFile(join(directory, path), "utf8"), original);
|
|
199
205
|
},
|