#!/usr/bin/env python3 """Job #2972 (route 114) Q2 instrument: lane-wide counterexample search. For every ACCEPTED return of type=audit, test whether the revision it states is served: (i) text: the patch's own added lines are present verbatim in the document the patch's header names (and in the served corpus at large, by distinctive line) (ii) record: GET /history/ names this audit's return id as the author of a version, and whether it is the newest version (iii) content: sha256 of the served target equals the revision the audit declares (revision_sha, or the accepted version's content_sha in /history) (iv) address: GET /files/ resolves (extension of #1566's P3 to audits that enumeration never saw; POST-REGISTRATION, reported as exploratory) Controls (pre-registered): C1 negative: every audit's added lines are also tested against a document it does NOT name; a positive there means the test is measuring something other than presence. C2 positive: the removed lines (the pre-audit text, expected to be served) must be found at a rate strictly above the added-line rate wherever both exist. Inputs are the cached artifacts only (cache/ids.jsonl, cache/browse/*.json, cache/docs/*); no research bytes are fetched here except /history and /files, which are anonymous. """ import hashlib import json import os import re import sys import time from concurrent.futures import ThreadPoolExecutor sys.stdout.reconfigure(encoding="utf-8", errors="replace") sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from sweep import BASE, get, jget, D, PROJ # noqa: E402 (same harness, same anonymity) HERE = os.path.dirname(os.path.abspath(__file__)) CACHE = os.path.join(HERE, "cache") DOCS = os.path.join(CACHE, "docs") BROWSE = os.path.join(CACHE, "browse") HDR_NEW = re.compile(r"^\+\+\+\s+(?:[ab]/)?(.+?)\s*$") HDR_OLD = re.compile(r"^---\s+(?:[ab]/)?(.+?)\s*$") def load_ids(): return [json.loads(l) for l in open(os.path.join(CACHE, "ids.jsonl"), encoding="utf-8")] def served_bytes(path): f = os.path.join(DOCS, path.replace("/", "__")) if not os.path.exists(f): return None return open(f, "rb").read() def lines_of(patch): add, rem = [], [] for ln in (patch or "").splitlines(): if ln.startswith("+++") or ln.startswith("---"): continue if ln.startswith("+"): add.append(ln[1:]) elif ln.startswith("-"): rem.append(ln[1:]) return ([x for x in add if x.strip()], [x for x in rem if x.strip()], [x for x in add if not x.strip()], [x for x in rem if not x.strip()]) def header_targets(patch): new, old = [], [] for ln in (patch or "").splitlines(): m = HDR_NEW.match(ln) if m: new.append(norm(m.group(1))) m = HDR_OLD.match(ln) if m: old.append(norm(m.group(1))) return new, old def norm(p): p = p.strip().strip('"') if p in ("/dev/null", "dev/null"): return None for pre in ("a/", "b/"): if p.startswith(pre): p = p[2:] return p def history(path): """Versions of a served path, as the corpus ships them (anonymous GET /history/).""" st, obj = jget(PROJ + "/history/" + path) if st != 200 or not isinstance(obj, dict): return {"http": st, "versions": []} vers = obj.get("versions") or [] return {"http": st, "versions": vers, "sha256_of_body": hashlib.sha256( json.dumps(obj, sort_keys=True).encode()).hexdigest()} def blobs_of(rid): p = os.path.join(BROWSE, "%d.json" % rid) if not os.path.exists(p): return {} return json.load(open(p, encoding="utf-8")) def main(): rows = load_ids() existing = [r for r in rows if r.get("type")] census = {} for r in existing: census.setdefault(r["type"], {}).setdefault(r["status"], 0) census[r["type"]][r["status"]] += 1 audits = [r for r in existing if r["type"] == "audit"] A_acc = [r for r in audits if r["status"] == "accepted"] A_sup = [r for r in audits if r["status"] == "superseded"] A_rev = [r for r in A_acc if r["patch_chars"] or r["revision_sha"]] # ---- corpus-wide index for the distinctive-line scan -------------------------------- corpus = {} for name in os.listdir(DOCS): p = name.replace("__", "/") corpus[p] = open(os.path.join(DOCS, name), "rb").read().decode("utf-8", "replace") listing = json.load(open(os.path.join(CACHE, "listing.json"), encoding="utf-8")) out = {"at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "instrument": "audit-lane.py", "census": census, "n_existing_returns": len(existing), "n_absent_ids": len([r for r in rows if str(r.get("status", "")).startswith("HTTP_")]), "id_range": [1, max(r["id"] for r in rows)], "corpus": {"paths": len(corpus), "listing_sha256": listing["sha256_of_joined_paths"]}, "audit_lane": {"total": len(audits), "accepted": len(A_acc), "superseded": len(A_sup), "accepted_with_revision": len(A_rev), "accepted_ids": sorted(r["id"] for r in A_acc)}, "rows": [], "controls": {}} # ---- resolve targets ---------------------------------------------------------------- tests = [] for r in sorted(A_rev, key=lambda x: x["id"]): b = blobs_of(r["id"]) patch = b.get("patch") or "" add, rem, add_blank, rem_blank = lines_of(patch) hn, ho = header_targets(patch) declared = r.get("declared_target") rev_path = b.get("revision_path") or r.get("revision_path") tgt = None for c in hn + ho: if c: tgt = c break tests.append({"return": r["id"], "row": r, "body": b, "patch": patch, "add": add, "rem": rem, "add_blank": add_blank, "rem_blank": rem_blank, "header_new": hn, "header_old": ho, "declared": declared, "revision_path": rev_path, "target": tgt}) # C1 negative control pairs: audit i tested against audit i+1's target for i, t in enumerate(tests): t["neg_target"] = None for j in range(1, len(tests)): cand = tests[(i + j) % len(tests)]["target"] if cand and cand != t["target"]: t["neg_target"] = cand break # ---- history + files probes (anonymous, threaded) ------------------------------------ def probes(t): res = {"history": None, "neg_history": None, "files_patch_hash": None, "files_revision_sha": None} p = t["target"] or t["revision_path"] if p: res["history"] = history(p) if t["neg_target"]: res["neg_history"] = history(t["neg_target"]) ph = t["row"].get("patch_hash") if ph: st, body = get(BASE + "/files/" + ph) res["files_patch_hash"] = {"http": st, "sha256_of_body": hashlib.sha256(body).hexdigest(), "content_addressed": st == 200 and hashlib.sha256(body).hexdigest() == ph} rs = t["row"].get("revision_sha") if rs: st, body = get(BASE + "/files/" + rs) res["files_revision_sha"] = {"http": st, "sha256_of_body": hashlib.sha256(body).hexdigest(), "content_addressed": st == 200 and hashlib.sha256(body).hexdigest() == rs} return res with ThreadPoolExecutor(max_workers=8) as ex: for t, res in zip(tests, ex.map(probes, tests)): t["probes"] = res # ---- the three tests per audit -------------------------------------------------------- for t in tests: r = t["row"] served = served_bytes(t["target"]) if t["target"] else None text = served.decode("utf-8", "replace") if served is not None else None row = {"return": r["id"], "status": r["status"], "patch_chars": r["patch_chars"], "patch_hash": r["patch_hash"], "revision_sha": r["revision_sha"], "revision_path": t["revision_path"], "declared_target": t["declared"], "header_new": t["header_new"], "header_old": t["header_old"], "target": t["target"], "declared_vs_header": (t["declared"] != t["target"]) if t["declared"] else None, "patch_status": r["patch_status"], "effects_applied_at": r["effects_applied_at"], "added_lines": len(t["add"]), "removed_lines": len(t["rem"]), "added_blank": len(t["add_blank"]), "removed_blank": len(t["rem_blank"]), "target_served": served is not None, "served_sha256": hashlib.sha256(served).hexdigest() if served else None} # (i) text presence if text is not None and t["add"]: row["added_present"] = sum(1 for x in t["add"] if x in text) row["added_present_frac"] = round(row["added_present"] / len(t["add"]), 4) else: row["added_present"], row["added_present_frac"] = (0 if text is None else 0), 0.0 if text is not None and t["rem"]: row["removed_present"] = sum(1 for x in t["rem"] if x in text) row["removed_present_frac"] = round(row["removed_present"] / len(t["rem"]), 4) else: row["removed_present"], row["removed_present_frac"] = None, None # (i') whole-corpus distinctive line scan: longest added line >= 60 chars dist = sorted([x for x in t["add"] if len(x) >= 60], key=len, reverse=True) if dist: needle = dist[0] hits = [p for p, body in corpus.items() if needle in body] row["distinctive_needle"] = needle[:160] row["distinctive_needle_len"] = len(needle) row["corpus_hits"] = hits row["corpus_hit_count"] = len(hits) row["corpus_hits_include_target"] = (t["target"] in hits) if t["target"] else None else: row["distinctive_needle"] = None row["corpus_hit_count"] = None # C1 negative control neg = t["neg_target"] nserved = served_bytes(neg) if neg else None if nserved is not None and t["add"]: ntext = nserved.decode("utf-8", "replace") row["neg_target"] = neg row["neg_added_present"] = sum(1 for x in t["add"] if x in ntext) row["neg_added_frac"] = round(row["neg_added_present"] / len(t["add"]), 4) else: row["neg_target"], row["neg_added_present"], row["neg_added_frac"] = neg, None, None # (ii) record: /history authorship h = (t["probes"] or {}).get("history") or {} vers = h.get("versions") or [] mine = [v for v in vers if v.get("return_id") == r["id"]] newest = None for v in vers: if newest is None or str(v.get("created_at") or "") > str(newest.get("created_at") or ""): newest = v row["history_http"] = h.get("http") row["history_versions"] = len(vers) row["history_versions_with_this_id"] = len(mine) row["history_this_id_is_newest"] = bool(newest and newest.get("return_id") == r["id"]) row["history_newest_return_id"] = newest.get("return_id") if newest else None row["history_newest_content_sha"] = newest.get("content_sha") if newest else None row["history_newest_created_at"] = newest.get("created_at") if newest else None # (iii) content identity: is the served text the revision the audit declares? rs = r["revision_sha"] row["served_equals_revision_sha"] = bool(rs and row["served_sha256"] == rs) row["served_equals_newest_content_sha"] = bool( row.get("history_newest_content_sha") and row["served_sha256"] == row["history_newest_content_sha"]) row["revision_sha_authors_a_version"] = bool(mine) # (iv) declared address of the patch fp = (t["probes"] or {}).get("files_patch_hash") or {} fr = (t["probes"] or {}).get("files_revision_sha") or {} row["patch_hash_http"] = fp.get("http") row["patch_hash_is_content_addressed"] = fp.get("content_addressed") row["revision_sha_http"] = fr.get("http") row["revision_sha_is_content_addressed"] = fr.get("content_addressed") # verdict add_all = bool(t["add"]) and row["added_present"] == len(t["add"]) row["verdict"] = ("REVISED-TEXT-SERVED" if add_all else "REVISION-IS-CURRENT" if (row["history_this_id_is_newest"] or row["served_equals_revision_sha"]) else "PROVENANCE-RECORDED" if (row["history_versions_with_this_id"] or row["revision_sha_authors_a_version"]) else "NO-OBSERVABLE-PRODUCT") out["rows"].append(row) # ---- controls ------------------------------------------------------------------------- neg_positive = [r["return"] for r in out["rows"] if r.get("neg_added_present") not in (None, 0)] c2 = [] for r in out["rows"]: if r.get("removed_present") is not None and r["removed_lines"]: c2.append({"return": r["return"], "removed_frac": r["removed_present_frac"], "added_frac": r["added_present_frac"], "removed_strictly_above_added": r["removed_present_frac"] > r["added_present_frac"]}) out["controls"] = { "C1_negative_no_positive": {"violations": neg_positive, "pass": not neg_positive, "note": "added lines tested against a document the audit " "does not name"}, "C2_positive_removed_above_added": {"rows": c2, "pass": all(x["removed_strictly_above_added"] for x in c2) if c2 else None}, } # ---- falsifier / predictions --------------------------------------------------------- fired3 = [r["return"] for r in out["rows"] if r["verdict"] in ("REVISED-TEXT-SERVED", "REVISION-IS-CURRENT")] out["falsifiers"] = { "F1_lane_is_only_four": {"fired": len(A_rev) <= 4 or len(A_acc) <= 4, "audit_lane_total": len(audits), "accepted": len(A_acc), "accepted_with_revision": len(A_rev)}, "F3_added_lines_served_in_own_target": { "fired": bool([r["return"] for r in out["rows"] if r["added_present"] and r["added_present"] == r["added_lines"]]), "counterexamples": [r["return"] for r in out["rows"] if r["added_present"] and r["added_present"] == r["added_lines"]]}, "F4_verdict_carried_by_corpus": {"fired": bool(fired3), "counterexamples": fired3}, "F5_declared_target_disagrees_with_header": { "rows": [r["return"] for r in out["rows"] if r["declared_vs_header"]], "fired": any(r["declared_vs_header"] for r in out["rows"] if r["declared_vs_header"] is not None)}, } out["prediction_P2_reproduces_1373_T5"] = { r["return"]: {"added_present": r["added_present"], "added_lines": r["added_lines"], "removed_present": r["removed_present"], "removed_lines": r["removed_lines"]} for r in out["rows"] if r["return"] in (101, 151, 152)} with open(os.path.join(HERE, "census.json"), "w", encoding="utf-8") as f: json.dump({k: v for k, v in out.items() if k != "rows"}, f, indent=1) with open(os.path.join(HERE, "audit-lane.json"), "w", encoding="utf-8") as f: json.dump(out, f, indent=1) # ---- summary ------------------------------------------------------------------------- print("census:", json.dumps(census)) print("audit lane: total %d accepted %d superseded %d accepted-with-revision %d" % (len(audits), len(A_acc), len(A_sup), len(A_rev))) print() print("%-6s %-28s %-9s %-7s %-9s %-8s %s" % ("id", "target", "added", "removed", "hist_new", "patch_h", "verdict")) for r in out["rows"]: print("%-6d %-28s %-9s %-7s %-9s %-8s %s" % ( r["return"], (r["target"] or "-")[-28:], "%s/%s" % (r["added_present"], r["added_lines"]), ("%s/%s" % (r["removed_present"], r["removed_lines"])) if r["removed_present"] is not None else "-", "%s" % r["history_newest_return_id"], r["patch_hash_http"], r["verdict"])) print() print("controls:", json.dumps(out["controls"], indent=1)[:1200]) print("falsifiers:", json.dumps(out["falsifiers"], indent=1)) if __name__ == "__main__": main()