@criogaid/pi-hashline-edit 0.6.0 → 0.7.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 (148) hide show
  1. package/README.md +161 -304
  2. package/THIRD_PARTY_NOTICES.md +80 -0
  3. package/package.json +10 -6
  4. package/src/core/AGENTS.md +20 -0
  5. package/src/core/apply.test.ts +189 -60
  6. package/src/core/apply.ts +110 -74
  7. package/src/core/case-fold.test.ts +40 -0
  8. package/src/core/case-fold.ts +81 -0
  9. package/src/core/character-set.test.ts +103 -0
  10. package/src/core/character-set.ts +170 -0
  11. package/src/core/errors.test.ts +23 -0
  12. package/src/core/errors.ts +70 -4
  13. package/src/core/lines.ts +7 -0
  14. package/src/core/regex.ts +21 -0
  15. package/src/core/replace-schema.ts +34 -0
  16. package/src/core/replace.ts +37 -54
  17. package/src/core/search-analysis.test.ts +199 -0
  18. package/src/core/search-analysis.ts +375 -0
  19. package/src/core/search-matrix.test.ts +64 -0
  20. package/src/core/search-matrix.testing.ts +183 -0
  21. package/src/core/search-schema.ts +18 -0
  22. package/src/core/search.test.ts +145 -0
  23. package/src/core/search.ts +119 -0
  24. package/src/core/text.test.ts +16 -8
  25. package/src/core/text.ts +26 -6
  26. package/src/core/types.ts +16 -11
  27. package/src/index.ts +25 -15
  28. package/src/integration/config-registration.test.ts +30 -24
  29. package/src/integration/forget-grep.test.ts +52 -2
  30. package/src/integration/fusion-dispatch.test.ts +21 -105
  31. package/src/integration/grep-errors.test.ts +50 -0
  32. package/src/integration/grep-flags.test.ts +280 -0
  33. package/src/integration/grep-js-completeness.test.ts +208 -0
  34. package/src/integration/grep-line-candidates.test.ts +233 -0
  35. package/src/integration/grep-matrix.test.ts +121 -0
  36. package/src/integration/grep-path-matrix.test.ts +132 -0
  37. package/src/integration/grep-prefilter-coverage.test.ts +189 -0
  38. package/src/integration/grep-regex.test.ts +410 -0
  39. package/src/integration/grep-rg.test.ts +220 -402
  40. package/src/integration/grep-scan-budget.test.ts +89 -0
  41. package/src/integration/grep-translation.test.ts +206 -0
  42. package/src/integration/large-text.test.ts +57 -1
  43. package/src/integration/model-budget-lifecycle.test.ts +128 -0
  44. package/src/integration/mutation-file-types.test.ts +12 -25
  45. package/src/integration/mutation-file-types.testing.ts +33 -0
  46. package/src/integration/npm-package.test.ts +17 -0
  47. package/src/integration/npm-package.testing.ts +123 -0
  48. package/src/integration/publication-read-changes.test.ts +344 -0
  49. package/src/integration/read-changes.test.ts +98 -0
  50. package/src/integration/replace-package.test.ts +17 -36
  51. package/src/integration/replace-package.testing.ts +33 -0
  52. package/src/integration/replace-worker-boundary.test.ts +107 -0
  53. package/src/integration/report-replay.test.ts +90 -0
  54. package/src/integration/utf8-errors.test.ts +46 -0
  55. package/src/pi/AGENTS.md +55 -0
  56. package/src/pi/action-fusion.test.ts +253 -103
  57. package/src/pi/action-fusion.ts +116 -290
  58. package/src/pi/anchor-format.ts +9 -0
  59. package/src/pi/argument-diagnostics.ts +478 -0
  60. package/src/pi/argument-error.testing.ts +43 -0
  61. package/src/pi/argument-error.ts +78 -0
  62. package/src/pi/argument-format.test.ts +208 -0
  63. package/src/pi/argument-validation.test.ts +742 -45
  64. package/src/pi/argument-validation.ts +56 -59
  65. package/src/pi/budgets.ts +45 -16
  66. package/src/pi/codemode.test.ts +96 -0
  67. package/src/pi/config.test.ts +14 -10
  68. package/src/pi/config.ts +21 -7
  69. package/src/pi/diagnostic-buffer.test.ts +52 -16
  70. package/src/pi/diagnostic-buffer.ts +11 -14
  71. package/src/pi/directory-sync.test.ts +4 -1
  72. package/src/pi/edit-tool.ts +100 -93
  73. package/src/pi/error-text.ts +39 -16
  74. package/src/pi/execute.test.ts +362 -315
  75. package/src/pi/extension-loading.test.ts +14 -0
  76. package/src/pi/extension-loading.testing.ts +3 -0
  77. package/src/pi/failure-context.test.ts +220 -88
  78. package/src/pi/failure-context.ts +161 -180
  79. package/src/pi/failure-path.test.ts +85 -88
  80. package/src/pi/file-commit.test.ts +78 -45
  81. package/src/pi/file-commit.ts +218 -151
  82. package/src/pi/file-read.test.ts +29 -0
  83. package/src/pi/file-read.ts +60 -0
  84. package/src/pi/file-snapshot.test.ts +1 -1
  85. package/src/pi/forget-tool.ts +189 -90
  86. package/src/pi/forget.test.ts +309 -22
  87. package/src/pi/forget.testing.ts +41 -113
  88. package/src/pi/fusion-card.test.ts +40 -14
  89. package/src/pi/fusion-card.ts +84 -50
  90. package/src/pi/grep-candidates.test.ts +127 -0
  91. package/src/pi/grep-candidates.ts +193 -0
  92. package/src/pi/grep-matrix.test.ts +83 -0
  93. package/src/pi/grep-output.ts +181 -138
  94. package/src/pi/grep-render.ts +28 -25
  95. package/src/pi/grep-scope.ts +61 -53
  96. package/src/pi/grep-search.ts +299 -242
  97. package/src/pi/grep-tool.ts +150 -140
  98. package/src/pi/grep-worker.mjs +4 -0
  99. package/src/pi/grep-worker.ts +14 -0
  100. package/src/pi/grep.test.ts +631 -632
  101. package/src/pi/model-budgets.test.ts +43 -0
  102. package/src/pi/model-budgets.ts +44 -0
  103. package/src/pi/mutation-outcome.testing.ts +5 -5
  104. package/src/pi/mutation-result.test.ts +21 -74
  105. package/src/pi/mutation-result.ts +74 -87
  106. package/src/pi/mutation-runner.ts +83 -75
  107. package/src/pi/output-budgets.test.ts +166 -0
  108. package/src/pi/path.ts +7 -0
  109. package/src/pi/read-tool.ts +151 -137
  110. package/src/pi/read.test.ts +39 -33
  111. package/src/pi/regex-worker-contract.ts +73 -0
  112. package/src/pi/regex-worker.ts +101 -0
  113. package/src/pi/registration.test.ts +111 -92
  114. package/src/pi/render.test.ts +193 -19
  115. package/src/pi/render.ts +137 -29
  116. package/src/pi/replace-regex.ts +11 -44
  117. package/src/pi/replace-tool.ts +38 -145
  118. package/src/pi/replace-worker-contract.ts +19 -0
  119. package/src/pi/replace-worker.ts +12 -8
  120. package/src/pi/replace.test.ts +93 -160
  121. package/src/pi/report-cause.ts +99 -0
  122. package/src/pi/report-failure.testing.ts +20 -0
  123. package/src/pi/report-schema.ts +512 -0
  124. package/src/pi/report.test.ts +276 -0
  125. package/src/pi/report.ts +288 -0
  126. package/src/pi/result-safety.test.ts +122 -427
  127. package/src/pi/rg-process.test.ts +78 -79
  128. package/src/pi/rg-process.ts +124 -177
  129. package/src/pi/rg-regex.test.ts +50 -0
  130. package/src/pi/rg-regex.ts +709 -0
  131. package/src/pi/text-stream.test.ts +67 -38
  132. package/src/pi/text-stream.ts +96 -75
  133. package/src/pi/tool-call.testing.ts +3 -39
  134. package/src/pi/write-tool.ts +45 -34
  135. package/src/pi/write.test.ts +45 -37
  136. package/src/testing/extension-loader.testing.ts +24 -0
  137. package/src/testing/installation.testing.ts +77 -0
  138. package/src/testing/process.testing.ts +33 -0
  139. package/src/testing/regex-host.testing.ts +20 -0
  140. package/src/testing/regex-translation-corpus.ts +308 -0
  141. package/src/testing/rg-line-listing.fixture.mjs +7 -0
  142. package/src/testing/run-tests.ts +47 -0
  143. package/src/testing/session.testing.ts +127 -0
  144. package/src/pi/rg-line-ranges.test.ts +0 -66
  145. package/src/pi/rg-line-ranges.ts +0 -75
  146. package/src/pi/rg-match-bytes.ts +0 -95
  147. package/src/pi/rg-text-view.ts +0 -192
  148. package/src/pi/tool-prompts.ts +0 -15
@@ -1,13 +1,18 @@
1
+ import { rejectsArgument } from "./argument-error.testing.ts";
1
2
  import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2
3
  import { tmpdir } from "node:os";
3
4
  import { join } from "node:path";
4
5
  import assert from "node:assert/strict";
5
6
  import { test } from "node:test";
6
7
  import { makeWriteOverride } from "./write-tool.ts";
7
- import { createActionFusionExecutor, THEN_RUN_STALE, THEN_RUN_SUCCEEDED } from "./action-fusion.ts";
8
+ import { createActionFusionExecutor } from "./action-fusion.ts";
8
9
  import { fileRevision } from "./file-commit.ts";
9
10
  import { callTool } from "./tool-call.testing.ts";
10
- import { staleTargetNotice } from "./mutation-result.ts";
11
+ import { reportOf } from "./report.ts";
12
+ import { REREAD } from "./report-schema.ts";
13
+ import { initTheme } from "@earendil-works/pi-coding-agent";
14
+ import { theme } from "../../node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/theme/theme.js";
15
+ initTheme("dark");
11
16
 
12
17
  const context = (cwd: string) => ({ cwd }) as any;
13
18
 
@@ -39,7 +44,7 @@ test("write rejects unknown fields rather than ignoring a misspelled create mode
39
44
  const args = { path: "file.txt", content: "new\n", modee: "create" };
40
45
  await assert.rejects(
41
46
  callTool(tool, args, { ctx: context(dir) }),
42
- /Validation failed for tool "write":\n {2}- modee: schema is false/,
47
+ rejectsArgument("modee", /not allowed/),
43
48
  );
44
49
  assert.equal(await readFile(file, "utf8"), "original\n");
45
50
  }
@@ -52,8 +57,8 @@ test("write rejects unwritable content before accessing a missing target", async
52
57
  const valid = { path: file, content: "ready\n", mode: "create" };
53
58
  assert.equal(tool.prepareArguments(valid), valid);
54
59
  for (const [content, expected] of [
55
- ["bad\0", /Invalid argument content: UNSUPPORTED_TEXT: NUL bytes are not editable\./],
56
- ["\ud800", /Invalid argument content: INVALID_UNICODE:/],
60
+ ["bad\0", rejectsArgument("content", /NUL/)],
61
+ ["\ud800", rejectsArgument("content", /encoded losslessly/)],
57
62
  ] as const) {
58
63
  await assert.rejects(
59
64
  callTool(tool, { path: file, content, mode: "create" }, { ctx: context(dir) }),
@@ -83,7 +88,7 @@ test("write omits mode → rejects before changing files or running commands", a
83
88
  { path, content: "changed\n", ...(executor ? { then_run: { command: "check" } } : {}) },
84
89
  { ctx: context(dir) },
85
90
  ),
86
- /\n {2}- mode:/,
91
+ rejectsArgument("mode", /required/),
87
92
  );
88
93
  }
89
94
  }
@@ -105,15 +110,21 @@ test("write supports create-only and overwrite-only modes", async () =>
105
110
  assert.doesNotMatch(created.content[0].text, /Revision:|[0-9a-f]{64}/);
106
111
  assert.equal(created.details.publishedRevision, await fileRevision(target));
107
112
  assert.equal("revision" in created.details, false);
108
- await assert.rejects(
109
- callTool(write, { path: "new.txt", content: "bad\n", mode: "create" }, options),
110
- /already exists/,
113
+ const exists = await callTool(
114
+ write,
115
+ { path: "new.txt", content: "bad\n", mode: "create" },
116
+ options,
111
117
  );
118
+ assert.equal(exists.isError, true);
119
+ assert.equal(reportOf(exists)?.error, "TARGET_EXISTS");
112
120
  assert.equal(await readFile(target, "utf8"), "new\n");
113
- await assert.rejects(
114
- callTool(write, { path: "missing.txt", content: "bad\n", mode: "overwrite" }, options),
115
- /does not exist/,
121
+ const missing = await callTool(
122
+ write,
123
+ { path: "missing.txt", content: "bad\n", mode: "overwrite" },
124
+ options,
116
125
  );
126
+ assert.equal(missing.isError, true);
127
+ assert.equal(reportOf(missing)?.error, "PATH_NOT_FOUND");
117
128
  await callTool(write, { path: "new.txt", content: "updated\n", mode: "overwrite" }, options);
118
129
  assert.equal(await readFile(target, "utf8"), "updated\n");
119
130
  }));
@@ -132,7 +143,7 @@ test("write rejects obsolete expectedRevision without overwriting", async () =>
132
143
  const write = makeWriteOverride(dir, fusion);
133
144
  await assert.rejects(
134
145
  callTool(write, params, { ctx: context(dir) }),
135
- /Validation failed for tool "write":\n {2}- expectedRevision: schema is false/,
146
+ rejectsArgument("expectedRevision", /not allowed/),
136
147
  );
137
148
  assert.equal(await readFile(target, "utf8"), "original\n");
138
149
  }
@@ -154,7 +165,10 @@ test("write returns only a summary for empty, short, and long content", async ()
154
165
  undefined,
155
166
  context(dir),
156
167
  );
157
- assert.deepEqual(result.content, [{ type: "text", text: `Created ${path}.` }]);
168
+ assert.equal(reportOf(result)?.created, true);
169
+ assert.equal(reportOf(result)?.path, path);
170
+ assert.equal(reportOf(result)?.publication, "PUBLISHED");
171
+ assert.equal(result.content.length, 1);
158
172
  assert.equal(await readFile(join(dir, path), "utf8"), content);
159
173
  }
160
174
  }));
@@ -178,10 +192,8 @@ test("write returns its summary and command output after an unchanged then_run",
178
192
  undefined,
179
193
  context(dir),
180
194
  );
181
- assert.deepEqual(result.content, [
182
- { type: "text", text: "Created unchanged.txt." },
183
- { type: "text", text: "[then_run:succeeded]\nchecked" },
184
- ]);
195
+ assert.equal(reportOf(result)?.created, true);
196
+ assert.deepEqual(reportOf(result)?.then_run, { status: "succeeded", output: "checked" });
185
197
  assert.equal(result.details.actionFusion.freshness, "unchanged");
186
198
  }));
187
199
 
@@ -205,17 +217,19 @@ test("write reports changed freshness when then_run changes the target", async (
205
217
  );
206
218
  const text = result.content.map((block: any) => block.text).join("\n");
207
219
  assert.doesNotMatch(text, /revision:|[0-9a-f]{64}/i);
208
- assert.deepEqual(result.content, [
209
- { type: "text", text: "Created changed.txt." },
210
- { type: "text", text: staleTargetNotice(THEN_RUN_STALE) },
211
- { type: "text", text: `${THEN_RUN_SUCCEEDED}\nchecked` },
212
- ]);
220
+ assert.equal(reportOf(result)?.created, true);
221
+ assert.equal(reportOf(result)?.freshness, "changed");
222
+ assert.equal(reportOf(result)?.next, REREAD);
223
+ assert.deepEqual(reportOf(result)?.then_run, { status: "succeeded", output: "checked" });
213
224
  assert.doesNotMatch(text, /Fresh anchors:|\b1#[0-9A-Z]+\b/);
214
225
  assert.equal(result.details.actionFusion.freshness, "changed");
215
- assert.deepEqual(
216
- write.renderResult(result, { isPartial: false }, {}, { isError: false }).render(100),
217
- [],
218
- );
226
+ const rendered = write
227
+ .renderResult(result, { isPartial: false }, theme, { isError: false })
228
+ .render(100)
229
+ .join("\n");
230
+ assert.match(rendered, /changed/);
231
+ assert.match(rendered, /Read the file/);
232
+ assert.doesNotMatch(rendered, /Command|checked/);
219
233
  }));
220
234
 
221
235
  test("write reports missing freshness when then_run removes the target", async () =>
@@ -240,7 +254,7 @@ test("write reports missing freshness when then_run removes the target", async (
240
254
  const text = result.content.map((block: any) => block.text).join("\n");
241
255
  assert.ok(result.details.actionFusion);
242
256
  assert.equal(result.details.actionFusion.freshness, "missing");
243
- assert.match(text, /Re-read/);
257
+ assert.equal(reportOf(result)?.next, REREAD);
244
258
  assert.doesNotMatch(text, /Fresh anchors:/);
245
259
  }));
246
260
 
@@ -262,7 +276,7 @@ test("write preserves command failure and changed freshness", async () =>
262
276
  assert.ok(result.details.actionFusion);
263
277
  assert.equal(result.details.actionFusion.freshness, "changed");
264
278
  assert.equal(result.details.actionFusion.command, "failed");
265
- assert.match(text, /Re-read/);
279
+ assert.equal(reportOf(result)?.next, REREAD);
266
280
  assert.doesNotMatch(text, /Fresh anchors:/);
267
281
  }));
268
282
 
@@ -270,14 +284,8 @@ test("write rejects NUL content", async () =>
270
284
  withTemp(async (dir) => {
271
285
  const write = makeWriteOverride(dir);
272
286
  await assert.rejects(
273
- write.execute(
274
- "nul",
275
- { path: "nul.txt", content: "a\0b", mode: "create" },
276
- undefined,
277
- undefined,
278
- context(dir),
279
- ),
280
- /UNSUPPORTED_TEXT/,
287
+ callTool(write, { path: "nul.txt", content: "a\0b", mode: "create" }, { ctx: context(dir) }),
288
+ rejectsArgument("content", /NUL/),
281
289
  );
282
290
  await assert.rejects(readFile(join(dir, "nul.txt")), /ENOENT/);
283
291
  }));
@@ -0,0 +1,24 @@
1
+ /** Use Pi's embedded-runtime module mapping against an isolated extension installation. */
2
+ import assert from "node:assert/strict";
3
+ import { createRequire } from "node:module";
4
+ import type { ExtensionFactory } from "@earendil-works/pi-coding-agent";
5
+ import { createJiti } from "../../node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/jiti-static-loader.js";
6
+ import { VIRTUAL_MODULES } from "../../node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/virtual-modules.js";
7
+
8
+ export async function loadInstalledExtension(entry: string): Promise<ExtensionFactory> {
9
+ const require = createRequire(entry);
10
+ assert.throws(() => require.resolve("@earendil-works/pi-coding-agent"), {
11
+ code: "MODULE_NOT_FOUND",
12
+ });
13
+ // typebox is a host-provided peer: the installed layout must NOT resolve its own
14
+ // copy; jiti's virtualModules supply it instead.
15
+ assert.throws(() => require.resolve("typebox/type"), { code: "MODULE_NOT_FOUND" });
16
+ const jiti = createJiti(import.meta.url, {
17
+ moduleCache: false,
18
+ virtualModules: VIRTUAL_MODULES,
19
+ tryNative: false,
20
+ });
21
+ const factory = await jiti.import<ExtensionFactory>(entry, { default: true });
22
+ assert.equal(typeof factory, "function");
23
+ return factory;
24
+ }
@@ -0,0 +1,77 @@
1
+ /** Isolated installed-package layouts with only npm's locked production dependency tree. */
2
+ import assert from "node:assert/strict";
3
+ import { cp, mkdir, mkdtemp, readdir, rm, stat } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import { dirname, join, relative } from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+ import type { TestContext } from "node:test";
8
+ import manifest from "../../package.json" with { type: "json" };
9
+ import lock from "../../package-lock.json" with { type: "json" };
10
+ import { runTestProcess } from "./process.testing.ts";
11
+
12
+ const repository = fileURLToPath(new URL("../../", import.meta.url));
13
+
14
+ export async function installedExtension(t: TestContext, source: "source" | "packed" = "source") {
15
+ const root = await mkdtemp(join(tmpdir(), "hashline-install-"));
16
+ t.after(() => rm(root, { recursive: true, force: true }));
17
+ const packageDir = join(root, "node_modules", manifest.name);
18
+ const agentDir = join(root, "agent");
19
+ await mkdir(packageDir, { recursive: true });
20
+ await mkdir(agentDir);
21
+ for (const [path, metadata] of Object.entries(lock.packages)) {
22
+ if (
23
+ !path ||
24
+ ("dev" in metadata && metadata.dev) ||
25
+ ("devOptional" in metadata && metadata.devOptional)
26
+ )
27
+ continue;
28
+ const dependency = join(repository, path);
29
+ try {
30
+ await stat(dependency);
31
+ } catch (error) {
32
+ if (
33
+ error instanceof Error &&
34
+ "code" in error &&
35
+ error.code === "ENOENT" &&
36
+ "optional" in metadata &&
37
+ metadata.optional
38
+ )
39
+ continue;
40
+ throw error;
41
+ }
42
+ // Copy instead of linking: dependency resolution must not walk back into the checkout.
43
+ await cp(dependency, join(root, path), {
44
+ recursive: true,
45
+ dereference: true,
46
+ filter: (candidate) => candidate !== join(dependency, "node_modules"),
47
+ });
48
+ }
49
+ if (source === "packed") {
50
+ const npmCli =
51
+ process.env.npm_execpath ??
52
+ join(
53
+ dirname(process.execPath),
54
+ process.platform === "win32"
55
+ ? "node_modules/npm/bin/npm-cli.js"
56
+ : "../lib/node_modules/npm/bin/npm-cli.js",
57
+ );
58
+ await runTestProcess(
59
+ process.execPath,
60
+ [npmCli, "pack", "--ignore-scripts", "--pack-destination", root],
61
+ repository,
62
+ agentDir,
63
+ );
64
+ const archives = (await readdir(root)).filter((name) => name.endsWith(".tgz"));
65
+ assert.equal(archives.length, 1, "npm pack must produce one archive");
66
+ await runTestProcess(
67
+ "tar",
68
+ ["-xzf", archives[0], "-C", relative(root, packageDir), "--strip-components=1"],
69
+ root,
70
+ agentDir,
71
+ );
72
+ } else {
73
+ await cp(join(repository, "src"), join(packageDir, "src"), { recursive: true });
74
+ await cp(join(repository, "package.json"), join(packageDir, "package.json"));
75
+ }
76
+ return { root, packageDir, agentDir, entry: join(packageDir, manifest.pi.extensions[0]) };
77
+ }
@@ -0,0 +1,33 @@
1
+ /** Own the environment used by test runners and isolated package probes. */
2
+ import { execFile as execFileCallback } from "node:child_process";
3
+ import { promisify } from "node:util";
4
+
5
+ const execFile = promisify(execFileCallback);
6
+ const PROCESS_TIMEOUT_MS = 60_000;
7
+
8
+ export function testEnvironment(agentDir: string): NodeJS.ProcessEnv {
9
+ const env = Object.fromEntries(
10
+ Object.entries(process.env).filter(([key]) => {
11
+ const name = key.toUpperCase();
12
+ return (
13
+ !["NODE_OPTIONS", "NODE_PATH", "PI_CODING_AGENT_DIR"].includes(name) &&
14
+ !name.startsWith("JITI_")
15
+ );
16
+ }),
17
+ );
18
+ return { ...env, PI_CODING_AGENT_DIR: agentDir };
19
+ }
20
+
21
+ /** Start a bounded child without shell interpretation or inherited Node loaders. */
22
+ export async function runTestProcess(
23
+ command: string,
24
+ args: readonly string[],
25
+ cwd: string,
26
+ agentDir: string,
27
+ ) {
28
+ return execFile(command, args, {
29
+ cwd,
30
+ env: testEnvironment(agentDir),
31
+ timeout: PROCESS_TIMEOUT_MS,
32
+ });
33
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Host V8 regex support. Pi supports Node 22.19.0, whose V8 rejects modifier groups
3
+ * (`(?i:…)`) and duplicate named groups; grep validates patterns with the host engine, so
4
+ * cases that need this syntax run only where the host compiles them.
5
+ */
6
+ export function hostCompiles(pattern: string, flags = ""): boolean {
7
+ try {
8
+ new RegExp(pattern, flags);
9
+ return true;
10
+ } catch {
11
+ return false;
12
+ }
13
+ }
14
+
15
+ /** Skip reason for `test`/`t.test` options when the host cannot compile every pattern. */
16
+ export function unsupportedByHost(patterns: readonly string[], flags = ""): string | false {
17
+ return patterns.every((pattern) => hostCompiles(pattern, flags))
18
+ ? false
19
+ : `host V8 ${process.version} cannot compile this regex syntax`;
20
+ }
@@ -0,0 +1,308 @@
1
+ // Selected Test262 witnesses retain their upstream copyright and BSD license in THIRD_PARTY_NOTICES.md.
2
+ // Copyright 2022 Igalia S.L. All rights reserved.
3
+ // Copyright (C) 2017 the V8 project authors. All rights reserved.
4
+ // Copyright 2025 Mathias Bynens. All rights reserved.
5
+
6
+ export interface RegexWitness {
7
+ readonly pattern: string;
8
+ readonly flags: string;
9
+ readonly hits: readonly string[];
10
+ readonly misses: readonly string[];
11
+ readonly rejectCandidates: boolean;
12
+ readonly source: string;
13
+ }
14
+
15
+ const test262 = "https://github.com/tc39/test262/blob/main/test/built-ins/RegExp/";
16
+ const local = "JS-to-rg translation contract";
17
+
18
+ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
19
+ {
20
+ pattern: "(?:([])\\1|b)",
21
+ flags: "",
22
+ hits: ["b"],
23
+ misses: ["a"],
24
+ rejectCandidates: true,
25
+ source: local,
26
+ },
27
+ {
28
+ pattern: "(?!a)[a-z]",
29
+ flags: "",
30
+ hits: ["b", "z"],
31
+ misses: ["a", "A"],
32
+ rejectCandidates: true,
33
+ source: local,
34
+ },
35
+ {
36
+ pattern: "(?=ab)a[bcd]",
37
+ flags: "",
38
+ hits: ["ab"],
39
+ misses: ["ac", "ad"],
40
+ rejectCandidates: true,
41
+ source: local,
42
+ },
43
+ {
44
+ pattern: "[a-z](?<=b)",
45
+ flags: "",
46
+ hits: ["b"],
47
+ misses: ["a", "c"],
48
+ rejectCandidates: true,
49
+ source: local,
50
+ },
51
+ {
52
+ pattern: "(?!ab)[a-z]{2}",
53
+ flags: "",
54
+ hits: ["ac", "ba"],
55
+ misses: ["ab"],
56
+ rejectCandidates: true,
57
+ source: local,
58
+ },
59
+ {
60
+ pattern: "[a-z]{2}(?<!ab)",
61
+ flags: "",
62
+ hits: ["ac", "ba"],
63
+ misses: ["ab"],
64
+ rejectCandidates: true,
65
+ source: local,
66
+ },
67
+ {
68
+ pattern: "(?i:(?!k)[a-z])",
69
+ flags: "u",
70
+ hits: ["a", "ſ"],
71
+ misses: ["k", "K", "K"],
72
+ rejectCandidates: true,
73
+ source: local,
74
+ },
75
+ {
76
+ pattern: "a\\b[ a]",
77
+ flags: "",
78
+ hits: ["a "],
79
+ misses: ["aa"],
80
+ rejectCandidates: true,
81
+ source: local,
82
+ },
83
+ {
84
+ pattern: "k\\b[\\w ]",
85
+ flags: "iu",
86
+ hits: ["k ", "K "],
87
+ misses: ["kk", "Kk"],
88
+ rejectCandidates: true,
89
+ source: local,
90
+ },
91
+ {
92
+ pattern: "(?m:[\\ra]^b)",
93
+ flags: "",
94
+ hits: ["\rb"],
95
+ misses: ["ab"],
96
+ rejectCandidates: true,
97
+ source: local,
98
+ },
99
+ {
100
+ pattern: "a^b",
101
+ flags: "",
102
+ hits: [],
103
+ misses: ["ab", "a^b"],
104
+ rejectCandidates: true,
105
+ source: local,
106
+ },
107
+ {
108
+ pattern: "(?=needle)",
109
+ flags: "",
110
+ hits: ["needle"],
111
+ misses: ["haystack"],
112
+ rejectCandidates: true,
113
+ source: local,
114
+ },
115
+ {
116
+ pattern: "(?<=needle)",
117
+ flags: "",
118
+ hits: ["needle"],
119
+ misses: ["haystack"],
120
+ rejectCandidates: true,
121
+ source: local,
122
+ },
123
+ {
124
+ pattern: "(cat|dog)\\1",
125
+ flags: "",
126
+ hits: ["catcat", "dogdog"],
127
+ misses: ["catdog", "dogcat"],
128
+ rejectCandidates: true,
129
+ source: local,
130
+ },
131
+ {
132
+ pattern: "(a|bc)\\1",
133
+ flags: "",
134
+ hits: ["aa", "bcbc"],
135
+ misses: ["abc", "bca"],
136
+ rejectCandidates: true,
137
+ source: local,
138
+ },
139
+ {
140
+ pattern: "(a{1,2})b\\1",
141
+ flags: "",
142
+ hits: ["aba", "aabaa"],
143
+ misses: ["abb"],
144
+ rejectCandidates: true,
145
+ source: local,
146
+ },
147
+ {
148
+ pattern: "(?i:([\\q{ks|ab}]))(?-i:\\1)",
149
+ flags: "v",
150
+ hits: ["KsKs", "abab"],
151
+ misses: ["Ksks", "abAB"],
152
+ rejectCandidates: true,
153
+ source: local,
154
+ },
155
+ {
156
+ pattern: "(a(?=b))b\\1",
157
+ flags: "",
158
+ hits: ["aba"],
159
+ misses: ["abb"],
160
+ rejectCandidates: false,
161
+ source: local,
162
+ },
163
+ {
164
+ pattern: "^(é|Σ|𐐀)$",
165
+ flags: "iu",
166
+ hits: ["É", "é", "ς", "σ", "Σ", "𐐨"],
167
+ misses: ["e", "s", "a"],
168
+ rejectCandidates: true,
169
+ source: local,
170
+ },
171
+ {
172
+ pattern: "^\\p{Letter}+$",
173
+ flags: "u",
174
+ hits: ["é", "Σ", "𐐀"],
175
+ misses: ["123", "😀"],
176
+ rejectCandidates: true,
177
+ source: local,
178
+ },
179
+ {
180
+ pattern: "^[\\p{Letter}--[a-z]]+$",
181
+ flags: "v",
182
+ hits: ["É", "Σ"],
183
+ misses: ["abc", "123"],
184
+ rejectCandidates: true,
185
+ source: local,
186
+ },
187
+ {
188
+ pattern: "^[^\\p{Letter}]$",
189
+ flags: "v",
190
+ hits: ["1", "😀"],
191
+ // Malformed-byte widening may also admit non-ASCII letters as candidates.
192
+ misses: ["a"],
193
+ rejectCandidates: true,
194
+ source: local,
195
+ },
196
+ {
197
+ pattern: `^[\\q{prefix${"a".repeat(300)}suffix|prefix${"b".repeat(300)}suffix}]$`,
198
+ flags: "v",
199
+ hits: [`prefix${"a".repeat(300)}suffix`, `prefix${"b".repeat(300)}suffix`],
200
+ misses: ["wrong", "prefixwrong", "wrongsuffix"],
201
+ rejectCandidates: true,
202
+ source: local,
203
+ },
204
+ {
205
+ pattern: "(?:(?<x>a)|(?<x>b))\\k<x>",
206
+ flags: "",
207
+ hits: ["aa", "bb"],
208
+ misses: ["abab", "cdef"],
209
+ rejectCandidates: false,
210
+ source: `${test262}named-groups/duplicate-names-exec.js`,
211
+ },
212
+ {
213
+ pattern: "(?:(?:(?<x>a)|(?<x>b))\\k<x>){2}",
214
+ flags: "",
215
+ hits: ["aabb"],
216
+ misses: ["abab"],
217
+ rejectCandidates: false,
218
+ source: `${test262}named-groups/duplicate-names-exec.js`,
219
+ },
220
+ {
221
+ pattern: "^(?:(?<a>x)|(?<a>y)|z)\\k<a>$",
222
+ flags: "",
223
+ hits: ["xx", "z"],
224
+ misses: ["zz"],
225
+ rejectCandidates: false,
226
+ source: `${test262}named-groups/duplicate-names-exec.js`,
227
+ },
228
+ {
229
+ pattern: "^(?:(?<a>x)|(?<a>y)|z){2}\\k<a>$",
230
+ flags: "",
231
+ hits: ["xz", "yz"],
232
+ misses: ["xzx", "yzy"],
233
+ rejectCandidates: false,
234
+ source: `${test262}named-groups/duplicate-names-exec.js`,
235
+ },
236
+ {
237
+ pattern: "(.)(?<=(\\1\\1))",
238
+ flags: "i",
239
+ hits: ["abB"],
240
+ misses: ["ab"],
241
+ rejectCandidates: false,
242
+ source: `${test262}lookBehind/back-references.js`,
243
+ },
244
+ {
245
+ pattern: "(..)(?<=\\1\\1\\1)",
246
+ flags: "",
247
+ hits: ["fababab"],
248
+ misses: ["fabxbab", "faxabab"],
249
+ rejectCandidates: false,
250
+ source: `${test262}lookBehind/back-references.js`,
251
+ },
252
+ {
253
+ pattern: "(?<=(.))(\\w+)(?=\\1)",
254
+ flags: "",
255
+ hits: [" 'foo' ", ' "foo" '],
256
+ misses: [' .foo" '],
257
+ rejectCandidates: false,
258
+ source: `${test262}lookBehind/back-references.js`,
259
+ },
260
+ {
261
+ pattern: "(?<=ab(?=c)\\wd)\\w\\w",
262
+ flags: "",
263
+ hits: ["abcdef"],
264
+ misses: [],
265
+ rejectCandidates: false,
266
+ source: `${test262}lookBehind/nested-lookaround.js`,
267
+ },
268
+ {
269
+ pattern: "(?<=a(?=([bc]{2}(?<!a{2}))d)\\w{3})\\w\\w",
270
+ flags: "",
271
+ hits: ["abcdef"],
272
+ misses: [],
273
+ rejectCandidates: false,
274
+ source: `${test262}lookBehind/nested-lookaround.js`,
275
+ },
276
+ {
277
+ pattern: "(?<=a(?=([bc]{2}(?<!a*))d)\\w{3})\\w\\w",
278
+ flags: "",
279
+ hits: [],
280
+ misses: ["abcdef"],
281
+ rejectCandidates: false,
282
+ source: `${test262}lookBehind/nested-lookaround.js`,
283
+ },
284
+ {
285
+ pattern: "(?<!(^|[ab]))\\w{2}",
286
+ flags: "",
287
+ hits: ["abcdef"],
288
+ misses: [],
289
+ rejectCandidates: false,
290
+ source: `${test262}lookBehind/captures-negative.js`,
291
+ },
292
+ {
293
+ pattern: "^[[0-9]--\\p{ASCII_Hex_Digit}]+$",
294
+ flags: "v",
295
+ hits: [],
296
+ misses: ["0", "9", "C", "☃", "𝌆", "🇧🇪"],
297
+ rejectCandidates: true,
298
+ source: `${test262}unicodeSets/generated/character-class-difference-character-property-escape.js`,
299
+ },
300
+ {
301
+ pattern: "^[[0-9]&&[0-9]]+$",
302
+ flags: "v",
303
+ hits: ["0", "9"],
304
+ misses: ["6\uFE0F\u20E3", "C", "☃", "𝌆", "🇧🇪"],
305
+ rejectCandidates: true,
306
+ source: `${test262}unicodeSets/generated/character-class-intersection-character-class.js`,
307
+ },
308
+ ];
@@ -0,0 +1,7 @@
1
+ // Emit protocol bytes in separate writes so parser tests exercise chunk boundaries.
2
+ const chunks = JSON.parse(process.argv[2]);
3
+ for (const chunk of chunks) {
4
+ await new Promise((resolve) => process.stdout.write(Buffer.from(chunk, "base64"), resolve));
5
+ await new Promise((resolve) => setImmediate(resolve));
6
+ }
7
+ if (process.argv[3] === "wait") setInterval(() => {}, 1000);