fix: close audited CAD delivery gate gaps
This commit is contained in:
@@ -9,6 +9,7 @@ import gzip
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import statistics
|
||||
import sys
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
@@ -123,14 +124,57 @@ def inspect_render(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
from PIL import Image, ImageStat
|
||||
|
||||
image = Image.open(path).convert("RGB")
|
||||
stat = ImageStat.Stat(image.resize((128, 128)))
|
||||
sample = image.resize((128, 128))
|
||||
stat = ImageStat.Stat(sample)
|
||||
spread = max(stat.var)
|
||||
status = "PASS" if image.width >= 320 and image.height >= 240 and spread > 2.0 else "FAIL"
|
||||
pixels = list(sample.getdata())
|
||||
border = []
|
||||
for index in range(128):
|
||||
border.extend((sample.getpixel((index, 0)), sample.getpixel((index, 127))))
|
||||
for index in range(1, 127):
|
||||
border.extend((sample.getpixel((0, index)), sample.getpixel((127, index))))
|
||||
background = tuple(int(statistics.median(pixel[channel] for pixel in border)) for channel in range(3))
|
||||
foreground = [max(abs(pixel[channel] - background[channel]) for channel in range(3)) > 12 for pixel in pixels]
|
||||
occupancy = sum(foreground) / len(foreground)
|
||||
xs = [index % 128 for index, present in enumerate(foreground) if present]
|
||||
ys = [index // 128 for index, present in enumerate(foreground) if present]
|
||||
bbox_w = max(xs) - min(xs) + 1 if xs else 0
|
||||
bbox_h = max(ys) - min(ys) + 1 if ys else 0
|
||||
|
||||
def luminance(pixel: tuple[int, int, int]) -> float:
|
||||
return pixel[0] * 0.2126 + pixel[1] * 0.7152 + pixel[2] * 0.0722
|
||||
|
||||
transitions = 0
|
||||
comparisons = 0
|
||||
for y in range(128):
|
||||
for x in range(128):
|
||||
here = luminance(sample.getpixel((x, y)))
|
||||
if x + 1 < 128:
|
||||
transitions += abs(here - luminance(sample.getpixel((x + 1, y)))) > 45
|
||||
comparisons += 1
|
||||
if y + 1 < 128:
|
||||
transitions += abs(here - luminance(sample.getpixel((x, y + 1)))) > 45
|
||||
comparisons += 1
|
||||
edge_ratio = transitions / comparisons
|
||||
status = (
|
||||
"PASS"
|
||||
if image.width >= 320
|
||||
and image.height >= 240
|
||||
and spread > 2.0
|
||||
and 0.01 <= occupancy <= 0.95
|
||||
and bbox_w >= 24
|
||||
and bbox_h >= 24
|
||||
and edge_ratio < 0.45
|
||||
else "FAIL"
|
||||
)
|
||||
add_check(
|
||||
checks,
|
||||
f"render:{path.name}",
|
||||
status,
|
||||
f"{image.width}x{image.height}, max channel variance {spread:.2f}",
|
||||
(
|
||||
f"{image.width}x{image.height}, variance {spread:.2f}, "
|
||||
f"foreground {occupancy:.1%}, bbox {bbox_w}x{bbox_h}/128, edge ratio {edge_ratio:.3f}"
|
||||
),
|
||||
)
|
||||
except Exception as exc:
|
||||
add_check(checks, f"render:{path.name}", "FAIL", f"Image inspection failed: {exc}")
|
||||
@@ -201,16 +245,73 @@ def inspect_offline(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
add_check(checks, "offline:payload", "FAIL", f"Embedded payload decode failed: {exc}")
|
||||
|
||||
|
||||
WEB_BROWSER_CHECKS = {
|
||||
"desktop:viewport",
|
||||
"desktop:#floor-controls",
|
||||
"desktop:#category-controls",
|
||||
"desktop:#explode",
|
||||
"desktop:#section",
|
||||
"desktop:#snapshot",
|
||||
"desktop:idle-render",
|
||||
"mobile:viewport",
|
||||
"mobile:#floor-controls",
|
||||
"mobile:#category-controls",
|
||||
"mobile:#explode",
|
||||
"mobile:#section",
|
||||
"mobile:#snapshot",
|
||||
"mobile:idle-render",
|
||||
}
|
||||
OFFLINE_BROWSER_CHECKS = {
|
||||
"offline:viewport",
|
||||
"offline:model-ready",
|
||||
"offline:canvas",
|
||||
"offline:render",
|
||||
}
|
||||
|
||||
|
||||
def inspect_browser_report(path: Path, checks: list[dict[str, Any]], name: str = "browser:smoke") -> None:
|
||||
try:
|
||||
report = json.loads(path.read_text(encoding="utf-8"))
|
||||
report_checks = report.get("checks")
|
||||
if not isinstance(report_checks, list) or not report_checks:
|
||||
raise ValueError("checks must be a non-empty list")
|
||||
by_name: dict[str, dict[str, Any]] = {}
|
||||
for item in report_checks:
|
||||
if not isinstance(item, dict) or not isinstance(item.get("name"), str):
|
||||
raise ValueError("every browser check must be an object with a name")
|
||||
if item["name"] in by_name:
|
||||
raise ValueError(f"duplicate browser check: {item['name']}")
|
||||
by_name[item["name"]] = item
|
||||
required = OFFLINE_BROWSER_CHECKS if name == "offline:browser-smoke" else WEB_BROWSER_CHECKS
|
||||
missing = sorted(required - set(by_name))
|
||||
failed = sorted(item_name for item_name, item in by_name.items() if item.get("status") != "PASS")
|
||||
if missing:
|
||||
raise ValueError("missing required checks: " + ", ".join(missing))
|
||||
if failed:
|
||||
raise ValueError("non-PASS checks: " + ", ".join(failed))
|
||||
expected_viewports = (
|
||||
{"offline:viewport": "1440x900 expected 1440x900"}
|
||||
if name == "offline:browser-smoke"
|
||||
else {
|
||||
"desktop:viewport": "1440x900 expected 1440x900",
|
||||
"mobile:viewport": "390x844 expected 390x844",
|
||||
}
|
||||
)
|
||||
for check_name, expected_detail in expected_viewports.items():
|
||||
if by_name[check_name].get("detail") != expected_detail:
|
||||
raise ValueError(f"{check_name} has untrusted viewport evidence: {by_name[check_name].get('detail')!r}")
|
||||
for error_key in ("console_errors", "page_errors", "failed_requests"):
|
||||
if not isinstance(report.get(error_key), list):
|
||||
raise ValueError(f"{error_key} must be a list")
|
||||
passed = (
|
||||
report.get("status") == "PASS"
|
||||
and not report.get("console_errors")
|
||||
and not report.get("page_errors")
|
||||
and not report.get("failed_requests")
|
||||
)
|
||||
detail = report.get("detail") or json.dumps(report, ensure_ascii=False)[:500]
|
||||
detail = f"validated {len(required)}/{len(required)} required checks; " + (
|
||||
report.get("detail") or json.dumps(report, ensure_ascii=False)[:300]
|
||||
)
|
||||
add_check(checks, name, "PASS" if passed else "FAIL", detail)
|
||||
except Exception as exc:
|
||||
add_check(checks, name, "FAIL", f"Browser report invalid: {exc}")
|
||||
@@ -224,7 +325,7 @@ def inspect_audit(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
raise ValueError("audit has no gates")
|
||||
for gate in gates:
|
||||
status = str(gate.get("status", "FAIL"))
|
||||
if status not in {"PASS", "WARN", "SKIP", "FAIL"}:
|
||||
if status not in {"PASS", "WARN", "SKIP", "FAIL", "NOT_APPLICABLE"}:
|
||||
status = "FAIL"
|
||||
add_check(
|
||||
checks,
|
||||
@@ -236,6 +337,47 @@ def inspect_audit(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
add_check(checks, "audit:report", "FAIL", f"Extraction audit invalid: {exc}")
|
||||
|
||||
|
||||
def verifiable_model_artifact(path: Path) -> tuple[bool, str]:
|
||||
"""Reject scope ledgers backed only by arbitrary non-empty files."""
|
||||
try:
|
||||
suffix = path.suffix.lower()
|
||||
if suffix == ".glb":
|
||||
from pygltflib import GLTF2
|
||||
|
||||
model = GLTF2().load(str(path))
|
||||
primitives = sum(len(mesh.primitives) for mesh in model.meshes or [])
|
||||
if primitives <= 0:
|
||||
return False, "GLB has no mesh primitives"
|
||||
return True, f"GLB with {primitives} primitive(s)"
|
||||
if suffix == ".blend":
|
||||
if path.read_bytes()[:7] != b"BLENDER":
|
||||
return False, "invalid Blender header"
|
||||
return True, "Blender scene"
|
||||
if suffix == ".json":
|
||||
document = json.loads(path.read_text(encoding="utf-8"))
|
||||
showcase_schema.validate_spec(document)
|
||||
return True, f"building spec with {len(document['floors'])} floor(s)"
|
||||
return False, f"unsupported model artifact type {suffix or '<none>'}"
|
||||
except Exception as exc:
|
||||
return False, str(exc)
|
||||
|
||||
|
||||
def verifiable_site_artifact(path: Path) -> tuple[bool, str]:
|
||||
model_ok, model_detail = verifiable_model_artifact(path)
|
||||
if model_ok:
|
||||
return True, model_detail
|
||||
try:
|
||||
from PIL import Image
|
||||
|
||||
with Image.open(path) as image:
|
||||
image.verify()
|
||||
if image.width < 320 or image.height < 240:
|
||||
return False, f"site image too small: {image.width}x{image.height}"
|
||||
return True, f"site image {image.width}x{image.height}"
|
||||
except Exception:
|
||||
return False, model_detail
|
||||
|
||||
|
||||
def inspect_scope(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
"""Validate a single-building/campus scope ledger against real artifacts."""
|
||||
try:
|
||||
@@ -255,6 +397,8 @@ def inspect_scope(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
raise ValueError("requested must be a non-empty list of building IDs")
|
||||
if len(set(requested)) != len(requested):
|
||||
raise ValueError("requested contains duplicate building IDs")
|
||||
if mode == "single" and len(requested) != 1:
|
||||
raise ValueError("single mode requires exactly one requested building ID")
|
||||
if not isinstance(buildings, list):
|
||||
raise ValueError("buildings must be a list")
|
||||
by_id: dict[str, dict[str, Any]] = {}
|
||||
@@ -264,9 +408,17 @@ def inspect_scope(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
raise ValueError(f"missing or duplicate building id: {building_id!r}")
|
||||
by_id[building_id] = item
|
||||
missing = [item for item in requested if item not in by_id]
|
||||
if missing:
|
||||
unexpected = sorted(set(by_id) - set(requested))
|
||||
if missing or unexpected:
|
||||
add_check(
|
||||
checks, "scope:inventory", "FAIL", "Requested buildings missing from ledger: " + ", ".join(missing)
|
||||
checks,
|
||||
"scope:inventory",
|
||||
"FAIL",
|
||||
(
|
||||
("Requested buildings missing from ledger: " + ", ".join(missing) if missing else "")
|
||||
+ ("; " if missing and unexpected else "")
|
||||
+ ("Unrequested buildings in ledger: " + ", ".join(unexpected) if unexpected else "")
|
||||
),
|
||||
)
|
||||
else:
|
||||
add_check(
|
||||
@@ -295,19 +447,32 @@ def inspect_scope(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
add_check(checks, f"scope:building:{building_id}", "FAIL", "PASS building has no artifacts")
|
||||
continue
|
||||
missing_artifacts = []
|
||||
verified_models = []
|
||||
invalid_models = []
|
||||
for artifact in artifacts:
|
||||
artifact_path = Path(str(artifact))
|
||||
if not artifact_path.is_absolute():
|
||||
artifact_path = path.parent / artifact_path
|
||||
if not artifact_path.exists() or (artifact_path.is_file() and artifact_path.stat().st_size == 0):
|
||||
missing_artifacts.append(str(artifact))
|
||||
continue
|
||||
verified, detail = verifiable_model_artifact(artifact_path)
|
||||
if verified:
|
||||
verified_models.append(f"{artifact}: {detail}")
|
||||
else:
|
||||
invalid_models.append(f"{artifact}: {detail}")
|
||||
failed = bool(missing_artifacts or not verified_models)
|
||||
add_check(
|
||||
checks,
|
||||
f"scope:building:{building_id}",
|
||||
"FAIL" if missing_artifacts else "PASS",
|
||||
"FAIL" if failed else "PASS",
|
||||
"Missing artifacts: " + ", ".join(missing_artifacts)
|
||||
if missing_artifacts
|
||||
else f"{len(artifacts)} declared artifact(s) exist",
|
||||
else (
|
||||
f"{len(verified_models)} verifiable model artifact(s)"
|
||||
if verified_models
|
||||
else "No verifiable GLB/BLEND/building-spec artifact; rejected: " + "; ".join(invalid_models)
|
||||
),
|
||||
)
|
||||
|
||||
site = scope.get("site", {})
|
||||
@@ -321,23 +486,32 @@ def inspect_scope(path: Path, checks: list[dict[str, Any]]) -> None:
|
||||
else:
|
||||
artifacts = site.get("artifacts", [])
|
||||
missing_site = []
|
||||
verified_site = []
|
||||
for artifact in artifacts if isinstance(artifacts, list) else []:
|
||||
artifact_path = Path(str(artifact))
|
||||
if not artifact_path.is_absolute():
|
||||
artifact_path = path.parent / artifact_path
|
||||
if not artifact_path.exists() or (artifact_path.is_file() and artifact_path.stat().st_size == 0):
|
||||
missing_site.append(str(artifact))
|
||||
if not artifacts or missing_site:
|
||||
continue
|
||||
verified, detail = verifiable_site_artifact(artifact_path)
|
||||
if verified:
|
||||
verified_site.append(f"{artifact}: {detail}")
|
||||
if not artifacts or missing_site or not verified_site:
|
||||
add_check(
|
||||
checks,
|
||||
"scope:site",
|
||||
"FAIL",
|
||||
"Campus site has no artifacts"
|
||||
if not artifacts
|
||||
else "Missing site artifacts: " + ", ".join(missing_site),
|
||||
else (
|
||||
"Missing site artifacts: " + ", ".join(missing_site)
|
||||
if missing_site
|
||||
else "Campus site has no verifiable model or image artifact"
|
||||
),
|
||||
)
|
||||
else:
|
||||
add_check(checks, "scope:site", "PASS", f"{len(artifacts)} site artifact(s) exist")
|
||||
add_check(checks, "scope:site", "PASS", f"{len(verified_site)} verified site artifact(s)")
|
||||
except Exception as exc:
|
||||
add_check(checks, "scope:manifest", "FAIL", f"Scope manifest invalid: {exc}")
|
||||
|
||||
@@ -353,7 +527,9 @@ def main() -> int:
|
||||
parser.add_argument("--offline-browser-report")
|
||||
parser.add_argument("--render", nargs="*", default=[])
|
||||
parser.add_argument("--audit")
|
||||
parser.add_argument("--require-audit", action="store_true")
|
||||
parser.add_argument("--scope-manifest")
|
||||
parser.add_argument("--require-scope-manifest", action="store_true")
|
||||
parser.add_argument("--browser-report")
|
||||
parser.add_argument("--require-browser-report", action="store_true")
|
||||
parser.add_argument(
|
||||
@@ -377,11 +553,16 @@ def main() -> int:
|
||||
try:
|
||||
source_report = json.loads(Path(args.source_report).read_text(encoding="utf-8"))
|
||||
showcase_schema.validate_source_report(source_report)
|
||||
source_gate = source_report["gate"]
|
||||
source_status = str(source_gate.get("status", "FAIL"))
|
||||
add_check(
|
||||
checks,
|
||||
"cad_source_report",
|
||||
"PASS",
|
||||
f"Source gate PASS; {len(source_report['files'])} readable DXF file(s)",
|
||||
source_status,
|
||||
(
|
||||
f"Source gate {source_status}; {len(source_report['files'])} inspected DXF file(s)"
|
||||
+ (f"; partial reason: {source_gate.get('partial_reason')}" if source_status == "WARN" else "")
|
||||
),
|
||||
)
|
||||
except Exception as exc:
|
||||
add_check(checks, "cad_source_report", "FAIL", str(exc))
|
||||
@@ -440,8 +621,12 @@ def main() -> int:
|
||||
inspect_web(Path(args.web_dir), checks)
|
||||
if args.audit:
|
||||
inspect_audit(Path(args.audit), checks)
|
||||
elif args.require_audit:
|
||||
add_check(checks, "audit:report", "FAIL", "Required extraction audit was not supplied")
|
||||
if args.scope_manifest:
|
||||
inspect_scope(Path(args.scope_manifest), checks)
|
||||
elif args.require_scope_manifest:
|
||||
add_check(checks, "scope:manifest", "FAIL", "Required scope manifest was not supplied")
|
||||
if args.offline_viewer:
|
||||
inspect_offline(Path(args.offline_viewer), checks)
|
||||
if args.offline_browser_report:
|
||||
|
||||
Reference in New Issue
Block a user