{
 "item": "corridor",
 "batch": "e03-pilot-rerun",
 "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 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\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[2] = 0; slots[3] = WAD; slots[4] = 0;\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]; slots[15] = 0; slots[9] = 0; slots[10] = 0; slots[11] = 0; slots[12] = 0;\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 = slots[2];\n        uint256 pHat = slots[5];\n        uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8];\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            sizeHat = wmul(sizeHat, _powWad(SIZE_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[16];\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 = sqrt(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            sizeHat = wmul(sizeHat, SIZE_BLEND_DECAY) + wmul(tradeRatio, WAD - SIZE_BLEND_DECAY);\n            if (sizeHat > WAD) sizeHat = WAD;\n        }\n\n\n        stepTradeCount = stepTradeCount + 1;\n        if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP;\n\n        // Keep V2's price blend fixed. Widths are relative fair-price uncertainty.\n        uint256 fair = wmul(pHat, 700000000000000000) + wmul(fastHat, 300000000000000000);\n        uint256 flowSize = wmul(lambdaHat, sizeHat);\n        uint256 protect = 4 * BPS + wmul(sigmaHat, 350000000000000000)\n            + wmul(flowSize, 400000000000000000) + wmul(tradeRatio, 200000000000000000);\n        uint256 attract = 24 * BPS + wmul(sigmaHat, 200000000000000000)\n            + wmul(flowSize, 300000000000000000);\n        // Bound each interval width before converting it into a fee.\n        if (protect > 150 * BPS) protect = 150 * BPS;\n        if (attract > 80 * BPS) attract = 80 * BPS;\n        bool high = spot >= fair;\n        uint256 lower = wmul(fair, WAD - (high ? protect : attract));\n        uint256 upper = wmul(fair, WAD + (high ? attract : protect));\n        uint256 bidFee = spot > lower ? wdiv(spot - lower, spot) : 0;\n        uint256 askFee = upper > spot ? wdiv(upper - spot, upper) : 0;\n        // Protective quotes may retreat further than liquidity-attracting quotes.\n        uint256 bidCap = high ? 250 * BPS : 45 * BPS;\n        uint256 askCap = high ? 45 * BPS : 250 * BPS;\n        if (bidFee > bidCap) bidFee = bidCap;\n        if (askFee > askCap) askFee = askCap;\n        if (bidFee < BPS) bidFee = BPS;\n        if (askFee < BPS) askFee = BPS;\n\n        slots[0] = bidFee; slots[1] = askFee; slots[2] = trade.timestamp;\n        slots[5] = pHat;\n        slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat;\n        slots[16] = spot;\n        slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\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    function getName() external pure override returns (string memory) { return \"corridor_p12a24\"; }\n}\n",
 "sha256": "ea92438a8f0cdad2d600ef99336dafa6bb32d95d018a844893729cbcdc91083f",
 "design": "Asymmetric fair-price corridor from build cb935deb (p12a24) with the tox/dir/act updates removed because their values are never read (slots 3,4,9 no longer written; afterSwap max gas 211k, down from 242k). fair = 0.7*pHat + 0.3*fastHat (V2's estimators). Protect width = PB + 0.35*sigmaHat + 0.4*lambda*size + 0.2*tradeRatio on the side away from spot; attract width = AB + 0.2*sigmaHat + 0.3*lambda*size; fee = distance from spot to the corridor edge, capped at 250 bps (protect side) and 45 bps (attract side), with a 1 bps floor. Frozen: PB=4, AB=24 bps.",
 "conclusion": "The corridor fee rule loses heavily to V2 on every variant tried: -53 to -95 per seed on screening and -56 on stage 2. On the holdout it is far below V2 (mean_diff in paired). Do not pursue this rule.",
 "tuning": [
  {
   "id": "p4a12",
   "params": {
    "protect_base_bps": 4,
    "attract_base_bps": 12
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -56.2976,
   "se": 9.512,
   "oog": 0,
   "max_ok_gas": 190625
  },
  {
   "id": "p4a24",
   "params": {
    "protect_base_bps": 4,
    "attract_base_bps": 24
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -53.116,
   "se": 9.5428,
   "oog": 0,
   "max_ok_gas": 190625
  },
  {
   "id": "p4a36",
   "params": {
    "protect_base_bps": 4,
    "attract_base_bps": 36
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -54.9547,
   "se": 9.4767,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p4a48",
   "params": {
    "protect_base_bps": 4,
    "attract_base_bps": 48
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -58.4068,
   "se": 9.4769,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p12a12",
   "params": {
    "protect_base_bps": 12,
    "attract_base_bps": 12
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -70.9564,
   "se": 9.7321,
   "oog": 0,
   "max_ok_gas": 190682
  },
  {
   "id": "p12a24",
   "params": {
    "protect_base_bps": 12,
    "attract_base_bps": 24
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -69.7977,
   "se": 9.5357,
   "oog": 0,
   "max_ok_gas": 190625
  },
  {
   "id": "p12a28",
   "params": {
    "protect_base_bps": 12,
    "attract_base_bps": 28
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -69.678,
   "se": 9.6786,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p12a32",
   "params": {
    "protect_base_bps": 12,
    "attract_base_bps": 32
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -69.5507,
   "se": 9.5902,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p12a36",
   "params": {
    "protect_base_bps": 12,
    "attract_base_bps": 36
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -70.4004,
   "se": 9.6708,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p12a48",
   "params": {
    "protect_base_bps": 12,
    "attract_base_bps": 48
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -71.7785,
   "se": 9.3776,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p24a12",
   "params": {
    "protect_base_bps": 24,
    "attract_base_bps": 12
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -93.5612,
   "se": 10.1045,
   "oog": 0,
   "max_ok_gas": 190625
  },
  {
   "id": "p24a24",
   "params": {
    "protect_base_bps": 24,
    "attract_base_bps": 24
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -93.043,
   "se": 9.8927,
   "oog": 0,
   "max_ok_gas": 190682
  },
  {
   "id": "p24a36",
   "params": {
    "protect_base_bps": 24,
    "attract_base_bps": 36
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -93.8614,
   "se": 9.7009,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p24a48",
   "params": {
    "protect_base_bps": 24,
    "attract_base_bps": 48
   },
   "seeds": "70000..70015",
   "n": 16,
   "mean_diff": -94.6807,
   "se": 9.6452,
   "oog": 0,
   "max_ok_gas": 190801
  },
  {
   "id": "p4a24@stage2",
   "params": {
    "protect_base_bps": 4,
    "attract_base_bps": 24
   },
   "seeds": "72000..72063",
   "n": 64,
   "mean_diff": -56.3048,
   "se": 3.462,
   "oog": 0,
   "max_ok_gas": 211411
  },
  {
   "id": "p4a36@stage2",
   "params": {
    "protect_base_bps": 4,
    "attract_base_bps": 36
   },
   "seeds": "72000..72063",
   "n": 64,
   "mean_diff": -57.3791,
   "se": 3.4382,
   "oog": 0,
   "max_ok_gas": 211411
  }
 ],
 "paired": {
  "n": 200,
  "mean_diff": -49.01417770952255,
  "se": 1.9385450191692153,
  "worst": [
   {
    "seed": 4100042,
    "diff": -296.2064051680701
   },
   {
    "seed": 4100098,
    "diff": -171.24560428038933
   },
   {
    "seed": 4100186,
    "diff": -134.22064948740865
   },
   {
    "seed": 4100155,
    "diff": -121.5005462577517
   },
   {
    "seed": 4100189,
    "diff": -110.22113543495254
   }
  ],
  "seeds": "4100000..4100199"
 },
 "gas": {
  "max_ok": 212870,
  "oog": 0,
  "reverts": 0,
  "other_halts": 0,
  "init_max": 236379
 },
 "v2_gas": {
  "max_ok": 243457,
  "oog": 6,
  "reverts": 0,
  "other_halts": 0,
  "init_max": 236377
 },
 "valid": true,
 "seeds": {
  "screen": "70000..70015 (16, all 14 variants)",
  "stage2": "72000..72063 (64, top 2)",
  "holdout": "4100000..4100199 (200 fresh, never tuned on)"
 },
 "deviations": [
  "On this machine runs did not execute in parallel: wall time equalled CPU time, about 1 s per seed per strategy whether 1 or 15 processes ran. Single processes running about 200 or more seeds were killed with exit 137. The plan was therefore reduced from 256 screen seeds, 512 stage-2 seeds and a 2000-seed holdout to 16, 64 and 200. The holdout is fresh, but the brief's 2000-seed requirement is NOT met.",
  "V2 runs once per seed block and each strategy runs separately against VanillaStrategy in its own run_batch call (n_workers=1). Configs are those of MatchRunner._build_configs, restricted to the needed seed range; edges were checked identical to the full-range build on seeds 70000..70003.",
  "The brief's 'attract bases 28/32' were screened with protect base 12 (p12a28, p12a32).",
  "V2 OOG: 0 on the 16-seed and 64-seed tuning blocks; 6 on the 200-seed holdout (max_ok 243457). The candidate had 0 OOG everywhere."
 ],
 "validate": "amm-match validate passed: Strategy 'corridor_p12a24' validated successfully! (getName still returns the build's original name)",
 "blocked": null
}