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.

test(why): pin that decisions dropped by the search cap stay recoverable (#2291)

Raghav Chamadiyadcaee56b58d1
diff --git a/packages/server/src/repowise/server/mcp_server/tool_why.py b/packages/server/src/repowise/server/mcp_server/tool_why.py
index 2b9f6ddf4..85b805c26 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_why.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_why.py
@@ -1461,8 +1461,9 @@ def _collapse_restatements(records: list) -> list[tuple[Any, list[str]]]:
"""``(kept, folded_ids)`` per distinct decision, input order preserved.
Runs on records rather than on the projected dicts so the collapse happens
- *before* the lineage walk, which costs a query per surviving record. Records
- citing no evidence at all cannot be compared this way and are always kept.
+ *before* the lineage walk and the projection, neither of which should spend
+ work on three phrasings of one decision. Records citing no evidence at all
+ cannot be compared this way and are always kept.
"""
by_evidence: dict[tuple[object, ...], int] = {}
out: list[tuple[Any, list[str]]] = []
@@ -1885,7 +1886,15 @@ async def _attach_decision_evidence(session: Any, records: list) -> None:
async def _attach_response_decision_evidence(
session: Any, result: dict[str, Any], records: list
) -> None:
- """Hydrate only decision rows that the bounded response will expose."""
+ """Hydrate the decision rows *result* currently carries.
+
+ Shared by all three modes, so what it covers is whatever the caller has
+ built by the time it runs. Search mode calls it before its cap, which
+ means the whole projected pool rather than the served head. That is
+ deliberate: the rows the cap sheds are written to the omission store, and
+ they have to carry their evidence by then or recovery returns bodies with
+ no provenance.
+ """
ids: set[str] = set()
def visit(value: object) -> None:
@@ -1976,8 +1985,14 @@ async def _why_search(query: str, targets: list[str] | None, repo: str | None) -
)
target_set = set(targets) if targets else set()
- # Rank wide, collapse restatements, then cap — so the cap spends its slots on
- # distinct decisions — and only walk lineage for what survives.
+ # Rank wide, collapse restatements, and project the whole surviving pool.
+ # The decisions cap comes last, at the bottom of this function, and what it
+ # sheds is banked whole: the omission document is the projected rows, so
+ # building only the three that are served would leave recovery with nothing
+ # to hand back. Measured on this repo, a
+ # realistic question collapses to a median of 7 records and up to 33, and
+ # capping first saves 4-21ms of a call whose cost is dominated by a fixed
+ # annotation floor. The recovery is worth more than the milliseconds.
ranked = _rank_keyword_matches(all_decisions, query, target_set)
if not ranked:
return await _why_no_match(
@@ -1993,7 +2008,6 @@ async def _why_search(query: str, targets: list[str] | None, repo: str | None) -
collapsed, decision_results, lineage_by_id, collector, accepted
)
- # No further slice: the cap is on *bodies*, applied to ``collapsed`` above.
# Semantic hits append as id-plus-snippet at roughly 200 chars each, so
# dropping them to fit a record count would cost the lane that carries a
# calibrated relevance score for the sake of no measurable payload.
diff --git a/tests/unit/server/mcp/test_why_search_payload.py b/tests/unit/server/mcp/test_why_search_payload.py
index 9cbf29b20..07110e7d3 100644
--- a/tests/unit/server/mcp/test_why_search_payload.py
+++ b/tests/unit/server/mcp/test_why_search_payload.py
@@ -337,3 +337,72 @@ async def test_status_breaks_ties_without_gating(session, setup_mcp):
ids = [d["id"] for d in result["decisions"]]
assert ids.index("strong-proposed") < ids.index("weak-active"), ids
+
+
+async def test_decisions_dropped_by_the_cap_stay_recoverable_with_their_bodies(
+ session, setup_mcp, tmp_path
+):
+ """The records past the cap are banked whole, not as a list of ids.
+
+ Search mode ranks wide and serves three. What it does with the rest is the
+ reason the projection is built for the whole surviving pool rather than for
+ the three: every dropped record is written to the omission store with its
+ decision body, its rationale and the evidence annotation already attached,
+ so ``repowise expand`` answers the follow-up without a second search.
+
+ Capping the pool before the projection would make that document a list of
+ titles, and nothing else here would notice. This pins what recovery is
+ worth so that trade has to be made deliberately.
+ """
+ import repowise.server.mcp_server as mcp_mod
+ from repowise.core.distill.store import OmissionStore
+ from repowise.server.mcp_server import get_why
+ from repowise.server.mcp_server.tool_why import _MAX_SEARCH_DECISIONS
+
+ # ``default_store_path`` walks up for an existing ``.repowise/`` and falls
+ # back to the user's home, so without this both the collector under test
+ # and the read below land in the developer's real store.
+ (tmp_path / ".repowise").mkdir(exist_ok=True)
+ mcp_mod._repo_path = str(tmp_path)
+
+ total = 9
+ for n in range(total):
+ await _seed(
+ session,
+ setup_mcp,
+ id_=f"zeb{n}",
+ title=f"Zebrafish caching strategy variant {n}",
+ decision=f"body of decision zeb{n}",
+ rationale=f"rationale of decision zeb{n}",
+ # Distinct cited evidence, so the restatement collapse keeps them
+ # apart and the cap is what does the dropping.
+ commits=[f"{n:040x}"],
+ confidence=0.9 - n / 100,
+ )
+
+ result = await get_why("why the zebrafish caching strategy variant")
+
+ served = [d["id"] for d in result["decisions"]]
+ assert len(served) == _MAX_SEARCH_DECISIONS, served
+ # ``>=``: the shared fixture corpus is free to grow a record that also
+ # clears the floor. What matters is that the whole pool was counted.
+ assert result["decisions_total"] >= total
+
+ refs = result["_meta"]["omitted"]["refs"]
+ assert refs, "a dropped decision must leave a recovery reference"
+ with OmissionStore.open_default(tmp_path) as store:
+ banked = "".join(store.get(ref) or "" for ref in refs)
+
+ for decision_id in (f"zeb{n}" for n in range(total)):
+ if decision_id in served:
+ continue
+ assert f"body of decision {decision_id}" in banked, decision_id
+ assert f"rationale of decision {decision_id}" in banked, decision_id
+ # Per record, not as a total: the annotation also stamps provenance on
+ # lineage rows, so a count over the whole document would pass on one
+ # well-connected record.
+ start = banked.index(f'"id": "{decision_id}"')
+ nxt = banked.find('"id": "', start + 1)
+ row = banked[start : nxt if nxt != -1 else len(banked)]
+ assert '"provenance"' in row, decision_id