from __future__ import annotations import argparse import json import os from pathlib import Path from .formatting import derive_output_label, discover_source_files, export_release_ready_trace, format_and_sort_trace from .time_windows import infer_time_window def _default_output_root(input_path: str | Path) -> Path: resolved = Path(input_path) if resolved.is_dir(): return resolved.parent / f"{resolved.name}-formatted" if resolved.parent.name.startswith("trace-"): return resolved.parent.parent / f"{resolved.parent.name}-formatted" return resolved.parent / f"{resolved.stem}-formatted" def _resolve_raw_output_path(args: argparse.Namespace) -> Path: if args.output: explicit = Path(args.output) return explicit if explicit.stem.endswith("-raw") else explicit.with_name(f"{explicit.stem}-raw.jsonl") output_root = Path(args.output_root) if args.output_root else _default_output_root(args.input) source_files = discover_source_files(args.input) time_window = infer_time_window( source_files, start_time=None if args.no_truncate_to_window else args.start_time, end_time=None if args.no_truncate_to_window else args.end_time, ) if (args.start_time and args.end_time) or (not args.no_truncate_to_window) else None label = derive_output_label(args.input, time_window=time_window) return output_root / f"{label}-raw.jsonl" def _resolve_release_output_path(args: argparse.Namespace) -> Path: if args.output: explicit = Path(args.output) return explicit if not explicit.stem.endswith("-raw") else explicit.with_name(f"{explicit.stem[:-4]}.jsonl") input_path = Path(args.input) if input_path.stem.endswith("-raw"): return input_path.with_name(f"{input_path.stem[:-4]}.jsonl") return input_path.with_suffix(".jsonl") def build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description="Format raw trace shards into one time-sorted trace jsonl.") subparsers = parser.add_subparsers(dest="command", required=True) format_parser = subparsers.add_parser( "format", help="Format a raw trace directory or one .jsonl/.jsonl.zst file into one unified *-raw jsonl.", ) format_parser.add_argument("input", help="Raw trace directory or one .jsonl/.jsonl.zst file.") format_parser.add_argument( "--output", default=None, help="Explicit raw output jsonl path. Defaults to a sibling trace-*-formatted/