Skip to content

Architecture

Kronik Maintainers edited this page Dec 1, 2026 · 2 revisions

Architecture

Kronik is one Rust binary with a thin imperative shell around a deterministic core, and every turn passes through the log before anything else happens. This page describes the parts that exist in the code, the path of a turn through them, what lies on disk, and the decisions behind it all. Parts that later milestones add are marked with their milestone.

The parts

HTTP clients ──▶ gateway ───┐
                            ├──▶ scheduler ──▶ turn executor ──▶ effect gate (M2) ──▶ external services
CLI ──▶ JSON API ───────────┘                        │
                                                     │  turn record; from M2 also TurnBegin and EffectResult
                                                     ▼
                                  log: segments, group commit, fsync
                                                     │  applied in LSN order
                                                     ▼
                                  state.db and one SQLite database per actor
Part What it does Crate
Gateway HTTP on http_listen, or HTTPS with a certificate from files or by ACME (M6). Matches requests against the routes in the current deployments' manifests, admits root inputs, handles Idempotency-Key per actor, and answers only after commit. A turn that outlives the gateway timeout (30 s) gives 504 and still completes. kronik-gateway
JSON API /v1 over a Unix socket and, with a bearer token, over TCP (M5): deploy, messages, actors, history, turns, dead letters, from M2 grants, limits, secrets, orphans and timers, from M3 forks, proposals and policy, and from M5 tokens. Every request is checked against the scope its route needs. The CLI and the MCP server are clients of it. kronik-api
Scheduler Activates actors and runs one turn at a time per actor, different actors in parallel. Owns the mailboxes, logical time, the turn pipeline, migrate turns, dead letters and startup recovery, and from M2 the timer wheel. kronik-sched
Turn executor Wasmtime configured for determinism, a code store per hash, the deploy-time import check, a fresh instance per turn, the host side of the WIT, and the fuel and memory limits. kronik-runtime
Effect gate (M2) Checks capabilities against the configuration of the turn's epoch, writes TurnBegin before the first external effect, inserts secrets, applies rate limits, records every result, and returns recorded results when a turn runs again. kronik-sched, kronik-effects
Log Append-only segment files of Protobuf records, each with a CRC and an LSN; a group-commit writer that acknowledges an append after fsync; recovery of a torn tail. kronik-log, kronik-proto
State state.db per branch and one SQLite database per actor, written only by the applier, in LSN order, from committed records. kronik-store
Secret store (M2) The master key and the encrypted secret store. kronik-privacy
Blob store (M25, M26) Content-addressed files for large HTTP bodies and for blobs an actor holds. The log stores a reference, not the bytes. kronik-blob

M3 adds the fork engine with replay and the behavior diff, and policy and approvals. M5 adds the MCP server, which serves the JSON API's operations as tools. M6 adds shipping and restore, M7 foreign actors in gVisor, M20 the opt-in microVM level, M24 long-lived connections, and M25 and M26 the blob store. M28 adds the OpenTelemetry and audit export. M16, on main and in no release yet, adds the cluster runtime.

A turn, step by step

  1. Admission. The gateway matches a route and admits a root input with a fresh root id. An operator message through the API is a root input too; a message from another actor is not. A repeat of a committed request with the same Idempotency-Key gets the saved response instead of a new turn.
  2. Scheduling. The scheduler queues the input in the actor's mailbox. When the actor's turn comes, it gets an execution id, a 32-byte seed and its logical time, the largest of the host's clock, the largest time already handed out and the time of the input that started it (ADR 0006).
  3. Execution. The runtime creates a fresh instance of the kind's current code hash and calls handle. The guest sees the actor's state at the start of the turn plus its own writes. The host buffers state writes, SQL changes (run in a savepoint and captured as changesets, ADR 0005), outgoing messages and timer operations.
  4. Effects (M2). Before the turn's first external effect, first call or first use of a secret, the gate appends and fsyncs a TurnBegin record with everything needed to run the turn again (I14). Each effect's result is appended and fsynced as an EffectResult before the guest sees it.
  5. Commit. The turn record carries the input, the outcome, the writes, the SQL changesets, the messages, the timer operations, the effects and the fuel used. Only after it is fsynced are the writes applied, the messages put in their receivers' mailboxes, the timers registered and the response sent. A failed turn writes a failed turn record and discards what it buffered; a failed message or timer becomes a dead letter.
  6. Recovery. At startup, a torn tail of the active segment is truncated, a corrupt record anywhere else stops the startup with STORAGE_CORRUPT, and the applier catches up from the log. From M2, every turn that has a TurnBegin but no turn record runs again with its recorded effect results before the gateway and the API accept requests.

Crates

Crate Responsibility Code since
kronik The binary: the CLI and kronik serve M1
kronik-core Ids, causal keys, logical time, error codes and kronik explain texts, and the I/O boundary Env M1
kronik-proto The Protobuf schema of the log and the code generated from it M1
kronik-log Segment format, group-commit writer, reader and recovery M1
kronik-store state.db, the applier and the SQL state per actor M1
kronik-runtime Wasmtime configuration, code store, import allowlist, host implementations of the WIT, limits M1
kronik-sched The scheduler and the turn pipeline; from M2 also the effect gate, call, compensations, timers and re-runs M1
kronik-gateway HTTP ingress, routing and idempotency at ingress M1
kronik-api The JSON API over the Unix socket, and the request and response types the CLI shares M1
kronik-sim Deterministic simulation testing of the whole daemon M1
kronik-effects Capabilities, grants and limits per epoch, delivery classes, HTTP fingerprints, secret headers and scrubbing, host-side cryptography, rate limits M2
kronik-privacy The master key, the secret store, sealed values, erasure, reseal and pseudonyms M2, M8
kronik-fork Branches, isolated and continuous replay, the diff engine, proposals M3
kronik-policy CEL rules against behavior diffs, approvals and guards M3
kronik-mcp The MCP server on top of the JSON API, at /mcp on the API socket and through kronik mcp M5
kronik-ship Shipping to and restore from S3-compatible object storage, including blobs M6, M26
kronik-foreign Foreign actors: OCI images in gVisor by default, and the opt-in Firecracker microVM M7, M20
kronik-blob Content-addressed blob files for large bodies and app blobs, outside the log M25, M26
kronik-metrics Process metrics for /metrics and for the OTLP metrics export M6, M28
kronik-otel Traces and the audit export, built from committed log records M28
kronik-cluster The Raft core, the replicated log, the members and their transport. On main, and in no release yet M16
kronik-ui The admin web interface's assets, served by the admin listener under /ui/. Shipped in v0.6.0, for a single node M27
kronik-fleet Partial fleet enrollment, transport, operational events and webhooks. Not an accepted milestone M29, M30
kronik-keyhelper Optional GNU process that talks to a PKCS#11 module. It is not linked into the musl daemon M23
kronik-guest-init Static PID 1 for a microVM guest M20
kronik-testhost Harness for the crate tests. Not part of the daemon —

M16 is built on main and in no release yet: its decisions are ADRs 0168 to 0173, its design lists what it does not cover yet (its §9), and the project status page has what was verified on three VMs. Placement, distributed replay and rolling upgrades (M17–M19) are a design still (ADR 0162).

Functional core, imperative shell. The daemon crates do all I/O through kronik_core::env::Env: clock, task spawning, file system, network, entropy and processes. Env has a real implementation on tokio and the standard library and a simulated one in kronik-sim, which runs the same daemon code on a seeded single-threaded executor with injected crashes (ADR 0004). Clippy's disallowed-methods keeps direct calls to the clock, to thread and task spawning, to sleeps and timeouts and to the OS random source out of daemon code; the real Env implementations opt out per module.

Data on disk

<data>/code/<blake3-hex>.wasm                          components, by code hash
<data>/code/<blake3-hex>.<engine-version>.cwasm        precompiled code, a cache
<data>/branches/main/log/<start lsn, 20 digits>.seg    the log
<data>/branches/main/state.db                          materialized state
<data>/branches/main/sql/<2 hex>/<actor>-<8 hex>.db    SQL state, one file per actor
<data>/keys/master.key                                 the master key (M2)
<data>/keys/secrets.db                                 the secret store (M2)
<data>/keys/keystore.db                                per-subject privacy keys (M8)
<data>/blobs/objects/<2 hex>/<name>                    immutable blob files (M25, M26)
<data>/blobs/tmp/<16 hex>.part                         a blob file being written
<data>/branches/<fork>/blobs/...                       a fork's own blob space, same layout

The data directory is /var/lib/kronik unless kronik serve --data-dir says otherwise. The log is the truth (I4): state.db and the SQL databases are derived from it and are brought up to date from it at startup, and the precompiled code is a cache for this machine only. The secret store is not in the log; the log refers to a secret by its name and a keyed hash of its version.

A segment starts with a header (magic KRNK, format version, start LSN, branch name and a CRC) and holds entries of the form [u32 len][u32 crc32c][u64 lsn][u8 kind][protobuf]. LSNs increase by one across all record kinds and segments. A segment is closed at 64 MiB or after 60 seconds, and a closed segment is never changed again.

Record Written by
TurnBegin a turn, before its first external effect or call (M2)
EffectResult a turn, before the guest sees an effect's result (M2)
TurnRecord every finished turn, committed or failed
Deployment deploy; promote and rollback from M3
Grant grant and revoke (M2)
Limits limits set (M2)
Policy policy set (M3)
Approval approve (M3)
PrivacyMode privacy mode (M8)
ForeignActor foreign add and remove (M7)
BranchCreated fork create (M3)
Erasure privacy erase (M8)
EngineUpgrade upgrade (M6)
SnapshotMarker snapshot (M6)
SimulatorConfig stubs set: a target's effect simulators (M12)
StateRebase rollback via replay: the replayed turns and the switch (M13)
GuardTriggered a kronik watch guard that fired (M11)
AuditEvent secrets, tokens, forks, proposals, reseal and snapshot by request: the mutations without a record of their own, after they succeeded; changes no state (M28)
SecretVersion a version of a secret, recorded so a later replay can fetch that version (M23)
SecretLink a secret name linked to an external backend (M23)

Long-lived connections (M24) are rows in state.db. The frames a turn sends or receives are part of that turn's record, not a record kind of their own.

A record with an enum value of 0 or an unknown enum value is STORAGE_CORRUPT; it is never read as a default.

Decisions

The architecture decision records hold the choices that the brief left open, with the alternatives and the measurements behind them:

  • ADR 0001: Wasmtime pinned at an exact version and configured for determinism (NaN canonicalization, deterministic relaxed SIMD, no threads, fuel instead of epochs), a fresh instance per turn from a pre-linked component in a pooling allocator, deterministic WASI and the deploy-time import allowlist.
  • ADR 0002: TypeScript components built with jco and ComponentizeJS, without the fetch-event and stdio features, so that they stay inside the allowlist (M4; http until M22).
  • ADR 0070: standard fetch through wasi:http/outgoing-handler as http.fetch effects, driven only while the guest waits (M22).
  • ADR 0003: foreign actors in gVisor with runsc on the systrap platform, which needs no KVM, in a network namespace with only loopback, reached through runsc port-forward --stream (M7).
  • ADR 0004: deterministic simulation testing on Kronik's own executor rather than madsim, and the Env trait set that the daemon does its I/O through.
  • ADR 0005: per-actor SQL state with SQLite sessions and changesets, one file per actor, deterministic time and randomness inside SQL, and the deny list.
  • ADR 0006: logical time adds 1 ms over the log's high-water mark only when a new process starts, not on every turn.
  • ADR 0007: policy rules in CEL, evaluated by the cel crate against the behavior diff (M3).
  • ADR 0008: the MCP server, built with the official Rust SDK (rmcp) over the in-process JSON API (M5).
  • ADR 0009: TLS in the gateway with rustls, and ACME with instant-acme (M6).
  • ADR 0010: the OpenAPI description and the CLI's JSON schemas, generated from the API's types with schemars (M5).

Those are the early records. Later decisions are in the same directory, next to the code they describe.

The design documents fix the interfaces of a milestone before it is built: M1 core design (crates, log format, ids, JSON API and the guest SDK contract), M2 effects design (the effect pipeline, re-runs after a crash, call and compensations, grants, limits, secrets and timers) and M3 fork design (forks, replay, the behavior diff, proposals, promote and rollback, and the policy interface).

Clone this wiki locally