#16908: fix(cli): clean stale shell completion profile entries on uninstall (AI-assisted)
cli
commands
size: M
Cluster:
Completion Performance Improvements
## Summary
- Problem: `openclaw uninstall --state` removed completion cache files but did not clean shell profile entries, leaving stale `source ~/.openclaw/completions/openclaw.zsh` lines.
- Why it matters: opening a new terminal after uninstall could print `no such file or directory` errors during shell startup.
- What changed:
- Added completion profile cleanup during state uninstall (`uninstallCompletionFromAllProfiles`).
- Added profile cleanup helpers in completion CLI (`uninstallCompletionFromProfile`, `uninstallCompletionFromAllProfiles`).
- Made completion install source lines resilient by guarding on file readability/existence before sourcing.
- Added targeted unit tests for completion profile behavior and uninstall flow.
- What did NOT change (scope boundary): no gateway/service uninstall logic changes, no dependency/version updates, no unrelated refactors.
## AI Assistance
- This PR is **AI-assisted** (implemented with Codex under operator supervision).
## Testing Degree
- **Fully tested** for this change scope.
- Ran:
- `pnpm check`
- `pnpm vitest run --config vitest.unit.config.ts src/cli/completion-cli.test.ts src/commands/uninstall.test.ts`
## Prompts / Session Logs (condensed)
- Primary user ask: fix stale completion source errors after uninstall.
- Session log emphasis:
- Remediation plan:
- identify root cause in uninstall/completion code paths,
- clean stale profile entries during uninstall,
- harden completion source lines with file-existence guards.
- Testing plan:
- add regression tests for stale-line cleanup and uninstall integration,
- run lint/type/style gate (`pnpm check`),
- run focused vitest suite for touched behavior.
- Verification outcome:
- 2 test files passed, 4 tests passed.
- `pnpm check` passed.
## Change Type (select all)
- [x] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Docs
- [ ] Security hardening
- [ ] Chore/infra
## Scope (select all touched areas)
- [ ] Gateway / orchestration
- [ ] Skills / tool execution
- [ ] Auth / tokens
- [ ] Memory / storage
- [ ] Integrations
- [ ] API / contracts
- [x] UI / DX
- [ ] CI/CD / infra
## Linked Issue/PR
- Closes: None
- Related: None
## User-visible / Behavior Changes
- `openclaw uninstall --state` now removes OpenClaw completion entries from shell profiles to prevent stale startup errors.
- Newly installed completion entries now source cached completion files only when they exist.
## Security Impact (required)
- New permissions/capabilities? `No`
- Secrets/tokens handling changed? `No`
- New/changed network calls? `No`
- Command/tool execution surface changed? `No`
- Data access scope changed? `No`
- If any `Yes`, explain risk + mitigation: `N/A`
## Repro + Verification
### Environment
- OS: macOS
- Runtime/container: Node 22+, pnpm
- Model/provider: N/A
- Integration/channel (if any): CLI shell completion (zsh/bash/fish/powershell profiles)
- Relevant config (redacted): default state dir under `~/.openclaw`
### Steps
1. Install completion for zsh.
2. Remove completion cache/state (`uninstall --state`).
3. Open a new shell.
### Expected
- No shell startup error from stale OpenClaw completion source lines.
### Actual
- Before fix: shell could print missing file error for `~/.openclaw/completions/openclaw.zsh`.
- After fix: stale completion entries are removed on uninstall; guarded source lines avoid startup errors.
## Evidence
- [x] Failing test/log before + passing after
- [x] Trace/log snippets
- [ ] Screenshot/recording
- [ ] Perf numbers (if relevant)
## Human Verification (required)
- Verified scenarios:
- Completion install writes guarded source line.
- Completion uninstall helper removes stale lines even when cache file is already missing.
- `uninstall --state` invokes completion profile cleanup.
- Edge cases checked:
- Dry-run uninstall does not modify profiles.
- What you did **not** verify:
- Full interactive shell startup across every shell/OS combination.
## Compatibility / Migration
- Backward compatible? `Yes`
- Config/env changes? `No`
- Migration needed? `No`
- If yes, exact upgrade steps: `N/A`
## Failure Recovery (if this breaks)
- How to disable/revert this change quickly: revert this PR commit.
- Files/config to restore: `src/cli/completion-cli.ts`, `src/commands/uninstall.ts`.
- Known bad symptoms reviewers should watch for: completion block unexpectedly removed when it should remain.
## Risks and Mitigations
- Risk: profile line matching could remove unrelated lines if they contain similar tokens.
- Mitigation: matching is constrained to OpenClaw completion header/command/cache naming patterns; regression tests cover stale-line removal and normal install behavior.
Agent-Signoff: Clawdex
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes stale shell completion entries after uninstalling OpenClaw state. The implementation adds file-existence guards to completion source lines (preventing errors if cache files are deleted) and implements cleanup logic to remove completion profile entries during `openclaw uninstall --state`.
**Key changes:**
- `formatCompletionSourceLine` now generates guarded source lines: `[ -r "path" ] && source "path"` for bash/zsh, `test -r "path"; and source "path"` for fish, and `if (Test-Path "path") { . "path" }` for PowerShell
- Added `uninstallCompletionFromProfile` and `uninstallCompletionFromAllProfiles` helpers to remove completion entries from shell profiles
- Modified `uninstallCommand` to call cleanup before removing state directory
- `isCompletionProfileLine` now matches completion cache file patterns (`basename.{zsh,bash,fish,ps1}`) in addition to explicit cache paths, enabling removal of stale entries even when cache files are already deleted
- Added comprehensive unit tests for both install guards and uninstall cleanup
The changes are backward compatible and properly handle dry-run mode.
<h3>Confidence Score: 5/5</h3>
- Safe to merge with minimal risk
- The PR addresses a clear bug with a well-scoped solution. The implementation is clean, follows existing patterns, includes comprehensive tests, and handles edge cases properly (dry-run mode, missing profiles, already-deleted cache files). The line-matching logic is appropriately constrained to OpenClaw-specific patterns.
- No files require special attention
<sub>Last reviewed commit: b6a1ac4</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#6694: fix: Cache completion install
by s0up4200 · 2026-02-01
81.6%
#17962: fix(cli): speed up completion plugin loading
by forketyfork · 2026-02-16
81.5%
#17680: perf(cli): skip plugin loading during completion generation
by mcrolly · 2026-02-16
81.1%
#9148: Fix: Speed up shell completion generation from ~4.6s to <200ms
by vishaltandale00 · 2026-02-04
80.7%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
80.6%
#13840: perf(cli): skip plugin loading during completion generation
by lailoo · 2026-02-11
79.6%
#22488: fix(cli): redirect plugin logs to stderr during completion
by pierreeurope · 2026-02-21
78.5%
#9158: perf(completion): add fast path for cached completions
by gavinbmoore · 2026-02-04
76.9%
#20553: fix(completion): guard zsh compdef call for environments without co...
by mr-sk · 2026-02-19
75.4%
#6382: fix(cli): silence Powerlevel10k Zsh warning from completion script
by dungngo4520 · 2026-02-01
75.4%