Преглед изворни кода

fix(formal): keep run evidence outside git

root пре 3 месеци
родитељ
комит
0e53d1a8ab
2 измењених фајлова са 39 додато и 6 уклоњено
  1. 15 1
      tests/unit/test_6400_formal_contract.py
  2. 24 5
      tools/run_6400_formal.py

+ 15 - 1
tests/unit/test_6400_formal_contract.py

@@ -7,7 +7,9 @@ import json
 from pathlib import Path
 import re
 
-from tools.run_6400_formal import compiled_property_ids, property_cell_ids
+import pytest
+
+from tools.run_6400_formal import compiled_property_ids, formal_run_base, property_cell_ids
 
 
 ROOT = Path(__file__).resolve().parents[2]
@@ -78,6 +80,18 @@ def test_runner_is_fail_closed_and_commit_bound() -> None:
     assert '"bitstream_sha256": None' in runner
 
 
+def test_formal_run_base_is_external_and_rejects_git_worktree(
+    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+    external = tmp_path / "formal-runs"
+    monkeypatch.setenv("PRODU2_FORMAL_RUN_BASE", str(external))
+    assert formal_run_base() == external.resolve()
+
+    monkeypatch.setenv("PRODU2_FORMAL_RUN_BASE", str(ROOT / "receipts"))
+    with pytest.raises(RuntimeError, match="outside the Git worktree"):
+        formal_run_base()
+
+
 def test_runner_reads_yosys_jny_property_cell_sorts(tmp_path: Path) -> None:
     model = tmp_path / "model"
     model.mkdir()

+ 24 - 5
tools/run_6400_formal.py

@@ -42,6 +42,25 @@ def canonical_sha256(value: Any) -> str:
     return hashlib.sha256(data.encode("utf-8")).hexdigest()
 
 
+def evidence_path(path: Path) -> str:
+    resolved = path.resolve()
+    try:
+        return resolved.relative_to(ROOT.resolve()).as_posix()
+    except ValueError:
+        return resolved.as_posix()
+
+
+def formal_run_base() -> Path:
+    configured = os.environ.get("PRODU2_FORMAL_RUN_BASE")
+    base = Path(configured) if configured else (
+        Path(os.environ.get("XDG_STATE_HOME", "/tmp")) / "prod2u" / "formal-runs"
+    )
+    resolved = base.expanduser().resolve()
+    if resolved == ROOT.resolve() or ROOT.resolve() in resolved.parents:
+        raise RuntimeError("formal run base must be outside the Git worktree")
+    return resolved
+
+
 def run_text(argv: list[str], *, check: bool = True) -> str:
     result = subprocess.run(
         argv,
@@ -244,7 +263,7 @@ def property_cell_ids(values: Iterable[str]) -> set[str]:
 
 def relative_artifacts(workdir: Path, pattern: str) -> list[dict[str, str]]:
     return [
-        {"path": path.relative_to(ROOT).as_posix(), "sha256": sha256_file(path)}
+        {"path": evidence_path(path), "sha256": sha256_file(path)}
         for path in sorted(workdir.rglob(pattern))
         if path.is_file()
     ]
@@ -323,9 +342,9 @@ def run_job(job: dict[str, Any], run_root: Path, timeout: int) -> dict[str, Any]
         "timed_out": timed_out,
         "duration_seconds": round(duration, 3),
         "command": command,
-        "workdir": workdir.relative_to(ROOT).as_posix(),
+        "workdir": evidence_path(workdir),
         "process_log": {
-            "path": process_log.relative_to(ROOT).as_posix(),
+            "path": evidence_path(process_log),
             "sha256": sha256_file(process_log),
         },
         "sby_log_sha256": sha256_file(logfile) if logfile.is_file() else "0" * 64,
@@ -373,7 +392,7 @@ def main(argv: list[str] | None = None) -> int:
     run_id = args.run_id or timestamp.strftime("6400-formal-%Y%m%dT%H%M%SZ")
     if not re.fullmatch(r"[A-Za-z0-9._-]+", run_id):
         parser.error("run ID may contain only letters, digits, dot, underscore, and hyphen")
-    run_root = ROOT / "receipts" / "6400-formal" / run_id
+    run_root = formal_run_base() / run_id
     if run_root.exists():
         parser.error(f"run directory already exists: {run_root}")
     run_root.mkdir(parents=True)
@@ -474,7 +493,7 @@ def main(argv: list[str] | None = None) -> int:
     print(json.dumps({
         "status": receipt["status"],
         "run_id": run_id,
-        "receipt": receipt_path.relative_to(ROOT).as_posix(),
+        "receipt": evidence_path(receipt_path),
         "receipt_sha256": sha256_file(receipt_path),
         "target_count": len(target_results),
         "task_count": len(results),