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

durable/object

lib/durable/object.vk

lib/durable/object.vk Pure-Veko Durable Objects runtime built directly on top of db/kv's CRC-32 crash-safe WAL. Provides named, single-writer, versioned stateful actors with atomic transactional mutations, durable alarms that survive restarts, FIFO persistent inboxes, and namespace enumeration. Requires .fs and .clocks capabilities.

ObjectRef struct
signature
ObjectRef = struct { ns str, id str, version i64 = ..., alarm_ns i64 = ... }

Reference to a persistent named Durable Object inside a db/kv store.

AlarmEvent struct
signature
AlarmEvent = struct { ns str, id str, fire_at_ns i64, tag str }

One due durable alarm returned by poll-due-alarms.

InboxMsg struct
signature
InboxMsg = struct { seq i64, sent_ns i64, kind str, payload any, attempts i64 = ... }

One queued durable inbox message returned by drain-inbox / process-inbox.

InboxOpts struct
signature
InboxOpts = struct { max_attempts i64 = ..., base_backoff dur = ..., max_backoff dur = ..., batch_limit i64 = ... }

Retry, visibility backoff, and batch limits for process-inbox.

signature
DeadLetterMsg = struct { ns str, id str, seq i64, kind str, payload any, attempts i64, last_error str, failed_ns i64 }

Persistent Dead-Letter Queue (DLQ) record created when an inbox message exhausts max_attempts.

open-object fn caps: .clocks
signature
open-object = fn [mut db kv/KvStore, ns str, id str] ObjectRef

Opens or creates a Durable Object identified by (ns, id) in db.

Executable Example
veko
use [kv = db/kv, dobj = durable/object]
p = str/join [(os/temp-dir) '/veko_do_doctest_1.vkkv'] ''
_ = os/remove/or p [.ok]
mut db = kv/open-fast p
mut obj = dobj/open-object mut db 'cart' 'user-42'
_ = dobj/put-field mut db mut obj 'total' 199
_ = kv/close mut db
mut db2 = kv/open-fast p
obj2 = dobj/open-object mut db2 'cart' 'user-42'
tot = dobj/get db2 obj2 'total'
_ = kv/close mut db2
_ = os/remove/or p [.ok]
if tot != 199 or obj2.version != 1 [ err 'durable object persistence failed' ] else [ .ok ]
get fn
signature
get = fn [db kv/KvStore, obj ObjectRef, field str] any

Reads a single durable state field from obj (returns err if absent; combine with /or).

signature
get-or = fn [db kv/KvStore, obj ObjectRef, field str, fallback any] any

Reads a single durable state field from obj, returning fallback when absent.

put-field fn caps: .clocks
signature
put-field = fn [mut db kv/KvStore, mut obj ObjectRef, field str, val any] enum

Atomically writes one field and increments the object's version in a single db/kv batch.

put-fields fn caps: .clocks
signature
put-fields = fn [mut db kv/KvStore, mut obj ObjectRef, fields map] enum

Atomically writes every key/value pair in fields and bumps the object's version in one WAL batch.

delete-field fn caps: .clocks
signature
delete-field = fn [mut db kv/KvStore, mut obj ObjectRef, field str] enum

Atomically deletes a state field and bumps the object's version.

signature
state = fn [db kv/KvStore, obj ObjectRef] map

Loads all current state fields of obj as a map.

mutate fn caps: .clocks
signature
mutate = fn [mut db kv/KvStore, mut obj ObjectRef, handler any] any

Executes a transactional actor turn (handler cur_state): - If handler returns err, zero writes are committed (full atomic rollback). - Otherwise handler returns { state = updated_map, result = value }, whose changed/deleted keys and incremented version are committed atomically in a single db/kv batch.

Executable Example
veko
use [kv = db/kv, dobj = durable/object]
p = str/join [(os/temp-dir) '/veko_do_doctest_2.vkkv'] ''
_ = os/remove/or p [.ok]
mut db = kv/open-fast p
mut acct = dobj/open-object mut db 'ledger' 'a1'
_ = dobj/put-field mut db mut acct 'bal' 100
r = dobj/mutate mut db mut acct fn [st] [
  b = map/get/or st 'bal' [ 0 ]
  mut ns = st
  map/set mut ns 'bal' (b + 50)
  { state = ns, result = (b + 50) }
]
_ = kv/close mut db
_ = os/remove/or p [.ok]
if r != 150 or acct.version != 2 [ err 'mutate failed' ] else [ .ok ]
signature
mutate-cas = fn [mut db kv/KvStore, mut obj ObjectRef, expected_version i64, handler any] any

Optimistic Compare-And-Swap (CAS) transactional mutation: verifies on-disk version == expected_version before committing.

set-alarm-at fn caps: .clocks
signature
set-alarm-at = fn [mut db kv/KvStore, mut obj ObjectRef, fire_at_ns i64, tag str] enum

Schedules a persistent alarm for obj firing at absolute Unix timestamp fire_at_ns.

set-alarm fn caps: .clocks
signature
set-alarm = fn [mut db kv/KvStore, mut obj ObjectRef, delay dur, tag str] i64

Schedules a persistent alarm firing after delay from now (time/unix).

clear-alarm fn caps: .clocks
signature
clear-alarm = fn [mut db kv/KvStore, mut obj ObjectRef] enum

Cancels any pending durable alarm on obj.

poll-due-alarms fn caps: .clocks
signature
poll-due-alarms = fn [mut db kv/KvStore, ns str] block[AlarmEvent]

Scans namespace ns for alarms with fire_at_ns <= time/unix, atomically clears them, and returns them.

enqueue fn caps: .clocks
signature
enqueue = fn [mut db kv/KvStore, mut obj ObjectRef, kind str, payload any] i64

Enqueues a durable FIFO message into obj's mailbox and returns its monotonic sequence number.

enqueue-batch fn caps: .clocks
signature
enqueue-batch = fn [mut db kv/KvStore, mut obj ObjectRef, messages block] i64

Enqueues a block of { kind, payload } messages into obj's mailbox in a single db/kv WAL batch.

signature
drain-inbox = fn [mut db kv/KvStore, mut obj ObjectRef] block[InboxMsg]

Atomically drains and returns all pending FIFO messages in obj's mailbox.

process-inbox fn caps: .clocks
signature
process-inbox = fn [mut db kv/KvStore, mut obj ObjectRef, opts InboxOpts, handler any] map

Transactionally processes visible inbox messages via (handler state msg) with exponential visibility backoff and automatic Dead-Letter Queue (do_dlq/<ns>/<id>/<seq>) routing on exhausted max_attempts, committing all state changes + ACKs + retries + DLQ moves in a single atomic db/kv WAL batch.

signature
list-dead-letters = fn [db kv/KvStore, obj ObjectRef] block[DeadLetterMsg]

Lists all Dead-Letter Queue (DeadLetterMsg) records for obj.

requeue-dead-letter fn caps: .clocks
signature
requeue-dead-letter = fn [mut db kv/KvStore, mut obj ObjectRef, seq i64] bool

Atomically moves dead-lettered message seq back into obj's live inbox with attempts = 0.

signature
list-ids = fn [db kv/KvStore, ns str] block[str]

Lists all Durable Object IDs currently stored under namespace ns.

signature
destroy = fn [mut db kv/KvStore, obj ObjectRef] enum

Atomically deletes all state, metadata, alarms, and queued messages belonging to obj.