@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { test } from "node:test";
|
|
3
|
-
import { MAX_CONTENT_KIB, outputBudgetsFor } from "./budgets.ts";
|
|
4
|
-
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
5
|
-
import { openTestSession } from "../testing/session.testing.ts";
|
|
6
|
-
import { registerModelBudgets } from "./model-budgets.ts";
|
|
7
|
-
|
|
8
|
-
test("session start and model selection → budgets follow the live model", async (t) => {
|
|
9
|
-
let get: ReturnType<typeof registerModelBudgets> | undefined;
|
|
10
|
-
const { session, model } = await openTestSession(t, {
|
|
11
|
-
tools: [],
|
|
12
|
-
configure(pi) {
|
|
13
|
-
get = registerModelBudgets(pi);
|
|
14
|
-
assert.deepEqual(get(), DEFAULT_OUTPUT_BUDGETS);
|
|
15
|
-
},
|
|
16
|
-
});
|
|
17
|
-
assert.ok(get);
|
|
18
|
-
assert.deepEqual(get(), outputBudgetsFor(model.contextWindow));
|
|
19
|
-
const selected = { ...model, id: "small", contextWindow: 65_536 };
|
|
20
|
-
await session.setModel(selected);
|
|
21
|
-
const small = get();
|
|
22
|
-
assert.ok(Object.isFrozen(small));
|
|
23
|
-
assert.deepEqual(small, outputBudgetsFor(selected.contextWindow));
|
|
24
|
-
selected.contextWindow = 1_048_576;
|
|
25
|
-
const large = get();
|
|
26
|
-
assert.deepEqual(large, outputBudgetsFor(selected.contextWindow));
|
|
27
|
-
assert.ok(large.readBytes > small.readBytes);
|
|
28
|
-
assert.ok(large.blockBytes > small.blockBytes);
|
|
29
|
-
assert.ok(large.fieldBytes > small.fieldBytes);
|
|
30
|
-
assert.ok(large.grepPreviewUnits > small.grepPreviewUnits);
|
|
31
|
-
await session.setModel({ ...model, id: "small-again", contextWindow: 65_536 });
|
|
32
|
-
assert.deepEqual(get(), small);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("unknown or invalid model capacity → bounded defaults are used", () => {
|
|
36
|
-
assert.deepEqual(DEFAULT_OUTPUT_BUDGETS, {
|
|
37
|
-
readBytes: 64 * 1024,
|
|
38
|
-
grepBytes: 50 * 1024,
|
|
39
|
-
blockBytes: 16 * 1024,
|
|
40
|
-
fieldBytes: 4 * 1024,
|
|
41
|
-
grepPreviewUnits: 500,
|
|
42
|
-
});
|
|
43
|
-
for (const capacity of [
|
|
44
|
-
undefined,
|
|
45
|
-
0,
|
|
46
|
-
-1,
|
|
47
|
-
NaN,
|
|
48
|
-
Infinity,
|
|
49
|
-
-Infinity,
|
|
50
|
-
2.5,
|
|
51
|
-
Number.MAX_SAFE_INTEGER + 1,
|
|
52
|
-
]) {
|
|
53
|
-
assert.equal(outputBudgetsFor(capacity), DEFAULT_OUTPUT_BUDGETS);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test("declared model capacity → read and grep shares have independent byte minimums and scaled previews", () => {
|
|
58
|
-
for (const [capacity, readBytes, grepBytes, grepPreviewUnits] of [
|
|
59
|
-
[1, 512, 512, 500],
|
|
60
|
-
[2_047, 512, 512, 500],
|
|
61
|
-
[2_048, 512, 512, 500],
|
|
62
|
-
[2_052, 513, 512, 500],
|
|
63
|
-
[4_095, 1_023, 512, 500],
|
|
64
|
-
[4_096, 1_024, 512, 500],
|
|
65
|
-
[4_104, 1_026, 513, 500],
|
|
66
|
-
[200_000, 50_000, 25_000, 500],
|
|
67
|
-
[384_000, 96_000, 48_000, 500],
|
|
68
|
-
[384_007, 96_001, 48_000, 500],
|
|
69
|
-
[400_000, 100_000, 50_000, 500],
|
|
70
|
-
[400_799, 100_199, 50_099, 500],
|
|
71
|
-
[400_800, 100_200, 50_100, 501],
|
|
72
|
-
[1_000_000, 250_000, 125_000, 1_250],
|
|
73
|
-
]) {
|
|
74
|
-
const budget = outputBudgetsFor(capacity);
|
|
75
|
-
const scenario = `contextWindow=${capacity}`;
|
|
76
|
-
assert.equal(budget.readBytes, readBytes, scenario);
|
|
77
|
-
assert.equal(budget.grepBytes, grepBytes, scenario);
|
|
78
|
-
assert.equal(budget.grepPreviewUnits, grepPreviewUnits, scenario);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("declared capacity reaches the ceiling → budgets use shares of the capped capacity", () => {
|
|
83
|
-
for (const capacity of [
|
|
84
|
-
MAX_CONTENT_KIB * 1024,
|
|
85
|
-
MAX_CONTENT_KIB * 1024 + 1,
|
|
86
|
-
Number.MAX_SAFE_INTEGER,
|
|
87
|
-
]) {
|
|
88
|
-
assert.deepEqual(outputBudgetsFor(capacity), {
|
|
89
|
-
readBytes: 1_048_576,
|
|
90
|
-
grepBytes: 524_288,
|
|
91
|
-
blockBytes: 1_048_576,
|
|
92
|
-
fieldBytes: 262_144,
|
|
93
|
-
grepPreviewUnits: 5_242,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
});
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
2
|
-
import { byteRevision } from "./file-revision.ts";
|
|
3
|
-
import type { MutationOutcome } from "./mutation-result.ts";
|
|
4
|
-
|
|
5
|
-
/** Build a completed overwrite fixture with matching published and observed bytes. */
|
|
6
|
-
export function publishedMutation<T extends object | undefined>(
|
|
7
|
-
content: string,
|
|
8
|
-
result: AgentToolResult<T>,
|
|
9
|
-
): MutationOutcome<T extends object ? T : object> {
|
|
10
|
-
const revision = byteRevision(Buffer.from(content));
|
|
11
|
-
return {
|
|
12
|
-
details: (result.details ?? {}) as T extends object ? T : object,
|
|
13
|
-
facts: {},
|
|
14
|
-
commit: {
|
|
15
|
-
publication: "PUBLISHED",
|
|
16
|
-
publishedRevision: revision,
|
|
17
|
-
observedRevision: revision,
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
2
|
-
import { test } from "node:test";
|
|
3
|
-
import assert from "node:assert/strict";
|
|
4
|
-
import { generateDiffString } from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import { generateMutationDetails } from "./mutation-result.ts";
|
|
6
|
-
import { displayCarriageReturns } from "./anchor-format.ts";
|
|
7
|
-
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
8
|
-
import { tmpdir } from "node:os";
|
|
9
|
-
import { join } from "node:path";
|
|
10
|
-
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
11
|
-
import { makeWriteOverride, DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
12
|
-
import { makeReplaceTool } from "./tool-factories.testing.ts";
|
|
13
|
-
import type { SuccessReport } from "./report-schema.ts";
|
|
14
|
-
import { callTool, createToolContext } from "./tool-call.testing.ts";
|
|
15
|
-
import { reportOf } from "./report.ts";
|
|
16
|
-
|
|
17
|
-
// Checked by tsc at the report and tool boundaries; these callers must never run.
|
|
18
|
-
function invalidMutationResults(
|
|
19
|
-
replacement: Extract<SuccessReport, { tool: "replace" }>,
|
|
20
|
-
result: Awaited<ReturnType<ReturnType<typeof makeReplaceTool>["execute"]>>,
|
|
21
|
-
) {
|
|
22
|
-
// @ts-expect-error Edit reports cannot carry replacement counts.
|
|
23
|
-
const countedEdit: Extract<SuccessReport, { tool: "edit" }> = { tool: "edit", matches: 1 };
|
|
24
|
-
// @ts-expect-error A replacement report cannot be used as an edit report.
|
|
25
|
-
const edit: Extract<SuccessReport, { tool: "edit" }> = replacement;
|
|
26
|
-
// @ts-expect-error A possibly failed tool result has no guaranteed diff.
|
|
27
|
-
const diff: string = result.details.diff;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
test("command content includes images → report text joins only text blocks", async (t) => {
|
|
31
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-command-content-"));
|
|
32
|
-
try {
|
|
33
|
-
const path = join(dir, "file.txt");
|
|
34
|
-
const executeTool: SessionCommand = async () =>
|
|
35
|
-
commandCompletion({
|
|
36
|
-
content: [
|
|
37
|
-
{ type: "text", text: "first" },
|
|
38
|
-
{ type: "image", data: "iVBORw0KGgo=", mimeType: "image/png" },
|
|
39
|
-
{ type: "text", text: "second" },
|
|
40
|
-
],
|
|
41
|
-
});
|
|
42
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
43
|
-
const result = await callTool(
|
|
44
|
-
makeWriteOverride(dir, fusion),
|
|
45
|
-
{ path, content: "saved\n", mode: "create", then_run: { command: "check" } },
|
|
46
|
-
{ ctx: { ...(await createToolContext(dir, t)), executeTool } },
|
|
47
|
-
);
|
|
48
|
-
assert.equal(reportOf(result, "write")?.then_run?.output, "first\nsecond");
|
|
49
|
-
assert.equal(await readFile(path, "utf8"), "saved\n");
|
|
50
|
-
} finally {
|
|
51
|
-
await rm(dir, { recursive: true, force: true });
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("oversized command diagnostic → bounds model output and retains the original command card", async (t) => {
|
|
56
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-command-budget-"));
|
|
57
|
-
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
58
|
-
const budget = DEFAULT_OUTPUT_BUDGETS;
|
|
59
|
-
const output = `opening\n${"x".repeat(budget.fieldBytes * 2)}\nlast line`;
|
|
60
|
-
const executeTool: SessionCommand = async () =>
|
|
61
|
-
commandCompletion({ content: [{ type: "text", text: output }] }, true);
|
|
62
|
-
let displayedOutput: unknown;
|
|
63
|
-
const fusion = createActionFusionExecutor((progress) => {
|
|
64
|
-
if (progress.command === "failed") displayedOutput = progress.result?.content[0];
|
|
65
|
-
});
|
|
66
|
-
const result = await callTool(
|
|
67
|
-
makeWriteOverride(dir, fusion),
|
|
68
|
-
{ path: "file.txt", content: "saved", mode: "create", then_run: { command: "check" } },
|
|
69
|
-
{ ctx: { ...(await createToolContext(dir, t)), executeTool } },
|
|
70
|
-
);
|
|
71
|
-
const report = reportOf({ content: result.content }, "write");
|
|
72
|
-
assert.equal(report?.then_run?.status, "failed");
|
|
73
|
-
const shown = report.then_run.output;
|
|
74
|
-
assert.ok(shown && Buffer.byteLength(shown) <= budget.fieldBytes);
|
|
75
|
-
assert.ok(shown.startsWith("opening\n") && shown.endsWith("\nlast line"));
|
|
76
|
-
assert.deepEqual(displayedOutput, { type: "text", text: output });
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
for (const [name, before, after] of [
|
|
80
|
-
["LF", "guard\nold\ntail\n", "guard\nnew\ntail\n"],
|
|
81
|
-
["CRLF", "guard\r\nold\r\ntail\r\n", "guard\r\nnew\r\ntail\r\n"],
|
|
82
|
-
["mixed", "guard\r\nold\ntail", "guard\r\nnew\ntail"],
|
|
83
|
-
["standalone CR", "guard\nold\rtail", "guard\nnew\rtail"],
|
|
84
|
-
["ending conversion", "same\r\n", "same\n"],
|
|
85
|
-
["empty file", "", "created"],
|
|
86
|
-
["BOM and literal escapes", "\uFEFFguard\nold\\n\\0", "\uFEFFguard\nnew\\n\\0"],
|
|
87
|
-
["no-op", "unchanged\n", "unchanged\n"],
|
|
88
|
-
] as const) {
|
|
89
|
-
test(`${name} mutation → details contain only the logical-view diff`, () => {
|
|
90
|
-
const details = generateMutationDetails(before, after);
|
|
91
|
-
const logical = generateDiffString(before.replace(/\r\n/g, "\n"), after.replace(/\r\n/g, "\n"));
|
|
92
|
-
assert.deepEqual(details, {
|
|
93
|
-
diff: displayCarriageReturns(logical.diff),
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
}
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import { Worker } from "node:worker_threads";
|
|
2
|
-
import { runRegexReplacements } from "./replace-regex.ts";
|
|
3
|
-
import assert from "node:assert/strict";
|
|
4
|
-
import { test } from "node:test";
|
|
5
|
-
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
6
|
-
import { tmpdir } from "node:os";
|
|
7
|
-
import { join } from "node:path";
|
|
8
|
-
import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import { applyEdits } from "../core/apply.ts";
|
|
10
|
-
import { HashlineError } from "../core/errors.ts";
|
|
11
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
12
|
-
import { openTestSession } from "../testing/session.testing.ts";
|
|
13
|
-
import { createAnchorFormatter } from "./anchor-format.ts";
|
|
14
|
-
import { formatArgumentError } from "./argument-error.ts";
|
|
15
|
-
import { formatByteSize, outputBudgetsFor } from "./budgets.ts";
|
|
16
|
-
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
17
|
-
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
18
|
-
import { describeEditFailure } from "./failure-context.ts";
|
|
19
|
-
import { grepOutput } from "./grep-output.ts";
|
|
20
|
-
import { registerModelBudgets } from "./model-budgets.ts";
|
|
21
|
-
import { describeMutationFailure } from "./mutation-result.ts";
|
|
22
|
-
import { reportOf } from "./report.ts";
|
|
23
|
-
import { workerFailure } from "./report-cause.ts";
|
|
24
|
-
import { callTool } from "./tool-call.testing.ts";
|
|
25
|
-
|
|
26
|
-
const small = outputBudgetsFor(65_536);
|
|
27
|
-
const large = outputBudgetsFor(1_048_576);
|
|
28
|
-
|
|
29
|
-
function wrongHash(text: string): string {
|
|
30
|
-
const hash = computeLineHash(1, text, DEFAULT_CONFIG.hashLen);
|
|
31
|
-
return (hash[0] === "0" ? "1" : "0") + hash.slice(1);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
test("byte budget sizes include fractional KiB → round to one decimal and preserve whole units", () => {
|
|
35
|
-
for (const [bytes, expected] of [
|
|
36
|
-
[outputBudgetsFor(200_000).readBytes, "48.8 KiB"],
|
|
37
|
-
[outputBudgetsFor(200_000).grepBytes, "24.4 KiB"],
|
|
38
|
-
[outputBudgetsFor(200_000).blockBytes, "48.8 KiB"],
|
|
39
|
-
[outputBudgetsFor(200_000).fieldBytes, "12.2 KiB"],
|
|
40
|
-
[16 * 1024, "16 KiB"],
|
|
41
|
-
[50 * 1024, "50 KiB"],
|
|
42
|
-
[256 * 1024, "256 KiB"],
|
|
43
|
-
[64 * 1024 * 1024, "64 MiB"],
|
|
44
|
-
[1024, "1 KiB"],
|
|
45
|
-
[1023 * 1024, "1023 KiB"],
|
|
46
|
-
[1024 * 1024, "1 MiB"],
|
|
47
|
-
[1025 * 1024, "1025 KiB"],
|
|
48
|
-
[4 * 1024 * 1024, "4 MiB"],
|
|
49
|
-
] as const) {
|
|
50
|
-
assert.equal(formatByteSize(bytes), expected);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test("model budget grows → argument issues remain complete JSON with counted omissions", () => {
|
|
55
|
-
const issues = Array.from({ length: 100 }, (_, index) => ({
|
|
56
|
-
field: `steps[${index}].lines`,
|
|
57
|
-
reason: "中文🙂".repeat(100),
|
|
58
|
-
fix: "Supply a line.",
|
|
59
|
-
}));
|
|
60
|
-
const reports = [outputBudgetsFor(1), small, large].map((budget) => {
|
|
61
|
-
const text = formatArgumentError(issues, false, budget);
|
|
62
|
-
assert.ok(Buffer.byteLength(text) <= budget.blockBytes);
|
|
63
|
-
const report = reportOf({ content: [{ type: "text", text }] }, "edit");
|
|
64
|
-
assert.ok(report);
|
|
65
|
-
assert.equal((report.issues?.length ?? 0) + (report.omittedIssues ?? 0), issues.length);
|
|
66
|
-
return report;
|
|
67
|
-
});
|
|
68
|
-
assert.ok((reports[2].issues?.length ?? 0) > (reports[1].issues?.length ?? 0));
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("model row allowance grows → anchor failures return complete observations instead of omitted rows", () => {
|
|
72
|
-
const text = "中文🙂".repeat(800);
|
|
73
|
-
const applied = applyEdits(
|
|
74
|
-
text,
|
|
75
|
-
[{ op: "delete", anchor: { line: 1, hash: wrongHash(text) } }],
|
|
76
|
-
DEFAULT_CONFIG.hashLen,
|
|
77
|
-
0,
|
|
78
|
-
);
|
|
79
|
-
assert.equal(applied.ok, false);
|
|
80
|
-
if (applied.ok) return;
|
|
81
|
-
const snapshot = { currentText: text, anchors: createAnchorFormatter(DEFAULT_CONFIG.hashLen) };
|
|
82
|
-
const reports = [small, large].map((budget) => {
|
|
83
|
-
const report = describeMutationFailure(
|
|
84
|
-
"edit",
|
|
85
|
-
describeEditFailure(snapshot.currentText, applied.failures, snapshot.anchors, budget),
|
|
86
|
-
{
|
|
87
|
-
budget,
|
|
88
|
-
},
|
|
89
|
-
);
|
|
90
|
-
assert.equal(report.error, "ANCHOR_MISMATCH");
|
|
91
|
-
return report;
|
|
92
|
-
});
|
|
93
|
-
assert.ok(reports[0].failures?.[0].notShown);
|
|
94
|
-
assert.equal(reports[0].failures?.[0].observed, undefined);
|
|
95
|
-
assert.equal(reports[1].failures?.[0].observed, snapshot.anchors.row(1, text));
|
|
96
|
-
assert.equal(reports[1].failures?.[0].notShown, undefined);
|
|
97
|
-
for (const report of reports) {
|
|
98
|
-
assert.equal(report.publication, "NOT_PUBLISHED");
|
|
99
|
-
assert.ok(report.next?.includes("intended target"));
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test("model budget grows → grep payload and diagnostics use that call's allowances", async () => {
|
|
104
|
-
const paths = Array.from({ length: 3_000 }, () => "中文🙂".repeat(3));
|
|
105
|
-
const payload = paths.join("\n");
|
|
106
|
-
const warning = "opening\n" + "中文🙂".repeat(800) + "\nfinal cause";
|
|
107
|
-
const outputs = await Promise.all(
|
|
108
|
-
[small, large].map((budget) =>
|
|
109
|
-
grepOutput({
|
|
110
|
-
cwd: process.cwd(),
|
|
111
|
-
missing: [],
|
|
112
|
-
anchors: createAnchorFormatter(DEFAULT_CONFIG.hashLen),
|
|
113
|
-
context: 0,
|
|
114
|
-
outputMode: "files",
|
|
115
|
-
search: {
|
|
116
|
-
files: paths.map((path) => ({
|
|
117
|
-
filePath: join(process.cwd(), path),
|
|
118
|
-
rows: [],
|
|
119
|
-
snapshot: { revision: "", validUtf8: true },
|
|
120
|
-
})),
|
|
121
|
-
scanLimitReached: false,
|
|
122
|
-
matchLimitReached: false,
|
|
123
|
-
},
|
|
124
|
-
warnings: [{ reason: warning }],
|
|
125
|
-
effectiveLimit: 3_000,
|
|
126
|
-
literalPipe: false,
|
|
127
|
-
budget,
|
|
128
|
-
}),
|
|
129
|
-
),
|
|
130
|
-
);
|
|
131
|
-
assert.equal(outputs[0].facts.outputLimit, formatByteSize(small.grepBytes));
|
|
132
|
-
assert.equal(outputs[1].payload, payload);
|
|
133
|
-
assert.equal(outputs[1].facts.outputLimit, undefined);
|
|
134
|
-
assert.ok(Buffer.byteLength(outputs[0].facts.diagnostics!) <= small.fieldBytes);
|
|
135
|
-
assert.equal(outputs[1].facts.diagnostics, warning);
|
|
136
|
-
for (const output of outputs) {
|
|
137
|
-
assert.ok(output.facts.diagnostics?.startsWith("opening"));
|
|
138
|
-
assert.ok(output.facts.diagnostics?.endsWith("final cause"));
|
|
139
|
-
assert.ok(!output.facts.diagnostics?.includes("\uFFFD"));
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test("worker error reaches the tool → text is bounded once using the selected model", async (t) => {
|
|
144
|
-
const text = "中文🙂".repeat(800);
|
|
145
|
-
const reply = workerFailure(
|
|
146
|
-
new HashlineError("REGEX_WORKER_FAILED", text, { cause: new Error(text) }),
|
|
147
|
-
);
|
|
148
|
-
t.mock.method(Worker.prototype, "postMessage", function (this: Worker) {
|
|
149
|
-
queueMicrotask(() => this.emit("message", reply));
|
|
150
|
-
});
|
|
151
|
-
let error: unknown;
|
|
152
|
-
try {
|
|
153
|
-
await runRegexReplacements(
|
|
154
|
-
"source",
|
|
155
|
-
[{ find: "source", replace: "target", literal: false }],
|
|
156
|
-
5_000,
|
|
157
|
-
undefined,
|
|
158
|
-
);
|
|
159
|
-
assert.fail("Worker failure must reject");
|
|
160
|
-
} catch (failure) {
|
|
161
|
-
error = failure;
|
|
162
|
-
}
|
|
163
|
-
const reports = [small, large].map((budget) =>
|
|
164
|
-
describeMutationFailure("replace", error, { budget }),
|
|
165
|
-
);
|
|
166
|
-
assert.ok(Buffer.byteLength(reports[0].message!) <= small.fieldBytes);
|
|
167
|
-
assert.ok(Buffer.byteLength(reports[0].cause!.message) <= small.fieldBytes);
|
|
168
|
-
assert.equal(reports[1].message, text);
|
|
169
|
-
assert.equal(reports[1].cause?.message, text);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
test("model switches while a mutation waits → failure uses its starting budget snapshot", async (t) => {
|
|
173
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-budget-"));
|
|
174
|
-
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
175
|
-
const path = join(dir, "queued.txt");
|
|
176
|
-
const text = "中文🙂".repeat(800);
|
|
177
|
-
await writeFile(path, text);
|
|
178
|
-
let budgets: ReturnType<typeof registerModelBudgets> | undefined;
|
|
179
|
-
const { session, model } = await openTestSession(t, {
|
|
180
|
-
cwd: dir,
|
|
181
|
-
tools: [],
|
|
182
|
-
configure(pi) {
|
|
183
|
-
budgets = registerModelBudgets(pi);
|
|
184
|
-
},
|
|
185
|
-
});
|
|
186
|
-
assert.ok(budgets);
|
|
187
|
-
await session.setModel({ ...model, id: "small", contextWindow: 65_536 });
|
|
188
|
-
const entered = Promise.withResolvers<void>();
|
|
189
|
-
const release = Promise.withResolvers<void>();
|
|
190
|
-
const holding = withFileMutationQueue(path, async () => {
|
|
191
|
-
entered.resolve();
|
|
192
|
-
await release.promise;
|
|
193
|
-
});
|
|
194
|
-
await entered.promise;
|
|
195
|
-
try {
|
|
196
|
-
const running = callTool(makeEditOverride(dir, DEFAULT_CONFIG, undefined, budgets), {
|
|
197
|
-
path,
|
|
198
|
-
steps: [{ action: "delete", anchor: `1#${wrongHash(text)}` }],
|
|
199
|
-
});
|
|
200
|
-
await session.setModel({ ...model, id: "large", contextWindow: 1_048_576 });
|
|
201
|
-
assert.equal(budgets().readBytes, large.readBytes);
|
|
202
|
-
release.resolve();
|
|
203
|
-
const report = reportOf(await running, "edit");
|
|
204
|
-
assert.ok(report?.failures?.[0].notShown);
|
|
205
|
-
assert.equal(report.failures[0].observed, undefined);
|
|
206
|
-
const next = reportOf(
|
|
207
|
-
await callTool(makeEditOverride(dir, DEFAULT_CONFIG, undefined, budgets), {
|
|
208
|
-
path,
|
|
209
|
-
steps: [{ action: "delete", anchor: `1#${wrongHash(text)}` }],
|
|
210
|
-
}),
|
|
211
|
-
"edit",
|
|
212
|
-
);
|
|
213
|
-
assert.equal(
|
|
214
|
-
next?.failures?.[0].observed,
|
|
215
|
-
createAnchorFormatter(DEFAULT_CONFIG.hashLen).row(1, text),
|
|
216
|
-
);
|
|
217
|
-
} finally {
|
|
218
|
-
release.resolve();
|
|
219
|
-
await holding;
|
|
220
|
-
}
|
|
221
|
-
});
|
package/src/pi/pi.test.ts
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
4
|
-
import { tmpdir, homedir } from "node:os";
|
|
5
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
6
|
-
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
7
|
-
import { commandCompletion } from "./action-fusion.testing.ts";
|
|
8
|
-
import { makeEditOverride } from "./tool-factories.testing.ts";
|
|
9
|
-
import { makeGrepOverride } from "./tool-factories.testing.ts";
|
|
10
|
-
import { makeReadOverride } from "./tool-factories.testing.ts";
|
|
11
|
-
import { makeReplaceTool } from "./tool-factories.testing.ts";
|
|
12
|
-
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
13
|
-
import { canonicalPath } from "./path.ts";
|
|
14
|
-
import { join, resolve } from "node:path";
|
|
15
|
-
import { pathToFileURL } from "node:url";
|
|
16
|
-
import { callTool } from "./tool-call.testing.ts";
|
|
17
|
-
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
18
|
-
|
|
19
|
-
test("canonicalPath resolves relative and absolute", () => {
|
|
20
|
-
const cwd = resolve("/cwd");
|
|
21
|
-
const absolute = resolve("/abs/x.ts");
|
|
22
|
-
assert.equal(canonicalPath(cwd, "foo.ts"), join(cwd, "foo.ts"));
|
|
23
|
-
assert.equal(canonicalPath(cwd, "./foo.ts"), join(cwd, "foo.ts"));
|
|
24
|
-
assert.equal(canonicalPath(cwd, absolute), absolute);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("canonicalPath expands ~ to home directory", () => {
|
|
28
|
-
const home = homedir();
|
|
29
|
-
assert.equal(canonicalPath("/cwd", "~"), home);
|
|
30
|
-
assert.equal(canonicalPath("/cwd", "~/foo.ts"), join(home, "foo.ts"));
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("canonicalPath matches Pi's file URL, @ prefix, and Unicode space handling", () => {
|
|
34
|
-
const cwd = resolve("/cwd");
|
|
35
|
-
const target = join(cwd, "space name.txt");
|
|
36
|
-
const fileUrl = pathToFileURL(target).href;
|
|
37
|
-
for (const input of [
|
|
38
|
-
"space name.txt",
|
|
39
|
-
"space\u00a0name.txt",
|
|
40
|
-
"space\u2009name.txt",
|
|
41
|
-
"@space name.txt",
|
|
42
|
-
fileUrl,
|
|
43
|
-
`@${fileUrl}`,
|
|
44
|
-
]) {
|
|
45
|
-
assert.equal(canonicalPath(cwd, input), target, input);
|
|
46
|
-
}
|
|
47
|
-
assert.throws(() => canonicalPath(cwd, "file:///bad%ZZ"));
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("canonicalPath follows Pi's Windows shell-path conventions", () => {
|
|
51
|
-
const cwd = resolve("/cwd");
|
|
52
|
-
if (process.platform === "win32") {
|
|
53
|
-
const target = "C:\\src\\file.txt";
|
|
54
|
-
for (const input of [
|
|
55
|
-
"/c/src/file.txt",
|
|
56
|
-
"/mnt/c/src/file.txt",
|
|
57
|
-
"/cygdrive/c/src/file.txt",
|
|
58
|
-
"@/c/src/file.txt",
|
|
59
|
-
]) {
|
|
60
|
-
assert.equal(canonicalPath(cwd, input), target, input);
|
|
61
|
-
}
|
|
62
|
-
assert.equal(canonicalPath(cwd, "~\\file.txt"), join(homedir(), "file.txt"));
|
|
63
|
-
assert.equal(canonicalPath(cwd, "@~\\file.txt"), join(homedir(), "file.txt"));
|
|
64
|
-
} else {
|
|
65
|
-
assert.equal(canonicalPath(cwd, "/c/src/file.txt"), "/c/src/file.txt");
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("Windows drive-rooted input → uses the workspace volume", {
|
|
70
|
-
skip: process.platform !== "win32",
|
|
71
|
-
}, () => {
|
|
72
|
-
for (const drive of ["C:", "D:"]) {
|
|
73
|
-
for (const input of ["/docs/file.txt", "\\docs\\file.txt"]) {
|
|
74
|
-
assert.equal(canonicalPath(`${drive}/workspace`, input), `${drive}\\docs\\file.txt`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("Windows shell drive paths with Unicode line separators → preserve the filename", {
|
|
80
|
-
skip: process.platform !== "win32",
|
|
81
|
-
}, () => {
|
|
82
|
-
const cwd = resolve("/cwd");
|
|
83
|
-
for (const prefix of ["/c", "/mnt/c", "/cygdrive/c"]) {
|
|
84
|
-
for (const separator of ["\u2028", "\u2029"]) {
|
|
85
|
-
const name = `before${separator}after.txt`;
|
|
86
|
-
assert.equal(canonicalPath(cwd, `${prefix}/src/${name}`), join("C:/src", name));
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("file tools share Pi-style URL and @ path resolution", async () => {
|
|
92
|
-
const dir = await mkdtemp(join(tmpdir(), "hashline-path-"));
|
|
93
|
-
try {
|
|
94
|
-
const file = join(dir, "target.txt");
|
|
95
|
-
const url = pathToFileURL(file).href;
|
|
96
|
-
const ctx = { cwd: dir } as Parameters<ReturnType<typeof makeReadOverride>["execute"]>[4];
|
|
97
|
-
await callTool(
|
|
98
|
-
makeWriteOverride(dir),
|
|
99
|
-
{ path: `@${url}`, content: "before\n", mode: "create" },
|
|
100
|
-
{
|
|
101
|
-
toolCallId: "write",
|
|
102
|
-
ctx,
|
|
103
|
-
},
|
|
104
|
-
);
|
|
105
|
-
const read = await callTool(
|
|
106
|
-
makeReadOverride(dir, DEFAULT_CONFIG),
|
|
107
|
-
{ path: url },
|
|
108
|
-
{ toolCallId: "read", ctx },
|
|
109
|
-
);
|
|
110
|
-
if (read.content[0]?.type !== "text") throw new Error("Expected a text read result");
|
|
111
|
-
assert.match(read.content[0].text, /before/);
|
|
112
|
-
await callTool(
|
|
113
|
-
makeReplaceTool(dir, DEFAULT_CONFIG),
|
|
114
|
-
{ path: url, replacements: [{ literal: true, find: "before", replace: "after" }] },
|
|
115
|
-
{ toolCallId: "replace", ctx },
|
|
116
|
-
);
|
|
117
|
-
await callTool(
|
|
118
|
-
makeEditOverride(dir, DEFAULT_CONFIG),
|
|
119
|
-
{
|
|
120
|
-
path: `@${url}`,
|
|
121
|
-
steps: [
|
|
122
|
-
{ action: "replace", anchor: `1#${computeLineHash(1, "after", 4)}`, lines: ["edited"] },
|
|
123
|
-
],
|
|
124
|
-
},
|
|
125
|
-
{ toolCallId: "edit", ctx },
|
|
126
|
-
);
|
|
127
|
-
const matches = await callTool(
|
|
128
|
-
makeGrepOverride(dir, DEFAULT_CONFIG),
|
|
129
|
-
{ path: `@${url}`, pattern: "edited", literal: true },
|
|
130
|
-
{ toolCallId: "grep" },
|
|
131
|
-
);
|
|
132
|
-
assert.equal(matches.content[0].type, "text");
|
|
133
|
-
assert.match(matches.content[0].text, /edited/);
|
|
134
|
-
assert.equal(await readFile(file, "utf8"), "edited\n");
|
|
135
|
-
let commands = 0;
|
|
136
|
-
const fusion = createActionFusionExecutor(() => {});
|
|
137
|
-
const fusionContext = {
|
|
138
|
-
cwd: dir,
|
|
139
|
-
executeTool: async () => {
|
|
140
|
-
commands++;
|
|
141
|
-
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
142
|
-
},
|
|
143
|
-
};
|
|
144
|
-
await callTool(
|
|
145
|
-
makeWriteOverride(dir, fusion),
|
|
146
|
-
{
|
|
147
|
-
path: `@${url}`,
|
|
148
|
-
content: "edited\n",
|
|
149
|
-
mode: "overwrite",
|
|
150
|
-
then_run: { command: "check" },
|
|
151
|
-
},
|
|
152
|
-
{ toolCallId: "fused", ctx: fusionContext },
|
|
153
|
-
);
|
|
154
|
-
assert.equal(commands, 1);
|
|
155
|
-
} finally {
|
|
156
|
-
await rm(dir, { recursive: true, force: true });
|
|
157
|
-
}
|
|
158
|
-
});
|