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.

feat(knowledge-graph): show the layer-naming prompt each layer's files by folder (#4206)

Raghav Chamadiya53384647cbaf
diff --git a/packages/core/src/repowise/core/generation/knowledge_graph.py b/packages/core/src/repowise/core/generation/knowledge_graph.py
index ece49b8a8..ccd30415c 100644
--- a/packages/core/src/repowise/core/generation/knowledge_graph.py
+++ b/packages/core/src/repowise/core/generation/knowledge_graph.py
@@ -10,6 +10,7 @@ the LLM to:
from __future__ import annotations
import json
+from collections import Counter
from typing import Any
import structlog
@@ -197,6 +198,7 @@ async def _enrich_layers(
"id": layer.get("id", ""),
"heuristic_label": layer["name"],
"file_count": len(file_paths),
+ "folders": _folder_breakdown(file_paths),
"top_files": top_files,
})
@@ -258,6 +260,22 @@ def _dedupe_layer_names(layers: list[dict]) -> None:
seen.add(name.casefold())
+_FOLDER_DEPTH = 2
+_TOP_FOLDERS = 5
+
+
+def _folder_breakdown(file_paths: list[str]) -> list[tuple[str, int]]:
+ """``(folder, file count)`` for a layer's largest folders, two segments deep.
+
+ The top files are ranked by PageRank, so a layer that is mostly one area
+ but headed by another reads as the head; the counts show the majority.
+ """
+ counts = Counter(
+ "/".join(p.split("/")[:-1][:_FOLDER_DEPTH]) or "(root)" for p in file_paths
+ )
+ return counts.most_common(_TOP_FOLDERS)
+
+
def _build_layer_naming_prompt(
batch_context: list[dict],
tech_stack: list[dict],
@@ -292,6 +310,11 @@ def _build_layer_naming_prompt(
lines.append(f"\n--- Community \"{ctx['id']}\" ---")
lines.append(f"Heuristic label: {ctx['heuristic_label']}")
lines.append(f"File count: {ctx['file_count']}")
+ folders = ctx.get("folders") or []
+ if folders:
+ shown = ", ".join(f"{folder} {n}" for folder, n in folders)
+ rest = ctx["file_count"] - sum(n for _, n in folders)
+ lines.append(f"Files by folder: {shown}" + (f", other folders {rest}" if rest else ""))
lines.append(f"Top files: {', '.join(ctx['top_files'][:10])}")
lines.append("")
diff --git a/tests/unit/generation/test_knowledge_graph_enrichment.py b/tests/unit/generation/test_knowledge_graph_enrichment.py
index 31e752d54..fe59bd7fe 100644
--- a/tests/unit/generation/test_knowledge_graph_enrichment.py
+++ b/tests/unit/generation/test_knowledge_graph_enrichment.py
@@ -98,6 +98,34 @@ class TestEnrichLayers:
assert result.layers[0]["description"] == "Handles data flow"
assert result.layers[1]["name"] == "CLI Interface"
+ @pytest.mark.asyncio
+ async def test_naming_prompt_counts_each_layers_files_by_folder(self):
+ layers = [
+ {
+ "id": "layer:config",
+ "name": "config",
+ "description": "",
+ "nodeIds": [
+ "file:packages/web/a.tsx",
+ "file:packages/web/b.tsx",
+ "file:.github/workflows/ci.yml",
+ "file:README.md",
+ ],
+ },
+ {"id": "layer:cli", "name": "cli", "description": "", "nodeIds": ["file:src/main.py"]},
+ ]
+ llm = _make_llm_client('{"layers": []}')
+ await enrich_knowledge_graph(
+ _make_kg_skeleton(layers=layers), llm, _make_graph_builder(), _make_repo_structure(), []
+ )
+ prompt = llm.generate.call_args_list[0].args[1]
+ assert "Files by folder: packages/web 2, .github/workflows 1, (root) 1" in prompt
+ # A single-folder layer keeps its one folder and the existing fields.
+ cli = prompt.split('Community "layer:cli"')[1]
+ assert "File count: 1" in cli
+ assert "Files by folder: src 1\n" in cli
+ assert "Top files: src/main.py" in cli
+
@pytest.mark.asyncio
async def test_positional_index_responses_are_ignored(self):
# Regression: a model answering with list positions instead of layer