{
 "item": "later",
 "batch": "e08-boost-cap",
 "blocked": null,
 "confirmation": null,
 "code": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport {AMMStrategyBase} from \"./AMMStrategyBase.sol\";\nimport {TradeInfo} from \"./IAMMStrategy.sol\";\n\ncontract Strategy is AMMStrategyBase {\n    uint256 constant ELAPSED_CAP = 8;\n    uint256 constant SIGNAL_THRESHOLD = WAD / 650;\n    uint256 constant DIR_DECAY = 750000000000000000;\n    uint256 constant ACT_DECAY = 530000000000000000;\n    uint256 constant SIZE_DECAY = 530000000000000000;\n    uint256 constant TOX_DECAY = 910000000000000000;\n    uint256 constant SIGMA_DECAY = 690000000000000000;\n    uint256 constant LAMBDA_DECAY = 990000000000000000;\n    uint256 constant SIZE_BLEND_DECAY = 828000000000000000;\n    uint256 constant TOX_BLEND_DECAY = 51000000000000000;\n    uint256 constant ACT_BLEND_DECAY = 985000000000000000;\n    uint256 constant PHAT_ALPHA = 270000000000000000;\n    uint256 constant PHAT_ALPHA_RETAIL = 60000000000000000;\n    uint256 constant PHAT_ALPHA_SAME = 120000000000000000;\n    uint256 constant REVERT_U = 0;\n    uint256 constant DIR_IMPACT_MULT = 4;\n    uint256 constant GATE_SIGMA_MULT = 7200000000000000000;\n    uint256 constant MIN_GATE = 30000000000000000;\n    uint256 constant TRADE_TOX_BOOST = 3500 * BPS;\n    uint256 constant STALE_ATTRACT_FRAC = 1050000000000000000;\n    uint256 constant RET_CAP = WAD / 35;\n    uint256 constant TOX_CAP = WAD / 5;\n    uint256 constant TRADE_RATIO_CAP = WAD / 5;\n    uint256 constant LAMBDA_CAP = 1820000000000000000;\n    uint256 constant STEP_COUNT_CAP = 64;\n    uint256 constant BASE_FEE = 4 * BPS;\n    uint256 constant SIGMA_COEF = 200000000000000000;\n    uint256 constant LAMBDA_COEF = 12 * BPS;\n    uint256 constant FLOW_SIZE_COEF = 4842 * BPS;\n    uint256 constant TOX_COEF = 300 * BPS;\n    uint256 constant TOX_QUAD_COEF = 15000 * BPS;\n    uint256 constant ACT_COEF = 91843 * BPS;\n    uint256 constant DIR_COEF = 20 * BPS;\n    uint256 constant DIR_TOX_COEF = 100 * BPS;\n    uint256 constant STALE_DIR_COEF = 6850 * BPS;\n    uint256 constant SIGMA_TOX_COEF = 500 * BPS;\n    uint256 constant TAIL_KNEE = 600 * BPS;\n    uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000;\n    uint256 constant TAIL_SLOPE_ATTRACT = 850000000000000000;\n\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[3] = WAD; \n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000;\n        slots[13] = 902500000000; slots[14] = slots[5]; \n        return (BASE_FEE, BASE_FEE);\n    }\n\n    function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) {\n        uint256 prevBidFee = slots[0]; uint256 prevAskFee = slots[1]; uint256 lastTs = uint64(slots[2]);\n        uint256 dirState = slots[3]; uint256 actEma = slots[4]; uint256 pHat = slots[5];\n        uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8];\n        uint256 toxEma = slots[9]; uint256 stepTradeCount = slots[10];\n\n        uint256 sigmaForFast = sigmaHat;\n        uint256 fastHat = slots[14];\n        uint256 uncertainty = slots[13];\n        uint256 silent = trade.timestamp > lastTs ? trade.timestamp - lastTs : 0;\n        bool isNewStep = trade.timestamp > lastTs;\n        if (isNewStep) {\n            uint256 elapsedRaw = trade.timestamp - lastTs;\n            uint256 elapsed = elapsedRaw > ELAPSED_CAP ? ELAPSED_CAP : elapsedRaw;\n            dirState = _decayCentered(dirState, DIR_DECAY, elapsed);\n            actEma = wmul(actEma, _powWad(ACT_DECAY, elapsed));\n            sizeHat = wmul(sizeHat, _powWad(SIZE_DECAY, elapsed));\n            toxEma = wmul(toxEma, _powWad(TOX_DECAY, elapsed));\n            if (stepTradeCount > 0 && elapsedRaw > 0) {\n                uint256 lambdaInst = (stepTradeCount * WAD) / elapsedRaw;\n                if (lambdaInst > LAMBDA_CAP) lambdaInst = LAMBDA_CAP;\n                lambdaHat = wmul(lambdaHat, LAMBDA_DECAY) + wmul(lambdaInst, WAD - LAMBDA_DECAY);\n            }\n            uncertainty += wmul(sigmaForFast, sigmaForFast) * elapsedRaw;\n            if (uncertainty > 100000000000000) uncertainty = 100000000000000;\n            stepTradeCount = 0;\n        }\n\n        bool firstInStep = stepTradeCount == 0;\n        uint256 spot = trade.reserveX > 0 ? wdiv(trade.reserveY, trade.reserveX) : pHat;\n        if (pHat == 0) pHat = spot;\n\n        uint256 feeUsed = trade.isBuy ? prevBidFee : prevAskFee;\n        uint256 gamma = feeUsed < WAD ? WAD - feeUsed : 0;\n        uint256 pImplied;\n        if (gamma == 0) { pImplied = spot; }\n        else { pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma); }\n\n        // S: infer a no-trade interval center from the last post-trade spot.\n        uint256 silenceScale = WAD;\n        if (firstInStep && silent > 1) {\n            uint256 g = silent - 1;\n            if (g > 1) g = 1;\n            uint256 w = WAD - _powWad(800000000000000000, g);\n            uint256 s = slots[2] >> 64;\n            if (s == 0) s = pHat;\n            uint256 c = (wmul(s, WAD - prevBidFee) + wdiv(s, WAD - prevAskFee)) / 2;\n            if (c >= pHat) pHat += wmul(w, c - pHat);\n            else pHat -= wmul(w, pHat - c);\n            silenceScale = WAD - wmul(300000000000000000, w);\n        }\n\n        {\n            uint256 ret = pHat > 0 ? wdiv(absDiff(pImplied, pHat), pHat) : 0;\n            uint256 alpha = firstInStep ? PHAT_ALPHA : PHAT_ALPHA_RETAIL;\n            uint256 prePhat = pHat;\n            bool secondPrint = !firstInStep && stepTradeCount == 1;\n            bool sameDir = false; bool oppDir = false;\n            if (secondPrint) {\n                bool up2 = pImplied >= pHat;\n                uint256 d1 = slots[12];\n                if (d1 != 0) { sameDir = (d1 == 1) == up2; oppDir = !sameDir; }\n                if (sameDir) alpha = PHAT_ALPHA_SAME;\n            }\n            if (firstInStep) { slots[12] = pImplied >= pHat ? 1 : 2; }\n            uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT);\n            if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE;\n            alpha = wmul(alpha, silenceScale);\n            adaptiveGate = wmul(adaptiveGate, silenceScale);\n            if (ret <= adaptiveGate) { pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); }\n            if (oppDir && REVERT_U > 0) {\n                uint256 pre = slots[11];\n                if (pre > pHat) pHat = pHat + wmul(pre - pHat, REVERT_U);\n                else pHat = pHat - wmul(pHat - pre, REVERT_U);\n            }\n            if (firstInStep) {\n                if (ret > RET_CAP) ret = RET_CAP;\n                sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(ret, WAD - SIGMA_DECAY);\n            }\n        }\n\n        // Interior retail routing equalizes fee-adjusted marginal prices.\n        // Subsequent trades in a step cannot be the initial arbitrage trade.\n        if (!firstInStep && feeUsed > 30 * BPS && gamma > 0) {\n            uint256 normalSpot = trade.isBuy\n                ? wdiv(wmul(spot, gamma), WAD - 30 * BPS)\n                : wdiv(wmul(spot, WAD - 30 * BPS), gamma);\n            pHat = wmul(pHat, 90 * WAD / 100) + wmul(normalSpot, 10 * WAD / 100);\n        }\n\n        {\n            uint256 ret = fastHat > 0 ? wdiv(absDiff(pImplied, fastHat), fastHat) : 0;\n            uint256 alpha;\n            if (firstInStep) {\n                uint256 processSigma = sigmaForFast;\n                uint256 obsVar = uncertainty + wmul(processSigma, processSigma);\n                uint256 probArb;\n                uint256 z2 = wdiv(wmul(ret, ret), obsVar);\n                uint256 density = _powWad(wdiv(WAD, WAD + z2 / 16), 8);\n                uint256 sigmaObs = _isqrt(obsVar * WAD);\n                uint256 odds = wmul(density, wdiv(6700000000000000, sigmaObs));\n                bool arbFlag = ret <= wmul(sigmaForFast, 1220000000000000000);\n                uint256 prior = arbFlag ? 650000000000000000 : 400000000000000000;\n                uint256 tr = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0;\n                if (tr > 3000000000000000) prior = wmul(prior, 6 * WAD / 10);\n                if (silent > 1) prior = wmul(prior, 8 * WAD / 10);\n                odds = wmul(odds, wdiv(prior, WAD - prior));\n                probArb = wdiv(odds, WAD + odds);\n                alpha = wmul(probArb, 600000000000000000) + wmul(WAD - probArb, 80000000000000000);\n                slots[15] = pImplied >= fastHat ? 1 : 2;\n                uncertainty = wmul(uncertainty, WAD - alpha);\n            } else {\n                if ((slots[15] == 1) == (pImplied >= fastHat)) alpha = PHAT_ALPHA_RETAIL;\n            }\n            uint256 gate = wmul(sigmaHat, GATE_SIGMA_MULT);\n            if (gate < MIN_GATE) gate = MIN_GATE;\n            if (ret <= gate) fastHat = wmul(fastHat, WAD - alpha) + wmul(pImplied, alpha);\n            slots[13] = uncertainty;\n        }\n\n        if (!firstInStep && feeUsed > 30 * BPS && gamma > 0) {\n            uint256 normalObs = trade.isBuy\n                ? wdiv(wmul(spot, gamma), WAD - 30 * BPS)\n                : wdiv(wmul(spot, WAD - 30 * BPS), gamma);\n            if ((slots[15] == 1) == (normalObs >= fastHat)) {\n                fastHat = wmul(fastHat, 9 * WAD / 10) + wmul(normalObs, WAD / 10);\n            }\n        }\n        slots[14] = fastHat;\n        uint256 tradeRatio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0;\n        if (tradeRatio > TRADE_RATIO_CAP) tradeRatio = TRADE_RATIO_CAP;\n\n        if (tradeRatio > SIGNAL_THRESHOLD) {\n            uint256 push = tradeRatio * DIR_IMPACT_MULT;\n            if (push > WAD / 4) push = WAD / 4;\n            if (trade.isBuy) {\n                dirState = dirState + push;\n                if (dirState > 2 * WAD) dirState = 2 * WAD;\n            } else {\n                dirState = dirState > push ? dirState - push : 0;\n            }\n            actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY);\n            sizeHat = wmul(sizeHat, SIZE_BLEND_DECAY) + wmul(tradeRatio, WAD - SIZE_BLEND_DECAY);\n            if (sizeHat > WAD) sizeHat = WAD;\n        }\n\n        uint256 tox = pHat > 0 ? wdiv(absDiff(spot, pHat), pHat) : 0;\n        if (tox > TOX_CAP) tox = TOX_CAP;\n        toxEma = wmul(toxEma, TOX_BLEND_DECAY) + wmul(tox, WAD - TOX_BLEND_DECAY);\n        uint256 toxSignal = toxEma;\n\n        stepTradeCount = stepTradeCount + 1;\n        if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP;\n\n        uint256 flowSize = wmul(lambdaHat, sizeHat);\n        uint256 fBase = BASE_FEE + wmul(SIGMA_COEF, sigmaHat) + wmul(LAMBDA_COEF, lambdaHat) + wmul(FLOW_SIZE_COEF, flowSize);\n        uint256 fMid = fBase + wmul(TOX_COEF, toxSignal) + wmul(TOX_QUAD_COEF, wmul(toxSignal, toxSignal)) + wmul(ACT_COEF, actEma);\n        fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal));\n\n        uint256 dirDev; bool sellPressure;\n        if (dirState >= WAD) { dirDev = dirState - WAD; sellPressure = true; }\n        else { dirDev = WAD - dirState; sellPressure = false; }\n\n        uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal));\n        uint256 bidFee; uint256 askFee;\n        if (sellPressure) {\n            bidFee = fMid + skew;\n            askFee = fMid > skew ? fMid - skew : 0;\n        } else {\n            askFee = fMid + skew;\n            bidFee = fMid > skew ? fMid - skew : 0;\n        }\n\n        {\n            uint256 staleHat = wmul(pHat, 700000000000000000) + wmul(fastHat, 300000000000000000);\n            uint256 staleTox = wdiv(absDiff(spot, staleHat), staleHat);\n            if (staleTox > TOX_CAP) staleTox = TOX_CAP;\n            uint256 staleShift = wmul(STALE_DIR_COEF, staleTox);\n            uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC);\n            if (spot >= staleHat) {\n                bidFee = bidFee + staleShift;\n                askFee = askFee > attractShift ? askFee - attractShift : 0;\n            } else {\n                askFee = askFee + staleShift;\n                bidFee = bidFee > attractShift ? bidFee - attractShift : 0;\n            }\n        }\n\n        {\n            bool tradeAligned = (trade.isBuy && spot >= pHat) || (!trade.isBuy && spot < pHat);\n            if (tradeAligned) {\n                uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio);\n                if (!firstInStep && tradeBoost > 16 * BPS) tradeBoost = 16 * BPS;\n                if (trade.isBuy) { bidFee = bidFee + tradeBoost; }\n                else { askFee = askFee + tradeBoost; }\n            }\n        }\n\n        if (sellPressure) {\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_PROTECT));\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_ATTRACT));\n        } else {\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_PROTECT));\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_ATTRACT));\n        }\n\n        slots[0] = bidFee; slots[1] = askFee; slots[2] = (spot << 64) | trade.timestamp;\n        slots[3] = dirState; slots[4] = actEma; slots[5] = pHat;\n        slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat;\n        slots[9] = toxEma; slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\n    }\n\n    function _compressTailWithSlope(uint256 fee, uint256 slope) internal pure returns (uint256) {\n        if (fee <= TAIL_KNEE) return fee;\n        return TAIL_KNEE + wmul(fee - TAIL_KNEE, slope);\n    }\n\n    function _powWad(uint256 factor, uint256 exp) internal pure returns (uint256 result) {\n        result = WAD;\n        while (exp > 0) {\n            if (exp & 1 == 1) result = wmul(result, factor);\n            factor = wmul(factor, factor);\n            exp >>= 1;\n        }\n    }\n\n    function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) {\n        uint256 mul = _powWad(decayFactor, elapsed);\n        if (centered >= WAD) { return WAD + wmul(centered - WAD, mul); }\n        uint256 below = wmul(WAD - centered, mul);\n        return below < WAD ? WAD - below : 0;\n    }\n\n    // Floor square root: bit-length seed and seven Newton steps (all uint256).\n    function _isqrt(uint256 x) internal pure returns (uint256 r) {\n        if (x == 0) return 0;\n        unchecked {\n            uint256 y = x;\n            r = 1;\n            if (y >> 128 != 0) { y >>= 128; r <<= 64; }\n            if (y >> 64 != 0) { y >>= 64; r <<= 32; }\n            if (y >> 32 != 0) { y >>= 32; r <<= 16; }\n            if (y >> 16 != 0) { y >>= 16; r <<= 8; }\n            if (y >> 8 != 0) { y >>= 8; r <<= 4; }\n            if (y >> 4 != 0) { y >>= 4; r <<= 2; }\n            if (y >> 2 != 0) { r <<= 1; }\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            uint256 t = x / r;\n            return r < t ? r : t;\n        }\n    }\n\n    function getName() external pure override returns (string memory) { return \"no_cubic\"; }\n}\n",
 "sha256": "fdcc291efe456ef7e874c2153ec5250686822984bb797543c2f9b2c9bc35a31c",
 "base_sha256": "610c1905e7dd5559b84e40a3b2d6f8e0b53a7b0ac7574ae33b6c1770a28367cb",
 "v2_sha256": "28417e116f57f1029fdd1803832e852574902c1ae7253c05657f317db76da4ab",
 "simulator_commit": "6b6921f8d463314aa7c96234e1be25a25245f704",
 "rule": "In G's aligned-trade block, after `uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio);` insert `if (!firstInStep && tradeBoost > K * BPS) tradeBoost = K * BPS;`. firstInStep = (stepTradeCount == 0), evaluated before the count increment (G line 84). tradeAligned test, side, tail compression, flow terms, stale shifts, estimators and direction skew unchanged. When firstInStep, G fees are returned unchanged.",
 "variants": {
  "K4": {
   "K": 4,
   "sha256": "304bdaa4301ba639f565deb04fff170021fe80367e86c6cf34c01f51c8d27806"
  },
  "K8": {
   "K": 8,
   "sha256": "e5e2784d9847a55b1c0fc07b393407b95cff3a031168a5a5d319f580ff1ca45a"
  },
  "K16": {
   "K": 16,
   "sha256": "fdcc291efe456ef7e874c2153ec5250686822984bb797543c2f9b2c9bc35a31c"
  },
  "K32": {
   "K": 32,
   "sha256": "bcdc7f031cc618174fcfdfd0667855ae86277cc2c373f60c53a2673dc55be1eb"
  }
 },
 "variant_generator": {
  "usage": "python gen.py G.sol  (writes scratch/K{4,8,16,32}.sol)",
  "source": "import sys,hashlib\nsrc=open(sys.argv[1]).read()\nOLD=\"                uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio);\\n\"\nassert src.count(OLD)==1\nfor K in (4,8,16,32):\n    new=OLD+\"                if (!firstInStep && tradeBoost > %d * BPS) tradeBoost = %d * BPS;\\n\"%(K,K)\n    out=src.replace(OLD,new)\n    p=\"scratch/K%d.sol\"%K; open(p,\"w\").write(out)\n    print(K,p,hashlib.sha256(out.encode()).hexdigest())\n"
 },
 "paired": {
  "n": 512,
  "mean_diff": 0.024837860619309926,
  "se": 0.005447715057273596,
  "sd": 0.12326772028704708,
  "n_nonzero": 507,
  "worst": {
   "seed": 22302,
   "diff": -0.3374747009715975,
   "edge_a": 477.9106822589233,
   "edge_b": 478.2481569598949
  },
  "worst5": [
   {
    "seed": 22302,
    "diff": -0.3374747009715975,
    "edge_a": 477.9106822589233,
    "edge_b": 478.2481569598949
   },
   {
    "seed": 22073,
    "diff": -0.32499663577550564,
    "edge_a": 649.0080844299102,
    "edge_b": 649.3330810656857
   },
   {
    "seed": 22243,
    "diff": -0.16098523093796757,
    "edge_a": 554.6013877720702,
    "edge_b": 554.7623730030082
   },
   {
    "seed": 22014,
    "diff": -0.15270746417729697,
    "edge_a": 630.7653957993203,
    "edge_b": 630.9181032634976
   },
   {
    "seed": 22092,
    "diff": -0.13929725619436795,
    "edge_a": 551.365999030722,
    "edge_b": 551.5052962869164
   }
  ],
  "stage": "tuning"
 },
 "seeds": {
  "screen": [
   20000,
   20255
  ],
  "tuning": [
   22000,
   22511
  ],
  "none_ge_4000000": true
 },
 "stages": {
  "screen": {
   "seeds": [
    20000,
    20255
   ],
   "n": 256,
   "v2_baseline_reused_for_all": true,
   "v2_gas": {
    "max_ok_afterSwap": 242807,
    "init_max": 236377,
    "max_ok_any": 242807,
    "oog": 1,
    "other_errors": 0,
    "oog_seeds": [
     [
      20120,
      1
     ]
    ],
    "vanilla_oog": 0,
    "vanilla_other_errors": 0
   },
   "v2_run_checks": {
    "complete": true,
    "counters_per_seed_match_global": true,
    "max_abs_reconciliation_error": 6.792788553866558e-12,
    "error": null,
    "seconds": 78.942
   },
   "results": {
    "G": {
     "sha256": "610c1905e7dd5559b84e40a3b2d6f8e0b53a7b0ac7574ae33b6c1770a28367cb",
     "valid": true,
     "invalid_reasons": [],
     "paired": {
      "n": 256,
      "mean_diff": 7.125186787249049e-07,
      "se": 7.125186787249048e-07,
      "sd": 1.1400298859598477e-05,
      "n_nonzero": 1,
      "worst": {
       "seed": 20000,
       "diff": 0.0,
       "edge_a": 665.2002798921455,
       "edge_b": 665.2002798921455
      },
      "worst5": [
       {
        "seed": 20000,
        "diff": 0.0,
        "edge_a": 665.2002798921455,
        "edge_b": 665.2002798921455
       },
       {
        "seed": 20001,
        "diff": 0.0,
        "edge_a": 567.1144477036481,
        "edge_b": 567.1144477036481
       },
       {
        "seed": 20002,
        "diff": 0.0,
        "edge_a": 392.31576206842044,
        "edge_b": 392.31576206842044
       },
       {
        "seed": 20003,
        "diff": 0.0,
        "edge_a": 578.97085140201,
        "edge_b": 578.97085140201
       },
       {
        "seed": 20004,
        "diff": 0.0,
        "edge_a": 567.5623192759305,
        "edge_b": 567.5623192759305
       }
      ]
     },
     "gas": {
      "max_ok_afterSwap": 211174,
      "init_max": 220944,
      "max_ok_any": 220944,
      "oog": 0,
      "other_errors": 0,
      "oog_seeds": [],
      "vanilla_oog": 0,
      "vanilla_other_errors": 0
     },
     "retail_edge_delta": {
      "mean": -6.020185186361715e-06,
      "se": 6.020185186361715e-06
     },
     "arb_edge_delta": {
      "mean": 6.732703870471202e-06,
      "se": 6.732703870471202e-06
     },
     "retail_volume_y_delta": {
      "mean": -0.008992851499215249,
      "se": 0.008992851499215249
     },
     "v2_volume_terciles": [
      {
       "tercile": "low",
       "n": 85,
       "mean_diff": 0.0,
       "se": 0.0,
       "v2_volume_y_range": [
        57993.978854731424,
        70034.4499637037
       ]
      },
      {
       "tercile": "mid",
       "n": 85,
       "mean_diff": 0.0,
       "se": 0.0,
       "v2_volume_y_range": [
        70763.75606739354,
        82050.93095984553
       ]
      },
      {
       "tercile": "high",
       "n": 86,
       "mean_diff": 2.120985834343903e-06,
       "se": 2.1209858343439028e-06,
       "v2_volume_y_range": [
        82207.89120657643,
        96938.17774984562
       ]
      }
     ],
     "run_checks": {
      "complete": true,
      "counters_per_seed_match_global": true,
      "max_abs_reconciliation_error": 6.792788553866558e-12,
      "error": null,
      "seconds": 69.362
     },
     "params": {
      "control": "unmodified G"
     }
    },
    "K4": {
     "sha256": "304bdaa4301ba639f565deb04fff170021fe80367e86c6cf34c01f51c8d27806",
     "valid": true,
     "invalid_reasons": [],
     "paired": {
      "n": 256,
      "mean_diff": -0.06357584645467629,
      "se": 0.017305453975916252,
      "sd": 0.27688726361466004,
      "n_nonzero": 256,
      "worst": {
       "seed": 20153,
       "diff": -1.334188033069836,
       "edge_a": 667.0629952791033,
       "edge_b": 668.3971833121732
      },
      "worst5": [
       {
        "seed": 20153,
        "diff": -1.334188033069836,
        "edge_a": 667.0629952791033,
        "edge_b": 668.3971833121732
       },
       {
        "seed": 20017,
        "diff": -0.9455682899188105,
        "edge_a": 575.7800259525285,
        "edge_b": 576.7255942424473
       },
       {
        "seed": 20069,
        "diff": -0.8891723883778013,
        "edge_a": 535.739109542636,
        "edge_b": 536.6282819310138
       },
       {
        "seed": 20031,
        "diff": -0.860395832067752,
        "edge_a": 643.8452267345501,
        "edge_b": 644.7056225666179
       },
       {
        "seed": 20161,
        "diff": -0.8381321284470005,
        "edge_a": 605.098030510229,
        "edge_b": 605.936162638676
       }
      ]
     },
     "gas": {
      "max_ok_afterSwap": 211336,
      "init_max": 220956,
      "max_ok_any": 220956,
      "oog": 0,
      "other_errors": 0,
      "oog_seeds": [],
      "vanilla_oog": 0,
      "vanilla_other_errors": 0
     },
     "retail_edge_delta": {
      "mean": -0.057209157713344894,
      "se": 0.01762532988693547
     },
     "arb_edge_delta": {
      "mean": -0.0063666887412063,
      "se": 0.0053943826144530575
     },
     "retail_volume_y_delta": {
      "mean": 125.60020666732487,
      "se": 3.586442226107012
     },
     "v2_volume_terciles": [
      {
       "tercile": "low",
       "n": 85,
       "mean_diff": -0.03554068704521636,
       "se": 0.022984437601697036,
       "v2_volume_y_range": [
        57993.978854731424,
        70034.4499637037
       ]
      },
      {
       "tercile": "mid",
       "n": 85,
       "mean_diff": -0.05736263379208675,
       "se": 0.025403851618598566,
       "v2_volume_y_range": [
        70763.75606739354,
        82050.93095984553
       ]
      },
      {
       "tercile": "high",
       "n": 86,
       "mean_diff": -0.09742598164216704,
       "se": 0.038794332471913544,
       "v2_volume_y_range": [
        82207.89120657643,
        96938.17774984562
       ]
      }
     ],
     "run_checks": {
      "complete": true,
      "counters_per_seed_match_global": true,
      "max_abs_reconciliation_error": 5.8548721426632255e-12,
      "error": null,
      "seconds": 70.13
     },
     "params": {
      "K": 4,
      "cap": "min(tradeBoost,4*BPS) when !firstInStep"
     }
    },
    "K8": {
     "sha256": "e5e2784d9847a55b1c0fc07b393407b95cff3a031168a5a5d319f580ff1ca45a",
     "valid": true,
     "invalid_reasons": [],
     "paired": {
      "n": 256,
      "mean_diff": 0.012884534086773591,
      "se": 0.01180254400911631,
      "sd": 0.18884070414586096,
      "n_nonzero": 256,
      "worst": {
       "seed": 20153,
       "diff": -1.0440220816776673,
       "edge_a": 667.3531612304955,
       "edge_b": 668.3971833121732
      },
      "worst5": [
       {
        "seed": 20153,
        "diff": -1.0440220816776673,
        "edge_a": 667.3531612304955,
        "edge_b": 668.3971833121732
       },
       {
        "seed": 20207,
        "diff": -0.6821597634847194,
        "edge_a": 720.4402760155364,
        "edge_b": 721.1224357790211
       },
       {
        "seed": 20238,
        "diff": -0.5387036571144108,
        "edge_a": 734.7929447354891,
        "edge_b": 735.3316483926035
       },
       {
        "seed": 20221,
        "diff": -0.45817273478076004,
        "edge_a": 668.2962420202604,
        "edge_b": 668.7544147550411
       },
       {
        "seed": 20076,
        "diff": -0.41725647597849047,
        "edge_a": 419.8988331707072,
        "edge_b": 420.31608964668567
       }
      ]
     },
     "gas": {
      "max_ok_afterSwap": 211334,
      "init_max": 220956,
      "max_ok_any": 220956,
      "oog": 0,
      "other_errors": 0,
      "oog_seeds": [],
      "vanilla_oog": 0,
      "vanilla_other_errors": 0
     },
     "retail_edge_delta": {
      "mean": 0.011433657257376995,
      "se": 0.011867604765428804
     },
     "arb_edge_delta": {
      "mean": 0.0014508768294015234,
      "se": 0.0029551261310491674
     },
     "retail_volume_y_delta": {
      "mean": 46.607525415938255,
      "se": 2.104492800746796
     },
     "v2_volume_terciles": [
      {
       "tercile": "low",
       "n": 85,
       "mean_diff": 0.025169203920211203,
       "se": 0.015619677336565378,
       "v2_volume_y_range": [
        57993.978854731424,
        70034.4499637037
       ]
      },
      {
       "tercile": "mid",
       "n": 85,
       "mean_diff": 0.020311363964727287,
       "se": 0.015241517181607427,
       "v2_volume_y_range": [
        70763.75606739354,
        82050.93095984553
       ]
      },
      {
       "tercile": "high",
       "n": 86,
       "mean_diff": -0.006597762139601538,
       "se": 0.027783417440834478,
       "v2_volume_y_range": [
        82207.89120657643,
        96938.17774984562
       ]
      }
     ],
     "run_checks": {
      "complete": true,
      "counters_per_seed_match_global": true,
      "max_abs_reconciliation_error": 7.844391802791506e-12,
      "error": null,
      "seconds": 70.178
     },
     "params": {
      "K": 8,
      "cap": "min(tradeBoost,8*BPS) when !firstInStep"
     }
    },
    "K16": {
     "sha256": "fdcc291efe456ef7e874c2153ec5250686822984bb797543c2f9b2c9bc35a31c",
     "valid": true,
     "invalid_reasons": [],
     "paired": {
      "n": 256,
      "mean_diff": 0.017552568417545267,
      "se": 0.006968941482207769,
      "sd": 0.1115030637153243,
      "n_nonzero": 250,
      "worst": {
       "seed": 20076,
       "diff": -0.46860022261211043,
       "edge_a": 419.84748942407356,
       "edge_b": 420.31608964668567
      },
      "worst5": [
       {
        "seed": 20076,
        "diff": -0.46860022261211043,
        "edge_a": 419.84748942407356,
        "edge_b": 420.31608964668567
       },
       {
        "seed": 20153,
        "diff": -0.39444690870459453,
        "edge_a": 668.0027364034686,
        "edge_b": 668.3971833121732
       },
       {
        "seed": 20205,
        "diff": -0.3044045715282664,
        "edge_a": 685.6964663391755,
        "edge_b": 686.0008709107037
       },
       {
        "seed": 20031,
        "diff": -0.24411491529463092,
        "edge_a": 644.4615076513232,
        "edge_b": 644.7056225666179
       },
       {
        "seed": 20145,
        "diff": -0.21065852133949647,
        "edge_a": 609.9960407305674,
        "edge_b": 610.2066992519069
       }
      ]
     },
     "gas": {
      "max_ok_afterSwap": 211334,
      "init_max": 220956,
      "max_ok_any": 220956,
      "oog": 0,
      "other_errors": 0,
      "oog_seeds": [],
      "vanilla_oog": 0,
      "vanilla_other_errors": 0
     },
     "retail_edge_delta": {
      "mean": 0.018894459105540662,
      "se": 0.007069629001403697
     },
     "arb_edge_delta": {
      "mean": -0.0013418906879538728,
      "se": 0.001274882201413139
     },
     "retail_volume_y_delta": {
      "mean": 10.51353840589374,
      "se": 1.3779786164709482
     },
     "v2_volume_terciles": [
      {
       "tercile": "low",
       "n": 85,
       "mean_diff": 0.00881432919340555,
       "se": 0.010822464545783256,
       "v2_volume_y_range": [
        57993.978854731424,
        70034.4499637037
       ]
      },
      {
       "tercile": "mid",
       "n": 85,
       "mean_diff": 0.014860976469495146,
       "se": 0.009199416269626011,
       "v2_volume_y_range": [
        70763.75606739354,
        82050.93095984553
       ]
      },
      {
       "tercile": "high",
       "n": 86,
       "mean_diff": 0.02884949457610499,
       "se": 0.015302627956816145,
       "v2_volume_y_range": [
        82207.89120657643,
        96938.17774984562
       ]
      }
     ],
     "run_checks": {
      "complete": true,
      "counters_per_seed_match_global": true,
      "max_abs_reconciliation_error": 5.6559201766503975e-12,
      "error": null,
      "seconds": 71.34
     },
     "params": {
      "K": 16,
      "cap": "min(tradeBoost,16*BPS) when !firstInStep"
     }
    },
    "K32": {
     "sha256": "bcdc7f031cc618174fcfdfd0667855ae86277cc2c373f60c53a2673dc55be1eb",
     "valid": true,
     "invalid_reasons": [],
     "paired": {
      "n": 256,
      "mean_diff": 0.002157114702040719,
      "se": 0.003024704750959111,
      "sd": 0.048395276015345774,
      "n_nonzero": 82,
      "worst": {
       "seed": 20076,
       "diff": -0.4637330918528164,
       "edge_a": 419.85235655483285,
       "edge_b": 420.31608964668567
      },
      "worst5": [
       {
        "seed": 20076,
        "diff": -0.4637330918528164,
        "edge_a": 419.85235655483285,
        "edge_b": 420.31608964668567
       },
       {
        "seed": 20041,
        "diff": -0.18664983613507502,
        "edge_a": 734.4521997159768,
        "edge_b": 734.6388495521119
       },
       {
        "seed": 20031,
        "diff": -0.1857384065400538,
        "edge_a": 644.5198841600778,
        "edge_b": 644.7056225666179
       },
       {
        "seed": 20157,
        "diff": -0.06531472104393288,
        "edge_a": 658.7235115790382,
        "edge_b": 658.7888263000822
       },
       {
        "seed": 20145,
        "diff": -0.057122511299326106,
        "edge_a": 610.1495767406076,
        "edge_b": 610.2066992519069
       }
      ]
     },
     "gas": {
      "max_ok_afterSwap": 211334,
      "init_max": 220956,
      "max_ok_any": 220956,
      "oog": 0,
      "other_errors": 0,
      "oog_seeds": [],
      "vanilla_oog": 0,
      "vanilla_other_errors": 0
     },
     "retail_edge_delta": {
      "mean": 0.0021683788977631124,
      "se": 0.002916254368560941
     },
     "arb_edge_delta": {
      "mean": -1.1264195677443412e-05,
      "se": 0.00044375281894083377
     },
     "retail_volume_y_delta": {
      "mean": 0.9115731072109554,
      "se": 0.24995246701951054
     },
     "v2_volume_terciles": [
      {
       "tercile": "low",
       "n": 85,
       "mean_diff": 0.001967365798484747,
       "se": 0.007812455421281439,
       "v2_volume_y_range": [
        57993.978854731424,
        70034.4499637037
       ]
      },
      {
       "tercile": "mid",
       "n": 85,
       "mean_diff": 0.0032446752965684562,
       "se": 0.0019712597367358125,
       "v2_volume_y_range": [
        70763.75606739354,
        82050.93095984553
       ]
      },
      {
       "tercile": "high",
       "n": 86,
       "mean_diff": 0.0012697426818942066,
       "se": 0.004274023028262045,
       "v2_volume_y_range": [
        82207.89120657643,
        96938.17774984562
       ]
      }
     ],
     "run_checks": {
      "complete": true,
      "counters_per_seed_match_global": true,
      "max_abs_reconciliation_error": 6.59383658785373e-12,
      "error": null,
      "seconds": 70.535
     },
     "params": {
      "K": 32,
      "cap": "min(tradeBoost,32*BPS) when !firstInStep"
     }
    }
   }
  },
  "tuning": {
   "seeds": [
    22000,
    22511
   ],
   "n": 512,
   "variant": "K16",
   "v2_gas": {
    "max_ok_afterSwap": 243605,
    "init_max": 236377,
    "max_ok_any": 243605,
    "oog": 6,
    "other_errors": 0,
    "oog_seeds": [
     [
      22086,
      1
     ],
     [
      22136,
      2
     ],
     [
      22442,
      3
     ]
    ],
    "vanilla_oog": 0,
    "vanilla_other_errors": 0
   },
   "v2_run_checks": {
    "complete": true,
    "counters_per_seed_match_global": true,
    "max_abs_reconciliation_error": 7.73070496506989e-12,
    "error": null,
    "seconds": 158.226
   },
   "result": {
    "sha256": "fdcc291efe456ef7e874c2153ec5250686822984bb797543c2f9b2c9bc35a31c",
    "valid": true,
    "invalid_reasons": [],
    "paired": {
     "n": 512,
     "mean_diff": 0.024837860619309926,
     "se": 0.005447715057273596,
     "sd": 0.12326772028704708,
     "n_nonzero": 507,
     "worst": {
      "seed": 22302,
      "diff": -0.3374747009715975,
      "edge_a": 477.9106822589233,
      "edge_b": 478.2481569598949
     },
     "worst5": [
      {
       "seed": 22302,
       "diff": -0.3374747009715975,
       "edge_a": 477.9106822589233,
       "edge_b": 478.2481569598949
      },
      {
       "seed": 22073,
       "diff": -0.32499663577550564,
       "edge_a": 649.0080844299102,
       "edge_b": 649.3330810656857
      },
      {
       "seed": 22243,
       "diff": -0.16098523093796757,
       "edge_a": 554.6013877720702,
       "edge_b": 554.7623730030082
      },
      {
       "seed": 22014,
       "diff": -0.15270746417729697,
       "edge_a": 630.7653957993203,
       "edge_b": 630.9181032634976
      },
      {
       "seed": 22092,
       "diff": -0.13929725619436795,
       "edge_a": 551.365999030722,
       "edge_b": 551.5052962869164
      }
     ]
    },
    "gas": {
     "max_ok_afterSwap": 211358,
     "init_max": 220956,
     "max_ok_any": 220956,
     "oog": 0,
     "other_errors": 0,
     "oog_seeds": [],
     "vanilla_oog": 0,
     "vanilla_other_errors": 0
    },
    "retail_edge_delta": {
     "mean": 0.026499161581571506,
     "se": 0.005739353455250888
    },
    "arb_edge_delta": {
     "mean": -0.001661300962183039,
     "se": 0.001055411056423464
    },
    "retail_volume_y_delta": {
     "mean": 10.757929555841642,
     "se": 1.2372163518971107
    },
    "v2_volume_terciles": [
     {
      "tercile": "low",
      "n": 170,
      "mean_diff": 0.010142482150891564,
      "se": 0.0085835901387339,
      "v2_volume_y_range": [
       57217.7264928254,
       71012.91741447589
      ]
     },
     {
      "tercile": "mid",
      "n": 171,
      "mean_diff": 0.026886580863826927,
      "se": 0.005248104553028521,
      "v2_volume_y_range": [
       71066.6132568273,
       82164.31782400906
      ]
     },
     {
      "tercile": "high",
      "n": 171,
      "mean_diff": 0.037398580957431066,
      "se": 0.012826764899170428,
      "v2_volume_y_range": [
       82203.85587259429,
       97931.83497242021
      ]
     }
    ],
    "run_checks": {
     "complete": true,
     "counters_per_seed_match_global": true,
     "max_abs_reconciliation_error": 7.503331289626658e-12,
     "error": null,
     "seconds": 141.617
    }
   }
  }
 },
 "selection": {
  "rule": "best valid mean_diff vs V2 on screen",
  "ranking_screen_mean_diff": {
   "G": 7.125186787249049e-07,
   "K4": -0.06357584645467629,
   "K8": 0.012884534086773591,
   "K16": 0.017552568417545267,
   "K32": 0.002157114702040719
  },
  "selected": "K16"
 },
 "freeze": {
  "variant": "K16",
  "K": 16,
  "sha256": "fdcc291efe456ef7e874c2153ec5250686822984bb797543c2f9b2c9bc35a31c",
  "frozen_after": "tuning stage 22000..22511"
 },
 "gas": {
  "max_ok_afterSwap": 211358,
  "init_max": 220956,
  "max_ok_any": 220956,
  "oog": 0,
  "other_errors": 0,
  "oog_seeds": [],
  "vanilla_oog": 0,
  "vanilla_other_errors": 0,
  "stage": "tuning"
 },
 "v2_gas": {
  "max_ok_afterSwap": 243605,
  "init_max": 236377,
  "max_ok_any": 243605,
  "oog": 6,
  "other_errors": 0,
  "oog_seeds": [
   [
    22086,
    1
   ],
   [
    22136,
    2
   ],
   [
    22442,
    3
   ]
  ],
  "vanilla_oog": 0,
  "vanilla_other_errors": 0,
  "stage": "tuning"
 },
 "G_control": {
  "sha256": "610c1905e7dd5559b84e40a3b2d6f8e0b53a7b0ac7574ae33b6c1770a28367cb",
  "valid": true,
  "invalid_reasons": [],
  "paired": {
   "n": 256,
   "mean_diff": 7.125186787249049e-07,
   "se": 7.125186787249048e-07,
   "sd": 1.1400298859598477e-05,
   "n_nonzero": 1,
   "worst": {
    "seed": 20000,
    "diff": 0.0,
    "edge_a": 665.2002798921455,
    "edge_b": 665.2002798921455
   },
   "worst5": [
    {
     "seed": 20000,
     "diff": 0.0,
     "edge_a": 665.2002798921455,
     "edge_b": 665.2002798921455
    },
    {
     "seed": 20001,
     "diff": 0.0,
     "edge_a": 567.1144477036481,
     "edge_b": 567.1144477036481
    },
    {
     "seed": 20002,
     "diff": 0.0,
     "edge_a": 392.31576206842044,
     "edge_b": 392.31576206842044
    },
    {
     "seed": 20003,
     "diff": 0.0,
     "edge_a": 578.97085140201,
     "edge_b": 578.97085140201
    },
    {
     "seed": 20004,
     "diff": 0.0,
     "edge_a": 567.5623192759305,
     "edge_b": 567.5623192759305
    }
   ]
  },
  "gas": {
   "max_ok_afterSwap": 211174,
   "init_max": 220944,
   "max_ok_any": 220944,
   "oog": 0,
   "other_errors": 0,
   "oog_seeds": [],
   "vanilla_oog": 0,
   "vanilla_other_errors": 0
  },
  "retail_edge_delta": {
   "mean": -6.020185186361715e-06,
   "se": 6.020185186361715e-06
  },
  "arb_edge_delta": {
   "mean": 6.732703870471202e-06,
   "se": 6.732703870471202e-06
  },
  "retail_volume_y_delta": {
   "mean": -0.008992851499215249,
   "se": 0.008992851499215249
  },
  "v2_volume_terciles": [
   {
    "tercile": "low",
    "n": 85,
    "mean_diff": 0.0,
    "se": 0.0,
    "v2_volume_y_range": [
     57993.978854731424,
     70034.4499637037
    ]
   },
   {
    "tercile": "mid",
    "n": 85,
    "mean_diff": 0.0,
    "se": 0.0,
    "v2_volume_y_range": [
     70763.75606739354,
     82050.93095984553
    ]
   },
   {
    "tercile": "high",
    "n": 86,
    "mean_diff": 2.120985834343903e-06,
    "se": 2.1209858343439028e-06,
    "v2_volume_y_range": [
     82207.89120657643,
     96938.17774984562
    ]
   }
  ],
  "run_checks": {
   "complete": true,
   "counters_per_seed_match_global": true,
   "max_abs_reconciliation_error": 6.792788553866558e-12,
   "error": null,
   "seconds": 69.362
  },
  "params": {
   "control": "unmodified G"
  },
  "note": "unmodified G vs V2 on screen block; nonzero only via V2 OOG seed(s)"
 },
 "edge_deltas_tuning": {
  "retail": {
   "mean": 0.026499161581571506,
   "se": 0.005739353455250888
  },
  "arb": {
   "mean": -0.001661300962183039,
   "se": 0.001055411056423464
  },
  "retail_volume_y": {
   "mean": 10.757929555841642,
   "se": 1.2372163518971107
  }
 },
 "v2_volume_terciles_tuning": [
  {
   "tercile": "low",
   "n": 170,
   "mean_diff": 0.010142482150891564,
   "se": 0.0085835901387339,
   "v2_volume_y_range": [
    57217.7264928254,
    71012.91741447589
   ]
  },
  {
   "tercile": "mid",
   "n": 171,
   "mean_diff": 0.026886580863826927,
   "se": 0.005248104553028521,
   "v2_volume_y_range": [
    71066.6132568273,
    82164.31782400906
   ]
  },
  {
   "tercile": "high",
   "n": 171,
   "mean_diff": 0.037398580957431066,
   "se": 0.012826764899170428,
   "v2_volume_y_range": [
    82203.85587259429,
    97931.83497242021
   ]
  }
 ],
 "checks": {
  "G_sha_verified": true,
  "V2_sha_verified": true,
  "setup_exit": 0,
  "instrumentation_patch_applied": true,
  "maturin_release_rebuild": true,
  "harness_tests_all_pass": true,
  "amm_match_validate_variants": "all 4 validated",
  "chunk": 32,
  "n_workers": 8,
  "steps": 10000,
  "afterSwap_gas_limit": 250000,
  "validity": "harness.validity(mode=ref): complete seeds, per-seed/global counters, |retail+arb-total|<1e-8, no other errors, Vanilla clean, cand OOG <= V2 OOG",
  "all_stages_valid": true
 },
 "notes": [
  "Training only (screen + tuning); no confirmation run, confirmation=null.",
  "Tuning stage +0.0248 (se 0.0054) is far below the +5.0 top-10 target; this cap is a small effect.",
  "No board claim."
 ]
}