#!/usr/bin/env python3 """Job #1833 (cross-lane synthesis): compact digest of the eight named returns. Reads only the fetched records under evidence/job1833/, prints a digest and writes artifacts/job1833-digest.json. No network. Purpose: make the eight returns comparable (id, type, lane, rung, what was claimed, what the decisive evidence was) without reading ~200 KB of reports into view. """ import json import os import re import sys try: sys.stdout.reconfigure(encoding="utf-8", errors="replace") except Exception: # noqa: BLE001 pass RUN = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) IDS = [165, 162, 161, 159, 153, 152, 151, 101] # Headings whose section carries the claim, in preference order. WANT = ("result", "verdict", "finding", "conclusion", "summary", "what this shows", "evidence", "the connection", "claim") def sections(md): """Split a markdown report into {heading: body} by ATX headings.""" out = [] cur = None for line in md.splitlines(): if line.startswith("#"): cur = [line.lstrip("#").strip(), []] out.append(cur) elif cur is not None: cur[1].append(line) return [(h, "\n".join(b).strip()) for h, b in out] def pick(md, n=900): secs = sections(md) for h, b in secs: low = h.lower() if any(w in low for w in WANT) and len(b) > 80: return h, b[:n] if secs: h, b = max(secs, key=lambda kv: len(kv[1])) return h, b[:n] return "", md[:n] def main(): dig = [] for rid in IDS: p = os.path.join(RUN, "evidence", "job1833", "return-%d" % rid, "return.json") if not os.path.exists(p): dig.append({"id": rid, "missing": True}) continue d = json.load(open(p, encoding="utf-8")) res = d.get("research") or {} rep = d.get("report_md") or d.get("human_md") or "" heads = [l.strip() for l in rep.splitlines() if l.strip().startswith("#")] h, body = pick(rep) dig.append({ "id": rid, "job_id": d.get("job_id"), "type": d.get("type"), "lane": d.get("lane_id"), "status": d.get("status"), "rung": d.get("author_rung"), "final_rung": d.get("final_rung"), "handle": d.get("handle"), "model": d.get("model"), "title": (heads[0] if heads else "")[:200], "headings": heads[:12], "research_keys": sorted(res.keys()) if res else [], "research_outcome": res.get("outcome"), "route_id": res.get("route_id"), "evidence_md": (res.get("evidence_md") or "")[:1200], "next_step_q": ((res.get("next_step") or {}).get("question") or "")[:400], "obstacle_kind": ((res.get("obstacle") or {}).get("kind")), "obstacle_statement": ((res.get("obstacle") or {}).get("statement") or "")[:700], "depends_on": res.get("depends_on"), "files": [f.get("name") for f in (d.get("files") or [])], "section_used": h, "section_body": body, "report_chars": len(rep), "cites": d.get("cites"), "dependencies": d.get("dependencies"), }) out = os.path.join(RUN, "artifacts", "job1833-digest.json") tmp = out + ".tmp" with open(tmp, "w", encoding="utf-8", newline="\n") as fh: fh.write(json.dumps(dig, indent=1, sort_keys=True) + "\n") os.replace(tmp, out) for e in dig: if e.get("missing"): print("=== #%s MISSING" % e["id"]) continue print("=== #%(id)s %(type)s lane=%(lane)s %(status)s rung=%(rung)s job=%(job_id)s " "@%(handle)s %(model)s" % e) print(" title:", e["title"][:160]) if e["research_keys"]: print(" research: outcome=%s route=%s obstacle=%s depends_on=%s" % ( e["research_outcome"], e["route_id"], e["obstacle_kind"], e["depends_on"])) print(" headings:", " | ".join(h[:60] for h in e["headings"][:7])) print(" [%s] %s" % (e["section_used"][:60], e["section_body"][:700].replace("\n", " "))) if e["files"]: print(" files:", ", ".join(e["files"])[:200]) print() print("wrote", out) return 0 if __name__ == "__main__": raise SystemExit(main())