Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(c4): keep a container's own components in L3 when the repo has a root container (#2678)

Swati Ahuja308029efe758
diff --git a/packages/server/src/repowise/server/services/c4_builder/__init__.py b/packages/server/src/repowise/server/services/c4_builder/__init__.py
index aca731150..068d58441 100644
--- a/packages/server/src/repowise/server/services/c4_builder/__init__.py
+++ b/packages/server/src/repowise/server/services/c4_builder/__init__.py
@@ -229,14 +229,13 @@ async def build_l3(session: AsyncSession, repo_id: str, container_id_value: str)
)[0]
# Files outside this container map to their owning container so cross-
- # container edges show as component → other-container.
- file_to_box: dict[str, str] = dict(in_container_index)
- other_containers_index = await _file_to_container_map(
- session,
- repo_id,
- [c for c in containers if c.id != container.id],
- )
- file_to_box.update(other_containers_index)
+ # container edges show as component → other-container. Built from the
+ # all-container map so a root ("") container, which matches every path,
+ # cannot claim this container's files; component mappings go on last.
+ file_to_box: dict[str, str] = {
+ path: cid for path, cid in full_file_to_container.items() if cid != container.id
+ }
+ file_to_box.update(in_container_index)
file_to_external = await external_node_to_system_id(session, repo_id)
relations = await aggregate_relations(
diff --git a/tests/unit/server/test_c4.py b/tests/unit/server/test_c4.py
index 8be35239c..542d7db15 100644
--- a/tests/unit/server/test_c4.py
+++ b/tests/unit/server/test_c4.py
@@ -241,3 +241,53 @@ def test_the_batched_and_per_container_paths_agree_on_a_nested_container() -> No
siblings = tuple(r + "/" for r in roots if r != root)
per_container = _owned_by(node_id, root, siblings)
assert per_container is (batched == [root]), (node_id, root)
+
+
+async def test_l3_relations_with_root_container(client: AsyncClient, app) -> None:
+ """A root manifest adds a catch-all ``""`` container. Its file mapping must
+ not overwrite the focused container's component mapping, or every L3 edge
+ inside a nested container is lost."""
+ repo = await create_test_repo(client)
+ repo_id = repo["id"]
+ async with app.state.session_factory() as session:
+ files = [
+ "pyproject.toml",
+ "scripts/run.py",
+ "packages/core/pyproject.toml",
+ "packages/core/parser/lex.py",
+ "packages/core/graph/build.py",
+ ]
+ await batch_upsert_graph_nodes(
+ session,
+ repo_id,
+ [
+ {"node_id": f, "node_type": "file", "language": "python", "symbol_count": 1}
+ for f in files
+ ],
+ )
+ await batch_upsert_graph_edges(session, repo_id, [
+ {"source_node_id": "packages/core/graph/build.py", "target_node_id": "packages/core/parser/lex.py", "edge_type": "imports"},
+ {"source_node_id": "scripts/run.py", "target_node_id": "packages/core/graph/build.py", "edge_type": "imports"},
+ ])
+ await session.commit()
+
+ l2 = (await client.get(f"/api/graph/{repo_id}/c4/l2")).json()
+ assert {c["path"] for c in l2["containers"]} == {"", "packages/core"}
+
+ body = (
+ await client.get(
+ f"/api/graph/{repo_id}/c4/l3", params={"container_id": "pkg:packages/core"}
+ )
+ ).json()
+ comp_ids = {c["id"] for c in body["components"]}
+ edges = {(r["source_id"], r["target_id"]) for r in body["relations"]}
+ # The in-container edge survives as component → component.
+ assert any(s in comp_ids and t in comp_ids and s != t for s, t in edges)
+ # The root container's file still shows as an inbound container edge.
+ assert any(s == "pkg:." and t in comp_ids for s, t in edges)
+
+ # And the root container's own L3 sees the outbound edge to packages/core.
+ root = (
+ await client.get(f"/api/graph/{repo_id}/c4/l3", params={"container_id": "pkg:."})
+ ).json()
+ assert ("pkg:packages/core" in {r["target_id"] for r in root["relations"]})