@criogaid/pi-hashline-edit 0.10.0 → 0.12.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 +21 -7
- package/README.zh-CN.md +22 -8
- package/package.json +7 -6
- package/src/core/AGENTS.md +2 -2
- package/src/core/apply.test.ts +4 -4
- package/src/core/apply.ts +3 -3
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +1 -1
- package/src/core/ranges.test.ts +2 -1
- package/src/core/ranges.ts +1 -1
- package/src/core/regex.ts +6 -2
- package/src/core/replace.test.ts +51 -0
- package/src/core/replace.ts +10 -4
- package/src/core/types.ts +1 -1
- package/src/index.ts +9 -9
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +2 -2
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -5
- package/src/integration/grep-js-completeness.test.ts +12 -11
- package/src/integration/grep-line-candidates.test.ts +5 -3
- package/src/integration/grep-matrix.test.ts +5 -4
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +5 -4
- package/src/integration/grep-prefilter-coverage.test.ts +17 -5
- package/src/integration/grep-process-limits.test.ts +2 -4
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +93 -48
- package/src/integration/grep-scan-budget.test.ts +8 -8
- package/src/integration/grep-translation.test.ts +8 -7
- package/src/integration/large-text.test.ts +5 -5
- package/src/integration/model-budget-lifecycle.test.ts +9 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.test.ts +12 -10
- package/src/integration/npm-package.testing.ts +151 -4
- package/src/integration/publication-read-changes.test.ts +54 -54
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +1 -1
- package/src/integration/report-replay.test.ts +9 -6
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +13 -12
- package/src/pi/action-fusion.test.ts +125 -105
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +9 -21
- package/src/pi/argument-diagnostics.ts +4 -3
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +281 -139
- package/src/pi/argument-validation.ts +21 -31
- package/src/pi/budgets.ts +1 -4
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +2 -1
- package/src/pi/config.ts +8 -3
- package/src/pi/diagnostic-buffer.test.ts +23 -13
- package/src/pi/edit-tool.ts +48 -63
- package/src/pi/error-text.ts +7 -2
- package/src/pi/execute.test.ts +178 -157
- package/src/pi/failure-context.test.ts +5 -4
- package/src/pi/failure-context.ts +1 -1
- package/src/pi/failure-path.test.ts +37 -37
- package/src/pi/file-commit.test.ts +47 -19
- package/src/pi/file-commit.ts +36 -9
- package/src/pi/fusion-card.test.ts +22 -21
- package/src/pi/fusion-card.ts +9 -5
- package/src/pi/grep-candidates.test.ts +18 -20
- package/src/pi/grep-candidates.ts +13 -17
- package/src/pi/grep-matrix.test.ts +3 -3
- package/src/pi/grep-order.test.ts +12 -11
- package/src/pi/grep-output.ts +45 -17
- package/src/pi/grep-reread.ts +7 -2
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +10 -10
- package/src/pi/grep-search.ts +8 -11
- package/src/pi/grep-tool.ts +40 -53
- package/src/pi/grep.test.ts +141 -116
- package/src/pi/model-budgets.test.ts +2 -1
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +8 -8
- package/src/pi/mutation-result.ts +1 -1
- package/src/pi/mutation-runner.ts +10 -6
- package/src/pi/output-budgets.test.ts +53 -31
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +13 -18
- package/src/pi/read.test.ts +67 -41
- package/src/pi/registration.test.ts +25 -19
- package/src/pi/render.test.ts +94 -32
- package/src/pi/render.ts +7 -4
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +6 -16
- package/src/pi/replace.test.ts +45 -34
- package/src/pi/report-failure.testing.ts +9 -1
- package/src/pi/report-schema.ts +225 -86
- package/src/pi/report.test.ts +81 -87
- package/src/pi/report.ts +35 -22
- package/src/pi/result-safety.test.ts +121 -100
- package/src/pi/rg-process.test.ts +2 -2
- package/src/pi/rg-process.ts +9 -15
- package/src/pi/rg-regex.test.ts +13 -0
- package/src/pi/rg-regex.ts +2 -2
- package/src/pi/schema.ts +23 -0
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +9 -15
- package/src/pi/write.test.ts +30 -31
- package/src/testing/run-tests.ts +1 -1
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared tool argument boundary. Pi owns preparation and acceptance; tool checks
|
|
3
|
-
* report semantic issues. Failure-only diagnostics
|
|
3
|
+
* report semantic issues. Failure-only diagnostics use pi-schema's public coercer,
|
|
4
4
|
* then project and aggregate the declared schema in argument-diagnostics.ts.
|
|
5
5
|
* Pi still validates returned arguments in its mandatory tool-call pipeline.
|
|
6
6
|
* Tool schemas have no cross-field constraints.
|
|
7
7
|
*/
|
|
8
8
|
import { validateToolArguments, type ToolCall } from "@earendil-works/pi-ai";
|
|
9
|
-
import {
|
|
9
|
+
import { compile } from "@earendil-works/pi-schema";
|
|
10
|
+
import { formats } from "@earendil-works/pi-schema/formats";
|
|
11
|
+
import type { Static, TObject } from "typebox";
|
|
10
12
|
import { errorMessage } from "../core/errors.ts";
|
|
11
13
|
import { diagnoseArguments } from "./argument-diagnostics.ts";
|
|
12
14
|
import { formatArgumentError } from "./argument-error.ts";
|
|
@@ -28,45 +30,34 @@ export function createArgumentPreparer<T extends TObject>(
|
|
|
28
30
|
check: CheckArguments | undefined,
|
|
29
31
|
budgets: OutputBudgetProvider,
|
|
30
32
|
): (args: unknown) => Static<T> {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
type: "toolCall",
|
|
37
|
-
id: "argument-validation",
|
|
38
|
-
name,
|
|
39
|
-
arguments: args as ToolCall["arguments"],
|
|
40
|
-
},
|
|
41
|
-
);
|
|
33
|
+
const diagnosticValidator = compile(parameters, {
|
|
34
|
+
formats,
|
|
35
|
+
coerce: true,
|
|
36
|
+
unresolvedRefs: "reject",
|
|
37
|
+
});
|
|
42
38
|
|
|
43
39
|
return (args) => {
|
|
44
40
|
let checked: unknown = args;
|
|
45
41
|
let schemaFailure: unknown;
|
|
46
42
|
try {
|
|
47
|
-
checked =
|
|
43
|
+
checked = validateToolArguments(
|
|
44
|
+
{ name, parameters, description: "" },
|
|
45
|
+
// Pi owns shape checking and coercion at this external boundary.
|
|
46
|
+
{
|
|
47
|
+
type: "toolCall",
|
|
48
|
+
id: "argument-validation",
|
|
49
|
+
name,
|
|
50
|
+
arguments: args as ToolCall["arguments"],
|
|
51
|
+
},
|
|
52
|
+
);
|
|
48
53
|
} catch (error) {
|
|
49
54
|
schemaFailure = error;
|
|
50
55
|
}
|
|
51
56
|
if (schemaFailure !== undefined) {
|
|
52
|
-
let observation: { readonly value: unknown } | undefined;
|
|
53
|
-
// Pi does not expose failed prepared values. A successful allOf refinement
|
|
54
|
-
// observes its error pass without changing coercion, null handling or acceptance.
|
|
55
|
-
const observer = Type.Object(parameters.properties, {
|
|
56
|
-
...ObjectOptions(parameters),
|
|
57
|
-
allOf: [
|
|
58
|
-
Type.Refine(Type.Unknown(), (value: unknown) => {
|
|
59
|
-
observation = { value };
|
|
60
|
-
return true;
|
|
61
|
-
}),
|
|
62
|
-
],
|
|
63
|
-
});
|
|
64
57
|
try {
|
|
65
|
-
|
|
58
|
+
checked = diagnosticValidator.coerce(structuredClone(args));
|
|
66
59
|
} catch {
|
|
67
|
-
//
|
|
68
|
-
}
|
|
69
|
-
if (!observation) {
|
|
60
|
+
// Preparation failures retain Pi's original cause in the shared error envelope.
|
|
70
61
|
throw new Error(
|
|
71
62
|
formatArgumentError(
|
|
72
63
|
[{ field: "$", reason: errorMessage(schemaFailure) }],
|
|
@@ -76,7 +67,6 @@ export function createArgumentPreparer<T extends TObject>(
|
|
|
76
67
|
{ cause: schemaFailure },
|
|
77
68
|
);
|
|
78
69
|
}
|
|
79
|
-
checked = observation.value;
|
|
80
70
|
}
|
|
81
71
|
const issues: ArgumentIssue[] = [];
|
|
82
72
|
check?.(checked, (field, reason, fix) =>
|
package/src/pi/budgets.ts
CHANGED
|
@@ -23,7 +23,7 @@ const fallbackBudgets = {
|
|
|
23
23
|
fieldBytes: 4 * 1024,
|
|
24
24
|
grepPreviewUnits: 500,
|
|
25
25
|
};
|
|
26
|
-
|
|
26
|
+
const DEFAULT_OUTPUT_BUDGETS = Object.freeze(fallbackBudgets);
|
|
27
27
|
export type OutputBudgets = typeof DEFAULT_OUTPUT_BUDGETS;
|
|
28
28
|
export type OutputBudgetProvider = () => OutputBudgets;
|
|
29
29
|
|
|
@@ -45,9 +45,6 @@ export function outputBudgetsFor(contextWindow: number | undefined): OutputBudge
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
/** Standalone tool factories have no session model until a provider is supplied. */
|
|
49
|
-
export const defaultOutputBudgets: OutputBudgetProvider = () => DEFAULT_OUTPUT_BUDGETS;
|
|
50
|
-
|
|
51
48
|
const MIB = 1024 * 1024;
|
|
52
49
|
|
|
53
50
|
/** Size wording for every model-facing limit: whole MiB as `256 MiB`, otherwise `195.3 KiB`. */
|
package/src/pi/codemode.test.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { Type } from "typebox";
|
|
|
6
6
|
import { createCodemodeToolDefinition } from "../../node_modules/@earendil-works/pi-coding-agent/dist/extensions/codemode/tool.js";
|
|
7
7
|
import { openTestSession, responseStream, SESSION_TIMEOUT_MS } from "../testing/session.testing.ts";
|
|
8
8
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
9
|
-
import { makeWriteOverride } from "./
|
|
9
|
+
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
10
10
|
|
|
11
11
|
for (const entry of ["direct", "codemode"] as const) {
|
|
12
12
|
test(`${entry} mutation waits on then_run → the call and model continuation stay pending`, {
|
|
@@ -17,7 +17,12 @@ for (const entry of ["direct", "codemode"] as const) {
|
|
|
17
17
|
const { cwd, session, model } = await openTestSession(t, {
|
|
18
18
|
tools: ["write", "bash", "codemode"],
|
|
19
19
|
configure(pi, cwd) {
|
|
20
|
-
pi.registerTool(
|
|
20
|
+
pi.registerTool(
|
|
21
|
+
makeWriteOverride(
|
|
22
|
+
cwd,
|
|
23
|
+
createActionFusionExecutor(() => {}),
|
|
24
|
+
),
|
|
25
|
+
);
|
|
21
26
|
pi.registerTool(createCodemodeToolDefinition());
|
|
22
27
|
pi.registerTool({
|
|
23
28
|
name: "bash",
|
package/src/pi/config.test.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { tmpdir } from "node:os";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import assert from "node:assert/strict";
|
|
6
6
|
import { test } from "node:test";
|
|
7
|
-
import {
|
|
7
|
+
import { loadConfig } from "./config.ts";
|
|
8
|
+
import { DEFAULT_CONFIG } from "./tool-factories.testing.ts";
|
|
8
9
|
|
|
9
10
|
async function withSettings(
|
|
10
11
|
fn: (
|
package/src/pi/config.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { Type, type Static, type TSchema } from "typebox";
|
|
|
13
13
|
import { Value } from "typebox/value";
|
|
14
14
|
import { HASH_LEN_MAX, HASH_LEN_MIN } from "../core/hash.ts";
|
|
15
15
|
import { GREP_CONTEXT_RANGE, integerRange, POSITIVE_SAFE_INTEGER } from "./schema.ts";
|
|
16
|
-
import { MAX_CONTENT_KIB } from "./budgets.ts";
|
|
16
|
+
import { MAX_CONTENT_KIB, type OutputBudgetProvider } from "./budgets.ts";
|
|
17
17
|
|
|
18
18
|
function integerSetting(
|
|
19
19
|
range: ReturnType<typeof integerRange>,
|
|
@@ -85,6 +85,13 @@ const configSchema = Type.Object({
|
|
|
85
85
|
|
|
86
86
|
export type HashlineEditConfig = Static<typeof configSchema>;
|
|
87
87
|
|
|
88
|
+
/** What a tool captures at registration and keeps for its lifetime. */
|
|
89
|
+
export interface ToolEnvironment {
|
|
90
|
+
readonly cwd: string;
|
|
91
|
+
readonly config: HashlineEditConfig;
|
|
92
|
+
readonly budgets: OutputBudgetProvider;
|
|
93
|
+
}
|
|
94
|
+
|
|
88
95
|
function asRecord(value: unknown): Record<string, unknown> {
|
|
89
96
|
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
90
97
|
? (value as Record<string, unknown>)
|
|
@@ -107,8 +114,6 @@ function resolveSetting<T extends TSchema>(schema: T, value: unknown): Static<T>
|
|
|
107
114
|
) as Static<T>;
|
|
108
115
|
}
|
|
109
116
|
|
|
110
|
-
export const DEFAULT_CONFIG: HashlineEditConfig = resolveSetting(configSchema, {});
|
|
111
|
-
|
|
112
117
|
/** Parse JSON directly without stripping comments (standard JSON forbids comments; on error fall back to default). */
|
|
113
118
|
function readSettings(filePath: string): Record<string, unknown> {
|
|
114
119
|
try {
|
|
@@ -3,9 +3,11 @@ import { test } from "node:test";
|
|
|
3
3
|
import childProcess from "node:child_process";
|
|
4
4
|
import { syncBuiltinESMExports } from "node:module";
|
|
5
5
|
import { DiagnosticBuffer } from "./diagnostic-buffer.ts";
|
|
6
|
-
import { DEFAULT_OUTPUT_BUDGETS } from "./
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { DEFAULT_OUTPUT_BUDGETS } from "./tool-factories.testing.ts";
|
|
7
|
+
import { grepOutput } from "./grep-output.ts";
|
|
8
|
+
import { createAnchorFormatter } from "./anchor-format.ts";
|
|
9
|
+
import { runRgPaths } from "./rg-process.ts";
|
|
10
|
+
const MAX_RG_STDERR_BYTES = 64 * 1024;
|
|
9
11
|
import { mkdtemp, writeFile, rm } from "node:fs/promises";
|
|
10
12
|
import { tmpdir } from "node:os";
|
|
11
13
|
import { join } from "node:path";
|
|
@@ -46,19 +48,27 @@ test("diagnostics within the budget are unchanged", () => {
|
|
|
46
48
|
assert.equal(buffer.toString(), source);
|
|
47
49
|
});
|
|
48
50
|
|
|
49
|
-
test("search warning aggregation retains the final cause and labels truncation", () => {
|
|
50
|
-
const { facts, payload } =
|
|
51
|
+
test("search warning aggregation retains the final cause and labels truncation", async () => {
|
|
52
|
+
const { facts, payload } = await grepOutput({
|
|
51
53
|
budget: DEFAULT_OUTPUT_BUDGETS,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
cwd: process.cwd(),
|
|
55
|
+
anchors: createAnchorFormatter(4),
|
|
56
|
+
context: 0,
|
|
57
|
+
search: {
|
|
58
|
+
files: [
|
|
59
|
+
{
|
|
60
|
+
filePath: join(process.cwd(), "confirmed match"),
|
|
61
|
+
rows: [],
|
|
62
|
+
snapshot: { revision: "", validUtf8: true },
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
matchLimitReached: false,
|
|
66
|
+
scanLimitReached: false,
|
|
56
67
|
},
|
|
57
|
-
warnings: [diagnostic(DEFAULT_OUTPUT_BUDGETS.fieldBytes)],
|
|
58
|
-
|
|
59
|
-
outputMode: "content",
|
|
60
|
-
matchLimitReached: false,
|
|
68
|
+
warnings: [{ reason: diagnostic(DEFAULT_OUTPUT_BUDGETS.fieldBytes) }],
|
|
69
|
+
outputMode: "files",
|
|
61
70
|
effectiveLimit: 100,
|
|
71
|
+
literalPipe: false,
|
|
62
72
|
});
|
|
63
73
|
assert.equal(payload, "confirmed match");
|
|
64
74
|
assert.match(facts.diagnostics ?? "", /regex parse error:/);
|
package/src/pi/edit-tool.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Override edit: hashline ops via structured `
|
|
2
|
+
* Override edit: hashline ops via structured `steps` (LINE#HASH anchors).
|
|
3
3
|
*
|
|
4
|
-
* Each op in `
|
|
4
|
+
* Each op in `steps` references line anchors from read, grep, or edit failure diagnostics.
|
|
5
5
|
* The core verifies each anchor live against
|
|
6
6
|
* the current file content — no prior-read revision guard: a cited line
|
|
7
7
|
* that changed (or was misremembered) fails its own anchor; unchanged lines
|
|
@@ -14,11 +14,10 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
17
|
-
import { Type, type Static
|
|
17
|
+
import { Type, type Static } from "typebox";
|
|
18
18
|
import { applyEdits } from "../core/apply.ts";
|
|
19
19
|
import { unwritableTextError } from "../core/text.ts";
|
|
20
20
|
import { EDIT_ANCHOR_FIELDS, type Anchor, type Edit } from "../core/types.ts";
|
|
21
|
-
import type { HashlineEditConfig } from "./config.ts";
|
|
22
21
|
import { anchorPattern, createAnchorFormatter, parseAnchorToken } from "./anchor-format.ts";
|
|
23
22
|
import {
|
|
24
23
|
diffCardRenderers,
|
|
@@ -27,14 +26,14 @@ import {
|
|
|
27
26
|
type DiffCounts,
|
|
28
27
|
} from "./render.ts";
|
|
29
28
|
import { describeEditFailure } from "./failure-context.ts";
|
|
30
|
-
import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
|
|
31
29
|
import {
|
|
32
30
|
mutationToolBase,
|
|
33
31
|
runTextMutation,
|
|
34
|
-
type ActionFusionExecutor,
|
|
35
32
|
type MutationTarget,
|
|
33
|
+
type MutationToolEnvironment,
|
|
36
34
|
} from "./mutation-runner.ts";
|
|
37
35
|
import { argumentItems, type ReportArgumentIssue } from "./argument-validation.ts";
|
|
36
|
+
import { filePathField, literalUnion } from "./schema.ts";
|
|
38
37
|
|
|
39
38
|
/** Split a validated anchor token; prepareArguments and the schema already checked it. */
|
|
40
39
|
function parseAnchor(value: string): Anchor;
|
|
@@ -44,7 +43,7 @@ function parseAnchor(value: string | undefined) {
|
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
/** Operation item fields besides `action`. */
|
|
47
|
-
type EditItemField = (typeof EDIT_ANCHOR_FIELDS)[number] | "
|
|
46
|
+
type EditItemField = (typeof EDIT_ANCHOR_FIELDS)[number] | "lines";
|
|
48
47
|
|
|
49
48
|
/** What one op does and which item fields it takes; it takes no others. */
|
|
50
49
|
interface EditOpSpec {
|
|
@@ -56,21 +55,21 @@ interface EditOpSpec {
|
|
|
56
55
|
/** Every op and its fields; the schema states them, and argument checks enforce them. */
|
|
57
56
|
const EDIT_OPS = {
|
|
58
57
|
replace: {
|
|
59
|
-
meaning: "Replace the cited line(s) with
|
|
60
|
-
required: ["anchor", "
|
|
58
|
+
meaning: "Replace the cited line(s) with lines.",
|
|
59
|
+
required: ["anchor", "lines"],
|
|
61
60
|
optional: ["end"],
|
|
62
61
|
},
|
|
63
62
|
delete: { meaning: "Delete the cited line(s).", required: ["anchor"], optional: ["end"] },
|
|
64
63
|
insert_after: {
|
|
65
|
-
meaning: "Insert
|
|
66
|
-
required: ["anchor", "
|
|
64
|
+
meaning: "Insert lines after the anchor line, which is kept; do not repeat it in lines.",
|
|
65
|
+
required: ["anchor", "lines"],
|
|
67
66
|
},
|
|
68
67
|
insert_before: {
|
|
69
|
-
meaning: "Insert
|
|
70
|
-
required: ["anchor", "
|
|
68
|
+
meaning: "Insert lines before the anchor line, which is kept; do not repeat it in lines.",
|
|
69
|
+
required: ["anchor", "lines"],
|
|
71
70
|
},
|
|
72
|
-
append: { meaning: "Add
|
|
73
|
-
prepend: { meaning: "Add
|
|
71
|
+
append: { meaning: "Add lines at the end of the file.", required: ["lines"] },
|
|
72
|
+
prepend: { meaning: "Add lines at the start of the file.", required: ["lines"] },
|
|
74
73
|
} as const satisfies Record<string, EditOpSpec>;
|
|
75
74
|
type EditOpName = keyof typeof EDIT_OPS;
|
|
76
75
|
const EDIT_OP_NAMES = Object.keys(EDIT_OPS) as EditOpName[];
|
|
@@ -93,17 +92,13 @@ function buildEditSchema(hashLen: number) {
|
|
|
93
92
|
const anchorField = (description: string) => Type.Optional(Type.String({ pattern, description }));
|
|
94
93
|
const editOpSchema = Type.Object(
|
|
95
94
|
{
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
action:
|
|
100
|
-
EDIT_OP_NAMES.map((op) =>
|
|
101
|
-
Type.Literal(op, { description: describeEditOp(EDIT_OPS[op]) }),
|
|
102
|
-
) as [TLiteral<EditOpName>, ...TLiteral<EditOpName>[]],
|
|
103
|
-
),
|
|
95
|
+
// Session evidence is mixed: OpenAI calls usually follow declaration order, while
|
|
96
|
+
// GLM and local calls often use lexical key order. Keep `action`, `anchor`,
|
|
97
|
+
// `end`, `lines` so both orders choose the operation and range before its payload.
|
|
98
|
+
action: literalUnion(EDIT_OP_NAMES, (op) => describeEditOp(EDIT_OPS[op])),
|
|
104
99
|
anchor: anchorField("LINE#HASH of the target line."),
|
|
105
100
|
end: anchorField("Inclusive last LINE#HASH of the range; omit for one line."),
|
|
106
|
-
|
|
101
|
+
lines: Type.Optional(
|
|
107
102
|
Type.Array(Type.String({ pattern: "^[^\\r\\n]*$" }), {
|
|
108
103
|
minItems: 1,
|
|
109
104
|
description:
|
|
@@ -113,19 +108,16 @@ function buildEditSchema(hashLen: number) {
|
|
|
113
108
|
},
|
|
114
109
|
{
|
|
115
110
|
additionalProperties: false,
|
|
116
|
-
description: "One
|
|
111
|
+
description: "One step. Omit, or set to null, each field its action does not take.",
|
|
117
112
|
},
|
|
118
113
|
);
|
|
119
114
|
return Type.Object(
|
|
120
115
|
{
|
|
121
|
-
path:
|
|
122
|
-
|
|
123
|
-
description: "Path to the file (relative or absolute)",
|
|
124
|
-
}),
|
|
125
|
-
edits: Type.Array(editOpSchema, {
|
|
116
|
+
path: filePathField(),
|
|
117
|
+
steps: Type.Array(editOpSchema, {
|
|
126
118
|
minItems: 1,
|
|
127
119
|
maxItems: MAX_EDIT_OPS,
|
|
128
|
-
description: `
|
|
120
|
+
description: `Steps applied to one snapshot; ranges must not overlap. Each gap between lines takes at most one insertion; insert_after N and insert_before N+1 target the same gap, so put all new lines for a gap in one step. An insertion at the start of a replaced or deleted range goes before it. Anchors are LINE#HASH with exactly ${hashLen} hash characters. Copy each anchor from read, grep, or edit failure diagnostics; never write a hash you have not seen there. Shell output such as sed, head, or cat has no anchors, so read or grep those lines first.`,
|
|
129
121
|
}),
|
|
130
122
|
},
|
|
131
123
|
{ additionalProperties: false },
|
|
@@ -135,13 +127,13 @@ type EditSchema = ReturnType<typeof buildEditSchema>;
|
|
|
135
127
|
|
|
136
128
|
type EditParams = Static<EditSchema>;
|
|
137
129
|
|
|
138
|
-
type EditOpInput = EditParams["
|
|
139
|
-
type EditOpSchema = EditSchema["properties"]["
|
|
130
|
+
type EditOpInput = EditParams["steps"][number];
|
|
131
|
+
type EditOpSchema = EditSchema["properties"]["steps"]["items"];
|
|
140
132
|
|
|
141
133
|
/** How to correct edit argument issues; each reason states only what is wrong. */
|
|
142
134
|
const EDIT_ISSUE_FIX = {
|
|
143
135
|
emptyReplace: 'Use {"action":"delete"} to remove lines, or supply the replacement lines.',
|
|
144
|
-
|
|
136
|
+
emptyLines: 'Remove this step or supply at least one line ([""] for a blank line).',
|
|
145
137
|
unsafeLine: 'Copy a complete "LINE#HASH" token from the latest tool result.',
|
|
146
138
|
hashLength: "Read or grep the file for current anchors.",
|
|
147
139
|
} as const;
|
|
@@ -157,14 +149,14 @@ function checkEditArguments(
|
|
|
157
149
|
item: EditOpSchema,
|
|
158
150
|
report: ReportArgumentIssue,
|
|
159
151
|
): void {
|
|
160
|
-
argumentItems((args as {
|
|
152
|
+
argumentItems((args as { steps?: unknown } | null)?.steps).forEach((value, index) => {
|
|
161
153
|
if (value === null || typeof value !== "object") return;
|
|
162
154
|
const edit = value as Record<string, unknown>;
|
|
163
155
|
// The schema reports an unknown action; which fields it takes is then unknown.
|
|
164
156
|
if (typeof edit.action !== "string" || !Object.hasOwn(EDIT_OPS, edit.action)) return;
|
|
165
157
|
const name = edit.action as EditOpName;
|
|
166
158
|
const spec: EditOpSpec = EDIT_OPS[name];
|
|
167
|
-
const path = `
|
|
159
|
+
const path = `steps[${index}]`;
|
|
168
160
|
const accepted = new Set([...spec.required, ...(spec.optional ?? [])]);
|
|
169
161
|
for (const [field, declaration] of Object.entries(item.properties)) {
|
|
170
162
|
if (field === "action") continue;
|
|
@@ -179,18 +171,18 @@ function checkEditArguments(
|
|
|
179
171
|
);
|
|
180
172
|
}
|
|
181
173
|
|
|
182
|
-
const
|
|
183
|
-
if (accepted.has("
|
|
184
|
-
if (
|
|
174
|
+
const lines = edit.lines;
|
|
175
|
+
if (accepted.has("lines") && Array.isArray(lines)) {
|
|
176
|
+
if (lines.length === 0) {
|
|
185
177
|
report(
|
|
186
|
-
`${path}.
|
|
178
|
+
`${path}.lines`,
|
|
187
179
|
"is empty",
|
|
188
|
-
name === "replace" ? EDIT_ISSUE_FIX.emptyReplace : EDIT_ISSUE_FIX.
|
|
180
|
+
name === "replace" ? EDIT_ISSUE_FIX.emptyReplace : EDIT_ISSUE_FIX.emptyLines,
|
|
189
181
|
);
|
|
190
182
|
}
|
|
191
|
-
|
|
183
|
+
lines.forEach((line, lineIndex) => {
|
|
192
184
|
const unwritable = typeof line === "string" ? unwritableTextError(line) : undefined;
|
|
193
|
-
if (unwritable) report(`${path}.
|
|
185
|
+
if (unwritable) report(`${path}.lines[${lineIndex}]`, unwritable.message);
|
|
194
186
|
});
|
|
195
187
|
}
|
|
196
188
|
const cited: { anchor?: number; end?: number } = {};
|
|
@@ -226,7 +218,7 @@ function checkEditArguments(
|
|
|
226
218
|
}
|
|
227
219
|
|
|
228
220
|
/**
|
|
229
|
-
* Translate validated public operations into core
|
|
221
|
+
* Translate validated public operations into core steps, parsing numeric anchor
|
|
230
222
|
* positions. Argument checks guarantee each op carries exactly the fields it takes.
|
|
231
223
|
*/
|
|
232
224
|
function toCoreEdits(ops: readonly EditOpInput[]): Edit[] {
|
|
@@ -237,16 +229,16 @@ function toCoreEdits(ops: readonly EditOpInput[]): Edit[] {
|
|
|
237
229
|
op: "replace",
|
|
238
230
|
anchor: parseAnchor(op.anchor!),
|
|
239
231
|
end: parseAnchor(op.end),
|
|
240
|
-
body: op.
|
|
232
|
+
body: op.lines!,
|
|
241
233
|
};
|
|
242
234
|
case "delete":
|
|
243
235
|
return { op: "delete", anchor: parseAnchor(op.anchor!), end: parseAnchor(op.end) };
|
|
244
236
|
case "insert_after":
|
|
245
237
|
case "insert_before":
|
|
246
|
-
return { op: op.action, anchor: parseAnchor(op.anchor!), body: op.
|
|
238
|
+
return { op: op.action, anchor: parseAnchor(op.anchor!), body: op.lines! };
|
|
247
239
|
case "append":
|
|
248
240
|
case "prepend":
|
|
249
|
-
return { op: op.action, body: op.
|
|
241
|
+
return { op: op.action, body: op.lines! };
|
|
250
242
|
}
|
|
251
243
|
});
|
|
252
244
|
}
|
|
@@ -255,14 +247,14 @@ function toCoreEdits(ops: readonly EditOpInput[]): Edit[] {
|
|
|
255
247
|
function editHeader(args: EditParams, theme: Theme, counts?: DiffCounts): string {
|
|
256
248
|
let t = theme.fg("toolTitle", theme.bold("edit "));
|
|
257
249
|
t += formatHeaderPath(args.path, theme);
|
|
258
|
-
const
|
|
259
|
-
const n =
|
|
250
|
+
const steps = Array.isArray(args.steps) ? args.steps : [];
|
|
251
|
+
const n = steps.length;
|
|
260
252
|
if (n) {
|
|
261
253
|
const opCounts: Record<string, number> = Object.fromEntries(
|
|
262
254
|
[...EDIT_OP_NAMES, "unknown"].map((kind) => [kind, 0]),
|
|
263
255
|
);
|
|
264
256
|
// Renderers receive partial arguments; fixed buckets keep one-pass counting O(1) in space.
|
|
265
|
-
for (const edit of
|
|
257
|
+
for (const edit of steps) {
|
|
266
258
|
const kind = edit?.action;
|
|
267
259
|
if (typeof kind === "string" && Object.hasOwn(opCounts, kind)) opCounts[kind]++;
|
|
268
260
|
else opCounts.unknown++;
|
|
@@ -277,33 +269,26 @@ function editHeader(args: EditParams, theme: Theme, counts?: DiffCounts): string
|
|
|
277
269
|
return t;
|
|
278
270
|
}
|
|
279
271
|
|
|
280
|
-
export function makeEditOverride(
|
|
281
|
-
cwd: string,
|
|
282
|
-
config: HashlineEditConfig,
|
|
283
|
-
fusion?: ActionFusionExecutor,
|
|
284
|
-
budgets: OutputBudgetProvider = defaultOutputBudgets,
|
|
285
|
-
) {
|
|
272
|
+
export function makeEditOverride(env: MutationToolEnvironment) {
|
|
286
273
|
// Schema, verification, and recovery use the registered configuration.
|
|
287
|
-
const { hashLen, shiftRadius } = config;
|
|
274
|
+
const { hashLen, shiftRadius } = env.config;
|
|
288
275
|
const schema = buildEditSchema(hashLen);
|
|
289
276
|
|
|
290
277
|
return {
|
|
291
278
|
...mutationToolBase({
|
|
279
|
+
...env,
|
|
292
280
|
tool: "edit",
|
|
293
|
-
cwd,
|
|
294
|
-
fusion,
|
|
295
|
-
budgets,
|
|
296
281
|
schema,
|
|
297
282
|
description:
|
|
298
283
|
"Edit whole lines using LINE#HASH anchors checked against the current file. Use replace instead when the same change repeats in several places, or when only a fragment of a long line changes.",
|
|
299
284
|
promptSnippet: "Edit file lines using verified anchors",
|
|
300
285
|
promptGuidelines: [
|
|
301
|
-
"Batch all
|
|
286
|
+
"Batch all steps for one file into a single edit call.",
|
|
302
287
|
"Before citing a line after write, edit, or replace, get its anchor from a new read or grep. Only after edit, lines above the earliest changed line keep their anchors.",
|
|
303
288
|
],
|
|
304
289
|
check: (args, report) =>
|
|
305
|
-
checkEditArguments(args, hashLen, schema.properties.
|
|
306
|
-
run: (params, target) => runEdit(target, params.
|
|
290
|
+
checkEditArguments(args, hashLen, schema.properties.steps.items, report),
|
|
291
|
+
run: (params, target) => runEdit(target, params.steps, hashLen, shiftRadius),
|
|
307
292
|
}),
|
|
308
293
|
...diffCardRenderers("edit", "Editing…", editHeader),
|
|
309
294
|
};
|
package/src/pi/error-text.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { errnoCode, HashlineError } from "../core/errors.ts";
|
|
2
2
|
|
|
3
|
+
export interface SearchWarning {
|
|
4
|
+
readonly path?: string;
|
|
5
|
+
readonly reason: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Cancellation, worded like Pi's built-in tools. A mutation report's
|
|
5
10
|
* `publication` says where it stopped, so the message does not.
|
|
@@ -37,9 +42,9 @@ export function searchChangedError(): HashlineError<"FILE_CHANGED"> {
|
|
|
37
42
|
* A per-file failure that leaves the rest of the search valid: a filesystem error, or the
|
|
38
43
|
* file changing while it was searched or displayed. Undefined for any other failure.
|
|
39
44
|
*/
|
|
40
|
-
export function fileReadWarning(filePath: string, error: unknown):
|
|
45
|
+
export function fileReadWarning(filePath: string, error: unknown): SearchWarning | undefined {
|
|
41
46
|
if (!(error instanceof Error)) return undefined;
|
|
42
47
|
const changed = error instanceof HashlineError && error.errorCode === "FILE_CHANGED";
|
|
43
48
|
if (!changed && errnoCode(error) === undefined) return undefined;
|
|
44
|
-
return
|
|
49
|
+
return { path: filePath, reason: error.message };
|
|
45
50
|
}
|