{
 "approach": "Deepen YQ's retail-print fair-price update. Swept PHAT_ALPHA_RETAIL, the pHat shock gate (GATE_SIGMA_MULT, MIN_GATE) one at a time around YQ's values, plus a sigmaHat-update gate (only first prints with ret>0.5*sigmaHat). YQ's retail alpha 0.05 is already best; MIN_GATE 0.03 best; sigma gate hurts badly. Only change kept: GATE_SIGMA_MULT 10->5 (tighter adaptive outlier gate on pHat updates).",
 "tried": [
  {
   "variant": "a0",
   "params": {
    "alpha": 0
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -0.8514,
   "se": 0.0874
  },
  {
   "variant": "a0.02",
   "params": {
    "alpha": 0.02
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -0.352,
   "se": 0.0609
  },
  {
   "variant": "a0.1",
   "params": {
    "alpha": 0.1
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -0.3859,
   "se": 0.0685
  },
  {
   "variant": "g5",
   "params": {
    "gm": 5
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": 0.2118,
   "se": 0.0616
  },
  {
   "variant": "g20",
   "params": {
    "gm": 20
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -1.0222,
   "se": 0.0901
  },
  {
   "variant": "m0.01",
   "params": {
    "mg": 0.01
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -0.0692,
   "se": 0.0307
  },
  {
   "variant": "m0.06",
   "params": {
    "mg": 0.06
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -0.6816,
   "se": 0.0628
  },
  {
   "variant": "sgate",
   "params": {
    "sgate": true
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -2.4636,
   "se": 0.1461
  },
  {
   "variant": "g5m01",
   "params": {
    "gm": 5,
    "mg": 0.01
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": -0.5024,
   "se": 0.111
  },
  {
   "variant": "g7",
   "params": {
    "gm": 7
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": 0.1923,
   "se": 0.0481
  },
  {
   "variant": "g3",
   "params": {
    "gm": 3
   },
   "seeds": "1000..1099",
   "n": 100,
   "mean_diff": 0.138,
   "se": 0.0807
  },
  {
   "variant": "alpha0 (pilot)",
   "params": {
    "alpha": 0
   },
   "seeds": "0..39",
   "n": 40,
   "mean_diff": -0.82,
   "se": 0.125
  },
  {
   "variant": "alpha0.02 (pilot)",
   "params": {
    "alpha": 0.02
   },
   "seeds": "0..49",
   "n": 50,
   "mean_diff": -0.357,
   "se": 0.094
  }
 ],
 "next": [
  "Replicate g5 on a larger fresh block (>=2000 seeds) on a multi-core machine; this one had 1 CPU/1.5GB so n was cut to 300.",
  "Fine-grid GATE_SIGMA_MULT in {4,5,6,7} jointly with MIN_GATE {0.02,0.03,0.04}; g3/g5/g7 all positive, m0.01 with g5 negative.",
  "Score on seeds 0..999 (not done here for time)."
 ],
 "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    // --- decay / update constants ---\n    uint256 constant ELAPSED_CAP = 8;\n    uint256 constant SIGNAL_THRESHOLD = WAD / 500; // ~20 bps of reserve\n    uint256 constant DIR_DECAY = 800000000000000000; // 0.80\n    uint256 constant ACT_DECAY = 700000000000000000; // 0.70\n    uint256 constant SIZE_DECAY = 700000000000000000; // 0.70\n    uint256 constant TOX_DECAY = 910000000000000000; // 0.91\n    uint256 constant SIGMA_DECAY = 824000000000000000; // 0.824\n    uint256 constant LAMBDA_DECAY = 990000000000000000; // 0.99\n    uint256 constant SIZE_BLEND_DECAY = 818000000000000000; // 0.818\n    uint256 constant TOX_BLEND_DECAY = 51000000000000000; // 0.051\n    uint256 constant ACT_BLEND_DECAY = 985000000000000000; // 0.985\n    uint256 constant PHAT_ALPHA = 260000000000000000; // 0.26\n    uint256 constant PHAT_ALPHA_RETAIL = 50000000000000000; // 0.05\n    uint256 constant DIR_IMPACT_MULT = 2;\n\n    // --- Adaptive Shock Gate ---\n    uint256 constant GATE_SIGMA_MULT = 5 * WAD;\n    uint256 constant MIN_GATE = 30000000000000000; // 0.03 WAD\n\n    // --- Cubic Toxicity ---\n    uint256 constant TOX_CUBIC_COEF = 15000 * BPS;\n\n    // --- Trade-Tox Boost ---\n    uint256 constant TRADE_TOX_BOOST = 2500 * BPS;\n\n    // --- Asymmetric Stale Dir ---\n    uint256 constant STALE_ATTRACT_FRAC = 1124000000000000000; // 1.124\n\n    // --- state caps ---\n    uint256 constant RET_CAP = WAD / 10; // 10%\n    uint256 constant TOX_CAP = WAD / 5; // 20%\n    uint256 constant TRADE_RATIO_CAP = WAD / 5; // 20%\n    uint256 constant LAMBDA_CAP = 5 * WAD; // max 5 trades/step estimate\n    uint256 constant STEP_COUNT_CAP = 64; // guardrail\n\n    // --- fee model constants ---\n    uint256 constant BASE_FEE = 3 * BPS;\n    uint256 constant SIGMA_COEF = 200000000000000000; // 0.20\n    uint256 constant LAMBDA_COEF = 12 * BPS;\n    uint256 constant FLOW_SIZE_COEF = 4842 * BPS;\n    uint256 constant TOX_COEF = 250 * BPS;\n    uint256 constant TOX_QUAD_COEF = 11700 * 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 = 500 * BPS;\n    uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000; // 0.93\n    uint256 constant TAIL_SLOPE_ATTRACT = 955000000000000000; // 0.955\n\n    // slots[0] = bid fee\n    // slots[1] = ask fee\n    // slots[2] = last timestamp\n    // slots[3] = dirState (centered at WAD, [0, 2*WAD])\n    // slots[4] = actEma\n    // slots[5] = pHat\n    // slots[6] = sigmaHat\n    // slots[7] = lambdaHat\n    // slots[8] = sizeHat\n    // slots[9] = toxEma\n    // slots[10] = stepTradeCount (raw integer)\n\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE;\n        slots[1] = BASE_FEE;\n        slots[2] = 0;\n        slots[3] = WAD; // neutral direction\n        slots[4] = 0;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000; // 0.095% initial sigma guess\n        slots[7] = 800000000000000000; // 0.8 initial arrival-rate guess\n        slots[8] = 2000000000000000; // 0.2% reserve-size ratio guess\n        slots[9] = 0;\n        slots[10] = 0;\n        return (BASE_FEE, BASE_FEE);\n    }\n\n    function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) {\n        uint256 prevBidFee = slots[0];\n        uint256 prevAskFee = slots[1];\n        uint256 lastTs = slots[2];\n        uint256 dirState = slots[3];\n        uint256 actEma = slots[4];\n        uint256 pHat = slots[5];\n        uint256 sigmaHat = slots[6];\n        uint256 lambdaHat = slots[7];\n        uint256 sizeHat = slots[8];\n        uint256 toxEma = slots[9];\n        uint256 stepTradeCount = slots[10];\n\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\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\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\n            stepTradeCount = 0;\n        }\n\n        bool firstInStep = stepTradeCount == 0;\n\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) {\n            pImplied = spot;\n        } else {\n            pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma);\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 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT);\n            if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE;\n            if (ret <= adaptiveGate) {\n                pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha);\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        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\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\n            actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY);\n\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 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\n        fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal));\n\n        // Cubic toxicity\n        {\n            uint256 toxCubed = wmul(toxSignal, wmul(toxSignal, toxSignal));\n            fMid = fMid + wmul(TOX_CUBIC_COEF, toxCubed);\n        }\n\n        uint256 dirDev;\n        bool sellPressure;\n        if (dirState >= WAD) {\n            dirDev = dirState - WAD;\n            sellPressure = true;\n        } else {\n            dirDev = WAD - dirState;\n            sellPressure = false;\n        }\n\n        uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal));\n\n        uint256 bidFee;\n        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        // Directional protection using stale-price sign\n        {\n            uint256 staleShift = wmul(STALE_DIR_COEF, toxSignal);\n            uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC);\n            if (spot >= pHat) {\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        // Trade-aligned toxicity boost\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 (trade.isBuy) {\n                    bidFee = bidFee + tradeBoost;\n                } else {\n                    askFee = askFee + tradeBoost;\n                }\n            }\n        }\n\n        // Asymmetric tail compression\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        slots[0] = bidFee;\n        slots[1] = askFee;\n        slots[2] = trade.timestamp;\n        slots[3] = dirState;\n        slots[4] = actEma;\n        slots[5] = pHat;\n        slots[6] = sigmaHat;\n        slots[7] = lambdaHat;\n        slots[8] = sizeHat;\n        slots[9] = toxEma;\n        slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\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) {\n            return WAD + wmul(centered - WAD, mul);\n        }\n        uint256 below = wmul(WAD - centered, mul);\n        return below < WAD ? WAD - below : 0;\n    }\n\n    function getName() external pure override returns (string memory) {\n        return \"yq\";\n    }\n}\n",
 "code_sha256": "f619d00208130378c32cbb632f6ee08c8d0e048c6173a5dd42ccb40f445dc3a4",
 "delta": "From YQ 00fae418: change line 25 `uint256 constant GATE_SIGMA_MULT = 10 * WAD;` to `uint256 constant GATE_SIGMA_MULT = 5 * WAD;`. Nothing else.",
 "score": {
  "n": 300,
  "mean_edge": 524.264,
  "se": null,
  "seed_start": 400000,
  "note": "Seeds 0..999 not scored: out of time (1 CPU, ~0.6 s/sim). Reported mean is on fresh block 400000..400299; YQ on same seeds 524.027. se of absolute mean not computed."
 },
 "paired": {
  "reference": "YQ 00fae418",
  "seed_start": 400000,
  "n": 300,
  "mean_diff": 0.2374,
  "se": 0.037,
  "worst": [
   {
    "seed": 400034,
    "diff": -2.234
   },
   {
    "seed": 400261,
    "diff": -1.929
   },
   {
    "seed": 400181,
    "diff": -1.848
   },
   {
    "seed": 400007,
    "diff": -1.674
   },
   {
    "seed": 400164,
    "diff": -1.54
   }
  ],
  "note": "n=300 not the requested 2000: a 600-seed run hit the 10-min command timeout; machine limited to 1 CPU."
 },
 "seconds_per_simulation": 0.612,
 "validate_ok": true,
 "blocked": null,
 "citations": [
  "https://github.com/benedictbrady/amm-challenge",
  "https://raw.githubusercontent.com/jiayaoqijia/amm-challenge-yq/3e9dc7d5a6192c88b562a0f8d9c172ca566ec94d/contracts/src/Strategy.sol"
 ]
}