#!/usr/bin/env python3 """Give every sampled accepted patch a content-addressed base, from served bytes alone. Procedure per (return, corpus path): 1. candidates = the /docs rendering plus every /history version whose bytes are served at host root /files/; 2. a candidate is a BASE if applying every hunk of the patch to it, in order, at a unique offset, succeeds; 3. the reconstructed post-image is computed; if it is byte-identical to another served version, the base identification is *proven* (the patch maps served bytes to served bytes), not merely plausible; 4. record base_sha256, post_sha256 and the matching served versions. Read-only on the server; writes identify_base.json + stdout. """ import json, os, sys, hashlib HERE = os.path.dirname(os.path.abspath(__file__)) SAH_HOME = os.path.abspath(os.path.join(HERE, "..", "..", "..")) sys.path.insert(0, os.path.join(SAH_HOME, "tools")) sys.path.insert(0, HERE) from anchor_probe import get, parse_patch, occ_offsets QUEUE = {"12", "97", "1328", "454"} def candidates(path): docs, how = get("/docs/" + path) out = [] if docs is not None: out.append({"name": "docs(current)", "sha": hashlib.sha256(docs.encode()).hexdigest(), "text": docs}) h, _ = get("/history/" + path) if h: try: versions = json.loads(h).get("versions") or [] except Exception: versions = [] for v in versions: sha = v.get("content_sha") if not sha: continue t, _ = get("host:/files/" + sha) if t: out.append({"name": f"v{v.get('version')}", "sha": sha, "text": t}) return out def apply_hunks(base_text, hunks): cur, notes = base_text, [] for hk in hunks: pre, post = hk["pre_slice"], hk["post_slice"] offs = occ_offsets(cur, pre) notes.append({"header": hk["header"], "pre_occ": len(offs)}) if len(offs) != 1: return None, notes o = offs[0] cur = cur[:o] + post + cur[o + len(pre):] return cur, notes def main(): rows = {} for rid in ["13", "12", "80", "101", "151", "152", "97", "1328"]: patch = open(os.path.join(HERE, "patches", f"return-{rid}.patch")).read() for s in parse_patch(patch): path = s["new"] or s["old"] if not path or "/" not in path: continue cands = candidates(path) bases, post_matches = [], None for c in cands: res, notes = apply_hunks(c["text"], s["hunks"]) if res is None: continue matches = [c2["name"] for c2 in cands if c2["text"] == res] bases.append({"base": c["name"], "base_sha256": hashlib.sha256(c["text"].encode()).hexdigest(), "post_sha256": hashlib.sha256(res.encode()).hexdigest(), "post_len": len(res), "post_matches_served_versions": matches, "hunks_applied": len(notes)}) if matches: post_matches = {"base": c["name"], "matches": matches} rec = {"return_id": rid, "queue_status": rid in QUEUE, "path": path, "n_candidates": len(cands), "candidate_names": [c["name"] for c in cands], "bases": bases, "proven": post_matches} rows[f"{rid}:{path}"] = rec tag = "QUEUE" if rid in QUEUE else "integrated" if bases: b = bases[0] print(f"#{rid:<5} {tag:<10} {path}\n" f" base={b['base']} base_sha256={b['base_sha256'][:16]} " f"post_sha256={b['post_sha256'][:16]} post_len={b['post_len']} " f"n_bases={len(bases)} post_matches={b['post_matches_served_versions']}") else: print(f"#{rid:<5} {tag:<10} {path}\n NO BASE among {len(cands)} candidates") json.dump(rows, open(os.path.join(HERE, "identify_base.json"), "w"), indent=1) proven = [k for k, v in rows.items() if v["proven"]] queue_done = [k for k, v in rows.items() if v["queue_status"] and v["bases"]] print(f"\nrows with a base from served bytes: {len([k for k, v in rows.items() if v['bases']])}/{len(rows)}") print(f"rows whose post-image equals a served version (PROVEN): {len(proven)} -> {proven}") print(f"queue rows with a content-addressed base: {len(queue_done)} -> {queue_done}") if __name__ == "__main__": main()