"""Regenerate the packaged session activity fixture from the pinned source shard.""" import argparse import hashlib import io import tempfile from pathlib import Path from urllib.request import urlopen import ijson SHA256 = "e73649253f159ae41cd5ebb10bf09927268e84342fc2f5a7378c66568b99e566" PROJECTION_SHA256 = "d329d0ea419c4a475be6863607d7e551e24f8970858f95a5b7818793087e75eb" URL = ( "https://huggingface.co/datasets/harvardMadsys/freeinference_agentic_trace/" "resolve/bb26cf72c9c20f2b05f765d88548156a74d3c253/" "release/2026-08-30_2026-09-05/traces/2026-09-01.jsonl.zst" ) def project(source): try: from compression import zstd reader = zstd.open(io.BytesIO(source), "rb") except ImportError: import zstandard reader = io.BufferedReader(zstandard.ZstdDecompressor().stream_reader(io.BytesIO(source))) # A single session can contain gigabytes of expanded prefix-block arrays. # Parse scalar fields incrementally, retaining only this projection. events = [] sessions = [] with reader: for path, event, value in ijson.parse(reader, multiple_values=True): if event in ("start_map", "end_map"): if path == "" or path.endswith("tool_calls.item.subagent"): if event == "start_map": sessions.append({"rows": []}) else: session = sessions.pop() for timestamp, step in session["rows"]: events.append((timestamp, session["id"], step)) elif event == "end_map" and path.endswith("requests.item"): session = sessions[-1] session["rows"].append((session.pop("end"), session.pop("step"))) elif event == "string" and (path == "session_id" or path.endswith("subagent.session_id")): sessions[-1]["id"] = value elif event == "number": if path.endswith("requests.item.end_ms"): sessions[-1]["end"] = value elif path.endswith("requests.item.step"): sessions[-1]["step"] = value events.sort() entities = {} rows = [] for timestamp, session, _ in events: entity = entities.setdefault(session, len(entities)) rows.append(f"{timestamp - events[0][0]}\t{entity}\n") return "".join(rows).encode("ascii") def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--source", type=Path, help="Use an existing copy of the source .zst file.") parser.add_argument("--output", type=Path, default=Path(__file__).with_name("activity.tsv")) args = parser.parse_args() if args.source: source = args.source.read_bytes() else: with urlopen(URL, timeout=90) as response: source = response.read() if hashlib.sha256(source).hexdigest() != SHA256: raise SystemExit("Source checksum mismatch") data = project(source) if hashlib.sha256(data).hexdigest() != PROJECTION_SHA256: raise SystemExit("Projection checksum mismatch") with tempfile.NamedTemporaryFile(dir=args.output.parent, delete=False) as tmp: tmp.write(data) Path(tmp.name).replace(args.output) print(hashlib.sha256(data).hexdigest(), args.output) if __name__ == "__main__": main()