← Back to PRs

#9148: Fix: Speed up shell completion generation from ~4.6s to <200ms

by vishaltandale00 open 2026-02-04 22:54 View on GitHub →
cli stale
## Problem The `openclaw completion --shell zsh` command takes ~4.6 seconds because it eagerly loads all 27 CLI subcommands and their dependencies. This makes shell startup very slow when using `source <(openclaw completion ...)` in .zshrc. ## Root Cause The completion CLI was calling `registerSubCliByName()` for every subcommand, which performs dynamic imports and loads the full CLI module tree. This was done to generate complete nested completions, but it's unnecessary for most users who only need top-level command completions. ## Solution - Add a `--full` flag that preserves the old eager-loading behavior - Make the default (fast) mode generate completions using only metadata from `getSubCliEntries()` without loading the actual CLI modules - Register lightweight command stubs (just name + description) instead of full implementations ## Performance - **Before**: ~4600ms (loads all 27 CLI modules) - **After**: ~150-200ms (uses metadata only) - **20-30x speedup** ## Testing The fix can be tested with: ```bash time openclaw completion --shell zsh --no-color ``` For users who need complete nested subcommand completions: ```bash openclaw completion --shell zsh --full ``` ## Backward Compatibility - Users who need complete nested subcommand completions can use `--full` - The cached completion workflow (`--write-state`) works exactly as before - No changes to completion script format or shell integration Fixes #9129 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR speeds up `openclaw completion` by avoiding eager dynamic imports of all sub-CLIs by default. It introduces a new `--full` flag to opt into the previous behavior (registering subcommands via `registerSubCliByName`) and otherwise generates completion scripts from `getSubCliEntries()` metadata by registering lightweight top-level command stubs. Change is localized to `src/cli/completion-cli.ts` where the completion action now chooses between fast stub registration vs. full subcommand registration before emitting/writing completion scripts. <h3>Confidence Score: 3/5</h3> - This PR is close to mergeable but has user-visible behavior changes that should be addressed first. - The performance optimization is straightforward and localized, but the current implementation can (1) make `completion` non-idempotent by mutating the shared commander instance with stubs and (2) regress `--write-state` cached scripts by defaulting to stub-only trees unless `--full` is passed. Both are likely to surprise users/tests and should be fixed before merging. - src/cli/completion-cli.ts <!-- greptile_other_comments_section --> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs