@criogaid/pi-hashline-edit 0.3.1 → 0.5.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 (75) hide show
  1. package/README.md +166 -61
  2. package/package.json +8 -3
  3. package/src/core/apply.test.ts +459 -313
  4. package/src/core/apply.ts +251 -215
  5. package/src/core/errors.ts +9 -0
  6. package/src/core/hash.test.ts +61 -23
  7. package/src/core/hash.ts +49 -20
  8. package/src/core/lines.test.ts +46 -27
  9. package/src/core/lines.ts +36 -28
  10. package/src/core/ranges.test.ts +73 -19
  11. package/src/core/ranges.ts +15 -15
  12. package/src/core/replace.ts +109 -0
  13. package/src/core/text.test.ts +3 -3
  14. package/src/core/text.ts +26 -9
  15. package/src/core/types.ts +56 -41
  16. package/src/index.ts +23 -19
  17. package/src/integration/config-registration.test.ts +206 -0
  18. package/src/integration/grep-rg.test.ts +879 -251
  19. package/src/integration/large-text.test.ts +63 -0
  20. package/src/integration/replace-package.test.ts +41 -0
  21. package/src/pi/action-fusion.test.ts +612 -207
  22. package/src/pi/action-fusion.ts +442 -254
  23. package/src/pi/anchor-format.test.ts +53 -21
  24. package/src/pi/anchor-format.ts +48 -20
  25. package/src/pi/budgets.ts +25 -0
  26. package/src/pi/config.test.ts +178 -35
  27. package/src/pi/config.ts +96 -37
  28. package/src/pi/directory-sync.test.ts +55 -0
  29. package/src/pi/e2e-scenarios.test.ts +360 -0
  30. package/src/pi/edit-tool.ts +307 -279
  31. package/src/pi/error-text.ts +26 -0
  32. package/src/pi/execute.test.ts +1718 -713
  33. package/src/pi/failure-context.test.ts +110 -83
  34. package/src/pi/failure-context.ts +232 -73
  35. package/src/pi/failure-path.test.ts +180 -66
  36. package/src/pi/file-commit.test.ts +446 -233
  37. package/src/pi/file-commit.ts +348 -203
  38. package/src/pi/fusion-card.test.ts +118 -83
  39. package/src/pi/fusion-card.ts +203 -87
  40. package/src/pi/grep-output.ts +225 -0
  41. package/src/pi/grep-render.ts +59 -0
  42. package/src/pi/grep-scope.ts +139 -0
  43. package/src/pi/grep-search.ts +231 -0
  44. package/src/pi/grep-tool.ts +215 -774
  45. package/src/pi/grep.test.ts +825 -376
  46. package/src/pi/mutation-outcome.testing.ts +22 -0
  47. package/src/pi/mutation-result.test.ts +86 -0
  48. package/src/pi/mutation-result.ts +118 -63
  49. package/src/pi/mutation-runner.ts +151 -0
  50. package/src/pi/path.ts +20 -12
  51. package/src/pi/pi.test.ts +100 -52
  52. package/src/pi/read-tool.ts +245 -134
  53. package/src/pi/registration.test.ts +271 -114
  54. package/src/pi/render.test.ts +227 -84
  55. package/src/pi/render.ts +86 -116
  56. package/src/pi/replace-regex.ts +54 -0
  57. package/src/pi/replace-tool.ts +247 -241
  58. package/src/pi/replace-worker.mjs +4 -0
  59. package/src/pi/replace-worker.ts +10 -0
  60. package/src/pi/replace.test.ts +778 -390
  61. package/src/pi/result-safety.test.ts +821 -344
  62. package/src/pi/rg-line-ranges.test.ts +57 -31
  63. package/src/pi/rg-line-ranges.ts +45 -82
  64. package/src/pi/rg-process.test.ts +113 -0
  65. package/src/pi/{rg-line-filter.ts → rg-process.ts} +72 -125
  66. package/src/pi/rg-text-view.ts +102 -48
  67. package/src/pi/schema.ts +19 -0
  68. package/src/pi/text-stream.test.ts +118 -0
  69. package/src/pi/text-stream.ts +136 -0
  70. package/src/pi/tool-call.testing.ts +37 -0
  71. package/src/pi/write-tool.ts +114 -52
  72. package/src/pi/write.test.ts +250 -97
  73. package/src/core/index.ts +0 -14
  74. package/src/pi/rg-line-filter.test.ts +0 -85
  75. package/src/pi/state.ts +0 -26
@@ -3,79 +3,193 @@ import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import assert from "node:assert/strict";
5
5
  import { test } from "node:test";
6
- import { createActionFusionExecutor, ActionFusionError } from "./action-fusion.ts";
7
- import { byteRevision, FileMutationError } from "./file-commit.ts";
6
+ import {
7
+ createActionFusionExecutor,
8
+ ActionFusionError,
9
+ THEN_RUN_STALE,
10
+ THEN_RUN_SUCCEEDED,
11
+ } from "./action-fusion.ts";
12
+ import { FileMutationError } from "./file-commit.ts";
13
+ import { staleTargetNotice } from "./mutation-result.ts";
14
+ import { publishedMutation } from "./mutation-outcome.testing.ts";
8
15
 
9
16
  async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
10
- const dir = await mkdtemp(join(tmpdir(), "hashline-failure-"));
11
- try { return await run(dir); } finally { await rm(dir, { recursive: true, force: true }); }
17
+ const dir = await mkdtemp(join(tmpdir(), "hashline-failure-"));
18
+ try {
19
+ return await run(dir);
20
+ } finally {
21
+ await rm(dir, { recursive: true, force: true });
22
+ }
12
23
  }
13
24
 
14
25
  const context = (cwd: string) => ({ cwd }) as any;
15
- const result = (content: string) => ({ content: [{ type: "text" as const, text: "mutation" }], details: { publication: "PUBLISHED", publishedRevision: byteRevision(content) } });
26
+ const result = (content: string) =>
27
+ publishedMutation(content, {
28
+ content: [{ type: "text", text: "mutation" }],
29
+ details: undefined,
30
+ });
16
31
 
17
- test("published post-processing failure skips command and preserves publication state", async () => withTemp(async (dir) => {
18
- const target = join(dir, "published.txt");
19
- await writeFile(target, "content\n");
20
- let commanded = false;
21
- const updates: any[] = [];
22
- const fusion = createActionFusionExecutor(async () => { commanded = true; return "never"; });
23
- await assert.rejects(
24
- fusion({ toolCallId: "published", absolutePath: target, thenRun: { command: "check" }, mutate: async () => { throw new FileMutationError("post_process", "PUBLISHED", "file was published but result generation failed"); }, signal: undefined, ctx: context(dir), onUpdate: (update) => updates.push(update) }),
25
- (error: unknown) => error instanceof ActionFusionError && error.publication === "PUBLISHED" && error.command === "skipped" && !commanded && /Re-read before retrying/.test(error.message),
26
- );
27
- assert.ok(updates.every((update) => update.details.actionFusion.mutationCompleted === false));
28
- assert.equal(updates.at(-1).details.actionFusion.publication, "PUBLISHED");
29
- }));
32
+ test("published post-processing failure skips command and preserves publication state", async () =>
33
+ withTemp(async (dir) => {
34
+ const target = join(dir, "published.txt");
35
+ await writeFile(target, "content\n");
36
+ let commanded = false;
37
+ const updates: any[] = [];
38
+ const fusion = createActionFusionExecutor(async () => {
39
+ commanded = true;
40
+ return "never";
41
+ });
42
+ await assert.rejects(
43
+ fusion({
44
+ toolCallId: "published",
45
+ absolutePath: target,
46
+ thenRun: { command: "check" },
47
+ mutate: async () => {
48
+ throw new FileMutationError(
49
+ "post_process",
50
+ "PUBLISHED",
51
+ "file was published but result generation failed",
52
+ );
53
+ },
54
+ signal: undefined,
55
+ ctx: context(dir),
56
+ onUpdate: (update) => updates.push(update),
57
+ }),
58
+ (error: unknown) =>
59
+ error instanceof ActionFusionError &&
60
+ error.publication === "PUBLISHED" &&
61
+ error.command === "skipped" &&
62
+ !commanded &&
63
+ /Re-read before retrying/.test(error.message),
64
+ );
65
+ assert.ok(updates.every((update) => update.details.actionFusion.mutationCompleted === false));
66
+ assert.equal(updates.at(-1).details.actionFusion.publication, "PUBLISHED");
67
+ }));
30
68
 
31
- test("command failure still reports final changed freshness", async () => withTemp(async (dir) => {
32
- const target = join(dir, "failed.txt");
33
- await writeFile(target, "before\n");
34
- const fusion = createActionFusionExecutor(async () => { await writeFile(target, "command changed\n"); throw new Error("command failed"); });
35
- const outcome: any = await fusion({ toolCallId: "failed", absolutePath: target, thenRun: { command: "check" }, mutate: async () => { await writeFile(target, "mutation\n"); return result("mutation\n"); }, signal: undefined, ctx: context(dir) });
36
- assert.deepEqual(outcome.details.actionFusion, { publication: "PUBLISHED", command: "failed", freshness: "changed" });
37
- assert.equal((outcome.content[1].text.match(/Re-read/g) ?? []).length, 1);
38
- assert.match(outcome.content[1].text, /then_run:stale/);
39
- }));
69
+ test("command failure still reports final changed freshness", async () =>
70
+ withTemp(async (dir) => {
71
+ const target = join(dir, "failed.txt");
72
+ await writeFile(target, "before\n");
73
+ const fusion = createActionFusionExecutor(async () => {
74
+ await writeFile(target, "command changed\n");
75
+ throw new Error("command failed");
76
+ });
77
+ const outcome: any = await fusion({
78
+ toolCallId: "failed",
79
+ absolutePath: target,
80
+ thenRun: { command: "check" },
81
+ mutate: async () => {
82
+ await writeFile(target, "mutation\n");
83
+ return result("mutation\n");
84
+ },
85
+ signal: undefined,
86
+ ctx: context(dir),
87
+ });
88
+ assert.deepEqual(outcome.details.actionFusion, {
89
+ publication: "PUBLISHED",
90
+ command: "failed",
91
+ freshness: "changed",
92
+ });
93
+ assert.deepEqual(outcome.content[1], { type: "text", text: staleTargetNotice(THEN_RUN_STALE) });
94
+ assert.equal(outcome.content.length, 3);
95
+ assert.match(outcome.content[2].text, /command failed/);
96
+ }));
40
97
 
41
- test("timeout and cancellation do not rerun mutation and still inspect freshness", async () => withTemp(async (dir) => {
42
- const target = join(dir, "cancelled.txt");
43
- await writeFile(target, "before\n");
44
- let mutations = 0;
45
- const controller = new AbortController();
46
- const fusion = createActionFusionExecutor(async () => { controller.abort(); throw new Error("timed out"); });
47
- const outcome: any = await fusion({ toolCallId: "cancelled", absolutePath: target, thenRun: { command: "check" }, mutate: async () => { mutations++; await writeFile(target, "mutation\n"); return result("mutation\n"); }, signal: controller.signal, ctx: context(dir) });
48
- assert.equal(outcome.details.actionFusion.command, "cancelled");
49
- assert.equal(outcome.details.actionFusion.freshness, "unchanged");
50
- assert.equal(mutations, 1);
51
- }));
98
+ test("timeout and cancellation do not rerun mutation and still inspect freshness", async () =>
99
+ withTemp(async (dir) => {
100
+ const target = join(dir, "cancelled.txt");
101
+ await writeFile(target, "before\n");
102
+ let mutations = 0;
103
+ const controller = new AbortController();
104
+ const fusion = createActionFusionExecutor(async () => {
105
+ controller.abort();
106
+ throw new Error("timed out");
107
+ });
108
+ const outcome: any = await fusion({
109
+ toolCallId: "cancelled",
110
+ absolutePath: target,
111
+ thenRun: { command: "check" },
112
+ mutate: async () => {
113
+ mutations++;
114
+ await writeFile(target, "mutation\n");
115
+ return result("mutation\n");
116
+ },
117
+ signal: controller.signal,
118
+ ctx: context(dir),
119
+ });
120
+ assert.equal(outcome.details.actionFusion.command, "cancelled");
121
+ assert.equal(outcome.details.actionFusion.freshness, "unchanged");
122
+ assert.equal(mutations, 1);
123
+ }));
52
124
 
53
- test("queue is released after a failed command", async () => withTemp(async (dir) => {
54
- const target = join(dir, "queue.txt");
55
- await writeFile(target, "before\n");
56
- let calls = 0;
57
- const fusion = createActionFusionExecutor(async () => { calls++; if (calls === 1) throw new Error("first command failed"); return "ok"; });
58
- const first = fusion({ toolCallId: "first", absolutePath: target, thenRun: { command: "fail" }, mutate: async () => { await writeFile(target, "first\n"); return result("first\n"); }, signal: undefined, ctx: context(dir) });
59
- const failed: any = await first;
60
- assert.equal(failed.details.actionFusion.command, "failed");
61
- const second = await fusion({ toolCallId: "second", absolutePath: target, thenRun: { command: "ok" }, mutate: async () => { await writeFile(target, "second\n"); return result("second\n"); }, signal: undefined, ctx: context(dir) });
62
- assert.equal(calls, 2);
63
- assert.match(second.content[1].type === "text" ? second.content[1].text : "", /then_run:succeeded/);
64
- }));
125
+ test("queue is released after a failed command", async () =>
126
+ withTemp(async (dir) => {
127
+ const target = join(dir, "queue.txt");
128
+ await writeFile(target, "before\n");
129
+ let calls = 0;
130
+ const fusion = createActionFusionExecutor(async () => {
131
+ calls++;
132
+ if (calls === 1) throw new Error("first command failed");
133
+ return "ok";
134
+ });
135
+ const first = fusion({
136
+ toolCallId: "first",
137
+ absolutePath: target,
138
+ thenRun: { command: "fail" },
139
+ mutate: async () => {
140
+ await writeFile(target, "first\n");
141
+ return result("first\n");
142
+ },
143
+ signal: undefined,
144
+ ctx: context(dir),
145
+ });
146
+ const failed: any = await first;
147
+ assert.equal(failed.details.actionFusion.command, "failed");
148
+ const second = await fusion({
149
+ toolCallId: "second",
150
+ absolutePath: target,
151
+ thenRun: { command: "ok" },
152
+ mutate: async () => {
153
+ await writeFile(target, "second\n");
154
+ return result("second\n");
155
+ },
156
+ signal: undefined,
157
+ ctx: context(dir),
158
+ });
159
+ assert.equal(calls, 2);
160
+ assert.match(
161
+ second.content[1].type === "text" ? second.content[1].text : "",
162
+ /then_run:succeeded/,
163
+ );
164
+ }));
65
165
 
66
- test("result generation failure cannot overwrite an already successful command", async () => withTemp(async (dir) => {
67
- const target = join(dir, "finalize.txt");
68
- const progress: any[] = [];
69
- const fusion = createActionFusionExecutor(async () => "command finished", (event) => progress.push(event));
70
- await assert.rejects(fusion({
71
- toolCallId: "finalize", absolutePath: target, thenRun: { command: "check" }, signal: undefined, ctx: context(dir),
72
- mutate: async () => { await writeFile(target, "saved\n"); return result("saved\n"); },
73
- finalizeMutation: () => { throw new Error("mutation result failure"); },
74
- }), /mutation result failure/);
75
- assert.equal(progress.filter((event) => event.command === "succeeded").length, 1);
76
- assert.equal(progress.at(-1).publication, "PUBLISHED");
77
- assert.equal(progress.at(-1).freshness, "unchanged");
78
- assert.equal(progress.at(-1).command, "succeeded");
79
- assert.equal(progress.at(-1).output, "command finished");
80
- assert.equal(progress.at(-1).reason, undefined);
81
- }));
166
+ test("successful command finalizes anchors and retains completed progress", async () =>
167
+ withTemp(async (dir) => {
168
+ const target = join(dir, "finalize.txt");
169
+ const progress: any[] = [];
170
+ const fusion = createActionFusionExecutor(
171
+ async () => "command finished",
172
+ (event) => progress.push(event),
173
+ );
174
+ const completed = await fusion({
175
+ toolCallId: "finalize",
176
+ absolutePath: target,
177
+ thenRun: { command: "check" },
178
+ signal: undefined,
179
+ ctx: context(dir),
180
+ mutate: async () => {
181
+ await writeFile(target, "saved\n");
182
+ return { ...result("saved\n"), anchors: " ANCHOR" };
183
+ },
184
+ });
185
+ assert.deepEqual(completed.content, [
186
+ { type: "text", text: "mutation ANCHOR" },
187
+ { type: "text", text: `${THEN_RUN_SUCCEEDED}\ncommand finished` },
188
+ ]);
189
+ assert.equal(progress.filter((event) => event.command === "succeeded").length, 1);
190
+ assert.equal(progress.at(-1).publication, "PUBLISHED");
191
+ assert.equal(progress.at(-1).freshness, "unchanged");
192
+ assert.equal(progress.at(-1).command, "succeeded");
193
+ assert.equal(progress.at(-1).output, "command finished");
194
+ assert.equal(progress.at(-1).reason, undefined);
195
+ }));