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(suspects): report when candidates reach the change index cap (#3930)

Yushu Li108163ded567
diff --git a/packages/core/src/repowise/core/suspects/index.py b/packages/core/src/repowise/core/suspects/index.py
index 5c1d4ba2d..4e53489b5 100644
--- a/packages/core/src/repowise/core/suspects/index.py
+++ b/packages/core/src/repowise/core/suspects/index.py
@@ -91,8 +91,10 @@ async def find_suspects(
*until* defaults to the symptom time, else now; *since* to 14 days before it.
"""
from repowise.core.attribution import attribution_enabled
+ from repowise.core.ingestion.git_indexer._constants import _DEFAULT_COMMIT_LIMIT
from repowise.core.persistence.crud import get_co_change_partners, list_changes
from repowise.core.persistence.crud.changes import ChangeFilter
+ from repowise.core.repo_config import load_repo_config
repo_path = Path(repo_path)
until = _aware(until or symptom.at or datetime.now(UTC))
@@ -104,8 +106,12 @@ async def find_suspects(
rows = [r for r in rows if r.merged_at is not None]
window = {"since": since.isoformat(), "until": until.isoformat()}
if not rows:
- return {"window": window, "candidates": 0, "suspects": [], "note": NOTE}
+ return {
+ "window": window, "candidates": 0, "candidates_capped": False,
+ "suspects": [], "note": NOTE,
+ }
+ commit_limit = int(load_repo_config(repo_path).get("commit_limit") or 0) or _DEFAULT_COMMIT_LIMIT
files_of = {
r.change_id: tuple(f["path"] for f in json.loads(r.files_json or "[]")) for r in rows
}
@@ -144,6 +150,7 @@ async def find_suspects(
return {
"window": window,
"candidates": len(candidates),
+ "candidates_capped": len(rows) >= commit_limit,
# Changes git could not read: ranked on their title and files alone.
"texts_missing": sum(1 for r in rows if r.head_sha not in texts),
"symptom_files": list(named),
diff --git a/packages/server/src/repowise/server/schemas/changes.py b/packages/server/src/repowise/server/schemas/changes.py
index 972060cdc..5072a92e4 100644
--- a/packages/server/src/repowise/server/schemas/changes.py
+++ b/packages/server/src/repowise/server/schemas/changes.py
@@ -224,6 +224,7 @@ class Suspect(BaseModel):
class SuspectsResponse(BaseModel):
window: dict[str, str]
candidates: int
+ candidates_capped: bool
#: Changes git could not read; they are ranked on title and files alone.
texts_missing: int = 0
symptom_files: list[str] = []
diff --git a/packages/types/src/generated/http.ts b/packages/types/src/generated/http.ts
index 30f115634..b075d2468 100644
--- a/packages/types/src/generated/http.ts
+++ b/packages/types/src/generated/http.ts
@@ -4125,6 +4125,7 @@ export interface SuspectsRequest {
export interface SuspectsResponse {
window: Record<string, string>;
candidates: number;
+ candidates_capped: boolean;
texts_missing?: number;
symptom_files?: string[];
suspects: Suspect[];
diff --git a/tests/unit/cli/test_suspects_cmd.py b/tests/unit/cli/test_suspects_cmd.py
index 94f7ef95a..38f7c5b5b 100644
--- a/tests/unit/cli/test_suspects_cmd.py
+++ b/tests/unit/cli/test_suspects_cmd.py
@@ -96,6 +96,7 @@ def test_ranks_the_change_the_symptom_names(tmp_path):
assert out.exit_code == 0, out.output
data = json.loads(out.output)
assert data["candidates"] == 2
+ assert data["candidates_capped"] is False
top = data["suspects"][0]
assert top["id"] == f"commit-{a}" and top["rank"] == 1
assert {"text", "recency"} <= set(top["signals"])
diff --git a/tests/unit/server/mcp/test_changes_mcp.py b/tests/unit/server/mcp/test_changes_mcp.py
index fcfbd11df..88c50cbc3 100644
--- a/tests/unit/server/mcp/test_changes_mcp.py
+++ b/tests/unit/server/mcp/test_changes_mcp.py
@@ -201,6 +201,7 @@ async def test_suspects_rank_recent_changes_against_the_targets(setup_mcp, sessi
block = result["suspects"]
assert block["window"]["since"].startswith("2026-03-02")
assert block["candidates"] == 3 # pr-2, pr-3, pr-4; pr-1 is older than 14 days
+ assert block["candidates_capped"] is False
top = block["suspects"][0]
assert (top["id"], top["rank"]) == ("pr-2", 1)
assert "text" in top["signals"] and "recency" in top["signals"]
diff --git a/tests/unit/server/test_changes_routes.py b/tests/unit/server/test_changes_routes.py
index 135ae056c..f1d383ca9 100644
--- a/tests/unit/server/test_changes_routes.py
+++ b/tests/unit/server/test_changes_routes.py
@@ -405,3 +405,28 @@ async def test_suspects_rank_the_window_with_evidence(client: AsyncClient, app)
assert (await client.post(url, json={})).status_code == 422
assert (await client.post(url, json={"symptom": "x", "until": "soon"})).status_code == 422
+
+
[email protected](("since", "count", "capped"), [
+ ("2026-01-01T00:00:00Z", 3, True),
+ ("2026-01-02T00:00:00Z", 2, False),
+ ("2026-01-04T00:00:00Z", 0, False),
+])
+async def test_suspects_http_preserves_candidate_cap_status(
+ client: AsyncClient, app, tmp_path: Path, since: str, count: int, capped: bool,
+) -> None:
+ from repowise.core.repo_config import save_repo_config
+
+ repo = await create_test_repo(client, tmp_path)
+ await _seed(app, repo["id"])
+ save_repo_config(repo["local_path"], {"commit_limit": 3})
+ response = await client.post(
+ f"/api/repos/{repo['id']}/suspects",
+ json={"symptom": "crash", "since": since, "until": "2026-01-10T00:00:00Z", "limit": 1},
+ )
+ assert response.status_code == 200, response.text
+ got = response.json()
+ assert got["candidates"] == count
+ assert got["candidates_capped"] is capped
+ assert len(got["suspects"]) == min(count, 1)
diff --git a/tests/unit/test_suspects.py b/tests/unit/test_suspects.py
index 3ebcc544b..b20a56762 100644
--- a/tests/unit/test_suspects.py
+++ b/tests/unit/test_suspects.py
@@ -4,6 +4,8 @@ from __future__ import annotations
from datetime import UTC, datetime, timedelta
+import pytest
+
from repowise.core.co_change import CoChangePartner
from repowise.core.suspects import (
Candidate,
@@ -196,3 +198,96 @@ def test_one_import_hop_reads_singular():
out = rank_suspects(Symptom(files=(QUERY,), at=NOW), _candidates(), ctx, signals=["graph"])
hop = next(s for s in out if s.change_id == "c").signals["graph"].evidence
assert hop == (f"1 import hop from {QUERY} to {COMPILER}",)
+
+
+async def suspects_index(tmp_path, monkeypatch):
+ from repowise.core.persistence import (
+ create_engine,
+ create_session_factory,
+ get_session,
+ init_db,
+ upsert_repository,
+ )
+ from repowise.core.persistence.models import Change
+ from repowise.core.repo_config import save_repo_config
+ from repowise.core.suspects import index
+
+ save_repo_config(tmp_path, {"commit_limit": 3})
+ monkeypatch.setattr(index, "read_change_texts", lambda *args: {})
+ monkeypatch.setattr(index, "_tracked_files", lambda path: frozenset())
+ engine = create_engine("sqlite+aiosqlite:///:memory:")
+ try:
+ await init_db(engine)
+ async with get_session(create_session_factory(engine)) as session:
+ repo = await upsert_repository(session, name="repo", local_path=str(tmp_path))
+ session.add_all([
+ Change(
+ repository_id=repo.id,
+ change_id=f"change-{i}",
+ identity_kind="commit",
+ identity_value=f"{i:040x}",
+ shape="commit",
+ title=f"Change {i}",
+ merged_at=NOW - timedelta(days=i + 1),
+ head_sha=f"{i:040x}",
+ )
+ for i in range(3)
+ ])
+ await session.flush()
+ yield session, repo.id, tmp_path
+ finally:
+ await engine.dispose()
+
+
[email protected]("commit_limit", [2, 3, "3"])
+async def test_candidates_capped_when_the_window_holds_commit_limit_changes(
+ suspects_index, commit_limit,
+):
+ from repowise.core.repo_config import save_repo_config
+ from repowise.core.suspects.index import find_suspects
+
+ session, repo_id, path = suspects_index
+ save_repo_config(path, {"commit_limit": commit_limit})
+ out = await find_suspects(session, repo_id, path, Symptom(at=NOW), limit=1)
+ assert out["candidates_capped"] is True
+ assert out["candidates"] == 3 # Count candidates before the result limit.
+ assert len(out["suspects"]) == 1
+ assert out["suspects"][0]["id"] == "change-0"
+ assert out["suspects"][0]["rank"] == 1
+
+
+async def test_candidates_not_capped_below_the_limit(suspects_index):
+ from repowise.core.suspects.index import find_suspects
+
+ session, repo_id, path = suspects_index
+ out = await find_suspects(
+ session, repo_id, path, Symptom(at=NOW), since=NOW - timedelta(days=2),
+ )
+ assert out["candidates"] == 2 # The stored third change is outside the window.
+ assert out["candidates_capped"] is False
+ assert [s["id"] for s in out["suspects"]] == ["change-0", "change-1"]
+
+
+async def test_empty_suspects_window_is_not_capped(suspects_index):
+ from repowise.core.suspects.index import find_suspects
+
+ session, repo_id, path = suspects_index
+ out = await find_suspects(
+ session, repo_id, path, Symptom(at=NOW), since=NOW - timedelta(hours=12),
+ )
+ assert out["candidates"] == 0
+ assert out["candidates_capped"] is False
+ assert out["suspects"] == []
+
+
[email protected]("config", [{}, {"commit_limit": 0}, {"commit_limit": "0"}, {"commit_limit": None}])
+async def test_candidates_use_the_default_commit_limit(suspects_index, config):
+ from repowise.core.repo_config import save_repo_config
+ from repowise.core.suspects.index import find_suspects
+
+ session, repo_id, path = suspects_index
+ save_repo_config(path, config)
+ out = await find_suspects(session, repo_id, path, Symptom(at=NOW))
+ assert out["candidates"] == 3
+ assert out["candidates_capped"] is False