Durable and Asynchronous Agent Work › Worked example: a task that should be retried
Language AI HandbookAll chapters 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.
| t | worker A does | durable store after write |
|---|---|---|
| 0 | claims task T, lease until 30 | status=running, lease=(A,30), attempts=1 |
| 1 | logs tool_called("search", …) | events=[…, tool_called] |
| 3 | search returns; logs tool_result | events=[…, tool_result] |
| 4 | logs model_response(step=1) (the summary plan) | events=[…, model_response] |
| 5 | crash | store has everything through step 4; T is running with a live-but-dead lease |
| 30 | lease expires | a scheduler notices T is running with lease.expiry <= now |
| 31 | worker B scans, sees T claimable, claims it, attempts=2, then replays the log: it recalls tool_result and the model response without re-calling either | status=running, lease=(B,61), attempts=2 |
| 32 | worker B logs tool_called("send_email", …), uses idempotency key T:2:send_email:sha(input), sends | events=[…, tool_called("send_email")] |
| 33 | logs tool_result("send_email", {"ok": true}) | events=[…, tool_result("send_email")] |
| 34 | logs task_completed, releases lease | status=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 attempts a completion after another claim advanced the generation to , 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!