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(context): calculate days_ago from last change (#3153)

Nehant Fopse4c43d6d6e094
diff --git a/packages/server/src/repowise/server/mcp_server/tool_context/targets.py b/packages/server/src/repowise/server/mcp_server/tool_context/targets.py
index fcecfde7b..a2b29d32c 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_context/targets.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_context/targets.py
@@ -11,11 +11,11 @@ from __future__ import annotations
import contextlib
import json
import re
-from datetime import datetime
+from datetime import UTC, datetime
from pathlib import Path
from typing import Any
-from sqlalchemy import or_, select
+from sqlalchemy import func, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.analysis.decisions.lifecycle import is_governing
@@ -372,6 +372,38 @@ def _size_exclusion_note(repo_root: Any, target: str) -> str | None:
)
+async def _days_since_last_change(
+ session: AsyncSession,
+ repo_id: str,
+ last_commit_at: datetime | None,
+ as_of_ts: datetime | None = None,
+) -> int | None:
+ """Whole days from a file's last commit to the index's reference date.
+
+ Uses ``as_of_ts`` (the anchor ``fix_annotation`` already uses) when given,
+ else the newest commit in the index, so it agrees with the other ages on
+ the file however old the index is. Rows are stored naive-UTC, so a naive
+ timestamp is read as UTC.
+ """
+ if last_commit_at is None:
+ return None
+ reference = as_of_ts
+ if reference is None:
+ reference = (
+ await session.execute(
+ select(func.max(GitMetadata.last_commit_at)).where(
+ GitMetadata.repository_id == repo_id
+ )
+ )
+ ).scalar()
+ reference = reference or last_commit_at
+ return max(0, (_as_utc(reference) - _as_utc(last_commit_at)).days)
+
+
+def _as_utc(moment: datetime) -> datetime:
+ return moment if moment.tzinfo else moment.replace(tzinfo=UTC)
+
+
async def _resolve_one_target(
session: AsyncSession,
repository: Repository,
@@ -1116,7 +1148,9 @@ async def _resolve_one_target(
meta.last_commit_at.isoformat() if meta.last_commit_at else None
)
last_change["author"] = meta.primary_owner_name
- last_change["days_ago"] = meta.age_days
+ last_change["days_ago"] = await _days_since_last_change(
+ session, repo_id, meta.last_commit_at, as_of_ts
+ )
else:
last_change["date"] = None
last_change["author"] = None
diff --git a/tests/unit/server/mcp/test_context.py b/tests/unit/server/mcp/test_context.py
index 5f18f5f4c..4d0b4e4ed 100644
--- a/tests/unit/server/mcp/test_context.py
+++ b/tests/unit/server/mcp/test_context.py
@@ -135,7 +135,9 @@ async def test_get_context_single_file(setup_mcp):
assert t["ownership"]["contributor_count"] == 2
# Last change
assert t["last_change"]["author"] == "Alice"
- assert t["last_change"]["days_ago"] == 443
+ # Days from the file's last commit to the repository's newest one, not the
+ # file's age: this row is the newest, so nothing has changed since.
+ assert t["last_change"]["days_ago"] == 0
# Decisions. The fixture record is ``proposed`` with no acceptance behind
# it, so it is a candidate — it used to be served as a governing decision.
assert t["decisions"] == []
@@ -1015,3 +1017,73 @@ def test_survivors_keep_their_detail_when_identity_cards_overflow():
# Eviction came first, so the fair share had room: no survivor lost a block.
assert not out.get("dropped_blocks")
assert len(json.dumps(out, separators=(",", ":"), default=str)) <= 6000
+
+
+def _git_row(rid: str, path: str, last_commit_at: datetime | None) -> GitMetadata:
+ return GitMetadata(
+ id=f"gm-{path}",
+ repository_id=rid,
+ file_path=path,
+ commit_count_total=1,
+ commit_count_90d=0,
+ commit_count_30d=0,
+ first_commit_at=datetime(2020, 1, 1, tzinfo=UTC),
+ last_commit_at=last_commit_at,
+ primary_owner_name="Dana",
+ primary_owner_email="[email protected]",
+ primary_owner_commit_pct=1.0,
+ top_authors_json=json.dumps([{"name": "Dana", "count": 1}]),
+ significant_commits_json=json.dumps([]),
+ co_change_partners_json=json.dumps([]),
+ is_hotspot=False,
+ is_stable=True,
+ churn_percentile=0.1,
+ age_days=2000,
+ created_at=_NOW,
+ updated_at=_NOW,
+ )
+
+
+async def _days_ago(path: str) -> int | None:
+ from repowise.server.mcp_server import get_context
+
+ result = await get_context([path], include=["last_change"], compact=False)
+ return result["targets"][path]["last_change"]["days_ago"]
+
+
+async def test_last_change_days_ago_counts_to_the_newest_commit_not_the_files_age(
+ setup_mcp, session
+):
+ # A later commit elsewhere in the repository moves the reference date:
+ # service.py was last changed 2026-03-15, four days before it.
+ session.add(_git_row(setup_mcp, "src/later.py", datetime(2026, 3, 19, tzinfo=UTC)))
+ await session.flush()
+
+ assert await _days_ago("src/auth/service.py") == 4
+ assert await _days_ago("src/later.py") == 0
+
+
+async def test_last_change_days_ago_is_none_without_a_last_commit(setup_mcp, session):
+ session.add(_git_row(setup_mcp, "src/undated.py", None))
+ await session.flush()
+
+ assert await _days_ago("src/undated.py") is None
+
+
+async def test_last_change_days_ago_accepts_a_naive_timestamp(setup_mcp, session):
+ # SQLite hands timestamps back naive; they are read as UTC, not rejected.
+ session.add(_git_row(setup_mcp, "src/naive.py", datetime(2026, 3, 10)))
+ await session.flush()
+
+ assert await _days_ago("src/naive.py") == 5
+
+
+async def test_days_since_last_change_prefers_as_of_ts(session):
+ from repowise.server.mcp_server.tool_context.targets import _days_since_last_change
+
+ last = datetime(2026, 3, 15)
+ as_of = datetime(2026, 3, 25)
+ assert await _days_since_last_change(session, "any-repo-id", last, as_of) == 10