{"item":"c","batch":"e09-learn-head","blocked":null,"label":"partial","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        int256[10] memory z;\n        z[0] = fastHat > 0 ? ((int256(spot) - int256(fastHat)) * 1e6) / int256(fastHat) : int256(0);\n        z[1] = pHat > 0 ? ((int256(spot) - int256(pHat)) * 1e6) / int256(pHat) : int256(0);\n        z[2] = int256(arbProbability / 1e12);\n        z[3] = int256(sigmaHat / 1e12);\n        z[4] = int256(lambdaHat / 1e12);\n        z[5] = int256(sizeHat / 1e12);\n        z[6] = int256(toxSignal / 1e12);\n        z[7] = (int256(dirState) - 1e18) / 1e12;\n        z[8] = int256(stepTradeCount) * 1e6;\n        z[9] = int256(silent > 8 ? 8 : silent) * 1e6;\n        lambdaHat = originalLambda; sizeHat = originalSize;\n        (uint256 bidFee, uint256 askFee) = _head(z, stepTradeCount == 1);\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 _head(int256[10] memory z, bool first) internal pure returns (uint256 bidFee, uint256 askFee) {\n        z[0] = ((z[0] - 37) * 117127575) / 1e6;\n        if (z[0] > 4e6) z[0] = 4e6; else if (z[0] < -4e6) z[0] = -4e6;\n        z[1] = ((z[1] - 27) * 138571526) / 1e6;\n        if (z[1] > 4e6) z[1] = 4e6; else if (z[1] < -4e6) z[1] = -4e6;\n        z[2] = ((z[2] - 322119) * 2942156) / 1e6;\n        if (z[2] > 4e6) z[2] = 4e6; else if (z[2] < -4e6) z[2] = -4e6;\n        z[3] = ((z[3] - 4065) * 757860236) / 1e6;\n        if (z[3] > 4e6) z[3] = 4e6; else if (z[3] < -4e6) z[3] = -4e6;\n        z[4] = ((z[4] - 911122) * 15414759) / 1e6;\n        if (z[4] > 4e6) z[4] = 4e6; else if (z[4] < -4e6) z[4] = -4e6;\n        z[5] = ((z[5] - 862) * 3389875219) / 1e6;\n        if (z[5] > 4e6) z[5] = 4e6; else if (z[5] < -4e6) z[5] = -4e6;\n        z[6] = ((z[6] - 4767) * 186735218) / 1e6;\n        if (z[6] > 4e6) z[6] = 4e6; else if (z[6] < -4e6) z[6] = -4e6;\n        z[7] = ((z[7] - 2) * 94009610) / 1e6;\n        if (z[7] > 4e6) z[7] = 4e6; else if (z[7] < -4e6) z[7] = -4e6;\n        z[8] = ((z[8] - 1532492) * 1278166) / 1e6;\n        if (z[8] > 4e6) z[8] = 4e6; else if (z[8] < -4e6) z[8] = -4e6;\n        z[9] = ((z[9] - 853363) * 1037439) / 1e6;\n        if (z[9] > 4e6) z[9] = 4e6; else if (z[9] < -4e6) z[9] = -4e6;\n        int256[8] memory a;\n        a[0] = 1664664 + (z[0] * (-527058) + z[1] * (-401004) + z[2] * (-12637) + z[3] * 7729 + z[4] * 4886 + z[5] * 1534 + z[6] * 260622 + z[7] * (-1279) + z[8] * 3918 + z[9] * (-3468)) / 1e6;\n        if (a[0] > 1e6) a[0] = 1e6; else if (a[0] < -1e6) a[0] = -1e6;\n        a[1] = 3586895 + (z[0] * 1038326 + z[1] * 166779 + z[2] * (-74131) + z[3] * (-2403) + z[4] * (-6182) + z[5] * (-167541) + z[6] * 232746 + z[7] * 58332 + z[8] * 28195 + z[9] * (-23275)) / 1e6;\n        if (a[1] > 1e6) a[1] = 1e6; else if (a[1] < -1e6) a[1] = -1e6;\n        a[2] = (-784269) + (z[0] * (-41367) + z[1] * (-575422) + z[2] * 12099 + z[3] * 1864 + z[4] * 4186 + z[5] * 12573 + z[6] * 229319 + z[7] * (-4510) + z[8] * (-2149) + z[9] * (-1759)) / 1e6;\n        if (a[2] > 1e6) a[2] = 1e6; else if (a[2] < -1e6) a[2] = -1e6;\n        a[3] = 105010 + (z[0] * 167744 + z[1] * 203088 + z[2] * 1704 + z[3] * 1678 + z[4] * (-1342) + z[5] * 8595 + z[6] * 84694 + z[7] * 2615 + z[8] * 3263 + z[9] * (-6676)) / 1e6;\n        if (a[3] > 1e6) a[3] = 1e6; else if (a[3] < -1e6) a[3] = -1e6;\n        a[4] = (-595071) + (z[0] * (-357404) + z[1] * (-399695) + z[2] * (-754) + z[3] * 9951 + z[4] * (-4677) + z[5] * 11793 + z[6] * (-373701) + z[7] * (-16322) + z[8] * 6591 + z[9] * (-7107)) / 1e6;\n        if (a[4] > 1e6) a[4] = 1e6; else if (a[4] < -1e6) a[4] = -1e6;\n        a[5] = (-693332) + (z[0] * 51840 + z[1] * (-43338) + z[2] * 7547 + z[3] * 70923 + z[4] * 49196 + z[5] * 98678 + z[6] * 275602 + z[7] * 215 + z[8] * 2124 + z[9] * (-2206)) / 1e6;\n        if (a[5] > 1e6) a[5] = 1e6; else if (a[5] < -1e6) a[5] = -1e6;\n        a[6] = 4571535 + (z[0] * (-32767) + z[1] * 21496 + z[2] * (-8146) + z[3] * 4933 + z[4] * (-4229) + z[5] * (-376112) + z[6] * (-1002849) + z[7] * (-7763) + z[8] * 46025 + z[9] * (-30391)) / 1e6;\n        if (a[6] > 1e6) a[6] = 1e6; else if (a[6] < -1e6) a[6] = -1e6;\n        a[7] = (-3599529) + (z[0] * 1061743 + z[1] * 474761 + z[2] * 9933 + z[3] * (-12109) + z[4] * (-5067) + z[5] * 9167 + z[6] * (-404552) + z[7] * 8476 + z[8] * (-40186) + z[9] * 30856) / 1e6;\n        if (a[7] > 1e6) a[7] = 1e6; else if (a[7] < -1e6) a[7] = -1e6;\n        int256 ob = 10401 + (a[0] * (-4397) + a[1] * 3148 + a[2] * (-4836) + a[3] * 3933 + a[4] * (-2259) + a[5] * 3908 + a[6] * (-4364) + a[7] * 3929) / 1e6;\n        if (ob < 0) ob = 0; else if (ob > 1e5) ob = 1e5;\n        int256 ok = 10272 + (a[0] * 4995 + a[1] * (-3523) + a[2] * 2666 + a[3] * (-4088) + a[4] * 3613 + a[5] * 3712 + a[6] * (-4539) + a[7] * (-2783)) / 1e6;\n        if (ok < 0) ok = 0; else if (ok > 1e5) ok = 1e5;\n        bidFee = uint256(ob) * 1e12; askFee = uint256(ok) * 1e12;\n        if (first) {\n            if (askFee > bidFee && bidFee > 30 * BPS) bidFee = 30 * BPS;\n            else if (bidFee > askFee && askFee > 30 * BPS) askFee = 30 * BPS;\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    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_c\"; }\n}\n","sha256":"a0215776ff2f7e2d5b84d113dd2718e06732d13b1675cdbf110d4bdaf0014e19","valid":true,"verdict":"negative: the frozen RS2 10-8-2 neural fee head loses to RS2 on the single final test; do not submit","seeds":{"final":[769400,769911],"n":512,"recheck":[769000,769255],"monitor":[768800,768863],"es_blocks":[766200,766455],"distill_traces":[766100,766159],"gas_probe":[769950,769969],"none_ge_5000000":true},"paired":{"n":512,"mean_diff":-18.962612670904427,"se":0.6864779293865971,"worst":[{"seed":769436,"d":-117.68898277849928},{"seed":769543,"d":-100.81911897096256},{"seed":769865,"d":-73.43544131791185},{"seed":769442,"d":-73.31821138624827},{"seed":769747,"d":-69.48624744272547}]},"retail_edge_delta":{"mean":-5.320727545946966,"se":0.11195377895944253},"arb_edge_delta":{"mean":-13.641885124957458,"se":0.6783163871392797},"reconciliation_max_abs":1.1368683772161603e-13,"arm_mean_edge":{"candidate":508.5219291330455,"rs2":527.4845418039499},"baseline":{"name":"RS2","sha256":"f24b6ca198b744c380842c3a17964a6dc21f311bb04984b26733afa3fee8eaf2","source":"https://api.imdnotdead.fun/artifacts/19f2ec857df536e8ab62ab1da89ec6623332521ce46849f15bccff402c63e1e5","gas":{"max_ok":200257,"init_max_ok":243099,"oog":0,"other_halts":0}},"gas":{"max_ok":218095,"init_max_ok":243084,"oog":0,"other_halts":0,"limits":{"afterSwap_lt":235000,"init_lt":245000},"probe":"random nonzero 10-8-2 weights on seeds 769950..769969: afterSwap max_ok 205300, init 243084, oog 0","es_all_members_max_ok":221078,"es_oog_total":0},"weights":{"note":"fixed point: z_i=clamp((x_i-mu_i)*k_i/1e6,+-4e6); a_j=clamp(b1_j+sum z_i*W1_ji/1e6,+-1e6); fee_k=clamp(b2_k+sum a_j*W2_kj/1e6,0,1e5)*1e12 WAD (k=0 bid,1 ask); then RS2 first-in-step 30bps and 36bps asymmetric caps; weights are bytecode literals, no init writes; bid/ask stored in slots 0/1","W":{"mu":[37,27,322119,4065,911122,862,4767,2,1532492,853363],"k":[117127575,138571526,2942156,757860236,15414759,3389875219,186735218,94009610,1278166,1037439],"W1":[[-527058,-401004,-12637,7729,4886,1534,260622,-1279,3918,-3468],[1038326,166779,-74131,-2403,-6182,-167541,232746,58332,28195,-23275],[-41367,-575422,12099,1864,4186,12573,229319,-4510,-2149,-1759],[167744,203088,1704,1678,-1342,8595,84694,2615,3263,-6676],[-357404,-399695,-754,9951,-4677,11793,-373701,-16322,6591,-7107],[51840,-43338,7547,70923,49196,98678,275602,215,2124,-2206],[-32767,21496,-8146,4933,-4229,-376112,-1002849,-7763,46025,-30391],[1061743,474761,9933,-12109,-5067,9167,-404552,8476,-40186,30856]],"b1":[1664664,3586895,-784269,105010,-595071,-693332,4571535,-3599529],"W2":[[-4397,3148,-4836,3933,-2259,3908,-4364,3929],[4995,-3523,2666,-4088,3613,3712,-4539,-2783]],"b2":[10401,10272]}},"normalization":{"features":["spot/fastHat-1","spot/pHat-1","probArb","sigmaHat","retailRate(lambdaHat blended)","retailSize(sizeHat blended)","toxEma","dirState-1","stepPosition","silence(cap 8)"],"raw_units":"1e6-scaled ints (gaps as fraction*1e6, WAD values /1e12, counts*1e6)","mu":[37,27,322119,4065,911122,862,4767,2,1532492,853363],"k":[117127575,138571526,2942156,757860236,15414759,3389875219,186735218,94009610,1278166,1037439],"source":"mean and sd over the distillation trace rows; k=round(1e12/sd)"},"distillation":{"rows":702567,"corr_bid_ask":[0.9734787474402591,0.98110982254288],"rmse_bid_ask_1e6":[1065.6818725150001,849.2784092621321],"target_sd_1e6":[4655.239263442031,4379.482325030004],"method":"RS2 recorder (same estimators, features written to slots 16..25) driven in pyrevm by an approximate float replay (GBM, closed-form arb, Poisson lognormal retail split by marginal price vs 30bps vanilla) on train seeds 766100..766159 x 10000 steps; hardtanh net fit by Adam on MSE, numpy seed 903"},"es":{"type":"diagonal (sep) CMA-ES, own numpy implementation, rng default_rng([903,gen])","dims":106,"lam":8,"mu":4,"scale_rule":"0.1*max(|w|,0.3*layer_rms)","final_sigma":0.9092687766580079,"generations":4,"members":32,"seconds":733.2365038394928,"fitness":"paired mean edge vs RS2 (RS2 run once per block) on the same 64-seed CRN block for every member; block 766200+64*gen","log":[{"gen":0,"member":0,"mean_diff":-35.0238,"se":2.9355,"max_ok":218723,"oog":0},{"gen":0,"member":1,"mean_diff":-67.2121,"se":2.8058,"max_ok":218381,"oog":0},{"gen":0,"member":2,"mean_diff":-100.7793,"se":3.5434,"max_ok":218187,"oog":0},{"gen":0,"member":3,"mean_diff":-43.9206,"se":3.3235,"max_ok":218999,"oog":0},{"gen":0,"member":4,"mean_diff":-44.7318,"se":3.3774,"max_ok":218944,"oog":0},{"gen":0,"member":5,"mean_diff":-48.4127,"se":2.9357,"max_ok":218896,"oog":0},{"gen":0,"member":6,"mean_diff":-42.5428,"se":3.2994,"max_ok":218966,"oog":0},{"gen":0,"member":7,"mean_diff":-36.6067,"se":2.8834,"max_ok":220754,"oog":0},{"gen":1,"member":0,"mean_diff":-52.0888,"se":2.1646,"max_ok":216875,"oog":0},{"gen":1,"member":1,"mean_diff":-30.62,"se":2.6388,"max_ok":219884,"oog":0},{"gen":1,"member":2,"mean_diff":-41.8504,"se":2.4024,"max_ok":219882,"oog":0},{"gen":1,"member":3,"mean_diff":-24.3967,"se":2.6597,"max_ok":215007,"oog":0},{"gen":1,"member":4,"mean_diff":-89.9201,"se":2.4953,"max_ok":215014,"oog":0},{"gen":1,"member":5,"mean_diff":-65.5592,"se":2.7116,"max_ok":219099,"oog":0},{"gen":1,"member":6,"mean_diff":-41.1662,"se":2.2676,"max_ok":221078,"oog":0},{"gen":1,"member":7,"mean_diff":-37.4303,"se":2.4045,"max_ok":216416,"oog":0},{"gen":2,"member":0,"mean_diff":-30.1135,"se":1.722,"max_ok":215033,"oog":0},{"gen":2,"member":1,"mean_diff":-45.1452,"se":1.7372,"max_ok":219940,"oog":0},{"gen":2,"member":2,"mean_diff":-40.9719,"se":1.9623,"max_ok":219219,"oog":0},{"gen":2,"member":3,"mean_diff":-24.9213,"se":1.6581,"max_ok":216443,"oog":0},{"gen":2,"member":4,"mean_diff":-25.886,"se":1.7575,"max_ok":218992,"oog":0},{"gen":2,"member":5,"mean_diff":-30.0839,"se":1.9332,"max_ok":219013,"oog":0},{"gen":2,"member":6,"mean_diff":-69.335,"se":2.1946,"max_ok":220952,"oog":0},{"gen":2,"member":7,"mean_diff":-39.1961,"se":1.8503,"max_ok":218403,"oog":0},{"gen":3,"member":0,"mean_diff":-23.5618,"se":2.6915,"max_ok":214938,"oog":0},{"gen":3,"member":1,"mean_diff":-35.018,"se":2.6234,"max_ok":217045,"oog":0},{"gen":3,"member":2,"mean_diff":-34.7362,"se":2.7416,"max_ok":217999,"oog":0},{"gen":3,"member":3,"mean_diff":-38.6113,"se":2.6284,"max_ok":214932,"oog":0},{"gen":3,"member":4,"mean_diff":-48.3889,"se":2.8075,"max_ok":220415,"oog":0},{"gen":3,"member":5,"mean_diff":-30.5079,"se":2.5976,"max_ok":214932,"oog":0},{"gen":3,"member":6,"mean_diff":-26.7077,"se":2.539,"max_ok":214932,"oog":0},{"gen":3,"member":7,"mean_diff":-32.6605,"se":2.7099,"max_ok":217478,"oog":0}],"checkpoints":[{"gen":0,"sha256":"a0215776ff2f7e2d5b84d113dd2718e06732d13b1675cdbf110d4bdaf0014e19","frozen_at_s":0.005396127700805664},{"gen":1,"sha256":"b834f9a131328240f07fa0bc1f9e50402eb9be46f5634c3e5166f7e9c7428465","frozen_at_s":171.6681306362152},{"gen":2,"sha256":"e48cdb1fdb72699f96609407c074a50c016d885875ad52a13ac8dfb114190c7e","frozen_at_s":360.8195643424988},{"gen":3,"sha256":"124d4b4f4d8d67985c1e8cd6275082c3b3613e879c7f36f719468d0d17f86a68","frozen_at_s":540.4128501415253},{"gen":4,"sha256":"018208ed9d44172331dfebaa1d23d47bbf045fffdd46a80634c84feb1b7151c5","frozen_at_s":733.2365038394928}],"stopped_early":"ran 733 s of the <=25 min budget so monitor, recheck and final could finish in this run"},"monitor_curve":[{"gen":0,"sha256":"a0215776ff2f7e2d5b84d113dd2718e06732d13b1675cdbf110d4bdaf0014e19","gain":-21.300187768854883,"se":2.4160959618063944,"max_ok":214903,"init_max_ok":243084,"oog":0,"other_halts":0},{"gen":1,"sha256":"b834f9a131328240f07fa0bc1f9e50402eb9be46f5634c3e5166f7e9c7428465","gain":-27.27867349506553,"se":2.3159226569873113,"max_ok":216574,"init_max_ok":243084,"oog":0,"other_halts":0},{"gen":2,"sha256":"e48cdb1fdb72699f96609407c074a50c016d885875ad52a13ac8dfb114190c7e","gain":-23.835527065613757,"se":2.4283900065539576,"max_ok":214885,"init_max_ok":243084,"oog":0,"other_halts":0},{"gen":3,"sha256":"124d4b4f4d8d67985c1e8cd6275082c3b3613e879c7f36f719468d0d17f86a68","gain":-22.31923197607088,"se":2.536974454533558,"max_ok":214894,"init_max_ok":243084,"oog":0,"other_halts":0},{"gen":4,"sha256":"018208ed9d44172331dfebaa1d23d47bbf045fffdd46a80634c84feb1b7151c5","gain":-23.60351427584967,"se":2.4988275584102198,"max_ok":214887,"init_max_ok":243084,"oog":0,"other_halts":0}],"recheck":[{"gen":0,"sha256":"a0215776ff2f7e2d5b84d113dd2718e06732d13b1675cdbf110d4bdaf0014e19","paired":{"n":256,"mean_diff":-19.446476893468407,"se":0.970084771379569,"worst":[{"seed":769089,"d":-108.57480994000707},{"seed":769175,"d":-93.7698896112239},{"seed":769213,"d":-92.70688216745668},{"seed":769067,"d":-73.83742468158744},{"seed":769232,"d":-58.128897770236335}]},"retail_edge_delta":{"mean":-5.380104588945689,"se":0.15479009125469914},"arb_edge_delta":{"mean":-14.06637230452272,"se":0.9611920714184773},"gas":{"max_ok":217892,"init_max_ok":243084,"oog":0,"other_halts":0}},{"gen":3,"sha256":"124d4b4f4d8d67985c1e8cd6275082c3b3613e879c7f36f719468d0d17f86a68","paired":{"n":256,"mean_diff":-20.315273292775302,"se":1.0179838591313792,"worst":[{"seed":769089,"d":-110.95544356078108},{"seed":769175,"d":-100.70220647469569},{"seed":769213,"d":-97.38576805720379},{"seed":769067,"d":-77.43990057974861},{"seed":769232,"d":-59.169514464528675}]},"retail_edge_delta":{"mean":-2.294601237238276,"se":0.11979035025528054},"arb_edge_delta":{"mean":-18.02067205553703,"se":1.0190321875183996},"gas":{"max_ok":217883,"init_max_ok":243084,"oog":0,"other_halts":0}}],"frozen":{"gen":0,"rule":"largest recheck mean of the two best monitor checkpoints; gen 0 = the distilled head before any ES update"},"calibration":{"source":"https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol","seeds":[0,999],"mean":525.5258821780834,"expected":525.5258821780834,"match":true},"harness":"H.harness_source (https://api.imdnotdead.fun/artifacts/26cb8e86f05170308de2a4b24821ac7fad2a17077f8d94af6e0f31985343fe89) run(): MatchRunner configs, 10000 steps, run_batch(X,VanillaStrategy,configs,8), chunks of 20, gas_stats reset and collected per chunk; compare() for paired/retail/arb","generator":"\"\"\"RS2 -> recorder / 10-8-2 fixed-point fee head generator.\"\"\"\nimport hashlib\nfrom pathlib import Path\nRS2 = Path(__file__).with_name('rs2.sol').read_text()\nassert hashlib.sha256(RS2.encode()).hexdigest() == 'f24b6ca198b744c380842c3a17964a6dc21f311bb04984b26733afa3fee8eaf2'\nNI, NH, NO = 10, 8, 2\nFEATS = ['spot/fastHat-1', 'spot/pHat-1', 'probArb', 'sigmaHat', 'retailRate(lambdaHat blended)',\n         'retailSize(sizeHat blended)', 'toxEma', 'dirState-1', 'stepPosition', 'silence(cap 8)']\nA = '        uint256 flowSize = wmul(lambdaHat, sizeHat);\\n'\nB = '        else if (askFee > bidFee && bidFee > 36 * BPS) bidFee = 36 * BPS;\\n'\ni0 = RS2.index(A); i1 = RS2.index(B) + len(B)\n\nFEAT_CODE = '''        int256[10] memory z;\n        z[0] = fastHat > 0 ? ((int256(spot) - int256(fastHat)) * 1e6) / int256(fastHat) : int256(0);\n        z[1] = pHat > 0 ? ((int256(spot) - int256(pHat)) * 1e6) / int256(pHat) : int256(0);\n        z[2] = int256(arbProbability / 1e12);\n        z[3] = int256(sigmaHat / 1e12);\n        z[4] = int256(lambdaHat / 1e12);\n        z[5] = int256(sizeHat / 1e12);\n        z[6] = int256(toxSignal / 1e12);\n        z[7] = (int256(dirState) - 1e18) / 1e12;\n        z[8] = int256(stepTradeCount) * 1e6;\n        z[9] = int256(silent > 8 ? 8 : silent) * 1e6;\n'''\n\n\ndef recorder():\n    rec = '        for (uint256 q = 0; q < 10; q++) slots[16 + q] = uint256(z[q]);\\n'\n    return RS2[:i0] + FEAT_CODE + RS2[i0:i1] + rec + RS2[i1:]\n\n\ndef lit(v):\n    v = int(v)\n    return f'({v})' if v < 0 else str(v)\n\n\ndef nn(W):\n    \"\"\"W: dict with mu[10], k[10], W1[8][10], b1[8], W2[2][8], b2[2] (all ints).\"\"\"\n    body = FEAT_CODE + '''        lambdaHat = originalLambda; sizeHat = originalSize;\n        (uint256 bidFee, uint256 askFee) = _head(z, stepTradeCount == 1);\n'''\n    s = RS2[:i0] + body + RS2[i1:]\n    # dirState etc. still declared earlier; original mapping removed\n    s = s.replace('        lambdaHat = originalLambda; sizeHat = originalSize;\\n        uint256 dirDev', '        uint256 dirDev')\n    h = ['    function _head(int256[10] memory z, bool first) internal pure returns (uint256 bidFee, uint256 askFee) {']\n    for i in range(NI):\n        h.append(f'        z[{i}] = ((z[{i}] - {lit(W[\"mu\"][i])}) * {lit(W[\"k\"][i])}) / 1e6;')\n        h.append(f'        if (z[{i}] > 4e6) z[{i}] = 4e6; else if (z[{i}] < -4e6) z[{i}] = -4e6;')\n    h.append('        int256[8] memory a;')\n    for j in range(NH):\n        terms = ' + '.join(f'z[{i}] * {lit(W[\"W1\"][j][i])}' for i in range(NI) if int(W[\"W1\"][j][i]) != 0) or '0'\n        h.append(f'        a[{j}] = {lit(W[\"b1\"][j])} + ({terms}) / 1e6;')\n        h.append(f'        if (a[{j}] > 1e6) a[{j}] = 1e6; else if (a[{j}] < -1e6) a[{j}] = -1e6;')\n    for k, nm in enumerate(['b', 'k']):\n        terms = ' + '.join(f'a[{j}] * {lit(W[\"W2\"][k][j])}' for j in range(NH) if int(W[\"W2\"][k][j]) != 0) or '0'\n        h.append(f'        int256 o{nm} = {lit(W[\"b2\"][k])} + ({terms}) / 1e6;')\n        h.append(f'        if (o{nm} < 0) o{nm} = 0; else if (o{nm} > 1e5) o{nm} = 1e5;')\n    h.append('        bidFee = uint256(ob) * 1e12; askFee = uint256(ok) * 1e12;')\n    h.append('''        if (first) {\n            if (askFee > bidFee && bidFee > 30 * BPS) bidFee = 30 * BPS;\n            else if (bidFee > askFee && askFee > 30 * BPS) askFee = 30 * BPS;\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    s = s.replace('    struct PosteriorInput', '\\n'.join(h) + '\\n    struct PosteriorInput', 1)\n    s = s.replace('return \"censored_10\";', 'return \"rs2_nn_head_c\";')\n    return s\n","trainer":"import numpy as np, json, time\nX = np.load('scratch/trace.npy').astype(np.float64)\nF, Y = X[:, :10], X[:, 10:12]\nmu = F.mean(0); sd = np.maximum(F.std(0), 1.0)\nZ = np.clip((F - mu) / sd, -4, 4)\nrng = np.random.default_rng(903)\nW1 = rng.normal(0, .3, (8, 10)); b1 = np.zeros(8)\nW2 = rng.normal(0, 300, (2, 8)); b2 = Y.mean(0).copy()\nP = [W1, b1, W2, b2]; M = [np.zeros_like(p) for p in P]; V = [np.zeros_like(p) for p in P]\nlrs = [3e-3, 3e-3, 3.0, 3.0]\nt0 = time.time(); n = len(Z)\ndef fwd(Zb):\n    a = Zb @ W1.T + b1; h = np.clip(a, -1, 1); o = h @ W2.T + b2\n    return a, h, o\nfor it in range(1, 6001):\n    idx = rng.integers(0, n, 4096); Zb, Yb = Z[idx], Y[idx]\n    a, h, o = fwd(Zb)\n    oc = np.clip(o, 0, 1e5); g = 2 * (oc - Yb) * ((o > 0) & (o < 1e5)) / len(Zb)\n    gW2 = g.T @ h; gb2 = g.sum(0)\n    gh = g @ W2 * ((a > -1) & (a < 1))\n    gW1 = gh.T @ Zb; gb1 = gh.sum(0)\n    lr_scale = 1.0 if it < 4000 else 0.3\n    for p, gr, m, v, lr in zip(P, [gW1, gb1, gW2, gb2], M, V, lrs):\n        m *= .9; m += .1 * gr; v *= .999; v += .001 * gr * gr\n        p -= lr * lr_scale * (m / (1 - .9 ** it)) / (np.sqrt(v / (1 - .999 ** it)) + 1e-12)\n_, _, o = fwd(Z); o = np.clip(o, 0, 1e5)\nrmse = np.sqrt(((o - Y) ** 2).mean(0)); base = Y.std(0)\ncorr = [float(np.corrcoef(o[:, k], Y[:, k])[0, 1]) for k in range(2)]\nprint('rmse(1e-6 fee)', rmse, 'target sd', base, 'corr', corr, time.time() - t0)\nWq = dict(mu=[int(round(x)) for x in mu], k=[int(round(1e12 / s)) for s in sd],\n          W1=[[int(round(x * 1e6)) for x in r] for r in W1], b1=[int(round(x * 1e6)) for x in b1],\n          W2=[[int(round(x)) for x in r] for r in W2], b2=[int(round(x)) for x in b2])\njson.dump(dict(W=Wq, float=dict(mu=mu.tolist(), sd=sd.tolist(), W1=W1.tolist(), b1=b1.tolist(), W2=W2.tolist(), b2=b2.tolist()),\n               fit=dict(rows=int(n), rmse_bid_ask_1e6=rmse.tolist(), target_sd=base.tolist(), corr=corr)),\n          open('scratch/distilled.json', 'w'))\n","protocol":{"repo":"https://github.com/benedictbrady/amm-challenge","commit":"6b6921f8d463314aa7c96234e1be25a25245f704"},"notes":["All figures are local paired measurements vs RS2, not leaderboard claims. Nothing was run on seeds >= 5000000 and nothing was compared with STACK.","The final block 769400..769911 was run once, on the single frozen hash.","ES did not improve on the distilled start: no checkpoint beat gen 0 on recheck.","Likely cause of the loss: the 10-8-2 hardtanh head replaces RS2 stale-shift, trade-boost and tail logic but cannot see trade size or side. A distillation fit with corr ~0.97 is not close enough, and arb edge falls sharply.","Distillation traces came from an approximate Python replay, not the Rust engine, so the training distribution only approximates the real one."]}