{"blocked":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[15] = 400000000000000000 | (uint256(2000000000000000) << 160); 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            // Process variance is propagated inside the censored update.\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        // Censored assumed-density posterior. No external or future inputs.\n        uint256 arbProbability;\n        (fastHat, uncertainty, arbProbability) = _posterior(PosteriorInput(\n            fastHat, uncertainty, pImplied, spot, slots[2] >> 64,\n            prevBidFee, prevAskFee, firstInStep, silent, trade.isBuy,\n            trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0\n        ));\n        // Packed streak: bit 0 direction (AMM buys X); bits 1.. count.\n        // Only first prints update it; slots[2] already supplies the timestamp.\n        if (firstInStep) {\n            uint256 prior = slots[16];\n            uint256 count = prior >> 1;\n            bool same = (prior & 1 == 1) == trade.isBuy;\n            if (arbProbability > WAD / 2) {\n                count = same && silent <= 2 ? count + 1 : 1;\n                // Saturation preserves the predicate count > n forever.\n                if (count > 7) count = 7;\n                slots[16] = (count << 1) | (trade.isBuy ? 1 : 0);\n            } else {\n                slots[16] = 0;\n            }\n        }\n        slots[13] = uncertainty;\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 originalLambda = lambdaHat; uint256 originalSize = sizeHat;\n\n        if (342406799193418304 > 0) {\n            uint256 packedFlow = slots[15];\n            uint256 rate = uint80(packedFlow);\n            uint256 count = uint80(packedFlow >> 80);\n            uint256 meanSize = uint80(packedFlow >> 160);\n            if (rate == 0) rate = 400000000000000000;\n            if (meanSize == 0) meanSize = 2000000000000000;\n            if (isNewStep) {\n                uint256 inst = count / silent;\n                if (inst > 3 * WAD) inst = 3 * WAD;\n                uint256 retention = _powWad(999000000000000000, silent > 64 ? 64 : silent);\n                rate = wmul(rate, retention) + wmul(inst, WAD-retention);\n                count = 0;\n            }\n            uint256 weight = firstInStep ? WAD-arbProbability : WAD;\n            uint256 correctedWeight = weight;\n            if (2 == 2) {\n                uint256 missing = wmul(feeUsed, 196030880396139429888);\n                if (missing > 333333333333333376) missing = 333333333333333376;\n                correctedWeight = wdiv(weight, WAD-missing);\n            }\n            count += correctedWeight;\n            uint256 learning = wmul(WAD-999000000000000000, weight);\n            meanSize = _blend(meanSize, tradeRatio, learning);\n            slots[15]=rate | (count << 80) | (meanSize << 160);\n            uint256 retailRate = wmul(rate, 366383424913407168);\n            uint256 retailSize = 2 == 0 ? sizeHat : wmul(meanSize,1883296158119298304);\n            lambdaHat = _blend(lambdaHat,retailRate,342406799193418304);\n            sizeHat = _blend(sizeHat,retailSize,342406799193418304);\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        lambdaHat = originalLambda; sizeHat = originalSize;\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        if (firstInStep) {\n            if (askFee > bidFee && bidFee > 30 * BPS) bidFee = 30 * BPS;\n            else if (bidFee > askFee && askFee > 30 * BPS) askFee = 30 * BPS;\n        }\n\n        if (bidFee > askFee && askFee > 36 * BPS) askFee = 36 * BPS;\n        else if (askFee > bidFee && bidFee > 36 * BPS) bidFee = 36 * BPS;\n\n        // Apply to final RS2 quotes, retaining the normal fee bounds.\n        {\n            uint256 streak = slots[16];\n            if ((streak >> 1) > 6) {\n                if (streak & 1 == 1) {\n                    bidFee = clampFee(bidFee + 200000000000000);\n                    askFee = askFee > 50000000000000 ? askFee - 50000000000000 : 0;\n                } else {\n                    askFee = clampFee(askFee + 200000000000000);\n                    bidFee = bidFee > 50000000000000 ? bidFee - 50000000000000 : 0;\n                }\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\n    struct PosteriorInput {\n        uint256 mu; uint256 v; uint256 obs; uint256 spot; uint256 preSpot;\n        uint256 bid; uint256 ask; bool first; uint256 silent; bool buy; uint256 size;\n    }\n    function _posterior(PosteriorInput memory x) internal pure returns (uint256, uint256, uint256) {\n        if (x.mu == 0) x.mu = x.spot;\n        if (x.v < 10000000000) x.v = 10000000000;\n        uint256 lo = wmul(x.preSpot, WAD-x.bid);\n        uint256 hi = wdiv(x.preSpot, WAD-x.ask);\n        if (x.first) {\n            if (x.silent>1 && x.preSpot>0) {\n                uint256 elapsed=x.silent>8?8:x.silent;\n                x.v += 902500000000*(elapsed-1);\n                (x.mu,x.v)=_band(x.mu,x.v,lo,hi);\n            }\n            x.v+=902500000000;\n        }\n        uint256 mRetail = x.mu;\n        uint256 vRetail = x.v;\n        // A missing own-pool trade is a censored price observation.\n        if (x.first && x.preSpot > 0) {\n            (mRetail, vRetail) = _band(x.mu, x.v, lo, hi);\n        }\n        if (!x.first) {\n            // A later print is retail. Only an interior route is an observation\n            // of the other pool, with explicit observation uncertainty.\n            uint256 fee = x.buy ? x.bid : x.ask;\n            if (fee > 30 * BPS) {\n                uint256 normal = x.buy ? wdiv(wmul(x.spot, WAD-fee), WAD-30*BPS)\n                    : wdiv(wmul(x.spot, WAD-30*BPS), WAD-fee);\n                uint256 residual = wdiv(absDiff(normal, x.mu),x.mu);\n                uint256 noise = 9000000000000 + wmul(residual,residual);\n                uint256 k = wdiv(x.v,x.v+noise);\n                return (_blend(x.mu,normal,k),wmul(x.v,WAD-k),0);\n            }\n            return (x.mu,x.v,0);\n        }\n        uint256 e = wdiv(absDiff(x.obs,x.mu),x.mu);\n        uint256 totalV = x.v + 22500000000;\n        uint256 z = wdiv(wmul(e,e),totalV);\n        uint256 density = _powWad(wdiv(WAD,WAD+z/8),4);\n        uint256 odds = wmul(density,wdiv(4000000000000000,_isqrt(totalV*WAD)));\n        if (x.size > 3000000000000000) odds = wmul(odds,4*WAD/10);\n        if (x.silent > 1) odds = wmul(odds,8*WAD/10);\n        uint256 sd = _isqrt(x.v*WAD);\n        uint256 width = wdiv(hi-lo,x.mu);\n        uint256 distance = x.mu<lo?lo-x.mu:(x.mu>hi?x.mu-hi:0);\n        uint256 zBand = wdiv(wdiv(distance,x.mu),sd);\n        uint256 mass = wmul(wdiv(width,width+2*sd),wdiv(WAD,WAD+wmul(zBand,zBand)));\n        if (mass < WAD/100) mass = WAD/100;\n        odds = wdiv(odds,mass);\n        uint256 prob = wdiv(odds,WAD+odds);\n        uint256 kArb = wdiv(x.v,totalV);\n        uint256 mArb = _blend(x.mu,x.obs,kArb);\n        uint256 mean = _blend(mRetail,mArb,prob);\n        uint256 between = wdiv(absDiff(mArb,mRetail),x.mu);\n        uint256 variance = wmul(prob,wmul(x.v,WAD-kArb)) + wmul(WAD-prob,vRetail)\n            + wmul(wmul(prob,WAD-prob),wmul(between,between));\n        if (variance > 100000000000000) variance = 100000000000000;\n        return (mean,variance,prob);\n    }\n    function _blend(uint256 a,uint256 b,uint256 w) internal pure returns(uint256) {\n        return b>=a ? a+wmul(b-a,w) : a-wmul(a-b,w);\n    }\n    function _band(uint256 mu,uint256 v,uint256 lo,uint256 hi) internal pure returns(uint256,uint256) {\n        // Soft interval likelihood avoids discontinuities at quadrature nodes.\n        uint256 sd=wmul(mu,_isqrt(v*WAD));\n        uint256[3] memory acc;\n        for(uint256 j=0;j<7;j++) {\n            uint256 p=j<3 ? (mu>(3-j)*sd?mu-(3-j)*sd:1) : mu+(j-3)*sd;\n            uint256 w=j==3?72:(j==2||j==4?45:(j==1||j==5?10:1));\n            uint256 distance=p<lo?lo-p:(p>hi?p-hi:0);\n            uint256 z=sd>0?wdiv(distance,sd):0;\n            uint256 likelihood=wdiv(WAD,WAD+4*wmul(z,z));\n            w=wmul(w*WAD,likelihood);\n            acc[0]+=w;acc[1]+=wmul(w,p);\n            uint256 d=wdiv(absDiff(p,mu),mu);\n            acc[2]+=wmul(w,wmul(d,d));\n        }\n        uint256 m=wdiv(acc[1],acc[0]);\n        uint256 d=wdiv(absDiff(m,mu),mu);\n        uint256 second=wdiv(acc[2],acc[0]);\n        return(m,second>wmul(d,d)?second-wmul(d,d):10000000000);\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 \"censored_10\"; }\n}\n","sha256":"03f2991d797ea6a38bff79dbe67625866dd536eb4015903062bc1739b765f0bf","params":{"n":6,"b":2,"r":0.25,"G":2},"mechanism":"On each first print, RS2 posterior p > 0.5 extends the same-direction arbitrage streak in slot 16; a retail first print clears it, while a direction change or gap above G starts a new streak at one. While count > n, the final RS2 fee on that direction gains b basis points and the opposite fee loses r*b basis points, with fee bounds preserved and no extra initialization write.","source":{"url":"https://benedict.dev/optimization-arena-learnings","quote":"fee += SURGE_BUMP * (arbCou","rendered_fragment":"if (arbCount > 3) fee += SURGE_BUMP * (arbCou","note":"The source is a truncated code fragment in the Distilling Insights from Optimized Code diagram; it supplies no complete formula or evidence that this rule improves RS2."},"baseline":{"name":"RS2","url":"https://api.imdnotdead.fun/artifacts/19f2ec857df536e8ab62ab1da89ec6623332521ce46849f15bccff402c63e1e5","sha256":"f24b6ca198b744c380842c3a17964a6dc21f311bb04984b26733afa3fee8eaf2"},"generator_source":"def generate(base, params):\n    n, b, r, G = params\n    assert int(n) == n and 1 <= n <= 6\n    assert 0 <= b <= 10 and 0 <= r <= 1\n    assert int(G) == G and 1 <= G <= 8\n    bump = int(round(b * 10**14))\n    rebate = int(round(b * r * 10**14))\n    # Compile-time neutral: exact original source and no additional storage cost.\n    if bump == 0:\n        return base\n    marker = '        slots[13] = uncertainty;'\n    update = f'''        // Packed streak: bit 0 direction (AMM buys X); bits 1.. count.\n        // Only first prints update it; slots[2] already supplies the timestamp.\n        if (firstInStep) {{\n            uint256 prior = slots[16];\n            uint256 count = prior >> 1;\n            bool same = (prior & 1 == 1) == trade.isBuy;\n            if (arbProbability > WAD / 2) {{\n                count = same && silent <= {int(G)} ? count + 1 : 1;\n                // Saturation preserves the predicate count > n forever.\n                if (count > {int(n)+1}) count = {int(n)+1};\n                slots[16] = (count << 1) | (trade.isBuy ? 1 : 0);\n            }} else {{\n                slots[16] = 0;\n            }}\n        }}\n'''\n    assert base.count(marker) == 1\n    s = base.replace(marker, update + marker)\n    marker = '        slots[0] = bidFee; slots[1] = askFee; slots[2] = (spot << 64) | trade.timestamp;'\n    adjust = f'''        // Apply to final RS2 quotes, retaining the normal fee bounds.\n        {{\n            uint256 streak = slots[16];\n            if ((streak >> 1) > {int(n)}) {{\n                if (streak & 1 == 1) {{\n                    bidFee = clampFee(bidFee + {bump});\n                    askFee = askFee > {rebate} ? askFee - {rebate} : 0;\n                }} else {{\n                    askFee = clampFee(askFee + {bump});\n                    bidFee = bidFee > {rebate} ? bidFee - {rebate} : 0;\n                }}\n            }}\n        }}\n'''\n    assert s.count(marker) == 1\n    return s.replace(marker, adjust + marker)\n\n\ndef recover_rs2_source(frozen_source):\n    \"\"\"Recover the exact supplied base from a generated active source.\n\n    Offline usage with result.json: base = recover_rs2_source(result['code']);\n    source = generate(base, [n, b_bps, r, G]).\n    \"\"\"\n    s = frozen_source\n    for begin, end in [\n        ('        // Packed streak:', '        slots[13] = uncertainty;'),\n        ('        // Apply to final RS2 quotes',\n         '        slots[0] = bidFee; slots[1] = askFee; slots[2] = (spot << 64) | trade.timestamp;'),\n    ]:\n        if begin in s:\n            i = s.index(begin)\n            j = s.index(end, i)\n            s = s[:i] + s[j:]\n    return s\n","search":{"method":"seed-90 random-order grid, integer n=1..6, b=1..10 bps, r in [0,.25,.5,.75,1], G=1..8; b=0 neutral control included separately","seed":90,"evaluations":44,"active_evaluations":43,"elapsed_seconds":1502.6099892110215,"budget_seconds":1500,"training_seeds":[656179,656242],"block_size":64,"block_policy":"A common randomly selected block within 656100..658999 was reused across candidates for comparable ranking. Search ends after completing the evaluation that crosses 25 minutes; baseline setup is outside the timer.","best":{"params":[5,2,0.5,6],"paired":{"n":64,"mean_diff":0.03829263099596947,"se":0.037042195057832905,"worst":[{"seed":656200,"d":-0.42167501961932885},{"seed":656193,"d":-0.4161480819514054},{"seed":656239,"d":-0.4044005600003402},{"seed":656205,"d":-0.3931142362505966},{"seed":656195,"d":-0.371304389781244}]},"retail_edge_delta":{"mean":-0.24791444133073526,"se":0.04002041335868048},"arb_edge_delta":{"mean":0.28620707232670334,"se":0.015290459139308505},"reconciliation_max_abs":8.171241461241152e-14,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0},"valid":true,"elapsed_seconds":1204.0540148179862,"sha256":"3ae127fc0192f86f1ac33cd0b6e2da1895fa0c741b6c868d4ce158c8385f8b03"},"log":[{"params":[3,0,0.5,4],"mean_diff":0.0,"se":0.0,"valid":true,"gas":{"max_ok":197172,"init_max_ok":243099,"oog":0,"halts":0}},{"params":[4,10,0.0,3],"mean_diff":-0.7517868125650953,"se":0.09375693350610521,"valid":true,"gas":{"max_ok":219692,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,1,1.0,5],"mean_diff":-0.04032500208398471,"se":0.026058581147663882,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[4,7,0.25,8],"mean_diff":-0.43004140792979406,"se":0.07791355856016448,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,5,0.75,4],"mean_diff":-0.1073545604215198,"se":0.05809614558683878,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[1,2,0.75,3],"mean_diff":-0.2164139445725315,"se":0.05988173588584651,"valid":true,"gas":{"max_ok":219678,"init_max_ok":243075,"oog":0,"halts":0}},{"params":[1,1,0.25,1],"mean_diff":-0.11492792562005683,"se":0.03612129113732739,"valid":true,"gas":{"max_ok":219678,"init_max_ok":243075,"oog":0,"halts":0}},{"params":[5,2,0.25,8],"mean_diff":0.03627840023740436,"se":0.03744344876244392,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,4,0.5,4],"mean_diff":-0.207576078145995,"se":0.07196625731178044,"valid":true,"gas":{"max_ok":219702,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,9,0.25,4],"mean_diff":-0.4171052129584121,"se":0.083099463592598,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[4,6,1.0,5],"mean_diff":-0.34185031925705367,"se":0.07260126888482116,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,3,0.0,7],"mean_diff":0.022664389263346685,"se":0.03749095573384591,"valid":true,"gas":{"max_ok":219697,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[1,5,0.0,2],"mean_diff":-0.9107895279779603,"se":0.10101818154557222,"valid":true,"gas":{"max_ok":219668,"init_max_ok":243075,"oog":0,"halts":0}},{"params":[3,10,1.0,7],"mean_diff":-1.2801046899389572,"se":0.10722688934534824,"valid":true,"gas":{"max_ok":219685,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,10,0.5,4],"mean_diff":-0.4878907629942386,"se":0.08975959021886935,"valid":true,"gas":{"max_ok":219685,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[4,7,1.0,6],"mean_diff":-0.4713777434621047,"se":0.07731052224357288,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,3,0.75,3],"mean_diff":0.005848368996881348,"se":0.043144147953355395,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,10,0.0,5],"mean_diff":-0.4378763552814604,"se":0.0729552470323113,"valid":true,"gas":{"max_ok":219692,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[1,2,0.75,6],"mean_diff":-0.2267503263583146,"se":0.06091521179094269,"valid":true,"gas":{"max_ok":219678,"init_max_ok":243075,"oog":0,"halts":0}},{"params":[5,5,0.25,7],"mean_diff":-0.09060463500263083,"se":0.06061919459099852,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[1,7,0.5,5],"mean_diff":-1.5673834659671897,"se":0.13051046795778098,"valid":true,"gas":{"max_ok":219678,"init_max_ok":243075,"oog":0,"halts":0}},{"params":[1,10,0.5,7],"mean_diff":-2.5760700817097906,"se":0.15025227914388348,"valid":true,"gas":{"max_ok":219680,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,8,0.5,3],"mean_diff":-0.29203641067227615,"se":0.07602570214592447,"valid":true,"gas":{"max_ok":219702,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,10,1.0,2],"mean_diff":-0.3129131315609266,"se":0.06395112421448328,"valid":true,"gas":{"max_ok":219685,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,7,0.75,3],"mean_diff":-0.5656398565581426,"se":0.09504640033980402,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,8,1.0,4],"mean_diff":-0.34116588971931794,"se":0.07901337437683492,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,5,0.5,5],"mean_diff":-0.3056108012803431,"se":0.07234607679990636,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[1,2,1.0,6],"mean_diff":-0.2792163050914125,"se":0.05993223111577568,"valid":true,"gas":{"max_ok":219681,"init_max_ok":243075,"oog":0,"halts":0}},{"params":[3,4,0.25,2],"mean_diff":-0.16858967097040978,"se":0.06684617907868809,"valid":true,"gas":{"max_ok":219702,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,8,0.0,8],"mean_diff":-0.8196480812914704,"se":0.09939640875156995,"valid":true,"gas":{"max_ok":219697,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,6,1.0,2],"mean_diff":-0.07536759968981954,"se":0.05107533459380152,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,10,0.5,3],"mean_diff":-0.40867589777930213,"se":0.06778924339832941,"valid":true,"gas":{"max_ok":219685,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,9,0.75,3],"mean_diff":-0.35011743814569485,"se":0.06861036447331918,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,8,0.75,6],"mean_diff":-0.2804606544246191,"se":0.06910990747378308,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,2,0.5,6],"mean_diff":0.03829263099596947,"se":0.037042195057832905,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,7,0.75,7],"mean_diff":-0.1894350691917479,"se":0.05859573585178405,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[2,4,0.25,2],"mean_diff":-0.370245545815159,"se":0.06806273698843907,"valid":true,"gas":{"max_ok":219676,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,9,0.5,3],"mean_diff":-0.9851402673667087,"se":0.0980361796657393,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,3,0.25,2],"mean_diff":-0.10639779780257275,"se":0.05403138629572003,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,4,0.25,2],"mean_diff":0.0070449147180848826,"se":0.04267063022781322,"valid":true,"gas":{"max_ok":219702,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[6,2,0.25,2],"mean_diff":0.03512381736996506,"se":0.031082678230316976,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[2,2,0.75,3],"mean_diff":-0.09432727870323543,"se":0.04978287185085155,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[5,9,0.5,2],"mean_diff":-0.3601913658144493,"se":0.07287312978727706,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}},{"params":[3,7,0.25,8],"mean_diff":-0.632104460244002,"se":0.09019400059164528,"valid":true,"gas":{"max_ok":219690,"init_max_ok":243084,"oog":0,"halts":0}}],"rejected":0,"recheck_seeds":[659000,659255],"recheck":[{"params":[5,2,0.5,6],"paired":{"n":256,"mean_diff":-0.04863751917195791,"se":0.016524907230198983,"worst":[{"seed":659239,"d":-1.561662937137612},{"seed":659115,"d":-0.8056320984146623},{"seed":659102,"d":-0.6601399835709572},{"seed":659158,"d":-0.656949603997873},{"seed":659232,"d":-0.5337014775234366}]},"retail_edge_delta":{"mean":-0.33992212371363073,"se":0.0156416376526564},"arb_edge_delta":{"mean":0.2912846045416714,"se":0.006964286037270698},"reconciliation_max_abs":1.1368683772161603e-13,"gas":{"max_ok":219847,"init_max_ok":243084,"oog":0,"halts":0},"valid":true,"training":{"n":64,"mean_diff":0.03829263099596947,"se":0.037042195057832905,"worst":[{"seed":656200,"d":-0.42167501961932885},{"seed":656193,"d":-0.4161480819514054},{"seed":656239,"d":-0.4044005600003402},{"seed":656205,"d":-0.3931142362505966},{"seed":656195,"d":-0.371304389781244}]}},{"params":[5,2,0.25,8],"paired":{"n":256,"mean_diff":-0.049285666847256104,"se":0.016582439512760644,"worst":[{"seed":659239,"d":-1.591819012012479},{"seed":659115,"d":-0.8004374372160328},{"seed":659102,"d":-0.7344920017357026},{"seed":659158,"d":-0.6430409945213569},{"seed":659127,"d":-0.5543825365728026}]},"retail_edge_delta":{"mean":-0.3431796437222179,"se":0.015560562426398288},"arb_edge_delta":{"mean":0.29389397687496127,"se":0.006971924058216211},"reconciliation_max_abs":1.1368683772161603e-13,"gas":{"max_ok":219847,"init_max_ok":243084,"oog":0,"halts":0},"valid":true,"training":{"n":64,"mean_diff":0.03627840023740436,"se":0.03744344876244392,"worst":[{"seed":656200,"d":-0.42225648352439293},{"seed":656193,"d":-0.412554508926803},{"seed":656195,"d":-0.4082795318272474},{"seed":656239,"d":-0.4029734489947714},{"seed":656227,"d":-0.37103939502020467}]}},{"params":[6,2,0.25,2],"paired":{"n":256,"mean_diff":-0.023129404421867505,"se":0.01175490378848943,"worst":[{"seed":659232,"d":-0.5959642206919398},{"seed":659118,"d":-0.5902955901973428},{"seed":659210,"d":-0.5166479959017352},{"seed":659036,"d":-0.5159923307729173},{"seed":659102,"d":-0.47871587992926834}]},"retail_edge_delta":{"mean":-0.22314860053030694,"se":0.011618777967067815},"arb_edge_delta":{"mean":0.20001919610843932,"se":0.005054147494220728},"reconciliation_max_abs":1.1368683772161603e-13,"gas":{"max_ok":219847,"init_max_ok":243084,"oog":0,"halts":0},"valid":true,"training":{"n":64,"mean_diff":0.03512381736996506,"se":0.031082678230316976,"worst":[{"seed":656227,"d":-0.3688406234495005},{"seed":656238,"d":-0.29261009058149057},{"seed":656200,"d":-0.27163721542598296},{"seed":656206,"d":-0.2637234781203688},{"seed":656193,"d":-0.25266182250840075}]}}],"freeze":{"params":[6,2,0.25,2],"sha256":"03f2991d797ea6a38bff79dbe67625866dd536eb4015903062bc1739b765f0bf","reason":"highest recheck paired mean among 3 valid active settings","timestamp_utc":"2026-11-09T03:42:00Z"},"selection_note":"Highest recheck mean among the best three valid active settings. Neutral remains the deployment fallback; a measured active loss is still reported rather than replaced with a zero-difference control."},"held_out":{"baseline":"RS2","seeds":[659400,659911],"n":512,"mean_diff":-0.03720859638420837,"se":0.011712129572383543,"worst":[{"seed":659623,"d":-3.161472518950518},{"seed":659436,"d":-1.6338733415856268},{"seed":659781,"d":-1.1046336007735817},{"seed":659446,"d":-0.9283530947300846},{"seed":659518,"d":-0.8819510202640686}],"valid":true,"candidate_mean_edge":532.2185664093685,"baseline_mean_edge":532.2557750057526,"reconciliation_max_abs":1.1013412404281553e-13},"retail_edge_delta":{"mean":-0.23310598543837235,"se":0.010803716632656073},"arb_edge_delta":{"mean":0.19589738905416243,"se":0.004627327553843282},"gas":{"max_ok":225494,"init_max_ok":243084,"oog":0,"halts":{"revert":0,"other_halt":0,"tx_error":0,"bad_return":0},"scope":"Frozen candidate, search plus recheck plus held-out plus fresh STACK confirmation","afterSwap_reject_at":235000,"afterInitialize_reject_at":245000,"held_candidate":{"max_ok":225482,"init_max_ok":243084,"oog":0,"halts":0},"held_rs2":{"max_ok":203010,"init_max_ok":243099,"oog":0,"halts":0}},"validation":{"neutral_seeds":[659950,659981],"neutral_n":32,"neutral_rows_equal":true,"neutral_mean_diff":0.0,"CH_seeds":[0,999],"CH_mean_edge":525.5258821780834,"CH_expected_mean_edge":525.5258821780834,"search_source_hashes_reproduced":43,"CH_match":true,"state_machine_tests":"Passed actual compiled EVM tests of p=0.5 threshold, activation at n+1, saturation, same-step persistence, G boundary, gap reset, direction change, retail reset, both fee directions."},"protocol":{"repository":"https://github.com/benedictbrady/amm-challenge","commit":"6b6921f8d463314aa7c96234e1be25a25245f704","harness_url":"https://api.imdnotdead.fun/artifacts/33f54e81f3372088e9eced52c0174f310ac348af98ef5727b73785c088679194","run":"amm_sim_rs.run_batch(candidate_bytecode, VanillaStrategy_bytecode, configs, 8), each strategy in its own market","gas_stats":"Reset and read for every chunk of at most 20 seeds; reject any oog, revert, other_halt, tx_error or bad_return in either role, and reject candidate gas at the stated limits.","configs":"10000 steps; default_rng(seed): size U(19,21), rate U(.6,1), sigma U(.000882,.001008); initial price 100, X 100, Y 10000, mu 0, dt 1, retail size sigma 1.2, buy probability .5; simulator seed=seed, retail RNG internally seed+1.","edge_decomposition":"Unmodified supplied harness decomposition: negative ideal pre-retail arbitrage profit from preceding spot and quotes; retail=edge-arb. Paired means and sample standard errors (ddof=1); worst five are lowest seedwise total-edge differences."},"notes":["All reported simulation figures are local measurements; no leaderboard submission or top-10 claim.","The slot-16 count saturates at n+1 because only the predicate count>n is needed; direction is bit 0, count occupies higher bits. Existing slots[2] supplies elapsed time. Later prints retain the current streak.","The adjustment is applied after RS2 quote caps, then protected-side fee is clamped to the standard maximum and the opposite-side subtraction floors at zero. Both total adjustment and rebate are constant once the streak is active, rather than scaled by its length.","Neutral b=0 returns RS2 source exactly, avoiding gas or behavioral changes. The new slot is never written by afterInitialize.","Search only sampled one 64-seed training block; the separate recheck and held-out results are the evidence for generalization.","The generator uses no additional dependencies: recover_rs2_source(code) reconstructs the exact baseline, then generate(base,[n,b,r,G]) builds any setting offline.","No new baseline-family constants were tuned. The baseline getName string was retained.","Held-out RS2 comparison is a measured loss; retain RS2."],"stack_confirmation":{"seeds":[5018000,5019999],"baseline":"STACK","baseline_url":"https://api.imdnotdead.fun/artifacts/c33720874aa1285e9dc766540b8a90410118a8e8b840d949ea0dcc211bdb27c1","baseline_sha256":"2eaafc7960e520b8a7fd322ec8afaff8fa677c730228a961efb509cc30b25719","paired":{"n":2000,"mean_diff":0.30648184609557894,"se":0.01396395421965462,"worst":[{"seed":5019916,"d":-1.8502006971542642},{"seed":5019491,"d":-1.8178667244561666},{"seed":5018879,"d":-1.7461383827569534},{"seed":5018856,"d":-1.7417550032117788},{"seed":5019279,"d":-1.7197791071915844}]},"retail_edge_delta":{"mean":0.14088533284283836,"se":0.013977968476983198},"arb_edge_delta":{"mean":0.16559651325274138,"se":0.006590603010975625},"reconciliation_max_abs":2.2737367544323206e-13,"candidate_gas":{"max_ok":225494,"init_max_ok":243084,"oog":0,"halts":{"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}},"baseline_gas":{"max_ok":214029,"init_max_ok":220962,"oog":0,"halts":{"revert":0,"other_halt":0,"tx_error":0,"bad_return":0}},"selection":"Frozen before any fresh confirmation seed was run; not used to change parameters.","valid":true}}