8000 GitHub - lazily-hub/lazily-kt: Lazy reactive primitives for Kotlin/JVM — Sources, Computeds, and Effects with automatic dependency tracking, plus the lazily-spec wire protocol, CRDTs, and distributed plane. · GitHub
[go: up one dir, main page]

Skip to content
lazily-hubPublic

About

Lazy reactive primitives for Kotlin/JVM — Sources, Computeds, and Effects with automatic dependency tracking, plus the lazily-spec wire protocol, CRDTs, and distributed plane.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

lazily-kt

Native Kotlin port of the lazily reactive core — a first-class reactive binding alongside lazily-rs, lazily-py, and lazily-zig, with no FFI dependency for the reactive graph. Plus the lazily-spec IPC wire types, a reactive full-Harel state chart, an AsyncContext async reactive graph, a lock-backed ThreadSafeContext, an in-process ShmBlobArena blob host, and an agent-doc state-projection consumer.

io.github.lazily:lazily · Kotlin 2.0.21 · JVM 21 · v0.48.0

Feature Set

Coverage by feature family across every binding, generated from coverage.json in lazily-spec. Legend: ✅ shipped · ~ partial · — absent · ⊘ not applicable. The canonical matrix with per-cell notes and platform carve-outs lives in lazily-spec § Cross-Language Coverage.

Summary — family × language

Family Rust Python Kotlin JS Dart Zig Go C++ C# GDScript
Reactive graph ✅ ~ ~ ~ ✅ ✅ ✅ ✅ ✅ ~
Materialization ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Family sync ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Statecharts ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
View ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
Keyed collections ✅ ✅ ✅ ✅ ✅ ~ ✅ ✅ ✅ —
Reactive queue ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Broadcast topic ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Work queue ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
CRDT data types ✅ ~ ~ ~ ~ ~ ~ ~ ✅ —
Lossless tree ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Egress ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ~
Ingress ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Wire codec ✅ ✅ ✅ ✅ ~ ✅ ✅ ✅ ✅ —
Transport & FFI ✅ ~ ~ ~ ~ ✅ ✅ ~ ✅ —
Message passing ✅ ✅ ✅ ✅ ✅ ~ ✅ ✅ ✅ —
Reliable sync ~ ~ ~ ~ ~ ~ ~ ~ ~ —
Durable owner ✅ — — — — — — — — —
Durable capability tiers ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Distributed plane ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Causal receipts ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Security boundary ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Membership ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Coordination ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Presence ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Temporal ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Rate shaping ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Windowing ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Resilience ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Portable stdlib ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ 10BC8 —
Service plane ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —
Instrumentation ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ —

Roll-up rule: a family cell is ✅ only when every required row in that family is ✅; ~ when the family is mixed (some shipped or partial); — when no required row is shipped or partial; ⊘ only when every required row in the family is not applicable. Rows the spec marks MAY (optional) are excluded from the roll-up — declining an optional feature is not a gap.

A family cell summarises 86 feature rows. For row-level marks, per-cell notes, and platform carve-outs see the canonical coverage matrix in lazily-spec.

CRDT convergence and the wire protocol are pinned by the shared conformance fixtures and JSON Schemas in lazily-spec and the Lean models in lazily-formal.

The reactive family

lazily-kt mirrors lazily-rs Context semantics across all three context layers — single-threaded base, lock-backed thread-safe, and coroutine-backed async:

  • Computed — a lazily-computed, memoized derived value. Tracks its dependencies automatically, computes on first read, caches, and recomputes only when read after an upstream change.
  • Source — a mutable value that invalidates dependent Computeds/Signals when it changes.
  • Effect — a side-effecting observer that reruns whenever a tracked dependency invalidates; a cleanup closure runs before each rerun and on dispose.

The core primitives are Source / Computed / Effect. Signal is a derived construct, not a core primitive — Signal ≡ Computed.eager, a guarded Computed plus a puller Effect that recomputes the instant a dependency changes, so its value is materialized by the time set / batch returns (no intermediate unset value).

Values are lazy by default; reach for the derived Signal when you need eager push semantics. SlotHandle and CellHandle remain compatibility aliases; Computed, Source, SignalHandle, and EffectHandle are the canonical lightweight ids over the shared node table.

Why it behaves the way it does

  • Pull-based, glitch-free refresh — a Computed that reads other nodes always observes values consistent with the current inputs.
  • == guard on set — setting an equal value is a no-op (no downstream cascade).
  • computed has a == guard — an equal recompute suppresses downstream invalidation.
  • batch coalesces invalidations into one effect flush.
  • Dynamic dependencies — the tracking stack auto-discovers edges on each recompute (no stale subscriptions); cycles are detected and throw.

Core qualification evidence

scripts/core-conformance.json exposes a shell-free report command for the core-v1 profile. It reruns the canonical reactive-graph corpus against Context, ThreadSafeContext, and AsyncContext, executes the assertion mutation probes, and writes fresh report-root evidence. Fifteen of the sixteen Core fixtures currently pass. The bounded feedback-drain fixture is reported as an explicit whole-fixture skip until Kotlin can stop and attribute a self-rescheduling effect drain. The optional native C-ABI proof is intentionally unclaimed: the JVM boundary tests do not build and invoke an exported native symbol.

Usage

Latest-durable projection egress

LatestDurableProjectionCore<K, V> implements the lazily-spec latest-state projection contract for durable sinks. upsertDesired accepts monotone per-key epochs, claim moves one desired value into the single-flight slot, and ackApplied or failRetryable completes that fenced attempt. reconnect advances the connection generation and requeues only flights that were not superseded by newer desired state. Stale generation or epoch acknowledgements cannot clear newer work.

Use LatestDurableProjection, ThreadSafeLatestDurableProjection, or AsyncLatestDurableProjection when callers also need reactive entry(key) and generation() views. Sink side effects remain outside the graph: serialize the four mutation operations and the actual sink write on the same per-key lane.

Reactive egress

EgressCore<T> is the send-side mirror of ingress (#lzegress): monotone sequence assignment, a bounded unacknowledged window, a cumulative ACK watermark, bounded retry/backoff/exhaustion, and a producer-generation fence. EgressCell, ThreadSafeEgressCell, and AsyncEgressCell project pending, inflight, ackedThrough, and retry as Computeds and clear exactly the reader kinds each transition changes. attachTransport installs the one transport Effect for the current incarnation; after reconnect the old attachment is inert and a replacement replays the retained records. Only the async flavor's attachment is async-coloured (AsyncEgressTransport).

val ctx = Context()
val egress = EgressCell<String>(ctx, generation = 1, EgressPolicy(inflightLimit = 8))
egress.attachTransport { envelope -> socket.trySend(envelope) } // true = accepted
egress.enqueue("hello")       // claimed and sent by the attachment Effect
egress.ack(generation = 1, through = 0) // reopens the window reactively

Generic effect compare-and-swap

EffectCasCore<K, A, P, R, E> (#lzeffectcas) owns stable effect identity, keyed single-flight, latest-wins supersession, logical-clock retry, reconnect fencing, exact terminal receipts, and resumable ordered plans around an opaque authority token A. The application adapter (CompareAndApply) only observes the token and performs one atomic compare-and-apply. durableState() / EffectCasCore.hydrate(...) round-trip the full retained image for crash recovery; a successor then reconnects at a newer generation and replays the same effect identity.

import io.github.lazily.Context

val ctx = Context()
val a = ctx.source(2)
val b = ctx.source(3)

// Lazy: computes on first read, caches, recomputes only when a or b changes.
val sum = ctx.slot { ctx.get(a) + ctx.get(b) }
ctx.get(sum) // 5

ctx.set(a, 10)
ctx.get(sum) // 13

// Eager: recomputes immediately when a dependency changes.
val parity = ctx.signal { if (ctx.get(a) % 2 == 0) "even" else "odd" }
ctx.getSignal(parity) // "even"
ctx.set(a, 11)
ctx.getSignal(parity) // "odd" — already updated before the read

A side-effecting observer with cleanup:

val handle = ctx.effect {
    val v = ctx.get(a)
    // returned closure is the cleanup: runs before the next rerun and on dispose
    { println("a was $v") }
}
ctx.set(a, 42)   // reruns the effect (previous cleanup ran first)
ctx.disposeEffect(handle)

Coalesce multiple writes into one flush:

ctx.batch {
    set(a, 1)
    set(b, 2)
} // dependent effects fire once

State machine

StateMachine<S, E> is a finite state machine backed by a Cell, so any slot, signal, or effect that reads state is invalidated on transition. The transition is pure (state, event) -> next?; returning null rejects the event (guard), and an equal self-transition is accepted but suppressed by the cell's == guard:

val m = StateMachine(ctx, "Red") { s, _: String ->
    when (s) {
        "Red" -> "Green"; "Green" -> "Yellow"; "Yellow" -> "Red"
        else -> null
    }
}
m.send("advance") // true
m.state           // "Green"
m.stateIs("Green") // a Signal<Boolean> that tracks the predicate

State chart

StateChart is a Harel/SCXML hierarchical state machine — the native counterpart of lazily-formal's LazilyFormal.StateChart and lazily-rs/src/statechart.rs. A chart is compute, not protocol: it is never serialized as a distinct wire kind, and its active configuration lives in a Cell, so any slot/signal/effect reading configuration, activeLeaves, or matches is invalidated on a real transition.

Implemented subset (per the spec's implementation-status note): compound states, orthogonal (parallel) regions, shallow + deep history (record-on-exit / restore-on-enter), entry/exit/transition actions (exit innermost-first → transition → entry outermost-first), named guards (fail-closed), and external + internal transitions. run actions and {"expr": …} context guards are rejected explicitly; final states are accepted as leaves (completion/done events are not raised, the deferred slice the spec permits).

import io.github.lazily.Context
import io.github.lazily.ChartDef
import io.github.lazily.StateChart
import kotlinx.serialization.json.Json

val def = ChartDef.fromJson(Json.parseToJsonElement(chartJson))
val ctx = Context()
val chart = StateChart(ctx, def)

chart.activeLeaves(ctx)                       // initial leaves
chart.send(ctx, "TICK", emptyMap())           // true if any transition was taken
chart.matches(ctx, "playing")                 // hierarchical "state-in" predicate
chart.lastActions()                           // exit → transition → entry actions

send is deterministic by construction — a total function of (chart, configuration, history, guards, event), mirroring the Lean StateChart.send.

lazily-spec IPC

Ipc.kt implements the language-agnostic lazily-spec wire types — Snapshot, Delta, DeltaOp (all seven variants: CellSet, SlotValue, Invalidate, NodeAdd, NodeRemove, EdgeAdd, EdgeRemove), IpcMessage, NodeState (Payload / SharedBlob / Opaque), IpcValue (Inline / SharedBlob), ShmBlobRef, PeerPermissions, the optional wire-stable NodeKey (key field on NodeSnapshot/NodeAdd, omitted in JSON when absent), and the multi-writer CrdtSync plane (WireStamp, CrdtOp, CrdtSync, and the IpcMessage.CrdtSyncMessage variant). Every type round-trips the canonical externally-tagged JSON shape via toJson() / fromJson(); IpcMessage adds encodeJson() / decodeJson() for direct transport, and the JSON codec is byte-compatible with lazily-rs.

StateGraphMirror is a pure native mirror that applies a snapshot and then delta ops to a local node/edge view, and Snapshot / Delta / CrdtSync expose filterReadable(permissions, peer) for per-peer capability filtering.

Shared-memory blob arena

ShmBlobArena is the in-process host for the shared-memory blob plane — the Kotlin counterpart of lazily-rs::ShmBlobArena. It writes a fixed 40-byte LZSH header ({ magic, version, header_len, generation, epoch, len, checksum }, little-endian) before each payload and validates the header + FNV-1a-64 checksum on read, so IpcMessage control frames carry compact ShmBlobRef descriptors instead of embedding large bytes inline. The byte layout and checksum are identical across lazily-rs / lazily-py / lazily-zig / lazily-kt, pinned by conformance/arena_blob.json (ShmBlobArenaTest). This host is heap-backed (a ByteArray); a future transport may back it with a memory-mapped ByteBuffer for true cross-process sharing without changing the contract.

Async reactive context

AsyncContext is a separate reactive surface for suspend-returning computations — the Kotlin counterpart of lazily-rs::AsyncContext and the lazily-spec Async Reactive Context contract. It is compute, not protocol: only resolved slot values cross IPC/FFI as ordinary cell payloads. Cells are the synchronous input layer (cell / get / set); computedAsync / memoAsync slots and effectAsync effects are async. It implements the full contract: the Empty / Computing / Resolved / Error slot state machine with revision-based stale-completion discard, in-flight deduplication (concurrent getAsync callers share one compute), the five-point cancellation contract (waiter-cancellation-safe, stale-discard, explicit cancel, disposal-awaits-cleanups, cleanup-before-body), compute-context dependency tracking registered before each awaited read, executor-scheduled serialized async effects, and synchronous batch that schedules async reruns only at the outermost exit. signalAsync is the eager (memo slot + puller effect) counterpart of the synchronous Signal.

val ctx = AsyncContext()
val a = ctx.source(2)
val sum = ctx.computedAsync { get(a) + 3 }
ctx.getAsync(sum)             // suspends, computes, caches -> 5
ctx.set(a, 10)
ctx.getAsync(sum)             // dependency invalidated -> 13

State-projection consumer (optional FFI)

StateProjectionClient and StateProjectionBridgeSupport consume the agent-doc binary's DocumentStateProjection over its C ABI. LazilyFFI provides the JNA bindings to that C-ABI surface, and ships in the io.github.lazily:lazily-ffi feature variant rather than the main jar (#lzktoptionaljna). This is an optional transport for consuming authoritative projections from the Rust binary — it is independent of the reactive core. A state chart or any other compute runs natively, never via this FFI channel (routing chart logic through JNA to a Rust Context would be circular).

Conformance

lazily-kt replays the shared lazily-spec conformance fixtures:

  • IPC fixtures round-trip through IpcMessage.fromJson / toJson (IpcConformanceTest).
  • The sample workflow state-projection IPC fixtures (conformance/agent-doc/snapshot_agent_doc_state.json, conformance/agent-doc/delta_agent_doc_state.json) decode, round-trip, and validate their type_tag vocabulary + decoded payload phases (AgentDocStateConformanceTest).
  • The ShmBlobArena host fixture (conformance/arena_blob.json) is replayed byte-for-byte — descriptor, 40-byte LZSH header, payload region, FNV-1a-64 checksum, and round-trip read (ShmBlobArenaTest).
  • State-chart fixtures read from the canonical sibling ../lazily-spec/conformance/statechart/ are replayed by StateChartConformanceTest, asserting accepted, active, matches, and actions identically to every other binding.
  • The Async Reactive Context contract (slot state machine, stale discard, cancellation, dependency tracking, effect cleanup ordering, batch) is covered by AsyncContextTest.
  • The Thread-safe Reactive Context contract (lock-backed Cell/Slot/Signal/Effect, ==/memo guards, glitch-free refresh, synchronous eager flush, reentrant callbacks, atomic cross-thread batch, clonable handles, per-thread dependency tracking) — the spec's thread_safe = host row — is covered by ThreadSafeContextTest, including multi-thread convergence, cross-thread handle reads, and a thread-safe ThreadSafeStateMachine.
  • The keyed cell collections layer (SourceMap / ComputedMap / SourceTree / keyed reconciliation) replays the shared conformance/collections/ fixtures (CollectionsConformanceTest) — value / set-membership / order reactivity independence, stable handles, and atomic move.
  • The CRDT and semantic-tree collection models replay the remaining shared conformance/collections/ fixtures (CollectionsCrdtConformanceTest) — the move-aware sequence CRDT (seqcrdt_convergence), the Fugue/RGA character CRDT (textcrdt_convergence), the memoized semantic tree (semtree_incremental), and manufactured text identity (stableid_alignment). All seven conformance/collections/ fixtures are now replayed, covering the full Binding Conformance Matrix keyed-collections + CRDT rows.
  • QueueFamilyConformanceTest replays all eleven canonical queue-family fixtures (five queuecell_*, four topiccell_*, two workqueue_*) against Context, ThreadSafeContext, and AsyncContext. Its capability/skip ledger, positive step counts, exact steps[].expected.invalidates checks, and concurrency probes prevent a staged or non-reactive flavor from reporting green.
  • The distributed CRDT plane runtime (LWW / MV / PN-counter registers, HLC clock, StampFrontier, causal-stability watermark, idempotent ingress into a reactive root cell) is covered by CrdtRuntimeTest.
  • The C-ABI FFI host boundary (LazilyFfiBytes / LazilyFfiStatus / LazilyFfiMessageKind incl. CrdtSync = 3, decode→IpcMessage→canonical JSON re-encode, panic-guarded) is covered by LazilyFfiBoundaryTest.
  • The replay-equivalence proof replays conformance/replay/ — the log-binding, divergence-localization, and canonical-encoding fixtures (ReplayConformanceTest), driving the corpus's own accumulator / drifting_accumulator subjects. Each record step also cross-checks that the recorded sum digest IS the digest of the subject's final sum, so a harness that fingerprinted some other value cannot pass.
  • SimConsumerTestkit runs one generated history through a deterministic in-memory consumer and explicitly selected Postgres, NATS, or external-process adapters. Its narrow SimConsumerWorldEvidence seam proves world execution without prescribing a scheduler; SimConsumerTestkitConformanceTest replays all five conformance/simulation/consumer_testkit.json scenarios, including first-step observation, history-prefix, and simulation-bypass failures.

Not yet implemented: the ffi = host symbol export is provided as a JVM embeddable channel + C header + JNI-ready native entry table (src/main/resources/native/lazily_ffi.h); real extern "C" symbol export ships via a Graal native-image build of the artifact.

Thread-safe reactive context

ThreadSafeContext (ThreadSafeContext.kt) is the lock-backed counterpart of Context — the thread_safe capability the spec requires of any binding whose platform exposes preemptive multi-threading. The JVM/Kotlin runtime structurally supports OS threads and a shared heap, so lazily-kt declares thread_safe = host (not none).

It satisfies the spec contract: a single ReentrantLock serializes every graph mutation and read, so observers fire synchronously within the invalidating set/batch, preserving glitch-free pull-based ordering. The JVM memory model's monitor happens-before guarantee is the counterpart of Rust's Send + Sync obligation. Handles (ThreadSafeComputed / ThreadSafeSource / ThreadSafeEffectHandle / ThreadSafeSignalHandle) are value classes — clonable by value — so a handle minted on one thread may be read on another through the shared context. A ThreadLocal tracking stack mirrors lazily-rs's thread_local! tracking, so two threads computing concurrently never mix their dependency edges. ReentrantLock is reentrant, so a compute/effect callback that re-enters the same context (e.g. a slot reading another slot) does not self-deadlock. batch runs its whole block under the lock, so a batch is atomic across threads.

val ctx = ThreadSafeContext()
val src = ctx.source(1)
val doubled = ctx.signal { ctx.get(src) * 2 }  // eager, materialized
val eff = ctx.effect { println("now ${ctx.get(doubled)}"); null }

// From any thread: clonable handle, synchronous observer.
ctx.set(src, 21)   // observer fires synchronously before this returns

ThreadSafeStateMachine mirrors StateMachine over a ThreadSafeContext — the flat FSM whose send/state/onTransition/stateIs are safe to call from any thread sharing the context.

CRDT sequence / text + manufactured identity + semantic tree

Beyond the single-value register plane, lazily-kt implements the cell-model's mergeable sequence and text surfaces — the native counterparts of the lazily-rs models and the conformance/collections/ compute fixtures:

  • SeqCrdt — a move-aware, mergeable ordered sequence (#lzseqcrdt). Each element is three independent LWW registers (value, fractional-index position, tombstone). A move is a single LWW reassignment of position (not delete+reinsert), so concurrent moves of the same element converge to the later stamp without duplication, and a concurrent move + value edit both apply. Merge is commutative, associative, and idempotent; the caller-driven HLC keeps behaviour deterministic.
  • TextCrdt — a Fugue/RGA-style character CRDT (#lztextcrdt) for concurrent free-text edits. Each character is an element with a unique OpId
    • left-origin; order is a pure deterministic function of the element set, so merge (a union of elements, tombstones sticky) converges regardless of delivery order. Includes causally-stable tombstone GC. It also implements the CrdtTree document contract: identity-preserving merge, version-vector delta, empty-frontier snapshot, and materialized value share one state model.
  • SemTree — a memoized semantic derivation over a SourceTree (#lzsemtree). One memo slot per node folds (node value, child derived values); editing one node recomputes only its ancestor chain (a sibling subtree stays cached), and the memo guard stops propagation when the folded result is unchanged.
  • StableId — manufactured identity for markdown text (#lzstableid): in-band anchors (survive body rewrite), content-derived hashes of normalized text (survive reflow/reorder), and word-LCS similarity alignment (≥ 0.5 ⇒ Edited, else Inserted). assignStableKeys flows identity through an edit so the reconciler emits Update, not remove+insert.

Durable outbox stores

io.github.lazily.outbox.Outbox<S> owns the shared append/ack/prune/replay protocol while OutboxStore supplies ordered byte persistence. The default InMemoryOutbox follows that same protocol. Android hosts can implement the small RoomOutboxDao boundary and wrap it in RoomStore; Room annotations and database ownership stay in the application, so the portable JVM artifact does not acquire an Android dependency.

Replay-equivalence proof

ReplayHarness / ReplayLog / ReplayFingerprint (Replay.kt) make replay equivalence provable rather than assumed, per lazily-spec/docs/replay-equivalence.md: given the same event log, a rebuilt graph observes the same values at every checkpoint, and any deviation is a defect in the graph rather than a tolerance.

class Projection : ReplayGraph {
    private var sum = 0L

    override fun apply(event: ReplayEvent) {
        sum += event.payload as Long
    }

    override fun observe(): Map<String, Any?> = mapOf("sum" to sum)
}

val log = ReplayLog.fromRecords(listOf("add" to 1L, "add" to 2L, "add" to 3L))
val harness = ReplayHarness(::Projection)

val fingerprint = harness.record(log) // pin it, or commit `toWire()`
harness.verify(log, fingerprint)      // throws unless the replay is identical
harness.prove(log)                    // record + re-replay, no fingerprint needed
  • The fingerprint is bound to its log. verify compares the log digest before any observed value, so a fingerprint recorded against a different log throws ReplayLogMismatchException and is never compared. It matters because [+1,+2,+3] and [+3,+2,+1] settle to the same sum: a value-only comparison would pass and certify nothing about the log in front of it. check, the non-raising reporting form, refuses a stale fingerprint too.
  • Divergence is localized. Every event is checkpointed (stride = 1), and ReplayDivergenceException.first names the first diverging checkpoint's seq and the cell label that differed, not merely the end state where the defect is still visible. stride is itself part of the fingerprint, so one sampled at stride 2 is refused by a stride-1 harness (ReplayStrideMismatchException). Each fault is a distinct exception type, so a driver routes on the type rather than on a message string.
  • The encoding is canonical, or it fails. canonicalBytes is type-tagged and length-framed, with mapping and set members ordered by their own encoded bytes: {a:1,b:2} equals {b:2,a:1}, [1,2] does not equal [2,1], and 1, "1", 1.0, true and the byte string 1 are five different values. A value the encoding does not define raises ReplayEncodingException instead of falling back on toString(), whose JVM default embeds an identity hash and would report a false divergence on every run. A host type opts in by implementing ReplayCanonical.
  • Hashing is SHA-256 from java.security.MessageDigest. The spec leaves both hash and byte layout binding-chosen — fingerprints are pinned next to a test in one language, never exchanged between bindings — so matching lazily-py's BLAKE2b-256 digests would buy a new dependency for nothing.
  • The outbox is the intended log source. DurableOutbox.replayFrom already is a replay source; replayLogFromOutbox(outbox, cursor = 0) makes it a fingerprinted one, with outbox epochs as event seqs so a truncated prefix shows up in the log digest instead of silently shifting every event.

Checkpoint values only: sibling effect order is deliberately free across the family, so the sequence of effects a replay fires is not a stable thing to fingerprint and this contract does not ask a binding to.

Keyed cell collections

SourceMap / ComputedMap and SourceTree are the native implementation of the lazily-spec keyed cell collections layer (Cell Model § Keyed cell collections) — a composition of cells, not a new cell kind. Each entry is an ordinary cell; a dedicated membership cell tracks the key set; a dedicated order cell tracks the ordered key list. The three reactive planes are independent by construction:

  • writing an entry value invalidates only that entry's value readers;
  • inserting / removing a key invalidates membership + order readers, never unrelated entry value readers;
  • a pure reorder (atomic move) invalidates order readers only — membership readers (len / contains) and value readers are untouched, and the moved entry keeps its same cell handle (not remove + re-mint).
val ctx = Context()
val map = SourceMap(ctx, listOf("a" to 1, "b" to 2, "c" to 3))

map.setValue("a", 10)        // value reader of "a" only
map.insert("d", 4)           // membership + order readers
map.moveTo("b", 3)           // order reader only; "b" keeps its handle
map.keysNow()                // [a, c, d, b]

reconcile(prior, target) diffs two keyed sequences by stable key, emitting the minimal move-minimized {insert, remove, move, update} op set (longest- increasing-subsequence over prior indices preserved); applying it to a live SourceMap keeps stable entries' value cells un-invalidated. SourceTree composes the same guarantees node-by-node for an ordered keyed tree.

Reactive queue

QueueCell is the native implementation of the lazily-spec reactive queue (Cell Model § Reactive queues) — a FIFO collection composed of reactive cells, not a new cell kind. It is specified as a single-producer, single-consumer (SPSC) primitive; MPSC (multi-producer) is a usage rule on the same primitive — multiple producers push inside one batch { … } and the batch serializes the pushes into a deterministic order. There is no separate MPSCQueueCell type.

The reactive shell owns demand-driven reader-kind computeds (head / len / is_empty / is_full) plus a closed source and invalidates by reader kind: a push to a non-empty queue does NOT invalidate the head reader (head unchanged); a pop does. Each successful op clears exactly the computeds whose values changed in one multi-root frontier walk; an unobserved reader is never eagerly derived. The storage backend is pluggable via QueueStorage; the default VecDequeStorage is unbounded, and a bounded form exposes reactive backpressure via is_full.

The same API shape is available as ThreadSafeQueueCell / AsyncQueueCell, ThreadSafeTopicCell / AsyncTopicCell, and ThreadSafeWorkQueueCell / AsyncWorkQueueCell. Async reader derives are synchronous because their values are already in memory; when composed inside an async computed, overloads accepting AsyncComputeContext register the edge.

val ctx = Context()
val q = QueueCell.unbounded<String>(ctx)

// SPSC: total FIFO.
q.tryPush("a")
q.tryPush("b")
assertEquals("a", q.head())
assertEquals(2, q.len())

assertEquals("a", (q.tryPop() as QueuePop.Value).value)
assertEquals("b", (q.tryPop() as QueuePop.Value).value)
assertTrue(q.isEmpty())

// MPSC: multiple producers push inside one batch → one invalidation pass.
ctx.batch {
    q.tryPush("p1-a")
    q.tryPush("p2-a")
    q.tryPush("p1-b")
}
assertEquals(3, q.len())

// Bounded queue → reactive backpressure via is_full.
val bq = QueueCell.bounded<Int>(ctx, 2)
bq.tryPush(1); bq.tryPush(2)
assertTrue(bq.isFull())            // at capacity
assertEquals(QueuePushError.Full, bq.tryPush(3))
(bq.tryPop() as QueuePop.Value)    // pop frees a slot
assertFalse(bq.isFull())           // is_full invalidated (true → false)

The shell / storage split (QueueStorage interface + VecDequeStorage default) is the integration seam for future backends — a distributed RaftQueueStorage or an external-broker adapter (KafkaStorage, etc.) plugs into the same reactive shell without changing the API.

WorkQueueCell supplies the competing-consumer sibling: workers pull exclusive FIFO leases, settle them with worker-owned delivery IDs, and unacked items redeliver after the strict visibility deadline. Repeated failures route to the DLQ at maxDeliveries; pendingLen, isEmpty, inFlightLen, and deadLetterLen are independent reactive reads.

val work = WorkQueueCell<String>(ctx, visibilityTimeout = 30, maxDeliveries = 3)
work.push("render-report")
val delivery = requireNotNull(work.claim("worker-a", now = 100))
check(work.ack("worker-a", delivery.deliveryId))

The instance is the local serialization point. Distributed/HA assignment must put claim behind a leader or consensus log.

Transport-agnostic reactive ingress

IngressCell is the native implementation of the lazily-spec transport-agnostic ingress family (transport-ingress.md, #designimplementtransport). A client consuming a remote stream usually grows four accidental mechanisms — a refresh() loop, a hand-rolled relevance check, a reconnect path that forgets what it applied, and one copy of all three per transport. Each of those is a derive being simulated with a call, and this family makes them derives while keeping the transport a value the primitive never touches.

The admission algebra lives in the graph-agnostic IngressCore and the reactivity lives in the shells (IngressCell / ThreadSafeIngressCell / AsyncIngressCell), the same split the queue and map families make: invalidation is a graph write, so every core mutator returns an IngressChange — which reader kinds the transition dirtied — and each shell clears exactly that set in one frontier walk, so no reader ever observes "new value, old authority".

Each keyed scope exposes four reader kinds (value / readiness / authority / retry) plus three independent receipt channels (accepted / dropped / error) and a derived IngressSchedule. The negative cases are the contract: a buffered out-of-order envelope invalidates nothing and mints no receipt, a tick inside the freshness horizon invalidates nothing, an empty drain invalidates nothing, and a suspend invalidates readiness only.

Admission applies a normative order — lifecycle → generation fence → freshness → generation handoff → dedupe → ordering → backpressure → merge. Two orderings are load-bearing: the fence outranks dedupe (else a zombie producer reads as a duplicate) and freshness outranks ordering (else an expired envelope takes a reorder slot). A generation handoff is a baseline reset — it discards the superseded incarnation's buffered successors and its undrained window. Backpressure reuses the relay Overflow policy, validated against the merge algebra's conflates flag at construction exactly as RelayCell does, and Block refuses without advancing the watermark so the producer's retry stays in order. A drain is an egress, not an ack: it never moves the watermark.

Admission is not async-coloured. Whether an envelope is admissible is a function of the fence, the watermark, the reorder buffer, and the observed clock — nothing to await — so the async flavor uses synchronous computes on the async graph and returns plain values like the other two. Awaiting belongs to the transport, which is outside the primitive by construction.

val ctx = Context()
val ingress = IngressCell<String, Long>(ctx, IngressPolicy(freshnessHorizon = 100), sum())

// In-order delivery folds into one coalesced window under ⊕.
ingress.admit(IngressEnvelope("alpha", generation = 1, sequence = 0, stampedAt = 0, payload = 5))
ingress.admit(IngressEnvelope("alpha", generation = 1, sequence = 1, stampedAt = 0, payload = 7))
assertEquals(12L, ingress.value("alpha"))
assertEquals(IngressReadiness.Ready, ingress.readiness("alpha"))

// Out of order buffers, and invalidates nothing a reader can observe.
ingress.admit(IngressEnvelope("alpha", 1, 4, 0, 9))   // Buffered(gapFrom = 2)

// A zombie producer is fenced before its sequence is even consulted.
val zombie = ingress.admit(IngressEnvelope("alpha", 0, 0, 0, 99))
assertEquals(IngressAdmission.Dropped(IngressDropReason.StaleGeneration), zombie)
assertEquals(1, ingress.dropped().size)   // dropped only; accepted is untouched

// A drain is an egress: the watermark does not move, so a replay resumes here.
assertEquals(12L, ingress.drain("alpha"))
assertEquals(1L, ingress.view("alpha")?.deliveredThrough)

// The transport is a value. `pump` admits a decoded batch, then asks the
// transport to replay whatever gap the algebra still reports.
val transport = InProcIngress<String, Long>(IngressTransportKind.EventChannel)
transport.push(IngressEnvelope("beta", 1, 0, 0, 1))
transport.push(IngressEnvelope("beta", 1, 2, 0, 4))
ingress.pump(transport)
assertEquals(listOf("beta" to ReplayRequest(1, 1)), transport.replays())

The canonical conformance/ingress/*.json corpus is replayed against all three flavors by IngressFamilyConformanceTest, with invalidates asserted per reader kind in both directions through a cache-validity probe (so over-invalidation is as visible as under-), a positive replayed-step count per flavor, and a three-row capability ledger enforced by grepping src/main/kotlin in both directions.

Distributed CRDT plane

The CrdtSync wire types live in Ipc.kt; Crdt.kt is the runtime integration slice (#lzcrdtplane5b) — the merge: crdt ingress mechanism (Cell Model § Multi-write cells): local edits mint CrdtOps; remote CrdtOps merge into a ReplicatedCell and the converged value is fed into the reactive graph as an ordinary cell update (equality-guarded, so an equal merge invalidates nothing). It includes a hybrid logical clock (CrdtClock), the per-peer StampFrontier (per-peer max), the causal-stability watermark (min over membership — fail-closed when a member is unobserved), the tombstone GC contract, and the LWW / MV / PN-counter register types. Merge is commutative, associative, and idempotent; out-of-order, duplicated, or batched delivery all converge.

Distributed plane — transport + signaling (seam + platform adapter)

CrdtPlaneRuntime (CrdtPlane.kt) is the per-session runtime glue over those primitives: it owns the plane clock/frontier/membership, an op-log (dedup by (node, stamp)), a NodeId↔NodeKey index, and a registry of replicated root cells. localUpdate mints a broadcastable CrdtOp (or null for a value-preserving edit); ingest folds a peer's CrdtSync frame in exactly once (idempotent re-delivery applies 0), advancing the clock, frontier, and membership; syncFrame / syncFrameSince / syncReply drive anti-entropy.

The networking is a consumer-provided seam — no bundled native WebRTC library. DataChannel (DataChannel.kt) is the ordered byte-frame surface a real RTCDataChannel backend must supply; WebRtcSink / WebRtcSource (WebRtcTransport.kt) bridge it to IPC with outbound per-peer permission filtering (omission, not redaction). The signaling wire protocol (ClientMessage / ServerMessage, kebab-case tags), the RoomCore server router (anti-spoof to→from rewrite, roster-excludes-self, unknown_target), and a SignalingClient over a SignalingSocket seam live in Signaling.kt. Everything is testable via zero-dependency in-memory loopbacks (InMemoryDataChannel, InMemorySignalingSocket); wiring a real WebSocket / WebRTC backend is the deliberate follow-up.

C-ABI FFI boundary

LazilyFfiBoundary.kt exposes lazily-kt's own C-ABI FFI host boundary (protocol § FFI Boundary, ffi.json): LazilyFfiBytes / LazilyFfiStatus / LazilyFfiMessageKind (with the required CrdtSync = 3 discriminant), explicit allocation ownership, panics caught before crossing the C ABI (LazilyFfiChannel.panicGuard), and a channel that decodes each accepted frame as IpcMessage and re-encodes canonical JSON bytes. The JVM channel is the conformance-tested surface; the extern "C" symbols in lazily_ffi.h are exported via a Graal native-image build or the JNI shim (LazilyFfiNative). lazily-kt's platform CAN host a native in-process boundary, so it declares the ffi = host capability.

Optional feature — protobuf graph-boundary codec

The default dependency graph of io.github.lazily:lazily is three artifacts: kotlin-stdlib, kotlinx-coroutines-core (the async plane) and kotlinx-serialization-json (the wire plane and the state-chart parser). jna is no longer among them — see the FFI section below (#lzktoptionaljna). The protobuf graph-boundary codec (ProtobufGraphBoundaryProjection, PROTOBUF_GRAPH_BOUNDARY_FEATURE) is not in it — it lives in a Gradle feature variant, so protobuf-kotlin and protobuf-java are downloaded only by consumers that ask for it (#lzktoptionaldeps).

The split lands in the release AFTER v0.42.0 — the snippets below apply from that version on. In v0.42.0 and earlier the codec is in the main jar and protobuf-kotlin is an unconditional runtime dependency.

Gradle consumers request the capability:

dependencies {
    implementation("io.github.lazily:lazily:$lazilyVersion")
    implementation("io.github.lazily:lazily:$lazilyVersion") {
        capabilities { requireCapability("io.github.lazily:lazily-protobuf-codec") }
    }
}

Maven consumers add the classifier artifact and protobuf-kotlin explicitly; the POM lists it as <optional>true</optional>, which Maven does not resolve transitively.

<dependency>
  <groupId>io.github.lazily</groupId>
  <artifactId>lazily</artifactId>
  <version>${lazily.version}</version>
  <classifier>protobuf-codec</classifier>
</dependency>
<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-kotlin</artifactId>
  <version>4.31.1</version>
</dependency>

Optional feature — JNA FFI binding

jna was the largest artifact in the default graph at ~1.9 MB: the jar embeds native stubs for every supported platform, and exactly one source needed it — LazilyFFI, the JNA binding to the agent-doc binary's C ABI. It now lives in its own feature variant, so only consumers that actually call the binary download it (#lzktoptionaljna).

LazilyFfiChannel and LazilyFfiNative are unaffected and remain in the main jar. Despite the name, that plane is pure JVM: LazilyFfiNative is the entry-table shape a native peer links against, and it delegates to LazilyFfiChannel, which encodes and decodes IpcMessage in Kotlin. Embedding the FFI channel in-process needs no capability and no jna.

The split lands in the release AFTER v0.42.0 — the snippets below apply from that version on. In v0.42.0 and earlier LazilyFFI is in the main jar and jna is an unconditional runtime dependency.

Gradle consumers request the capability:

dependencies {
    implementation("io.github.lazily:lazily:$lazilyVersion")
    implementation("io.github.lazily:lazily:$lazilyVersion") {
        capabilities { requireCapability("io.github.lazily:lazily-ffi") }
    }
}

Maven consumers add the classifier artifact and jna explicitly; the POM lists it as <optional>true</optional>, which Maven does not resolve transitively.

<dependency>
  <groupId>io.github.lazily</groupId>
  <artifactId>lazily</artifactId>
  <version>${lazily.version}</version>
  <classifier>ffi</classifier>
</dependency>
<dependency>
  <groupId>net.java.dev.jna</groupId>
  <artifactId>jna</artifactId>
  <version>5.15.0</version>
</dependency>

Both sets above are pinned by scripts/check-published-dependencies.py, which reads the generated POM and Gradle Module Metadata rather than build.gradle.kts, and compares them in both directions — a new unconditional dependency and a pin nothing declares each fail it. It runs in make check and in CI.

Development

See CONTRIBUTING.md for toolchain requirements, local checks, formal-model setup, and the Core API compatibility workflow.

Benchmarks

Performance benchmarks mirroring the lazily-rs benches/ coverage (BENCHMARKS.md for full results):

make benchmark          # reactive-core micro-bench (parity with context.rs)
make benchmark-scale    # spreadsheet-scale bench, default N=1,000,000 (scale.rs)

The micro-bench (Benchmarks.kt) covers cached reads, cold first get, dependency fan-out, set-cell invalidation, memo equality suppression, effect flushing, batch storms, typed cache reads, and thread-safe contention. The scale bench (ScaleBench.kt) models a spreadsheet of 2N reactive nodes and highlights the lazy-pull viewport win (off-viewport formulas stay dirty and never recompute). Override the graph size with LAZILY_SCALE_N.

The lazily family

lazily is one reactive kernel — Source / Computed / Effect, keyed collections, state charts, CRDTs, and a distributed plane — implemented natively in each language and held to a single cross-language contract:

  • lazily-spec — the wire protocol, the generated feature matrix, and the conformance corpus every binding replays.
  • lazily-formal — the Lean 4 formal model the bindings share.
repo language
lazily-rs Rust — the reference implementation
lazily-py Python
lazily-go Go
lazily-kt Kotlin / JVM — you are here
lazily-js JavaScript / TypeScript
lazily-cs C# / .NET
lazily-cpp C++
lazily-zig Zig
lazily-dart Dart / Flutter
lazily-react React / Preact bindings layered over lazily-js — not a separate language binding

Per-binding feature parity is tracked in the coverage.json-generated matrix in lazily-spec; read it there rather than any hand copy.

See also

  • lazily-spec — language-agnostic wire protocol + the conformance fixtures (IPC and state-chart) every binding replays.
  • lazily-formal — Lean 4 formal model (shared primitives, flat FSM kernel, full Harel StateChart); the executable reference behind the state-chart fixtures and the deterministic send lazily-kt inherits.
  • lazily-rs / lazily-py / lazily-zig — sibling reactive cores.

About

Lazy reactive primitives for Kotlin/JVM — Sources, Computeds, and Effects with automatic dependency tracking, plus the lazily-spec wire protocol, CRDTs, and distributed plane.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0