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

@@ -1144,6 +1144,90 @@ def main() -> int:
check("two separate footprint hulls", len(polys) == 2, f"n={len(polys)} spans={spans}")
check("no hull bridges the void", all(s < 25.0 for s in spans), f"spans={spans}")
# --- leak gates: frame ledger — every detected frame in config OR explicitly discarded ---
fl_frames = WORK / "frames_ledger_probe.json"
proc = run([
sys.executable, "scripts/detect_sheet_frames.py",
"--dxf", str(mf_path), "--out-config", str(fl_frames),
"--out-report", str(WORK / "frames_ledger_probe.md"), "--vendor-dir", str(VENDOR),
])
check("ledger probe detect runs", proc.returncode == 0, proc.stderr[-300:])
fl_detected = json.loads(fl_frames.read_text(encoding="utf-8"))["drawings"][0]["floors"]
fl_f1 = next(f for f in fl_detected if f["name"] == "一层平面图")
def ledger_audit(tag: str, extra: dict) -> tuple[int, str]:
cfg = {
"scale": 0.001,
"drawings": [{"file": "multiframe.dxf", "floors": [
{"key": "F1", "name": "一层平面图", "z": 0.0, "height": 4.2,
"clip": fl_f1["clip"], "offset": fl_f1["offset"]},
]}],
**extra,
}
cfg_path = WORK / f"ledger_cfg_{tag}.json"
cfg_path.write_text(json.dumps(cfg, ensure_ascii=False), encoding="utf-8")
spec_path = WORK / f"ledger_spec_{tag}.json"
run([sys.executable, "scripts/derive_showcase_spec.py",
"--dxf-dir", str(dxf_dir), "--config", str(cfg_path),
"--out", str(spec_path), "--vendor-dir", str(VENDOR)])
proc = run([sys.executable, "scripts/audit_showcase_extraction.py",
"--dxf-dir", str(dxf_dir), "--config", str(cfg_path),
"--spec", str(spec_path), "--out", str(WORK / f"ledger_audit_{tag}"),
"--vendor-dir", str(VENDOR), "--frames", str(fl_frames)])
body = (WORK / f"ledger_audit_{tag}" / "extraction_audit.json").read_text(encoding="utf-8")
return proc.returncode, body
rc_miss, md_miss = ledger_audit("miss", {})
check("unaccounted frame FAILs ledger", rc_miss != 0 and "frame_ledger" in md_miss and "二层平面图" in md_miss,
md_miss[-400:] if rc_miss == 0 else "")
rc_ok, md_ok = ledger_audit("ok", {"discarded_frames": [{"name": "二层平面图", "reason": "测试弃用"}]})
check("explicit discard PASSes ledger", rc_ok == 0, md_ok[-400:] if rc_ok != 0 else "")
# --- leak gates: declared areas + storey count cross-checks (twin-tower scenario) ---
tt_spec_data = json.loads((WORK / "tt_spec.json").read_text(encoding="utf-8"))
tt_area = sum(float(f["footprint"].get("area", 0.0)) for f in tt_spec_data["floors"].values())
def tt_audit(tag: str, extra: dict) -> tuple[int, str]:
cfg = dict(tt_config, **extra)
cfg_path = WORK / f"tt_cfg_{tag}.json"
cfg_path.write_text(json.dumps(cfg, ensure_ascii=False), encoding="utf-8")
proc = run([sys.executable, "scripts/audit_showcase_extraction.py",
"--dxf-dir", str(tt_dir), "--config", str(cfg_path),
"--spec", str(WORK / "tt_spec.json"), "--out", str(WORK / f"tt_audit_{tag}"),
"--vendor-dir", str(VENDOR)])
body = (WORK / f"tt_audit_{tag}" / "extraction_audit.json").read_text(encoding="utf-8")
return proc.returncode, body
rc, body = tt_audit("good", {"declared_areas_m2": {"F4": round(tt_area)},
"expected_storeys_above_ground": 1})
check("area+storey cross-checks PASS on truth", rc == 0, body[-400:] if rc != 0 else "")
rc, body = tt_audit("badarea", {"declared_areas_m2": {"F4": round(tt_area * 2)}})
check("declared-area mismatch FAILs", rc != 0 and "area_declared" in body, body[-400:] if rc == 0 else "")
rc, body = tt_audit("badcount", {"expected_storeys_above_ground": 5})
check("storey-count mismatch FAILs", rc != 0 and "storey_count" in body, body[-400:] if rc == 0 else "")
# --- leak warning: unfamiliar layer dialect must be called out ---
dial_dir = WORK / "dxf_dialect"
dial_dir.mkdir(parents=True, exist_ok=True)
dial_doc = ezdxf.new("R2010")
for layer in ("XQTB-QT", "PUB_TEXT"):
dial_doc.layers.add(layer)
dial_msp = dial_doc.modelspace()
for i in range(500):
dial_msp.add_line((i * 100, 0), (i * 100, 4000), dxfattribs={"layer": "XQTB-QT"})
dial_msp.add_text("一层平面图", dxfattribs={"layer": "PUB_TEXT", "height": 800}).set_placement((20000, -5000))
dial_doc.saveas(dial_dir / "dialect.dxf")
proc = run([
sys.executable, "scripts/cad_dxf_report.py",
"--input", str(dial_dir), "--work", str(WORK / "dialect_work"),
"--output", str(WORK / "dialect_report"), "--vendor-dir", str(VENDOR),
])
check("dialect report runs", proc.returncode == 0, proc.stderr[-400:])
if proc.returncode == 0:
drep = json.loads((WORK / "dialect_report" / "cad_read_report.json").read_text(encoding="utf-8"))
dw = " ".join({i["file"]: i for i in drep["files"]}.get("dialect.dxf", {}).get("warnings", []))
check("unknown layer dialect warned", "unknown_layer_dialect" in dw, dw)
# --- floor_order gaps must warn loudly, not silently drop floors (13号 b2: F11-F16 消失) ---
fo_config = {
"scale": 0.001,