#9158: perf(completion): add fast path for cached completions
cli
stale
Cluster:
Completion Performance Improvements
## 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
#9148: Fix: Speed up shell completion generation from ~4.6s to <200ms
by vishaltandale00 · 2026-02-04
87.9%
#6694: fix: Cache completion install
by s0up4200 · 2026-02-01
86.2%
#13840: perf(cli): skip plugin loading during completion generation
by lailoo · 2026-02-11
81.4%
#17962: fix(cli): speed up completion plugin loading
by forketyfork · 2026-02-16
81.2%
#17680: perf(cli): skip plugin loading during completion generation
by mcrolly · 2026-02-16
81.0%
#16710: feat: fast-path --version, --help, and no-args to skip full module ...
by xin-wen · 2026-02-15
77.3%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
76.9%
#16908: fix(cli): clean stale shell completion profile entries on uninstall...
by lorstyang · 2026-02-15
76.9%
#6382: fix(cli): silence Powerlevel10k Zsh warning from completion script
by dungngo4520 · 2026-02-01
76.5%
#18938: fix(completion): avoid compdef error in zsh by guarding completion ...
by gcsenyan · 2026-02-17
76.0%