From d6f93bb8f56c959a12cbfd918a5c4392f613dbce Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 10 Apr 2026 17:41:39 +0000 Subject: [PATCH] fix: fix flock/binding issues with claude_run_with_watchdog --- lib/agent-sdk.sh | 13 +++++++++++-- lib/formula-session.sh | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/agent-sdk.sh b/lib/agent-sdk.sh index 6bee6d3..580bfe4 100644 --- a/lib/agent-sdk.sh +++ b/lib/agent-sdk.sh @@ -104,6 +104,14 @@ claude_run_with_watchdog() { kill "$grace_pid" 2>/dev/null || true wait "$grace_pid" 2>/dev/null || true + # When timeout fires (rc=124), explicitly kill the orphaned claude process + # tail --pid is a passive waiter, not a supervisor + if [ "$rc" -eq 124 ]; then + kill "$pid" 2>/dev/null || true + sleep 1 + kill -KILL "$pid" 2>/dev/null || true + fi + # Output the captured stdout cat "$out_file" return "$rc" @@ -132,7 +140,8 @@ agent_run() { mkdir -p "$(dirname "$lock_file")" local output rc log "agent_run: starting (resume=${resume_id:-(new)}, dir=${run_dir})" - output=$(cd "$run_dir" && flock -w 600 "$lock_file" claude_run_with_watchdog claude "${args[@]}" 2>>"$LOGFILE") && rc=0 || rc=$? + # Acquire lock separately (flock cannot exec bash functions) + output=$(cd "$run_dir" && ( flock -w 600 "$lock_file" || exit 1; claude_run_with_watchdog claude "${args[@]}" ) 2>>"$LOGFILE") && rc=0 || rc=$? if [ "$rc" -eq 124 ]; then log "agent_run: timeout after ${CLAUDE_TIMEOUT:-7200}s (exit code $rc)" elif [ "$rc" -ne 0 ]; then @@ -173,7 +182,7 @@ agent_run() { local nudge="You stopped but did not push any code. You have uncommitted changes. Commit them and push." log "agent_run: nudging (uncommitted changes)" local nudge_rc - output=$(cd "$run_dir" && flock -w 600 "$lock_file" claude_run_with_watchdog claude -p "$nudge" --resume "$_AGENT_SESSION_ID" --output-format json --dangerously-skip-permissions --max-turns 50 ${CLAUDE_MODEL:+--model "$CLAUDE_MODEL"} 2>>"$LOGFILE") && nudge_rc=0 || nudge_rc=$? + output=$(cd "$run_dir" && ( flock -w 600 "$lock_file" || exit 1; claude_run_with_watchdog claude -p "$nudge" --resume "$_AGENT_SESSION_ID" --output-format json --dangerously-skip-permissions --max-turns 50 ${CLAUDE_MODEL:+--model "$CLAUDE_MODEL"} ) 2>>"$LOGFILE") && nudge_rc=0 || nudge_rc=$? if [ "$nudge_rc" -eq 124 ]; then log "agent_run: nudge timeout after ${CLAUDE_TIMEOUT:-7200}s (exit code $nudge_rc)" elif [ "$nudge_rc" -ne 0 ]; then diff --git a/lib/formula-session.sh b/lib/formula-session.sh index d7f38a3..450b655 100644 --- a/lib/formula-session.sh +++ b/lib/formula-session.sh @@ -31,7 +31,7 @@ # Requires: lib/env.sh, lib/worktree.sh, lib/agent-sdk.sh sourced first for shared helpers. # Source agent-sdk for claude_run_with_watchdog watchdog helper -source "$(dirname "$0")/agent-sdk.sh" +source "$(dirname "${BASH_SOURCE[0]}")/agent-sdk.sh" # ── Run guards ───────────────────────────────────────────────────────────