# SPDX-License-Identifier: Apache-2.0 """Rebuild the scaled Weka records without loading a model or tokenizer.""" import argparse import copy from pathlib import Path import orjson from huggingface_hub import hf_hub_download SOURCE = "semianalysisai/cc-traces-weka-062126-256k" REVISION = "8fecd2fc56694469f758f0afbbb6335ad3043740" TOKEN_DIVISOR = 8 TIME_DIVISOR = 8 IDLE_CAP_SECONDS = 1 # AgentX uses this gap threshold to distinguish a new stream from a continuation. STREAM_GAP_SECONDS = 3600 OUTPUT_LIMIT = 10_000 MODIFICATION_NOTICE = ( f"Modified for agentx-mini from {SOURCE}@{REVISION}, published by SemiAnalysis " "under Apache-2.0. Token lengths scaled by 8; timeline compressed with long " "stream-separation gaps retained; sessions filtered to fewer than 10,000 " "requested output tokens. See README.md for details and LICENSE for terms." ) def requests(trace): for entry in trace["requests"]: yield from entry["requests"] if entry["type"] == "subagent" else (entry,) def make_timeline(calls): """Use one shared clock; retain long gaps that identify separate streams.""" intervals = sorted((r["t"], r["t"] + r["api_time"]) for r in calls) origin, end = intervals[0] gaps = [] for start, stop in intervals[1:]: if start > end: gaps.append((end, start)) end = max(end, stop) def timeline(seconds): value = (seconds - origin) / TIME_DIVISOR for start, end in gaps: if seconds <= start: break original_width = end - start scaled_width = original_width / TIME_DIVISOR if original_width > STREAM_GAP_SECONDS: # The runtime idle cap skips this wait after graph reconstruction. target_width = min(original_width, STREAM_GAP_SECONDS + 1) else: target_width = min(scaled_width, IDLE_CAP_SECONDS) fraction = min(seconds - start, original_width) / original_width value -= fraction * (scaled_width - target_width) return value return timeline def scale_prefix(entry): # Round the combined prefix once so separate rounding cannot add tokens. tools, system = entry.get("tool_tokens", 0), entry.get("system_tokens", 0) scaled_tools = (tools + TOKEN_DIVISOR - 1) // TOKEN_DIVISOR total = (tools + system + TOKEN_DIVISOR - 1) // TOKEN_DIVISOR if "tool_tokens" in entry: entry["tool_tokens"] = scaled_tools if "system_tokens" in entry: entry["system_tokens"] = total - scaled_tools def distill_trace(raw): calls = list(requests(raw)) requested_output = sum(max(1, (r["out"] + TOKEN_DIVISOR - 1) // TOKEN_DIVISOR) for r in calls) if requested_output >= OUTPUT_LIMIT: return None block_size = raw["block_size"] if block_size % TOKEN_DIVISOR or any(r["in"] != len(r["hash_ids"]) * block_size for r in calls): raise ValueError("Expected complete hash blocks divisible by the token scale") timeline = make_timeline(calls) def duration(start, seconds): return max(0.0, timeline(start + seconds) - timeline(start)) trace = copy.deepcopy(raw) trace.pop("totals", None) trace["block_size"] = block_size // TOKEN_DIVISOR scale_prefix(trace) for request in requests(trace): request["in"] //= TOKEN_DIVISOR request["out"] = (request["out"] + TOKEN_DIVISOR - 1) // TOKEN_DIVISOR start = request["t"] request["t"] = timeline(start) request["api_time"] = duration(start, request["api_time"]) if request.get("ttft") is not None: request["ttft"] = duration(start, request["ttft"]) if request.get("think_time") is not None: think = request["think_time"] request["think_time"] = duration(start - think, think) for entry in trace["requests"]: scale_prefix(entry) if entry["type"] == "subagent": start = entry["t"] entry["t"] = timeline(start) if entry.get("duration_ms") is not None: # Map millisecond endpoints before subtraction to retain precision. end = start + entry["duration_ms"] / 1000 entry["duration_ms"] = round( max(0.0, timeline(end) * 1000 - timeline(start) * 1000) ) entry["total_tokens"] = sum(r["in"] + r["out"] for r in entry["requests"]) # Weka's optional, opaque totals field carries the change notice without # adding an unknown schema field or changing any replay request. calls = list(requests(trace)) trace["totals"] = { "input_tokens": sum(r["in"] for r in calls), "output_tokens": sum(r["out"] for r in calls), "modification_notice": MODIFICATION_NOTICE, } return trace def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--input", type=Path, help="Original traces.jsonl; otherwise download the pinned HF source" ) parser.add_argument("--output", type=Path, required=True, help="New output directory") args = parser.parse_args() source = args.input or Path( hf_hub_download(SOURCE, "traces.jsonl", repo_type="dataset", revision=REVISION) ) args.output.mkdir(parents=True, exist_ok=False) count = 0 with source.open("rb") as inputs, (args.output / "traces.jsonl").open("wb") as output: for line in inputs: trace = distill_trace(orjson.loads(line)) if trace is not None: output.write(orjson.dumps(trace) + b"\n") count += 1 print(f"Wrote {count} sessions to {args.output / 'traces.jsonl'}") if __name__ == "__main__": main()