Wave 11d: bankrupt-close setter integration (Phase 1) - #102
Conversation
Wires the Wave 11a-ii-A bankrupt-close setters into the production hot paths that surface a deficit. Mirrors toly engine v12.20.6 patterns at toly:4980-4982, 7103-7112, 7138-7144, 7210, 7214, 7869-7870, 8364, 8402. Changes ------- 1. `settle_losses` / `resolve_flat_negative` refactored into `_with_context(idx, Option<&mut InstructionContext>)` variants. The 1-arg form delegates with `None` (routes through `trigger_bankruptcy_hmax_lock_without_context`); the `Some(ctx)` form routes through `trigger_bankruptcy_hmax_lock(ctx)` (which enforces the `positive_pnl_usability_mutated` admission gate). `settle_losses_with_context` fires the lock when a Live account exhausts capital while still negative-PnL. `resolve_flat_negative_ with_context` fires the lock BEFORE `absorb_protocol_loss` so the envelope sees pre-loss equity. 2. `enqueue_adl` Step 2 calls `trigger_bankruptcy_hmax_lock(ctx)?` when a bankruptcy deficit `d > 0` is observed. Fires BEFORE `use_insurance_buffer`, so the envelope reset reflects the full deficit (including the portion insurance absorbs). Mirrors toly:4980-4982. 3. ctx threaded into the four ctx-having hot-path settle_losses / resolve_flat_negative call sites: - `touch_account_live_local` step 6 + step 7 - `execute_trade_not_atomic` post-trade pair settle (a + b) - `liquidate_at_oracle_internal` ExactPartial step 9 - `liquidate_at_oracle_internal` FullClose step Contextless paths (`deposit_not_atomic`, `reconcile_resolved_not_atomic`, `settle_flat_negative_pnl_not_atomic`) keep the 1-arg form which transparently uses `_without_context` for the lock writer. Deferred -------- The toly:5057-5066 wiring of `book_or_start_active_close_residual_to_side` into `enqueue_adl` Step 7 (d_social branch when uncertified_potential == 0) requires replacing fork's K-adjust algorithm with toly's v12.20.6 certified/potential/ B-residual algorithm. That replacement breaks `t11_54_worked_example_regression` (Kani proof pinning fork's K-adjust post-condition — asserts `engine.adl_coeff_long != 0i128` after ADL with d > 0). Per Wave 11d STOP CONDITION (no Kani proof regression) the algorithm migration is surfaced as a follow-up wave (Wave 11e candidate). The bankrupt-close setter remains wired into the recovery dispatcher (Wave 11a-ii-C); Phase 1 of Wave 11d adds the lock setter into every Live deficit path that mirrors toly. Verification ------------ - cargo build --release: clean - cargo test --features test --release: 283 passed, 0 failed - scripts/verify-engine-preservation.sh: silent - scripts/verify-engine-deferred-absent.sh: silent - Kani harness count: 356 → 360 (+4): - proof_enqueue_adl_arms_bankruptcy_lock_when_d_positive - proof_enqueue_adl_leaves_bankruptcy_lock_when_d_zero - proof_settle_losses_with_context_arms_lock_when_capital_exhausted - proof_resolve_flat_negative_with_context_arms_lock_before_absorb Schema impact ------------- None. No new RiskEngine / Account / InstructionContext fields. Pure behavior wiring on top of Wave 11a-i (B-tracking schema) + Wave 11a-ii-A (setter helpers) + Wave 11a-ii-C (recovery dispatcher). No NFT or wrapper follow-up required. KEEP_LIST --------- - KL-FORK-ENGINE-BANKRUPT-CLOSE-1: state-machine setters now have production callers in every Live deficit path (settle / enqueue_adl Step 2). Residual booking integration (Step 7 d_social branch) deferred to Wave 11e per STOP CONDITION above. KL stays partially revoked one notch closer to full closure. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR extends bankruptcy h-max-lock arming to loss settlement and flat-negative resolution by introducing context-aware variants of ChangesBankruptcy H-Max Lock Arming and Context Threading
Possibly related PRs
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Wave 11d — wires the Wave 11a-ii-A bankrupt-close setters into the production hot paths that surface a Live-market deficit. Mirrors toly engine v12.20.6 patterns at toly:4980-4982, 7103-7112, 7138-7144, 7210, 7214, 7869-7870, 8364, 8402.
settle_losses/resolve_flat_negativerefactored to_with_context(Option<&mut InstructionContext>)variants. TheSome(ctx)path threads the bankruptcy h_max lock throughtrigger_bankruptcy_hmax_lock(ctx); theNonepath uses_without_context.enqueue_adlStep 2: arms the lock whend > 0, BEFOREuse_insurance_buffer.touch_account_live_local(settle + resolve),execute_trade_not_atomicpost-trade pair settle,liquidate_at_oracle_internalExactPartial step 9,liquidate_at_oracle_internalFullClose step.Deferred to Wave 11e candidate
The toly:5057 wiring of
book_or_start_active_close_residual_to_sideintoenqueue_adlStep 7 (d_socialbranch whenuncertified_potential == 0) requires replacing fork's K-adjust algorithm with toly's v12.20.6 certified/potential/B-residual algorithm. That replacement breaks thet11_54_worked_example_regressionKani proof which pins fork's K-adjust post-condition (adl_coeff_long != 0after ADL withd > 0).Per Wave 11d STOP CONDITION ("If integration breaks existing Kani proof → STOP, redesign, surface") the algorithm migration is surfaced as a follow-up wave. The bankrupt-close setter remains wired into the recovery dispatcher (Wave 11a-ii-C); Phase 1 of Wave 11d adds the lock setter into every Live deficit settle path that mirrors toly.
Verification
cargo build --release: cleancargo test --features test --release: 283 passed, 0 failedscripts/verify-engine-preservation.sh: silentscripts/verify-engine-deferred-absent.sh: silentSchema impact
None. No new RiskEngine / Account / InstructionContext fields. Pure behavior wiring on top of Wave 11a-i (B-tracking schema) + Wave 11a-ii-A (setter helpers) + Wave 11a-ii-C (recovery dispatcher). No NFT or wrapper follow-up required.
KEEP_LIST
KL-FORK-ENGINE-BANKRUPT-CLOSE-1: state-machine setters now have production callers in every Live deficit path (settle / enqueue_adl Step 2). Residual booking integration (Step 7d_socialbranch) deferred to Wave 11e per STOP CONDITION. KL stays partially revoked one notch closer to full closure.Test plan
cargo build --releasecleancargo test --features test --release283 / 283 pass🤖 Generated with Claude Code
Summary by CodeRabbit
Tests
Refactor