← Back to PRs

#9158: perf(completion): add fast path for cached completions

by gavinbmoore open 2026-02-04 23:09 View on GitHub →
cli stale
## Summary Reduces shell completion time from ~4.6s to ~100ms by using cached completions. ## The Problem Running `source <(openclaw completion --shell zsh)` in `.zshrc` takes ~4.6s because it loads all subcommand modules to build the completion tree. This adds 4.6s to every new terminal session startup. ## The Fix 1. **Fast path**: When outputting completions, check for cached completion script first. If cache exists, output it directly without loading subcommand modules. 2. **Auto-cache**: On first generation (when cache doesn't exist), silently write the completion script to cache after outputting it. This means the first run is slow but subsequent runs are fast. 3. **`--no-cache` flag**: Users can bypass the cache if needed. ## Combined with existing features - The doctor check already detects `source <(openclaw completion ...)` patterns and offers to upgrade them to cached file usage - `openclaw completion --write-state` explicitly generates the cache - `openclaw completion --install` sets up the profile to source from cache ## Performance - **Before**: ~4.6s (loads 30+ subcommand modules) - **After**: ~100ms (reads cached file) - **First run**: Still slow, but auto-caches for next time Fixes #9129 🤖 Generated by Claw overnight build <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds a cached-completion fast path to `openclaw completion`: when neither `--install` nor `--write-state` is used, it attempts to read an existing completion script from `$OPENCLAW_STATE_DIR/completions` and print it directly, avoiding eager loading of all subcommand modules. When the cache is missing, it falls back to generating the script and (optionally) writes it to the cache for subsequent runs. It also introduces a `--no-cache` flag to force regeneration. <h3>Confidence Score: 4/5</h3> - Mostly safe to merge, but has one correctness issue in option handling. - Change is localized and the caching approach is straightforward, but the cache fast-path currently runs before validating `--shell`, which can produce incorrect output for invalid inputs and diverges from the intended error behavior. - src/cli/completion-cli.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs