Runtime implementations
Three Runtimes execute a Definition: PostgreSQL, SQLite and the test Runtime. Each implements the same PlatformInstance interface, runs the same application code and runs the shared conformance suite. They differ in storage, in how a write execution is wrapped, and in the reactor and projector that pair with them.
| PostgreSQL | SQLite | Test Runtime | |
|---|---|---|---|
| Package | @pyn-backend/platform-runtime-postgresql |
@pyn-backend/platform-runtime-sqlite |
@pyn-backend/platform-runtime-test |
| Source | src/alpyne/runtime/postgresql |
src/alpyne/runtime/sqlite |
src/alpyne/runtime/test |
| Storage | PostgreSQL tables through Knex | SQLite tables through Knex | In-memory maps |
| Write execution | One read committed transaction |
One transaction opened with BEGIN IMMEDIATE |
Cloned store, merged on commit |
| Reactor | src/alpyne/reactor/postgresql |
src/alpyne/reactor/sqlite |
src/alpyne/reactor/test |
| Projector | src/alpyne/projector/local |
src/alpyne/projector/sqlite |
None |
| Harness | src/alpyne/harness/spec |
src/alpyne/harness/spec-sqlite |
src/alpyne/harness/test |
| Conformance | Every suite against PostgreSQL; the single-connection subset against PGlite | The single-connection subset | Every suite |
PostgreSQL
Section titled “PostgreSQL”The PostgreSQL Runtime stores Items, Relations, Events, idempotency tokens and projections in PostgreSQL tables and reaches them through Knex. createPostgresqlPlatformInstance builds one instance per auth context and Workspace and resolves the Workspace’s reactor key from the database.
A read execution runs its callback against the database connection with no transaction. A write execution runs inside bundle.runTransaction. It opens one transaction at the read committed isolation level, freezes the system clock so ctx.now holds one value for the attempt, and runs the callback. When the callback returns, the transaction commits, which is the commit Unit of Work defines. When it throws, the transaction rolls back. After the transaction ends, the bundle’s writer throws on any further write, so a writable reference from the attempt rejects operations.
The write executor handles idempotency and retries in one place, helpers/readwrite.ts. When the execution carries an Event Identifier and idempotencyScope, the executor inserts an idempotency_token row inside the same transaction as the writes. A primary-key violation on that insert surfaces as an idempotency rejection. The executor classifies a state transition conflict or a unique-constraint violation on the relation table as transient and retries the whole execution, up to maximumTransientRetries additional attempts (default one).
App Hooks (runCreate, runSeed) and setRefreshBy each run inside their own transaction. publishExternalEvent hands the Events to the platform service, which appends them for the reactor.
The PostgreSQL reactor claims Events with FOR UPDATE SKIP LOCKED, and the local projector selects unprojected Events the same way. Event processing and Persistence and projection describe both.
SQLite
Section titled “SQLite”The SQLite Runtime mirrors the PostgreSQL Runtime file for file under src/alpyne/runtime/sqlite, with the SQL adapted to SQLite. It stores the same tables in a SQLite database through Knex and reads the reactor key from configuration (pyn2.reactorKey).
A write execution runs inside one transaction opened with BEGIN IMMEDIATE. That statement takes the database write lock at the start rather than at the first write, so two overlapping write executions fail at BEGIN instead of racing to SQLITE_BUSY. The system clock is frozen for the attempt and the writer is closed after the transaction, as in PostgreSQL.
Idempotency and retries follow the PostgreSQL executor. SQLite has no named constraints. The executor recognizes an idempotency rejection by a unique violation on idempotency_token (event_id, scope), and a transient Relation conflict by a unique violation on the relation table’s two partial unique indexes.
Property patches use a chain of json_set calls, one per top-level key, which keeps a null-valued key rather than deleting it, matching PostgreSQL’s || merge. Full-text search runs through SQLite FTS (helpers/fts-query.ts).
The SQLite Runtime pairs with the SQLite reactor and the SQLite projector. Its harness, src/alpyne/harness/spec-sqlite, wires the SQLite platform service and reactor into createHarnessInstance.
Test Runtime
Section titled “Test Runtime”The test Runtime holds a Workspace in memory: a store of Items keyed by Identifier, a store of Relations, a list of pending transitions, an Event array and a set of claimed idempotency tokens. createTestBundle seeds the store with the Workspace Item under a system auth context.
A write execution runs inside runTransaction, which clones the Item and Relation stores, freezes the system clock and runs the callback against the clone. On return, commitTransaction merges the attempt’s Item and Relation changes into the shared store and replays the attempt’s transitions in order, each checked against the source state at commit time. A transition whose source state no longer matches throws a state transition conflict from the commit, and the shared store is left untouched. Events appended during the attempt are pushed to the shared array only after the merge succeeds.
Idempotency claims are transaction-local. The executor rejects a token already in the shared set at the start of the attempt, and runTransaction rejects it again at commit time if a sibling claimed it while the attempt ran, so the token is added to the shared set only when the commit succeeds. The state transition conflict is the transient error the test executor retries, with the same default of one additional attempt.
The test Runtime has no projector and does not expose Metrics. Its harness, src/alpyne/harness/test, pairs it with the test reactor, whose in-memory Event stream claims Events from the shared array.
Related
Section titled “Related”- Execution model explains executions, attempts and the Event lifecycle these Runtimes implement.
- Test an app chooses a harness and drives the reactor with
tickAll. - Runtime conformance lists the suites each harness runs.
- Scope and conformance states the contract every Runtime implements.
PlatformInstanceis the interface each Runtime exposes.

