{
 "item": "a",
 "blocked": null,
 "n": "260",
 "scope": "Gas envelope only. Weights are random nonzero signed fixed-point (seeded), not trained; edges are reported only as a side-effect and carry no performance meaning. No performance-gain claim.",
 "calibration": {
  "source": "https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol",
  "sha256_fetched": "6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3",
  "seeds": "0..999",
  "mean": "525.5258821780834",
  "expected": "525.5258821780834",
  "abs_diff": "0.0",
  "pass": true
 },
 "protocol": "H.harness_source configs (size,rate,sigma draw order; 10000 steps; price seed=s, retail s+1); each arm alone vs VanillaStrategy via amm_sim_rs.run_batch(X,VanillaStrategy,configs,8); chunks of 20; gas_stats_reset() before and gas_stats() after each chunk, aggregated per arm (sum counters, max of max_ok_gas). Seeds 726100..726199 (12-16-2) and 726100..726139 (others) only; no seed >=5000000 used.",
 "deviation": "Brief allowed 726100..726611; only the first 100/40 seeds were run because of the time budget (the 12-16-2 arm took 3.5 s/seed). Smaller samples => observed maxima are weaker evidence; they are not a proof of worst-case safety.",
 "rows": [
  {
   "architecture": "RS2",
   "file": "RS2.sol",
   "sha256": "f24b6ca198b744c380842c3a17964a6dc21f311bb04984b26733afa3fee8eaf2",
   "inputs": null,
   "hidden": null,
   "weights": null,
   "biases": null,
   "MACs": "0",
   "deployed_bytes": "9822",
   "seeds": "726100..726139",
   "n_seeds": "40",
   "gas": {
    "max_ok": "202387",
    "mean_ok": "117940",
    "calls": "409914",
    "init_max_ok": "243099",
    "oog": "0",
    "other_halts": "0"
   },
   "valid": true,
   "note": "original RS2 (old fee mapping intact), reference only"
  },
  {
   "architecture": "skeleton",
   "file": "skeleton.sol",
   "sha256": "2e7731d6fdc0b96c783b38fb14dcdf17b03470acd6cff306cf05ae4ba55527cf",
   "inputs": null,
   "hidden": null,
   "weights": null,
   "biases": null,
   "MACs": "0",
   "deployed_bytes": "8119",
   "seeds": "726100..726139",
   "n_seeds": "40",
   "gas": {
    "max_ok": "199093",
    "mean_ok": "106123",
    "calls": "454192",
    "init_max_ok": "243084",
    "oog": "0",
    "other_halts": "0"
   },
   "valid": true,
   "note": "RS2 estimators/state kept, fee mapping removed, fixed 30 bps written to slots 0/1"
  },
  {
   "architecture": "8-8-2",
   "file": "head_8_8_2.sol",
   "sha256": "b3b1f0367f43bc6729a8feee6739d254e8c4c4e894dad386cd1a446c614a3587",
   "inputs": "8",
   "hidden": "8",
   "weights": "80",
   "biases": "10",
   "MACs": "80",
   "deployed_bytes": "12075",
   "seeds": "726100..726139",
   "n_seeds": "40",
   "gas": {
    "max_ok": "215663",
    "mean_ok": "134951",
    "calls": "345465",
    "init_max_ok": "243081",
    "oog": "0",
    "other_halts": "0"
   },
   "valid": true,
   "note": null,
   "gas_per_MAC_over_skeleton_max": "207.1"
  },
  {
   "architecture": "10-12-2",
   "file": "head_10_12_2.sol",
   "sha256": "6a39c8b1ee9b6420e1aae979882026595a112a3da295fdbf60d25b8e003c6b31",
   "inputs": "10",
   "hidden": "12",
   "weights": "144",
   "biases": "14",
   "MACs": "144",
   "deployed_bytes": "14701",
   "seeds": "726100..726139",
   "n_seeds": "40",
   "gas": {
    "max_ok": "227348",
    "mean_ok": "140519",
    "calls": "458741",
    "init_max_ok": "243093",
    "oog": "0",
    "other_halts": "0"
   },
   "valid": true,
   "note": null,
   "gas_per_MAC_over_skeleton_max": "196.2"
  },
  {
   "architecture": "12-16-2",
   "file": "head_12_16_2.sol",
   "sha256": "e20a107ac7b14207fc97f08c4049ae4b55301fb331b308625a11d0fe9193d66f",
   "inputs": "12",
   "hidden": "16",
   "weights": "224",
   "biases": "18",
   "MACs": "224",
   "deployed_bytes": "18311",
   "seeds": "726100..726199",
   "n_seeds": "100",
   "gas": {
    "max_ok": "242794",
    "mean_ok": "156724",
    "calls": "1156460",
    "init_max_ok": "243103",
    "oog": "0",
    "other_halts": "0"
   },
   "valid": false,
   "note": "no OOG/halt observed, but afterSwap max 242794 exceeds the 235000 recommendation bound (7.2k below the 250000 limit): NOT recommended",
   "gas_per_MAC_over_skeleton_max": "195.1"
  }
 ],
 "head_design": {
  "inputs_live_causal": [
   "sigmaHat*500",
   "lambdaHat (retail-blended)",
   "sizeHat*100 (retail-blended)",
   "toxEma*200",
   "dirState-WAD (signed)",
   "signed (spot-staleHat)/staleHat*200",
   "signed tradeRatio*100 (by isBuy)",
   "arbProbability",
   "firstInStep",
   "uncertainty*1e5",
   "min(silent,8)/8",
   "(12th) min(silent,8)/8 is the 12th; 8-input head uses first 8, 10-input first 10"
  ],
  "feature_clamp": "each input clamped to [-4e18,4e18]",
  "hidden": "h_j = min(max(sum_i x_i*W_ij/1e6 + b_j, 0), 4e18) (clamped ReLU)",
  "output": "o_k = sum_j h_j*V_jk/1e6 + c_k, fee_k = clamp(o_k, 0, MAX_FEE); fee_0=bid, fee_1=ask stored in slots[0]/slots[1] and returned",
  "weights": "compile-time literals in bytecode (unrolled), |W| in [1e5,1e6], |V| in [20,300], all nonzero, both signs; no new afterInitialize writes; no new slots (RS2 slots 0..15 only)",
  "arithmetic": "Solidity 0.8 checked int256 (overflow checks retained => conservative gas); memory arrays for x and h"
 },
 "arithmetic_tests": {
  "head": {
   "head_8_8_2": {
    "cases": 419,
    "mismatches": 0,
    "hidden_at_cap": 515,
    "hidden_at_zero": 1663,
    "outputs_at_zero": 0,
    "outputs_at_MAX_FEE": 0,
    "W_neg": 27,
    "W_pos": 37,
    "zero_weights": 0,
    "max_abs_preact_numerator": "16102105051085205882294514",
    "overflow_bound": "|x|<=4e18,|W|<=1e6 => |sum|<= 8*4e24 << 2^255; |h|<=4e18,|V|<=300 => <= 8*1.2e21"
   },
   "head_10_12_2": {
    "cases": 423,
    "mismatches": 0,
    "hidden_at_cap": 1047,
    "hidden_at_zero": 2476,
    "outputs_at_zero": 3,
    "outputs_at_MAX_FEE": 0,
    "W_neg": 65,
    "W_pos": 55,
    "zero_weights": 0,
    "max_abs_preact_numerator": "21085032000000000000000000",
    "overflow_bound": "|x|<=4e18,|W|<=1e6 => |sum|<= 10*4e24 << 2^255; |h|<=4e18,|V|<=300 => <= 12*1.2e21"
   },
   "head_12_16_2": {
    "cases": 427,
    "mismatches": 0,
    "hidden_at_cap": 1406,
    "hidden_at_zero": 3424,
    "outputs_at_zero": 66,
    "outputs_at_MAX_FEE": 0,
    "W_neg": 97,
    "W_pos": 95,
    "zero_weights": 0,
    "max_abs_preact_numerator": "19989159638437463845487820",
    "overflow_bound": "|x|<=4e18,|W|<=1e6 => |sum|<= 12*4e24 << 2^255; |h|<=4e18,|V|<=300 => <= 16*1.2e21"
   }
  },
  "sequence": {
   "skeleton.sol": {
    "calls": 8,
    "returns_equal_slots01": true,
    "fees_bps": [
     [
      30.0,
      30.0
     ],
     [
      30.0,
      30.0
     ],
     [
      30.0,
      30.0
     ],
     [
      30.0,
      30.0
     ],
     [
      30.0,
      30.0
     ],
     [
      30.0,
      30.0
     ],
     [
      30.0,
      30.0
     ],
     [
      30.0,
      30.0
     ]
    ]
   },
   "head_8_8_2.sol": {
    "calls": 8,
    "returns_equal_slots01": true,
    "fees_bps": [
     [
      25.34442986119126,
      46.68096073041925
     ],
     [
      31.14583607813392,
      46.28658672125409
     ],
     [
      26.47685886819436,
      48.45235623632738
     ],
     [
      28.55098235277087,
      47.28224132184572
     ],
     [
      26.31847284955642,
      50.394533792999
     ],
     [
      27.17883160458362,
      46.45827117168979
     ],
     [
      26.32735046751187,
      50.38132132434429
     ],
     [
      27.18185876418196,
      44.78197361276596
     ]
    ]
   },
   "head_10_12_2.sol": {
    "calls": 8,
    "returns_equal_slots01": true,
    "fees_bps": [
     [
      20.59116559586602,
      9.95066558698719
     ],
     [
      22.61985901190003,
      20.68364250879494
     ],
     [
      22.96943901307536,
      4.77801130314437
     ],
     [
      23.815711805279,
      12.15466384462557
     ],
     [
      20.70833341114755,
      4.26979588517242
     ],
     [
      26.61008603127265,
      11.63776315524098
     ],
     [
      20.77661947243491,
      4.44889727354077
     ],
     [
      26.49826238710246,
      12.46521094967189
     ]
    ]
   },
   "head_12_16_2.sol": {
    "calls": 8,
    "returns_equal_slots01": true,
    "fees_bps": [
     [
      25.32031384339941,
      23.40434605646243
     ],
     [
      25.55561408880737,
      22.62328627969499
     ],
     [
      24.84646498890879,
      25.27659145579098
     ],
     [
      23.8806891299828,
      24.14579180125648
     ],
     [
      24.60194859482502,
      24.9489944628965
     ],
     [
      21.23053405692212,
      26.83202519062736
     ],
     [
      23.912035615446,
      25.28686445020079
     ],
     [
      20.03599894982244,
      28.83127650013262
     ]
    ]
   }
  }
 },
 "overflow_bounds": "Layer1 |numerator| <= n_in*4e18*1e6 <= 4.8e25; Layer2 |numerator| <= n_h*4e18*300 <= 1.92e22; both far below 2^255 ~ 5.8e76. Observed max layer-1 numerator 2.1e25.",
 "state_access": "Only slots[0..15] read/written (same set as RS2); afterInitialize unchanged from RS2.",
 "largest_safe_head": {
  "architecture": "10-12-2",
  "sha256": "6a39c8b1ee9b6420e1aae979882026595a112a3da295fdbf60d25b8e003c6b31",
  "afterSwap_max_ok": "227348",
  "margin_to_235000": "7652",
  "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 staleHat = wmul(pHat, 700000000000000000) + wmul(fastHat, 300000000000000000);\n        int256[10] memory xin;\n        xin[0] = _ci(sigmaHat, 500);\n        xin[1] = _ci(lambdaHat, 1);\n        xin[2] = _ci(sizeHat, 100);\n        xin[3] = _ci(toxSignal, 200);\n        xin[4] = _ci(actEma, 200);\n        xin[5] = (dirState >= WAD ? _ci(dirState - WAD, 1) : -_ci(WAD - dirState, 1));\n        xin[6] = (spot >= staleHat ? _ci(wdiv(spot - staleHat, staleHat), 200) : -_ci(wdiv(staleHat - spot, staleHat), 200));\n        xin[7] = (trade.isBuy ? _ci(tradeRatio, 100) : -_ci(tradeRatio, 100));\n        xin[8] = _ci(arbProbability, 1);\n        xin[9] = (firstInStep ? int256(WAD) : int256(0));\n        (uint256 bidFee, uint256 askFee) = _head(xin);\n        lambdaHat = originalLambda; sizeHat = originalSize;\n        slots[0] = bidFee; slots[1] = askFee; slots[2] = (spot << 64) | trade.timestamp;\n        slots[3] = dirState; slots[4] = actEma; slots[5] = pHat;\n        slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat;\n        slots[9] = toxEma; slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\n    }\n\n\n    struct PosteriorInput {\n        uint256 mu; uint256 v; uint256 obs; uint256 spot; uint256 preSpot;\n        uint256 bid; uint256 ask; bool first; uint256 silent; bool buy; uint256 size;\n    }\n    function _posterior(PosteriorInput memory x) internal pure returns (uint256, uint256, uint256) {\n        if (x.mu == 0) x.mu = x.spot;\n        if (x.v < 10000000000) x.v = 10000000000;\n        uint256 lo = wmul(x.preSpot, WAD-x.bid);\n        uint256 hi = wdiv(x.preSpot, WAD-x.ask);\n        if (x.first) {\n            if (x.silent>1 && x.preSpot>0) {\n                uint256 elapsed=x.silent>8?8:x.silent;\n                x.v += 902500000000*(elapsed-1);\n                (x.mu,x.v)=_band(x.mu,x.v,lo,hi);\n            }\n            x.v+=902500000000;\n        }\n        uint256 mRetail = x.mu;\n        uint256 vRetail = x.v;\n        // A missing own-pool trade is a censored price observation.\n        if (x.first && x.preSpot > 0) {\n            (mRetail, vRetail) = _band(x.mu, x.v, lo, hi);\n        }\n        if (!x.first) {\n            // A later print is retail. Only an interior route is an observation\n            // of the other pool, with explicit observation uncertainty.\n            uint256 fee = x.buy ? x.bid : x.ask;\n            if (fee > 30 * BPS) {\n                uint256 normal = x.buy ? wdiv(wmul(x.spot, WAD-fee), WAD-30*BPS)\n                    : wdiv(wmul(x.spot, WAD-30*BPS), WAD-fee);\n                uint256 residual = wdiv(absDiff(normal, x.mu),x.mu);\n                uint256 noise = 9000000000000 + wmul(residual,residual);\n                uint256 k = wdiv(x.v,x.v+noise);\n                return (_blend(x.mu,normal,k),wmul(x.v,WAD-k),0);\n            }\n            return (x.mu,x.v,0);\n        }\n        uint256 e = wdiv(absDiff(x.obs,x.mu),x.mu);\n        uint256 totalV = x.v + 22500000000;\n        uint256 z = wdiv(wmul(e,e),totalV);\n        uint256 density = _powWad(wdiv(WAD,WAD+z/8),4);\n        uint256 odds = wmul(density,wdiv(4000000000000000,_isqrt(totalV*WAD)));\n        if (x.size > 3000000000000000) odds = wmul(odds,4*WAD/10);\n        if (x.silent > 1) odds = wmul(odds,8*WAD/10);\n        uint256 sd = _isqrt(x.v*WAD);\n        uint256 width = wdiv(hi-lo,x.mu);\n        uint256 distance = x.mu<lo?lo-x.mu:(x.mu>hi?x.mu-hi:0);\n        uint256 zBand = wdiv(wdiv(distance,x.mu),sd);\n        uint256 mass = wmul(wdiv(width,width+2*sd),wdiv(WAD,WAD+wmul(zBand,zBand)));\n        if (mass < WAD/100) mass = WAD/100;\n        odds = wdiv(odds,mass);\n        uint256 prob = wdiv(odds,WAD+odds);\n        uint256 kArb = wdiv(x.v,totalV);\n        uint256 mArb = _blend(x.mu,x.obs,kArb);\n        uint256 mean = _blend(mRetail,mArb,prob);\n        uint256 between = wdiv(absDiff(mArb,mRetail),x.mu);\n        uint256 variance = wmul(prob,wmul(x.v,WAD-kArb)) + wmul(WAD-prob,vRetail)\n            + wmul(wmul(prob,WAD-prob),wmul(between,between));\n        if (variance > 100000000000000) variance = 100000000000000;\n        return (mean,variance,prob);\n    }\n    function _blend(uint256 a,uint256 b,uint256 w) internal pure returns(uint256) {\n        return b>=a ? a+wmul(b-a,w) : a-wmul(a-b,w);\n    }\n    function _band(uint256 mu,uint256 v,uint256 lo,uint256 hi) internal pure returns(uint256,uint256) {\n        // Soft interval likelihood avoids discontinuities at quadrature nodes.\n        uint256 sd=wmul(mu,_isqrt(v*WAD));\n        uint256[3] memory acc;\n        for(uint256 j=0;j<7;j++) {\n            uint256 p=j<3 ? (mu>(3-j)*sd?mu-(3-j)*sd:1) : mu+(j-3)*sd;\n            uint256 w=j==3?72:(j==2||j==4?45:(j==1||j==5?10:1));\n            uint256 distance=p<lo?lo-p:(p>hi?p-hi:0);\n            uint256 z=sd>0?wdiv(distance,sd):0;\n            uint256 likelihood=wdiv(WAD,WAD+4*wmul(z,z));\n            w=wmul(w*WAD,likelihood);\n            acc[0]+=w;acc[1]+=wmul(w,p);\n            uint256 d=wdiv(absDiff(p,mu),mu);\n            acc[2]+=wmul(w,wmul(d,d));\n        }\n        uint256 m=wdiv(acc[1],acc[0]);\n        uint256 d=wdiv(absDiff(m,mu),mu);\n        uint256 second=wdiv(acc[2],acc[0]);\n        return(m,second>wmul(d,d)?second-wmul(d,d):10000000000);\n    }\n\n    function _compressTailWithSlope(uint256 fee, uint256 slope) internal pure returns (uint256) {\n        if (fee <= TAIL_KNEE) return fee;\n        return TAIL_KNEE + wmul(fee - TAIL_KNEE, slope);\n    }\n\n    function _powWad(uint256 factor, uint256 exp) internal pure returns (uint256 result) {\n        result = WAD;\n        while (exp > 0) {\n            if (exp & 1 == 1) result = wmul(result, factor);\n            factor = wmul(factor, factor);\n            exp >>= 1;\n        }\n    }\n\n    function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) {\n        uint256 mul = _powWad(decayFactor, elapsed);\n        if (centered >= WAD) { return WAD + wmul(centered - WAD, mul); }\n        uint256 below = wmul(WAD - centered, mul);\n        return below < WAD ? WAD - below : 0;\n    }\n\n    // Floor square root: bit-length seed and seven Newton steps (all uint256).\n    function _isqrt(uint256 x) internal pure returns (uint256 r) {\n        if (x == 0) return 0;\n        unchecked {\n            uint256 y = x;\n            r = 1;\n            if (y >> 128 != 0) { y >>= 128; r <<= 64; }\n            if (y >> 64 != 0) { y >>= 64; r <<= 32; }\n            if (y >> 32 != 0) { y >>= 32; r <<= 16; }\n            if (y >> 16 != 0) { y >>= 16; r <<= 8; }\n            if (y >> 8 != 0) { y >>= 8; r <<= 4; }\n            if (y >> 4 != 0) { y >>= 4; r <<= 2; }\n            if (y >> 2 != 0) { r <<= 1; }\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            r = (r + x / r) >> 1;\n            uint256 t = x / r;\n            return r < t ? r : t;\n        }\n    }\n\n    function _head(int256[10] memory x) internal pure returns (uint256 f0, uint256 f1) {\n        int256[12] memory h;\n        h[0] = _cr((x[0] * (501213) + x[1] * (596713) + x[2] * (700977) + x[3] * (-979482) + x[4] * (-768808) + x[5] * (-886004) + x[6] * (-907045) + x[7] * (-481381) + x[8] * (-496349) + x[9] * (-226556)) / 1000000 + (-160091283153628532));\n        h[1] = _cr((x[0] * (-934348) + x[1] * (924154) + x[2] * (-459016) + x[3] * (162259) + x[4] * (-401177) + x[5] * (718777) + x[6] * (-735143) + x[7] * (109971) + x[8] * (344553) + x[9] * (483843)) / 1000000 + (276965620261754602));\n        h[2] = _cr((x[0] * (-686453) + x[1] * (587926) + x[2] * (-636888) + x[3] * (750156) + x[4] * (-260434) + x[5] * (-591264) + x[6] * (-608793) + x[7] * (528715) + x[8] * (-751921) + x[9] * (-779255)) / 1000000 + (344437903923921674));\n        h[3] = _cr((x[0] * (-192650) + x[1] * (664901) + x[2] * (812780) + x[3] * (-399510) + x[4] * (-891023) + x[5] * (-514498) + x[6] * (-395226) + x[7] * (-682489) + x[8] * (984038) + x[9] * (-874277)) / 1000000 + (-387555377882260698));\n        h[4] = _cr((x[0] * (328691) + x[1] * (190501) + x[2] * (535429) + x[3] * (-796751) + x[4] * (-909482) + x[5] * (-615675) + x[6] * (678389) + x[7] * (-567984) + x[8] * (109833) + x[9] * (743770)) / 1000000 + (338586329731278966));\n        h[5] = _cr((x[0] * (-433489) + x[1] * (-787773) + x[2] * (-587855) + x[3] * (344048) + x[4] * (-606820) + x[5] * (556936) + x[6] * (757424) + x[7] * (739471) + x[8] * (-675974) + x[9] * (-882940)) / 1000000 + (90336059635894326));\n        h[6] = _cr((x[0] * (455254) + x[1] * (772142) + x[2] * (711087) + x[3] * (613159) + x[4] * (267198) + x[5] * (435769) + x[6] * (370703) + x[7] * (-164115) + x[8] * (122234) + x[9] * (242942)) / 1000000 + (76605925815489686));\n        h[7] = _cr((x[0] * (183732) + x[1] * (-598380) + x[2] * (-399251) + x[3] * (-615784) + x[4] * (746891) + x[5] * (-548600) + x[6] * (319697) + x[7] * (-414016) + x[8] * (815996) + x[9] * (271435)) / 1000000 + (242281904470082615));\n        h[8] = _cr((x[0] * (-290350) + x[1] * (-496002) + x[2] * (-985753) + x[3] * (143375) + x[4] * (-377539) + x[5] * (-842874) + x[6] * (-701762) + x[7] * (863014) + x[8] * (-886370) + x[9] * (550772)) / 1000000 + (-147610742867949437));\n        h[9] = _cr((x[0] * (-329248) + x[1] * (699492) + x[2] * (-943036) + x[3] * (-301355) + x[4] * (-366253) + x[5] * (827539) + x[6] * (-215914) + x[7] * (-612667) + x[8] * (-950926) + x[9] * (254217)) / 1000000 + (123978846989533189));\n        h[10] = _cr((x[0] * (567519) + x[1] * (-755145) + x[2] * (-184175) + x[3] * (800490) + x[4] * (-167622) + x[5] * (-316948) + x[6] * (-627267) + x[7] * (916469) + x[8] * (495744) + x[9] * (-794727)) / 1000000 + (406228636259939657));\n        h[11] = _cr((x[0] * (978856) + x[1] * (460155) + x[2] * (-790421) + x[3] * (-920085) + x[4] * (434616) + x[5] * (646484) + x[6] * (252200) + x[7] * (911873) + x[8] * (-756203) + x[9] * (-533491)) / 1000000 + (95347749421075811));\n        int256 o0 = (h[0] * (229) + h[1] * (246) + h[2] * (88) + h[3] * (144) + h[4] * (-90) + h[5] * (-81) + h[6] * (-89) + h[7] * (-189) + h[8] * (-193) + h[9] * (-269) + h[10] * (-217) + h[11] * (-145)) / 1000000 + (2975892514950709);\n        f0 = o0 <= 0 ? 0 : (o0 >= int256(MAX_FEE) ? MAX_FEE : uint256(o0));\n        int256 o1 = (h[0] * (-77) + h[1] * (-215) + h[2] * (-147) + h[3] * (117) + h[4] * (221) + h[5] * (127) + h[6] * (-191) + h[7] * (26) + h[8] * (-226) + h[9] * (135) + h[10] * (-91) + h[11] * (145)) / 1000000 + (2913544237355247);\n        f1 = o1 <= 0 ? 0 : (o1 >= int256(MAX_FEE) ? MAX_FEE : uint256(o1));\n    }\n    function _cr(int256 v) internal pure returns (int256) { return v <= 0 ? int256(0) : (v >= 4000000000000000000 ? int256(4000000000000000000) : v); }\n    function _ci(uint256 v, uint256 m) internal pure returns (int256) { return v >= 4000000000000000000 / m ? int256(4000000000000000000) : int256(v * m); }\n\n    function getName() external pure override returns (string memory) { return \"censored_10\"; }\n}\n"
 },
 "generator": {
  "sha256": "c7dcd932e9e3ff601298e247ee4f8cc02edf95907688d6ca23114569d7175c41",
  "source": "\"\"\"Generate RS2-estimator + fixed-point clamped-ReLU head strategies.\nusage: python gen.py RS2.sol outdir\nWrites skeleton.sol, head_{I}_{H}_2.sol, headtest_{I}_{H}.sol, weights.json\"\"\"\nimport sys, json, random, hashlib\nfrom pathlib import Path\n\nWAD = 10**18\nSCALE = 10**6          # weight denominator\nHCAP = 4 * WAD         # clamped-ReLU ceiling, also input clamp\nARCHS = [(8, 8), (10, 12), (12, 16)]\n\nSTART = \"        uint256 flowSize = wmul(lambdaHat, sizeHat);\"\nEND = \"        slots[0] = bidFee; slots[1] = askFee;\"\n\n# live causal features (all computed in afterSwap from current trade + stored state)\nFEATS = [\n    \"_ci(sigmaHat, 500)\",\n    \"_ci(lambdaHat, 1)\",\n    \"_ci(sizeHat, 100)\",\n    \"_ci(toxSignal, 200)\",\n    \"_ci(actEma, 200)\",\n    \"(dirState >= WAD ? _ci(dirState - WAD, 1) : -_ci(WAD - dirState, 1))\",\n    \"(spot >= staleHat ? _ci(wdiv(spot - staleHat, staleHat), 200) : -_ci(wdiv(staleHat - spot, staleHat), 200))\",\n    \"(trade.isBuy ? _ci(tradeRatio, 100) : -_ci(tradeRatio, 100))\",\n    \"_ci(arbProbability, 1)\",\n    \"(firstInStep ? int256(WAD) : int256(0))\",\n    \"_ci(uncertainty, 100000)\",\n    \"_ci((silent > 8 ? 8 : silent) * WAD, 1) / 8\",\n]\n\n\ndef weights(nin, nh, seed):\n    rng = random.Random(seed)\n    nz = lambda lo, hi: rng.choice([-1, 1]) * rng.randint(lo, hi)\n    W = [[nz(100_000, 1_000_000) for _ in range(nh)] for _ in range(nin)]\n    B = [nz(10**16, 5 * 10**17) for _ in range(nh)]\n    V = [[nz(20, 300) for _ in range(2)] for _ in range(nh)]\n    C = [30 * 10**14 + nz(10**13, 10**14) for _ in range(2)]\n    return W, B, V, C\n\n\ndef head_fn(nin, nh, W, B, V, C):\n    L = [f\"    function _head(int256[{nin}] memory x) internal pure returns (uint256 f0, uint256 f1) {{\",\n         f\"        int256[{nh}] memory h;\"]\n    for j in range(nh):\n        terms = \" + \".join(f\"x[{i}] * ({W[i][j]})\" for i in range(nin))\n        L.append(f\"        h[{j}] = _cr(({terms}) / {SCALE} + ({B[j]}));\")\n    for k in range(2):\n        terms = \" + \".join(f\"h[{j}] * ({V[j][k]})\" for j in range(nh))\n        L.append(f\"        int256 o{k} = ({terms}) / {SCALE} + ({C[k]});\")\n        L.append(f\"        f{k} = o{k} <= 0 ? 0 : (o{k} >= int256(MAX_FEE) ? MAX_FEE : uint256(o{k}));\")\n    L.append(\"    }\")\n    L.append(f\"    function _cr(int256 v) internal pure returns (int256) {{ return v <= 0 ? int256(0) : (v >= {HCAP} ? int256({HCAP}) : v); }}\")\n    L.append(f\"    function _ci(uint256 v, uint256 m) internal pure returns (int256) {{ return v >= {HCAP} / m ? int256({HCAP}) : int256(v * m); }}\")\n    return \"\\n\".join(L)\n\n\ndef body(nin):\n    L = [\"        uint256 staleHat = wmul(pHat, 700000000000000000) + wmul(fastHat, 300000000000000000);\",\n         f\"        int256[{nin}] memory xin;\"]\n    for i in range(nin):\n        L.append(f\"        xin[{i}] = {FEATS[i]};\")\n    L.append(\"        (uint256 bidFee, uint256 askFee) = _head(xin);\")\n    return \"\\n\".join(L) + \"\\n\"\n\n\ndef split(src):\n    a = src.index(START); b = src.index(END)\n    return src[:a], src[b:]\n\n\ndef main():\n    src = Path(sys.argv[1]).read_text(); out = Path(sys.argv[2]); out.mkdir(exist_ok=True)\n    pre, post = split(src)\n    # the estimator block blended lambdaHat/sizeHat for the old fee then restored them; keep restore semantics\n    tail_fix = \"        lambdaHat = originalLambda; sizeHat = originalSize;\\n\"\n    close = \"\\n    function getName()\"\n    skel = pre + \"        uint256 bidFee = 30 * BPS; uint256 askFee = 30 * BPS;\\n\" + tail_fix + post\n    (out / \"skeleton.sol\").write_text(skel)\n    meta = {}\n    for nin, nh in ARCHS:\n        W, B, V, C = weights(nin, nh, 1000 * nin + nh)\n        s = pre + body(nin) + tail_fix + post\n        s = s.replace(close, \"\\n\" + head_fn(nin, nh, W, B, V, C) + \"\\n\" + close, 1)\n        name = f\"head_{nin}_{nh}_2\"\n        (out / f\"{name}.sol\").write_text(s)\n        t = (\"// SPDX-License-Identifier: MIT\\npragma solidity ^0.8.24;\\nimport {AMMStrategyBase} from \\\"./AMMStrategyBase.sol\\\";\\n\"\n             \"import {TradeInfo} from \\\"./IAMMStrategy.sol\\\";\\ncontract HT is AMMStrategyBase {\\n\"\n             f\"    function run(int256[{nin}] memory x) external pure returns (uint256, uint256) {{ return _head(x); }}\\n\"\n             \"    function afterInitialize(uint256, uint256) external pure override returns (uint256, uint256) { return (0,0); }\\n\"\n             \"    function afterSwap(TradeInfo calldata) external pure override returns (uint256, uint256) { return (0,0); }\\n\"\n             \"    function getName() external pure override returns (string memory) { return \\\"ht\\\"; }\\n\"\n             + head_fn(nin, nh, W, B, V, C) + \"\\n}\\n\")\n        (out / f\"headtest_{nin}_{nh}.sol\").write_text(t)\n        meta[name] = dict(inputs=nin, hidden=nh, outputs=2, W=W, B=B, V=V, C=C,\n                          weights=nin * nh + nh * 2, biases=nh + 2, MACs=nin * nh + nh * 2,\n                          sha256=hashlib.sha256(s.encode()).hexdigest())\n    (out / \"weights.json\").write_text(json.dumps(meta))\n\n\nif __name__ == \"__main__\":\n    main()\n"
 },
 "harness": {
  "harness_source_sha256": "b8a1e110d746241c7201c95dd7e519f1388efffe7eaa0badeda30c3474e2447b",
  "from": "https://api.imdnotdead.fun/artifacts/26cb8e86f05170308de2a4b24821ac7fad2a17077f8d94af6e0f31985343fe89 (harness_source)",
  "runner_note": "runner imported H.harness configs/aggregate, recorded only edges and gas (skipped arb/retail decomposition for speed)"
 },
 "throughput": {
  "RS2_s_per_seed": "1.72",
  "skeleton": "1.62",
  "8-8-2": "1.96",
  "10-12-2": "2.87",
  "12-16-2": "3.50",
  "note": "wall seconds per seed incl. Vanilla, 8 threads, 16-core host; heads with random weights also changed trade counts"
 },
 "recommendation": "Marginal cost about 195-207 gas per MAC (checked int256, memory arrays) over the estimators-only skeleton (afterSwap max 199093). Budget to 235000 leaves ~36k => <= ~180 MACs. 8-8-2 (80 MACs, max 215663) is the safe choice with ~19k margin; 10-12-2 (144 MACs, max 227348) passes the bound but with only 7.7k margin on 40 seeds; 12-16-2 (224 MACs, 242794) fails the bound. To go larger, use unchecked arithmetic / stack locals or drop RS2 _band quadrature cost; re-measure on the full 726100..726611 set before relying on 10-12-2. init max 243.1k for all arms incl. original RS2 (simulator-reported, not caused by the head) is under 245000 but has little margin."
}