Files
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

174 lines
6.2 KiB
Python

"""M5 — undo/redo on the headless backend, and what it costs.
The plan called this "add ezdxf redo". The larger half was that **undo did not
work either**: `_undo_stack` was declared, read and cleaned up, but nothing ever
appended to it, so `drawing_undo` could only ever answer "Nothing to undo". Four
places promised otherwise — the `@cad_tool` summary ("Step back one operation"),
the tool title, the docstring, and the alias corpus, which routes `UNDO` and
"take that back" straight at it. The existing test asserted
``isinstance(result, dict)``, which a permanent failure satisfies.
Why it is opt-in. ezdxf has no journal; the only way to restore a previous state
is a full DXF snapshot. Measured on this machine:
10 entities -> 3.1 ms, 19 KB
100 entities -> 5.6 ms, 31 KB
1000 entities -> 28.4 ms, 146 KB
5000 entities -> 129.7 ms, 658 KB
Snapshotting on every mutation would make each new entity in a 5000-entity
drawing cost 130 ms. So `EZDXF_UNDO_DEPTH` defaults to 0 (off) and the tools
refuse with a capability that names the setting, rather than the server quietly
either lying or getting slow.
"""
from __future__ import annotations
import pytest
import pytest_asyncio
import config
from backends.base import UnsupportedCapabilityError
from backends.ezdxf_backend import EzdxfBackend
pytestmark = pytest.mark.asyncio
@pytest_asyncio.fixture
async def undoable(monkeypatch):
"""A backend with undo switched on, as a user opting in would have it."""
monkeypatch.setattr(config.settings, "ezdxf_undo_depth", 8)
backend = EzdxfBackend()
await backend.connect()
await backend.drawing_new()
yield backend
await backend.disconnect()
async def _count(backend) -> int:
return len(list(backend._doc.modelspace()))
# ── the promise, kept ───────────────────────────────────────────────────────
async def test_undo_actually_removes_the_last_entity(undoable):
await undoable.entity_create_line(0, 0, 10, 0)
await undoable.entity_create_circle(5, 5, 2)
assert await _count(undoable) == 2
result = await undoable.drawing_undo()
assert result["ok"] is True
assert await _count(undoable) == 1, "the circle must be gone"
async def test_redo_puts_it_back(undoable):
await undoable.entity_create_line(0, 0, 10, 0)
await undoable.entity_create_circle(5, 5, 2)
await undoable.drawing_undo()
result = await undoable.drawing_redo()
assert result["ok"] is True
assert await _count(undoable) == 2
async def test_undo_and_redo_walk_the_whole_history(undoable):
for i in range(4):
await undoable.entity_create_line(0, i, 10, i)
assert await _count(undoable) == 4
for expected in (3, 2, 1):
await undoable.drawing_undo()
assert await _count(undoable) == expected
for expected in (2, 3, 4):
await undoable.drawing_redo()
assert await _count(undoable) == expected
async def test_undo_stops_at_the_start_without_claiming_success(undoable):
await undoable.entity_create_line(0, 0, 10, 0)
await undoable.drawing_undo()
result = await undoable.drawing_undo()
assert result["ok"] is False
assert await _count(undoable) == 0
async def test_redo_with_nothing_to_redo_says_so(undoable):
await undoable.entity_create_line(0, 0, 10, 0)
result = await undoable.drawing_redo()
assert result["ok"] is False
# ── the branch that must not be reachable ───────────────────────────────────
async def test_drawing_after_an_undo_discards_the_redo_branch(undoable):
"""Redoing onto a timeline the user left would restore a state that never
existed — geometry they had deleted reappearing beside geometry they drew
afterwards."""
await undoable.entity_create_line(0, 0, 10, 0)
await undoable.entity_create_circle(5, 5, 2)
await undoable.drawing_undo() # circle gone
await undoable.entity_create_arc(0, 0, 5, 0, 90) # new branch
result = await undoable.drawing_redo()
assert result["ok"] is False, "the circle's future is no longer reachable"
types = sorted(e.dxftype() for e in undoable._doc.modelspace())
assert types == ["ARC", "LINE"], f"the circle must not come back: {types}"
async def test_history_is_bounded_by_the_configured_depth(monkeypatch):
monkeypatch.setattr(config.settings, "ezdxf_undo_depth", 2)
backend = EzdxfBackend()
await backend.connect()
await backend.drawing_new()
try:
for i in range(5):
await backend.entity_create_line(0, i, 10, i)
undone = 0
while (await backend.drawing_undo())["ok"]:
undone += 1
assert undone == 2, f"depth 2 must buy exactly two steps back, got {undone}"
finally:
await backend.disconnect()
# ── off by default, and honest about it ─────────────────────────────────────
async def test_undo_is_off_by_default_and_refuses_with_the_setting_named(backend):
assert config.settings.ezdxf_undo_depth == 0
with pytest.raises(UnsupportedCapabilityError) as excinfo:
await backend.drawing_undo()
assert excinfo.value.capability == "undo_history"
assert "EZDXF_UNDO_DEPTH" in str(excinfo.value), "the refusal must name the way in"
async def test_redo_refuses_the_same_way_when_history_is_off(backend):
with pytest.raises(UnsupportedCapabilityError) as excinfo:
await backend.drawing_redo()
assert excinfo.value.capability == "undo_history"
async def test_the_capability_is_declared_on_both_backends():
from backends.com_backend import ComBackend
ezdxf_feature = EzdxfBackend().capabilities().features["undo_history"]
assert ezdxf_feature.supported is False
assert "EZDXF_UNDO_DEPTH" in (ezdxf_feature.reason or "")
# AutoCAD keeps its own undo journal, so the live backend needs no setting.
assert ComBackend().capabilities().features["undo_history"].supported is True
async def test_switching_it_on_flips_the_capability(monkeypatch):
monkeypatch.setattr(config.settings, "ezdxf_undo_depth", 4)
assert EzdxfBackend().capabilities().features["undo_history"].supported is True