@criogaid/pi-hashline-edit 0.9.0 → 0.11.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 +12 -12
- package/README.zh-CN.md +12 -12
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -1,32 +1,35 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { test } from "node:test";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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";
|
|
5
7
|
|
|
6
|
-
test("
|
|
7
|
-
let
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
+
},
|
|
12
16
|
});
|
|
13
|
-
assert.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
assert.
|
|
19
|
-
|
|
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;
|
|
20
25
|
const large = get();
|
|
21
|
-
assert.
|
|
22
|
-
assert.ok(large.readBytes >
|
|
23
|
-
assert.ok(large.blockBytes >
|
|
24
|
-
assert.ok(large.fieldBytes >
|
|
25
|
-
assert.ok(large.grepPreviewUnits >
|
|
26
|
-
|
|
27
|
-
assert.
|
|
28
|
-
model = { ...model, contextWindow: 65_536 };
|
|
29
|
-
assert.equal(get().readBytes, first.readBytes);
|
|
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);
|
|
30
33
|
});
|
|
31
34
|
|
|
32
35
|
test("unknown or invalid model capacity → bounded defaults are used", () => {
|
package/src/pi/model-budgets.ts
CHANGED
|
@@ -1,44 +1,12 @@
|
|
|
1
|
-
/** Per-extension
|
|
1
|
+
/** Per-extension budgets. Tool preparation and execution share the same provider. */
|
|
2
2
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { outputBudgetsFor, type OutputBudgetProvider
|
|
3
|
+
import { outputBudgetsFor, type OutputBudgetProvider } from "./budgets.ts";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
NonNullable<ExtensionContext["model"]>,
|
|
7
|
-
"provider" | "id" | "contextWindow"
|
|
8
|
-
>;
|
|
9
|
-
|
|
10
|
-
/** Reuse the snapshot until selection or its declared capacity changes. No provider request is made. */
|
|
11
|
-
export function createOutputBudgetProvider(
|
|
12
|
-
readModel: () => BudgetModel | undefined,
|
|
13
|
-
): OutputBudgetProvider {
|
|
14
|
-
let provider: string | undefined;
|
|
15
|
-
let id: string | undefined;
|
|
16
|
-
let contextWindow: number | undefined;
|
|
17
|
-
let cached: OutputBudgets | undefined;
|
|
18
|
-
return () => {
|
|
19
|
-
const model = readModel();
|
|
20
|
-
if (
|
|
21
|
-
cached === undefined ||
|
|
22
|
-
provider !== model?.provider ||
|
|
23
|
-
id !== model?.id ||
|
|
24
|
-
!Object.is(contextWindow, model?.contextWindow)
|
|
25
|
-
) {
|
|
26
|
-
provider = model?.provider;
|
|
27
|
-
id = model?.id;
|
|
28
|
-
contextWindow = model?.contextWindow;
|
|
29
|
-
cached = outputBudgetsFor(contextWindow);
|
|
30
|
-
}
|
|
31
|
-
return cached;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Retain Pi's live context getter, including after session restore, without calculating on events. */
|
|
5
|
+
/** Retain Pi's context once per session: its model getter stays live, so no event recalculates. */
|
|
36
6
|
export function registerModelBudgets(pi: ExtensionAPI): OutputBudgetProvider {
|
|
37
7
|
let context: ExtensionContext | undefined;
|
|
38
|
-
|
|
8
|
+
pi.on("session_start", (_event, ctx) => {
|
|
39
9
|
context = ctx;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
pi.on("model_select", bind);
|
|
43
|
-
return createOutputBudgetProvider(() => context?.model);
|
|
10
|
+
});
|
|
11
|
+
return () => outputBudgetsFor(context?.model?.contextWindow);
|
|
44
12
|
}
|
|
@@ -1,46 +1,55 @@
|
|
|
1
|
+
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
1
2
|
import { test } from "node:test";
|
|
2
3
|
import assert from "node:assert/strict";
|
|
3
4
|
import { generateDiffString } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import {
|
|
5
|
-
commandText,
|
|
6
|
-
finalizeMutation,
|
|
7
|
-
generateMutationDetails,
|
|
8
|
-
type MutationFacts,
|
|
9
|
-
type MutationFinalDetails,
|
|
10
|
-
type MutationOutcome,
|
|
11
|
-
} from "./mutation-result.ts";
|
|
5
|
+
import { generateMutationDetails } from "./mutation-result.ts";
|
|
12
6
|
import { displayCarriageReturns } from "./anchor-format.ts";
|
|
13
|
-
import {
|
|
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 } 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";
|
|
14
16
|
|
|
15
|
-
// Checked by tsc; these callers must never run.
|
|
16
|
-
function
|
|
17
|
-
replacement:
|
|
18
|
-
result:
|
|
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"]>>,
|
|
19
21
|
) {
|
|
20
|
-
// @ts-expect-error
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
replacement,
|
|
27
|
-
{ freshness: "unchanged" },
|
|
28
|
-
undefined,
|
|
29
|
-
DEFAULT_OUTPUT_BUDGETS,
|
|
30
|
-
);
|
|
31
|
-
// @ts-expect-error A failed mutation has no diff details.
|
|
32
|
-
const diff: string = result.diff;
|
|
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;
|
|
33
28
|
}
|
|
34
29
|
|
|
35
|
-
test("command content includes images → report text joins only text blocks", () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
{
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
+
}
|
|
44
53
|
});
|
|
45
54
|
|
|
46
55
|
for (const [name, before, after] of [
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Mutation report building: details, diffs, and
|
|
3
|
-
* classification.
|
|
2
|
+
* Mutation report building: details, diffs, and failure classification.
|
|
4
3
|
*
|
|
5
4
|
* @module pi-hashline-edit/pi
|
|
6
5
|
*/
|
|
@@ -9,19 +8,26 @@ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
|
9
8
|
import { FileMutationError, type CommitResult, type FreshnessObservation } from "./file-commit.ts";
|
|
10
9
|
import { generateDiffString } from "@earendil-works/pi-coding-agent";
|
|
11
10
|
import { displayCarriageReturns } from "./anchor-format.ts";
|
|
11
|
+
import { errorMessage } from "../core/errors.ts";
|
|
12
12
|
import { normalizeLineEndings } from "../core/lines.ts";
|
|
13
13
|
import type {
|
|
14
|
+
ActionFusionProgress,
|
|
14
15
|
CommandResult,
|
|
15
16
|
CommandStatus,
|
|
16
17
|
FailureReport,
|
|
17
18
|
MutationToolName,
|
|
18
|
-
|
|
19
|
+
ReportTool,
|
|
19
20
|
SuccessFacts,
|
|
20
21
|
SuccessReport,
|
|
21
22
|
} from "./report-schema.ts";
|
|
22
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
describeFailure,
|
|
25
|
+
reportResult,
|
|
26
|
+
type FailureContext,
|
|
27
|
+
type ReportDetails,
|
|
28
|
+
} from "./report.ts";
|
|
29
|
+
import { causeOf } from "./report-cause.ts";
|
|
23
30
|
import type { OutputBudgets } from "./budgets.ts";
|
|
24
|
-
import type { ActionFusionProgress } from "./action-fusion.ts";
|
|
25
31
|
|
|
26
32
|
/** Final success carries tool details; final failure carries only its report; neither carries progress. */
|
|
27
33
|
export type MutationFinalDetails<TDetails> =
|
|
@@ -39,25 +45,10 @@ export function generateMutationDetails(before: string, after: string) {
|
|
|
39
45
|
return { diff: displayCarriageReturns(diff) };
|
|
40
46
|
}
|
|
41
47
|
|
|
42
|
-
/** Keep result-building failures distinct from file publication failures. */
|
|
43
|
-
export function postProcessMutation<T>(publication: PublicationStatus, build: () => T): T {
|
|
44
|
-
try {
|
|
45
|
-
return build();
|
|
46
|
-
} catch (error) {
|
|
47
|
-
throw new FileMutationError(
|
|
48
|
-
"POST_PROCESS_FAILED",
|
|
49
|
-
"post_process",
|
|
50
|
-
publication,
|
|
51
|
-
"Result generation failed.",
|
|
52
|
-
{ cause: error },
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
48
|
/** The facts a mutation tool states about its own outcome. */
|
|
58
49
|
export type MutationFacts<TTool extends MutationToolName = MutationToolName> = {
|
|
59
50
|
[T in TTool]: SuccessFacts<T> & {
|
|
60
|
-
[K in Exclude<"matches"
|
|
51
|
+
[K in Exclude<"matches", keyof SuccessFacts<T>>]?: never;
|
|
61
52
|
};
|
|
62
53
|
}[TTool];
|
|
63
54
|
|
|
@@ -86,6 +77,46 @@ export interface CommandFacts {
|
|
|
86
77
|
readonly progressError?: unknown;
|
|
87
78
|
}
|
|
88
79
|
|
|
80
|
+
/**
|
|
81
|
+
* A mutation that did not complete, so its then_run command never ran. Action
|
|
82
|
+
* Fusion owns the command status; the mutation's own failure stays unchanged.
|
|
83
|
+
*/
|
|
84
|
+
export class CommandNotRun extends Error {
|
|
85
|
+
readonly status: Extract<CommandStatus, "skipped" | "cancelled">;
|
|
86
|
+
/** A progress observer failed; the mutation's failure is unaffected. */
|
|
87
|
+
readonly progressError: unknown;
|
|
88
|
+
|
|
89
|
+
constructor(
|
|
90
|
+
error: unknown,
|
|
91
|
+
status: Extract<CommandStatus, "skipped" | "cancelled">,
|
|
92
|
+
progressError?: unknown,
|
|
93
|
+
) {
|
|
94
|
+
super(errorMessage(error), { cause: error });
|
|
95
|
+
this.name = "CommandNotRun";
|
|
96
|
+
this.status = status;
|
|
97
|
+
this.progressError = progressError;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Classify a mutation tool's failure with what only a mutation states: what it published and,
|
|
103
|
+
* when the call was fused, why its command never ran.
|
|
104
|
+
*/
|
|
105
|
+
export function describeMutationFailure(
|
|
106
|
+
tool: ReportTool,
|
|
107
|
+
caught: unknown,
|
|
108
|
+
context: FailureContext,
|
|
109
|
+
): FailureReport {
|
|
110
|
+
const command = caught instanceof CommandNotRun ? caught : undefined;
|
|
111
|
+
// The mutation's own failure, unchanged.
|
|
112
|
+
const error = command ? command.cause : caught;
|
|
113
|
+
return describeFailure(tool, error, context, {
|
|
114
|
+
publication: error instanceof FileMutationError ? error.publication : "NOT_PUBLISHED",
|
|
115
|
+
then_run: command ? { status: command.status } : undefined,
|
|
116
|
+
progressError: causeOf(command?.progressError, context.budget.fieldBytes),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
89
120
|
/** Build the mutation report from commit, freshness, and command facts; the TUI uses its diff details. */
|
|
90
121
|
export function finalizeMutation<TDetails extends object, TTool extends MutationToolName>(
|
|
91
122
|
tool: TTool,
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared execution pipeline for the file mutation tools (edit, replace, write).
|
|
3
3
|
*
|
|
4
|
+
* `mutationToolBase` assembles the definition every mutation tool shares:
|
|
5
|
+
* then_run's field, description, and guideline when Action Fusion is on,
|
|
6
|
+
* argument preparation, strict sampling, and execution. Each tool supplies its schema, text,
|
|
7
|
+
* semantic checks, and `run`, and adds its own renderers.
|
|
8
|
+
*
|
|
4
9
|
* Owns the sequencing every mutation tool must keep identical once Pi has
|
|
5
10
|
* prepared and validated the arguments: separating then_run, path resolution,
|
|
6
|
-
* the shared file mutation queue,
|
|
7
|
-
* finalization. Each tool
|
|
8
|
-
*
|
|
9
|
-
* `finalizeMutation`
|
|
11
|
+
* the shared file mutation queue, the hand-off to Action Fusion, and
|
|
12
|
+
* finalization. Each tool returns a typed `MutationOutcome` (commit facts,
|
|
13
|
+
* details, outcome facts); Action Fusion returns facts too, and this module's
|
|
14
|
+
* single `finalizeMutation` call turns them into the result, so nothing
|
|
10
15
|
* downstream reads publication or revisions back from result details. Every
|
|
11
|
-
* failure leaves as one report with the call's path
|
|
16
|
+
* failure leaves as one report with the call's path and publication.
|
|
12
17
|
*
|
|
13
18
|
* `runTextMutation` adds the read-modify-write sequence shared by edit and
|
|
14
19
|
* replace: snapshot bound to the bytes read, cancellation before apply and
|
|
@@ -20,13 +25,27 @@
|
|
|
20
25
|
|
|
21
26
|
import { withFileMutationQueue, type ExtensionToolContext } from "@earendil-works/pi-coding-agent";
|
|
22
27
|
import type { AgentToolResult, AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
|
|
23
|
-
import type {
|
|
28
|
+
import type { Static, TObject, TProperties } from "typebox";
|
|
29
|
+
import {
|
|
30
|
+
ACTION_FUSION_GUIDELINES,
|
|
31
|
+
withThenRunDescription,
|
|
32
|
+
withThenRunSchema,
|
|
33
|
+
type createActionFusionExecutor,
|
|
34
|
+
type FusedMutation,
|
|
35
|
+
type ThenRunInput,
|
|
36
|
+
} from "./action-fusion.ts";
|
|
37
|
+
import { createArgumentPreparer, type CheckArguments } from "./argument-validation.ts";
|
|
24
38
|
import type { MutationToolName } from "./report-schema.ts";
|
|
25
|
-
import { commitFreshness, commitReplacement, readEditableSnapshot } from "./file-commit.ts";
|
|
26
39
|
import {
|
|
40
|
+
commitFile,
|
|
41
|
+
commitFreshness,
|
|
42
|
+
FileMutationError,
|
|
43
|
+
readEditableSnapshot,
|
|
44
|
+
} from "./file-commit.ts";
|
|
45
|
+
import {
|
|
46
|
+
describeMutationFailure,
|
|
27
47
|
finalizeMutation,
|
|
28
48
|
generateMutationDetails,
|
|
29
|
-
postProcessMutation,
|
|
30
49
|
type MutationOutcome,
|
|
31
50
|
type MutationFinalDetails,
|
|
32
51
|
type MutationResultDetails,
|
|
@@ -35,12 +54,19 @@ import {
|
|
|
35
54
|
import { canonicalPath } from "./path.ts";
|
|
36
55
|
import { throwIfCancelled } from "./error-text.ts";
|
|
37
56
|
import { runTool } from "./report.ts";
|
|
38
|
-
import type {
|
|
57
|
+
import type { OutputBudgets } from "./budgets.ts";
|
|
58
|
+
import type { ToolEnvironment } from "./config.ts";
|
|
59
|
+
import { STRICT_SAMPLING } from "./schema.ts";
|
|
39
60
|
|
|
40
61
|
export type ActionFusionExecutor = ReturnType<typeof createActionFusionExecutor>;
|
|
41
62
|
|
|
63
|
+
/** A mutation tool's registration environment; fusion is undefined when Action Fusion is off. */
|
|
64
|
+
export interface MutationToolEnvironment extends ToolEnvironment {
|
|
65
|
+
readonly fusion: ActionFusionExecutor | undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
42
68
|
/** Result details shared by the read-modify-write tools. */
|
|
43
|
-
|
|
69
|
+
type TextMutationDetails = ReturnType<typeof generateMutationDetails>;
|
|
44
70
|
|
|
45
71
|
/** The file a mutation targets, resolved once per call. */
|
|
46
72
|
export interface MutationTarget {
|
|
@@ -49,78 +75,98 @@ export interface MutationTarget {
|
|
|
49
75
|
readonly budget: OutputBudgets;
|
|
50
76
|
}
|
|
51
77
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
>
|
|
78
|
+
/** A mutation tool's own parameters; `execute` separates then_run. */
|
|
79
|
+
type MutationParams<P extends TProperties> = Static<TObject<P>> & { readonly path: string };
|
|
80
|
+
|
|
81
|
+
/** What a mutation tool supplies to the shared definition; renderers stay with the tool. */
|
|
82
|
+
interface MutationToolSpec<P extends TProperties, TDetails, TTool extends MutationToolName>
|
|
83
|
+
extends MutationToolEnvironment {
|
|
57
84
|
readonly tool: TTool;
|
|
58
|
-
|
|
59
|
-
readonly
|
|
60
|
-
readonly
|
|
85
|
+
/** Parameters without then_run, which Action Fusion adds. */
|
|
86
|
+
readonly schema: TObject<P>;
|
|
87
|
+
readonly description: string;
|
|
88
|
+
readonly promptSnippet: string;
|
|
89
|
+
readonly promptGuidelines?: readonly string[];
|
|
90
|
+
/** Semantic argument checks the schema cannot express. */
|
|
91
|
+
readonly check?: CheckArguments;
|
|
61
92
|
/** Perform the mutation. Runs inside the file mutation queue. */
|
|
62
93
|
run(
|
|
63
|
-
params: Omit<
|
|
94
|
+
params: Omit<MutationParams<P>, "then_run">,
|
|
64
95
|
target: MutationTarget,
|
|
65
96
|
): Promise<MutationOutcome<TDetails, TTool>>;
|
|
66
97
|
}
|
|
67
98
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
/** Queue and publish one validated mutation call, then run its then_run command when fused. */
|
|
78
|
-
export async function executeMutation<
|
|
79
|
-
TParams extends { path: string },
|
|
99
|
+
/**
|
|
100
|
+
* The definition every mutation tool shares. Action Fusion adds then_run to the
|
|
101
|
+
* schema, description, and guidelines only when it is on; arguments are prepared
|
|
102
|
+
* against the parameters Pi registers; `execute` queues and publishes one
|
|
103
|
+
* validated call, then runs its then_run command when fused.
|
|
104
|
+
*/
|
|
105
|
+
export function mutationToolBase<
|
|
106
|
+
P extends TProperties,
|
|
80
107
|
TDetails extends object,
|
|
81
108
|
TTool extends MutationToolName,
|
|
82
|
-
>(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
109
|
+
>(spec: MutationToolSpec<P, TDetails, TTool>) {
|
|
110
|
+
const { tool, cwd, schema, fusion, budgets } = spec;
|
|
111
|
+
const actionFusion = fusion !== undefined;
|
|
112
|
+
const parameters = withThenRunSchema(schema, tool, actionFusion);
|
|
113
|
+
return {
|
|
114
|
+
name: tool,
|
|
115
|
+
label: tool,
|
|
116
|
+
description: withThenRunDescription(spec.description, actionFusion),
|
|
117
|
+
promptSnippet: spec.promptSnippet,
|
|
118
|
+
promptGuidelines: [
|
|
119
|
+
...(spec.promptGuidelines ?? []),
|
|
120
|
+
...(actionFusion ? ACTION_FUSION_GUIDELINES : []),
|
|
121
|
+
],
|
|
122
|
+
parameters,
|
|
123
|
+
prepareArguments: createArgumentPreparer(tool, parameters, spec.check, budgets),
|
|
124
|
+
constrainedSampling: STRICT_SAMPLING,
|
|
125
|
+
renderShell: "default" as const,
|
|
126
|
+
execute(
|
|
127
|
+
toolCallId: string,
|
|
128
|
+
params: MutationParams<P> & { then_run?: ThenRunInput },
|
|
129
|
+
signal: AbortSignal | undefined,
|
|
130
|
+
onUpdate: AgentToolUpdateCallback<MutationResultDetails<TDetails>> | undefined,
|
|
131
|
+
ctx: ExtensionToolContext,
|
|
132
|
+
): Promise<AgentToolResult<MutationFinalDetails<TDetails>>> {
|
|
133
|
+
const { then_run, ...mutationParams } = params;
|
|
134
|
+
const displayPath = mutationParams.path;
|
|
135
|
+
const budget = budgets();
|
|
136
|
+
return runTool(
|
|
99
137
|
tool,
|
|
100
|
-
displayPath,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
138
|
+
{ path: displayPath, signal, budget },
|
|
139
|
+
async () => {
|
|
140
|
+
const absolutePath = canonicalPath(cwd, displayPath);
|
|
141
|
+
const target: MutationTarget = { absolutePath, signal, budget };
|
|
142
|
+
const mutate = (): Promise<MutationOutcome<TDetails, TTool>> =>
|
|
143
|
+
withFileMutationQueue(absolutePath, () => spec.run(mutationParams, target));
|
|
144
|
+
const settled: FusedMutation<TDetails, TTool> = fusion
|
|
145
|
+
? await fusion({
|
|
146
|
+
toolCallId,
|
|
147
|
+
absolutePath,
|
|
148
|
+
thenRun: then_run,
|
|
149
|
+
mutate,
|
|
150
|
+
signal,
|
|
151
|
+
ctx,
|
|
152
|
+
onUpdate,
|
|
153
|
+
})
|
|
154
|
+
: await mutate().then((outcome) => ({
|
|
155
|
+
outcome,
|
|
156
|
+
observation: commitFreshness(outcome.commit),
|
|
157
|
+
}));
|
|
158
|
+
const { outcome, observation, command, terminate } = settled;
|
|
159
|
+
const result = finalizeMutation(tool, displayPath, outcome, observation, command, budget);
|
|
160
|
+
return terminate === undefined ? result : { ...result, terminate };
|
|
161
|
+
},
|
|
162
|
+
describeMutationFailure,
|
|
105
163
|
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
toolCallId,
|
|
109
|
-
tool,
|
|
110
|
-
displayPath,
|
|
111
|
-
absolutePath,
|
|
112
|
-
thenRun: then_run,
|
|
113
|
-
budget,
|
|
114
|
-
mutate,
|
|
115
|
-
signal,
|
|
116
|
-
ctx,
|
|
117
|
-
onUpdate,
|
|
118
|
-
});
|
|
119
|
-
});
|
|
164
|
+
},
|
|
165
|
+
};
|
|
120
166
|
}
|
|
121
167
|
|
|
122
168
|
/** A whole-file text transformation produced from one snapshot. */
|
|
123
|
-
|
|
169
|
+
interface TextChange<TTool extends MutationToolName> {
|
|
124
170
|
/** Complete replacement text; equal to the snapshot for a no-op. */
|
|
125
171
|
readonly text: string;
|
|
126
172
|
/** The tool's own outcome facts, such as replace's match count. */
|
|
@@ -146,10 +192,24 @@ export async function runTextMutation<TTool extends MutationToolName>(
|
|
|
146
192
|
// Cancelled before write: don't touch the disk.
|
|
147
193
|
throwIfCancelled(signal);
|
|
148
194
|
|
|
149
|
-
const commit = await
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
195
|
+
const commit = await commitFile(absolutePath, next.text, {
|
|
196
|
+
mode: "overwrite",
|
|
197
|
+
baseRevision,
|
|
198
|
+
signal,
|
|
199
|
+
});
|
|
200
|
+
try {
|
|
201
|
+
return {
|
|
202
|
+
commit,
|
|
203
|
+
details: generateMutationDetails(currentText, next.text),
|
|
204
|
+
facts: next.facts,
|
|
205
|
+
};
|
|
206
|
+
} catch (error) {
|
|
207
|
+
// Keep result-building failures distinct from file publication failures.
|
|
208
|
+
throw new FileMutationError(
|
|
209
|
+
"POST_PROCESS_FAILED",
|
|
210
|
+
commit.publication,
|
|
211
|
+
"Result generation failed.",
|
|
212
|
+
{ cause: error },
|
|
213
|
+
);
|
|
214
|
+
}
|
|
155
215
|
}
|