@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
|
@@ -1,30 +1,41 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import { computeLineHash } from "../core/hash.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { anchorPattern, createAnchorFormatter, parseAnchorToken } from "./anchor-format.ts";
|
|
5
5
|
import { getState } from "./state.ts";
|
|
6
6
|
|
|
7
7
|
test("anchor formatter snapshots configured hash length and hashes undisplayed content", () => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
const state = getState();
|
|
9
|
+
const previous = state.config;
|
|
10
|
+
try {
|
|
11
|
+
state.config = { ...previous, hashLen: 6 };
|
|
12
|
+
const formatter = createAnchorFormatter();
|
|
13
|
+
state.config = { ...previous, hashLen: 4 };
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
assert.equal(formatter.hashLen, 6);
|
|
16
|
+
assert.equal(formatter.token(3, "full content"), `3#${computeLineHash(3, "full content", 6)}`);
|
|
17
|
+
assert.equal(formatter.reference(3, "ABCDEF"), "3#ABCDEF");
|
|
18
|
+
assert.match(formatter.token(3, "full content"), new RegExp(anchorPattern(6)));
|
|
19
|
+
assert.doesNotMatch(formatter.token(3, "full content"), new RegExp(anchorPattern(4)));
|
|
20
|
+
assert.equal(
|
|
21
|
+
formatter.row(3, "full content", "full…"),
|
|
22
|
+
`3#${computeLineHash(3, "full content", 6)}│full…`,
|
|
23
|
+
);
|
|
24
|
+
assert.equal(createAnchorFormatter().hashLen, 4);
|
|
25
|
+
} finally {
|
|
26
|
+
state.config = previous;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("anchor tokens parse at any hash length so wrong lengths can be explained", () => {
|
|
31
|
+
assert.deepEqual(parseAnchorToken("12#GF"), { line: 12, hash: "GF" });
|
|
32
|
+
assert.deepEqual(parseAnchorToken("12#GFYR"), { line: 12, hash: "GFYR" });
|
|
33
|
+
assert.equal(parseAnchorToken("12#gfyr"), undefined);
|
|
34
|
+
assert.equal(parseAnchorToken("0#GFYR"), undefined);
|
|
24
35
|
});
|
|
25
36
|
|
|
26
37
|
test("anchor rows expose carriage returns while checksums retain source content", () => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
const formatter = createAnchorFormatter(4);
|
|
39
|
+
assert.equal(formatter.row(1, "a\rb"), `1#${computeLineHash(1, "a\rb", 4)}│a␍b`);
|
|
40
|
+
assert.notEqual(formatter.token(1, "a\rb"), formatter.token(1, "a␍b"));
|
|
30
41
|
});
|
package/src/pi/anchor-format.ts
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
1
|
import { computeLineHash } from "../core/hash.ts";
|
|
2
2
|
import { getState } from "./state.ts";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/** Schema pattern for a `LINE#HASH` anchor whose hash has exactly `hashLen` characters. */
|
|
5
|
+
export function anchorPattern(hashLen: number): string {
|
|
6
|
+
return `^([1-9][0-9]*)#([0-9A-Z]{${hashLen}})$`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ANCHOR_TOKEN = /^([1-9][0-9]*)#([0-9A-Z]+)$/;
|
|
10
|
+
|
|
11
|
+
/** Split a `LINE#HASH` token of any hash length; undefined when the shape is wrong. */
|
|
12
|
+
export function parseAnchorToken(value: string): { line: number; hash: string } | undefined {
|
|
13
|
+
const match = ANCHOR_TOKEN.exec(value);
|
|
14
|
+
return match ? { line: Number(match[1]), hash: match[2]! } : undefined;
|
|
15
|
+
}
|
|
5
16
|
|
|
6
17
|
/** Anchor serialization bound to one hash-length snapshot. */
|
|
7
18
|
export interface AnchorFormatter {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
19
|
+
readonly hashLen: number;
|
|
20
|
+
reference(line: number, hash: string): string;
|
|
21
|
+
token(line: number, content: string): string;
|
|
22
|
+
row(line: number, content: string, displayContent?: string): string;
|
|
12
23
|
}
|
|
13
24
|
|
|
14
25
|
/** Make CR visible without changing the source text used for checksums or patches. */
|
|
15
26
|
export function displayCarriageReturns(text: string): string {
|
|
16
|
-
|
|
27
|
+
return text.replace(/\r/g, "␍");
|
|
17
28
|
}
|
|
18
29
|
|
|
19
30
|
export function createAnchorFormatter(hashLen = getState().config.hashLen): AnchorFormatter {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
const reference = (line: number, hash: string) => `${line}#${hash}`;
|
|
32
|
+
const token = (line: number, content: string) =>
|
|
33
|
+
reference(line, computeLineHash(line, content, hashLen));
|
|
34
|
+
return {
|
|
35
|
+
hashLen,
|
|
36
|
+
token,
|
|
37
|
+
reference,
|
|
38
|
+
row: (line, content, displayContent = content) =>
|
|
39
|
+
`${token(line, content)}│${displayCarriageReturns(displayContent)}`,
|
|
40
|
+
};
|
|
28
41
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing output byte budgets.
|
|
3
|
+
*
|
|
4
|
+
* README "Output budgets" documents each value; change both together. Result
|
|
5
|
+
* and notice text derive their size wording from these constants.
|
|
6
|
+
*
|
|
7
|
+
* @module pi-hashline-edit/pi
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Each diagnostic or anchor block: failure details, input-anchor checks, candidate neighborhoods, mutation anchors. */
|
|
11
|
+
export const MAX_BLOCK_BYTES = 16 * 1024;
|
|
12
|
+
/** One recovery-candidate or cited row inside a failure diagnostic. */
|
|
13
|
+
export const MAX_RECOVERY_CANDIDATE_BYTES = 4 * 1024;
|
|
14
|
+
/** grep search diagnostics appended to a result. */
|
|
15
|
+
export const MAX_SEARCH_DIAGNOSTIC_BYTES = 4 * 1024;
|
|
16
|
+
|
|
17
|
+
/** Size wording for notices, e.g. `16 KiB`. */
|
|
18
|
+
export function formatKiB(bytes: number): string {
|
|
19
|
+
return `${bytes / 1024} KiB`;
|
|
20
|
+
}
|
package/src/pi/config.test.ts
CHANGED
|
@@ -6,37 +6,67 @@ import { test } from "node:test";
|
|
|
6
6
|
import { loadConfig } from "./config.ts";
|
|
7
7
|
|
|
8
8
|
test("actionFusion defaults on and honors explicit disabling", async () => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
const root = await mkdtemp(join(tmpdir(), "hashline-config-"));
|
|
10
|
+
try {
|
|
11
|
+
await mkdir(join(root, ".pi"));
|
|
12
|
+
for (const [actionFusion, expected] of [
|
|
13
|
+
[undefined, true],
|
|
14
|
+
[false, false],
|
|
15
|
+
[true, true],
|
|
16
|
+
[null, true],
|
|
17
|
+
["false", true],
|
|
18
|
+
[0, true],
|
|
19
|
+
]) {
|
|
20
|
+
await writeFile(
|
|
21
|
+
join(root, ".pi", "settings.json"),
|
|
22
|
+
JSON.stringify({ hashlineEdit: { actionFusion } }),
|
|
23
|
+
);
|
|
24
|
+
assert.deepEqual(loadConfig(root), {
|
|
25
|
+
enabled: true,
|
|
26
|
+
actionFusion: expected,
|
|
27
|
+
hashLen: 4,
|
|
28
|
+
shiftRadius: 15,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
} finally {
|
|
32
|
+
await rm(root, { recursive: true, force: true });
|
|
33
|
+
}
|
|
19
34
|
});
|
|
20
35
|
|
|
21
36
|
test("config rejects fractional values and tolerates malformed settings shapes", async () => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
const root = await mkdtemp(join(tmpdir(), "hashline-config-"));
|
|
38
|
+
try {
|
|
39
|
+
await mkdir(join(root, ".pi"));
|
|
40
|
+
const path = join(root, ".pi", "settings.json");
|
|
41
|
+
for (const value of [3.5, -1, "4", null]) {
|
|
42
|
+
await writeFile(
|
|
43
|
+
path,
|
|
44
|
+
JSON.stringify({ hashlineEdit: { hashLen: value, shiftRadius: value } }),
|
|
45
|
+
);
|
|
46
|
+
assert.deepEqual(loadConfig(root), {
|
|
47
|
+
enabled: true,
|
|
48
|
+
actionFusion: true,
|
|
49
|
+
hashLen: 4,
|
|
50
|
+
shiftRadius: 15,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
for (const rootValue of [
|
|
54
|
+
null,
|
|
55
|
+
[],
|
|
56
|
+
42,
|
|
57
|
+
"settings",
|
|
58
|
+
{ hashlineEdit: [] },
|
|
59
|
+
{ hashlineEdit: "invalid" },
|
|
60
|
+
]) {
|
|
61
|
+
await writeFile(path, JSON.stringify(rootValue));
|
|
62
|
+
const config = loadConfig(root);
|
|
63
|
+
assert.ok(Number.isInteger(config.hashLen));
|
|
64
|
+
assert.ok(Number.isInteger(config.shiftRadius));
|
|
65
|
+
}
|
|
66
|
+
await writeFile(path, JSON.stringify({ hashlineEdit: { hashLen: 8, shiftRadius: 0 } }));
|
|
67
|
+
assert.equal(loadConfig(root).hashLen, 8);
|
|
68
|
+
assert.equal(loadConfig(root).shiftRadius, 0);
|
|
69
|
+
} finally {
|
|
70
|
+
await rm(root, { recursive: true, force: true });
|
|
71
|
+
}
|
|
42
72
|
});
|
package/src/pi/config.ts
CHANGED
|
@@ -9,29 +9,40 @@
|
|
|
9
9
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import * as fs from "node:fs";
|
|
11
11
|
import * as path from "node:path";
|
|
12
|
+
import { HASH_LEN_MAX, HASH_LEN_MIN } from "../core/hash.ts";
|
|
13
|
+
import { isIntegerInRange } from "./schema.ts";
|
|
12
14
|
|
|
13
15
|
export interface HashlineEditConfig {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
/** Master switch: when false the extension registers no tools — pi's built-ins remain. */
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
/** Expose optional commands after edit/replace/write. Enabled by default; false disables them. */
|
|
19
|
+
actionFusion: boolean;
|
|
20
|
+
/** Line hash length (default 4). */
|
|
21
|
+
hashLen: number;
|
|
22
|
+
/** First-pass ±line radius before full-file recovery (default 15; 0 disables recovery). */
|
|
23
|
+
shiftRadius: number;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
const SHIFT_RADIUS_MAX = 100;
|
|
27
|
+
|
|
28
|
+
export const DEFAULT_CONFIG: HashlineEditConfig = {
|
|
29
|
+
enabled: true,
|
|
30
|
+
actionFusion: true,
|
|
31
|
+
hashLen: 4,
|
|
32
|
+
shiftRadius: 15,
|
|
33
|
+
};
|
|
25
34
|
|
|
26
35
|
/** Parse JSON directly without stripping comments (standard JSON forbids comments; on error fall back to default). */
|
|
27
36
|
function readSettings(filePath: string): Record<string, unknown> {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
try {
|
|
38
|
+
if (!fs.existsSync(filePath)) return {};
|
|
39
|
+
const value: unknown = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
40
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
41
|
+
? (value as Record<string, unknown>)
|
|
42
|
+
: {};
|
|
43
|
+
} catch {
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
35
46
|
}
|
|
36
47
|
|
|
37
48
|
/**
|
|
@@ -39,20 +50,22 @@ function readSettings(filePath: string): Record<string, unknown> {
|
|
|
39
50
|
* the global one wholesale; missing fields fall back to DEFAULT_CONFIG.
|
|
40
51
|
*/
|
|
41
52
|
export function loadConfig(cwd?: string): HashlineEditConfig {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
const globalSettings = readSettings(path.join(getAgentDir(), "settings.json"));
|
|
54
|
+
const projectSettings = cwd ? readSettings(path.join(cwd, ".pi", "settings.json")) : {};
|
|
55
|
+
const value = projectSettings.hashlineEdit ?? globalSettings.hashlineEdit;
|
|
56
|
+
const raw =
|
|
57
|
+
value !== null && typeof value === "object" && !Array.isArray(value)
|
|
58
|
+
? (value as Record<string, unknown>)
|
|
59
|
+
: {};
|
|
60
|
+
return {
|
|
61
|
+
enabled: typeof raw.enabled === "boolean" ? raw.enabled : DEFAULT_CONFIG.enabled,
|
|
62
|
+
actionFusion:
|
|
63
|
+
typeof raw.actionFusion === "boolean" ? raw.actionFusion : DEFAULT_CONFIG.actionFusion,
|
|
64
|
+
hashLen: isIntegerInRange(raw.hashLen, HASH_LEN_MIN, HASH_LEN_MAX)
|
|
65
|
+
? raw.hashLen
|
|
66
|
+
: DEFAULT_CONFIG.hashLen,
|
|
67
|
+
shiftRadius: isIntegerInRange(raw.shiftRadius, 0, SHIFT_RADIUS_MAX)
|
|
68
|
+
? raw.shiftRadius
|
|
69
|
+
: DEFAULT_CONFIG.shiftRadius,
|
|
70
|
+
};
|
|
58
71
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { commitFile, FileMutationError } from "./file-commit.ts";
|
|
8
|
+
|
|
9
|
+
test("macOS attempts directory sync, tolerates unsupported operations, and preserves published I/O failures", async (t) => {
|
|
10
|
+
const directory = await fs.mkdtemp(join(tmpdir(), "hashline-directory-sync-"));
|
|
11
|
+
const nativeOpen = fs.open;
|
|
12
|
+
const platform = Object.getOwnPropertyDescriptor(process, "platform")!;
|
|
13
|
+
let syncs = 0;
|
|
14
|
+
let closes = 0;
|
|
15
|
+
let failure: string | undefined;
|
|
16
|
+
try {
|
|
17
|
+
Object.defineProperty(process, "platform", { value: "darwin" });
|
|
18
|
+
t.mock.method(fs, "open", async (...args: Parameters<typeof fs.open>) => {
|
|
19
|
+
if (args[0] !== directory) return nativeOpen(...args);
|
|
20
|
+
return {
|
|
21
|
+
async sync() {
|
|
22
|
+
syncs++;
|
|
23
|
+
if (failure) throw Object.assign(new Error(failure), { code: failure });
|
|
24
|
+
},
|
|
25
|
+
async close() {
|
|
26
|
+
closes++;
|
|
27
|
+
},
|
|
28
|
+
} as Awaited<ReturnType<typeof fs.open>>;
|
|
29
|
+
});
|
|
30
|
+
syncBuiltinESMExports();
|
|
31
|
+
for (failure of [undefined, "EINVAL", "ENOTSUP", "EIO"]) {
|
|
32
|
+
const path = join(directory, failure ?? "success");
|
|
33
|
+
const commit = commitFile(path, "published\n");
|
|
34
|
+
if (failure === "EIO") {
|
|
35
|
+
await assert.rejects(
|
|
36
|
+
commit,
|
|
37
|
+
(error: unknown) =>
|
|
38
|
+
error instanceof FileMutationError &&
|
|
39
|
+
error.publication === "PUBLISHED" &&
|
|
40
|
+
/EIO/.test(error.message),
|
|
41
|
+
);
|
|
42
|
+
} else {
|
|
43
|
+
assert.equal((await commit).publication, "PUBLISHED");
|
|
44
|
+
}
|
|
45
|
+
assert.equal(await fs.readFile(path, "utf8"), "published\n");
|
|
46
|
+
}
|
|
47
|
+
assert.equal(syncs, 4);
|
|
48
|
+
assert.equal(closes, 4);
|
|
49
|
+
} finally {
|
|
50
|
+
Object.defineProperty(process, "platform", platform);
|
|
51
|
+
t.mock.restoreAll();
|
|
52
|
+
syncBuiltinESMExports();
|
|
53
|
+
await fs.rm(directory, { recursive: true, force: true });
|
|
54
|
+
}
|
|
55
|
+
});
|