Skip to content

perf(cli): defer Playwright and isomorphic-git until a command needs them - #1439

Merged
N0zoM1z0 merged 2 commits into
mainfrom
perf/cli-lazy-browser-scenario
Oct 10, 2026
Merged

N0zoM1z0 merged 2 commits into
mainfrom
perf/cli-lazy-browser-scenario

Conversation

@kaoru0822-kitauji

@kaoru0822-kitauji kaoru0822-kitauji commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

capture-browser-scenario imported its Playwright-backed provider (composition/browserScenario.js → PlaywrightScenarioBrowser → playwright-core) at module scope. Because the CLI registers every command at startup, every rea invocation loaded playwright-core, about 95 ms in a CPU profile, even for commands that never use a browser. The command now imports the composition when it runs. capture-electron-scenario already does this (src/cli/electronCommands.ts). This Playwright improvement applies to CLI command registration. Actual stdio MCP startup still resolves Playwright; loadOptionalObservationProviders invokes its dynamic provider factories during startup.

Agents that drive REA through the CLI pay this startup cost on every call. Related to the responsiveness work in #1065.

A second commit does the same for isomorphic-git. Only import-reference-source uses it, through ReferenceSourceVcsAdapter, but the static import also loaded it and its Buffer polyfills (sha.js, to-buffer, typed-array-buffer, …) in every CLI and MCP process.

Measurements (macOS arm64, Node 26.5, median of 12 runs after 2 warmups, both commits vs main)

Command main This PR
rea --version 477 ms 367 ms (−23%)
rea demangle-swift x 476 ms 368 ms (−23%)
rea list-architectures XLD 940 ms 824 ms (−12%)

Playwright alone accounted for about 95 ms and isomorphic-git for about 17 ms.

Change

  • src/cliBrowserScenarioCommands.ts: dynamic import("./composition/browserScenario.js") after input validation, in place of the static import.
  • src/application/ReferenceSourceVcsAdapter.ts: isomorphic-git is imported inside resolveSourceHead, which runs only when a .git entry exists. Import failures fall into the existing unknown VCS result, the same as unresolvable refs.
  • New tests/boundary/cli/cliStartupImports.test.ts: runs the built CLI with a module.registerHooks resolve hook that reports resolved playwright-core and isomorphic-git modules.
    • It asserts that rea --help resolves neither. This fails on main and passes here.
    • Control cases check that the hook does detect each package when its code actually runs (the browser scenario composition, and reading a directory that contains .git).

No contract, output, or error-shape change. Input validation errors are returned before the import, as before.

Verification

  • npm run test:focused -- tests/boundary/cli/cliStartupImports.test.ts, browserCli.test.ts, scenarioCliInputDiagnostics.test.ts, cliToolCoverage.test.ts, jsonInputStatus.test.ts, tests/evaluation/moduleBoundaries.test.ts, tests/boundary/filesystem/referenceSourceVcs.test.ts, referenceSourceImportStatus.test.ts, referenceSourceUtf8.test.ts: all pass.
  • Real run: rea capture-browser-scenario with headless Brave against a local page returned capture Evidence through the lazily loaded provider.
  • npm run typecheck, oxlint and oxfmt; the pre-push check:fast passed.

No real Hopper/Ghidra verification (not affected).

🤖 Generated with Claude Code

The capture-browser-scenario command imported its Playwright-backed
provider at module scope, so every CLI invocation loaded playwright-core
(about 95 ms of startup) even when no browser was involved. Import the
composition when the command runs, as capture-electron-scenario already
does. Light commands start about 100 ms faster.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

… state

import-reference-source is the only consumer, but the static import made
every CLI and MCP process load isomorphic-git and its Buffer polyfills
(about 17 ms of startup). Extend the startup import test to cover it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji kaoru0822-kitauji changed the title perf(cli): load Playwright only for browser scenario captures perf(cli): defer Playwright and isomorphic-git until a command needs them Oct 10, 2026
@N0zoM1z0

Copy link
Copy Markdown
Collaborator

Independent review of d992433b against frozen latest main ce16f7bc, using Linux / pinned Node 24.18.0 and two CPUs:

  • Actual public CLI module-resolution probes find both optional packages on main --help and neither on this head. Help output is identical.
  • Interleaved --version measurements (two warmups, eight samples per build) have medians 1748.5 ms and 1308.5 ms, a 25.2% reduction in this environment. This supports startup savings; it is not a model-token benchmark.
  • A 79-byte source-owned JavaScript target produces byte-identical 41,859-byte CLI Evidence, including its Evidence ID, and unchanged source bytes. Actual SDK stdio MCP returns the same Evidence ID, exact text/structured parity and a successful follow-up ping.
  • Public CLI Git import preserves the actual committed source HEAD. A real headless Chromium scenario performs click/wait actions, captures DOM/URL and reports terminated owned-process cleanup.
  • All ten adjacent suites pass: 167 tests, two Windows-only skips. These include CLI input diagnostics, Git worktree/detached-HEAD reading, browser CLI parity, module boundaries and optional-provider failure isolation.

The actual stdio probe still resolves Playwright during MCP startup; it skips isomorphic-git. I corrected the description to keep the Playwright savings scoped to CLI registration. Final-head CI is 19/19 successful. No source changes were needed in review.

Thanks for isolating these optional startup dependencies without changing the default response contract.

@N0zoM1z0
N0zoM1z0 merged commit eeb83f6 into main Oct 10, 2026
19 checks passed
@N0zoM1z0
N0zoM1z0 deleted the perf/cli-lazy-browser-scenario branch October 10, 2026 05:16
@CassieFox23

CassieFox23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: This merged while I was testing. So this is a post-merge check on Linux. Every command I tried got faster. Startup dropped about 20% on Node 22, 24 and 26. Memory dropped about 35 MiB. Playwright and Git code now load only on demand. Browser capture still works. This is one box, so take the numbers as rough.

Speed and memory (rea --version, base → head)

Node startup, median of 12 peak RSS, median of 5
22.23.1 1508 → 1181 ms (−22%) 272 → 236 MiB
24.18.0 1269 → 1005 ms (−21%) 338 → 305 MiB
26.11.1 1106 → 879 ms (−21%) 301 → 265 MiB

All eight timed commands got 15% to 22% faster. Load sat near 3, with another job running. This box is slower than the PR's Mac. The share saved is close.

What loads (rea --help, same on all three Nodes)

base head
modules resolved 1642 1268
playwright-core files 279 0
isomorphic-git files 2 0

Head still loads each one when a command needs it.

Nothing broke

Check 22 24 26
verify:browser, head, Chromium 154 - pass -
Real capture-browser-scenario, head - pass -
import-reference-source, six .git layouts same - -
PR body's nine test files, head - pass -
New test plus touched tests, head pass pass pass
New test on base fails fails fails

The base failure is the point. It sees both packages at startup.

Not run

  • list-architectures XLD needs macOS and XLD. I timed it here only as a fast error.
  • demangle-swift x also errors here, on both sides. It still measures startup.
  • I did not time rea mcp startup.
  • I ran verify:browser on head and Node 24 only.
  • I did not run the full test suite.

Thanks, kaoru0822-kitauji, for a small change with a clear test.

Check details
  • verify:browser reports "verified": true. It drives capture-browser-scenario through the CLI.
  • The hand-run capture finished all steps on base and head. Head loaded 279 Playwright files for it.
  • The six .git layouts gave the same result on base and head. They cover a worktree pointer, a detached HEAD, an empty folder and a bad pointer.
  • The nine files gave 161 passed and 2 skipped. The skips are Windows only.
  • On Node 22 and 26 I ran four files. That was 9 passed and 2 skipped.
  • On base the new test fails on one case. It expected an empty set. It got isomorphic-git and playwright-core.
  • With a real .git, head loads 79 more modules. So the Git code is deferred, not dropped.
All startup medians (ms, min to max in brackets)

Load average read 2.5 to 3.7 at the start and end of each batch. Another job was running on the box.

Node command base head change
22 --version 1508 (1474–1633) 1181 (1147–1217) −21.6%
22 demangle-swift x 1508 (1452–1591) 1174 (1127–1268) −22.2%
22 list-architectures XLD 1520 (1466–1666) 1227 (1155–1281) −19.3%
22 --help 1484 (1434–1522) 1201 (1129–1296) −19.1%
22 analyze-javascript-application 1547 (1498–1636) 1227 (1197–1306) −20.7%
22 inspect-artifact 1489 (1463–1556) 1195 (1164–1288) −19.7%
22 import-reference-source (git) 1493 (1437–1538) 1213 (1189–1317) −18.8%
22 import-reference-source (no git) 1479 (1434–1690) 1193 (1126–1549) −19.3%
24 --version 1269 (1193–1408) 1005 (960–1060) −20.8%
24 demangle-swift x 1216 (1140–1346) 1004 (942–1043) −17.4%
24 list-architectures XLD 1314 (1256–1355) 1069 (1003–1101) −18.6%
24 --help 1224 (1182–1283) 977 (943–1035) −20.2%
24 analyze-javascript-application 1251 (1231–1333) 1040 (1002–1085) −16.9%
24 inspect-artifact 1251 (1195–1349) 1029 (973–1099) −17.8%
24 import-reference-source (git) 1208 (1180–1288) 1022 (979–1048) −15.4%
24 import-reference-source (no git) 1229 (1183–1337) 1017 (957–1071) −17.3%
26 --version 1106 (1072–1124) 879 (847–916) −20.5%
26 demangle-swift x 1111 (1076–1128) 874 (857–894) −21.3%
26 list-architectures XLD 1105 (1063–1221) 875 (844–955) −20.8%
26 --help 1061 (1046–1188) 864 (829–953) −18.6%
26 analyze-javascript-application 1135 (1081–1234) 898 (870–960) −20.9%
26 inspect-artifact 1155 (1095–1198) 910 (874–1005) −21.2%
26 import-reference-source (git) 1107 (1071–1128) 896 (879–955) −19.1%
26 import-reference-source (no git) 1068 (1050–1144) 852 (840–876) −20.2%
All peak memory medians (MiB, min to max in brackets)
Node command base head change
22 --version 272.4 (267.4–273.4) 235.5 (232.8–239.8) −36.9
22 --help 270.4 (267.2–271.7) 234.7 (232.2–237.2) −35.7
22 analyze-javascript-application 278.1 (275.4–279.5) 246.5 (242.9–250.6) −31.6
22 import-reference-source (git) 271.6 (271.0–274.2) 243.2 (240.0–244.7) −28.4
24 --version 337.9 (335.9–338.3) 304.5 (304.1–306.6) −33.4
24 --help 337.5 (336.2–338.8) 305.0 (304.7–305.3) −32.4
24 analyze-javascript-application 361.3 (342.0–362.8) 321.0 (317.4–321.3) −40.3
24 import-reference-source (git) 340.7 (336.0–341.0) 309.1 (308.3–315.0) −31.6
26 --version 301.1 (300.0–301.6) 264.5 (263.8–264.6) −36.6
26 --help 300.2 (299.5–301.1) 262.9 (260.7–263.8) −37.4
26 analyze-javascript-application 302.4 (301.0–303.7) 274.6 (268.2–276.2) −27.9
26 import-reference-source (git) 301.4 (299.2–302.3) 263.8 (263.4–266.2) −37.6
Commands

Timing: two warm-up pairs, then 12 pairs. Base and head swap order each pair. Exit codes matched on every run.

nice -n 10 taskset -c 2,3 node scripts/rea.mjs <args> >/dev/null 2>&1

Memory: five pairs, not pinned.

/usr/bin/time -v node scripts/rea.mjs <args>   # "Maximum resident set size"

Module log: a module.registerHooks resolve hook that writes each resolved URL.

node --import=./hook.mjs scripts/rea.mjs --help

Targets: a two-file JS app, one small JS file, and folders with hand-made .git layouts.

Browser check:

dbus-run-session -- env -i PATH=... HOME=<scratch> TMPDIR=/tmp/<short> \
  REA_BROWSER_EXECUTABLE=/usr/bin/chromium-browser npm run verify:browser

Tests:

npm run test:focused -- <the nine files from the PR body>
node scripts/run-development-tests.mjs focused tests/boundary/cli/cliStartupImports.test.ts ...
Test setup

🤖 Generated with Claude Code (Opus 5.5) 🦊

@morluto morluto added performance Performance regressions, scaling limits, and bounded-resource work area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
@morluto morluto added the area: cli CLI adapter and one-shot commands label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli CLI adapter and one-shot commands area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior performance Performance regressions, scaling limits, and bounded-resource work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants