Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(dead-code): keep a stored commit time comparable to a fresh one (#1702)
Raghav Chamadiya5242d4e224a6
diff --git a/packages/core/src/repowise/core/persistence/crud/git.py b/packages/core/src/repowise/core/persistence/crud/git.pyindex 835953a98..d75778d60 100644--- a/packages/core/src/repowise/core/persistence/crud/git.py+++ b/packages/core/src/repowise/core/persistence/crud/git.py@@ -7,7 +7,7 @@ every public name, so existing imports are unaffected.from __future__ import annotationsfrom collections.abc import Sequence-from datetime import datetime+from datetime import UTC, datetimefrom sqlalchemy import delete, func, or_, select, textfrom sqlalchemy.ext.asyncio import AsyncSession@@ -134,6 +134,13 @@ _DEAD_CODE_GIT_FIELDS = ()+def _as_aware_utc(value: datetime | None) -> datetime | None:+ """Treat a naive stored timestamp as the UTC it was written as."""+ if value is None or value.tzinfo is not None:+ return value+ return value.replace(tzinfo=UTC)++async def get_dead_code_git_fields(session: AsyncSession, repository_id: str) -> dict[str, dict]:"""Return ``{file_path: {field: value}}`` for the dead-code scoring fields.@@ -151,6 +158,15 @@ async def get_dead_code_git_fields(session: AsyncSession, repository_id: str) ->The values are one update-interval stale for idle files, whose time-decayedwindows are rewritten later in the same run, after analysis. That isstrictly closer to the truth than the empty dict it replaces.++ ``last_commit_at`` comes back tz-aware. SQLite drops the offset from+ ``DateTime(timezone=True)``, and the analyzer merges these rows with freshly+ read git metadata, which *is* aware — so a naive value here meets an aware+ one in ``datetime.now(UTC) - last_commit`` and in the ``>`` that picks a+ package's newest commit, and raises ``TypeError``. The dead-code phase+ catches broadly, so the whole analysis was skipped for a one-line warning+ and every finding kept its previous verdict. Normalizing at the read is what+ keeps that from reaching either site."""rows = (await session.execute(@@ -160,7 +176,13 @@ async def get_dead_code_git_fields(session: AsyncSession, repository_id: str) ->).where(GitMetadata.repository_id == repository_id))).all()- return {r[0]: dict(zip(_DEAD_CODE_GIT_FIELDS, r[1:], strict=True)) for r in rows}+ return {+ r[0]: {+ f: _as_aware_utc(v) if f == "last_commit_at" else v+ for f, v in zip(_DEAD_CODE_GIT_FIELDS, r[1:], strict=True)+ }+ for r in rows+ }# Repo-wide-walk signals (co-change pairs, Hassan entropy). A pass that diddiff --git a/tests/unit/persistence/test_dead_code_crud.py b/tests/unit/persistence/test_dead_code_crud.pyindex 79d8669c8..da43beca3 100644--- a/tests/unit/persistence/test_dead_code_crud.py+++ b/tests/unit/persistence/test_dead_code_crud.py@@ -386,3 +386,39 @@ async def test_summary_uses_shared_confidence_thresholds(async_session, monkeypa"medium": 2,"low": 1,}+++async def test_get_dead_code_git_fields_returns_last_commit_at_aware(async_session):+ """A stored timestamp must come back comparable to fresh git metadata.++ SQLite drops the offset from ``DateTime(timezone=True)``, so this read used+ to hand the analyzer a naive value. It merges those rows with freshly read+ git metadata, which is aware, and then does ``datetime.now(UTC) - value``+ when ageing a zombie package and ``>`` when picking the package's newest+ commit. Either raises ``TypeError: can't subtract offset-naive and+ offset-aware datetimes``, which the dead-code phase catches broadly — the+ whole analysis is skipped and every finding silently keeps its previous+ verdict on every incremental update.+ """+ from datetime import UTC, datetime++ from repowise.core.persistence.crud import get_dead_code_git_fields+ from repowise.core.persistence.models import GitMetadata++ repo = await insert_repo(async_session)+ async_session.add(+ GitMetadata(+ repository_id=repo.id,+ file_path="a.py",+ last_commit_at=datetime(2025, 12, 21, 0, 6, 49, tzinfo=UTC),+ )+ )+ await async_session.commit()++ stored = (await get_dead_code_git_fields(async_session, repo.id))["a.py"]["last_commit_at"]++ assert stored.tzinfo is not None, "a naive value here skips the whole dead-code analysis"+ # The two operations that raised, exactly as the analyzer performs them.+ assert (datetime.now(UTC) - stored).days >= 0+ assert stored > datetime(2020, 1, 1, tzinfo=UTC)+ assert stored == datetime(2025, 12, 21, 0, 6, 49, tzinfo=UTC), "must not shift the instant"