Files
openclaw/tools/delegate-with-healing.sh
Clawd Bot ca9b510922 chore: align with upstream openclaw/openclaw and overlay local additions
- Reset master to upstream/main (16,697 commits)
- Overlay 2,271 local-only files (skills, tools, workspace, configs, apps)
- Restore IDENTITY.md and USER.md templates
- Build verified, gateway running, Discord working

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 07:40:46 +01:00

188 lines
5.1 KiB
Bash

#!/usr/bin/env bash
# Delegate task with self-healing retry logic
# Wrapper for manual task delegation med automatic error recovery
set -euo pipefail
REPO_ROOT="/home/alex/clawd"
KANBAN_API="http://192.168.1.220:5003"
LOG_DIR="/home/alex/clawd/logs"
HEALING_LOG="$LOG_DIR/healing-wrapper.log"
MAX_RETRIES=3
HEALING_AGENT="sonnet"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$HEALING_LOG"
}
# Fetch task details from Kanban
get_task() {
local task_id="$1"
curl -s "$KANBAN_API/tasks/$task_id"
}
# Execute task with agent
execute_task() {
local task_id="$1"
local agent="$2"
local timeout="$3"
log "🚀 Executing task $task_id with agent $agent (timeout: ${timeout}s)"
# Get task details
local task=$(get_task "$task_id")
local title=$(echo "$task" | jq -r '.title // "Untitled"')
local description=$(echo "$task" | jq -r '.description // ""')
# Build agent prompt
local prompt="**Task $task_id: $title**
$description
**Instructions:**
- Complete the task as described
- Report progress and results
- Commit any changes to git
- Update task status when done
Work carefully and methodically."
# Execute with agent
local result=$(cd "$REPO_ROOT" && pnpm -s openclaw agent \
--local \
--json \
--agent "$agent" \
--timeout "$timeout" \
--message "$prompt" 2>&1)
echo "$result"
}
# Analyze failure and generate fix
heal_failure() {
local task_id="$1"
local agent="$2"
local failure_output="$3"
local retry_count="$4"
log "🔧 Healing task $task_id failure (attempt $retry_count/$MAX_RETRIES)"
# Build healing prompt
local healing_prompt="**Self-Healing Task Recovery**
An OpenClaw agent task failed. Your job is to diagnose and fix it.
**Task ID:** $task_id
**Agent:** $agent
**Retry:** $retry_count/$MAX_RETRIES
**Failure Output:**
\`\`\`
$failure_output
\`\`\`
**Analysis Required:**
1. What went wrong? (root cause)
2. How to fix it? (specific changes)
3. Should we retry? (yes/no + reasoning)
**Actions You Can Take:**
- Edit code/config files
- Install missing dependencies
- Fix syntax errors
- Adjust task parameters
- Recommend different agent/model
Be specific. Make changes if needed. Report back."
# Spawn healing agent
log "🤖 Spawning healing agent ($HEALING_AGENT)..."
local healing_result=$(cd "$REPO_ROOT" && pnpm -s openclaw agent \
--local \
--json \
--agent "$HEALING_AGENT" \
--timeout 300 \
--message "$healing_prompt" 2>&1)
log "📋 Healing analysis complete"
# Check if healing recommends retry
if echo "$healing_result" | jq -e '.reply' | grep -qi "retry\|re-run\|try again\|should.*retry"; then
log "✅ Healing agent recommends retry"
return 0
else
log "❌ Healing agent does not recommend retry"
return 1
fi
}
# Main delegation with healing loop
delegate_task() {
local task_id="$1"
local agent="${2:-auto}"
local timeout="${3:-1800}" # 30 min default
log "📋 Delegating task $task_id to $agent (max retries: $MAX_RETRIES)"
local retry_count=0
local success=false
while [[ $retry_count -le $MAX_RETRIES ]]; do
if [[ $retry_count -gt 0 ]]; then
log "🔄 Retry $retry_count/$MAX_RETRIES for task $task_id"
fi
# Execute task
local output=$(execute_task "$task_id" "$agent" "$timeout" || echo "FAILED")
# Check for success
if echo "$output" | jq -e '.success == true' &>/dev/null; then
log "✅ Task $task_id completed successfully"
success=true
break
elif echo "$output" | grep -qiE "(completed|done|success|finished)"; then
log "✅ Task $task_id completed (heuristic match)"
success=true
break
fi
# Task failed - attempt healing
log "⚠️ Task $task_id failed on attempt $((retry_count + 1))"
if [[ $retry_count -lt $MAX_RETRIES ]]; then
if heal_failure "$task_id" "$agent" "$output" "$((retry_count + 1))"; then
retry_count=$((retry_count + 1))
sleep 5 # Brief pause before retry
continue
else
log "❌ Healing failed - stopping retries"
break
fi
else
log "❌ Max retries exceeded for task $task_id"
break
fi
done
if [[ $success == true ]]; then
log "🎉 Task $task_id delegation successful after $retry_count retries"
return 0
else
log "💥 Task $task_id delegation failed after $retry_count retries"
return 1
fi
}
# CLI
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <task-id> [agent] [timeout-seconds]"
echo ""
echo "Examples:"
echo " $0 335 # Delegate task 335 (auto-detect agent)"
echo " $0 335 gemini-flash # Use specific agent"
echo " $0 335 gemini-flash 3600 # 1 hour timeout"
exit 1
fi
delegate_task "$@"