Lease-Based Task Ownership for AI Agents: Stop Duplicate Work Before It Starts
Long-running agents need recoverable ownership, not permanent locks. Use leases, fencing tokens, heartbeats, and reconciliation to prevent duplicate work.
Two AI workers claiming the same job is not a cute race condition. It is how you send two invoices, publish two posts, or let separate agents rewrite the same file. Permanent locks avoid overlap until the owner crashes; then they turn your queue into a graveyard.
A lease gives one worker temporary ownership that can be recovered after failure. The trick is making expired owners harmless, because a slow agent may wake up after another worker has taken over.
What Should a Lease Record Contain?
Store a task ID, owner ID, expiry time, monotonically increasing fencing token, and the version of the task being processed. Claim the task with one atomic database operation: only an unowned or expired row may transition to your owner ID.
UPDATE tasks
SET owner_id = :worker,
lease_until = now() + interval '60 seconds',
fence = fence + 1
WHERE id = :id
AND (lease_until IS NULL OR lease_until < now())
RETURNING fence;
If the update returns nothing, another worker owns the task. Do not ask the model to negotiate. Back off, pick another job, or subscribe to a completion event.
Why Is Expiry Alone Not Enough?
Suppose worker A pauses during a slow model call. Its lease expires, worker B claims the job, and then A resumes. Both now believe they can write. A fencing token solves this by making every claim newer than the last.
Downstream mutation APIs must reject stale tokens. If worker A holds fence 41 and worker B holds 42, a database write, object-store update, or tool gateway should accept 42 and reject 41. Without enforcement at the side effect, the token is decorative.
Use heartbeats to renew healthy work, but cap the total runtime. An agent that loops forever should not retain authority forever. Separate a renewable execution lease from the immutable business command and its idempotency key.
How Do You Handle Long Model Calls?
Do not hold a database transaction open while waiting for an LLM. Claim the lease, commit, then call the model. Renew in a lightweight background loop and stop work immediately when renewal fails.
For tools with irreversible effects, verify lease ownership and fence immediately before execution. Record the effect in an idempotency ledger. Lease ownership prevents concurrent planners; idempotency prevents duplicate business effects across retries.
| Failure | Lease response | Extra control |
|---|---|---|
| Worker crash | Expire and reclaim | Idempotency key |
| Network partition | Reject stale fence | Reconciliation |
| Slow model response | Stop after failed renewal | Runtime budget |
| Duplicate queue delivery | One atomic claim wins | Unique task identity |
How Should You Test It?
Freeze a worker after claim, let the lease expire, and start a replacement. Resume the old worker and confirm every mutation rejects its fence. Kill workers before and after tool calls. Skew clocks in tests or, better, rely on database time so worker clocks do not decide ownership.
Track claim conflicts, renewal failures, expired active leases, stale-fence rejections, and reconciliation volume. If you never see rejections in chaos tests, the protection is probably not wired to the real side effect.
Leases make ownership recoverable. Fencing makes recovery safe. Idempotency makes effects repeatable. You need all three before autonomous workers can share a production queue without stepping on each other.
Sources
> Want more like this?
Get the best AI insights delivered weekly.
By subscribing, you agree to our Privacy Policy. You can unsubscribe at any time.
> Related Articles
pgvector Can Return 4 of Your 10 Matches—Where Did the Rest Go?
A filtered vector search can quietly starve your AI app of evidence. pgvector has a fix, but builders need to measure recall and latency before trusting it.
Agent Memory Consolidation Scheduling: When to Merge, Summarize or Forget
Your agent doesn't need more memory — it needs a maintenance schedule. Here's how to build merge, summarize, and decay policies that keep long-term agents sharp instead of senile.
LLM Output Schema Evolution: Migrating Producers and Consumers Without a Flag Day
Your JSON schema will change. Here's how to evolve LLM output contracts safely — with dual readers, shadow validation, and staged rollouts instead of a risky big-bang cutover.
Most Read
- 1 Kimi K3.1 Pro: China's Open-Weight Model Just Caught the Frontier
- 2 OpenAI Is Buying the Bottleneck: Can Orchestra Bio Cut a 15-Year Drug Timeline?
- 3 1,068 Tokens Per Second: Can Mercury 3 Change How LLMs Generate Text?
- 4 Same Price, Better Coding: How Good Is DeepSeek V4.1 Flash 1017?
- 5 Used OpenCode? Should You Rotate Your API Key After the Console Breach?
Latest News
- Claude Code Left Secrets in 1,122 GitHub Files—Check This Setting
- Gemini Omni 1.2 Flash Makes 15-Second Video Cheaper—Then Comes the Edit
- GPT-6 Cyber Can Build Exploits—But Builders Face a Different Test
- 1,122 GitHub Files Exposed Secrets—What Did “Don't Ask Again” Save?
- Gemini Omni 1.2 Flash Offers $1.05 Clips—What Does That Buy You?
- GPT-6 Cyber Hits 48.5% on ExploitGym—Who Gets the Keys?
Tags
> Stay in the loop
Weekly AI tools & insights.