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(search): drop page_id when page_type + path rebuild it (#3156)

Satvik Mishraf66bb53ced7e
diff --git a/packages/server/src/repowise/server/mcp_server/tool_search.py b/packages/server/src/repowise/server/mcp_server/tool_search.py
index 5eb624976..4a054124e 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_search.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_search.py
@@ -554,9 +554,15 @@ def _drop_derivable_page_ids(results: list[dict]) -> list[dict]:
Consumers rebuild with the same expression; ``ui/src/chat/source-citations``
does exactly that, and used to skip any row whose ``page_id`` was missing.
+
+ ``add_row_paths`` runs first and deletes ``target_path`` wherever it equals
+ the new ``path``, so the id's second half is ``target_path`` when present
+ and ``path`` otherwise. Without that fallback an ordinary file row rebuilt
+ as ``"file_page:"`` and kept its id.
"""
for item in results:
- derived = f"{item.get('page_type', '')}:{item.get('target_path', '')}"
+ target = item.get("target_path") or item.get("path", "")
+ derived = f"{item.get('page_type', '')}:{target}"
if item.get("page_id") == derived:
item.pop("page_id", None)
return results
diff --git a/packages/ui/__tests__/chat/source-citations.test.tsx b/packages/ui/__tests__/chat/source-citations.test.tsx
index 8cc58e0fd..9cb964592 100644
--- a/packages/ui/__tests__/chat/source-citations.test.tsx
+++ b/packages/ui/__tests__/chat/source-citations.test.tsx
@@ -147,6 +147,19 @@ describe("extractSources", () => {
expect(sources[0]?.confidence).toBeUndefined();
});
+ it("rebuilds a search row's page id from path when page_id and target_path are gone", () => {
+ // search_codebase drops target_path where it equals path, and then drops
+ // page_id because page_type + path rebuild it.
+ const sources = extractSources(
+ [searchCall([{ page_type: "file_page", path: "src/auth.py", title: "auth.py" }])],
+ "repo1",
+ );
+
+ expect(sources).toHaveLength(1);
+ expect(sources[0]?.pageId).toBe("file_page:src/auth.py");
+ expect(sources[0]?.targetPath).toBe("src/auth.py");
+ });
+
it("cites files from get_why health stale_decisions", () => {
const sources = extractSources(
[
diff --git a/packages/ui/src/chat/source-citations.tsx b/packages/ui/src/chat/source-citations.tsx
index 6974a72a5..79ea46b3a 100644
--- a/packages/ui/src/chat/source-citations.tsx
+++ b/packages/ui/src/chat/source-citations.tsx
@@ -58,8 +58,11 @@ export function extractSources(
// wherever those two rebuild it. Derive rather than skip: the previous
// `const pageId = r.page_id` dropped the whole row — and its citation —
// for any result without one. Same fallback the get_context branch uses.
+ // The tool also drops `target_path` where it equals `path`, so fall back
+ // to `path`, the same way the server builds the id it compares against.
const pageType = r.page_type as string | undefined;
- const targetPath = r.target_path as string | undefined;
+ const targetPath =
+ (r.target_path as string | undefined) || (r.path as string | undefined);
const pageId =
(r.page_id as string | undefined) ??
(pageType && targetPath ? `${pageType}:${targetPath}` : "");
@@ -70,7 +73,7 @@ export function extractSources(
pageId,
title: (r.title as string) ?? pageId,
pageType: (r.page_type as string) ?? "file_page",
- targetPath: (r.target_path as string) ?? "",
+ targetPath: targetPath ?? "",
confidence: r.confidence_score as number | undefined,
toolName: tc.name,
});
diff --git a/tests/unit/server/mcp/test_payload_cuts.py b/tests/unit/server/mcp/test_payload_cuts.py
index 5812714b3..9c004a652 100644
--- a/tests/unit/server/mcp/test_payload_cuts.py
+++ b/tests/unit/server/mcp/test_payload_cuts.py
@@ -15,6 +15,7 @@ the failure mode each of these changes was designed around.
from __future__ import annotations
from repowise.server.mcp_server._budget.budgeter import truncate_to_budget
+from repowise.server.mcp_server._page_paths import add_row_paths
from repowise.server.mcp_server.tool_answer.answer import (
_build_best_guesses,
_drop_duplicated_guess_excerpts,
@@ -43,6 +44,27 @@ def test_page_id_dropped_when_page_type_and_target_path_rebuild_it():
assert f"{results[0]['page_type']}:{results[0]['target_path']}" == "file_page:rich/ansi.py"
+def test_page_id_dropped_after_add_row_paths_moved_target_path_to_path():
+ """The shape every search response actually has at this point.
+
+ ``add_row_paths`` runs first and deletes ``target_path`` where it equals
+ ``path``, so an ordinary file row reaches the drop with only ``path``.
+ """
+ results = [
+ {
+ "page_id": "file_page:rich/ansi.py",
+ "page_type": "file_page",
+ "target_path": "rich/ansi.py",
+ "title": "ansi",
+ }
+ ]
+ add_row_paths(results)
+ assert "target_path" not in results[0]
+ _drop_derivable_page_ids(results)
+ assert "page_id" not in results[0]
+ assert f"{results[0]['page_type']}:{results[0]['path']}" == "file_page:rich/ansi.py"
+
+
def test_page_id_kept_when_it_cannot_be_rebuilt():
"""The other direction, and it is a real case, not a hypothetical.
diff --git a/tests/unit/server/mcp/test_search.py b/tests/unit/server/mcp/test_search.py
index adadc7807..9981c2826 100644
--- a/tests/unit/server/mcp/test_search.py
+++ b/tests/unit/server/mcp/test_search.py
@@ -1873,8 +1873,10 @@ class TestPathlessPagesInCodeLocationModes:
assert pathless["target_path"] == key
assert file_page["path"] == "pkg/cmd/release/list.go"
assert "target_path" not in file_page
- # Not derivable without target_path, so the id stays for citations.
- assert file_page["page_id"] == "file_page:pkg/cmd/release/list.go"
+ # page_type + path rebuild the id, so it is dropped; citations rebuild
+ # it from path the same way.
+ assert "page_id" not in file_page
+ assert f"{file_page['page_type']}:{file_page['path']}" == "file_page:pkg/cmd/release/list.go"
@pytest.mark.asyncio
async def test_federated_rows_carry_path(self, setup_mcp, monkeypatch):
diff --git a/tests/unit/server/mcp/test_tombstones.py b/tests/unit/server/mcp/test_tombstones.py
index bfda5781d..1ab7e2dca 100644
--- a/tests/unit/server/mcp/test_tombstones.py
+++ b/tests/unit/server/mcp/test_tombstones.py
@@ -107,8 +107,11 @@ async def test_search_drops_tombstoned_pages(setup_mcp, session, monkeypatch):
mcp_mod._vector_store.search = fake_search
result = await search_codebase("auth service")
# ``page_id`` is omitted only where ``page_type`` + ``target_path`` rebuild
- # it; a file page serves ``path`` instead of ``target_path`` and keeps it.
- ids = [r.get("page_id") or f"{r['page_type']}:{r['target_path']}" for r in result["results"]]
+ # it, or ``path`` on a file page that serves ``path`` instead.
+ ids = [
+ r.get("page_id") or f"{r['page_type']}:{r.get('target_path') or r['path']}"
+ for r in result["results"]
+ ]
assert "file_page:src/auth/service.py" not in ids
assert "file_page:src/db/models.py" in ids