Simulation kernel (phase 1)

Specification of crates/sim: how simulated time passes, how messages travel, what agents can see, and what is guaranteed to be reproducible. The matching rules themselves are in MATCHING_RULES.md.

1. Time and ordering

2. Path of a message

agent callback at t
 └─ compute delay (agent profile; sampled once per callback)
     └─ order_out link ──────────────► gateway session: one message at a time, gateway_ns each
                                          throttle → pre-trade risk → client id ↦ exchange id
          ◄── order_in link ─── reply ─┤  (throttle/risk rejections and "unknown order" answers)
                                          └─ hop_ns (constant) ─► matching-engine queue
                                                                  (one per instrument, FIFO)
            service starts: the command takes effect on the book at this instant
            service time = engine_base_ns + engine_per_event_ns × events produced
            service ends: results leave the engine
 private events ─► owner's gateway session ─► execution reports ─► order_in link ─► agent
 public events  ─► one packet per command ─► each subscriber's feed link ─► agent's book view
Hop Model Notes
Compute LatencyProfile::compute The agent “thinks” once per callback; all requests from that callback then leave in order
Agent → gateway order_out link One link per agent
Gateway ExchangeTiming::gateway_ns per message Each agent’s session is its own one-at-a-time server, so bursts from one participant queue there
Gateway → engine hop_ns, constant Constant, so the engine receives messages in forwarding order. This is what makes gateway-assigned exchange ids exact
Engine engine_base_ns + engine_per_event_ns × events Per-instrument queue: simultaneous orders from many participants wait their turn; big sweeps and uncrosses take longer
Exchange → agent order_in link Execution reports for one command are delivered together
Feed One link per feed: an agent’s private feed uses the agent’s feed model; a shared feed group uses the group’s model One packet per engine command, handed to every feed of the instrument (see §3a)
Signal signal link per (agent, signal) Each update reaches each subscriber with its own delay

Links never reorder. A message cannot overtake the one sent before it on the same link: arrival = max(send time + sampled delay, previous arrival). Different links are independent, so a participant’s market data and its execution reports can arrive in either order, as in reality.

Latency models (LatencyModel): constant; uniform; floor + log-normal jitter (median and log standard deviation); floor + Pareto/Lomax jitter (power-law tail, capped at 1,000 s); and any model plus random spikes. Delays are integer nanoseconds.

Profiles (LatencyProfile::colocated, proximity, remote) are illustrative orders of magnitude, not calibrations:

The default ExchangeTiming (gateway 8 µs, hop 2 µs, engine 3 µs + 0.15 µs per event) puts a co-located round trip in the tens of microseconds, the order of magnitude published for European venues (RESEARCH_SURVEY.md A1). It is not calibrated to any venue.

3. What an agent sees and does

An agent implements Agent with optional callbacks: on_start, on_market_data, on_report, on_timer and on_signal. Through Ctx it can:

The kernel calls agents one at a time. A callback runs instantly in simulated time; its cost is modelled by the compute delay applied to whatever it sends.

3a. Market-data feeds: private or shared

Every subscription goes through a feed: one instrument’s packets, one link, one book view.

Each member chooses a mode:

Equivalence (tested). Twelve agents on three shared snapshot groups produce exactly the same exchange event stream as the same agents on private callback feeds, provided each private feed has its group’s constant latency.

Cost. Per-packet work scales with the number of feeds, not the number of subscribers. With 1,000 agents on three location groups, the kernel runs 5.3× faster than real time instead of 0.025× (BENCHMARKS.md, kernel section).

When to use which.

4. Gateway

One session per agent:

5. Sessions and signals

6. Reproducibility guarantees

7. Known simplifications