Skip to content

perf(cli): decouple lazy CLI startup from AnalysisView, session contracts, and MCP registration - #13435

Merged
morluto merged 2 commits into
mainfrom
perf/cli-startup-decoupling
Nov 22, 2026
Merged

morluto merged 2 commits into
mainfrom
perf/cli-startup-decoupling

Conversation

@morluto

@morluto morluto commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Summary

Eliminates three unnecessary static dependency chains pulled into lazy single-command CLI startups (even when running --help or single-command queries):

  1. Splits nativeTarget (which pulls OPEN_BINARY_TOOL_CONTRACT from sessionToolContracts.ts and transitively the ~1.2–1.3 MB analysisViewResult schema module) out of src/cli/options.ts into src/cli/nativeTargetOption.ts.
  2. Defers AnalysisViewService.js to dynamic import(...) inside src/cli/analysisEvidenceDelivery.ts (when detail === "summary") and src/cli/analysisViewCommands.ts (when running inspect_analysis_view).
  3. Defers mcpRegistration.js to dynamic import(...) in src/cli/cliShell.ts, guarded by a single shared predicate isMcpRegistrationCommand exported from src/cli/mcpBuiltinCommands.ts (also removing the duplicate precedingFlags list).

Addresses #13275.

Problem and expected behavior

Previously, single-command CLI invocations via cliLoader / cliShell still eagerly loaded:

  • sessionToolContracts.js (483.7 KB chunk on baseline; 92 KB after decoupling) and analysisViewResult.js (~1.2–1.3 MB) on every command importing cli/options.ts or contractCommand.ts because options.ts imported OPEN_BINARY_TOOL_CONTRACT for nativeTarget.
  • AnalysisViewService.js (~101 KB) on every command registering via contractCommand.ts because analysisEvidenceDelivery.ts imported inspectAnalysisViewValidated statically even though summary delivery is opt-in.
  • mcpRegistration.js and its client-config registration stack inside cliShell.ts (97.1 KB -> 62.7 KB) even when running unrelated commands.

With this change:

  • All existing CLI command arguments, options, schemas, mcp add, inspect_analysis_view, --detail summary --evidence-output ..., and closeCliResources() behavior remain identical (100% SHA-256 stdout parity across tested commands).
  • Lazy single-command startups that do not define analysisViewResult in their own family contract (such as inspect-uefi-nvram and inspect-sqlite-database) no longer load sessionToolContracts, analysisViewResult, AnalysisViewService, or mcpRegistration, cutting startup wall time roughly in half and peak RSS by ~89 MiB.

Change and scope

  • src/cli/nativeTargetOption.ts: new owner for nativeTarget and NATIVE_OPEN_HELP.
  • src/cli/options.ts: retains only publishArgvInput, cliDecimalField, and cliDecimalShape (bundled options chunk shrinks to 6.0 KB importing only zod and jsonSerialization).
  • src/cli/coreBinaryCommands.ts, src/cli/coreNativeCommands.ts, src/cli/decompileDirectoryCommand.ts: updated nativeTarget imports to ./nativeTargetOption.js.
  • src/cli/analysisEvidenceDelivery.ts: dynamically imports ../application/analysisView/AnalysisViewService.js inside publishDeliveredEvidence only when input.detail === "summary".
  • src/cli/analysisViewCommands.ts: dynamically imports ../application/analysisView/AnalysisViewService.js when running inspect_analysis_view.
  • src/cli/mcpBuiltinCommands.ts, src/cli/mcpRegistration.ts, src/cli/cliShell.ts: unified isMcpRegistrationCommand in mcpBuiltinCommands.ts and dynamically loaded ./mcpRegistration.js from cliShell.ts only for rea mcp add invocations.
  • tests/boundary/cli/cliStartupImports.test.ts: added mcp-registration to DEFERRED_IMPORTS, added HEAVY_SHARED_MODULES (analysis-view-service, analysis-view-result, session-tool-contracts), added positive detection assertions, and verified single-command startup exclusion for inspect-uefi-nvram, inspect-sqlite-database, inspect-wasm-artifact, and guided-workflows.

Not included in this PR: build-time compact JSON Schema generation or splitting APPLICATION_TOOL_CONTRACTS / domain summary schemas out of family *ToolContracts.ts files (tracked in #13275).

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/cli/nativeTargetOption.ts owns the native open-target CLI flag projection; src/cli/mcpBuiltinCommands.ts is the single owner of isMcpRegistrationCommand (replacing duplicate flag lists in mcpRegistration.ts and mcpBuiltinCommands.ts); cliShell.ts and analysisEvidenceDelivery.ts lazily resolve execution-only modules at dispatch/summary time.
  • Contribution scope fit and linked maintainer decision: not applicable (internal CLI module bundling/startup optimization; zero provider or target-interaction changes).
  • CLI and MCP/tool-catalog contract: unchanged (--help stdout SHA-256 identical across all tested commands).
  • Provider, bridge, target-format, or platform compatibility: tested on local macOS arm64 Node v24.22.1; cross-platform CLI boundary checks run in CI.
  • Evidence, artifact, provenance, or reconstruction contract: unchanged.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): mcpRegistration.js still registers closeCliMcpRegistrationResources via ownCliProcessResource when loaded, so closeCliResources() disposes its owned ArtifactResourceScope identically when mcp add runs.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: verified via npm run docs:check (no catalog contract changes).

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated:

    • tests/boundary/cli/cliStartupImports.test.ts (positive triggers for analysis-view-service, mcp-registration, analysis-view-result, session-tool-contracts, plus single-command --help resolution assertions for inspect-uefi-nvram, inspect-sqlite-database, inspect-wasm-artifact, and guided-workflows).
  • Base reproduction or other evidence:
    Measured on compiled dist/cli-runtime (scripts/rea.mjs) in fresh isolated child processes via /usr/bin/time -l (3 runs each: run 1 cold, best of runs 2–3 warm; Mac arm64 Node v24.22.1, baseline origin/main 375483b6b3):

    Command (scripts/rea.mjs ...) Baseline cold / warm Optimized cold / warm CPU user+sys (cold / warm) Peak RSS Stdout SHA-256 parity
    inspect-uefi-nvram --help 272.7 ms / 267.1 ms 133.7 ms / 133.4 ms (~2.04x / 2.00x) 370 / 350 ms -> 150 / 150 ms 194.4 -> 105.2 MiB (-89.2 MiB) 588e196e489d56a0 (exact)
    inspect-sqlite-database --help 263.9 ms / 262.5 ms 133.0 ms / 129.7 ms (~1.98x / 2.02x) 350 / 350 ms -> 150 / 150 ms 192.8 -> 104.0 MiB (-88.8 MiB) 0a27aa0e9584d842 (exact)
    inspect-wasm-artifact --help 276.8 ms / 278.1 ms 210.3 ms / 208.8 ms (~1.32x / 1.33x) 360 / 370 ms -> 260 / 260 ms 197.3 -> 157.2 MiB (-40.1 MiB) 93fba6b2ba2e3d8b (exact)
    guided-workflows --help 325.0 ms / 321.6 ms 272.9 ms / 270.2 ms (~1.19x / 1.19x) 430 / 430 ms -> 370 / 360 ms 204.0 -> 193.8 MiB (-10.2 MiB) d738d1a0b0d1f68d (exact)
    mcp add --help (control) 635.8 ms / 601.0 ms 607.2 ms / 602.7 ms 810 / 810 ms -> 810 / 810 ms 329.5 -> 329.3 MiB 3b0ec226cc03086d (exact)

    Compiled dist/cli-runtime/ static imports (^import ... from):

    • options-zOUZKmj_.js: 6.0 KB, badStaticImports=[]
    • contractCommand-B8ca8l_7.js: 97.2 KB, badStaticImports=[]
    • uefiCommands-CMdq-_Ww.js: 92.6 KB, badStaticImports=[]
    • sqliteCommands-rSUDN5pE.js: 2.0 KB, badStaticImports=[]
    • cliShell-DOzHQqIw.js: 62.7 KB (down from 97.1 KB), badStaticImports=[]
    • sessionToolContracts chunk: 92 KB (down from 483.7 KB)
  • User-visible CLI/MCP output (if applicable): identical --help output bytes across all tested commands.

  • Remaining proof gaps: Linux/Windows CI environments pending GitHub Actions run; end-to-end execution of mcp add and --detail summary --evidence-output relies on existing CLI boundary suites in CI (only cliStartupImports.test.ts + readmeConsistency.test.ts ran after the final test edit, while cliLazyRegistration.test.ts, commandEvidenceDelivery.test.ts, clientProfiles.test.ts, and cliBuiltinArguments.test.ts passed prior to the final cliStartupImports.test.ts assertion adjustment with unchanged production files).

  • Observed, derived, and inferred claims remain distinguishable.

  • Artifact identity, source provenance, and failed attempts remain preserved.

  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npx oxfmt --write src/cli/... tests/boundary/cli/cliStartupImports.test.ts — formatted cleanly
  • npm run check:fast — passed typecheck, lint, and architecture guards across 2951 source files
  • npm run test:focused -- tests/boundary/cli/cliStartupImports.test.ts — passed 1 file / 7 tests
  • npm run docs:check — passed docs:check:built (6/6 turbo tasks) and tests/conformance/readmeConsistency.test.ts (8/8 tests)
  • Earlier npm run test:focused run (with identical production source) — passed cliLazyRegistration.test.ts (14/14), commandEvidenceDelivery.test.ts (11/11), cliBuiltinArguments.test.ts (9/9), and clientProfiles.test.ts (15/15)

Compatibility, safety, and release

  • Breaking changes or migration steps: none (nativeTarget moved to internal src/cli/nativeTargetOption.ts; no public CLI or MCP contract change).
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable (src/cli/ module boundary only; Linux/Windows via CI).
  • Package or release metadata impact: none (docs:check passed).
  • Security, privacy, process, or containment review: mcpRegistration still initializes ownCliProcessResource before executing serveMcpRegistration.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T14:25:43.905089Z 75f9722 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

[Medium impact] The PR appears safe to merge; no blocking issue remains.

Summary

This PR keeps heavy modules out of unrelated CLI startups:

  • Native target options load their large contract only for native commands.
  • Analysis-view code loads only when a command requests a view.
  • The CLI loads MCP registration only for mcp add.

The earlier bundled-control finding is addressed. No new actionable issue or security issue was found.

Tests were not rerun during this review. Supersession could not be checked because GitHub access required authentication.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Start[CLI invocation] --> Command{Requested operation}
  Command -->|Unrelated command| Normal[Run without heavy services]
  Command -->|mcp add| Registration[Import mcpRegistration]
  Command -->|inspect-analysis-view| View[Import AnalysisViewService]
  Command -->|Summary evidence delivery| View
  Native[Native command registration] --> Target[nativeTargetOption]
  Target --> Contracts[sessionToolContracts]
Loading

Reviews (2) · Last reviewed commit: "test(cli): use bundled CLI runs for heav..." · Reviewed by Greptile

@morluto
morluto merged commit 3f83c87 into main Nov 22, 2026
40 checks passed
@morluto
morluto deleted the perf/cli-startup-decoupling branch November 22, 2026 14:29
@morluto morluto added area: cli CLI adapter and one-shot commands performance Performance regressions, scaling limits, and bounded-resource work labels Nov 22, 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 performance Performance regressions, scaling limits, and bounded-resource work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant