解决三个缺口:① 放大没复用标定/瓦片数学(LLM 一次 zoom 只出一张,宽区域仍读不清);
②「看清没」由 VLM 自评(易过度自信/幻觉);③ 只能按 handle / 手算 bbox 指认,
图上看到但没 handle 的东西指认不了。
核心思路:把「放大」升级成一个语义化工具——LLM 只指认「要看哪里」,由 agent 复用
tiling.py 的标定数学算出该切几张、每张覆盖多大,用构造保证可读。
新增 dwg_review/inspect.py(agent 侧,不编 DLL):
- TOOL_SCHEMA:inspect_region(target, purpose);target 三形态 {handle} / {window} /
{label,cx,cy,span},purpose = text(8px) / line(6px)。
- InspectState:单图框内「截图标签 → 世界可见范围」登记表,支持逐级下钻
(返回的 inspect-N 可继续作为 label 指认)。
- resolve_target / _point_box:label+cx+cy → WCS。cy 是图像坐标(上→下),
世界 y 必须翻转(y = y2 - cy*H)——单测专门钉住。
- plan_tiles:复用 world_per_px + tiles_for_box + subdivide;超 INSPECT_MAX_TILES(4)
则硬失败并给出「缩小 target / 分多次请求」的指引(不静默降级);拿不到比例/像素
则退回单张并如实告知。
关键几何:CAD 的 _.ZOOM _W 是等比适配(不拉伸),故「截图上的归一化坐标」必须按
max(W/ew, H/eh) 反算真实可见范围(新增 tiling.visible_box)——直接用送进去的窗口会偏。
zoom_to_window 返回的 view 是「加过 margin 的窗口」,不是可见范围,不能混用。
证据层配套:evidence_model.make_image(..., window=) 记录每张图的可见世界范围;
capture_images 用 view_px(overall 图原始像素)+ visible_box 逐张登记。
子循环接入:_run_readonly_tool 在只读白名单校验之前拦截 inspect_region(它不在
READ_ONLY_TOOLS 里,内部只调白名单内的只读 MCP 工具);_review_tools 启用时用
inspect_region 取代三个裸视口工具;inspect_region 计入 _VIEW_TOOLS → 结束复位视口。
配置:INSPECT_ENABLED(True) / INSPECT_MAX_TILES(4)。
端到端实测(真实图纸 1 图框):取证与审查两阶段均在最大化态、结束精确还原(4 项 PASS);
LLM 的 11 次取证调用里 2 次是 inspect_region,且都显式给 window + purpose:"text"
(x[20000,115000] y[-6500,3000],即 A0 图框底部横条=标题栏所在)。
验收口径达成:报告报出标题栏乱码 ???????????????,而 get_text_content 对全图返回 0 条文字
(本图文字全在块/自定义实体内)→ 块内文字是被视觉放大读出来的。
pytest 343 passed。
100 lines
3.7 KiB
Python
100 lines
3.7 KiB
Python
"""`dwg_review.checklist` 单元测试。"""
|
|
|
|
import json
|
|
|
|
from dwg_review.checklist import (
|
|
DEFAULT_CHECKLIST,
|
|
build_system_prompt,
|
|
checklist_summary,
|
|
load_checklist,
|
|
)
|
|
|
|
|
|
def test_load_valid_checklist(tmp_path):
|
|
data = {
|
|
"name": "自定义清单",
|
|
"categories": [{"title": "类别A", "severity": "high", "checks": ["检查1"]}],
|
|
}
|
|
path = tmp_path / "cl.json"
|
|
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
|
loaded = load_checklist(path)
|
|
assert loaded["name"] == "自定义清单"
|
|
assert loaded["categories"][0]["title"] == "类别A"
|
|
|
|
|
|
def test_load_missing_file_falls_back(tmp_path, capsys):
|
|
loaded = load_checklist(tmp_path / "nope.json")
|
|
assert loaded is DEFAULT_CHECKLIST
|
|
assert "无法加载审查清单" in capsys.readouterr().out
|
|
|
|
|
|
def test_load_malformed_structure_falls_back(tmp_path, capsys):
|
|
path = tmp_path / "cl.json"
|
|
path.write_text(json.dumps({"name": "缺 categories"}), encoding="utf-8")
|
|
assert load_checklist(path) is DEFAULT_CHECKLIST
|
|
assert "结构异常" in capsys.readouterr().out
|
|
|
|
|
|
def test_load_invalid_json_falls_back(tmp_path, capsys):
|
|
path = tmp_path / "cl.json"
|
|
path.write_text("{ not json", encoding="utf-8")
|
|
assert load_checklist(path) is DEFAULT_CHECKLIST
|
|
assert "无法加载审查清单" in capsys.readouterr().out
|
|
|
|
|
|
def test_load_empty_categories_falls_back(tmp_path):
|
|
path = tmp_path / "cl.json"
|
|
path.write_text(json.dumps({"categories": []}), encoding="utf-8")
|
|
assert load_checklist(path) is DEFAULT_CHECKLIST
|
|
|
|
|
|
def test_build_system_prompt_includes_categories_and_checks():
|
|
prompt = build_system_prompt(DEFAULT_CHECKLIST)
|
|
assert DEFAULT_CHECKLIST["name"] in prompt
|
|
for cat in DEFAULT_CHECKLIST["categories"]:
|
|
assert cat["title"] in prompt
|
|
assert f"严重度:{cat['severity']}" in prompt
|
|
for chk in cat["checks"]:
|
|
assert chk in prompt
|
|
|
|
|
|
def test_build_system_prompt_tool_hint_toggle():
|
|
with_tools = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
|
|
without = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=False)
|
|
assert "只读" in with_tools and "取证工具" in with_tools
|
|
assert "只读" not in without
|
|
|
|
|
|
def test_build_system_prompt_instructs_inspect_region():
|
|
prompt = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
|
|
# 放大取证走 inspect_region 这一个语义化入口(不再让 LLM 自己组合裸视口工具)
|
|
assert "inspect_region" in prompt
|
|
assert "handle" in prompt and "label" in prompt and "window" in prompt
|
|
assert "cx" in prompt and "cy" in prompt
|
|
assert "从上到下" in prompt # cy 方向必须写清(图像坐标)
|
|
assert "inspect-N" in prompt # 返回标签可继续下钻
|
|
assert "不需要判断截图够不够清楚" in prompt
|
|
|
|
|
|
def test_build_system_prompt_requires_structured_json_output():
|
|
# allow_tools=False 以隔离「工具提示块」里的「只读」字样
|
|
prompt = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=False)
|
|
assert "JSON" in prompt
|
|
assert '"summary"' in prompt
|
|
assert '"issues"' in prompt
|
|
assert '"severity"' in prompt and '"category"' in prompt and '"handle"' in prompt
|
|
# 结构化契约本身不得含「只读」,否则会破坏 allow_tools 开关的判别
|
|
assert "只读" not in prompt
|
|
|
|
|
|
def test_build_system_prompt_forbids_fabricated_handle():
|
|
prompt = build_system_prompt(DEFAULT_CHECKLIST)
|
|
assert "不要编造" in prompt
|
|
|
|
|
|
def test_checklist_summary_mentions_name_and_count():
|
|
summary = checklist_summary(DEFAULT_CHECKLIST)
|
|
assert DEFAULT_CHECKLIST["name"] in summary
|
|
assert str(len(DEFAULT_CHECKLIST["categories"])) in summary
|
|
assert DEFAULT_CHECKLIST["categories"][0]["title"] in summary
|