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(context): cap one file's reference sites so a mock-heavy test cannot flood the list (#3183)

Raghav Chamadiya5f3b0a46a14c
diff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.md
index 951971f9d..2680d51ba 100644
--- a/docs/agent/MCP_TOOLS.md
+++ b/docs/agent/MCP_TOOLS.md
@@ -178,6 +178,7 @@ A file's compact symbol list holds its top 15 symbols: types (classes, interface
- more than 200 sites, a stale call line, an unreadable file, or an uncommitted edit outside the scanned files
- a rename on import, export or destructure, a default export, or a CommonJS export
- a dynamic use the graph recorded; access built from strings is otherwise not seen
+- more than 3 plain `reference` sites in one file: the rest are counted in `sites_omitted_by_file`, recoverable from `_meta.omitted`
```
get_context(targets=["src/auth/middleware.ts", "src/api/routes.ts"], include=["callers"])
diff --git a/packages/server/src/repowise/server/mcp_server/_edit_sites.py b/packages/server/src/repowise/server/mcp_server/_edit_sites.py
index 24d99e880..c54dfcb1e 100644
--- a/packages/server/src/repowise/server/mcp_server/_edit_sites.py
+++ b/packages/server/src/repowise/server/mcp_server/_edit_sites.py
@@ -13,6 +13,7 @@ from __future__ import annotations
import asyncio
import json
import re
+from collections import Counter
from itertools import islice
from pathlib import Path
from typing import Any
@@ -24,11 +25,16 @@ from repowise.core.fs_walk import PRUNED_DIRS, iter_glob
from repowise.core.ingestion.languages.registry import REGISTRY
from repowise.core.ingestion.symbol_identity import id_segment_name
from repowise.core.persistence.models import GraphEdge, GraphNode
+from repowise.core.test_paths import is_test_related_path
+from repowise.server.mcp_server._budget import OmissionCollector, cap_collection
from repowise.server.mcp_server._helpers import read_repo_file_text
from repowise.server.mcp_server._meta import _working_tree_dirty_paths, uncommitted_targets
MAX_SITES = 200
MAX_TEXT_CHARS = 160
+# Plain mentions kept per file when a collector can hold the rest: a mock-heavy
+# test can name a symbol hundreds of times and bury its real callers.
+MAX_REFERENCES_PER_FILE = 3
# Ceiling on files read per call; past it the set is reported incomplete
# rather than read without bound.
MAX_FILES = 300
@@ -151,6 +157,19 @@ def _scan(
return sites, unreadable, stale, renamed, is_default
+def _cap_references_per_file(sites: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ """*sites* with plain references past the per-file cap dropped; other kinds always stay."""
+ seen: Counter[str] = Counter()
+ kept: list[dict[str, Any]] = []
+ for site in sites:
+ if site["kind"] == "reference":
+ seen[site["path"]] += 1
+ if seen[site["path"]] > MAX_REFERENCES_PER_FILE:
+ continue
+ kept.append(site)
+ return kept
+
+
def _unseen_dirty_files(root: Path, local_path: str) -> list[str] | None:
"""Uncommitted files the index has not seen; ``None`` when git cannot say."""
dirty = _working_tree_dirty_paths(local_path)
@@ -169,13 +188,19 @@ def _unseen_dirty_files(root: Path, local_path: str) -> list[str] | None:
async def reference_edit_set(
- session: AsyncSession, repo_id: str, repo_root: str | Path, node: GraphNode
+ session: AsyncSession,
+ repo_id: str,
+ repo_root: str | Path,
+ node: GraphNode,
+ collector: OmissionCollector | None = None,
) -> dict[str, Any]:
"""Every live site naming *node*, classified, with a completeness verdict.
Returns ``{"sites": [{"path", "line", "kind", "text"}], "complete": bool}``
plus ``reasons`` when incomplete and ``sites_omitted`` past the cap. Kinds:
- definition | import | call | reference.
+ definition | import | call | reference. With a *collector*, plain
+ references past ``MAX_REFERENCES_PER_FILE`` in one file move to it and
+ ``sites_omitted_by_file`` counts every row not shown.
"""
root = Path(repo_root).resolve()
name = _bare_name(node)
@@ -294,8 +319,6 @@ async def reference_edit_set(
reasons.append(f"renamed on import or export: {', '.join(sorted(renamed_in)[:3])}")
if is_default:
reasons.append("default export: importers may rename it")
- if len(sites) > MAX_SITES:
- reasons.append(f"over {MAX_SITES} sites")
if node.kind in _MEMBER_KINDS or node.node_id.count("::") > 1:
reasons.append("member: calls through untyped receivers are not scanned")
touched = sorted({s["path"] for s in sites if s["path"] in unindexed_set})
@@ -304,10 +327,41 @@ async def reference_edit_set(
if not any(s["kind"] == "definition" for s in sites) and def_file not in unreadable:
reasons.append("definition not found at its indexed line")
- sites.sort(key=lambda s: (s["kind"] != "definition", s["path"], s["line"]))
- out: dict[str, Any] = {"sites": sites[:MAX_SITES], "complete": not reasons}
- if len(sites) > MAX_SITES:
- out["sites_omitted"] = len(sites) - MAX_SITES
+ sites.sort(
+ key=lambda s: (
+ s["kind"] != "definition",
+ is_test_related_path(s["path"]),
+ s["path"],
+ s["line"],
+ )
+ )
+ kept = _cap_references_per_file(sites) if collector is not None else sites
+ if len(kept) > MAX_SITES:
+ reasons.append(f"over {MAX_SITES} sites")
+ if len(kept) < len(sites):
+ reasons.append(
+ f"{len(sites) - len(kept)} references past {MAX_REFERENCES_PER_FILE} per file"
+ " not shown: sites_omitted_by_file; the full list is under _meta.omitted"
+ )
+ out: dict[str, Any] = {"sites": kept[:MAX_SITES], "complete": not reasons}
+ if collector is None:
+ if len(sites) > MAX_SITES:
+ out["sites_omitted"] = len(sites) - MAX_SITES
+ elif len(out["sites"]) < len(sites):
+ shown = {id(s) for s in out["sites"]}
+ cap_collection(
+ out,
+ "sites",
+ sites,
+ MAX_SITES,
+ collector,
+ emitted=out["sites"],
+ label=f"{node.node_id} :: references.sites not shown",
+ reason="per_file_reference_cap" if len(kept) < len(sites) else "construction_cap",
+ )
+ out["sites_omitted_by_file"] = dict(
+ Counter(s["path"] for s in sites if id(s) not in shown)
+ )
if reasons:
out["reasons"] = reasons
return out
diff --git a/packages/server/src/repowise/server/mcp_server/tool_context/targets.py b/packages/server/src/repowise/server/mcp_server/tool_context/targets.py
index ae08a6d75..cbeb5f98f 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_context/targets.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_context/targets.py
@@ -1310,7 +1310,9 @@ async def _resolve_one_target(
)
root = repo_root or getattr(repository, "local_path", None)
if ref_node is not None and root:
- result_data["references"] = await reference_edit_set(session, repo_id, root, ref_node)
+ result_data["references"] = await reference_edit_set(
+ session, repo_id, root, ref_node, collector
+ )
else:
result_data["references_note"] = "references require a symbol target in the graph"
diff --git a/tests/unit/server/mcp/test_context_references.py b/tests/unit/server/mcp/test_context_references.py
index f5002271d..64291b529 100644
--- a/tests/unit/server/mcp/test_context_references.py
+++ b/tests/unit/server/mcp/test_context_references.py
@@ -378,3 +378,123 @@ async def test_get_context_serves_references_for_a_symbol(setup_mcp, session, tm
assert t["references"]["complete"] is True
assert (_USER, 9, "call") in _sites(t["references"])
+
+
+def _collector(tmp_path):
+ from repowise.server.mcp_server._budget import OmissionCollector
+
+ return OmissionCollector("get_context", store_path=tmp_path / "omissions.db")
+
+
+async def test_small_list_is_unchanged_by_the_per_file_cap(session, repo_id, tmp_path):
+ node = await _seed(session, repo_id, tmp_path)
+
+ bare = await reference_edit_set(session, repo_id, tmp_path, node)
+ capped = await reference_edit_set(session, repo_id, tmp_path, node, _collector(tmp_path))
+
+ assert json.dumps(capped) == json.dumps(bare)
+
+
+async def test_mock_heavy_python_test_collapses_and_sorts_after_callers(
+ session, repo_id, tmp_path
+):
+ node = await _seed(
+ session,
+ repo_id,
+ tmp_path,
+ extra=[("tests/test_catalog.py", _DEF, "imports", {"imported_names_json": '["backend_of"]'})],
+ )
+ (tmp_path / "tests").mkdir()
+ (tmp_path / "tests" / "test_catalog.py").write_text(
+ "from pkg.catalog import backend_of\n"
+ + "".join(f"mock.patch('pkg.catalog.backend_of', side_effect={i})\n" for i in range(20)),
+ encoding="utf-8",
+ )
+ collector = _collector(tmp_path)
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node, collector)
+
+ test_rows = [s for s in _sites(out) if s[0] == "tests/test_catalog.py"]
+ assert test_rows == [
+ ("tests/test_catalog.py", 1, "import"),
+ ("tests/test_catalog.py", 2, "reference"),
+ ("tests/test_catalog.py", 3, "reference"),
+ ("tests/test_catalog.py", 4, "reference"),
+ ]
+ assert _sites(out)[-len(test_rows) :] == test_rows # production files first
+ assert (_USER, 9, "call") in _sites(out)
+ assert out["sites_omitted"] == 17
+ assert out["sites_omitted_by_file"] == {"tests/test_catalog.py": 17}
+ assert out["sites_reduced_reason"] == "per_file_reference_cap"
+ assert out["complete"] is False
+ assert any("sites_omitted_by_file" in r for r in out["reasons"])
+ response: dict = {}
+ collector.attach(response)
+ assert response["_meta"]["omitted"]["refs"]
+
+
+async def test_mock_heavy_typescript_test_keeps_the_real_caller(session, repo_id, tmp_path):
+ spec = "import { pick } from './a'\n" + "vi.mocked(pick).mockReturnValue(1)\n" * 30
+ node = await _seed_js(
+ session,
+ repo_id,
+ tmp_path,
+ {
+ "web/a.ts": "export function pick(x: number): number { return x }\n",
+ "web/a.test.ts": spec,
+ "web/b.ts": "import { pick } from './a'\nexport const run = () => [pick]\n",
+ },
+ )
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node, _collector(tmp_path))
+
+ paths = [p for p, _n, _k in _sites(out)]
+ assert paths.index("web/b.ts") < paths.index("web/a.test.ts")
+ assert ("web/b.ts", 2, "reference") in _sites(out)
+ assert paths.count("web/a.test.ts") == 1 + _edit_sites.MAX_REFERENCES_PER_FILE
+ assert out["sites_omitted_by_file"] == {"web/a.test.ts": 30 - _edit_sites.MAX_REFERENCES_PER_FILE}
+ assert out["complete"] is False
+
+
+async def test_call_sites_are_never_collapsed(session, repo_id, tmp_path):
+ extra = []
+ for i in range(8):
+ rel = f"svc/user{i}.py"
+ (tmp_path / "svc").mkdir(exist_ok=True)
+ (tmp_path / rel).write_text(
+ "from pkg.catalog import backend_of\n" + "backend_of(1)\n" * 6, encoding="utf-8"
+ )
+ extra.append((rel, _DEF, "imports", {"imported_names_json": '["backend_of"]'}))
+ extra.append(
+ (f"{rel}::run", _SYM, "calls", {"call_lines_json": json.dumps(list(range(2, 8)))})
+ )
+ node = await _seed(session, repo_id, tmp_path, extra=extra)
+
+ out = await reference_edit_set(session, repo_id, tmp_path, node, _collector(tmp_path))
+
+ calls = [s for s in _sites(out) if s[2] == "call" and s[0].startswith("svc/user")]
+ assert len(calls) == 8 * 6
+ assert out["complete"] is True
+ assert "sites_omitted_by_file" not in out
+
+
+async def test_get_context_collapses_a_flood_into_recoverable_counts(setup_mcp, session, tmp_path):
+ from repowise.server.mcp_server import get_context
+
+ await _seed(session, setup_mcp, tmp_path)
+ await session.commit()
+ with (tmp_path / _USER).open("a", encoding="utf-8") as fh:
+ fh.write("spies = [\n" + " backend_of,\n" * 40 + "]\n")
+
+ resp = await get_context([_SYM], include=["references"])
+ refs = resp["targets"][_SYM]["references"]
+
+ assert refs["sites_omitted_by_file"] == {_USER: 40 + 1 - _edit_sites.MAX_REFERENCES_PER_FILE}
+ assert (_USER, 9, "call") in _sites(refs)
+ assert refs["complete"] is False
+ assert resp["_meta"]["omitted"]["refs"]