Files
autocad-mcp-pro/tests/test_refiner.py
T
Umut CANandClaude Opus 5 b03a7615b1 release: v1.5.0 — discovery layer, honesty debt closed, Wave A
Tool count 131 -> 154; 474 -> 1242 collected tests.

The release exists for two measured reasons. A 149-tool catalog costs a client
~40,300 tokens before it has asked for anything, and a drafter searching the way
drafters think -- FILLET, BPOLY, QSELECT, WBLOCK -- scored df=0 against the stock
index, because those words were simply not in the data.

Discovery and batching
- DISCOVERY_MODE=search replaces the catalog with search_tools + call_tool,
  ranked over an AutoCAD command and synonym corpus (discovery/aliases.py) on
  top of fastmcp's BM25 index. Measured: 40,305 -> 356 tokens advertised.
- cad_batch runs a step list in one round trip; fields= projects result-heavy
  responses. The `core` profile is gone -- discovery replaced it.

Honesty debt (Tier 0)
- OCS/ECS: `extrusion` appeared nowhere in this codebase, so mirrored and
  rotated geometry reported coordinates that were quietly wrong. backends/ocs.py
  translates at the backend boundary; every tool coordinate is WCS on both
  engines, and a tilted plane refuses rather than answering with a number that
  looks fine.
- drawing_save_as(fmt="dwg") wrote DXF bytes to a .dwg path. drawing_audit never
  repaired. analysis_measure_area shoelaced points the caller typed in.
- ezdxf calls gained a per-call timeout, so one hung call can no longer wedge a
  server whose document lock is a single asyncio.Lock.

Architecture
- The 1854-line backend ABC is 19 contract modules; @capability(key, reason=)
  makes optional methods declarative and a gate fails on undeclared holes.
- 8 layout/viewport tools, CHSPACE, CAD_PROGID escape hatch.
- Wave A: selection filters, boundary tracing, hatch depth, annotation objects,
  measurement -- 13 tools of a planned 39. The REGION and 2D-boolean family was
  cut on evidence: add_region() yields zero ACIS bytes and the greiner_hormann
  substitute loses 28.2% of the area on a square with one semicircular edge.

Verified against a live AutoCAD 2026, which found four COM defects no amount of
reading would have caught -- AcadPViewport has no ViewCenter, and the line that
set it had been swallowed by a bare except since v1.4.

Benchmark v3 (15 tasks) and what it caught
- v2 scored this server 10/10, which carried no information: every task in it
  exercised something the server was built around. Five tasks were added because
  they can fail, and three did while being written -- a hatch could not be
  measured though the refusal listed HATCH as measurable, the boundary tools and
  the measure tool disagreed about the same shape (139.2177 vs 139.2699), and a
  circle stored as two bulged vertices measured 0.0.
- Competitor reports are NOT re-scored. They are pinned v1.4 runs against v2 and
  were never asked these five questions; the chart shows those rows `not run`.
- Correctness A/B: 21/24 -> 24/24, zero regressions.

Performance, stated plainly
The published v1.4 perf report was recorded on CPython 3.14 and the new one on
3.11.15, so the apparent 2-6x speedup is the interpreter, not this release.
Re-measured on one interpreter, v1.5.0 is 28% slower creating entities and 22%
slower on the 10k roundtrip. Fully attributed: EZDXF_CALL_TIMEOUT=0 restores
v1.4.0's exact numbers. That is the deadlock fix being paid for, the knob is
documented, and winning it back is on the 1.6 roadmap.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 09:45:20 +03:00

142 lines
4.6 KiB
Python

from __future__ import annotations
import pytest
import config
from engineering.refiner import RepairRegistry, refine_drawing
pytestmark = pytest.mark.asyncio
async def test_transaction_stack_is_separate_from_drawing_undo(backend, monkeypatch):
"""An undo must not consume a transaction checkpoint, or vice versa."""
monkeypatch.setattr(config.settings, "ezdxf_undo_depth", 4)
await backend.drawing_new()
await backend.entity_create_line(0, 0, 10, 0)
await backend.transaction_begin()
undo = await backend.drawing_undo()
status = await backend.system_status()
assert undo["ok"] is True, "undo walks its own history"
assert status["transaction_depth"] == 1, "and leaves the checkpoint standing"
await backend.transaction_rollback()
async def test_transaction_rollback_restores_document(backend):
await backend.entity_create_line(0, 0, 10, 0)
await backend.transaction_begin()
await backend.entity_create_circle(5, 5, 2)
await backend.transaction_rollback()
entities = await backend.entity_list(limit=100)
assert [entity.type for entity in entities] == ["LINE"]
async def test_refiner_does_not_mutate_when_transaction_cannot_start(backend, monkeypatch):
await backend.entity_create_line(0, 0, 20, 0)
await backend.entity_create_line(0, 0, 20, 0)
async def transaction_rejected():
return {"ok": False, "error": "A transaction is already active"}
async def unexpected_transaction_end():
raise AssertionError("refiner must not end a transaction it did not start")
monkeypatch.setattr(backend, "transaction_begin", transaction_rejected)
monkeypatch.setattr(backend, "transaction_commit", unexpected_transaction_end)
monkeypatch.setattr(backend, "transaction_rollback", unexpected_transaction_end)
result = await refine_drawing(backend, focus=["duplicate_entities"])
assert result.status == "transaction_unavailable"
assert result.rounds[0].transaction == "not_started"
assert len(await backend.entity_list(type_filter="LINE")) == 2
async def test_refiner_repairs_construction_and_duplicates(backend):
await backend.entity_create_line(0, 0, 20, 0)
await backend.entity_create_line(0, 0, 20, 0)
await backend.construction_xline(0, 0, 0)
result = await refine_drawing(
backend,
max_rounds=3,
min_score=95,
focus=["duplicate_entities", "construction_left"],
)
assert result.status == "threshold_met"
assert result.final_score == 100.0
assert result.rounds[0].score_after > result.rounds[0].score_before
assert await backend.drawing_critique(["duplicate_entities", "construction_left"]) == []
async def test_refiner_dry_run_does_not_mutate_drawing(backend):
await backend.entity_create_line(0, 0, 20, 0)
await backend.entity_create_line(0, 0, 20, 0)
result = await refine_drawing(
backend,
focus=["duplicate_entities"],
dry_run=True,
)
assert result.status == "dry_run"
assert len(await backend.entity_list(type_filter="LINE")) == 2
assert result.rounds[0].actions[0].status == "planned"
async def test_refiner_rolls_back_when_custom_repair_lowers_score(backend):
await backend.entity_create_line(0, 0, 20, 0)
await backend.entity_create_line(0, 0, 20, 0)
registry = RepairRegistry()
async def harmful_repair(active_backend, issue):
await active_backend.construction_xline(0, 0, 0)
return {"created": "construction"}
registry.register("duplicate_entities", harmful_repair)
before = len(await backend.entity_list(limit=100))
result = await refine_drawing(
backend,
focus=["duplicate_entities", "construction_left"],
registry=registry,
)
assert result.status == "rolled_back"
assert result.rounds[0].transaction == "rolled_back"
assert len(await backend.entity_list(limit=100)) == before
async def test_refiner_leaves_missing_gdt_datum_for_manual_resolution(backend):
backend._gdt_datums_referenced = {"A"}
result = await refine_drawing(backend, focus=["gdt"])
assert result.status == "manual_required"
assert result.remaining_issues[0]["focus"] == "gdt"
async def test_server_drawing_refine_returns_structured_payload(backend):
import server
class Ctx:
lifespan_context = {"backend": backend}
await backend.entity_create_line(0, 0, 20, 0)
await backend.entity_create_line(0, 0, 20, 0)
result = await server.drawing_refine(
max_rounds=3,
min_score=95,
focus=["duplicate_entities"],
ctx=Ctx(),
)
assert result["status"] == "threshold_met"
assert result["final_score"] == 100.0