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(stats): one file count that leaves out import-target graph nodes (#4373)

Raghav Chamadiya060d342fb5b9
diff --git a/docs/reference/COMPUTED_GLOSSARY.md b/docs/reference/COMPUTED_GLOSSARY.md
index 758495701..644c017a0 100644
--- a/docs/reference/COMPUTED_GLOSSARY.md
+++ b/docs/reference/COMPUTED_GLOSSARY.md
@@ -72,6 +72,7 @@ workspace overlays, MCP responses, and CLI output.
| Symbol node | Graph node for an extracted symbol. | `GraphBuilder.add_file()` | `{node_type: "symbol", kind: "function", name: "main"}` |
| External node | Node for third-party or unresolvable dependencies. | Import resolution paths | `external:react` |
| Synthetic module symbol | Symbol node for top-level calls in a file. | `GraphBuilder.add_file()` | `src/app.py::__module__` |
+| File count | The "files" figure on `repowise status`, the dashboard, Stats and workspace views: one per file the index read, in any language. Import-target nodes (third-party packages, and paths an import names that traversal never parsed) are not counted. Health's "code files" is the code-language subset of these, inside the health exclude rules. | `count_files()` | `6,363 files` |
| Symbol count | The "symbols" figure on `repowise status`, the dashboard, workspace views and `get_overview`: one per distinct symbol id in the symbol index. An overload set counts once, and the synthetic module symbol is not counted. | `count_symbols()` | `76,924 symbols` |
| `defines` edge | File-to-symbol containment. | `GraphBuilder.add_file()` | `src/app.py -> src/app.py::main` |
| `imports` edge | File-to-file import relationship. | `GraphBuilder.build()` | `src/app.py -> src/settings.py` |
diff --git a/packages/cli/src/repowise/cli/commands/status_cmd.py b/packages/cli/src/repowise/cli/commands/status_cmd.py
index feffdc1ff..ddd222c2e 100644
--- a/packages/cli/src/repowise/cli/commands/status_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/status_cmd.py
@@ -61,7 +61,6 @@ def _query_repo_counts(repo_path: Path) -> tuple[int, int]:
"""Return ``(file_count, symbol_count)`` from a repo's DB."""
async def _query() -> tuple[int, int]:
- from sqlalchemy import func as sa_func
from sqlalchemy import select as sa_select
from repowise.core.persistence import (
@@ -69,8 +68,8 @@ def _query_repo_counts(repo_path: Path) -> tuple[int, int]:
create_session_factory,
get_session,
)
- from repowise.core.persistence.crud import count_symbols
- from repowise.core.persistence.models import GraphNode, Repository
+ from repowise.core.persistence.crud import count_files, count_symbols
+ from repowise.core.persistence.models import Repository
url = get_db_url_for_repo(repo_path)
# Without this a store one repowise older reports 0 files / 0 pages:
@@ -88,16 +87,8 @@ def _query_repo_counts(repo_path: Path) -> tuple[int, int]:
if repo_id is None:
return 0, 0
- file_count_result = await session.execute(
- sa_select(sa_func.count())
- .select_from(GraphNode)
- .where(
- GraphNode.repository_id == repo_id,
- GraphNode.node_type == "file",
- )
- )
return (
- file_count_result.scalar_one(),
+ await count_files(session, repo_id),
await count_symbols(session, repo_id),
)
finally:
diff --git a/packages/core/src/repowise/core/analysis/external_systems/links.py b/packages/core/src/repowise/core/analysis/external_systems/links.py
index 1acbc4950..f695b2504 100644
--- a/packages/core/src/repowise/core/analysis/external_systems/links.py
+++ b/packages/core/src/repowise/core/analysis/external_systems/links.py
@@ -19,6 +19,12 @@ from .records import field, package_key
#: Graph nodes standing for a third-party package carry this ``node_id`` prefix.
EXTERNAL_NODE_PREFIX = "external:"
+#: ``language`` of a graph "file" row that is an import target, not a file the
+#: index read: a third-party package, or a path an edge names that traversal
+#: never parsed (a directory, an excluded or generated file). Traversal drops
+#: unknown-language files, so no parsed file carries either tag.
+IMPORT_TARGET_LANGUAGES = ("external", "unknown")
+
@dataclass(frozen=True, slots=True)
class ExternalSystemLink:
diff --git a/packages/core/src/repowise/core/generation/editor_files/data.py b/packages/core/src/repowise/core/generation/editor_files/data.py
index bfa3d98d2..6c5f1e688 100644
--- a/packages/core/src/repowise/core/generation/editor_files/data.py
+++ b/packages/core/src/repowise/core/generation/editor_files/data.py
@@ -36,7 +36,6 @@ class TechStackItem:
class KeyModule:
name: str # display name, e.g. "src/api"
purpose: str # short description (~80 chars)
- file_count: int
owner: str | None
diff --git a/packages/core/src/repowise/core/generation/editor_files/fetcher.py b/packages/core/src/repowise/core/generation/editor_files/fetcher.py
index f2cbe8d23..fbe93f458 100644
--- a/packages/core/src/repowise/core/generation/editor_files/fetcher.py
+++ b/packages/core/src/repowise/core/generation/editor_files/fetcher.py
@@ -170,7 +170,7 @@ class EditorFileDataFetcher:
# Fetch module pages sorted by their target_path (proxy for PageRank
# ordering is applied by joining with graph_nodes).
pages_result = await self._session.execute(
- select(Page, GraphNode.pagerank, GraphNode.symbol_count)
+ select(Page, GraphNode.pagerank)
.join(
GraphNode,
(GraphNode.repository_id == Page.repository_id)
@@ -194,7 +194,7 @@ class EditorFileDataFetcher:
owner_map = await self._get_owners_for_paths(target_paths)
modules: list[KeyModule] = []
- for page, _pagerank, symbol_count in rows:
+ for page, _pagerank in rows:
skip_preamble = _is_structure_only(page)
purpose = _extract_sentences(
page.content or "", max_sentences=1, skip_preamble=skip_preamble
@@ -206,7 +206,6 @@ class EditorFileDataFetcher:
KeyModule(
name=page.target_path,
purpose=purpose,
- file_count=symbol_count or 0,
owner=owner_map.get(page.target_path),
)
)
diff --git a/packages/core/src/repowise/core/persistence/crud/external_systems.py b/packages/core/src/repowise/core/persistence/crud/external_systems.py
index 21f89ad0e..f790f9512 100644
--- a/packages/core/src/repowise/core/persistence/crud/external_systems.py
+++ b/packages/core/src/repowise/core/persistence/crud/external_systems.py
@@ -8,11 +8,12 @@ from __future__ import annotations
from typing import Any
-from sqlalchemy import func, select
+from sqlalchemy import ColumnElement, and_, func, select
from sqlalchemy.ext.asyncio import AsyncSession
from ...analysis.external_systems.links import (
EXTERNAL_NODE_PREFIX,
+ IMPORT_TARGET_LANGUAGES,
declaration_name_candidates,
)
from ..models import (
@@ -280,6 +281,28 @@ async def count_symbols(session: AsyncSession, repository_id: str) -> int:
return int(n or 0)
+def indexed_file() -> ColumnElement[bool]:
+ """Graph rows for files the index read, the population every "files" count uses.
+
+ ``node_type == "file"`` alone also holds import targets: third-party
+ packages and paths an edge names that traversal never parsed.
+ """
+ return and_(
+ GraphNode.node_type == "file",
+ GraphNode.language.notin_(IMPORT_TARGET_LANGUAGES),
+ )
+
+
+async def count_files(session: AsyncSession, repository_id: str) -> int:
+ """Indexed files in one repository: the figure every surface calls "files"."""
+ n = await session.scalar(
+ select(func.count(GraphNode.id)).where(
+ GraphNode.repository_id == repository_id, indexed_file()
+ )
+ )
+ return int(n or 0)
+
+
# Chunk size for the scoped existence SELECT — stays under SQLite's
# host-parameter limit when a wide catch-up update touches many files.
_SYMBOL_RECONCILE_CHUNK = 400
diff --git a/packages/core/src/repowise/core/stats_highlights.py b/packages/core/src/repowise/core/stats_highlights.py
index a0d582f83..ccace68d1 100644
--- a/packages/core/src/repowise/core/stats_highlights.py
+++ b/packages/core/src/repowise/core/stats_highlights.py
@@ -23,6 +23,7 @@ from datetime import UTC, datetime, timedelta
from itertools import pairwise
from typing import Any
+from repowise.core.analysis.external_systems.links import IMPORT_TARGET_LANGUAGES
from repowise.core.test_paths import is_test_related_path
Row = Mapping[str, Any]
@@ -94,13 +95,9 @@ def _offset_minutes(raw: Any) -> int | None:
return None
-def _is_external(node: Row) -> bool:
- node_id = str(node.get("node_id") or node.get("id") or "")
- return (
- node.get("language") == "external"
- or node_id.startswith("external:")
- or node.get("external_system_id") is not None
- )
+def _is_import_target(node: Row) -> bool:
+ """The row form of ``crud.indexed_file``, negated: not a file the index read."""
+ return node.get("language") in IMPORT_TARGET_LANGUAGES
# ---------------------------------------------------------------------------
@@ -125,7 +122,7 @@ def file_mix(file_nodes: Iterable[Row]) -> dict[str, Any]:
code_paths: list[str] = []
for node in file_nodes:
lang = node.get("language")
- if not lang or _is_external(node):
+ if not lang or _is_import_target(node):
continue
spec = REGISTRY.get(lang)
if spec is not None and spec.is_code:
@@ -152,12 +149,12 @@ def build_scale(
) -> dict[str, Any]:
"""Counts and NLOC, plus the :func:`file_mix` languages and module count.
- ``file_nodes`` are graph file nodes; external dependency nodes are dropped
- so the file count describes the repo. ``symbol_count`` is
+ ``file_nodes`` are graph file nodes; import targets are dropped so the
+ file count is ``crud.count_files``. ``symbol_count`` is
``crud.count_symbols``, the figure every other surface shows.
"""
file_nodes = list(file_nodes)
- file_count = sum(1 for node in file_nodes if not _is_external(node))
+ file_count = sum(1 for node in file_nodes if not _is_import_target(node))
total_nloc = 0
test_nloc = 0
diff --git a/packages/server/src/repowise/server/routers/_repo_summary.py b/packages/server/src/repowise/server/routers/_repo_summary.py
index abad6a15b..31bd53d56 100644
--- a/packages/server/src/repowise/server/routers/_repo_summary.py
+++ b/packages/server/src/repowise/server/routers/_repo_summary.py
@@ -13,7 +13,7 @@ from sqlalchemy import case, func, select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession
-from repowise.core.persistence.crud import count_symbols_by_repo
+from repowise.core.persistence.crud import count_symbols_by_repo, indexed_file
from repowise.core.persistence.crud.analysis.shown import ungated
from repowise.core.persistence.models import (
DeadCodeFinding,
@@ -35,14 +35,15 @@ def _fresh_case(column: Any, value: Any) -> Any:
async def _file_figures(session: AsyncSession) -> _Figures:
- # `graph_nodes` also holds symbol rows, so every count is scoped to files.
+ # `graph_nodes` also holds symbol and import-target rows; `indexed_file` is
+ # the population `crud.count_files` counts.
result = await session.execute(
select(
GraphNode.repository_id,
func.count(GraphNode.id),
_fresh_case(GraphNode.is_entry_point, True),
)
- .where(GraphNode.node_type == "file")
+ .where(indexed_file())
.group_by(GraphNode.repository_id)
)
return [
diff --git a/packages/server/src/repowise/server/routers/repos.py b/packages/server/src/repowise/server/routers/repos.py
index 3ddde678d..91cad0c4b 100644
--- a/packages/server/src/repowise/server/routers/repos.py
+++ b/packages/server/src/repowise/server/routers/repos.py
@@ -385,15 +385,7 @@ async def get_repo_stats(
"""Get aggregate stats for a repository."""
await _get_repo_or_404(session, repo_id)
- # File nodes only: `graph_nodes` also holds symbol rows.
- file_count = await _scalar_or(
- session,
- select(func.count(GraphNode.id)).where(
- GraphNode.repository_id == repo_id,
- GraphNode.node_type == "file",
- ),
- 0,
- )
+ file_count = await crud.count_files(session, repo_id)
symbol_count = await crud.count_symbols(session, repo_id)
entry_point_count = await _scalar_or(
session,
diff --git a/packages/server/src/repowise/server/routers/stats.py b/packages/server/src/repowise/server/routers/stats.py
index 7b9b5689d..4e618c0a3 100644
--- a/packages/server/src/repowise/server/routers/stats.py
+++ b/packages/server/src/repowise/server/routers/stats.py
@@ -179,7 +179,6 @@ async def stats_highlights(
select(
GraphNode.node_id,
GraphNode.language,
- GraphNode.external_system_id,
).where(GraphNode.repository_id == repo_id, GraphNode.node_type == "file"),
)
metrics = await _rows(
diff --git a/packages/server/src/repowise/server/routers/workspace.py b/packages/server/src/repowise/server/routers/workspace.py
index 03fd1b67a..f488c2a3b 100644
--- a/packages/server/src/repowise/server/routers/workspace.py
+++ b/packages/server/src/repowise/server/routers/workspace.py
@@ -13,6 +13,7 @@ from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException, Query, Request
+from repowise.core.analysis.external_systems.links import IMPORT_TARGET_LANGUAGES
from repowise.core.docs_mode import resolve_docs_mode
from repowise.core.workspace import reads
from repowise.core.workspace.update import synced_at_iso
@@ -67,6 +68,13 @@ def _require_workspace(ws_config: object) -> None:
raise HTTPException(status_code=404, detail="Not running in workspace mode")
+# The raw-sqlite form of ``crud.indexed_file``: these readers open each repo's
+# store directly, without a session.
+_INDEXED_FILE_SQL = "node_type = 'file' AND language NOT IN ({})".format(
+ ", ".join(f"'{lang}'" for lang in IMPORT_TARGET_LANGUAGES)
+)
+
+
def _query_top_language(db_path: Path) -> str:
"""Return the most common language across graph_nodes in a repo's wiki.db."""
if not db_path.exists():
@@ -75,7 +83,7 @@ def _query_top_language(db_path: Path) -> str:
with sqlite3.connect(str(db_path)) as conn:
row = conn.execute(
"SELECT language, COUNT(*) AS cnt FROM graph_nodes "
- "WHERE node_type = 'file' AND language IS NOT NULL AND language != '' "
+ f"WHERE {_INDEXED_FILE_SQL} AND language IS NOT NULL AND language != '' "
"GROUP BY language ORDER BY cnt DESC LIMIT 1"
).fetchone()
return row[0] if row else "unknown"
@@ -158,8 +166,8 @@ def _query_repo_stats(db_path: Path) -> dict:
result["repo_id"] = row[0]
result["indexed_at"] = synced_at_iso(row[1])
- # file count (graph_nodes)
- row = c.execute("SELECT COUNT(*) FROM graph_nodes WHERE node_type = 'file'").fetchone()
+ # The raw-sqlite form of crud.count_files.
+ row = c.execute(f"SELECT COUNT(*) FROM graph_nodes WHERE {_INDEXED_FILE_SQL}").fetchone()
result["file_count"] = row[0] if row else 0
# The raw-sqlite form of crud.count_symbols: one store per repo here.
diff --git a/packages/server/src/repowise/server/services/overview_payload.py b/packages/server/src/repowise/server/services/overview_payload.py
index 0cc49acab..5dc9a178b 100644
--- a/packages/server/src/repowise/server/services/overview_payload.py
+++ b/packages/server/src/repowise/server/services/overview_payload.py
@@ -89,12 +89,7 @@ async def docs_split(session: AsyncSession, repo_id: str) -> dict:
async def stat_strip(session: AsyncSession, repo_id: str) -> dict:
"""Graph, docs, dead-export and hotspot counts, in payload order."""
- file_count = await _count(
- session,
- select(func.count(GraphNode.id)).where(
- GraphNode.repository_id == repo_id, GraphNode.node_type == "file"
- ),
- )
+ file_count = await crud.count_files(session, repo_id)
symbol_count = await crud.count_symbols(session, repo_id)
entry_point_count = await _count(
session,
@@ -169,7 +164,7 @@ async def language_mix(session: AsyncSession, repo_id: str) -> dict:
that, count languages in SQL and fetch only code paths.
"""
node_rows = await session.execute(
- select(GraphNode.node_id, GraphNode.language, GraphNode.external_system_id).where(
+ select(GraphNode.node_id, GraphNode.language).where(
GraphNode.repository_id == repo_id, GraphNode.node_type == "file"
)
)
diff --git a/tests/unit/generation/test_editor_file_sections.py b/tests/unit/generation/test_editor_file_sections.py
index e20fb13b9..c6bc7ee2c 100644
--- a/tests/unit/generation/test_editor_file_sections.py
+++ b/tests/unit/generation/test_editor_file_sections.py
@@ -86,7 +86,7 @@ def _full_data(**kw) -> EditorFileData:
"indexed_at": "2026-10-19",
"indexed_commit": "a1b2c3d",
"architecture_summary": "A demo service.",
- "key_modules": [KeyModule(name="src/api", purpose="HTTP layer", file_count=4, owner=None)],
+ "key_modules": [KeyModule(name="src/api", purpose="HTTP layer", owner=None)],
"entry_points": ["src/main.py"],
"hotspots": [
HotspotFile(
diff --git a/tests/unit/generation/test_kg_claude_md_onboarding.py b/tests/unit/generation/test_kg_claude_md_onboarding.py
index b4c9deb06..686580062 100644
--- a/tests/unit/generation/test_kg_claude_md_onboarding.py
+++ b/tests/unit/generation/test_kg_claude_md_onboarding.py
@@ -97,8 +97,8 @@ class TestClaudeMdKGSection:
tmpl = jinja_env.get_template("claude_md.j2")
data = self._data(
key_modules=[
- KeyModule(name="src/api", purpose="API layer", file_count=4, owner=None),
- KeyModule(name="src/core", purpose="", file_count=9, owner=None),
+ KeyModule(name="src/api", purpose="API layer", owner=None),
+ KeyModule(name="src/core", purpose="", owner=None),
]
)
rendered = tmpl.render(data=data)
diff --git a/tests/unit/server/test_repos.py b/tests/unit/server/test_repos.py
index 90041cd0c..1c50dc467 100644
--- a/tests/unit/server/test_repos.py
+++ b/tests/unit/server/test_repos.py
@@ -236,13 +236,20 @@ async def test_stats_file_count_excludes_symbol_nodes(client: AsyncClient, sessi
)
)
session.add_all(make_wiki_symbol(repo.id, f"src/mod{i}.py", f"sym{j}") for j in range(5))
+ # Import targets are file rows too: a third-party package and a path an
+ # import names that traversal never parsed.
+ for node_id, language in [("external:requests", "external"), ("src/generated", "unknown")]:
+ session.add(
+ GraphNode(repository_id=repo.id, node_id=node_id, node_type="file", language=language)
+ )
await session.commit()
data = (await client.get(f"/api/repos/{repo.id}/stats")).json()
overview = (await client.get(f"/api/repos/{repo.id}/overview-summary")).json()
symbols = (await client.get("/api/symbols", params={"repo_id": repo.id})).json()
- assert data["file_count"] == 2 # not 14
+ assert data["file_count"] == 2 # not 16, not 4
+ assert overview["stats"]["file_count"] == 2
# One figure, the symbol index's row count: not 12 symbol nodes, not 12 emissions.
assert data["symbol_count"] == 10
assert overview["stats"]["symbol_count"] == 10
diff --git a/tests/unit/server/test_repos_summary.py b/tests/unit/server/test_repos_summary.py
index 74fdf2317..2004d1415 100644
--- a/tests/unit/server/test_repos_summary.py
+++ b/tests/unit/server/test_repos_summary.py
@@ -65,6 +65,15 @@ async def _seed(session, repo_id: str, *, files: int, symbols_per_file: int = 3)
session.add_all(
make_wiki_symbol(repo_id, f"src/mod{i}.py", f"sym{j}") for j in range(symbols_per_file)
)
+ # An import target per file, also stored as a file row: not a file the index read.
+ session.add(
+ GraphNode(
+ repository_id=repo_id,
+ node_id=f"external:pkg{i}",
+ node_type="file",
+ language="external",
+ )
+ )
session.add(
GitMetadata(
repository_id=repo_id,
@@ -137,7 +146,7 @@ async def test_file_count_excludes_symbol_nodes(client: AsyncClient, session) ->
assert resp.status_code == 200
row = resp.json()["repos"][0]
- assert row["file_count"] == 4 # not 20
+ assert row["file_count"] == 4 # not 24: symbol and import-target rows are not files
assert row["symbol_count"] == 12 # not 16: `__module__` nodes are not symbols
assert row["entry_point_count"] == 1
diff --git a/tests/unit/server/test_stats_superlatives.py b/tests/unit/server/test_stats_superlatives.py
index 44d8ea42b..aca9675a4 100644
--- a/tests/unit/server/test_stats_superlatives.py
+++ b/tests/unit/server/test_stats_superlatives.py
@@ -111,6 +111,7 @@ def test_scale_counts_code_languages_and_skips_external_nodes() -> None:
{"node_id": "b.ts", "language": "typescript", "symbol_count": 2},
{"node_id": "c.json", "language": "json", "symbol_count": 0},
{"node_id": "external:requests", "language": "external", "symbol_count": 0},
+ {"node_id": "src/generated", "language": "unknown", "symbol_count": 0},
]
metrics = [
{"nloc": 100, "module": "a", "is_test": False},
@@ -121,6 +122,7 @@ def test_scale_counts_code_languages_and_skips_external_nodes() -> None:
assert scale["symbol_count"] == 4 # passed in, not the per-file sum
assert scale["language_count"] == 2
assert {lang["language"] for lang in scale["languages"]} == {"python", "typescript"}
+ assert [lang["language"] for lang in scale["docs_config_languages"]] == ["json"]
assert scale["total_nloc"] == 140
assert scale["test_nloc"] == 40
diff --git a/tests/unit/server/test_workspace_router.py b/tests/unit/server/test_workspace_router.py
index d1f97c884..74d982911 100644
--- a/tests/unit/server/test_workspace_router.py
+++ b/tests/unit/server/test_workspace_router.py
@@ -1024,6 +1024,9 @@ class TestQueryRepoStats:
("src/b.py::qux", "symbol", "python"),
("src/b.py::quux", "symbol", "python"),
("src/c.py::corge", "symbol", "python"),
+ # Import targets: a package and a path traversal never parsed.
+ ("external:os", "file", "external"),
+ ("src/generated", "file", "unknown"),
],
)
@@ -1061,6 +1064,21 @@ class TestQueryRepoStats:
assert top_language == "python"
+ def test_top_language_ignores_import_targets(self, tmp_path: Path) -> None:
+ """A repo importing more packages than it has files is not written in "external"."""
+ db_path = tmp_path / ".repowise" / "wiki.db"
+ self._make_wiki_db(
+ db_path,
+ rows=[],
+ graph_nodes=[
+ ("src/a.py", "file", "python"),
+ ("external:os", "file", "external"),
+ ("external:sys", "file", "external"),
+ ],
+ )
+
+ assert workspace._query_top_language(db_path) == "python"
+
# ---------------------------------------------------------------------------
# GET /api/workspace/system-graph + /diagnostics