@criogaid/pi-hashline-edit 0.9.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.
Files changed (110) hide show
  1. package/README.md +9 -10
  2. package/README.zh-CN.md +8 -9
  3. package/THIRD_PARTY_NOTICES.md +0 -6
  4. package/package.json +1 -1
  5. package/src/core/AGENTS.md +4 -5
  6. package/src/core/apply.test.ts +96 -271
  7. package/src/core/apply.ts +62 -169
  8. package/src/core/case-fold.ts +8 -67
  9. package/src/core/character-set.ts +4 -4
  10. package/src/core/errors.test.ts +23 -7
  11. package/src/core/errors.ts +32 -5
  12. package/src/core/hash.test.ts +8 -16
  13. package/src/core/hash.ts +3 -23
  14. package/src/core/lines.ts +2 -4
  15. package/src/core/regex.ts +3 -4
  16. package/src/core/replace.test.ts +47 -1
  17. package/src/core/replace.ts +19 -13
  18. package/src/core/search-analysis.test.ts +24 -27
  19. package/src/core/search-analysis.ts +34 -65
  20. package/src/core/search-matrix.testing.ts +0 -2
  21. package/src/core/search.ts +1 -1
  22. package/src/core/text.test.ts +5 -5
  23. package/src/core/text.ts +5 -12
  24. package/src/core/types.ts +7 -43
  25. package/src/integration/fusion-dispatch.test.ts +2 -5
  26. package/src/integration/grep-flags.test.ts +0 -14
  27. package/src/integration/grep-js-completeness.test.ts +34 -8
  28. package/src/integration/grep-line-candidates.test.ts +4 -8
  29. package/src/integration/grep-matrix.test.ts +3 -7
  30. package/src/integration/grep-path-matrix.test.ts +5 -5
  31. package/src/integration/grep-prefilter-coverage.test.ts +3 -7
  32. package/src/integration/grep-process-limits.test.ts +78 -0
  33. package/src/integration/grep-rg.test.ts +118 -20
  34. package/src/integration/grep-scan-budget.test.ts +2 -2
  35. package/src/integration/grep-translation.test.ts +12 -27
  36. package/src/integration/large-text.test.ts +1 -47
  37. package/src/integration/model-budget-lifecycle.test.ts +2 -1
  38. package/src/integration/publication-read-changes.test.ts +123 -45
  39. package/src/integration/replace-worker-boundary.test.ts +21 -11
  40. package/src/integration/report-replay.test.ts +20 -22
  41. package/src/pi/AGENTS.md +15 -15
  42. package/src/pi/action-fusion.test.ts +164 -296
  43. package/src/pi/action-fusion.ts +148 -111
  44. package/src/pi/anchor-format.test.ts +9 -9
  45. package/src/pi/anchor-format.ts +6 -8
  46. package/src/pi/argument-diagnostics.ts +21 -25
  47. package/src/pi/argument-error.ts +2 -1
  48. package/src/pi/argument-validation.test.ts +129 -157
  49. package/src/pi/argument-validation.ts +5 -8
  50. package/src/pi/budgets.ts +5 -4
  51. package/src/pi/config.test.ts +5 -2
  52. package/src/pi/config.ts +4 -6
  53. package/src/pi/diagnostic-buffer.test.ts +12 -1
  54. package/src/pi/diagnostic-buffer.ts +1 -2
  55. package/src/pi/directory-sync.test.ts +0 -1
  56. package/src/pi/edit-tool.ts +46 -156
  57. package/src/pi/error-text.ts +4 -8
  58. package/src/pi/execute.test.ts +83 -196
  59. package/src/pi/failure-context.test.ts +144 -126
  60. package/src/pi/failure-context.ts +44 -93
  61. package/src/pi/failure-path.test.ts +168 -141
  62. package/src/pi/file-commit.test.ts +110 -53
  63. package/src/pi/file-commit.ts +90 -137
  64. package/src/pi/file-snapshot.test.ts +3 -5
  65. package/src/pi/fusion-card.test.ts +4 -1
  66. package/src/pi/fusion-card.ts +16 -24
  67. package/src/pi/grep-candidates.test.ts +2 -101
  68. package/src/pi/grep-candidates.ts +23 -69
  69. package/src/pi/grep-matrix.test.ts +2 -2
  70. package/src/pi/grep-order.test.ts +4 -4
  71. package/src/pi/grep-output.ts +62 -96
  72. package/src/pi/grep-render.ts +1 -1
  73. package/src/pi/grep-reread.ts +92 -0
  74. package/src/pi/grep-scope.ts +40 -42
  75. package/src/pi/grep-search.ts +22 -31
  76. package/src/pi/grep-tool.ts +55 -82
  77. package/src/pi/grep.test.ts +80 -45
  78. package/src/pi/model-budgets.test.ts +25 -23
  79. package/src/pi/model-budgets.ts +6 -38
  80. package/src/pi/mutation-result.test.ts +44 -35
  81. package/src/pi/mutation-result.ts +51 -20
  82. package/src/pi/mutation-runner.ts +126 -70
  83. package/src/pi/output-budgets.test.ts +36 -14
  84. package/src/pi/path.ts +1 -1
  85. package/src/pi/read-tool.ts +5 -5
  86. package/src/pi/regex-worker-contract.ts +3 -13
  87. package/src/pi/regex-worker.ts +1 -10
  88. package/src/pi/registration.test.ts +64 -36
  89. package/src/pi/render.test.ts +38 -28
  90. package/src/pi/render.ts +59 -63
  91. package/src/pi/replace-regex.ts +14 -2
  92. package/src/pi/replace-tool.ts +20 -82
  93. package/src/pi/replace-worker.ts +1 -1
  94. package/src/pi/replace.test.ts +9 -5
  95. package/src/pi/report-cause.ts +3 -8
  96. package/src/pi/report-failure.testing.ts +1 -2
  97. package/src/pi/report-schema.ts +62 -81
  98. package/src/pi/report.test.ts +171 -25
  99. package/src/pi/report.ts +33 -87
  100. package/src/pi/result-safety.test.ts +149 -176
  101. package/src/pi/rg-process.test.ts +77 -63
  102. package/src/pi/rg-process.ts +48 -64
  103. package/src/pi/rg-regex.ts +40 -400
  104. package/src/pi/schema.ts +12 -0
  105. package/src/pi/text-stream.test.ts +0 -21
  106. package/src/pi/text-stream.ts +14 -21
  107. package/src/pi/write-tool.ts +33 -62
  108. package/src/pi/write.test.ts +1 -3
  109. package/src/testing/regex-translation-corpus.ts +12 -12
  110. package/src/pi/replace-worker-contract.ts +0 -10
@@ -4,14 +4,12 @@ import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import assert from "node:assert/strict";
6
6
  import { test } from "node:test";
7
- import { createActionFusionExecutor, type ActionFusionProgress } from "./action-fusion.ts";
8
- import { FileMutationError } from "./file-commit.ts";
9
- import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
10
- import { REREAD } from "./report-schema.ts";
11
- import { CommandNotRun, describeFailure, renderReport, reportOf } from "./report.ts";
7
+ import { createActionFusionExecutor } from "./action-fusion.ts";
8
+ import type { ActionFusionProgress } from "./report-schema.ts";
9
+ import { outputBudgetsFor } from "./budgets.ts";
10
+ import { reportOf } from "./report.ts";
12
11
  import { callTool, createToolContext } from "./tool-call.testing.ts";
13
12
  import { makeWriteOverride } from "./write-tool.ts";
14
- import { publishedMutation } from "./mutation-outcome.testing.ts";
15
13
 
16
14
  async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
17
15
  const dir = await mkdtemp(join(tmpdir(), "hashline-failure-"));
@@ -21,7 +19,6 @@ async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
21
19
  await rm(dir, { recursive: true, force: true });
22
20
  }
23
21
  }
24
- const mutation = (content: string) => publishedMutation(content, { content: [], details: {} });
25
22
 
26
23
  test("temporary preparation and cleanup failures → preserve the commit failure and target bytes", async (t) =>
27
24
  withTemp(async (dir) => {
@@ -51,7 +48,6 @@ test("temporary preparation and cleanup failures → preserve the commit failure
51
48
  const report = reportOf(result, "write");
52
49
  assert.equal(result.isError, true);
53
50
  assert.equal(report?.error, "PUBLISH_FAILED");
54
- assert.equal(report.stage, "commit");
55
51
  assert.equal(report.publication, "NOT_PUBLISHED");
56
52
  assert.deepEqual(report.cause, { code: original.code, message: original.message });
57
53
  assert.equal(await readFile(target, "utf8"), "before\n");
@@ -62,99 +58,120 @@ test("temporary preparation and cleanup failures → preserve the commit failure
62
58
  }
63
59
  }));
64
60
 
65
- test("published post-processing failure → skips command and preserves publication state", async (t) =>
61
+ test("publication boundary and observer failures → preserve recovery and emit each diagnostic once", async (t) =>
66
62
  withTemp(async (dir) => {
67
- const target = join(dir, "published.txt");
68
- await writeFile(target, "content\n");
69
- let commanded = false;
70
- const progress: ActionFusionProgress[] = [];
71
- const mutationError = new FileMutationError(
72
- "POST_PROCESS_FAILED",
73
- "post_process",
74
- "PUBLISHED",
75
- "result generation failed",
76
- );
77
- const fusion = createActionFusionExecutor(
78
- async () => {
79
- commanded = true;
80
- return { status: "succeeded", result: { content: [{ type: "text", text: "never" }] } };
81
- },
82
- (update) => progress.push(update),
83
- );
84
- await assert.rejects(
85
- fusion({
86
- budget: DEFAULT_OUTPUT_BUDGETS,
87
- toolCallId: "published",
88
- tool: "edit",
89
- displayPath: "published.txt",
90
- absolutePath: target,
91
- thenRun: { command: "check" },
92
- mutate: async () => {
93
- throw mutationError;
94
- },
95
- signal: undefined,
96
- ctx: await createToolContext(dir, t),
97
- }),
98
- (error: unknown) => {
99
- assert.ok(error instanceof CommandNotRun);
100
- assert.equal(error.status, "skipped");
101
- assert.equal(error.cause, mutationError);
102
- assert.equal(Object.hasOwn(error, "error"), false);
103
- const report = describeFailure("edit", error, { budget: DEFAULT_OUTPUT_BUDGETS });
104
- assert.equal(report.publication, "PUBLISHED");
105
- assert.equal(report.stage, "post_process");
106
- assert.equal(report.then_run?.status, "skipped");
107
- assert.match(report.next ?? "", /change is saved; do not repeat/);
108
- const diagnostic = renderReport(report);
109
- assert.equal(diagnostic.split(mutationError.message).length - 1, 1);
110
- assert.equal(diagnostic.split('"status": "skipped"').length - 1, 1);
111
- return true;
112
- },
113
- );
114
- assert.equal(commanded, false);
115
- assert.ok(progress.every((update) => update.mutationCompleted === false));
116
- assert.equal(await readFile(target, "utf8"), "content\n");
117
- }));
118
-
119
- test("command fails and modifies the target → reports changed freshness and command failure", async (t) =>
120
- withTemp(async (dir) => {
121
- const target = join(dir, "failed.txt");
122
- const fusion = createActionFusionExecutor(async () => {
123
- await writeFile(target, "command changed\n");
124
- throw new Error("command failed");
125
- });
126
- const outcome = await fusion({
127
- budget: DEFAULT_OUTPUT_BUDGETS,
128
- toolCallId: "failed",
129
- tool: "edit",
130
- displayPath: "failed.txt",
131
- absolutePath: target,
132
- thenRun: { command: "check" },
133
- mutate: async () => {
134
- await writeFile(target, "mutation\n");
135
- return mutation("mutation\n");
136
- },
137
- signal: undefined,
138
- ctx: await createToolContext(dir, t),
139
- });
140
- const report = reportOf(outcome, "edit");
141
- assert.equal(report?.publication, "PUBLISHED");
142
- assert.equal(report?.then_run?.status, "failed");
143
- assert.equal(report?.freshness, "changed");
144
- assert.equal(report?.next, REREAD);
145
- assert.equal(report?.then_run?.output, "command failed");
146
- assert.doesNotMatch(
147
- outcome.content.map((block) => (block.type === "text" ? block.text : "")).join("\n"),
148
- /Updated anchors|1#AB/,
149
- );
150
- assert.equal(await readFile(target, "utf8"), "command changed\n");
63
+ const budget = outputBudgetsFor(4096);
64
+ const context = await createToolContext(dir, t);
65
+ for (const publication of ["NOT_PUBLISHED", "PUBLISHED", "UNKNOWN"] as const) {
66
+ for (const progressError of [
67
+ undefined,
68
+ "display unavailable",
69
+ Object.assign(new Error("display unavailable " + "界".repeat(budget.fieldBytes)), {
70
+ code: "EIO",
71
+ cause: new Error("nested " + "界".repeat(budget.fieldBytes)),
72
+ }),
73
+ ]) {
74
+ const target = join(dir, "boundary.txt");
75
+ await writeFile(target, "before\n");
76
+ const original = Object.assign(new Error("injected filesystem failure"), { code: "EIO" });
77
+ const nativeOpen = fs.open;
78
+ const nativeRename = fs.rename;
79
+ let published = false;
80
+ let commands = 0;
81
+ const progress: ActionFusionProgress[] = [];
82
+ try {
83
+ t.mock.method(fs, "open", async (...args: Parameters<typeof nativeOpen>) => {
84
+ if (
85
+ publication === "NOT_PUBLISHED" &&
86
+ String(args[0]).includes(".hashline-commit-") &&
87
+ args[1] === "w"
88
+ )
89
+ throw original;
90
+ if (publication === "PUBLISHED" && published && String(args[0]) === target)
91
+ throw original;
92
+ return nativeOpen(...args);
93
+ });
94
+ t.mock.method(fs, "rename", async (...args: Parameters<typeof nativeRename>) => {
95
+ if (String(args[1]) === target && publication === "UNKNOWN") throw original;
96
+ await nativeRename(...args);
97
+ published = true;
98
+ });
99
+ syncBuiltinESMExports();
100
+ const fusion = createActionFusionExecutor(
101
+ async () => {
102
+ commands++;
103
+ return { status: "succeeded", result: { content: [] } };
104
+ },
105
+ (update) => {
106
+ progress.push(update);
107
+ if (progressError !== undefined) throw progressError;
108
+ },
109
+ );
110
+ const result = await callTool(
111
+ makeWriteOverride(dir, fusion, () => budget),
112
+ { path: target, mode: "overwrite", content: "after\n", then_run: { command: "check" } },
113
+ { ctx: context },
114
+ );
115
+ const report = reportOf(result, "write");
116
+ assert.equal(result.isError, true);
117
+ assert.ok(report);
118
+ assert.equal(
119
+ report.error,
120
+ publication === "PUBLISHED" ? "POST_PROCESS_FAILED" : "PUBLISH_FAILED",
121
+ );
122
+ assert.equal(report.publication, publication);
123
+ assert.deepEqual(report.cause, { code: original.code, message: original.message });
124
+ assert.equal(report.then_run?.status, "skipped");
125
+ if (publication === "PUBLISHED")
126
+ assert.match(report.next ?? "", /change is saved; do not repeat/);
127
+ else if (publication === "UNKNOWN")
128
+ assert.match(report.next ?? "", /may or may not be saved/);
129
+ else assert.equal(report.next, undefined);
130
+ assert.equal(result.content.length, 1);
131
+ const block = result.content[0];
132
+ assert.equal(block.type, "text");
133
+ const diagnostic = block.text;
134
+ for (const once of [
135
+ original.message,
136
+ report.message,
137
+ '"then_run"',
138
+ '"status": "skipped"',
139
+ ])
140
+ assert.equal(diagnostic.split(once).length - 1, 1);
141
+ assert.equal(commands, 0);
142
+ assert.deepEqual(
143
+ progress.map((update) => update.command),
144
+ ["waiting", "skipped"],
145
+ );
146
+ assert.ok(progress.every((update) => !update.mutationCompleted));
147
+ if (typeof progressError === "string") {
148
+ assert.deepEqual(report.progressError, { message: progressError });
149
+ } else if (progressError) {
150
+ assert.equal(report.progressError?.code, "EIO");
151
+ assert.ok(report.progressError.message.startsWith("display unavailable"));
152
+ assert.ok(Buffer.byteLength(report.progressError.message) <= budget.fieldBytes);
153
+ assert.notEqual(report.progressError.message, progressError.message);
154
+ assert.ok(report.progressError.cause);
155
+ assert.ok(report.progressError.cause.message.startsWith("nested"));
156
+ assert.ok(Buffer.byteLength(report.progressError.cause.message) <= budget.fieldBytes);
157
+ assert.notEqual(report.progressError.cause.message, progressError.cause.message);
158
+ } else assert.equal(report.progressError, undefined);
159
+ } finally {
160
+ t.mock.restoreAll();
161
+ syncBuiltinESMExports();
162
+ }
163
+ assert.equal(
164
+ await readFile(target, "utf8"),
165
+ publication === "PUBLISHED" ? "after\n" : "before\n",
166
+ );
167
+ }
168
+ }
151
169
  }));
152
170
 
153
171
  test("cancellation after publication → does not rerun mutation and still inspects freshness", async (t) =>
154
172
  withTemp(async (dir) => {
155
173
  for (const state of ["changed", "missing"] as const) {
156
174
  const target = join(dir, `${state}.txt`);
157
- let mutations = 0;
158
175
  const controller = new AbortController();
159
176
  const fusion = createActionFusionExecutor(async () => {
160
177
  if (state === "changed") await writeFile(target, "command changed\n");
@@ -162,26 +179,15 @@ test("cancellation after publication → does not rerun mutation and still inspe
162
179
  controller.abort();
163
180
  throw new Error("timed out");
164
181
  });
165
- const outcome = await fusion({
166
- budget: DEFAULT_OUTPUT_BUDGETS,
167
- toolCallId: "cancelled",
168
- tool: "edit",
169
- displayPath: `${state}.txt`,
170
- absolutePath: target,
171
- thenRun: { command: "check" },
172
- mutate: async () => {
173
- mutations++;
174
- await writeFile(target, "mutation\n");
175
- return mutation("mutation\n");
176
- },
177
- signal: controller.signal,
178
- ctx: await createToolContext(dir, t),
179
- });
180
- const report = reportOf(outcome, "edit");
182
+ const result = await callTool(
183
+ makeWriteOverride(dir, fusion),
184
+ { path: target, mode: "create", content: "mutation\n", then_run: { command: "check" } },
185
+ { signal: controller.signal, ctx: await createToolContext(dir, t) },
186
+ );
187
+ const report = reportOf(result, "write");
181
188
  assert.equal(report?.publication, "PUBLISHED");
182
189
  assert.equal(report?.then_run?.status, "cancelled");
183
190
  assert.equal(report?.freshness, state);
184
- assert.equal(mutations, 1);
185
191
  if (state === "changed") assert.equal(await readFile(target, "utf8"), "command changed\n");
186
192
  else await assert.rejects(readFile(target), { code: "ENOENT" });
187
193
  }
@@ -198,22 +204,18 @@ test("first command fails → releases the queue for the next mutation", async (
198
204
  return { status: "succeeded", result: { content: [{ type: "text", text: "ok" }] } };
199
205
  });
200
206
  for (const content of ["first\n", "second\n"]) {
201
- const result = await fusion({
202
- budget: DEFAULT_OUTPUT_BUDGETS,
203
- toolCallId: content,
204
- tool: "edit",
205
- displayPath: "queue.txt",
206
- absolutePath: target,
207
- thenRun: { command: "check" },
208
- mutate: async () => {
209
- await writeFile(target, content);
210
- return mutation(content);
207
+ const result = await callTool(
208
+ makeWriteOverride(dir, fusion),
209
+ {
210
+ path: target,
211
+ content,
212
+ mode: calls === 0 ? "create" : "overwrite",
213
+ then_run: { command: "check" },
211
214
  },
212
- signal: undefined,
213
- ctx,
214
- });
215
+ { toolCallId: content, ctx },
216
+ );
215
217
  assert.equal(
216
- reportOf(result, "edit")?.then_run?.status,
218
+ reportOf(result, "write")?.then_run?.status,
217
219
  calls === 1 ? "failed" : "succeeded",
218
220
  );
219
221
  }
@@ -232,26 +234,51 @@ test("command succeeds on an unchanged target → retains the result and complet
232
234
  }),
233
235
  (event) => progress.push(event),
234
236
  );
235
- const completed = await fusion({
236
- budget: DEFAULT_OUTPUT_BUDGETS,
237
- toolCallId: "finalize",
238
- tool: "edit",
239
- displayPath: "finalize.txt",
240
- absolutePath: target,
241
- thenRun: { command: "check" },
242
- signal: undefined,
243
- ctx: await createToolContext(dir, t),
244
- mutate: async () => {
245
- await writeFile(target, "saved\n");
246
- return mutation("saved\n");
247
- },
248
- });
237
+ const completed = await callTool(
238
+ makeWriteOverride(dir, fusion),
239
+ { path: target, content: "saved\n", mode: "create", then_run: { command: "check" } },
240
+ { ctx: await createToolContext(dir, t) },
241
+ );
249
242
  assert.equal(completed.content.length, 1);
250
- assert.equal(reportOf(completed, "edit")?.then_run?.status, "succeeded");
251
- assert.equal(reportOf(completed, "edit")?.then_run?.output, "command finished");
243
+ assert.equal(reportOf(completed, "write")?.then_run?.status, "succeeded");
244
+ assert.equal(reportOf(completed, "write")?.then_run?.output, "command finished");
252
245
  assert.equal(progress.filter((event) => event.command === "succeeded").length, 1);
253
246
  assert.equal(progress.at(-1)?.freshness, "unchanged");
254
247
  assert.deepEqual(progress.at(-1)?.result, {
255
248
  content: [{ type: "text", text: "command finished" }],
256
249
  });
257
250
  }));
251
+
252
+ test("cancellation before mutation → reports a cancelled command without publishing", async (t) =>
253
+ withTemp(async (dir) => {
254
+ const target = join(dir, "cancelled.txt");
255
+ await writeFile(target, "before\n");
256
+ const controller = new AbortController();
257
+ controller.abort();
258
+ let commands = 0;
259
+ const progress: ActionFusionProgress[] = [];
260
+ const fusion = createActionFusionExecutor(
261
+ async () => {
262
+ commands++;
263
+ return { status: "succeeded", result: { content: [] } };
264
+ },
265
+ (event) => progress.push(event),
266
+ );
267
+ const result = await callTool(
268
+ makeWriteOverride(dir, fusion),
269
+ { path: target, mode: "overwrite", content: "after\n", then_run: { command: "check" } },
270
+ { signal: controller.signal, ctx: await createToolContext(dir, t) },
271
+ );
272
+ const report = reportOf(result, "write");
273
+ assert.equal(result.isError, true);
274
+ assert.equal(report?.error, "OPERATION_ABORTED");
275
+ assert.equal(report?.publication, "NOT_PUBLISHED");
276
+ assert.equal(report?.then_run?.status, "cancelled");
277
+ assert.deepEqual(
278
+ progress.map((event) => event.command),
279
+ ["waiting", "cancelled"],
280
+ );
281
+ assert.ok(progress.every((event) => !event.mutationCompleted));
282
+ assert.equal(commands, 0);
283
+ assert.equal(await readFile(target, "utf8"), "before\n");
284
+ }));