Browse Source

package: enforce authoritative receipt bindings

Codex Resource Selftest 4 months ago
parent
commit
5bb614904f
2 changed files with 44 additions and 41 deletions
  1. 24 13
      tools/codex_harness.py
  2. 20 28
      tools/prod2u_p4_harness.py

+ 24 - 13
tools/codex_harness.py

@@ -509,8 +509,8 @@ def create_unverified_package(
     td_receipt_sha256: str | None = None,
     td_receipt_sha256: str | None = None,
     package_class: str = "UNVERIFIED",
     package_class: str = "UNVERIFIED",
 ) -> Path:
 ) -> Path:
-    if not COMMIT_RE.fullmatch(commit):
-        raise ValueError("commit must be 8 lowercase hexadecimal characters")
+    if not (COMMIT_RE.fullmatch(commit) or FULL_COMMIT_RE.fullmatch(commit)):
+        raise ValueError("commit must be a full or 8-character lowercase hexadecimal SHA")
     if not RUN_ID_RE.fullmatch(run_id):
     if not RUN_ID_RE.fullmatch(run_id):
         raise ValueError("run_id must be 32 lowercase hexadecimal characters")
         raise ValueError("run_id must be 32 lowercase hexadecimal characters")
     if package_class not in {"UNVERIFIED", "UNVERIFIED_EARLY_BUILD"}:
     if package_class not in {"UNVERIFIED", "UNVERIFIED_EARLY_BUILD"}:
@@ -539,8 +539,17 @@ def create_unverified_package(
     if not project.is_file():
     if not project.is_file():
         raise FileNotFoundError(f"project is missing: {project}")
         raise FileNotFoundError(f"project is missing: {project}")
 
 
+    repo = _repo_root()
+    if FULL_COMMIT_RE.fullmatch(commit):
+        commit_full = commit
+    else:
+        resolved = _run_git(repo, ["rev-parse", "HEAD"]).stdout.decode("ascii", "strict").strip().lower()
+        if not FULL_COMMIT_RE.fullmatch(resolved) or resolved[:8] != commit:
+            raise ValueError("short commit does not match the current full HEAD")
+        commit_full = resolved
+    compatibility_commit = commit
     bitstream_sha256 = sha256_file(bitstream)
     bitstream_sha256 = sha256_file(bitstream)
-    package = Path(output_root) / PROJECT_ID / f"{commit}-{bitstream_sha256[:16]}"
+    package = Path(output_root) / PROJECT_ID / f"{compatibility_commit}-{bitstream_sha256[:16]}"
     package.mkdir(parents=True, exist_ok=True)
     package.mkdir(parents=True, exist_ok=True)
     package_bitstream = package / "fw.bit"
     package_bitstream = package / "fw.bit"
     package_project = package / "project.json"
     package_project = package / "project.json"
@@ -548,7 +557,6 @@ def create_unverified_package(
     evidence.mkdir(parents=True, exist_ok=True)
     evidence.mkdir(parents=True, exist_ok=True)
     shutil.copy2(bitstream, package_bitstream)
     shutil.copy2(bitstream, package_bitstream)
     shutil.copy2(project, package_project)
     shutil.copy2(project, package_project)
-    repo = _repo_root()
     artifacts = {
     artifacts = {
         "protocol.md": repo / PROTOCOL_PATH,
         "protocol.md": repo / PROTOCOL_PATH,
         "requirements.zh-CN.md": repo / REQUIREMENTS_PATH,
         "requirements.zh-CN.md": repo / REQUIREMENTS_PATH,
@@ -575,14 +583,14 @@ def create_unverified_package(
             receipt,
             receipt,
             evidence / "td-primary-receipt.json",
             evidence / "td-primary-receipt.json",
             run_id=run_id,
             run_id=run_id,
-            commit=commit,
+            commit=commit_full,
             bitstream_sha256=bitstream_sha256,
             bitstream_sha256=bitstream_sha256,
             project_config_sha256=project_config_sha256,
             project_config_sha256=project_config_sha256,
         )
         )
     else:
     else:
         _write_json(
         _write_json(
             evidence / "td-primary-receipt.json",
             evidence / "td-primary-receipt.json",
-            _receipt_payload(name="td-primary", run_id=run_id, commit=commit, bitstream_sha256=bitstream_sha256, project_config_sha256=project_config_sha256),
+            _receipt_payload(name="td-primary", run_id=run_id, commit=commit_full, bitstream_sha256=bitstream_sha256, project_config_sha256=project_config_sha256),
         )
         )
     receipt_specs = {
     receipt_specs = {
         "td-confirmation": "td-confirmation-receipt.json",
         "td-confirmation": "td-confirmation-receipt.json",
@@ -602,14 +610,14 @@ def create_unverified_package(
                 source_path,
                 source_path,
                 evidence / filename,
                 evidence / filename,
                 run_id=run_id,
                 run_id=run_id,
-                commit=commit,
+                commit=commit_full,
                 bitstream_sha256=bitstream_sha256,
                 bitstream_sha256=bitstream_sha256,
                 project_config_sha256=project_config_sha256,
                 project_config_sha256=project_config_sha256,
             )
             )
         else:
         else:
             _write_json(
             _write_json(
                 evidence / filename,
                 evidence / filename,
-                _receipt_payload(name=name, run_id=run_id, commit=commit, bitstream_sha256=bitstream_sha256, project_config_sha256=project_config_sha256),
+                _receipt_payload(name=name, run_id=run_id, commit=commit_full, bitstream_sha256=bitstream_sha256, project_config_sha256=project_config_sha256),
             )
             )
     _write_json(
     _write_json(
         evidence / "package-selftest-result.json",
         evidence / "package-selftest-result.json",
@@ -617,7 +625,8 @@ def create_unverified_package(
             "schema": "prod2u-p4-normal-v3-package-selftest-result-v1",
             "schema": "prod2u-p4-normal-v3-package-selftest-result-v1",
             "status": "not_run",
             "status": "not_run",
             "verification_run_id": run_id,
             "verification_run_id": run_id,
-            "commit": commit,
+            "commit": commit_full,
+            "commit_full": commit_full,
             "bitstream_sha256": bitstream_sha256,
             "bitstream_sha256": bitstream_sha256,
         },
         },
     )
     )
@@ -656,9 +665,10 @@ def create_unverified_package(
         "status": "UNVERIFIED",
         "status": "UNVERIFIED",
         "delivery_mode": "urgent_single_td",
         "delivery_mode": "urgent_single_td",
         "verification_complete": False,
         "verification_complete": False,
-        "confirmation_td_present": True,
+        "confirmation_td_present": confirmation_bitstream_sha256 is not None,
         "verification_run_id": run_id,
         "verification_run_id": run_id,
-        "commit": commit,
+        "commit": compatibility_commit,
+        "commit_full": commit_full,
         "bitstream_sha256": bitstream_sha256,
         "bitstream_sha256": bitstream_sha256,
         "project_config_sha256": sha256_file(package_project),
         "project_config_sha256": sha256_file(package_project),
         "layers": {
         "layers": {
@@ -685,9 +695,10 @@ def create_unverified_package(
         "project_id": PROJECT_ID,
         "project_id": PROJECT_ID,
         "delivery_mode": "urgent_single_td",
         "delivery_mode": "urgent_single_td",
         "verification_complete": False,
         "verification_complete": False,
-        "confirmation_td_present": True,
+        "confirmation_td_present": confirmation_bitstream_sha256 is not None,
         "verification_run_id": run_id,
         "verification_run_id": run_id,
-        "commit": commit,
+        "commit": compatibility_commit,
+        "commit_full": commit_full,
         "bitstream": "fw.bit",
         "bitstream": "fw.bit",
         "bitstream_sha256": bitstream_sha256,
         "bitstream_sha256": bitstream_sha256,
         "project": "project.json",
         "project": "project.json",

+ 20 - 28
tools/prod2u_p4_harness.py

@@ -685,7 +685,10 @@ def formal_tool_env(work_root: Path) -> dict[str, str]:
 
 
 
 
 def active_rtl_sources() -> list[str]:
 def active_rtl_sources() -> list[str]:
-    return [relative for relative in PRODUCTION_RTL_SOURCES if (ROOT / relative).is_file()]
+    missing = [relative for relative in PRODUCTION_RTL_SOURCES if not (ROOT / relative).is_file()]
+    if missing:
+        raise FileNotFoundError(f"active source closure is incomplete: {missing}")
+    return list(PRODUCTION_RTL_SOURCES)
 
 
 
 
 def hdl_testbenches() -> list[dict[str, Any]]:
 def hdl_testbenches() -> list[dict[str, Any]]:
@@ -3287,7 +3290,7 @@ def package_selftest_payload(
             if verification_complete is not False:
             if verification_complete is not False:
                 failures.append("urgent_verification_complete_must_be_false")
                 failures.append("urgent_verification_complete_must_be_false")
         commit = str(info.get("commit", ""))
         commit = str(info.get("commit", ""))
-        if not (FULL_COMMIT_RE.fullmatch(commit) or SHORT_COMMIT_RE.fullmatch(commit)):
+        if not FULL_COMMIT_RE.fullmatch(commit):
             failures.append("package_commit_invalid")
             failures.append("package_commit_invalid")
         info_commit_full = str(info.get("commit_full", ""))
         info_commit_full = str(info.get("commit_full", ""))
         if info_commit_full and not FULL_COMMIT_RE.fullmatch(info_commit_full):
         if info_commit_full and not FULL_COMMIT_RE.fullmatch(info_commit_full):
@@ -3296,10 +3299,7 @@ def package_selftest_payload(
             failures.append("package_commit_full_mismatch")
             failures.append("package_commit_full_mismatch")
         if info.get("project_config_sha256") != expected_project_config_sha256:
         if info.get("project_config_sha256") != expected_project_config_sha256:
             failures.append("package_project_config_sha256_mismatch")
             failures.append("package_project_config_sha256_mismatch")
-        if info.get("td_source_manifest_sha256") not in {
-            None,
-            expected_td_source_manifest_sha256,
-        }:
+        if info.get("td_source_manifest_sha256") != expected_td_source_manifest_sha256:
             failures.append("package_td_source_manifest_sha256_mismatch")
             failures.append("package_td_source_manifest_sha256_mismatch")
         if info.get("bitstream_sha256") != digest:
         if info.get("bitstream_sha256") != digest:
             failures.append("package_bitstream_sha256_mismatch")
             failures.append("package_bitstream_sha256_mismatch")
@@ -3379,15 +3379,9 @@ def package_selftest_payload(
     if primary_td:
     if primary_td:
         if not receipt_commit_matches(primary_td, package_expected_commit):
         if not receipt_commit_matches(primary_td, package_expected_commit):
             failures.append("primary_td_commit_mismatch")
             failures.append("primary_td_commit_mismatch")
-        if primary_td.get("project_config_sha256") not in {
-            None,
-            expected_project_config_sha256,
-        }:
+        if primary_td.get("project_config_sha256") != expected_project_config_sha256:
             failures.append("primary_td_project_config_sha256_mismatch")
             failures.append("primary_td_project_config_sha256_mismatch")
-        if primary_td.get("td_source_manifest_sha256") not in {
-            None,
-            expected_td_source_manifest_sha256,
-        }:
+        if primary_td.get("td_source_manifest_sha256") != expected_td_source_manifest_sha256:
             failures.append("primary_td_td_source_manifest_sha256_mismatch")
             failures.append("primary_td_td_source_manifest_sha256_mismatch")
         if primary_td.get("bitstream_sha256") != digest:
         if primary_td.get("bitstream_sha256") != digest:
             failures.append("primary_td_bitstream_sha256_mismatch")
             failures.append("primary_td_bitstream_sha256_mismatch")
@@ -3396,15 +3390,9 @@ def package_selftest_payload(
     if confirmation_td and confirmation_td.get("status") == "pass":
     if confirmation_td and confirmation_td.get("status") == "pass":
         if not receipt_commit_matches(confirmation_td, package_expected_commit):
         if not receipt_commit_matches(confirmation_td, package_expected_commit):
             failures.append("confirmation_td_commit_mismatch")
             failures.append("confirmation_td_commit_mismatch")
-        if confirmation_td.get("project_config_sha256") not in {
-            None,
-            expected_project_config_sha256,
-        }:
+        if confirmation_td.get("project_config_sha256") != expected_project_config_sha256:
             failures.append("confirmation_td_project_config_sha256_mismatch")
             failures.append("confirmation_td_project_config_sha256_mismatch")
-        if confirmation_td.get("td_source_manifest_sha256") not in {
-            None,
-            expected_td_source_manifest_sha256,
-        }:
+        if confirmation_td.get("td_source_manifest_sha256") != expected_td_source_manifest_sha256:
             failures.append("confirmation_td_td_source_manifest_sha256_mismatch")
             failures.append("confirmation_td_td_source_manifest_sha256_mismatch")
         if (
         if (
             confirmation_td.get("bitstream_sha256") not in {None, ""}
             confirmation_td.get("bitstream_sha256") not in {None, ""}
@@ -3522,7 +3510,9 @@ def package_selftest_payload(
 
 
     script_result: dict[str, Any] = {"status": "not_run", "returncode": None}
     script_result: dict[str, Any] = {"status": "not_run", "returncode": None}
     selftest = package / "selftest.ps1"
     selftest = package / "selftest.ps1"
-    if not failures and selftest.is_file():
+    if not selftest.is_file():
+        failures.append("package_selftest_script_missing")
+    elif not failures:
         completed = subprocess.run(
         completed = subprocess.run(
             [
             [
                 "powershell.exe",
                 "powershell.exe",
@@ -3667,10 +3657,8 @@ def _td_final_failures(
         failures.append(prefix + "schema")
         failures.append(prefix + "schema")
     if receipt.get("status") != "pass":
     if receipt.get("status") != "pass":
         failures.append(prefix + "status")
         failures.append(prefix + "status")
-    if not receipt_commit_matches(receipt, expected_commit):
+    if receipt_commit_full(receipt) != expected_commit:
         failures.append(prefix + "commit")
         failures.append(prefix + "commit")
-    if receipt.get("commit_full") not in {None, "", expected_commit}:
-        failures.append(prefix + "commit_full")
     if receipt.get("project_config_sha256") != expected_project_config_sha256:
     if receipt.get("project_config_sha256") != expected_project_config_sha256:
         failures.append(prefix + "project_config_sha256")
         failures.append(prefix + "project_config_sha256")
     if receipt.get("td_source_manifest_sha256") != expected_td_source_manifest_sha256:
     if receipt.get("td_source_manifest_sha256") != expected_td_source_manifest_sha256:
@@ -3806,7 +3794,7 @@ def _offline_final_failures(
     failures: list[str] = []
     failures: list[str] = []
     if receipt.get("schema") != OFFLINE_RECEIPT_SCHEMA:
     if receipt.get("schema") != OFFLINE_RECEIPT_SCHEMA:
         failures.append("offline:schema")
         failures.append("offline:schema")
-    if receipt.get("status") != "pass" or not receipt_commit_matches(receipt, expected_commit):
+    if receipt.get("status") != "pass" or receipt_commit_full(receipt) != expected_commit:
         failures.append("offline:status_or_commit")
         failures.append("offline:status_or_commit")
     if receipt.get("project_config_sha256") != expected_project_config_sha256:
     if receipt.get("project_config_sha256") != expected_project_config_sha256:
         failures.append("offline:project_config_sha256")
         failures.append("offline:project_config_sha256")
@@ -3968,7 +3956,11 @@ def _package_final_failures(
     actual_files = {
     actual_files = {
         path.relative_to(package_dir).as_posix(): sha256_file(path)
         path.relative_to(package_dir).as_posix(): sha256_file(path)
         for path in sorted(package_dir.rglob("*"))
         for path in sorted(package_dir.rglob("*"))
-        if path.is_file() and path not in {info_path, packaged_selftest_path}
+        if path.is_file() and path not in {
+            info_path,
+            package_dir / "package_manifest.json",
+            packaged_selftest_path,
+        }
     }
     }
     if not info or info.get("file_sha256") != actual_files:
     if not info or info.get("file_sha256") != actual_files:
         failures.append("package:file_manifest:info")
         failures.append("package:file_manifest:info")