C++(cad_mcp_plugins):
- 新增 bvh.h:header-only 2D BVH(质心中位分割,叶<=4,SelfPairs 交叉对枚举)
- 新增 find_collisions.cpp:粗筛 AABB 相交实体对,附重叠盒 + 逐轴重叠比 ratio
(= 逐轴重叠长/较短长 之积,退化轴取 1.0),按 ratio 降序 + 分页;
narrow=true 只对返回页跑 intersectWith 细筛(附 exact)
- 容器过滤(关键):图框块/分区边界会与一切相交,用重叠【度】判定
deg >= max(exclude_container_deg=8, exclude_container_frac=0.25 * 其他实体数),
与图幅/缩放无关;被剔除者在 excluded_containers 里如实报告
(初版用面积判据对多图框并排失效:3 框只占并集 27/13/7% 全部漏过,
全模型 163 对噪声;改度判据后图框 deg=35/25/23 vs 其余 deg<=3,分离干净)
- cmakelists.txt + 两个 mcp_config.json 注册(28 工具)
agent 侧(Python):
- cad_tools.READ_ONLY_TOOLS += find_collisions(受控子循环可调用)
- checks.check_collisions + run_deterministic_checks(受 CHECK_COLLISIONS 开关,默认关)
- config += CHECK_COLLISIONS=False / COLLISION_MIN_OVERLAP=0.5
- checklist.py / review_checklist.json += 「干涉/碰撞」类别(high)+ 提示词协议
(先 find_collisions 拿候选 → inspect_region 目视确认;AABB 相交 != 真干涉)
验证:
- verify_collisions.py 17/17 PASS:全模型候选 163→80(全部 ratio=0.000),
min_overlap=0.5 → 0 对;逐图框 0 对;narrow top-5 中 4 条 exact=true;
--inject 注入深重叠矩形报出 ratio=0.902 并清理 2/2
- pytest 355 passed(新增 12 项)
- 端到端:LLM 主动调用 find_collisions{min_overlap:0.2,narrow:true,window:<图框>},未误报
108 lines
4.0 KiB
Python
108 lines
4.0 KiB
Python
"""`dwg_review.cad_tools` 单元测试。"""
|
|
|
|
from dwg_review.cad_tools import READ_ONLY_TOOLS, CadTools, is_write_tool
|
|
from dwg_review.mcp_client import result_json
|
|
from tests.fakes import FakeMcpClient, ok_text, tool_schema
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 写类工具判定
|
|
# ---------------------------------------------------------------------------
|
|
def test_write_prefixes_detected():
|
|
for name in ("add_leader_annotation", "delete_entities", "draw_line", "set_layer"):
|
|
assert is_write_tool(name) is True
|
|
|
|
|
|
def test_write_exact_names_detected():
|
|
assert is_write_tool("set_entity_properties") is True
|
|
assert is_write_tool("highlight_entities") is True
|
|
|
|
|
|
def test_readonly_tools_are_not_write():
|
|
for name in ("query_entities", "get_text_content", "zoom_to_window",
|
|
"find_zero_length", "find_collisions"):
|
|
assert is_write_tool(name) is False
|
|
|
|
|
|
def test_find_collisions_is_whitelisted():
|
|
assert "find_collisions" in READ_ONLY_TOOLS
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 注册表
|
|
# ---------------------------------------------------------------------------
|
|
def test_registry_built_from_client():
|
|
client = FakeMcpClient()
|
|
tools = CadTools(client)
|
|
assert "query_entities" in tools.names
|
|
assert tools.has("get_viewport_screenshot") is True
|
|
assert tools.has("__nope__") is False
|
|
assert tools.names == sorted(tools.names)
|
|
|
|
|
|
def test_describe_returns_tool_dict():
|
|
client = FakeMcpClient()
|
|
tools = CadTools(client)
|
|
desc = tools.describe("query_entities")
|
|
assert desc["name"] == "query_entities"
|
|
assert "inputSchema" in desc
|
|
|
|
|
|
def test_refresh_picks_up_new_tools():
|
|
client = FakeMcpClient(tools=[tool_schema("a")])
|
|
tools = CadTools(client)
|
|
assert tools.names == ["a"]
|
|
client._tools = [tool_schema("a"), tool_schema("b")]
|
|
tools.refresh()
|
|
assert tools.names == ["a", "b"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# openai_tools
|
|
# ---------------------------------------------------------------------------
|
|
def test_openai_tools_format():
|
|
client = FakeMcpClient(tools=[tool_schema("query_entities", "查询", {"window": {"type": "object"}})])
|
|
entry = CadTools(client).openai_tools()[0]
|
|
assert entry["type"] == "function"
|
|
assert entry["function"]["name"] == "query_entities"
|
|
assert entry["function"]["description"] == "查询"
|
|
assert entry["function"]["parameters"]["properties"] == {"window": {"type": "object"}}
|
|
|
|
|
|
def test_openai_tools_defaults_schema_when_missing():
|
|
client = FakeMcpClient(tools=[{"name": "bare", "description": ""}])
|
|
entry = CadTools(client).openai_tools()[0]
|
|
assert entry["function"]["parameters"] == {"type": "object", "properties": {}}
|
|
|
|
|
|
def test_openai_tools_names_filter():
|
|
client = FakeMcpClient()
|
|
names = CadTools(client).openai_tools(names={"query_entities", "get_layers"})
|
|
assert sorted(n["function"]["name"] for n in names) == ["get_layers", "query_entities"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# call
|
|
# ---------------------------------------------------------------------------
|
|
def test_call_returns_normalized_response():
|
|
client = FakeMcpClient(responses={"query_entities": ok_text({"total": 7})})
|
|
tools = CadTools(client)
|
|
assert result_json(tools.call("query_entities", {"window": {}})) == {"total": 7}
|
|
assert client.calls[-1]["name"] == "query_entities"
|
|
assert client.calls[-1]["arguments"] == {"window": {}}
|
|
|
|
|
|
def test_call_marks_write_tools():
|
|
client = FakeMcpClient()
|
|
tools = CadTools(client)
|
|
tools.call("add_leader_annotation", {"text": "x"})
|
|
tools.call("query_entities", {"window": {}})
|
|
assert client.calls[0]["is_write"] is True
|
|
assert client.calls[1]["is_write"] is False
|
|
|
|
|
|
def test_call_explicit_is_write_overrides_inference():
|
|
client = FakeMcpClient()
|
|
CadTools(client).call("query_entities", {}, is_write=True)
|
|
assert client.calls[-1]["is_write"] is True
|