ANTIGRAVITY LABJP
Articles/Agents & Manager
Agents & Manager/2026-06-13Advanced

When a Scheduled Agent Runs Twice — Designing for Idempotency Against Overlap and Retry

A scheduled agent can do the same work twice when the next run triggers before the last one finishes. Here is a design with an overlap lock and an idempotency guard that survives mid-run failures, drawn from a double-publish incident I ran into in production.

agents89idempotency5scheduled-execution3lockingreliability6

Premium Article

I once found two nearly identical articles published within minutes of each other.

Because I run several sites on autopilot as an indie developer, I let scheduled agents handle content generation and report aggregation. One day generation ran heavier than usual and a single run overran its window. Before it finished, the next trigger fired, a second agent started, produced another article on the same topic, and pushed it.

We tend to assume a scheduled job "runs once at a fixed time," but in reality it can "run when the time comes even if the previous run hasn't finished." This article lays out an idempotency design for agents that survives both overlap and retry.

Why double execution happens

There are two main causes. If you do not separate them, your fix will miss.

The first is overlap. When the previous run drags on and the next trigger time arrives before it ends, two runs proceed in parallel. Generative agents vary widely in duration depending on input data and model response time — a job that usually finishes in five minutes occasionally takes twenty. A fixed-interval schedule does not absorb that jitter.

The second is retry. Most unattended execution environments automatically re-run failed tasks. The catch is "how far did it get before failing?" If the network drops right before the push, after the article file is already written, the run is judged a "failure" — yet the file remains, and re-running the same work duplicates the artifact.

These two differ in nature. Overlap is a "two run at once" problem, prevented with a lock. Retry is a "runs twice with a gap" problem, absorbed by making the artifact idempotent. You need both.

Prevent overlap with flock

The most reliable way to prevent overlap is to take an exclusive lock at the entry point of the run. Linux flock provides an advisory lock on a file descriptor and makes "give up immediately if the lock can't be taken" easy to express.

#!/usr/bin/env bash
# run_agent_guarded.sh
# If the same task is already running, skip this time
set -euo pipefail
 
LOCK="/tmp/agent-content-antigravity.lock"
 
exec 9>"$LOCK"
if ! flock -n 9; then
  echo "⏭ Previous run still in progress. Skipping this time ($(date '+%F %T'))"
  exit 0
fi
 
# From here, single execution is guaranteed
echo "▶ Started $(date '+%F %T')"
 
# --- actual agent work ---
generate_and_push_article
 
echo "■ Finished $(date '+%F %T')"
# Descriptor 9 closes automatically at process exit, releasing the lock

The -n (non-blocking) on flock -n is the crux. If the lock can't be taken it returns an exit code without waiting, so an overlapping run quietly yields to the next cycle. By not building a wait queue, you avoid runs piling up like a snowball. Put the lock file somewhere shared across processes such as /tmp; the descriptor releases on process exit, so no explicit release is needed.

That stops overlap. But a lock alone does not stop retry duplication, because two runs separated in time can each acquire the lock.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Separate and understand the two causes of double execution: overlap from a long-running previous run, and automatic retry after failure
Drop in an overlap guard with flock and an idempotency guard keyed on the work item, straight into your bash flow
Learn to design for at-least-once execution so that even a double run converges to a single artifact
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

Agents & Manager2026-06-13
Building Idempotent Scheduled Agents with the Antigravity SDK
Scheduling an Antigravity SDK agent is almost a one-liner. The hard part is making it idempotent — so a double trigger never runs the job twice, a crash never drops a day, and the result always converges to one. Here is how I build idempotent scheduled agents, learned from the maintenance jobs I run as an indie developer.
Agents & Manager2026-06-13
Instruction Drift in Scheduled Agents — A Three-Layer Design for Keeping Definitions, Docs, and Reality Aligned
Scheduled agents keep logging success even after their instructions diverge from reality. Here is the three-layer drift-detection design — definition, documentation, reality — I built after silent failures in my own operations.
Agents & Manager2026-06-02
Rehearsing an Agent's Actions Before They Touch Production — Designing a Zero-Side-Effect Dry-Run Layer
Some accidents survive shadow mode and canaries: the very first time an agent touches an external API. This is the design and TypeScript implementation of a zero-side-effect dry-run layer you can bolt onto Antigravity's parallel agents, with the real numbers from running six sites autonomously.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →