{
 "credit": "Taylor (gtmcknight), github.com/gtmcknight/amm-sim@4812a6382311624e1506aca131ca35c378f930d9 research/solved: the solved policy, replica, Gaussian filter, qm tables and arb/retail tell that LEAN, AGE, QLX5 and this test are built on. Fast replica: fastlean (artifact a88ff982eba7e078bc6ccb1555b90a211c00c9fd76ec90a62ab13a4074f2c3d0), a port of Taylor's code.",
 "mech": "sync-grade: weight of the synced fee table graded by the age (in steps) of the last exact reading of the rival, in place of the synced flag",
 "already_present": {
  "value": false,
  "where": "Not in leanbase.py, stalerival.py, real.py or tellpol.py. real.RealPolicy.lookup picks the table by a flag: tag = str(phs) + ('s' if self.synced else 'c'). The only age-graded quantity is stalerival's pull strength a = a0 + (ainf - a0) * router_age / (router_age + tau), which moves the rival ESTIMATE s2h and then sets synced = True; it does not grade the table. leanbase.py and tellpol.py only decide arb/retail. So the mechanism itself was tested, not a neighbour.",
  "what_the_flag_is_under_lean": "Measured on LEAN and AGE, seeds 64000..64063: the flag is set on every callback not read as arbitrage (LEAN 449,443 of 449,443; AGE 449,712 of 449,712), because a callback either binds the router bound or is pulled, and both set the flag. The unsynced tables 1c and 2c are therefore never read, and a grade has something to act on only where the flag was set by a pull while the last exact reading is at least one step old: LEAN 44,506 of 690,591 callbacks (6.4%), AGE 44,111 of 691,059."
 },
 "definition": {
  "formula": "On a callback not read as arbitrage (phase 1 or 2), with u = t - t_exact: lam = 1 if the flag is set and u == 0; lam = linf + (l1 - linf) * gtau / (u - 1 + gtau) if the flag is set and u >= 1; lam = 0 if the flag is not set. Table values at LEAN's own (sd level, w, w2) position are blended, a = lam * a_s + (1 - lam) * a_c and b likewise (s = synced table, c = unsynced table of the same phase), then LEAN's fee map is applied once: fa = 1 - exp(-(a - w) * 1e-4) if a > w else 0, fb = 1 - exp(-(b + w) * 1e-4) if b > -w else 0. lam >= 1 or lam <= 0 reads one table, so l1 = linf = 1 is LEAN bit for bit. Phase 0, the filter, the tell, the rival estimate and its pull are untouched.",
  "parameters": {
   "l1": "weight of the synced table when the last exact reading is 1 step old",
   "linf": "weight as the age grows without bound",
   "gtau": "steps scale of the decay from l1 to linf (> 0)"
  },
  "state_kept": "One integer, t_exact: the step of the last callback on which the router bound bound (stalerival's `binding`, real.py's `s2h = cand`), -2^40 before the first. Inputs used: timestamp and the policy's own state only.",
  "baselines": {
   "LEAN": "a0=0.35 ainf=0.35",
   "AGE": "a0=0 ainf=0.7 tau=1",
   "stacked_on_AGE": "same grade with the AGE pull"
  }
 },
 "settings_tried": [
  {
   "parameters": {
    "l1": 1.0,
    "linf": 0.5,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.003844,
   "se": 0.002673,
   "seeds_positive": 542,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 1.0,
    "linf": 0.0,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.001535,
   "se": 0.002942,
   "seeds_positive": 530,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 1.0,
    "linf": 0.0,
    "gtau": 4.0
   },
   "n": 1024,
   "mean_diff": 0.002212,
   "se": 0.002833,
   "seeds_positive": 525,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 1.0,
    "linf": 0.5,
    "gtau": 4.0
   },
   "n": 1024,
   "mean_diff": -0.000196,
   "se": 0.002376,
   "seeds_positive": 535,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.75,
    "linf": 0.75,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.000498,
   "se": 0.002702,
   "seeds_positive": 526,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.5,
    "linf": 0.5,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.000687,
   "se": 0.003102,
   "seeds_positive": 536,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.25,
    "linf": 0.25,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.000801,
   "se": 0.003582,
   "seeds_positive": 534,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.0,
    "linf": 0.0,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.001993,
   "se": 0.003446,
   "seeds_positive": 516,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.5,
    "linf": 1.0,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.003141,
   "se": 0.002906,
   "seeds_positive": 522,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.0,
    "linf": 1.0,
    "gtau": 1.0
   },
   "n": 1024,
   "mean_diff": 0.001629,
   "se": 0.002931,
   "seeds_positive": 527,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.5,
    "linf": 1.0,
    "gtau": 4.0
   },
   "n": 1024,
   "mean_diff": -0.000546,
   "se": 0.00317,
   "seeds_positive": 543,
   "dropped_early": false
  },
  {
   "parameters": {
    "l1": 0.0,
    "linf": 1.0,
    "gtau": 4.0
   },
   "n": 1024,
   "mean_diff": 0.001619,
   "se": 0.003553,
   "seeds_positive": 528,
   "dropped_early": false
  }
 ],
 "selection": {
  "seeds": "64000..65023 in 8 rounds of 128, each setting paired with LEAN on the same seeds",
  "rule": "drop once mean_diff + 3*se < 0; chosen = surviving setting with the largest mean_diff after the last round (rule and the 12 settings fixed before any edge was run)",
  "dropped_early": 0,
  "reading": "No setting was dropped and none is separated from LEAN or from the others: all 12 lie within 1.5 se of zero. The chosen one is the top of 12 noisy estimates, so its +0.0038 carries selection bias.",
  "lean_mean_edge": 539.018328
 },
 "chosen": {
  "l1": 1.0,
  "linf": 0.5,
  "gtau": 1.0,
  "meaning": "lam = 0.5 + 0.5 / u for u >= 1: 1, 0.75, 0.667, ... toward 0.5"
 },
 "report": {
  "seeds": "10000..12047, run once",
  "vs_lean": {
   "n": 2048,
   "mean_diff": 0.003221,
   "se": 0.001723,
   "seeds_positive": 1059
  },
  "on_age": {
   "n": 2048,
   "mean_diff": -0.00349,
   "se": 0.003145,
   "seeds_positive": 1023
  },
  "mean_edge": {
   "lean": 536.603391,
   "graded_on_lean": 536.606612,
   "age": 536.6231,
   "graded_on_age": 536.61961
  },
  "age_minus_lean_same_seeds": {
   "n": 2048,
   "mean_diff": 0.019709,
   "se": 0.004066,
   "seeds_positive": 1140
  },
  "reading": "+0.0032 se 0.0017 over LEAN (1.9 se) and -0.0035 se 0.0031 stacked on AGE: no gain that survives stacking, and an order of magnitude below the +0.03 bar. The grade applies to about 6% of callbacks; why the effect is this small was not analysed further."
 },
 "promote": false,
 "contract_cost": {
  "basis": "Cost sketch on the QLX5 source (sha256 e183e5bc3fb794b7067703b10a7da9920267aa5f4caa968e16ca101b02338f40, decoded from artifact 1ef80bfb6d6791b877b1f4048a5e63cf2ae54153e6e8763ae3f8ab13132dfb15) with the chosen setting, compiled here with solc 0.8.24, optimizer 200, viaIR, paris against amm-challenge's AMMStrategyBase.sol. The unedited QLX5 compiles to 22,440 runtime bytes here, the figure its artifact reports. Gas is forge's gasleft() difference around the internal fee rule fees() over 280 inputs (phase 1,2 x w -60..60 bps x w2 -40..40 bps x four variances), not an engine afterSwap peak. The sketch was not checked against the replica's fees and never ran in the engine.",
  "storage_words": {
   "new": 0,
   "how": "slot 2 of QLX5 is fb | fa<<64 | t<<128 | cnt<<192 | syn<<255 with cnt read through a 32-bit mask: bits 224..254 are free and hold t_exact + 1 (0 = never). No extra SLOAD or SSTORE."
  },
  "gas": {
   "fee_rule_today": {
    "mean_synced_table": 55733,
    "mean_unsynced_table": 46217,
    "max": 58733
   },
   "added_on_a_blended_callback": {
    "loop_form": {
     "min": 33617,
     "mean": 35643,
     "max": 38078
    },
    "inline_form": {
     "min": 32275,
     "mean": 34111,
     "max": 36142
    }
   },
   "added_on_other_phase_1_2_callbacks_mean": {
    "loop_form": 261,
    "inline_form": 1626
   },
   "peak": "QLX5's engine peak is 208,366 of 245,000, i.e. 36,634 spare. A blended callback reads a second table (two more surface evaluations), +32,275..38,078 in the fee rule alone. If the peak callback is a blended one the peak becomes about 240,600..246,400: at or over the limit. Whether it is one was not measured; a first trade after many silent steps that is pulled has an old exact reading, so it is likely. With the chosen setting only callbacks with u >= 2 blend (u == 1 gives lam = 1)."
  },
  "characters": {
   "inline_form": {
    "added": 323,
    "total": 47553
   },
   "loop_form": {
    "added": 351,
    "total": 47581
   },
   "limit": 50000
  },
  "runtime_bytes": {
   "today": 22440,
   "inline_form": 26256,
   "loop_form": 23450,
   "limit": 24576,
   "note": "Written as two extra surf() calls the contract is 1,680 bytes over the limit (viaIR inlines the surface code again); written as a second pass of one loop over the two tables it is +1,010 and fits with 1,126 to spare."
  },
  "solidity_changes_loop_form": [
   "fees(..., bool syn, ...) -> fees(..., int256 lam, ...); tb=(ph==1?1:3)+(lam>=U?0:1)",
   "bytes memory b=QPL;int256 xa;int256 xb;bool bd=ph!=0&&lam>0&&lam<U; for(uint256 q=bd?2:1;q>0;){q--;QT memory t=qt(tb-q); int256 ya=surf(t,b,pw,p2,k,tt,uint256(nc1)-1)+wq-w; int256 yb=surf(t,b,320*U-pw,nc1*U-p2,k,tt,uint256(nc1)-1)+w-wq; if(bd&&q==0){xa=ya+mul(lam,xa-ya);xb=yb+mul(lam,xb-yb);}else{xa=ya;xb=yb;}}",
   "afterSwap: uint256 te=s2>>224&0x7fffffff; on the bound (or split-test hit) te=t.timestamp+1; int256 lam=syn?U:int256(0);if(ph!=0&&syn&&te!=t.timestamp+1)lam=U/2+(te==0?int256(0):U/(2*int256(t.timestamp+1-te))); slots[2] |= te<<224"
  ]
 },
 "checks": {
  "base": "fastlean.py sha256 d9c758bb280fe24e09c039496f84ab0804173ff70cec24c332f8683f28efa582 (the artifact's source_sha256); Taylor files fetched with tell/fetch.sh: 34 files, 0 mismatched",
  "patched_sha256": "1118bac6d290501c18dd879cf660c269d06c31970fb9d9e64fc811ffea016e6a",
  "patch_applies": "patch -p1 of the decoded diff on the artifact's fastlean/fastlean.py gives the file that was run, byte for byte",
  "parity_unpatched_path": "patched file with grade off vs CPython leanbase:Policy (Taylor's replica2.run, run.Blind), seeds 64000, 10000, 64003, LEAN and AGE pulls: difference 0.0 on all 6",
  "oracle": "CPython oracle (stalerival.Policy.on_trade and real.RealPolicy.lookup restated with the grade lines, inside Taylor's replica2.run) vs the patched replica: 8 comparisons, grades (1,1,1), (1,0,1), (0.5,0.5,1), (0,1,4) under the LEAN and the AGE pull, seeds 64000, 10000, 64003: max abs difference 0.0",
  "grade_1_1_1_equals_ungraded_bit_for_bit": {
   "LEAN_64000_64063": true,
   "AGE_64000_64063": true
  },
  "callback_counts_64000_64063": {
   "LEAN": {
    "callbacks": 690591,
    "read_as_arb_phase0": 241148,
    "non_arb": 449443,
    "non_arb_with_flag_set": 449443,
    "graded_callbacks_pulled_with_exact_reading_age_ge_1": 44506
   },
   "AGE": {
    "callbacks": 691059,
    "read_as_arb_phase0": 241347,
    "non_arb": 449712,
    "non_arb_with_flag_set": 449712,
    "graded_callbacks_pulled_with_exact_reading_age_ge_1": 44111
   }
  },
  "se": "sd of the paired differences (n-1) over sqrt(n)",
  "environment": "CPython 3.12.3, numpy 2.2.6, llvmlite 0.44.0, numba 0.61.2 (PyPI cp312 manylinux x86_64 wheels, sha256 checked, unpacked: the machine has no pip), Linux x86_64, 16 processes"
 },
 "side_finding": "QLX5's own afterSwap has the same property as the replica: after `if(qx||bound){r=c;syn=true;} else if(c!=r){...pull...;syn=true;}` the flag is false in phase 1 or 2 only when c == r exactly, so its unsynced tables (qt(2), qt(4)) are all but unreachable; on the replica that case occurred 0 times in 449,443. What dropping them would save in bytes was not measured here.",
 "patch_zb64": "eNq1WFtz2kgWfvevOOt5sBQElrja2LjsVIVdV6VSMxtX+cHFUi1oQLGQZLUIZn/9fqdbNwheZzM7zAR3nz597n0uNJtNEucLobJQiqhctJLdSaPRIP/40e0tNdvdgXNBDf5zSbe3J8T/3UbfgsyaidlKjh7SjbRPmnO5oDCOnzeJdefQR4e+OvTY/uzQl0eHxtg/ug6tHNri/7ZDyUo5pOb4t4tmcm4PT5qEz+npaSpF2Ponvn6Pw2C2axmitA2yFQl6WVMm/FCS9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nLevqMkafX+qbi6Mxh+Udn+Jqug3kBE+7IbXV6pmHMl+jzRm10Y8PiR6j/M2HHBP3oCybjork4PT194EqiuH6YH6POzMQi0tmKwFrOsjjdcX+ezZCxVkiRN2bw3iSoQOkmmvoCR+DyLFX1i9ED7MsytbQecSIjK1athOP9W4w2qdQMCthFl/THz156WfMd9t3A089rAEXhnMJ5ZYXQYTPlwc16yJnwzLHwuXtjuLXwETCCYaKCiRz33gSkSFOxs56qqYAHgskPoW0e3njvjgmeh6ezR/dsYhfBhP0/pn6iaiDhVkv4rtzAe7YWWEtYVEceEsyIoOeDUZ7QitTIPxpGcUbs67yT8jgCii/7xwyJ+rhPk/sxw0woTLBZCS3y8V+qqHOseLqm6JUqVuppxQyd0CtJhnX6LL5dZIL8dZedQ6kHK9D+fFZ3bj7s2Sbt6Kf/HxftDaE=",
 "patch_encoding": "base64(zlib(unified diff, -p1, against fastlean/fastlean.py of artifact a88ff982...)); adds load(..., grade=(l1, linf, gtau)); grade=None is the unmodified replica",
 "run": [
  "tabs = fastlean.load('amm', a0=0.35, ainf=0.35, grade=(1.0, 0.5, 1.0)); edges = fastlean.run_batch(tabs, range(10000, 12048))   # LEAN + chosen grade",
  "tabs = fastlean.load('amm', a0=0.0, ainf=0.7, tau=1.0, grade=(1.0, 0.5, 1.0))   # stacked on AGE"
 ],
 "not_done": [
  "Replica only: no engine run, no gate (P, Q, T, F, G, E), no contract delivered, nothing submitted.",
  "The fast replica is LEAN, not QLX5: it has no rival split test. In QLX5 a split-test hit also sets the flag and would count as an exact reading; the grade on QLX5 itself was not measured.",
  "Age was measured in steps only. Age in callbacks within a step, a separate grade for phase 1 and phase 2, blending the two fees instead of the two table values, and weights outside [0, 1] were not tested (12 settings, one family).",
  "How the graded callbacks split by age u (1, 2, 3+) was not counted.",
  "Solidity: a cost sketch only. Compiled and its fee rule gas-measured with forge; not compared with the replica fees (gate F), not run in the engine, its afterSwap peak not measured, and whether the peak callback is a blended one is not known.",
  "Seeds 64000, 64003 and 10000 were also run by the correctness check (oracle) with three of the twelve settings before selection; the settings and the rule were already fixed. Seed 10000 belongs to the report block.",
  "The kit's setup_fast.sh and score.py were not used: the replica's own run lines were followed with wheels unpacked from PyPI, and the stopping rule was coded in the driver.",
  "stalerival.py, leanbase.py and the QLX5 source exist only inside compressed fields of their artifacts, so sources.json cannot quote them line by line; what is quoted from the artifacts is their plain-text fields.",
  "The driver, oracle and Solidity sketch are not delivered (only this file and sources.json may be written); the patch above is the mechanism in full."
 ]
}
