#!/usr/bin/env python3 """evidence_doc_validator.py — warn-only 自洽校验器 v0.1(schema:证据件格式规范 v1.1) 用法: python3 evidence_doc_validator.py [--fetch] 检查项(全部 warn-only,退出码恒为 0): [M0] 机器块存在性 —— 无 ```yaml / ```machine 围栏 → info 提示(v1 前旧件属正常) [C3] 槽内禁 scheme —— 机器块内不得出现 pin:// / metafile:// / metaapp:// [C5] 槽内全值 —— 机器块内不得出现省略号「…」(标识符一律全值裸 pinid) [R1] reading 三件套 —— 每个 basis: reading 必须伴随含两个 " | " 的 reading 行 [R2] 联署有效性 —— kind: countersign 必须带 scope 与 does_not_cover [C2] verdict 同尺 —— verdict_map 每个 verdict 须有 independent_reading [C6] retrieve_path —— 建议机器形态;纯散文形态 → warn 提示 [--fetch] —— byte_baselines 的 sha256 按 retrieve_path 回取复算 (files/content → metafile 直取;manapi /content → 提围栏体后复算) """ import sys, re, json, hashlib, urllib.request MANAPI = "https://manapi.metaid.io/content/" METAFILE = "https://file.metaid.io/metafile-indexer/api/v1/files/content/" FULL_PIN = re.compile(r"\b[0-9a-f]{64}i[0-9]+\b") SCHEME = re.compile(r"(pin|metafile|metaapp)://") def http_get(url, timeout=25): req = urllib.request.Request(url, headers={"User-Agent": "evidence-doc-validator/0.1.3"}) with urllib.request.urlopen(req, timeout=timeout) as r: return r.read() def fetch_pin_content(pin): raw = http_get(MANAPI + pin) data = json.loads(raw.decode("utf-8")) return data.get("content", "") def machine_block(content): m = re.search(r"```(?:yaml|machine)\s*\n(.*?)```", content, re.S) return m.group(1) if m else None def records(block, id_key): """把机器块切成以 '- :' 开头的记录列表 [(id, {field: value})]; 字段下的 '- item' 列表项收进 _list(v1.1 readings[] 支持)""" out, cur_id, cur, last_key = [], None, {}, None for line in block.splitlines(): m = re.match(r"\s*-\s*" + id_key + r"\s*:\s*(.+)$", line) if m: if cur_id is not None: out.append((cur_id, cur)) cur_id, cur, last_key = m.group(1).strip().strip('"'), {}, None continue if cur_id is not None: f = re.match(r"\s*([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(.*)$", line) li = re.match(r"\s*-\s+(.+)$", line) if f: cur[f.group(1)] = f.group(2).strip().strip('"') last_key = f.group(1) elif li and last_key: cur.setdefault(last_key + "_list", []).append(li.group(1).strip().strip('"')) if cur_id is not None: out.append((cur_id, cur)) return out def sha256(b): return hashlib.sha256(b).hexdigest() def fence_body(text): m = re.search(r"```\w*\n(.*?)\n```", text, re.S) return m.group(1).encode("utf-8") if m else None def validate(pin, do_fetch=False): warn, ok, info = [], [], [] content = fetch_pin_content(pin) block = machine_block(content) kind_m = re.search(r"^\s*kind\s*:\s*(\S+)", block or "", re.M) kind = kind_m.group(1) if kind_m else "(无机器块)" if block is None: info.append("[M0] 无 ```yaml/```machine 机器块(v1 之前旧件属正常,信息性)") else: for m in SCHEME.finditer(block): warn.append(f"[C3] 槽内出现 scheme:{m.group(0)}…(槽内一律裸 pinid)") break if "…" in block: warn.append("[C5] 槽内出现省略号「…」(标识符必须全值)") for cid, rec in records(block, "id"): if rec.get("basis") == "reading": entries = rec.get("readings_list") or ([rec["reading"]] if rec.get("reading") else []) if not entries: warn.append(f"[R1] claim {cid}: basis=reading 但无 reading/readings 槽") else: bad = [e for e in entries if e.count(" | ") < 2] if bad: warn.append(f"[R1] claim {cid}: {len(bad)}/{len(entries)} 条 reading 缺三件套分隔(值 | 路由 | 时刻)") else: ok.append(f"[R1] claim {cid}: readings×{len(entries)} 三件套齐") if kind == "countersign": has_scope = re.search(r"^\s*scope\s*:", block, re.M) or "scope:" in block has_dnc = "does_not_cover" in block if has_scope and has_dnc: ok.append("[R2] countersign: scope + does_not_cover 双双在(联署有效)") else: warn.append(f"[R2] countersign 缺 scope 或 does_not_cover(has_scope={bool(has_scope)}, has_dnc={has_dnc})→ 无效联署") vms = records(block, "claim_id") for vid, rec in vms: if "verdict" in rec: if rec.get("independent_reading"): ok.append(f"[C2] verdict_map {vid}: independent_reading 在") else: warn.append(f"[C2] verdict_map {vid}: 缺 independent_reading") if "retrieve_path" in block: prose_only = not FULL_PIN.search(block.split("retrieve_path", 1)[1][:400]) if prose_only: warn.append("[C6] retrieve_path 疑似纯散文形态(建议机器形态:/<全值pinid>)") else: ok.append("[C6] retrieve_path 含全值 pinid(机器形态)") if do_fetch and block: for bid, rec in records(block, "sha256"): target = bid.strip() # v0.1.1 fix: sha256 在记录 id 槽,不在字段槽(v0.1 误读空串导致假 NO-MATCH) path = rec.get("retrieve_path", "") pin_m = FULL_PIN.search(path) or FULL_PIN.search(json.dumps(rec)) if not pin_m: info.append(f"[fetch] baseline {target[:8]}…: retrieve_path 无法解析出 pinid,跳过") continue src = pin_m.group(0) try: if "manapi" in path or "content" == path.split("/")[0]: data = json.loads(http_get(MANAPI + src).decode("utf-8")) # v0.1.2 fix: 先解 JSON 再提围栏 text = data.get("content", "") body = fence_body(text) cands = [sha256(body)] if body else [] cands.append(sha256(text.encode("utf-8"))) else: b = http_get(METAFILE + src) cands = [sha256(b)] status = "MATCH" if target in cands else "NO-MATCH" (ok if status == "MATCH" else warn).append( f"[fetch] baseline {target[:8]}… ← {src[:12]}… : {status}") except Exception as e: warn.append(f"[fetch] baseline {target[:8]}… ← {src[:12]}… : 取回失败 {type(e).__name__}: {e}") print(f"== evidence_doc_validator v0.1.3 (warn-only) ==") print(f"pin : {pin}") print(f"kind : {kind}") for line in ok: print("OK ", line) for line in info: print("INFO ", line) for line in warn: print("WARN ", line) print(f"summary: ok={len(ok)} warn={len(warn)} info={len(info)} (warn-only, exit 0)") return 0 if __name__ == "__main__": args = [a for a in sys.argv[1:] if not a.startswith("--")] flags = [a for a in sys.argv[1:] if a.startswith("--")] if not args: print(__doc__) sys.exit(0) sys.exit(validate(args[0], "--fetch" in flags))