fix: enforce strict CAD showcase delivery gates
This commit is contained in:
@@ -8,8 +8,11 @@ import hashlib
|
||||
import importlib.metadata
|
||||
import json
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
@@ -32,8 +35,10 @@ def fingerprint(config: Path, source_report: Path, dxf_dir: Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
tracked = [config, source_report, *sorted(dxf_dir.glob("*.dxf"))]
|
||||
tracked.extend(sorted(SCRIPTS.glob("*.py")))
|
||||
tracked.extend(sorted(SCRIPTS.glob("*.mjs")))
|
||||
tracked.extend(sorted((ROOT / "schemas").glob("*.json")))
|
||||
tracked.extend(sorted((ROOT / "assets" / "web-showcase-template").glob("*")))
|
||||
tracked.extend(path for path in (ROOT / "package.json", ROOT / "package-lock.json") if path.exists())
|
||||
for path in tracked:
|
||||
if not path.is_file():
|
||||
continue
|
||||
@@ -132,8 +137,122 @@ def python_script(name: str, *args: str) -> list[str]:
|
||||
return [sys.executable, str(SCRIPTS / name), *args]
|
||||
|
||||
|
||||
def parse_waivers(values: list[str]) -> dict[str, str]:
|
||||
waivers: dict[str, str] = {}
|
||||
for value in values:
|
||||
name, separator, reason = value.partition("=")
|
||||
name = name.strip()
|
||||
reason = reason.strip()
|
||||
if not separator or not name or not reason:
|
||||
raise ValueError(f"Invalid waiver {value!r}; expected CHECK=REASON")
|
||||
if name in waivers:
|
||||
raise ValueError(f"Duplicate waiver for {name!r}")
|
||||
waivers[name] = reason
|
||||
return waivers
|
||||
|
||||
|
||||
def enforce_audit_gates(pipeline: Pipeline, audit_path: Path, waivers: dict[str, str]) -> bool:
|
||||
report = json.loads(audit_path.read_text(encoding="utf-8"))
|
||||
blocked = []
|
||||
waived = []
|
||||
for gate in report.get("gates", []):
|
||||
status = str(gate.get("status", "FAIL"))
|
||||
if status == "PASS":
|
||||
continue
|
||||
name = f"audit:{gate.get('name', 'unnamed')}"
|
||||
if status in {"WARN", "SKIP"} and name in waivers:
|
||||
waived.append({"name": name, "original_status": status, "reason": waivers[name]})
|
||||
else:
|
||||
blocked.append({"name": name, "status": status, "detail": gate.get("detail", "")})
|
||||
pipeline.state["audit_waivers"] = waived
|
||||
if blocked:
|
||||
pipeline.state["status"] = "BLOCKED"
|
||||
pipeline.state["blocked_gates"] = blocked
|
||||
pipeline.save()
|
||||
for gate in blocked:
|
||||
print(f"GATE BLOCKED [{gate['name']}]: {gate['detail']}", file=sys.stderr)
|
||||
return False
|
||||
pipeline.save()
|
||||
return True
|
||||
|
||||
|
||||
def available_port() -> int:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as stream:
|
||||
stream.bind(("127.0.0.1", 0))
|
||||
return int(stream.getsockname()[1])
|
||||
|
||||
|
||||
def wait_for_http(url: str, timeout: float = 20.0) -> None:
|
||||
deadline = time.monotonic() + timeout
|
||||
last_error: Exception | None = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=1) as response:
|
||||
if response.status < 500:
|
||||
return
|
||||
except Exception as exc: # pragma: no cover - timing depends on host
|
||||
last_error = exc
|
||||
time.sleep(0.2)
|
||||
raise RuntimeError(f"Local Web server did not become ready: {last_error}")
|
||||
|
||||
|
||||
def run_browser_smoke(pipeline: Pipeline, web_dir: Path, report: Path, screenshots: Path) -> None:
|
||||
"""Serve the formal Web bundle and mechanically prove desktop/mobile loading."""
|
||||
pipeline.run(
|
||||
"browser_dependencies",
|
||||
["npm", "install", "--prefix", str(ROOT), "--ignore-scripts"],
|
||||
[ROOT / "node_modules" / "playwright" / "package.json"],
|
||||
)
|
||||
pipeline.run(
|
||||
"browser_runtime",
|
||||
[str(ROOT / "node_modules" / ".bin" / "playwright"), "install", "chromium"],
|
||||
[],
|
||||
)
|
||||
report.parent.mkdir(parents=True, exist_ok=True)
|
||||
port = available_port()
|
||||
url = f"http://127.0.0.1:{port}/"
|
||||
server = subprocess.Popen(
|
||||
[
|
||||
sys.executable,
|
||||
"-m",
|
||||
"http.server",
|
||||
str(port),
|
||||
"--bind",
|
||||
"127.0.0.1",
|
||||
"--directory",
|
||||
str(web_dir),
|
||||
],
|
||||
cwd=ROOT,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
try:
|
||||
wait_for_http(url)
|
||||
pipeline.run(
|
||||
"browser_smoke",
|
||||
[
|
||||
"node",
|
||||
str(SCRIPTS / "web_smoke_test.mjs"),
|
||||
"--url",
|
||||
url,
|
||||
"--out",
|
||||
str(report),
|
||||
"--screenshots-dir",
|
||||
str(screenshots),
|
||||
],
|
||||
[report, screenshots],
|
||||
)
|
||||
finally:
|
||||
server.terminate()
|
||||
try:
|
||||
server.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired: # pragma: no cover - defensive cleanup
|
||||
server.kill()
|
||||
server.wait(timeout=5)
|
||||
|
||||
|
||||
def doctor() -> int:
|
||||
modules = ["ezdxf", "jsonschema", "numpy", "shapely", "trimesh", "matplotlib", "PIL", "pygltflib"]
|
||||
modules = ["ezdxf", "jsonschema", "numpy", "scipy", "shapely", "trimesh", "matplotlib", "PIL", "pygltflib"]
|
||||
checks = []
|
||||
failed = False
|
||||
for module in modules:
|
||||
@@ -210,6 +329,11 @@ def run_pipeline(args: argparse.Namespace) -> int:
|
||||
return 3
|
||||
out_dir = Path(args.out_dir).resolve()
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
waivers = parse_waivers(args.waiver)
|
||||
except ValueError as exc:
|
||||
print(f"GATE FAIL [waiver]: {exc}", file=sys.stderr)
|
||||
return 3
|
||||
pipeline = Pipeline(out_dir / "pipeline_state.json", fingerprint(config, source_report, dxf_dir), args.resume)
|
||||
spec = out_dir / "building_spec.json"
|
||||
audit = out_dir / "extraction_audit.json"
|
||||
@@ -247,6 +371,8 @@ def run_pipeline(args: argparse.Namespace) -> int:
|
||||
if args.frames:
|
||||
audit_cmd.extend(["--frames", *args.frames])
|
||||
pipeline.run("audit", audit_cmd, [audit, audit_md])
|
||||
if not enforce_audit_gates(pipeline, audit, waivers):
|
||||
return 3
|
||||
pipeline.run(
|
||||
"overlays",
|
||||
python_script(
|
||||
@@ -307,6 +433,10 @@ def run_pipeline(args: argparse.Namespace) -> int:
|
||||
["npm", "install", "--prefix", str(web_dir), "--ignore-scripts"],
|
||||
[web_dir / "node_modules" / "three" / "build" / "three.module.js"],
|
||||
)
|
||||
browser_report = (
|
||||
Path(args.browser_report).resolve() if args.browser_report else out_dir / "browser_smoke_report.json"
|
||||
)
|
||||
run_browser_smoke(pipeline, web_dir, browser_report, out_dir / "browser_smoke")
|
||||
|
||||
standard = bundle / "variants" / "standard" / "standard.glb"
|
||||
offline = out_dir / "viewer.html"
|
||||
@@ -355,20 +485,32 @@ def run_pipeline(args: argparse.Namespace) -> int:
|
||||
str(manifest),
|
||||
"--render",
|
||||
str(preview),
|
||||
"--audit",
|
||||
str(audit),
|
||||
"--budget-mb",
|
||||
str(args.budget_mb),
|
||||
"--evidence-out",
|
||||
str(evidence),
|
||||
)
|
||||
if args.web:
|
||||
verify_cmd.extend(["--web-dir", str(web_dir)])
|
||||
verify_cmd.extend(
|
||||
["--web-dir", str(web_dir), "--browser-report", str(browser_report), "--require-browser-report"]
|
||||
)
|
||||
if args.offline:
|
||||
verify_cmd.extend(["--offline-viewer", str(offline)])
|
||||
if args.browser_report:
|
||||
verify_cmd.extend(["--browser-report", args.browser_report])
|
||||
if args.require_browser_report:
|
||||
if args.require_browser_report and not args.web:
|
||||
verify_cmd.append("--require-browser-report")
|
||||
if args.scope_manifest:
|
||||
verify_cmd.extend(["--scope-manifest", str(Path(args.scope_manifest).resolve())])
|
||||
for value in args.waiver:
|
||||
verify_cmd.extend(["--waiver", value])
|
||||
pipeline.run("delivery_verification", verify_cmd, [evidence])
|
||||
verification = json.loads(evidence.read_text(encoding="utf-8"))
|
||||
if verification.get("status") != "PASS":
|
||||
pipeline.state["status"] = "BLOCKED"
|
||||
pipeline.save()
|
||||
print(f"GATE BLOCKED [delivery_verification]: {verification.get('status')}", file=sys.stderr)
|
||||
return 3
|
||||
pipeline.state["status"] = "PASS"
|
||||
pipeline.state["finished_at"] = now()
|
||||
pipeline.save()
|
||||
@@ -401,6 +543,14 @@ def parser() -> argparse.ArgumentParser:
|
||||
run.add_argument("--offline", action="store_true")
|
||||
run.add_argument("--browser-report")
|
||||
run.add_argument("--require-browser-report", action="store_true")
|
||||
run.add_argument("--scope-manifest", help="Single-building/campus scope ledger included in final verification")
|
||||
run.add_argument(
|
||||
"--waiver",
|
||||
action="append",
|
||||
default=[],
|
||||
metavar="CHECK=REASON",
|
||||
help="Explicitly acknowledge one WARN/SKIP audit or delivery check",
|
||||
)
|
||||
run.add_argument("--budget-mb", type=float, default=25.0)
|
||||
run.add_argument("--resume", action="store_true")
|
||||
return root
|
||||
|
||||
Reference in New Issue
Block a user