diff --git a/skills/kugetsu/scripts/kugetsu-queue-daemon.sh b/skills/kugetsu/scripts/kugetsu-queue-daemon.sh index a36f1cb..b746414 100644 --- a/skills/kugetsu/scripts/kugetsu-queue-daemon.sh +++ b/skills/kugetsu/scripts/kugetsu-queue-daemon.sh @@ -41,6 +41,31 @@ check_task_completion() { local session_id=$(python3 -c "import json; print(json.load(open('$item')).get('opencode_session_id', ''))" 2>/dev/null) local issue_ref=$(python3 -c "import json; print(json.load(open('$item')).get('issue_ref', ''))" 2>/dev/null) local pid=$(python3 -c "import json; print(json.load(open('$item')).get('pid', ''))" 2>/dev/null) + local notified_at=$(python3 -c "import json; print(json.load(open('$item')).get('notified_at', ''))" 2>/dev/null) + + local timed_out=false + if [ -n "$notified_at" ]; then + local notified_epoch=$(date -d "$notified_at" +%s 2>/dev/null || echo "0") + local now_epoch=$(date +%s) + local hours_elapsed=$(( (now_epoch - notified_epoch) / 3600 )) + if [ "$hours_elapsed" -ge "${TASK_TIMEOUT_HOURS:-1}" ]; then + timed_out=true + log_warn "queue-daemon" "Task $queue_id ($issue_ref) timed out after ${hours_elapsed}h" + fi + fi + + if [ "$timed_out" = true ]; then + if [ -n "$pid" ] && [ "$pid" != "None" ]; then + kill "$pid" 2>/dev/null || true + fi + if [ -n "$session_id" ]; then + opencode session stop "$session_id" 2>/dev/null || true + fi + update_queue_item_state "$queue_id" "error" + log_error "queue-daemon" "Task $queue_id ($issue_ref) marked error — timeout after ${hours_elapsed}h" + release_lock "$issue_ref" + return + fi if [ -n "$pid" ] && [ "$pid" != "None" ]; then if ! kill -0 "$pid" 2>/dev/null; then