@criogaid/pi-hashline-edit 0.5.1 → 0.6.1
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 +51 -15
- package/package.json +6 -6
- package/src/core/apply.test.ts +66 -16
- package/src/core/apply.ts +36 -19
- package/src/core/lines.ts +3 -3
- package/src/index.ts +2 -0
- package/src/integration/forget-grep.test.ts +98 -0
- package/src/integration/fusion-dispatch.test.ts +343 -0
- package/src/integration/grep-rg.test.ts +269 -33
- package/src/integration/mutation-file-types.test.ts +44 -0
- package/src/pi/action-fusion.test.ts +71 -128
- package/src/pi/action-fusion.ts +83 -45
- package/src/pi/argument-validation.test.ts +136 -0
- package/src/pi/argument-validation.ts +102 -0
- package/src/pi/budgets.ts +4 -1
- package/src/pi/config.test.ts +1 -0
- package/src/pi/config.ts +4 -0
- package/src/pi/directory-sync.test.ts +1 -1
- package/src/pi/edit-tool.ts +55 -36
- package/src/pi/execute.test.ts +185 -645
- package/src/pi/extension-loading.test.ts +34 -0
- package/src/pi/extension-loading.testing.ts +15 -0
- package/src/pi/failure-context.test.ts +4 -3
- package/src/pi/failure-path.test.ts +3 -3
- package/src/pi/file-commit.test.ts +24 -17
- package/src/pi/file-commit.ts +36 -9
- package/src/pi/file-snapshot.test.ts +24 -0
- package/src/pi/forget-tool.ts +268 -0
- package/src/pi/forget.test.ts +343 -0
- package/src/pi/forget.testing.ts +273 -0
- package/src/pi/grep-output.ts +57 -28
- package/src/pi/grep-search.ts +35 -12
- package/src/pi/grep-tool.ts +28 -9
- package/src/pi/grep.test.ts +173 -152
- package/src/pi/mutation-result.test.ts +23 -0
- package/src/pi/mutation-runner.ts +8 -4
- package/src/pi/path.ts +16 -2
- package/src/pi/pi.test.ts +3 -2
- package/src/pi/read-tool.ts +58 -26
- package/src/pi/read.test.ts +361 -0
- package/src/pi/registration.test.ts +115 -2
- package/src/pi/render.test.ts +45 -14
- package/src/pi/replace-tool.ts +18 -27
- package/src/pi/replace.test.ts +8 -107
- package/src/pi/result-safety.test.ts +123 -149
- package/src/pi/rg-match-bytes.ts +95 -0
- package/src/pi/rg-process.test.ts +40 -59
- package/src/pi/rg-text-view.ts +52 -5
- package/src/pi/tool-call.testing.ts +50 -0
- package/src/pi/tool-prompts.ts +15 -0
- package/src/pi/write-tool.ts +17 -26
- package/src/pi/write.test.ts +70 -61
- package/src/pi/e2e-scenarios.test.ts +0 -361
package/src/pi/read-tool.ts
CHANGED
|
@@ -16,24 +16,32 @@ import {
|
|
|
16
16
|
highlightCode,
|
|
17
17
|
type ReadToolInput,
|
|
18
18
|
type ReadToolDetails,
|
|
19
|
-
type
|
|
19
|
+
type ExtensionToolContext,
|
|
20
20
|
type Theme,
|
|
21
21
|
type ToolDefinition,
|
|
22
22
|
} from "@earendil-works/pi-coding-agent";
|
|
23
23
|
import type { AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
|
|
24
24
|
import { Text } from "@earendil-works/pi-tui";
|
|
25
|
+
import { Type } from "typebox";
|
|
25
26
|
import { scanTextLines } from "./text-stream.ts";
|
|
26
27
|
import { createAnchorFormatter, displayCarriageReturns, parseHashline } from "./anchor-format.ts";
|
|
27
28
|
import { canonicalPath } from "./path.ts";
|
|
28
29
|
import { renderToolError } from "./render.ts";
|
|
30
|
+
import {
|
|
31
|
+
type ForgetReceiptDetails,
|
|
32
|
+
readReceipt,
|
|
33
|
+
withoutResultTag,
|
|
34
|
+
withResultTag,
|
|
35
|
+
} from "./forget-tool.ts";
|
|
29
36
|
import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
|
|
30
37
|
import { throwIfCancelled } from "./error-text.ts";
|
|
31
38
|
import { formatKiB } from "./budgets.ts";
|
|
32
39
|
import type { HashlineEditConfig } from "./config.ts";
|
|
40
|
+
import { createArgumentPreparer } from "./argument-validation.ts";
|
|
33
41
|
|
|
34
42
|
const DEFAULT_OFFSET = 1;
|
|
35
43
|
|
|
36
|
-
type ReadDetails = ReadToolDetails & { nativeRead?: true };
|
|
44
|
+
type ReadDetails = ReadToolDetails & { nativeRead?: true } & ForgetReceiptDetails;
|
|
37
45
|
|
|
38
46
|
/**
|
|
39
47
|
* First result line: `<path> · <N> lines`, optionally ` (from line <offset>)` and
|
|
@@ -121,24 +129,26 @@ export function makeReadOverride(
|
|
|
121
129
|
const { defaultLimit } = config.read;
|
|
122
130
|
const maxBytes = config.read.maxKiB * 1024;
|
|
123
131
|
const builtin = createReadToolDefinition(cwd);
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
...builtin.parameters.properties,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
limit:
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
132
|
+
// A TypeBox object, not a spread of Pi's plain JSON schema: argument diagnostics pick
|
|
133
|
+
// single fields from it, which needs TypeBox's own schema kinds.
|
|
134
|
+
const parameters = Type.Object(
|
|
135
|
+
{
|
|
136
|
+
path: Type.String({ ...builtin.parameters.properties.path, minLength: 1 }),
|
|
137
|
+
offset: Type.Optional(
|
|
138
|
+
Type.Number({
|
|
139
|
+
...POSITIVE_SAFE_INTEGER,
|
|
140
|
+
description: `1-based line to start from (default ${DEFAULT_OFFSET}).`,
|
|
141
|
+
}),
|
|
142
|
+
),
|
|
143
|
+
limit: Type.Optional(
|
|
144
|
+
Type.Number({
|
|
145
|
+
...POSITIVE_SAFE_INTEGER,
|
|
146
|
+
description: `Maximum lines to read (default ${defaultLimit}).`,
|
|
147
|
+
}),
|
|
148
|
+
),
|
|
140
149
|
},
|
|
141
|
-
|
|
150
|
+
{ additionalProperties: false },
|
|
151
|
+
);
|
|
142
152
|
|
|
143
153
|
return {
|
|
144
154
|
name: "read" as const,
|
|
@@ -151,6 +161,7 @@ export function makeReadOverride(
|
|
|
151
161
|
"For large files, pass read offset and limit to read only the relevant section.",
|
|
152
162
|
],
|
|
153
163
|
parameters: parameters as typeof builtin.parameters,
|
|
164
|
+
prepareArguments: createArgumentPreparer("read", parameters),
|
|
154
165
|
renderShell: "default" as const,
|
|
155
166
|
|
|
156
167
|
renderCall: builtin.renderCall,
|
|
@@ -160,7 +171,8 @@ export function makeReadOverride(
|
|
|
160
171
|
if (isPartial) return new Text(theme.fg("warning", "Reading…"), 0, 0);
|
|
161
172
|
const content = result.content?.[0];
|
|
162
173
|
if (context?.isError) return renderToolError(result, theme, expanded);
|
|
163
|
-
if (result.details?.nativeRead)
|
|
174
|
+
if (result.details?.nativeRead)
|
|
175
|
+
return builtin.renderResult!(withoutResultTag(result), options, theme, context);
|
|
164
176
|
// Collapsed (not expanded): show nothing — the call line carries the
|
|
165
177
|
// title, matching the built-in read's fold behavior.
|
|
166
178
|
if (!expanded) return new Text("", 0, 0);
|
|
@@ -173,7 +185,7 @@ export function makeReadOverride(
|
|
|
173
185
|
params: ReadToolInput,
|
|
174
186
|
signal: AbortSignal | undefined,
|
|
175
187
|
onUpdate: AgentToolUpdateCallback<ReadToolDetails | undefined> | undefined,
|
|
176
|
-
ctx:
|
|
188
|
+
ctx: ExtensionToolContext,
|
|
177
189
|
) {
|
|
178
190
|
throwIfCancelled(signal);
|
|
179
191
|
const offset = params.offset ?? DEFAULT_OFFSET;
|
|
@@ -181,7 +193,16 @@ export function makeReadOverride(
|
|
|
181
193
|
const anchors = createAnchorFormatter(hashLen);
|
|
182
194
|
const readNative = async () => {
|
|
183
195
|
const result = await builtin.execute(toolCallId, params, signal, onUpdate, ctx);
|
|
184
|
-
return
|
|
196
|
+
return withResultTag(
|
|
197
|
+
toolCallId,
|
|
198
|
+
{ ...result, details: { ...result.details, nativeRead: true as const } },
|
|
199
|
+
config.forget,
|
|
200
|
+
() =>
|
|
201
|
+
readReceipt(
|
|
202
|
+
params.path,
|
|
203
|
+
result.content.some((block) => block.type === "image") ? { image: true } : undefined,
|
|
204
|
+
),
|
|
205
|
+
);
|
|
185
206
|
};
|
|
186
207
|
|
|
187
208
|
const absPath = canonicalPath(cwd, params.path as string);
|
|
@@ -265,10 +286,21 @@ export function makeReadOverride(
|
|
|
265
286
|
const header = `${formatReadHeader(params.path, stats.totalLines, start, stats.finalNewline)}\n`;
|
|
266
287
|
const body = truncation.content;
|
|
267
288
|
|
|
268
|
-
return
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
289
|
+
return withResultTag(
|
|
290
|
+
toolCallId,
|
|
291
|
+
{
|
|
292
|
+
content: [{ type: "text" as const, text: header + body + tail }],
|
|
293
|
+
details: truncation.truncated ? { truncation } : pagination ? { pagination } : undefined,
|
|
294
|
+
},
|
|
295
|
+
config.forget,
|
|
296
|
+
() =>
|
|
297
|
+
readReceipt(
|
|
298
|
+
params.path,
|
|
299
|
+
rows.length > 0
|
|
300
|
+
? { start, end: start + rows.length - 1, truncated: truncation.truncated }
|
|
301
|
+
: undefined,
|
|
302
|
+
),
|
|
303
|
+
);
|
|
272
304
|
},
|
|
273
305
|
};
|
|
274
306
|
}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
9
|
+
import { makeReadOverride } from "./read-tool.ts";
|
|
10
|
+
import { computeLineHash } from "../core/hash.ts";
|
|
11
|
+
import { callTool } from "./tool-call.testing.ts";
|
|
12
|
+
import { DEFAULT_CONFIG } from "./config.ts";
|
|
13
|
+
import { FORGET_MIN_BYTES, formatKiB } from "./budgets.ts";
|
|
14
|
+
import { taggedResultId } from "./forget.testing.ts";
|
|
15
|
+
|
|
16
|
+
const DEFAULT_READ_MAX_BYTES = DEFAULT_CONFIG.read.maxKiB * 1024;
|
|
17
|
+
|
|
18
|
+
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
19
|
+
const dir = await mkdtemp(join(tmpdir(), "hl-"));
|
|
20
|
+
try {
|
|
21
|
+
return await fn(dir);
|
|
22
|
+
} finally {
|
|
23
|
+
await rm(dir, { recursive: true, force: true });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Call a tool through Pi's argument preparation and schema validation, as production does. */
|
|
28
|
+
const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0" });
|
|
29
|
+
|
|
30
|
+
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s } as Theme;
|
|
31
|
+
|
|
32
|
+
initTheme();
|
|
33
|
+
|
|
34
|
+
test("read execute: text outputs LINE#HASH│content", async () => {
|
|
35
|
+
await withDir(async (dir) => {
|
|
36
|
+
await writeFile(join(dir, "f.txt"), "line1\nline2\n");
|
|
37
|
+
const r: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
38
|
+
const text = r.content[0];
|
|
39
|
+
assert.equal(text.type, "text");
|
|
40
|
+
assert.match(text.text, /1#[0-9A-Z]+│line1/);
|
|
41
|
+
assert.match(text.text, /2#[0-9A-Z]+│line2/);
|
|
42
|
+
assert.match(text.text, /f\.txt · 2 lines/);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("read execute: Pi-supported images without NUL bypass text decoding", async () =>
|
|
47
|
+
withDir(async (dir) => {
|
|
48
|
+
for (const [name, bytes, mime] of [
|
|
49
|
+
["picture.jpg", Buffer.from([0xff, 0xd8, 0xff, 0xd9]), "image/jpeg"],
|
|
50
|
+
["picture.gif", Buffer.from("GIF89a"), "image/gif"],
|
|
51
|
+
] as const) {
|
|
52
|
+
await writeFile(join(dir, name), bytes);
|
|
53
|
+
const result: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: name });
|
|
54
|
+
assert.ok(result.content[0].text.startsWith(`Read image file [${mime}]`));
|
|
55
|
+
assert.deepEqual(await readFile(join(dir, name)), bytes);
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
test("read execute: a missing final newline is stated in the header", async () => {
|
|
60
|
+
await withDir(async (dir) => {
|
|
61
|
+
await writeFile(join(dir, "f.txt"), "line1\nline2");
|
|
62
|
+
const bare: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
63
|
+
assert.match(bare.content[0].text, /f\.txt · 2 lines · no trailing newline/);
|
|
64
|
+
|
|
65
|
+
await writeFile(join(dir, "g.txt"), "line1\nline2\n");
|
|
66
|
+
const terminated: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "g.txt" });
|
|
67
|
+
assert.doesNotMatch(terminated.content[0].text, /no trailing newline/);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("hash length stays 4 even for runs of identical lines (no explosion)", async () => {
|
|
72
|
+
await withDir(async (dir) => {
|
|
73
|
+
const f = join(dir, "f.txt");
|
|
74
|
+
await writeFile(f, "\n\n\n\ncode\n");
|
|
75
|
+
const r: any = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "f.txt" });
|
|
76
|
+
const text: string = r.content[0].text;
|
|
77
|
+
for (const m of text.matchAll(/\d+#([0-9A-Z]+)│/g)) {
|
|
78
|
+
assert.equal(m[1].length, 4, `anchor ${m[0]} hash is not 4 chars`);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("read byte truncation counts UTF-8 and separators without cutting anchors", async () =>
|
|
84
|
+
withDir(async (dir) => {
|
|
85
|
+
const maxBytes = DEFAULT_READ_MAX_BYTES;
|
|
86
|
+
const hashLen = DEFAULT_CONFIG.hashLen;
|
|
87
|
+
const prefixBytes = Buffer.byteLength(`1#${"X".repeat(hashLen)}│`);
|
|
88
|
+
const first = "界".repeat(40000);
|
|
89
|
+
const second = "x".repeat(maxBytes - Buffer.byteLength(first) - 2 * prefixBytes);
|
|
90
|
+
await writeFile(join(dir, "large.txt"), `${first}\n${second}\n`);
|
|
91
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "large.txt" });
|
|
92
|
+
assert.equal(result.details.truncation.outputLines, 1);
|
|
93
|
+
assert.equal(result.details.truncation.truncatedBy, "bytes");
|
|
94
|
+
assert.equal(result.details.truncation.maxBytes, maxBytes);
|
|
95
|
+
assert.match(result.content[0].text, new RegExp(`truncated.*${formatKiB(maxBytes)}`));
|
|
96
|
+
assert.ok(
|
|
97
|
+
result.content[0].text.includes(`1#${computeLineHash(1, first, hashLen)}│${first}\n`),
|
|
98
|
+
);
|
|
99
|
+
assert.doesNotMatch(result.content[0].text, /\n2#/);
|
|
100
|
+
const next = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
101
|
+
path: "large.txt",
|
|
102
|
+
offset: 2,
|
|
103
|
+
limit: 1,
|
|
104
|
+
});
|
|
105
|
+
assert.ok(next.content[0].text.includes(`2#${computeLineHash(2, second, hashLen)}│${second}`));
|
|
106
|
+
}));
|
|
107
|
+
|
|
108
|
+
test("read reports an oversized first row without suggesting an ineffective retry", async () =>
|
|
109
|
+
withDir(async (dir) => {
|
|
110
|
+
await writeFile(join(dir, "long.txt"), "x".repeat(DEFAULT_READ_MAX_BYTES));
|
|
111
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "long.txt" });
|
|
112
|
+
assert.equal(result.details.truncation.firstLineExceedsLimit, true);
|
|
113
|
+
assert.equal(result.details.truncation.outputLines, 0);
|
|
114
|
+
assert.doesNotMatch(result.content[0].text, /^\d+#/m);
|
|
115
|
+
assert.doesNotMatch(result.content[0].text, /use offset\/limit/);
|
|
116
|
+
assert.match(result.content[0].text, /cannot split.*line/);
|
|
117
|
+
assert.match(result.content[0].text, /bash.*chunks.*replace/);
|
|
118
|
+
}));
|
|
119
|
+
|
|
120
|
+
test("read preserves empty files and explicit limits above the native default", async () =>
|
|
121
|
+
withDir(async (dir) => {
|
|
122
|
+
await writeFile(join(dir, "empty.txt"), "");
|
|
123
|
+
const empty = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "empty.txt" });
|
|
124
|
+
assert.match(empty.content[0].text, /0 lines/);
|
|
125
|
+
assert.equal(empty.details, undefined);
|
|
126
|
+
await writeFile(join(dir, "many.txt"), "x\n".repeat(2001));
|
|
127
|
+
const many = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
128
|
+
path: "many.txt",
|
|
129
|
+
limit: 2001,
|
|
130
|
+
});
|
|
131
|
+
assert.match(many.content[0].text, /\n2001#[0-9A-Z]+│x/);
|
|
132
|
+
assert.equal(many.details, undefined);
|
|
133
|
+
}));
|
|
134
|
+
|
|
135
|
+
test("read uses the configured default limit and respects explicit limits", async () =>
|
|
136
|
+
withDir(async (dir) => {
|
|
137
|
+
const defaultLimit = DEFAULT_CONFIG.read.defaultLimit;
|
|
138
|
+
const totalLines = defaultLimit + 100;
|
|
139
|
+
await writeFile(
|
|
140
|
+
join(dir, "large.txt"),
|
|
141
|
+
Array.from({ length: totalLines }, (_, i) => `line${i + 1}\n`).join(""),
|
|
142
|
+
);
|
|
143
|
+
for (const limit of [undefined, defaultLimit + 50]) {
|
|
144
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
145
|
+
path: "large.txt",
|
|
146
|
+
limit,
|
|
147
|
+
});
|
|
148
|
+
const end = limit ?? defaultLimit;
|
|
149
|
+
assert.match(result.content[0].text, new RegExp(`large\\.txt · ${totalLines} lines`));
|
|
150
|
+
assert.match(result.content[0].text, new RegExp(`\\n${end}#[0-9A-Z]+│line${end}`));
|
|
151
|
+
assert.doesNotMatch(result.content[0].text, new RegExp(`\\n${end + 1}#[0-9A-Z]+│`));
|
|
152
|
+
assert.deepEqual(result.details, {
|
|
153
|
+
pagination: { start: 1, end, totalLines, nextOffset: end + 1 },
|
|
154
|
+
});
|
|
155
|
+
assert.match(result.content[0].text, new RegExp(`offset ${end + 1}`));
|
|
156
|
+
}
|
|
157
|
+
}));
|
|
158
|
+
|
|
159
|
+
test("read pagination supports offset windows and stops suggesting continuation at EOF", async () =>
|
|
160
|
+
withDir(async (dir) => {
|
|
161
|
+
const totalLines = DEFAULT_CONFIG.read.defaultLimit + 100;
|
|
162
|
+
const start = 20;
|
|
163
|
+
const end = start + DEFAULT_CONFIG.read.defaultLimit - 1;
|
|
164
|
+
await writeFile(
|
|
165
|
+
join(dir, "pages.txt"),
|
|
166
|
+
Array.from({ length: totalLines }, (_, i) => `line${i + 1}\n`).join(""),
|
|
167
|
+
);
|
|
168
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
169
|
+
const page = await call(read, { path: "pages.txt", offset: start });
|
|
170
|
+
assert.match(page.content[0].text, new RegExp(`offset ${end + 1}`));
|
|
171
|
+
assert.deepEqual(page.details, {
|
|
172
|
+
pagination: { start, end, totalLines, nextOffset: end + 1 },
|
|
173
|
+
});
|
|
174
|
+
const next = await call(read, {
|
|
175
|
+
path: "pages.txt",
|
|
176
|
+
offset: page.details.pagination.nextOffset,
|
|
177
|
+
});
|
|
178
|
+
assert.match(next.content[0].text, new RegExp(`\\n${end + 1}#[0-9A-Z]+│line${end + 1}`));
|
|
179
|
+
assert.match(next.content[0].text, new RegExp(`\\n${totalLines}#[0-9A-Z]+│line${totalLines}`));
|
|
180
|
+
assert.equal(next.details, undefined);
|
|
181
|
+
assert.doesNotMatch(next.content[0].text, /to continue/);
|
|
182
|
+
for (const offset of [totalLines - DEFAULT_CONFIG.read.defaultLimit + 1, totalLines + 1]) {
|
|
183
|
+
const result = await call(read, { path: "pages.txt", offset });
|
|
184
|
+
assert.equal(result.details, undefined);
|
|
185
|
+
assert.doesNotMatch(result.content[0].text, /to continue/);
|
|
186
|
+
}
|
|
187
|
+
}));
|
|
188
|
+
|
|
189
|
+
test("read rejects noninteger and nonpositive offsets and limits before reading", async () =>
|
|
190
|
+
withDir(async (dir) => {
|
|
191
|
+
await writeFile(join(dir, "pages.txt"), "first\nsecond\nthird\n");
|
|
192
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
193
|
+
for (const key of ["offset", "limit"] as const) {
|
|
194
|
+
const schema = read.parameters.properties[key];
|
|
195
|
+
assert.equal(schema.type, "number");
|
|
196
|
+
assert.equal("minimum" in schema ? schema.minimum : undefined, 1);
|
|
197
|
+
assert.equal("multipleOf" in schema ? schema.multipleOf : undefined, 1);
|
|
198
|
+
assert.equal("maximum" in schema ? schema.maximum : undefined, Number.MAX_SAFE_INTEGER);
|
|
199
|
+
for (const invalid of [
|
|
200
|
+
0,
|
|
201
|
+
-4,
|
|
202
|
+
0.5,
|
|
203
|
+
1.5,
|
|
204
|
+
NaN,
|
|
205
|
+
Infinity,
|
|
206
|
+
-Infinity,
|
|
207
|
+
Number.MAX_SAFE_INTEGER + 1,
|
|
208
|
+
]) {
|
|
209
|
+
const params = { path: "pages.txt", [key]: invalid };
|
|
210
|
+
await assert.rejects(
|
|
211
|
+
call(read, params),
|
|
212
|
+
new RegExp(`Validation failed for tool "read":\\n {2}- ${key}: `),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
assert.deepEqual(
|
|
217
|
+
validateToolArguments(read as any, {
|
|
218
|
+
type: "toolCall",
|
|
219
|
+
id: "valid",
|
|
220
|
+
name: "read",
|
|
221
|
+
arguments: { path: "pages.txt", offset: 2, limit: 550 },
|
|
222
|
+
}),
|
|
223
|
+
{ path: "pages.txt", offset: 2, limit: 550 },
|
|
224
|
+
);
|
|
225
|
+
await assert.rejects(
|
|
226
|
+
call(read, { path: "missing.txt", offset: 1.5 }),
|
|
227
|
+
/Validation failed for tool "read"/,
|
|
228
|
+
);
|
|
229
|
+
const page = await call(read, { path: "pages.txt", offset: 2, limit: 1 });
|
|
230
|
+
assert.match(page.content[0].text, /\n2#[0-9A-Z]+│second/);
|
|
231
|
+
assert.match(page.content[0].text, /offset 3/);
|
|
232
|
+
assert.deepEqual(page.details, {
|
|
233
|
+
pagination: { start: 2, end: 2, totalLines: 3, nextOffset: 3 },
|
|
234
|
+
});
|
|
235
|
+
await writeFile(join(dir, "long.txt"), "x".repeat(DEFAULT_READ_MAX_BYTES + 1));
|
|
236
|
+
const long = await call(read, { path: "long.txt", offset: 1, limit: 1 });
|
|
237
|
+
assert.equal(long.details.truncation.firstLineExceedsLimit, true);
|
|
238
|
+
assert.equal(long.details.truncation.outputLines, 0);
|
|
239
|
+
assert.equal(long.details.truncation.maxBytes, DEFAULT_READ_MAX_BYTES);
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
test("read schema rejects empty paths and unknown fields before file access", async () =>
|
|
243
|
+
withDir(async (dir) => {
|
|
244
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
245
|
+
const invalidArgs: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
|
|
246
|
+
{ path: "" },
|
|
247
|
+
{ path: "missing.txt", offest: 3 },
|
|
248
|
+
];
|
|
249
|
+
for (const args of invalidArgs) {
|
|
250
|
+
await assert.rejects(call(read, args), /Validation failed for tool "read"/);
|
|
251
|
+
}
|
|
252
|
+
}));
|
|
253
|
+
|
|
254
|
+
test("read byte truncation takes precedence over line pagination", async () =>
|
|
255
|
+
withDir(async (dir) => {
|
|
256
|
+
await writeFile(join(dir, "large.txt"), `first\n${"x".repeat(DEFAULT_READ_MAX_BYTES)}\ntail\n`);
|
|
257
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), {
|
|
258
|
+
path: "large.txt",
|
|
259
|
+
limit: 2,
|
|
260
|
+
});
|
|
261
|
+
assert.equal(result.details.truncation.truncatedBy, "bytes");
|
|
262
|
+
assert.equal(result.details.pagination, undefined);
|
|
263
|
+
assert.equal(result.details.truncation.maxBytes, DEFAULT_READ_MAX_BYTES);
|
|
264
|
+
assert.doesNotMatch(result.content[0].text, /showing lines|to continue/);
|
|
265
|
+
}));
|
|
266
|
+
|
|
267
|
+
test("read bounds oversized selected lines while preserving truncation metadata and later anchors", async () =>
|
|
268
|
+
withDir(async (dir) => {
|
|
269
|
+
const long = "界".repeat(200_000);
|
|
270
|
+
await writeFile(join(dir, "long.txt"), `first\r\n${long}\r\nlast`);
|
|
271
|
+
const read = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
272
|
+
const result = await call(read, { path: "long.txt" });
|
|
273
|
+
assert.match(result.content[0].text, /1#[0-9A-Z]+│first/);
|
|
274
|
+
assert.doesNotMatch(result.content[0].text, /2#[0-9A-Z]+│/);
|
|
275
|
+
assert.equal(result.details.truncation.firstLineExceedsLimit, false);
|
|
276
|
+
assert.equal(result.details.truncation.outputLines, 1);
|
|
277
|
+
const expectedRows = ["first", long, "last"].map(
|
|
278
|
+
(text, index) => `${index + 1}#${computeLineHash(index + 1, text, 4)}│${text}`,
|
|
279
|
+
);
|
|
280
|
+
assert.equal(result.details.truncation.totalBytes, Buffer.byteLength(expectedRows.join("\n")));
|
|
281
|
+
const oversized = await call(read, { path: "long.txt", offset: 2, limit: 1 });
|
|
282
|
+
assert.equal(oversized.details.truncation.firstLineExceedsLimit, true);
|
|
283
|
+
assert.equal(oversized.details.truncation.outputBytes, 0);
|
|
284
|
+
const last = await call(read, { path: "long.txt", offset: 3, limit: 1 });
|
|
285
|
+
assert.match(last.content[0].text, /3#[0-9A-Z]+│last/);
|
|
286
|
+
assert.match(last.content[0].text, /no trailing newline/);
|
|
287
|
+
}));
|
|
288
|
+
|
|
289
|
+
test("read budgets visible standalone CR characters using rendered UTF-8 bytes", async () =>
|
|
290
|
+
withDir(async (dir) => {
|
|
291
|
+
const content = "\r".repeat(90_000);
|
|
292
|
+
await writeFile(join(dir, "cr.txt"), content);
|
|
293
|
+
const result = await call(makeReadOverride(dir, DEFAULT_CONFIG), { path: "cr.txt", limit: 1 });
|
|
294
|
+
assert.equal(result.details.truncation.firstLineExceedsLimit, true);
|
|
295
|
+
assert.equal(result.details.truncation.outputBytes, 0);
|
|
296
|
+
assert.equal(
|
|
297
|
+
result.details.truncation.totalBytes,
|
|
298
|
+
Buffer.byteLength(`1#${computeLineHash(1, content, 4)}│${"␍".repeat(content.length)}`),
|
|
299
|
+
);
|
|
300
|
+
}));
|
|
301
|
+
|
|
302
|
+
test("read renders native content without interpreting anchor-like prefixes", async () =>
|
|
303
|
+
withDir(async (dir) => {
|
|
304
|
+
const tool = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
305
|
+
for (const content of ["12#abc│ordinary content", "12#ABCD│ordinary content"]) {
|
|
306
|
+
for (const suffix of ["\n", "\n\0tail"]) {
|
|
307
|
+
await writeFile(join(dir, "prefix.txt"), content + suffix);
|
|
308
|
+
const result = await call(tool, { path: "prefix.txt" });
|
|
309
|
+
// Round-trip details as persisted session results do before rendering.
|
|
310
|
+
const rendered = tool.renderResult!(
|
|
311
|
+
JSON.parse(JSON.stringify(result)),
|
|
312
|
+
{ expanded: true, isPartial: false },
|
|
313
|
+
stubTheme as Theme,
|
|
314
|
+
{ args: { path: "prefix.txt" }, state: {}, cwd: dir, isError: false } as any,
|
|
315
|
+
)
|
|
316
|
+
.render(120)
|
|
317
|
+
.join("\n");
|
|
318
|
+
assert.ok(rendered.includes(content), rendered);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}));
|
|
322
|
+
|
|
323
|
+
test("read accepts safe offsets and limits whose sum exceeds the safe integer range", async () =>
|
|
324
|
+
withDir(async (dir) => {
|
|
325
|
+
await writeFile(join(dir, "range.txt"), "first\nsecond\nthird\n");
|
|
326
|
+
const tool = makeReadOverride(dir, DEFAULT_CONFIG);
|
|
327
|
+
for (const offset of [2, 3, Number.MAX_SAFE_INTEGER]) {
|
|
328
|
+
const result = await call(tool, {
|
|
329
|
+
path: "range.txt",
|
|
330
|
+
offset,
|
|
331
|
+
limit: Number.MAX_SAFE_INTEGER,
|
|
332
|
+
});
|
|
333
|
+
assert.equal(result.details, undefined);
|
|
334
|
+
assert.doesNotMatch(result.content[0].text, /to continue|│first/);
|
|
335
|
+
if (offset <= 3) assert.match(result.content[0].text, /│third/);
|
|
336
|
+
else assert.doesNotMatch(result.content[0].text, /│/);
|
|
337
|
+
}
|
|
338
|
+
}));
|
|
339
|
+
|
|
340
|
+
test("read text reaches the forget byte threshold → only eligible results receive a tag", async () =>
|
|
341
|
+
withDir(async (dir) => {
|
|
342
|
+
const path = join(dir, "threshold.txt");
|
|
343
|
+
const read = makeReadOverride(dir, { ...DEFAULT_CONFIG, forget: true });
|
|
344
|
+
await writeFile(path, "界\n");
|
|
345
|
+
const initial: Awaited<ReturnType<typeof read.execute>> = await callTool(read, { path });
|
|
346
|
+
const initialBlock = initial.content[0];
|
|
347
|
+
assert.ok(initialBlock.type === "text");
|
|
348
|
+
const initialBytes = Buffer.byteLength(initialBlock.text);
|
|
349
|
+
for (const bytes of [FORGET_MIN_BYTES - 1, FORGET_MIN_BYTES, FORGET_MIN_BYTES + 1]) {
|
|
350
|
+
await writeFile(path, `界${"x".repeat(bytes - initialBytes)}\n`);
|
|
351
|
+
const result: Awaited<ReturnType<typeof read.execute>> = await callTool(
|
|
352
|
+
read,
|
|
353
|
+
{ path },
|
|
354
|
+
{ toolCallId: `read-${bytes}` },
|
|
355
|
+
);
|
|
356
|
+
const body = result.content[0];
|
|
357
|
+
assert.ok(body.type === "text");
|
|
358
|
+
assert.equal(Buffer.byteLength(body.text), bytes);
|
|
359
|
+
assert.equal(taggedResultId(result) !== undefined, bytes >= FORGET_MIN_BYTES);
|
|
360
|
+
}
|
|
361
|
+
}));
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { createEditTool } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
3
|
+
import { makeEditOverride } from "./edit-tool.ts";
|
|
4
|
+
import { makeReadOverride } from "./read-tool.ts";
|
|
5
|
+
import { loadConfig } from "./config.ts";
|
|
6
|
+
import { DEFAULT_CONFIG } from "./config.ts";
|
|
1
7
|
import { mkdir, mkdtemp, rm, writeFile, readFile } from "node:fs/promises";
|
|
2
8
|
import { tmpdir } from "node:os";
|
|
3
9
|
import { join } from "node:path";
|
|
@@ -64,7 +70,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
|
|
|
64
70
|
}
|
|
65
71
|
initTheme("dark");
|
|
66
72
|
const cases = [
|
|
67
|
-
{ name: "write", args: { content: "published\n" } },
|
|
73
|
+
{ name: "write", args: { content: "published\n", mode: "create" } },
|
|
68
74
|
{ name: "edit", args: { edits: [{ op: "append", body: ["appended"] }] } },
|
|
69
75
|
{ name: "replace", args: { replacements: [{ find: "published", replace: "published" }] } },
|
|
70
76
|
];
|
|
@@ -266,7 +272,10 @@ test("mutation cards use Fusion by default and explicit false removes command su
|
|
|
266
272
|
disabledTools.push(tool);
|
|
267
273
|
},
|
|
268
274
|
} as any);
|
|
269
|
-
assert.
|
|
275
|
+
assert.deepEqual(
|
|
276
|
+
disabledTools.map((tool) => tool.name),
|
|
277
|
+
tools.map((tool) => tool.name),
|
|
278
|
+
);
|
|
270
279
|
for (const tool of disabledTools.filter((tool) =>
|
|
271
280
|
["edit", "replace", "write"].includes(tool.name),
|
|
272
281
|
)) {
|
|
@@ -287,3 +296,107 @@ test("mutation cards use Fusion by default and explicit false removes command su
|
|
|
287
296
|
await rm(dir, { recursive: true, force: true });
|
|
288
297
|
}
|
|
289
298
|
});
|
|
299
|
+
|
|
300
|
+
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
301
|
+
const dir = await mkdtemp(join(tmpdir(), "hl-"));
|
|
302
|
+
try {
|
|
303
|
+
return await fn(dir);
|
|
304
|
+
} finally {
|
|
305
|
+
await rm(dir, { recursive: true, force: true });
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Call a tool through Pi's argument preparation and schema validation, as production does. */
|
|
310
|
+
const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "0" });
|
|
311
|
+
|
|
312
|
+
/** Extract a `LINE#HASH` anchor from a read/edit result text block. */
|
|
313
|
+
function anchorLine(block: string, line: number) {
|
|
314
|
+
const m = new RegExp(`^${line}#([0-9A-Z]+)(?:│|$)`, "m").exec(block);
|
|
315
|
+
if (!m) throw new Error(`line ${line} anchor not found in block`);
|
|
316
|
+
return `${line}#${m[1]}`;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
test("disabled config registers no tools — built-ins remain", async () => {
|
|
320
|
+
await withDir(async (dir) => {
|
|
321
|
+
const oldCwd = process.cwd();
|
|
322
|
+
try {
|
|
323
|
+
await mkdir(join(dir, ".pi"));
|
|
324
|
+
await writeFile(
|
|
325
|
+
join(dir, ".pi", "settings.json"),
|
|
326
|
+
JSON.stringify({ hashlineEdit: { enabled: false } }),
|
|
327
|
+
);
|
|
328
|
+
await writeFile(join(dir, "f.txt"), "old value\n");
|
|
329
|
+
process.chdir(dir);
|
|
330
|
+
const registered: string[] = [];
|
|
331
|
+
registerHashline({
|
|
332
|
+
on() {},
|
|
333
|
+
registerTool(tool: { name: string }) {
|
|
334
|
+
registered.push(tool.name);
|
|
335
|
+
},
|
|
336
|
+
} as any);
|
|
337
|
+
assert.deepEqual(registered, []);
|
|
338
|
+
const builtin = createEditTool(dir);
|
|
339
|
+
const params = validateToolArguments(builtin, {
|
|
340
|
+
name: "edit",
|
|
341
|
+
arguments: { path: "f.txt", edits: [{ oldText: "old value", newText: "new value" }] },
|
|
342
|
+
} as any);
|
|
343
|
+
await call(builtin, params);
|
|
344
|
+
assert.equal(await readFile(join(dir, "f.txt"), "utf-8"), "new value\n");
|
|
345
|
+
} finally {
|
|
346
|
+
process.chdir(oldCwd);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test("loaded configuration controls hash length and recovery radius", async () =>
|
|
352
|
+
withDir(async (dir) => {
|
|
353
|
+
const text = "changed\ntarget\npadding\ntarget\n";
|
|
354
|
+
await mkdir(join(dir, ".pi"));
|
|
355
|
+
await writeFile(join(dir, "configured.txt"), text);
|
|
356
|
+
for (const [hashLen, shiftRadius, expected] of [
|
|
357
|
+
[6, 0, /no checksum-matching candidate found/],
|
|
358
|
+
[6, 1, /checksum-matching candidate 2#/],
|
|
359
|
+
[8, 3, /ambiguous checksum matches/],
|
|
360
|
+
] as const) {
|
|
361
|
+
await writeFile(
|
|
362
|
+
join(dir, ".pi", "settings.json"),
|
|
363
|
+
JSON.stringify({ hashlineEdit: { hashLen, shiftRadius } }),
|
|
364
|
+
);
|
|
365
|
+
const config = loadConfig(dir);
|
|
366
|
+
const read = await call(makeReadOverride(dir, config), { path: "configured.txt" });
|
|
367
|
+
assert.equal(anchorLine(read.content[0].text, 1).split("#")[1].length, hashLen);
|
|
368
|
+
await assert.rejects(
|
|
369
|
+
call(makeEditOverride(dir, config), {
|
|
370
|
+
path: "configured.txt",
|
|
371
|
+
edits: [{ op: "delete", anchor: `1#${computeLineHash(1, "target", hashLen)}` }],
|
|
372
|
+
}),
|
|
373
|
+
expected,
|
|
374
|
+
);
|
|
375
|
+
assert.equal(await readFile(join(dir, "configured.txt"), "utf8"), text);
|
|
376
|
+
}
|
|
377
|
+
}));
|
|
378
|
+
|
|
379
|
+
test("configured read defaults bound omitted limits and returned bytes", async () =>
|
|
380
|
+
withDir(async (dir) => {
|
|
381
|
+
const read = makeReadOverride(dir, {
|
|
382
|
+
...DEFAULT_CONFIG,
|
|
383
|
+
read: { defaultLimit: 2, maxKiB: 1 },
|
|
384
|
+
});
|
|
385
|
+
const description: unknown = Reflect.get(read.parameters.properties.limit, "description");
|
|
386
|
+
assert.ok(typeof description === "string");
|
|
387
|
+
assert.match(description, /default 2\)/);
|
|
388
|
+
await writeFile(join(dir, "short.txt"), "a\nb\nc\n");
|
|
389
|
+
const paged = await call(read, { path: "short.txt" });
|
|
390
|
+
assert.deepEqual(paged.details.pagination, { start: 1, end: 2, totalLines: 3, nextOffset: 3 });
|
|
391
|
+
assert.match(paged.content[0].text, /offset 3/);
|
|
392
|
+
assert.match(paged.content[0].text, /^2#[0-9A-Z]+│b$/m);
|
|
393
|
+
assert.doesNotMatch(paged.content[0].text, /^3#/m);
|
|
394
|
+
const explicit = await call(read, { path: "short.txt", limit: 3 });
|
|
395
|
+
assert.match(explicit.content[0].text, /^3#[0-9A-Z]+│c$/m);
|
|
396
|
+
await writeFile(join(dir, "wide.txt"), `${"x".repeat(2048)}\n`);
|
|
397
|
+
const wide = await call(read, { path: "wide.txt" });
|
|
398
|
+
assert.equal(wide.details.truncation.firstLineExceedsLimit, true);
|
|
399
|
+
assert.equal(wide.details.truncation.outputLines, 0);
|
|
400
|
+
assert.equal(wide.details.truncation.maxBytes, 1024);
|
|
401
|
+
assert.equal(wide.details.truncation.outputBytes, 0);
|
|
402
|
+
}));
|