@criogaid/pi-hashline-edit 0.7.0 → 0.8.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 (118) hide show
  1. package/README.md +74 -222
  2. package/README.zh-CN.md +162 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +172 -134
  6. package/src/core/apply.ts +28 -30
  7. package/src/core/case-fold.test.ts +11 -7
  8. package/src/core/case-fold.ts +2 -6
  9. package/src/core/character-set.ts +14 -0
  10. package/src/core/hash.test.ts +1 -25
  11. package/src/core/hash.ts +0 -5
  12. package/src/core/lines.ts +25 -7
  13. package/src/core/ranges.test.ts +41 -9
  14. package/src/core/ranges.ts +11 -4
  15. package/src/core/regex.ts +36 -6
  16. package/src/core/replace-schema.ts +18 -11
  17. package/src/core/replace.test.ts +90 -0
  18. package/src/core/replace.ts +6 -5
  19. package/src/core/search-analysis.test.ts +9 -0
  20. package/src/core/search-analysis.ts +35 -31
  21. package/src/core/search.ts +1 -1
  22. package/src/core/types.ts +5 -7
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +16 -8
  25. package/src/integration/fusion-dispatch.test.ts +41 -22
  26. package/src/integration/grep-flags.test.ts +6 -13
  27. package/src/integration/grep-js-completeness.test.ts +41 -31
  28. package/src/integration/grep-order.test.ts +78 -0
  29. package/src/integration/grep-path-matrix.test.ts +4 -3
  30. package/src/integration/grep-regex.test.ts +13 -16
  31. package/src/integration/grep-rg.test.ts +127 -87
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +3 -4
  34. package/src/integration/model-budget-lifecycle.test.ts +15 -6
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +1 -1
  37. package/src/integration/npm-package.testing.ts +6 -32
  38. package/src/integration/publication-read-changes.test.ts +58 -26
  39. package/src/integration/replace-package.testing.ts +4 -1
  40. package/src/integration/replace-worker-boundary.test.ts +34 -24
  41. package/src/integration/report-replay.test.ts +2 -2
  42. package/src/integration/utf8-errors.test.ts +2 -2
  43. package/src/pi/AGENTS.md +12 -13
  44. package/src/pi/action-fusion.test.ts +71 -216
  45. package/src/pi/action-fusion.ts +37 -74
  46. package/src/pi/anchor-format.test.ts +16 -0
  47. package/src/pi/anchor-format.ts +7 -3
  48. package/src/pi/argument-diagnostics.ts +1 -1
  49. package/src/pi/argument-error.testing.ts +7 -4
  50. package/src/pi/argument-error.ts +5 -7
  51. package/src/pi/argument-format.test.ts +5 -16
  52. package/src/pi/argument-validation.test.ts +59 -25
  53. package/src/pi/argument-validation.ts +4 -5
  54. package/src/pi/budgets.ts +5 -7
  55. package/src/pi/config.test.ts +15 -5
  56. package/src/pi/config.ts +6 -6
  57. package/src/pi/diagnostic-buffer.test.ts +2 -3
  58. package/src/pi/diagnostic-buffer.ts +2 -2
  59. package/src/pi/edit-tool.ts +9 -12
  60. package/src/pi/execute.test.ts +80 -149
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +12 -9
  63. package/src/pi/failure-path.test.ts +115 -50
  64. package/src/pi/file-commit.test.ts +30 -46
  65. package/src/pi/file-commit.ts +11 -40
  66. package/src/pi/file-revision.ts +25 -0
  67. package/src/pi/fusion-card-interaction.test.ts +143 -0
  68. package/src/pi/fusion-card.test.ts +193 -37
  69. package/src/pi/fusion-card.ts +186 -116
  70. package/src/pi/grep-candidates.ts +25 -27
  71. package/src/pi/grep-matrix.test.ts +1 -1
  72. package/src/pi/grep-order.test.ts +323 -0
  73. package/src/pi/grep-output.testing.ts +24 -0
  74. package/src/pi/grep-output.ts +62 -69
  75. package/src/pi/grep-render.ts +4 -1
  76. package/src/pi/grep-scope.ts +1 -1
  77. package/src/pi/grep-search.ts +85 -56
  78. package/src/pi/grep-tool.ts +17 -48
  79. package/src/pi/grep.test.ts +270 -169
  80. package/src/pi/mutation-outcome.testing.ts +2 -3
  81. package/src/pi/mutation-result.test.ts +26 -19
  82. package/src/pi/mutation-result.ts +25 -40
  83. package/src/pi/mutation-runner.ts +9 -17
  84. package/src/pi/output-budgets.test.ts +25 -16
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +4 -5
  87. package/src/pi/read-tool.ts +32 -80
  88. package/src/pi/read.test.ts +145 -92
  89. package/src/pi/regex-worker-contract.ts +5 -23
  90. package/src/pi/regex-worker.ts +4 -10
  91. package/src/pi/registration.test.ts +54 -49
  92. package/src/pi/render.test.ts +196 -93
  93. package/src/pi/render.ts +32 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +27 -26
  96. package/src/pi/replace-worker-contract.ts +3 -12
  97. package/src/pi/replace.test.ts +144 -226
  98. package/src/pi/report-cause.ts +23 -19
  99. package/src/pi/report-failure.testing.ts +2 -2
  100. package/src/pi/report-schema.ts +55 -43
  101. package/src/pi/report.test.ts +138 -71
  102. package/src/pi/report.ts +40 -43
  103. package/src/pi/result-safety.test.ts +157 -117
  104. package/src/pi/rg-process.test.ts +36 -10
  105. package/src/pi/rg-process.ts +9 -3
  106. package/src/pi/rg-regex.test.ts +17 -0
  107. package/src/pi/rg-regex.ts +28 -22
  108. package/src/pi/text-stream.test.ts +35 -36
  109. package/src/pi/text-stream.ts +14 -14
  110. package/src/pi/tool-call.testing.ts +17 -7
  111. package/src/pi/write-tool.ts +9 -11
  112. package/src/pi/write.test.ts +43 -75
  113. package/src/testing/regex-translation-corpus.ts +16 -0
  114. package/src/testing/rg-line-listing.fixture.mjs +1 -1
  115. package/src/integration/forget-grep.test.ts +0 -148
  116. package/src/pi/forget-tool.ts +0 -367
  117. package/src/pi/forget.test.ts +0 -630
  118. package/src/pi/forget.testing.ts +0 -201
@@ -12,7 +12,8 @@ import { createActionFusionExecutor } from "./action-fusion.ts";
12
12
  import { makeReplaceTool } from "./replace-tool.ts";
13
13
  import { makeWriteOverride } from "./write-tool.ts";
14
14
  import { makeEditOverride } from "./edit-tool.ts";
15
- import { byteRevision, commitFreshness, FileMutationError } from "./file-commit.ts";
15
+ import { commitFreshness, FileMutationError } from "./file-commit.ts";
16
+ import { byteRevision } from "./file-revision.ts";
16
17
  import { finalizeMutation, postProcessMutation, type MutationOutcome } from "./mutation-result.ts";
17
18
  import { CommandNotRun, decodeReport, reportOf } from "./report.ts";
18
19
  import { REREAD } from "./report-schema.ts";
@@ -32,7 +33,7 @@ const invoke = (
32
33
  ) => callTool(tool, args, { toolCallId, signal, onUpdate, ctx: context });
33
34
 
34
35
  function assertFailureByteBudgets(message: string): void {
35
- const report = decodeReport(message);
36
+ const report = decodeReport(message, "edit");
36
37
  assert.ok(report);
37
38
  assert.equal(report.error, "ANCHOR_MISMATCH");
38
39
  assert.ok(report.failures && report.failures.length > 0);
@@ -51,45 +52,66 @@ function assertFailureByteBudgets(message: string): void {
51
52
  }
52
53
  }
53
54
 
54
- test("replace withholds anchors in progress and after commands change or remove the file", async (t) => {
55
+ test("edit and replace commands change or remove the file → withhold anchors and report freshness", async (t) => {
55
56
  const dir = await mkdtemp(join(tmpdir(), "hashline-result-"));
56
57
  try {
57
- for (const state of ["changed", "missing", "unknown"] as const) {
58
- for (const fails of [false, true]) {
59
- const path = join(dir, `${state}-${fails}.txt`);
60
- await writeFile(path, "before\n");
61
- const updates: any[] = [];
62
- const fusion = createActionFusionExecutor(async (_id, _input, _signal, _ctx, onUpdate) => {
63
- if (state === "changed") await writeFile(path, "command output\n");
64
- else {
65
- await rm(path);
66
- if (state === "unknown") await mkdir(path);
67
- }
68
- onUpdate?.({ content: [{ type: "text", text: "progress" }], details: undefined });
69
- if (fails) throw new Error("command failed");
70
- return { status: "succeeded", output: "done" };
71
- });
72
- const result = await invoke(
73
- makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
74
- "replace",
75
- {
76
- path,
77
- replacements: [{ find: "before", replace: "after" }],
78
- then_run: { command: "check" },
79
- },
80
- undefined,
81
- (update: any) => updates.push(update),
82
- await ctx(dir, t),
83
- );
84
- assert.ok(result.details.actionFusion);
85
- assert.equal(result.details.actionFusion.freshness, state);
86
- assert.equal(result.details.publication, "PUBLISHED");
87
- assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
88
- assert.equal(reportOf(result)?.freshness, state);
89
- assert.equal(reportOf(result)?.next, REREAD);
90
- assert.equal(result.content.length, 1);
91
- for (const update of updates)
92
- assert.doesNotMatch(text(update), /Updated anchors|\d+#[0-9A-Z]+│/);
58
+ const context = await ctx(dir, t);
59
+ for (const toolName of ["edit", "replace"] as const) {
60
+ for (const state of ["changed", "missing", "unknown"] as const) {
61
+ for (const fails of [false, true]) {
62
+ const path = join(dir, `${toolName}-${state}-${fails}.txt`);
63
+ await writeFile(path, "before\n");
64
+ const updates: AgentToolResult<unknown>[] = [];
65
+ const fusion = createActionFusionExecutor(
66
+ async (_id, _input, _signal, _ctx, onUpdate) => {
67
+ if (state === "changed") await writeFile(path, "command output\n");
68
+ else {
69
+ await rm(path);
70
+ if (state === "unknown") await mkdir(path);
71
+ }
72
+ onUpdate?.({ content: [{ type: "text", text: "progress" }], details: undefined });
73
+ if (fails) throw new Error("command failed");
74
+ return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
75
+ },
76
+ );
77
+ const tool =
78
+ toolName === "edit"
79
+ ? makeEditOverride(dir, DEFAULT_CONFIG, fusion)
80
+ : makeReplaceTool(dir, DEFAULT_CONFIG, fusion);
81
+ const args =
82
+ toolName === "edit"
83
+ ? {
84
+ edits: [
85
+ {
86
+ op: "replace",
87
+ anchor: `1#${computeLineHash(1, "before", DEFAULT_CONFIG.hashLen)}`,
88
+ body: ["after"],
89
+ },
90
+ ],
91
+ }
92
+ : { replacements: [{ literal: true, find: "before", replace: "after" }] };
93
+ const result = await callTool(
94
+ tool,
95
+ { path, ...args, then_run: { command: "check" } },
96
+ {
97
+ onUpdate: (update: AgentToolResult<unknown>) => updates.push(update),
98
+ ctx: context,
99
+ },
100
+ );
101
+ assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
102
+ const report = reportOf(result, toolName);
103
+ assert.equal(report?.freshness, state);
104
+ assert.equal(report?.publication, "PUBLISHED");
105
+ assert.equal(report?.next, REREAD);
106
+ assert.equal(report?.then_run?.status, fails ? "failed" : "succeeded");
107
+ assert.equal(result.content.length, 1);
108
+ assert.ok(updates.length > 0);
109
+ for (const update of updates)
110
+ assert.doesNotMatch(text(update), /Updated anchors|\d+#[0-9A-Z]+│/);
111
+ if (state === "changed") assert.equal(await readFile(path, "utf8"), "command output\n");
112
+ else if (state === "missing") await assert.rejects(stat(path), { code: "ENOENT" });
113
+ else assert.equal((await stat(path)).isDirectory(), true);
114
+ }
93
115
  }
94
116
  }
95
117
  } finally {
@@ -108,7 +130,7 @@ test("progress callback failures preserve publication and do not prevent the com
108
130
  const fusion = createActionFusionExecutor(
109
131
  async () => {
110
132
  commands++;
111
- return { status: "succeeded", output: "done" };
133
+ return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
112
134
  },
113
135
  callback === "reporter" ? fail : undefined,
114
136
  );
@@ -128,9 +150,8 @@ test("progress callback failures preserve publication and do not prevent the com
128
150
  );
129
151
  assert.equal(commands, 1);
130
152
  assert.equal(await readFile(path, "utf8"), "saved\n");
131
- assert.ok(result.details.actionFusion);
132
- assert.equal(result.details.actionFusion.publication, "PUBLISHED");
133
- assert.equal(result.details.actionFusion.command, "succeeded");
153
+ assert.equal(reportOf(result, "write")?.publication, "PUBLISHED");
154
+ assert.equal(reportOf(result, "write")?.then_run?.status, "succeeded");
134
155
  assert.match(text(result), /display unavailable/);
135
156
  }
136
157
  const fusion = createActionFusionExecutor(undefined, () => {
@@ -138,6 +159,7 @@ test("progress callback failures preserve publication and do not prevent the com
138
159
  });
139
160
  await assert.rejects(
140
161
  fusion({
162
+ budget: DEFAULT_OUTPUT_BUDGETS,
141
163
  toolCallId: "failed",
142
164
  absolutePath: join(dir, "failed"),
143
165
  tool: "write",
@@ -156,59 +178,84 @@ test("progress callback failures preserve publication and do not prevent the com
156
178
  }),
157
179
  (error: unknown) =>
158
180
  error instanceof CommandNotRun &&
159
- error.error instanceof FileMutationError &&
160
- error.error.publication === "PUBLISHED" &&
161
- /original failure/.test(error.error.message),
181
+ error.cause instanceof FileMutationError &&
182
+ error.cause.publication === "PUBLISHED" &&
183
+ /original failure/.test(error.cause.message),
162
184
  );
163
185
  } finally {
164
186
  await rm(dir, { recursive: true, force: true });
165
187
  }
166
188
  });
167
189
 
168
- test("standalone and Fusion finalizers → take freshness from commit facts", async (t) => {
190
+ test("standalone and Fusion finalizers → take publication and freshness from commit facts, independent of diff", async (t) => {
169
191
  const dir = await mkdtemp(join(tmpdir(), "hashline-finalizer-"));
170
192
  try {
193
+ const path = join(dir, "file");
194
+ const context = await ctx(dir, t);
195
+ const publishedRevision = byteRevision(Buffer.from("published\n"));
171
196
  for (const publication of ["PUBLISHED", "NOT_PUBLISHED"] as const) {
172
- for (const observedRevision of ["published", "external"]) {
173
- const mutation = {
174
- details: {
175
- publication: "UNKNOWN",
176
- publishedRevision: "ignored",
177
- observedRevision: "ignored",
178
- },
179
- facts: {},
180
- commit: {
181
- publication,
182
- baseRevision: "published",
183
- publishedRevision: "published",
184
- observedRevision,
185
- created: false,
186
- },
187
- } satisfies MutationOutcome<object>;
188
- const fresh = observedRevision === "published";
189
- const observation = commitFreshness(mutation.commit);
190
- assert.equal(observation.freshness, fresh ? "unchanged" : "changed");
191
- const standalone = finalizeMutation("edit", "file", mutation, observation);
192
- const fused = await createActionFusionExecutor()({
193
- tool: "edit",
194
- displayPath: "file",
195
- toolCallId: "no-command",
196
- absolutePath: join(dir, "file"),
197
- thenRun: undefined,
198
- mutate: async () => mutation,
199
- signal: undefined,
200
- ctx: await ctx(dir, t),
201
- });
202
- assert.deepEqual(fused.content, standalone.content);
203
- assert.equal(standalone.details.publication, "UNKNOWN");
204
- assert.equal(reportOf(standalone)?.publication, publication);
205
- assert.equal(reportOf(fused)?.publication, publication);
206
- assert.equal(reportOf(standalone)?.freshness, fresh ? undefined : "changed");
207
- assert.deepEqual(mutation.details, {
208
- publication: "UNKNOWN",
209
- publishedRevision: "ignored",
210
- observedRevision: "ignored",
211
- });
197
+ for (const content of ["published\n", "external\n"]) {
198
+ await writeFile(path, content);
199
+ for (const details of [{}, { diff: "" }]) {
200
+ const expectedDetails = structuredClone(details);
201
+ const mutation = {
202
+ details,
203
+ facts: {},
204
+ commit: {
205
+ publication,
206
+ publishedRevision,
207
+ observedRevision: byteRevision(Buffer.from(content)),
208
+ created: false,
209
+ },
210
+ } satisfies MutationOutcome<object>;
211
+ const fresh = content === "published\n";
212
+ const observation = commitFreshness(mutation.commit);
213
+ assert.equal(observation.freshness, fresh ? "unchanged" : "changed");
214
+ const standalone = finalizeMutation(
215
+ "edit",
216
+ path,
217
+ mutation,
218
+ observation,
219
+ undefined,
220
+ DEFAULT_OUTPUT_BUDGETS,
221
+ );
222
+ assert.equal(reportOf(standalone, "edit")?.publication, publication);
223
+ assert.equal(reportOf(standalone, "edit")?.freshness, fresh ? undefined : "changed");
224
+ for (const thenRun of [undefined, { command: "check" }]) {
225
+ let commands = 0;
226
+ const fused = await createActionFusionExecutor(async () => {
227
+ commands++;
228
+ return {
229
+ status: "succeeded",
230
+ result: { content: [{ type: "text", text: "checked" }] },
231
+ };
232
+ })({
233
+ budget: DEFAULT_OUTPUT_BUDGETS,
234
+ tool: "edit",
235
+ displayPath: path,
236
+ toolCallId: "commit-facts",
237
+ absolutePath: path,
238
+ thenRun,
239
+ mutate: async () => mutation,
240
+ signal: undefined,
241
+ ctx: context,
242
+ });
243
+ if (!thenRun) assert.deepEqual(fused.content, standalone.content);
244
+ assert.equal(fused.content.length, 1);
245
+ const report = reportOf(fused, "edit");
246
+ assert.equal(report?.publication, publication);
247
+ assert.equal(report?.freshness, fresh ? undefined : "changed");
248
+ assert.equal(
249
+ report?.then_run?.status,
250
+ thenRun ? (fresh ? "succeeded" : "skipped") : undefined,
251
+ );
252
+ assert.equal(commands, thenRun && fresh ? 1 : 0);
253
+ for (const [key, value] of Object.entries(expectedDetails))
254
+ assert.deepEqual(Reflect.get(fused.details, key), value);
255
+ assert.equal(await readFile(path, "utf8"), content);
256
+ }
257
+ assert.deepEqual(mutation.details, expectedDetails);
258
+ }
212
259
  }
213
260
  }
214
261
  } finally {
@@ -244,7 +291,7 @@ test("unpaired surrogate arguments are rejected before Fusion", async (t) => {
244
291
  const fusion = createActionFusionExecutor(
245
292
  async () => {
246
293
  commands++;
247
- return { status: "succeeded", output: "done" };
294
+ return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
248
295
  },
249
296
  (event) => events.push(event.command),
250
297
  );
@@ -260,7 +307,7 @@ test("unpaired surrogate arguments are rejected before Fusion", async (t) => {
260
307
  tool: makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
261
308
  args: {
262
309
  path,
263
- replacements: [{ find: "original", replace: "\udfff" }],
310
+ replacements: [{ literal: true, find: "original", replace: "\udfff" }],
264
311
  then_run: { command: "check" },
265
312
  },
266
313
  field: "replacements[0].replace",
@@ -311,7 +358,7 @@ test("ambiguous recovery bounds candidate lists and never claims content identit
311
358
  );
312
359
  assert.equal(failure.isError, true);
313
360
  assertFailureByteBudgets(text(failure));
314
- const report = reportOf(failure);
361
+ const report = reportOf(failure, "edit");
315
362
  assert.ok(
316
363
  report?.failures?.every((entry) => entry.result === "ambiguous" && entry.omittedCandidates),
317
364
  );
@@ -331,7 +378,7 @@ test("NUL arguments are rejected before Fusion for all mutation tools", async (t
331
378
  const fusion = createActionFusionExecutor(
332
379
  async () => {
333
380
  commands++;
334
- return { status: "succeeded", output: "unexpected" };
381
+ return { status: "succeeded", result: { content: [{ type: "text", text: "unexpected" }] } };
335
382
  },
336
383
  (event) => events.push(event.command),
337
384
  );
@@ -345,7 +392,7 @@ test("NUL arguments are rejected before Fusion for all mutation tools", async (t
345
392
  tool: makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
346
393
  args: {
347
394
  path,
348
- replacements: [{ find: "original", replace: "\0" }],
395
+ replacements: [{ literal: true, find: "original", replace: "\0" }],
349
396
  then_run: { command: "check" },
350
397
  },
351
398
  field: "replacements[0].replace",
@@ -384,7 +431,7 @@ test("Fusion skips the command when a replacement produces unencodable text", as
384
431
  const fusion = createActionFusionExecutor(
385
432
  async () => {
386
433
  commands++;
387
- return { status: "succeeded", output: "unexpected" };
434
+ return { status: "succeeded", result: { content: [{ type: "text", text: "unexpected" }] } };
388
435
  },
389
436
  (event) => events.push(event.command),
390
437
  );
@@ -392,15 +439,15 @@ test("Fusion skips the command when a replacement produces unencodable text", as
392
439
  makeReplaceTool(dir, DEFAULT_CONFIG, fusion),
393
440
  {
394
441
  path,
395
- replacements: [{ find: "^.", replace: "x", regex: true }],
442
+ replacements: [{ find: "^.", replace: "x", literal: false }],
396
443
  then_run: { command: "check" },
397
444
  },
398
445
  { ctx: await ctx(dir, t) },
399
446
  );
400
447
  assert.equal(result.isError, true);
401
- assert.equal(reportOf(result)?.error, "INVALID_UNICODE");
402
- assert.equal(reportOf(result)?.publication, "NOT_PUBLISHED");
403
- assert.equal(reportOf(result)?.then_run?.status, "skipped");
448
+ assert.equal(reportOf(result, "replace")?.error, "INVALID_UNICODE");
449
+ assert.equal(reportOf(result, "replace")?.publication, "NOT_PUBLISHED");
450
+ assert.equal(reportOf(result, "replace")?.then_run?.status, "skipped");
404
451
  assert.deepEqual(events, ["waiting", "skipped"]);
405
452
  assert.equal(await readFile(path, "utf8"), before);
406
453
  assert.equal(commands, 0);
@@ -442,7 +489,7 @@ const noOpCases = [
442
489
  },
443
490
  {
444
491
  makeTool: (cwd: string, fusion?: Fusion) => makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
445
- params: { replacements: [{ find: "same", replace: "same" }] },
492
+ params: { replacements: [{ literal: true, find: "same", replace: "same" }] },
446
493
  },
447
494
  { makeTool: makeWriteOverride, params: { content: "same\n", mode: "overwrite" } },
448
495
  ];
@@ -458,7 +505,7 @@ test("all mutation tools succeed without rewriting on no-op, with and without Fu
458
505
  let commands = 0;
459
506
  const fusion = createActionFusionExecutor(async () => {
460
507
  commands++;
461
- return { status: "succeeded", output: "checked" };
508
+ return { status: "succeeded", result: { content: [{ type: "text", text: "checked" }] } };
462
509
  });
463
510
  const tool: any = makeTool(dir, mode === "standalone" ? undefined : fusion);
464
511
  const result = await invoke(
@@ -469,21 +516,14 @@ test("all mutation tools succeed without rewriting on no-op, with and without Fu
469
516
  undefined,
470
517
  await ctx(dir, t),
471
518
  );
472
- assert.equal(reportOf(result)?.publication, "NOT_PUBLISHED");
473
- assert.equal(result.details.publication, "NOT_PUBLISHED");
474
- const revision = byteRevision(Buffer.from("same\n"));
475
- for (const key of ["baseRevision", "publishedRevision", "observedRevision"])
476
- assert.equal(result.details[key], revision);
477
- assert.equal("revision" in result.details, false);
519
+ assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
520
+ assert.equal(reportOf(result, tool.name)?.freshness, undefined);
478
521
  assert.doesNotMatch(text(result), /Updated anchors|\d+#[0-9A-Z]+│/);
479
522
  assert.equal(commands, mode === "command" ? 1 : 0);
480
- if (mode !== "standalone") {
481
- assert.equal(
482
- result.details.actionFusion.command,
483
- mode === "command" ? "succeeded" : "not_requested",
484
- );
485
- assert.equal(result.details.actionFusion.freshness, "unchanged");
486
- }
523
+ assert.equal(
524
+ reportOf(result, tool.name)?.then_run?.status,
525
+ mode === "command" ? "succeeded" : undefined,
526
+ );
487
527
  const after = await stat(path);
488
528
  assert.deepEqual(
489
529
  [after.ino, after.mtimeMs, after.ctimeMs],
@@ -510,7 +550,7 @@ test("no-op Fusion still detects external changes and reports command failures",
510
550
  commands++;
511
551
  if (scenario === "failed") throw new Error("check failed");
512
552
  await writeFile(path, "external\n");
513
- return { status: "succeeded", output: "done" };
553
+ return { status: "succeeded", result: { content: [{ type: "text", text: "done" }] } };
514
554
  },
515
555
  (progress) => {
516
556
  if (
@@ -530,15 +570,15 @@ test("no-op Fusion still detects external changes and reports command failures",
530
570
  undefined,
531
571
  await ctx(dir, t),
532
572
  );
533
- assert.equal(result.details.publication, "NOT_PUBLISHED");
573
+ assert.equal(reportOf(result, tool.name)?.publication, "NOT_PUBLISHED");
534
574
  assert.equal(commands, scenario === "before" ? 0 : 1);
535
575
  assert.equal(
536
- result.details.actionFusion.command,
576
+ reportOf(result, tool.name)?.then_run?.status,
537
577
  scenario === "before" ? "skipped" : scenario === "failed" ? "failed" : "succeeded",
538
578
  );
539
579
  assert.equal(
540
- result.details.actionFusion.freshness,
541
- scenario === "failed" ? "unchanged" : "changed",
580
+ reportOf(result, tool.name)?.freshness,
581
+ scenario === "failed" ? undefined : "changed",
542
582
  );
543
583
  }
544
584
  }
@@ -1,5 +1,8 @@
1
1
  import { test } from "node:test";
2
2
  import assert from "node:assert/strict";
3
+ import childProcess from "node:child_process";
4
+ import { syncBuiltinESMExports } from "node:module";
5
+ import { Readable } from "node:stream";
3
6
  import { recordSearchDiagnostics, runRgLines } from "./rg-process.ts";
4
7
  import { fileURLToPath } from "node:url";
5
8
  import { HashlineError } from "../core/errors.ts";
@@ -42,16 +45,39 @@ async function parseListing(chunks: readonly string[], maxLines = 10, stop = fal
42
45
  return { files, result };
43
46
  }
44
47
 
45
- test("line listing split at every byte → newline paths, empty rows, and grouped files survive", async () => {
46
- const listing =
47
- "dir\nfile.txt\0" + "1:\n2:x\n10:[Omitted long matching line]\n\n" + "other.txt\0" + "3:y\n\n";
48
- const { files, result } = await parseListing([...listing]);
49
- assert.deepEqual(files, [
50
- { path: "dir\nfile.txt", lines: [1, 2, 10] },
51
- { path: "other.txt", lines: [3] },
52
- ]);
53
- assert.equal(result.code, 0);
54
- assert.equal(result.stopped, false);
48
+ test("line listing split at every byte → newline paths, empty rows, and grouped files survive", async (t) => {
49
+ const listing = Buffer.from(
50
+ "dir\n文件.txt\0" + "1:\n2:x\n10:[Omitted long matching line]\n\n" + "other.txt\0" + "3:y\n\n",
51
+ );
52
+ const spawn = childProcess.spawn;
53
+ let chunks: Buffer[] = [];
54
+ try {
55
+ t.mock.method(childProcess, "spawn", (...args: Parameters<typeof spawn>) => {
56
+ const child = spawn(...args);
57
+ assert.ok(child.stdout);
58
+ const stream = Readable.from(chunks);
59
+ t.mock.method(child.stdout, Symbol.asyncIterator, () => stream[Symbol.asyncIterator]());
60
+ return child;
61
+ });
62
+ syncBuiltinESMExports();
63
+ for (let split = 1; split < listing.length; split++) {
64
+ chunks = [listing.subarray(0, split), listing.subarray(split)];
65
+ const { files, result } = await parseListing([]);
66
+ assert.deepEqual(
67
+ files,
68
+ [
69
+ { path: "dir\n文件.txt", lines: [1, 2, 10] },
70
+ { path: "other.txt", lines: [3] },
71
+ ],
72
+ `split=${split}`,
73
+ );
74
+ assert.equal(result.code, 0);
75
+ assert.equal(result.stopped, false);
76
+ }
77
+ } finally {
78
+ t.mock.restoreAll();
79
+ syncBuiltinESMExports();
80
+ }
55
81
  });
56
82
 
57
83
  test("line listing exceeds maxLines → only the overflowing file requests all-line review", async () => {
@@ -12,15 +12,21 @@ import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
12
12
  import type { Readable } from "node:stream";
13
13
  import { HashlineError } from "../core/errors.ts";
14
14
  import { throwIfCancelled } from "./error-text.ts";
15
+ import { formatByteSize } from "./budgets.ts";
15
16
 
16
- export const COMMON_RG_ARGS = ["--no-config", "--color=never"];
17
+ /**
18
+ * Every rg run visits input roots in caller order and sorts each directory's entries.
19
+ * This is deterministic traversal, not global full-path sorting. rg walks in one thread;
20
+ * matching stays parallel in grep-search's workers.
21
+ */
22
+ export const COMMON_RG_ARGS = ["--no-config", "--color=never", "--sort=path"];
17
23
  export const MAX_RG_RECORD_BYTES = 16 * 1024 * 1024;
18
- const RG_RECORD_LIMIT_MESSAGE = `ripgrep output record exceeds ${MAX_RG_RECORD_BYTES / 1024 ** 2} MiB.`;
24
+ const RG_RECORD_LIMIT_MESSAGE = `ripgrep output record exceeds ${formatByteSize(MAX_RG_RECORD_BYTES)}.`;
19
25
  /**
20
26
  * A ripgrep process or output-protocol failure. A finished run contributes its
21
27
  * exit code and, as the cause, its stderr.
22
28
  */
23
- export function ripgrepFailure(
29
+ function ripgrepFailure(
24
30
  message: string,
25
31
  run?: { readonly code: number | null; readonly stderr: string } | { readonly error: unknown },
26
32
  ): HashlineError<"RIPGREP_FAILED"> {
@@ -48,3 +48,20 @@ test("capture disables i inside a caseless reference → retain the reference's
48
48
  ["a[Aa]"],
49
49
  );
50
50
  });
51
+
52
+ test("deeply nested pattern → line candidates retain the native match", () => {
53
+ for (const depth of [50, 200, 500, 1_000, 2_000, 4_000]) {
54
+ const pattern = `${"(?:".repeat(depth)}A${")".repeat(depth)}`;
55
+ assert.equal(new RegExp(pattern).test("A"), true);
56
+ const candidates = lineCandidatePatterns({
57
+ patterns: [pattern],
58
+ literal: false,
59
+ flags: "",
60
+ multiline: false,
61
+ });
62
+ // An unavailable line analysis delegates candidate discovery to the file prefilter.
63
+ assert.ok(
64
+ candidates === undefined || candidates.some((candidate) => new RegExp(candidate).test("A")),
65
+ );
66
+ }
67
+ });