@criogaid/pi-hashline-edit 0.4.0 → 0.5.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 +116 -20
- package/package.json +3 -3
- package/src/core/apply.test.ts +194 -104
- package/src/core/apply.ts +6 -37
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +8 -8
- package/src/core/hash.ts +5 -5
- package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
- package/src/core/text.ts +1 -3
- package/src/core/types.ts +4 -7
- package/src/index.ts +9 -11
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +73 -60
- package/src/integration/large-text.test.ts +9 -8
- package/src/integration/replace-package.test.ts +11 -7
- package/src/pi/action-fusion.test.ts +110 -34
- package/src/pi/action-fusion.ts +77 -114
- package/src/pi/anchor-format.test.ts +43 -22
- package/src/pi/anchor-format.ts +23 -8
- package/src/pi/budgets.ts +7 -2
- package/src/pi/config.test.ts +153 -40
- package/src/pi/config.ts +87 -41
- package/src/pi/e2e-scenarios.test.ts +12 -11
- package/src/pi/edit-tool.ts +20 -156
- package/src/pi/error-text.ts +10 -5
- package/src/pi/execute.test.ts +299 -175
- package/src/pi/failure-context.test.ts +7 -2
- package/src/pi/failure-context.ts +155 -8
- package/src/pi/failure-path.test.ts +33 -26
- package/src/pi/file-commit.ts +2 -1
- package/src/pi/fusion-card.ts +83 -2
- package/src/pi/grep-output.ts +26 -9
- package/src/pi/grep-render.ts +6 -5
- package/src/pi/grep-scope.ts +2 -15
- package/src/pi/grep-search.ts +29 -15
- package/src/pi/grep-tool.ts +94 -78
- package/src/pi/grep.test.ts +357 -320
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +51 -2
- package/src/pi/mutation-result.ts +62 -55
- package/src/pi/mutation-runner.ts +16 -33
- package/src/pi/pi.test.ts +10 -5
- package/src/pi/read-tool.ts +61 -33
- package/src/pi/registration.test.ts +1 -5
- package/src/pi/render.test.ts +16 -8
- package/src/pi/render.ts +4 -91
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +24 -58
- package/src/pi/replace-worker.ts +2 -2
- package/src/pi/replace.test.ts +62 -58
- package/src/pi/result-safety.test.ts +124 -84
- package/src/pi/rg-line-ranges.ts +3 -2
- package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +35 -4
- package/src/pi/rg-text-view.ts +17 -33
- package/src/pi/schema.ts +2 -10
- package/src/pi/write-tool.ts +1 -2
- package/src/pi/write.test.ts +10 -9
- package/src/core/index.ts +0 -14
- package/src/pi/state.ts +0 -26
package/src/pi/render.test.ts
CHANGED
|
@@ -5,10 +5,12 @@ import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/m
|
|
|
5
5
|
import { displayCarriageReturns } from "./anchor-format.ts";
|
|
6
6
|
import { makeEditOverride } from "./edit-tool.ts";
|
|
7
7
|
import { makeReplaceTool } from "./replace-tool.ts";
|
|
8
|
-
import { renderDiffPreview
|
|
8
|
+
import { renderDiffPreview } from "./render.ts";
|
|
9
|
+
import { withMutationStatus } from "./fusion-card.ts";
|
|
9
10
|
import { makeWriteOverride } from "./write-tool.ts";
|
|
10
11
|
import { generateMutationDetails } from "./mutation-result.ts";
|
|
11
12
|
import type { ActionFusionDetails } from "./action-fusion.ts";
|
|
13
|
+
import { DEFAULT_CONFIG } from "./config.ts";
|
|
12
14
|
|
|
13
15
|
const versions = { publishedRevision: "r", observedRevision: "r" };
|
|
14
16
|
const mutationDetails = (actionFusion?: ActionFusionDetails) => ({
|
|
@@ -18,7 +20,10 @@ const mutationDetails = (actionFusion?: ActionFusionDetails) => ({
|
|
|
18
20
|
|
|
19
21
|
test("mutation headers refresh in place and retain isolated per-call counts", () => {
|
|
20
22
|
initTheme("dark");
|
|
21
|
-
for (const tool of [
|
|
23
|
+
for (const tool of [
|
|
24
|
+
makeEditOverride(process.cwd(), DEFAULT_CONFIG),
|
|
25
|
+
makeReplaceTool(process.cwd(), DEFAULT_CONFIG),
|
|
26
|
+
]) {
|
|
22
27
|
const args = {
|
|
23
28
|
path: "a.txt",
|
|
24
29
|
edits: [{ op: "append" as const, body: ["new"] }],
|
|
@@ -54,7 +59,7 @@ test("mutation headers refresh in place and retain isolated per-call counts", ()
|
|
|
54
59
|
|
|
55
60
|
test("edit call titles show structured batches and tolerate invalid partial input", () => {
|
|
56
61
|
initTheme("dark");
|
|
57
|
-
const tool = makeEditOverride(process.cwd());
|
|
62
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
58
63
|
const edits = [
|
|
59
64
|
{ op: "replace" as const, anchor: "1#AB", body: ["new"] },
|
|
60
65
|
{ op: "append" as const, body: ["more"] },
|
|
@@ -73,7 +78,7 @@ test("edit call titles show structured batches and tolerate invalid partial inpu
|
|
|
73
78
|
|
|
74
79
|
test("mutation card owns stale-anchor notices without internal status", () => {
|
|
75
80
|
initTheme("dark");
|
|
76
|
-
const tool = withMutationStatus(makeEditOverride(process.cwd()));
|
|
81
|
+
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
77
82
|
const args = { path: "a.txt", edits: [{ op: "append" as const, body: ["new"] }] };
|
|
78
83
|
const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
|
|
79
84
|
const card = tool.renderCall!(args, theme, context);
|
|
@@ -103,7 +108,7 @@ test("mutation card owns stale-anchor notices without internal status", () => {
|
|
|
103
108
|
|
|
104
109
|
test("mutation card omits status when then_run was not requested", () => {
|
|
105
110
|
initTheme("dark");
|
|
106
|
-
const tool = withMutationStatus(makeEditOverride(process.cwd()));
|
|
111
|
+
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
107
112
|
const args = { path: "a.txt", edits: [{ op: "append" as const, body: ["new"] }] };
|
|
108
113
|
const context: any = { args, state: {}, isPartial: false, isError: false, invalidate() {} };
|
|
109
114
|
const card = tool.renderCall!(args, theme, context);
|
|
@@ -180,7 +185,10 @@ test("mutation previews hide CRLF boundaries even with mixed endings or an unter
|
|
|
180
185
|
assert.equal(details.diff, displayCarriageReturns(generateDiffString(before, after).diff));
|
|
181
186
|
assert.match(details.diff, /␍/);
|
|
182
187
|
assert.ok(details.patch.includes("-old\r\n"));
|
|
183
|
-
for (const tool of [
|
|
188
|
+
for (const tool of [
|
|
189
|
+
makeEditOverride(process.cwd(), DEFAULT_CONFIG),
|
|
190
|
+
makeReplaceTool(process.cwd(), DEFAULT_CONFIG),
|
|
191
|
+
]) {
|
|
184
192
|
for (const expanded of [false, true]) {
|
|
185
193
|
const context: any = { args: { path: "a.txt" }, state: {}, isError: false };
|
|
186
194
|
const rendered = tool.renderResult(
|
|
@@ -207,7 +215,7 @@ test("diff previews retain standalone CR and literal control-picture characters"
|
|
|
207
215
|
{ publishedRevision: "r", observedRevision: "r" },
|
|
208
216
|
"PUBLISHED",
|
|
209
217
|
);
|
|
210
|
-
const tool = makeEditOverride(process.cwd());
|
|
218
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
211
219
|
const result = tool.renderResult(
|
|
212
220
|
{ content: [], details },
|
|
213
221
|
{ expanded: true, isPartial: false },
|
|
@@ -225,7 +233,7 @@ test("diff previews retain standalone CR and literal control-picture characters"
|
|
|
225
233
|
|
|
226
234
|
test("withMutationStatus renderResult initializes mutationShell defensively", () => {
|
|
227
235
|
initTheme("dark");
|
|
228
|
-
const tool = withMutationStatus(makeEditOverride(process.cwd()));
|
|
236
|
+
const tool = withMutationStatus(makeEditOverride(process.cwd(), DEFAULT_CONFIG));
|
|
229
237
|
const context: any = { args: { path: "a.txt" }, state: {}, isError: false };
|
|
230
238
|
const container = tool.renderResult!(
|
|
231
239
|
{
|
package/src/pi/render.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared helpers for the hashline-aware tool renderers.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* an anchor can be copied straight into an edit op. The user-facing TUI
|
|
6
|
-
* renderers (read, grep) strip that prefix back to a clean ` N: content` form.
|
|
7
|
-
* Parsing the anchor format in one place keeps read and grep in sync.
|
|
4
|
+
* Mutation previews and shared error presentation for the user-facing TUI.
|
|
8
5
|
*
|
|
9
6
|
* @module pi-hashline-edit/pi
|
|
10
7
|
*/
|
|
@@ -12,32 +9,10 @@
|
|
|
12
9
|
import {
|
|
13
10
|
renderDiff,
|
|
14
11
|
type Theme,
|
|
15
|
-
type ToolDefinition,
|
|
16
12
|
type ToolRenderResultOptions,
|
|
17
13
|
} from "@earendil-works/pi-coding-agent";
|
|
18
14
|
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
19
|
-
import {
|
|
20
|
-
import type { TSchema } from "typebox";
|
|
21
|
-
|
|
22
|
-
const HASHLINE_RE = /^(\d+)#[A-Za-z0-9]+│(.*)$/;
|
|
23
|
-
|
|
24
|
-
export interface HashlineRow {
|
|
25
|
-
/** Line number as written in the anchor (string form). */
|
|
26
|
-
lineNo: string;
|
|
27
|
-
/** Line content with the `LINE#HASH│` prefix removed. */
|
|
28
|
-
content: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Parse a `LINE#HASH│content` anchor line.
|
|
33
|
-
*
|
|
34
|
-
* @returns the row, or `null` for non-anchor lines (headers, notices, free
|
|
35
|
-
* text) so callers can fall through to their own formatting.
|
|
36
|
-
*/
|
|
37
|
-
export function parseHashline(line: string): HashlineRow | null {
|
|
38
|
-
const m = line.match(HASHLINE_RE);
|
|
39
|
-
return m ? { lineNo: m[1], content: m[2] } : null;
|
|
40
|
-
}
|
|
15
|
+
import { Text, type Component } from "@earendil-works/pi-tui";
|
|
41
16
|
|
|
42
17
|
/** Max diff lines shown when a result is rendered collapsed. */
|
|
43
18
|
const MAX_COLLAPSED_DIFF_LINES = 24;
|
|
@@ -66,15 +41,10 @@ export interface DiffCounts {
|
|
|
66
41
|
removed: number;
|
|
67
42
|
}
|
|
68
43
|
|
|
69
|
-
|
|
44
|
+
/** Row-local render state shared by the mutation call and result renderers. */
|
|
45
|
+
export interface MutationRenderState {
|
|
70
46
|
diffCounts?: DiffCounts;
|
|
71
47
|
callText?: Text;
|
|
72
|
-
mutationShell?: {
|
|
73
|
-
box: Box;
|
|
74
|
-
call?: Component;
|
|
75
|
-
result?: Component;
|
|
76
|
-
fileState?: { freshness?: string };
|
|
77
|
-
};
|
|
78
48
|
}
|
|
79
49
|
|
|
80
50
|
/** Count added/removed lines in a pi-format diff (`+N content` / `-N content` / ` N content`). */
|
|
@@ -175,60 +145,3 @@ export function renderMutationResult<TArgs>(
|
|
|
175
145
|
}
|
|
176
146
|
return new Text(renderDiffPreview(diff, expanded, theme), 0, 0);
|
|
177
147
|
}
|
|
178
|
-
|
|
179
|
-
/** Keep the mutation card's background independent of the fused command's lifetime. */
|
|
180
|
-
export function withMutationStatus<TParams extends TSchema, TDetails>(
|
|
181
|
-
tool: ToolDefinition<TParams, TDetails, MutationRenderState>,
|
|
182
|
-
): ToolDefinition<TParams, TDetails, MutationRenderState> {
|
|
183
|
-
return {
|
|
184
|
-
...tool,
|
|
185
|
-
renderShell: "self",
|
|
186
|
-
renderCall(args, theme, context) {
|
|
187
|
-
const shell = (context.state.mutationShell ??= { box: new Box(1, 1) });
|
|
188
|
-
shell.call = tool.renderCall!(args, theme, { ...context, lastComponent: shell.call });
|
|
189
|
-
shell.box.clear();
|
|
190
|
-
shell.box.addChild(shell.call);
|
|
191
|
-
shell.box.setBgFn((line: string) =>
|
|
192
|
-
theme.bg(
|
|
193
|
-
context.isPartial ? "toolPendingBg" : context.isError ? "toolErrorBg" : "toolSuccessBg",
|
|
194
|
-
line,
|
|
195
|
-
),
|
|
196
|
-
);
|
|
197
|
-
return shell.box;
|
|
198
|
-
},
|
|
199
|
-
renderResult(result, options, theme, context) {
|
|
200
|
-
const shell = (context.state.mutationShell ??= { box: new Box(1, 1) });
|
|
201
|
-
const details = result.details as
|
|
202
|
-
| { actionFusion?: { mutationCompleted?: boolean; freshness?: string } }
|
|
203
|
-
| undefined;
|
|
204
|
-
const isPartial = options.isPartial && details?.actionFusion?.mutationCompleted !== true;
|
|
205
|
-
// Fused errors retain combined details for the model; show the mutation's summary here.
|
|
206
|
-
const fusedError = context.isError && (context.args as { then_run?: unknown })?.then_run;
|
|
207
|
-
shell.result = fusedError
|
|
208
|
-
? renderToolError(result, theme)
|
|
209
|
-
: tool.renderResult!(result, { ...options, isPartial }, theme, {
|
|
210
|
-
...context,
|
|
211
|
-
isPartial,
|
|
212
|
-
lastComponent: shell.result,
|
|
213
|
-
});
|
|
214
|
-
// Pi runs renderCall first; update its box in place without invalidating the tool row.
|
|
215
|
-
shell.box.addChild(shell.result);
|
|
216
|
-
const file = details?.actionFusion ?? shell.fileState;
|
|
217
|
-
if (file) {
|
|
218
|
-
shell.fileState = { freshness: file.freshness };
|
|
219
|
-
if (file.freshness === "changed" || file.freshness === "missing") {
|
|
220
|
-
shell.box.addChild(
|
|
221
|
-
new Text(theme.fg("warning", `Anchors are stale: target ${file.freshness}.`), 0, 0),
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
shell.box.setBgFn((line: string) =>
|
|
226
|
-
theme.bg(
|
|
227
|
-
isPartial ? "toolPendingBg" : context.isError ? "toolErrorBg" : "toolSuccessBg",
|
|
228
|
-
line,
|
|
229
|
-
),
|
|
230
|
-
);
|
|
231
|
-
return new Container();
|
|
232
|
-
},
|
|
233
|
-
};
|
|
234
|
-
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run a replace batch that contains regex rules in a worker thread, so a
|
|
3
|
+
* catastrophic pattern can be stopped by the configured timeout or by
|
|
4
|
+
* cancellation. The worker (`replace-worker.mjs` → `replace-worker.ts`) calls
|
|
5
|
+
* the same `core/replace.ts` engine that literal-only batches run in-process.
|
|
6
|
+
*
|
|
7
|
+
* @module pi-hashline-edit/pi
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Worker } from "node:worker_threads";
|
|
11
|
+
import type { applyReplacements, Replacement } from "../core/replace.ts";
|
|
12
|
+
import { cancellationError, throwIfCancelled } from "./error-text.ts";
|
|
13
|
+
|
|
14
|
+
type ReplaceResult = ReturnType<typeof applyReplacements>;
|
|
15
|
+
|
|
16
|
+
export async function runRegexReplacements(
|
|
17
|
+
source: string,
|
|
18
|
+
rules: readonly Replacement[],
|
|
19
|
+
timeoutMs: number,
|
|
20
|
+
signal: AbortSignal | undefined,
|
|
21
|
+
): Promise<ReplaceResult> {
|
|
22
|
+
throwIfCancelled(signal);
|
|
23
|
+
const worker = new Worker(new URL("./replace-worker.mjs", import.meta.url), {
|
|
24
|
+
workerData: { source, rules },
|
|
25
|
+
});
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
let settled = false;
|
|
28
|
+
const finish = (result: ReplaceResult | Error, terminate = false) => {
|
|
29
|
+
if (settled) return;
|
|
30
|
+
settled = true;
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
signal?.removeEventListener("abort", abort);
|
|
33
|
+
const complete = () => (result instanceof Error ? reject(result) : resolve(result));
|
|
34
|
+
if (terminate) void worker.terminate().then(complete, reject);
|
|
35
|
+
else complete();
|
|
36
|
+
};
|
|
37
|
+
const abort = () => finish(cancellationError(), true);
|
|
38
|
+
const timer = setTimeout(
|
|
39
|
+
() => finish(new Error(`regex evaluation timed out after ${timeoutMs}ms`), true),
|
|
40
|
+
timeoutMs,
|
|
41
|
+
);
|
|
42
|
+
worker.on("message", (message: { result?: ReplaceResult; error?: string }) => {
|
|
43
|
+
if (message.error !== undefined) finish(new Error(message.error));
|
|
44
|
+
else if (message.result) finish(message.result);
|
|
45
|
+
else finish(new Error("Regex worker returned an invalid result"));
|
|
46
|
+
});
|
|
47
|
+
worker.on("error", (error) =>
|
|
48
|
+
finish(error instanceof Error ? error : new Error(String(error))),
|
|
49
|
+
);
|
|
50
|
+
worker.on("exit", (code) => finish(new Error(`Regex worker exited with code ${code}`)));
|
|
51
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
52
|
+
if (signal?.aborted) abort();
|
|
53
|
+
});
|
|
54
|
+
}
|
package/src/pi/replace-tool.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*
|
|
19
19
|
* Concurrency: read-modify-write is wrapped in Pi's withFileMutationQueue
|
|
20
20
|
* (shared with `edit`), so a `replace` and an `edit` on the same file never
|
|
21
|
-
* interleave. Regex batches run in a cancellable worker with a time limit.
|
|
21
|
+
* interleave. Regex batches run in a cancellable worker with a time limit (replace-regex).
|
|
22
22
|
*
|
|
23
23
|
* @module pi-hashline-edit/pi
|
|
24
24
|
*/
|
|
@@ -31,12 +31,13 @@ import {
|
|
|
31
31
|
} from "@earendil-works/pi-coding-agent";
|
|
32
32
|
import type { AgentToolResult, AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
|
|
33
33
|
import { Type, type Static } from "typebox";
|
|
34
|
-
import { Worker } from "node:worker_threads";
|
|
35
34
|
import { splitLines } from "../core/lines.ts";
|
|
36
|
-
import { applyReplacements, buildRegex
|
|
35
|
+
import { applyReplacements, buildRegex } from "../core/replace.ts";
|
|
36
|
+
import { runRegexReplacements } from "./replace-regex.ts";
|
|
37
37
|
import { unwritableTextReason } from "../core/text.ts";
|
|
38
38
|
import { ACTION_FUSION_GUIDELINES, withThenRunSchema, type ThenRunInput } from "./action-fusion.ts";
|
|
39
39
|
import { createAnchorFormatter, type AnchorFormatter } from "./anchor-format.ts";
|
|
40
|
+
import type { HashlineEditConfig } from "./config.ts";
|
|
40
41
|
import {
|
|
41
42
|
formatDiffCounts,
|
|
42
43
|
renderMutationCall,
|
|
@@ -51,12 +52,8 @@ import {
|
|
|
51
52
|
type MutationTarget,
|
|
52
53
|
type TextMutationDetails,
|
|
53
54
|
} from "./mutation-runner.ts";
|
|
54
|
-
import {
|
|
55
|
-
|
|
56
|
-
errorMessage,
|
|
57
|
-
invalidArgument,
|
|
58
|
-
throwIfCancelled,
|
|
59
|
-
} from "./error-text.ts";
|
|
55
|
+
import { errorMessage } from "../core/errors.ts";
|
|
56
|
+
import { invalidArgument, throwIfCancelled } from "./error-text.ts";
|
|
60
57
|
type ReplaceDetails = TextMutationDetails;
|
|
61
58
|
type ReplaceRenderContext = Parameters<
|
|
62
59
|
NonNullable<ToolDefinition<typeof replaceSchema>["renderCall"]>
|
|
@@ -111,49 +108,6 @@ function createReplaceSchema(actionFusion: boolean) {
|
|
|
111
108
|
}
|
|
112
109
|
type ReplaceParams = Static<typeof replaceSchema> & { then_run?: ThenRunInput };
|
|
113
110
|
|
|
114
|
-
const REGEX_TIMEOUT_MS = 5_000;
|
|
115
|
-
|
|
116
|
-
async function applyRegexReplacements(
|
|
117
|
-
source: string,
|
|
118
|
-
rules: readonly Replacement[],
|
|
119
|
-
signal: AbortSignal | undefined,
|
|
120
|
-
): Promise<{ text: string; count: number }> {
|
|
121
|
-
throwIfCancelled(signal);
|
|
122
|
-
const worker = new Worker(new URL("./replace-worker.mjs", import.meta.url), {
|
|
123
|
-
workerData: { source, rules },
|
|
124
|
-
});
|
|
125
|
-
return new Promise((resolve, reject) => {
|
|
126
|
-
let settled = false;
|
|
127
|
-
const finish = (result: { text: string; count: number } | Error, terminate = false) => {
|
|
128
|
-
if (settled) return;
|
|
129
|
-
settled = true;
|
|
130
|
-
clearTimeout(timer);
|
|
131
|
-
signal?.removeEventListener("abort", abort);
|
|
132
|
-
const complete = () => (result instanceof Error ? reject(result) : resolve(result));
|
|
133
|
-
if (terminate) void worker.terminate().then(complete, reject);
|
|
134
|
-
else complete();
|
|
135
|
-
};
|
|
136
|
-
const abort = () => finish(cancellationError(), true);
|
|
137
|
-
const timer = setTimeout(
|
|
138
|
-
() => finish(new Error(`regex evaluation timed out after ${REGEX_TIMEOUT_MS}ms`), true),
|
|
139
|
-
REGEX_TIMEOUT_MS,
|
|
140
|
-
);
|
|
141
|
-
worker.on(
|
|
142
|
-
"message",
|
|
143
|
-
(message: { result?: { text: string; count: number }; error?: string }) => {
|
|
144
|
-
if (message.error !== undefined) finish(new Error(message.error));
|
|
145
|
-
else if (message.result) finish(message.result);
|
|
146
|
-
else finish(new Error("Regex worker returned an invalid result"));
|
|
147
|
-
},
|
|
148
|
-
);
|
|
149
|
-
worker.on("error", (error) =>
|
|
150
|
-
finish(error instanceof Error ? error : new Error(String(error))),
|
|
151
|
-
);
|
|
152
|
-
worker.on("exit", (code) => finish(new Error(`Regex worker exited with code ${code}`)));
|
|
153
|
-
signal?.addEventListener("abort", abort, { once: true });
|
|
154
|
-
if (signal?.aborted) abort();
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
111
|
/**
|
|
158
112
|
* Bound candidate anchors by stripping common prefix/suffix lines in O(n).
|
|
159
113
|
* For a pure deletion, retain its first surviving successor. Shifted suffixes
|
|
@@ -243,7 +197,11 @@ function checkReplaceArguments(args: unknown): void {
|
|
|
243
197
|
});
|
|
244
198
|
}
|
|
245
199
|
|
|
246
|
-
export function makeReplaceTool(
|
|
200
|
+
export function makeReplaceTool(
|
|
201
|
+
cwd: string,
|
|
202
|
+
config: HashlineEditConfig,
|
|
203
|
+
fusion?: ActionFusionExecutor,
|
|
204
|
+
) {
|
|
247
205
|
const parameters = createReplaceSchema(fusion !== undefined);
|
|
248
206
|
return {
|
|
249
207
|
name: "replace" as const,
|
|
@@ -294,8 +252,7 @@ export function makeReplaceTool(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
294
252
|
{
|
|
295
253
|
cwd,
|
|
296
254
|
fusion,
|
|
297
|
-
|
|
298
|
-
run: (mutationParams, target) => runReplace(target, mutationParams.replacements),
|
|
255
|
+
run: (mutationParams, target) => runReplace(target, mutationParams.replacements, config),
|
|
299
256
|
},
|
|
300
257
|
{ toolCallId, params, signal, onUpdate, ctx },
|
|
301
258
|
);
|
|
@@ -303,15 +260,24 @@ export function makeReplaceTool(cwd: string, fusion?: ActionFusionExecutor) {
|
|
|
303
260
|
};
|
|
304
261
|
}
|
|
305
262
|
|
|
306
|
-
function runReplace(
|
|
307
|
-
|
|
263
|
+
function runReplace(
|
|
264
|
+
target: MutationTarget,
|
|
265
|
+
rules: ReplaceParams["replacements"],
|
|
266
|
+
config: HashlineEditConfig,
|
|
267
|
+
) {
|
|
268
|
+
const anchorFormatter = createAnchorFormatter(config.hashLen);
|
|
308
269
|
|
|
309
270
|
return runTextMutation("replace", target, async (currentText) => {
|
|
310
271
|
let newText: string;
|
|
311
272
|
let count: number;
|
|
312
273
|
try {
|
|
313
274
|
({ text: newText, count } = rules.some((rule) => rule.regex === true)
|
|
314
|
-
? await
|
|
275
|
+
? await runRegexReplacements(
|
|
276
|
+
currentText,
|
|
277
|
+
rules,
|
|
278
|
+
config.replace.regexTimeoutMs,
|
|
279
|
+
target.signal,
|
|
280
|
+
)
|
|
315
281
|
: applyReplacements(currentText, rules));
|
|
316
282
|
} catch (error) {
|
|
317
283
|
throwIfCancelled(target.signal, `before apply; ${target.displayPath} was not changed.`);
|
package/src/pi/replace-worker.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parentPort, workerData } from "node:worker_threads";
|
|
2
|
-
import { applyReplacements } from "
|
|
3
|
-
import { errorMessage } from "
|
|
2
|
+
import { applyReplacements } from "../core/replace.ts";
|
|
3
|
+
import { errorMessage } from "../core/errors.ts";
|
|
4
4
|
|
|
5
5
|
try {
|
|
6
6
|
const { source, rules } = workerData;
|