#20553: fix(completion): guard zsh compdef call for environments without compinit
cli
size: XS
Cluster:
Zsh and PowerShell Completion Fixes
## Summary
- Guard the `compdef` call in generated zsh completions with `$+functions[compdef]` so it doesn't error in non-interactive shells or environments where `compinit` hasn't run
- Re-initialize the `_comps` associative array if it was lost (e.g. shell state snapshot/restore) to prevent `_comps: assignment to invalid subscript range`
- Remove leading blank line so `#compdef` is on line 1 per zsh completion conventions
## Problem
The generated zsh completion script calls `compdef` unconditionally. This fails in two scenarios:
1. **Non-interactive shells** (`zsh -c`, SSH command execution, remote editors like Zed) where `compinit` never runs → `command not found: compdef`
2. **Shell snapshot restore** (e.g. Claude Code, tmux-resurrect) where the `compdef` function survives but the `_comps` associative array is lost → `compdef:153: _comps: assignment to invalid subscript range`
## Test plan
- [ ] Source the generated completion file in a non-interactive shell: `zsh -c "source ~/.openclaw/completions/openclaw.zsh"` — should produce no error
- [ ] Source in an interactive shell with `compinit` — completions should register normally
- [ ] Regenerate completions with `openclaw completion --write-state` and verify the guard is present
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Guards zsh completion's `compdef` call to prevent errors in non-interactive shells and shell snapshot/restore scenarios. The fix removes the leading blank line (putting `#compdef` on line 1 per zsh conventions), wraps the `compdef` call with a check for the function's existence, and re-initializes the `_comps` associative array if lost.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The changes are surgical and targeted, fixing specific edge cases in zsh completion generation without modifying core logic. The guard pattern `$+functions[compdef]` is standard zsh practice, and the `_comps` re-initialization follows zsh completion conventions. No breaking changes or side effects expected.
- No files require special attention
<sub>Last reviewed commit: 0f44a24</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#17325: fix(completion): avoid zsh compdef error when compinit is not initi...
by ephelia-ai · 2026-02-15
91.6%
#18938: fix(completion): avoid compdef error in zsh by guarding completion ...
by gcsenyan · 2026-02-17
88.3%
#19109: fix(completion): initialize compinit before compdef
by Clawborn · 2026-02-17
88.1%
#17437: fix(completion): avoid rc=1 for optionless zsh leaf commands
by ephelia-ai · 2026-02-15
79.8%
#6382: fix(cli): silence Powerlevel10k Zsh warning from completion script
by dungngo4520 · 2026-02-01
78.6%
#9148: Fix: Speed up shell completion generation from ~4.6s to <200ms
by vishaltandale00 · 2026-02-04
78.5%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
77.9%
#22488: fix(cli): redirect plugin logs to stderr during completion
by pierreeurope · 2026-02-21
77.5%
#11165: fix(cli): filter empty flags in zsh and PowerShell nested completion
by Yida-Dev · 2026-02-07
75.8%
#16908: fix(cli): clean stale shell completion profile entries on uninstall...
by lorstyang · 2026-02-15
75.4%