File size: 9,645 Bytes
881f9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
"""Backfill canonical scenario field into existing per-trial JSON captures.

Notebook 03's `canonical_rows` gate requires each per-trial output JSON to carry
`data["scenario"] = <input scenario object>` (with at least an `id` key). The
runners and the upstream AOB `plan-execute` CLI did not propagate this until
the run_experiment.sh post-processing step landed; pre-existing captures are
all classified as `legacy` and excluded from cross-cell aggregation.

This script walks each `benchmarks/cell_<X>/raw/<run_id>/` directory, reads the
companion `latencies.jsonl` to map output path -> source scenario file, opens
each per-trial JSON and injects `data["scenario"]` from the source scenario
file. Idempotent: if `data["scenario"]` is already a dict with an `id`, the
file is left alone.

Usage:
  python3 scripts/backfill_canonical_scenario.py            # dry-run, all cells
  python3 scripts/backfill_canonical_scenario.py --apply    # write changes
  python3 scripts/backfill_canonical_scenario.py --apply --cell B  # one cell

Exit codes: 0 always (parse errors are reported but do not fail the sweep).
"""

from __future__ import annotations

import argparse
import json
import pathlib
import sys

CELL_DIRS = {
    "A": "benchmarks/cell_A_direct",
    "B": "benchmarks/cell_B_mcp_baseline",
    "C": "benchmarks/cell_C_mcp_optimized",
    "D": "benchmarks/cell_D",
    "Y": "benchmarks/cell_Y_plan_execute",
    "Z": "benchmarks/cell_Z_hybrid",
    "ZSD": "benchmarks/cell_ZSD",
}


def repo_root_from(start: pathlib.Path) -> pathlib.Path:
    cur = start.resolve()
    for cand in [cur, *cur.parents]:
        if (cand / "benchmarks").exists() and (cand / "data" / "scenarios").exists():
            return cand
    raise SystemExit(f"Could not locate repo root from {start}")


def load_json(path: pathlib.Path) -> dict | None:
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError):
        return None


def map_output_to_scenario(
    run_dir: pathlib.Path, repo_root: pathlib.Path
) -> dict[pathlib.Path, pathlib.Path]:
    """Read latencies.jsonl and return {resolved_trial_output -> resolved_scenario_file}."""
    latencies_file = run_dir / "latencies.jsonl"
    out: dict[pathlib.Path, pathlib.Path] = {}
    if not latencies_file.exists():
        return out
    for line_num, line in enumerate(
        latencies_file.read_text(encoding="utf-8").splitlines(), start=1
    ):
        line = line.strip()
        if not line:
            continue
        try:
            row = json.loads(line)
        except json.JSONDecodeError:
            print(
                f"  WARN: {latencies_file}:{line_num} unparseable, skipping",
                file=sys.stderr,
            )
            continue
        out_path_raw = row.get("output_path")
        scen_path_raw = row.get("scenario_file")
        if not out_path_raw or not scen_path_raw:
            continue
        out_path = (
            (repo_root / out_path_raw)
            if not pathlib.Path(out_path_raw).is_absolute()
            else pathlib.Path(out_path_raw)
        )
        scen_path = (
            (repo_root / scen_path_raw)
            if not pathlib.Path(scen_path_raw).is_absolute()
            else pathlib.Path(scen_path_raw)
        )
        try:
            out_path = out_path.resolve()
        except OSError:
            pass
        try:
            scen_path = scen_path.resolve()
        except OSError:
            pass
        out[out_path] = scen_path
    return out


def _step_failed(step: dict) -> bool:
    if not isinstance(step, dict):
        return False
    if step.get("success") is False:
        return True
    if step.get("error"):
        return True
    resp = step.get("response")
    if isinstance(resp, dict) and resp.get("error"):
        return True
    return False


def _derive_success(data: dict) -> bool | None:
    raw = data.get("success")
    if isinstance(raw, bool):
        return raw
    # Match Notebook 03's history-first precedence so all three call sites
    # (run_experiment.sh post-process, this backfill, notebook
    # load_*_records) walk the same step array.
    steps = data.get("history") or data.get("trajectory") or []
    if not steps and not data.get("answer"):
        return None
    for step in steps:
        if _step_failed(step):
            return False
    return bool(data.get("answer"))


def backfill_run_dir(
    run_dir: pathlib.Path, repo_root: pathlib.Path, apply: bool
) -> dict:
    stats = {
        "checked": 0,
        "already_canonical": 0,
        "backfilled_scenario": 0,
        "backfilled_success": 0,
        "skipped_no_mapping": 0,
        "skipped_no_scenario": 0,
        "errors": 0,
    }
    if not run_dir.is_dir():
        return stats
    output_to_scenario = map_output_to_scenario(run_dir, repo_root)
    for trial_file in sorted(run_dir.glob("*.json")):
        if trial_file.name in ("meta.json", "config.json", "summary.json"):
            continue
        stats["checked"] += 1
        try:
            resolved = trial_file.resolve()
        except OSError:
            resolved = trial_file
        scen_path = output_to_scenario.get(resolved)
        if scen_path is None:
            stats["skipped_no_mapping"] += 1
            continue
        scenario = load_json(scen_path)
        if scenario is None:
            stats["skipped_no_scenario"] += 1
            continue
        payload = load_json(trial_file)
        if payload is None or not isinstance(payload, dict):
            stats["errors"] += 1
            continue

        existing = payload.get("scenario")
        scenario_already = isinstance(existing, dict) and existing.get("id")
        success_already = isinstance(payload.get("success"), bool)

        if scenario_already and success_already:
            stats["already_canonical"] += 1
            continue

        changed = False
        if not scenario_already:
            payload["scenario"] = scenario
            stats["backfilled_scenario"] += 1
            changed = True
        if not success_already:
            derived = _derive_success(payload)
            if derived is not None:
                payload["success"] = derived
                stats["backfilled_success"] += 1
                changed = True
        if changed and apply:
            trial_file.write_text(
                json.dumps(payload, indent=2, default=str) + "\n", encoding="utf-8"
            )
    return stats


def backfill_cell(
    cell_label: str, cell_path: pathlib.Path, repo_root: pathlib.Path, apply: bool
) -> dict:
    raw_dir = cell_path / "raw"
    totals = {
        "runs": 0,
        "checked": 0,
        "already_canonical": 0,
        "backfilled_scenario": 0,
        "backfilled_success": 0,
        "skipped_no_mapping": 0,
        "skipped_no_scenario": 0,
        "errors": 0,
    }
    if not raw_dir.is_dir():
        return totals
    for run_dir in sorted(p for p in raw_dir.iterdir() if p.is_dir()):
        stats = backfill_run_dir(run_dir, repo_root, apply)
        totals["runs"] += 1
        for k in (
            "checked",
            "already_canonical",
            "backfilled_scenario",
            "backfilled_success",
            "skipped_no_mapping",
            "skipped_no_scenario",
            "errors",
        ):
            totals[k] += stats[k]
        if stats["checked"]:
            print(
                f"  {cell_label}/{run_dir.name}: checked={stats['checked']} "
                f"+scen={stats['backfilled_scenario']} +success={stats['backfilled_success']} "
                f"canonical={stats['already_canonical']} no_map={stats['skipped_no_mapping']} "
                f"no_scen={stats['skipped_no_scenario']} errors={stats['errors']}"
            )
    return totals


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--apply", action="store_true", help="Actually write changes (default: dry-run)"
    )
    parser.add_argument(
        "--cell",
        action="append",
        choices=sorted(CELL_DIRS),
        help="Limit to one or more cells (default: all)",
    )
    parser.add_argument(
        "--repo-root",
        type=pathlib.Path,
        default=None,
        help="Override repo root detection",
    )
    args = parser.parse_args()

    repo_root = (
        args.repo_root.resolve()
        if args.repo_root
        else repo_root_from(pathlib.Path.cwd())
    )
    print(f"Repo root: {repo_root}")
    print(f"Mode:      {'APPLY (writing)' if args.apply else 'DRY-RUN (no changes)'}")

    cells_to_process = args.cell or sorted(CELL_DIRS)
    grand = {
        "runs": 0,
        "checked": 0,
        "already_canonical": 0,
        "backfilled_scenario": 0,
        "backfilled_success": 0,
        "skipped_no_mapping": 0,
        "skipped_no_scenario": 0,
        "errors": 0,
    }
    for cell in cells_to_process:
        cell_path = repo_root / CELL_DIRS[cell]
        print(f"== Cell {cell}: {cell_path.relative_to(repo_root)} ==")
        totals = backfill_cell(cell, cell_path, repo_root, args.apply)
        for k in grand:
            grand[k] += totals[k]
        print(
            f"  cell totals: runs={totals['runs']} checked={totals['checked']} "
            f"+scen={totals['backfilled_scenario']} +success={totals['backfilled_success']} "
            f"canonical={totals['already_canonical']}"
        )
    print("\n== Sweep totals ==")
    for k, v in grand.items():
        print(f"  {k:24s} {v}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())