From 12edd572c716ea9f481cd999a8a1586fbc8ce032 Mon Sep 17 00:00:00 2001 From: Lee Campbell Date: Mon, 23 Mar 2026 07:57:21 +0800 Subject: [PATCH] fix: recover agent state on timeout instead of losing progress When Claude timed out (exit 124), `set -euo pipefail` in agent-loop.sh prevented sync_state from running, so all work from that iteration was lost. Additionally, entrypoint.sh treated timeout as fatal and broke the loop, preventing any retry. Now run_claude captures the timeout exit code so sync_state always runs to preserve progress, and entrypoint.sh continues the loop on timeout so the next iteration can pick up where the previous one left off. Co-Authored-By: Claude Opus 4.6 (1M context) --- autonomous/agent-loop.sh | 6 +++++- autonomous/entrypoint.sh | 6 ++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/autonomous/agent-loop.sh b/autonomous/agent-loop.sh index 88036dd..befc8dd 100644 --- a/autonomous/agent-loop.sh +++ b/autonomous/agent-loop.sh @@ -57,10 +57,12 @@ sync_state() { fi } +CLAUDE_RC=0 + run_claude() { local prompt="$1" timeout "$CLAUDE_TIMEOUT" claude --dangerously-skip-permissions --print \ - --output-format stream-json --verbose "$prompt" + --output-format stream-json --verbose "$prompt" || CLAUDE_RC=$? } load_prompt() { @@ -288,3 +290,5 @@ EOF fi ;; esac + +exit $CLAUDE_RC diff --git a/autonomous/entrypoint.sh b/autonomous/entrypoint.sh index eb8d262..0c0be31 100644 --- a/autonomous/entrypoint.sh +++ b/autonomous/entrypoint.sh @@ -27,7 +27,7 @@ echo "Building..." dotnet build --no-restore # ── Run the agent ── -MAX_ITERATIONS="${MAX_ITERATIONS:-10}" +MAX_ITERATIONS="${MAX_ITERATIONS:-30}" COOLDOWN="${COOLDOWN_SECONDS:-30}" export CLAUDE_TIMEOUT="${TIMEOUT_SECONDS:-1800}" @@ -39,9 +39,11 @@ for i in $(seq 1 "$MAX_ITERATIONS"); do EXIT_CODE=0 bash /usr/local/bin/agent-loop.sh || EXIT_CODE=$? - if [ "$EXIT_CODE" -ne 0 ]; then + if [ "$EXIT_CODE" -ne 0 ] && [ "$EXIT_CODE" -ne 124 ]; then echo "Iteration $i failed with exit code $EXIT_CODE" break + elif [ "$EXIT_CODE" -eq 124 ]; then + echo "Iteration $i timed out (exit code 124), continuing..." fi # Done?