#!/usr/bin/env python3 """Backfill or recompute cost fields for all staged benchmark cache jsonl files.""" from __future__ import annotations import argparse import json import sys from pathlib import Path from typing import Dict, Iterable, List PROJECT_ROOT = Path(__file__).resolve().parents[1] if str(PROJECT_ROOT) not in sys.path: sys.path.insert(0, str(PROJECT_ROOT)) from answer_utils import repair_confidence_reason_record_fields from cascade_utils import attach_dynamic_costs def _read_jsonl(path: Path) -> List[Dict]: rows: List[Dict] = [] with path.open("r", encoding="utf-8") as handle: for line in handle: line = line.strip() if line: rows.append(repair_confidence_reason_record_fields(json.loads(line))) return rows def _write_jsonl(path: Path, rows: List[Dict]) -> None: path.parent.mkdir(parents=True, exist_ok=True) with path.open("w", encoding="utf-8") as handle: for row in rows: handle.write(json.dumps(row, ensure_ascii=False) + "\n") def _iter_cache_files(root: Path) -> Iterable[Path]: for path in sorted(root.rglob("*.stage*.cached.jsonl")): if path.is_file(): yield path def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", default="cache/benchmarks", help="Stage cache root directory.") parser.add_argument( "--missing-only", action="store_true", help="Only fill missing cost fields. Default behavior is to recompute all cost fields with current pricing.", ) args = parser.parse_args() root = Path(args.root) if not root.exists(): raise FileNotFoundError(f"Cache root does not exist: {root}") force_recompute = not args.missing_only total_files = 0 total_rows = 0 for path in _iter_cache_files(root): rows = _read_jsonl(path) if not rows: print(f"[Skip] empty {path}") continue changed = attach_dynamic_costs(rows, force_recompute=force_recompute) if changed: _write_jsonl(path, rows) print(f"[Done] {path} rows={len(rows)} recompute={'yes' if force_recompute else 'missing-only'}") else: print(f"[Skip] up-to-date {path} rows={len(rows)}") total_files += 1 total_rows += len(rows) print(f"[Summary] files={total_files} rows={total_rows} mode={'recompute' if force_recompute else 'missing-only'}") return 0 if __name__ == "__main__": raise SystemExit(main())