Jelajahi Sumber

fix: make resource policy and package evidence truthful

Codex Resource Selftest 4 bulan lalu
induk
melakukan
1c622c830c

+ 3 - 3
projects/prod2u-door-temp/project.json

@@ -113,9 +113,9 @@
     "y_limit_raw": "A11"
     "y_limit_raw": "A11"
   },
   },
   "resource_policy": {
   "resource_policy": {
-    "target_ram_blocks": 1,
-    "request_ram_blocks": 1,
-    "temperature_ram_blocks": 1,
+    "target_ram": "optional",
+    "request_ram": "optional",
+    "temperature_ram": "optional",
     "dsp": 0,
     "dsp": 0,
     "shared_pwm_comparators": 1,
     "shared_pwm_comparators": 1,
     "shared_motor_executors": 1,
     "shared_motor_executors": 1,

+ 15 - 12
schemas/project.schema.json

@@ -462,24 +462,27 @@
                        "resource_policy":  {
                        "resource_policy":  {
                                          "type":  "object",
                                          "type":  "object",
                                          "required":  [
                                          "required":  [
-                                                          "target_ram_blocks",
-                                                          "request_ram_blocks",
-                                                          "temperature_ram_blocks",
+                                                          "target_ram",
+                                                          "request_ram",
+                                                          "temperature_ram",
                                                           "dsp",
                                                           "dsp",
                                                           "shared_pwm_comparators",
                                                           "shared_pwm_comparators",
                                                           "shared_motor_executors",
                                                           "shared_motor_executors",
                                                           "shared_i2c_readers"
                                                           "shared_i2c_readers"
                                                       ],
                                                       ],
                                          "properties":  {
                                          "properties":  {
-                                                            "target_ram_blocks":  {
-                                                                                      "type":  "integer"
-                                                                                  },
-                                                            "request_ram_blocks":  {
-                                                                                       "type":  "integer"
-                                                                                   },
-                                                            "temperature_ram_blocks":  {
-                                                                                           "type":  "integer"
-                                                                                       },
+                                                            "target_ram":  {
+                                                                                 "type":  "string",
+                                                                                 "enum":  ["optional", "required", "forbidden"]
+                                                                             },
+                                                            "request_ram":  {
+                                                                                  "type":  "string",
+                                                                                  "enum":  ["optional", "required", "forbidden"]
+                                                                              },
+                                                            "temperature_ram":  {
+                                                                                         "type":  "string",
+                                                                                         "enum":  ["optional", "required", "forbidden"]
+                                                                                     },
                                                             "dsp":  {
                                                             "dsp":  {
                                                                         "type":  "integer"
                                                                         "type":  "integer"
                                                                     },
                                                                     },

+ 26 - 0
src/generated/prod2u_door_temp.adc

@@ -0,0 +1,26 @@
+# Generated from project.json. Do not edit.
+# Active interface only; reserved board loads are intentionally absent.
+set_pin_assignment { sys_clk } { LOCATION = J1; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { host_uart_rx } { LOCATION = N11; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { host_uart_tx } { LOCATION = M10; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { temp_scl } { LOCATION = M8; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { temp_sda } { LOCATION = N9; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[0] } { LOCATION = G16; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[1] } { LOCATION = F16; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[2] } { LOCATION = N1; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[3] } { LOCATION = M1; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[4] } { LOCATION = R16; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[5] } { LOCATION = P16; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[6] } { LOCATION = P9; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { ptc_pwm_out[7] } { LOCATION = T9; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { fan_pwm_out[0] } { LOCATION = B1; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { fan_pwm_out[1] } { LOCATION = C4; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { fan_pwm_out[2] } { LOCATION = B5; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { fan_pwm_out[3] } { LOCATION = A14; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { motor_en_n } { LOCATION = A10; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { x_step } { LOCATION = C11; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { x_dir } { LOCATION = B12; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { y_step } { LOCATION = D15; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { y_dir } { LOCATION = H14; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { x_limit_raw } { LOCATION = B9; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }
+set_pin_assignment { y_limit_raw } { LOCATION = A11; IOSTANDARD = LVCMOS33; PULLTYPE = NONE; }

+ 53 - 0
tools/codex_harness.py

@@ -43,6 +43,11 @@ def _receipt_payload(*, name: str, run_id: str, commit: str, bitstream_sha256: s
         "commit": commit,
         "commit": commit,
         "bitstream_sha256": bitstream_sha256,
         "bitstream_sha256": bitstream_sha256,
         "project_config_sha256": project_config_sha256,
         "project_config_sha256": project_config_sha256,
+        "td_source_manifest_sha256": None,
+        "command": None,
+        "tool_version": None,
+        "placeholder": True,
+        "authoritative": False,
         "authority_boundary": "Offline package placeholder. No COM, JTAG, programming, or board action.",
         "authority_boundary": "Offline package placeholder. No COM, JTAG, programming, or board action.",
     }
     }
 
 
@@ -55,6 +60,8 @@ def _copy_bound_receipt(
     commit: str,
     commit: str,
     bitstream_sha256: str,
     bitstream_sha256: str,
     project_config_sha256: str,
     project_config_sha256: str,
+    td_source_manifest_sha256: str | None = None,
+    receipt_role: str | None = None,
     preserve_bitstream_sha256: bool = False,
     preserve_bitstream_sha256: bool = False,
     require_bitstream_sha256: bool = False,
     require_bitstream_sha256: bool = False,
 ) -> dict[str, Any]:
 ) -> dict[str, Any]:
@@ -69,6 +76,8 @@ def _copy_bound_receipt(
         "commit": commit,
         "commit": commit,
         "project_config_sha256": project_config_sha256,
         "project_config_sha256": project_config_sha256,
     }
     }
+    if td_source_manifest_sha256 is not None:
+        bindings["td_source_manifest_sha256"] = td_source_manifest_sha256
     for field, expected in bindings.items():
     for field, expected in bindings.items():
         observed = value.get(field)
         observed = value.get(field)
         if observed not in {None, "", expected}:
         if observed not in {None, "", expected}:
@@ -85,6 +94,13 @@ def _copy_bound_receipt(
     elif value.get("status") == "pass" and not SHA256_RE.fullmatch(str(value.get("bitstream_sha256", ""))):
     elif value.get("status") == "pass" and not SHA256_RE.fullmatch(str(value.get("bitstream_sha256", ""))):
         raise ValueError(f"confirmation receipt requires its own bitstream SHA256: {source}")
         raise ValueError(f"confirmation receipt requires its own bitstream SHA256: {source}")
     value["commit_full"] = commit
     value["commit_full"] = commit
+    if receipt_role is not None:
+        value["receipt_role"] = receipt_role
+        value["command"] = value.get("command") or f"final verification lane: {receipt_role}"
+        value["tool_version"] = value.get("tool_version") or "captured by bound offline aggregate"
+    if value.get("status") == "pass":
+        value["authoritative"] = True
+        value["placeholder"] = False
     value["source_receipt_sha256"] = sha256_file(source)
     value["source_receipt_sha256"] = sha256_file(source)
     _write_json(destination, value)
     _write_json(destination, value)
     return value
     return value
@@ -519,6 +535,7 @@ def create_unverified_package(
     production_integration_commit: str | None = None,
     production_integration_commit: str | None = None,
     source_manifest_sha256: str | None = None,
     source_manifest_sha256: str | None = None,
     td_receipt_sha256: str | None = None,
     td_receipt_sha256: str | None = None,
+    offline_receipt: Path | None = None,
     package_class: str = "UNVERIFIED",
     package_class: str = "UNVERIFIED",
 ) -> Path:
 ) -> Path:
     if not (COMMIT_RE.fullmatch(commit) or FULL_COMMIT_RE.fullmatch(commit)):
     if not (COMMIT_RE.fullmatch(commit) or FULL_COMMIT_RE.fullmatch(commit)):
@@ -566,6 +583,13 @@ def create_unverified_package(
         if not FULL_COMMIT_RE.fullmatch(resolved) or resolved[:8] != commit:
         if not FULL_COMMIT_RE.fullmatch(resolved) or resolved[:8] != commit:
             raise ValueError("short commit does not match the current full HEAD")
             raise ValueError("short commit does not match the current full HEAD")
         commit_full = resolved
         commit_full = resolved
+    if package_class == "UNVERIFIED_FINAL_CANDIDATE" and offline_receipt is not None:
+        identity = _git_identity(repo)
+        if identity["head"] != commit_full:
+            raise ValueError("final package commit does not match current HEAD")
+        clean = _run_git(repo, ["status", "--porcelain"]).stdout.decode("utf-8", "replace")
+        if clean:
+            raise ValueError("final package requires a clean worktree")
     compatibility_commit = commit
     compatibility_commit = commit
     bitstream_sha256 = sha256_file(bitstream)
     bitstream_sha256 = sha256_file(bitstream)
     package = Path(output_root) / PROJECT_ID / f"{compatibility_commit}-{bitstream_sha256[:16]}"
     package = Path(output_root) / PROJECT_ID / f"{compatibility_commit}-{bitstream_sha256[:16]}"
@@ -602,6 +626,27 @@ def create_unverified_package(
     observed_source_manifest_sha256 = sha256_file(package / "source_manifest.json")
     observed_source_manifest_sha256 = sha256_file(package / "source_manifest.json")
     if source_manifest_sha256 is not None and source_manifest_sha256 != observed_source_manifest_sha256:
     if source_manifest_sha256 is not None and source_manifest_sha256 != observed_source_manifest_sha256:
         raise ValueError("source manifest hash mismatch")
         raise ValueError("source manifest hash mismatch")
+    offline_payload: dict[str, Any] | None = None
+    if offline_receipt is not None:
+        try:
+            loaded = json.loads(Path(offline_receipt).read_text(encoding="utf-8-sig"))
+        except (OSError, UnicodeError, json.JSONDecodeError) as exc:
+            raise ValueError(f"offline receipt is not readable JSON: {offline_receipt}") from exc
+        if not isinstance(loaded, dict):
+            raise ValueError("offline receipt root must be an object")
+        expected_bindings = {
+            "verification_run_id": run_id,
+            "commit_full": commit_full,
+            "project_config_sha256": project_config_sha256,
+            "td_source_manifest_sha256": sha256_file(repo / SOURCE_MANIFEST_PATH),
+        }
+        for field, expected in expected_bindings.items():
+            if loaded.get(field) != expected:
+                raise ValueError(f"offline receipt binding mismatch for {field}")
+        if loaded.get("status") != "pass":
+            raise ValueError("offline receipt must have status=pass")
+        offline_payload = loaded
+        shutil.copy2(Path(offline_receipt), evidence / "software-offline-receipt.json")
     receipt_sources = dict(verification_receipts or {})
     receipt_sources = dict(verification_receipts or {})
     if td_receipt is not None:
     if td_receipt is not None:
         receipt = Path(td_receipt)
         receipt = Path(td_receipt)
@@ -616,6 +661,8 @@ def create_unverified_package(
             commit=commit_full,
             commit=commit_full,
             bitstream_sha256=bitstream_sha256,
             bitstream_sha256=bitstream_sha256,
             project_config_sha256=project_config_sha256,
             project_config_sha256=project_config_sha256,
+            td_source_manifest_sha256=sha256_file(repo / SOURCE_MANIFEST_PATH),
+            receipt_role="td-primary",
         )
         )
     else:
     else:
         _write_json(
         _write_json(
@@ -643,6 +690,8 @@ def create_unverified_package(
                 commit=commit_full,
                 commit=commit_full,
                 bitstream_sha256=bitstream_sha256,
                 bitstream_sha256=bitstream_sha256,
                 project_config_sha256=project_config_sha256,
                 project_config_sha256=project_config_sha256,
+                td_source_manifest_sha256=sha256_file(repo / SOURCE_MANIFEST_PATH),
+                receipt_role=name,
                 preserve_bitstream_sha256=name == "td-confirmation",
                 preserve_bitstream_sha256=name == "td-confirmation",
                 require_bitstream_sha256=name == "td-primary",
                 require_bitstream_sha256=name == "td-primary",
             )
             )
@@ -748,6 +797,7 @@ def create_unverified_package(
         "confirmation_bitstream_sha256": confirmation_bitstream_sha256,
         "confirmation_bitstream_sha256": confirmation_bitstream_sha256,
         "confirmation_synthesis_input_hash": confirmation_synthesis_input_hash,
         "confirmation_synthesis_input_hash": confirmation_synthesis_input_hash,
         "td_source_manifest_sha256": sha256_file(repo / SOURCE_MANIFEST_PATH),
         "td_source_manifest_sha256": sha256_file(repo / SOURCE_MANIFEST_PATH),
+        "software_offline_receipt": "evidence/software-offline-receipt.json" if offline_payload is not None else None,
         "canonical_bitstream_build": "primary",
         "canonical_bitstream_build": "primary",
         "bitstream_hash_equal": confirmation_bitstream_sha256 == bitstream_sha256 if confirmation_bitstream_sha256 else None,
         "bitstream_hash_equal": confirmation_bitstream_sha256 == bitstream_sha256 if confirmation_bitstream_sha256 else None,
         "td_receipt_sha256": sha256_file(evidence / "td-primary-receipt.json"),
         "td_receipt_sha256": sha256_file(evidence / "td-primary-receipt.json"),
@@ -785,6 +835,7 @@ def create_unverified_package(
         "td_receipt_sha256": sha256_file(evidence / "td-primary-receipt.json"),
         "td_receipt_sha256": sha256_file(evidence / "td-primary-receipt.json"),
         "td_source_receipt_sha256": td_receipt_sha256,
         "td_source_receipt_sha256": td_receipt_sha256,
         "td_source_manifest_sha256": sha256_file(repo / SOURCE_MANIFEST_PATH),
         "td_source_manifest_sha256": sha256_file(repo / SOURCE_MANIFEST_PATH),
+        "software_offline_receipt": "evidence/software-offline-receipt.json" if offline_payload is not None else None,
         "canonical_bitstream_build": "primary",
         "canonical_bitstream_build": "primary",
         "confirmation_bitstream_sha256": confirmation_bitstream_sha256,
         "confirmation_bitstream_sha256": confirmation_bitstream_sha256,
         "bitstream_hash_equal": confirmation_bitstream_sha256 == bitstream_sha256 if confirmation_bitstream_sha256 else None,
         "bitstream_hash_equal": confirmation_bitstream_sha256 == bitstream_sha256 if confirmation_bitstream_sha256 else None,
@@ -821,6 +872,7 @@ def main() -> int:
     package.add_argument("--production-integration-commit")
     package.add_argument("--production-integration-commit")
     package.add_argument("--source-manifest-sha256")
     package.add_argument("--source-manifest-sha256")
     package.add_argument("--td-receipt-sha256")
     package.add_argument("--td-receipt-sha256")
+    package.add_argument("--offline-receipt", type=Path)
     package.add_argument(
     package.add_argument(
         "--package-class",
         "--package-class",
         choices=("UNVERIFIED", "UNVERIFIED_EARLY_BUILD", "UNVERIFIED_FINAL_CANDIDATE"),
         choices=("UNVERIFIED", "UNVERIFIED_EARLY_BUILD", "UNVERIFIED_FINAL_CANDIDATE"),
@@ -858,6 +910,7 @@ def main() -> int:
             production_integration_commit=args.production_integration_commit,
             production_integration_commit=args.production_integration_commit,
             source_manifest_sha256=args.source_manifest_sha256,
             source_manifest_sha256=args.source_manifest_sha256,
             td_receipt_sha256=args.td_receipt_sha256,
             td_receipt_sha256=args.td_receipt_sha256,
+            offline_receipt=args.offline_receipt,
             package_class=args.package_class,
             package_class=args.package_class,
         ))
         ))
     return 0
     return 0

+ 10 - 4
tools/generate_project.py

@@ -287,9 +287,9 @@ def _validate_project(payload: Mapping[str, Any]) -> None:
     _keys(
     _keys(
         resources,
         resources,
         {
         {
-            "target_ram_blocks",
-            "request_ram_blocks",
-            "temperature_ram_blocks",
+            "target_ram",
+            "request_ram",
+            "temperature_ram",
             "dsp",
             "dsp",
             "shared_pwm_comparators",
             "shared_pwm_comparators",
             "shared_motor_executors",
             "shared_motor_executors",
@@ -298,7 +298,13 @@ def _validate_project(payload: Mapping[str, Any]) -> None:
         "resource_policy",
         "resource_policy",
     )
     )
     for key, value in resources.items():
     for key, value in resources.items():
-        _integer(value, f"resource_policy.{key}", minimum=0)
+        if key in {"target_ram", "request_ram", "temperature_ram"}:
+            _require(
+                value in {"optional", "required", "forbidden"},
+                f"resource_policy.{key} must be optional, required, or forbidden",
+            )
+        else:
+            _integer(value, f"resource_policy.{key}", minimum=0)
 
 
     protocol = _object(payload["protocol"], "protocol")
     protocol = _object(payload["protocol"], "protocol")
     _keys(protocol, {"header_hex", "crc", "commands", "set_all", "temperature_data"}, "protocol")
     _keys(protocol, {"header_hex", "crc", "commands", "set_all", "temperature_data"}, "protocol")

+ 28 - 1
tools/prod2u_p4_harness.py

@@ -3533,7 +3533,7 @@ def package_selftest_payload(
         if completed.returncode != 0:
         if completed.returncode != 0:
             failures.append("package_selftest_script_failed")
             failures.append("package_selftest_script_failed")
 
 
-    return {
+    result = {
         "schema": PACKAGE_RECEIPT_SCHEMA,
         "schema": PACKAGE_RECEIPT_SCHEMA,
         "status": "pass" if not failures else "fail",
         "status": "pass" if not failures else "fail",
         "generated_at": utc_now(),
         "generated_at": utc_now(),
@@ -3587,6 +3587,33 @@ def package_selftest_payload(
         "selftest": script_result,
         "selftest": script_result,
         "safety": dict(SAFETY_BOUNDARY),
         "safety": dict(SAFETY_BOUNDARY),
     }
     }
+    packaged_selftest_path.write_text(
+        json.dumps(result, indent=2, sort_keys=True) + "\n",
+        encoding="utf-8",
+        newline="\n",
+    )
+    try:
+        package_info = json.loads(info_path.read_text(encoding="utf-8-sig"))
+        if isinstance(package_info, dict):
+            package_info["package_selftest_result_sha256"] = sha256_file(packaged_selftest_path)
+            info_path.write_text(
+                json.dumps(package_info, indent=2, sort_keys=True) + "\n",
+                encoding="utf-8",
+                newline="\n",
+            )
+        if manifest_path.is_file():
+            package_manifest = json.loads(manifest_path.read_text(encoding="utf-8-sig"))
+            if isinstance(package_manifest, dict) and info_path.is_file():
+                package_manifest["package_info_sha256"] = sha256_file(info_path)
+                manifest_path.write_text(
+                    json.dumps(package_manifest, indent=2, sort_keys=True) + "\n",
+                    encoding="utf-8",
+                    newline="\n",
+                )
+    except (OSError, UnicodeError, json.JSONDecodeError):
+        result["failures"] = sorted(set(result["failures"] + ["package_selftest_persistence_failed"]))
+        result["status"] = "fail"
+    return result
 
 
 
 
 def cmd_strict_offline(args: argparse.Namespace) -> int:
 def cmd_strict_offline(args: argparse.Namespace) -> int: