#!/usr/bin/env python3 """Independent checker for job #4537 (route 148 step check). Reads only the declared inputs (this directory's `served/`, `step.json`, `fetch_manifest.json`) and re-derives every number the job reports: * the pinned sha256 of each served source file, on raw bytes and on CRLF->LF; * that the assigned step is byte-identical to the route's stored event, to the step-setter #1837 and to the previous step check #2019; * that sahdated 1.1.2's write guard is a 25-line text window and not object-level; * that there is no `unresolved` status and no typed `hashed-artifact` exemption; * the LIMIT/12-of-12 semantics of test_recognition_2940.py; * the baseline unresolved share from #1837's own population.json (exact rational); * the whole-corpus scan for anything recorded after the step-setter that answers the step, routeless returns included. Exit 0 iff every check passes. stdlib + numpy + sympy only. """ import ast import glob import hashlib import json import os import re import sys import numpy as np import sympy HERE = os.path.dirname(os.path.abspath(__file__)) # The job's own directory keeps the served records under served/; a fresh reproduction # directory holds the same files flat beside this script. Both layouts are supported. SERVED = os.path.join(HERE, "served") if not os.path.isdir(SERVED): SERVED = HERE RESULTS = [] def check(name, cond, got=None): RESULTS.append({"check": name, "pass": bool(cond), "got": got}) return bool(cond) def sha(path, crlf=False): with open(path, "rb") as fh: b = fh.read() if crlf: b = b.replace(b"\r\n", b"\n") return hashlib.sha256(b).hexdigest() def load(path): with open(path, encoding="utf-8") as fh: return json.load(fh) def text(path): with open(path, encoding="utf-8") as fh: return fh.read() # ------------------------------------------------------------------ declared inputs STEP = load(os.path.join(HERE, "step.json")) S_RETURNS = {} for p in sorted(glob.glob(os.path.join(SERVED, "return-*.json"))): rec = load(p) S_RETURNS[rec["id"]] = rec ROUTE = load(os.path.join(SERVED, "route-148.json")) MANIFEST = load(os.path.join(HERE, "fetch_manifest.json")) # ------------------------------------------------------- 1. served bytes are pinned # The six returns whose records this comparison reads are shipped in full (served JSON); # the files the checker CONSUMES are shipped beside them and hashed below. The other 178 # returns in the window are scanned through corpus_after.jsonl (text + attachment names). DECLARED = (1541, 1549, 1562, 1837, 2019, 2024) USED = { (1541, "sahdated-1.1.1-served.py"), (1541, "sahdated-write-exemptions.json"), (1837, "sahdated-1.1.2.py"), (1837, "test_recognition_2940.py"), (1837, "population_2940.py"), (1837, "reader_table_2940.py"), (1837, "population.json"), (1837, "reader-table.json"), (1837, "test-v111.json"), (1837, "test-v112.json"), } PUBLISHED = {} for rid, rec in S_RETURNS.items(): if rid not in DECLARED: continue for f in (rec.get("files") or []): PUBLISHED[(rid, f["name"])] = f["sha256"] bad_raw, bad_lf, missing = [], [], [] for (rid, name), want in sorted(PUBLISHED.items()): p = os.path.join(SERVED, "f%d_%s" % (rid, name)) if not os.path.exists(p): if (rid, name) in USED: missing.append("%d/%s" % (rid, name)) continue if sha(p, crlf=False) != want: bad_raw.append("%d/%s" % (rid, name)) if sha(p, crlf=True) != want: bad_lf.append("%d/%s" % (rid, name)) check("every file the checker consumes is shipped", not missing, missing[:5]) check("every shipped attached file hashes to its return's published sha256 on RAW bytes", not bad_raw, bad_raw[:5]) check("...and (informational) which of them only match after CRLF->LF", True, {"raw_only": sorted(set(bad_lf)), "shipped": len(PUBLISHED) - len(missing), "declared_total": len(PUBLISHED)}) PINNED = { "f1837_sahdated-1.1.2.py": "4cf3d5f1302576469d6422e54eb8ab74b5f9c2f527b11a2a3dc06fc31dc9f43a", "f1837_test_recognition_2940.py": "4c073a38fcb84e4454e56e246ac42c69dab0fc472690e312ebe0c211befcbff0", "f1837_reader_table_2940.py": "dcef10ee523280774292e701441522eaa2fc0103ad2f92977e574dd506679077", "f1837_population_2940.py": "33b6a2372f6c8f153200fa6939bb2ce10877b613bdf06ffbe2a92d51fdc2c878", "f1541_sahdated-1.1.1-served.py": "d638f6f647d4aaed79ffdb81e492c9062e660be376f96e68c1a0dd64039130e0", } for name, want in sorted(PINNED.items()): check("pinned sha256 %s" % name, sha(os.path.join(SERVED, name)) == want, sha(os.path.join(SERVED, name))) # --------------------------------------------- 2. the assigned step is unchanged step = STEP["step"] route_event = [e for e in ROUTE["events"] if e.get("detail", {}).get("next_step")] latest = max(route_event, key=lambda e: e.get("created_at") or "") check("route 148 is active, revision 4, last_return_id 2019", ROUTE["state"] == "active" and ROUTE["revision"] == 4 and ROUTE["last_return_id"] == "2019", [ROUTE["state"], ROUTE["revision"], ROUTE["last_return_id"]]) check("the route's latest recorded step event is #2019's and carries the step", latest["return_id"] == "2019" and latest["outcome"] == "promising", [latest["return_id"], latest["outcome"]]) check("assigned step == route event 911's step (whole dict, canonical JSON)", json.dumps(step, sort_keys=True) == json.dumps(latest["detail"]["next_step"], sort_keys=True), None) check("assigned step == #1837 research.next_step (the step-setter)", json.dumps(step, sort_keys=True) == json.dumps(S_RETURNS[1837]["research"]["next_step"], sort_keys=True), None) check("assigned step == #2019 research.next_step (previous step check)", json.dumps(step, sort_keys=True) == json.dumps(S_RETURNS[2019]["research"]["next_step"], sort_keys=True), None) check("route 148's stored next_step field IS the assigned step", json.dumps(ROUTE.get("next_step") or {}, sort_keys=True) == json.dumps(step, sort_keys=True), None) check("pursuit job #4180 is on the route's job list", any(j["id"] == "4180" for j in ROUTE["jobs"]), [(j["id"], j["status"]) for j in ROUTE["jobs"]]) # ------------------------- 3. sahdated 1.1.2's guard is a text window, not object-level src = text(os.path.join(SERVED, "f1837_sahdated-1.1.2.py")) tree = ast.parse(src) producers = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == "producers") pseg = ast.get_source_segment(src, producers) check("producers' signature has a fixed 25-line guard window", "guard_window=25" in ast.unparse(producers.args), ast.unparse(producers.args)) check("the guard verdict is computed from a text window `near` (guarded if routed or keyed)", re.search(r'near = "\\n"\.join\(code\[max\(0, n - guard_window\):n\]\)', pseg) is not None and '"guarded" if (routed or keyed) else "unguarded"' in pseg, None) check("no `ast.` call inside producers (the dumped object is never resolved)", not re.search(r"\bast\.", pseg), re.findall(r"\bast\.\w+", pseg)) check("the only ast use in the module is _no_write_lines (a print/json.loads filter)", sorted(set(re.findall(r"\bast\.\w+", src))) == ["ast.Attribute", "ast.Call", "ast.Name", "ast.parse", "ast.walk"], sorted(set(re.findall(r"\bast\.\w+", src)))) check("the exemption matcher keys on path-tail + `contains` text only", 'e.get("contains") and e["contains"] in line' in pseg and 'row["exempt"] = {"why": e.get("why"), "contains": e["contains"]}' in pseg, None) check("no `unresolved` site status exists in 1.1.2's producers", '"unresolved": ' not in pseg and '"unresolved"' not in pseg, re.findall(r'"unresolved"', pseg)) check("no `hashed-artifact` string anywhere in the three served route-148 modules", not any("hashed-artifact" in text(os.path.join(SERVED, n)) for n in PINNED if n.startswith("f1837_sahdated") or "test_recognition" in n or "population_2940" in n), None) check("VERSION is sahdated/1.1.2", re.search(r'^VERSION\s*=\s*"sahdated/1\.1\.2"', src, re.M) is not None, re.search(r'^VERSION\s*=\s*(.+)$', src, re.M).group(1)) # ------------------------------------------- 4. the exemption list has no typed kinds EX = load(os.path.join(SERVED, "f1541_sahdated-write-exemptions.json")) kinds = sorted({k for e in EX["exempt"] for k in e}) check("the job2830 exemption records carry only path/contains/why (no `kind`, no provenance)", set(kinds) <= {"path", "contains", "why"}, kinds) # ------------------------------ 5. the LIMIT case is excluded from the 12/12 pass count tst = text(os.path.join(SERVED, "f1837_test_recognition_2940.py")) ttree = ast.parse(tst) check("the LIMIT test is the adjacent-undated-object case (h_window_leak.py)", '"h_window_leak.py"' in tst and re.search(r'"an undated object dumped beside a dated literal is unguarded", "LIMIT"', tst) is not None, None) check("passed/total exclude LIMIT entries", 'if r["kind"] != "LIMIT"' in tst and tst.count('r["kind"] != "LIMIT"') == 2, None) check("exit status compares only those counts", 'sys.exit(0 if out["passed"] == out["total"] else 1)' in tst, None) n_cases = len(re.search(r"CASES = \{(.*?)\n\}", tst, re.S).group(1).split('":')) - 1 n_tests = len(re.findall(r"\n test\(", tst)) n_limit = len(re.findall(r', "LIMIT",', tst)) check("13 tests: 12 non-LIMIT + 1 LIMIT over 8 fixture files (python-side count)", (n_tests, n_limit, n_cases) == (13, 1, 8), [n_tests, n_limit, n_cases]) v112 = load(os.path.join(SERVED, "f1837_test-v112.json")) v111 = load(os.path.join(SERVED, "f1837_test-v111.json")) check("shipped v112 run: 12/12 passed, zero failed GAP/CONTROL, LIMIT recorded as a limit", v112["passed"] == 12 and v112["total"] == 12 and not v112["failed_gap"] and not v112["failed_control"] and len(v112["known_limits"]) == 1, v112["known_limits"]) check("the recorded LIMIT outcome is `guarded` (i.e. the LIMIT case does NOT pass)", v112["known_limits"]["an undated object dumped beside a dated literal is unguarded"] == ["guarded"], v112["known_limits"]) check("shipped v111 run: 7/12 with exactly the 5 GAP tests failing", v111["passed"] == 7 and len(v111["failed_gap"]) == 5 and not v111["failed_control"], v111["failed_gap"]) # ------------------- 6. baseline unresolved share, recomputed from population.json POP = load(os.path.join(SERVED, "f1837_population.json")) old, new = POP["old_counts"], POP["new_counts"] real_old = old["guarded"] + old["unguarded"] + old.get("exempt", 0) real_new = new["guarded"] + new["routed"] + new["unguarded"] + new.get("exempt", 0) share_new = sympy.Rational(new["unguarded"], real_new) share_old = sympy.Rational(old["unguarded"], real_old) check("#1837's measured population is 21 files", POP["files"] == 21, POP["files"]) check("1.1.2 counts are guarded 5 / routed 8 / not-a-write 33 / unguarded 28", (new["guarded"], new["routed"], new["not-a-write"], new["unguarded"]) == (5, 8, 33, 28), new) check("1.1.1 counts are guarded 21 / unguarded 51", (old["guarded"], old["unguarded"]) == (21, 51), old) check("real file writes under 1.1.2 = guarded+routed+unguarded+exempt = 41", real_new == 41, real_new) check("baseline unresolved share 28/41 = %s > 1/5 (the step's failure threshold)" % share_new, share_new > sympy.Rational(1, 5) and share_new == sympy.Rational(28, 41), [str(share_new), float(share_new)]) check("...and the 1.1.1 whole-file guard left 51/72 = %s unresolved" % share_old, share_old == sympy.Rational(51, 72), [str(share_old), float(share_old)]) check("not-a-write sites (33) are excluded from the 41 real file writes", new["not-a-write"] == 33 and real_new + new["not-a-write"] == new["guarded"] + new["routed"] + new["unguarded"] + new["not-a-write"], [real_new, new["not-a-write"]]) # --------------------- 7. #1837's four hashed JSON outputs (the exemption-class target) json_outputs = sorted(f["name"] for f in (S_RETURNS[1837].get("files") or []) if f["name"].endswith(".json")) check("#1837 serves exactly 4 JSON outputs (the step's `hashed-artifact` population)", json_outputs == ["population.json", "reader-table.json", "test-v111.json", "test-v112.json"], json_outputs) check("...and none of the 4 carries a top-level instant key (they are the conflict)", all(not (set(r) & {"covered_at", "observed_at", "measured_at", "generated_at", "walked_at", "stamped_at", "recorded_at", "written_at", "at"}) for r in (load(os.path.join(SERVED, "f1837_%s" % n)) for n in json_outputs)), [sorted(load(os.path.join(SERVED, "f1837_%s" % n)))[:4] for n in json_outputs]) # ------------------------------------ 8. whole-corpus scan for an answering return CUTOFF = S_RETURNS[1837]["created_at"] VOCAB = ["sahdated", "instant_fields", "hashed-artifact", "object-level", "job2830", "1.1.3", "2940", "reader_table", "limit test", "unresolved"] CORPUS_PATH = os.path.join(HERE, "corpus_after.jsonl") CORPUS = [json.loads(l) for l in open(CORPUS_PATH, encoding="utf-8") if l.strip()] check("the shipped comparison window holds 179 returns, all strictly after #1837", len(CORPUS) == 179 and all(r["created_at"] > CUTOFF for r in CORPUS), len(CORPUS)) check("...and every declared return recorded in that window is in the shipped corpus", all(r["id"] in {c["id"] for c in CORPUS} for r in S_RETURNS.values() if r["created_at"] > CUTOFF), None) after = CORPUS strong = [] for r in after: t = r["text"].lower() hits = [v for v in VOCAB if v in t] if len(hits) >= 3: strong.append((r["id"], r["route"], hits)) check("the only strong step-vocabulary return in the window is #2019 (route 148)", [s[0] for s in strong] == [2019], strong) check("#2024 (route 100) names #2019 only as an unrelated return", "unrelated" in (S_RETURNS[2024]["report_md"] or ""), None) attachments_after = sorted((r["id"], n) for r in after for n in (r.get("files") or []) if re.search(r"sahdated|2940|2830|producers", n, re.I)) check("no return after #1837 serves any sahdated/2940/2830/producers production file", attachments_after == [], attachments_after) check("#2019 serves one file only: its own comparison report", [f["name"] for f in (S_RETURNS[2019].get("files") or [])] == ["dated-step-report.md"], [f["name"] for f in (S_RETURNS[2019].get("files") or [])]) check("no return after #1837 is on route 148 other than #2019", sorted(r["id"] for r in after if str(r.get("route")) == "148") == [2019], sorted(r["id"] for r in after if str(r.get("route")) == "148")) check("#2019 is a comparison, not an implementation: its own outcome is promising", S_RETURNS[2019]["research"]["outcome"] == "promising", None) # ---------------------------------- 8b. the shipped scan agrees with this recomputation SCAN = load(os.path.join(HERE, "scan.json")) check("scan.json covers the same 179 returns, in time order", [r["id"] for r in SCAN["rows"]] == [r["id"] for r in after], None) check("scan.json's per-return vocabulary hits equal this checker's recomputation", all(sorted(r["vocab_hits"]) == sorted([v for v in VOCAB if v in c["text"].lower()]) for r, c in zip(SCAN["rows"], after)), None) check("scan.json's summary agrees: strong hits [2019], route-148 rows [2019], no production file", SCAN["strong_hits"] == [2019] and SCAN["route_148_in_window"] == [2019] and SCAN["production_files_in_window"] == [], SCAN["strong_hits"]) check("scan.json marks `implements_step` false on every row", not any(r["implements_step"] for r in SCAN["rows"]), None) # ----------------------------------------- 9. fetch completeness (absences recorded) errs = [m["path"] for m in MANIFEST if "error" in m] present = [int(m["id"]) for m in MANIFEST if m.get("id")] check("fetch manifest records the 84 404 ids rather than inventing them", len(errs) == 84 and all(e.startswith("return-") for e in errs), len(errs)) check("live head is #2025 and 184 return records were retrieved", max(present) == 2025 and len(present) == 184, [max(present), len(present)]) missing_in_range = [i for i in range(1838, 2026) if i not in present] check("the 9 absent ids in 1838..2025 are gaps in the server's public ids", missing_in_range == [1858, 1866, 1870, 1938, 1939, 1961, 1965, 1999, 2000], missing_in_range) # ------------------------------------------------------------------------- verdict passed = sum(1 for r in RESULTS if r["pass"]) total = len(RESULTS) out = {"job": "4537", "route": 148, "attempt": STEP["attempt_id"], "outcome": "promising" if passed == total else "OPEN", "passed": passed, "total": total, "failed": [r["check"] for r in RESULTS if not r["pass"]], "checks": RESULTS} print(json.dumps(out, indent=1, sort_keys=True)) with open(os.path.join(HERE, "check.out.json"), "w", encoding="utf-8", newline="\n") as fh: fh.write(json.dumps(out, indent=1, sort_keys=True) + "\n") sys.exit(0 if passed == total else 1)