"""Independent re-derivation of proxy answers from what the model can see (SQL, sandboxed Python, brute force).""" import itertools import json import math import re from fractions import Fraction from .core import run_python from .tasks import FAMILIES FIN_SQL = {0: """ WITH paid AS (SELECT invoice_id, SUM(amount) AS p FROM payments GROUP BY invoice_id), bal AS (SELECT i.customer_id, (i.amount - COALESCE(p.p, 0)) * f.usd_per_unit AS usd FROM invoices i JOIN fx_rates f ON f.currency = i.currency LEFT JOIN paid p ON p.invoice_id = i.id WHERE i.status = 'open' AND julianday('2026-09-30') - julianday(i.due) > 60 AND i.amount - COALESCE(p.p, 0) > 0.005) SELECT (SELECT ROUND(SUM(usd), 2) FROM bal), (SELECT c.name FROM bal b JOIN customers c ON c.id = b.customer_id GROUP BY c.id ORDER BY SUM(usd) DESC LIMIT 1) """, 1: """ SELECT COUNT(*), ROUND(SUM(p2.amount * f.usd_per_unit), 2) FROM payments p2 JOIN invoices i ON i.id = p2.invoice_id JOIN fx_rates f ON f.currency = i.currency WHERE EXISTS (SELECT 1 FROM payments p1 WHERE p1.invoice_id = p2.invoice_id AND p1.amount = p2.amount AND p1.id != p2.id AND julianday(p2.paid_on) - julianday(p1.paid_on) BETWEEN 0 AND 3 AND (p1.paid_on < p2.paid_on OR (p1.paid_on = p2.paid_on AND p1.id < p2.id))) """} SCI_CODE = r''' import csv, json, math rows = list(csv.DictReader(open("data.csv"))) def fit(xs, ys): n = len(xs); mx = sum(xs)/n; my = sum(ys)/n b = sum((x-mx)*(y-my) for x, y in zip(xs, ys)) / sum((x-mx)**2 for x in xs) return b, my - b*mx k = list(rows[0]) out = {} if k[0] == "temperature_K": b, a = fit([1/float(r[k[0]]) for r in rows], [math.log(float(r[k[1]])) for r in rows]) out = {"activation_energy_kj_per_mol": -b*8.314/1000, "k_at_375K_per_s": math.exp(a + b/375)} elif k[0] == "time_h": w = [r for r in rows if 0.05 <= float(r["od600"]) <= 0.5] b, a = fit([float(r["time_h"]) for r in w], [math.log(float(r["od600"])) for r in w]) out = {"doubling_time_min": math.log(2)/b*60, "rows_used": len(w)} elif k[0] == "genotype": c = {r["genotype"]: int(r["count"]) for r in rows}; n = sum(c.values()) p = (2*c["AA"] + c["Aa"])/(2*n); e = [n*p*p, 2*n*p*(1-p), n*(1-p)**2] chi = sum((o-x)**2/x for o, x in zip([c["AA"], c["Aa"], c["aa"]], e)) out = {"allele_frequency_A": p, "chi_square": chi, "decision": "reject" if chi > 3.841 else "fail to reject"} else: g = [r for r in rows if not r["flag"]] b, a = fit([float(r["standard_mg_per_L"]) for r in g], [float(r["absorbance"]) for r in g]) out = {"slope": b, "intercept": a} print(json.dumps(out)) ''' def close(a, b): if isinstance(b, str): return a == b return math.isclose(a, b, rel_tol=1e-6, abs_tol=1e-9) def main(seeds=10): bad = 0 for seed in range(seeds): t = FAMILIES["finance-receivables"](seed) got = t.db.execute(FIN_SQL[t.variant]).fetchone() want = list(t.answer.values()) ok = (abs(got[0] - want[0]) <= 0.02 and got[1] == want[1]) if t.variant == 0 else \ (got[0] == want[0] and abs(got[1] - want[1]) <= 0.02) bad += not ok print("finance", seed, ok, got, want) if not ok else None t = FAMILIES["science-lab"](seed) code, out = run_python({"data.csv": t.csv, "main.py": SCI_CODE}, ["python", "main.py"]) got = json.loads(out) if "intercept" in got: # standard curve: recompute the sample from the stated absorbance absorb = float(re.search(r"absorbance ([\d.]+)\. Report", t.question).group(1)) got = {"slope": got["slope"], "sample_mg_per_L": (absorb - got["intercept"]) / got["slope"]} ok = got.keys() == t.answer.keys() and all(close(got[k], v) for k, v in t.answer.items()) bad += not ok print("science", seed, ok, got, t.answer) if not ok else None t = FAMILIES["math-oneshot"](seed) txt = t.text if "grid paths" in txt: w, h, bx, by = map(int, re.findall(r"\((\d+),(\d+)\)", txt)[1] + re.findall(r"\((\d+),(\d+)\)", txt)[2]) grid = [[0] * (h + 1) for _ in range(w + 1)] for x in range(w + 1): for y in range(h + 1): grid[x][y] = 0 if (x, y) == (bx, by) else (1 if x == y == 0 else (grid[x - 1][y] if x else 0) + (grid[x][y - 1] if y else 0)) ok = str(grid[w][h]) == t.truth elif "marbles" in txt: red, blue = map(int, re.findall(r"(\d+) red and (\d+) blue", txt)[0]) bag = ["r"] * red + ["b"] * blue draws = list(itertools.combinations(range(len(bag)), 3)) p = Fraction(sum(1 for d in draws if sum(bag[i] == "r" for i in d) == 2), len(draws)) ok = f"{p.numerator}/{p.denominator}" == t.truth elif "last two digits" in txt: a, b = map(int, re.findall(r"of (\d+)\^(\d+)", txt)[0]) ok = str(a ** b)[-2:] == t.truth else: ok = True # triples and knights are already brute-forced by the generator bad += not ok print("math", seed, ok, txt, t.truth) if not ok else None print("independent mismatches:", bad) if __name__ == "__main__": main()