@criogaid/pi-hashline-edit 0.1.3 → 0.3.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 (44) hide show
  1. package/README.md +77 -23
  2. package/package.json +1 -1
  3. package/src/core/apply.test.ts +87 -10
  4. package/src/core/apply.ts +106 -84
  5. package/src/core/lines.test.ts +12 -1
  6. package/src/core/lines.ts +43 -1
  7. package/src/core/ranges.test.ts +28 -0
  8. package/src/core/ranges.ts +25 -0
  9. package/src/core/text.ts +5 -0
  10. package/src/core/types.ts +37 -20
  11. package/src/integration/grep-rg.test.ts +188 -1
  12. package/src/pi/action-fusion.test.ts +3 -1
  13. package/src/pi/action-fusion.ts +38 -35
  14. package/src/pi/anchor-format.test.ts +30 -0
  15. package/src/pi/anchor-format.ts +28 -0
  16. package/src/pi/config.ts +1 -1
  17. package/src/pi/edit-tool.ts +91 -100
  18. package/src/pi/execute.test.ts +239 -14
  19. package/src/pi/failure-context.test.ts +99 -62
  20. package/src/pi/failure-context.ts +67 -64
  21. package/src/pi/failure-path.test.ts +20 -2
  22. package/src/pi/file-commit.test.ts +21 -0
  23. package/src/pi/file-commit.ts +35 -3
  24. package/src/pi/fusion-card.test.ts +38 -1
  25. package/src/pi/fusion-card.ts +41 -17
  26. package/src/pi/grep-tool.ts +84 -55
  27. package/src/pi/grep.test.ts +77 -6
  28. package/src/pi/mutation-result.ts +48 -9
  29. package/src/pi/path.ts +22 -0
  30. package/src/pi/pi.test.ts +64 -2
  31. package/src/pi/read-tool.ts +9 -30
  32. package/src/pi/registration.test.ts +28 -1
  33. package/src/pi/render.test.ts +111 -0
  34. package/src/pi/render.ts +30 -6
  35. package/src/pi/replace-tool.ts +144 -140
  36. package/src/pi/replace.test.ts +193 -1
  37. package/src/pi/result-safety.test.ts +241 -8
  38. package/src/pi/rg-line-filter.test.ts +7 -1
  39. package/src/pi/rg-line-filter.ts +10 -12
  40. package/src/pi/rg-line-ranges.test.ts +10 -0
  41. package/src/pi/rg-line-ranges.ts +21 -15
  42. package/src/pi/rg-text-view.ts +88 -0
  43. package/src/pi/write-tool.ts +13 -47
  44. package/src/pi/write.test.ts +25 -36
@@ -6,8 +6,6 @@ import { test } from "node:test";
6
6
  import { makeWriteOverride } from "./write-tool.ts";
7
7
  import { createActionFusionExecutor } from "./action-fusion.ts";
8
8
  import { fileRevision } from "./file-commit.ts";
9
- import { makeEditOverride } from "./edit-tool.ts";
10
- import { getState } from "./state.ts";
11
9
 
12
10
  const context = (cwd: string) => ({ cwd }) as any;
13
11
 
@@ -25,7 +23,10 @@ test("write schema follows the shared actionFusion switch", () => {
25
23
  test("write preserves native default create/overwrite behavior", async () => withTemp(async (dir) => {
26
24
  const write = makeWriteOverride(dir) as any;
27
25
  const target = join(dir, "file.txt");
28
- await write.execute("create", { path: "file.txt", content: "one\n" }, undefined, undefined, context(dir));
26
+ const created = await write.execute("create", { path: "file.txt", content: "one\n" }, undefined, undefined, context(dir));
27
+ assert.doesNotMatch(created.content[0].text, /Revision:|[0-9a-f]{64}/);
28
+ assert.equal(created.details.publishedRevision, await fileRevision(target));
29
+ assert.equal(created.details.revision, created.details.publishedRevision);
29
30
  assert.equal(await readFile(target, "utf8"), "one\n");
30
31
  await write.execute("overwrite", { path: "file.txt", content: "two\n" }, undefined, undefined, context(dir));
31
32
  assert.equal(await readFile(target, "utf8"), "two\n");
@@ -56,72 +57,60 @@ test("concurrent writes cannot both consume the same expectedRevision", async ()
56
57
  assert.match(rejected.reason.message, /expectedRevision/);
57
58
  }));
58
59
 
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;
60
+ test("write returns only a summary for empty, short, and long content", async () => withTemp(async (dir) => {
61
+ const contents = ["", "before\n", Array.from({ length: 500 }, (_, index) => `line ${index + 1}`).join("\n")];
62
+ for (const [index, content] of contents.entries()) {
63
+ const path = `file-${index}.txt`;
64
+ const result = await makeWriteOverride(dir).execute("write", { path, content }, undefined, undefined, context(dir));
65
+ assert.deepEqual(result.content, [{ type: "text", text: `Created ${path}.` }]);
66
+ assert.equal(await readFile(join(dir, path), "utf8"), content);
73
67
  }
74
68
  }));
75
69
 
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) => {
70
+ test("write returns its summary and command output after an unchanged then_run", async () => withTemp(async (dir) => {
84
71
  const fusion = createActionFusionExecutor(async () => "checked");
85
72
  const write = makeWriteOverride(dir, fusion) as any;
86
73
  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\]/);
74
+ assert.deepEqual(result.content, [
75
+ { type: "text", text: "Created unchanged.txt." },
76
+ { type: "text", text: "[then_run:succeeded]\nchecked" },
77
+ ]);
90
78
  assert.equal(result.details.actionFusion.freshness, "unchanged");
91
79
  }));
92
80
 
93
- test("write omits pre-command anchors when then_run changes the target", async () => withTemp(async (dir) => {
81
+ test("write reports changed freshness when then_run changes the target", async () => withTemp(async (dir) => {
94
82
  const target = join(dir, "changed.txt");
95
83
  const fusion = createActionFusionExecutor(async () => { await writeFile(target, "command changed\n"); return "checked"; });
96
84
  const write = makeWriteOverride(dir, fusion) as any;
97
85
  const result = await write.execute("changed", { path: "changed.txt", content: "mutation\n", then_run: { command: "check" } }, undefined, undefined, context(dir));
98
86
  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, /published revision may not describe the final file/i);
87
+ assert.doesNotMatch(text, /revision:|[0-9a-f]{64}/i);
88
+ assert.doesNotMatch(text, /anchors are omitted/);
89
+ assert.equal((text.match(/Re-read/g) ?? []).length, 1);
90
+ assert.equal((text.match(/\[then_run:stale\]/g) ?? []).length, 1);
102
91
  assert.doesNotMatch(text, /Fresh anchors:|\b1#[0-9A-Z]+\b/);
103
92
  assert.equal(result.details.actionFusion.freshness, "changed");
104
93
  assert.deepEqual(write.renderResult(result, { isPartial: false }, {}, { isError: false }).render(100), []);
105
94
  }));
106
95
 
107
- test("write omits anchors when then_run removes the target", async () => withTemp(async (dir) => {
96
+ test("write reports missing freshness when then_run removes the target", async () => withTemp(async (dir) => {
108
97
  const target = join(dir, "missing.txt");
109
98
  const fusion = createActionFusionExecutor(async () => { await rm(target); return "removed"; });
110
99
  const result = await makeWriteOverride(dir, fusion).execute("missing", { path: "missing.txt", content: "mutation\n", then_run: { command: "remove" } }, undefined, undefined, context(dir));
111
100
  const text = result.content.map((block: any) => block.text).join("\n");
112
101
  assert.equal(result.details.actionFusion.freshness, "missing");
113
- assert.match(text, /Pre-command anchors are omitted/);
102
+ assert.match(text, /Re-read/);
114
103
  assert.doesNotMatch(text, /Fresh anchors:/);
115
104
  }));
116
105
 
117
- test("write omits anchors when a failed then_run changed the target", async () => withTemp(async (dir) => {
106
+ test("write preserves command failure and changed freshness", async () => withTemp(async (dir) => {
118
107
  const target = join(dir, "failed.txt");
119
108
  const fusion = createActionFusionExecutor(async () => { await writeFile(target, "changed before failure\n"); throw new Error("command failed"); });
120
109
  const result = await makeWriteOverride(dir, fusion).execute("failed", { path: "failed.txt", content: "mutation\n", then_run: { command: "fail" } }, undefined, undefined, context(dir));
121
110
  const text = result.content.map((block: any) => block.text).join("\n");
122
111
  assert.equal(result.details.actionFusion.freshness, "changed");
123
112
  assert.equal(result.details.actionFusion.command, "failed");
124
- assert.match(text, /Pre-command anchors are omitted/);
113
+ assert.match(text, /Re-read/);
125
114
  assert.doesNotMatch(text, /Fresh anchors:/);
126
115
  }));
127
116