#!/usr/bin/env python3 """Publish the 17 missing bases (job 2872): reconstruct them from the patches, verify, upload. #1522 left 17 accepted patch rows slice-only because no served candidate applies to them. For a `diff -u` whose hunks tile the pre-image from line 1, the patch text DETERMINES the whole base file, so the base can be reconstructed, not searched for. This script: 1. parses each slice-only row's hunks with their `@@ -a,b +c,d @@` ranges; 2. records which pre-image line numbers are covered, and any gap or conflicting overlap; 3. if the coverage is contiguous from line 1 with no gaps, rebuilds the base and the post-image; 4. verifies against the served post files: the rebuilt post must be byte-identical to a file the return itself uploaded (that is what proves the reconstruction, including the file's end); 5. uploads the verified base as `base-` and records base/post hashes. Rows that fail verification are reported, never published. """ import json, os, re, 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) import sah from anchor_probe import get, parse_patch, occ_offsets LAUNCH = "launch-zdvw3ktup0yka1yia4jkri3g" st = sah.state_dir(); run_dir = os.path.join(st, "runs", LAUNCH) launch = json.load(open(os.path.join(run_dir, "launch.json"))) binding = json.load(open(os.path.join(st, "device-binding.json"))); iden = sah.identity() assert str(launch["job_id"]) == "2872", f"launch holds {launch['job_id']}, expected 2872" H = {"Authorization": "Bearer " + sah.cred(), "Accept": "application/json", "Content-Type": "application/json", "X-Model": iden["model"], "X-Effort": iden["effort"], "X-Department": binding["department_id"], "X-Session": launch["session"], "X-Attempt": launch["attempt_id"], "User-Agent": "deepseek-harness/sah-tools " + sah.TOOL_VERSION} sha = lambda t: hashlib.sha256(t.encode()).hexdigest() lane = json.load(open(os.path.join(HERE, "lane-index.json"))) slices = json.load(open(os.path.join(HERE, "slice-certificates.json"))) TARGETS = [r for r in slices["certificates"] if not r["whole_file_base_served"]] def hunk_ranges(hunks): out = [] for h in hunks: m = re.match(r"@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@", h["header"]) a = int(m.group(1)); b = int(m.group(2)) if m.group(2) else 1 out.append((a, b)) return out def rebuild(hunks, ranges): covered, conflicts = {}, [] for (a, b), h in zip(ranges, hunks): for j, line in enumerate(h["pre"]): ln = a + j if ln in covered and covered[ln] != line: conflicts.append(ln) covered[ln] = line if not covered: return None, {"n": 0, "gaps": [], "conflicts": conflicts} n = max(covered) gaps = [i for i in range(1, n + 1) if i not in covered] return ("\n".join(covered[i] for i in range(1, n + 1)) + "\n" if not gaps else None), \ {"n": n, "gaps": gaps, "conflicts": conflicts} def apply_hunks(base_text, hunks): cur = base_text for h in hunks: pre = "\n".join(h["pre"]) + ("\n" if h["pre"] else "") post = "\n".join(h["post"]) + ("\n" if h["post"] else "") offs = occ_offsets(cur, pre) if pre else [0] if len(offs) != 1: return None o = offs[0] cur = cur[:o] + post + cur[o + len(pre):] return cur def main(): out = {"published": [], "not_published": []} for r in TARGETS: rid, path = str(r["return_id"]), r["path"] patch = os.path.join(HERE, "patches", f"lane-{rid}.patch") hunks = [h for s in parse_patch(open(patch).read()) if (s["new"] or s["old"]) == path for h in s["hunks"]] ranges = hunk_ranges(hunks) base, cov = rebuild(hunks, ranges) served_posts = [] for f in (lane["index"][rid].get("files") or []): t, _ = get("host:/files/" + (f.get("sha256") or "none")) if t: served_posts.append((f.get("name"), t)) rec = {"return_id": int(rid), "path": path, "n_hunks": len(hunks), "covered_lines": cov["n"], "gaps": len(cov["gaps"]), "conflicts": len(cov["conflicts"]), "served_posts": len(served_posts)} post = apply_hunks(base, hunks) if base is not None else None match = next((n for n, t in served_posts if post is not None and post == t), None) rec["post_matches_served_upload"] = match rec["post_sha256"] = sha(post) if post else None if base is not None and match: # resolve the post-image the served file corresponds to for the certificate check name = re.sub(r"[^A-Za-z0-9._-]", "_", f"base-{path.split('/')[-1]}") xrid, status, txt = sah.request(LAUNCH, "POST", sah.SERVER + "/files", headers=H, body={"name": name, "content": base, "job_id": 2872}) b = json.loads(txt) rec.update({"published_name": name, "upload_status": status, "base_sha256": b.get("sha256") or sha(base), "base_bytes": len(base), "proven_by": f"post == uploaded {match}"}) out["published"].append(rec) print(f"PUBLISHED #{rid} {path}: base {rec['base_sha256'][:16]} ({len(base)}B) " f"post==upload:{match}") else: rec["reason"] = ("gaps in pre-image coverage" if base is None else "no served post file to verify against") out["not_published"].append(rec) print(f"NOT PUBLISHED #{rid} {path}: {rec['reason']} " f"(covered={cov['n']}, gaps={len(cov['gaps'])}, served_posts={len(served_posts)})") json.dump(out, open(os.path.join(HERE, "published-bases.json"), "w"), indent=1) print(f"\npublished={len(out['published'])} not_published={len(out['not_published'])}") return out if __name__ == "__main__": main()