Open a section from the left

Durable state, snapshots, and tail evidence

wal_open maps a fixed-size, file-backed log. cap is the number of records, each with a 128-byte payload. The head holds a magic, version, payload size, capacity, a committed tail, and a sync count. wal_append writes the body, computes CRC32C over the fields, then publishes the tail; a record that is not committed is invisible. wal_append_item accepts any trivially copyable type up to the payload size. wal_recover replays committed records in order and stops at the first bad sequence, length, kind, or checksum, then moves the tail back to the last good record. wal_sync is an explicit group commit. The log does not choose record meanings; the caller writes its own Intent, Report, cash, or position payloads and applies them in the recovery callback.

SnapRing<T, Cap> is a single-writer, multi-reader snapshot ring. The header is 64 bytes; each cell is a sequence word followed by T, and cells start at offset 64. publish makes the sequence odd while the body is copied and even again before the published count advances. read copies a record and retries if the sequence changed. snap_open maps the file and snap_view validates magic, version, slot size, stride, and capacity. A reader in another language maps the same file and reads magic, version, slot_bytes, stride, and cap; it never needs the C++ type.

TscClock::calibrate samples TSC and CLOCK_MONOTONIC_RAW over a short window and stores a CycleScale. now_ns and now_us read rdtsc only. publish(Desk&, offset_us) writes the desk clock at the publish boundary; the offset is applied once, outside the measured section. publish(UsSlot&) writes a standalone slot.

TailBoard holds one Wait and one gap histogram per lane, a note count, a stall count, and the maximum cycles. tail_note records a sample and returns true only on the sample that crosses the armed trigger. tail_report returns percentiles. TailTape<Cap> is a small event tape that freezes on demand, so a sidecar can read the last events without stopping the writer.

ShadowStats compares a real fill with a shadow fill. A buy's slippage is real_px - sim_px; a sell's is sim_px - real_px. Positive means the real execution was worse. ShadowBook keeps one row per execution algorithm. report returns counts, signed and absolute slippage, notionals, and percentiles.

ReplayTape stores a bounded sequence of ring positions and items. replay_capture_all copies a published range. replay_ring republishes in order and FaultPlan can drop a publish or leave a slot unacked. An unacked run eventually makes publish fail, so the replay stops instead of overwriting live data.

MboBook stores live orders in fixed slots and indexes them by id with fixed bucket chains. apply accepts add, modify, cancel, and trade. best, depth, qty_at, and invariant derive the view from the live orders. RouteBook::unbind removes one binding, generation advances on every change, and route_control parses strategy|algo control text. StratEngine::routes applies those notes and pin_cpu pins the strategy thread at setup.

net_pack / net_unpack put a fixed desk record behind a 32-byte NetHeader with magic, version, length, and CRC32C. TermLog is a single-writer, multi-reader term log: data, pad, and heartbeat frames; a term rotates with a pad frame; each reader owns one absolute position; publish fails closed when the slowest reader would be overwritten. UdpLink is a non-blocking IPv4 datagram socket for pitlane-to-pitlane traffic; open / close belong to startup and teardown. ReplicaLog is a leader/follower log: follower_append validates prev_index / prev_term, the leader commits at a quorum of match positions, and apply returns committed entries in index order. RemoteHub drains desk reports and quotes into the outbound log and publishes decoded remote Intent frames into Desk::intents, so the local Center remains the hub.

Oms keeps caller-owned orders in a fixed pool. submit / ack / fill / cancel / confirm_cancel / reject / amend move Phase, reject duplicate venue fill ids, and keep filled, leaves, and avg_px. parent_progress aggregates children. sor compares venues by effective buy or sell price including taker fees, respects venue caps and lots, allocates passive quantity by fill weight, and ShownHold accounts for visible quantity already sent. Ems opens a parent slice plan, emits child slices through the SOR when slice_poll cuts one, and records fills. Pms uses average cost for long and short positions with today/yesterday quantity, realized PnL, fees, and unrealized PnL. Rms is a pure pre-trade check over quantity, notional, daily notional, position, live orders, order rate, price band, cash, and a halt flag. A sell beyond the net long reports RmsCode::Short unless RmsReq::allow_short is set, which is how a product keeps shorting off without a special venue rule.

clear/clearing.hpp holds ClearingBook, the clearing and settlement path. Fills open FIFO lots on both sides; a close consumes the oldest opposite lot, so lots opened before the last settle (yesterday) are closed before today's. Each lot keeps the trade price (close basis) and a settlement price (settlement basis). settle(now, marks, n) marks open lots to the settlement price, books that move as settled PnL, rebases the lot, flips every lot to yesterday, zeroes the day counters, and appends a compact day record; settle(now) rolls the day without revaluation. Fees use fee_ppm for opens, close_fee_ppm (default fee_ppm) for yesterday closes, and close_today_fee_ppm (default the close fee) for today closes, with a per-fill minimum and the sell-side stamp duty kept apart. Cash is set_cash plus a deposit / withdraw ledger read by cash_moves_since; cash_balance is the opening balance plus net flows, equity is cash_balance + realized - fees + unrealized, equity_settle values the book on the settlement basis, today_pnl excludes flows since the last settle, and total_pnl excludes external cash. Rows carry today/yesterday quantities, both PnL bases, marks, margin, market value, and their currency; ccy_id / set_rate convert totals to the base currency and fx_missing counts unmapped rows. set_shares turns equity into a unit NAV with a peak and drawdown, and the last four settled days ride in the report. report also reconciles net cleared quantity against Pms per account. ClearingBoard publishes a report through two slots so a reader never blocks the trading thread.

FactorEngine keeps one fixed price ring and one quote ring and derives Return, Momentum, Volatility, ZScore, Ema, Rsi, Ofi, and Imbalance in integer scaled units. FactorEval records factor/forward-return pairs and reports Pearson IC, Spearman rank IC, hit rate, and quintile returns. FactorPipeline runs the same factor path in Research, Backtest, Paper, and Live stages; only Research and Backtest evaluate, and signal emits a SigNote in any stage when the factor clears a threshold.