scripts/evaluate_gate0v_composite_release.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/evaluate_gate0v_composite_release.py · SHA-256: 2be0252340222fe4…
raise ValueError("lot_id and bank_id must be non-empty trimmed strings")
if not _is_valid_sha256_hex(construct_manifest_sha256):
raise ValueError(f"Invalid construct_manifest_sha256={construct_manifest_sha256!r}")
ctx, ctx_err = _resolve_governed_manifest_and_arm_v2_round8(
arm_id=arm_id,
canonical_manifest_path=canonical_manifest_path,
openislet_root=openislet_root,
)
if ctx is None:
raise ValueError(f"Governed canonical manifest resolution failed for modeled receipt: {ctx_err}")
manifest_sha = str(ctx["manifest_sha256"])
if canonical_arm_manifest_sha256 is not None and str(canonical_arm_manifest_sha256) != manifest_sha:
raise ValueError(
f"FAIL_CANONICAL_MANIFEST_SHA256_MISMATCH: caller supplied {canonical_arm_manifest_sha256!r} != governed bytes {manifest_sha!r}"
)
product_id = str(ctx["product_id"])
bank_origin = str(ctx["bank_origin"])
solver_digest = _canonical_sha256_v2_round5(composite_result)
return _seal_gate0v_receipt_v2_round5({
"schema_revision": "v2_round5_unified",
"record_id": _make_canonical_gate0v_record_id_v2_round9("G0V-MOD", product_id, lot_id),
"gate_source": "GATE-0V",
"product_id": product_id,
"lot_id": lot_id,
"bank_id": bank_id,
"bank_origin": bank_origin,
"canonical_arm_manifest_sha256": manifest_sha,
"construct_manifest_sha256": construct_manifest_sha256,
"evidence_tier": "MODELED_COMPUTATIONAL_ONLY",
"evidence_class": "MODELED_COMPUTATIONAL_ONLY",
"operational_wetlab_authority": False,
"plumbing_control_only": False,
"decision": str(composite_result["composite_verdict"]),
"authority_scope": "MODELED_COMPUTATIONAL_ONLY",
"bank_qualified": False,
"promotion_authorized": False,
"physical_qualification_complete": False,
"release_authorizing": False,
"modeled_qualification_only": True,
"qualification_scope": str(composite_result.get("qualification_scope") or QUALIFICATION_SCOPE_MODELED_PENDING_PHYSICAL),
"producer_composite_verdict": str(composite_result["composite_verdict"]),
"producer_composite_result_sha256": solver_digest,
"lineages_verified": sorted(_GY030_REQUIRED_5_LINEAGES),
"whole_graft_stressed_tested": False,
})
_SYNTHETIC_FORBIDDEN_TOP_LEVEL_SIMULATION_KEYS_V2_ROUND13 = (
"owner_gate0v_qualification_tier",
"owner_gate0v_verdict",
"lineages_verified",
"whole_graft_stressed_tested",
"estimand1_u_esc",
"estimand2_u_cond",
"caspase_fold_activity",
"five_virus_max_fold_vs_wt",
"nhs20_cdc_c5b9_u95_pct",
)
def _is_synthetic_plumbing_installation_v2_round13(trust_ctx: Dict[str, Any] | None) -> bool:
"""Audit R12-06: Determine whether a verified trust_ctx belongs to a synthetic/plumbing control
installation (as opposed to an OWNER_PROVISIONED_PRODUCTION installation)."""
if not isinstance(trust_ctx, dict):
return False
t_mode = str(trust_ctx.get("provenance_mode") or "")
i_mode = str(trust_ctx.get("installation_provenance_mode") or "")
inst_id = str(trust_ctx.get("installation_id") or "")
is_prod = (
t_mode == "OWNER_PROVISIONED_PRODUCTION"
and i_mode == "OWNER_PROVISIONED_PRODUCTION_INSTALLATION"
and not any(
tok in s.upper()
for s in (t_mode, i_mode, inst_id)
for tok in _SYNTHETIC_ISSUER_FORBIDDEN_MARKERS_V2_ROUND10
)
)
return not is_prod
def _finalize_gate0v_cli_receipt(
raw_receipt: Dict[str, Any],
*,
is_synthetic_plumbing: bool,
) -> Dict[str, Any]:
"""Audit R12-06: Single unified receipt finalizer routed across EVERY return path (PASS,
FAIL/adverse, AND HOLD) of `issue_gate0v_physical_receipt_from_governed_root`.
Whenever `is_synthetic_plumbing` is True, strips ALL production-named simulation/evaluation
fields from top level into `synthetic_plumbing_simulation`, enforces all 5 authority flags
strictly False, sets uniform SYNTHETIC_FIXTURE / SYNTHETIC_PLUMBING_CONTROL_FIXTURE /
plumbing_control_only=True typing, assigns a safe non-authorizing decision
(`SYNTHETIC_SELF_TEST_PASS` / `SYNTHETIC_SELF_TEST_FAIL` / `SYNTHETIC_SELF_TEST_HOLD`),
and seals `archive_sha256`."""
body = copy.deepcopy({k: v for k, v in raw_receipt.items() if k not in ("archive_sha256", "record_sha256")})
if not is_synthetic_plumbing:
return _seal_gate0v_receipt_v2_round5(body)
orig_decision = str(body.get("decision") or "")
orig_scope = str(body.get("authority_scope") or "")
orig_ev_tier = str(body.get("evidence_tier") or "")
orig_bank_qual = bool(body.get("bank_qualified", False))
orig_promo = bool(body.get("promotion_authorized", False))
orig_phys = bool(body.get("physical_qualification_complete", False))
orig_rel_auth = bool(body.get("release_authorizing", False))
if orig_decision.startswith("PASS_") or orig_decision == "SYNTHETIC_SELF_TEST_PASS" or orig_phys is True:
sim_outcome = "PASS"
safe_decision = "SYNTHETIC_SELF_TEST_PASS"
synth_prefix = "G0V-SYNTH"
elif orig_decision.startswith("HOLD_") or orig_decision == "SYNTHETIC_SELF_TEST_HOLD" or orig_ev_tier == "UNAUTHENTICATED_HOLD":
sim_outcome = "HOLD"
safe_decision = "SYNTHETIC_SELF_TEST_HOLD"
synth_prefix = "G0V-SYNTH-HOLD"
else:
sim_outcome = "FAIL"
safe_decision = "SYNTHETIC_SELF_TEST_FAIL"
synth_prefix = "G0V-SYNTH-FAIL"
qual_tier_val = body.pop("owner_gate0v_qualification_tier", None)
eval_verdict_val = body.pop("owner_gate0v_verdict", None)
lineages_val = body.pop("lineages_verified", None)
whole_graft_val = body.pop("whole_graft_stressed_tested", None)
sim_metrics: Dict[str, Any] = {}
for mk in (
"estimand1_u_esc",
"estimand2_u_cond",
"caspase_fold_activity",
"five_virus_max_fold_vs_wt",
"nhs20_cdc_c5b9_u95_pct",
):
if mk in body:
sim_metrics[mk] = body.pop(mk)
existing_sim = body.get("synthetic_plumbing_simulation")
sim_block: Dict[str, Any] = copy.deepcopy(existing_sim) if isinstance(existing_sim, dict) else {}
sim_block["simulated_outcome"] = sim_outcome
sim_block["all_criteria_satisfied"] = (sim_outcome == "PASS")
if qual_tier_val is not None or "simulated_qualification_tier" not in sim_block:
sim_block["simulated_qualification_tier"] = qual_tier_val
if eval_verdict_val is not None or "simulated_evaluator_verdict" not in sim_block:
sim_block["simulated_evaluator_verdict"] = eval_verdict_val
if lineages_val is not None or "simulated_five_lineage_verified" not in sim_block:
sim_block["simulated_five_lineage_verified"] = list(lineages_val) if isinstance(lineages_val, list) else []
if whole_graft_val is not None or "simulated_whole_graft_passed" not in sim_block:
sim_block["simulated_whole_graft_passed"] = bool(whole_graft_val) if whole_graft_val is not None else False
if "simulated_decision" not in sim_block:
sim_block["simulated_decision"] = orig_decision
if "simulated_authority_scope" not in sim_block:
sim_block["simulated_authority_scope"] = orig_scope
if "simulated_bank_qualified" not in sim_block:
sim_block["simulated_bank_qualified"] = orig_bank_qual
if "simulated_physical_pass" not in sim_block:
sim_block["simulated_physical_pass"] = (sim_outcome == "PASS")
if "simulated_promotion_pass" not in sim_block:
sim_block["simulated_promotion_pass"] = orig_promo
if "simulated_release_authorizing" not in sim_block:
sim_block["simulated_release_authorizing"] = orig_rel_auth
if sim_metrics:
sim_block["simulated_metrics"] = sim_metrics
body["record_id"] = _make_canonical_gate0v_record_id_v2_round9(
synth_prefix,
str(body.get("product_id") or ""),
str(body.get("lot_id") or ""),
)
body["evidence_tier"] = "SYNTHETIC_FIXTURE"
body["evidence_class"] = "SYNTHETIC_PLUMBING_CONTROL_FIXTURE"
body["plumbing_control_only"] = True
body["operational_wetlab_authority"] = False
body["bank_qualified"] = False
body["promotion_authorized"] = False
body["physical_qualification_complete"] = False
body["release_authorizing"] = False
body["decision"] = safe_decision
body["authority_scope"] = "SYNTHETIC_SELF_TEST"
body["custody_verification_mode"] = "SYNTHETIC_PLUMBING_CONTROL_FIXTURE_ONLY"
body["synthetic_plumbing_simulation"] = sim_block
for _fk in _SYNTHETIC_FORBIDDEN_TOP_LEVEL_SIMULATION_KEYS_V2_ROUND13:
body.pop(_fk, None)
return _seal_gate0v_receipt_v2_round5(body)
def issue_gate0v_physical_receipt_from_governed_root(
lookup_request: _AbcMapping[str, Any] | None = None,
*,
arm_id: Any = None,
lot_id: Any = None,
bank_id: Any = None,
product_id: Any = None,
construct_manifest_sha256: Any = None,
dossier_id: Any = None,
qa_signoff_id: Any = None,
expected_dossier_sha256: Any = None,
expected_qa_signoff_sha256: Any = None,
expected_canonical_manifest_sha256: Any = None,
release_scope: Any = None,
governed_store_root: Path | str | None = None,
canonical_manifest_path: Path | str | None = None,
openislet_root: Path | None = None,
) -> Tuple[Dict[str, Any], int]:
"""Audit R7-06 + R7-07 + R7-12 + R12-06 governed Gate 0V physical receipt producer entry point.
Reads ONLY separately governed on-disk stores (`physical_dossiers/`, `qa_signoffs/`,
`qa_issuer_registry.json`, `trusted_qualification_store.json`), derives
`canonical_arm_manifest_sha256` strictly from governed manifest bytes, validates independent
QA issuer authority, enforces owner product-family qualification tier rules via
`v38.is_qualification_tier_allowed_for_product`, validates all 5 quantitative Gate 0V domains
with typed safe finite conversions, executes the real owner `v38.evaluate_wholegraft_gate0v_and_stress_ap1903`
before issuing or registering authority, routes EVERY return path (PASS, FAIL/adverse, AND HOLD)
through `_finalize_gate0v_cli_receipt`, and registers passing receipts strictly ONCE under
`<governed_store_root>/issued_receipts/<archive_sha256>.json`.
Returns `(receipt_dict, exit_code)` where exit_code is 0 (PASS), 2 (HOLD), or 3 (FAIL)."""
# Audit R8-03, R9-03 & R12-06: Load & verify owner-configured trust anchor + installation identity
# upfront so EVERY return path knows whether this installation is synthetic plumbing vs production!
resolved_root_pre = _resolve_openislet_root(openislet_root)
trust_ctx, trust_err = _load_and_verify_owner_trust_config_v2_round9(resolved_root_pre)
is_synthetic_installation = _is_synthetic_plumbing_installation_v2_round13(trust_ctx)
def _return_finalized(raw_rec: Dict[str, Any], rc: int, *, force_synthetic: bool = False) -> Tuple[Dict[str, Any], int]:
return (
_finalize_gate0v_cli_receipt(
raw_rec,
is_synthetic_plumbing=(is_synthetic_installation or force_synthetic),
),
rc,
)
req_map: Dict[str, Any] = {}
if lookup_request is not None:
if not isinstance(lookup_request, _AbcMapping):
return _return_finalized(
_make_fail_closed_gate0v_custody_receipt_v2_round7(
decision="FAIL_MALFORMED_ARCHIVED_MEASUREMENT",
authority_scope="REJECTED_MALFORMED_ARCHIVED_MEASUREMENT",
evidence_tier="UNAUTHENTICATED_FAIL",
record_prefix="G0V-MALFORMED-FAIL",
product_id="",
lot_id="",
bank_id="",
bank_origin="",
manifest_sha="",
construct_manifest_sha256="",
dossier_id=None,
qa_signoff_id=None,
custody_failure_reasons=[f"LOOKUP_REQUEST_NOT_MAPPING:{type(lookup_request).__name__}"],
),
3,
)
req_map = dict(lookup_request)