@criogaid/pi-hashline-edit 0.4.0 → 0.5.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 +116 -20
- package/package.json +3 -3
- package/src/core/apply.test.ts +194 -104
- package/src/core/apply.ts +6 -37
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +8 -8
- package/src/core/hash.ts +5 -5
- package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
- package/src/core/text.ts +1 -3
- package/src/core/types.ts +4 -7
- package/src/index.ts +9 -11
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +73 -60
- package/src/integration/large-text.test.ts +9 -8
- package/src/integration/replace-package.test.ts +11 -7
- package/src/pi/action-fusion.test.ts +110 -34
- package/src/pi/action-fusion.ts +77 -114
- package/src/pi/anchor-format.test.ts +43 -22
- package/src/pi/anchor-format.ts +23 -8
- package/src/pi/budgets.ts +7 -2
- package/src/pi/config.test.ts +153 -40
- package/src/pi/config.ts +87 -41
- package/src/pi/e2e-scenarios.test.ts +12 -11
- package/src/pi/edit-tool.ts +20 -156
- package/src/pi/error-text.ts +10 -5
- package/src/pi/execute.test.ts +299 -175
- package/src/pi/failure-context.test.ts +7 -2
- package/src/pi/failure-context.ts +155 -8
- package/src/pi/failure-path.test.ts +33 -26
- package/src/pi/file-commit.ts +2 -1
- package/src/pi/fusion-card.ts +83 -2
- package/src/pi/grep-output.ts +26 -9
- package/src/pi/grep-render.ts +6 -5
- package/src/pi/grep-scope.ts +2 -15
- package/src/pi/grep-search.ts +29 -15
- package/src/pi/grep-tool.ts +94 -78
- package/src/pi/grep.test.ts +357 -320
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +51 -2
- package/src/pi/mutation-result.ts +62 -55
- package/src/pi/mutation-runner.ts +16 -33
- package/src/pi/pi.test.ts +10 -5
- package/src/pi/read-tool.ts +61 -33
- package/src/pi/registration.test.ts +1 -5
- package/src/pi/render.test.ts +16 -8
- package/src/pi/render.ts +4 -91
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +24 -58
- package/src/pi/replace-worker.ts +2 -2
- package/src/pi/replace.test.ts +62 -58
- package/src/pi/result-safety.test.ts +124 -84
- package/src/pi/rg-line-ranges.ts +3 -2
- package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +35 -4
- package/src/pi/rg-text-view.ts +17 -33
- package/src/pi/schema.ts +2 -10
- package/src/pi/write-tool.ts +1 -2
- package/src/pi/write.test.ts +10 -9
- package/src/core/index.ts +0 -14
- package/src/pi/state.ts +0 -26
package/src/pi/anchor-format.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
2
|
-
|
|
1
|
+
import { computeLineHash, HASH_ALPHABET, HASH_LEN_MIN, HASH_LEN_MAX } from "../core/hash.ts";
|
|
2
|
+
|
|
3
|
+
function anchorSource(hashQuantifier: string): string {
|
|
4
|
+
return `([1-9][0-9]*)#([${HASH_ALPHABET}]${hashQuantifier})`;
|
|
5
|
+
}
|
|
3
6
|
|
|
4
7
|
/** Schema pattern for a `LINE#HASH` anchor whose hash has exactly `hashLen` characters. */
|
|
5
8
|
export function anchorPattern(hashLen: number): string {
|
|
6
|
-
return
|
|
9
|
+
return `^${anchorSource(`{${hashLen}}`)}$`;
|
|
7
10
|
}
|
|
8
11
|
|
|
9
|
-
const ANCHOR_TOKEN =
|
|
12
|
+
const ANCHOR_TOKEN = new RegExp(`^${anchorSource("+")}$`);
|
|
13
|
+
const HASHLINE_ROW = new RegExp(`^${anchorSource(`{${HASH_LEN_MIN},${HASH_LEN_MAX}}`)}│(.*)$`);
|
|
10
14
|
|
|
11
15
|
/** Split a `LINE#HASH` token of any hash length; undefined when the shape is wrong. */
|
|
12
16
|
export function parseAnchorToken(value: string): { line: number; hash: string } | undefined {
|
|
@@ -14,9 +18,21 @@ export function parseAnchorToken(value: string): { line: number; hash: string }
|
|
|
14
18
|
return match ? { line: Number(match[1]), hash: match[2]! } : undefined;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
export interface HashlineRow {
|
|
22
|
+
/** Line number as written in the anchor (string form). */
|
|
23
|
+
lineNo: string;
|
|
24
|
+
/** Line content with the `LINE#HASH│` prefix removed. */
|
|
25
|
+
content: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Parse a display row at any supported hash length; return null for non-anchor text. */
|
|
29
|
+
export function parseHashline(line: string): HashlineRow | null {
|
|
30
|
+
const match = HASHLINE_ROW.exec(line);
|
|
31
|
+
return match ? { lineNo: match[1], content: match[3] } : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Anchor serialization bound to a caller-supplied hash length. */
|
|
18
35
|
export interface AnchorFormatter {
|
|
19
|
-
readonly hashLen: number;
|
|
20
36
|
reference(line: number, hash: string): string;
|
|
21
37
|
token(line: number, content: string): string;
|
|
22
38
|
row(line: number, content: string, displayContent?: string): string;
|
|
@@ -27,12 +43,11 @@ export function displayCarriageReturns(text: string): string {
|
|
|
27
43
|
return text.replace(/\r/g, "␍");
|
|
28
44
|
}
|
|
29
45
|
|
|
30
|
-
export function createAnchorFormatter(hashLen
|
|
46
|
+
export function createAnchorFormatter(hashLen: number): AnchorFormatter {
|
|
31
47
|
const reference = (line: number, hash: string) => `${line}#${hash}`;
|
|
32
48
|
const token = (line: number, content: string) =>
|
|
33
49
|
reference(line, computeLineHash(line, content, hashLen));
|
|
34
50
|
return {
|
|
35
|
-
hashLen,
|
|
36
51
|
token,
|
|
37
52
|
reference,
|
|
38
53
|
row: (line, content, displayContent = content) =>
|
package/src/pi/budgets.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Fixed model-facing output budgets.
|
|
3
3
|
*
|
|
4
4
|
* README "Output budgets" documents each value; change both together. Result
|
|
5
|
-
* and notice text derive their size wording from these constants.
|
|
5
|
+
* and notice text derive their size wording from these constants. Budgets that
|
|
6
|
+
* users can configure (read line and byte limits, grep line limit) live in the
|
|
7
|
+
* settings schema in config.ts; Pi's own total-output limit stays with Pi.
|
|
6
8
|
*
|
|
7
9
|
* @module pi-hashline-edit/pi
|
|
8
10
|
*/
|
|
9
11
|
|
|
12
|
+
/** grep line preview, in UTF-16 units, excluding its partial-line label. */
|
|
13
|
+
export const GREP_MAX_LINE_LENGTH = 500;
|
|
14
|
+
|
|
10
15
|
/** Each diagnostic or anchor block: failure details, input-anchor checks, candidate neighborhoods, mutation anchors. */
|
|
11
16
|
export const MAX_BLOCK_BYTES = 16 * 1024;
|
|
12
17
|
/** One recovery-candidate or cited row inside a failure diagnostic. */
|
package/src/pi/config.test.ts
CHANGED
|
@@ -3,12 +3,45 @@ import { tmpdir } from "node:os";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import assert from "node:assert/strict";
|
|
5
5
|
import { test } from "node:test";
|
|
6
|
-
import { loadConfig } from "./config.ts";
|
|
6
|
+
import { DEFAULT_CONFIG, loadConfig } from "./config.ts";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
async function withSettings(
|
|
9
|
+
fn: (
|
|
10
|
+
write: (settings: unknown) => Promise<void>,
|
|
11
|
+
root: string,
|
|
12
|
+
agentDir: string,
|
|
13
|
+
) => Promise<void>,
|
|
14
|
+
) {
|
|
9
15
|
const root = await mkdtemp(join(tmpdir(), "hashline-config-"));
|
|
16
|
+
const agentDir = join(root, "agent");
|
|
17
|
+
const previousAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
10
18
|
try {
|
|
19
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
20
|
+
await mkdir(agentDir);
|
|
11
21
|
await mkdir(join(root, ".pi"));
|
|
22
|
+
const path = join(root, ".pi", "settings.json");
|
|
23
|
+
await fn((settings) => writeFile(path, JSON.stringify(settings)), root, agentDir);
|
|
24
|
+
} finally {
|
|
25
|
+
if (previousAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
26
|
+
else process.env.PI_CODING_AGENT_DIR = previousAgentDir;
|
|
27
|
+
await rm(root, { recursive: true, force: true });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("defaults come from the settings schema", () => {
|
|
32
|
+
assert.deepEqual(DEFAULT_CONFIG, {
|
|
33
|
+
enabled: true,
|
|
34
|
+
actionFusion: true,
|
|
35
|
+
hashLen: 4,
|
|
36
|
+
shiftRadius: 15,
|
|
37
|
+
read: { defaultLimit: 500, maxKiB: 256 },
|
|
38
|
+
grep: { defaultLimit: 100, defaultContext: 0 },
|
|
39
|
+
replace: { regexTimeoutMs: 5_000 },
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("actionFusion defaults on and honors explicit disabling", () =>
|
|
44
|
+
withSettings(async (write, root) => {
|
|
12
45
|
for (const [actionFusion, expected] of [
|
|
13
46
|
[undefined, true],
|
|
14
47
|
[false, false],
|
|
@@ -17,38 +50,16 @@ test("actionFusion defaults on and honors explicit disabling", async () => {
|
|
|
17
50
|
["false", true],
|
|
18
51
|
[0, true],
|
|
19
52
|
]) {
|
|
20
|
-
await
|
|
21
|
-
|
|
22
|
-
JSON.stringify({ hashlineEdit: { actionFusion } }),
|
|
23
|
-
);
|
|
24
|
-
assert.deepEqual(loadConfig(root), {
|
|
25
|
-
enabled: true,
|
|
26
|
-
actionFusion: expected,
|
|
27
|
-
hashLen: 4,
|
|
28
|
-
shiftRadius: 15,
|
|
29
|
-
});
|
|
53
|
+
await write({ hashlineEdit: { actionFusion } });
|
|
54
|
+
assert.deepEqual(loadConfig(root), { ...DEFAULT_CONFIG, actionFusion: expected });
|
|
30
55
|
}
|
|
31
|
-
}
|
|
32
|
-
await rm(root, { recursive: true, force: true });
|
|
33
|
-
}
|
|
34
|
-
});
|
|
56
|
+
}));
|
|
35
57
|
|
|
36
|
-
test("config rejects fractional values and tolerates malformed settings shapes",
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
await mkdir(join(root, ".pi"));
|
|
40
|
-
const path = join(root, ".pi", "settings.json");
|
|
58
|
+
test("config rejects fractional values and tolerates malformed settings shapes", () =>
|
|
59
|
+
withSettings(async (write, root) => {
|
|
41
60
|
for (const value of [3.5, -1, "4", null]) {
|
|
42
|
-
await
|
|
43
|
-
|
|
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
|
-
});
|
|
61
|
+
await write({ hashlineEdit: { hashLen: value, shiftRadius: value } });
|
|
62
|
+
assert.deepEqual(loadConfig(root), DEFAULT_CONFIG);
|
|
52
63
|
}
|
|
53
64
|
for (const rootValue of [
|
|
54
65
|
null,
|
|
@@ -58,15 +69,117 @@ test("config rejects fractional values and tolerates malformed settings shapes",
|
|
|
58
69
|
{ hashlineEdit: [] },
|
|
59
70
|
{ hashlineEdit: "invalid" },
|
|
60
71
|
]) {
|
|
61
|
-
await
|
|
62
|
-
|
|
63
|
-
assert.ok(Number.isInteger(config.hashLen));
|
|
64
|
-
assert.ok(Number.isInteger(config.shiftRadius));
|
|
72
|
+
await write(rootValue);
|
|
73
|
+
assert.deepEqual(loadConfig(root), DEFAULT_CONFIG);
|
|
65
74
|
}
|
|
66
|
-
await
|
|
75
|
+
await write({ hashlineEdit: { hashLen: 8, shiftRadius: 0 } });
|
|
67
76
|
assert.equal(loadConfig(root).hashLen, 8);
|
|
68
77
|
assert.equal(loadConfig(root).shiftRadius, 0);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
}));
|
|
79
|
+
|
|
80
|
+
test("tool groups resolve each setting independently", () =>
|
|
81
|
+
withSettings(async (write, root) => {
|
|
82
|
+
await write({
|
|
83
|
+
hashlineEdit: {
|
|
84
|
+
read: { defaultLimit: 50, maxKiB: 0 },
|
|
85
|
+
grep: { defaultContext: 21, defaultLimit: 7 },
|
|
86
|
+
replace: "fast",
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
assert.deepEqual(loadConfig(root), {
|
|
90
|
+
...DEFAULT_CONFIG,
|
|
91
|
+
read: { defaultLimit: 50, maxKiB: DEFAULT_CONFIG.read.maxKiB },
|
|
92
|
+
grep: { defaultLimit: 7, defaultContext: DEFAULT_CONFIG.grep.defaultContext },
|
|
93
|
+
});
|
|
94
|
+
await write({ hashlineEdit: { replace: { regexTimeoutMs: 20_000 } } });
|
|
95
|
+
assert.equal(loadConfig(root).replace.regexTimeoutMs, 20_000);
|
|
96
|
+
}));
|
|
97
|
+
|
|
98
|
+
test("project settings replace global groups while absent project settings use global values", () =>
|
|
99
|
+
withSettings(async (write, root, agentDir) => {
|
|
100
|
+
const globalConfig = {
|
|
101
|
+
...DEFAULT_CONFIG,
|
|
102
|
+
enabled: false,
|
|
103
|
+
actionFusion: false,
|
|
104
|
+
hashLen: 8,
|
|
105
|
+
shiftRadius: 0,
|
|
106
|
+
read: { defaultLimit: 7, maxKiB: 2 },
|
|
107
|
+
grep: { defaultLimit: 3, defaultContext: 2 },
|
|
108
|
+
replace: { regexTimeoutMs: 1_000 },
|
|
109
|
+
};
|
|
110
|
+
await writeFile(
|
|
111
|
+
join(agentDir, "settings.json"),
|
|
112
|
+
JSON.stringify({ hashlineEdit: globalConfig }),
|
|
113
|
+
);
|
|
114
|
+
assert.deepEqual(loadConfig(), globalConfig);
|
|
115
|
+
assert.deepEqual(loadConfig(root), globalConfig);
|
|
116
|
+
for (const settings of [{}, null, [], 42, "settings"]) {
|
|
117
|
+
await write(settings);
|
|
118
|
+
assert.deepEqual(loadConfig(root), globalConfig);
|
|
119
|
+
}
|
|
120
|
+
await write({ hashlineEdit: { read: { defaultLimit: 2 } } });
|
|
121
|
+
assert.deepEqual(loadConfig(root), {
|
|
122
|
+
...DEFAULT_CONFIG,
|
|
123
|
+
read: { defaultLimit: 2, maxKiB: DEFAULT_CONFIG.read.maxKiB },
|
|
124
|
+
});
|
|
125
|
+
await write({ hashlineEdit: {} });
|
|
126
|
+
assert.deepEqual(loadConfig(root), DEFAULT_CONFIG);
|
|
127
|
+
}));
|
|
128
|
+
|
|
129
|
+
test("invalid tool groups default independently without discarding valid sibling groups", () =>
|
|
130
|
+
withSettings(async (write, root) => {
|
|
131
|
+
const configured = {
|
|
132
|
+
...DEFAULT_CONFIG,
|
|
133
|
+
enabled: false,
|
|
134
|
+
hashLen: 6,
|
|
135
|
+
shiftRadius: 0,
|
|
136
|
+
read: { defaultLimit: 2, maxKiB: 1 },
|
|
137
|
+
grep: { defaultLimit: 3, defaultContext: 2 },
|
|
138
|
+
replace: { regexTimeoutMs: 1_000 },
|
|
139
|
+
};
|
|
140
|
+
for (const group of ["read", "grep", "replace"] as const) {
|
|
141
|
+
for (const value of [null, [], 42, "invalid", true]) {
|
|
142
|
+
await write({ hashlineEdit: { ...configured, [group]: value } });
|
|
143
|
+
assert.deepEqual(loadConfig(root), { ...configured, [group]: DEFAULT_CONFIG[group] });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}));
|
|
147
|
+
|
|
148
|
+
test("tool settings accept their boundary values and default invalid leaves", () =>
|
|
149
|
+
withSettings(async (write, root) => {
|
|
150
|
+
const cases = [
|
|
151
|
+
{
|
|
152
|
+
group: "read",
|
|
153
|
+
key: "defaultLimit",
|
|
154
|
+
valid: [1, Number.MAX_SAFE_INTEGER],
|
|
155
|
+
invalid: [0, -1, 1.5, Number.MAX_SAFE_INTEGER + 1, "2", null],
|
|
156
|
+
},
|
|
157
|
+
{ group: "read", key: "maxKiB", valid: [1, 4096], invalid: [0, 4097, 1.5, "2", null] },
|
|
158
|
+
{
|
|
159
|
+
group: "grep",
|
|
160
|
+
key: "defaultLimit",
|
|
161
|
+
valid: [1, Number.MAX_SAFE_INTEGER],
|
|
162
|
+
invalid: [0, -1, 1.5, Number.MAX_SAFE_INTEGER + 1, "2", null],
|
|
163
|
+
},
|
|
164
|
+
{ group: "grep", key: "defaultContext", valid: [0, 20], invalid: [-1, 21, 1.5, "2", null] },
|
|
165
|
+
{
|
|
166
|
+
group: "replace",
|
|
167
|
+
key: "regexTimeoutMs",
|
|
168
|
+
valid: [1_000, 300_000],
|
|
169
|
+
invalid: [999, 300_001, 1000.5, "1000", null],
|
|
170
|
+
},
|
|
171
|
+
] as const;
|
|
172
|
+
for (const { group, key, valid, invalid } of cases) {
|
|
173
|
+
for (const value of valid) {
|
|
174
|
+
await write({ hashlineEdit: { [group]: { [key]: value } } });
|
|
175
|
+
assert.deepEqual(loadConfig(root), {
|
|
176
|
+
...DEFAULT_CONFIG,
|
|
177
|
+
[group]: { ...DEFAULT_CONFIG[group], [key]: value },
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
for (const value of invalid) {
|
|
181
|
+
await write({ hashlineEdit: { [group]: { [key]: value } } });
|
|
182
|
+
assert.deepEqual(loadConfig(root), DEFAULT_CONFIG);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}));
|
package/src/pi/config.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Config loading: project `.pi/settings.json` replaces global,
|
|
3
|
-
* falls back to
|
|
4
|
-
* camelCase).
|
|
2
|
+
* Config loading: project `.pi/settings.json` replaces global, and each setting
|
|
3
|
+
* that is missing or invalid falls back to its schema default. Config field
|
|
4
|
+
* `hashlineEdit` (drop the `pi-` prefix, camelCase).
|
|
5
5
|
*
|
|
6
6
|
* @module pi-hashline-edit/pi
|
|
7
7
|
*/
|
|
@@ -9,37 +9,98 @@
|
|
|
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 { Type, type Static, type TSchema } from "typebox";
|
|
13
|
+
import { Value } from "typebox/value";
|
|
12
14
|
import { HASH_LEN_MAX, HASH_LEN_MIN } from "../core/hash.ts";
|
|
13
|
-
import {
|
|
15
|
+
import { GREP_CONTEXT_RANGE, integerRange, POSITIVE_SAFE_INTEGER } from "./schema.ts";
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
hashLen: number;
|
|
22
|
-
/** First-pass ±line radius before full-file recovery (default 15; 0 disables recovery). */
|
|
23
|
-
shiftRadius: number;
|
|
17
|
+
function integerSetting(
|
|
18
|
+
range: ReturnType<typeof integerRange>,
|
|
19
|
+
value: number,
|
|
20
|
+
description: string,
|
|
21
|
+
) {
|
|
22
|
+
return Type.Number({ ...range, default: value, description });
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
/** Settings schema: the single source of setting types, bounds, and defaults. */
|
|
26
|
+
export const configSchema = Type.Object({
|
|
27
|
+
enabled: Type.Boolean({
|
|
28
|
+
default: true,
|
|
29
|
+
description:
|
|
30
|
+
"Master switch: when false the extension registers no tools; Pi's built-ins remain.",
|
|
31
|
+
}),
|
|
32
|
+
actionFusion: Type.Boolean({
|
|
33
|
+
default: true,
|
|
34
|
+
description: "Expose optional then_run commands after edit/replace/write.",
|
|
35
|
+
}),
|
|
36
|
+
hashLen: integerSetting(
|
|
37
|
+
integerRange(HASH_LEN_MIN, HASH_LEN_MAX),
|
|
38
|
+
4,
|
|
39
|
+
"Line hash length shared by every anchored tool.",
|
|
40
|
+
),
|
|
41
|
+
shiftRadius: integerSetting(
|
|
42
|
+
integerRange(0, 100),
|
|
43
|
+
15,
|
|
44
|
+
"First-pass ±line radius before full-file recovery; 0 disables recovery.",
|
|
45
|
+
),
|
|
46
|
+
read: Type.Object({
|
|
47
|
+
defaultLimit: integerSetting(
|
|
48
|
+
POSITIVE_SAFE_INTEGER,
|
|
49
|
+
500,
|
|
50
|
+
"Lines returned when limit is omitted.",
|
|
51
|
+
),
|
|
52
|
+
maxKiB: integerSetting(integerRange(1, 4096), 256, "Anchored text returned per call, in KiB."),
|
|
53
|
+
}),
|
|
54
|
+
grep: Type.Object({
|
|
55
|
+
defaultLimit: integerSetting(
|
|
56
|
+
POSITIVE_SAFE_INTEGER,
|
|
57
|
+
100,
|
|
58
|
+
"Matching lines returned when limit is omitted.",
|
|
59
|
+
),
|
|
60
|
+
defaultContext: integerSetting(
|
|
61
|
+
GREP_CONTEXT_RANGE,
|
|
62
|
+
0,
|
|
63
|
+
"Context lines shown when context is omitted.",
|
|
64
|
+
),
|
|
65
|
+
}),
|
|
66
|
+
replace: Type.Object({
|
|
67
|
+
regexTimeoutMs: integerSetting(
|
|
68
|
+
integerRange(1_000, 300_000),
|
|
69
|
+
5_000,
|
|
70
|
+
"Time limit for one regex batch, in milliseconds.",
|
|
71
|
+
),
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
27
74
|
|
|
28
|
-
export
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
75
|
+
export type HashlineEditConfig = Static<typeof configSchema>;
|
|
76
|
+
|
|
77
|
+
function asRecord(value: unknown): Record<string, unknown> {
|
|
78
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
79
|
+
? (value as Record<string, unknown>)
|
|
80
|
+
: {};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Keep each valid leaf; replace a missing or invalid leaf with its default. */
|
|
84
|
+
function resolveSetting<T extends TSchema>(schema: T, value: unknown): Static<T> {
|
|
85
|
+
if (Type.IsObject(schema)) {
|
|
86
|
+
const source = asRecord(value);
|
|
87
|
+
return Object.fromEntries(
|
|
88
|
+
Object.entries(schema.properties).map(([key, property]) => [
|
|
89
|
+
key,
|
|
90
|
+
resolveSetting(property, source[key]),
|
|
91
|
+
]),
|
|
92
|
+
) as Static<T>;
|
|
93
|
+
}
|
|
94
|
+
return (Value.Check(schema, value) ? value : Value.Create(schema)) as Static<T>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const DEFAULT_CONFIG: HashlineEditConfig = resolveSetting(configSchema, {});
|
|
34
98
|
|
|
35
99
|
/** Parse JSON directly without stripping comments (standard JSON forbids comments; on error fall back to default). */
|
|
36
100
|
function readSettings(filePath: string): Record<string, unknown> {
|
|
37
101
|
try {
|
|
38
102
|
if (!fs.existsSync(filePath)) return {};
|
|
39
|
-
|
|
40
|
-
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
41
|
-
? (value as Record<string, unknown>)
|
|
42
|
-
: {};
|
|
103
|
+
return asRecord(JSON.parse(fs.readFileSync(filePath, "utf-8")));
|
|
43
104
|
} catch {
|
|
44
105
|
return {};
|
|
45
106
|
}
|
|
@@ -47,25 +108,10 @@ function readSettings(filePath: string): Record<string, unknown> {
|
|
|
47
108
|
|
|
48
109
|
/**
|
|
49
110
|
* Load config. The `hashlineEdit` in project `cwd/.pi/settings.json` replaces
|
|
50
|
-
* the global one wholesale; missing
|
|
111
|
+
* the global one wholesale; missing or invalid settings use their defaults.
|
|
51
112
|
*/
|
|
52
113
|
export function loadConfig(cwd?: string): HashlineEditConfig {
|
|
53
114
|
const globalSettings = readSettings(path.join(getAgentDir(), "settings.json"));
|
|
54
115
|
const projectSettings = cwd ? readSettings(path.join(cwd, ".pi", "settings.json")) : {};
|
|
55
|
-
|
|
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
|
-
};
|
|
116
|
+
return resolveSetting(configSchema, projectSettings.hashlineEdit ?? globalSettings.hashlineEdit);
|
|
71
117
|
}
|
|
@@ -18,6 +18,7 @@ import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
|
18
18
|
import { computeLineHash } from "../core/hash.ts";
|
|
19
19
|
import { splitLines } from "../core/lines.ts";
|
|
20
20
|
import { callTool } from "./tool-call.testing.ts";
|
|
21
|
+
import { DEFAULT_CONFIG } from "./config.ts";
|
|
21
22
|
|
|
22
23
|
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
23
24
|
const dir = await mkdtemp(join(tmpdir(), "hl-e2e-"));
|
|
@@ -31,7 +32,7 @@ async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
|
31
32
|
const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0" });
|
|
32
33
|
|
|
33
34
|
function h(text: string, line: number) {
|
|
34
|
-
return `${line}#${computeLineHash(line, splitLines(text)[line - 1])}`;
|
|
35
|
+
return `${line}#${computeLineHash(line, splitLines(text)[line - 1], 4)}`;
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
function anchorLine(block: string, line: number) {
|
|
@@ -62,8 +63,8 @@ test("E2E Dev D1: Search-and-edit single function without separate read call", a
|
|
|
62
63
|
].join("\n") + "\n";
|
|
63
64
|
await writeFile(file, original);
|
|
64
65
|
|
|
65
|
-
const grep = makeGrepOverride(dir);
|
|
66
|
-
const edit = makeEditOverride(dir);
|
|
66
|
+
const grep = makeGrepOverride(dir, DEFAULT_CONFIG);
|
|
67
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
67
68
|
|
|
68
69
|
// Turn 1: Search with context: 3
|
|
69
70
|
const grepResult = await call(grep, {
|
|
@@ -105,7 +106,7 @@ test("E2E Dev D2: Chained multi-site edits using returned fresh anchors", async
|
|
|
105
106
|
].join("\n") + "\n";
|
|
106
107
|
await writeFile(file, original);
|
|
107
108
|
|
|
108
|
-
const edit = makeEditOverride(dir);
|
|
109
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
109
110
|
|
|
110
111
|
// Step 1: Edit version
|
|
111
112
|
const r1 = await call(edit, {
|
|
@@ -167,7 +168,7 @@ test("E2E Dev D3: Recovery from shifted anchor after external line insertion", a
|
|
|
167
168
|
].join("\n") + "\n";
|
|
168
169
|
await writeFile(file, externallyModified);
|
|
169
170
|
|
|
170
|
-
const edit = makeEditOverride(dir);
|
|
171
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
171
172
|
|
|
172
173
|
// Agent attempts edit with stale anchor
|
|
173
174
|
let candidateAnchor = "";
|
|
@@ -211,7 +212,7 @@ test("E2E Dev D4: Bulk replace with Action Fusion then_run validation", async ()
|
|
|
211
212
|
return "All checks passed";
|
|
212
213
|
});
|
|
213
214
|
|
|
214
|
-
const replace = makeReplaceTool(dir, fusionExecutor);
|
|
215
|
+
const replace = makeReplaceTool(dir, DEFAULT_CONFIG, fusionExecutor);
|
|
215
216
|
const res = await call(replace, {
|
|
216
217
|
path: "api.ts",
|
|
217
218
|
replacements: [{ find: "old_var", replace: "new_var" }],
|
|
@@ -234,7 +235,7 @@ test("E2E Dev D5: Line ending preservation and deletion successor anchor", async
|
|
|
234
235
|
const crlfText = "line1\r\nline2\r\nline3\r\nline4\r\n";
|
|
235
236
|
await writeFile(file, crlfText);
|
|
236
237
|
|
|
237
|
-
const edit = makeEditOverride(dir);
|
|
238
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
238
239
|
|
|
239
240
|
// Delete line 2: line 3 shifts to line 2 (deletion successor)
|
|
240
241
|
const res = await call(edit, {
|
|
@@ -255,7 +256,7 @@ test("E2E Dev D6: structured edit batches use one original snapshot", async () =
|
|
|
255
256
|
const file = join(dir, "batch.txt");
|
|
256
257
|
const original = "alpha\nbeta\ngamma\n";
|
|
257
258
|
await writeFile(file, original);
|
|
258
|
-
await call(makeEditOverride(dir), {
|
|
259
|
+
await call(makeEditOverride(dir, DEFAULT_CONFIG), {
|
|
259
260
|
path: "batch.txt",
|
|
260
261
|
edits: [
|
|
261
262
|
{ op: "replace", anchor: h(original, 1), body: ["ALPHA"] },
|
|
@@ -278,7 +279,7 @@ test("E2E Holdout H1: Ambiguous candidates provide distinguishing neighborhoods"
|
|
|
278
279
|
) + "\n";
|
|
279
280
|
await writeFile(file, text);
|
|
280
281
|
|
|
281
|
-
const edit = makeEditOverride(dir);
|
|
282
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
282
283
|
// The cited content moved away from line 3 and now occurs at lines 2 and 5.
|
|
283
284
|
const staleAnchor = h("header\nheader\n return true;\n", 3);
|
|
284
285
|
let candidateAnchor = "";
|
|
@@ -321,7 +322,7 @@ test("E2E Holdout H2: Action Fusion command failure preserves file changes and a
|
|
|
321
322
|
throw new Error("Syntax error on line 42");
|
|
322
323
|
});
|
|
323
324
|
|
|
324
|
-
const edit = makeEditOverride(dir, fusionExecutor);
|
|
325
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG, fusionExecutor);
|
|
325
326
|
const res = await call(edit, {
|
|
326
327
|
path: "fusion_fail.ts",
|
|
327
328
|
edits: [{ op: "replace", anchor: h("let x = 1;\n", 1), body: ["let x = 2;"] }],
|
|
@@ -343,7 +344,7 @@ test("E2E Holdout H3: UTF-8 BOM file retains BOM at byte 0 after first-line edit
|
|
|
343
344
|
const bomContent = "\uFEFFline1\nline2\n";
|
|
344
345
|
await writeFile(file, bomContent);
|
|
345
346
|
|
|
346
|
-
const edit = makeEditOverride(dir);
|
|
347
|
+
const edit = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
347
348
|
const a1 = h(bomContent, 1);
|
|
348
349
|
|
|
349
350
|
await call(edit, {
|