Execution model
Every handler runs inside an execution started by PlatformInstance.execute. A read execution reads Items and returns a value. A write execution reads and writes Items, executes transitions and publishes Events, and commits as one unit. Each execution acts as one principal inside one Workspace and sees one value of ctx.now.
import type { PlatformInstance } from '@alpynehq/alpyne-types/runtime';
interface Request { readonly id: string; readonly traceId: string;}
export const approveClaim = async (instance: PlatformInstance, claimId: string, request: Request) => { const before = await instance.execute('read', async (current) => { const item = await current.templates.claim.getItem(claimId); return item?.state?.value; // => 'draft' });
await instance.execute( 'readwrite', { id: request.id, payload: { claimId }, traceId: request.traceId }, async (ctx) => { const item = await ctx.root.templates.claim.getItem(claimId); if (item === undefined || item.state?.value !== 'draft') return; const updated = await item.state.transitions.approve.execute({}); await updated.updateProperties({ approvedAt: ctx.now }); await updated.events.approved.publish({}); // Appended in the same transaction }, { idempotencyScope: 'expenses/approve', maximumTransientRetries: 2 }, );
return before;};Read and write executions
Section titled “Read and write executions”execute('read', fn) calls fn(current, ctx) with the root App and a ReadRuntimeInstance. Queries run here. The context reads committed data under the instance’s auth context. It cannot update an Item, execute a transition or publish an Event, and the Runtime rejects an attempt to do so.
execute('readwrite', event, fn, options) calls fn(ctx) with a WriteRuntimeInstance. Mutations, Listeners and App Hooks run here. The event argument names the cause of the write: an external Event { id?, payload, traceId } for a request arriving from outside the platform, or the internal Event the reactor is processing. An Event published while processing an internal Event inherits it as parent. That inheritance builds the lineage.
The PostgreSQL and SQLite Runtimes wrap a write execution in one database transaction. Item writes, transition updates, Event appends and the idempotency token commit together when fn returns and roll back together when it throws. The test Runtime holds the same boundary in memory: a transaction works on a copy of the store and merges it back on commit. Writable Item references belong to the attempt that produced them. Use the Item returned by updateProperties or execute, or look it up again, instead of holding a reference across executions.
Attempts, retries and idempotency
Section titled “Attempts, retries and idempotency”One run of fn is an attempt. When an attempt fails with an error the Runtime classifies as transient, the Runtime rolls the attempt back and starts a new one. A state transition conflict and a Relation uniqueness collision between overlapping transactions are both transient. maximumTransientRetries bounds the additional attempts; the default is one, and zero disables retries (Limits and errors). A new attempt gets fresh Item references and a fresh ctx.now, and it sees what the competing execution committed. Write fn so a second run is safe: read, decide, then write.
Any other error propagates to the caller after the rollback. External effects the attempt performed, such as an HTTP call, are not undone. Prefer to publish an Event and perform such work in a Listener.
Supplying an Event id together with an idempotencyScope makes the execution run at most once per Workspace, Event and scope. The Runtime writes an idempotency token in the same transaction as the writes. A second execution for the same key fails with an idempotency rejection, and none of its writes survive. The reactor uses this to make Listener processing safe under redelivery, and an API entry point uses it to make a retried request harmless.
Event lifecycle
Section titled “Event lifecycle”The sequence diagram shows one Event from publish to projection. Each arrow after the commit runs in its own execution with its own time.
sequenceDiagram
participant M as Mutation
participant R as Runtime
participant X as Reactor
participant L as Listener
participant P as Projector
M->>R: publish(payload)
R-->>R: append Event in the write transaction
R-->>M: commit
X->>R: claim committed Event
X->>L: execute('readwrite', event, listener)
L-->>R: commit Listener writes
P->>R: read Event, write Metric row
- Append.
publishparses the payload against the Event’s input and inserts the Event with the parent and lineage of the current cause. The append is part of the write transaction. - Commit. The transaction commits. The Event is now durable history, visible to
selectEventsand to the reactor. - Claim. The reactor claims each Event once, so two reactors never process the same Event. It skips Events in paused Workspaces.
- Listener run. For each Listener registered on the Event, the reactor starts a write execution as
systemin the Event’s Workspace. It passes the Event as the cause and a fixedidempotencyScope. Parent Listeners run in the same execution. - Projection. The projector reads the committed Event and writes a row for each Metric that selects it.
immediate folds steps 3 and 4 into the publishing execution. The Runtime appends the Event marked complete, so the reactor never claims it, and runs the Listeners inline before immediate resolves. The Listener writes join the caller’s transaction. Neither mode waits for projection.
ctx.now is the execution time the Runtime supplies. Within one write attempt it does not move: the Mutation, its immediate Listeners and every createdAt the attempt writes share one instant. A retry receives a fresh time. An Event keeps the createdAt of the attempt that appended it, so a Listener reads event.createdAt for when something happened and ctx.now for when it is being handled. Under the test Runtime the harness sets that clock, so a test drives schedules and time-based logic deterministically.
Boundaries
Section titled “Boundaries”- A read execution cannot modify Items or publish Events.
- Awaiting
publishawaits the append. It does not await the Listeners or the projection. - The execution is the atomic boundary. There is no nested transaction or retry block inside a handler.
- A retry is a new attempt. Nothing from the failed attempt survives, including Item references.
Related
Section titled “Related”- Behavior and change for what Queries, Mutations and Listeners are.
- Handle events to choose between
publishandimmediate. - Test an app to drive the reactor and projector with
tickAll. - Unit of Work for the normative execution contract.
- Runtime implementations for how each Runtime provides the boundary.

