Skip to content

perf: fast-path completion/version/shell-scripts to bypass full discovery#898

Merged
jackwener merged 2 commits intomainfrom
perf/completion-fast-path
Apr 8, 2026
Merged

perf: fast-path completion/version/shell-scripts to bypass full discovery#898
jackwener merged 2 commits intomainfrom
perf/completion-fast-path

Conversation

@jackwener
Copy link
Copy Markdown
Owner

Summary

  • --get-completions: reads cli-manifest.json directly via new completion-fast.ts, never loads registry/discovery/Commander
  • --version / -V: prints PKG_VERSION and exits immediately
  • completion <shell>: prints shell script (bash/zsh/fish) without any discovery
  • Full startup path converted to dynamic import() so fast paths never pay the import cost of discovery.js, registry.js, cli.js, hooks.js, node-network.js, update-check.js
  • Fallback: if no manifest exists, --get-completions still falls through to full discovery path

Files changed

  • src/completion-fast.ts (NEW) — lightweight manifest reader + shell script generators, depends only on node:fs
  • src/main.ts — fast-path checks before discovery; heavy imports converted to dynamic import()
  • src/completion.ts — removed manifest functions (moved to completion-fast.ts), cleaned up unused fs import

Design decisions

  • USER_CLIS_DIR inlined as path.join(os.homedir(), '.opencli', 'clis') to avoid importing the entire discovery.ts module
  • BUILTIN_COMMANDS list duplicated in completion-fast.ts (intentional — avoids any transitive dependency on registry.js)
  • --help intentionally NOT fast-pathed — it depends on the full Commander tree (per codex-mini0 review)
  • Plugin commands not in fast path — would require loading discoverPlugins()

Context

P0a from performance analysis discussion: #893 (comment)...
Discussed with @codex-mini0 in #OpenCLI thread.

Test plan

  • tsc --noEmit passes
  • All 579 unit tests pass (48 test files)
  • Manual: opencli --version returns version without delay
  • Manual: opencli completion zsh prints script without delay
  • Manual: opencli --get-completions twitter returns completions quickly (with manifest present)
  • Manual: compare time opencli --version before/after to measure improvement

… discovery

Lightweight commands (--get-completions, --version, completion <shell>) now
resolve before any heavy module loading. Key changes:

- New completion-fast.ts: manifest-based completion + shell script generators
  with zero dependency on registry/discovery/cli modules
- main.ts: static imports replaced with dynamic import() for the full startup
  path so the fast path never pays the cost of loading discovery, registry,
  Commander, hooks, etc.
- USER_CLIS_DIR inlined to avoid importing the entire discovery module
- completion.ts: removed manifest functions (moved to completion-fast.ts),
  now only used as fallback when manifest is unavailable
1. --version fast path: only match when argv[0] is --version/-V,
   not anywhere in argv. Prevents intercepting `opencli gh --version`
   which should pass through to the subcommand.

2. Completion fast path: require ALL manifests to exist (hasAllManifests),
   not just one. If user clis dir exists but has no manifest, fall back
   to full discovery so user adapters aren't silently dropped.
   If user clis dir doesn't exist at all, skip its manifest requirement
   since there are no user adapters to miss.
@jackwener jackwener merged commit 93a650b into main Apr 8, 2026
11 checks passed
@jackwener jackwener deleted the perf/completion-fast-path branch April 8, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant