fix: harden Codex hook finalization

This commit is contained in:
thanhnv
2026-07-24 13:06:23 +07:00
parent bfebfa9d39
commit 34a6b013c0
8 changed files with 79 additions and 24 deletions
@@ -857,14 +857,39 @@ def op_finalize(req):
# summary as the closing verification control.
reasons = []
if req.get("assistant_summary"):
ok, why = h4_scan(req.get("assistant_summary"), "output")
add_evidence(rec, "H3/H7", "finalize-verify", "allow" if ok else "flag", why)
if not ok:
h4 = h4_scan(req.get("assistant_summary"), "output")
if h4 == "ok":
add_evidence(
rec, "H3/H7", "finalize-verify", "allow", "h4_ok")
elif h4 == "unavailable":
# An output that could not be scanned must never be certified.
# Match begin/pre-tool graceful degradation: keep the developer
# workflow available, but make the missing control explicit.
rec["integration_mode"] = "observed_only"
rec["certification_strength"] = "observed_only"
add_evidence(
rec, "H3/H7", "finalize-verify", "degraded",
"gate_unavailable_no_bash")
reasons.append("output_gate_unavailable")
else:
# A policy block or timeout is fail-closed for certification.
# Treat unknown statuses the same way so a future contract change
# cannot silently certify an unverified output.
why = {
"blocked": "h4_blocked",
"timeout": "h4_internal_timeout",
}.get(h4, "h4_unknown_status")
add_evidence(
rec, "H3/H7", "finalize-verify", "flag", why)
reasons.append("output_flagged:%s" % why)
# Certification decision.
strength = rec.get("certification_strength", "observed_only")
certified = True
if reasons:
# Any closing output-verification failure above is certification-fatal,
# even when every earlier admission/tool control succeeded.
certified = False
if enforcement_mode() != "enforce":
certified = False
reasons.append("observe_mode")