Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(news): fail loud on unparseable created_at instead of silent "Recently" (#4370)

LearningCircuitecb8bd04516a
diff --git a/changelog.d/+format-time-ago-fail-loud.misc.md b/changelog.d/+format-time-ago-fail-loud.misc.md
new file mode 100644
index 0000000000..34c058a5b8
--- /dev/null
+++ b/changelog.d/+format-time-ago-fail-loud.misc.md
@@ -0,0 +1 @@
+The news-feed "time ago" formatter no longer swallows unparseable timestamps behind a silent "Recently" label. Since `created_at` is always written as a valid ISO timestamp, a value that won't parse means a corrupt row — that row is now logged and skipped by the feed builder instead of rendering with a misleading time.
diff --git a/src/local_deep_research/news/api.py b/src/local_deep_research/news/api.py
index 80e254d888..3927c6cc48 100644
--- a/src/local_deep_research/news/api.py
+++ b/src/local_deep_research/news/api.py
@@ -156,7 +156,9 @@ def get_news_feed(
"title": r.title
if hasattr(r, "title")
else None, # Include title field if exists
- "created_at": r.created_at if r.created_at else None,
+ # created_at is NOT NULL (set to isoformat() on every
+ # insert), so it's always a usable timestamp string.
+ "created_at": r.created_at,
"completed_at": r.completed_at
if r.completed_at
else None,
@@ -596,39 +598,37 @@ def get_subscription_history(
def _format_time_ago(timestamp: str) -> str:
- """Format timestamp as 'X hours ago' string."""
- # ResearchHistory.created_at can be empty/None on legacy rows. A missing
- # timestamp isn't an error worth a full traceback per news item — return a
- # neutral label instead of letting parser.parse(None) raise into the
- # except below (which logged a stack trace for every such row).
- if not timestamp:
- return "Recently"
- try:
- from dateutil import parser
-
- dt = parser.parse(timestamp)
-
- # If dt is naive, assume it's in UTC
- if dt.tzinfo is None:
- dt = dt.replace(tzinfo=timezone.utc)
-
- now = datetime.now(timezone.utc)
- diff = now - dt
-
- if diff.days > 0:
- return f"{diff.days} day{'s' if diff.days > 1 else ''} ago"
- if diff.seconds > 3600:
- hours = diff.seconds // 3600
- return f"{hours} hour{'s' if hours > 1 else ''} ago"
- if diff.seconds > 60:
- minutes = diff.seconds // 60
- return f"{minutes} minute{'s' if minutes > 1 else ''} ago"
- return "Just now"
- except Exception:
- # Unparseable but non-empty value — note it without a traceback so a
- # feed full of bad timestamps can't bury real errors in the log.
- logger.warning(f"Could not parse news timestamp '{timestamp}'")
- return "Recently"
+ """Format timestamp as 'X hours ago' string.
+
+ Raises on unparseable input instead of masking it with a neutral label.
+ ResearchHistory.created_at is a NOT NULL column written as
+ datetime.now(UTC).isoformat() on every insert path, so a value that won't
+ parse means the row is corrupt — not a routine edge case. The only caller
+ (the per-row loop in get_news_feed) already wraps each row in a
+ try/except that logs the failure and skips the row, so a bad timestamp
+ surfaces in the logs and drops that one card rather than rendering it with
+ a misleading "Recently".
+ """
+ from dateutil import parser
+
+ dt = parser.parse(timestamp)
+
+ # If dt is naive, assume it's in UTC
+ if dt.tzinfo is None:
+ dt = dt.replace(tzinfo=timezone.utc)
+
+ now = datetime.now(timezone.utc)
+ diff = now - dt
+
+ if diff.days > 0:
+ return f"{diff.days} day{'s' if diff.days > 1 else ''} ago"
+ if diff.seconds > 3600:
+ hours = diff.seconds // 3600
+ return f"{hours} hour{'s' if hours > 1 else ''} ago"
+ if diff.seconds > 60:
+ minutes = diff.seconds // 60
+ return f"{minutes} minute{'s' if minutes > 1 else ''} ago"
+ return "Just now"
def get_subscription(subscription_id: str) -> Optional[Dict[str, Any]]:
diff --git a/tests/news/test_api_functions.py b/tests/news/test_api_functions.py
index ce6d708379..88b95f3b6e 100644
--- a/tests/news/test_api_functions.py
+++ b/tests/news/test_api_functions.py
@@ -465,12 +465,11 @@ class TestFormatTimeAgo:
assert "day" in result
def test_format_time_ago_invalid_timestamp(self):
- """Test handling of invalid timestamp."""
+ """Invalid timestamps raise (caller logs + skips the row)."""
from local_deep_research.news.api import _format_time_ago
- result = _format_time_ago("not a valid timestamp")
-
- assert result == "Recently"
+ with pytest.raises(ValueError):
+ _format_time_ago("not a valid timestamp")
def test_format_time_ago_timezone_handling(self):
"""Test handling of timestamps without timezone."""
diff --git a/tests/news/test_api_module_extended.py b/tests/news/test_api_module_extended.py
index 4084c025d5..d5c0fbb597 100644
--- a/tests/news/test_api_module_extended.py
+++ b/tests/news/test_api_module_extended.py
@@ -190,11 +190,11 @@ class TestPrivateFormatTimeAgo:
assert isinstance(result, str)
def test_handles_invalid_input(self):
- """Test handles invalid input gracefully."""
+ """Invalid input raises (caller logs + skips the row)."""
from local_deep_research.news.api import _format_time_ago
- result = _format_time_ago("not-a-date")
- assert isinstance(result, str)
+ with pytest.raises(ValueError):
+ _format_time_ago("not-a-date")
class TestCreateSubscription:
diff --git a/tests/news/test_format_time_ago_behavior.py b/tests/news/test_format_time_ago_behavior.py
index ad79e0d312..69ebfc3a57 100644
--- a/tests/news/test_format_time_ago_behavior.py
+++ b/tests/news/test_format_time_ago_behavior.py
@@ -5,6 +5,8 @@ Tests time formatting, edge cases, pluralization, timezone handling, and error r
from datetime import datetime, timedelta, timezone
+import pytest
+
from local_deep_research.news.api import _format_time_ago
@@ -188,27 +190,32 @@ class TestFormatTimeAgoTimezone:
class TestFormatTimeAgoErrorHandling:
- """Tests for error recovery."""
+ """Unparseable input raises rather than returning a neutral label.
+
+ created_at is NOT NULL and always written as isoformat(), so anything
+ that won't parse is corrupt data. The per-row loop in get_news_feed
+ catches the raise, logs it, and skips the row.
+ """
- def test_invalid_string_returns_recently(self):
- result = _format_time_ago("not-a-timestamp")
- assert result == "Recently"
+ def test_invalid_string_raises(self):
+ with pytest.raises(ValueError):
+ _format_time_ago("not-a-timestamp")
- def test_empty_string_returns_recently(self):
- result = _format_time_ago("")
- assert result == "Recently"
+ def test_empty_string_raises(self):
+ with pytest.raises(ValueError):
+ _format_time_ago("")
- def test_none_returns_recently(self):
- result = _format_time_ago(None)
- assert result == "Recently"
+ def test_none_raises(self):
+ with pytest.raises(TypeError):
+ _format_time_ago(None)
- def test_integer_returns_recently(self):
- result = _format_time_ago(12345)
- assert result == "Recently"
+ def test_integer_raises(self):
+ with pytest.raises(TypeError):
+ _format_time_ago(12345)
- def test_gibberish_returns_recently(self):
- result = _format_time_ago("xyz123!@#")
- assert result == "Recently"
+ def test_gibberish_raises(self):
+ with pytest.raises(ValueError):
+ _format_time_ago("xyz123!@#")
# --- Input format variants ---
diff --git a/tests/news/test_news_api.py b/tests/news/test_news_api.py
index b0443dc7a7..4e36508e91 100644
--- a/tests/news/test_news_api.py
+++ b/tests/news/test_news_api.py
@@ -940,10 +940,8 @@ class TestTimeFormatting:
assert "day" in result.lower()
def test_format_time_ago_none(self):
- """Test formatting time with None input."""
+ """None is not a valid timestamp and raises (caller logs + skips)."""
from local_deep_research.news.api import _format_time_ago
- # None will cause an error, returns "Recently" on exception
- result = _format_time_ago(None)
-
- assert result == "Recently"
+ with pytest.raises(TypeError):
+ _format_time_ago(None)
diff --git a/tests/news/test_news_api_extended.py b/tests/news/test_news_api_extended.py
index 26634a5127..a6d68909e6 100644
--- a/tests/news/test_news_api_extended.py
+++ b/tests/news/test_news_api_extended.py
@@ -492,12 +492,11 @@ class TestFormatTimeAgo:
assert "1 minute ago" in result
def test_format_invalid_timestamp(self):
- """Test formatting with invalid timestamp."""
+ """Invalid timestamps raise (caller logs + skips the row)."""
from local_deep_research.news.api import _format_time_ago
- result = _format_time_ago("invalid-timestamp")
-
- assert result == "Recently"
+ with pytest.raises(ValueError):
+ _format_time_ago("invalid-timestamp")
def test_format_naive_datetime(self):
"""Test formatting with naive datetime string assumes UTC."""
@@ -1074,7 +1073,6 @@ class TestFormatTimeAgoExtended:
result = _format_time_ago(future.isoformat())
# Negative diff.days would be -1, so it's not > 0
- # Future times should show "Just now" or "Recently"
assert result is not None
def test_format_very_old_timestamp(self):