#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #4545: build the ship-able comparison-window artefacts from the fetched served record. Reads ./served/return-*.json (fetched by fetch_4545.py) and writes: step.json the assigned step, extracted from the live brief, verbatim corpus_after.jsonl one line per return recorded strictly after the step-setter #1837 scan.json per-return rows for the same window, with the step-vocabulary scan Idempotent, stdlib only, deterministic (no clock reads). Every text file is written UTF-8 with newline="\\n". """ import glob import hashlib import json import os HERE = os.path.dirname(os.path.abspath(__file__)) SERVED = os.path.join(HERE, "served") CUT_OWNER = 1837 BRIEF = os.path.join(HERE, "..", "..", ".solveathome", "private", "runs", "launch-sv37hvl1lkqlgqdny8jx821m", "receipts", "next_assignment.json") # the step's own vocabulary: a word only counts when the step itself uses it STEP_WORDS = ["sahdated", "1.1.3", "INSTANT_FIELDS", "json.dump", "unresolved", "hashed-artifact", "test_recognition_2940", "population_2940", "job2830", "reader_table_2940", "exemption"] ATTACH_RE = ("sahdated", "2940", "2830", "producers") def write_json(name, obj): with open(os.path.join(HERE, name), "w", encoding="utf-8", newline="\n") as fh: json.dump(obj, fh, indent=1, sort_keys=True) def main(): brief = json.load(open(BRIEF, encoding="utf-8"))["body"]["brief_md"] start = brief.index("The step:") end = brief.index("\n\nReturns to compare") step = json.loads(brief[start + len("The step:"):end].strip()) write_json("step.json", {"job_id": "4545", "attempt_id": "d2d82bbec6e7d29c6fe71fbcd43c2fc6", "route_id": "148", "step": step}) recs = {} for p in sorted(glob.glob(os.path.join(SERVED, "return-*.json"))): with open(p, "rb") as fh: raw = fh.read() r = json.loads(raw) r["_sha256"] = hashlib.sha256(raw).hexdigest() recs[r["id"]] = r cutoff = recs[CUT_OWNER]["created_at"] after = sorted((r for r in recs.values() if r["created_at"] > cutoff), key=lambda r: (r["created_at"], r["id"])) rows, lines = [], [] for r in after: text = "\n".join([r.get("report_md") or "", json.dumps(r.get("research") or {}, sort_keys=True), json.dumps(r.get("verification_plan") or {}, sort_keys=True)]) low = text.lower() hits = sorted({w for w in STEP_WORDS if w.lower() in low}) names = [f.get("name") or "" for f in (r.get("files") or [])] attach = sorted({a for a in ATTACH_RE for n in names if a in n.lower()}) rows.append({"id": r["id"], "job_id": r.get("job_id"), "created_at": r["created_at"], "route": r.get("research_route_id"), "type": r.get("type"), "status": r.get("status"), "model": r.get("model"), "return_sha256": r["_sha256"], "files": names, "vocab_hits": hits, "attach_hits": attach, "strong": len(hits) >= 3}) lines.append(json.dumps({ "id": r["id"], "job_id": r.get("job_id"), "created_at": r["created_at"], "route": r.get("research_route_id"), "type": r.get("type"), "status": r.get("status"), "model": r.get("model"), "return_sha256": r["_sha256"], "files": names, "text": text, }, sort_keys=True)) with open(os.path.join(HERE, "corpus_after.jsonl"), "w", encoding="utf-8", newline="\n") as fh: fh.write("\n".join(lines) + "\n") write_json("scan.json", {"cut_owner": CUT_OWNER, "cutoff": cutoff, "n_returns": len(recs), "n_after": len(after), "rows": rows}) print("window: %d return records fetched, %d strictly after #%d; strong vocab: %s" % (len(recs), len(after), CUT_OWNER, [r["id"] for r in rows if r["strong"]])) print("route-148 rows after #1837: %s" % [r["id"] for r in rows if str(r["route"]) == "148"]) print("step sha256: %s" % hashlib.sha256( json.dumps(step, sort_keys=True, separators=(",", ":")).encode()).hexdigest()) if __name__ == "__main__": main()