@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
package/src/pi/text-stream.ts
CHANGED
|
@@ -35,6 +35,17 @@ export async function scanTextFile(
|
|
|
35
35
|
let lastByte = -1;
|
|
36
36
|
let hasNul = false;
|
|
37
37
|
let decodingError: unknown;
|
|
38
|
+
/** Strict text of a chunk, or of the final flush without bytes; undefined once malformed. */
|
|
39
|
+
const decodeStrict = (bytes?: Buffer): string | undefined => {
|
|
40
|
+
if (decodingError) return undefined;
|
|
41
|
+
try {
|
|
42
|
+
return decode(bytes, bytes !== undefined);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (!(error instanceof Utf8DecodingError)) throw error;
|
|
45
|
+
decodingError = error;
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
38
49
|
const stream = createReadStream(path, {
|
|
39
50
|
fd: handle,
|
|
40
51
|
autoClose: false,
|
|
@@ -55,15 +66,7 @@ export async function scanTextFile(
|
|
|
55
66
|
}
|
|
56
67
|
// NUL takes precedence even when an earlier chunk contained malformed UTF-8.
|
|
57
68
|
if (hasNul) continue;
|
|
58
|
-
let text
|
|
59
|
-
if (!decodingError) {
|
|
60
|
-
try {
|
|
61
|
-
text = decode(bytes, true);
|
|
62
|
-
} catch (error) {
|
|
63
|
-
if (!(error instanceof Utf8DecodingError)) throw error;
|
|
64
|
-
decodingError = error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
69
|
+
let text = decodeStrict(bytes);
|
|
67
70
|
if (lossy) text = lossy(bytes, true);
|
|
68
71
|
if (text !== undefined) await onChunk?.(text, byteLength);
|
|
69
72
|
}
|
|
@@ -74,22 +77,13 @@ export async function scanTextFile(
|
|
|
74
77
|
}
|
|
75
78
|
throwIfCancelled(signal);
|
|
76
79
|
if (!hasNul) {
|
|
77
|
-
let tail
|
|
78
|
-
if (!decodingError) {
|
|
79
|
-
try {
|
|
80
|
-
tail = decode();
|
|
81
|
-
} catch (error) {
|
|
82
|
-
if (!(error instanceof Utf8DecodingError)) throw error;
|
|
83
|
-
decodingError = error;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
80
|
+
let tail = decodeStrict();
|
|
86
81
|
if (lossy) tail = lossy();
|
|
87
82
|
else if (decodingError && decoding === "strict") throw decodingError;
|
|
88
83
|
if (tail) await onChunk?.(tail, byteLength);
|
|
89
84
|
}
|
|
90
85
|
return {
|
|
91
86
|
revision: revision.digest(),
|
|
92
|
-
byteLength,
|
|
93
87
|
totalLines: lineFeeds + (lastByte !== -1 && lastByte !== 10 ? 1 : 0),
|
|
94
88
|
finalNewline: lastByte === -1 || lastByte === 10,
|
|
95
89
|
hasNul,
|
|
@@ -117,7 +111,7 @@ export async function scanTextLines(
|
|
|
117
111
|
readonly decoding?: Utf8Decoding;
|
|
118
112
|
} = {},
|
|
119
113
|
) {
|
|
120
|
-
const maxBytes =
|
|
114
|
+
const { signal, decoding, maxLineBytes: maxBytes = Infinity } = options;
|
|
121
115
|
let number = 1;
|
|
122
116
|
let byteLength = 0;
|
|
123
117
|
let carriageReturns = 0;
|
|
@@ -144,7 +138,6 @@ export async function scanTextLines(
|
|
|
144
138
|
number++;
|
|
145
139
|
selected = select(number);
|
|
146
140
|
};
|
|
147
|
-
const { signal, decoding } = options;
|
|
148
141
|
const stats = await scanTextFile(path, {
|
|
149
142
|
signal,
|
|
150
143
|
decoding,
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { loadConfig, type HashlineEditConfig } from "./config.ts";
|
|
5
|
+
import { outputBudgetsFor, type OutputBudgetProvider } from "./budgets.ts";
|
|
6
|
+
import { makeReadOverride as read } from "./read-tool.ts";
|
|
7
|
+
import { makeGrepOverride as grep } from "./grep-tool.ts";
|
|
8
|
+
import { makeEditOverride as edit } from "./edit-tool.ts";
|
|
9
|
+
import { makeReplaceTool as replace } from "./replace-tool.ts";
|
|
10
|
+
import { makeWriteOverride as write } from "./write-tool.ts";
|
|
11
|
+
import type { MutationToolEnvironment } from "./mutation-runner.ts";
|
|
12
|
+
|
|
13
|
+
// The runner isolates global settings; create an empty project root for schema defaults.
|
|
14
|
+
const configRoot = mkdtempSync(join(tmpdir(), "hashline-default-config-"));
|
|
15
|
+
export const DEFAULT_CONFIG = (() => {
|
|
16
|
+
try {
|
|
17
|
+
return loadConfig(configRoot);
|
|
18
|
+
} finally {
|
|
19
|
+
rmSync(configRoot, { recursive: true, force: true });
|
|
20
|
+
}
|
|
21
|
+
})();
|
|
22
|
+
export const DEFAULT_OUTPUT_BUDGETS = outputBudgetsFor(undefined);
|
|
23
|
+
export const defaultOutputBudgets = () => outputBudgetsFor(undefined);
|
|
24
|
+
|
|
25
|
+
function toolEnvironment(
|
|
26
|
+
cwd: string,
|
|
27
|
+
config: HashlineEditConfig,
|
|
28
|
+
budgets: OutputBudgetProvider,
|
|
29
|
+
): { cwd: string; config: HashlineEditConfig; budgets: OutputBudgetProvider } {
|
|
30
|
+
return { cwd, config, budgets };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const makeReadOverride = (
|
|
34
|
+
cwd: string,
|
|
35
|
+
config: HashlineEditConfig,
|
|
36
|
+
budgets = defaultOutputBudgets,
|
|
37
|
+
) => read(toolEnvironment(cwd, config, budgets));
|
|
38
|
+
export const makeGrepOverride = (
|
|
39
|
+
cwd: string,
|
|
40
|
+
config: HashlineEditConfig,
|
|
41
|
+
budgets = defaultOutputBudgets,
|
|
42
|
+
) => grep(toolEnvironment(cwd, config, budgets));
|
|
43
|
+
export const makeEditOverride = (
|
|
44
|
+
cwd: string,
|
|
45
|
+
config: HashlineEditConfig,
|
|
46
|
+
fusion: MutationToolEnvironment["fusion"] = undefined,
|
|
47
|
+
budgets = defaultOutputBudgets,
|
|
48
|
+
) => edit({ ...toolEnvironment(cwd, config, budgets), fusion });
|
|
49
|
+
export const makeReplaceTool = (
|
|
50
|
+
cwd: string,
|
|
51
|
+
config: HashlineEditConfig,
|
|
52
|
+
fusion: MutationToolEnvironment["fusion"] = undefined,
|
|
53
|
+
budgets = defaultOutputBudgets,
|
|
54
|
+
) => replace({ ...toolEnvironment(cwd, config, budgets), fusion });
|
|
55
|
+
export const makeWriteOverride = (
|
|
56
|
+
cwd: string,
|
|
57
|
+
fusion: MutationToolEnvironment["fusion"] = undefined,
|
|
58
|
+
budgets = defaultOutputBudgets,
|
|
59
|
+
) => write({ ...toolEnvironment(cwd, DEFAULT_CONFIG, budgets), fusion });
|
package/src/pi/write-tool.ts
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
1
|
import { Type, type Static } from "typebox";
|
|
2
|
-
import type { AgentToolResult
|
|
2
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
3
3
|
import { Text } from "@earendil-works/pi-tui";
|
|
4
4
|
import {
|
|
5
5
|
createWriteToolDefinition,
|
|
6
|
-
type ExtensionToolContext,
|
|
7
6
|
type Theme,
|
|
8
7
|
type ToolRenderResultOptions,
|
|
9
8
|
} from "@earendil-works/pi-coding-agent";
|
|
10
|
-
import {
|
|
11
|
-
ACTION_FUSION_GUIDELINES,
|
|
12
|
-
withThenRunDescription,
|
|
13
|
-
withThenRunSchema,
|
|
14
|
-
type ThenRunInput,
|
|
15
|
-
} from "./action-fusion.ts";
|
|
16
9
|
import { commitFile } from "./file-commit.ts";
|
|
17
|
-
import {
|
|
10
|
+
import { mutationToolBase, type MutationToolEnvironment } from "./mutation-runner.ts";
|
|
18
11
|
import { throwIfCancelled } from "./error-text.ts";
|
|
19
|
-
import { createArgumentPreparer } from "./argument-validation.ts";
|
|
20
|
-
import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
|
|
21
12
|
import { unwritableTextError } from "../core/text.ts";
|
|
22
|
-
import { reportLines, renderToolError, splitReport } from "./render.ts";
|
|
23
|
-
import type {
|
|
13
|
+
import { MUTATION_CARD_SHOWN, reportLines, renderToolError, splitReport } from "./render.ts";
|
|
14
|
+
import type { MutationOutcome } from "./mutation-result.ts";
|
|
24
15
|
import type { ReportDetails } from "./report.ts";
|
|
16
|
+
import { filePathField } from "./schema.ts";
|
|
25
17
|
|
|
26
18
|
const writeSchema = Type.Object(
|
|
27
19
|
{
|
|
28
|
-
path:
|
|
20
|
+
path: filePathField(),
|
|
29
21
|
content: Type.String({
|
|
30
22
|
description: "Complete file content, written exactly as supplied, including line endings.",
|
|
31
23
|
}),
|
|
@@ -37,44 +29,46 @@ const writeSchema = Type.Object(
|
|
|
37
29
|
{ additionalProperties: false },
|
|
38
30
|
);
|
|
39
31
|
|
|
40
|
-
type WriteParams = Static<typeof writeSchema
|
|
32
|
+
type WriteParams = Static<typeof writeSchema>;
|
|
41
33
|
/** write shows the built-in card; its report states every fact. */
|
|
42
34
|
type WriteMutationDetails = object;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const WRITE_CARD_SHOWN = new Set(["publication", "created", "then_run", "progressError"]);
|
|
35
|
+
/** The write card's call line shows the content. */
|
|
36
|
+
const WRITE_CARD_SHOWN = new Set([...MUTATION_CARD_SHOWN, "publication"]);
|
|
46
37
|
type WriteRenderContext = Parameters<
|
|
47
38
|
NonNullable<ReturnType<typeof createWriteToolDefinition>["renderCall"]>
|
|
48
39
|
>[2];
|
|
49
40
|
|
|
50
|
-
export function makeWriteOverride(
|
|
51
|
-
cwd
|
|
52
|
-
fusion?: ActionFusionExecutor,
|
|
53
|
-
budgets: OutputBudgetProvider = defaultOutputBudgets,
|
|
54
|
-
) {
|
|
55
|
-
const parameters = withThenRunSchema(writeSchema, "write", fusion !== undefined);
|
|
56
|
-
const builtin = createWriteToolDefinition(cwd);
|
|
41
|
+
export function makeWriteOverride(env: MutationToolEnvironment) {
|
|
42
|
+
const builtin = createWriteToolDefinition(env.cwd);
|
|
57
43
|
return {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
parameters,
|
|
67
|
-
prepareArguments: createArgumentPreparer(
|
|
68
|
-
"write",
|
|
69
|
-
parameters,
|
|
70
|
-
(args, report) => {
|
|
44
|
+
...mutationToolBase({
|
|
45
|
+
...env,
|
|
46
|
+
tool: "write",
|
|
47
|
+
schema: writeSchema,
|
|
48
|
+
description:
|
|
49
|
+
"Write a whole file exactly as supplied, including its line endings. Do not use write for partial changes to an existing file; use edit or replace. Choose create for a new file or overwrite for an existing file. Creates missing parent directories.",
|
|
50
|
+
promptSnippet: "Write complete file content to a path",
|
|
51
|
+
check: (args, report) => {
|
|
71
52
|
const content = (args as { content?: unknown } | null)?.content;
|
|
72
53
|
const unwritable = typeof content === "string" ? unwritableTextError(content) : undefined;
|
|
73
54
|
if (unwritable) report("content", unwritable.message);
|
|
74
55
|
},
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
56
|
+
async run(
|
|
57
|
+
params,
|
|
58
|
+
{ absolutePath, signal },
|
|
59
|
+
): Promise<MutationOutcome<WriteMutationDetails, "write">> {
|
|
60
|
+
throwIfCancelled(signal);
|
|
61
|
+
const commit = await commitFile(absolutePath, params.content, {
|
|
62
|
+
mode: params.mode,
|
|
63
|
+
signal,
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
commit,
|
|
67
|
+
details: {},
|
|
68
|
+
facts: {},
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
}),
|
|
78
72
|
renderCall(args: WriteParams, theme: Theme, context: WriteRenderContext) {
|
|
79
73
|
return builtin.renderCall!(args, theme, context);
|
|
80
74
|
},
|
|
@@ -85,39 +79,10 @@ export function makeWriteOverride(
|
|
|
85
79
|
context: WriteRenderContext,
|
|
86
80
|
) {
|
|
87
81
|
if (context.isError) return renderToolError(result, "write", theme, options.expanded);
|
|
88
|
-
const { report } = splitReport(result);
|
|
82
|
+
const { report } = splitReport(result, "write");
|
|
89
83
|
const facts = report ? reportLines(report, theme, WRITE_CARD_SHOWN) : [];
|
|
90
84
|
if (facts.length) return new Text(facts.join("\n"), 0, 0);
|
|
91
85
|
return builtin.renderResult!({ ...result, details: undefined }, options, theme, context);
|
|
92
86
|
},
|
|
93
|
-
async execute(
|
|
94
|
-
toolCallId: string,
|
|
95
|
-
params: WriteParams,
|
|
96
|
-
signal: AbortSignal | undefined,
|
|
97
|
-
onUpdate: AgentToolUpdateCallback<WriteDetails> | undefined,
|
|
98
|
-
ctx: ExtensionToolContext,
|
|
99
|
-
) {
|
|
100
|
-
return executeMutation<WriteParams, WriteMutationDetails, "write">(
|
|
101
|
-
{
|
|
102
|
-
tool: "write",
|
|
103
|
-
cwd,
|
|
104
|
-
fusion,
|
|
105
|
-
budgets,
|
|
106
|
-
async run(mutationParams, { absolutePath, signal }) {
|
|
107
|
-
throwIfCancelled(signal);
|
|
108
|
-
const commit = await commitFile(absolutePath, mutationParams.content, {
|
|
109
|
-
mode: mutationParams.mode,
|
|
110
|
-
signal,
|
|
111
|
-
});
|
|
112
|
-
return {
|
|
113
|
-
commit,
|
|
114
|
-
details: {},
|
|
115
|
-
facts: commit.created ? { created: true } : {},
|
|
116
|
-
};
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
{ toolCallId, params, signal, onUpdate, ctx },
|
|
120
|
-
);
|
|
121
|
-
},
|
|
122
87
|
};
|
|
123
88
|
}
|
package/src/pi/write.test.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
import { commandCompletion, type SessionCommand } from "./action-fusion.testing.ts";
|
|
1
2
|
import { rejectsArgument } from "./argument-error.testing.ts";
|
|
2
3
|
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
4
|
import { tmpdir } from "node:os";
|
|
4
5
|
import { join } from "node:path";
|
|
5
6
|
import assert from "node:assert/strict";
|
|
6
7
|
import { test } from "node:test";
|
|
7
|
-
import { makeWriteOverride } from "./
|
|
8
|
+
import { makeWriteOverride } from "./tool-factories.testing.ts";
|
|
8
9
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
9
10
|
import { callTool } from "./tool-call.testing.ts";
|
|
10
11
|
import { reportOf } from "./report.ts";
|
|
11
|
-
import { REREAD } from "./report-schema.ts";
|
|
12
12
|
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
13
13
|
import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
|
|
14
14
|
initTheme("dark");
|
|
@@ -28,7 +28,7 @@ test("write receives an unknown field → rejects without changing the file", as
|
|
|
28
28
|
withTemp(async (dir) => {
|
|
29
29
|
const file = join(dir, "file.txt");
|
|
30
30
|
await writeFile(file, "original\n");
|
|
31
|
-
for (const fusion of [undefined, createActionFusionExecutor()]) {
|
|
31
|
+
for (const fusion of [undefined, createActionFusionExecutor(() => {})]) {
|
|
32
32
|
const tool = makeWriteOverride(dir, fusion);
|
|
33
33
|
const args = { path: "file.txt", content: "new\n", mode: "overwrite", unexpected: true };
|
|
34
34
|
await assert.rejects(
|
|
@@ -43,8 +43,6 @@ test("write rejects unwritable content before accessing a missing target", async
|
|
|
43
43
|
withTemp(async (dir) => {
|
|
44
44
|
const file = join(dir, "missing.txt");
|
|
45
45
|
const tool = makeWriteOverride(dir);
|
|
46
|
-
const valid = { path: file, content: "ready\n", mode: "create" };
|
|
47
|
-
assert.equal(tool.prepareArguments(valid), valid);
|
|
48
46
|
for (const [content, expected] of [
|
|
49
47
|
["bad\0", rejectsArgument("content", /NUL/)],
|
|
50
48
|
["\ud800", rejectsArgument("content", /encoded losslessly/)],
|
|
@@ -64,10 +62,11 @@ test("write omits mode → rejects before changing files or running commands", a
|
|
|
64
62
|
const missing = join(missingParent, "new.txt");
|
|
65
63
|
await writeFile(existing, "original\n");
|
|
66
64
|
let commandRuns = 0;
|
|
67
|
-
const
|
|
65
|
+
const executeTool: SessionCommand = async () => {
|
|
68
66
|
commandRuns++;
|
|
69
|
-
return {
|
|
70
|
-
}
|
|
67
|
+
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
68
|
+
};
|
|
69
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
71
70
|
for (const executor of [undefined, fusion]) {
|
|
72
71
|
const tool = makeWriteOverride(dir, executor);
|
|
73
72
|
for (const path of [existing, missing]) {
|
|
@@ -75,7 +74,7 @@ test("write omits mode → rejects before changing files or running commands", a
|
|
|
75
74
|
callTool(
|
|
76
75
|
tool,
|
|
77
76
|
{ path, content: "changed\n", ...(executor ? { then_run: { command: "check" } } : {}) },
|
|
78
|
-
{ ctx: context(dir) },
|
|
77
|
+
{ ctx: { ...context(dir), executeTool } },
|
|
79
78
|
),
|
|
80
79
|
rejectsArgument("mode", /required/),
|
|
81
80
|
);
|
|
@@ -100,7 +99,6 @@ test("write supports create-only and overwrite-only modes", async () =>
|
|
|
100
99
|
assert.doesNotMatch(created.content[0].text, /Revision:|[0-9a-f]{64}/);
|
|
101
100
|
assert.deepEqual(await readFile(target), Buffer.from("new\n"));
|
|
102
101
|
assert.equal(reportOf(created, "write")?.publication, "PUBLISHED");
|
|
103
|
-
assert.equal(reportOf(created, "write")?.created, true);
|
|
104
102
|
const exists = await callTool(
|
|
105
103
|
write,
|
|
106
104
|
{ path: "new.txt", content: "bad\n", mode: "create" },
|
|
@@ -142,23 +140,21 @@ test("write returns only a summary for empty, short, and long content", async ()
|
|
|
142
140
|
undefined,
|
|
143
141
|
context(dir),
|
|
144
142
|
);
|
|
145
|
-
assert.equal(reportOf(result, "write")?.created, true);
|
|
146
143
|
assert.equal(reportOf(result, "write")?.path, path);
|
|
147
144
|
assert.equal(reportOf(result, "write")?.publication, "PUBLISHED");
|
|
148
145
|
assert.equal(result.content.length, 1);
|
|
149
146
|
const block = result.content[0];
|
|
150
147
|
assert.equal(block.type, "text");
|
|
151
|
-
assert.deepEqual(JSON.parse(block.text), { publication: "PUBLISHED"
|
|
148
|
+
assert.deepEqual(JSON.parse(block.text), { publication: "PUBLISHED" });
|
|
152
149
|
assert.equal(await readFile(join(dir, path), "utf8"), content);
|
|
153
150
|
}
|
|
154
151
|
}));
|
|
155
152
|
|
|
156
153
|
test("write returns its summary and command output after an unchanged then_run", async () =>
|
|
157
154
|
withTemp(async (dir) => {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}));
|
|
155
|
+
const executeTool: SessionCommand = async () =>
|
|
156
|
+
commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
157
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
162
158
|
const write = makeWriteOverride(dir, fusion) as any;
|
|
163
159
|
const result = await write.execute(
|
|
164
160
|
"unchanged",
|
|
@@ -170,9 +166,8 @@ test("write returns its summary and command output after an unchanged then_run",
|
|
|
170
166
|
},
|
|
171
167
|
undefined,
|
|
172
168
|
undefined,
|
|
173
|
-
context(dir),
|
|
169
|
+
{ ...context(dir), executeTool },
|
|
174
170
|
);
|
|
175
|
-
assert.equal(reportOf(result, "write")?.created, true);
|
|
176
171
|
assert.deepEqual(reportOf(result, "write")?.then_run, {
|
|
177
172
|
status: "succeeded",
|
|
178
173
|
output: "checked",
|
|
@@ -182,10 +177,11 @@ test("write returns its summary and command output after an unchanged then_run",
|
|
|
182
177
|
test("write reports changed freshness when then_run changes the target", async () =>
|
|
183
178
|
withTemp(async (dir) => {
|
|
184
179
|
const target = join(dir, "changed.txt");
|
|
185
|
-
const
|
|
180
|
+
const executeTool: SessionCommand = async () => {
|
|
186
181
|
await writeFile(target, "command changed\n");
|
|
187
|
-
return {
|
|
188
|
-
}
|
|
182
|
+
return commandCompletion({ content: [{ type: "text", text: "checked" }] });
|
|
183
|
+
};
|
|
184
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
189
185
|
const write = makeWriteOverride(dir, fusion) as any;
|
|
190
186
|
const result = await callTool(
|
|
191
187
|
write,
|
|
@@ -195,13 +191,12 @@ test("write reports changed freshness when then_run changes the target", async (
|
|
|
195
191
|
mode: "create",
|
|
196
192
|
then_run: { command: "check" },
|
|
197
193
|
},
|
|
198
|
-
{ toolCallId: "changed", ctx: context(dir) },
|
|
194
|
+
{ toolCallId: "changed", ctx: { ...context(dir), executeTool } },
|
|
199
195
|
);
|
|
200
196
|
const text = result.content.map((block: any) => block.text).join("\n");
|
|
201
197
|
assert.doesNotMatch(text, /revision:|[0-9a-f]{64}/i);
|
|
202
|
-
assert.equal(reportOf(result, "write")?.created, true);
|
|
203
198
|
assert.equal(reportOf(result, "write")?.freshness, "changed");
|
|
204
|
-
assert.
|
|
199
|
+
assert.match(reportOf(result, "write")?.next ?? "", /Read the file before further edits/);
|
|
205
200
|
assert.deepEqual(reportOf(result, "write")?.then_run, {
|
|
206
201
|
status: "succeeded",
|
|
207
202
|
output: "checked",
|
|
@@ -219,10 +214,11 @@ test("write reports changed freshness when then_run changes the target", async (
|
|
|
219
214
|
test("write reports missing freshness when then_run removes the target", async () =>
|
|
220
215
|
withTemp(async (dir) => {
|
|
221
216
|
const target = join(dir, "missing.txt");
|
|
222
|
-
const
|
|
217
|
+
const executeTool: SessionCommand = async () => {
|
|
223
218
|
await rm(target);
|
|
224
|
-
return {
|
|
225
|
-
}
|
|
219
|
+
return commandCompletion({ content: [{ type: "text", text: "removed" }] });
|
|
220
|
+
};
|
|
221
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
226
222
|
const result = await makeWriteOverride(dir, fusion).execute(
|
|
227
223
|
"missing",
|
|
228
224
|
{
|
|
@@ -233,27 +229,28 @@ test("write reports missing freshness when then_run removes the target", async (
|
|
|
233
229
|
},
|
|
234
230
|
undefined,
|
|
235
231
|
undefined,
|
|
236
|
-
context(dir),
|
|
232
|
+
{ ...context(dir), executeTool },
|
|
237
233
|
);
|
|
238
234
|
assert.equal(reportOf(result, "write")?.freshness, "missing");
|
|
239
|
-
assert.equal(reportOf(result, "write")?.next,
|
|
235
|
+
assert.equal(reportOf(result, "write")?.next, undefined);
|
|
240
236
|
}));
|
|
241
237
|
|
|
242
238
|
test("write preserves command failure and changed freshness", async () =>
|
|
243
239
|
withTemp(async (dir) => {
|
|
244
240
|
const target = join(dir, "failed.txt");
|
|
245
|
-
const
|
|
241
|
+
const executeTool: SessionCommand = async () => {
|
|
246
242
|
await writeFile(target, "changed before failure\n");
|
|
247
|
-
|
|
248
|
-
}
|
|
243
|
+
return commandCompletion({ content: [{ type: "text", text: "command failed" }] }, true);
|
|
244
|
+
};
|
|
245
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
249
246
|
const result = await makeWriteOverride(dir, fusion).execute(
|
|
250
247
|
"failed",
|
|
251
248
|
{ path: "failed.txt", content: "mutation\n", mode: "create", then_run: { command: "fail" } },
|
|
252
249
|
undefined,
|
|
253
250
|
undefined,
|
|
254
|
-
context(dir),
|
|
251
|
+
{ ...context(dir), executeTool },
|
|
255
252
|
);
|
|
256
253
|
assert.equal(reportOf(result, "write")?.freshness, "changed");
|
|
257
254
|
assert.equal(reportOf(result, "write")?.then_run?.status, "failed");
|
|
258
|
-
assert.
|
|
255
|
+
assert.match(reportOf(result, "write")?.next ?? "", /Read the file before further edits/);
|
|
259
256
|
}));
|
|
@@ -29,7 +29,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
29
29
|
flags: "",
|
|
30
30
|
hits: ["b", "z"],
|
|
31
31
|
misses: ["a", "A"],
|
|
32
|
-
rejectCandidates:
|
|
32
|
+
rejectCandidates: false,
|
|
33
33
|
source: local,
|
|
34
34
|
},
|
|
35
35
|
{
|
|
@@ -37,7 +37,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
37
37
|
flags: "",
|
|
38
38
|
hits: ["ab"],
|
|
39
39
|
misses: ["ac", "ad"],
|
|
40
|
-
rejectCandidates:
|
|
40
|
+
rejectCandidates: false,
|
|
41
41
|
source: local,
|
|
42
42
|
},
|
|
43
43
|
{
|
|
@@ -45,7 +45,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
45
45
|
flags: "",
|
|
46
46
|
hits: ["b"],
|
|
47
47
|
misses: ["a", "c"],
|
|
48
|
-
rejectCandidates:
|
|
48
|
+
rejectCandidates: false,
|
|
49
49
|
source: local,
|
|
50
50
|
},
|
|
51
51
|
{
|
|
@@ -53,7 +53,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
53
53
|
flags: "",
|
|
54
54
|
hits: ["ac", "ba"],
|
|
55
55
|
misses: ["ab"],
|
|
56
|
-
rejectCandidates:
|
|
56
|
+
rejectCandidates: false,
|
|
57
57
|
source: local,
|
|
58
58
|
},
|
|
59
59
|
{
|
|
@@ -61,7 +61,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
61
61
|
flags: "",
|
|
62
62
|
hits: ["ac", "ba"],
|
|
63
63
|
misses: ["ab"],
|
|
64
|
-
rejectCandidates:
|
|
64
|
+
rejectCandidates: false,
|
|
65
65
|
source: local,
|
|
66
66
|
},
|
|
67
67
|
{
|
|
@@ -69,7 +69,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
69
69
|
flags: "u",
|
|
70
70
|
hits: ["a", "ſ"],
|
|
71
71
|
misses: ["k", "K", "K"],
|
|
72
|
-
rejectCandidates:
|
|
72
|
+
rejectCandidates: false,
|
|
73
73
|
source: local,
|
|
74
74
|
},
|
|
75
75
|
{
|
|
@@ -85,7 +85,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
85
85
|
flags: "iu",
|
|
86
86
|
hits: ["k ", "K "],
|
|
87
87
|
misses: ["kk", "Kk"],
|
|
88
|
-
rejectCandidates:
|
|
88
|
+
rejectCandidates: false,
|
|
89
89
|
source: local,
|
|
90
90
|
},
|
|
91
91
|
{
|
|
@@ -93,7 +93,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
93
93
|
flags: "",
|
|
94
94
|
hits: ["\rb"],
|
|
95
95
|
misses: ["ab"],
|
|
96
|
-
rejectCandidates:
|
|
96
|
+
rejectCandidates: false,
|
|
97
97
|
source: local,
|
|
98
98
|
},
|
|
99
99
|
{
|
|
@@ -141,7 +141,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
141
141
|
flags: "",
|
|
142
142
|
hits: ["catcat", "dogdog"],
|
|
143
143
|
misses: ["catdog", "dogcat"],
|
|
144
|
-
rejectCandidates:
|
|
144
|
+
rejectCandidates: false,
|
|
145
145
|
source: local,
|
|
146
146
|
},
|
|
147
147
|
{
|
|
@@ -149,7 +149,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
149
149
|
flags: "",
|
|
150
150
|
hits: ["aa", "bcbc"],
|
|
151
151
|
misses: ["abc", "bca"],
|
|
152
|
-
rejectCandidates:
|
|
152
|
+
rejectCandidates: false,
|
|
153
153
|
source: local,
|
|
154
154
|
},
|
|
155
155
|
{
|
|
@@ -165,7 +165,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
165
165
|
flags: "v",
|
|
166
166
|
hits: ["KsKs", "abab"],
|
|
167
167
|
misses: ["Ksks", "abAB"],
|
|
168
|
-
rejectCandidates:
|
|
168
|
+
rejectCandidates: false,
|
|
169
169
|
source: local,
|
|
170
170
|
},
|
|
171
171
|
{
|
|
@@ -214,7 +214,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
|
|
|
214
214
|
flags: "v",
|
|
215
215
|
hits: [`prefix${"a".repeat(300)}suffix`, `prefix${"b".repeat(300)}suffix`],
|
|
216
216
|
misses: ["wrong", "prefixwrong", "wrongsuffix"],
|
|
217
|
-
rejectCandidates:
|
|
217
|
+
rejectCandidates: false,
|
|
218
218
|
source: local,
|
|
219
219
|
},
|
|
220
220
|
{
|
package/src/testing/run-tests.ts
CHANGED
|
@@ -31,7 +31,7 @@ const agentDir = mkdtempSync(join(tmpdir(), "hashline-test-agent-"));
|
|
|
31
31
|
try {
|
|
32
32
|
const result = spawnSync(
|
|
33
33
|
process.execPath,
|
|
34
|
-
["--test", filter, ...process.argv.slice(3), ...files],
|
|
34
|
+
["--experimental-test-module-mocks", "--test", filter, ...process.argv.slice(3), ...files],
|
|
35
35
|
{
|
|
36
36
|
cwd: fileURLToPath(new URL("../../", import.meta.url)),
|
|
37
37
|
env: testEnvironment(agentDir),
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/** Replacement worker request and reply decoder; shared worker errors are checked by the host. */
|
|
2
|
-
import { workerReplyDecoder } from "./regex-worker-contract.ts";
|
|
3
|
-
import { REPLACEMENT_RESULT_SCHEMA, type Replacement } from "../core/replace-schema.ts";
|
|
4
|
-
|
|
5
|
-
export interface ReplacementRequest {
|
|
6
|
-
readonly source: string;
|
|
7
|
-
readonly rules: readonly Replacement[];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const decodeWorkerReply = workerReplyDecoder(REPLACEMENT_RESULT_SCHEMA);
|