Error handling patterns and recovery protocols for agents
/plugin marketplace add p4ndroid/ai-dev-pipeline-architecture/plugin install ai-dev-pipeline@ai-dev-pipeline-marketplaceThis 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
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.