"""Agent 轨迹回放：python 08_agent_debug.py。"""
from collections import Counter

def classify(record):
    if record["tool"] != "refund":
        return "01 routing"
    if record["args"] != {"order_id":"o1","amount_cents":4200}:
        return "02 argument semantics"
    if record["result"] != "ok":
        return "03 tool failure"
    if record["extra_writes"]:
        return "04 unauthorized side effects"
    if record["end_state"] != {"o1":"refunded"}:
        return "05 final state"
    return "06 passed"

baseline = dict(tool="refund", args={"order_id":"o1","amount_cents":4200}, result="ok",
                end_state={"o1":"refunded"}, extra_writes=[])
cases = [{**baseline, **patch} for patch in (
    {}, {"tool":"status"}, {"args":{"order_id":"o1","amount_cents":42}},
    {"result":"timeout"}, {"extra_writes":["o2"]}, {"end_state":{"o1":"paid"}}
)]
for i, record in enumerate(cases):
    print("case", i, classify(record))
assert classify(cases[2]) == "02 argument semantics"
assert classify({**cases[2],"args":baseline["args"]}) == "06 passed"
print("first failure distribution:", dict(Counter(map(classify,cases))))

