fix: close audited CAD delivery gate gaps

This commit is contained in:
2026-07-10 15:58:51 +08:00
parent a761175c09
commit 6c1336fa59
15 changed files with 713 additions and 124 deletions

View File

@@ -7,6 +7,7 @@ import argparse
import hashlib
import importlib.metadata
import json
import os
import shutil
import socket
import subprocess
@@ -30,16 +31,31 @@ def now() -> str:
return datetime.now(timezone.utc).isoformat()
def fingerprint(config: Path, source_report: Path, dxf_dir: Path) -> str:
DERIVE_FORBIDDEN_WARNINGS = (
"WARNING isolated_cluster",
"WARNING unsupported_roof",
"WARNING floor_order",
"WARNING topless_group",
)
def fingerprint(
config: Path,
source_report: Path,
dxf_dir: Path,
extra_inputs: tuple[Path, ...] = (),
) -> str:
"""Hash source data and pipeline code so resume cannot reuse stale work."""
digest = hashlib.sha256()
tracked = [config, source_report, *sorted(dxf_dir.glob("*.dxf"))]
tracked = [config, source_report, *extra_inputs, *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(sorted(path for path in (ROOT / "tools").rglob("*") if path.is_file()))
tracked.extend(sorted(path for path in (ROOT / "assets" / "web-showcase-template").rglob("*") if path.is_file()))
tracked.extend(sorted(path for path in (ROOT / "assets" / "offline-viewer-libs").rglob("*") if path.is_file()))
tracked.extend(path for path in (ROOT / "package.json", ROOT / "package-lock.json") if path.exists())
for path in tracked:
for path in sorted(set(tracked), key=lambda item: str(item)):
if not path.is_file():
continue
digest.update(str(path.relative_to(ROOT) if path.is_relative_to(ROOT) else path).encode("utf-8"))
@@ -64,11 +80,22 @@ class Pipeline:
if state_path.exists():
try:
current = json.loads(state_path.read_text(encoding="utf-8"))
except Exception:
current = {}
except Exception as exc:
raise SystemExit(f"Pipeline state is corrupt and was not overwritten: {state_path}: {exc}") from exc
else:
current = {}
if current.get("input_fingerprint") != input_fingerprint:
if resume and current:
print(
json.dumps(
{
"status": "INVALIDATED",
"reason": "input fingerprint changed",
"state": str(state_path),
},
ensure_ascii=False,
)
)
current = {}
self.state: dict[str, Any] = {
"state_version": 1,
@@ -82,7 +109,15 @@ class Pipeline:
def save(self) -> None:
self.state["updated_at"] = now()
self.path.parent.mkdir(parents=True, exist_ok=True)
self.path.write_text(json.dumps(self.state, ensure_ascii=False, indent=2), encoding="utf-8")
temporary = self.path.with_name(f".{self.path.name}.{os.getpid()}.tmp")
try:
with temporary.open("w", encoding="utf-8") as stream:
json.dump(self.state, stream, ensure_ascii=False, indent=2)
stream.flush()
os.fsync(stream.fileno())
os.replace(temporary, self.path)
finally:
temporary.unlink(missing_ok=True)
def run(
self,
@@ -137,6 +172,26 @@ def python_script(name: str, *args: str) -> list[str]:
return [sys.executable, str(SCRIPTS / name), *args]
def resolve_executable(name: str) -> str:
"""Resolve command shims portably (notably npm.cmd on Windows)."""
candidates = [name]
if sys.platform == "win32":
candidates = [f"{name}.cmd", f"{name}.exe", name]
for candidate in candidates:
found = shutil.which(candidate)
if found:
return found
raise SystemExit(f"Required executable not found on PATH: {name}")
def npm_command(*args: str) -> list[str]:
return [resolve_executable("npm"), *args]
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:
@@ -157,7 +212,7 @@ def enforce_audit_gates(pipeline: Pipeline, audit_path: Path, waivers: dict[str,
waived = []
for gate in report.get("gates", []):
status = str(gate.get("status", "FAIL"))
if status == "PASS":
if status in {"PASS", "NOT_APPLICABLE"}:
continue
name = f"audit:{gate.get('name', 'unnamed')}"
if status in {"WARN", "SKIP"} and name in waivers:
@@ -196,21 +251,25 @@ def wait_for_http(url: str, timeout: float = 20.0) -> None:
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."""
def ensure_node_runtime(pipeline: Pipeline, *, browser: bool) -> None:
pipeline.run(
"node_dependencies",
["npm", "install", "--prefix", str(ROOT), "--ignore-scripts"],
npm_command("install", "--prefix", str(ROOT), "--ignore-scripts"),
[
ROOT / "node_modules" / "playwright" / "package.json",
ROOT / "node_modules" / "@gltf-transform" / "core" / "package.json",
],
)
pipeline.run(
"browser_runtime",
[str(ROOT / "node_modules" / ".bin" / "playwright"), "install", "chromium"],
[],
)
if browser:
pipeline.run(
"browser_runtime",
node_script(ROOT / "node_modules" / "playwright" / "cli.js", "install", "chromium"),
[],
)
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."""
report.parent.mkdir(parents=True, exist_ok=True)
port = available_port()
url = f"http://127.0.0.1:{port}/"
@@ -233,16 +292,15 @@ def run_browser_smoke(pipeline: Pipeline, web_dir: Path, report: Path, screensho
wait_for_http(url)
pipeline.run(
"browser_smoke",
[
"node",
str(SCRIPTS / "web_smoke_test.mjs"),
node_script(
SCRIPTS / "web_smoke_test.mjs",
"--url",
url,
"--out",
str(report),
"--screenshots-dir",
str(screenshots),
],
),
[report, screenshots],
)
finally:
@@ -258,16 +316,15 @@ def run_offline_browser_smoke(pipeline: Pipeline, viewer: Path, report: Path, sc
"""Open the single-file viewer through file:// and prove that its model renders."""
pipeline.run(
"offline_browser_smoke",
[
"node",
str(SCRIPTS / "offline_viewer_smoke.mjs"),
node_script(
SCRIPTS / "offline_viewer_smoke.mjs",
"--viewer",
str(viewer),
"--out",
str(report),
"--screenshot",
str(screenshot),
],
),
[report, screenshot],
)
@@ -284,7 +341,10 @@ def doctor() -> int:
checks.append({"name": module, "status": "FAIL", "detail": str(exc)})
failed = True
for executable in ("node", "npm", "dwg2dxf"):
path = shutil.which(executable)
try:
path = resolve_executable(executable)
except SystemExit:
path = None
checks.append({"name": executable, "status": "PASS" if path else "OPTIONAL", "path": path})
for executable, resolver in (("ODAFileConverter", oda_converter), ("blender", default_blender)):
candidate = resolver()
@@ -308,6 +368,10 @@ def inspect_command(args: argparse.Namespace) -> int:
"--converter",
args.converter,
)
if args.allow_partial:
command.extend(["--allow-partial", "--partial-reason", args.partial_reason])
if args.no_convert:
command.append("--no-convert")
return subprocess.run(command, cwd=ROOT).returncode
@@ -337,7 +401,7 @@ def build_with_trimesh(pipeline: Pipeline, bundle: Path) -> list[Path]:
)
pipeline.run(
f"compress_{key}",
["node", str(ROOT / "tools" / "compress.mjs"), str(raw_glb), str(glb)],
node_script(ROOT / "tools" / "compress.mjs", str(raw_glb), str(glb)),
[glb],
)
glbs.append(glb)
@@ -348,20 +412,40 @@ def run_pipeline(args: argparse.Namespace) -> int:
dxf_dir = Path(args.dxf_dir).resolve()
config = Path(args.config).resolve()
source_report = Path(args.source_report).resolve()
scope_manifest = Path(args.scope_manifest).resolve()
frame_inputs = tuple(Path(path).resolve() for path in args.frames)
try:
waivers = parse_waivers(args.waiver)
except ValueError as exc:
print(f"GATE FAIL [waiver]: {exc}", file=sys.stderr)
return 3
try:
report = json.loads(source_report.read_text(encoding="utf-8"))
showcase_schema.validate_source_report(report, dxf_dir)
except (OSError, json.JSONDecodeError, showcase_schema.ValidationFailure) as exc:
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:
print(
"GATE BLOCKED [cad_source_report]: partial source report requires "
"--waiver 'cad_source_report=<user-approved reason>'",
file=sys.stderr,
)
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)
pipeline = Pipeline(
out_dir / "pipeline_state.json",
fingerprint(config, source_report, dxf_dir, (scope_manifest, *frame_inputs)),
args.resume,
)
if source_status == "WARN":
pipeline.state["source_waiver"] = {
"check": "cad_source_report",
"reason": waivers["cad_source_report"],
}
pipeline.save()
spec = out_dir / "building_spec.json"
audit = out_dir / "extraction_audit.json"
audit_md = out_dir / "extraction_audit.md"
@@ -380,7 +464,7 @@ def run_pipeline(args: argparse.Namespace) -> int:
args.vendor_dir,
),
[spec],
forbidden_stderr=("WARNING topless_group",),
forbidden_stderr=DERIVE_FORBIDDEN_WARNINGS,
)
audit_cmd = python_script(
"audit_showcase_extraction.py",
@@ -434,15 +518,9 @@ def run_pipeline(args: argparse.Namespace) -> int:
if args.blender:
variant_cmd.extend(["--blender", args.blender])
pipeline.run("variants", variant_cmd, [manifest, bundle / "variant_specs" / "standard.json"])
if args.backend == "trimesh" or args.web or args.offline:
ensure_node_runtime(pipeline, browser=args.web or args.offline)
if args.backend == "trimesh":
pipeline.run(
"node_dependencies",
["npm", "install", "--prefix", str(ROOT), "--ignore-scripts"],
[
ROOT / "node_modules" / "playwright" / "package.json",
ROOT / "node_modules" / "@gltf-transform" / "core" / "package.json",
],
)
glbs = build_with_trimesh(pipeline, bundle)
else:
glbs = [bundle / "variants" / key / f"{key}.glb" for key in ("overview", "standard", "detail")]
@@ -465,7 +543,7 @@ def run_pipeline(args: argparse.Namespace) -> int:
if not args.skip_web_deps:
pipeline.run(
"web_dependencies",
["npm", "install", "--prefix", str(web_dir), "--ignore-scripts"],
npm_command("install", "--prefix", str(web_dir), "--ignore-scripts"),
[web_dir / "node_modules" / "three" / "build" / "three.module.js"],
)
browser_report = (
@@ -495,17 +573,6 @@ def run_pipeline(args: argparse.Namespace) -> int:
),
[offline],
)
if not args.web:
pipeline.run(
"offline_node_dependencies",
["npm", "install", "--prefix", str(ROOT), "--ignore-scripts"],
[ROOT / "node_modules" / "playwright" / "package.json"],
)
pipeline.run(
"offline_browser_runtime",
[str(ROOT / "node_modules" / ".bin" / "playwright"), "install", "chromium"],
[],
)
run_offline_browser_smoke(
pipeline,
offline,
@@ -543,10 +610,14 @@ def run_pipeline(args: argparse.Namespace) -> int:
str(preview),
"--audit",
str(audit),
"--require-audit",
"--budget-mb",
str(args.budget_mb),
"--evidence-out",
str(evidence),
"--scope-manifest",
str(scope_manifest),
"--require-scope-manifest",
)
if args.web:
verify_cmd.extend(
@@ -563,8 +634,6 @@ def run_pipeline(args: argparse.Namespace) -> int:
)
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])
@@ -591,6 +660,9 @@ def parser() -> argparse.ArgumentParser:
inspect.add_argument("--output", required=True)
inspect.add_argument("--vendor-dir", default="work/vendor")
inspect.add_argument("--converter", choices=("auto", "libredwg", "oda"), default="auto")
inspect.add_argument("--no-convert", action="store_true")
inspect.add_argument("--allow-partial", action="store_true")
inspect.add_argument("--partial-reason", default="")
run = sub.add_parser("run", help="Run extract -> audit -> overlays -> variants -> verify from an approved config")
run.add_argument("--dxf-dir", required=True)
run.add_argument("--config", required=True)