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(analysis): weight reviewer recency by each author's own recent commits (#3074)

Amanf86760f9ae75
diff --git a/packages/core/src/repowise/core/analysis/reviewers.py b/packages/core/src/repowise/core/analysis/reviewers.py
index 901068c59..1bd0627b6 100644
--- a/packages/core/src/repowise/core/analysis/reviewers.py
+++ b/packages/core/src/repowise/core/analysis/reviewers.py
@@ -7,8 +7,9 @@ already computed:
weighted by share of file commits.
- Co-change: ownership of files that historically co-change with the
touched paths (``co_change_partners_json``).
- - Recency: weight commit count by the file's 90-day activity so people
- who *just* worked here outrank people who touched the file in 2018.
+ - Recency: weight each author by their own share of the file's 90-day
+ commits (``recent_commit_count``), so people who *just* worked here
+ outrank people who touched the file in 2018.
Rows may be dicts, dataclasses or ORM rows; JSON columns may be text or
already decoded.
@@ -87,14 +88,16 @@ def suggest_reviewers(
for m in rows:
authors = json_field(m, "top_authors_json", [])
total = sum(int(a.get("commit_count", 0)) for a in authors) or 1
- commits_90d = field(m, "commit_count_90d") or 0
+ # Each author's own commits in the file's 90-day window, not the
+ # file-level count scaled by share: a row indexed before #2862's
+ # fix has no ``recent_commit_count`` key, and a missing signal
+ # scores zero recency rather than inventing one from an estimate.
+ recent_total = sum(int(a.get("recent_commit_count", 0)) for a in authors)
for a in authors:
cnt = int(a.get("commit_count", 0))
share = cnt / total
- # Recency: commits_90d weighted by share is a rough estimate
- # of how much each author contributed recently.
- recent = int(commits_90d * share)
- score = weight * share + _W_RECENT * (recent / max(commits_90d, 1))
+ recent = int(a.get("recent_commit_count", 0))
+ score = weight * share + _W_RECENT * recent / max(recent_total, 1)
_bump(
a.get("name", ""),
a.get("email") or None,
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py b/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py
index f89b05d2a..2761662aa 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py
@@ -465,6 +465,7 @@ class _Authors:
"name": name,
"email": self.emails.get(name, ""),
"commit_count": count,
+ "recent_commit_count": self.recent_counts.get(name, 0),
}
if name in self.last_ts:
entry["last_commit_ts"] = self.last_ts[name]
diff --git a/tests/unit/analysis/test_ownership_rollup_folds.py b/tests/unit/analysis/test_ownership_rollup_folds.py
index ba689b04d..8764a7752 100644
--- a/tests/unit/analysis/test_ownership_rollup_folds.py
+++ b/tests/unit/analysis/test_ownership_rollup_folds.py
@@ -114,8 +114,61 @@ def test_reviewer_fold_is_the_same_over_stored_and_decoded_rows() -> None:
rows = [shape(r) for r in _ROWS]
assert cochange_paths(rows[:1]) == {"lib/util.py"}
out = suggest_reviewers(rows[:1], rows[1:], limit=5)
- assert [s["name"] for s in out] == ["Bob", "Alice"]
- assert out[0]["co_change_paths"] == ["lib/util.py"]
+ # Alice leads on raw ownership share. Pre-#2862, Bob led instead: the
+ # old recency formula spiked on lib/util.py's commits_90d=1 (a
+ # file-level artifact, `recent/max(commits_90d, 1)` == 1.0 for any
+ # single-commit file), not a genuine per-author recency signal — this
+ # fixture's rows carry no ``recent_commit_count`` (pre-reindex data),
+ # so the fixed formula gives neither author a recency bonus here.
+ assert [s["name"] for s in out] == ["Alice", "Bob"]
+ assert out[0]["co_change_paths"] == []
+
+
+def test_reviewer_recency_ranks_the_recently_active_author_first() -> None:
+ """Equal share, different recent activity: the recent author outranks (#2862)."""
+ row = {
+ "file_path": "src/hot.py",
+ "top_authors_json": json.dumps(
+ [
+ {
+ "name": "Newbie",
+ "email": "[email protected]",
+ "commit_count": 5,
+ "recent_commit_count": 5,
+ },
+ {
+ "name": "OldTimer",
+ "email": "[email protected]",
+ "commit_count": 5,
+ "recent_commit_count": 0,
+ },
+ ]
+ ),
+ "co_change_partners_json": "[]",
+ "commit_count_90d": 5,
+ }
+ out = suggest_reviewers([row], [])
+ assert [s["name"] for s in out] == ["Newbie", "OldTimer"]
+ assert out[0]["recent_commits"] == 5
+ assert out[1]["recent_commits"] == 0
+
+
+def test_reviewer_recency_is_zero_for_a_row_indexed_before_the_fix() -> None:
+ """No ``recent_commit_count`` key: no recency bonus, not an invented estimate (#2862)."""
+ row = {
+ "file_path": "src/legacy.py",
+ "top_authors_json": json.dumps(
+ [
+ {"name": "Alice", "email": "[email protected]", "commit_count": 3},
+ {"name": "Bob", "email": "[email protected]", "commit_count": 1},
+ ]
+ ),
+ "co_change_partners_json": "[]",
+ "commit_count_90d": 4,
+ }
+ out = suggest_reviewers([row], [])
+ assert [s["recent_commits"] for s in out] == [0, 0]
+ assert out[0]["name"] == "Alice"
def test_a_sole_owner_is_a_silo_in_both_rollups() -> None:
diff --git a/tests/unit/server/golden/ownership_rollups.json b/tests/unit/server/golden/ownership_rollups.json
index dd59240eb..9ed01bb2d 100644
--- a/tests/unit/server/golden/ownership_rollups.json
+++ b/tests/unit/server/golden/ownership_rollups.json
@@ -598,8 +598,8 @@
{
"name": "Bob Stone",
"email": "[email protected]",
- "score": 1.99,
- "recent_commits": 8,
+ "score": 1.35,
+ "recent_commits": 0,
"owned_paths": [
"src/api.py"
],
@@ -614,8 +614,8 @@
{
"name": "Alice",
"email": "[email protected]",
- "score": 1.4633,
- "recent_commits": 15,
+ "score": 1.05,
+ "recent_commits": 0,
"owned_paths": [
"src/api.py",
"src/core.py"
@@ -632,8 +632,8 @@
{
"name": "Bob Stone",
"email": "[email protected]",
- "score": 0.7133,
- "recent_commits": 5,
+ "score": 0.5,
+ "recent_commits": 0,
"owned_paths": [
"src/core.py"
],
@@ -648,8 +648,8 @@
{
"name": "Carol",
"email": null,
- "score": 0.14,
- "recent_commits": 2,
+ "score": 0.1,
+ "recent_commits": 0,
"owned_paths": [
"src/core.py"
],
@@ -668,8 +668,8 @@
{
"name": "Alice",
"email": "[email protected]",
- "score": 1.1633,
- "recent_commits": 15,
+ "score": 0.75,
+ "recent_commits": 0,
"owned_paths": [
"lib/util.py"
],
diff --git a/tests/unit/test_git_indexer.py b/tests/unit/test_git_indexer.py
index 531ca0dad..837e81ce5 100644
--- a/tests/unit/test_git_indexer.py
+++ b/tests/unit/test_git_indexer.py
@@ -448,6 +448,43 @@ class TestStableClassification:
assert authors["Alice"]["last_commit_ts"] < authors["Bob"]["last_commit_ts"]
assert authors["Bob"]["last_commit_ts"] == bob_last
+ def test_top_authors_carry_each_authors_own_recent_commit_count(self) -> None:
+ """Each top-author entry records how many of ITS OWN commits on this
+ file fall in the 90-day window, not the file's own window-wide count (#2862)."""
+ import json
+
+ indexer = GitIndexer("/tmp/repo")
+ mock_repo = MagicMock()
+
+ now = datetime.now(UTC)
+ # A bare MagicMock resolves ``float(repo.head.commit.committed_date)``
+ # to 1.0 (its default __float__), anchoring the 90-day window at the
+ # epoch instead of raising and falling back to wall-clock "now" — so
+ # the anchor must be set explicitly for the window to mean anything.
+ mock_repo.head.commit.committed_date = int(now.timestamp())
+ # Alice: one commit 200 days ago (outside the window) and one 10 days
+ # ago (inside). Bob: one commit 150 days ago (outside) only.
+ specs = [
+ ("Alice", "[email protected]", now - timedelta(days=200)),
+ ("Alice", "[email protected]", now - timedelta(days=10)),
+ ("Bob", "[email protected]", now - timedelta(days=150)),
+ ]
+ log_lines = []
+ for i, (name, email, when) in enumerate(specs):
+ ts = int(when.timestamp())
+ log_lines.append(
+ f"\x00sha{i:04d}\x1f{name}\x1f{email}\x1f{name}\x1f{email}\x1f{ts}\x1f{_iso(ts)}\x1f\x1ffeat: c{i}\x1f"
+ )
+ mock_repo.git.log.return_value = "\n".join(log_lines)
+
+ meta = indexer._index_file("shared.py", mock_repo)
+ authors = {a["name"]: a for a in json.loads(meta["top_authors_json"])}
+
+ assert authors["Alice"]["commit_count"] == 2
+ assert authors["Alice"]["recent_commit_count"] == 1
+ assert authors["Bob"]["commit_count"] == 1
+ assert authors["Bob"]["recent_commit_count"] == 0
+
class TestGitWindowAnchor:
"""Recency windows are anchored to the indexed commit by default;