diff --git a/SKILL.md b/SKILL.md index 5eaa1b2..9e32322 100644 --- a/SKILL.md +++ b/SKILL.md @@ -55,6 +55,7 @@ The pipeline is designed so a first pass either succeeds or STOPS at a loud gate 1. Read report clean: every `Warnings` entry resolved. `multi_frame_sheet` -> ran `detect_sheet_frames.py` and used its clip/offset config; `cached_xref_content` -> the XREF content is CACHED as in-file block definitions, expand blocks (≈bind) instead of asking for a re-export; `likely_xref_shell` (no cached content) -> user re-exported with XREFs bound. 2. Frame alignment proven: detect report shows `grid-aligned, votes x>=3 y>=3` per floor (any `WEAK grid match` floor was verified on its overlay SVG). 3. Audit exit code 0, and gates `floors_aligned` / `glazing_captured` / `no_annotation_layers_classified` all PASS. `floors_aligned` also cross-checks every floor's wall profile against the group's reference floor (a strong correlation peak at a non-zero shift = "self-consistent but misplaced", the 13号 F3 错槽 pattern that envelope centers cannot see). Never pass `--no-strict-gates` to proceed. + Leak gates (SKIP unless armed — arm them on every new project, they are the cross-evidence net): `--frames ` arms `frame_ledger` (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); config `declared_areas_m2` arms `area_declared` (model footprint area vs 经济技术指标表/标注, ±15% default — 1#B lost half the building and only the site plan showed it); config `expected_storeys_above_ground` arms `storey_count` (vs 总图 `22F` labels / elevation ladder). 4. No `WARNING topless_group` from derive (roof handled via caps or `roof_specs`), and no `WARNING mesh_from_polygons` from the GLB build (else slabs/roofs silently vanished — install mapbox_earcut). 5. Visual check done by the Agent, not assumed: `verify_showcase.py` previews plus one rendered iso/plan image actually LOOKED AT (floors stacked, roof present, no slab fins beyond facade — default `cap_buffer` 1.3 is for colonnades, use ~0.1 otherwise; and check the AIR between separate masses in the front/side views — bridged footprint hulls lay slab plates between twin towers, invisible in top views and to every gate). 6. Campus placement (if 总图/定位图 exists): building envelope dims match the site rect 1:1 (e.g. 面积核算 frame), rotation taken from the rect edge angle, 180° flip disambiguated by an asymmetric feature (machine rooms / basement / entrance), and a top-view overlay image confirms the building inside its site rect. Neighbour gaps measured numerically: no overlaps, 贴建 joints 0.15-0.3 m with corroborating evidence, buildings inside 红线. @@ -76,6 +77,7 @@ The pipeline is designed so a first pass either succeeds or STOPS at a loud gate - Install missing parser dependencies into the current project work area, not globally: `python3 -m pip install --target work/vendor ezdxf` - Inspect the Markdown/JSON report before modeling. Confirm drawings, units, bounding boxes, and the top layers for walls/glazing/roof/columns/beams/railings/doors/detail lines/floor lines/grid. + - `unknown_layer_dialect` warning = a plan file whose layers match no known wall naming (*WALL*/*墙*): a design-institute convention this pipeline has never seen. Build the layer mapping consciously from the top-layers list before modeling — patterns age, and a wrong mapping extracts nothing silently. - Resolve the report's `Warnings` section before modeling. `cached_xref_content` means the "empty" sheet's INSERT stubs reference block definitions cached INSIDE the file (AutoCAD keeps unbound-XREF content as blocks; both libredwg and ODA export the cache) — expand blocks in detect/derive (`expand_blocks` ≈ bind), no re-export needed. Only `likely_xref_shell` / `near_empty` WITHOUT cached content means geometry truly lives outside: ask the user to bind/insert the XREFs in CAD and re-export that DXF (do not model the shell and report the floor as done). 3. Build the extraction config and `building_spec.json`. diff --git a/references/generalization_test_log.md b/references/generalization_test_log.md index 15357de..22d353f 100644 --- a/references/generalization_test_log.md +++ b/references/generalization_test_log.md @@ -318,6 +318,18 @@ glazing 在 walls 之前匹配。3.0m×22 层梯(0..66,机房顶69)。 层高建议(27号 2# 实测:19 层枚举图名自动展开、step 3.0×11 建议命中; 建议只是证据不自动填 z——楼梯剖面的半层节拍是反例)。回归 +11 项。 +23. **查漏门禁四件套**("新项目会不会漏"的正面回应——全部是账目/互证型 + 不变量,对没见过的方言天然有效): + - `frame_ledger`(audit --frames):检出框必须入 config 或列入 + `discarded_frames` 带理由——静默弃用的框正是整翼消失的所在; + - `area_declared`(config `declared_areas_m2`):模型 footprint 面积对 + 经济技术指标表/标注互证(±15% 默认)——1#B 半栋消失只有总图能暴露; + - `storey_count`(config `expected_storeys_above_ground`):地上层数对 + 总图 22F 标签/标高梯级数互证; + - `unknown_layer_dialect`(读图报告):平面文件无任何 *WALL*/*墙* 类图层 + 时点名"陌生方言,先自觉建映射"。 + 三门禁未布防时 SKIP(既有项目零影响),新项目应全部布防。回归 +8 项。 + **仍留档**:⑦ 未命名详图簇(核心筒大样)仍靠 clip 带宽人工剔除,标题纯度 只覆盖有名字的辅助框(需要"详图簇 vs 楼层框"的几何判别设计);2# 的 "十九、二十一层平面图"修改单簇(无障碍改造相关)未并入模型(需用户确认 diff --git a/scripts/audit_showcase_extraction.py b/scripts/audit_showcase_extraction.py index 831c548..386f81b 100755 --- a/scripts/audit_showcase_extraction.py +++ b/scripts/audit_showcase_extraction.py @@ -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 (mezzanines FM, 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 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) diff --git a/scripts/cad_dxf_report.py b/scripts/cad_dxf_report.py index 1b40401..70838cb 100755 --- a/scripts/cad_dxf_report.py +++ b/scripts/cad_dxf_report.py @@ -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: diff --git a/tests/test_extraction_pipeline.py b/tests/test_extraction_pipeline.py index 3cd5e34..761001b 100644 --- a/tests/test_extraction_pipeline.py +++ b/tests/test_extraction_pipeline.py @@ -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,