{
 "approach": "Simplify NP by deleting SIGMA_TOX_COEF and its fMid term and the dir skew (DIR_COEF, DIR_TOX_COEF, and the bid/ask skew branch), then retune STALE_DIR_COEF. dirState stays because sellPressure still picks the tail-compression slope. Selection on 30000..31999 chose STALE_DIR_COEF=6900 by highest mean; 6900 beat the unchanged 6850 by 0.0319 se 0.0099 on those seeds. One frozen file was then measured once on fresh seeds. Reproduced first on seeds 0..999: CH 525.5258821780834, NP 526.0337302497852, both matching the brief. Deviation from the planned grid: the first 300-step grid showed the coefficient is far steeper than that spacing resolves (6250: -1.93, 7150: -0.29), so the 5-point grid actually used for selection is 6750, 6800, 6850, 6900, 6950. The coarse points 6550 and 7450 were never run. All cells used the same seeds 30000..31999, n=2000 each. Tail warning: the spread of paired differences grows with the coefficient (se 0.0047 at 6850, 0.0108 at 6900, 0.0163 at 6950, 0.0477 at 7150), so values above 6900 pick up large losses in a few runs; 6950 tied 6900 on mean with a fatter tail. Gas: the executor's gas_used field is a stub (limit/2), so gas was bisected on the minimum passing limit over a hand-built 7-call afterSwap sequence, not over full simulations: candidate about 110909 and NP about 114191 against the 250000 limit. seconds_per_simulation is wall-clock per simulation on 4 cores with 4 workers. Seeds 900000..909999 were not touched.",
 "tried": [
  {
   "variant": "NP-sigmaTox",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": 0.0005152506845752783,
   "se": 0.0013651816738014355
  },
  {
   "variant": "NP-dirSkew",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": 0.02206095511824722,
   "se": 0.005289229427372105
  },
  {
   "variant": "NP-sigmaTox-dirSkew,STALE=6850",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": 0.0239517221986857,
   "se": 0.00474662765357198
  },
  {
   "variant": "NP-sigmaTox-dirSkew,STALE=6900",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": 0.05583380492228025,
   "se": 0.010803925724817583
  },
  {
   "variant": "NP-sigmaTox-dirSkew,STALE=6800",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": -0.02735758394917613,
   "se": 0.005671436372297666
  },
  {
   "variant": "NP-sigmaTox-dirSkew,STALE=6950",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": 0.05515273904324968,
   "se": 0.01633246074005449
  },
  {
   "variant": "NP-sigmaTox-dirSkew,STALE=6750",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": -0.10747787337015777,
   "se": 0.008493790331229151
  },
  {
   "variant": "NP-sigmaTox-dirSkew,STALE=7150",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": -0.2855059505400698,
   "se": 0.047732370401082844
  },
  {
   "variant": "NP-sigmaTox-dirSkew,STALE=6250",
   "seeds": "30000..31999",
   "n": 2000,
   "mean_diff": -1.93309220229079,
   "se": 0.02625580722237213
  },
  {
   "variant": "frozen candidate = NP-sigmaTox-dirSkew,STALE=6900, dead dirDev removed, _powWad unrolled (edges identical to the 6900 cell, max abs diff 0.0)",
   "seeds": "30000..30199",
   "n": 200,
   "mean_diff": 0.0329293873039336,
   "se": null
  }
 ],
 "next": "Promotion candidate: the fresh-seed gain clears twice its se. Not tested here: retuning STALE_ATTRACT_FRAC alongside STALE_DIR_COEF, which may address the tail growth above 6900; and removing dirState by fixing one tail slope, the remaining use of the direction state. Replicate on separate fresh seeds before relying on the exact gain size, since the file carries one selected coefficient.",
 "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 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 STALE_DIR_COEF = 6900 * 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[2] = 0; slots[3] = WAD; slots[4] = 0;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000;\n        slots[9] = 0; 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]; uint256 prevAskFee = slots[1]; uint256 lastTs = 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        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            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        {\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) { pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); }\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        if(!firstInStep&&feeUsed>30*BPS&&gamma>0){uint256 nS=trade.isBuy?wdiv(wmul(spot,gamma),WAD-30*BPS):wdiv(wmul(spot,WAD-30*BPS),gamma);pHat=wmul(pHat,9*WAD/10)+wmul(nS,WAD/10);}\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 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        bool sellPressure = dirState >= WAD;\n        uint256 bidFee = fMid; uint256 askFee = fMid;\n\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        {\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) { 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        slots[0] = bidFee; slots[1] = askFee; slots[2] = 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    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        if (exp & 1 != 0) result = wmul(result, factor);\n        factor = wmul(factor, factor);\n        if (exp & 2 != 0) result = wmul(result, factor);\n        factor = wmul(factor, factor);\n        if (exp & 4 != 0) result = wmul(result, factor);\n        factor = wmul(factor, factor);\n        if (exp & 8 != 0) result = wmul(result, factor);\n    }\n\n    function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) {\n        uint256 mul = _powWad(decayFactor, elapsed);\n        if (centered >= WAD) { return WAD + wmul(centered - WAD, mul); }\n        uint256 below = wmul(WAD - centered, mul);\n        return below < WAD ? WAD - below : 0;\n    }\n\n    function getName() external pure override returns (string memory) { return \"no_cubic\"; }\n}\n",
 "code_sha256": "2442a31062badec9ee5a392e6d5baf4f7edce2be21e64b980e422f3f0a3915c9",
 "delta": [
  "delete constant SIGMA_TOX_COEF and the line fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal))",
  "delete constants DIR_COEF and DIR_TOX_COEF and the skew computation; bidFee and askFee both start at fMid",
  "replace the dirDev/sellPressure branch with bool sellPressure = dirState >= WAD (behaviour-preserving)",
  "STALE_DIR_COEF 6850*BPS -> 6900*BPS",
  "unroll the bounded while loop in _powWad into 4 explicit squarings, valid because every caller passes elapsed <= ELAPSED_CAP = 8 (behaviour-preserving; done because the brief says no loops and NP inherits this loop from CH)"
 ],
 "score": {
  "seed_start": 0,
  "n": 1000,
  "mean_edge": 526.0898322135695,
  "se": 2.7911988257408225
 },
 "paired": {
  "reference": "NP",
  "seed_start": 800000,
  "n": 2000,
  "mean_diff": 0.08544055740758415,
  "se": 0.00859920220619589,
  "worst": [
   {
    "seed": 800107,
    "diff": -3.757678856233781
   },
   {
    "seed": 801417,
    "diff": -3.393084682258632
   },
   {
    "seed": 801130,
    "diff": -2.7309923441267756
   },
   {
    "seed": 800045,
    "diff": -2.4165416058738742
   },
   {
    "seed": 801284,
    "diff": -2.0678497506362987
   }
  ]
 },
 "seconds_per_simulation": 0.12815452647209166,
 "validate_ok": true,
 "blocked": null,
 "citations": [
  "https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol",
  "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/match.py",
  "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/evm/strategy.rs"
 ]
}