regex_match gates fail in the standalone runtime: Cannot find module 're2js'

Merged
AG
2mo ago
+478-10

Fix bundled regex_match gates by embedding the pinned RE2 engine source in the SDK.

Standalone CLI and authored Node bundles previously tried to resolve re2js beside the executing bundle, where no package exists. Lowering now imports a committed compressed CJS source constant, so both bundles can emit self-contained gate commands. The generator checks the CJS engine, compares inflated bytes and version, and preserves an existing equivalent embed. re2js is pinned to 2.8.6 so validation and execution use the same version. Regenerate with npm run gen:re2js --prefix packages/sdk; generation is deliberately separate from build.

Regression coverage includes an unconditional esbuild bundle executed outside the checkout, passing and failing gate verdicts, an installed-engine drift check, and a regex gate in the existing Bun 1.4.0 standalone lifecycle case. Named gates remain inside each producer's spec; the authored journal now includes run-3 and complete-4.

The commands and literal outputs below cover 139 focused tests, 21 standalone/live-journal tests, SDK typechecking/build, and generated-source checks. The full SDK suite was not run. The standalone suite requires Bun exactly 1.4.0: another version fails its hook rather than skipping. Setup transcripts are committed in kernel-build.txt and bun-install.txt.

The unconditional bundle regression was mutation-verified by restoring the original lowering file from HEAD, capturing MODULE_NOT_FOUND, restoring the fixed bytes, and capturing the pass. The two diffs and both outputs are included below. A separate version mutation changes RE2JS_VERSION from 2.8.6 to 2.8.5, captures --check failing, restores the original bytes, and captures success.

Tradeoffs: bundles contain both the validation engine and the compressed execution source. The generated constant is about 81 KB; the journaled command remains about 82 KB, as before. Level 9 compression changes the emitted command bytes (and therefore hashes of specs containing regex gates) relative to the old default compression. Subsequent regeneration preserves compressed bytes when the inflated source and version agree.

CI coverage comes from .github/workflows/cloud-runtime-artifact.yml, whose SDK job runs the whole vitest suite. Its PR paths cover packages/sdk/**, including engine dependency bumps, but a future change only to scripts/generate-re2js-embed.mjs would not trigger it. No workflow files were changed. Implementation stays on the requested current branch; no PR was opened or merged.

check-1.txt
$ cd packages/sdk && npx vitest run tests/named-gate-engine.test.ts tests/named-gate-diagnostics.test.ts tests/artifact-gates.test.ts tests/gate-contract.test.ts tests/preflight.test.ts

 RUN  v2.1.9 /home/daytona/.relayflow-v2-supervisor/durable/repository/packages/sdk

 ✓ tests/named-gate-diagnostics.test.ts (20 tests) 817ms
 ✓ tests/artifact-gates.test.ts (27 tests) 315ms
 ✓ tests/named-gate-engine.test.ts (2 tests) 299ms
 ✓ tests/preflight.test.ts (70 tests) 89ms
 ✓ tests/gate-contract.test.ts (20 tests) 105ms

 Test Files  5 passed (5)
      Tests  139 passed (139)
   Start at  18:18:20
   Duration  3.03s (transform 2.25s, setup 43ms, collect 5.74s, tests 1.63s, environment 1ms, prepare 258ms)


exit: 0
check-2.txt
$ cd packages/sdk && npx tsc --noEmit

exit: 0
check-3.txt
$ cd packages/sdk && npm run build

> @relayflows/sdk@2.0.42 build
> tsc && node scripts/make-cli-executable.mjs


exit: 0
check-4.txt
$ node scripts/generate-re2js-embed.mjs --check
re2js 2.8.6 embed is current

exit: 0
generate-again.txt
$ npm run gen:re2js --prefix packages/sdk

> @relayflows/sdk@2.0.42 gen:re2js
> node ../../scripts/generate-re2js-embed.mjs

re2js 2.8.6 embed is current

exit: 0
live-tests.txt
$ cd packages/sdk && FLOWS_BUILD_BUN=/tmp/regex-gate-bun-1.4.0/node_modules/.bin/bun RELAYFLOWD_BIN=/home/daytona/.relayflows-toolchain/target/2962130851/debug/relayflowd npx vitest run tests/authored-node-runtime.test.ts tests/named-gate-journal.test.ts

 RUN  v2.1.9 /home/daytona/.relayflow-v2-supervisor/durable/repository/packages/sdk

 ✓ tests/named-gate-journal.test.ts (5 tests) 1445ms
   ✓ a lowered subprocess_gate journals the gate command's streams > persists what the gate printed before a timeout killed it 801ms
   ✓ a gate on an agent step > journals the command's streams and surfaces them in the authored failure 450ms
 ✓ tests/authored-node-runtime.test.ts (16 tests) 29692ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > serializes the immutable prepared binding facts through the Node and CLI boundary 1132ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > suppresses the loader warning while preserving authored experimental warnings 1160ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > awaits agent plus three run steps and resumes without repeating effects 1801ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > accepts a predicate-gated flow: the `<step>.gate` child is journaled, verified, and not counted as an authored step 1659ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > resumes a run killed mid-step: the retried child passes the durable result verifier 2142ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > parks an f.human across the IPC boundary, answers it, and resumes the Node body with the answer 2527ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > stops on parent SIGKILL and replays completed children before success 2552ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > stops on parent SIGTERM and replays completed children before success 2467ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > stops on parent blocked-SIGKILL and replays completed children before success 2432ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > stops on parent SIGKILL and replays completed children before declined 2505ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > refuses unawaited rather than reporting terminal success 1250ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > refuses manual then rather than reporting terminal success 1313ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > dispatches the captured child graph even when the source changes before execution 1222ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > rejects a forged result frame without durable completion 1160ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > refuses missing Node before body effects or root admission 500ms
   ✓ Bun 1.4.0 standalone → native Node authored lifecycle > refuses an old Node candidate before body effects 527ms

 Test Files  2 passed (2)
      Tests  21 passed (21)
   Start at  18:19:01
   Duration  30.79s (transform 803ms, setup 33ms, collect 1.94s, tests 31.14s, environment 0ms, prepare 57ms)


exit: 0
mutation-revert.diff
--- fixed/named-gate-lowering.ts
+++ reverted/named-gate-lowering.ts
@@ -1 +1,3 @@
-import { RE2JS_DEFLATED_BASE64 } from './re2js-embedded.js';
+import { readFileSync } from 'node:fs';
+import { createRequire } from 'node:module';
+import { deflateRawSync } from 'node:zlib';
@@ -6,0 +9 @@
+let re2Source: string | undefined;
@@ -10 +13,2 @@
-  return `const re2={};new Function('exports',require('node:zlib').inflateRawSync(Buffer.from(${JSON.stringify(RE2JS_DEFLATED_BASE64)},'base64')).toString())(re2);`;
+  re2Source ??= deflateRawSync(readFileSync(createRequire(import.meta.url).resolve('re2js'))).toString('base64');
+  return `const re2={};new Function('exports',require('node:zlib').inflateRawSync(Buffer.from(${JSON.stringify(re2Source)},'base64')).toString())(re2);`;
mutation-reverted.txt
$ cd packages/sdk && npx vitest run tests/named-gate-engine.test.ts -t "lowers and executes"

 RUN  v2.1.9 /home/daytona/.relayflow-v2-supervisor/durable/repository/packages/sdk

 ❯ tests/named-gate-engine.test.ts (2 tests | 1 failed | 1 skipped) 79ms
   × lowers and executes a bundled regex gate without package resolution 78ms
     → node:internal/modules/cjs/loader:1453
  const err = new Error(message);
              ^

Error: Cannot find module 're2js'
Require stack:
- /tmp/named-gate-engine-cBM0KV/bundle.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1453:15)
    at require.resolve (node:internal/modules/helpers:163:19)
    at embeddedRE2 (file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6331:76)
    at gateCommand (file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6423:17)
    at lowerNamedGates (file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6365:16)
    at file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6474:13
    at ModuleJob.run (node:internal/modules/esm/module_job:430:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:655:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/named-gate-engine-cBM0KV/bundle.mjs' ]
}

Node.js v25.6.0
: expected 1 to be +0 // Object.is equality

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯

 FAIL  tests/named-gate-engine.test.ts > lowers and executes a bundled regex gate without package resolution
AssertionError: node:internal/modules/cjs/loader:1453
  const err = new Error(message);
              ^

Error: Cannot find module 're2js'
Require stack:
- /tmp/named-gate-engine-cBM0KV/bundle.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1453:15)
    at require.resolve (node:internal/modules/helpers:163:19)
    at embeddedRE2 (file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6331:76)
    at gateCommand (file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6423:17)
    at lowerNamedGates (file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6365:16)
    at file:///tmp/named-gate-engine-cBM0KV/bundle.mjs:6474:13
    at ModuleJob.run (node:internal/modules/esm/module_job:430:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:655:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/named-gate-engine-cBM0KV/bundle.mjs' ]
}

Node.js v25.6.0
: expected 1 to be +0 // Object.is equality

- Expected
+ Received

- 0
+ 1

 ❯ embeddedRE2 ../../../../../../../tmp/named-gate-engine-cBM0KV/bundle.mjs:6331:76
 ❯ gateCommand ../../../../../../../tmp/named-gate-engine-cBM0KV/bundle.mjs:6423:17
 ❯ lowerNamedGates ../../../../../../../tmp/named-gate-engine-cBM0KV/bundle.mjs:6365:16
 ❯ ../../../../../../../tmp/named-gate-engine-cBM0KV/bundle.mjs:6474:13
 ❯ tests/named-gate-engine.test.ts:54:44
     52|     const env = { ...process.env, NODE_PATH: '' };
     53|     const lowered = spawnSync(process.execPath, [bundle], { cwd: direc…
     54|     expect(lowered.status, lowered.stderr).toBe(0);
       |                                            ^
     55|     const command = lowered.stdout.trim();
     56|     expect(command).toMatch(/^node -e /);

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  1 failed | 1 skipped (2)
   Start at  18:18:47
   Duration  240ms (transform 36ms, setup 15ms, collect 32ms, tests 79ms, environment 0ms, prepare 32ms)


exit: 1
mutation-restore.diff
--- reverted/named-gate-lowering.ts
+++ restored/named-gate-lowering.ts
@@ -1,3 +1 @@
-import { readFileSync } from 'node:fs';
-import { createRequire } from 'node:module';
-import { deflateRawSync } from 'node:zlib';
+import { RE2JS_DEFLATED_BASE64 } from './re2js-embedded.js';
@@ -9 +6,0 @@
-let re2Source: string | undefined;
@@ -13,2 +10 @@
-  re2Source ??= deflateRawSync(readFileSync(createRequire(import.meta.url).resolve('re2js'))).toString('base64');
-  return `const re2={};new Function('exports',require('node:zlib').inflateRawSync(Buffer.from(${JSON.stringify(re2Source)},'base64')).toString())(re2);`;
+  return `const re2={};new Function('exports',require('node:zlib').inflateRawSync(Buffer.from(${JSON.stringify(RE2JS_DEFLATED_BASE64)},'base64')).toString())(re2);`;
mutation-restored.txt
$ cd packages/sdk && npx vitest run tests/named-gate-engine.test.ts -t "lowers and executes"

 RUN  v2.1.9 /home/daytona/.relayflow-v2-supervisor/durable/repository/packages/sdk

 ✓ tests/named-gate-engine.test.ts (2 tests | 1 skipped) 134ms

 Test Files  1 passed (1)
      Tests  1 passed | 1 skipped (2)
   Start at  18:18:47
   Duration  299ms (transform 38ms, setup 14ms, collect 39ms, tests 134ms, environment 0ms, prepare 30ms)


exit: 0
version-stale.txt
$ node scripts/generate-re2js-embed.mjs --check
re2js embed is stale; run npm run gen:re2js --prefix packages/sdk

exit: 1
version-restored.txt
$ node scripts/generate-re2js-embed.mjs --check
re2js 2.8.6 embed is current

exit: 0

Checks

Relayflow found no way to run this repository's tests — no check command, no CI test job it could follow, no test target and no recognised project file — so no checks ran. Review the change with that in mind.

Fixes #457


[!NOTE]
Medium Risk
Changes how regex gates are lowered and executed in bundled/standalone paths; stale embed or version skew would break gates, and command-byte changes affect specs containing regex gates.

Overview
Fixes regex_match gates in standalone and esbuild-bundled runtimes that previously failed with Cannot find module 're2js' because lowering resolved the package at emit time.

named-gate-lowering now inlines the RE2 engine from a committed re2js-embedded.ts constant (RE2JS_DEFLATED_BASE64 + version) instead of reading node_modules via createRequire. Gate commands stay self-contained node -e snippets that inflate and eval the pinned CJS source.

Adds scripts/generate-re2js-embed.mjs and npm run gen:re2js to regenerate or --check staleness against installed re2js. The dependency is pinned to 2.8.6. Regression tests cover embed drift, a temp-dir esbuild bundle with NODE_PATH cleared, and a regex_match step in the Bun standalone lifecycle fixture (journal expectations updated for a third gated run).

Tradeoff: ~81 KB generated embed in the SDK; level-9 compression can change journaled gate command bytes (and thus hashes) vs the old runtime deflate default.

Reviewed by Cursor Bugbot for commit a0d56a8482ce5c941997b19215ef841a9adaa500. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

Fixes regex_match gates in standalone bundles failing with Cannot find module 're2js' by embedding a committed, compressed copy of the RE2 engine source in the SDK, so both standalone CLI and authored Node bundles are self-contained. Fixes #457.

  • re2js is pinned to exactly 2.8.6; npm run gen:re2js --prefix packages/sdk regenerates the embed, and the generator's --check mode verifies it is current.
  • The embed adds about 81 KB to the emitted command; level-9 compression changes hashes of specs containing regex gates, but regeneration preserves existing compressed bytes when the inflated source and version agree.