78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
"""看一条 rollout 轨迹在干什么:工具调用序列 + 结果摘要 + claim/verdict。
|
|
|
|
python3 scripts/trace.py runs/<run>/<job>/rollouts/00 [--full]
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def text_of(content) -> str:
|
|
if isinstance(content, str):
|
|
return content
|
|
out = []
|
|
for c in content or []:
|
|
if isinstance(c, dict) and c.get("type") == "text":
|
|
out.append(c.get("text", ""))
|
|
return "".join(out)
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("rollout_dir")
|
|
ap.add_argument("--full", action="store_true")
|
|
ap.add_argument("--width", type=int, default=220)
|
|
a = ap.parse_args()
|
|
rd = Path(a.rollout_dir)
|
|
ev = rd / "agent" / "events.jsonl"
|
|
if not ev.exists():
|
|
print("no events.jsonl"); return 1
|
|
n = turns = 0
|
|
w = a.width if not a.full else 10_000
|
|
for line in ev.open(errors="replace"):
|
|
try:
|
|
e = json.loads(line)
|
|
except json.JSONDecodeError:
|
|
continue
|
|
t = e.get("type")
|
|
if t == "turn_end":
|
|
turns += 1
|
|
msg = e.get("message") or {}
|
|
txt = text_of(msg.get("content"))
|
|
if txt.strip():
|
|
print(f"--- turn {turns} assistant: {txt.strip()[:w]}")
|
|
elif t == "tool_execution_start":
|
|
n += 1
|
|
args = e.get("args") or {}
|
|
short = args.get("command") or args.get("path") or json.dumps(args, ensure_ascii=False)
|
|
print(f"[{n}] {e.get('toolName')}: {str(short)[:w]}")
|
|
elif t == "tool_execution_end":
|
|
r = e.get("result") or {}
|
|
txt = text_of(r.get("content") if isinstance(r, dict) else r)
|
|
flag = "ERR " if e.get("isError") else ""
|
|
print(f" -> {flag}{txt.strip()[:w].replace(chr(10), ' | ')}")
|
|
elif t == "agent_end":
|
|
print(f"=== agent_end: {len(e.get('messages', []))} messages, {turns} turns, {n} tool calls")
|
|
elif t in ("auto_retry_start", "auto_retry_end", "compaction_start"):
|
|
print(f"!!! {t}: {json.dumps(e, ensure_ascii=False)[:w]}")
|
|
for f in ("claim.json", "verdict.json", "rollout.json"):
|
|
p = rd / f
|
|
if p.exists():
|
|
d = json.loads(p.read_text())
|
|
keep = {k: d.get(k) for k in ("terminal", "state", "error_class", "summary", "failed_gate", "claim_consistent", "review", "exit_code", "wall_s", "timeout_reason") if k in d}
|
|
print(f"{f}: {json.dumps(keep, ensure_ascii=False)}")
|
|
g = rd / "agent" / "guard.log"
|
|
if g.exists():
|
|
lines = [l for l in g.read_text().splitlines() if '"kind": "init"' not in l and '"kind":"init"' not in l]
|
|
print(f"guard.log: {len(lines)} 次越界拦截")
|
|
for l in lines[-3:]:
|
|
print(" ", l[:w])
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|