|
| 1 | +"""Task 1746: Cross-layer circular dependency test. |
| 2 | +
|
| 3 | +Builds a module-level dependency graph from all src/orb/ files and asserts |
| 4 | +there are no circular dependencies between top-level packages. |
| 5 | +
|
| 6 | +Current known cycles are whitelisted so the test passes today while preventing |
| 7 | +any NEW cycles from being introduced. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import collections |
| 13 | + |
| 14 | +from tests.unit.architecture.conftest import ( |
| 15 | + SRC_ORB, |
| 16 | + collect_python_files, |
| 17 | + extract_imports, |
| 18 | +) |
| 19 | + |
| 20 | +_TOP_PACKAGES = frozenset( |
| 21 | + [ |
| 22 | + "domain", |
| 23 | + "application", |
| 24 | + "infrastructure", |
| 25 | + "interface", |
| 26 | + "api", |
| 27 | + "cli", |
| 28 | + "providers", |
| 29 | + "config", |
| 30 | + "mcp", |
| 31 | + "monitoring", |
| 32 | + "sdk", |
| 33 | + ] |
| 34 | +) |
| 35 | + |
| 36 | +# Known cycles that exist in the current codebase. Each cycle is stored as a |
| 37 | +# frozenset of the participating package names (direction-independent, duplicate-free). |
| 38 | +# A detected cycle is "known" if its node-set is a subset of any known cycle node-set. |
| 39 | +_KNOWN_CYCLE_NODESETS: list[frozenset[str]] = [ |
| 40 | + frozenset({"api", "application", "config", "infrastructure"}), |
| 41 | + frozenset({"application", "config", "infrastructure"}), |
| 42 | + frozenset({"application", "config", "infrastructure", "cli"}), |
| 43 | + frozenset({"infrastructure", "cli"}), |
| 44 | + frozenset({"api", "application", "config", "infrastructure", "cli", "interface"}), |
| 45 | + frozenset({"application", "config", "infrastructure", "cli", "interface"}), |
| 46 | + frozenset({"cli", "interface"}), |
| 47 | + frozenset({"config", "infrastructure", "cli", "interface"}), |
| 48 | + frozenset({"infrastructure", "cli", "interface"}), |
| 49 | + frozenset({"application", "config", "infrastructure", "cli", "interface", "mcp", "sdk"}), |
| 50 | + frozenset({"infrastructure", "cli", "interface", "mcp", "sdk"}), |
| 51 | + frozenset({"config", "infrastructure", "cli", "interface", "monitoring"}), |
| 52 | + frozenset({"infrastructure", "cli", "interface", "monitoring"}), |
| 53 | + frozenset({"application", "config", "infrastructure", "cli", "interface", "providers"}), |
| 54 | + frozenset({"cli", "interface", "providers"}), |
| 55 | + frozenset({"config", "infrastructure", "cli", "interface", "providers"}), |
| 56 | + frozenset({"infrastructure", "cli", "interface", "providers"}), |
| 57 | + frozenset({"config", "infrastructure"}), |
| 58 | +] |
| 59 | + |
| 60 | + |
| 61 | +def _build_graph() -> dict[str, set[str]]: |
| 62 | + graph: dict[str, set[str]] = collections.defaultdict(set) |
| 63 | + for f in collect_python_files(SRC_ORB): |
| 64 | + parts = f.relative_to(SRC_ORB).parts |
| 65 | + if not parts: |
| 66 | + continue |
| 67 | + src_pkg = parts[0] |
| 68 | + if src_pkg not in _TOP_PACKAGES: |
| 69 | + continue |
| 70 | + for imp in extract_imports(f): |
| 71 | + if imp.startswith("orb."): |
| 72 | + rest = imp[4:].split(".")[0] |
| 73 | + if rest in _TOP_PACKAGES and rest != src_pkg: |
| 74 | + graph[src_pkg].add(rest) |
| 75 | + return dict(graph) |
| 76 | + |
| 77 | + |
| 78 | +def _find_cycles(graph: dict[str, set[str]]) -> list[list[str]]: |
| 79 | + cycles: list[list[str]] = [] |
| 80 | + visited: set[str] = set() |
| 81 | + rec_stack: set[str] = set() |
| 82 | + path: list[str] = [] |
| 83 | + seen_keys: set[tuple[str, ...]] = set() |
| 84 | + |
| 85 | + def dfs(node: str) -> None: |
| 86 | + visited.add(node) |
| 87 | + rec_stack.add(node) |
| 88 | + path.append(node) |
| 89 | + for neighbor in sorted(graph.get(node, [])): |
| 90 | + if neighbor not in visited: |
| 91 | + dfs(neighbor) |
| 92 | + elif neighbor in rec_stack: |
| 93 | + start = path.index(neighbor) |
| 94 | + cycle = path[start:] + [neighbor] |
| 95 | + key = tuple(sorted(set(cycle))) |
| 96 | + if key not in seen_keys: |
| 97 | + seen_keys.add(key) |
| 98 | + cycles.append(cycle) |
| 99 | + path.pop() |
| 100 | + rec_stack.discard(node) |
| 101 | + |
| 102 | + for node in sorted(graph.keys()): |
| 103 | + if node not in visited: |
| 104 | + dfs(node) |
| 105 | + return cycles |
| 106 | + |
| 107 | + |
| 108 | +def test_no_new_circular_dependencies() -> None: |
| 109 | + """No new circular dependencies may be introduced between top-level packages.""" |
| 110 | + graph = _build_graph() |
| 111 | + cycles = _find_cycles(graph) |
| 112 | + |
| 113 | + new_cycles = [] |
| 114 | + for cycle in cycles: |
| 115 | + cycle_nodeset = frozenset(cycle) # includes the repeated last node, frozenset dedupes |
| 116 | + is_known = any(cycle_nodeset <= known for known in _KNOWN_CYCLE_NODESETS) |
| 117 | + if not is_known: |
| 118 | + new_cycles.append(" -> ".join(cycle)) |
| 119 | + |
| 120 | + assert new_cycles == [], ( |
| 121 | + f"NEW circular dependencies detected between top-level packages:\n" |
| 122 | + + "\n".join(f" {c}" for c in new_cycles) |
| 123 | + ) |
0 commit comments