Skip to content

Commit 953bc17

Browse files
Test Userclaude
andcommitted
fix: retry on transient API network errors instead of failing immediately
Add check_network_error() helper and wire it into the retry loop so that 400 Network error responses from the Claude API are retried (up to MAX_RETRIES, 60s backoff) rather than treated as non-retryable failures. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent deb5491 commit 953bc17

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

scripts/night-watch-cron.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,15 @@ while [ "${ATTEMPT}" -lt "${MAX_RETRIES}" ]; do
565565
BACKOFF_MIN=$(( BACKOFF / 60 ))
566566
log "RATE-LIMITED: Attempt ${ATTEMPT}/${MAX_RETRIES}, retrying in ${BACKOFF_MIN}m"
567567
sleep "${BACKOFF}"
568+
elif check_network_error "${LOG_FILE}" "${LOG_LINE_BEFORE}"; then
569+
# Transient API network error — retry with a short backoff
570+
ATTEMPT=$((ATTEMPT + 1))
571+
if [ "${ATTEMPT}" -ge "${MAX_RETRIES}" ]; then
572+
log "NETWORK-ERROR: All ${MAX_RETRIES} attempts exhausted for ${ELIGIBLE_PRD}"
573+
break
574+
fi
575+
log "NETWORK-ERROR: Attempt ${ATTEMPT}/${MAX_RETRIES}, retrying in 60s"
576+
sleep 60
568577
elif check_context_exhausted "${LOG_FILE}" "${LOG_LINE_BEFORE}"; then
569578
# Context window exhausted — checkpoint progress and resume in a fresh session
570579
ATTEMPT=$((ATTEMPT + 1))

scripts/night-watch-helpers.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,19 @@ check_rate_limited() {
847847
fi
848848
}
849849

850+
# Detect transient API network errors (e.g. "Network error" in a 400 response).
851+
# Usage: check_network_error <log_file> [start_line]
852+
# Returns 0 if a network error was detected, 1 otherwise.
853+
check_network_error() {
854+
local log_file="${1:?log_file required}"
855+
local start_line="${2:-0}"
856+
if [ "${start_line}" -gt 0 ] 2>/dev/null; then
857+
tail -n "+$((start_line + 1))" "${log_file}" 2>/dev/null | grep -qi "Network error"
858+
else
859+
tail -20 "${log_file}" 2>/dev/null | grep -qi "Network error"
860+
fi
861+
}
862+
850863
# Detect context window exhaustion from Claude API logs.
851864
# Usage: check_context_exhausted <log_file> [start_line]
852865
# Returns 0 if context exhausted, 1 otherwise.

0 commit comments

Comments
 (0)