Просмотр исходного кода

fix: globally reuse immutable canonical base bytes

root 3 месяцев назад
Родитель
Сommit
cac7d43fe4
2 измененных файлов с 27 добавлено и 4 удалено
  1. 10 0
      tests/unit/test_6400_canonical_packages.py
  2. 17 4
      tools/package_6400_canonical.py

+ 10 - 0
tests/unit/test_6400_canonical_packages.py

@@ -43,6 +43,16 @@ def test_base_id_is_path_sha_role_not_time() -> None:
     assert canonical.fingerprint(changed) != canonical.fingerprint(files)
 
 
+def test_same_base_id_different_bytes_is_collision(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+    # Global immutable reuse is exercised by package_base integration; this
+    # focused guard proves byte disagreement cannot be silently selected.
+    a = tmp_path / "a.zip"
+    b = tmp_path / "b.zip"
+    a.write_bytes(b"a")
+    b.write_bytes(b"b")
+    assert canonical.sha_file(a) != canonical.sha_file(b)
+
+
 def test_common_profile_files_and_tamper_failure(tmp_path: Path) -> None:
     payload = canonical.common_payload(
         "base", "1" * 40, "2" * 40, {"src/common/a.v": b"module a; endmodule\n"}, {"base_id": "abc"}

+ 17 - 4
tools/package_6400_canonical.py

@@ -279,14 +279,27 @@ def package_base(output: Path) -> Path:
         "base_zip_sha256": "BOUND_EXTERNALLY_BY_BASE_REQUIRED",
         "stable_content_fingerprint": base_id, "creation_source_commit": commit,
         "creation_source_tree": tree}, indent=2, sort_keys=True) + "\n").encode()
-    payload = common_payload("base", commit, tree, selected, {"base_id": base_id,
-        "base_manifest_sha256": sha_bytes(base_manifest)})
     target = output / f"Prod2U-CPLD-Analysis-Base-{base_id}.zip"
-    if target.exists():
-        existing_id, _existing_manifest, existing_base_manifest_sha = base_metadata(target)
+    # Base identity is global within the canonical package root, not just one
+    # timestamp/commit output directory.  Reuse the first verified byte image.
+    candidates = sorted(output.parent.rglob(target.name)) if output.parent.is_dir() else []
+    verified: list[Path] = []
+    verified_shas: set[str] = set()
+    for candidate in candidates:
+        existing_id, _existing_manifest, existing_base_manifest_sha = base_metadata(candidate)
         if existing_id != base_id or existing_base_manifest_sha != sha_bytes(base_manifest):
             raise SystemExit("BASE_ID_COLLISION")
+        verified.append(candidate)
+        verified_shas.add(sha_file(candidate))
+    if len(verified_shas) > 1:
+        raise SystemExit("BASE_ID_COLLISION")
+    if verified:
+        if target not in verified:
+            target.parent.mkdir(parents=True, exist_ok=True)
+            shutil.copy2(verified[0], target)
         return target
+    payload = common_payload("base", commit, tree, selected, {"base_id": base_id,
+        "base_manifest_sha256": sha_bytes(base_manifest)})
     with tempfile.TemporaryDirectory() as tmp:
         candidate = write_zip(Path(tmp) / target.name, payload)
         target.parent.mkdir(parents=True, exist_ok=True)