Error handling patterns and recovery protocols for agents
This skill inherits all available tools. When active, it can use any tool Claude has access to.
Use these patterns for consistent error handling across all agents.
Every agent should handle these common errors:
| Error | Recovery |
|---|---|
| Uncommitted changes | Stash, commit, or abort |
| File already exists | Ask user: overwrite or abort |
| Branch doesn't exist | List available branches |
| Tests fail | Fix before proceeding |
| MCP tool unavailable | Check prerequisites, provide installation instructions |
| Sub-agent timeout | Retry or escalate to user |
When a workflow fails, perform cleanup:
git stash listgit stash popgit checkout {ORIGINAL_BRANCH}When a sub-agent fails, follow this sequence:
1. CAPTURE: Record error details
- Agent name and task
- Error message and type
- Partial progress (files created, etc.)
2. STABILIZE: Restore safe state
- Spawn git-operator: restore-workflow
- Pop any stashes created
- Return to original branch
3. CHECKPOINT: Present options to user
- R) Retry - Attempt the operation again
- S) Skip - Continue without this step
- A) Abort - Stop workflow, save state
- D) Debug - Spawn debug-analyst
4. RECORD: Update workflow state
- Mark task status in 00-index.md
- Log error for post-mortem
- Continue or exit based on user choice
To prevent infinite recursion:
[DEPTH: 3/5]Depth tracking rules:
| Caller | Sub-Agent | Depth Change |
|---|---|---|
| Main session | Orchestrator | 0 → 1 |
| Orchestrator | Specialist | +1 |
| Orchestrator | Worker | +1 |
| Specialist | Worker | +1 |
| Worker | Worker | +1 |
At max depth:
Detect and prevent agent cycles:
If agent_name in spawning_chain:
ABORT with "Cycle detected: {chain} → {agent_name}"
Example cycle: pr-review-manager → task-implementer → pr-review-manager