Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix(reddit): pick fetch bucket by age of from_date, not span alone

Trevin Chowa21c463b6f2a
diff --git a/skills/last30days/scripts/lib/reddit.py b/skills/last30days/scripts/lib/reddit.py
index 741bac32..ddb37b03 100644
--- a/skills/last30days/scripts/lib/reddit.py
+++ b/skills/last30days/scripts/lib/reddit.py
@@ -13,6 +13,7 @@ import sys
import time
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, as_completed, wait as futures_wait
+from datetime import date, datetime, timezone
from typing import Any, Dict, List, Optional, Set
def _first_of(*values, default=None):
@@ -456,30 +457,49 @@ def _dedupe_posts(posts: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
_TIMEFRAME_ORDER = {"hour": 0, "day": 1, "week": 2, "month": 3, "year": 4, "all": 5}
+def _days_to_reddit_bucket(days: float) -> str:
+ """Map a day count onto the smallest Reddit rolling bucket that covers it.
+
+ Adds one day of slack so calendar windows that cross a day boundary still
+ fit inside Reddit's rolling ``t=`` buckets (a yesterday→today request needs
+ ``week``, not ``day``).
+ """
+ covered = days + 1
+ if covered <= 1:
+ return "day"
+ if covered <= 7:
+ return "week"
+ if covered <= 31:
+ return "month"
+ if covered <= 366:
+ return "year"
+ return "all"
+
+
def _window_to_time_filter(from_date: str, to_date: str) -> str:
"""Map a requested YYYY-MM-DD window onto Reddit's coarse `t` param.
- Reddit's ``t=day|week|month`` buckets are rolling windows ending "now", not
- calendar spans. A one-day request often covers yesterday→today, so ``t=day``
- (last 24h) under-fetches yesterday morning and the later date filter cannot
- recover those posts. Round up one bucket so the fetch covers the calendar
- window; Phase 5 still trims to ``from_date``/``to_date``. Falls back to
- ``month`` (previous behaviour) if the dates don't parse.
+ Reddit's ``t=day|week|month`` buckets are rolling windows ending *now*, not
+ calendar spans and not anchored to ``to_date``. Coverage therefore needs:
+
+ 1. Span — a yesterday→today request needs more than rolling ``t=day``.
+ 2. Historical reach — a one-day request ending two weeks ago still needs a
+ bucket that reaches ``from_date``; span-alone would pick ``week`` and
+ the API would omit the entire requested range.
+
+ Take the wider of the two; the caller then mins with the depth default.
+ Phase 5 still trims to ``from_date``/``to_date``. Falls back to ``month``
+ if the dates don't parse.
"""
try:
- span = (time.mktime(time.strptime(to_date, "%Y-%m-%d"))
- - time.mktime(time.strptime(from_date, "%Y-%m-%d"))) / 86400
+ start = date.fromisoformat(from_date)
+ end = date.fromisoformat(to_date)
except (ValueError, TypeError):
return "month"
- if span <= 1:
- return "week"
- if span <= 7:
- return "month"
- if span <= 31:
- return "year"
- if span <= 366:
- return "year"
- return "all"
+ span_days = max(0, (end - start).days)
+ # Age of from_date relative to "today" — Reddit always anchors to now.
+ age_days = max(0, (datetime.now(timezone.utc).date() - start).days)
+ return _days_to_reddit_bucket(max(span_days, age_days))
def search_reddit(
diff --git a/tests/test_reddit.py b/tests/test_reddit.py
index 1028027c..9dbaa23a 100644
--- a/tests/test_reddit.py
+++ b/tests/test_reddit.py
@@ -264,10 +264,43 @@ if __name__ == "__main__":
unittest.main()
-def test_window_to_time_filter_rounds_up_for_rolling_buckets():
- """Calendar windows need the next Reddit rolling bucket (Greptile on #860)."""
+def test_window_to_time_filter_rounds_up_for_rolling_buckets(monkeypatch):
+ """Recent calendar windows need the next Reddit rolling bucket (Greptile on #860)."""
+ from datetime import date
from lib import reddit
+ class _FrozenDateTime:
+ @staticmethod
+ def now(tz=None):
+ class _D:
+ @staticmethod
+ def date():
+ return date(2026, 7, 24)
+ return _D()
+
+ monkeypatch.setattr(reddit, "datetime", _FrozenDateTime)
assert reddit._window_to_time_filter("2026-07-23", "2026-07-24") == "week"
assert reddit._window_to_time_filter("2026-07-17", "2026-07-24") == "month"
- assert reddit._window_to_time_filter("2026-06-24", "2026-07-24") == "year"
+ assert reddit._window_to_time_filter("2026-06-24", "2026-07-24") == "month"
+ # Just past the month bucket (+1 slack) needs year.
+ assert reddit._window_to_time_filter("2026-06-23", "2026-07-24") == "year"
+
+
+def test_window_to_time_filter_covers_historical_from_date(monkeypatch):
+ """Short historical windows must still reach from_date (Greptile follow-up on #860)."""
+ from datetime import date
+ from lib import reddit
+
+ class _FrozenDateTime:
+ @staticmethod
+ def now(tz=None):
+ class _D:
+ @staticmethod
+ def date():
+ return date(2026, 7, 24)
+ return _D()
+
+ monkeypatch.setattr(reddit, "datetime", _FrozenDateTime)
+ # One-day request ending two weeks ago: span alone would pick "week" and
+ # miss the entire range; age of from_date requires "month".
+ assert reddit._window_to_time_filter("2026-07-09", "2026-07-10") == "month"