File size: 2,572 Bytes
734d13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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())