Concurrency & Determinism Contract

zmodu provides two concurrency models designed for different state-ownership patterns:

  1. Single-owner Worker + Mailbox: A dedicated or pooled worker owns its state exclusively and processes messages sequentially.
  2. Fiber fan-out (std.Io.Group): Multiple concurrent I/O tasks run within a single request without shared mutable state.

1. Model Selection Table

Problem ShapeUseDo Not UseKey Constraint
Mutable state updated by sequential messages (order book, session state, counters)Worker + Mailbox(T, capacity)Shared struct guarded by std.Io.MutexSingle owner; full mailbox returns error.Full for explicit backpressure
Fan out multiple independent I/O calls within one request and join resultsstd.Io.Group (fibers)Spawning a Worker per requestEach task uses its own allocator/context; cancellation is cooperative
Blocking system calls, large file I/O, or synchronous C libraries.execution_class = .blocking workerRunning blocking calls on the CPU worker pool or HTTP fiberIsolates blocking waits on the dedicated blocking pool (withBlockingThreads)
Delayed or periodic workTimerWheel / PrecisionTimer / cron.Schedulersleep loops inside worker handlersTimerWheel delivers messages into the worker’s mailbox
Lock-free 1-to-N hot-path broadcastruntime.HotBus(E, N)Shared mutable subscriber array with locksCall freeze() at startup before the first publish
Cross-process event broadcastDistributedEventBus / RaftAd-hoc raw TCP socketsConfigure own_key/peer_keys or cluster_secret
Parallel AI tool execution in a single turnTool.parallel_safe = true + AgentSpec.ioUnsynchronized shared DB connection in parallel toolsAll tools in the turn must declare parallel_safe; results allocate from ctx.allocator

2. Determinism Boundaries

The runtime replay subsystem (runtime/recorder.zig + replay_driver.zig) guarantees deterministic ordering inside the execution plane:

DimensionDeterministic?Mechanism
Message delivery orderYesSequencer assigns monotonic sequence numbers; DeliveryLog records per-track order
Timer arm / cancel orderYesSingle-writer TimerWheel and FIFO command queue
Ready-ring scheduling orderYesDeterministic ring and batching rules in Scheduler
Clock readingsYes (with manual clock)Injectable runtime/clock.zig (Clock.manual in tests/replay)
External HTTP / DB / filesystem I/ONoNetwork and disk I/O outside DeliveryLog are not captured automatically
Shared-memory mutation bypassing MailboxNoGoverned by OS thread scheduling

To include external I/O in deterministic replay (as shown in examples/quant-replay), record normalized domain events at the application boundary and feed the recorded ZDL1 segment log back through ReplayFromLog.

3. Cooperative Cancellation

  • AgentHandle.requestCancel and Budget check cancellation between steps; in-flight HTTP or LLM calls rely on their socket timeout.
  • Tool.timeout_ms checks elapsed time on return; long-running custom tools should poll try ctx.checkDeadline() inside loops.
  • std.Io cancellation takes effect only at suspension points.