-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathanalyze.py
More file actions
286 lines (253 loc) · 10.2 KB
/
Copy pathanalyze.py
File metadata and controls
286 lines (253 loc) · 10.2 KB
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
"""``clawbench-analyze`` — aggregate error analysis over a batch of run outputs (#159).
Reads a directory of completed run outputs (each ``<run>/data/interception.json``,
optionally ``<run>/reward.json``), reuses the per-run classifier, and produces an
aggregate report: Stage-1 (interception) and Stage-2 (judged) rates, a per-category
breakdown, a failure taxonomy, the interceptor false-positive check, and the
self-report-vs-actual gap. Output as Markdown and/or JSON.
"""
from __future__ import annotations
import argparse
import json
import re
import sys
from collections import Counter
from pathlib import Path
from typing import Any
from clawbench.runner.run_support.results import classify_run
# heuristic: an agent message claiming the task is finished
_CLAIM_RE = re.compile(
r"\b(task (is )?(complete|completed|done|finished|accomplished)|"
r"successfully (completed|submitted|saved|booked)|i (have|'ve) (completed|finished|done))\b",
re.IGNORECASE,
)
def _read_json(path: Path) -> Any:
try:
return json.loads(path.read_text())
except (OSError, json.JSONDecodeError):
return None
def _task_category(
run_dir: Path,
task: dict[str, Any] | None,
run_meta: dict[str, Any] | None = None,
) -> str:
"""Category from task metadata, else derived from the task-id name segments."""
if isinstance(run_meta, dict):
for key in ("metaclass", "category"):
if run_meta.get(key):
return str(run_meta[key])
if isinstance(task, dict):
meta = task.get("metadata")
if isinstance(meta, dict):
for key in ("metaclass", "category"):
if meta.get(key):
return str(meta[key])
# e.g. "v2-536-daily-life-shopping-etsy" -> "daily-life-shopping"
task_name = (
str(run_meta.get("test_case"))
if isinstance(run_meta, dict) and run_meta.get("test_case")
else run_dir.name
)
parts = task_name.split("-")
if len(parts) >= 4 and parts[0].startswith("v"):
return "-".join(parts[2:-1]) or "uncategorized"
if len(parts) >= 3 and parts[0].isdigit():
return "-".join(parts[1:-1]) or "uncategorized"
return "uncategorized"
def _claimed_success(run_dir: Path) -> bool:
msgs = run_dir / "data" / "agent-messages.jsonl"
if not msgs.is_file():
return False
try:
with msgs.open(errors="replace") as f:
for line in f:
try:
event = json.loads(line)
except json.JSONDecodeError:
continue
stack = [event]
while stack:
value = stack.pop()
if isinstance(value, dict):
if (
value.get("role") == "assistant"
or value.get("type") == "assistant"
):
if _CLAIM_RE.search(json.dumps(value, ensure_ascii=False)):
return True
continue
stack.extend(value.values())
elif isinstance(value, list):
stack.extend(value)
except OSError:
return False
return False
def discover_runs(runs_dir: Path) -> list[Path]:
"""Discover canonical runs recursively, with an interception-only fallback."""
runs = {p.parent for p in runs_dir.rglob("run-meta.json")}
runs.update(p.parent.parent for p in runs_dir.rglob("data/interception.json"))
return sorted(runs)
def run_summary(run_dir: Path) -> dict[str, Any]:
"""Per-run: task id/category, Stage-1 intercept, Stage-2 judged, failure class."""
run_meta = _read_json(run_dir / "run-meta.json")
interception = _read_json(run_dir / "data" / "interception.json")
if isinstance(run_meta, dict) and isinstance(run_meta.get("intercepted"), bool):
intercepted = run_meta["intercepted"]
else:
intercepted = bool(
isinstance(interception, dict) and interception.get("intercepted")
)
task = _read_json(run_dir / "data" / "task.json") or _read_json(
run_dir / "task.json"
)
judged = None
judge = _read_json(run_dir / "judge.json") or _read_json(
run_dir / "data" / "judge.json"
)
if isinstance(judge, dict) and isinstance(judge.get("match"), bool):
judged = judge["match"]
elif isinstance(run_meta, dict) and isinstance(run_meta.get("judge_match"), bool):
judged = run_meta["judge_match"]
else:
reward = _read_json(run_dir / "reward.json")
if isinstance(reward, dict) and isinstance(reward.get("reward"), (int, float)):
judged = float(reward["reward"]) >= 1.0
cls = classify_run(run_dir, intercepted, recording_required=False)
meta_metrics = run_meta.get("run_metrics") if isinstance(run_meta, dict) else None
actions = meta_metrics.get("actions") if isinstance(meta_metrics, dict) else None
return {
"task": (
str(run_meta.get("test_case"))
if isinstance(run_meta, dict) and run_meta.get("test_case")
else run_dir.name
),
"category": _task_category(
run_dir,
task if isinstance(task, dict) else None,
run_meta if isinstance(run_meta, dict) else None,
),
"intercepted": intercepted,
"judged": judged,
"result_category": (
run_meta.get("result_category")
if isinstance(run_meta, dict) and run_meta.get("result_category")
else cls.get("result_category")
),
"actions": actions
if isinstance(actions, int)
else cls.get("metrics", {}).get("actions", 0),
"claimed_success": _claimed_success(run_dir),
}
def analyze_batch(runs_dir: Path) -> dict[str, Any]:
"""Aggregate the per-run summaries into an error-analysis report dict."""
runs = [run_summary(r) for r in discover_runs(runs_dir)]
n = len(runs)
intercepted = sum(1 for r in runs if r["intercepted"])
have_judge = [r for r in runs if r["judged"] is not None]
judged_pass = sum(1 for r in have_judge if r["judged"])
# per-category Stage-1 breakdown
by_cat: dict[str, dict[str, int]] = {}
for r in runs:
c = by_cat.setdefault(r["category"], {"n": 0, "intercepted": 0})
c["n"] += 1
c["intercepted"] += int(r["intercepted"])
# validity checks
zero_action_intercepted = sum(
1 for r in runs if r["actions"] == 0 and r["intercepted"]
)
# self-report gap: claimed success but did NOT pass (judged False, or not intercepted)
def _failed(r: dict[str, Any]) -> bool:
return (r["judged"] is False) or (r["judged"] is None and not r["intercepted"])
claimed_but_failed = sum(1 for r in runs if r["claimed_success"] and _failed(r))
claimed_total = sum(1 for r in runs if r["claimed_success"])
return {
"n_runs": n,
"stage1_intercepted": intercepted,
"stage1_rate": round(intercepted / n, 4) if n else 0.0,
"stage2_judged_of": len(have_judge),
"stage2_pass": judged_pass,
"stage2_rate": round(judged_pass / len(have_judge), 4) if have_judge else None,
"failure_taxonomy": dict(Counter(r["result_category"] for r in runs)),
"by_category": {
k: {**v, "rate": round(v["intercepted"] / v["n"], 4)}
for k, v in sorted(by_cat.items())
},
"interceptor_false_positives": zero_action_intercepted, # should be 0
"self_report_claimed": claimed_total,
"self_report_claimed_but_failed": claimed_but_failed,
}
def format_report(stats: dict[str, Any]) -> str:
n = stats["n_runs"]
lines = ["# ClawBench batch error analysis", ""]
lines.append(f"- **Runs:** {n}")
lines.append(
f"- **Stage-1 intercepted:** {stats['stage1_intercepted']}/{n} "
f"({stats['stage1_rate']:.0%})"
)
if stats["stage2_rate"] is not None:
lines.append(
f"- **Stage-2 judged pass:** {stats['stage2_pass']}/{stats['stage2_judged_of']} "
f"({stats['stage2_rate']:.0%})"
)
lines.append(
f"- **Interceptor false-positives** (0-action but intercepted): "
f"{stats['interceptor_false_positives']} (should be 0)"
)
if stats["self_report_claimed"]:
lines.append(
f"- **Self-report gap:** {stats['self_report_claimed_but_failed']}/"
f"{stats['self_report_claimed']} runs that claimed success actually failed"
)
lines += ["", "## Failure taxonomy", ""]
for k, v in sorted(stats["failure_taxonomy"].items(), key=lambda kv: -kv[1]):
lines.append(f"- {k or 'unknown'}: {v}")
lines += [
"",
"## Per-category Stage-1",
"",
"| category | n | intercepted | rate |",
"|---|--:|--:|--:|",
]
for cat, v in stats["by_category"].items():
lines.append(f"| {cat} | {v['n']} | {v['intercepted']} | {v['rate']:.0%} |")
return "\n".join(lines) + "\n"
def build_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
prog="clawbench-analyze",
description="Aggregate error analysis over a batch of ClawBench run outputs.",
)
p.add_argument(
"--runs-dir",
type=Path,
required=True,
help="Batch output dir (contains <run>/data/)",
)
p.add_argument(
"--out", type=Path, default=None, help="Write the Markdown report here"
)
p.add_argument(
"--json",
action="store_true",
help="Print the stats as JSON instead of Markdown",
)
return p
def main(argv: list[str] | None = None) -> int:
args = build_parser().parse_args(argv)
if not args.runs_dir.is_dir():
print(f"ERROR: runs dir not found: {args.runs_dir}", file=sys.stderr)
return 1
stats = analyze_batch(args.runs_dir)
if stats["n_runs"] == 0:
print(f"ERROR: no runs found under {args.runs_dir}", file=sys.stderr)
return 1
if args.json:
print(json.dumps(stats, indent=2))
else:
report = format_report(stats)
if args.out:
args.out.write_text(report)
print(f"Wrote report to {args.out}")
else:
print(report)
return 0
if __name__ == "__main__":
raise SystemExit(main())