@criogaid/pi-hashline-edit 0.8.0 → 0.10.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 +13 -10
- package/README.zh-CN.md +12 -9
- 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 +118 -293
- package/src/core/apply.ts +63 -165
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.ts +4 -4
- 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 +16 -8
- package/src/core/ranges.ts +10 -6
- 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 +7 -43
- package/src/integration/config-registration.test.ts +2 -2
- package/src/integration/fusion-dispatch.test.ts +2 -5
- package/src/integration/grep-flags.test.ts +0 -14
- package/src/integration/grep-js-completeness.test.ts +34 -8
- package/src/integration/grep-line-candidates.test.ts +4 -8
- package/src/integration/grep-matrix.test.ts +3 -7
- package/src/integration/grep-path-matrix.test.ts +5 -5
- package/src/integration/grep-prefilter-coverage.test.ts +3 -7
- package/src/integration/grep-process-limits.test.ts +78 -0
- package/src/integration/grep-rg.test.ts +123 -25
- package/src/integration/grep-scan-budget.test.ts +2 -2
- package/src/integration/grep-translation.test.ts +12 -27
- package/src/integration/large-text.test.ts +1 -47
- package/src/integration/model-budget-lifecycle.test.ts +13 -3
- package/src/integration/mutation-file-types.testing.ts +1 -1
- package/src/integration/npm-package.testing.ts +1 -1
- package/src/integration/publication-read-changes.test.ts +125 -47
- package/src/integration/replace-worker-boundary.test.ts +21 -11
- package/src/integration/report-replay.test.ts +21 -23
- package/src/integration/utf8-errors.test.ts +1 -1
- package/src/pi/AGENTS.md +16 -16
- package/src/pi/action-fusion.test.ts +168 -300
- package/src/pi/action-fusion.ts +148 -111
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +52 -207
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +20 -22
- package/src/pi/argument-validation.test.ts +382 -418
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +5 -4
- package/src/pi/config.test.ts +5 -2
- package/src/pi/config.ts +4 -6
- package/src/pi/diagnostic-buffer.test.ts +12 -1
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +173 -226
- package/src/pi/error-text.ts +4 -8
- package/src/pi/execute.test.ts +165 -266
- package/src/pi/failure-context.test.ts +144 -126
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +168 -141
- package/src/pi/file-commit.test.ts +110 -53
- package/src/pi/file-commit.ts +90 -137
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +4 -1
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +2 -101
- package/src/pi/grep-candidates.ts +23 -69
- package/src/pi/grep-matrix.test.ts +2 -2
- package/src/pi/grep-order.test.ts +4 -4
- package/src/pi/grep-output.ts +62 -96
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +92 -0
- package/src/pi/grep-scope.ts +40 -42
- package/src/pi/grep-search.ts +22 -31
- package/src/pi/grep-tool.ts +55 -82
- package/src/pi/grep.test.ts +81 -46
- package/src/pi/model-budgets.test.ts +25 -23
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +51 -20
- package/src/pi/mutation-runner.ts +126 -70
- package/src/pi/output-budgets.test.ts +38 -16
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +3 -1
- package/src/pi/read-tool.ts +5 -5
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +82 -20
- package/src/pi/render.test.ts +79 -42
- package/src/pi/render.ts +69 -63
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -83
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +9 -5
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +1 -2
- package/src/pi/report-schema.ts +65 -81
- package/src/pi/report.test.ts +171 -25
- package/src/pi/report.ts +33 -87
- package/src/pi/result-safety.test.ts +177 -189
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +48 -64
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +12 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/write-tool.ts +33 -62
- package/src/pi/write.test.ts +1 -3
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/pi/replace-worker-contract.ts +0 -10
package/src/pi/edit-tool.ts
CHANGED
|
@@ -5,9 +5,7 @@
|
|
|
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
|
|
8
|
-
* elsewhere never block the edit.
|
|
9
|
-
* at execution; Pi may coerce a single object before validation. Legacy
|
|
10
|
-
* oldText/newText inputs fail schema validation before mutation.
|
|
8
|
+
* elsewhere never block the edit.
|
|
11
9
|
*
|
|
12
10
|
* Concurrency safety: read-modify-write is wrapped in withFileMutationQueue.
|
|
13
11
|
* AbortSignal is honored — checked after read / before write.
|
|
@@ -15,48 +13,28 @@
|
|
|
15
13
|
* @module pi-hashline-edit/pi
|
|
16
14
|
*/
|
|
17
15
|
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
type Theme,
|
|
21
|
-
type ToolDefinition,
|
|
22
|
-
type ToolRenderResultOptions,
|
|
23
|
-
} from "@earendil-works/pi-coding-agent";
|
|
24
|
-
import type { AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
|
|
25
|
-
import { Type, type Static } from "typebox";
|
|
26
|
-
import {
|
|
27
|
-
ACTION_FUSION_GUIDELINES,
|
|
28
|
-
withThenRunDescription,
|
|
29
|
-
withThenRunSchema,
|
|
30
|
-
type ThenRunInput,
|
|
31
|
-
} from "./action-fusion.ts";
|
|
16
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
import { Type, type Static, type TLiteral } from "typebox";
|
|
32
18
|
import { applyEdits } from "../core/apply.ts";
|
|
33
19
|
import { unwritableTextError } from "../core/text.ts";
|
|
34
20
|
import { EDIT_ANCHOR_FIELDS, type Anchor, type Edit } from "../core/types.ts";
|
|
35
21
|
import type { HashlineEditConfig } from "./config.ts";
|
|
36
22
|
import { anchorPattern, createAnchorFormatter, parseAnchorToken } from "./anchor-format.ts";
|
|
37
23
|
import {
|
|
24
|
+
diffCardRenderers,
|
|
38
25
|
formatDiffCounts,
|
|
39
|
-
|
|
40
|
-
renderMutationResult,
|
|
26
|
+
formatHeaderPath,
|
|
41
27
|
type DiffCounts,
|
|
42
28
|
} from "./render.ts";
|
|
43
29
|
import { describeEditFailure } from "./failure-context.ts";
|
|
44
30
|
import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
|
|
45
31
|
import {
|
|
46
|
-
|
|
32
|
+
mutationToolBase,
|
|
47
33
|
runTextMutation,
|
|
48
34
|
type ActionFusionExecutor,
|
|
49
35
|
type MutationTarget,
|
|
50
|
-
type TextMutationDetails,
|
|
51
36
|
} from "./mutation-runner.ts";
|
|
52
|
-
import type
|
|
53
|
-
import {
|
|
54
|
-
argumentItems,
|
|
55
|
-
createArgumentPreparer,
|
|
56
|
-
type ReportArgumentIssue,
|
|
57
|
-
} from "./argument-validation.ts";
|
|
58
|
-
type EditDetails = MutationResultDetails<TextMutationDetails>;
|
|
59
|
-
type EditRenderContext = Parameters<NonNullable<ToolDefinition<EditSchema>["renderCall"]>>[2];
|
|
37
|
+
import { argumentItems, type ReportArgumentIssue } from "./argument-validation.ts";
|
|
60
38
|
|
|
61
39
|
/** Split a validated anchor token; prepareArguments and the schema already checked it. */
|
|
62
40
|
function parseAnchor(value: string): Anchor;
|
|
@@ -65,90 +43,79 @@ function parseAnchor(value: string | undefined) {
|
|
|
65
43
|
return value === undefined ? undefined : parseAnchorToken(value)!;
|
|
66
44
|
}
|
|
67
45
|
|
|
68
|
-
/**
|
|
46
|
+
/** Operation item fields besides `action`. */
|
|
47
|
+
type EditItemField = (typeof EDIT_ANCHOR_FIELDS)[number] | "body";
|
|
48
|
+
|
|
49
|
+
/** What one op does and which item fields it takes; it takes no others. */
|
|
50
|
+
interface EditOpSpec {
|
|
51
|
+
readonly meaning: string;
|
|
52
|
+
readonly required: readonly EditItemField[];
|
|
53
|
+
readonly optional?: readonly EditItemField[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Every op and its fields; the schema states them, and argument checks enforce them. */
|
|
57
|
+
const EDIT_OPS = {
|
|
58
|
+
replace: {
|
|
59
|
+
meaning: "Replace the cited line(s) with body.",
|
|
60
|
+
required: ["anchor", "body"],
|
|
61
|
+
optional: ["end"],
|
|
62
|
+
},
|
|
63
|
+
delete: { meaning: "Delete the cited line(s).", required: ["anchor"], optional: ["end"] },
|
|
64
|
+
insert_after: {
|
|
65
|
+
meaning: "Insert body after the anchor line, which is kept; do not repeat it in body.",
|
|
66
|
+
required: ["anchor", "body"],
|
|
67
|
+
},
|
|
68
|
+
insert_before: {
|
|
69
|
+
meaning: "Insert body before the anchor line, which is kept; do not repeat it in body.",
|
|
70
|
+
required: ["anchor", "body"],
|
|
71
|
+
},
|
|
72
|
+
append: { meaning: "Add body at the end of the file.", required: ["body"] },
|
|
73
|
+
prepend: { meaning: "Add body at the start of the file.", required: ["body"] },
|
|
74
|
+
} as const satisfies Record<string, EditOpSpec>;
|
|
75
|
+
type EditOpName = keyof typeof EDIT_OPS;
|
|
76
|
+
const EDIT_OP_NAMES = Object.keys(EDIT_OPS) as EditOpName[];
|
|
77
|
+
|
|
78
|
+
/** Operations in one call; constrained sampling ends a runaway batch at this bound. */
|
|
79
|
+
const MAX_EDIT_OPS = 64;
|
|
80
|
+
|
|
81
|
+
/** An op's meaning followed by the fields it takes. */
|
|
82
|
+
function describeEditOp({ meaning, required, optional = [] }: EditOpSpec): string {
|
|
83
|
+
return `${meaning} Takes ${required.join(" and ")}${optional.length ? `; ${optional.join(", ")} is optional` : ""}.`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Edit parameters; anchors must carry exactly `hashLen` hash characters. Each
|
|
88
|
+
* operation is one flat object, so providers can constrain sampling to the
|
|
89
|
+
* schema; argument checks enforce which fields each op takes.
|
|
90
|
+
*/
|
|
69
91
|
function buildEditSchema(hashLen: number) {
|
|
70
92
|
const pattern = anchorPattern(hashLen);
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
description:
|
|
88
|
-
"Copy, or move (also removes the source), whole lines whose text and indentation stay unchanged; use a body edit when either changes.",
|
|
89
|
-
}),
|
|
90
|
-
anchor: requiredAnchor,
|
|
91
|
-
end: optionalEnd,
|
|
92
|
-
};
|
|
93
|
-
const editOpSchema = Type.Union([
|
|
94
|
-
Type.Object(
|
|
95
|
-
{
|
|
96
|
-
op: Type.Literal("replace", { description: "Replace the cited line(s) with `body`." }),
|
|
97
|
-
anchor: requiredAnchor,
|
|
98
|
-
end: optionalEnd,
|
|
99
|
-
body: bodyLines,
|
|
100
|
-
},
|
|
101
|
-
{ additionalProperties: false },
|
|
102
|
-
),
|
|
103
|
-
Type.Object(
|
|
104
|
-
{
|
|
105
|
-
op: Type.Literal("delete", { description: "Delete the cited line(s)." }),
|
|
106
|
-
anchor: requiredAnchor,
|
|
107
|
-
end: optionalEnd,
|
|
108
|
-
},
|
|
109
|
-
{ additionalProperties: false },
|
|
110
|
-
),
|
|
111
|
-
Type.Object(
|
|
112
|
-
{
|
|
113
|
-
op: Type.Union([Type.Literal("insert_after"), Type.Literal("insert_before")], {
|
|
93
|
+
const anchorField = (description: string) => Type.Optional(Type.String({ pattern, description }));
|
|
94
|
+
const editOpSchema = Type.Object(
|
|
95
|
+
{
|
|
96
|
+
// Constrained sampling and some gateways emit keys alphabetically; `action`
|
|
97
|
+
// sorts first, so the operation is chosen before the fields it governs.
|
|
98
|
+
// TypeBox infers a union's static type only from a tuple.
|
|
99
|
+
action: Type.Union(
|
|
100
|
+
EDIT_OP_NAMES.map((op) =>
|
|
101
|
+
Type.Literal(op, { description: describeEditOp(EDIT_OPS[op]) }),
|
|
102
|
+
) as [TLiteral<EditOpName>, ...TLiteral<EditOpName>[]],
|
|
103
|
+
),
|
|
104
|
+
anchor: anchorField("LINE#HASH of the target line."),
|
|
105
|
+
end: anchorField("Inclusive last LINE#HASH of the range; omit for one line."),
|
|
106
|
+
body: Type.Optional(
|
|
107
|
+
Type.Array(Type.String({ pattern: "^[^\\r\\n]*$" }), {
|
|
108
|
+
minItems: 1,
|
|
114
109
|
description:
|
|
115
|
-
|
|
116
|
-
}),
|
|
117
|
-
anchor: requiredAnchor,
|
|
118
|
-
body: bodyLines,
|
|
119
|
-
},
|
|
120
|
-
{ additionalProperties: false },
|
|
121
|
-
),
|
|
122
|
-
Type.Object(
|
|
123
|
-
{
|
|
124
|
-
...transferFields,
|
|
125
|
-
before: Type.String({
|
|
126
|
-
pattern,
|
|
127
|
-
description: "Insert captured lines before this original line.",
|
|
128
|
-
}),
|
|
129
|
-
},
|
|
130
|
-
{ additionalProperties: false },
|
|
131
|
-
),
|
|
132
|
-
Type.Object(
|
|
133
|
-
{
|
|
134
|
-
...transferFields,
|
|
135
|
-
after: Type.String({
|
|
136
|
-
pattern,
|
|
137
|
-
description: "Insert captured lines after this original line.",
|
|
110
|
+
'New lines, one per element, without CR/LF. At least one; [""] is one blank line.',
|
|
138
111
|
}),
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}),
|
|
147
|
-
body: bodyLines,
|
|
148
|
-
},
|
|
149
|
-
{ additionalProperties: false },
|
|
150
|
-
),
|
|
151
|
-
]);
|
|
112
|
+
),
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
additionalProperties: false,
|
|
116
|
+
description: "One operation. Omit, or set to null, each field its action does not take.",
|
|
117
|
+
},
|
|
118
|
+
);
|
|
152
119
|
return Type.Object(
|
|
153
120
|
{
|
|
154
121
|
path: Type.String({
|
|
@@ -157,7 +124,8 @@ function buildEditSchema(hashLen: number) {
|
|
|
157
124
|
}),
|
|
158
125
|
edits: Type.Array(editOpSchema, {
|
|
159
126
|
minItems: 1,
|
|
160
|
-
|
|
127
|
+
maxItems: MAX_EDIT_OPS,
|
|
128
|
+
description: `Operations on 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 body. 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.`,
|
|
161
129
|
}),
|
|
162
130
|
},
|
|
163
131
|
{ additionalProperties: false },
|
|
@@ -165,87 +133,120 @@ function buildEditSchema(hashLen: number) {
|
|
|
165
133
|
}
|
|
166
134
|
type EditSchema = ReturnType<typeof buildEditSchema>;
|
|
167
135
|
|
|
168
|
-
type EditParams = Static<EditSchema
|
|
136
|
+
type EditParams = Static<EditSchema>;
|
|
169
137
|
|
|
170
|
-
type EditOpInput =
|
|
138
|
+
type EditOpInput = EditParams["edits"][number];
|
|
139
|
+
type EditOpSchema = EditSchema["properties"]["edits"]["items"];
|
|
171
140
|
|
|
172
141
|
/** How to correct edit argument issues; each reason states only what is wrong. */
|
|
173
142
|
const EDIT_ISSUE_FIX = {
|
|
174
|
-
emptyReplace: 'Use {"
|
|
143
|
+
emptyReplace: 'Use {"action":"delete"} to remove lines, or supply the replacement lines.',
|
|
175
144
|
emptyBody: 'Remove this edit or supply at least one line ([""] for a blank line).',
|
|
176
145
|
unsafeLine: 'Copy a complete "LINE#HASH" token from the latest tool result.',
|
|
177
146
|
hashLength: "Read or grep the file for current anchors.",
|
|
178
147
|
} as const;
|
|
179
148
|
|
|
180
|
-
/**
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
149
|
+
/**
|
|
150
|
+
* Report semantic argument issues: the fields each op takes, and field values
|
|
151
|
+
* the schema cannot judge. Malformed shapes remain Pi's schema responsibility;
|
|
152
|
+
* Pi has already removed null optional fields.
|
|
153
|
+
*/
|
|
154
|
+
function checkEditArguments(
|
|
155
|
+
args: unknown,
|
|
156
|
+
hashLen: number,
|
|
157
|
+
item: EditOpSchema,
|
|
158
|
+
report: ReportArgumentIssue,
|
|
159
|
+
): void {
|
|
160
|
+
argumentItems((args as { edits?: unknown } | null)?.edits).forEach((value, index) => {
|
|
161
|
+
if (value === null || typeof value !== "object") return;
|
|
162
|
+
const edit = value as Record<string, unknown>;
|
|
163
|
+
// The schema reports an unknown action; which fields it takes is then unknown.
|
|
164
|
+
if (typeof edit.action !== "string" || !Object.hasOwn(EDIT_OPS, edit.action)) return;
|
|
165
|
+
const name = edit.action as EditOpName;
|
|
166
|
+
const spec: EditOpSpec = EDIT_OPS[name];
|
|
167
|
+
const path = `edits[${index}]`;
|
|
168
|
+
const accepted = new Set([...spec.required, ...(spec.optional ?? [])]);
|
|
169
|
+
for (const [field, declaration] of Object.entries(item.properties)) {
|
|
170
|
+
if (field === "action") continue;
|
|
171
|
+
const given = edit[field] !== undefined;
|
|
172
|
+
if (given && !accepted.has(field as EditItemField))
|
|
173
|
+
report(`${path}.${field}`, "is not allowed", `"${name}": ${describeEditOp(spec)}`);
|
|
174
|
+
else if (!given && spec.required.includes(field as EditItemField))
|
|
175
|
+
report(
|
|
176
|
+
`${path}.${field}`,
|
|
177
|
+
`is required for "${name}"`,
|
|
178
|
+
Reflect.get(declaration, "description"),
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const body = edit.body;
|
|
183
|
+
if (accepted.has("body") && Array.isArray(body)) {
|
|
187
184
|
if (body.length === 0) {
|
|
188
185
|
report(
|
|
189
|
-
|
|
186
|
+
`${path}.body`,
|
|
190
187
|
"is empty",
|
|
191
|
-
|
|
192
|
-
? EDIT_ISSUE_FIX.emptyReplace
|
|
193
|
-
: EDIT_ISSUE_FIX.emptyBody,
|
|
188
|
+
name === "replace" ? EDIT_ISSUE_FIX.emptyReplace : EDIT_ISSUE_FIX.emptyBody,
|
|
194
189
|
);
|
|
195
190
|
}
|
|
196
191
|
body.forEach((line, lineIndex) => {
|
|
197
192
|
const unwritable = typeof line === "string" ? unwritableTextError(line) : undefined;
|
|
198
|
-
if (unwritable) report(
|
|
193
|
+
if (unwritable) report(`${path}.body[${lineIndex}]`, unwritable.message);
|
|
199
194
|
});
|
|
200
195
|
}
|
|
196
|
+
const cited: { anchor?: number; end?: number } = {};
|
|
201
197
|
for (const field of EDIT_ANCHOR_FIELDS) {
|
|
202
|
-
const
|
|
203
|
-
if (typeof
|
|
204
|
-
const token = parseAnchorToken(
|
|
205
|
-
if (
|
|
198
|
+
const anchor = edit[field];
|
|
199
|
+
if (!accepted.has(field) || typeof anchor !== "string") continue;
|
|
200
|
+
const token = parseAnchorToken(anchor);
|
|
201
|
+
if (!token) continue;
|
|
202
|
+
if (Number.isSafeInteger(token.line)) {
|
|
203
|
+
cited[field] = token.line;
|
|
204
|
+
} else {
|
|
206
205
|
report(
|
|
207
|
-
|
|
206
|
+
`${path}.${field}`,
|
|
208
207
|
"line number exceeds the safe integer range",
|
|
209
208
|
EDIT_ISSUE_FIX.unsafeLine,
|
|
210
209
|
);
|
|
211
210
|
}
|
|
212
|
-
if (token
|
|
211
|
+
if (token.hash.length !== hashLen) {
|
|
213
212
|
report(
|
|
214
|
-
|
|
213
|
+
`${path}.${field}`,
|
|
215
214
|
`has ${token.hash.length} hash characters; hashLen is ${hashLen}`,
|
|
216
215
|
EDIT_ISSUE_FIX.hashLength,
|
|
217
216
|
);
|
|
218
217
|
}
|
|
219
218
|
}
|
|
219
|
+
|
|
220
|
+
// The cited line numbers alone decide this, whatever the file holds. An
|
|
221
|
+
// unsafe line number was reported above, and comparing it would add a false issue.
|
|
222
|
+
if (cited.anchor !== undefined && cited.end !== undefined && cited.end < cited.anchor) {
|
|
223
|
+
report(`${path}.end`, `line ${cited.end} is before anchor line ${cited.anchor}`);
|
|
224
|
+
}
|
|
220
225
|
});
|
|
221
226
|
}
|
|
222
227
|
|
|
223
|
-
/**
|
|
228
|
+
/**
|
|
229
|
+
* Translate validated public operations into core edits, parsing numeric anchor
|
|
230
|
+
* positions. Argument checks guarantee each op carries exactly the fields it takes.
|
|
231
|
+
*/
|
|
224
232
|
function toCoreEdits(ops: readonly EditOpInput[]): Edit[] {
|
|
225
233
|
return ops.map((op) => {
|
|
226
|
-
switch (op.
|
|
234
|
+
switch (op.action) {
|
|
227
235
|
case "replace":
|
|
228
236
|
return {
|
|
229
237
|
op: "replace",
|
|
230
|
-
anchor: parseAnchor(op.anchor),
|
|
238
|
+
anchor: parseAnchor(op.anchor!),
|
|
231
239
|
end: parseAnchor(op.end),
|
|
232
|
-
body: op.body
|
|
240
|
+
body: op.body!,
|
|
233
241
|
};
|
|
234
242
|
case "delete":
|
|
235
|
-
return { op: "delete", anchor: parseAnchor(op.anchor), end: parseAnchor(op.end) };
|
|
236
|
-
case "copy":
|
|
237
|
-
case "move": {
|
|
238
|
-
const source = { op: op.op, anchor: parseAnchor(op.anchor), end: parseAnchor(op.end) };
|
|
239
|
-
return "before" in op
|
|
240
|
-
? { ...source, before: parseAnchor(op.before) }
|
|
241
|
-
: { ...source, after: parseAnchor(op.after) };
|
|
242
|
-
}
|
|
243
|
+
return { op: "delete", anchor: parseAnchor(op.anchor!), end: parseAnchor(op.end) };
|
|
243
244
|
case "insert_after":
|
|
244
245
|
case "insert_before":
|
|
245
|
-
return { op: op.
|
|
246
|
+
return { op: op.action, anchor: parseAnchor(op.anchor!), body: op.body! };
|
|
246
247
|
case "append":
|
|
247
248
|
case "prepend":
|
|
248
|
-
return { op: op.
|
|
249
|
+
return { op: op.action, body: op.body! };
|
|
249
250
|
}
|
|
250
251
|
});
|
|
251
252
|
}
|
|
@@ -253,24 +254,16 @@ function toCoreEdits(ops: readonly EditOpInput[]): Edit[] {
|
|
|
253
254
|
/** Call header with total/per-kind operation counts and the result's diff counts. */
|
|
254
255
|
function editHeader(args: EditParams, theme: Theme, counts?: DiffCounts): string {
|
|
255
256
|
let t = theme.fg("toolTitle", theme.bold("edit "));
|
|
256
|
-
t +=
|
|
257
|
+
t += formatHeaderPath(args.path, theme);
|
|
257
258
|
const edits = Array.isArray(args.edits) ? args.edits : [];
|
|
258
259
|
const n = edits.length;
|
|
259
260
|
if (n) {
|
|
260
|
-
const opCounts: Record<
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
copy: 0,
|
|
264
|
-
move: 0,
|
|
265
|
-
insert_before: 0,
|
|
266
|
-
insert_after: 0,
|
|
267
|
-
append: 0,
|
|
268
|
-
prepend: 0,
|
|
269
|
-
unknown: 0,
|
|
270
|
-
};
|
|
261
|
+
const opCounts: Record<string, number> = Object.fromEntries(
|
|
262
|
+
[...EDIT_OP_NAMES, "unknown"].map((kind) => [kind, 0]),
|
|
263
|
+
);
|
|
271
264
|
// Renderers receive partial arguments; fixed buckets keep one-pass counting O(1) in space.
|
|
272
265
|
for (const edit of edits) {
|
|
273
|
-
const kind = edit?.
|
|
266
|
+
const kind = edit?.action;
|
|
274
267
|
if (typeof kind === "string" && Object.hasOwn(opCounts, kind)) opCounts[kind]++;
|
|
275
268
|
else opCounts.unknown++;
|
|
276
269
|
}
|
|
@@ -292,62 +285,27 @@ export function makeEditOverride(
|
|
|
292
285
|
) {
|
|
293
286
|
// Schema, verification, and recovery use the registered configuration.
|
|
294
287
|
const { hashLen, shiftRadius } = config;
|
|
295
|
-
const
|
|
288
|
+
const schema = buildEditSchema(hashLen);
|
|
296
289
|
|
|
297
290
|
return {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
fusion !== undefined,
|
|
303
|
-
),
|
|
304
|
-
promptSnippet: "Edit file lines using verified anchors",
|
|
305
|
-
promptGuidelines: [
|
|
306
|
-
"Batch all edits to one file into a single edit call.",
|
|
307
|
-
"After edit or replace, get fresh anchors with read or grep before citing any line it changed. If it added or removed lines, also refresh anchors below the earliest affected line; anchors above it remain valid. If the affected positions are unknown, read or grep the lines you intend to cite.",
|
|
308
|
-
...(fusion ? ACTION_FUSION_GUIDELINES : []),
|
|
309
|
-
],
|
|
310
|
-
parameters,
|
|
311
|
-
prepareArguments: createArgumentPreparer(
|
|
312
|
-
"edit",
|
|
313
|
-
parameters,
|
|
314
|
-
(args, report) => checkEditArguments(args, hashLen, report),
|
|
291
|
+
...mutationToolBase({
|
|
292
|
+
tool: "edit",
|
|
293
|
+
cwd,
|
|
294
|
+
fusion,
|
|
315
295
|
budgets,
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
)
|
|
329
|
-
return renderMutationResult(result, options, theme, context, "Editing…", editHeader, "edit");
|
|
330
|
-
},
|
|
331
|
-
|
|
332
|
-
async execute(
|
|
333
|
-
toolCallId: string,
|
|
334
|
-
params: EditParams,
|
|
335
|
-
signal: AbortSignal | undefined,
|
|
336
|
-
onUpdate: AgentToolUpdateCallback<EditDetails> | undefined,
|
|
337
|
-
ctx: ExtensionToolContext,
|
|
338
|
-
) {
|
|
339
|
-
return executeMutation<EditParams, TextMutationDetails, "edit">(
|
|
340
|
-
{
|
|
341
|
-
tool: "edit",
|
|
342
|
-
cwd,
|
|
343
|
-
fusion,
|
|
344
|
-
budgets,
|
|
345
|
-
run: (mutationParams, target) =>
|
|
346
|
-
runEdit(target, mutationParams.edits, hashLen, shiftRadius),
|
|
347
|
-
},
|
|
348
|
-
{ toolCallId, params, signal, onUpdate, ctx },
|
|
349
|
-
);
|
|
350
|
-
},
|
|
296
|
+
schema,
|
|
297
|
+
description:
|
|
298
|
+
"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
|
+
promptSnippet: "Edit file lines using verified anchors",
|
|
300
|
+
promptGuidelines: [
|
|
301
|
+
"Batch all edits to one file into a single edit call.",
|
|
302
|
+
"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
|
+
],
|
|
304
|
+
check: (args, report) =>
|
|
305
|
+
checkEditArguments(args, hashLen, schema.properties.edits.items, report),
|
|
306
|
+
run: (params, target) => runEdit(target, params.edits, hashLen, shiftRadius),
|
|
307
|
+
}),
|
|
308
|
+
...diffCardRenderers("edit", "Editing…", editHeader),
|
|
351
309
|
};
|
|
352
310
|
}
|
|
353
311
|
|
|
@@ -360,24 +318,13 @@ function runEdit(
|
|
|
360
318
|
const anchorFormatter = createAnchorFormatter(hashLen);
|
|
361
319
|
|
|
362
320
|
return runTextMutation<"edit">(target, (currentText) => {
|
|
363
|
-
const translated = toCoreEdits(editOps);
|
|
364
|
-
|
|
365
321
|
// Recovery reports checksum candidates from nearby lines, then the whole file
|
|
366
322
|
// if needed. Every failed anchor still rejects the batch; callers inspect
|
|
367
323
|
// candidates and resubmit with fresh anchors.
|
|
368
|
-
const result = applyEdits(currentText,
|
|
324
|
+
const result = applyEdits(currentText, toCoreEdits(editOps), hashLen, shiftRadius);
|
|
369
325
|
if (!result.ok) {
|
|
370
|
-
throw describeEditFailure(
|
|
371
|
-
result.failure,
|
|
372
|
-
{ currentText, anchors: anchorFormatter },
|
|
373
|
-
translated.length > 1,
|
|
374
|
-
target.budget,
|
|
375
|
-
);
|
|
326
|
+
throw describeEditFailure(currentText, result.failures, anchorFormatter, target.budget);
|
|
376
327
|
}
|
|
377
|
-
|
|
378
|
-
return {
|
|
379
|
-
text: result.text,
|
|
380
|
-
facts: {},
|
|
381
|
-
};
|
|
328
|
+
return { text: result.text, facts: {} };
|
|
382
329
|
});
|
|
383
330
|
}
|
package/src/pi/error-text.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { errnoCode, HashlineError } from "../core/errors.ts";
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Cancellation, worded like Pi's built-in tools. A mutation report's
|
|
5
|
-
* `publication`
|
|
5
|
+
* `publication` says where it stopped, so the message does not.
|
|
6
6
|
* Whatever a cancelled operation threw is its effect, not a cause.
|
|
7
7
|
*/
|
|
8
8
|
export function cancellationError(): HashlineError<"OPERATION_ABORTED"> {
|
|
@@ -26,7 +26,7 @@ export class FileChangedDuringReadError extends HashlineError<"FILE_CHANGED"> {
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* A searched file no longer matches what grep matched. grep binds matches to the
|
|
29
|
-
* revision grep-search hashed while matching, and grep-
|
|
29
|
+
* revision grep-search hashed while matching, and grep-reread reads the file again
|
|
30
30
|
* against it.
|
|
31
31
|
*/
|
|
32
32
|
export function searchChangedError(): HashlineError<"FILE_CHANGED"> {
|
|
@@ -37,12 +37,8 @@ export function searchChangedError(): HashlineError<"FILE_CHANGED"> {
|
|
|
37
37
|
* A per-file failure that leaves the rest of the search valid: a filesystem error, or the
|
|
38
38
|
* file changing while it was searched or displayed. Undefined for any other failure.
|
|
39
39
|
*/
|
|
40
|
-
export function fileReadWarning(
|
|
41
|
-
|
|
42
|
-
error: unknown,
|
|
43
|
-
signal?: AbortSignal,
|
|
44
|
-
): string | undefined {
|
|
45
|
-
if (signal?.aborted || !(error instanceof Error)) return undefined;
|
|
40
|
+
export function fileReadWarning(filePath: string, error: unknown): string | undefined {
|
|
41
|
+
if (!(error instanceof Error)) return undefined;
|
|
46
42
|
const changed = error instanceof HashlineError && error.errorCode === "FILE_CHANGED";
|
|
47
43
|
if (!changed && errnoCode(error) === undefined) return undefined;
|
|
48
44
|
return `Could not read ${filePath}: ${error.message}`;
|