@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,6 +12,8 @@ import assert from "node:assert/strict";
12
12
  import { test } from "node:test";
13
13
  import registerHashline from "../index.ts";
14
14
  import { initTheme } from "@earendil-works/pi-coding-agent";
15
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
16
+ import { ProcessTerminal, TuiMainScreen } from "@earendil-works/pi-tui";
15
17
  import { ToolExecutionComponent } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/tool-execution.js";
16
18
  import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
17
19
  import { computeLineHash } from "../core/hash.ts";
@@ -19,6 +21,7 @@ import { callTool } from "./tool-call.testing.ts";
19
21
  import type { AgentToolResult } from "@earendil-works/pi-agent-core";
20
22
  import type { ActionFusionProgress } from "./action-fusion.ts";
21
23
  import { reportOf } from "./report.ts";
24
+ import { formatByteSize } from "./budgets.ts";
22
25
  import { expectToolFailure } from "./report-failure.testing.ts";
23
26
 
24
27
  test("mutation cards use Fusion by default and explicit false removes command support", async () => {
@@ -34,8 +37,14 @@ test("mutation cards use Fusion by default and explicit false removes command su
34
37
  const tools: any[] = [];
35
38
  const entries: any[] = [];
36
39
  const renderers = new Map<string, any>();
40
+ let start: ((event: unknown, ctx: unknown) => void) | undefined;
37
41
  registerHashline({
38
- on() {},
42
+ on(event: string, handler: (event: unknown, ctx: unknown) => void) {
43
+ if (event === "session_start") start = handler;
44
+ },
45
+ getSettings() {
46
+ return {};
47
+ },
39
48
  registerEntryRenderer(type: string, renderer: any) {
40
49
  renderers.set(type, renderer);
41
50
  },
@@ -65,8 +74,8 @@ test("mutation cards use Fusion by default and explicit false removes command su
65
74
  ]);
66
75
  assert.equal(grep.parameters.additionalProperties, false);
67
76
  await assert.rejects(
68
- callTool(grep, { pattern: "needle", literal: true, noIgnore: false }),
69
- rejectsArgument("noIgnore", /not allowed/),
77
+ callTool(grep, { pattern: "needle", literal: true, unexpected: true }),
78
+ rejectsArgument("unexpected", /not allowed/),
70
79
  );
71
80
  for (const tool of tools.filter((tool) => ["edit", "replace", "write"].includes(tool.name))) {
72
81
  assert.ok(
@@ -80,28 +89,38 @@ test("mutation cards use Fusion by default and explicit false removes command su
80
89
  );
81
90
  }
82
91
  initTheme("dark");
92
+ const tui = new TuiMainScreen(new ProcessTerminal());
93
+ tui.requestRender = () => {};
94
+ assert.ok(start);
95
+ start(
96
+ {},
97
+ {
98
+ cwd: dir,
99
+ mode: "tui",
100
+ sessionManager: { getBranch: () => entries },
101
+ ui: {
102
+ setWidget(_key: string, content: Parameters<ExtensionContext["ui"]["setWidget"]>[1]) {
103
+ if (typeof content === "function") content(tui, theme);
104
+ },
105
+ },
106
+ },
107
+ );
83
108
  const cases = [
84
109
  { name: "write", args: { content: "published\n", mode: "create" } },
85
110
  { name: "edit", args: { edits: [{ op: "append", body: ["appended"] }] } },
86
- { name: "replace", args: { replacements: [{ find: "published", replace: "published" }] } },
87
- ];
111
+ {
112
+ name: "replace",
113
+ args: { replacements: [{ literal: true, find: "published", replace: "published" }] },
114
+ },
115
+ ] as const;
88
116
  for (const { name, args } of cases) {
89
117
  const tool = tools.find((tool) => tool.name === name);
90
118
  const params = { path: "published.txt", ...args, then_run: { command: "exit 7" } };
91
- const card = new ToolExecutionComponent(
92
- name,
93
- name,
94
- params,
95
- {},
96
- tool,
97
- { requestRender() {} } as any,
98
- dir,
99
- );
119
+ const card = new ToolExecutionComponent(name, name, params, {}, tool, tui, dir);
100
120
  card.setArgsComplete();
101
121
  card.markExecutionStarted();
102
122
  const frames: {
103
123
  command: string;
104
- publication: string;
105
124
  mutationCompleted: boolean;
106
125
  output: string;
107
126
  commandOutput: string;
@@ -127,12 +146,11 @@ test("mutation cards use Fusion by default and explicit false removes command su
127
146
  },
128
147
  ctx: { cwd: dir },
129
148
  });
130
- assert.equal(result.details.actionFusion.command, "failed");
131
149
  assert.equal(
132
- reportOf(result)?.publication,
150
+ reportOf(result, name)?.publication,
133
151
  name === "replace" ? "NOT_PUBLISHED" : "PUBLISHED",
134
152
  );
135
- assert.equal(reportOf(result)?.then_run?.status, "failed");
153
+ assert.equal(reportOf(result, name)?.then_run?.status, "failed");
136
154
  assert.ok(frames[0].output.includes(theme.getBgAnsi("toolPendingBg")));
137
155
  const running = frames.find((frame) => frame.command === "running")!;
138
156
  assert.ok(running, `${name} should emit running progress`);
@@ -158,18 +176,13 @@ test("mutation cards use Fusion by default and explicit false removes command su
158
176
  assert.ok(frame.expandedOutput.includes(expectedBackground));
159
177
  }
160
178
  if (name === "replace") {
161
- assert.equal(running.publication, "NOT_PUBLISHED");
162
- assert.match(running.output, /No net change/);
179
+ assert.match(running.output, /No diff available/);
163
180
  }
164
181
  card.updateResult({ ...result, isError: false });
165
182
  assert.ok(card.render(100).join("\n").includes(theme.getBgAnsi("toolSuccessBg")));
183
+ if (name === "replace") assert.match(card.render(100).join("\n"), /No net change/);
166
184
  card.setExpanded(true);
167
- assert.doesNotMatch(card.render(100).join("\n"), /publication=|freshness=/);
168
- assert.doesNotMatch(card.render(100).join("\n"), /Command exited|then_run:failed/);
169
- assert.doesNotMatch(
170
- frames.at(-1)!.commandOutput,
171
- /publication=|freshness=|File changes are saved|No file changes were published|mutation completed/,
172
- );
185
+ assert.doesNotMatch(card.render(100).join("\n"), /Command exited/);
173
186
  assert.ok(card.render(100).join("\n").includes(theme.getBgAnsi("toolSuccessBg")));
174
187
 
175
188
  const invalid = {
@@ -183,7 +196,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
183
196
  invalid,
184
197
  {},
185
198
  tool,
186
- { requestRender() {} } as any,
199
+ tui,
187
200
  dir,
188
201
  );
189
202
  const failed = await tool.execute(
@@ -195,7 +208,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
195
208
  { cwd: dir },
196
209
  );
197
210
  assert.equal(failed.isError, true);
198
- const report = reportOf(failed);
211
+ const report = reportOf(failed, name);
199
212
  assert.equal(report?.error, "PATH_NOT_FOUND");
200
213
  failedCard.updateResult(failed);
201
214
  const output = failedCard.render(100).join("\n");
@@ -218,7 +231,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
218
231
  .render(160)
219
232
  .join("\n");
220
233
  assert.match(commandOutput, /skipped[\s\S]*Not run.*mutation did not complete/);
221
- assert.doesNotMatch(commandOutput, /Error reading|does not exist|publication=|freshness=/);
234
+ assert.doesNotMatch(commandOutput, /Error reading|does not exist/);
222
235
  assert.ok(!commandOutput.includes(theme.getBgAnsi("toolErrorBg")));
223
236
  }
224
237
  assert.deepEqual(
@@ -240,15 +253,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
240
253
  edits: [{ op: "replace", anchor: `1#${computeLineHash(1, "target", 4)}`, body: ["changed"] }],
241
254
  then_run: { command: "exit 99" },
242
255
  };
243
- const staleCard = new ToolExecutionComponent(
244
- "edit",
245
- "stale",
246
- stale,
247
- {},
248
- edit,
249
- { requestRender() {} } as any,
250
- dir,
251
- );
256
+ const staleCard = new ToolExecutionComponent("edit", "stale", stale, {}, edit, tui, dir);
252
257
  const failed = await edit.execute(
253
258
  "stale",
254
259
  stale,
@@ -258,7 +263,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
258
263
  { cwd: dir },
259
264
  );
260
265
  assert.equal(failed.isError, true);
261
- assert.equal(reportOf(failed)?.failures?.[0].result, "shifted");
266
+ assert.equal(reportOf(failed, "edit")?.failures?.[0].result, "shifted");
262
267
  staleCard.updateResult(failed);
263
268
  assert.match(staleCard.render(160).join("\n"), /ANCHOR_MISMATCH/);
264
269
  const entry = entries.find(
@@ -269,10 +274,7 @@ test("mutation cards use Fusion by default and explicit false removes command su
269
274
  .render(160)
270
275
  .join("\n");
271
276
  assert.match(commandOutput, /skipped/);
272
- assert.doesNotMatch(
273
- commandOutput,
274
- /ANCHOR_MISMATCH|candidate|Input-anchor|publication=|freshness=/,
275
- );
277
+ assert.doesNotMatch(commandOutput, /ANCHOR_MISMATCH|candidate/);
276
278
  assert.equal(await readFile(join(dir, "stale.txt"), "utf8"), "header\ntarget\n");
277
279
  await writeFile(
278
280
  join(dir, ".pi", "settings.json"),
@@ -406,16 +408,19 @@ test("configured read defaults bound omitted limits and returned bytes", async (
406
408
  assert.match(description, /default 2\)/);
407
409
  await writeFile(join(dir, "short.txt"), "a\nb\nc\n");
408
410
  const paged = await call(read, { path: "short.txt" });
409
- assert.deepEqual(paged.details.pagination, { start: 1, end: 2, totalLines: 3, nextOffset: 3 });
410
- assert.match(reportOf(paged)?.next ?? "", /offset.*pagination\.nextOffset/);
411
+ assert.deepEqual(reportOf(paged, "read")?.pagination, {
412
+ start: 1,
413
+ end: 2,
414
+ totalLines: 3,
415
+ nextOffset: 3,
416
+ });
417
+ assert.match(reportOf(paged, "read")?.next ?? "", /offset.*pagination\.nextOffset/);
411
418
  assert.match(paged.content[0].text, /^2#[0-9A-Z]+│b$/m);
412
419
  assert.doesNotMatch(paged.content[0].text, /^3#/m);
413
420
  const explicit = await call(read, { path: "short.txt", limit: 3 });
414
421
  assert.match(explicit.content[0].text, /^3#[0-9A-Z]+│c$/m);
415
422
  await writeFile(join(dir, "wide.txt"), `${"x".repeat(2048)}\n`);
416
423
  const wide = await call(read, { path: "wide.txt" });
417
- assert.equal(wide.details.truncation.firstLineExceedsLimit, true);
418
- assert.equal(wide.details.truncation.outputLines, 0);
419
- assert.equal(wide.details.truncation.maxBytes, 1024);
420
- assert.equal(wide.details.truncation.outputBytes, 0);
424
+ assert.deepEqual(reportOf(wide, "read")?.lineTooLong, { line: 1, limit: formatByteSize(1024) });
425
+ assert.doesNotMatch(wide.content[0].text, /^\d+#/m);
421
426
  }));