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): bound line hits when a search mixes words and identifiers (#3199)
Raghav Chamadiya910f9916ea52
diff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.mdindex 8d33858e2..3b7aeb519 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. 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.+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. A query of several words caps `lines` at 15 (definitions, then calls, then imports) and lists the 5 files with the most omitted rows, with `lines_omitted_by_file_total` counting all of them.```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 97c0bf4e5..df22a0f34 100644--- a/packages/server/src/repowise/server/mcp_server/_line_hits.py+++ b/packages/server/src/repowise/server/mcp_server/_line_hits.py@@ -55,6 +55,11 @@ from repowise.server.mcp_server._symbol_lookup import symbol_id_variants_log = logging.getLogger("repowise.mcp.search")MAX_LINES = 50+# A multi-word query's page rows already rank files, so its lines are a short+# lead-in (production first) and lines_omitted_by_file names the top files only.+MAX_LINES_MIXED = 15+MAX_OMITTED_FILES = 5+OVER_LINES_MIXED = f"over {MAX_LINES_MIXED} lines for a multi-word query; search one name for more"MAX_LINES_PER_FILE = 5MAX_SYMBOLS = 3MAX_SCAN_FILES = 20_000@@ -66,6 +71,8 @@ _SNIFF_BYTES = 8192_MAX_STREAM_BYTES = 64_000_000OVER_LINES = f"over {MAX_LINES} lines"_KIND_ORDER = {"definition": 0, "import": 1, "call": 2, "reference": 3, "match": 4}+# Under the short mixed-query cap, call sites say more than imports.+_MIXED_KIND_ORDER = {"definition": 0, "call": 1, "import": 2, "reference": 3, "match": 3}_QUOTED = re.compile(r"""^(["'`])(.+)\1$""")_TOKEN = re.compile(r"^[A-Za-z0-9_$][\w$.]*$")_DECL_WORDS = r"def|class|function|const|let|var|fn|func|type|interface|struct|enum"@@ -372,11 +379,15 @@ async def _attach(rows, reasons = await asyncio.to_thread(_scan_listed, root, *literal)else:return+ mixed = not literal and len(query.split()) > 1+ order = _MIXED_KIND_ORDER if mixed else _KIND_ORDER+ # Production before test within each kind group: every kind for a mixed+ # query, definitions against the rest otherwise.rows.sort(key=lambda r: (- r["kind"] != "definition",+ order[r["kind"]] if mixed else r["kind"] != "definition",is_test_related_path(r["path"]),- _KIND_ORDER[r["kind"]],+ order[r["kind"]],r["path"],r["line"],)@@ -387,16 +398,22 @@ async def _attach(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"] = kept[:MAX_LINES]+ cap, over = (MAX_LINES_MIXED, OVER_LINES_MIXED) if mixed else (MAX_LINES, OVER_LINES)+ if len(kept) > cap and over not in reasons:+ reasons.append(over)+ response["lines"] = kept[:cap]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))+ by_file = Counter(r["path"] for r in hidden)+ if mixed and len(by_file) > MAX_OMITTED_FILES:+ response["lines_omitted_by_file"] = dict(by_file.most_common(MAX_OMITTED_FILES))+ response["lines_omitted_by_file_total"] = len(by_file)+ else:+ response["lines_omitted_by_file"] = dict(by_file)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 5cf2fe780..a71bdfacd 100644--- a/tests/unit/server/mcp/test_search_line_hits.py+++ b/tests/unit/server/mcp/test_search_line_hits.py@@ -433,3 +433,107 @@ async def test_call_sites_are_never_collapsed(tree, session, setup_mcp):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+++async def _mocking_tests(tree, session, rid, target, name, ext, language, n=8):+ """*n* test files that each import *name* and mock it six times."""+ head = f"from pkg.catalog import {name}\n" if ext == "py" else f"import {{ {name} }} from '../web/a'\n"+ for i in range(n):+ rel = f"tests/test_user{i}.{ext}" if ext == "py" else f"tests/user{i}.test.{ext}"+ (tree / "tests").mkdir(exist_ok=True)+ (tree / rel).write_text(head + f"mock({name})\n" * 6, encoding="utf-8")+ _importer(session, rid, rel, target, f'["{name}"]', language)+ await session.commit()+++def _recording_collector(monkeypatch):+ added: list = []++ class Recording(_line_hits.OmissionCollector):+ def add(self, label, rows, *a, **kw):+ added.extend(rows)+ return super().add(label, rows, *a, **kw)++ monkeypatch.setattr(_line_hits, "OmissionCollector", Recording)+ return added+++def _assert_mixed_bound(out, hidden, n_files):+ cap, top = _line_hits.MAX_LINES_MIXED, _line_hits.MAX_OMITTED_FILES+ assert len(out["lines"]) == cap+ assert out["complete"] is False+ assert _line_hits.OVER_LINES_MIXED in out["reasons"]+ assert out["_meta"]["omitted"]["refs"]+ by_file = out["lines_omitted_by_file"]+ assert len(by_file) == top+ ranks = [_line_hits._MIXED_KIND_ORDER[r["kind"]] for r in out["lines"]]+ assert ranks == sorted(ranks)+ assert out["lines_omitted_by_file_total"] == n_files+ assert list(by_file.values()) == sorted(by_file.values(), reverse=True)+ # Everything not shown is in the omission chunk, nothing twice.+ shown = {(r["path"], r["line"]) for r in out["lines"]}+ assert not shown & {(r["path"], r["line"]) for r in hidden}+ assert all(by_file[p] == sum(r["path"] == p for r in hidden) for p in by_file)+++async def test_mixed_python_query_bounds_lines_and_omitted_files(tree, session, setup_mcp, monkeypatch):+ await _mocking_tests(tree, session, setup_mcp, "pkg/catalog.py", "backend_of", "py", "python")+ hidden = _recording_collector(monkeypatch)++ out = await search_codebase("where backend_of is mocked", mode="hybrid")++ # Calls before imports, production before test within each kind.+ assert _rows(out)[:4] == [+ ("pkg/catalog.py", 4, "definition"),+ ("app/widget.py", 6, "call"),+ ("app/widget.py", 1, "import"),+ ("tests/test_user0.py", 1, "import"),+ ]+ _assert_mixed_bound(out, hidden, n_files=8)+ assert len(out["lines"]) + len(hidden) == 3 + 8 * 7+++async def test_mixed_typescript_query_bounds_lines_and_omitted_files(+ tree, session, setup_mcp, monkeypatch+):+ (tree / "web").mkdir()+ (tree / "web" / "a.ts").write_text(+ "export function pickModel(x: number): number { return x }\n", encoding="utf-8"+ )+ session.add(+ GraphNode(+ id="lh_ts_mixed",+ repository_id=setup_mcp,+ node_id="web/a.ts::pickModel",+ node_type="symbol",+ name="pickModel",+ file_path="web/a.ts",+ kind="function",+ language="typescript",+ start_line=1,+ end_line=1,+ created_at=_NOW,+ )+ )+ await _mocking_tests(tree, session, setup_mcp, "web/a.ts", "pickModel", "ts", "typescript", n=7)+ hidden = _recording_collector(monkeypatch)++ out = await search_codebase("pickModel mock setup", mode="hybrid")++ assert out["lines"][0]["path"] == "web/a.ts"+ _assert_mixed_bound(out, hidden, n_files=7)+++async def test_single_name_keeps_the_full_cap_and_every_omitted_file(tree, session, setup_mcp):+ await _mocking_tests(tree, session, setup_mcp, "pkg/catalog.py", "backend_of", "py", "python")++ out = await search_codebase("backend_of", mode="symbol")++ cap = _edit_sites.MAX_REFERENCES_PER_FILE+ assert len(out["lines"]) == 3 + 8 * (1 + cap)+ assert len(out["lines_omitted_by_file"]) == 8+ assert "lines_omitted_by_file_total" not in out+ assert not any(r == _line_hits.OVER_LINES_MIXED for r in out["reasons"])