#!/bin/bash # wq_stale_check — find in_progress items older than timeout, mark blocked API_URL="${WORK_QUEUE_API_URL:-}" [[ -z "$API_URL" ]] && API_URL=$(cat ~/.config/work_queue_api_url 2>/dev/null || echo "") [[ -z "$API_URL" ]] && { echo "Error: WORK_QUEUE_API_URL not set" >&2; exit 1; } timeout_minutes="${1:-30}" items=$(curl -sf "$API_URL/work?status=in_progress" | jq -r '.[] | @json' 2>/dev/null || echo "") [[ -z "$items" ]] && { echo "No in_progress items found."; exit 0; } count=0 now_ts=$(date -u +%s) while IFS= read -r item; do [[ -z "$item" ]] && continue work_id=$(echo "$item" | jq -r '.id') updated_at=$(echo "$item" | jq -r '.updated_at') agent=$(echo "$item" | jq -r '.assigned_agent') age_minutes=$(( (now_ts - $(date -u -d "$updated_at" +%s 2>/dev/null || echo "$now_ts")) / 60 )) if [[ "$age_minutes" -gt "$timeout_minutes" ]]; then echo "Stale: $work_id ($agent, ${age_minutes}m old) — marking blocked" curl -sf -X PATCH "$API_URL/work/$work_id" \ -H "Content-Type: application/json" \ -d "{\"status\":\"blocked\",\"notes\":\"Auto-blocked: stale for ${age_minutes} minutes (>$timeout_minutes)\"}" > /dev/null count=$((count + 1)) fi done <<< "$items" echo "Stale check complete: $count items marked blocked."