File size: 1,706 Bytes
04d5e92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Backfill failure_mode.json for every successful unit that lacks it."""
from __future__ import annotations

import json
import sys
import time
from pathlib import Path

from failure_mode_classifier import classify_and_write

OUTPUT_ROOT = Path("/opt/manipulationarena/assets")
STATE = OUTPUT_ROOT / "docker-live-runs" / "feedback-batch-state.json"


def main() -> int:
    state = json.loads(STATE.read_text(encoding="utf-8"))
    units = state.get("units") or {}
    done = skipped = failed = 0
    for key, rec in sorted(units.items()):
        if rec.get("status") != "success":
            continue
        hit = None
        for it in (rec.get("iterations") or []):
            if it.get("attack_success") is True and it.get("run_dir"):
                hit = it
        if hit is None:
            continue
        run_dir = Path(hit["run_dir"])
        channel_dir = run_dir.parents[2]
        if (channel_dir / "failure_mode.json").is_file():
            skipped += 1
            continue
        result = classify_and_write(run_dir, {"attack_success": True,
                                              "raw_result": json.loads((run_dir / "result.json").read_text(encoding="utf-8")).get("evaluation", {}).get("raw_result") or {}},
                                     iteration=hit.get("iteration"))
        if result:
            done += 1
            print("OK", key, "->", result)
        else:
            failed += 1
            print("FAIL", key)
        time.sleep(0.4)
    print(json.dumps({"classified": done, "already_present": skipped, "failed": failed}, ensure_ascii=False))
    return 0 if failed == 0 else 1


if __name__ == "__main__":
    sys.exit(main())