289 lines
12 KiB
Python
289 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""HEL-862 负例复现脚本:总管独立复核《HEL-861-独立复核.md》S6 肯定验证缺口。
|
|
|
|
零真实模型、零网络(socket 硬拒绝 + HF 离线)、零本地嵌入:S6 判据为纯函数
|
|
代入(真实 s6_result 构造器同一入口);既有判据不回归部分直接复用 audit861
|
|
的两组纯函数节(S6 全套正负例 + S2/S3 总判据)。同一脚本在修复前(a5ef23f)
|
|
与修复后代码上均可运行:
|
|
|
|
修复前(--phase prefix,在 a5ef23f 工作树上运行)→ 总管复核四组错误回答
|
|
(否定承诺仍过 / 否定已结仍过 / 对象与动作串错仍过 / 仅陈述告知但未肯定
|
|
承诺仍过)全部 pass=true,正确面试承诺已结/未结正例仍 pass=true
|
|
修复后(默认 postfix)→ 同一真实 s6_result 四组全部判失败,正确正例、
|
|
audit861 既有正例(已结/未结/未结同义"还没有收到"/audit860/rework 形态)
|
|
继续通过;audit861 既有负例与 S2/S3 总判据不回归
|
|
|
|
补正口径(固定合成语料领域内,限定受支持表达):正确对象、承诺动作、动作
|
|
肯定性与已结/未结状态绑定到同一项声明——单一小句内"承诺动词(前缀无否定
|
|
语素)→告知动作→面试结果"依次成立;已结/未结状态短语所在小句前缀无否定
|
|
语素。不以对象词/动作词在整段各出现一次凑数,不靠不断加禁词或整句黑名单
|
|
冒充肯定验证;无法证明则保守判失败。不新增模型、不做通用语义引擎。
|
|
|
|
退出码真实依赖正负例:除全部用例满足预期外,还对每个用例做翻转自检
|
|
(flip 任一用例的实际结果,用例总判必翻转);任一翻转不改变结论则退出 1。
|
|
|
|
用法:python -m spike.hel857.audit862 [--phase prefix|postfix] [输出json路径]
|
|
退出码:prefix=0 当且仅当四组反例全部复现且正例保留;postfix=0 当且仅当
|
|
四组反例全部判失败、正例保留、既有判据不回归、权威账与既有证据只读未动、
|
|
翻转自检全部通过。权威嵌入账只读快照核对(不补种不回退不双扣)。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import socket
|
|
import sqlite3
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
LEDGER = Path("/home/xiaomo/multica_workspaces/hello-af23464d222e/"
|
|
"hel-859-1f079e50f551/workdir/audit-data/embed_ledger.sqlite3")
|
|
os.environ["HEL857_EMBED_LEDGER"] = str(LEDGER)
|
|
os.environ.setdefault("HF_HUB_OFFLINE", "1")
|
|
os.environ.setdefault("TRANSFORMERS_OFFLINE", "1")
|
|
|
|
|
|
def deny_network(*a, **k):
|
|
raise RuntimeError("audit862 blocks network")
|
|
|
|
|
|
socket.socket.connect = deny_network
|
|
socket.socket.connect_ex = deny_network
|
|
|
|
from . import ab_scenarios as A
|
|
from .audit861 import section_s2_s3_no_regression, section_s6_affirmative
|
|
|
|
OUT = {"real_network_allowed": False, "real_model_requests": 0,
|
|
"local_embeddings": 0}
|
|
PHASE = "postfix"
|
|
|
|
|
|
def ledger_snapshot():
|
|
with sqlite3.connect(f"file:{LEDGER}?mode=ro", uri=True) as c:
|
|
return {"counts": dict(c.execute("select kind,sum(items) from embed_ops "
|
|
"group by kind")),
|
|
"rows": c.execute("select count(*) from embed_ops").fetchone()[0]}
|
|
|
|
|
|
PRESERVED_EVIDENCE = [
|
|
"ab_scenarios_run1.json", "ab_scenarios.json", "rework858_offline.json",
|
|
"rework859_offline.json", "rework860_offline.json", "audit859_prefix.json",
|
|
"audit859_postfix.json", "audit859_reuse860.json", "audit860_prefix.json",
|
|
"audit860_postfix.json", "audit861_prefix.json", "audit861_postfix.json"]
|
|
|
|
|
|
def evidence_hashes():
|
|
res = {}
|
|
for name in PRESERVED_EVIDENCE:
|
|
p = REPO / "spike/hel857/results" / name
|
|
res[name] = (hashlib.sha256(p.read_bytes()).hexdigest()
|
|
if p.exists() else "missing")
|
|
return res
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 总管复核四组(review861.py 原文逐字,同一真实 s6_result 入口)
|
|
def s6_answer(s):
|
|
return {"answer": s, "ok": True}
|
|
|
|
|
|
ORCH6 = {"pass": True, "status_annotation_in_block": True,
|
|
"unresolved_contrast": {"built": True, "self_consistent": True}}
|
|
|
|
REVIEW_GOOD_CLOSED = "你答应周五晚告诉我面试结果,现在已经告诉我了。"
|
|
REVIEW_GOOD_OPEN = "你答应周五晚告诉我面试结果,还没有告诉我。"
|
|
# 四组反例的预期极性按阶段翻转:prefix=复现(pass=true),postfix=判失败。
|
|
REVIEW_NEGATIVE_NAMES = (
|
|
"negative_promise_action_negated", "negative_closed_state_negated",
|
|
"negative_object_bound_to_other_action", "negative_no_promise_asserted")
|
|
# (名称, 已结侧回答, 未结侧回答)
|
|
REVIEW_CASES = {
|
|
"positive_interview": (REVIEW_GOOD_CLOSED, REVIEW_GOOD_OPEN),
|
|
"negative_promise_action_negated": (
|
|
"你未曾答应周五晚告诉我面试结果,现在已经告诉我了。",
|
|
REVIEW_GOOD_OPEN),
|
|
"negative_closed_state_negated": (
|
|
"你答应周五晚告诉我面试结果,但并非已经告诉我。",
|
|
REVIEW_GOOD_OPEN),
|
|
"negative_object_bound_to_other_action": (
|
|
"你答应周五晚告诉我体检结果,现在已经告诉我了;面试结果是另一件事。",
|
|
"你答应周五晚告诉我体检结果,还没有告诉我;面试结果是另一件事。"),
|
|
"negative_no_promise_asserted": (
|
|
"面试结果现在已经告诉我了。", REVIEW_GOOD_OPEN),
|
|
}
|
|
|
|
|
|
def review_expected(name: str) -> bool:
|
|
"""四组反例:prefix 期预期 pass=true(复现),postfix 期预期判失败。"""
|
|
if name in REVIEW_NEGATIVE_NAMES:
|
|
return PHASE == "prefix"
|
|
return True
|
|
|
|
# audit861 既有正负例的预期极性(不回归)
|
|
HELD_S6_EXPECT = {
|
|
"negative_wrong_object_medical_check": False,
|
|
"negative_unknown_closed_state": False,
|
|
"negative_closed_is_negated": False,
|
|
"positive_interview": True,
|
|
"positive_open_synonym_not_received": True,
|
|
"held_S6_resolved_answer_says_open": False,
|
|
"held_S6_both_answers_no_knowledge": False,
|
|
"held_S6_unresolved_claims_result_received": False,
|
|
"held_S6_unresolved_same_synonym_received": False,
|
|
"held_S6_positive_audit860": True,
|
|
"held_S6_positive_rework": True,
|
|
"held_S6_status_missing": False,
|
|
}
|
|
HELD_S2S3_EXPECT = {
|
|
"held_S2_details_absent_from_both_contexts": False,
|
|
"held_S2_unknown_answers": False,
|
|
"held_S2_positive": True,
|
|
"held_S2_request_failed": False,
|
|
"held_S3_unknown_answers": False,
|
|
"held_S3_correct_answers": True,
|
|
}
|
|
|
|
|
|
def section_review_four_groups() -> dict:
|
|
out = {}
|
|
for name, (closed, opened) in REVIEW_CASES.items():
|
|
result = A.s6_result(ORCH6, s6_answer(closed), s6_answer(opened))
|
|
mu = result["layers"]["model_use"]
|
|
out[name] = {
|
|
"resolved_input": closed, "open_input": opened,
|
|
"expected_pass": review_expected(name),
|
|
"actual_pass": result["pass"],
|
|
"layers": {
|
|
"resolved_states_promise": mu["resolved_states_promise"],
|
|
"resolved_affirms_closed": mu["resolved_affirms_closed"],
|
|
"unresolved_states_promise": mu["unresolved_states_promise"],
|
|
"unresolved_affirms_open": mu["unresolved_affirms_open"],
|
|
},
|
|
"result": result,
|
|
}
|
|
return out
|
|
|
|
|
|
def _case_table(review: dict, held_s6: dict, held_s2s3: dict) -> dict:
|
|
"""全部用例的实际 pass 与预期极性(退出码公式输入)。"""
|
|
table: dict[str, bool] = {}
|
|
expect: dict[str, bool] = {}
|
|
for name, c in review.items():
|
|
key = f"review.{name}"
|
|
table[key] = bool(c["actual_pass"])
|
|
expect[key] = bool(c["expected_pass"])
|
|
for name, res in held_s6.items():
|
|
key = f"held_s6.{name}"
|
|
table[key] = bool(res["pass"])
|
|
expect[key] = HELD_S6_EXPECT[name]
|
|
for name, res in held_s2s3.items():
|
|
key = f"held_s2s3.{name}"
|
|
table[key] = bool(res["pass"])
|
|
expect[key] = HELD_S2S3_EXPECT[name]
|
|
return table, expect
|
|
|
|
|
|
def _all_cases_match(table: dict, expect: dict) -> bool:
|
|
return all(table[k] == expect[k] for k in expect)
|
|
|
|
|
|
def _flip_dependence(table: dict, expect: dict) -> dict:
|
|
"""翻转自检:任一用例实际值翻转必须改变用例总判(退出码真实依赖正负例)。"""
|
|
base = _all_cases_match(table, expect)
|
|
flips = {}
|
|
for k in expect:
|
|
flipped = dict(table)
|
|
flipped[k] = not flipped[k]
|
|
flips[k] = _all_cases_match(flipped, expect) != base
|
|
return {"base_all_match": base, "every_flip_changes_verdict": all(
|
|
flips.values()), "per_case": flips}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
def main(argv: list[str]) -> dict:
|
|
global PHASE
|
|
argv = list(argv)
|
|
rest: list[str] = []
|
|
PHASE = "postfix"
|
|
it = iter(argv[1:])
|
|
for tok in it:
|
|
if tok == "--phase":
|
|
PHASE = next(it, "postfix")
|
|
elif tok.startswith("--phase="):
|
|
PHASE = tok.split("=", 1)[1]
|
|
elif not tok.startswith("--"):
|
|
rest.append(tok)
|
|
args = rest
|
|
OUT["phase"] = PHASE
|
|
|
|
commit = subprocess.run(["git", "rev-parse", "--short", "HEAD"],
|
|
capture_output=True, text=True,
|
|
cwd=str(REPO)).stdout.strip()
|
|
OUT["commit"] = commit
|
|
OUT["ledger_before"] = ledger_snapshot()
|
|
OUT["preserved_evidence_before"] = evidence_hashes()
|
|
|
|
OUT["review_four_groups"] = section_review_four_groups()
|
|
held_s6 = section_s6_affirmative()
|
|
held_s2s3 = section_s2_s3_no_regression()
|
|
OUT["held_s6_affirmative"] = held_s6
|
|
OUT["held_s2_s3_no_regression"] = held_s2s3
|
|
|
|
table, expect = _case_table(OUT["review_four_groups"], held_s6, held_s2s3)
|
|
OUT["case_expect"] = {k: bool(v) for k, v in expect.items()}
|
|
OUT["case_actual"] = {k: bool(v) for k, v in table.items()}
|
|
OUT["all_cases_match_expectation"] = _all_cases_match(table, expect)
|
|
OUT["flip_dependence"] = _flip_dependence(table, expect)
|
|
|
|
OUT["ledger_after"] = ledger_snapshot()
|
|
OUT["ledger_unchanged"] = OUT["ledger_before"] == OUT["ledger_after"]
|
|
OUT["preserved_evidence_after"] = evidence_hashes()
|
|
OUT["preserved_evidence_unchanged"] = (
|
|
OUT["preserved_evidence_before"] == OUT["preserved_evidence_after"])
|
|
|
|
r4 = OUT["review_four_groups"]
|
|
# ---- prefix:四组反例全部复现(pass=true)且正确正例保留 ----
|
|
four_groups_reproduced = (
|
|
all(r4[n]["actual_pass"] for n in REVIEW_NEGATIVE_NAMES)
|
|
and r4["positive_interview"]["actual_pass"])
|
|
# ---- postfix:四组反例全部判失败,正例保留 ----
|
|
four_groups_rejected = (
|
|
not any(r4[n]["actual_pass"] for n in REVIEW_NEGATIVE_NAMES)
|
|
and r4["positive_interview"]["actual_pass"])
|
|
OUT["summary"] = {
|
|
"phase": PHASE,
|
|
"four_groups_reproduced": four_groups_reproduced,
|
|
"four_groups_rejected": four_groups_rejected,
|
|
"all_cases_match_expectation": OUT["all_cases_match_expectation"],
|
|
"flip_dependence_holds": OUT["flip_dependence"][
|
|
"every_flip_changes_verdict"],
|
|
"ledger_unchanged": OUT["ledger_unchanged"],
|
|
"preserved_evidence_unchanged": OUT["preserved_evidence_unchanged"],
|
|
"pass": (four_groups_reproduced and PHASE == "prefix"
|
|
and OUT["all_cases_match_expectation"]
|
|
and OUT["flip_dependence"]["every_flip_changes_verdict"]
|
|
and OUT["ledger_unchanged"]
|
|
and OUT["preserved_evidence_unchanged"])
|
|
or (four_groups_rejected and PHASE == "postfix"
|
|
and OUT["all_cases_match_expectation"]
|
|
and OUT["flip_dependence"]["every_flip_changes_verdict"]
|
|
and OUT["ledger_unchanged"]
|
|
and OUT["preserved_evidence_unchanged"]),
|
|
}
|
|
out_path = Path(args[0]) if args else (
|
|
REPO / "spike/hel857/results/"
|
|
f"audit862_{'prefix' if PHASE == 'prefix' else 'postfix'}.json")
|
|
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
out_path.write_text(json.dumps(OUT, ensure_ascii=False, indent=2),
|
|
encoding="utf-8")
|
|
print(json.dumps(OUT["summary"] | {"out": str(out_path)},
|
|
ensure_ascii=False))
|
|
return OUT
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(0 if main(sys.argv)["summary"]["pass"] else 1)
|