The three track-H branches are merged; this is the integration that makes the six tools one surface. - TOOL_PACK_NAMES gains "plant" (pipe_takeoff, cable_takeoff); SECTION 26's four readers stay core. Lean gains drawing_understand and reaches 65 (55 with TOOL_PACKS=core). The `understand` and `plant` tags rank after `arch`, so the sections file as `understanding` (4) and `plant` (2): 247 tools in 28 groups. - discovery/aliases.py folds spec section 12's vocabulary into the six records the branches wrote - metraj, metre cetveli, boru / kablo listesi, fark, karsilastir, revizyon farki, olcek, birim in both Turkish spellings, BOQ, takeoff, scale check, COMPARE - without writing a phrase twice. The Russian terms are data only: the BM25 tokenizer keeps [a-z0-9], and no golden case pretends otherwise. - Eight golden tuning cases, four of them Turkish; the shared-command allowlist reconciled with what the merged records claim; the token-suite formula excludes the new pack; the inventory regenerated, the registry snapshot retaken and the README figures taken from check_doc_numbers. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FKFStjEFzpGyLq5bMpWxTd
272 lines
10 KiB
Python
272 lines
10 KiB
Python
"""Gates for the AutoCAD command / synonym alias corpus (``discovery.aliases``).
|
||
|
||
FastMCP's ``BM25SearchTransform`` indexes only tool name + description +
|
||
parameter names + parameter descriptions (see
|
||
``fastmcp/server/transforms/search/base.py::_extract_searchable_text``).
|
||
AutoCAD command names appear in none of those for this server, so a drafter
|
||
searching for ``BPOLY`` or ``QSELECT`` scores df=0 and gets nothing back.
|
||
|
||
These tests both *prove* that gap is real against the live registry and lock the
|
||
corpus that closes it to the live tool list, so the corpus can never silently
|
||
drift away from the registered tools.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
|
||
import pytest
|
||
|
||
import server
|
||
from discovery.aliases import (
|
||
SHARED_ACAD_COMMANDS,
|
||
TOOL_ALIASES,
|
||
ToolAliases,
|
||
alias_text,
|
||
aliases_for,
|
||
)
|
||
|
||
# AutoCAD command tokens: uppercase alphanumerics, no spaces, at least two
|
||
# characters (single-letter aliases like "L"/"C" are deliberately excluded --
|
||
# they are pure BM25 noise and collide with ordinary prose).
|
||
_ACAD_RE = re.compile(r"^[A-Z][A-Z0-9]+$")
|
||
|
||
# Commands proven below to be absent from every live tool's searchable text.
|
||
#
|
||
# Deliberately NOT in this list, and why:
|
||
# FILLET -- already present: the tool is literally named ``entity_fillet``.
|
||
# XCLIP -- the server has no xref/clipping support at all; any mapping
|
||
# would misroute the search, so the corpus honestly omits it.
|
||
# REVCLOUD -- no revision-cloud tool exists; a revcloud would have to be
|
||
# hand-built from arcs, so no single tool is the right answer.
|
||
#: AutoCAD commands that appear nowhere in the live tool text, so a
|
||
#: description-only ranker scores them df=0 and returns nothing.
|
||
#:
|
||
#: BPOLY and QSELECT used to be here and were removed in v1.5.0 (M8), when
|
||
#: `boundary_trace` and `selection_filter` landed and named those commands in
|
||
#: their own docstrings. The alias corpus is no longer the *only* way to reach
|
||
#: them, and saying otherwise would overstate what the corpus is worth. Two
|
||
#: commands moving out of this list is the honest cost of the drawing tools
|
||
#: growing to cover them — the corpus still carries the rest.
|
||
DF0_COMMANDS = (
|
||
"PEDIT",
|
||
"WBLOCK",
|
||
"OVERKILL",
|
||
"LAYTRANS",
|
||
"MATCHPROP",
|
||
)
|
||
|
||
|
||
async def _registered_tools() -> list:
|
||
return [t for t in await server._registered_tools() if getattr(t, "name", None)]
|
||
|
||
|
||
async def _registered_names() -> set[str]:
|
||
return {t.name for t in await _registered_tools()}
|
||
|
||
|
||
def _searchable_text(tool) -> str:
|
||
"""Mirror of fastmcp's ``_extract_searchable_text`` -- what BM25 indexes."""
|
||
parts = [tool.name]
|
||
if tool.description:
|
||
parts.append(tool.description)
|
||
schema = getattr(tool, "parameters", None) or {}
|
||
for param_name, param_info in (schema.get("properties") or {}).items():
|
||
parts.append(param_name)
|
||
if isinstance(param_info, dict) and param_info.get("description"):
|
||
parts.append(str(param_info["description"]))
|
||
return " ".join(parts).lower()
|
||
|
||
|
||
def _acad_owners() -> dict[str, list[str]]:
|
||
owners: dict[str, list[str]] = {}
|
||
for tool_name, record in TOOL_ALIASES.items():
|
||
for command in record.acad:
|
||
owners.setdefault(command, []).append(tool_name)
|
||
for names in owners.values():
|
||
names.sort()
|
||
return owners
|
||
|
||
|
||
# ── coverage ────────────────────────────────────────────────────────────────
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_every_registered_tool_has_an_alias_record():
|
||
"""Adding a tool without an alias record must fail this gate."""
|
||
missing = sorted(await _registered_names() - set(TOOL_ALIASES))
|
||
assert not missing, f"tools with no alias record: {missing}"
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_no_alias_key_is_an_unknown_tool():
|
||
"""Catches typos and tools that were renamed or removed."""
|
||
unknown = sorted(set(TOOL_ALIASES) - await _registered_names())
|
||
assert not unknown, f"alias keys that are not registered tools: {unknown}"
|
||
|
||
|
||
def test_every_record_is_a_tool_aliases_instance():
|
||
bad = sorted(k for k, v in TOOL_ALIASES.items() if not isinstance(v, ToolAliases))
|
||
assert not bad, f"records that are not ToolAliases: {bad}"
|
||
|
||
|
||
# ── shape ───────────────────────────────────────────────────────────────────
|
||
|
||
|
||
def test_acad_commands_are_uppercase_and_space_free():
|
||
bad: list[str] = []
|
||
for tool_name, record in TOOL_ALIASES.items():
|
||
for command in record.acad:
|
||
if not _ACAD_RE.fullmatch(command):
|
||
bad.append(f"{tool_name}:{command!r}")
|
||
assert not bad, f"malformed AutoCAD command tokens: {bad}"
|
||
|
||
|
||
def test_acad_commands_are_unique_within_a_record():
|
||
bad = sorted(k for k, v in TOOL_ALIASES.items() if len(set(v.acad)) != len(v.acad))
|
||
assert not bad, f"records with a repeated AutoCAD command: {bad}"
|
||
|
||
|
||
def test_synonyms_are_lowercase_non_empty_and_trimmed():
|
||
bad: list[str] = []
|
||
for tool_name, record in TOOL_ALIASES.items():
|
||
for phrase in record.synonyms:
|
||
if not phrase or phrase != phrase.lower() or phrase != phrase.strip():
|
||
bad.append(f"{tool_name}:{phrase!r}")
|
||
assert not bad, f"malformed synonyms: {bad}"
|
||
|
||
|
||
def test_every_tool_has_at_least_one_synonym():
|
||
"""``acad`` may legitimately be empty; a record with nothing in it is dead weight."""
|
||
empty = sorted(k for k, v in TOOL_ALIASES.items() if not v.synonyms)
|
||
assert not empty, f"records with no synonyms: {empty}"
|
||
|
||
|
||
def test_synonyms_are_unique_within_a_record():
|
||
bad = sorted(k for k, v in TOOL_ALIASES.items() if len(set(v.synonyms)) != len(v.synonyms))
|
||
assert not bad, f"records with a repeated synonym: {bad}"
|
||
|
||
|
||
# ── uniqueness ──────────────────────────────────────────────────────────────
|
||
#
|
||
# Rule: one AutoCAD command routes to exactly one tool. The only exceptions are
|
||
# the commands in ``SHARED_ACAD_COMMANDS`` -- option/dialog-driven umbrella
|
||
# commands (LAYER, ZOOM, ARRAY, LAYOUT, MEASUREGEOM, SETVAR) and the two places
|
||
# where this server genuinely exposes one operation through two tools (INSERT ->
|
||
# block_insert / entity_create_block_ref, ERASE -> entity_delete /
|
||
# entity_delete_many). Anything else is a copy-paste bug.
|
||
|
||
|
||
def test_each_acad_command_routes_to_a_single_tool():
|
||
unexpected = {
|
||
command: names
|
||
for command, names in _acad_owners().items()
|
||
if len(names) > 1 and command not in SHARED_ACAD_COMMANDS
|
||
}
|
||
assert not unexpected, f"AutoCAD command claimed by several tools: {unexpected}"
|
||
|
||
|
||
def test_shared_command_allowlist_has_no_stale_entries():
|
||
actually_shared = {c for c, names in _acad_owners().items() if len(names) > 1}
|
||
stale = sorted(SHARED_ACAD_COMMANDS - actually_shared)
|
||
assert not stale, f"SHARED_ACAD_COMMANDS lists commands that are not shared: {stale}"
|
||
|
||
|
||
# ── the df=0 regression guard ───────────────────────────────────────────────
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_key_autocad_commands_are_absent_from_live_tool_text():
|
||
"""Proves the gap: these commands score df=0 against the live BM25 corpus."""
|
||
tools = await _registered_tools()
|
||
hits = {
|
||
command: sorted(t.name for t in tools if command.lower() in _searchable_text(t))
|
||
for command in DF0_COMMANDS
|
||
}
|
||
leaked = {c: names for c, names in hits.items() if names}
|
||
assert not leaked, f"command already indexed -- drop it from DF0_COMMANDS: {leaked}"
|
||
|
||
|
||
def test_key_autocad_commands_are_covered_by_the_corpus():
|
||
"""...and proves the corpus closes it."""
|
||
owners = _acad_owners()
|
||
missing = sorted(c for c in DF0_COMMANDS if c not in owners)
|
||
assert not missing, f"df=0 commands still unreachable via the corpus: {missing}"
|
||
|
||
|
||
# ── helpers ─────────────────────────────────────────────────────────────────
|
||
|
||
|
||
def test_aliases_for_returns_the_record_or_none():
|
||
assert aliases_for("entity_fillet") is TOOL_ALIASES["entity_fillet"]
|
||
assert aliases_for("no_such_tool") is None
|
||
|
||
|
||
def test_alias_text_contains_commands_and_synonyms():
|
||
record = TOOL_ALIASES["entity_create_circle"]
|
||
text = alias_text("entity_create_circle")
|
||
for token in (*record.acad, *record.synonyms):
|
||
assert token in text
|
||
assert alias_text("no_such_tool") == ""
|
||
|
||
|
||
# ── track H: spec §12's five-language vocabulary ───────────────────────────
|
||
#
|
||
# One row per term the spec names, in the record(s) a plant engineer means by
|
||
# it. Turkish comes in both spellings (diacritic and ASCII-folded): the search
|
||
# tokenizer splits on a non-ASCII letter instead of folding it.
|
||
|
||
TRACK_H_TERMS = {
|
||
"pipe_takeoff": (
|
||
"takeoff",
|
||
"boq",
|
||
"metraj",
|
||
"metre cetveli",
|
||
"boru listesi",
|
||
"boru metrajı",
|
||
"boru metraji",
|
||
"boru metrajı çıkar",
|
||
"boru metraji cikar",
|
||
"спецификация", # specification: a bill of quantities
|
||
"ведомость", # statement: a quantity schedule
|
||
),
|
||
"cable_takeoff": (
|
||
"takeoff",
|
||
"boq",
|
||
"metraj",
|
||
"metre cetveli",
|
||
"kablo listesi",
|
||
"kablo metrajı",
|
||
"kablo metraji",
|
||
),
|
||
"drawing_diff": (
|
||
"compare",
|
||
"dwgcompare",
|
||
"fark",
|
||
"karşılaştır",
|
||
"karsilastir",
|
||
"revizyon farkı",
|
||
"revizyon farki",
|
||
"iki revizyonu karşılaştır",
|
||
"iki revizyonu karsilastir",
|
||
),
|
||
"drawing_scale_check": (
|
||
"scale check",
|
||
"ölçek",
|
||
"olcek",
|
||
"bu çizim ölçekli mi",
|
||
"bu cizim olcekli mi",
|
||
),
|
||
"drawing_understand": ("birim", "çizim birimi", "cizim birimi"),
|
||
}
|
||
|
||
|
||
def test_track_h_records_carry_the_five_language_vocabulary():
|
||
missing = {
|
||
tool: [term for term in terms if term not in TOOL_ALIASES[tool].synonyms]
|
||
for tool, terms in TRACK_H_TERMS.items()
|
||
}
|
||
missing = {tool: terms for tool, terms in missing.items() if terms}
|
||
assert not missing, f"track-H records miss spec §12 vocabulary: {missing}"
|
||
assert "COMPARE" in TOOL_ALIASES["drawing_diff"].acad
|