Skip to content

Unit of Work

  • An Event is a fact that happened.
  • A handler is the function an operation, a Listener or an App Hook declares. A Query’s handler runs in a read execution; every other handler runs in a write execution.
  • A read execution runs a Query against committed state.
  • A write execution is one Event applied by one Mutation, Listener or App Hook. It is identified by its Event and its handler scope.

The write execution is the unit of work for writes. The rules under Reads and Read committed apply to every execution. Every other guarantee on this page is a property of a write execution.

  • A read execution must not modify Items, Relations or Metrics and must not append Events. A write attempted through a read context fails.
  • Privilege does not promote a read execution to a write execution.
  • Every execution reads committed state. Each read sees state committed as of that read, or a value cached from an earlier read in the same attempt. It never observes another execution’s uncommitted writes.
  • A write execution also sees its own writes. A read after a successful write observes that write: the returned Item reference and a fresh lookup both expose the new value. A reference obtained before the write need not change in place.
  • A Runtime may cache within an attempt. Two reads of one Item within an attempt may return the same value even when another execution commits between them.
  • Read committed is the minimum every Runtime provides. A Runtime may be stricter; each Runtime states what more it provides.
  • A write execution is identified by its Event and its handler scope.
  • A write execution is applied at most once. A Runtime may attempt it more than once; at most one attempt commits.
  • A write execution’s writes commit together or not at all: Item writes, Relation writes, published Events and the record that the write execution was applied.
  • No weaker unit exists. Tolerating failure means using smaller units:
    • Tolerated part. A handler may run part of its work as tolerated (provisionally tolerate(fn)). If the part fails, its writes and published Events are discarded, the error is returned to the handler, and the rest of the write execution commits. A collision inside a tolerated part is returned, not retried.
    • Fan-out. For independent failure per Item, publish one Event per Item. Each is applied in its own write execution.
    • Listeners. Each Listener of an Event, other than an immediate Event, is its own write execution. One Listener failing does not affect another.
  • A handler has no capability to perform external effects.
  • The only way out of a write execution is a published Event. Host subscribers perform effects in response to committed Events, outside any execution.
  • An Event published by a write execution, other than an immediate Event, becomes visible only after the write execution commits. A write execution that does not commit publishes nothing.
  • An immediate Event is applied inside the write execution that publishes it. Its Listeners run before that write execution commits and are part of it: they see its writes, their writes commit or are discarded with it, and a Listener’s error is the publishing handler’s to handle.
  • A published Event carries lineage from the Event that caused it.
  • Chains are bounded in depth and never cycle (Events).

Concurrency is declared, not taken.

  • Property writes from concurrent write executions merge per Property key. Writes to different keys of one Item all survive; for one key, the last to commit wins.
  • Collisions are decided only by conditional operations:
    • a state transition compares and sets the Item’s state, and collides when the Item is not in the source state;
    • setIfNotExists and addIfNotExists write a unique Relation only when none exists, and return false otherwise;
    • an atomic increment adjusts a shared counter without reading it first, and never collides.
  • There is no lock primitive. An invariant that spans Items is expressed through a conditional operation.

An attempt has exactly one outcome:

  1. It commits.
  2. It collides on a conditional operation and is retried whole, up to a Runtime limit. A retry sees state committed since.
  3. It fails. The write execution is recorded as failed for that handler scope, visibly, and the Event is not treated as applied by it. Exhausting the retry limit is a failure.

A write execution is a pure function of the committed state it reads and its Event.

  • ctx.now is the Event’s time, fixed for the write execution, not per attempt.
  • Every Identifier created in a write execution, by createIdentifier() or for a new Item, is derived from the write execution: a hash of the Event Identifier, the handler scope and the call’s ordinal, with any time component taken from the Event’s time. Every attempt yields the same Identifiers.
  • Ambient randomness and clocks available to a handler are seeded and frozen from the Event.
  • Identifiers are therefore not secrets. A value that must be unguessable, such as an invite link or a token, comes from a separate host capability, never from createIdentifier().