veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

durable/workflow

lib/durable/workflow.vk

lib/durable/workflow.vk Pure-Veko Durable Workflows engine built on db/kv's CRC-32 crash-safe WAL. Provides crash-resilient, replay-safe memoized steps (step), durable retries (step-with-retry), durable timers (sleep-until), external event signals (wait-event / send-event), LIFO saga compensations (record-compensation / run-compensations), and execution history. Requires .fs and .clocks capabilities.

signature
WorkflowRun = struct { id str, name str, status enum = ..., step_count i64 = ..., wait_event str = ..., wake_at_ns i64 = ... }

Active handle to a durable workflow execution instance.

signature
RetryPolicy = struct { max_attempts i64 = ..., base_delay dur = ..., max_delay dur = ..., route_to_dlq bool = ... }

Exponential backoff + Dead-Letter Queue policy for step-retry-policy.

StepSpec struct
signature
StepSpec = struct { name str, work any }

Named step descriptor for single-fsync batched execution (step-batch).

start fn caps: .clocks
signature
start = fn [mut db kv/KvStore, id str, name str, input any] WorkflowRun

Starts a new durable workflow run id, or idempotently resumes an existing run after a crash/restart.

Executable Example
veko
use [kv = db/kv, wf = durable/workflow]
p = str/join [(os/temp-dir) '/veko_wf_doctest_1.vkkv'] ''
_ = os/remove/or p [.ok]
mut db = kv/open-fast p
mut run = wf/start mut db 'order-1' 'checkout' { total = 49 }
v1 = wf/step mut db mut run 'charge' fn [] [ 'tx-100' ]
// Re-open and replay: completed step returns persisted 'tx-100' without running closure!
_ = kv/close mut db
mut db2 = kv/open-fast p
mut run2 = wf/start mut db2 'order-1' 'checkout' { total = 49 }
v2 = wf/step mut db2 mut run2 'charge' fn [] [ 'tx-999-must-not-run' ]
_ = wf/complete mut db2 mut run2 v2
_ = kv/close mut db2
_ = os/remove/or p [.ok]
if v1 != 'tx-100' or v2 != 'tx-100' or run2.status != .completed [ err 'replay violated idempotency' ] else [ .ok ]
signature
get-status = fn [db kv/KvStore, id str] map

Returns the full persisted metadata map for workflow id.

signature
step-done? = fn [db kv/KvStore, wf WorkflowRun, step_name str] bool

Returns true if step_name has already been checkpointed as completed in db.

signature
step-result = fn [db kv/KvStore, wf WorkflowRun, step_name str] any

Returns the persisted checkpoint result for a completed step_name, or err if not done.

step fn caps: .clocks
signature
step = fn [mut db kv/KvStore, mut wf WorkflowRun, step_name str, work_fn any] any

Executes a durable, replay-safe memoized step step_name: - On replay (if step_name already completed in db), immediately returns its persisted value without invoking work_fn. - Otherwise calls (work_fn), atomically checkpoints its result to db/kv, and returns it.

step-with-retry fn caps: .clocks
signature
step-with-retry = fn [mut db kv/KvStore, mut wf WorkflowRun, step_name str, max_attempts i64, backoff dur, work_fn any] any

Executes a durable step with up to max_attempts retries and backoff delay between failures. Completed steps are memoized in db/kv and never re-run on replay.

sleep-until fn caps: .clocks
signature
sleep-until = fn [mut db kv/KvStore, mut wf WorkflowRun, step_name str, delay dur] bool

Durable sleep checkpoint: persists a wake-up timestamp on first invocation and returns false until time/unix >= wake_at_ns, after which it checkpoints done = true and returns true.

wait-event fn caps: .clocks
signature
wait-event = fn [mut db kv/KvStore, mut wf WorkflowRun, event_name str] map

Waits for an external signal event_name: - If already delivered via send-event, returns { ready = true, payload = ... }. - Otherwise persists status = 'waiting' and returns { ready = false, payload = .null }.

send-event fn caps: .clocks
signature
send-event = fn [mut db kv/KvStore, id str, event_name str, payload any] enum

Durably delivers an external event event_name with payload to workflow id.

record-compensation fn caps: .clocks
signature
record-compensation = fn [mut db kv/KvStore, mut wf WorkflowRun, comp_name str, data any] enum

Registers a durable LIFO saga compensation entry (comp_name, data) on wf.

run-compensations fn caps: .clocks
signature
run-compensations = fn [mut db kv/KvStore, mut wf WorkflowRun, comp_fn any] i64

Executes all registered compensations in LIFO order by invoking (comp_fn name data), clears them atomically, marks the workflow .compensated, and returns the count executed.

complete fn caps: .clocks
signature
complete = fn [mut db kv/KvStore, mut wf WorkflowRun, output any] enum

Atomically marks wf as .completed and persists output.

fail fn caps: .clocks
signature
fail = fn [mut db kv/KvStore, mut wf WorkflowRun, reason str] enum

Atomically marks wf as .failed and persists reason.

signature
history = fn [db kv/KvStore, id str] block

Returns the chronological durable execution log entries for workflow id.

signature
list-runs = fn [db kv/KvStore] block[str]

Lists all workflow run IDs stored in db.

step-retry-policy fn caps: .clocks
signature
step-retry-policy = fn [mut db kv/KvStore, mut wf WorkflowRun, step_name str, policy RetryPolicy, work_fn any] any

Executes a durable step with exponential backoff (RetryPolicy) and automatic Dead-Letter Queue (wf_dlq/<id>) routing on exhaustion.

signature
list-dead-letters = fn [db kv/KvStore] block[map]

Lists all dead-lettered workflow records stored under wf_dlq/*.

requeue-dead-letter fn caps: .clocks
signature
requeue-dead-letter = fn [mut db kv/KvStore, id str] WorkflowRun

Atomically clears the failed step's attempt counter and moves workflow id out of the Dead-Letter Queue so replay resumes at the failed step.

step-batch fn caps: .clocks
signature
step-batch = fn [mut db kv/KvStore, mut wf WorkflowRun, specs block[StepSpec]] block

Executes a batch of StepSpecs, skipping already-memoized steps on replay and checkpointing all newly completed steps in ONE db/kv WAL batch (fsync).

wait-event-timeout fn caps: .clocks
signature
wait-event-timeout = fn [mut db kv/KvStore, mut wf WorkflowRun, event_name str, timeout dur] map

Waits for external signal event_name up to timeout ({ ready = bool, timed_out = bool, payload = any }).

poll-runnable fn caps: .clocks
signature
poll-runnable = fn [db kv/KvStore] block[str]

Scans db for workflows ready to execute (status == 'running' or waiting timers/deadlines with wake_at_ns <= time/unix).

claim-run fn caps: .clocks
signature
claim-run = fn [mut db kv/KvStore, id str, worker_id str, lease_dur dur] bool

Atomically acquires or renews a distributed worker execution lease (worker_id, lease_dur) on workflow id.