# nautechsystems/nautilus_trader — Kavach Visual Invariant Atlas

> High-performance algorithmic trading platform and event-driven execution engine in Rust.

- **Repository:** https://github.com/nautechsystems/nautilus_trader
- **Commit:** `056f31e` (branch `develop`)
- **Proof Tier:** `gold_showcase`
- **Stateful Backend Supported:** `true`
- **Scan:** 278 ms · 680 files · $0.00 LLM tokens

## Detected Stateful Drivers & Gateways

| Driver | Language | Category | Kavach Adapter |
| :--- | :--- | :--- | :--- |
| `kavach-host (Rust scoped)` | rust | queue | `kavach-host` |

## Extracted System Invariants (3)

| Invariant | Proof State | Category | Rule Kind | Evidence |
| :--- | :--- | :--- | :--- | :--- |
| `n1_ouo_manager_partial_fill_leaves_preserved` | `verified` | state | `state_machine` | `harness/src/main.rs` |
| `n2_ouo_engine_partial_fill_leaves_preserved` | `verified` | state | `state_machine` | `harness/src/main.rs` |
| `n3_single_currency_margin_liquidation_closes_foreign_settled` | `verified` | conservation | `conservation` | `harness/src/main.rs` |

### `n1_ouo_manager_partial_fill_leaves_preserved`
- **Summary:** ModifyLocalQuantity equals sibling.filled_qty() + primary.leaves_qty() when sibling already has partial fills.
- **Proof State:** `verified`
- **Code:** `harness/src/main.rs`

### `n2_ouo_engine_partial_fill_leaves_preserved`
- **Summary:** OUO sibling remains open on matching engine with leaves_qty == primary.leaves_qty() and quantity == child_filled + primary.leaves_qty().
- **Proof State:** `verified`
- **Code:** `harness/src/main.rs`

### `n3_single_currency_margin_liquidation_closes_foreign_settled`
- **Summary:** Single-currency MarginAccount aggregates FX-converted PnL under base_currency and liquidates underwater foreign-settled positions.
- **Proof State:** `verified`
- **Code:** `harness/src/main.rs`

## Guided Causal Flows

### N1: Client OrderManager OUO sibling under-sizing
OrderManager::handle_contingencies sets sibling total quantity to leaves_qty alone, erasing prior sibling partial fills.

1. **`in_oms_ouo`**: Steps 1-2 verify healthy OUO fill sync when sibling has 0 prior fills; Step 3 introduces sibling_fill=30.000 and leg_fill=20.000 (50.000 remaining).
2. **`fn_oms_handle`**: OrderManager::handle_contingencies (manager.rs:545-547) runs on the OrderFilled event.
3. **`bug_n1`**: Unpatched emits ModifyLocalQuantity(50.000) instead of 30.000 + 50.000 = 80.000, leaving only 20.000 open on the sibling.
4. **`inv_n1`**: n1_ouo_manager_partial_fill_leaves_preserved fails on unpatched and passes (80.000) on patched.

### N2: OrderMatchingEngine OUO partial-fill & modify under-sizing
OrderMatchingEngine::update_contingent_quantity passes desired leaves_qty as total quantity to update_order.

1. **`in_me_ouo_fill`**: Pullback Limit buys 0.300 (resizing breakout StopLimit to 0.700), then breakout triggers and buys 0.200 (0.500 remaining).
2. **`fn_me_process_delta`**: OrderMatchingEngine::update_ouo_child_after_fill invokes update_contingent_quantity(&pullback, 0.500).
3. **`bug_n2`**: Unpatched updates pullback total quantity to 0.500 without engine_filled_qty (0.300), leaving only 0.200 open.
4. **`inv_n2`**: n2_ouo_engine_partial_fill_leaves_preserved fails on unpatched (0.200 != 0.500) and passes on patched (0.500 leaves, 0.800 total).

### N3: Single-currency MarginAccount liquidation bypass
SimulatedExchange ignores non-base-settled positions when planning and executing liquidations for single-currency MarginAccounts.

1. **`in_liq_underwater`**: Single-currency USD margin account (100 USD balance, 50 USD maint) holds 1000 USD/JPY entered at 200.00 when quote crashes to 100.00 (-1000 USD uPnL).
2. **`fn_ex_plan_liq`**: Unpatched SimulatedExchange::plan_liquidations buckets by p.settlement_currency (JPY), finds no USD positions, and emits 0 triggers.
3. **`bug_n3`**: Account stays open at -900 USD equity with zero LIQUIDATION- fills; patched converts JPY PnL via Mid xrate and liquidates all open positions at SIM.
4. **`inv_n3`**: n3_single_currency_margin_liquidation_closes_foreign_settled fails on unpatched and passes on patched.

## Replay-Verified Proofs (150/150 variants, 131/131 regressions)

- **N1: OrderManager OUO Partial-Fill Hedge Under-Sizing** (`crates/execution/src/order_manager/manager.rs:538-548`) — Passes sibling leaves_qty instead of filled_qty() + leaves_qty, shrinking open hedge orders. [45/45 variants · verdict `fixed`]
- **N2: OrderMatchingEngine Overwrites Total Quantity on OUO Fill** (`crates/execution/src/matching_engine/mod.rs:7972-8040`) — Drops engine_filled_qty(order) on L2 partial fills and ModifyOrder updates. [47/47 variants · verdict `fixed`]
- **N3: Single-Currency MarginAccount Skips Foreign FX Liquidations** (`crates/backtest/src/exchange.rs:1802-1905`) — Buckets by settlement_currency without FX conversion, leaving insolvent USD/JPY positions unliquidated. [58/58 variants · verdict `fixed`]

## Starter `.kavach/invariants.yaml`

```yaml
service: nautilus-trader
invariants:
  - name: n1_ouo_manager_partial_fill_leaves_preserved
    kind: non_negative
    sink: oms:contingencies
    field: $.leaves_delta
  - name: n2_ouo_engine_partial_fill_leaves_preserved
    kind: non_negative
    sink: matching:order
    field: $.leaves_delta
  - name: n3_single_currency_margin_liquidation_closes_foreign_settled
    kind: non_negative
    sink: margin:liquidation
    field: $.liquidations_triggered
```

## Driver Wrap Snippet

```text
// Scoped Rust host wrapper (kavach-host stdin/stdout JSONL)
kavach_host::serve(|env, input| {
  engine.handle_execution_event(env, input)
})
```

## Run Locally (Zero Upload)

```bash
brew install kavachlabs/tap/kavach && kavach map --auto --open
```
