#!/bin/bash # Reset FAILED and CANCELLED tasks to PENDING and delete their preserved workspaces. # Usage: reset-failed-tasks [--dry-run] set -euo pipefail DB_PATH="${CLAUDOMATOR_DB:-/site/doot.terst.org/data/claudomator.db}" DRY_RUN=false [[ "${1:-}" == "--dry-run" ]] && DRY_RUN=true # Collect preserved sandbox dirs before resetting so we can clean them up. SANDBOX_DIRS=$(sqlite3 "$DB_PATH" " SELECT DISTINCT e.sandbox_dir FROM executions e JOIN tasks t ON t.id = e.task_id WHERE t.state IN ('FAILED','CANCELLED') AND e.sandbox_dir IS NOT NULL AND e.sandbox_dir != ''; ") TASK_COUNT=$(sqlite3 "$DB_PATH" "SELECT COUNT(*) FROM tasks WHERE state IN ('FAILED','CANCELLED');") echo "Tasks to reset: $TASK_COUNT" if [[ "$DRY_RUN" == "true" ]]; then echo "[dry-run] Would reset $TASK_COUNT task(s) to PENDING." if [[ -n "$SANDBOX_DIRS" ]]; then echo "[dry-run] Workspaces to delete:" echo "$SANDBOX_DIRS" else echo "[dry-run] No preserved workspaces to delete." fi exit 0 fi sqlite3 "$DB_PATH" "UPDATE tasks SET state = 'PENDING' WHERE state IN ('FAILED','CANCELLED');" echo "Reset $TASK_COUNT task(s) to PENDING." DELETED=0 while IFS= read -r dir; do [[ -z "$dir" ]] && continue if [[ -d "$dir" ]]; then rm -rf "$dir" echo "Deleted workspace: $dir" DELETED=$((DELETED + 1)) fi done <<< "$SANDBOX_DIRS" echo "Deleted $DELETED workspace(s)."