|
|
@@ -21,6 +21,17 @@ ROOT = Path(__file__).resolve().parents[1]
|
|
|
OLD_BASELINE_DEFAULT = "0de9f93988344e3c11931482902196b02ac7a713"
|
|
|
ARTIFACT_DEFAULT = "f23d6d4c57959071cd3886ea5503fbe903aee547"
|
|
|
|
|
|
+# Standalone raw reference package identity (receipt-backed; ZIP not in default compact).
|
|
|
+REFERENCE_PACKAGE_FILENAME = "prod2u-reference-v1_2-3axis-20260805T171312Z.zip"
|
|
|
+REFERENCE_PACKAGE_SHA256 = (
|
|
|
+ "b8a1dd092f453b1b25a4faf4734e8c5a404bfa38752f19fb67cf842d228ec2b1"
|
|
|
+)
|
|
|
+REFERENCE_PACKAGE_SIZE = 4446292
|
|
|
+REFERENCE_CANONICAL_BRD_SHA256 = (
|
|
|
+ "269b5bab2f2e1215b9392079a003badc34238ae3cbe8e5094c9a11bba8b84ebb"
|
|
|
+)
|
|
|
+REFERENCE_RECEIPT_REL = "receipts/reference-package-20260805T171312Z.json"
|
|
|
+
|
|
|
|
|
|
def _sha256_bytes(data: bytes) -> str:
|
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
@@ -47,8 +58,8 @@ def build_source_closure_relations(
|
|
|
old_baseline_commit: str,
|
|
|
artifact_commit: str,
|
|
|
delivery_commit: str,
|
|
|
- old_to_artifact_unchanged: bool,
|
|
|
- artifact_to_delivery_unchanged: bool,
|
|
|
+ old_to_artifact_unchanged: bool | None,
|
|
|
+ artifact_to_delivery_unchanged: bool | None,
|
|
|
old_to_artifact_changed_paths: list[str] | None = None,
|
|
|
artifact_to_delivery_changed_paths: list[str] | None = None,
|
|
|
comparison_mode: str = "commit_compare",
|
|
|
@@ -65,7 +76,7 @@ def build_source_closure_relations(
|
|
|
"to_commit": artifact_commit,
|
|
|
"from_short": _short(old_baseline_commit),
|
|
|
"to_short": _short(artifact_commit),
|
|
|
- "unchanged": bool(old_to_artifact_unchanged),
|
|
|
+ "unchanged": old_to_artifact_unchanged,
|
|
|
"production_changed_paths": old_paths,
|
|
|
},
|
|
|
"artifact_to_delivery": {
|
|
|
@@ -73,38 +84,152 @@ def build_source_closure_relations(
|
|
|
"to_commit": delivery_commit,
|
|
|
"from_short": _short(artifact_commit),
|
|
|
"to_short": _short(delivery_commit),
|
|
|
- "unchanged": bool(artifact_to_delivery_unchanged),
|
|
|
+ "unchanged": artifact_to_delivery_unchanged,
|
|
|
"production_changed_paths": del_paths,
|
|
|
},
|
|
|
# Human-readable aliases (same authority, not a second truth).
|
|
|
- "OLD_TO_NEW_SOURCE_CLOSURE_UNCHANGED": bool(old_to_artifact_unchanged),
|
|
|
- "NEW_ARTIFACT_TO_DELIVERY_SOURCE_CLOSURE_UNCHANGED": bool(artifact_to_delivery_unchanged),
|
|
|
+ "OLD_TO_NEW_SOURCE_CLOSURE_UNCHANGED": old_to_artifact_unchanged,
|
|
|
+ "NEW_ARTIFACT_TO_DELIVERY_SOURCE_CLOSURE_UNCHANGED": artifact_to_delivery_unchanged,
|
|
|
}
|
|
|
return payload
|
|
|
|
|
|
|
|
|
-def reference_set_fingerprint(repo_root: Path | None = None) -> dict[str, Any]:
|
|
|
- """Stable reference-set fingerprint from inventory + board-viewer build-info."""
|
|
|
+def validate_source_closure_relations(relations: dict[str, Any] | Any) -> dict[str, Any]:
|
|
|
+ """Fail-closed consistency checks for dual-edge relations.
|
|
|
+
|
|
|
+ Negatives required by contract:
|
|
|
+ - unchanged=false + empty changed_paths + mode=commit_compare → FAIL
|
|
|
+ - unchanged=true + non-empty changed_paths → FAIL
|
|
|
+ - comparison_mode=commit_compare with unknown booleans → FAIL
|
|
|
+ """
|
|
|
+ failures: list[str] = []
|
|
|
+ if not isinstance(relations, dict):
|
|
|
+ return {"status": "fail", "failures": ["relations_not_object"]}
|
|
|
+
|
|
|
+ mode = relations.get("comparison_mode")
|
|
|
+ if mode not in {"commit_compare", "snapshot_hash", "comparison_unavailable"}:
|
|
|
+ failures.append(f"bad_comparison_mode:{mode}")
|
|
|
+
|
|
|
+ for edge_key in ("old_baseline_to_artifact", "artifact_to_delivery"):
|
|
|
+ edge = relations.get(edge_key) or {}
|
|
|
+ if not isinstance(edge, dict):
|
|
|
+ failures.append(f"{edge_key}_not_object")
|
|
|
+ continue
|
|
|
+ unchanged = edge.get("unchanged")
|
|
|
+ paths = edge.get("production_changed_paths") or []
|
|
|
+ if not isinstance(paths, list):
|
|
|
+ failures.append(f"{edge_key}_paths_not_list")
|
|
|
+ paths = []
|
|
|
+ if mode == "commit_compare":
|
|
|
+ if unchanged is True and paths:
|
|
|
+ failures.append(f"{edge_key}_unchanged_true_with_changed_paths")
|
|
|
+ if unchanged is False and not paths:
|
|
|
+ failures.append(f"{edge_key}_unchanged_false_empty_paths")
|
|
|
+ if unchanged not in (True, False):
|
|
|
+ failures.append(f"{edge_key}_unchanged_not_bool_under_commit_compare")
|
|
|
+ if mode == "comparison_unavailable":
|
|
|
+ if unchanged is not None:
|
|
|
+ failures.append(f"{edge_key}_guessed_bool_when_unavailable")
|
|
|
+
|
|
|
+ # Identity presence
|
|
|
+ old_edge = relations.get("old_baseline_to_artifact") or {}
|
|
|
+ art_edge = relations.get("artifact_to_delivery") or {}
|
|
|
+ if not old_edge.get("from_commit") or not old_edge.get("to_commit"):
|
|
|
+ failures.append("old_edge_missing_commits")
|
|
|
+ if not art_edge.get("from_commit") or not art_edge.get("to_commit"):
|
|
|
+ failures.append("art_edge_missing_commits")
|
|
|
+ if (
|
|
|
+ old_edge.get("to_commit")
|
|
|
+ and art_edge.get("from_commit")
|
|
|
+ and old_edge.get("to_commit") != art_edge.get("from_commit")
|
|
|
+ ):
|
|
|
+ failures.append("artifact_identity_mismatch_across_edges")
|
|
|
+
|
|
|
+ return {"status": "fail" if failures else "pass", "failures": failures}
|
|
|
+
|
|
|
+
|
|
|
+def load_reference_package_identity(repo_root: Path | None = None) -> dict[str, Any]:
|
|
|
+ """Raw reference package identity from canonical receipt (stable; no timestamps)."""
|
|
|
root = repo_root or ROOT
|
|
|
- inventory_path = root / "docs" / "REFERENCE-INVENTORY.json"
|
|
|
- build_info_path = root / "tools" / "board-viewer" / "generated" / "build-info.json"
|
|
|
- brd_rel = (
|
|
|
- "reference/original/board/FCB_FPGA_MCU_V1.2_20260717/"
|
|
|
- "FCB_FPGA+MCU_V1.2_20260717.brd"
|
|
|
+ receipt_path = root / REFERENCE_RECEIPT_REL
|
|
|
+ identity = {
|
|
|
+ "filename": REFERENCE_PACKAGE_FILENAME,
|
|
|
+ "size": REFERENCE_PACKAGE_SIZE,
|
|
|
+ "sha256": REFERENCE_PACKAGE_SHA256,
|
|
|
+ "canonical_brd_sha256": REFERENCE_CANONICAL_BRD_SHA256,
|
|
|
+ "receipt_path": REFERENCE_RECEIPT_REL,
|
|
|
+ }
|
|
|
+ if receipt_path.is_file():
|
|
|
+ receipt = _read_json(receipt_path)
|
|
|
+ zip_sha = receipt.get("zip_sha256")
|
|
|
+ zip_bytes = receipt.get("zip_bytes")
|
|
|
+ brd_sha = receipt.get("brd_sha256")
|
|
|
+ pkg_name = receipt.get("package_name")
|
|
|
+ if zip_sha and zip_sha != REFERENCE_PACKAGE_SHA256:
|
|
|
+ raise ValueError(
|
|
|
+ f"reference receipt zip sha mismatch: {zip_sha} != {REFERENCE_PACKAGE_SHA256}"
|
|
|
+ )
|
|
|
+ if brd_sha and brd_sha != REFERENCE_CANONICAL_BRD_SHA256:
|
|
|
+ raise ValueError(
|
|
|
+ f"reference receipt BRD sha mismatch: {brd_sha} != {REFERENCE_CANONICAL_BRD_SHA256}"
|
|
|
+ )
|
|
|
+ if zip_bytes and int(zip_bytes) != REFERENCE_PACKAGE_SIZE:
|
|
|
+ raise ValueError(
|
|
|
+ f"reference receipt size mismatch: {zip_bytes} != {REFERENCE_PACKAGE_SIZE}"
|
|
|
+ )
|
|
|
+ if pkg_name and f"{pkg_name}.zip" != REFERENCE_PACKAGE_FILENAME:
|
|
|
+ # Allow receipt package_name without .zip suffix.
|
|
|
+ if pkg_name != REFERENCE_PACKAGE_FILENAME.removesuffix(".zip"):
|
|
|
+ raise ValueError(f"reference receipt name mismatch: {pkg_name}")
|
|
|
+ identity["receipt_status"] = receipt.get("status")
|
|
|
+ return identity
|
|
|
+
|
|
|
+
|
|
|
+def derived_board_viewer_identity(repo_root: Path | None = None) -> dict[str, Any]:
|
|
|
+ """Derived board-viewer evidence identity (separate from raw reference package)."""
|
|
|
+ root = repo_root or ROOT
|
|
|
+ generated = root / "tools" / "board-viewer" / "generated"
|
|
|
+ names = (
|
|
|
+ "board-data.json",
|
|
|
+ "build-info.json",
|
|
|
+ "import-report.json",
|
|
|
+ "board-copper.svg",
|
|
|
+ "board-silkscreen.svg",
|
|
|
+ "board-mask-edge.svg",
|
|
|
)
|
|
|
- brd_path = root / brd_rel
|
|
|
+ assets: dict[str, str | None] = {}
|
|
|
+ for name in names:
|
|
|
+ path = generated / name
|
|
|
+ assets[name] = _sha256_file(path) if path.is_file() else None
|
|
|
+ build_info = _read_json(generated / "build-info.json") if (generated / "build-info.json").is_file() else {}
|
|
|
+ input_brd = None
|
|
|
+ sources = build_info.get("sources") if isinstance(build_info, dict) else None
|
|
|
+ if isinstance(sources, dict):
|
|
|
+ brd = sources.get("brd")
|
|
|
+ if isinstance(brd, dict):
|
|
|
+ input_brd = brd.get("sha256")
|
|
|
+ # Stable fingerprint MUST NOT include generated_at or absolute paths.
|
|
|
+ parts = {
|
|
|
+ "assets": assets,
|
|
|
+ "board_viewer_generator_version": build_info.get("generator_version"),
|
|
|
+ "input_brd_sha256": input_brd,
|
|
|
+ }
|
|
|
+ fp = _sha256_bytes(json.dumps(parts, sort_keys=True, separators=(",", ":")).encode("utf-8"))
|
|
|
+ return {
|
|
|
+ "schema": "prod2u-derived-board-viewer-identity-v1",
|
|
|
+ "sha256": fp,
|
|
|
+ "parts": parts,
|
|
|
+ # Informational only — not mixed into raw reference fingerprint.
|
|
|
+ "generated_at_informational": build_info.get("generated_at"),
|
|
|
+ }
|
|
|
|
|
|
- inventory = _read_json(inventory_path) if inventory_path.is_file() else {}
|
|
|
- build_info = _read_json(build_info_path) if build_info_path.is_file() else {}
|
|
|
- expected_brd = (
|
|
|
- inventory.get("evidence_boundaries", {}).get("expected_brd_sha256")
|
|
|
- if isinstance(inventory, dict)
|
|
|
- else None
|
|
|
- )
|
|
|
- brd_sha = _sha256_file(brd_path) if brd_path.is_file() else None
|
|
|
- if expected_brd and brd_sha and expected_brd != brd_sha:
|
|
|
- raise ValueError(f"BRD sha mismatch: expected {expected_brd} got {brd_sha}")
|
|
|
|
|
|
+def reference_set_fingerprint(repo_root: Path | None = None) -> dict[str, Any]:
|
|
|
+ """Stable raw reference-set fingerprint (no generated_at / temp paths)."""
|
|
|
+ root = repo_root or ROOT
|
|
|
+ inventory_path = root / "docs" / "REFERENCE-INVENTORY.json"
|
|
|
+ raw = load_reference_package_identity(root)
|
|
|
+ inventory = _read_json(inventory_path) if inventory_path.is_file() else {}
|
|
|
schematic_identity = {
|
|
|
"v1_2_schematic_pdf_status": inventory.get("evidence_boundaries", {})
|
|
|
.get("v1_2_schematic_pdf", {})
|
|
|
@@ -114,12 +239,12 @@ def reference_set_fingerprint(repo_root: Path | None = None) -> dict[str, Any]:
|
|
|
),
|
|
|
}
|
|
|
content_parts = {
|
|
|
- "inventory_sha256": _sha256_file(inventory_path) if inventory_path.is_file() else None,
|
|
|
- "build_info_sha256": _sha256_file(build_info_path) if build_info_path.is_file() else None,
|
|
|
- "brd_sha256": brd_sha or expected_brd,
|
|
|
+ "reference_package_filename": raw["filename"],
|
|
|
+ "reference_package_sha256": raw["sha256"],
|
|
|
+ "reference_package_size": raw["size"],
|
|
|
+ "canonical_brd_sha256": raw["canonical_brd_sha256"],
|
|
|
"schematic_identity": schematic_identity,
|
|
|
- "board_viewer_generator_version": build_info.get("generator_version"),
|
|
|
- "board_viewer_generated_at": build_info.get("generated_at"),
|
|
|
+ "inventory_sha256": _sha256_file(inventory_path) if inventory_path.is_file() else None,
|
|
|
}
|
|
|
fp = _sha256_bytes(
|
|
|
json.dumps(content_parts, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
|
|
@@ -128,7 +253,10 @@ def reference_set_fingerprint(repo_root: Path | None = None) -> dict[str, Any]:
|
|
|
"schema": "prod2u-reference-set-fingerprint-v1",
|
|
|
"sha256": fp,
|
|
|
"parts": content_parts,
|
|
|
- "canonical_brd_path": brd_rel,
|
|
|
+ "canonical_brd_path": (
|
|
|
+ "reference/original/board/FCB_FPGA_MCU_V1.2_20260717/"
|
|
|
+ "FCB_FPGA+MCU_V1.2_20260717.brd"
|
|
|
+ ),
|
|
|
"raw_brd_pdf_dsn_in_default_compact": False,
|
|
|
}
|
|
|
|
|
|
@@ -144,27 +272,33 @@ def build_reference_pointer(
|
|
|
) -> dict[str, Any]:
|
|
|
"""Build REFERENCE-POINTER.json (full for review-compact; summary for firmware)."""
|
|
|
root = repo_root or ROOT
|
|
|
+ raw = load_reference_package_identity(root)
|
|
|
ref_fp = reference_set_fingerprint(root)
|
|
|
- inventory_path = root / "docs" / "REFERENCE-INVENTORY.json"
|
|
|
- inventory_name = inventory_path.name if inventory_path.is_file() else "REFERENCE-INVENTORY.json"
|
|
|
- inventory_sha = (
|
|
|
- _sha256_file(inventory_path) if inventory_path.is_file() else ref_fp["parts"].get("inventory_sha256")
|
|
|
- )
|
|
|
+ derived = derived_board_viewer_identity(root)
|
|
|
pointer = {
|
|
|
"schema": "prod2u-reference-pointer-v1",
|
|
|
"generated_at_utc": datetime.now(timezone.utc).isoformat(),
|
|
|
"reference_changed": bool(reference_changed),
|
|
|
"reference_reused": bool(reference_reused),
|
|
|
- "filename": inventory_name,
|
|
|
- "sha256": inventory_sha,
|
|
|
+ # Raw reference package identity (NOT inventory filename/hash).
|
|
|
+ "filename": raw["filename"],
|
|
|
+ "size": raw["size"],
|
|
|
+ "sha256": raw["sha256"],
|
|
|
"content_fingerprint": ref_fp["sha256"],
|
|
|
- "brd_sha256": ref_fp["parts"].get("brd_sha256"),
|
|
|
+ "brd_sha256": raw["canonical_brd_sha256"],
|
|
|
"schematic_identity": ref_fp["parts"].get("schematic_identity"),
|
|
|
- "board_viewer_build_info": {
|
|
|
- "path": "tools/board-viewer/generated/build-info.json",
|
|
|
- "sha256": ref_fp["parts"].get("build_info_sha256"),
|
|
|
- "generator_version": ref_fp["parts"].get("board_viewer_generator_version"),
|
|
|
- "generated_at": ref_fp["parts"].get("board_viewer_generated_at"),
|
|
|
+ "raw_reference_package": {
|
|
|
+ "filename": raw["filename"],
|
|
|
+ "size": raw["size"],
|
|
|
+ "sha256": raw["sha256"],
|
|
|
+ "canonical_brd_sha256": raw["canonical_brd_sha256"],
|
|
|
+ "receipt_path": raw.get("receipt_path"),
|
|
|
+ },
|
|
|
+ "derived_board_viewer": {
|
|
|
+ "fingerprint_sha256": derived["sha256"],
|
|
|
+ "assets": derived["parts"]["assets"],
|
|
|
+ "board_viewer_generator_version": derived["parts"]["board_viewer_generator_version"],
|
|
|
+ "input_brd_sha256": derived["parts"]["input_brd_sha256"],
|
|
|
},
|
|
|
"artifact_source_commit": artifact_commit,
|
|
|
"delivery_commit": delivery_commit,
|
|
|
@@ -175,6 +309,8 @@ def build_reference_pointer(
|
|
|
"schema": pointer["schema"],
|
|
|
"reference_changed": pointer["reference_changed"],
|
|
|
"reference_reused": pointer["reference_reused"],
|
|
|
+ "filename": pointer["filename"],
|
|
|
+ "sha256": pointer["sha256"],
|
|
|
"content_fingerprint": pointer["content_fingerprint"],
|
|
|
"brd_sha256": pointer["brd_sha256"],
|
|
|
"artifact_source_commit": artifact_commit,
|
|
|
@@ -244,7 +380,6 @@ def build_release_set_document(
|
|
|
hardware_validation: str = "not_run",
|
|
|
) -> dict[str, Any]:
|
|
|
"""External release-set JSON written AFTER both packages exist."""
|
|
|
- # Fail closed on accidental cyclic fields.
|
|
|
for name, sha in (
|
|
|
(firmware_filename, firmware_sha256),
|
|
|
(review_compact_filename, review_compact_sha256),
|
|
|
@@ -352,7 +487,6 @@ def validate_release_set_selftest(
|
|
|
if sib_name != expected_sibling_filename_in_compact:
|
|
|
failures.append("compact_sibling_filename_mismatch")
|
|
|
|
|
|
- # Tamper vectors the caller can inject by mutating document before call.
|
|
|
identity = document.get("identity") or {}
|
|
|
if not identity.get("bitstream_sha256"):
|
|
|
failures.append("missing_bitstream_identity")
|