Files
hermes-agent/scripts/check_tool_discovery.py
alt-glitch db0568795e fix(code-health): tool-discovery and packaged-imports judge what discovery and the wheel do
tool-discovery now flags a registration in tools/registry.py or tools/__init__.py, which discovery never imports; both read one skip set in tools/registry.py.
packaged-imports no longer flags an import inside try/except ImportError or if TYPE_CHECKING, which cannot fail an installed wheel.
packaged-imports now flags a regular package under an unshipped namespace directory.
The FILE_LINES fix text no longer forbids flat siblings an area AGENTS.md prescribes, and is shorter.

CONTRIBUTING.md states the same flat-sibling exception as root AGENTS.md.
2026-11-30 15:28:01 +05:30

54 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Fail when a tracked module under ``tools/`` registers a tool that discovery never imports.
Inside a ``tools/`` package, discovery imports only ``<pkg>/tool.py``, so a module named anything
else that calls ``registry.register()`` at import is skipped and its tools are missing with no
error. Both questions are answered by discovery's own functions in ``tools/registry.py``.
Usage: python3 scripts/check_tool_discovery.py
"""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.append(str(ROOT))
from hermes_agent.compat.paths import current_path
from tools.registry import NOT_TOOL_MODULES, _module_registers_tools, _tool_module_candidates
def unreached_registrations(tools_dir: Path) -> list[Path]:
"""Tracked modules under ``tools_dir`` that register a tool at import but discovery skips."""
listed = subprocess.run(
["git", "ls-files", "-z", "--", "*.py"],
cwd=tools_dir, capture_output=True, text=True, check=True, timeout=60,
).stdout
registering = {
tools_dir / rel for rel in listed.split("\0")
if rel and _module_registers_tools(tools_dir / rel)
}
# Discovery also skips a package without __init__.py (setuptools would drop it from the wheel).
discovered = {p for p in _tool_module_candidates(tools_dir)
if p.name not in NOT_TOOL_MODULES
and (p.parent == tools_dir or (p.parent / "__init__.py").is_file())}
return sorted(registering - discovered)
def main() -> int:
missed = unreached_registrations(ROOT / current_path("tools"))
if missed:
print("These modules call registry.register() at import, but tool discovery never imports"
" them. Inside a package, register from <pkg>/tool.py and give the package an"
" __init__.py:", file=sys.stderr)
for path in missed:
print(f" {path.relative_to(ROOT).as_posix()}", file=sys.stderr)
return 1
print("check_tool_discovery: OK")
return 0
if __name__ == "__main__":
sys.exit(main())