{"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        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\n        {\n            int256[10] memory xin;\n            xin[0] = _cl((int256(wdiv(spot, fastHat)) - 1e18) * 200, 2e18);\n            xin[1] = _cl((int256(wdiv(spot, pHat)) - 1e18) * 200, 2e18);\n            xin[2] = int256(arbProbability);\n            xin[3] = _cl(int256(sigmaHat) * 1000 - 1e18, 2e18);\n            xin[4] = _cl(int256(lambdaHat) * 5 / 4 - 1e18, 2e18);\n            xin[5] = _cl(int256(sizeHat) * 250 - 1e18, 2e18);\n            xin[6] = _cl(int256(toxEma) * 1000 / 3 - 1e18, 2e18);\n            xin[7] = _cl(int256(dirState) - 1e18, 1e18);\n            xin[8] = trade.isBuy ? int256(1e18) : -int256(1e18);\n            xin[9] = firstInStep ? int256((silent > 8 ? 8 : silent) * WAD / 8)\n                : int256(WAD + (stepTradeCount > 8 ? 8 : stepTradeCount) * WAD / 8);\n            (int256 d0, int256 d1) = _head(xin);\n            bidFee = clampFee(uint256(int256(bidFee) + d0 > 0 ? int256(bidFee) + d0 : int256(0)));\n            askFee = clampFee(uint256(int256(askFee) + d1 > 0 ? int256(askFee) + d1 : int256(0)));\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    function _cl(int256 v,int256 m) internal pure returns(int256){ return v>m?m:(v<-m?-m:v); }\n    function _head(int256[10] memory x) internal pure returns(int256 o0,int256 o1){\n        int256 h0 = x[0]*(-83826182942922496) + x[1]*(-141574107798150496) + x[2]*(199210202785252608) + x[3]*(453516472057374528) + x[4]*(25609246890955464) + x[5]*(-467671908648113664) + x[6]*(-683199449583809408) + x[7]*(-1397276110137861120) + x[8]*(-499191891404438848) + x[9]*(-289615392447410176);\n        h0 = h0/1e18 + (79072031452611168); if (h0 < 0) h0 = 0; if (h0 > 4e18) h0 = 4e18;\n        int256 h1 = x[0]*(129097019697524400) + x[1]*(10333052728690950) + x[2]*(124087961511777136) + x[3]*(-209286271302109536) + x[4]*(-103420590221359456) + x[5]*(89543864387347008) + x[6]*(36444091307164936) + x[7]*(-197841386735257184) + x[8]*(274602244614618624) + x[9]*(8926083670827499);\n        h1 = h1/1e18 + (106834626341398192); if (h1 < 0) h1 = 0; if (h1 > 4e18) h1 = 4e18;\n        int256 h2 = x[0]*(-369730581916354624) + x[1]*(368760276992010944) + x[2]*(138211848930169696) + x[3]*(405815464227331072) + x[4]*(301663168247075712) + x[5]*(-63348064220875592) + x[6]*(815268633375751808) + x[7]*(628408820548126720) + x[8]*(-19091662458236568) + x[9]*(148429061031574880);\n        h2 = h2/1e18 + (86365856409173056); if (h2 < 0) h2 = 0; if (h2 > 4e18) h2 = 4e18;\n        int256 h3 = x[0]*(-432905181030032512) + x[1]*(-226218462769103456) + x[2]*(-887796879375159) + x[3]*(-97830753680313904) + x[4]*(-309579846944337792) + x[5]*(-679728556932747264) + x[6]*(-277860323928882560) + x[7]*(-46323521832670144) + x[8]*(498563800929354176) + x[9]*(-295781978330124224);\n        h3 = h3/1e18 + (210041800626958432); if (h3 < 0) h3 = 0; if (h3 > 4e18) h3 = 4e18;\n        int256 h4 = x[0]*(31756217950145936) + x[1]*(-718310930349020544) + x[2]*(-608559518939891072) + x[3]*(603834240706179200) + x[4]*(26411767641056008) + x[5]*(-182885233945728256) + x[6]*(-221257026068868448) + x[7]*(-776152108304649088) + x[8]*(664443546308789376) + x[9]*(403301942722026368);\n        h4 = h4/1e18 + (51610440707511704); if (h4 < 0) h4 = 0; if (h4 > 4e18) h4 = 4e18;\n        int256 h5 = x[0]*(184076680931505504) + x[1]*(104551921568122768) + x[2]*(265429165851144448) + x[3]*(159000262475493792) + x[4]*(-182231787898600576) + x[5]*(413876728746700608) + x[6]*(183617527273796224) + x[7]*(-47604387927408848) + x[8]*(-572592724572450496) + x[9]*(-147253394092700480);\n        h5 = h5/1e18 + (276172597428319008); if (h5 < 0) h5 = 0; if (h5 > 4e18) h5 = 4e18;\n        int256 h6 = x[0]*(374674833415296640) + x[1]*(392297146069611520) + x[2]*(1127363977853192320) + x[3]*(96334828532457120) + x[4]*(-458552470160123264) + x[5]*(-107422712891601840) + x[6]*(-294867004678429952) + x[7]*(-125751125922385472) + x[8]*(-329644196745498304) + x[9]*(-33075920459288068);\n        h6 = h6/1e18 + (-50522122458009776); if (h6 < 0) h6 = 0; if (h6 > 4e18) h6 = 4e18;\n        int256 h7 = x[0]*(168660681175323776) + x[1]*(-486310416626885760) + x[2]*(-573024182775955136) + x[3]*(-41608495769709720) + x[4]*(-110388763512135856) + x[5]*(330678783844347392) + x[6]*(375037000943517952) + x[7]*(655272077897641856) + x[8]*(342055775075585280) + x[9]*(-684531422819084800);\n        h7 = h7/1e18 + (357529586826538112); if (h7 < 0) h7 = 0; if (h7 > 4e18) h7 = 4e18;\n        int256 q0 = (h0*(65561082743041472) + h1*(78970500190659328) + h2*(-30041886168663404) + h3*(-142263790464932704) + h4*(85372678145043728) + h5*(1125310076690304) + h6*(-104762853597859296) + h7*(-46666035333263304))/1e18 + (244428927242761504);\n        q0 = _cl(q0, 1e18) * 10 * int256(BPS) / 1e18;\n        int256 q1 = (h0*(2721429827962168) + h1*(191664668749095200) + h2*(4836918397666250) + h3*(-110345519596867792) + h4*(28352720751102608) + h5*(-72049396201150560) + h6*(22240646063654108) + h7*(134222188896537392))/1e18 + (60695131997598880);\n        q1 = _cl(q1, 1e18) * 10 * int256(BPS) / 1e18;\n        o0 = q0; o1 = q1;\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 \"rs2_nn_head\"; }\n}\n", "sha256": "4d0afd2cc3de80357c309f6315653ea368ab571a53660473b726dc45c7fcacb0", "blocked": null, "valid": true, "status": "PARTIAL: heavily scaled down from the brief because the sim ran ~1 s/seed on 4 cores; the learned head did NOT beat RS2 (negative gain). Not a candidate for the board.", "seeds": {"train_blocks": [[756100, 756163], [756164, 756227]], "monitor": [758800, 758863], "recheck": [759000, 759063], "final": [759400, 759527], "probe": [759950, 759981], "note": "brief asked for recheck 256, final 512 seeds, ES <=25 min with 64-seed blocks over many generations; actual: 2 generations, pop 4, recheck 64, final 128, probe 32. None >=5000000."}, "paired": {"n": 128, "mean_diff": -0.8699008167324727, "se": 0.05548089615836006, "worst": [{"seed": 759468, "d": -2.525522202658067}, {"seed": 759504, "d": -2.221350996142405}, {"seed": 759413, "d": -2.2146143116355006}, {"seed": 759516, "d": -2.169343537284476}, {"seed": 759467, "d": -2.1015292762155013}]}, "retail_edge_delta": {"mean": -1.8992130131860008, "se": 0.06037611666496577}, "arb_edge_delta": {"mean": 1.0293121964535272, "se": 0.01766489177536228}, "paired_vs": "RS2 (sha f24b6ca1...eaf2), each arm alone vs VanillaStrategy", "gas": {"max_ok": 220613, "init_max_ok": 243106, "oog": 0, "other_halts": 0, "probe_max_ok": 217643, "note": "counters from final-test chunks (candidate and RS2) and probe: no oog/revert/halt; es-search members all had zero halts (a halt would have scored -1000, none did)"}, "weights": {"shape": "10-8-2", "count": 106, "layout": "W1[8][10] row-major (80), b1[8], W2[2][8], b2[2]; fixed-point 1e18", "values": [-0.08382618294292249, -0.1415741077981505, 0.19921020278525262, 0.45351647205737455, 0.025609246890955464, -0.46767190864811364, -0.6831994495838094, -1.3972761101378612, -0.49919189140443887, -0.2896153924474102, 0.1290970196975244, 0.01033305272869095, 0.12408796151177713, -0.20928627130210953, -0.10342059022135945, 0.089543864387347, 0.03644409130716494, -0.1978413867352572, 0.27460224461461863, 0.008926083670827499, -0.3697305819163546, 0.3687602769920109, 0.1382118489301697, 0.40581546422733106, 0.30166316824707573, -0.06334806422087559, 0.8152686333757518, 0.6284088205481267, -0.01909166245823657, 0.14842906103157488, -0.4329051810300325, -0.22621846276910346, -0.0008877968793751592, -0.09783075368031391, -0.3095798469443378, -0.6797285569327473, -0.27786032392888255, -0.04632352183267015, 0.4985638009293542, -0.2957819783301242, 0.03175621795014594, -0.7183109303490206, -0.6085595189398911, 0.6038342407061792, 0.02641176764105601, -0.18288523394572825, -0.22125702606886843, -0.7761521083046491, 0.6644435463087893, 0.40330194272202635, 0.1840766809315055, 0.10455192156812276, 0.26542916585114446, 0.1590002624754938, -0.1822317878986006, 0.4138767287467006, 0.1836175272737962, -0.04760438792740885, -0.5725927245724505, -0.14725339409270047, 0.37467483341529667, 0.3922971460696115, 1.1273639778531923, 0.09633482853245712, -0.45855247016012324, -0.10742271289160184, -0.29486700467842997, -0.12575112592238546, -0.3296441967454983, -0.03307592045928807, 0.1686606811753238, -0.48631041662688573, -0.5730241827759551, -0.04160849576970972, -0.11038876351213586, 0.3306787838443474, 0.37503700094351794, 0.6552720778976419, 0.34205577507558527, -0.6845314228190847, 0.07907203145261117, 0.10683462634139819, 0.08636585640917306, 0.21004180062695843, 0.0516104407075117, 0.276172597428319, -0.05052212245800978, 0.3575295868265381, 0.06556108274304147, 0.07897050019065932, -0.030041886168663403, -0.1422637904649327, 0.08537267814504373, 0.0011253100766903043, -0.10476285359785929, -0.04666603533326331, 0.0027214298279621685, 0.19166466874909519, 0.00483691839766625, -0.1103455195968678, 0.02835272075110261, -0.07204939620115056, 0.02224064606365411, 0.1342221888965374, 0.2444289272427615, 0.06069513199759888]}, "normalization": {"x0": "clamp((spot/fastHat-1)*200,+-2)", "x1": "clamp((spot/pHat-1)*200,+-2)", "x2": "arbProbability", "x3": "clamp(sigmaHat*1000-1,+-2)", "x4": "clamp(lambdaHat*1.25-1,+-2)", "x5": "clamp(sizeHat*250-1,+-2)", "x6": "clamp(toxEma*1000/3-1,+-2)", "x7": "clamp(dirState-1,+-1)", "x8": "trade direction +-1", "x9": "first-in-step: min(silent,8)/8; later prints: 1+min(stepTradeCount,8)/8 (step position and silence merged into one input)", "output": "ReLU hidden clamped to [0,4]; outputs clamped to +-1, scaled by 10 bps, ADDED to RS2 bid/ask (then clampFee)."}, "deviations": ["Network is a residual head added to the final RS2 quotes, not a replacement of the whole fee mapping.", "Output layer initialised at zero, so the initial point equals RS2 exactly (monitor gain 0.0 at generation 0); no separate distillation on traces was run.", "Silence and step position merged into one input.", "ES was a simplified sep-CMA-style (mu=2 recombination of pop 4, sigma shrink 0.9, not full diagonal covariance adaptation), with sigma hand-retuned after generation 0 (output weights 0.15->0.02, hidden 0.1) because generation 0 lost 2.4-4.7.", "Selection used block fitness only; the monitor block never influenced selection. Recheck candidates: best two members by train-block fitness."], "generator_trainer_harness": {"generator": "gen.py (Python emitting unrolled Solidity head into RS2 source before the slots write; weights as literals in bytecode, slots 0/1 store bid/ask, no init writes)", "trainer": "es.py own numpy", "harness": "harness.py from artifact 26cb8e86... (H.harness_source), run_batch(X,Vanilla,configs,8), 20-seed chunks, gas_stats reset per chunk", "ch_check": {"source": "ChengHua926 Strategy.sol @d34a5199", "seeds": "0..999", "mean": 525.5258821780834, "exact": true}, "ch_check_note": "the artifact H.code field is NOT the CH source (it gave 527.404); the raw GitHub CH file reproduced 525.5258821780834 exactly."}, "es_checkpoint": [{"gen": 0, "sha": "9add5eb71665022607891124b524dc0abfdff42ac4c53ee68e9053b2e539683e", "mean": [0.12498052279238737, 0.04701559775151031, 0.3305237269668719, 0.4181183049974521, 0.07676895092597275, -0.34570116868370715, -0.674781132066907, -1.591785683020626, -0.4735203500265923, -0.38086033621829213, 0.02947885204019517, 0.1524457450933009, 0.15923491608580706, -0.17348106233605584, -0.18350862428002152, 0.10074385938521478, 0.066610893571016, -0.2309895823623485, 0.2027479455745539, 0.10490353306880142, -0.3066184942872721, 0.2527719948863072, 0.025917127254160156, -0.11812416565954133, 0.2657396161083647, -0.0015100799022541858, 0.49404967662959615, 0.5170934730233891, -0.060980876739912045, 0.20053713515553429, -0.47124079947448616, -0.21976167979518213, 0.06189730715651701, -0.04481084356659151, -0.28925717290995856, -0.9069571154823026, -0.18032921557227477, -0.23744396404149606, 0.36026303875441335, -0.4013213731357951, 0.06515287027542044, -0.613859658255156, -0.5504568080894835, 0.49493185769787507, 0.0795531828264805, -0.021699287564359782, -0.17069806058896317, -0.838217533390973, 0.676685107876112, 0.37504391451731267, 0.16324722594535349, 0.1237902936739569, 0.296268767136473, 0.05870820521744612, -0.24896642153018048, 0.3845632784157175, 0.17698645895188897, -0.18903093402001844, -0.45861680386766857, -0.05514260556134013, 0.39492827414429554, 0.34343621012805725, 1.0201658055336063, 0.006304740981677908, -0.36900056512771107, -0.21562512460652308, -0.07943564114743518, -0.21601732880284094, -0.42767731419683347, 0.037628711060258155, 0.12181873592495032, -0.36381699212865687, -0.5169465531022617, 0.08021062147974843, -0.0792498480466508, 0.21076765176578843, 0.4990192156091719, 0.6141283836760866, 0.2514986678673684, -0.7060573934884281, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]}, {"gen": 1, "sha": "b18c214dcc3e3f3c927c42d8d54b4ce375f3e4ed5293be0498e0bcabbf415282", "mean": [0.15532376621457644, 0.06617238416959492, 0.21993709587273336, 0.36087412633337834, 0.07111023059833957, -0.36056172242860873, -0.5710987002457626, -1.559167851978036, -0.6034798582657266, -0.2588372309210637, 0.16981944735345633, -0.0667945604808202, 0.20068796949833995, -0.09151953282054624, -0.06592758001640761, 0.12081896289106292, -0.0523918694923227, -0.2539860621891517, 0.2706455366942421, -0.02433935135123627, -0.3735115545599793, 0.387054347318324, 0.16450700140894647, 0.16217557248830367, 0.19492219003451172, 0.0367319426384671, 0.7418387044498591, 0.6323762907699446, 0.05422690642235267, 0.22850687087973232, -0.3329226343638816, -0.24149096324656025, 0.03506670473400127, -0.13247758200511445, -0.17146856323991633, -0.7798470206343484, -0.1711681614968431, -0.0703170680392328, 0.41294397852458264, -0.33406652520798, 0.004246081525420487, -0.675481602655305, -0.4483445103640884, 0.5643909714285799, 0.08869095265374159, 0.029809401272583917, -0.20750498392085165, -0.782307106860062, 0.7002924805859587, 0.4384754522329414, 0.235964624507022, 0.0796510450822354, 0.26962903897039225, 0.1259488683179113, -0.18870600596567977, 0.40252358775918784, 0.16012293845041461, -0.0034015957314163098, -0.5279888374976784, -0.06516315749587762, 0.45351614450988076, 0.25624676943189567, 1.1365040869608651, 0.08585854726228982, -0.43547493905473555, -0.1379551909071683, -0.19859715118186247, -0.10660357680494024, -0.49575408793028974, -0.09365785773295185, 0.28481402571412107, -0.49907195941701676, -0.49260389546857153, 0.08815102607874988, -0.1670941076393926, 0.29078650229649283, 0.47760366384695235, 0.6158049690094562, 0.37060803801786946, -0.6654366681362497, 0.10665551407709066, 0.09971615021575139, 0.09061441191536167, 0.24320247559748817, 0.07183681515439755, 0.23800160404715137, -0.048977792063322576, 0.35070650074971416, 0.09425066107637753, 0.05954900753678839, -0.0060290483044617045, -0.12505787721388775, 0.08526841572767645, -0.025711890091266162, -0.07430896262726061, -0.04494965369951822, -0.026622282740486232, 0.21226341103499724, -0.0011734351226067377, -0.09575453338776682, 0.01809157241715155, -0.07185325256039696, 0.02253298018886439, 0.15922328554624016, 0.278050203178673, 0.046563296438994387]}], "recheck": [{"gen": 1, "i": 3, "block_fit": -0.9004546653188328, "gain": -0.9506184380694362, "se": 0.08736626774818981, "n": 64, "bad": []}, {"gen": 1, "i": 0, "block_fit": -0.9770474171686008, "gain": -1.086412588287768, "se": 0.08417864834484677, "n": 64, "bad": []}], "frozen_member": {"gen": 1, "i": 3, "block_fit": -0.9004546653188328, "gain": -0.9506184380694362, "se": 0.08736626774818981, "n": 64, "bad": []}, "members": [{"gen": 0, "i": 0, "fit": -2.3991122203459927, "afterSwap_gas": 220573}, {"gen": 0, "i": 1, "fit": -4.730388859333366, "afterSwap_gas": 220631}, {"gen": 0, "i": 2, "fit": -2.452029691862644, "afterSwap_gas": 220614}, {"gen": 0, "i": 3, "fit": -4.668959933023138, "afterSwap_gas": 220643}, {"gen": 1, "i": 0, "fit": -0.9770474171686008, "afterSwap_gas": 217812}, {"gen": 1, "i": 1, "fit": -2.2525518503801534, "afterSwap_gas": 217829}, {"gen": 1, "i": 2, "fit": -0.9798375145366638, "afterSwap_gas": 217794}, {"gen": 1, "i": 3, "fit": -0.9004546653188328, "afterSwap_gas": 217826}], "monitor_curve": [{"generation": 0, "gain": 0.0, "se": 0.0, "n": 64, "ckpt_sha": "9add5eb71665022607891124b524dc0abfdff42ac4c53ee68e9053b2e539683e", "train_block": [756100, 756163], "best_member_fit": -2.3991122203459927}, {"generation": 1, "gain": -1.3347011728636895, "se": 0.1100737930990517, "n": 64, "ckpt_sha": "b18c214dcc3e3f3c927c42d8d54b4ce375f3e4ed5293be0498e0bcabbf415282", "train_block": [756164, 756227], "best_member_fit": -0.9004546653188328}], "notes": ["Result: frozen head loses 0.87 (se 0.055, n=128) vs RS2 on the final block; retail edge -1.90, arb edge +1.03. No improvement toward the +5 needed; no board claim.", "Search budget (4 cores, ~1 s/seed, 12 s viaIR compile per member) allowed only 8 member evaluations; evidence is too thin to say the idea cannot work."]}