@criogaid/pi-hashline-edit 0.10.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.
Files changed (94) hide show
  1. package/README.md +4 -3
  2. package/README.zh-CN.md +4 -3
  3. package/package.json +1 -1
  4. package/src/core/apply.test.ts +2 -2
  5. package/src/core/apply.ts +2 -2
  6. package/src/core/character-set.test.ts +2 -3
  7. package/src/core/character-set.ts +1 -1
  8. package/src/core/ranges.test.ts +2 -1
  9. package/src/core/ranges.ts +1 -1
  10. package/src/core/types.ts +1 -1
  11. package/src/index.ts +8 -8
  12. package/src/integration/config-registration.test.ts +8 -10
  13. package/src/integration/fusion-dispatch.test.ts +2 -2
  14. package/src/integration/grep-errors.test.ts +3 -3
  15. package/src/integration/grep-flags.test.ts +5 -5
  16. package/src/integration/grep-js-completeness.test.ts +12 -11
  17. package/src/integration/grep-line-candidates.test.ts +3 -2
  18. package/src/integration/grep-matrix.test.ts +5 -4
  19. package/src/integration/grep-order.test.ts +4 -4
  20. package/src/integration/grep-path-matrix.test.ts +5 -4
  21. package/src/integration/grep-prefilter-coverage.test.ts +4 -3
  22. package/src/integration/grep-process-limits.test.ts +2 -4
  23. package/src/integration/grep-regex.test.ts +10 -10
  24. package/src/integration/grep-rg.test.ts +50 -48
  25. package/src/integration/grep-scan-budget.test.ts +8 -8
  26. package/src/integration/grep-translation.test.ts +8 -7
  27. package/src/integration/large-text.test.ts +5 -5
  28. package/src/integration/model-budget-lifecycle.test.ts +9 -9
  29. package/src/integration/mutation-file-types.testing.ts +4 -4
  30. package/src/integration/npm-package.testing.ts +3 -3
  31. package/src/integration/publication-read-changes.test.ts +54 -54
  32. package/src/integration/read-changes.test.ts +4 -4
  33. package/src/integration/replace-worker-boundary.test.ts +1 -1
  34. package/src/integration/report-replay.test.ts +5 -6
  35. package/src/integration/utf8-errors.test.ts +4 -4
  36. package/src/pi/AGENTS.md +6 -5
  37. package/src/pi/action-fusion.test.ts +125 -105
  38. package/src/pi/action-fusion.testing.ts +38 -0
  39. package/src/pi/action-fusion.ts +9 -21
  40. package/src/pi/argument-diagnostics.ts +4 -3
  41. package/src/pi/argument-format.test.ts +19 -18
  42. package/src/pi/argument-validation.test.ts +218 -139
  43. package/src/pi/budgets.ts +1 -4
  44. package/src/pi/codemode.test.ts +7 -2
  45. package/src/pi/config.test.ts +2 -1
  46. package/src/pi/config.ts +8 -3
  47. package/src/pi/diagnostic-buffer.test.ts +23 -13
  48. package/src/pi/edit-tool.ts +47 -62
  49. package/src/pi/error-text.ts +7 -2
  50. package/src/pi/execute.test.ts +174 -157
  51. package/src/pi/failure-context.test.ts +4 -3
  52. package/src/pi/failure-context.ts +1 -1
  53. package/src/pi/failure-path.test.ts +37 -37
  54. package/src/pi/file-commit.test.ts +47 -19
  55. package/src/pi/file-commit.ts +36 -9
  56. package/src/pi/fusion-card.test.ts +22 -21
  57. package/src/pi/grep-candidates.test.ts +18 -20
  58. package/src/pi/grep-candidates.ts +8 -13
  59. package/src/pi/grep-matrix.test.ts +3 -3
  60. package/src/pi/grep-order.test.ts +12 -11
  61. package/src/pi/grep-output.ts +36 -15
  62. package/src/pi/grep-reread.ts +7 -2
  63. package/src/pi/grep-rg.testing.ts +70 -0
  64. package/src/pi/grep-scope.ts +4 -4
  65. package/src/pi/grep-search.ts +8 -11
  66. package/src/pi/grep-tool.ts +23 -39
  67. package/src/pi/grep.test.ts +102 -116
  68. package/src/pi/model-budgets.test.ts +2 -1
  69. package/src/pi/mutation-outcome.testing.ts +0 -1
  70. package/src/pi/mutation-result.test.ts +8 -8
  71. package/src/pi/mutation-result.ts +1 -1
  72. package/src/pi/mutation-runner.ts +9 -5
  73. package/src/pi/output-budgets.test.ts +53 -31
  74. package/src/pi/pi.test.ts +18 -13
  75. package/src/pi/read-tool.ts +13 -18
  76. package/src/pi/read.test.ts +22 -41
  77. package/src/pi/registration.test.ts +25 -19
  78. package/src/pi/render.test.ts +51 -32
  79. package/src/pi/render.ts +7 -4
  80. package/src/pi/replace-regex.ts +1 -1
  81. package/src/pi/replace-tool.ts +6 -16
  82. package/src/pi/replace.test.ts +45 -34
  83. package/src/pi/report-failure.testing.ts +9 -1
  84. package/src/pi/report-schema.ts +205 -83
  85. package/src/pi/report.test.ts +66 -64
  86. package/src/pi/report.ts +23 -17
  87. package/src/pi/result-safety.test.ts +121 -100
  88. package/src/pi/rg-process.test.ts +2 -2
  89. package/src/pi/rg-process.ts +9 -15
  90. package/src/pi/schema.ts +23 -0
  91. package/src/pi/tool-factories.testing.ts +59 -0
  92. package/src/pi/write-tool.ts +9 -15
  93. package/src/pi/write.test.ts +30 -31
  94. package/src/testing/run-tests.ts +1 -1
@@ -7,17 +7,17 @@ import {
7
7
  type ToolRenderResultOptions,
8
8
  } from "@earendil-works/pi-coding-agent";
9
9
  import { commitFile } from "./file-commit.ts";
10
- import { mutationToolBase, type ActionFusionExecutor } from "./mutation-runner.ts";
10
+ import { mutationToolBase, type MutationToolEnvironment } from "./mutation-runner.ts";
11
11
  import { throwIfCancelled } from "./error-text.ts";
12
- import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
13
12
  import { unwritableTextError } from "../core/text.ts";
14
13
  import { MUTATION_CARD_SHOWN, reportLines, renderToolError, splitReport } from "./render.ts";
15
14
  import type { MutationOutcome } from "./mutation-result.ts";
16
15
  import type { ReportDetails } from "./report.ts";
16
+ import { filePathField } from "./schema.ts";
17
17
 
18
18
  const writeSchema = Type.Object(
19
19
  {
20
- path: Type.String({ minLength: 1, description: "Path to the file (relative or absolute)" }),
20
+ path: filePathField(),
21
21
  content: Type.String({
22
22
  description: "Complete file content, written exactly as supplied, including line endings.",
23
23
  }),
@@ -33,23 +33,17 @@ type WriteParams = Static<typeof writeSchema>;
33
33
  /** write shows the built-in card; its report states every fact. */
34
34
  type WriteMutationDetails = object;
35
35
  /** The write card's call line shows the content. */
36
- const WRITE_CARD_SHOWN = new Set([...MUTATION_CARD_SHOWN, "publication", "created"]);
36
+ const WRITE_CARD_SHOWN = new Set([...MUTATION_CARD_SHOWN, "publication"]);
37
37
  type WriteRenderContext = Parameters<
38
38
  NonNullable<ReturnType<typeof createWriteToolDefinition>["renderCall"]>
39
39
  >[2];
40
40
 
41
- export function makeWriteOverride(
42
- cwd: string,
43
- fusion?: ActionFusionExecutor,
44
- budgets: OutputBudgetProvider = defaultOutputBudgets,
45
- ) {
46
- const builtin = createWriteToolDefinition(cwd);
41
+ export function makeWriteOverride(env: MutationToolEnvironment) {
42
+ const builtin = createWriteToolDefinition(env.cwd);
47
43
  return {
48
44
  ...mutationToolBase({
45
+ ...env,
49
46
  tool: "write",
50
- cwd,
51
- fusion,
52
- budgets,
53
47
  schema: writeSchema,
54
48
  description:
55
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.",
@@ -71,7 +65,7 @@ export function makeWriteOverride(
71
65
  return {
72
66
  commit,
73
67
  details: {},
74
- facts: commit.created ? { created: true } : {},
68
+ facts: {},
75
69
  };
76
70
  },
77
71
  }),
@@ -85,7 +79,7 @@ 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);
@@ -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 "./write-tool.ts";
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(
@@ -62,10 +62,11 @@ test("write omits mode → rejects before changing files or running commands", a
62
62
  const missing = join(missingParent, "new.txt");
63
63
  await writeFile(existing, "original\n");
64
64
  let commandRuns = 0;
65
- const fusion = createActionFusionExecutor(async () => {
65
+ const executeTool: SessionCommand = async () => {
66
66
  commandRuns++;
67
- return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
68
- });
67
+ return commandCompletion({ content: [{ type: "text", text: "checked" }] });
68
+ };
69
+ const fusion = createActionFusionExecutor(() => {});
69
70
  for (const executor of [undefined, fusion]) {
70
71
  const tool = makeWriteOverride(dir, executor);
71
72
  for (const path of [existing, missing]) {
@@ -73,7 +74,7 @@ test("write omits mode → rejects before changing files or running commands", a
73
74
  callTool(
74
75
  tool,
75
76
  { path, content: "changed\n", ...(executor ? { then_run: { command: "check" } } : {}) },
76
- { ctx: context(dir) },
77
+ { ctx: { ...context(dir), executeTool } },
77
78
  ),
78
79
  rejectsArgument("mode", /required/),
79
80
  );
@@ -98,7 +99,6 @@ test("write supports create-only and overwrite-only modes", async () =>
98
99
  assert.doesNotMatch(created.content[0].text, /Revision:|[0-9a-f]{64}/);
99
100
  assert.deepEqual(await readFile(target), Buffer.from("new\n"));
100
101
  assert.equal(reportOf(created, "write")?.publication, "PUBLISHED");
101
- assert.equal(reportOf(created, "write")?.created, true);
102
102
  const exists = await callTool(
103
103
  write,
104
104
  { path: "new.txt", content: "bad\n", mode: "create" },
@@ -140,23 +140,21 @@ test("write returns only a summary for empty, short, and long content", async ()
140
140
  undefined,
141
141
  context(dir),
142
142
  );
143
- assert.equal(reportOf(result, "write")?.created, true);
144
143
  assert.equal(reportOf(result, "write")?.path, path);
145
144
  assert.equal(reportOf(result, "write")?.publication, "PUBLISHED");
146
145
  assert.equal(result.content.length, 1);
147
146
  const block = result.content[0];
148
147
  assert.equal(block.type, "text");
149
- assert.deepEqual(JSON.parse(block.text), { publication: "PUBLISHED", created: true });
148
+ assert.deepEqual(JSON.parse(block.text), { publication: "PUBLISHED" });
150
149
  assert.equal(await readFile(join(dir, path), "utf8"), content);
151
150
  }
152
151
  }));
153
152
 
154
153
  test("write returns its summary and command output after an unchanged then_run", async () =>
155
154
  withTemp(async (dir) => {
156
- const fusion = createActionFusionExecutor(async () => ({
157
- status: "succeeded",
158
- result: { content: [{ type: "text", text: "checked" }] },
159
- }));
155
+ const executeTool: SessionCommand = async () =>
156
+ commandCompletion({ content: [{ type: "text", text: "checked" }] });
157
+ const fusion = createActionFusionExecutor(() => {});
160
158
  const write = makeWriteOverride(dir, fusion) as any;
161
159
  const result = await write.execute(
162
160
  "unchanged",
@@ -168,9 +166,8 @@ test("write returns its summary and command output after an unchanged then_run",
168
166
  },
169
167
  undefined,
170
168
  undefined,
171
- context(dir),
169
+ { ...context(dir), executeTool },
172
170
  );
173
- assert.equal(reportOf(result, "write")?.created, true);
174
171
  assert.deepEqual(reportOf(result, "write")?.then_run, {
175
172
  status: "succeeded",
176
173
  output: "checked",
@@ -180,10 +177,11 @@ test("write returns its summary and command output after an unchanged then_run",
180
177
  test("write reports changed freshness when then_run changes the target", async () =>
181
178
  withTemp(async (dir) => {
182
179
  const target = join(dir, "changed.txt");
183
- const fusion = createActionFusionExecutor(async () => {
180
+ const executeTool: SessionCommand = async () => {
184
181
  await writeFile(target, "command changed\n");
185
- return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
186
- });
182
+ return commandCompletion({ content: [{ type: "text", text: "checked" }] });
183
+ };
184
+ const fusion = createActionFusionExecutor(() => {});
187
185
  const write = makeWriteOverride(dir, fusion) as any;
188
186
  const result = await callTool(
189
187
  write,
@@ -193,13 +191,12 @@ test("write reports changed freshness when then_run changes the target", async (
193
191
  mode: "create",
194
192
  then_run: { command: "check" },
195
193
  },
196
- { toolCallId: "changed", ctx: context(dir) },
194
+ { toolCallId: "changed", ctx: { ...context(dir), executeTool } },
197
195
  );
198
196
  const text = result.content.map((block: any) => block.text).join("\n");
199
197
  assert.doesNotMatch(text, /revision:|[0-9a-f]{64}/i);
200
- assert.equal(reportOf(result, "write")?.created, true);
201
198
  assert.equal(reportOf(result, "write")?.freshness, "changed");
202
- assert.equal(reportOf(result, "write")?.next, REREAD);
199
+ assert.match(reportOf(result, "write")?.next ?? "", /Read the file before further edits/);
203
200
  assert.deepEqual(reportOf(result, "write")?.then_run, {
204
201
  status: "succeeded",
205
202
  output: "checked",
@@ -217,10 +214,11 @@ test("write reports changed freshness when then_run changes the target", async (
217
214
  test("write reports missing freshness when then_run removes the target", async () =>
218
215
  withTemp(async (dir) => {
219
216
  const target = join(dir, "missing.txt");
220
- const fusion = createActionFusionExecutor(async () => {
217
+ const executeTool: SessionCommand = async () => {
221
218
  await rm(target);
222
- return { status: "succeeded", result: { content: [{ type: "text", text: "removed" }] } };
223
- });
219
+ return commandCompletion({ content: [{ type: "text", text: "removed" }] });
220
+ };
221
+ const fusion = createActionFusionExecutor(() => {});
224
222
  const result = await makeWriteOverride(dir, fusion).execute(
225
223
  "missing",
226
224
  {
@@ -231,7 +229,7 @@ test("write reports missing freshness when then_run removes the target", async (
231
229
  },
232
230
  undefined,
233
231
  undefined,
234
- context(dir),
232
+ { ...context(dir), executeTool },
235
233
  );
236
234
  assert.equal(reportOf(result, "write")?.freshness, "missing");
237
235
  assert.equal(reportOf(result, "write")?.next, undefined);
@@ -240,18 +238,19 @@ test("write reports missing freshness when then_run removes the target", async (
240
238
  test("write preserves command failure and changed freshness", async () =>
241
239
  withTemp(async (dir) => {
242
240
  const target = join(dir, "failed.txt");
243
- const fusion = createActionFusionExecutor(async () => {
241
+ const executeTool: SessionCommand = async () => {
244
242
  await writeFile(target, "changed before failure\n");
245
- throw new Error("command failed");
246
- });
243
+ return commandCompletion({ content: [{ type: "text", text: "command failed" }] }, true);
244
+ };
245
+ const fusion = createActionFusionExecutor(() => {});
247
246
  const result = await makeWriteOverride(dir, fusion).execute(
248
247
  "failed",
249
248
  { path: "failed.txt", content: "mutation\n", mode: "create", then_run: { command: "fail" } },
250
249
  undefined,
251
250
  undefined,
252
- context(dir),
251
+ { ...context(dir), executeTool },
253
252
  );
254
253
  assert.equal(reportOf(result, "write")?.freshness, "changed");
255
254
  assert.equal(reportOf(result, "write")?.then_run?.status, "failed");
256
- assert.equal(reportOf(result, "write")?.next, REREAD);
255
+ assert.match(reportOf(result, "write")?.next ?? "", /Read the file before further edits/);
257
256
  }));
@@ -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),