#!/usr/bin/env python3 """Prefill fixed-latency program, step-ledger rows A1-A3 and B2: the "empty request" probe (test tooling, stdlib only). A stack under test answers requests that do almost no model work, so what is left is the path of a request INTO the engine (HTTP and gateway, tokenizer and prompt assembly, prefix-cache lookup and import) and the first token: tiny a one-line prompt, one output token -> floor of HTTP + gateway + tokenizer + first token cold request SEQ of a recorded cell as recorded, one output token (the prompt is read, not cached; not a probe of the intake, a reference) cached the same body again, REPS times, one output token -> intake + tokenizer of a long prompt + prefix-cache lookup/import + first token tiny again, REPS times Same body and same client for every stack (the recorded forward bodies of a codex-compare cell, like replay_hash.py), so the differences are the stacks'. One JSON line per request: probe, i, wall and first-event seconds, input/cached/output tokens. Usage: pf_probe.py --cell DIR --url http://127.0.0.1:8081 --model qwen-iq3 --key-file FILE --out probe.jsonl [--seq 29] [--reps 5] """ import argparse import gzip import json import os import sys import time import urllib.error import urllib.request def send(url, hdr, body, timeout): data = json.dumps(body).encode() t0 = time.time() rec = {"req_bytes": len(data)} try: req = urllib.request.Request(url.rstrip("/") + "/v1/responses", data=data, headers=hdr, method="POST") with urllib.request.urlopen(req, timeout=timeout) as r: rec["status"] = r.status first = None last = b"" for line in r: if first is None: first = time.time() - t0 if line.startswith(b"data:") and b"response.completed" in line: last = line rec["t_first_s"] = round(first or 0, 4) if last: u = json.loads(last[5:]).get("response", {}).get("usage", {}) rec["input_tokens"] = u.get("input_tokens") rec["output_tokens"] = u.get("output_tokens") rec["cached_tokens"] = (u.get("input_tokens_details") or {}).get("cached_tokens") except urllib.error.HTTPError as e: rec["error"] = "HTTP %d %s" % (e.code, e.read()[:300].decode("utf-8", "replace")) except Exception as e: # recorded, the probe goes on rec["error"] = repr(e)[:200] rec["wall_s"] = round(time.time() - t0, 4) return rec def main(): ap = argparse.ArgumentParser() ap.add_argument("--cell", action="append", required=True) ap.add_argument("--url", required=True) ap.add_argument("--model", required=True) ap.add_argument("--key-file", default="") ap.add_argument("--out", required=True) ap.add_argument("--max-requests", type=int, default=0, help="accepted for the replay_hash.py command line; unused") ap.add_argument("--seq", type=int, default=29, help="1-based number of the recorded request used as the long prompt") ap.add_argument("--reps", type=int, default=5) ap.add_argument("--timeout", type=int, default=900) a = ap.parse_args() key = open(a.key_file).read().strip() if a.key_file else "" rounds = os.path.join(a.cell[0], "proxy", "rounds") names = sorted(n for n in os.listdir(rounds) if n.endswith(".fwd.json.gz")) body = json.loads(gzip.open(os.path.join(rounds, names[a.seq - 1])).read()) body["model"] = a.model body["temperature"] = 0.0 body["max_output_tokens"] = 1 # a vanilla session's reasoning blob is refused by Leleka (HTTP 400); its own sessions keep None there for it in body.get("input") or []: if isinstance(it, dict) and it.get("type") == "reasoning" and it.get("encrypted_content"): it["encrypted_content"] = None sid = body.get("prompt_cache_key") or "" hdr = {"Content-Type": "application/json", "Accept": "text/event-stream", "session-id": sid, "thread-id": sid, "x-codex-window-id": sid + ":0"} if key: hdr["Authorization"] = "Bearer " + key tiny = { "model": a.model, "input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "Say ok."}]}], "stream": True, "temperature": 0.0, "max_output_tokens": 1, } plan = [("tiny", tiny)] * 2 + [("cold", body)] + [("cached", body)] * a.reps + [("tiny", tiny)] * a.reps out = open(a.out, "a", encoding="utf-8") seen = {} for label, b in plan: seen[label] = seen.get(label, 0) + 1 rec = {"probe": label, "i": seen[label], "seq": a.seq} rec.update(send(a.url, hdr, b, a.timeout)) out.write(json.dumps(rec) + "\n") out.flush() print(json.dumps(rec), flush=True) time.sleep(0.5) return 0 if __name__ == "__main__": sys.exit(main())