Sub topic 12
Making reruns safe: idempotency and retries
This chapter assumes you have a wrapper script with a timeout, a lock, and logging, as built in earlier chapters. It also assumes you can use Git branches and read process exit codes.
Why reruns happen
A scheduled job runs more than once for the same period more often than you might expect:
- A systemd timer with
Persistent=trueruns a missed job at next boot. - You trigger a manual run to test.
- A retry follows a transient failure.
- A CI provider delivers a schedule event twice.
A job is idempotent if running it twice leaves the system in the same state as running it once. An agent job is not idempotent by default. Each run may open a new pull request, post another comment, or commit the same change again.
Make the output name deterministic
Derive the branch name from the period the run covers, not from the time it started. A nightly job for 2026-11-28 always uses agent/nightly-20261128. Before doing any work, check whether that branch already exists and skip if it does.
The same idea applies to other outputs. Search for an existing pull request or comment before creating one. Put a marker such as the date in its title so the search is reliable.
Retry only what is worth retrying
Retries help with transient problems, such as a rate limit or a dropped network connection. They do not help when the prompt is wrong, and they can double your cost when a run is slow. Follow these rules:
- Cap attempts at a small number, such as three.
- Increase the wait between attempts.
- Do not retry a timeout (status 124). A run that used its full time budget will likely do so again.
- Start each attempt from a clean checkout so a half-finished attempt cannot leak into the next.
#!/usr/bin/env bash
# rerun-safe-agent.sh
set -u
: "${AGENT_CMD:?set AGENT_CMD to your agent's non-interactive command}"
cd /srv/repos/my-project || exit 1
BRANCH="agent/nightly-$(date -u +%Y%m%d)"
LOG="$HOME/agent-logs/$BRANCH.log"
mkdir -p "$(dirname "$LOG")"
# Idempotency check: today's branch already exists on the remote.
if git ls-remote --exit-code --heads origin "$BRANCH" > /dev/null 2>&1; then
echo "$BRANCH already exists, skipping"
exit 0
fi
attempt=1
max_attempts=3
while true; do
git fetch origin main
git switch -C "$BRANCH" origin/main # clean start for every attempt
timeout 20m $AGENT_CMD < prompts/nightly.md >> "$LOG" 2>&1
status=$?
[ "$status" -eq 0 ] && break
[ "$status" -eq 124 ] && break # timeout: do not retry
[ "$attempt" -ge "$max_attempts" ] && break
sleep $((attempt * 60)) # 60 s, then 120 s
attempt=$((attempt + 1))
done
if [ "$status" -eq 0 ]; then
git push origin "$BRANCH"
fi
exit "$status"The script uses >> so the log keeps every attempt. It pushes only after the agent succeeds, so a failed run leaves no remote branch and the next run starts fresh.
Common errors
- Timestamp in the branch name. Every rerun gets a new name, so the existence check never matches.
- Retrying every non-zero status. A timeout or a bad prompt is retried at full cost.
- Pushing before validation. A branch from a failed run blocks the next run through the existence check.
Exercise
A nightly job names its branch agent/nightly-$(date +%s) and skips if that branch exists. Why does the skip never fire, and what is the fix?
Show answer
date +%s is the current time in seconds, so each run produces a different branch name and the existence check never finds a match. Use the date the run covers instead, for example date -u +%Y%m%d, so every rerun on the same day computes the same name.