Files
cdsl-cad/backend/tests/test_part_skills.py
T
2026-08-24 14:52:08 +08:00

346 lines
21 KiB
Python

from __future__ import annotations
import asyncio
import copy
import json
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "backend"))
from app.models.contracts import ChatMessage, MessagePart # noqa: E402
from app.services.agent_service import AgentService, TOOL_SCHEMAS, system_prompt # noqa: E402
from app.services.engine_service import build_revision, load_engine, validate_cdsl # noqa: E402
from app.services.library import CdslLibrary # noqa: E402
from app.services.part_skills import PartSkillLibrary # noqa: E402
from app.services.storage import WorkspaceStore # noqa: E402
from app.settings import ProviderConfig, ProviderModel, Settings # noqa: E402
BACKEND = ROOT / "backend"
PART_SKILL_ROOT = BACKEND / "agent" / "skills" / "cad-engine" / "references" / "part-skills"
def workplane(z: float = 0.0) -> dict[str, list[float]]:
return {
"origin_mm": [0.0, 0.0, z],
"x_dir": [1.0, 0.0, 0.0],
"y_dir": [0.0, 1.0, 0.0],
"normal": [0.0, 0.0, 1.0],
}
def document(part_id: str, sketches: list[dict], features: list[dict]) -> dict:
return {
"schema": "cad.cdsl.llm.v1",
"schema_version": "1.1.0",
"kind": "part",
"part_id": part_id,
"meta": {"unit": "mm"},
"geometry": {"sketches": sketches},
"features": features,
}
def mounting_plate_fixture() -> dict:
return document("golden-mounting-plate", [
{"id": "base", "workplane": workplane(), "profile": {"type": "rectangle", "center": [0, 0], "width_mm": 80, "height_mm": 60}},
{"id": "grid", "workplane": workplane(10), "profile": {"type": "circle_grid", "radius_mm": 3, "count_x": 2, "count_y": 2, "spacing_x_mm": 50, "spacing_y_mm": 30, "center_mm": [0, 0]}},
], [
{"id": "base_add", "atomic_id": "extrude_add_blind", "depends_on": [], "params": {"distance_mm": 10}, "sketch_id": "base"},
{"id": "mount_pattern", "atomic_id": "extrude_cut_blind", "depends_on": ["base_add"], "params": {"distance_mm": 10, "reverse": True}, "sketch_id": "grid"},
{"id": "counterbores", "atomic_id": "hole_counterbore", "depends_on": ["mount_pattern"], "params": {"diameter_mm": 6, "depth_mm": 10, "counterbore_diameter_mm": 12, "counterbore_depth_mm": 4, "positions": [{"mm": [-15, 0, 0]}, {"mm": [15, 0, 0]}], "host_face": {"frame": workplane(10)}}, "sketch_id": "base"},
])
def mounting_bracket_fixture() -> dict:
web_plane = {"origin_mm": [0, -20, 0], "x_dir": [1, 0, 0], "y_dir": [0, 0, -1], "normal": [0, 1, 0]}
return document("golden-mounting-bracket", [
{"id": "base", "workplane": workplane(), "profile": {"type": "rectangle", "center": [0, 0], "width_mm": 80, "height_mm": 40}},
{"id": "web", "workplane": web_plane, "profile": {"type": "rectangle", "center": [0, -15], "width_mm": 50, "height_mm": 30}},
{"id": "slot", "workplane": workplane(6), "profile": {"type": "obround", "center": [0, 0], "length_mm": 24, "width_mm": 8}},
{"id": "symmetric_holes", "workplane": workplane(6), "profile": {"type": "circles", "items": [{"center": [-25, 0], "radius_mm": 3}, {"center": [25, 0], "radius_mm": 3}]}},
], [
{"id": "base_add", "atomic_id": "extrude_add_blind", "depends_on": [], "params": {"distance_mm": 6}, "sketch_id": "base"},
{"id": "web_add", "atomic_id": "extrude_add_blind", "depends_on": ["base_add"], "params": {"distance_mm": 6}, "sketch_id": "web"},
{"id": "slot_cut", "atomic_id": "extrude_cut_blind", "depends_on": ["web_add"], "params": {"distance_mm": 6, "reverse": True}, "sketch_id": "slot"},
{"id": "symmetric_cut", "atomic_id": "extrude_cut_blind", "depends_on": ["slot_cut"], "params": {"distance_mm": 6, "reverse": True}, "sketch_id": "symmetric_holes"},
])
def flange_fixture() -> dict:
return document("golden-flange", [
{"id": "base", "workplane": workplane(), "profile": {"type": "annulus", "inner_radius_mm": 10, "outer_radius_mm": 40}},
{"id": "bolt_circle", "workplane": workplane(12), "profile": {"type": "circles", "items": [{"center": [25, 0], "radius_mm": 3}, {"center": [0, 25], "radius_mm": 3}, {"center": [-25, 0], "radius_mm": 3}, {"center": [0, -25], "radius_mm": 3}]}},
], [
{"id": "base_add", "atomic_id": "extrude_add_blind", "depends_on": [], "params": {"distance_mm": 12}, "sketch_id": "base"},
{"id": "bolt_holes", "atomic_id": "extrude_cut_blind", "depends_on": ["base_add"], "params": {"distance_mm": 12, "reverse": True}, "sketch_id": "bolt_circle"},
])
def shaft_fixture() -> dict:
end_plane = {"origin_mm": [0, 35, 0], "x_dir": [1, 0, 0], "y_dir": [0, 0, -1], "normal": [0, 1, 0]}
return document("golden-stepped-shaft", [
{"id": "shaft_profile", "workplane": workplane(), "profile": {"type": "polygon", "vertices": [[0, 0], [10, 0], [10, 15], [7, 15], [7, 35], [0, 35]]}},
], [
{"id": "shaft_add", "atomic_id": "revolve_add", "depends_on": [], "params": {"angle_deg": 360, "axis": {"origin_mm": [0, 0, 0], "direction": [0, 1, 0]}}, "sketch_id": "shaft_profile"},
{"id": "coaxial_bore", "atomic_id": "hole_blind", "depends_on": ["shaft_add"], "params": {"diameter_mm": 4, "depth_mm": 35, "positions": [{"mm": [0, 0, 0]}], "host_face": {"frame": end_plane}}, "sketch_id": "shaft_profile"},
])
def bearing_housing_fixture() -> dict:
return document("golden-bearing-housing", [
{"id": "base", "workplane": workplane(), "profile": {"type": "rectangle", "center": [0, 0], "width_mm": 100, "height_mm": 60}},
{"id": "housing", "workplane": workplane(8), "profile": {"type": "circle", "radius_mm": 28}},
{"id": "base_holes", "workplane": workplane(8), "profile": {"type": "circle_grid", "radius_mm": 4, "count_x": 2, "count_y": 2, "spacing_x_mm": 80, "spacing_y_mm": 40, "center_mm": [0, 0]}},
], [
{"id": "base_add", "atomic_id": "extrude_add_blind", "depends_on": [], "params": {"distance_mm": 8}, "sketch_id": "base"},
{"id": "housing_add", "atomic_id": "extrude_add_blind", "depends_on": ["base_add"], "params": {"distance_mm": 25}, "sketch_id": "housing"},
{"id": "bearing_seat", "atomic_id": "hole_counterbore", "depends_on": ["housing_add"], "params": {"diameter_mm": 12, "depth_mm": 25, "counterbore_diameter_mm": 30, "counterbore_depth_mm": 10, "positions": [{"mm": [0, 0, 0]}], "host_face": {"frame": workplane(33)}}, "sketch_id": "housing"},
{"id": "base_mount_holes", "atomic_id": "extrude_cut_blind", "depends_on": ["bearing_seat"], "params": {"distance_mm": 8, "reverse": True}, "sketch_id": "base_holes"},
])
def hex_nut_fixture() -> dict:
return document("golden-hex-nut", [
{"id": "hex", "workplane": workplane(), "profile": {"type": "polygon", "vertices": [[10, 0], [5, 8.660254], [-5, 8.660254], [-10, 0], [-5, -8.660254], [5, -8.660254]]}},
{"id": "thread_bore", "workplane": workplane(10), "profile": {"type": "circle", "radius_mm": 3}},
], [
{"id": "nut_add", "atomic_id": "extrude_add_blind", "depends_on": [], "params": {"distance_mm": 10}, "sketch_id": "hex"},
{"id": "m6_bore", "atomic_id": "extrude_cut_blind", "depends_on": ["nut_add"], "params": {"distance_mm": 10, "reverse": True}, "sketch_id": "thread_bore"},
])
GOLDEN_CASES = (
("带沉孔和孔阵列的安装底板", mounting_plate_fixture, {"planning/mounting-plate", "atomic/counterbored-hole-creation", "atomic/pattern-holes-from-datum"}, {"atomic/pattern-holes-from-datum": "expanded"}),
("带槽和对称孔的安装支架", mounting_bracket_fixture, {"planning/mounting-bracket", "functional/slotted-adjustment-feature", "functional/symmetric-feature-layout"}, {"atomic/pattern-holes-from-datum": "expanded"}),
("带螺栓圆的法兰", flange_fixture, {"planning/flange", "functional/flange-bolt-circle"}, {"functional/flange-bolt-circle": "expanded"}),
("带回转体同轴孔的阶梯轴", shaft_fixture, {"planning/simple-shaft", "functional/axisymmetric-revolve-strategy", "atomic/coaxial-bore-rule"}, {}),
("带轴承座孔和底座孔的轴承座", bearing_housing_fixture, {"planning/bearing-housing", "functional/bearing-bore-seat"}, {}),
("M6 六角螺母", hex_nut_fixture, {"planning/hexagonal-nut", "atomic/threaded-hole-creation"}, {"atomic/threaded-hole-creation": "approximated"}),
)
class PartSkillLibraryTests(unittest.TestCase):
def setUp(self) -> None:
self.library = PartSkillLibrary(PART_SKILL_ROOT)
def test_english_and_chinese_triggers(self) -> None:
english = self.library.select("Create a mounting plate with counterbored hole pattern")
chinese = self.library.select("带沉孔和孔阵列的安装底板")
self.assertIn("planning/mounting-plate", english["skill_ids"])
self.assertIn("planning/mounting-plate", chinese["skill_ids"])
self.assertIn("atomic/counterbored-hole-creation", chinese["skill_ids"])
def test_exclusions_and_selection_limits(self) -> None:
selection = self.library.select("Create a flange nut with an M6 threaded hole")
self.assertNotIn("planning/flange", selection["skill_ids"])
self.assertLessEqual(len(selection["planning_ids"]), 1)
self.assertLessEqual(len(selection["support_ids"]), 3)
self.assertEqual(len(selection["skill_ids"]), len(set(selection["skill_ids"])))
def test_inheritance_and_primary_family_conflict(self) -> None:
inherited = ["planning/mounting-plate", "atomic/counterbored-hole-creation"]
addition = self.library.select("Add one M6 threaded hole", inherited)
conflict = self.library.select("Add a flange bolt circle", inherited)
replacement = self.library.select("Replace the whole part with a flange bolt circle", inherited)
self.assertIn("planning/mounting-plate", addition["skill_ids"])
self.assertIn("atomic/threaded-hole-creation", addition["skill_ids"])
self.assertEqual(conflict["conflict"]["current"], "planning/mounting-plate")
self.assertEqual(conflict["conflict"]["matched"], "planning/flange")
self.assertEqual(replacement["planning_ids"], ["planning/flange"])
self.assertIsNone(replacement["conflict"])
def test_catalog_and_bridge_files_are_complete(self) -> None:
payload = json.loads((PART_SKILL_ROOT / "catalog.json").read_text(encoding="utf-8"))
categories = {kind: 0 for kind in ("planning", "functional", "atomic")}
for skill in payload["skills"]:
categories[skill["kind"]] += 1
self.assertTrue((PART_SKILL_ROOT / skill["bridge"]).is_file())
self.assertTrue((PART_SKILL_ROOT / skill["source"]).is_file())
self.assertTrue(skill["triggers"])
self.assertTrue(skill["capability_translation_rules"])
self.assertEqual(categories, {"planning": 6, "functional": 7, "atomic": 8})
self.assertTrue((PART_SKILL_ROOT / "LICENSE").is_file())
self.assertTrue((PART_SKILL_ROOT / "PROVENANCE.md").is_file())
def test_audit_records_exact_omitted_and_blocked_capability_states(self) -> None:
fixture = mounting_plate_fixture()
exact = self.library.audit(self.library.select("安装底板"), fixture)
finishing = self.library.audit(self.library.select("edge treatment"), fixture)
blocked = self.library.audit(self.library.select("flange bolt circle"), mounting_plate_fixture())
exact_statuses = {item["skill_id"]: item["status"] for item in exact["capability_translations"]}
finishing_statuses = {item["skill_id"]: item for item in finishing["capability_translations"]}
blocked_statuses = {item["skill_id"]: item["status"] for item in blocked["capability_translations"]}
self.assertEqual(exact_statuses["planning/mounting-plate"], "exact")
self.assertEqual(finishing_statuses["atomic/fillet-chamfer-last"]["status"], "omitted")
self.assertEqual(finishing_statuses["atomic/fillet-chamfer-last"]["translation"], "selector_unavailable")
self.assertEqual(blocked_statuses["functional/flange-bolt-circle"], "blocked")
class AgentPartSkillTests(unittest.TestCase):
def test_agent_stream_selects_and_injects_part_skill_before_first_model_call(self) -> None:
class CaptureAgent(AgentService):
def __init__(self, *args: object, **kwargs: object) -> None:
super().__init__(*args, **kwargs)
self.first_messages: list[dict[str, object]] = []
async def _complete(self, messages: list[dict[str, object]], *args: object, **kwargs: object) -> dict[str, object]:
self.first_messages = [dict(message) for message in messages]
return {"choices": [{"message": {"role": "assistant", "content": "已分析", "tool_calls": []}}]}
with tempfile.TemporaryDirectory() as directory:
settings = self._settings(Path(directory))
library = PartSkillLibrary(PART_SKILL_ROOT)
agent = CaptureAgent(settings, WorkspaceStore(settings), CdslLibrary(settings), library)
message = ChatMessage(id="user_1", role="user", parts=[MessagePart(type="text", text="生成一个法兰螺栓圆")])
async def consume() -> None:
async for _ in agent.stream([message], None, None):
pass
asyncio.run(consume())
self.assertIn("[planning/flange]", str(agent.first_messages[0]["content"]))
self.assertIn("[functional/flange-bolt-circle]", str(agent.first_messages[0]["content"]))
def test_prompt_contains_bridge_without_adding_tools(self) -> None:
library = PartSkillLibrary(PART_SKILL_ROOT)
selection = library.select("Create a flange with a bolt circle")
with tempfile.TemporaryDirectory() as directory:
settings = self._settings(Path(directory))
prompt = system_prompt(settings, "Create a flange with a bolt circle", part_skill_context=library.render_context(selection))
self.assertIn("[planning/flange]", prompt)
self.assertIn("circular-pattern atomic", prompt)
self.assertEqual([tool["function"]["name"] for tool in TOOL_SCHEMAS], ["search_cdsl_library", "read_cdsl_reference", "propose_design_intent", "read_current_cdsl", "generate_cdsl_model"])
def test_generation_persists_assumptions_and_selected_skills(self) -> None:
with tempfile.TemporaryDirectory() as directory:
settings = self._settings(Path(directory))
store = WorkspaceStore(settings)
library = PartSkillLibrary(PART_SKILL_ROOT)
agent = AgentService(settings, store, CdslLibrary(settings), library)
request = "M6 六角螺母"
selection = library.select(request)
engine = load_engine(settings)
intent = engine.design_intent_from_cdsl(hex_nut_fixture(), request=request)
intent_state = {"phase": "WAITING_FOR_INTENT", "design_intent_id": ""}
planned, _ = asyncio.run(agent._run_tool(
"propose_design_intent",
{"intent": intent, "summary": "M6 nut plan", "assumptions": []},
"",
request,
[],
part_skill_selection=selection,
intent_state=intent_state,
))
self.assertTrue(planned["ok"])
result, generated = asyncio.run(agent._run_tool(
"generate_cdsl_model",
{"design_intent_id": planned["design_intent_id"], "cdsl": hex_nut_fixture(), "summary": "M6 nut", "assumptions": ["M6 thread is represented as a cylindrical bore"]},
planned["task_id"],
request,
["reference-fixture"],
part_skill_selection=selection,
intent_state=intent_state,
))
self.assertTrue(result["ok"])
self.assertIsNotNone(generated)
task = store.read_task(str(generated["task_id"]))
revision = task["revisions"][0]
audit = json.loads(store.artifact_path(task["task_id"], revision["part_skills_path"]).read_text(encoding="utf-8"))
report = json.loads(store.artifact_path(task["task_id"], revision["report_path"]).read_text(encoding="utf-8"))
self.assertIn("M6 thread is represented as a cylindrical bore", audit["assumptions"])
self.assertIn("atomic/threaded-hole-creation", revision["part_skill_ids"])
self.assertEqual(report["generation_context"]["cdsl_reference_ids"], ["reference-fixture"])
@staticmethod
def _settings(root: Path) -> Settings:
provider = ProviderConfig("test", "Test", "https://example.invalid/v1", "test-key", (ProviderModel("test-model"),))
return Settings(
task_root=root / "tasks",
conversation_root=root / "conversations",
library_root=BACKEND / "cdsl_library",
engine_root=BACKEND / "engine" / "cdsl_engine",
llm_base_url=provider.base_url,
llm_api_key=provider.api_key,
llm_model="test-model",
llm_timeout_s=1,
default_provider_id="test",
providers=(provider,),
)
class PartSkillBuildAndGoldenTests(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.settings = AgentPartSkillTests._settings(Path(tempfile.mkdtemp()))
cls.engine = load_engine(cls.settings)
cls.library = PartSkillLibrary(PART_SKILL_ROOT)
def test_success_and_failure_revisions_always_write_part_skill_audit(self) -> None:
with tempfile.TemporaryDirectory() as directory:
settings = AgentPartSkillTests._settings(Path(directory))
store = WorkspaceStore(settings)
selection = self.library.select("带螺栓圆的法兰")
audit = self.library.audit(selection, flange_fixture(), ["Explicit circle expansion"])
success = build_revision(
settings=settings, store=store, task_id=None, request="带螺栓圆的法兰", cdsl=flange_fixture(),
reference_ids=["flange-reference"], summary="flange", part_skills=audit,
generation_assumptions=["Explicit circle expansion"],
)
success_audit = json.loads(store.artifact_path(success["task_id"], success["part_skills_path"]).read_text(encoding="utf-8"))
success_report = json.loads(store.artifact_path(success["task_id"], success["report_path"]).read_text(encoding="utf-8"))
self.assertEqual(success_audit["skill_ids"], audit["skill_ids"])
self.assertIn("generation_context", success_report)
invalid = copy.deepcopy(flange_fixture())
invalid["features"][0]["atomic_id"] = "not_supported"
failed_audit = self.library.audit(selection, invalid, [])
with self.assertRaisesRegex(ValueError, "CDSL schema violation"):
build_revision(
settings=settings, store=store, task_id=success["task_id"], request="bad flange", cdsl=invalid,
reference_ids=[], summary="bad", part_skills=failed_audit, generation_assumptions=[],
)
task = store.read_task(success["task_id"])
failed = task["revisions"][-1]
self.assertEqual(failed["status"], "failed")
self.assertTrue(store.artifact_path(task["task_id"], failed["part_skills_path"]).is_file())
report = json.loads(store.artifact_path(task["task_id"], failed["report_path"]).read_text(encoding="utf-8"))
self.assertEqual(report["generation_context"]["part_skill_ids"], audit["skill_ids"])
legacy_report = {"engine_result": {"engine": "cdsl_only"}}
self.assertEqual(legacy_report.get("generation_context", {}), {})
def test_golden_part_families_validate_preflight_rebuild_and_audit(self) -> None:
for request, factory, expected_skills, expected_statuses in GOLDEN_CASES:
with self.subTest(request=request):
cdsl = factory()
selection = self.library.select(request)
self.assertTrue(expected_skills.issubset(selection["skill_ids"]))
validate_cdsl(cdsl, self.engine)
with tempfile.TemporaryDirectory() as directory:
step_path = Path(directory) / "model.step"
result = self.engine.run_cdsl_only(copy.deepcopy(cdsl), step_path)
self.assertEqual(result["engine"], "cdsl_only")
self.assertTrue(step_path.is_file())
self.assertGreater(step_path.stat().st_size, 0)
audit = self.library.audit(selection, cdsl, ["golden fixture"])
statuses = {item["skill_id"]: item["status"] for item in audit["capability_translations"]}
for skill_id, status in expected_statuses.items():
self.assertEqual(statuses[skill_id], status)
self.assertTrue(audit["evidence"]["features"])
self.assertTrue(audit["evidence"]["profiles"])
if __name__ == "__main__":
unittest.main()