Answer architecture round 2: 0 🔴, 1 🟡 (0 misread); move task_bench's shared Side and Rust test-module scanner to the package top, rename figures.py to run_figures.py, check for cycles between a package's parts, and move REVIEW_LABEL into the backend protocol
CI / tests (pull_request) Successful in 5m13s

This commit is contained in:
2026-11-27 11:36:37 +01:00
parent b563a597f1
commit 7c7d43023b
23 changed files with 84 additions and 45 deletions
+51 -8
View File
@@ -31,6 +31,36 @@ def package_imports(bin_folder: pathlib.Path) -> dict[str, set[str]]:
return imported
def imported_names(module: pathlib.Path) -> list[str]:
"""The dotted name of each thing the file module imports, as `a.b.c` for `from a.b import c`. A deferred import inside a function counts."""
found: list[str] = []
for node in ast.walk(ast.parse(module.read_text(encoding="utf-8"))):
found += [alias.name for alias in node.names] if isinstance(node, ast.Import) else [f"{node.module}.{alias.name}" for alias in node.names] if isinstance(node, ast.ImportFrom) and node.level == 0 and node.module else []
return found
def part_imports(package: pathlib.Path) -> dict[str, set[str]]:
"""Each part of the package, with the other parts of it that its modules import, tests aside. A part is a module at the package's top, or a folder there with every module under it: a folder states that its modules belong together, so the top may not lean on a folder that leans on the top."""
imported: dict[str, set[str]] = {}
for module in package.rglob("*.py"):
if module.name.startswith("test_"):
continue
part = module.relative_to(package).parts[0].removesuffix(".py")
inside = [name.split(".")[1] for name in imported_names(module) if name.startswith(f"{package.name}.")]
imported.setdefault(part, set()).update(other for other in inside if other != part)
return imported
def part_cycles(bin_folder: pathlib.Path) -> dict[str, list[str]]:
"""Each package under bin_folder whose parts import in a cycle, with the parts along one."""
found = {}
for package in sorted(init.parent for init in bin_folder.glob("*/__init__.py")):
imported = part_imports(package)
if cycle(imported):
found[package.name] = cycle(imported)
return found
# The entry points that drive chunks. What two of them need lives in bin/chunk_driver/, so no other package imports one, its tests included.
DRIVERS = ("do_epic", "loop_claude")
@@ -62,18 +92,16 @@ def imports_of_test_modules(bin_folder: pathlib.Path) -> list[str]:
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 [f"{node.module}.{alias.name}" for alias in node.names] if isinstance(node, ast.ImportFrom) and node.level == 0 and node.module else []
for name in names:
top, *below = name.split(".")
tests = [part for part in below if part.startswith("test_")]
if tests and top != package:
found.append(f"{module.relative_to(bin_folder)} imports {name[:name.index(tests[0]) + len(tests[0])]}")
for name in imported_names(module):
top, *below = name.split(".")
tests = [part for part in below if part.startswith("test_")]
if tests and top != package:
found.append(f"{module.relative_to(bin_folder)} imports {name[:name.index(tests[0]) + len(tests[0])]}")
return sorted(set(found))
def cycle(imported: dict[str, set[str]]) -> list[str]:
"""One cycle among the packages, as the packages along it; empty where there is none."""
"""One cycle among the packages or parts that imported names, as those along it; empty where there is none."""
try:
graphlib.TopologicalSorter(imported).prepare()
except graphlib.CycleError as found:
@@ -135,6 +163,21 @@ class PackageCycles(unittest.TestCase):
})
self.assertEqual(package_imports(folder), {"a": {"b"}, "b": {"a"}, "c": set()})
def test_no_part_of_a_package_imports_one_that_imports_it_back(self):
self.assertEqual(part_cycles(ROOT / "bin"), {}, "move what both parts need into a module at the package's top that imports neither")
def test_reads_a_folder_of_a_package_as_one_part_and_a_module_at_its_top_as_another(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
tree(folder, {
"a/__init__.py": "", "a/main.py": "from a.kind.plant import x\nfrom a import shared\nimport b.kind\n",
"a/shared.py": "def f():\n\tfrom a.kind import plant\n",
"a/kind/__init__.py": "", "a/kind/plant.py": "import a.shared\nfrom a.kind.deep import other\n", "a/kind/deep/other.py": "import a.kind.plant\n",
"a/test_shared.py": "import a.main\n", "a/kind/test_plant.py": "import a.main\n",
})
self.assertEqual(part_imports(folder / "a"), {"__init__": set(), "main": {"kind", "shared"}, "shared": {"kind"}, "kind": {"shared"}})
self.assertEqual(sorted(cycle(part_imports(folder / "a"))[:2]), ["kind", "shared"])
def test_a_cycle_through_a_deferred_import_is_found(self):
self.assertEqual(sorted(cycle({"a": {"b"}, "b": {"a"}})[:2]), ["a", "b"])
self.assertEqual(cycle({"a": {"b"}, "b": set()}), [])