{
 "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] = 30 * BPS; slots[1] = 30 * BPS;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000;\n        slots[13] = 902500000000; slots[14] = slots[5];\n        return (30 * BPS, 30 * BPS);\n    }\n\n    function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) {\n        uint256 prevBidFee = slots[0]; uint256 prevAskFee = slots[1]; uint256 packed = slots[2]; uint256 lastTs = uint64(packed);\n        uint256 pHat = slots[5];\n        uint256 sigmaHat = slots[6];\n        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            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 = packed >> 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[11] = pHat; 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 = _sqrtFloor(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        stepTradeCount = stepTradeCount + 1;\n        if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP;\n\n        uint256 fair = wmul(pHat, 70 * WAD / 100) + wmul(fastHat, 30 * WAD / 100);\n        int256 gap = int256(spot * 10000 / fair) - 10000;\n        uint256 vb = sigmaHat * 10000 / WAD;\n        uint256 bidFee = _tableFee(gap, vb);\n        uint256 askFee = _tableFee(-gap, vb);\n\n        slots[0] = bidFee; slots[1] = askFee; slots[2] = (spot << 64) | trade.timestamp;\n        slots[5] = pHat;\n        slots[6] = sigmaHat;\n\n        slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\n    }\n\n\n    function _cell(bytes memory t, uint256 r, uint256 c) internal pure returns (uint256) {\n        uint256 k = 2 * (9 * r + c);\n        return (uint256(uint8(t[k])) << 8) | uint256(uint8(t[k+1]));\n    }\n    function _tableFee(int256 gap, uint256 vb) internal pure returns (uint256) {\n        bytes memory t = hex\"0014001400140014001e004e008000b200e20014001400140014001e004e008000b200e20014001400140014001e0050008200b200e40014001400140014001e0050008200b300e4\";\n        if (gap < -200) gap = -200;\n        if (gap > 199) gap = 199;\n        if (vb > 29) vb = 29;\n        uint256 x = uint256(gap + 200);\n        uint256 c = x / 50; uint256 dx = x % 50;\n        uint256 r = vb / 10; uint256 dv = vb % 10;\n        uint256 lo = _cell(t,r,c)*(50-dx)+_cell(t,r,c+1)*dx;\n        uint256 hi = _cell(t,r+1,c)*(50-dx)+_cell(t,r+1,c+1)*dx;\n        return (lo*(10-dv)+hi*dv)*BPS/500;\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 _sqrtFloor(uint256 x) internal pure returns (uint256 z) {\n        if (x == 0) return 0;\n        uint256 a = x; uint256 bits;\n        if (a >> 128 > 0) { a >>= 128; bits += 128; }\n        if (a >> 64 > 0) { a >>= 64; bits += 64; }\n        if (a >> 32 > 0) { a >>= 32; bits += 32; }\n        if (a >> 16 > 0) { a >>= 16; bits += 16; }\n        if (a >> 8 > 0) { a >>= 8; bits += 8; }\n        if (a >> 4 > 0) { a >>= 4; bits += 4; }\n        if (a >> 2 > 0) { a >>= 2; bits += 2; }\n        if (a >> 1 > 0) { bits += 1; }\n        z = uint256(1) << ((bits + 2) / 2);\n        while (true) { uint256 y = (z + x / z) / 2; if (y >= z) return z; z = y; }\n    }\n\n    function getName() external pure override returns (string memory) { return \"routing_R0\"; }\n}\n",
 "sha256": "9e65b5a2e9d192c5e09fcad07572de005faa824638eeaf81c75fff88869894f7",
 "design": {
  "name": "R0",
  "decision": "Reject for promotion: the frozen routing table underperforms V2 on the untouched confirmation block.",
  "origin": "Supplied C0 routing-table candidate, source sha256 83181f5c4bd06665d6d13167a018397ef31d3bf31720bd75e2f0002f2f984849. Reused its offline table and slow/fast price estimates. The unmeasured C1-C5 table sources were not present in the supplied artifact; these variants modify the supplied C0 table instead.",
  "mechanism": "Use 70% slow / 30% fast estimated fair price. Compute signed post-trade spot gap in bps. Bilinearly interpolate a fee table over gap and estimated sigma; mirror gap for bid/ask. No true price, config seed, oracle or external call is available to this contract.",
  "table_generation": "Sample C0 gap columns at -200,-150,-100,-50,0,50,100,150,200 for sigma rows 0,10,20,30 bps. Subtract each original row center, multiply by slope, add new center, clamp to floor/cap, and round to integer bps. Runtime clamps gap to [-200,199] and sigma to [0,29], then interpolates with integer arithmetic.",
  "gas_changes": [
   "Remove direction, activity, toxicity, size and arrival-rate estimator state and updates, all unread by table mapping.",
   "Remove initialization zero writes.",
   "Pack last spot above a 64-bit timestamp in slot 2, replacing slot 16.",
   "Replace base Babylonian sqrt with a bit-length initial bound and converged Newton iteration returning the exact floor."
  ],
  "retail_bounded_variants": [
   "R8",
   "R9",
   "R10",
   "R11"
  ],
  "retail_bounds": "R8-R11 return 28..36 bps on both sides for all table queries; initialization returns 30 bps. The frozen strategy need not obey that upper bound.",
  "selected_params": {
   "floor": 20,
   "center": 30,
   "slope": 1,
   "cap": 230,
   "table": [
    [
     20,
     20,
     20,
     20,
     30,
     78,
     128,
     178,
     226
    ],
    [
     20,
     20,
     20,
     20,
     30,
     78,
     128,
     178,
     226
    ],
    [
     20,
     20,
     20,
     20,
     30,
     80,
     130,
     178,
     228
    ],
    [
     20,
     20,
     20,
     20,
     30,
     80,
     130,
     179,
     228
    ]
   ]
  },
  "interpretation": "This is a failed fee-mapping pilot, with a valid measured result under the corrected comparator OOG rule. Lower gas alone did not recover score. No leaderboard score or rank is claimed."
 },
 "tuning": [
  {
   "id": "R0",
   "stage": "screen",
   "params": {
    "floor": 20,
    "center": 30,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      20,
      20,
      20,
      20,
      30,
      78,
      128,
      178,
      226
     ],
     [
      20,
      20,
      20,
      20,
      30,
      78,
      128,
      178,
      226
     ],
     [
      20,
      20,
      20,
      20,
      30,
      80,
      130,
      178,
      228
     ],
     [
      20,
      20,
      20,
      20,
      30,
      80,
      130,
      179,
      228
     ]
    ]
   },
   "n": 256,
   "mean_diff": -58.685372674490665,
   "se": 1.4161221156739274,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R1",
   "stage": "screen",
   "params": {
    "floor": 24,
    "center": 30,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      24,
      24,
      24,
      24,
      30,
      78,
      128,
      178,
      226
     ],
     [
      24,
      24,
      24,
      24,
      30,
      78,
      128,
      178,
      226
     ],
     [
      24,
      24,
      24,
      24,
      30,
      80,
      130,
      178,
      228
     ],
     [
      24,
      24,
      24,
      24,
      30,
      80,
      130,
      179,
      228
     ]
    ]
   },
   "n": 256,
   "mean_diff": -60.45430242666816,
   "se": 1.4181198733215268,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R2",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 30,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      28,
      28,
      28,
      28,
      30,
      78,
      128,
      178,
      226
     ],
     [
      28,
      28,
      28,
      28,
      30,
      78,
      128,
      178,
      226
     ],
     [
      28,
      28,
      28,
      28,
      30,
      80,
      130,
      178,
      228
     ],
     [
      28,
      28,
      28,
      28,
      30,
      80,
      130,
      179,
      228
     ]
    ]
   },
   "n": 256,
   "mean_diff": -62.06038493992363,
   "se": 1.4262682193997396,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R3",
   "stage": "screen",
   "params": {
    "floor": 20,
    "center": 33,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      20,
      20,
      20,
      20,
      33,
      81,
      131,
      181,
      229
     ],
     [
      20,
      20,
      20,
      20,
      33,
      81,
      131,
      181,
      229
     ],
     [
      20,
      20,
      20,
      20,
      33,
      83,
      133,
      181,
      230
     ],
     [
      20,
      20,
      20,
      20,
      33,
      83,
      133,
      182,
      230
     ]
    ]
   },
   "n": 256,
   "mean_diff": -61.250944469222645,
   "se": 1.4012970418732311,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R4",
   "stage": "screen",
   "params": {
    "floor": 24,
    "center": 33,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      24,
      24,
      24,
      24,
      33,
      81,
      131,
      181,
      229
     ],
     [
      24,
      24,
      24,
      24,
      33,
      81,
      131,
      181,
      229
     ],
     [
      24,
      24,
      24,
      24,
      33,
      83,
      133,
      181,
      230
     ],
     [
      24,
      24,
      24,
      24,
      33,
      83,
      133,
      182,
      230
     ]
    ]
   },
   "n": 256,
   "mean_diff": -62.71422418561202,
   "se": 1.4020314755157648,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R5",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 33,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      28,
      28,
      28,
      28,
      33,
      81,
      131,
      181,
      229
     ],
     [
      28,
      28,
      28,
      28,
      33,
      81,
      131,
      181,
      229
     ],
     [
      28,
      28,
      28,
      28,
      33,
      83,
      133,
      181,
      230
     ],
     [
      28,
      28,
      28,
      28,
      33,
      83,
      133,
      182,
      230
     ]
    ]
   },
   "n": 256,
   "mean_diff": -64.13247964242375,
   "se": 1.4082744186515497,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R6",
   "stage": "screen",
   "params": {
    "floor": 24,
    "center": 36,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      24,
      24,
      24,
      24,
      36,
      84,
      134,
      184,
      230
     ],
     [
      24,
      24,
      24,
      24,
      36,
      84,
      134,
      184,
      230
     ],
     [
      24,
      24,
      24,
      24,
      36,
      86,
      136,
      184,
      230
     ],
     [
      24,
      24,
      24,
      24,
      36,
      86,
      136,
      185,
      230
     ]
    ]
   },
   "n": 256,
   "mean_diff": -65.6288574267873,
   "se": 1.4008775179248303,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R7",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 36,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      28,
      28,
      28,
      28,
      36,
      84,
      134,
      184,
      230
     ],
     [
      28,
      28,
      28,
      28,
      36,
      84,
      134,
      184,
      230
     ],
     [
      28,
      28,
      28,
      28,
      36,
      86,
      136,
      184,
      230
     ],
     [
      28,
      28,
      28,
      28,
      36,
      86,
      136,
      185,
      230
     ]
    ]
   },
   "n": 256,
   "mean_diff": -67.02301387477026,
   "se": 1.4074616282769643,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R8",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 32,
    "slope": 0.25,
    "cap": 36,
    "table": [
     [
      28,
      28,
      28,
      28,
      32,
      36,
      36,
      36,
      36
     ],
     [
      28,
      28,
      28,
      28,
      32,
      36,
      36,
      36,
      36
     ],
     [
      28,
      28,
      28,
      28,
      32,
      36,
      36,
      36,
      36
     ],
     [
      28,
      28,
      28,
      28,
      32,
      36,
      36,
      36,
      36
     ]
    ]
   },
   "n": 256,
   "mean_diff": -168.63382864787232,
   "se": 2.6907425328979406,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R9",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 34,
    "slope": 0.25,
    "cap": 36,
    "table": [
     [
      28,
      28,
      28,
      28,
      34,
      36,
      36,
      36,
      36
     ],
     [
      28,
      28,
      28,
      28,
      34,
      36,
      36,
      36,
      36
     ],
     [
      28,
      28,
      28,
      28,
      34,
      36,
      36,
      36,
      36
     ],
     [
      28,
      28,
      28,
      29,
      34,
      36,
      36,
      36,
      36
     ]
    ]
   },
   "n": 256,
   "mean_diff": -169.50546011432832,
   "se": 2.6769661879232998,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R10",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 36,
    "slope": 0.25,
    "cap": 36,
    "table": [
     [
      30,
      30,
      30,
      30,
      36,
      36,
      36,
      36,
      36
     ],
     [
      30,
      30,
      30,
      30,
      36,
      36,
      36,
      36,
      36
     ],
     [
      30,
      30,
      30,
      30,
      36,
      36,
      36,
      36,
      36
     ],
     [
      30,
      30,
      30,
      31,
      36,
      36,
      36,
      36,
      36
     ]
    ]
   },
   "n": 256,
   "mean_diff": -170.43733646643793,
   "se": 2.6629562987716637,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R11",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 33,
    "slope": 0,
    "cap": 36,
    "table": [
     [
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33
     ],
     [
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33
     ],
     [
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33
     ],
     [
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33,
      33
     ]
    ]
   },
   "n": 256,
   "mean_diff": -175.84506065839764,
   "se": 2.731200784539368,
   "gas": {
    "max_ok": 178787,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R12",
   "stage": "screen",
   "params": {
    "floor": 24,
    "center": 33,
    "slope": 0.5,
    "cap": 160,
    "table": [
     [
      24,
      24,
      24,
      24,
      33,
      57,
      82,
      107,
      131
     ],
     [
      24,
      24,
      24,
      24,
      33,
      57,
      82,
      107,
      131
     ],
     [
      24,
      24,
      24,
      24,
      33,
      58,
      83,
      107,
      132
     ],
     [
      24,
      24,
      24,
      24,
      33,
      58,
      83,
      108,
      132
     ]
    ]
   },
   "n": 256,
   "mean_diff": -84.07366009646536,
   "se": 1.8630885794749392,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R13",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 33,
    "slope": 0.5,
    "cap": 160,
    "table": [
     [
      28,
      28,
      28,
      28,
      33,
      57,
      82,
      107,
      131
     ],
     [
      28,
      28,
      28,
      28,
      33,
      57,
      82,
      107,
      131
     ],
     [
      28,
      28,
      28,
      28,
      33,
      58,
      83,
      107,
      132
     ],
     [
      28,
      28,
      28,
      28,
      33,
      58,
      83,
      108,
      132
     ]
    ]
   },
   "n": 256,
   "mean_diff": -86.30377766284171,
   "se": 1.8670918894278772,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R14",
   "stage": "screen",
   "params": {
    "floor": 24,
    "center": 33,
    "slope": 1.5,
    "cap": 230,
    "table": [
     [
      24,
      24,
      24,
      24,
      33,
      105,
      180,
      230,
      230
     ],
     [
      24,
      24,
      24,
      24,
      33,
      105,
      180,
      230,
      230
     ],
     [
      24,
      24,
      24,
      24,
      33,
      108,
      183,
      230,
      230
     ],
     [
      24,
      24,
      24,
      24,
      33,
      108,
      183,
      230,
      230
     ]
    ]
   },
   "n": 256,
   "mean_diff": -108.29512821431766,
   "se": 1.5035159541801963,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R15",
   "stage": "screen",
   "params": {
    "floor": 28,
    "center": 33,
    "slope": 1.5,
    "cap": 230,
    "table": [
     [
      28,
      28,
      28,
      28,
      33,
      105,
      180,
      230,
      230
     ],
     [
      28,
      28,
      28,
      28,
      33,
      105,
      180,
      230,
      230
     ],
     [
      28,
      28,
      28,
      28,
      33,
      108,
      183,
      230,
      230
     ],
     [
      28,
      28,
      28,
      28,
      33,
      108,
      183,
      230,
      230
     ]
    ]
   },
   "n": 256,
   "mean_diff": -109.49622178794262,
   "se": 1.5082248514665706,
   "gas": {
    "max_ok": 178799,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 242788,
    "oog": 2,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    60000,
    60256
   ]
  },
  {
   "id": "R0",
   "stage": "select",
   "params": {
    "floor": 20,
    "center": 30,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      20,
      20,
      20,
      20,
      30,
      78,
      128,
      178,
      226
     ],
     [
      20,
      20,
      20,
      20,
      30,
      78,
      128,
      178,
      226
     ],
     [
      20,
      20,
      20,
      20,
      30,
      80,
      130,
      178,
      228
     ],
     [
      20,
      20,
      20,
      20,
      30,
      80,
      130,
      179,
      228
     ]
    ]
   },
   "n": 512,
   "mean_diff": -60.65510860016642,
   "se": 1.0360730790068138,
   "gas": {
    "max_ok": 178767,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 243649,
    "oog": 5,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    62000,
    62512
   ]
  },
  {
   "id": "R1",
   "stage": "select",
   "params": {
    "floor": 24,
    "center": 30,
    "slope": 1,
    "cap": 230,
    "table": [
     [
      24,
      24,
      24,
      24,
      30,
      78,
      128,
      178,
      226
     ],
     [
      24,
      24,
      24,
      24,
      30,
      78,
      128,
      178,
      226
     ],
     [
      24,
      24,
      24,
      24,
      30,
      80,
      130,
      178,
      228
     ],
     [
      24,
      24,
      24,
      24,
      30,
      80,
      130,
      179,
      228
     ]
    ]
   },
   "n": 512,
   "mean_diff": -62.20208988713736,
   "se": 1.0360640052542904,
   "gas": {
    "max_ok": 178767,
    "oog": 0,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 154608
   },
   "v2_gas": {
    "max_ok": 243649,
    "oog": 5,
    "reverts": 0,
    "other_halts": 0,
    "init_max": 236377
   },
   "valid": true,
   "seeds": [
    62000,
    62512
   ]
  }
 ],
 "paired": {
  "n": 2000,
  "mean_diff": -61.872396024831275,
  "se": 0.5570296725511997,
  "sd": 24.911124266178625,
  "n_nonzero": 2000,
  "max_abs_diff": 224.10725158292678,
  "worst": [
   {
    "seed": 4090805,
    "diff": -224.10725158292678,
    "edge": 515.2580181836979,
    "v2_edge": 739.3652697666247
   },
   {
    "seed": 4091646,
    "diff": -212.7300121939079,
    "edge": 546.408657799921,
    "v2_edge": 759.1386699938289
   },
   {
    "seed": 4091306,
    "diff": -204.61200925519557,
    "edge": 431.0695556483651,
    "v2_edge": 635.6815649035607
   },
   {
    "seed": 4091779,
    "diff": -202.83673334507102,
    "edge": 625.4084285903622,
    "v2_edge": 828.2451619354332
   },
   {
    "seed": 4091430,
    "diff": -199.15217417751967,
    "edge": 536.4127378294634,
    "v2_edge": 735.564912006983
   }
  ]
 },
 "gas": {
  "max_ok": 178799,
  "oog": 0,
  "reverts": 0,
  "other_halts": 0,
  "init_max": 154608
 },
 "v2_gas": {
  "max_ok": 243713,
  "oog": 54,
  "reverts": 0,
  "other_halts": 0,
  "init_max": 236377
 },
 "seeds": {
  "screen": [
   60000,
   60256
  ],
  "selection": [
   62000,
   62512
  ],
  "holdout": [
   4090000,
   4092000
  ],
  "end_exclusive": true,
  "confirmation_runs": 1
 },
 "blocked": null,
 "valid": true,
 "validation": {
  "command": "amm-match validate scratch/R0.sol",
  "exit_code": 0,
  "stdout": "Validating strategy...\nCompiling strategy...\nStrategy 'routing_R0' validated successfully!\n",
  "stderr": "",
  "source_hashes_verified": true,
  "sqrt_floor_cases": 10010,
  "config_equality_check_seeds": [
   95,
   100
  ],
  "table_shape_and_bounds_checked": true
 },
 "screen_callback_calls": 28561805,
 "frozen_before_holdout": {
  "id": "R0",
  "sha256": "9e65b5a2e9d192c5e09fcad07572de005faa824638eeaf81c75fff88869894f7",
  "selection_mean": -60.65510860016642,
  "selection_command": ". scratch/env.sh && python scratch/experiment.py select 62000 62512 R0 R1",
  "holdout_read": false
 },
 "source_quote_checks": [
  {
   "url": "https://api.imd.fun/artifacts/02c134d9ed51858f06d02b6dec25e6922016490f11e3a8174af512a6a77b6569",
   "quote_verified": true,
   "response_sha256": "02c134d9ed51858f06d02b6dec25e6922016490f11e3a8174af512a6a77b6569"
  },
  {
   "url": "https://api.imd.fun/artifacts/045a5e942a564d4acdd31950593749af412466c39ac11a9336262560d5e1cd70",
   "quote_verified": true,
   "response_sha256": "045a5e942a564d4acdd31950593749af412466c39ac11a9336262560d5e1cd70"
  },
  {
   "url": "https://api.imd.fun/artifacts/ddbbaac58282566e0505d895c83daf348487cc322e8c482e61d4d87e6e4628de",
   "quote_verified": true,
   "response_sha256": "ddbbaac58282566e0505d895c83daf348487cc322e8c482e61d4d87e6e4628de"
  },
  {
   "url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/match.py",
   "quote_verified": true,
   "response_sha256": "78915a1aa36341e61c29f333fb18b31a319f70ce3e8bc2f737ba8b003aea5c17"
  },
  {
   "url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/contracts/src/AMMStrategyBase.sol",
   "quote_verified": true,
   "response_sha256": "3f4154a6c4bb8f1223c7fc0bde1f9cd9dba10cef502e300d0f6d229f610b8869"
  }
 ],
 "measurement": {
  "repository": "https://github.com/benedictbrady/amm-challenge",
  "commit": "6b6921f8d463314aa7c96234e1be25a25245f704",
  "v2_sha256": "28417e116f57f1029fdd1803832e852574902c1ae7253c05657f317db76da4ab",
  "n_steps": 10000,
  "workers": 8,
  "normalizer": "VanillaStrategy",
  "comparison": "Each strategy is run separately against VanillaStrategy with identical configs; paired difference is candidate submission edge minus V2 submission edge.",
  "configuration": "The pinned MatchRunner._build_configs source is executed with its seed loop restricted to the requested interval. All draws, their order and config fields are unchanged. Each RNG is seeded by the actual target seed.",
  "selection": "Top two eligible screen means advance to 512 seeds; freeze the higher selection mean before any confirmation outcomes are read.",
  "validity": "Corrected requester rule: candidate OOG count <= V2 count on the same seeds, zero candidate reverts/other halts, all seeds completed, and candidate maximum successful swap gas < V2 maximum. Original harness valid flag rejects any OOG; that stricter flag is retained in raw run records but superseded here.",
  "statistics": "Arithmetic mean of paired seed differences; SE is sample standard deviation (ddof=1) divided by sqrt(n). No seeds or outliers removed. worst contains five most negative paired differences.",
  "gas_fields": "max_ok is maximum successful afterSwap gas; init_max is maximum successful afterInitialize gas; errors sum all instrumented submission callbacks. Gas limit is unchanged at 250000 for afterSwap.",
  "candidate_seconds": 269.792,
  "v2_seconds": 528.692,
  "candidate_raw_run": {
   "label": "R0",
   "seconds": 269.792,
   "n_sims": 2000,
   "error": null,
   "valid": true,
   "submission_errors": {
    "oog": 0,
    "revert": 0,
    "other_halt": 0,
    "tx_error": 0,
    "bad_return": 0
   },
   "max_ok_gas_afterSwap": 178799,
   "max_ok_gas_all": 178799,
   "stats": [
    {
     "role": "Baseline",
     "callback": "afterInitialize",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 21796,
     "sum_ok_gas": 43592000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Baseline",
     "callback": "afterSwap",
     "calls": 23219735,
     "ok": 23219735,
     "max_ok_gas": 22592,
     "sum_ok_gas": 523925858956,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Baseline",
     "callback": "getName",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 21526,
     "sum_ok_gas": 43052000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Submission",
     "callback": "afterInitialize",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 154608,
     "sum_ok_gas": 309216000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Submission",
     "callback": "afterSwap",
     "calls": 12302437,
     "ok": 12302437,
     "max_ok_gas": 178799,
     "sum_ok_gas": 997642655586,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Submission",
     "callback": "getName",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 21539,
     "sum_ok_gas": 43078000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    }
   ]
  },
  "v2_raw_run": {
   "label": "V2",
   "seconds": 528.692,
   "n_sims": 2000,
   "error": null,
   "valid": false,
   "submission_errors": {
    "oog": 54,
    "revert": 0,
    "other_halt": 0,
    "tx_error": 0,
    "bad_return": 0
   },
   "max_ok_gas_afterSwap": 243713,
   "max_ok_gas_all": 243713,
   "stats": [
    {
     "role": "Baseline",
     "callback": "afterInitialize",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 21796,
     "sum_ok_gas": 43592000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Baseline",
     "callback": "afterSwap",
     "calls": 21489744,
     "ok": 21489744,
     "max_ok_gas": 22592,
     "sum_ok_gas": 484872352668,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Baseline",
     "callback": "getName",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 21526,
     "sum_ok_gas": 43052000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Submission",
     "callback": "afterInitialize",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 236377,
     "sum_ok_gas": 472754000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Submission",
     "callback": "afterSwap",
     "calls": 20680354,
     "ok": 20680300,
     "max_ok_gas": 243713,
     "sum_ok_gas": 2304043998742,
     "oog": 54,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    },
    {
     "role": "Submission",
     "callback": "getName",
     "calls": 2000,
     "ok": 2000,
     "max_ok_gas": 21545,
     "sum_ok_gas": 43090000,
     "oog": 0,
     "revert": 0,
     "other_halt": 0,
     "tx_error": 0,
     "bad_return": 0
    }
   ]
  }
 },
 "reproduction": {
  "setup_artifact": "https://api.imd.fun/artifacts/02c134d9ed51858f06d02b6dec25e6922016490f11e3a8174af512a6a77b6569",
  "candidate_artifact": "https://api.imd.fun/artifacts/045a5e942a564d4acdd31950593749af412466c39ac11a9336262560d5e1cd70",
  "experiment_py": "import sys,json,inspect,textwrap,hashlib,time\nfrom pathlib import Path\nimport harness as h\nfrom amm_competition.competition.match import MatchRunner\n# Execute the pinned config builder verbatim except iterating only requested seeds.\ndef configs(s0,s1):\n runner=MatchRunner(n_simulations=s1,config=h.build_base_config(seed=None),n_workers=8,variance=h.BASELINE_VARIANCE)\n source=textwrap.dedent(inspect.getsource(MatchRunner._build_configs)).replace('range(self.n_simulations)','range(s0, s1)')\n ns=dict(vars(sys.modules[MatchRunner.__module__]));ns.update(s0=s0,s1=s1)\n exec(source,ns)\n cfg=ns['_build_configs'](runner)\n assert [c.seed for c in cfg]==list(range(s0,s1))\n return cfg\n\ndef audit(r):\n sub=[s for s in r['stats'] if s['role']=='Submission'];sw=[s for s in sub if s['callback']=='afterSwap'];ini=[s for s in sub if s['callback']=='afterInitialize']\n return dict(max_ok=max((s['max_ok_gas'] for s in sw),default=0),oog=sum(s['oog'] for s in sub),reverts=sum(s['revert'] for s in sub),other_halts=sum(s['other_halt']+s['tx_error']+s['bad_return'] for s in sub),init_max=max((s['max_ok_gas'] for s in ini),default=0))\ndef main():\n stage,s0,s1,*ids=sys.argv[1:];s0=int(s0);s1=int(s1);cfg=configs(s0,s1)\n vanilla=list(h.get_vanilla_bytecode_and_abi()[0])\n _,vbc,_=h.compile_src('scratch/V2.sol');vr,ve=h.run('V2',vbc,vanilla,cfg)\n Path(f'scratch/{stage}-v2.json').write_text(json.dumps(dict(run=vr,edges=ve,gas=audit(vr))))\n print('V2',stage,audit(vr),flush=True)\n for id in ids:\n  src,bc,ct=h.compile_src(f'scratch/{id}.sol');r,e=h.run(id,bc,vanilla,cfg)\n  g=audit(r);vg=audit(vr)\n  p=h.paired(e,ve,range(s0,s1)) if len(e)==len(ve)==s1-s0 else dict(n=0)\n  valid=r['error'] is None and vr['error'] is None and p['n']==s1-s0 and g['oog']<=vg['oog'] and g['reverts']==0 and g['other_halts']==0 and g['max_ok']<vg['max_ok']\n  out=dict(id=id,sha256=hashlib.sha256(src.encode()).hexdigest(),paired=p,gas=g,v2_gas=vg,valid=valid,seeds=[s0,s1],run=r,v2_run=vr,compile_seconds=ct)\n  Path(f'scratch/{stage}-{id}.json').write_text(json.dumps(out))\n  print(id,valid,p,g,flush=True)\nif __name__=='__main__':main()\n",
  "generator_py": "import json,re,hashlib\nfrom pathlib import Path\nx=json.load(open('../candidate-artifact.json'));s=x['code']\n# Remove estimators unused by the replacement mapping.\ns=s.replace('uint256 dirState = slots[3]; uint256 actEma = slots[4]; uint256 pHat = slots[5];','uint256 pHat = slots[5];')\ns=s.replace('uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8];','uint256 sigmaHat = slots[6];')\ns=s.replace('uint256 toxEma = slots[9]; uint256 stepTradeCount = slots[10];','uint256 stepTradeCount = slots[10];')\na=s.index('            dirState = _decayCentered');b=s.index('            uncertainty +=',a);s=s[:a]+s[b:]\na=s.index('        uint256 tradeRatio =');b=s.index('        stepTradeCount = stepTradeCount + 1;',a);s=s[:a]+s[b:]\ns=s.replace('slots[3] = dirState; slots[4] = actEma; slots[5] = pHat;','slots[5] = pHat;').replace('slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat;','slots[6] = sigmaHat;').replace('slots[9] = toxEma; slots[10] = stepTradeCount;','slots[10] = stepTradeCount;')\na=s.index('        slots[0] = BASE_FEE;');b=s.index('        return (BASE_FEE, BASE_FEE);',a)\ns=s[:a]+'''        slots[0] = 30 * BPS; slots[1] = 30 * BPS;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000;\n        slots[13] = 902500000000; slots[14] = slots[5];\n'''+s[b:];s=s.replace('return (BASE_FEE, BASE_FEE);','return (30 * BPS, 30 * BPS);')\n# Pack last timestamp (low 64 bits) and last spot (high bits) in one occupied slot.\ns=s.replace('uint256 lastTs = slots[2];','uint256 packed = slots[2]; uint256 lastTs = uint64(packed);').replace('uint256 s = slots[16];','uint256 s = packed >> 64;').replace('slots[2] = trade.timestamp;','slots[2] = (spot << 64) | trade.timestamp;').replace('        slots[16] = spot;','')\ns=s.replace('sqrt(obsVar * WAD)','_sqrtFloor(obsVar * WAD)')\na=s.index('    function _compressTailWithSlope');b=s.index('    function _powWad',a);s=s[:a]+s[b:]\na=s.index('    function _decayCentered');b=s.index('    function getName',a);s=s[:a]+'''    function _sqrtFloor(uint256 x) internal pure returns (uint256 z) {\n        if (x == 0) return 0;\n        uint256 a = x; uint256 bits;\n        if (a >> 128 > 0) { a >>= 128; bits += 128; }\n        if (a >> 64 > 0) { a >>= 64; bits += 64; }\n        if (a >> 32 > 0) { a >>= 32; bits += 32; }\n        if (a >> 16 > 0) { a >>= 16; bits += 16; }\n        if (a >> 8 > 0) { a >>= 8; bits += 8; }\n        if (a >> 4 > 0) { a >>= 4; bits += 4; }\n        if (a >> 2 > 0) { a >>= 2; bits += 2; }\n        if (a >> 1 > 0) { bits += 1; }\n        z = uint256(1) << ((bits + 2) / 2);\n        while (true) { uint256 y = (z + x / z) / 2; if (y >= z) return z; z = y; }\n    }\n\n'''+s[b:]\n# Nine gap columns instead of 21; preserve prior offline optimized table via linear sampling.\nold=x['tuning']['parameters']['fees_bps']\nparams=[]\nfor floor,center,slope,cap in [(20,30,1,230),(24,30,1,230),(28,30,1,230),(20,33,1,230),(24,33,1,230),(28,33,1,230),(24,36,1,230),(28,36,1,230),(28,32,0.25,36),(28,34,0.25,36),(28,36,0.25,36),(28,33,0,36),(24,33,0.5,160),(28,33,0.5,160),(24,33,1.5,230),(28,33,1.5,230)]:\n params.append(dict(floor=floor,center=center,slope=slope,cap=cap))\nfor i,p in enumerate(params):\n vals=[]\n for row in old:\n  for g in range(-200,201,50):\n   q=(g+200)/20; k=min(int(q),19);v=row[k]*(1-(q-k))+row[k+1]*(q-k)\n   vals.append(round(max(p['floor'],min(p['cap'],p['center']+p['slope']*(v-row[10])))))\n hx=b''.join(v.to_bytes(2,'big') for v in vals).hex();assert len(bytes.fromhex(hx))==72\n ns=s.replace('2 * (21 * r + c)','2 * (9 * r + c)')\n ns=re.sub(r'bytes memory t = hex\"[0-9a-f]+\";',f'bytes memory t = hex\"{hx}\";',ns)\n ns=ns.replace('x / 20','x / 50').replace('x % 20','x % 50').replace('(20-dx)','(50-dx)').replace('*BPS/200','*BPS/500')\n ns=ns.replace('\"routing_table\"',f'\"routing_R{i}\"')\n Path(f'scratch/R{i}.sol').write_text(ns)\n p['table']= [vals[j:j+9] for j in range(0,36,9)]\nPath('scratch/variants.json').write_text(json.dumps(params))\nprint('Generated',len(params),'variants')\n",
  "commands": [
   "Clone pinned commit, materialize supplied source_files and scratch/V2.sol.",
   "sh scratch/setup.sh",
   ". scratch/env.sh && git apply scratch/instrumentation.patch",
   ". scratch/env.sh && maturin develop --release --uv --manifest-path amm_sim_rs/Cargo.toml",
   "The generator reads the candidate artifact from ../candidate-artifact.json. Save embedded generator_py to scratch/generate.py and experiment_py to scratch/experiment.py.",
   ". scratch/env.sh && python scratch/generate.py",
   ". scratch/env.sh && python scratch/experiment.py screen 60000 60256 R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 R14 R15",
   ". scratch/env.sh && python scratch/experiment.py select 62000 62512 R0 R1",
   ". scratch/env.sh && python scratch/experiment.py holdout 4090000 4092000 R0",
   ". scratch/env.sh && amm-match validate scratch/R0.sol"
  ]
 },
 "notes": [
  "Nothing was fabricated. All numerical performance and gas values are measured locally, not taken from prior pilot measurements.",
  "Setup, screening, selection, confirmation and validation completed for the 16 C0-derived candidates. Planned C1-C5 source code was not supplied; their outcomes are not claimed.",
  "Do not promote the candidate when paired mean is negative; retain V2."
 ]
}
