Durable and Asynchronous Agent Work › Worked example: a task that should be retried

Michael BrenndoerferDecember 1, 20263 min read
Language AI HandbookAll chapters
Part 49 of 67Long-Horizon Agents and Interoperability
Chapter 376Durable and Asynchronous Agent Work › Worked example: a task that should be retried
Reading section 4 of 15 · Worked example: a task that should be retried · 3 minSection 4 of 15 · 3 min

Section 4 of 15 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Let's make a tiny task and walk it through a crash by hand. Our runner's model is:

Task = { id, # stable identity kind, # e.g. "draft_report" input, # opaque status, # pending | running | completed | failed lease, # {worker, expiry} or null attempts, # number of times started deadline, # absolute time (epoch seconds) or null events, # list of appended events checkpoint, # materialized state snapshot at some offset (optional) idempotency_log, # dict: key -> result for this task's side effects }

Here's the crash sequence for a task that drafts a report by (a) searching the web, (b) summarizing, (c) emailing the summary.

tworker A doesdurable store after write
0claims task T, lease until 30status=running, lease=(A,30), attempts=1
1logs tool_called("search", …)events=[…, tool_called]
3search returns; logs tool_resultevents=[…, tool_result]
4logs model_response(step=1) (the summary plan)events=[…, model_response]
5crashstore has everything through step 4; T is running with a live-but-dead lease
30lease expiresa scheduler notices T is running with lease.expiry <= now
31worker B scans, sees T claimable, claims it, attempts=2, then replays the log: it recalls tool_result and the model response without re-calling eitherstatus=running, lease=(B,61), attempts=2
32worker B logs tool_called("send_email", …), uses idempotency key T:2:send_email:sha(input), sendsevents=[…, tool_called("send_email")]
33logs tool_result("send_email", {"ok": true})events=[…, tool_result("send_email")]
34logs task_completed, releases leasestatus=completed

Two things to notice. First, the search was executed once even though the agent ran on two workers: because the tool_result event was already in the log when B replayed. Second, the email went out once even though B's step-3 call was "new" to B, because the idempotency key is derived from the logical call, and the "send_email" step never happened on A (A crashed before reaching it). If B also crashed after sending but before logging the result, the key still protects us: the retry carries the same key, and the (hypothetical) email service deduplicates.

Notice, too, the deliberate gap between t=5 and t=30. For twenty-five seconds the task is in a state that looks stuck: status is running, but the worker is gone. Nobody is doing work, yet the system is not broken. This is the lease doing its job. It is a self-healing silence: the task is not lost, it is merely waiting for the lease to expire before a new worker may claim it. This is why choosing the lease duration is a tradeoff between recovery speed (short leases reclaim faster) and renewal traffic plus false-positive expiry (short leases also mistake slow workers for dead ones more often).

A lease can expire while the original worker is still executing. An incremented ownership generation lets the SQLite store reject writes from the old claim. That protection is local to the resource that checks the generation.

Each claim increments the stored generation for that task. When a worker with generation nn attempts a completion after another claim advanced the generation to n+1n+1, the transaction checks the mismatch and rejects the write. The ownership generation is distinct from the sequence number that orders events.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy