@criogaid/pi-hashline-edit 0.1.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +279 -0
  3. package/package.json +50 -0
  4. package/src/core/apply.test.ts +327 -0
  5. package/src/core/apply.ts +264 -0
  6. package/src/core/hash.test.ts +54 -0
  7. package/src/core/hash.ts +68 -0
  8. package/src/core/index.ts +13 -0
  9. package/src/core/lines.test.ts +33 -0
  10. package/src/core/lines.ts +52 -0
  11. package/src/core/types.ts +87 -0
  12. package/src/index.ts +44 -0
  13. package/src/integration/grep-rg.test.ts +390 -0
  14. package/src/pi/action-fusion.test.ts +244 -0
  15. package/src/pi/action-fusion.ts +284 -0
  16. package/src/pi/config.test.ts +22 -0
  17. package/src/pi/config.ts +56 -0
  18. package/src/pi/edit-tool.ts +367 -0
  19. package/src/pi/execute.test.ts +521 -0
  20. package/src/pi/failure-context.test.ts +88 -0
  21. package/src/pi/failure-context.ts +98 -0
  22. package/src/pi/failure-path.test.ts +60 -0
  23. package/src/pi/file-commit.test.ts +200 -0
  24. package/src/pi/file-commit.ts +185 -0
  25. package/src/pi/fusion-card.test.ts +69 -0
  26. package/src/pi/fusion-card.ts +78 -0
  27. package/src/pi/grep-tool.ts +795 -0
  28. package/src/pi/grep.test.ts +515 -0
  29. package/src/pi/pi.test.ts +19 -0
  30. package/src/pi/read-tool.ts +178 -0
  31. package/src/pi/registration.test.ts +41 -0
  32. package/src/pi/render.ts +103 -0
  33. package/src/pi/replace-tool.ts +303 -0
  34. package/src/pi/replace.test.ts +287 -0
  35. package/src/pi/rg-line-filter.test.ts +79 -0
  36. package/src/pi/rg-line-filter.ts +358 -0
  37. package/src/pi/rg-line-ranges.test.ts +30 -0
  38. package/src/pi/rg-line-ranges.ts +106 -0
  39. package/src/pi/state.ts +27 -0
  40. package/src/pi/write-tool.ts +83 -0
  41. package/src/pi/write.test.ts +126 -0
@@ -0,0 +1,126 @@
1
+ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import assert from "node:assert/strict";
5
+ import { test } from "node:test";
6
+ import { makeWriteOverride } from "./write-tool.ts";
7
+ import { createActionFusionExecutor } from "./action-fusion.ts";
8
+ import { fileRevision } from "./file-commit.ts";
9
+ import { makeEditOverride } from "./edit-tool.ts";
10
+ import { getState } from "./state.ts";
11
+
12
+ const context = (cwd: string) => ({ cwd }) as any;
13
+
14
+ async function withTemp<T>(run: (dir: string) => Promise<T>): Promise<T> {
15
+ const dir = await mkdtemp(join(tmpdir(), "hashline-write-"));
16
+ try { return await run(dir); } finally { await rm(dir, { recursive: true, force: true }); }
17
+ }
18
+
19
+ test("write schema follows the shared actionFusion switch", () => {
20
+ assert.equal((makeWriteOverride("/tmp") as any).parameters.properties.then_run, undefined);
21
+ const fusion = createActionFusionExecutor();
22
+ assert.ok((makeWriteOverride("/tmp", fusion) as any).parameters.properties.then_run);
23
+ });
24
+
25
+ test("write preserves native default create/overwrite behavior", async () => withTemp(async (dir) => {
26
+ const write = makeWriteOverride(dir) as any;
27
+ const target = join(dir, "file.txt");
28
+ await write.execute("create", { path: "file.txt", content: "one\n" }, undefined, undefined, context(dir));
29
+ assert.equal(await readFile(target, "utf8"), "one\n");
30
+ await write.execute("overwrite", { path: "file.txt", content: "two\n" }, undefined, undefined, context(dir));
31
+ assert.equal(await readFile(target, "utf8"), "two\n");
32
+ }));
33
+
34
+ test("write supports create-only, overwrite-only, and expectedRevision", async () => withTemp(async (dir) => {
35
+ const write = makeWriteOverride(dir) as any;
36
+ await write.execute("create", { path: "new.txt", content: "new\n", mode: "create" }, undefined, undefined, context(dir));
37
+ await assert.rejects(write.execute("create-again", { path: "new.txt", content: "bad\n", mode: "create" }, undefined, undefined, context(dir)), /already exists/);
38
+ await assert.rejects(write.execute("missing-overwrite", { path: "missing.txt", content: "bad\n", mode: "overwrite" }, undefined, undefined, context(dir)), /does not exist/);
39
+ const revision = await fileRevision(join(dir, "new.txt"));
40
+ await write.execute("revision", { path: "new.txt", content: "updated\n", expectedRevision: revision }, undefined, undefined, context(dir));
41
+ await assert.rejects(write.execute("stale", { path: "new.txt", content: "bad\n", expectedRevision: revision }, undefined, undefined, context(dir)), /expectedRevision/);
42
+ assert.equal(await readFile(join(dir, "new.txt"), "utf8"), "updated\n");
43
+ }));
44
+
45
+ test("concurrent writes cannot both consume the same expectedRevision", async () => withTemp(async (dir) => {
46
+ const target = join(dir, "concurrent.txt");
47
+ await writeFile(target, "original\n");
48
+ const expectedRevision = await fileRevision(target);
49
+ const write = makeWriteOverride(dir);
50
+ const results = await Promise.allSettled(["first\n", "second\n"].map((content) =>
51
+ write.execute("concurrent", { path: target, content, expectedRevision }, undefined, undefined, context(dir)),
52
+ ));
53
+ assert.equal(results.filter((result) => result.status === "fulfilled").length, 1);
54
+ const rejected = results.find((result) => result.status === "rejected");
55
+ assert.ok(rejected && rejected.status === "rejected");
56
+ assert.match(rejected.reason.message, /expectedRevision/);
57
+ }));
58
+
59
+ test("write anchors chain into edit with a non-default hash length", async () => withTemp(async (dir) => {
60
+ const state = getState();
61
+ const previousConfig = state.config;
62
+ try {
63
+ state.config = { ...previousConfig, hashLen: 6 };
64
+ const result = await makeWriteOverride(dir).execute("write", { path: "anchors.txt", content: "before\n" }, undefined, undefined, context(dir));
65
+ const anchor = result.content[0].text.match(/1#([0-9A-Z]+)/);
66
+ assert.ok(anchor);
67
+ assert.equal(anchor[1].length, 6);
68
+ assert.doesNotMatch(result.content[0].text, /before|│/);
69
+ await makeEditOverride(dir).execute("edit", { path: "anchors.txt", edits: [{ op: "replace", anchor: anchor[0], body: ["after"] }] }, undefined, undefined, context(dir));
70
+ assert.equal(await readFile(join(dir, "anchors.txt"), "utf8"), "after\n");
71
+ } finally {
72
+ state.config = previousConfig;
73
+ }
74
+ }));
75
+
76
+ test("write returns at most 40 default anchors", async () => withTemp(async (dir) => {
77
+ const content = Array.from({ length: 500 }, (_, index) => `line ${index + 1}`).join("\n");
78
+ const result = await makeWriteOverride(dir).execute("write", { path: "large.txt", content }, undefined, undefined, context(dir));
79
+ assert.equal(result.content[0].text.match(/\b\d+#[0-9A-Z]+\b/g)?.length, 40);
80
+ assert.match(result.content[0].text, /… \(460 more/);
81
+ }));
82
+
83
+ test("write publishes anchors after an unchanged then_run", async () => withTemp(async (dir) => {
84
+ const fusion = createActionFusionExecutor(async () => "checked");
85
+ const write = makeWriteOverride(dir, fusion) as any;
86
+ const result = await write.execute("unchanged", { path: "unchanged.txt", content: "mutation\n", then_run: { command: "check" } }, undefined, undefined, context(dir));
87
+ const text = result.content.map((block: any) => block.text).join("\n");
88
+ assert.match(text, /Revision:[\s\S]*Fresh anchors: 1#[0-9A-Z]+/);
89
+ assert.match(text, /\[then_run:succeeded\]/);
90
+ assert.equal(result.details.actionFusion.freshness, "unchanged");
91
+ }));
92
+
93
+ test("write omits pre-command anchors when then_run changes the target", async () => withTemp(async (dir) => {
94
+ const target = join(dir, "changed.txt");
95
+ const fusion = createActionFusionExecutor(async () => { await writeFile(target, "command changed\n"); return "checked"; });
96
+ const write = makeWriteOverride(dir, fusion) as any;
97
+ const result = await write.execute("changed", { path: "changed.txt", content: "mutation\n", then_run: { command: "check" } }, undefined, undefined, context(dir));
98
+ const text = result.content.map((block: any) => block.text).join("\n");
99
+ assert.match(text, /Mutation revision:/);
100
+ assert.match(text, /Pre-command anchors are omitted/);
101
+ assert.match(text, /mutation revision may not describe the final file/i);
102
+ assert.doesNotMatch(text, /Fresh anchors:|\b1#[0-9A-Z]+\b/);
103
+ assert.equal(result.details.actionFusion.freshness, "changed");
104
+ assert.deepEqual(write.renderResult(result, { isPartial: false }, {}, { isError: false }).render(100), []);
105
+ }));
106
+
107
+ test("write omits anchors when then_run removes the target", async () => withTemp(async (dir) => {
108
+ const target = join(dir, "missing.txt");
109
+ const fusion = createActionFusionExecutor(async () => { await rm(target); return "removed"; });
110
+ const result = await makeWriteOverride(dir, fusion).execute("missing", { path: "missing.txt", content: "mutation\n", then_run: { command: "remove" } }, undefined, undefined, context(dir));
111
+ const text = result.content.map((block: any) => block.text).join("\n");
112
+ assert.equal(result.details.actionFusion.freshness, "missing");
113
+ assert.match(text, /Pre-command anchors are omitted/);
114
+ assert.doesNotMatch(text, /Fresh anchors:/);
115
+ }));
116
+
117
+ test("write omits anchors when a failed then_run changed the target", async () => withTemp(async (dir) => {
118
+ const target = join(dir, "failed.txt");
119
+ const fusion = createActionFusionExecutor(async () => { await writeFile(target, "changed before failure\n"); throw new Error("command failed"); });
120
+ const result = await makeWriteOverride(dir, fusion).execute("failed", { path: "failed.txt", content: "mutation\n", then_run: { command: "fail" } }, undefined, undefined, context(dir));
121
+ const text = result.content.map((block: any) => block.text).join("\n");
122
+ assert.equal(result.details.actionFusion.freshness, "changed");
123
+ assert.equal(result.details.actionFusion.command, "failed");
124
+ assert.match(text, /Pre-command anchors are omitted/);
125
+ assert.doesNotMatch(text, /Fresh anchors:/);
126
+ }));