Leak gates: cross-evidence nets for unseen-project conventions

Mechanical pattern tools WILL miss new dialects; these gates are
ledger/cross-evidence INVARIANTS that hold for conventions never seen:

- frame_ledger (audit --frames <detect out-config>): every detected plan
  frame must be used by the config or listed in discarded_frames with a
  reason — silently ignored frames are where wings vanish
- area_declared (config declared_areas_m2): model footprint area vs
  经济技术指标表 within ±15% — 1#B lost half the building and only the
  site plan exposed it
- storey_count (config expected_storeys_above_ground): above-ground F<n>
  count vs 总图 labels / elevation ladder
- unknown_layer_dialect (read report): plan file with no *WALL*/*墙* layer
  = unfamiliar institute convention, map layers consciously

All three gates SKIP when unarmed (zero impact on existing projects);
regression suite +8 checks, all green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-03 09:04:58 +08:00
parent fc05464749
commit ab5e72daae
5 changed files with 257 additions and 4 deletions

View File

@@ -43,6 +43,14 @@ def parse_args() -> argparse.Namespace:
action="store_true",
help="Report gate failures without a nonzero exit code",
)
parser.add_argument(
"--frames",
nargs="*",
default=None,
help="detect_sheet_frames out-config JSON(s): enables the frame-ledger gate "
"(every detected plan frame must be in the extraction config or listed "
"in config discarded_frames with a reason)",
)
return parser.parse_args()
@@ -167,7 +175,8 @@ def spec_counts(spec: dict[str, Any]) -> dict[str, dict[str, int]]:
return result
def audit(dxf_dir: Path, config: dict[str, Any], spec: dict[str, Any], vendor_dir: Path) -> dict[str, Any]:
def audit(dxf_dir: Path, config: dict[str, Any], spec: dict[str, Any], vendor_dir: Path,
frames: list[dict[str, Any]] | None = None) -> dict[str, Any]:
ezdxf, recover = derive.import_ezdxf(vendor_dir)
patterns = derive.layer_patterns(config)
max_chord = float(config.get("curve_chord_source_length", 750.0))
@@ -358,7 +367,7 @@ def audit(dxf_dir: Path, config: dict[str, Any], spec: dict[str, Any], vendor_di
"top_layers": dict(global_layer_counts.most_common(50)),
"agent_questions": agent_questions(unclassified_layers, global_high_risk, global_insert_names),
}
report["gates"] = evaluate_gates(report, spec, config)
report["gates"] = evaluate_gates(report, spec, config, frames)
return report
@@ -466,7 +475,8 @@ def floors_alignment_gate(spec: dict[str, Any], config: dict[str, Any]) -> dict[
}
def evaluate_gates(report: dict[str, Any], spec: dict[str, Any], config: dict[str, Any]) -> list[dict[str, Any]]:
def evaluate_gates(report: dict[str, Any], spec: dict[str, Any], config: dict[str, Any],
frames: list[dict[str, Any]] | None = None) -> list[dict[str, Any]]:
"""Hard checks that must pass before 3D generation.
A gate FAIL means the extraction is known-incomplete in a way that high
@@ -549,9 +559,135 @@ def evaluate_gates(report: dict[str, Any], spec: dict[str, Any], config: dict[st
),
}
)
gates.append(frame_ledger_gate(config, frames))
gates.append(declared_area_gate(spec, config))
gates.append(storey_count_gate(spec, config))
return gates
def _rect_overlap_ratio(a: list[float], b: list[float]) -> float:
"""Intersection area / area of rect a (a = the detected frame clip)."""
ax0, ay0, ax1, ay1 = min(a[0], a[2]), min(a[1], a[3]), max(a[0], a[2]), max(a[1], a[3])
bx0, by0, bx1, by1 = min(b[0], b[2]), min(b[1], b[3]), max(b[0], b[2]), max(b[1], b[3])
iw = min(ax1, bx1) - max(ax0, bx0)
ih = min(ay1, by1) - max(ay0, by0)
if iw <= 0 or ih <= 0:
return 0.0
area_a = max((ax1 - ax0) * (ay1 - ay0), 1e-9)
return (iw * ih) / area_a
def frame_ledger_gate(config: dict[str, Any], frames: list[dict[str, Any]] | None) -> dict[str, Any]:
"""Every DETECTED plan frame must be in the config or explicitly discarded.
Frames the Agent silently ignores are exactly where whole floors (or whole
wings) go missing on a new project. Pass detect_sheet_frames' out-config
via --frames; account for unused frames in config
`"discarded_frames": [{"name": ..., "reason": ...}]`.
"""
if not frames:
return {"name": "frame_ledger", "status": "SKIP",
"detail": "No --frames given; frame ledger not checked."}
discarded = {str(d.get("name", "")) for d in config.get("discarded_frames", [])}
cfg_by_file: dict[str, list[dict[str, Any]]] = {}
for drawing in config.get("drawings", []) or []:
cfg_by_file.setdefault(str(drawing.get("file", "")), []).extend(drawing.get("floors", []) or [])
unaccounted: list[str] = []
total = 0
for fcfg in frames:
for drawing in fcfg.get("drawings", []) or []:
file = str(drawing.get("file", ""))
cfg_floors = cfg_by_file.get(file, [])
for frame in drawing.get("floors", []) or []:
total += 1
name = str(frame.get("name", ""))
clip = frame.get("clip")
if name in discarded:
continue
if clip and any(
fl.get("clip") and _rect_overlap_ratio(clip, fl["clip"]) >= 0.3
for fl in cfg_floors
):
continue
unaccounted.append(f"{file}: {frame.get('key', '?')} {name}")
if unaccounted:
shown = "; ".join(unaccounted[:12]) + (" …" if len(unaccounted) > 12 else "")
return {
"name": "frame_ledger",
"status": "FAIL",
"detail": (
f"{len(unaccounted)}/{total} detected plan frames are neither used by the config "
f"nor listed in discarded_frames: {shown}. Silently ignored frames are where "
"floors/wings vanish — use them or discard them WITH a reason."
),
}
return {"name": "frame_ledger", "status": "PASS",
"detail": f"All {total} detected plan frames are used or explicitly discarded."}
def declared_area_gate(spec: dict[str, Any], config: dict[str, Any]) -> dict[str, Any]:
"""Model footprint areas must match declared areas (经济技术指标表/标注).
Cross-evidence beats self-consistency: 1#B passed every internal gate with
half the building missing — the site plan exposed it. Config:
`"declared_areas_m2": {"F1": 812, ...}`, optional
`"declared_area_tolerance"` (default 0.15).
"""
declared = config.get("declared_areas_m2")
if not declared:
return {"name": "area_declared", "status": "SKIP",
"detail": "No declared_areas_m2 in config; area cross-check not requested."}
tol = float(config.get("declared_area_tolerance", 0.15))
problems = []
for key, want in declared.items():
want = float(want)
floor = spec.get("floors", {}).get(str(key))
if floor is None:
problems.append(f"{key}: not in spec")
continue
got = float((floor.get("footprint") or {}).get("area") or 0.0)
if got <= 0:
problems.append(f"{key}: no footprint area in spec")
elif abs(got - want) / want > tol:
problems.append(f"{key}: model {round(got, 1)} m² vs declared {round(want, 1)} m² "
f"({round(100 * (got - want) / want):+d}%)")
if problems:
return {
"name": "area_declared",
"status": "FAIL",
"detail": ("Footprint area disagrees with the declared evidence: "
+ "; ".join(problems)
+ f" (tolerance ±{round(tol * 100)}%). A missing wing / swallowed neighbour "
"sheet moves the area far more than balconies do."),
}
return {"name": "area_declared", "status": "PASS",
"detail": f"{len(declared)} floor area(s) within ±{round(tol * 100)}% of declared values."}
def storey_count_gate(spec: dict[str, Any], config: dict[str, Any]) -> dict[str, Any]:
"""Above-ground storey count must match the declared count (总图 22F 标签 / 标高梯级数).
Config: `"expected_storeys_above_ground": 22`. Counts spec floors keyed
F<n> (mezzanines F<n>M, PH, RF, basements excluded).
"""
expected = config.get("expected_storeys_above_ground")
if expected is None:
return {"name": "storey_count", "status": "SKIP",
"detail": "No expected_storeys_above_ground in config; count cross-check not requested."}
import re as _re
actual = sum(1 for k in spec.get("floors", {}) if _re.fullmatch(r"F\d+", str(k)))
if actual != int(expected):
return {
"name": "storey_count",
"status": "FAIL",
"detail": (f"Spec has {actual} above-ground storeys (F<n> keys) but the declared "
f"count is {expected} (总图 label / elevation ladder). A silently dropped "
"floor_order entry or an unexpanded multi-floor frame is the usual cause."),
}
return {"name": "storey_count", "status": "PASS",
"detail": f"{actual} above-ground storeys match the declared count."}
def agent_questions(unclassified_layers: list[dict], high_risk: Counter, insert_names: Counter) -> list[str]:
questions = []
if unclassified_layers:
@@ -645,7 +781,10 @@ def main() -> int:
args = parse_args()
config = derive.read_json(Path(args.config))
spec = json.loads(Path(args.spec).read_text(encoding="utf-8"))
report = audit(Path(args.dxf_dir), config, spec, Path(args.vendor_dir))
frames = None
if args.frames:
frames = [json.loads(Path(p).read_text(encoding="utf-8")) for p in args.frames]
report = audit(Path(args.dxf_dir), config, spec, Path(args.vendor_dir), frames)
json_path, md_path = output_paths(Path(args.out))
json_path.parent.mkdir(parents=True, exist_ok=True)
md_path.parent.mkdir(parents=True, exist_ok=True)

View File

@@ -4,6 +4,7 @@
from __future__ import annotations
import argparse
import fnmatch
import json
import os
import shutil
@@ -313,6 +314,21 @@ def file_warnings(item: dict) -> list[str]:
"This file holds a whole sheet set — never model it as a single floor and never stack frames "
"without per-floor offsets. Run scripts/detect_sheet_frames.py to generate clip/offset config."
)
# Unfamiliar layer dialect: a plan file whose layers match NO known wall
# naming (*WALL*/*墙*/QIANG) means a design institute convention this
# pipeline has never seen — the layer mapping must be built consciously,
# or extraction silently captures nothing (patterns age, invariants don't).
wall_dialects = ("*WALL*", "*墙*", "*QIANG*")
layer_names = [l["name"] for l in item.get("layers", []) if l.get("entity_count", 0) > 0]
if (plan_titles >= 1 and (entities or 0) >= 400
and layer_names
and not any(fnmatch.fnmatch(n.upper(), p.upper()) for n in layer_names for p in wall_dialects)):
tops = ", ".join(l["name"] for l in item.get("top_layers", [])[:8])
warnings.append(
f"unknown_layer_dialect: {plan_titles} plan title(s) but no wall-like layer "
f"(*WALL*/*墙*) among {len(layer_names)} populated layers — an unfamiliar design-institute "
f"convention. Map layers consciously before modeling (top layers: {tops})."
)
cached = item.get("insert_block_content") or []
cached_total = sum(b["entities"] for b in cached)
if entities < NEAR_EMPTY_ENTITY_THRESHOLD and cached_total >= 200: