|
| 1 | +"""Every pointer an agent is handed to a guide section opens that section. |
| 2 | +
|
| 3 | +The resident text - the MCP server's instructions and tool descriptions, and |
| 4 | +each plugin skill's SKILL.md - says what a tool or a family does and points |
| 5 | +at a guide for the rest. A pointer is the whole of the discovery path: a |
| 6 | +section renamed out from under one, or a pointer trimmed away to fit a |
| 7 | +budget (#574 re-broke #282 that way), leaves an agent holding no way to the |
| 8 | +detail. So every pointer must resolve through the same `get_guide` the |
| 9 | +server answers with, and the tools that carry one now must keep it. |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import glob |
| 14 | +import os |
| 15 | +import re |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from dw.server import guides |
| 20 | +from tests.test_examples import REPO_ROOT |
| 21 | + |
| 22 | +pytest.importorskip("mcp", reason="the mcp extra is not installed") |
| 23 | + |
| 24 | +from tests.test_mcp_server import ok, server_over, tools_of # noqa: E402 |
| 25 | + |
| 26 | +SKILL_TEXTS = sorted( |
| 27 | + glob.glob(os.path.join(REPO_ROOT, "plugins", "dw", "skills", "*", "SKILL.md")) |
| 28 | + + glob.glob( |
| 29 | + os.path.join(REPO_ROOT, "plugins", "dw", "skills", "*", "references", "*.md") |
| 30 | + ) |
| 31 | +) |
| 32 | + |
| 33 | +# The guide a file name stands for, for the "WORKFLOW_GUIDE's ..." form. |
| 34 | +_BY_FILE = { |
| 35 | + file_name.removesuffix(".md"): name |
| 36 | + for name, (file_name, _summary) in guides.GUIDES.items() |
| 37 | +} |
| 38 | + |
| 39 | +# The forms a pointer is written in, with whitespace already collapsed: |
| 40 | +# get_guide("workflows", section="The loop") get_guide("loras") |
| 41 | +# the `acceleration` guide, section "Reading Memory While Offloading" |
| 42 | +# WORKFLOW_GUIDE's "The loop" |
| 43 | +_CALL = re.compile( |
| 44 | + r'get_guide\(\s*"(?P<name>[a-z-]+)"(?:\s*,\s*section\s*=\s*"(?P<section>[^"]+)")?\s*\)' |
| 45 | +) |
| 46 | +_PROSE = re.compile(r'the `(?P<name>[a-z-]+)` guide, section "(?P<section>[^"]+)"') |
| 47 | +_FILE = re.compile(r'(?P<file>[A-Z0-9_]+)\'s "(?P<section>[^"]+)"') |
| 48 | + |
| 49 | +# The tools whose description points at a guide today. A pointer trimmed |
| 50 | +# away to fit the surface budget fails here rather than leaving the tool |
| 51 | +# with no way to its detail; adding one to a tool means adding it here. |
| 52 | +TOOLS_THAT_POINT = { |
| 53 | + "get_job", |
| 54 | + "get_memory", |
| 55 | + "get_output_audio", |
| 56 | + "save_lora", |
| 57 | + "validate_workflow", |
| 58 | + "wait_for_job", |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +def pointers(text): |
| 63 | + """(guide name, section or None) for each pointer in `text`.""" |
| 64 | + flat = " ".join(text.split()) |
| 65 | + found = [(m["name"], m["section"]) for m in _CALL.finditer(flat)] |
| 66 | + found += [(m["name"], m["section"]) for m in _PROSE.finditer(flat)] |
| 67 | + found += [ |
| 68 | + (_BY_FILE[m["file"]], m["section"]) |
| 69 | + for m in _FILE.finditer(flat) |
| 70 | + if m["file"] in _BY_FILE |
| 71 | + ] |
| 72 | + return found |
| 73 | + |
| 74 | + |
| 75 | +def unresolved(text): |
| 76 | + """The pointers in `text` that `get_guide` refuses.""" |
| 77 | + bad = [] |
| 78 | + for name, section in pointers(text): |
| 79 | + try: |
| 80 | + guides.get_guide(name, section) |
| 81 | + except (guides.GuideError, KeyError) as error: |
| 82 | + bad.append((name, section, str(error)[:120])) |
| 83 | + return bad |
| 84 | + |
| 85 | + |
| 86 | +def mcp_texts(): |
| 87 | + server = server_over(ok({})) |
| 88 | + tools = asyncio.run(tools_of(server)) |
| 89 | + texts = {name: tool.description or "" for name, tool in tools.items()} |
| 90 | + texts["<instructions>"] = server.instructions |
| 91 | + return texts |
| 92 | + |
| 93 | + |
| 94 | +def test_every_pointer_in_the_mcp_surface_resolves(): |
| 95 | + bad = {name: unresolved(text) for name, text in mcp_texts().items()} |
| 96 | + assert not {name: b for name, b in bad.items() if b} |
| 97 | + |
| 98 | + |
| 99 | +def test_the_tools_that_point_at_a_guide_still_do(): |
| 100 | + texts = mcp_texts() |
| 101 | + lost = sorted(name for name in TOOLS_THAT_POINT if not pointers(texts[name])) |
| 102 | + assert not lost, f"these tools lost their guide pointer: {lost}" |
| 103 | + |
| 104 | + |
| 105 | +def test_the_server_instructions_point_at_the_authoring_section(): |
| 106 | + assert ("workflows", "Authoring a workflow from an agent") in pointers( |
| 107 | + mcp_texts()["<instructions>"] |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize( |
| 112 | + "path", SKILL_TEXTS, ids=lambda p: os.path.relpath(p, REPO_ROOT) |
| 113 | +) |
| 114 | +def test_every_pointer_in_a_skill_resolves(path): |
| 115 | + with open(path, encoding="utf-8") as handle: |
| 116 | + assert not unresolved(handle.read()) |
| 117 | + |
| 118 | + |
| 119 | +def test_the_pointer_forms_are_recognised(): |
| 120 | + """The extraction itself, so a form it stops matching cannot pass the |
| 121 | + resolution tests by finding nothing.""" |
| 122 | + text = ( |
| 123 | + 'see get_guide("workflows",\n section="The loop") and the ' |
| 124 | + '`acceleration` guide, section "Caching", and WORKFLOW_GUIDE\'s ' |
| 125 | + '"References", and get_guide("loras")' |
| 126 | + ) |
| 127 | + assert pointers(text) == [ |
| 128 | + ("workflows", "The loop"), |
| 129 | + ("loras", None), |
| 130 | + ("acceleration", "Caching"), |
| 131 | + ("workflows", "References"), |
| 132 | + ] |
0 commit comments