Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(search): cap one file's line hits so test mocks cannot flood an identifier search (#3191)
Raghav Chamadiya987db713c89e
diff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.mdindex 5526c5c4b..8d33858e2 100644--- a/docs/agent/MCP_TOOLS.md+++ b/docs/agent/MCP_TOOLS.md@@ -225,7 +225,7 @@ Hybrid search that routes by the shape of the query: identifiers search the symb**Page rows** also carry `symbols`: up to three `name:line` entries (`Owner.member` for members) in that file whose names match the query's words. The first three page rows whose file has a matching line also carry `matched_lines`: a sample of up to three `{line, text}` source lines read from the live file that share the most query words, code lines before comments and imports. Unlike `lines`, it is never every match.-An identifier or literal query also returns `lines` (`{path, line, kind, text}`, definitions first, at most 50): the `get_context` `references` of one to three exact symbols, else a live scan for the token (kind `definition` or `match`). `complete` is true only when they are every match (see `references` above); otherwise `reasons` says why.+An identifier or literal query also returns `lines` (`{path, line, kind, text}`, definitions first, at most 50): the `get_context` `references` of one to three exact symbols, else a live scan for the token (kind `definition` or `match`). `complete` is true only when they are every match (see `references` above); otherwise `reasons` says why. Past 3 plain `reference` lines in one file, and past 50 lines, rows are counted in `lines_omitted_by_file` and recoverable from `_meta.omitted`; test files sort after production files.```search_codebase(query="GitIndexer index_repo")diff --git a/packages/server/src/repowise/server/mcp_server/_line_hits.py b/packages/server/src/repowise/server/mcp_server/_line_hits.pyindex acb7cc985..97c0bf4e5 100644--- a/packages/server/src/repowise/server/mcp_server/_line_hits.py+++ b/packages/server/src/repowise/server/mcp_server/_line_hits.py@@ -18,6 +18,7 @@ import osimport reimport subprocessimport time+from collections import Counterfrom collections.abc import Containerfrom pathlib import Pathfrom typing import Any@@ -28,10 +29,13 @@ from repowise.core.fs_walk import PRUNED_DIRS, walk_repofrom repowise.core.persistence.database import get_sessionfrom repowise.core.persistence.models import GraphNodefrom repowise.core.repo_config import load_repo_config-from repowise.server.mcp_server._budget import register_post_enforce+from repowise.core.test_paths import is_test_related_path+from repowise.server.mcp_server._budget import OmissionCollector, register_post_enforcefrom repowise.server.mcp_server._edit_sites import (_MAX_FILE_BYTES,+ MAX_REFERENCES_PER_FILE,MAX_TEXT_CHARS,+ _cap_references_per_file,reference_edit_set,)from repowise.server.mcp_server._helpers import (@@ -368,10 +372,31 @@ async def _attach(rows, reasons = await asyncio.to_thread(_scan_listed, root, *literal)else:return- rows.sort(key=lambda r: (_KIND_ORDER[r["kind"]], r["path"], r["line"]))- if len(rows) > MAX_LINES and OVER_LINES not in reasons:+ rows.sort(+ key=lambda r: (+ r["kind"] != "definition",+ is_test_related_path(r["path"]),+ _KIND_ORDER[r["kind"]],+ r["path"],+ r["line"],+ )+ )+ kept = _cap_references_per_file(rows)+ if len(kept) < len(rows):+ reasons.append(+ f"{len(rows) - len(kept)} references past {MAX_REFERENCES_PER_FILE} per file"+ " not shown: lines_omitted_by_file; the full list is under _meta.omitted"+ )+ if len(kept) > MAX_LINES and OVER_LINES not in reasons:reasons.append(OVER_LINES)- response["lines"] = rows[:MAX_LINES]+ response["lines"] = kept[:MAX_LINES]+ if len(response["lines"]) < len(rows):+ shown = {id(r) for r in response["lines"]}+ hidden = [r for r in rows if id(r) not in shown]+ collector = OmissionCollector("search_codebase", repo_root=ctx.path)+ collector.add(f"{query} :: lines not shown", hidden)+ collector.attach(response)+ response["lines_omitted_by_file"] = dict(Counter(r["path"] for r in hidden))response["complete"] = not reasonsif reasons:response["reasons"] = reasonsdiff --git a/tests/unit/server/mcp/test_search_line_hits.py b/tests/unit/server/mcp/test_search_line_hits.pyindex e8f7bc1bc..5cf2fe780 100644--- a/tests/unit/server/mcp/test_search_line_hits.py+++ b/tests/unit/server/mcp/test_search_line_hits.py@@ -303,3 +303,133 @@ def test_definition_patterns():assert value.search(' "port": 47821,')assert value.search("const PORT: number = 47821;")assert value.search("connect(port=47821)") is None+++def _importer(session, rid: str, rel: str, target: str, names: str, language: str) -> None:+ session.add_all(+ [+ GraphNode(+ id=f"lh_file_{rel}",+ repository_id=rid,+ node_id=rel,+ node_type="file",+ language=language,+ created_at=_NOW,+ ),+ GraphEdge(+ id=f"lh_imp_{rel}",+ repository_id=rid,+ source_node_id=rel,+ target_node_id=target,+ edge_type="imports",+ imported_names_json=names,+ confidence=1.0,+ created_at=_NOW,+ ),+ ]+ )+++async def test_mock_heavy_python_test_collapses_into_recoverable_counts(tree, session, setup_mcp):+ (tree / "tests").mkdir()+ (tree / "tests" / "test_widget.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",+ )+ _importer(session, setup_mcp, "tests/test_widget.py", "pkg/catalog.py", '["backend_of"]', "python")+ await session.commit()++ out = await search_codebase("backend_of", mode="symbol")++ cap = _edit_sites.MAX_REFERENCES_PER_FILE+ assert _rows(out) == [+ ("pkg/catalog.py", 4, "definition"),+ ("app/widget.py", 1, "import"),+ ("app/widget.py", 6, "call"),+ ("tests/test_widget.py", 1, "import"),+ *(("tests/test_widget.py", n, "reference") for n in range(2, 2 + cap)),+ ]+ assert out["lines_omitted_by_file"] == {"tests/test_widget.py": 20 - cap}+ assert out["complete"] is False+ assert any("lines_omitted_by_file" in r for r in out["reasons"])+ assert out["_meta"]["omitted"]["refs"]+++async def test_mock_heavy_typescript_test_sorts_after_the_real_user(tree, session, setup_mcp):+ files = {+ "web/a.ts": "export function pick(x: number): number { return x }\n",+ "web/a.test.ts": "import { pick } from './a'\n" + "vi.mocked(pick).mockReturnValue(1)\n" * 30,+ "web/b.ts": "import { pick } from './a'\nexport const run = () => [pick]\n",+ }+ for rel, text in files.items():+ (tree / rel).parent.mkdir(parents=True, exist_ok=True)+ (tree / rel).write_text(text, encoding="utf-8")+ session.add(+ GraphNode(+ id="lh_ts_sym",+ repository_id=setup_mcp,+ node_id="web/a.ts::pick",+ node_type="symbol",+ name="pick",+ file_path="web/a.ts",+ kind="function",+ language="typescript",+ start_line=1,+ end_line=1,+ created_at=_NOW,+ )+ )+ for rel in ("web/a.test.ts", "web/b.ts"):+ _importer(session, setup_mcp, rel, "web/a.ts", '["pick"]', "typescript")+ await session.commit()++ out = await search_codebase("pick", mode="symbol")++ paths = [p for p, _n, _k in _rows(out)]+ assert ("web/b.ts", 2, "reference") in _rows(out)+ assert paths.index("web/b.ts") < paths.index("web/a.test.ts")+ assert paths.count("web/a.test.ts") == 1 + _edit_sites.MAX_REFERENCES_PER_FILE+ assert out["lines_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(tree, session, setup_mcp):+ for i in range(4):+ rel = f"svc/user{i}.py"+ (tree / "svc").mkdir(exist_ok=True)+ (tree / rel).write_text(+ "from pkg.catalog import backend_of\n" + "backend_of(1)\n" * 6, encoding="utf-8"+ )+ _importer(session, setup_mcp, rel, "pkg/catalog.py", '["backend_of"]', "python")+ session.add(+ GraphEdge(+ id=f"lh_call_{i}",+ repository_id=setup_mcp,+ source_node_id=f"{rel}::run",+ target_node_id="pkg/catalog.py::backend_of",+ edge_type="calls",+ call_lines_json="[2, 3, 4, 5, 6, 7]",+ confidence=0.95,+ created_at=_NOW,+ )+ )+ await session.commit()++ out = await search_codebase("backend_of", mode="symbol")++ calls = [r for r in _rows(out) if r[2] == "call" and r[0].startswith("svc/")]+ assert len(calls) == 4 * 6+ assert out["complete"] is True+ assert "lines_omitted_by_file" not in out+ assert "omitted" not in out["_meta"]+++def test_literal_rows_are_not_collapsed():+ rows = [{"path": "a.py", "line": n, "kind": "match", "text": "x"} for n in range(5)]+ assert _edit_sites._cap_references_per_file(rows) == rows