Skip to content

x402agent/SolanaOS / skills/solana-formal-verification

qedgen

Flagged

Formally verify programs by writing Lean 4 proofs. Trigger this skill whenever the user wants to formally verify code, generate Lean 4 proofs, prove properties about algorithms or smart contracts, verify invariants, convert program logic into formal specifications, or anything involving Lean 4 and formal verification. Also trigger when the user mentions "qedgen", "lean proof", "formal proof", "verify my code", "prove correctness", "formal verification", or wants mathematical guarantees about their implementation.

FileKindSize
.gitignoreother753 B
.npmignoreother352 B
Cargo.lockmanifest46.3 KB
Cargo.tomlmanifest307 B
LICENSEother1.0 KB
README.mdmarkdown3.7 KB
SKILL.mdskill md10.8 KB
bin/leanstralexecutablescript6.2 MB
claude.mdmarkdown6.8 KB
crates/qedgen/Cargo.tomlmanifest544 B
crates/qedgen/lean_support/QEDGen.leanother166 B
crates/qedgen/lean_support/QEDGen/Solana.leanother166 B
crates/qedgen/lean_support/QEDGen/Solana/Account.leanother4.0 KB
crates/qedgen/lean_support/QEDGen/Solana/Authority.leanother285 B
crates/qedgen/lean_support/QEDGen/Solana/Cpi.leanother3.3 KB
crates/qedgen/lean_support/QEDGen/Solana/State.leanother1.9 KB
crates/qedgen/lean_support/QEDGen/Solana/Token.leanother1.4 KB
crates/qedgen/lean_support/QEDGen/Solana/Valid.leanother2.0 KB
crates/qedgen/lean_support/README.mdmarkdown2.8 KB
crates/qedgen/lean_support/lake-manifest.jsonconfig119 B
crates/qedgen/lean_support/lakefile.leanother112 B
crates/qedgen/lean_support/lean-toolchainother25 B
crates/qedgen/lean_support/test_lemmas.leanother1.2 KB
crates/qedgen/src/api.rsscript32.3 KB
crates/qedgen/src/consolidate.rsscript6.9 KB
crates/qedgen/src/main.rsscript4.1 KB
crates/qedgen/src/project.rsscript3.1 KB
crates/qedgen/src/spec.rsscript19.3 KB
crates/qedgen/src/validate.rsscript5.6 KB
crates/qedgen/templates/.gitignoreother29 B
crates/qedgen/templates/Main.leanother154 B
crates/qedgen/templates/PROMPT_TEMPLATE.mdmarkdown3.2 KB
crates/qedgen/templates/README.lean.mdmarkdown1.6 KB
crates/qedgen/templates/lakefile.leanother293 B
crates/qedgen/templates/lean-toolchainother25 B
docs/CNAMEother10 B
docs/index.htmlother10.9 KB
docs/llms-full.txtmarkdown8.7 KB
docs/llms.txtmarkdown2.1 KB
docs/logo-dark.svgother648 B
docs/logo-light.svgother648 B
docs/style.cssother7.6 KB
example/escrow/.gitignoreother67 B
example/escrow/.prettierignoreother61 B
example/escrow/Anchor.tomlconfig353 B
example/escrow/Cargo.lockmanifest72.8 KB
example/escrow/Cargo.tomlmanifest215 B
example/escrow/migrations/deploy.tsscript428 B
example/escrow/package.jsonmanifest528 B
example/escrow/programs/escrow/Cargo.tomlmanifest376 B
example/escrow/programs/escrow/Xargo.tomlconfig62 B
example/escrow/programs/escrow/src/lib.rsscript6.7 KB
example/escrow/tests/escrow.tsscript8.6 KB
example/escrow/tsconfig.jsonconfig205 B
example/percolator/.cargo/config.tomlconfig33 B
example/percolator/.gitignoreother320 B
example/percolator/Cargo.lockmanifest8.5 KB
example/percolator/Cargo.tomlmanifest639 B
example/percolator/LICENSEother11.1 KB
example/percolator/README.mdmarkdown4.9 KB
example/percolator/examples/offsets.rsscript1.3 KB
example/percolator/scripts/audit-proof-strength.mdmarkdown6.4 KB
example/percolator/scripts/proof-strength-audit-results.mdmarkdown89.2 KB
example/percolator/scripts/run_kani_full_audit.shexecutablescript1.2 KB
example/percolator/spec.mdmarkdown108.4 KB
example/percolator/src/i128.rsscript20.4 KB
example/percolator/src/percolator.rsscript139.2 KB
example/percolator/src/wide_math.rsscript59.8 KB
example/percolator/tests/amm_tests.rsscript7.4 KB
example/percolator/tests/common/mod.rsscript4.7 KB
example/percolator/tests/fuzzing.proptest-regressionsother4.7 KB
example/percolator/tests/fuzzing.rsscript43.1 KB
example/percolator/tests/proofs_arithmetic.rsscript17.1 KB
example/percolator/tests/proofs_audit.rsscript35.0 KB
example/percolator/tests/proofs_instructions.rsscript64.8 KB
example/percolator/tests/proofs_invariants.rsscript21.0 KB
example/percolator/tests/proofs_lazy_ak.rsscript22.4 KB
example/percolator/tests/proofs_liveness.rsscript17.1 KB
example/percolator/tests/proofs_safety.rsscript104.2 KB
example/percolator/tests/proofs_v1131.rsscript25.4 KB
example/percolator/tests/unit_tests.rsscript93.8 KB
install.shexecutablescript4.9 KB
package.jsonmanifest1.1 KB

example/percolator/tests/proofs_v1131.rs

View on GitHub
//! Section 7 — v11.31 Spec Compliance Proofs
//!
//! Properties 46, 59-70: zero-rate core profile, configuration immutability,
//! bilateral OI decomposition, partial liquidation, deposit guards, profit conversion.

#![cfg(kani)]

mod common;
use common::*;

// ############################################################################
// PROPERTY 46: Zero-rate funding recomputation
// ############################################################################

/// recompute_r_last_from_final_state() always stores 0 and never reverts (spec §4.12).
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_recompute_r_last_always_zero() {
    let mut engine = RiskEngine::new(zero_fee_params());

    // Set arbitrary nonzero funding rate
    let rate: i64 = kani::any();
    engine.funding_rate_bps_per_slot_last = rate;

    engine.recompute_r_last_from_final_state();

    assert!(engine.funding_rate_bps_per_slot_last == 0,
        "r_last must be 0 after recompute_r_last_from_final_state");
}

// ############################################################################
// PROPERTY: accrue_market_to has no funding transfer (zero-rate core profile)
// ############################################################################

/// accrue_market_to with arbitrary stored rate and time elapsed does NOT modify K
/// via funding. Only mark-to-market (A*ΔP) changes K.
/// Symbolic rate (full i64) and slot delta exercise all rate/time combinations.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_accrue_no_funding_transfer() {
    let mut engine = RiskEngine::new(zero_fee_params());

    // Set up live OI on both sides
    engine.adl_mult_long = ADL_ONE;
    engine.adl_mult_short = ADL_ONE;
    engine.oi_eff_long_q = POS_SCALE;
    engine.oi_eff_short_q = POS_SCALE;
    engine.last_oracle_price = DEFAULT_ORACLE;
    engine.last_market_slot = 0;
    engine.funding_price_sample_last = DEFAULT_ORACLE;

    // Store arbitrary nonzero funding rate (symbolic, full i64 domain)
    let rate: i64 = kani::any();
    kani::assume(rate != 0);
    engine.funding_rate_bps_per_slot_last = rate;

    // Symbolic time delta (1..1000 slots)
    let dt: u16 = kani::any();
    kani::assume(dt >= 1 && dt <= 1000);

    let k_long_before = engine.adl_coeff_long;
    let k_short_before = engine.adl_coeff_short;

    // Same price, time passes — only funding could change K
    let result = engine.accrue_market_to(dt as u64, DEFAULT_ORACLE);
    assert!(result.is_ok());

    // K must NOT change (no mark ΔP=0, no funding in this revision)
    assert!(engine.adl_coeff_long == k_long_before,
        "K_long must not change from funding");
    assert!(engine.adl_coeff_short == k_short_before,
        "K_short must not change from funding");
}

/// accrue_market_to still applies mark-to-market correctly.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_accrue_mark_still_works() {
    let mut engine = RiskEngine::new(zero_fee_params());

    engine.adl_mult_long = ADL_ONE;
    engine.adl_mult_short = ADL_ONE;
    engine.oi_eff_long_q = POS_SCALE;
    engine.oi_eff_short_q = POS_SCALE;
    engine.last_oracle_price = DEFAULT_ORACLE;
    engine.last_market_slot = 0;
    engine.funding_price_sample_last = DEFAULT_ORACLE;

    let new_price: u64 = kani::any();
    kani::assume(new_price > 0 && new_price <= 2000 && new_price != DEFAULT_ORACLE);

    let k_long_before = engine.adl_coeff_long;
    let k_short_before = engine.adl_coeff_short;

    let result = engine.accrue_market_to(1, new_price);
    assert!(result.is_ok());

    // Mark must change K: K_long += A_long * ΔP, K_short -= A_short * ΔP
    let delta_p = (new_price as i128) - (DEFAULT_ORACLE as i128);
    let expected_k_long = k_long_before + (ADL_ONE as i128) * delta_p;
    let expected_k_short = k_short_before - (ADL_ONE as i128) * delta_p;

    assert!(engine.adl_coeff_long == expected_k_long,
        "K_long must reflect mark-to-market");
    assert!(engine.adl_coeff_short == expected_k_short,
        "K_short must reflect mark-to-market");
}

// ############################################################################
// PROPERTY: maintenance fees disabled (spec §8.2)
// ############################################################################

/// touch_account_full must NOT charge maintenance fees for any fee param or time delta.
/// Symbolic fee_per_slot and dt prove fee_credits invariance structurally.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_touch_no_maintenance_fee() {
    let mut params = zero_fee_params();
    // Symbolic fee parameter — even extreme values must not produce charges
    let fee_per_slot: u32 = kani::any();
    kani::assume(fee_per_slot >= 1);
    params.maintenance_fee_per_slot = U128::new(fee_per_slot as u128);
    let mut engine = RiskEngine::new(params);

    let idx = engine.add_user(0).unwrap();
    engine.deposit(idx, 1_000_000, DEFAULT_ORACLE, 0).unwrap();
    engine.last_oracle_price = DEFAULT_ORACLE;
    engine.last_market_slot = 0;

    let fc_before = engine.accounts[idx as usize].fee_credits.get();

    // Symbolic time delta (1..10000 slots)
    let dt: u16 = kani::any();
    kani::assume(dt >= 1 && dt <= 10000);

    let result = engine.touch_account_full(idx as usize, DEFAULT_ORACLE, dt as u64);
    assert!(result.is_ok());

    // fee_credits must NOT change (fees disabled per §8.2)
    assert!(engine.accounts[idx as usize].fee_credits.get() == fc_before,
        "fee_credits must not change — maintenance fees disabled");
}

// ############################################################################
// PROPERTY 62: Pure deposit no-insurance-draw
// ############################################################################

/// deposit never calls absorb_protocol_loss, never decrements I (spec property 62).
/// settle_losses MAY pay from capital to reduce negative PNL (that's loss settlement,
/// not insurance draw), but resolve_flat_negative is NOT called.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_deposit_no_insurance_draw() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let idx = engine.add_user(0).unwrap();
    // Start with zero capital
    engine.deposit(idx, 0, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // Set very large negative PNL (much more than any deposit)
    engine.set_pnl(idx as usize, -10_000_000i128);

    let ins_before = engine.insurance_fund.balance.get();

    // Deposit a small amount — capital insufficient to cover PNL
    let amount: u32 = kani::any();
    kani::assume(amount > 0 && amount <= 1_000_000);

    let result = engine.deposit(idx, amount as u128, DEFAULT_ORACLE, DEFAULT_SLOT);
    assert!(result.is_ok());

    // Insurance fund must NOT decrease (no absorb_protocol_loss via resolve_flat_negative)
    assert!(engine.insurance_fund.balance.get() >= ins_before,
        "deposit must never decrement I");

    // PNL must still be negative (settle_losses paid from capital but couldn't cover all)
    assert!(engine.accounts[idx as usize].pnl < 0,
        "negative PNL must survive deposit — resolve_flat_negative not called");
}

// ############################################################################
// PROPERTY 66: Flat authoritative deposit sweep
// ############################################################################

/// deposit does NOT sweep fee debt when PNL < 0 persists after settle_losses.
/// Symbolic deposit amount — for any amount, if PNL stays negative, no sweep.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_deposit_sweep_pnl_guard() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let idx = engine.add_user(0).unwrap();
    // Start with zero capital
    engine.deposit(idx, 0, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // Symbolic fee debt
    let debt: u16 = kani::any();
    kani::assume(debt >= 1 && debt <= 10_000);
    engine.accounts[idx as usize].fee_credits = I128::new(-(debt as i128));

    // Set large negative PNL that exceeds any deposit amount
    engine.set_pnl(idx as usize, -10_000_000i128);

    let fc_before = engine.accounts[idx as usize].fee_credits.get();

    // Symbolic deposit — always insufficient to cover PNL=-10M
    let amount: u32 = kani::any();
    kani::assume(amount >= 1 && amount <= 1_000_000);
    engine.deposit(idx, amount as u128, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // After deposit: capital went to settle_losses (paid toward PNL=-10M)
    // PNL is still very negative, so sweep must NOT happen
    assert!(engine.accounts[idx as usize].fee_credits.get() == fc_before,
        "deposit must not sweep when PNL < 0 after settle_losses");
    assert!(engine.accounts[idx as usize].pnl < 0,
        "PNL must still be negative — settle_losses can't cover full loss");
}

/// deposit DOES sweep fee debt on flat state with PNL >= 0.
/// Symbolic deposit amount exercises sweep with varying capital levels.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_deposit_sweep_when_pnl_nonneg() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let idx = engine.add_user(0).unwrap();
    // Symbolic initial capital — ensures fee_debt_sweep has capital to pay from
    let init_cap: u32 = kani::any();
    kani::assume(init_cap >= 10_000 && init_cap <= 1_000_000);
    engine.deposit(idx, init_cap as u128, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // Give account fee debt
    engine.accounts[idx as usize].fee_credits = I128::new(-5000);

    // PNL = 0 (flat position, no losses)
    assert!(engine.accounts[idx as usize].pnl == 0);

    // Symbolic deposit amount
    let dep: u32 = kani::any();
    kani::assume(dep >= 1 && dep <= 100_000);
    engine.deposit(idx, dep as u128, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // fee_credits must have improved (debt partially/fully paid)
    assert!(engine.accounts[idx as usize].fee_credits.get() > -5000,
        "deposit must sweep fee debt when flat with PNL >= 0");
}

// ############################################################################
// PROPERTY 61: Insurance top-up bounded arithmetic + now_slot
// ############################################################################

/// top_up_insurance_fund uses checked addition, enforces MAX_VAULT_TVL,
/// sets current_slot, and increases V and I by the same amount.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_top_up_insurance_now_slot() {
    let mut engine = RiskEngine::new(zero_fee_params());
    engine.current_slot = 50;

    let amount: u32 = kani::any();
    kani::assume(amount > 0 && amount <= 1_000_000);

    let now_slot: u64 = kani::any();
    kani::assume(now_slot >= 50 && now_slot <= 200);

    let v_before = engine.vault.get();
    let i_before = engine.insurance_fund.balance.get();

    let result = engine.top_up_insurance_fund(amount as u128, now_slot);
    assert!(result.is_ok());

    // current_slot updated
    assert!(engine.current_slot == now_slot, "current_slot must be updated");

    // V and I increase by exact same amount
    assert!(engine.vault.get() == v_before + amount as u128,
        "V must increase by amount");
    assert!(engine.insurance_fund.balance.get() == i_before + amount as u128,
        "I must increase by amount");
}

/// top_up_insurance_fund rejects now_slot < current_slot.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_top_up_insurance_rejects_stale_slot() {
    let mut engine = RiskEngine::new(zero_fee_params());
    engine.current_slot = 100;

    let result = engine.top_up_insurance_fund(1000, 50);
    assert!(result.is_err(), "must reject now_slot < current_slot");
}

// ############################################################################
// PROPERTY 69: Positive conversion denominator
// ############################################################################

/// Whenever flat auto-conversion consumes x > 0 released profit,
/// pnl_matured_pos_tot > 0 and h_den > 0.
/// We verify this by setting up a state with released profit and checking
/// that the haircut denominator is positive.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_positive_conversion_denominator() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let idx = engine.add_user(0).unwrap();
    engine.deposit(idx, 1_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // Set up matured positive PNL
    let pnl_val: u32 = kani::any();
    kani::assume(pnl_val > 0 && pnl_val <= 100_000);
    let pnl = pnl_val as i128;

    engine.set_pnl(idx as usize, pnl);
    // For released_pos to be > 0, the account must have matured PnL.
    // released_pos = pnl_matured_pos_tot contribution from this account.
    // In a flat account, after warmup, the released portion is positive.
    // We directly verify the haircut ratio:
    engine.pnl_matured_pos_tot = pnl_val as u128;

    let (h_num, h_den) = engine.haircut_ratio();
    // When pnl_matured_pos_tot > 0, h_den == pnl_matured_pos_tot > 0
    assert!(h_den > 0, "h_den must be positive when pnl_matured_pos_tot > 0");
    assert!(h_num <= h_den, "h_num must not exceed h_den");
}

// ############################################################################
// PROPERTY 64: Exact trade OI decomposition
// ############################################################################

/// Trade uses exact bilateral OI after-values for both gating and writeback.
/// Symbolic trade size exercises open, close, and flip paths.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_bilateral_oi_decomposition() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let a = engine.add_user(0).unwrap();
    let b = engine.add_user(0).unwrap();
    engine.deposit(a, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.deposit(b, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.last_crank_slot = DEFAULT_SLOT;
    engine.last_market_slot = DEFAULT_SLOT;
    engine.last_oracle_price = DEFAULT_ORACLE;

    // First trade: open a position (a long, b short)
    let open_size = (100 * POS_SCALE) as i128;
    let r1 = engine.execute_trade(a, b, DEFAULT_ORACLE, DEFAULT_SLOT, open_size, DEFAULT_ORACLE);
    assert!(r1.is_ok(), "initial trade must succeed");

    // Second trade: symbolic size exercises close, reduce, and flip paths
    let raw_size: i16 = kani::any();
    kani::assume(raw_size != 0);
    // Scale to position units — covers -32768..32767 * POS_SCALE
    let size_q = (raw_size as i128) * (POS_SCALE as i128);

    let result = engine.execute_trade(a, b, DEFAULT_ORACLE, DEFAULT_SLOT, size_q, DEFAULT_ORACLE);

    if result.is_ok() {
        let eff_a = engine.effective_pos_q(a as usize);
        let eff_b = engine.effective_pos_q(b as usize);

        // OI_long should be the sum of positive positions
        let expected_long = if eff_a > 0 { eff_a as u128 } else { 0 }
            + if eff_b > 0 { eff_b as u128 } else { 0 };
        let expected_short = if eff_a < 0 { eff_a.unsigned_abs() } else { 0 }
            + if eff_b < 0 { eff_b.unsigned_abs() } else { 0 };

        assert!(engine.oi_eff_long_q == expected_long,
            "OI_long must match bilateral decomposition");
        assert!(engine.oi_eff_short_q == expected_short,
            "OI_short must match bilateral decomposition");

        // OI balance: must be equal
        assert!(engine.oi_eff_long_q == engine.oi_eff_short_q,
            "OI_long must equal OI_short");
    }
}

// ############################################################################
// PROPERTY 68: Partial liquidation remainder nonzero
// ############################################################################

/// Partial liquidation with 0 < q_close < abs(eff) produces nonzero remainder.
/// Close most of the position (90%) so post-partial health check passes.
/// Non-vacuity: explicitly assert Ok(true) is reached.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_partial_liquidation_remainder_nonzero() {
    let mut params = zero_fee_params();
    params.maintenance_margin_bps = 100; // 1% margin — easy to restore health after partial
    let mut engine = RiskEngine::new(params);

    let a = engine.add_user(0).unwrap();
    let b = engine.add_user(0).unwrap();
    // Small deposit for a — high leverage. Large deposit for b — counterparty.
    engine.deposit(a, 50_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.deposit(b, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.last_crank_slot = DEFAULT_SLOT;
    engine.last_market_slot = DEFAULT_SLOT;
    engine.last_oracle_price = DEFAULT_ORACLE;

    // Open near-max leverage: 480 units, notional=480K, IM ~48K with 50K capital
    let size_q = (480 * POS_SCALE) as i128;
    engine.execute_trade(a, b, DEFAULT_ORACLE, DEFAULT_SLOT, size_q, DEFAULT_ORACLE).unwrap();

    let abs_eff = engine.effective_pos_q(a as usize).unsigned_abs();
    assert!(abs_eff > 0, "position must be open");

    // Close all but 1 unit — leaves minimal remainder
    // Post-partial: 1 unit notional = ~crash_price/POS_SCALE, MM ~= 0
    let q_close = abs_eff - POS_SCALE;
    assert!(q_close > 0 && q_close < abs_eff, "q_close must be valid partial");

    // Crash: 10% drop triggers liquidation (PNL = -480*100 = -48K, equity ~2K < MM=4800)
    let crash = 900u64;
    let result = engine.liquidate_at_oracle(a, DEFAULT_SLOT + 1, crash,
        LiquidationPolicy::ExactPartial(q_close));

    // Non-vacuity: partial MUST succeed
    assert!(result.is_ok(), "partial liquidation must not revert");
    assert!(result.unwrap(), "account must be liquidatable at crash price");

    // Core property: remainder must be nonzero
    let eff_after = engine.effective_pos_q(a as usize);
    assert!(eff_after != 0, "partial liquidation must leave nonzero remainder");
}

// ############################################################################
// PROPERTY 65: Liquidation policy determinism
// ############################################################################

/// liquidate accepts only FullClose or ExactPartial; ExactPartial with
/// q_close_q == 0 or q_close_q >= abs(eff) is rejected.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_liquidation_policy_validity() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let a = engine.add_user(0).unwrap();
    let b = engine.add_user(0).unwrap();
    engine.deposit(a, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.deposit(b, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.last_crank_slot = DEFAULT_SLOT;
    engine.last_market_slot = DEFAULT_SLOT;
    engine.last_oracle_price = DEFAULT_ORACLE;

    let size_q = (400 * POS_SCALE) as i128;
    engine.execute_trade(a, b, DEFAULT_ORACLE, DEFAULT_SLOT, size_q, DEFAULT_ORACLE).unwrap();

    let abs_eff = engine.effective_pos_q(a as usize).unsigned_abs();

    // ExactPartial(0) must fail
    let r1 = engine.liquidate_at_oracle(a, DEFAULT_SLOT + 1, 500,
        LiquidationPolicy::ExactPartial(0));
    // Either not liquidatable or rejected
    if let Ok(true) = r1 {
        panic!("ExactPartial(0) must not succeed as a partial liquidation");
    }
}

// ############################################################################
// PROPERTY 60: Direct fee-credit repayment cap
// ############################################################################

/// deposit_fee_credits applies only min(amount, debt), never makes fee_credits
/// positive, increases V and I by exactly the applied amount.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_deposit_fee_credits_cap() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let idx = engine.add_user(0).unwrap();
    engine.deposit(idx, 100_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // Give fee debt
    engine.accounts[idx as usize].fee_credits = I128::new(-5000);

    let v_before = engine.vault.get();
    let i_before = engine.insurance_fund.balance.get();

    let amount: u32 = kani::any();
    kani::assume(amount > 0 && amount <= 100_000);

    let result = engine.deposit_fee_credits(idx, amount as u128, DEFAULT_SLOT);
    assert!(result.is_ok());

    // fee_credits must be <= 0
    assert!(engine.accounts[idx as usize].fee_credits.get() <= 0,
        "fee_credits must never become positive");

    // Applied amount = min(amount, 5000)
    let expected_pay = core::cmp::min(amount as u128, 5000);
    assert!(engine.vault.get() == v_before + expected_pay, "V must increase by applied amount");
    assert!(engine.insurance_fund.balance.get() == i_before + expected_pay, "I must increase by applied amount");
}

// ############################################################################
// PROPERTY 70: Partial liquidation health check survives reset scheduling
// ############################################################################

/// Partial liquidation that closes a tiny amount MUST be rejected by the
/// mandatory post-partial health check (§9.4 step 14). Closing 1 unit out
/// of a large position at a crash price cannot restore health.
/// This proves enforcement: the health check rejects insufficient partials.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_partial_liq_health_check_mandatory() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let a = engine.add_user(0).unwrap();
    let b = engine.add_user(0).unwrap();
    engine.deposit(a, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.deposit(b, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.last_crank_slot = DEFAULT_SLOT;
    engine.last_market_slot = DEFAULT_SLOT;
    engine.last_oracle_price = DEFAULT_ORACLE;

    // Open near-max leverage position
    let size_q = (400 * POS_SCALE) as i128;
    engine.execute_trade(a, b, DEFAULT_ORACLE, DEFAULT_SLOT, size_q, DEFAULT_ORACLE).unwrap();

    // Symbolic tiny close amount (1..100 units — all too small to restore health)
    let tiny_close: u8 = kani::any();
    kani::assume(tiny_close >= 1);

    // Severe crash — account is deeply unhealthy
    let result = engine.liquidate_at_oracle(a, DEFAULT_SLOT + 1, 500,
        LiquidationPolicy::ExactPartial(tiny_close as u128));

    // Health check at step 14 MUST reject: closing a few units out of 400M
    // position at 50% crash cannot restore maintenance margin.
    // Result is Err(Undercollateralized) — NOT Ok(true).
    assert!(!matches!(result, Ok(true)),
        "tiny partial must be rejected by health check — remainder still unhealthy");
}

// ############################################################################
// PROPERTY 42: Post-reset funding recomputation stores exactly 0
// ############################################################################

/// keeper_crank recomputes r_last exactly once after final reset handling,
/// and the stored value is exactly 0 under the zero-rate core profile.
/// Symbolic initial rate proves this for all possible pre-crank rates.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_keeper_crank_r_last_zero() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let idx = engine.add_user(0).unwrap();
    engine.deposit(idx, 1_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // Symbolic nonzero rate before crank — full i64 domain
    let rate: i64 = kani::any();
    engine.funding_rate_bps_per_slot_last = rate;

    let result = engine.keeper_crank(DEFAULT_SLOT + 1, DEFAULT_ORACLE,
        &[(idx, None)], 64);
    assert!(result.is_ok());

    // r_last must be 0 after crank regardless of initial rate
    assert!(engine.funding_rate_bps_per_slot_last == 0,
        "r_last must be 0 after keeper_crank");
}

// ############################################################################
// PROPERTY 44: Deposit true-flat guard and latent-loss seniority
// ############################################################################

/// A deposit into an account with basis_pos_q != 0 neither routes unresolved
/// negative PnL through §7.3 nor sweeps fee debt.
/// Symbolic deposit amount and fee debt prove this for all combinations.
#[kani::proof]
#[kani::unwind(34)]
#[kani::solver(cadical)]
fn proof_deposit_nonflat_no_sweep_no_resolve() {
    let mut engine = RiskEngine::new(zero_fee_params());

    let a = engine.add_user(0).unwrap();
    let b = engine.add_user(0).unwrap();
    engine.deposit(a, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.deposit(b, 5_000_000, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();
    engine.last_crank_slot = DEFAULT_SLOT;
    engine.last_market_slot = DEFAULT_SLOT;
    engine.last_oracle_price = DEFAULT_ORACLE;

    // Open position for a
    let size_q = (100 * POS_SCALE) as i128;
    engine.execute_trade(a, b, DEFAULT_ORACLE, DEFAULT_SLOT, size_q, DEFAULT_ORACLE).unwrap();

    // Symbolic fee debt
    let debt: u16 = kani::any();
    kani::assume(debt >= 1 && debt <= 10_000);
    engine.accounts[a as usize].fee_credits = I128::new(-(debt as i128));
    engine.set_pnl(a as usize, -500i128);

    let fc_before = engine.accounts[a as usize].fee_credits.get();
    let ins_before = engine.insurance_fund.balance.get();

    // Symbolic deposit into account with open position (basis != 0)
    let dep_amount: u32 = kani::any();
    kani::assume(dep_amount >= 1 && dep_amount <= 1_000_000);
    engine.deposit(a, dep_amount as u128, DEFAULT_ORACLE, DEFAULT_SLOT).unwrap();

    // fee_credits unchanged (no sweep on non-flat account)
    assert!(engine.accounts[a as usize].fee_credits.get() == fc_before,
        "deposit must not sweep fee debt when basis != 0");

    // Insurance must not decrease (no resolve_flat_negative when not flat)
    assert!(engine.insurance_fund.balance.get() >= ins_before,
        "deposit must not decrement insurance on non-flat account");
}
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall