Download prepare_trace.py from KaiDecker/slopcodebench-reproduction: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/spaces/KaiDecker/slopcodebench-reproduction/resolve/main/prepare_trace.py
- Command line
-
hf download hf://spaces/KaiDecker/slopcodebench-reproduction/prepare_trace.py
-
curl -L -o prepare_trace.py https://huggingface.co/spaces/KaiDecker/slopcodebench-reproduction/resolve/main/prepare_trace.py
3.75 kB
| """Export a redacted event stream from the five saved Codex checkpoints.""" | |
| import argparse | |
| from collections import Counter | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import re | |
| EVENT_TYPES = {"thread.started", "turn.started", "item.started", "item.updated", | |
| "item.completed", "turn.completed", "error"} | |
| ITEM_TYPES = {"agent_message", "command_execution", "file_change", "todo_list"} | |
| ITEM_STATUS = {"in_progress", "completed", "failed"} | |
| TOKEN_FIELDS = ("input_tokens", "cached_input_tokens", "output_tokens") | |
| COMMAND_CATEGORIES = { | |
| "tests": r"\b(?:pytest|unittest)\b", | |
| "source_edit": r"\bapply_patch\b", | |
| "source_search": r"\b(?:rg|grep|find)\b", | |
| "version_control": r"\bgit\b", | |
| "python": r"\bpython(?:3)?\b", | |
| } | |
| def export(source): | |
| for checkpoint in range(1, 6): | |
| path = source / f"checkpoint_{checkpoint}" / "agent" / "stdout.jsonl" | |
| data = path.read_bytes() | |
| yield {"checkpoint": checkpoint, "event": "source", "sha256": hashlib.sha256(data).hexdigest()} | |
| aliases = {} | |
| for number, line in enumerate(data.decode("utf-8-sig").splitlines(), 1): | |
| if not line.strip(): | |
| continue | |
| event = json.loads(line) | |
| kind = event.get("type") | |
| row = {"checkpoint": checkpoint, "sequence": number, | |
| "event": kind if kind in EVENT_TYPES else "other"} | |
| if item := event.get("item"): | |
| item_id = item.get("id") | |
| if item_id is not None: | |
| row["item"] = aliases.setdefault(item_id, len(aliases) + 1) | |
| item_kind = item.get("type") | |
| if item_kind in ITEM_TYPES: | |
| row["item_type"] = item_kind | |
| if item.get("status") in ITEM_STATUS: | |
| row["status"] = item["status"] | |
| if type(item.get("exit_code")) is int: | |
| row["exit_code"] = item["exit_code"] | |
| if isinstance(item.get("changes"), list): | |
| row["file_change_count"] = len(item["changes"]) | |
| if isinstance(item.get("items"), list): | |
| row["plan_item_count"] = len(item["items"]) | |
| if isinstance(command := item.get("command"), str): | |
| row["command_category"] = next( | |
| (label for label, expression in COMMAND_CATEGORIES.items() | |
| if re.search(expression, command)), "other" | |
| ) | |
| removed = [key for key in ("text", "command", "aggregated_output", "changes", "items") | |
| if item.get(key)] | |
| if removed: | |
| row["redacted_fields"] = removed | |
| if event.get("message"): | |
| row.setdefault("redacted_fields", []).append("message") | |
| if isinstance(event.get("usage"), dict): | |
| row["usage"] = {key: event["usage"][key] for key in TOKEN_FIELDS | |
| if type(event["usage"].get(key)) is int} | |
| yield row | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("task_directory", type=Path, help="Saved l2m directory with checkpoint_1 through checkpoint_5") | |
| parser.add_argument("output", type=Path, help="New local JSONL output file") | |
| args = parser.parse_args() | |
| rows = list(export(args.task_directory)) | |
| with args.output.open("x", encoding="utf-8") as stream: | |
| for row in rows: | |
| stream.write(json.dumps(row) + "\n") | |
| counts = Counter(row["checkpoint"] for row in rows if row["event"] != "source") | |
| print(f"Exported {sum(counts.values())} events from {len(counts)} checkpoints.") | |
| if __name__ == "__main__": | |
| main() | |