@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
@@ -0,0 +1,62 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdtemp, open, rm, writeFile } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { makeReadOverride } from "../pi/read-tool.ts";
7
+ import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
8
+ import { computeLineHash } from "../core/hash.ts";
9
+ import { callTool } from "../pi/tool-call.testing.ts";
10
+
11
+ const call = (tool: any, params: any) => callTool(tool, params, { toolCallId: "large-text" });
12
+
13
+ test("read and line-based grep retain anchors on LF and CRLF files over 100 MiB", async () => {
14
+ const directory = await mkdtemp(join(tmpdir(), "hashline-large-text-"));
15
+ try {
16
+ const read = makeReadOverride(directory);
17
+ const grep = makeGrepOverrideWithBackend(directory, {});
18
+ for (const ending of ["\n", "\r\n"]) {
19
+ const path = join(directory, "large.txt");
20
+ const handle = await open(path, "w");
21
+ const line = "x".repeat(1024 - ending.length) + ending;
22
+ const block = Buffer.from(line.repeat(1024));
23
+ try {
24
+ await handle.writeFile("needle" + ending);
25
+ for (let i = 0; i < 101; i++) await handle.writeFile(block);
26
+ await handle.writeFile("tail" + ending);
27
+ } finally {
28
+ await handle.close();
29
+ }
30
+ const first: any = await call(read, { path, limit: 1 });
31
+ assert.match(first.content[0].text, /103426 lines/);
32
+ assert.ok(first.content[0].text.includes(`1#${computeLineHash(1, "needle")}│needle`));
33
+ const last: any = await call(read, { path, offset: 103426, limit: 1 });
34
+ assert.ok(last.content[0].text.includes(`103426#${computeLineHash(103426, "tail")}│tail`));
35
+ for (const modes of [{}, { literal: true }]) {
36
+ const result: any = await call(grep, { path, pattern: "needle", ...modes });
37
+ assert.ok(result.content[0].text.includes(`1#${computeLineHash(1, "needle")}│needle`));
38
+ assert.doesNotMatch(result.content[0].text, /Search incomplete/);
39
+ }
40
+ }
41
+ } finally {
42
+ await rm(directory, { recursive: true, force: true });
43
+ }
44
+ });
45
+
46
+ test("CRLF snapshots normalize terminators split across read chunks and preserve standalone CR", async () => {
47
+ const directory = await mkdtemp(join(tmpdir(), "hashline-snapshot-boundary-"));
48
+ try {
49
+ const path = join(directory, "boundary.txt");
50
+ await writeFile(path, "x".repeat(65535) + "\r\nneedle\r\nstandalone\r");
51
+ const grep = makeGrepOverrideWithBackend(directory, {});
52
+ const result: any = await call(grep, {
53
+ path,
54
+ pattern: ["needle", "standalone\r"],
55
+ literal: true,
56
+ });
57
+ assert.ok(result.content[0].text.includes(`2#${computeLineHash(2, "needle")}│needle`));
58
+ assert.ok(result.content[0].text.includes(`3#${computeLineHash(3, "standalone\r")}│`));
59
+ } finally {
60
+ await rm(directory, { recursive: true, force: true });
61
+ }
62
+ });
@@ -0,0 +1,37 @@
1
+ import assert from "node:assert/strict";
2
+ import { cp, mkdir, mkdtemp, rm } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { test } from "node:test";
5
+ import { Worker } from "node:worker_threads";
6
+
7
+ // Node cannot load TypeScript workers directly from an installed node_modules package.
8
+ test("regex worker starts from an installed-package path", async () => {
9
+ const root = await mkdtemp(join(process.cwd(), "node_modules", "hashline-worker-test-"));
10
+ try {
11
+ const src = join(root, "src");
12
+ await mkdir(join(src, "pi"), { recursive: true });
13
+ await cp(new URL("../core/", import.meta.url), join(src, "core"), { recursive: true });
14
+ for (const name of [
15
+ "replace-worker.mjs",
16
+ "replace-worker.ts",
17
+ "replace-apply.ts",
18
+ "error-text.ts",
19
+ ]) {
20
+ await cp(new URL(`../pi/${name}`, import.meta.url), join(src, "pi", name));
21
+ }
22
+ const worker = new Worker(join(src, "pi", "replace-worker.mjs"), {
23
+ workerData: {
24
+ source: "before\n",
25
+ rules: [{ find: "(before)", replace: "$1 after", regex: true }],
26
+ },
27
+ });
28
+ const result = await new Promise<unknown>((resolve, reject) => {
29
+ worker.once("message", resolve);
30
+ worker.once("error", reject);
31
+ worker.once("exit", (code) => reject(new Error(`Worker exited before responding: ${code}`)));
32
+ });
33
+ assert.deepEqual(result, { result: { text: "before after\n", count: 1 } });
34
+ } finally {
35
+ await rm(root, { recursive: true, force: true });
36
+ }
37
+ });