|
| 1 | +"""Architectural structural tests for capability admission control. |
| 2 | +
|
| 3 | +Verifies that: |
| 4 | +- _compute_capability_feasibility is called inside load_and_validate |
| 5 | +- All four content-serving surfaces gate on dispatch_feasible |
| 6 | +- CAPABILITY_GATE_CALLABLES entries have a corresponding BACKEND_CAPABILITY_INGREDIENTS input |
| 7 | +- Every run_python step using a CAPABILITY_GATE_CALLABLES callable reads a capability ingredient |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import ast |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from tests.arch._helpers import SRC_ROOT |
| 17 | + |
| 18 | +pytestmark = [pytest.mark.layer("arch"), pytest.mark.small] |
| 19 | + |
| 20 | + |
| 21 | +def _has_call_in_function(tree: ast.Module, func_name: str, target_call: str) -> bool: |
| 22 | + """Return True if the named function contains a call to target_call.""" |
| 23 | + for node in ast.walk(tree): |
| 24 | + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
| 25 | + if node.name != func_name: |
| 26 | + continue |
| 27 | + for child in ast.walk(node): |
| 28 | + if ( |
| 29 | + isinstance(child, ast.Call) |
| 30 | + and isinstance(child.func, ast.Name) |
| 31 | + and child.func.id == target_call |
| 32 | + ): |
| 33 | + return True |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | +def test_load_and_validate_calls_compute_capability_feasibility() -> None: |
| 38 | + """load_and_validate must invoke _compute_capability_feasibility.""" |
| 39 | + api_path = SRC_ROOT / "recipe" / "_api.py" |
| 40 | + tree = ast.parse(api_path.read_text(encoding="utf-8")) |
| 41 | + assert _has_call_in_function(tree, "load_and_validate", "_compute_capability_feasibility"), ( |
| 42 | + "load_and_validate must call _compute_capability_feasibility " |
| 43 | + "to detect DOA pipelines from capability-gated run_python steps." |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def test_open_kitchen_gates_on_dispatch_feasible() -> None: |
| 48 | + """open_kitchen must check dispatch_feasible before enabling the gate.""" |
| 49 | + kitchen_path = SRC_ROOT / "server" / "tools" / "tools_kitchen.py" |
| 50 | + src = kitchen_path.read_text(encoding="utf-8") |
| 51 | + tree = ast.parse(src) |
| 52 | + for node in ast.walk(tree): |
| 53 | + if isinstance(node, ast.AsyncFunctionDef) and node.name == "open_kitchen": |
| 54 | + src_dispatch_feasible = False |
| 55 | + for child in ast.walk(node): |
| 56 | + if isinstance(child, ast.Constant) and child.value == "dispatch_feasible": |
| 57 | + src_dispatch_feasible = True |
| 58 | + break |
| 59 | + assert src_dispatch_feasible, ( |
| 60 | + "open_kitchen must reference 'dispatch_feasible' to gate " |
| 61 | + "capability-DOA pipelines before revealing tools." |
| 62 | + ) |
| 63 | + return |
| 64 | + pytest.fail("open_kitchen function not found in tools_kitchen.py") |
| 65 | + |
| 66 | + |
| 67 | +def test_get_recipe_gates_on_dispatch_feasible() -> None: |
| 68 | + """get_recipe resource must check dispatch_feasible.""" |
| 69 | + kitchen_path = SRC_ROOT / "server" / "tools" / "tools_kitchen.py" |
| 70 | + src = kitchen_path.read_text(encoding="utf-8") |
| 71 | + assert "dispatch_feasible" in src, ( |
| 72 | + "get_recipe resource must reference 'dispatch_feasible' to gate " |
| 73 | + "capability-DOA recipe serving." |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def test_load_recipe_gates_on_dispatch_feasible() -> None: |
| 78 | + """load_recipe tool must check dispatch_feasible.""" |
| 79 | + recipe_path = SRC_ROOT / "server" / "tools" / "tools_recipe.py" |
| 80 | + src = recipe_path.read_text(encoding="utf-8") |
| 81 | + assert "dispatch_feasible" in src, ( |
| 82 | + "load_recipe tool must reference 'dispatch_feasible' to surface " |
| 83 | + "capability-DOA signal to calling orchestrators." |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def test_capability_gate_callables_have_matching_ingredient() -> None: |
| 88 | + """Every CAPABILITY_GATE_CALLABLES callable must map to a BACKEND_CAPABILITY_INGREDIENTS input. |
| 89 | +
|
| 90 | + This guards against adding a new gate callable without declaring its |
| 91 | + capability ingredient dependency. |
| 92 | + """ |
| 93 | + import autoskillit.smoke_utils as smoke_utils_mod |
| 94 | + from autoskillit.core import BACKEND_CAPABILITY_INGREDIENTS, CAPABILITY_GATE_CALLABLES |
| 95 | + |
| 96 | + for callable_name in CAPABILITY_GATE_CALLABLES: |
| 97 | + assert hasattr(smoke_utils_mod, callable_name), ( |
| 98 | + f"CAPABILITY_GATE_CALLABLES entry {callable_name!r} has no matching " |
| 99 | + f"callable in autoskillit.smoke_utils." |
| 100 | + ) |
| 101 | + assert callable_name in smoke_utils_mod.__all__, ( |
| 102 | + f"CAPABILITY_GATE_CALLABLES entry {callable_name!r} is not exported " |
| 103 | + f"via smoke_utils.__all__." |
| 104 | + ) |
| 105 | + |
| 106 | + assert len(BACKEND_CAPABILITY_INGREDIENTS) > 0, ( |
| 107 | + "BACKEND_CAPABILITY_INGREDIENTS must declare at least one capability ingredient key." |
| 108 | + ) |
| 109 | + assert len(CAPABILITY_GATE_CALLABLES) > 0, ( |
| 110 | + "CAPABILITY_GATE_CALLABLES must declare at least one capability gate callable." |
| 111 | + ) |
0 commit comments