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