Skip to content

perf(cli): keep single-command discovery lazy and skip throwaway Zod parses - #13254

Merged
morluto merged 2 commits into
mainfrom
perf/cli-lazy-discovery-and-defaults
Nov 22, 2026
Merged

morluto merged 2 commits into
mainfrom
perf/cli-lazy-discovery-and-defaults

Conversation

@morluto

@morluto morluto commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Summary

Keep single-command CLI discovery (--help, -h, --schema, --llms, --llms-full for known top-level commands) on the lazy single-registrar path instead of falling back to loading the entire CLI graph, replace throwaway z.object({ value: field }).safeParse({}) calls per schema field in presentOmittableDefaults, and index commandRegistry entries in a Map for O(1) duplicate/lookup checks.

Problem and expected behavior

  1. selectCliRegistrations in src/cli/cliLoader.ts only admitted COMMAND_OUTPUT_FLAGS, so running rea <known-command> --help or rea <known-command> --schema discarded RECORDED_CLI_COMMAND_REGISTRATIONS and imported src/cli.ts (all 55+ CLI registration modules and ~200+ command schemas), even though src/cli/discovery.ts answers single-command help, schema, and --llms/--llms-full directly from that single registered command.
  2. presentOmittableDefaults (src/cli/omittableDefaults.ts) ran z.object({ value: field }).safeParse({}) for every input field with optout === undefined, allocating thousands of throwaway ZodObject wrappers on full CLI loads.
  3. registerCliCommand and cliCommand in src/cli/commandRegistry.ts performed linear scans over entries on every registration and lookup.

Expected behavior: single-command discovery loads only that command's recorded registrar while root help/manifests, unknown commands (for did you mean), and integration/builtin flags (completions, mcp, --update, --version, --incur-update-check) continue using the complete CLI with identical output.

Change and scope

  • src/cli/cliLoader.ts: Add COMMAND_DISCOVERY_FLAGS (--help, -h, --schema, --llms, --llms-full) and allow them in selectCliRegistrations when command[0] matches a recorded registrar in RECORDED_CLI_COMMAND_REGISTRATIONS.
  • src/cli/omittableDefaults.ts: Update fillsOmission to short-circuit on field._zod.optin !== undefined and call field.safeParse(undefined).success directly instead of wrapping in z.object({ value: field }).safeParse({}).
  • src/cli/commandRegistry.ts: Store a { entries, byName: Map<string, CliCommandEntry> } registry in the WeakMap so insertion order in cliCommands() is preserved while duplicate checks in registerCliCommand() and lookups in cliCommand() are O(1).
  • tests/boundary/cli/cliLazyRegistration.test.ts & tests/boundary/cli/cliInputSchemaRequired.test.ts: Update lazy CLI expectations for known-command --help / --schema, guard root and unknown --help fallback, and test omission rules (required, .optional(), .default(), .prefault(), .catch(), .default().refine(() => false), .optional().transform(...)).

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/cli/cliLoader.ts (selectCliRegistrations), src/cli/omittableDefaults.ts (fillsOmission), and src/cli/commandRegistry.ts (registerCliCommand/cliCommand).
  • Contribution scope fit and linked maintainer decision: not applicable (internal CLI adapter performance fix with unchanged CLI output/contracts).
  • CLI and MCP/tool-catalog contract: unchanged; all CLI --help, --schema, --llms, and error behaviors stay identical.
  • Provider, bridge, target-format, or platform compatibility: unchanged.
  • Evidence, artifact, provenance, or reconstruction contract: unchanged.
  • Target selection, authority, upstream behavior, and cleanup details: not applicable.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none.

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/cliLazyRegistration.test.ts (lazy CLI discovery + updated lazy CLI execution)
    • tests/boundary/cli/cliInputSchemaRequired.test.ts (omittable defaults omission rules)
  • Base reproduction or other evidence (Node v24.22.1, macOS arm64, warm OS caches, median of 3 runs after 1 warmup on dist/cli-runtime/ built before and after):
Invocation Before (origin/main) After Delta
rea list-cutter-sessions --help 759.6 ms 265.0 ms -65.1% (2.87x faster)
rea list-cutter-sessions --schema 662.5 ms 261.9 ms -60.5% (2.53x faster)
rea list-cutter-sessions --json 355.6 ms 266.4 ms -25.1%
rea decompile --help 681.7 ms 296.1 ms -56.6% (2.30x faster)
rea decompile --schema 875.5 ms 301.3 ms -65.6% (2.91x faster)
rea demangle-swift --help 628.1 ms 296.8 ms -52.7% (2.12x faster)
rea --help (complete CLI) 782.0 ms 621.1 ms -20.6%
  • User-visible CLI/MCP output: unchanged (Usage: rea list-cutter-sessions ... and schema document JSON verified in cliLazyRegistration.test.ts).
  • Remaining proof gaps: full CI suite (check:pr) not run locally; Windows lazy CLI subprocess timing not measured here.

Validation performed

  • npm run build:cached — passed (built runtime & skills cleanly)
  • npm run test:focused -- tests/boundary/cli/cliLazyRegistration.test.ts tests/boundary/cli/cliInputSchemaRequired.test.ts tests/boundary/cli/dispatcher.test.ts — passed (3 test files, 54/54 tests passed)
  • npm run check:fast — passed (typecheck, lint, and architecture guards across 2943 source files)

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: CLI dispatch and schema rendering verified on macOS arm64 (Node v24.22.1).
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no target interaction or privilege/containment changes.

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-22T03:06:30.078156Z 987e7cc 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, with no new actionable findings.

Summary

The PR keeps known-command discovery lazy, avoids throwaway Zod wrappers, and adds indexed command lookup.

  • Known-command discovery loads only that command’s registrar.
  • CLI schema checks test field omission without a temporary object.
  • Command lookup uses a name map while listings keep registration order.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[CLI request] --> B{Request type}
  B -->|Known command or scoped discovery| C[Load recorded registrar]
  B -->|Root, unknown command, or built-in| D[Load complete CLI]
  B -->|Standalone version or retired command MCP flag| E[Load no registrars]
  C --> F[Build shared CLI shell]
  D --> F
  E --> F
  F --> G[Answer discovery or run command]
Loading

Reviews (4) · Last reviewed commit: "fix(cli): test all five lazy discovery f..." · Reviewed by Greptile

Comment thread src/cli/cliLoader.ts Resolved
@morluto
morluto merged commit 38bc4fe into main Nov 22, 2026
51 checks passed
@morluto
morluto deleted the perf/cli-lazy-discovery-and-defaults branch November 22, 2026 08:14
@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