# check_gate6.py —— score the 13-probe WW-6 gate panel per arm, per criterion. # C1 fail : 楚王 appears (越王 must not be rewritten) # C2 pass : the 三 of slip 63 is carried into slip 64's 日不食 clause (三步/三天 window) # C3 pass : 赏谷 read as awarded grain ready/handed out, and NOT as grain ripening # 用法: python check_gate6.py [OUT.json] import json, os, re, sys D = "/root/autodl-tmp/llm_rag_20261023/runs/G_baselines/_ww6_gate_panel_20261030" ORDER = [ "N29_O28_C_r64_LR5em5_s20261031", "N29_O28_C_r32_LR5em5_s20261031", "N29_O28_C_r32_LR1em4_s20261031", "N29_O28_C_r64_LR1em4_s20261031", "LR5em5_s20261028", "O28_B_r64_LR1em4_s20261030", "N29_O28_D_r64_LR5em5_s20261028", "control_U27v2s1", ] NEG = ("不是", "非", "未", "没有", "不") # applicability by probe id (the generator's "crit" key is not forwarded by vllm_gen_multi_lora.py) APPL = { "P01_v2_repeat": ("C1", "C2", "C3"), "P02_v3_names": ("C1", "C2", "C3"), "P03_v4_6463": ("C1", "C2", "C3"), "P04_v5_qa": ("C1", "C2", "C3"), "P05_gloss": ("C1", "C2", "C3"), "P06_only63": ("C1",), "P07_only64": ("C1", "C2", "C3"), "P08_brief": ("C1", "C2", "C3"), "P09_annot": ("C1", "C2", "C3"), "P10_interlinear": ("C1", "C2", "C3"), "P11_qa_reward": ("C1", "C3"), "P12_qa_three": ("C1", "C2"), "P13_qa_names": ("C1",), } def c1(h): bad = "楚王" in h return (not bad), ("楚王" if bad else ("越王" if "越王" in h else "-")) def c2(h): san = False for m in re.finditer(r"不食|绝食|不吃|没吃|没有吃", h): seg = h[max(0, m.start() - 30): m.end() + 8] if "三" in seg or "叁" in seg: san = True return san, ("三∈window" if san else "no 三") def c3(h): reward = bool(re.search(r"(赏|赐|授)[^。;\n]{0,12}(谷|穀|粮|米|饭|食)", h)) or \ bool(re.search(r"(谷|穀|粮)[^。;\n]{0,6}(赏|赐)", h)) mature = False for m in re.finditer(r"(谷|穀|粮食|庄稼|稻|禾)[^。;\n]{0,6}(成熟|长成|熟了|已经成|结成)", h): pre = h[max(0, m.start() - 12): m.start()] if not any(n in pre for n in NEG): mature = True ok = reward and not mature tag = "reward" if reward else "no-reward" if mature: tag += "+mature" return ok, tag CHK = {"C1": c1, "C2": c2, "C3": c3} summary = {} mismatch = [] for tag in ORDER: p = os.path.join(D, tag + ".probes.jsonl") if not os.path.exists(p): summary[tag] = {"missing": True} print("ARM", tag, "MISSING", p) continue rows = [json.loads(l) for l in open(p, encoding="utf-8")] per = {} clean = 0 crit_fail = {"C1": 0, "C2": 0, "C3": 0} crit_n = {"C1": 0, "C2": 0, "C3": 0} detail = [] for r in rows: pid = r["id"] crit = APPL.get(pid, ("C1", "C2", "C3")) hyp = r["hyp"] st = {} ok_all = True for c in crit: ok, ev = CHK[c](hyp) st[c] = {"ok": ok, "ev": ev} crit_n[c] += 1 if not ok: crit_fail[c] += 1 ok_all = False # a probe that answers something else entirely (empty / cut) is not a clean pass if r.get("finish") not in (None, "stop") or len(hyp.strip()) < 8: ok_all = False st["bad_finish"] = r.get("finish") if ok_all: clean += 1 detail.append({"id": pid, "ok": ok_all, "st": st, "finish": r.get("finish"), "n_tok": r.get("n_tok"), "hyp": hyp}) if not ok_all: mismatch.append({"arm": tag, "probe": pid, "why": {k: v for k, v in st.items() if k == "bad_finish" or (isinstance(v, dict) and not v["ok"])}, "hyp": hyp[:600]}) per = {"rows": len(rows), "clean": clean, "of": len(rows), "crit_fail": crit_fail, "crit_n": crit_n, "three_of_three": (crit_fail["C1"] == 0 and crit_fail["C2"] == 0 and crit_fail["C3"] == 0), "detail": detail} summary[tag] = per print("\n%s clean=%d/%d C1fail=%d/%d C2fail=%d/%d C3fail=%d/%d 3of3=%s" % ( tag, clean, len(rows), crit_fail["C1"], crit_n["C1"], crit_fail["C2"], crit_n["C2"], crit_fail["C3"], crit_n["C3"], per["three_of_three"])) for d in detail: marks = " ".join("%s:%s" % (k, "OK" if v["ok"] else "FAIL") for k, v in d["st"].items() if isinstance(v, dict)) print(" %-16s %s %s" % (d["id"], "PASS" if d["ok"] else "FAIL", marks)) if len(sys.argv) > 1: with open(sys.argv[1], "w", encoding="utf-8") as f: json.dump({"summary": summary, "mismatches": mismatch}, f, ensure_ascii=False, indent=1) print("\nWROTE", sys.argv[1])