Advanced~55 minHazelJS
Implement Durable Execution and Recovery
Persist AgentRuns with createDurableRunStore / SQL stores, checkpoints, worker leases, and safe resume after crashes.
- Authors
- editorial-team
- Published
- Last reviewed
- HITL guide or human-approval-gate pattern
- Idempotent write tools
- Configure file or SQL durable run stores on AgentRuntime
- Explain checkpoints, leases, and workerId reclaim
- Write a crash-resume test that asserts single side effects
On this page
Goal
Survive crashes and deploys mid-run: checkpoints persist, workers reclaim expired leases, resumes do not duplicate side effects.
Pattern: /patterns/durable-agent-run.
Step 1Choose a store
| Environment | API |
|---|---|
| Local / single node | createDurableRunStore(dir) |
| Multi-instance / prod | createSqlDurableRunStore(prisma) (where documented in Agent OS / agent package) |
Pass runRepository, checkpointService, and (for HITL) humanTaskService into AgentRuntime. Enable durableSuspend: true if you need approval waits.
Step 2Workers and leases
Set workerId and lease TTL (runLeaseTtlMs / RepositoryAgentRunLeaseService.reclaimExpired as documented) so a dead worker’s run can be claimed safely. Without leases, two workers can advance the same AgentRun.
Step 3Idempotency + reconciliation
Durable kernels are typically at-least-once. Before re-executing a write tool after timeout:
- Lookup by idempotency key / natural key
- Only then mutate
- If compensation is required, use /patterns/compensating-action
Step 4Resume paths
- After crash: load
executionId, continue via runtime resume APIs documented for your version - After HITL:
approveAndResume - After user input waits:
runtime.resume(executionId, input)where applicable
Always log stop reason and timeline for ops (/learn/agentic-development/observability-signals-for-agent-decisions).
Step 5Crash test (artifact)
- Start a write-heavy run with a mock that sleeps after success ack.
- Kill the process.
- Restart; reclaim lease; resume.
- Assert external system shows one side effect.
Checklist
- Store not in-memory in production
- Lease reclaim exercised
- Idempotent adapters on all writes
- HITL path uses same store
- Runbook: pause agent, revoke tools, export timeline