111 lines
4.5 KiB
Python
111 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""persist_writer_run._semantic_verdict 的离线测试。
|
|
|
|
语义状态固化到候选行是接受通道 DB 兜底的依据,绑定/哈希校验必须失败关闭:
|
|
报告版本、候选哈希、候选版本、上下文哈希、运行 ID 任一不一致都拒绝落库。
|
|
|
|
跑法:.venv/bin/python tests/skills/write-next-chapter/test_semantic_verdict.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
import pathlib
|
|
import sys
|
|
import unittest
|
|
|
|
PROJECT_ROOT = pathlib.Path(__file__).resolve().parents[3]
|
|
SCRIPT_DIR = PROJECT_ROOT / "muse" / "content" / "work" / "skills" / "generate" / "write-next-chapter" / "scripts"
|
|
if str(SCRIPT_DIR) not in sys.path:
|
|
sys.path.insert(0, str(SCRIPT_DIR))
|
|
|
|
import persist_writer_run as writer_persist # noqa: E402
|
|
|
|
|
|
CANDIDATE_SHA = "a" * 64
|
|
CONTEXT_SHA = "sha256:" + "b" * 64
|
|
|
|
|
|
def _context() -> dict:
|
|
return {
|
|
"runId": "run-prod-work12-ch2-x",
|
|
"contextSnapshot": {"contextSha256": CONTEXT_SHA},
|
|
}
|
|
|
|
|
|
def _report(*, status: str = "passed", candidate_sha: str = "sha256:" + CANDIDATE_SHA,
|
|
candidate_version=1) -> dict:
|
|
body = {
|
|
"schemaVersion": "semantic-detection-v3",
|
|
"runId": "run-prod-work12-ch2-x",
|
|
"sampleId": "writer-ch2",
|
|
"opaqueArmId": "production",
|
|
"inputSha256": "sha256:" + "1" * 64,
|
|
"candidateVersion": candidate_version,
|
|
"candidateSha256": candidate_sha,
|
|
"contextSnapshotSha256": CONTEXT_SHA,
|
|
"modelReceiptSha256": "sha256:" + "2" * 64,
|
|
"status": status,
|
|
"claims": [], "findings": [], "assertionVerdicts": [],
|
|
"hardConstraintVerdicts": [], "newSettingCandidates": [], "evidenceGaps": [],
|
|
}
|
|
body["reportSha256"] = "sha256:" + writer_persist.hashlib.sha256(
|
|
writer_persist._json(body).encode("utf-8")).hexdigest()
|
|
return body
|
|
|
|
|
|
class SemanticVerdictTest(unittest.TestCase):
|
|
def test_bound_passed_report_yields_status_and_bare_sha(self) -> None:
|
|
report = _report()
|
|
status, sha = writer_persist._semantic_verdict(
|
|
report, _context(), CANDIDATE_SHA, 1)
|
|
self.assertEqual(status, "passed")
|
|
self.assertEqual(sha, report["reportSha256"][len("sha256:"):])
|
|
|
|
def test_failed_and_needs_evidence_statuses_pass_through(self) -> None:
|
|
for status in ("failed", "needs_evidence"):
|
|
report = _report(status=status)
|
|
got, _ = writer_persist._semantic_verdict(report, _context(), CANDIDATE_SHA, 1)
|
|
self.assertEqual(got, status)
|
|
|
|
def test_unbound_candidate_sha_rejected(self) -> None:
|
|
report = _report(candidate_sha="sha256:" + "f" * 64)
|
|
with self.assertRaises(writer_persist.WriterPersistenceError):
|
|
writer_persist._semantic_verdict(report, _context(), CANDIDATE_SHA, 1)
|
|
|
|
def test_unbound_candidate_version_rejected(self) -> None:
|
|
report = _report(candidate_version=2)
|
|
with self.assertRaises(writer_persist.WriterPersistenceError):
|
|
writer_persist._semantic_verdict(report, _context(), CANDIDATE_SHA, 1)
|
|
|
|
def test_unbound_context_sha_rejected(self) -> None:
|
|
report = _report()
|
|
report["contextSnapshotSha256"] = "sha256:" + "c" * 64
|
|
with self.assertRaises(writer_persist.WriterPersistenceError):
|
|
writer_persist._semantic_verdict(report, _context(), CANDIDATE_SHA, 1)
|
|
|
|
def test_tampered_report_hash_rejected(self) -> None:
|
|
report = _report()
|
|
tampered = copy.deepcopy(report)
|
|
tampered["status"] = "passed"
|
|
tampered["reportSha256"] = report["reportSha256"]
|
|
tampered["claims"] = [{"claimId": "forged"}] # 改内容不改哈希
|
|
with self.assertRaises(writer_persist.WriterPersistenceError):
|
|
writer_persist._semantic_verdict(tampered, _context(), CANDIDATE_SHA, 1)
|
|
|
|
def test_bad_version_or_status_rejected(self) -> None:
|
|
bad_version = _report()
|
|
bad_version["schemaVersion"] = "semantic-detection-v2"
|
|
with self.assertRaises(writer_persist.WriterPersistenceError):
|
|
writer_persist._semantic_verdict(bad_version, _context(), CANDIDATE_SHA, 1)
|
|
bad_status = _report()
|
|
bad_status["status"] = "approved"
|
|
with self.assertRaises(writer_persist.WriterPersistenceError):
|
|
writer_persist._semantic_verdict(bad_status, _context(), CANDIDATE_SHA, 1)
|
|
with self.assertRaises(writer_persist.WriterPersistenceError):
|
|
writer_persist._semantic_verdict(None, _context(), CANDIDATE_SHA, 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|