From 51ec8443656046cd0cabc247191de3b870fd8522 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Tue, 7 Apr 2026 12:53:35 +0000 Subject: [PATCH] fix(queue-daemon): implement timeout handling for long-running tasks Check notified_at timestamp in check_task_completion() and mark tasks as error if they exceed TASK_TIMEOUT_HOURS (defaults to 1 hour). When timeout is detected: - Kill the task process (PID) if running - Stop the opencode session if exists - Mark queue item as error state Fixes #166 --- .../kugetsu/scripts/kugetsu-queue-daemon.sh | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/skills/kugetsu/scripts/kugetsu-queue-daemon.sh b/skills/kugetsu/scripts/kugetsu-queue-daemon.sh index a36f1cb..34620d7 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 + echo "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" + echo "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