Buckets:

download
raw
12.5 kB
"""Tier 0 static checks — preset YAML, skill ↔ MCP-tool consistency.
Fast (<5s), no model calls, no subprocess. Run on every push.
Catches drift between:
• preset YAML and the artifact registry (hooks/rules/skills/agents/output_styles)
• preset ``web_panels`` and the shared ``BUILTIN_PANELS`` registry
• SKILL.md ``mcp__server__tool`` references and the actual ``@mcp.tool()``
function names exposed by the framework MCP servers
MCP-server short names are matched at .mcp.json render time (Tier 1 covers
that).
"""
from __future__ import annotations
import ast
import re
from pathlib import Path
import pytest
from osprey.cli.build_profile import resolve_build_profile
from osprey.cli.templates.artifact_library import validate_artifacts
REPO_ROOT = Path(__file__).resolve().parents[2]
SRC_OSPREY = REPO_ROOT / "src" / "osprey"
PRESETS_DIR = SRC_OSPREY / "profiles" / "presets"
MCP_SERVER_ROOT = SRC_OSPREY / "mcp_server"
SKILL_ROOTS = (
SRC_OSPREY / "templates" / "skills",
SRC_OSPREY / "templates" / "claude_code" / "claude" / "skills",
)
COMMANDS_ROOT = SRC_OSPREY / "templates" / "claude_code" / "claude" / "commands"
_MCP_REF_RE = re.compile(r"mcp__([a-zA-Z0-9_-]+)__([a-zA-Z0-9_]+)")
def _all_presets() -> list[str]:
"""Return the name of every preset shipped with osprey.
Names rather than paths: a preset file is not a profile file. It carries
preset-side keys (``app_template:``) that resolution consumes and the
profile loader refuses outright, so the only way to read one is through
the same entry point the build uses.
"""
return sorted(p.stem for p in PRESETS_DIR.glob("*.yml") if not p.name.startswith("_"))
def _collect_framework_tool_names() -> dict[str, set[str]]:
"""Walk ``src/osprey/mcp_server/<server>/tools/`` and collect ``@mcp.tool()`` names.
Returns a mapping of ``server_name → set of tool function names``. The
server name is the package directory name; tool names are the function
names decorated with ``@mcp.tool()``.
This intentionally excludes ``mcp_server/<server>/server.py`` registration
code — only ``@mcp.tool()``-decorated functions count as tools.
"""
server_tools: dict[str, set[str]] = {}
for server_dir in MCP_SERVER_ROOT.iterdir():
if not server_dir.is_dir() or server_dir.name.startswith(("_", ".")):
continue
tools_dir = server_dir / "tools"
if not tools_dir.is_dir():
continue
names: set[str] = set()
for py_file in tools_dir.rglob("*.py"):
if py_file.name.startswith("_"):
continue
names.update(_extract_mcp_tool_names(py_file))
if names:
server_tools[server_dir.name] = names
return server_tools
def _extract_mcp_tool_names(py_file: Path) -> set[str]:
"""Parse a Python file and return the set of functions decorated with ``@mcp.tool()``."""
try:
tree = ast.parse(py_file.read_text(encoding="utf-8"))
except SyntaxError:
return set()
out: set[str] = set()
for node in ast.walk(tree):
if not isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef):
continue
for deco in node.decorator_list:
target = deco.func if isinstance(deco, ast.Call) else deco
if (
isinstance(target, ast.Attribute)
and target.attr == "tool"
and isinstance(target.value, ast.Name)
and target.value.id in {"mcp", "server"}
):
out.add(node.name)
return out
def _server_alias_map(server_tools: dict[str, set[str]]) -> dict[str, set[str]]:
"""Map every plausible server-name alias to its tool set.
The .mcp.json server name (e.g. ``controls``, ``python``, ``channel-finder``)
differs from the ``mcp_server/`` package directory (e.g. ``control_system``,
``python_executor``, ``channel_finder_hierarchical``). Skill files use the
.mcp.json form (``mcp__controls__channel_read``), so we expose both.
"""
aliases: dict[str, set[str]] = {}
for server, tools in server_tools.items():
aliases.setdefault(server, set()).update(tools)
# Hyphen variant (channel_finder_hierarchical → channel-finder-hierarchical)
aliases.setdefault(server.replace("_", "-"), set()).update(tools)
# Hard-coded aliases from registry/mcp.py FRAMEWORK_SERVERS naming
if "control_system" in server_tools:
aliases.setdefault("controls", set()).update(server_tools["control_system"])
if "python_executor" in server_tools:
aliases.setdefault("python", set()).update(server_tools["python_executor"])
# channel-finder is a single .mcp.json server name backed by one of three packages
cf_union: set[str] = set()
for pkg in (
"channel_finder_hierarchical",
"channel_finder_in_context",
"channel_finder_middle_layer",
):
if pkg in server_tools:
cf_union.update(server_tools[pkg])
if cf_union:
aliases.setdefault("channel-finder", set()).update(cf_union)
aliases.setdefault("channel_finder", set()).update(cf_union)
return aliases
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("preset", _all_presets())
def test_preset_yaml_parses_and_artifacts_resolve(preset: str) -> None:
"""Each preset must parse, validate, and reference only artifacts that exist."""
profile, _preset_dir = resolve_build_profile(None, preset=preset)
artifacts = {}
for artifact_type in ("hooks", "rules", "skills", "agents", "output_styles"):
names = getattr(profile, artifact_type, [])
if names:
artifacts[artifact_type] = list(names)
if artifacts:
validate_artifacts(artifacts)
@pytest.mark.parametrize("preset", _all_presets())
def test_preset_web_panels_against_registry(preset: str) -> None:
"""Every ``web_panels`` entry must be a built-in panel or a URL-backed custom panel.
Mirrors the real contract in ``build_profile`` validation: a panel is valid
if it is in the shared ``osprey.profiles.web_panels`` registry *or* it is
backed by a ``web.panels.<id>.url`` config override (rendered as an iframe
tab). A drifted entry that is neither would render a broken UI tab at runtime.
Plus the third case the Reach Contract introduced, which is why this check
reads the registry rather than a hardcoded pair: a panel whose URL the
build DERIVES is legitimately url-less in the preset. The deploying profile
derives it from its own sidecar block, and an attached persona is told it
by projection from the hosting render — so a persona preset that spelled
the URL would be stating a fact it cannot keep true. This scan sees only
what the preset chain itself declares, not what a deployment's sidecar
block would add; the build-time refusal for a selected panel the deployment
cannot back is ``reach.selected_panel_errors``, not this static scan.
"""
from osprey.deployment.reach import REACH_CONTRACTS
from osprey.profiles.web_panels import BUILTIN_PANELS
derived = {p.panel for c in REACH_CONTRACTS.values() for p in c.projected if p.panel}
profile, _preset_dir = resolve_build_profile(None, preset=preset)
config = getattr(profile, "config", {}) or {}
unknown = [
p
for p in profile.web_panels
if p not in BUILTIN_PANELS and p not in derived and f"web.panels.{p}.url" not in config
]
assert not unknown, (
f"{preset}.yml declares unknown web_panels: {unknown} "
f"(valid: built-in {sorted(BUILTIN_PANELS)}, a panel the Reach Contract "
f"projects a URL for {sorted(derived)}, or a web.panels.<id>.url override)"
)
def _iter_skill_files() -> list[Path]:
files: list[Path] = []
for root in SKILL_ROOTS:
if root.is_dir():
files.extend(root.rglob("SKILL.md"))
files.extend(root.rglob("*.md"))
# Dedup
return sorted(set(files))
def test_skill_referenced_mcp_tools_exist() -> None:
"""Every ``mcp__<server>__<tool>`` ref in a SKILL must resolve to a real tool.
Skipped for servers not built into this repo (facility-specific or
documentation-only references).
"""
server_tools = _collect_framework_tool_names()
aliases = _server_alias_map(server_tools)
orphans: list[str] = []
for skill_file in _iter_skill_files():
text = skill_file.read_text(encoding="utf-8", errors="replace")
for match in _MCP_REF_RE.finditer(text):
server, tool = match.group(1), match.group(2)
known_tools = aliases.get(server)
if known_tools is None:
continue # unknown server (facility-specific or doc example)
if tool not in known_tools:
rel = skill_file.relative_to(REPO_ROOT)
orphans.append(f"{rel}: mcp__{server}__{tool} not found")
assert not orphans, "Skill files reference MCP tools that do not exist:\n " + "\n ".join(
orphans
)
def test_phoebus2_extends_clone_round_trips(tmp_path: Path) -> None:
"""A second framework-server instance declared via the ``extends`` form must
round-trip through the same dotted-override path the build uses
(config_update_fields + resolve_env_vars) into a working clone. The framework
phoebus2 registry entry was deleted, so ``extends: phoebus`` is the only
supported way to stand up a second live Phoebus instance.
"""
import yaml
from osprey.registry.mcp import resolve_servers
from osprey.utils.config import resolve_env_vars
from osprey.utils.config_writer import config_update_fields
# The dotted overrides a facility would declare in config.yml for a second
# live Phoebus instance (its own bridge URL, cloned from the phoebus server).
overrides: dict = {
"claude_code.servers.phoebus2.extends": "phoebus",
"claude_code.servers.phoebus2.env.PHOEBUS_BRIDGE_URL": (
"${PHOEBUS2_BRIDGE_URL:-http://127.0.0.1:7980}"
),
}
config_path = tmp_path / "config.yml"
config_path.write_text("claude_code:\n servers:\n phoebus: {enabled: true}\n")
config_update_fields(config_path, overrides)
loaded = resolve_env_vars(
yaml.safe_load(config_path.read_text(encoding="utf-8")),
environ={"PHOEBUS2_BRIDGE_URL": "http://10.0.0.5:7980"},
)
servers = resolve_servers(
loaded["claude_code"],
{"project_root": str(tmp_path), "current_python_env": "/usr/bin/python3"},
)
p2 = [s for s in servers if s["name"] == "phoebus2"]
assert len(p2) == 1 and p2[0]["enabled"] is True
assert p2[0]["args"] == ["-m", "osprey.mcp_server.phoebus"]
# Server env survives verbatim (${...} expanded at MCP launch, not here).
assert p2[0]["env"]["PHOEBUS_BRIDGE_URL"] == "${PHOEBUS2_BRIDGE_URL:-http://127.0.0.1:7980}"
assert p2[0]["hooks_pre"][0]["matcher"] == "mcp__phoebus2__phoebus_drive"
def test_slash_commands_resolve_targets() -> None:
"""Every ``.claude/commands/*.md`` slash command must reference a real artifact.
Skipped silently when no commands directory ships with this repo.
"""
if not COMMANDS_ROOT.is_dir():
pytest.skip("no slash-command templates in this repo")
from osprey.cli.templates.artifact_library import list_artifacts
available_skills = set(list_artifacts("skills"))
available_agents = set(list_artifacts("agents"))
orphans: list[str] = []
for cmd_file in COMMANDS_ROOT.rglob("*.md*"):
text = cmd_file.read_text(encoding="utf-8", errors="replace")
# Match common references like @skill:NAME, /agent:NAME, "skill: NAME" frontmatter
for kind, pat in (
("skill", r"@skill:([a-z][a-z0-9_-]+)"),
("agent", r"@agent:([a-z][a-z0-9_-]+)"),
):
for m in re.finditer(pat, text):
ref = m.group(1)
pool = available_skills if kind == "skill" else available_agents
if ref not in pool:
rel = cmd_file.relative_to(REPO_ROOT)
orphans.append(f"{rel}: {kind} '{ref}' not in registry")
assert not orphans, "Slash commands reference unknown artifacts:\n " + "\n ".join(orphans)

Xet Storage Details

Size:
12.5 kB
·
Xet hash:
e3829ee293a180a69604b78a5ff065385952891078f7d89602258d5b733e128a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.