nautechsystems/nautilus_trader · error

venue-leg quantity calculation validated cumulative fills

Error message

venue-leg quantity calculation validated cumulative fills

What it means

During cache-restore of Polymarket orders, the current venue-leg filled quantity is computed as order.filled_qty() - prior_filled using checked_sub. The expect asserts the cumulative filled quantity never decreased between recorded legs; if the cached fill history is inconsistent (cumulative went backwards), the panic fires.

Source

Thrown at crates/adapters/polymarket/src/execution/lifecycle.rs:536

                    match venue_leg_filled_before_and_quantity(
                        order,
                        venue_order_id,
                        order.quantity().precision,
                    ) {
                        Ok(quantities) => quantities,
                        Err(e) => {
                            log::error!(
                                "Cannot restore Polymarket order {} venue-leg quantities: {e}",
                                order.client_order_id()
                            );
                            continue;
                        }
                    };

                let current_leg_filled = order
                    .filled_qty()
                    .checked_sub(prior_filled)
                    .expect("venue-leg quantity calculation validated cumulative fills");
                self.fill_tracker.restore_order(
                    venue_order_id,
                    current_leg_quantity,
                    current_leg_filled,
                    order.order_side(),
                );
            }

            for event in order.events() {
                match event {
                    OrderEventAny::Filled(fill) => {
                        if let Some(key) = polymarket_trade_key(fill.info.as_ref()) {
                            matched_fills.entry(key).or_default().push(fill.clone());
                        }
                    }
                    OrderEventAny::FillVoided(voided) => {
                        if let Some(key) = polymarket_trade_key(voided.info.as_ref()) {
                            voided_trades.insert(key);

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect and repair/clear the cached order/fill records for the affected venue_order_id, then reconnect.
  2. Verify the cache is not shared or downgraded between adapter versions with different fill bookkeeping.
  3. If reproducible, capture the cache state and file a bug; it indicates a persistence-ordering defect in fill tracking.

Example fix

// before
let current_leg_filled = order.filled_qty()
    .checked_sub(prior_filled)
    .expect("venue-leg quantity calculation validated cumulative fills");

// after (defensive)
let current_leg_filled = order.filled_qty()
    .checked_sub(prior_filled)
    .unwrap_or_else(|| {
        log::error!("cumulative filled_qty regressed for {venue_order_id}; clamping to 0");
        Decimal::ZERO
    });
Defensive patterns

Strategy: validation

Try / catch

// wrap reconnect/cache-restore in a supervised task
if let Err(e) = client.connect().await {
    log::error!("cache restore failed: {e}; clearing suspect cache and retrying");
}

Prevention

When it happens

Trigger: Calling load_orders_from_cache (at connect) for an order whose cached cumulative filled_qty is less than the prior leg's filled quantity — corrupted or partially-written cache state, or fill events recorded out of order.

Common situations: Recovering after a crash with an inconsistent cache; cache files edited or mixed between adapter versions; duplicate/out-of-order persisted fills.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/9d2a4194a313eaa3. Report an issue: GitHub.