#!/usr/bin/env python3 """job 2978 (triage of route 151): persist the boundary BEFORE the first research step. Same rule as job 2962's boundary: every field is READ from a record that already exists (next.out.json, brief.md, run.json) or MEASURED read-only from the harness database; nothing is retyped and the write instant is generated at write time. The previous job's boundary is left where it is -- this one is written beside the assignment it belongs to, `job2978/boundary-2978.json`. """ import datetime import glob import json import os import re import sqlite3 HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.dirname(HERE) THREAD = "8c0ac416-4b57-4d49-ad3d-3bce03c8fd53" nxt = json.load(open(os.path.join(RUN, "next.out.json"), encoding="utf-8")) run = json.load(open(os.path.join(RUN, "run.json"), encoding="utf-8")) brief = open(os.path.join(RUN, "brief.md"), encoding="utf-8").read() expires = None m = re.search(r"Expires ([^\n]+?) \(Coordinated Universal Time\)", brief) if m: expires = m.group(1).strip() time_hours = None m = re.search(r"Time: ([0-9.]+) hours", brief) if m: time_hours = float(m.group(1)) cpu_hours = None m = re.search(r"up to 16 GB and ([0-9.]+) CPU h", brief) if m: cpu_hours = float(m.group(1)) share = None m = re.search(r"Compute: ([0-9]+)% of the machine", brief) if m: share = int(m.group(1)) disk = None m = re.search(r"up to ([0-9.]+) GB of disk", brief) if m: disk = float(m.group(1)) sub_agents = "allowed, sharing the same budget" if "sub-agents allowed" in brief else None db = glob.glob(os.path.expanduser( "~/.config/freebuff-desktop/projects/TwinPrimeProject-*/desktop-v2.db"))[0] c = sqlite3.connect("file:%s?mode=ro" % db.replace("\\", "/"), uri=True) c.row_factory = sqlite3.Row rows = c.execute("select seq, role, parts_json, ts from messages where thread_id=? order by seq", (THREAD,)).fetchall() c.close() max_seq = max([r["seq"] for r in rows] or [None]) user_rows = [r for r in rows if r["role"] == "user"] last_user = user_rows[-1] try: parts = json.loads(last_user["parts_json"]) if last_user["parts_json"] else [] words = "".join(p.get("text") or "" for p in parts if isinstance(p, dict))[:200] except Exception: # noqa: BLE001 words = None now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") rec = { "job_id": nxt["job_id"], "attempt_id": nxt["attempt_id"], "type": nxt["type"], "assignment": nxt.get("session_jobs"), "mode": "general", "direction": nxt.get("direction"), "run": os.path.basename(RUN), "launch_id": run.get("launch_id"), "session": nxt["session"], "run_id": nxt["run_id"], "department_id": nxt.get("department_id"), "research_route_id": nxt.get("research_route_id"), "research_stage": None, "research_stage_source": ("the /next response for job #2978 carries no research_stage field; the " "job title begins 'Triage:' and route 151 is in state 'proposed', which " "is what the triage decides"), "purpose": nxt.get("purpose"), "protocol_version": nxt.get("protocol_version"), "guidance_version": nxt.get("guidance_version"), "instruction_url": run.get("instruction_url"), "instruction": ("the person's own words for this assignment (thread message seq %s): %r -- 'take " "the next assignment', which the department answered with job #2978; nothing else " "was supplied and no interpretation beyond the tool call is recorded" % (last_user["seq"], words)), "application": "Freebuff Desktop", "application_thread_id": THREAD, "app_db": os.path.basename(os.path.dirname(db)), "model_declared": run.get("model"), "effort_declared": run.get("effort"), "expires_at": expires, "limits": {"time_hours": time_hours, "cpu_hours": cpu_hours, "ram_gb": 16, "disk_gb": disk, "share_percent": share, "sub_agents": sub_agents}, "transcript_seq_start": last_user["seq"], "transcript_seq_last_user": last_user["seq"], "transcript_seq_max_at_boundary": max_seq, "at": now, "covered_at": now, "covered_at_source": "generated at write time by boundary-2978.py (job 2978, route 151 triage)", "note": ("this assignment's turn is OPEN at write time; the window is recorded so the return can " "cut exactly this assignment's turn later, with no inference from a timestamp"), } out = os.path.join(HERE, "boundary-2978.json") with open(out, "w", encoding="utf-8", newline="\n") as fh: json.dump(rec, fh, indent=1, sort_keys=True) fh.write("\n") print(json.dumps({"wrote": out, "seq_start": last_user["seq"], "seq_max": max_seq, "rows": len(rows), "job_id": rec["job_id"], "attempt_id": rec["attempt_id"], "route": rec["research_route_id"], "expires_at": rec["expires_at"], "limits": rec["limits"], "person_words": words}, indent=1))