Durable and Asynchronous Agent Work › Worked example: crash and recovery
Language AI HandbookAll chapters Let's prove the model. We submit a draft_report and run it with a simulated crash after step 0. We expect: step 0 (search) runs for real; step 1 (summarize) never runs on worker A; and when worker B claims the task, it reuses the recorded search result without re-invoking the tool.
PythonIn [18]Expand
EXECUTED.clear()
tid = submit("draft_report", {"topic": "durable agents"})
# Worker A: run, then crash after step 0
task_a = claim_task("A")
try:
run_task("A", task_a, crash_after_step=0)
except RuntimeError as e:
crash_message = str(e)ConsoleOut [19]Expand
worker A crashed: simulated crash after step 0 status after crash: running | lease worker: A
The store now has status=running, lease_worker=A, and the events list ends with tool_result(s0) + model_response(step=1). Worker A's lease is still live (we haven't waited), so no other worker can claim it yet. We need to wait for the lease to expire before B can take over. In practice you shorten the lease in tests, or trust that expirations happen quickly.
PythonIn [20]Expand
# Fast-forward: expire the lease manually to simulate waiting past LEASE_SECONDS
with tx() as con:
con.execute(
"UPDATE tasks SET lease_until = ? WHERE id = ?", (time.time() - 1, tid)
)
executed_before = len(EXECUTED)
task_b = claim_task("B")
result = run_task("B", task_b)
executed_after = len(EXECUTED)ConsoleOut [21]Expand
worker B result: completed tools executed on A: 1 tools executed on B (additional): 2 total real tool calls: 3 final status: completed
The output confirms that only three real tool calls occurred across two workers: search executed once on worker A, and summarize and send_email executed on worker B. That bounded rework is the property we want.
In this fixture, worker B finds the committed search result and skips that step. The other two stubs run on B. A crash before recording the result is covered separately below.
We can plot the accounting directly from the executed list and the recovered log, so "bounded rework" becomes a count instead of a claim.
PythonIn [22]Expand
executed_tools = [e["tool"] for e in EXECUTED]
plan_tools = [s["tool"] for s in plan_for("draft_report")]
recovered_tools = [e["tool"] for e in EXECUTED[:executed_before]]
real_executions = {tool: executed_tools.count(tool) for tool in plan_tools}VisualizationOut [23]Expand

Let's also confirm the event log tells the story.
PythonIn [24]Expand
logged_events = read_events(tid)ConsoleOut [25]Expand
1 tool_called {"call_id": "s0", "tool": "search", "input": {"q": "durable agent work"}, "step"
2 tool_result {"call_id": "s0", "tool": "search", "key": "e47f65649c9ca2b164ebc79ee7728b0b4ec7
3 model_response {"step": 1, "text": "did search"}
4 tool_called {"call_id": "s1", "tool": "summarize", "input": {"style": "brief"}, "step": 1}
5 tool_result {"call_id": "s1", "tool": "summarize", "key": "0ed53daecf4f881d1850091eef018387d
6 model_response {"step": 2, "text": "did summarize"}
7 tool_called {"call_id": "s2", "tool": "send_email", "input": {"to": "user@example.com"}, "st
8 tool_result {"call_id": "s2", "tool": "send_email", "key": "60e52a8a39da5dfdc55f2caf88f01c75
9 model_response {"step": 3, "text": "did send_email"}
10 task_completed {}The completed trace has ten events: an intent, result, and model-response marker for each of the three steps, followed by task_completed. B skips the first step because its completed-step marker exists. A cache hit follows a different path through the executor.
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!