|
| 1 | +"""Arch guard: any function in server/tools/ that checks DispatchStatus.RUNNING |
| 2 | +must also verify liveness via is_dispatch_session_alive or resolve_stale_running. |
| 3 | +
|
| 4 | +Without this guard, new MCP tools can silently encode the bug pattern that |
| 5 | +caused issue #4133 — treating a status claim as a fact without verifying |
| 6 | +the owning process is alive. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import ast |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +pytestmark = [pytest.mark.layer("arch"), pytest.mark.small] |
| 16 | + |
| 17 | +_LIVENESS_HELPERS: frozenset[str] = frozenset( |
| 18 | + {"is_dispatch_session_alive", "resolve_stale_running"} |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class _RunningStatusChecker(ast.NodeVisitor): |
| 23 | + """Find DispatchStatus.RUNNING comparisons and verify liveness helper usage.""" |
| 24 | + |
| 25 | + def __init__(self, fn_node: ast.FunctionDef | ast.AsyncFunctionDef) -> None: |
| 26 | + self.fn_node = fn_node |
| 27 | + self.running_check_linenos: list[int] = [] |
| 28 | + self.has_liveness_helper = False |
| 29 | + |
| 30 | + def visit_Compare(self, node: ast.Compare) -> None: |
| 31 | + if self._is_running_status_check(node): |
| 32 | + self.running_check_linenos.append(node.lineno) |
| 33 | + self.generic_visit(node) |
| 34 | + |
| 35 | + def visit_Call(self, node: ast.Call) -> None: |
| 36 | + if self._is_liveness_helper_call(node): |
| 37 | + self.has_liveness_helper = True |
| 38 | + self.generic_visit(node) |
| 39 | + |
| 40 | + def _is_running_status_check(self, node: ast.Compare) -> bool: |
| 41 | + if not isinstance(node.left, ast.Attribute): |
| 42 | + return False |
| 43 | + if node.left.attr != "status": |
| 44 | + return False |
| 45 | + for comparator in node.comparators: |
| 46 | + if not isinstance(comparator, ast.Attribute): |
| 47 | + continue |
| 48 | + if comparator.attr != "RUNNING": |
| 49 | + continue |
| 50 | + if not isinstance(node.ops[0], (ast.Eq, ast.Is)): |
| 51 | + continue |
| 52 | + return True |
| 53 | + return False |
| 54 | + |
| 55 | + def _is_liveness_helper_call(self, node: ast.Call) -> bool: |
| 56 | + if isinstance(node.func, ast.Name) and node.func.id in _LIVENESS_HELPERS: |
| 57 | + return True |
| 58 | + if isinstance(node.func, ast.Attribute) and node.func.attr in _LIVENESS_HELPERS: |
| 59 | + return True |
| 60 | + return False |
| 61 | + |
| 62 | + |
| 63 | +def _get_function_defs(tree: ast.Module) -> list[ast.FunctionDef | ast.AsyncFunctionDef]: |
| 64 | + fns: list[ast.FunctionDef | ast.AsyncFunctionDef] = [] |
| 65 | + for node in ast.walk(tree): |
| 66 | + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
| 67 | + fns.append(node) |
| 68 | + return fns |
| 69 | + |
| 70 | + |
| 71 | +def test_running_status_checks_require_liveness(): |
| 72 | + """Every DispatchStatus.RUNNING check in server/tools/ must be guarded |
| 73 | + by a liveness verification (is_dispatch_session_alive or resolve_stale_running). |
| 74 | + """ |
| 75 | + from autoskillit.core import paths |
| 76 | + |
| 77 | + tools_dir = paths.pkg_root() / "server" / "tools" |
| 78 | + assert tools_dir.is_dir(), f"server/tools/ directory not found at {tools_dir}" |
| 79 | + |
| 80 | + violations: list[str] = [] |
| 81 | + for py_file in sorted(tools_dir.glob("*.py")): |
| 82 | + try: |
| 83 | + tree = ast.parse(py_file.read_text()) |
| 84 | + except SyntaxError: |
| 85 | + continue |
| 86 | + |
| 87 | + for fn in _get_function_defs(tree): |
| 88 | + checker = _RunningStatusChecker(fn) |
| 89 | + checker.visit(fn) |
| 90 | + |
| 91 | + if not checker.running_check_linenos: |
| 92 | + continue |
| 93 | + |
| 94 | + if checker.has_liveness_helper: |
| 95 | + continue |
| 96 | + |
| 97 | + relpath = str(py_file.relative_to(paths.pkg_root())) |
| 98 | + for lineno in checker.running_check_linenos: |
| 99 | + violations.append( |
| 100 | + f"{relpath}:{lineno} — {fn.name}() checks " |
| 101 | + "DispatchStatus.RUNNING without verifying liveness. " |
| 102 | + f"Use {' or '.join(sorted(_LIVENESS_HELPERS))} " |
| 103 | + "before treating RUNNING as a blocking fact." |
| 104 | + ) |
| 105 | + |
| 106 | + assert not violations, "RUNNING status checks without liveness verification:\n" + "\n".join( |
| 107 | + f" {v}" for v in violations |
| 108 | + ) |
0 commit comments