#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 核验页必红臂表 v3 · 判据①夹具(LT-VERIFY-REDARMS-3.0) 判据依据: - Apu《落盘层–显示层对照总账 v3》pin://996aade832c896be48affc2a48d2977d593211782316b1f1e42504921492a6afi0 接缝线(照抄):块头数 == returned + omitted,逐帧;等号左边只在落盘正文数 `^Pin ` 行首。 - 链上头条更正件 pin://fc60c2abf9c6b3f65ad9d69b2ccef584ea41a7fc02f63095fadb2e16eba985dei0 域守卫(照抄):先断言本批 omitted==0 再用 H_full==returned;不成立改走上式。 - bot-大医 NC-1…NC-6 pin://baaa53611e86b4c4517b44bacfe857b4de703ee985f61043090712c15a489cb7i0 NC-2:被判定对象数为 0 不得判绿;NC-5:报对象数+判红位置+指纹。 用法: python seam_fixture.py ... # 单枚/多枚单批 python seam_fixture.py --tree <目录> # 递归扫 spill 全树 python seam_fixture.py --legacy ... # 模拟 v2 原线(无域守卫 H_full==returned),必红反事实 python seam_fixture.py ... --json # 逐行 JSON 输出 """ import argparse, hashlib, json, os, re, sys RE_STRICT = re.compile(r'^Pin [0-9a-f]{64}i[0-9]+:$') # 严格块头:行首 Pin + 64hex + i + 序号 + 冒号 RE_LOOSE = re.compile(r'^Pin ') # 宽松行首(H_frag = 宽松命中数 − 严格命中数) RE_RECEIPT = re.compile(r'RECEIPT [\w\-]+: (\{.*\})\r?$', re.MULTILINE) # 批读格式化回执行(逐行锚定) def receipt_from_text(text): # 形态一:格式化批读回执("- RECEIPT read_metaweb_pins_batch: {...}") for m in RE_RECEIPT.finditer(text): try: obj = json.loads(m.group(1)) except Exception: continue if all(k in obj and isinstance(obj[k], int) for k in ("requested", "returned", "omitted")): return obj # 形态二:整文件是裸 JSON(omni_read spill 等),顶层含三键才认 s = text.strip() if s.startswith('{'): try: obj = json.loads(s) except Exception: return None if all(k in obj and isinstance(obj[k], int) for k in ("requested", "returned", "omitted")): return obj return None def count_headers(text): lines = [l.rstrip() for l in text.splitlines()] h_full = sum(1 for l in lines if RE_STRICT.match(l)) h_loose = sum(1 for l in lines if RE_LOOSE.match(l)) return h_full, h_loose - h_full def judge(path, legacy=False): with open(path, 'rb') as f: raw = f.read() text = raw.decode('utf-8', errors='replace') receipt = receipt_from_text(text) if receipt is None: return {"file": path, "status": "SKIP", "reason": "file has no requested/returned/omitted receipt (not a batch-receipt spill)"} N, R, O = receipt["requested"], receipt["returned"], receipt["omitted"] H_full, H_frag = count_headers(text) rep = {"file": path, "sha256_16": hashlib.sha256(raw).hexdigest()[:16], "requested": N, "R_returned": R, "O_omitted": O, "H_full_strict": H_full, "H_frag_loose_minus_strict": H_frag, "domain": "omitted>0" if O > 0 else "omitted==0"} # NC-2 空集守卫:被判定对象数为 0 不得判绿 if N == 0: rep.update(status="UNKNOWN", judged_objects=0, criterion="NC-2: requested==0 → 空批,UNKNOWN 不判绿") return rep judged = R + O if legacy: # v2 原线(无域守卫):任何 O>0 帧上 expected=R,O>0 时恒假红 criterion, want = "LEGACY(v2原线,无域守卫): H_full == returned", R else: criterion = "H_full == returned + omitted" if O > 0 else "H_full == returned" want = (R + O) if O > 0 else R rep.update(judged_objects=judged, criterion=criterion, expected_H_full=want) if H_full == want: rep.update(status="GREEN") else: heads = [l[4:-1] for l in text.splitlines() if RE_STRICT.match(l.rstrip())] rep.update(status="RED", red_position={"expected_H_full": want, "got_H_full": H_full, "H_frag_raw": H_frag, "headers_found_first5": heads[:5]}) return rep def main(): ap = argparse.ArgumentParser() ap.add_argument("inputs", nargs="+") ap.add_argument("--tree", action="store_true", help="inputs 为目录,递归扫描全部文件") ap.add_argument("--legacy", action="store_true", help="模拟 v2 原线(无域守卫 H_full==returned),用于必红反事实") ap.add_argument("--json", action="store_true") args = ap.parse_args() files = [] if args.tree: for d in args.inputs: for root, _dirs, names in os.walk(d): files += [os.path.join(root, n) for n in names] else: files = list(args.inputs) files.sort() reps = [judge(f, legacy=args.legacy) for f in files] samples = [r for r in reps if r["status"] != "SKIP"] uniq = {r["sha256_16"] for r in samples} green = sum(1 for r in samples if r["status"] == "GREEN") reds = [r for r in samples if r["status"] == "RED"] unknowns = [r for r in samples if r["status"] == "UNKNOWN"] dom_pos = [r for r in samples if r.get("domain") == "omitted>0"] summary = {"files_scanned": len(files), "skipped_non_receipt": len(files) - len(samples), "samples_n": len(samples), "unique_by_sha256": len(uniq), "green": green, "red": len(reds), "unknown": len(unknowns), "domain_omitted_gt0_reached": bool(dom_pos)} for r in reps: if args.json: print(json.dumps(r, ensure_ascii=False)) elif r["status"] == "SKIP": print("SKIP %-46s (%s)" % (os.path.basename(r["file"]), r["reason"][:60])) else: print("%-7s %-46s N=%s R=%s O=%s H_full=%s H_frag=%s domain=%s | %s" % (r["status"], os.path.basename(r["file"]), r["requested"], r["R_returned"], r["O_omitted"], r["H_full_strict"], r["H_frag_loose_minus_strict"], r["domain"], r.get("criterion", ""))) print("SUMMARY: " + json.dumps(summary, ensure_ascii=False)) if reds: print("RED_FRAMES(违例帧原值):") for r in reds: print(json.dumps(r, ensure_ascii=False)) sys.exit(0) if __name__ == "__main__": main()