#!/usr/bin/env python3 """Which plain-multiply LCG reproduces the six retired seam z values? The reviewer's finding says the z scores quoted in reading 4 before 2026-08-20 (T19: -1.41, -0.19, -0.09; T23: -0.65, +0.13, -0.50) are "the exact output of the pre-2026-08-20 plain-multiply LCG (seed*1103515245, reproduced exactly), not a different random draw". This probes a family of plain-multiply forms and prints, for each, the two SEAMS blocks the served script prints. The form that reproduces all six printed values is the one the retired rows came from. Runs the served script unmodified except for the single `rnd` line, from a scratch tree, so the probe cannot touch the served artifact. python probe-lcg.py [--served PATH] [--node node] """ import argparse import os import re import shutil import subprocess import sys import tempfile HERE = os.path.dirname(os.path.abspath(__file__)) DEFAULT_SERVED = os.path.join(HERE, "repo", "research", "fold-profile-13-hotspot-sweep.js") # The line the served file uses today. IMUL_LINE = (" const rnd = () => { seed = (Math.imul(seed, 1103515245) + 12345) " "& 0x7fffffff; return seed / 0x7fffffff; };") # Plain-multiply candidates: the 1990s one-liner written several ways. All of # them are inexact once seed * 1103515245 exceeds 2**53 (it does from the second # call), which is the whole point of the 2026-08-20 change to Math.imul. CANDIDATES = [ ("mod2^31", "(seed * 1103515245 + 12345) % 2147483648"), ("mod0x80000000", "(seed * 1103515245 + 12345) % 0x80000000"), ("mod2**31", "(seed * 1103515245 + 12345) % 2**31"), ("and0x7fffffff", "(seed * 1103515245 + 12345) & 0x7fffffff"), ("and0xffffffff", "(seed * 1103515245 + 12345) & 0xffffffff"), ("mod2^32", "(seed * 1103515245 + 12345) % 4294967296"), ("toInt32trunc", "((seed * 1103515245 + 12345) | 0)"), ] RETIRED = { 19: ["-1.41", "-0.19", "-0.09"], 23: ["-0.65", "+0.13", "-0.50"], } SEAM_ROW = re.compile(r"^\s*(\d+)\s*\|.*\|\s*([-+]?\d+\.\d+)\s*\|") def seams(stdout): """Pull the two SEAMS blocks out of a run: {x: [z, z, z]}.""" out, tile = {}, None for line in stdout.splitlines(): m = re.match(r"^T(\d+):\s", line) if m: tile = int(m.group(1)) continue if line.strip().startswith("SEAMS at") and tile is not None: out.setdefault(tile, []) continue if tile in out and len(out[tile]) < 3: r = SEAM_ROW.match(line) if r: out[tile].append(r.group(2)) return {k: v for k, v in out.items() if len(v) == 3} def main(): ap = argparse.ArgumentParser() ap.add_argument("--served", default=DEFAULT_SERVED) ap.add_argument("--node", default="node") args = ap.parse_args() src = open(args.served, encoding="utf-8").read() if IMUL_LINE not in src: sys.exit("served file does not carry the expected Math.imul rnd line; refusing") print("served:", args.served) print("retired values to reproduce (printed z, sign-normalised):", {k: [z.lstrip("+") for z in v] for k, v in RETIRED.items()}) print() results = {} for name, expr in [("Math.imul (served)", None)] + CANDIDATES: if expr is None: body = src else: line = (" const rnd = () => { seed = " + expr + "; return seed / 0x7fffffff; };") body = src.replace(IMUL_LINE, line) assert body != src, name d = tempfile.mkdtemp(prefix="lcg-" + re.sub(r"[^A-Za-z0-9]", "_", name) + "-") try: p = os.path.join(d, "sweep.js") with open(p, "w", encoding="utf-8", newline="") as fh: fh.write(body) cp = subprocess.run([args.node, p], capture_output=True, text=True, encoding="utf-8", errors="replace", cwd=d) s = {k: [z.lstrip("+") for z in v] for k, v in seams(cp.stdout).items()} results[name] = s ref = {k: [z.lstrip("+") for z in v] for k, v in RETIRED.items()} verdict = "MATCH" if all(s.get(x) == v for x, v in ref.items()) else "" if not s: first = (cp.stderr.strip().splitlines() or ["(no stderr)"])[0] verdict = "no seam block printed (" + first[:60] + ")" print(f"{name:16s} T19={s.get(19)} T23={s.get(23)} {verdict}") finally: shutil.rmtree(d, ignore_errors=True) print() ref = {k: [z.lstrip("+") for z in v] for k, v in RETIRED.items()} hits = [n for n, s in results.items() if all(s.get(x) == v for x, v in ref.items())] print("candidates reproducing all six retired z values:", hits or "NONE") return 0 if hits else 1 if __name__ == "__main__": sys.exit(main())