#!/usr/bin/env python3 """Rounds of a captured Codex session as a teacher-forced probe set (docs/quality-gate.md, "245k equivalence", set S3). Offline; no runtime use. q245_s3.py CAPTURE_DIR OUT [--first N] [--count M] [--step S] [--max-reply T] Takes the requests N, N+S, ... (1-based index in CAPTURE_DIR/req-NNNNNN.json, M of them) of a `LELEKA_CAPTURE_DIR` capture of the port (prompt_ids and generated_ids of every served request) and writes the data-dir layout of q245_prep.py (p.ids, r_port.ids, meta.json) plus OUT/cap-s3 (the port's replay input). A request qualifies when its prompt ends with a 5-token assistant header (`<|im_start|>` assistant \n \n) and the reply is not empty; the reply is cut to --max-reply tokens (default 400). The replies are the tool calls, reasoning text and messages the port itself produced in the real Codex loop. """ import glob, json, os, sys IM_START = 248045 def cell_range(arm_dir, cell_name): """(first, count): the 1-based capture indexes of the completed requests of one cell, from the cells of the arm in start order (the capture numbers the requests of the engine process)""" cells = [] for name in os.listdir(arm_dir): cj = os.path.join(arm_dir, name, "cell.json") if os.path.exists(cj): rounds = [json.loads(l) for l in open(os.path.join(arm_dir, name, "proxy", "rounds.jsonl")) if l.strip()] ok = sum(1 for r in rounds if str(r.get("path", "")).endswith("/responses") and r.get("outcome") == "ok" and r.get("status") == 200) cells.append((json.load(open(cj)).get("start_utc"), name, ok)) cells.sort() first = 1 for _, name, ok in cells: if name == cell_name: return first, ok first += ok raise SystemExit("cell %s not found in %s" % (cell_name, arm_dir)) def main(argv): cap, out = argv[0], argv[1] opt = lambda name, d: int(argv[argv.index(name) + 1]) if name in argv else d first, count, step, max_reply = opt("--first", 1), opt("--count", 10**9), opt("--step", 1), opt("--max-reply", 400) if "--cell" in argv: # --cell ARM_DIR CELL_NAME i = argv.index("--cell") first, count = cell_range(argv[i + 1], argv[i + 2]) print("cell", argv[i + 2], "-> capture requests", first, "..", first + count - 1) files = sorted(glob.glob(os.path.join(cap, "req-*.json"))) os.makedirs(out, exist_ok=True) capdir = os.path.join(out, "cap-s3") os.makedirs(capdir, exist_ok=True) meta, k = [], 0 for idx in range(first - 1, len(files), step): if k >= count: break doc = json.load(open(files[idx])) prompt, reply = [int(x) for x in doc["prompt_ids"]], [int(x) for x in doc["generated_ids"]][:max_reply] turn_at = max((i for i, t in enumerate(prompt) if t == IM_START), default=-1) if turn_at < 0 or len(prompt) - turn_at != 5 or not reply: print("skip request", idx + 1, "header", len(prompt) - turn_at, "reply", len(reply)) continue k += 1 open(os.path.join(out, f"p{k}.ids"), "w").write(",".join(map(str, prompt))) open(os.path.join(out, f"r_port{k}.ids"), "w").write(",".join(map(str, reply))) meta.append({"k": k, "prompt_len": len(prompt), "reply_len": len(reply), "turn_at": turn_at, "header_len": 5, "source_file": os.path.basename(files[idx])}) json.dump({"index": k, "source": "port", "k": k, "max_new": len(reply), "temperature_milli": 0, "prompt_tokens": len(prompt), "prompt_ids": prompt, "generated_ids": reply, "top_p_micro": None, "top_k": None, "min_p_micro": None, "repetition_penalty_micro": None, "frequency_penalty_micro": None, "presence_penalty_micro": None, "penalty_last_n": None, "seed": None, "reused": 0, "rounds": 0, "produced": len(reply)}, open(os.path.join(capdir, "req-%06d.json" % k), "w")) json.dump({"capture": os.path.abspath(cap), "requests": meta}, open(os.path.join(out, "meta.json"), "w"), indent=1) print(len(meta), "requests; prompt lengths", [m["prompt_len"] for m in meta], "reply lengths", [m["reply_len"] for m in meta]) if __name__ == "__main__": main(sys.argv[1:])