"""Agent 数据切分：python 07_agent_data.py。默认只生成合成数据。"""
import hashlib
import re

def signature(text):
    normalized = re.sub(r"\d+", "<id>", re.sub(r"\s+", "", text.casefold()))
    return hashlib.sha256(normalized.encode()).hexdigest()

rows = [
    ("train", "f1", "o1", "请查订单 101 的退款资格", "query"),
    ("train", "f1", "o1", "请查订单 202 的退款资格", "query"),
    ("dev", "f2", "o2", "用户未确认：申请退款 301", "clarify"),
    ("test", "f3", "o3", "写入已提交但回包丢失 401", "verify"),
]

def audit(items):
    first = {}
    for split, family, entity, text, oracle in items:
        for key in (("family",family),("entity",entity),("text",signature(text))):
            if key in first and first[key] != split:
                raise ValueError(f"{key[0]} crosses {first[key]} and {split}")
            first[key] = split
    return len(first)

def oracle_query(before, after):
    return before == after

print("clean manifest:", audit(rows), "unique keys")
try:
    audit(rows+[("test","f4","o4","请查订单 999 的退款资格","query")])
except ValueError as exc:
    print("expected split failure:", exc)
else:
    raise AssertionError("paraphrase escaped")
assert oracle_query({"status":"paid"},{"status":"paid"})
assert not oracle_query({"status":"paid"},{"status":"refunded"})

