#13840: perf(cli): skip plugin loading during completion generation
cli
stale
size: S
trusted-contributor
Cluster:
Completion Performance Improvements
## Summary
Fixes #13810
## Problem
`openclaw completion --shell bash` (and zsh) takes ~24 seconds because it eagerly registers all subcommands, including `pairing` and `plugins` which call `registerPluginCliCommands()`. This triggers the full plugin loader via `jiti`, which Babel-transpiles ~423 files at runtime.
This makes `source <(openclaw completion --shell bash)` in `.bashrc`/`.zshrc` add ~24s to every new shell session.
**Root cause** in `src/cli/program/register.subclis.ts`:
```typescript
// pairing entry (line 178-179)
const { registerPluginCliCommands } = await import("../../plugins/cli.js");
registerPluginCliCommands(program, await loadConfig()); // ~24s jiti hit
// plugins entry (line 190-191)
const { registerPluginCliCommands } = await import("../../plugins/cli.js");
registerPluginCliCommands(program, await loadConfig()); // ~24s jiti hit
```
## Fix
Set `OPENCLAW_COMPLETION_MODE=1` during completion generation and check it in the `pairing` and `plugins` registration entries to skip plugin loading. The core CLI structure (`pairing-cli`, `plugins-cli`) is still registered so completion includes those commands.
**completion-cli.ts:**
```typescript
process.env.OPENCLAW_COMPLETION_MODE = "1";
// ... register all subcommands ...
delete process.env.OPENCLAW_COMPLETION_MODE;
```
**register.subclis.ts (pairing & plugins entries):**
```typescript
if (!isTruthyEnvValue(process.env.OPENCLAW_COMPLETION_MODE)) {
const { registerPluginCliCommands } = await import("../../plugins/cli.js");
registerPluginCliCommands(program, await loadConfig());
}
```
## Effect on User Experience
**Before fix:**
```bash
time openclaw completion --shell bash > /dev/null
# real 0m23.991s
```
**After fix:**
```bash
time openclaw completion --shell bash > /dev/null
# real ~2s (no jiti transpilation)
```
- `openclaw pairing` and `openclaw plugins` commands work normally (plugins load as before)
- Shell startup with `source <(openclaw completion --shell bash)` drops from ~24s to ~2s
## Testing
- ✅ 6 tests pass (pnpm vitest run src/cli/program/register.subclis.test.ts)
- ✅ Lint passes
- ✅ New tests verify plugin loading is skipped in completion mode
- ✅ New tests verify plugin loading works normally outside completion mode
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Performance optimization that reduces shell completion generation time from ~24s to ~2s by skipping plugin loading during `openclaw completion` execution. Uses `OPENCLAW_COMPLETION_MODE` environment variable to conditionally bypass `registerPluginCliCommands()` calls in `pairing` and `plugins` subcommand registration. The command structure is still registered for completion purposes, but the expensive jiti transpilation of ~423 plugin files is avoided. Normal command execution outside completion mode remains unchanged with full plugin loading.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- Well-scoped performance fix with comprehensive test coverage, clear problem/solution description, and no logical errors. The environment variable approach is thread-safe within the synchronous registration flow, and the optimization only affects completion generation without impacting normal command execution.
- No files require special attention
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#17680: perf(cli): skip plugin loading during completion generation
by mcrolly · 2026-02-16
91.8%
#17962: fix(cli): speed up completion plugin loading
by forketyfork · 2026-02-16
91.0%
#9148: Fix: Speed up shell completion generation from ~4.6s to <200ms
by vishaltandale00 · 2026-02-04
87.5%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
82.7%
#9158: perf(completion): add fast path for cached completions
by gavinbmoore · 2026-02-04
81.4%
#22488: fix(cli): redirect plugin logs to stderr during completion
by pierreeurope · 2026-02-21
80.9%
#6694: fix: Cache completion install
by s0up4200 · 2026-02-01
79.8%
#16908: fix(cli): clean stale shell completion profile entries on uninstall...
by lorstyang · 2026-02-15
79.6%
#6382: fix(cli): silence Powerlevel10k Zsh warning from completion script
by dungngo4520 · 2026-02-01
79.3%
#11165: fix(cli): filter empty flags in zsh and PowerShell nested completion
by Yida-Dev · 2026-02-07
77.7%