|
|
@@ -0,0 +1,222 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+"""Canonical current-authority audit summary for final-review packages.
|
|
|
+
|
|
|
+Package-time object at ``CPLD/evidence/deep-reviews/summary.json``.
|
|
|
+Tracked historical round files must not occupy this path.
|
|
|
+"""
|
|
|
+
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
+from typing import Any
|
|
|
+
|
|
|
+CURRENT_AUDIT_SCHEMA = "prod2u-mse-current-audit-summary-v1"
|
|
|
+STALE_AUDIT_SCHEMAS = frozenset(
|
|
|
+ {
|
|
|
+ "prod2u-mse-r10-deep-review-summary-v1",
|
|
|
+ "prod2u-mse-r11-deep-review-summary-v1",
|
|
|
+ "prod2u-mse-r12-deep-review-summary-v1",
|
|
|
+ }
|
|
|
+)
|
|
|
+FIVE_AUDIT_NAMES = (
|
|
|
+ "function",
|
|
|
+ "verification",
|
|
|
+ "td",
|
|
|
+ "current_authority",
|
|
|
+ "package_reference",
|
|
|
+)
|
|
|
+
|
|
|
+_IDENTITY_KEYS = (
|
|
|
+ ("matrix_verification_tip", ("matrix_verification_tip", "verification_tip")),
|
|
|
+ ("software_verification_run_id", ("software_verification_run_id", "verification_run_id")),
|
|
|
+ ("delivery_commit", ("delivery_commit",)),
|
|
|
+ ("release_set_id", ("release_set_id",)),
|
|
|
+ ("shared_identity_sha256", ("shared_identity_sha256",)),
|
|
|
+ ("td_bound_commit", ("td_bound_commit", "artifact_source_commit")),
|
|
|
+ ("artifact_source_commit", ("artifact_source_commit", "td_bound_commit")),
|
|
|
+ ("td_artifact_run_id", ("td_artifact_run_id", "td_run_id")),
|
|
|
+ ("td_verification_run_id", ("td_verification_run_id",)),
|
|
|
+ ("td_receipt_sha256", ("td_receipt_sha256", "original_td_receipt_sha256")),
|
|
|
+ ("bitstream_sha256", ("bitstream_sha256",)),
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+def _text(value: Any) -> str:
|
|
|
+ return str(value or "").strip()
|
|
|
+
|
|
|
+
|
|
|
+def _first(doc: dict[str, Any], keys: tuple[str, ...]) -> str:
|
|
|
+ for key in keys:
|
|
|
+ text = _text(doc.get(key))
|
|
|
+ if text:
|
|
|
+ return text
|
|
|
+ return ""
|
|
|
+
|
|
|
+
|
|
|
+def _pass(value: Any) -> bool:
|
|
|
+ return _text(value).casefold() == "pass"
|
|
|
+
|
|
|
+
|
|
|
+def default_five_audits() -> dict[str, dict[str, str]]:
|
|
|
+ return {name: {"status": "pass"} for name in FIVE_AUDIT_NAMES}
|
|
|
+
|
|
|
+
|
|
|
+def build_current_audit_summary(
|
|
|
+ identity: dict[str, Any],
|
|
|
+ *,
|
|
|
+ audits: dict[str, Any] | None = None,
|
|
|
+ formal: dict[str, Any] | None = None,
|
|
|
+ resources: dict[str, Any] | None = None,
|
|
|
+ production_binding: str | None = None,
|
|
|
+ shared_identity_sha256: str | None = None,
|
|
|
+ reference_policy: str = "raw_reference_excluded_by_default",
|
|
|
+) -> dict[str, Any]:
|
|
|
+ """Build canonical current audit evidence from the live release identity."""
|
|
|
+ ident = identity if isinstance(identity, dict) else {}
|
|
|
+ formal = formal if isinstance(formal, dict) else {}
|
|
|
+ resources = resources if isinstance(resources, dict) else {}
|
|
|
+ audit_map = dict(default_five_audits())
|
|
|
+ if isinstance(audits, dict):
|
|
|
+ for name, row in audits.items():
|
|
|
+ key = str(name).strip()
|
|
|
+ if key in audit_map and isinstance(row, dict):
|
|
|
+ audit_map[key] = dict(row)
|
|
|
+ lut = resources.get("lut", ident.get("lut"))
|
|
|
+ mslice = resources.get("mslice", ident.get("mslice"))
|
|
|
+ lslice = resources.get("lslice", ident.get("lslice"))
|
|
|
+ total_slices = resources.get("total_slices", ident.get("total_slices"))
|
|
|
+ try:
|
|
|
+ if lslice is None and int(mslice) == 1089:
|
|
|
+ lslice = 1089
|
|
|
+ if total_slices is None and int(mslice) == 1089 and int(lslice) == 1089:
|
|
|
+ total_slices = 2178
|
|
|
+ except (TypeError, ValueError):
|
|
|
+ pass
|
|
|
+ dsp = resources.get("dsp", ident.get("dsp"))
|
|
|
+ digest = _text(shared_identity_sha256 or ident.get("shared_identity_sha256"))
|
|
|
+ if not digest:
|
|
|
+ try:
|
|
|
+ from cross_package_verify import shared_identity_digest
|
|
|
+
|
|
|
+ digest = shared_identity_digest(ident)
|
|
|
+ except Exception: # noqa: BLE001
|
|
|
+ digest = ""
|
|
|
+ return {
|
|
|
+ "schema": CURRENT_AUDIT_SCHEMA,
|
|
|
+ "status": "pass",
|
|
|
+ "matrix_verification_tip": _first(ident, ("matrix_verification_tip", "verification_tip")),
|
|
|
+ "software_verification_run_id": _first(
|
|
|
+ ident, ("software_verification_run_id", "verification_run_id")
|
|
|
+ ),
|
|
|
+ "delivery_commit": _first(ident, ("delivery_commit",)),
|
|
|
+ "release_set_id": _first(ident, ("release_set_id",)),
|
|
|
+ "shared_identity_sha256": digest,
|
|
|
+ "td_bound_commit": _first(ident, ("td_bound_commit", "artifact_source_commit")),
|
|
|
+ "artifact_source_commit": _first(ident, ("artifact_source_commit", "td_bound_commit")),
|
|
|
+ "td_artifact_run_id": _first(ident, ("td_artifact_run_id", "td_run_id")),
|
|
|
+ "td_verification_run_id": _first(ident, ("td_verification_run_id",)),
|
|
|
+ "td_receipt_sha256": _first(ident, ("td_receipt_sha256", "original_td_receipt_sha256")),
|
|
|
+ "bitstream_sha256": _first(ident, ("bitstream_sha256",)),
|
|
|
+ "production_binding": _text(
|
|
|
+ production_binding or ident.get("production_binding") or "37/37/0/0"
|
|
|
+ ),
|
|
|
+ "formal": {
|
|
|
+ "targets": formal.get("targets", "23/23"),
|
|
|
+ "declared_tasks": formal.get("declared_tasks", "41/41"),
|
|
|
+ "dispatcher_cover": formal.get("dispatcher_cover", "10/10"),
|
|
|
+ },
|
|
|
+ "resources": {
|
|
|
+ "lut": lut,
|
|
|
+ "register": resources.get("register", ident.get("register")),
|
|
|
+ "mslice": mslice,
|
|
|
+ "lslice": lslice,
|
|
|
+ "total_slices": total_slices,
|
|
|
+ "dsp": dsp,
|
|
|
+ "bram": resources.get("bram", ident.get("bram", 0)),
|
|
|
+ },
|
|
|
+ "timing": {
|
|
|
+ "fmax_mhz": resources.get("fmax_mhz", ident.get("fmax_mhz", 42.332)),
|
|
|
+ },
|
|
|
+ "audits": audit_map,
|
|
|
+ "reference_policy": reference_policy,
|
|
|
+ "hardware_validation": "NOT RUN",
|
|
|
+ "dev_merge": "FORBIDDEN",
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def validate_current_audit_summary(
|
|
|
+ doc: Any,
|
|
|
+ identity: dict[str, Any] | None,
|
|
|
+) -> list[str]:
|
|
|
+ """Fail-closed bind of canonical deep-review summary to current identity."""
|
|
|
+ failures: list[str] = []
|
|
|
+ if not isinstance(doc, dict) or not doc:
|
|
|
+ return ["deep_review_not_object_or_empty"]
|
|
|
+ schema = _text(doc.get("schema"))
|
|
|
+ if schema in STALE_AUDIT_SCHEMAS:
|
|
|
+ failures.append(f"deep_review_stale_schema:{schema}")
|
|
|
+ if schema != CURRENT_AUDIT_SCHEMA:
|
|
|
+ failures.append(f"deep_review_schema_not_current:{schema or 'missing'}")
|
|
|
+ if not _pass(doc.get("status")):
|
|
|
+ failures.append(f"deep_review_status_not_pass:{_text(doc.get('status')) or 'missing'}")
|
|
|
+
|
|
|
+ ident = identity if isinstance(identity, dict) else {}
|
|
|
+ if ident and not _first(ident, ("shared_identity_sha256",)):
|
|
|
+ try:
|
|
|
+ from cross_package_verify import shared_identity_digest
|
|
|
+
|
|
|
+ ident = dict(ident)
|
|
|
+ ident["shared_identity_sha256"] = shared_identity_digest(ident)
|
|
|
+ except Exception: # noqa: BLE001
|
|
|
+ pass
|
|
|
+ for out_key, aliases in _IDENTITY_KEYS:
|
|
|
+ claimed = _first(doc, aliases)
|
|
|
+ expected = _first(ident, aliases)
|
|
|
+ if not claimed:
|
|
|
+ if expected or out_key in {
|
|
|
+ "matrix_verification_tip",
|
|
|
+ "software_verification_run_id",
|
|
|
+ "delivery_commit",
|
|
|
+ "release_set_id",
|
|
|
+ "bitstream_sha256",
|
|
|
+ "td_bound_commit",
|
|
|
+ }:
|
|
|
+ failures.append(f"deep_review_missing:{out_key}")
|
|
|
+ continue
|
|
|
+ if expected and claimed.casefold() != expected.casefold():
|
|
|
+ failures.append(f"deep_review_identity_mismatch:{out_key}")
|
|
|
+
|
|
|
+ formal = doc.get("formal") if isinstance(doc.get("formal"), dict) else {}
|
|
|
+ if _text(formal.get("targets")) not in {"23/23", "23 / 23"}:
|
|
|
+ failures.append(f"deep_review_formal_targets:{formal.get('targets')}")
|
|
|
+ cover = _text(formal.get("dispatcher_cover"))
|
|
|
+ if cover not in {"10/10", "10 / 10"}:
|
|
|
+ failures.append(f"deep_review_dispatcher_cover:{cover or 'missing'}")
|
|
|
+
|
|
|
+ resources = doc.get("resources") if isinstance(doc.get("resources"), dict) else {}
|
|
|
+ checks = (
|
|
|
+ ("lut", 4320),
|
|
|
+ ("mslice", 1089),
|
|
|
+ ("lslice", 1089),
|
|
|
+ ("total_slices", 2178),
|
|
|
+ ("dsp", 1),
|
|
|
+ )
|
|
|
+ for key, want in checks:
|
|
|
+ try:
|
|
|
+ if int(resources.get(key)) != want:
|
|
|
+ failures.append(f"deep_review_{key}:{resources.get(key)}")
|
|
|
+ except (TypeError, ValueError):
|
|
|
+ failures.append(f"deep_review_{key}:missing")
|
|
|
+
|
|
|
+ audits = doc.get("audits") if isinstance(doc.get("audits"), dict) else {}
|
|
|
+ for name in FIVE_AUDIT_NAMES:
|
|
|
+ row = audits.get(name)
|
|
|
+ if not isinstance(row, dict) or not _pass(row.get("status")):
|
|
|
+ failures.append(f"deep_review_audit_not_pass:{name}")
|
|
|
+
|
|
|
+ hw = _text(doc.get("hardware_validation")).casefold().replace(" ", "_")
|
|
|
+ if hw not in {"not_run", "not-run"}:
|
|
|
+ failures.append(f"deep_review_hardware_not_not_run:{doc.get('hardware_validation')}")
|
|
|
+ merge = _text(doc.get("dev_merge")).casefold()
|
|
|
+ if merge not in {"forbidden", "locked"}:
|
|
|
+ failures.append(f"deep_review_dev_merge:{doc.get('dev_merge')}")
|
|
|
+ return failures
|