#!/usr/bin/env python3 """Job #4548: project the served comparison window into one searchable, scrubbable file. Reads the fetched served/ return records, keeps every return with id >= the step-setter #1834 (the whole post-setter record, routeless returns included), and writes served_window.jsonl with the fields the check needs: identity, the full report text and the research object. Machine-local paths in the served report text are replaced by a placeholder (one line of #1878, route 120, uncited) and the substitution is recorded in window_scrub.json; nothing else is altered. """ import datetime import hashlib import json import os import re HERE = os.path.dirname(os.path.abspath(__file__)) SERVED = os.path.join(HERE, "served") SETTER = 1834 ABS = re.compile(r"(?", unesc)) unesc_rep = rep.replace("\\\\", "\\") if ABS.search(unesc_rep): scrub.append({"id": rec["id"], "field": "report_md", "hits": len(ABS.findall(unesc_rep))}) rep = ABS.sub("", unesc_rep) row = {"id": rec["id"], "route": rec.get("research_route_id"), "created_at": rec.get("created_at"), "status": rec.get("status"), "type": rec.get("type"), "model": rec.get("model"), "job_id": rec.get("job_id"), "report_md": rep, "research": res} for k in ("recipe_md", "finding", "human_md"): v = rec.get(k) if isinstance(v, str): uv = v.replace("\\\\", "\\") if ABS.search(uv): scrub.append({"id": rec["id"], "field": k, "hits": len(ABS.findall(uv))}) v = ABS.sub("", uv) row[k] = v rows.append(row) rows.sort(key=lambda r: r["id"]) with open(os.path.join(HERE, "served_window.jsonl"), "w", encoding="utf-8", newline="\n") as fh: for r in rows: fh.write(json.dumps(r, sort_keys=True) + "\n") with open(os.path.join(HERE, "window_scrub.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump({"served_return_sha256": files, "scrubbed": scrub, "built_at": datetime.datetime.now(datetime.timezone.utc).isoformat()}, fh, indent=1) print("served_window.jsonl: %d returns (ids %d..%d), %d scrubbed machine-local path(s): %s" % (len(rows), rows[0]["id"], rows[-1]["id"], len(scrub), ", ".join("#%d/%s" % (s["id"], s["field"]) for s in scrub) or "none")) print("post-setter returns: %d" % len([r for r in rows if r["id"] > SETTER])) print("routeless post-setter: %d" % len([r for r in rows if r["id"] > SETTER and r["route"] is None])) if __name__ == "__main__": main()