ACAgentic Craft

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

Before you start

  • HITL guide or human-approval-gate pattern
  • Idempotent write tools

You will leave with

  • 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

EnvironmentAPI
Local / single nodecreateDurableRunStore(dir)
Multi-instance / prodcreateSqlDurableRunStore(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:

  1. Lookup by idempotency key / natural key
  2. Only then mutate
  3. 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)

  1. Start a write-heavy run with a mock that sleeps after success ack.
  2. Kill the process.
  3. Restart; reclaim lease; resume.
  4. 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

Sources

Continue learning