#!/usr/bin/env python3 """T1 corrected: `code-sha256` is the hash of EVERY BYTE ABOVE THE OUTPUT BANNER, not of the whole file. The first pass hashed the whole served file against the header and got 0 matches out of 441, which is the signature of a wrong interpretation rather than 441 wrong headers -- and it was mine. `research/qc/embed.js` states the convention in its own header block (line 34): "code-sha256: every byte above the OUTPUT banner". So this pass cuts the file at the banner and hashes what is above it, trying the line-ending variants separately so that a systematic CRLF/LF cause is never confused with a genuinely edited file. usage: codehash.py [--out dir] """ import argparse import hashlib import json import os import re import sys sys.stdout.reconfigure(encoding="utf-8", errors="replace") HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.dirname(HERE) CACHE = os.path.join(RUN, "job128", "cache") LIST = os.path.join(RUN, "job128", "docs-list.txt") EXTS = (".py", ".js", ".mjs", ".sh", ".ts", ".cjs") # re.M matters: without it `^` matches only at the start of the file and every count comes back zero -- # measured here (first run of this script reported 0 of 0 rows, which is a regex bug, not a corpus fact) CODE_HEADER = re.compile(r"^\s*(?:\S+\s+)?(?:[#/;*]+\s*)?code-sha256:\s*([0-9a-f]{64})", re.I | re.M) # the banner the embed tool writes; matched on the TEXT of a line, and the byte offset is what matters BANNER = re.compile(r"^\s*(?://|#)\s*OUTPUT\b") def sha(b): return hashlib.sha256(b).hexdigest() def cache_path(p): return os.path.join(CACHE, "_projects_twin-primes_docs_" + p.replace("/", "_") + ".txt") def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", default=HERE) args = ap.parse_args() docs = [l.strip() for l in open(LIST, encoding="utf-8") if l.strip()] rows = [] for p in docs: if os.path.splitext(p)[1].lower() not in EXTS or not os.path.exists(cache_path(p)): continue raw = open(cache_path(p), "rb").read() text = raw.decode("utf-8", "replace") m = CODE_HEADER.search(text) if not m: continue want = m.group(1).lower() # byte offset of the start of the banner line off, cut = 0, None for ln in text.splitlines(keepends=True): if BANNER.match(ln): cut = off break off += len(ln.encode("utf-8")) above = raw[:cut] if cut is not None else raw variants = { "above_banner": sha(above), "above_banner_stripped_trailing_nl": sha(above.rstrip(b"\r\n")), "above_banner_lf": sha(above.replace(b"\r\n", b"\n")), "above_banner_crlf": sha(above.replace(b"\r\n", b"\n").replace(b"\n", b"\r\n")), "whole_file": sha(raw), } matched = [k for k, v in variants.items() if v == want] rows.append({"file": p, "line": text[:cut].count("\n") + 1 if cut is not None else None, "header": want, "has_banner": cut is not None, "matched_by": matched, "above_bytes": len(above), "src_crlf": b"\r\n" in raw}) ok = [r for r in rows if r["matched_by"]] bad = [r for r in rows if not r["matched_by"]] summary = { "scripts_with_a_code_sha256_header": len(rows), "match_on_bytes_above_banner": sum(1 for r in ok if "above_banner" in r["matched_by"]), "match_only_under_another_variant": len(ok) - sum(1 for r in ok if "above_banner" in r["matched_by"]), "no_variant_matched": len(bad), "files_without_a_banner": sum(1 for r in rows if not r["has_banner"]), "matched_by_variant": {k: sum(1 for r in ok if k in r["matched_by"]) for k in ("above_banner", "above_banner_stripped_trailing_nl", "above_banner_lf", "above_banner_crlf", "whole_file")}, "unmatched_files": [r["file"] for r in bad][:40], "files_containing_crlf": sum(1 for r in rows if r["src_crlf"]), } json.dump({"summary": summary, "rows": rows}, open(os.path.join(args.out, "codehash.json"), "w", encoding="utf-8"), indent=1, ensure_ascii=False) print(json.dumps(summary, indent=1)[:1600]) print() for r in bad[:10]: print(" UNMATCHED %s (line %s, banner %s)" % (r["file"], r["line"], r["has_banner"])) return 0 if __name__ == "__main__": sys.exit(main())