From 13768190918c203865ee6daa8992dd05fd49bed5 Mon Sep 17 00:00:00 2001 From: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com> Date: Thu, 16 Jul 2026 10:17:46 -0700 Subject: [PATCH] fix(corpus): normalize naive staleness timestamps Treat timezone-naive datetime metadata as UTC before comparing it with the aware staleness cutoff. Add a regression test for the crashing input. --- openagent_eval/corpus/staleness.py | 4 +++- tests/unit/test_corpus/test_staleness.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/openagent_eval/corpus/staleness.py b/openagent_eval/corpus/staleness.py index a2703d1..5ebca17 100644 --- a/openagent_eval/corpus/staleness.py +++ b/openagent_eval/corpus/staleness.py @@ -7,7 +7,7 @@ from __future__ import annotations import re -from datetime import datetime, timedelta, timezone +from datetime import UTC, datetime, timedelta, timezone from typing import Any from openagent_eval.corpus.base import BaseCorpusAnalyzer @@ -172,6 +172,8 @@ def _extract_timestamp_from_metadata( continue if isinstance(value, datetime): + if value.tzinfo is None: + return value.replace(tzinfo=UTC) return value if isinstance(value, str): diff --git a/tests/unit/test_corpus/test_staleness.py b/tests/unit/test_corpus/test_staleness.py index 1369207..374b45c 100644 --- a/tests/unit/test_corpus/test_staleness.py +++ b/tests/unit/test_corpus/test_staleness.py @@ -59,6 +59,20 @@ async def test_detects_stale_documents(self, detector, stale_doc): ) assert "stale.txt" in report.issues[0].document_ids + @pytest.mark.asyncio + async def test_naive_metadata_datetime_is_treated_as_utc(self, detector): + """Test that naive metadata datetimes do not fail UTC comparisons.""" + doc = CorpusDocument( + doc_id="naive.txt", + content="Old content.", + metadata={"last_modified": datetime(2020, 1, 15)}, + ) + + report = await detector.analyze([doc]) + + assert len(report.issues) == 1 + assert report.issues[0].metadata["last_updated"].endswith("+00:00") + @pytest.mark.asyncio async def test_health_score_decreases_with_staleness(self, detector, recent_doc, stale_doc): """Test health score decreases with more stale documents."""