Answer architecture round 2: 0 🔴, 3 🟡 (0 misread); the cycle's and the chunk run's suites sit beside their modules, and the claim rule and the model override have one home each
CI / tests (pull_request) Successful in 3m23s

This commit is contained in:
2026-11-19 11:29:32 +01:00
parent b8595db647
commit df210aaeca
31 changed files with 807 additions and 657 deletions
+37 -8
View File
@@ -10,17 +10,24 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from repo_checks import ROOT
def imported_by(module: pathlib.Path) -> set[str]:
"""The top-level name of each module that the file module imports. A deferred import inside a function counts: it only hides a cycle."""
found: set[str] = set()
for node in ast.walk(ast.parse(module.read_text(encoding="utf-8"))):
names = [alias.name for alias in node.names] if isinstance(node, ast.Import) else [node.module or ""] if isinstance(node, ast.ImportFrom) and node.level == 0 else []
found.update(name.split(".")[0] for name in names)
return found
def package_imports(bin_folder: pathlib.Path) -> dict[str, set[str]]:
"""Each package under bin_folder, with the other packages its modules import, tests aside. A deferred import inside a function counts: it only hides a cycle."""
"""Each package under bin_folder, with the other packages its modules import, tests aside."""
packages = {init.parent.name for init in bin_folder.glob("*/__init__.py")}
imported: dict[str, set[str]] = {package: set() for package in packages}
for package in packages:
for module in (bin_folder / package).rglob("*.py"):
if module.name.startswith("test_"):
continue
for node in ast.walk(ast.parse(module.read_text(encoding="utf-8"))):
names = [alias.name for alias in node.names] if isinstance(node, ast.Import) else [node.module or ""] if isinstance(node, ast.ImportFrom) and node.level == 0 else []
imported[package].update(top for name in names if (top := name.split(".")[0]) in packages and top != package)
imported[package].update(top for top in imported_by(module) if top in packages and top != package)
return imported
@@ -33,10 +40,21 @@ def driver_imports(bin_folder: pathlib.Path, drivers: tuple[str, ...]) -> list[s
found = []
for module in sorted(bin_folder.rglob("*.py")):
package = module.relative_to(bin_folder).parts[0]
for node in ast.walk(ast.parse(module.read_text(encoding="utf-8"))):
names = [alias.name for alias in node.names] if isinstance(node, ast.Import) else [node.module or ""] if isinstance(node, ast.ImportFrom) and node.level == 0 else []
found += [f"{module.relative_to(bin_folder)} imports {top}" for name in names if (top := name.split(".")[0]) in drivers and top != package]
return sorted(set(found))
found += [f"{module.relative_to(bin_folder)} imports {top}" for top in imported_by(module) if top in drivers and top != package]
return sorted(found)
# The packages a package may not import, its tests included, since they are the layer above it: chunk_driver drives chunks through agent_cycle.
ABOVE = {"agent_cycle": ("chunk_driver",)}
def upward_imports(bin_folder: pathlib.Path, above: dict[str, tuple[str, ...]]) -> list[str]:
"""Each module under bin_folder that imports a package above names as above its own, tests included, as `<module> imports <package>`."""
found = []
for package, banned in above.items():
for module in sorted((bin_folder / package).rglob("*.py")):
found += [f"{module.relative_to(bin_folder)} imports {top}" for top in imported_by(module) if top in banned]
return sorted(found)
def cycle(imported: dict[str, set[str]]) -> list[str]:
@@ -55,6 +73,17 @@ class PackageCycles(unittest.TestCase):
def test_no_package_imports_a_driver_of_chunks(self):
self.assertEqual(driver_imports(ROOT / "bin", DRIVERS), [], "move what the importer needs into bin/chunk_driver/, or a test double into the package whose code it stands in for")
def test_no_package_imports_the_layer_above_it(self):
self.assertEqual(upward_imports(ROOT / "bin", ABOVE), [], "pass the value in from the layer above, or in a test write it as a literal")
def test_an_import_of_the_layer_above_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
for package, name, source in (("low", "mod.py", "import other\n"), ("low", "test_mod.py", "from high.mod import x\n"), ("low", "run.py", "def f():\n\timport high\n"), ("high", "mod.py", "import low\n")):
(folder / package).mkdir(exist_ok=True)
(folder / package / name).write_text(source)
self.assertEqual(upward_imports(folder, {"low": ("high",)}), ["low/run.py imports high", "low/test_mod.py imports high"])
def test_a_driver_imported_from_another_package_is_found_in_tests_too(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)