◉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.

agents150idempotency11scheduled-execution3lockingreliability12

✦ 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-04-24
SRE for Antigravity Agents — Taming Probabilistic Systems with SLOs and Error Budgets
AI agents are probabilistic by nature, so running them in production without SRE thinking is risky. This guide applies SLIs, SLOs, and error budgets to Antigravity agents with working code, and shows why the textbook 14.4x burn-rate alert never fires on a loose SLO.
◈ Agents & Manager2026-10-22
17 Dead References in My Handover Docs Turned Out to Be 2 Files, and the Real Risk Was a Different Site's Playbook Mixed In
The full Python script I use to check the root .md files an agent reads. My naive version counted 17 dead references; fixed, it was 7 across 2 files. Then it missed the real problem. Here is how I found it.
◈ Agents & Manager2026-10-21
Hooks Load but Never Run — Isolating the Antigravity CLI 1.2.x Report with a Three-Line Canary
A reported problem in Antigravity CLI 1.2.1 and 1.2.2: hooks load and show up in /hooks, yet no hook process ever starts. Here is a three-line canary setup that tells a config mistake apart from an executor problem.