fix: close remaining CAD delivery review gaps

This commit is contained in:
2026-07-11 15:26:43 +08:00
parent 6c1336fa59
commit c12d1d57cb
11 changed files with 464 additions and 93 deletions

View File

@@ -21,6 +21,7 @@ from typing import Any
import showcase_schema
from build_showcase_variants import default_blender
from cad_dxf_report import oda_converter
from delivery_waivers import apply_waivers, parse_waivers
ROOT = Path(__file__).resolve().parent.parent
@@ -192,33 +193,28 @@ def node_script(path: Path, *args: str) -> list[str]:
return [resolve_executable("node"), str(path), *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 in {"PASS", "NOT_APPLICABLE"}:
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", "")})
checks = [
{
"name": f"audit:{gate.get('name', 'unnamed')}",
"status": str(gate.get("status", "FAIL")),
"detail": str(gate.get("detail", "")),
}
for gate in report.get("gates", [])
]
audit_waivers = {name: reason for name, reason in waivers.items() if name.startswith("audit:")}
apply_waivers(checks, audit_waivers)
waived = [
{
"name": check["name"],
"original_status": check["original_status"],
"reason": check["waiver_reason"],
}
for check in checks
if "waiver_reason" in check
]
blocked = [check for check in checks if check.get("status") not in {"PASS", "NOT_APPLICABLE"}]
pipeline.state["audit_waivers"] = waived
if blocked:
pipeline.state["status"] = "BLOCKED"
@@ -231,6 +227,95 @@ def enforce_audit_gates(pipeline: Pipeline, audit_path: Path, waivers: dict[str,
return True
def preflight_waiver_targets(
args: argparse.Namespace,
source_report: dict[str, Any],
audit_report: dict[str, Any],
scope_manifest: Path,
waivers: dict[str, str],
) -> list[dict[str, Any]]:
"""Return invalid waiver checks before overlays/model builds begin.
The orchestrator knows every check that can legitimately be WARN/SKIP by
this point. Browser/offline checks generated by this pipeline are expected
to PASS; the only future SKIP is an explicitly required browser report when
no served Web delivery was requested.
"""
source_gate = source_report.get("gate") or {}
checks = [
{
"name": "cad_source_report",
"status": str(source_gate.get("status", "FAIL")),
"detail": str(source_gate.get("detail", "")),
}
]
checks.extend(
{
"name": f"audit:{gate.get('name', 'unnamed')}",
"status": str(gate.get("status", "FAIL")),
"detail": str(gate.get("detail", "")),
}
for gate in audit_report.get("gates", [])
)
try:
scope = json.loads(scope_manifest.read_text(encoding="utf-8"))
requested = scope.get("requested", [])
raw_buildings = scope.get("buildings", [])
if not isinstance(raw_buildings, list):
raw_buildings = []
buildings = {str(item.get("id", "")): item for item in raw_buildings if isinstance(item, dict)}
for building_id in requested if isinstance(requested, list) else []:
item = buildings.get(str(building_id), {})
status = str(item.get("status", "FAIL"))
reason = str(item.get("reason", "")).strip()
check_status = "WARN" if status == "BLOCKED" and reason else "PASS" if status == "PASS" else "FAIL"
checks.append(
{
"name": f"scope:building:{building_id}",
"status": check_status,
"detail": reason or f"Scope building status is {status}",
}
)
if scope.get("mode") == "campus":
site = scope.get("site", {}) if isinstance(scope.get("site"), dict) else {}
status = str(site.get("status", "FAIL"))
reason = str(site.get("reason", "")).strip()
check_status = "WARN" if status == "BLOCKED" and reason else "PASS" if status == "PASS" else "FAIL"
checks.append(
{
"name": "scope:site",
"status": check_status,
"detail": reason or f"Campus site status is {status}",
}
)
except (OSError, json.JSONDecodeError):
# Final scope verification reports the malformed manifest. With no
# trustworthy target inventory, every scope waiver remains unused.
pass
if args.web:
checks.append({"name": "browser:smoke", "status": "PASS", "detail": "Generated browser smoke required"})
elif args.require_browser_report:
checks.append(
{
"name": "browser:smoke",
"status": "SKIP",
"detail": "Required browser report without a served Web delivery",
}
)
if args.offline:
checks.append(
{
"name": "offline:browser-smoke",
"status": "PASS",
"detail": "Generated offline browser smoke required",
}
)
apply_waivers(checks, waivers)
return [
check for check in checks if check.get("status") == "FAIL" and str(check.get("name", "")).startswith("waiver:")
]
def available_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as stream:
stream.bind(("127.0.0.1", 0))
@@ -426,13 +511,29 @@ def run_pipeline(args: argparse.Namespace) -> int:
print(f"GATE FAIL [source_report]: {exc}", file=sys.stderr)
return 3
source_status = str((report.get("gate") or {}).get("status", "FAIL"))
if source_status == "WARN" and "cad_source_report" not in waivers:
source_checks = [
{
"name": "cad_source_report",
"status": source_status,
"detail": str((report.get("gate") or {}).get("detail", "")),
}
]
source_waivers = {name: reason for name, reason in waivers.items() if name == "cad_source_report"}
apply_waivers(source_checks, source_waivers)
if source_checks[0]["status"] == "WARN":
print(
"GATE BLOCKED [cad_source_report]: partial source report requires "
"--waiver 'cad_source_report=<user-approved reason>'",
file=sys.stderr,
)
return 3
invalid_source_waiver = next((check for check in source_checks if check["status"] == "FAIL"), None)
if invalid_source_waiver:
print(
f"GATE FAIL [{invalid_source_waiver['name']}]: {invalid_source_waiver['detail']}",
file=sys.stderr,
)
return 3
out_dir = Path(args.out_dir).resolve()
out_dir.mkdir(parents=True, exist_ok=True)
pipeline = Pipeline(
@@ -482,6 +583,15 @@ def run_pipeline(args: argparse.Namespace) -> int:
if args.frames:
audit_cmd.extend(["--frames", *args.frames])
pipeline.run("audit", audit_cmd, [audit, audit_md])
audit_report = json.loads(audit.read_text(encoding="utf-8"))
invalid_waivers = preflight_waiver_targets(args, report, audit_report, scope_manifest, waivers)
if invalid_waivers:
pipeline.state["status"] = "BLOCKED"
pipeline.state["invalid_waivers"] = invalid_waivers
pipeline.save()
for check in invalid_waivers:
print(f"GATE FAIL [{check['name']}]: {check['detail']}", file=sys.stderr)
return 3
if not enforce_audit_gates(pipeline, audit, waivers):
return 3
pipeline.run(