AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary
Python ai-promptsai-skillblueskyclaudeclaude-codeclawhubdeep-researchhackernewsinstagramopenclawpolymarketrecencyredditresearchsocial-mediatiktoktrendstwitterweb-searchyoutube
fix(grounding): parse Serper's relative dates instead of dropping the results (#1156)
sunnyseed235cae291d5e
diff --git a/changelog.d/+serper-relative-dates.fixed.md b/changelog.d/+serper-relative-dates.fixed.mdnew file mode 100644index 00000000..edb9bde5--- /dev/null+++ b/changelog.d/+serper-relative-dates.fixed.md@@ -0,0 +1 @@+The `serper` web backend no longer discards every recent result. Google renders a relative date ("2 hours ago", "1 day ago") for fresh results, which the date parser did not understand, so the in-range check dropped them — the newer the result, the more likely it was thrown away. On a one-day research window the web lane came back empty every time, reported as `Web: 0 items (no results)` rather than as a failure. Relative dates now resolve against the query date, which is what the label states an age relative to, so a `--as-of` search of a past window keeps the results that belong to it. A sub-day label is subtracted from the clock rather than rounded up to the current date, so shortly after midnight "23 hours ago" still names yesterday, and an age too large to represent reads as an unparseable date instead of aborting the rest of the results. The `brave` and `exa` backends were unaffected, since they return ISO timestamps.diff --git a/skills/last30days/scripts/lib/grounding.py b/skills/last30days/scripts/lib/grounding.pyindex 2dd6744b..99ec6475 100644--- a/skills/last30days/scripts/lib/grounding.py+++ b/skills/last30days/scripts/lib/grounding.py@@ -2,10 +2,11 @@from __future__ import annotations+import reimport sysimport urllib.parsefrom dataclasses import dataclass-from datetime import datetime+from datetime import datetime, timedeltafrom urllib.parse import urlparsefrom . import dates, env, http, parallel_mcp, schema, web_search_keyless@@ -203,7 +204,63 @@ def parallel_search(return items, artifact-def _parse_serper_date(raw: str) -> str | None:+_SERPER_RELATIVE_RE = re.compile(+ r"^(?:about\s+)?(\d+)\s+(minute|min|hour|hr|day|week|month|year)s?\s+ago$", re.I+)+_SERPER_RELATIVE_DAYS = {"day": 1, "week": 7, "month": 30, "year": 365}+_SERPER_RELATIVE_WORDS = {"just now": 0, "today": 0, "yesterday": 1}+++def _now() -> datetime:+ """Query-time clock. Isolated so tests can pin it."""+ return datetime.now()+++def _parse_serper_relative(raw: str, now: datetime | None = None) -> str | None:+ """Resolve Serper's relative dates ("2 hours ago") to an ISO date.++ Google renders a relative date for recent results, so the fresher a result+ is the more likely it arrives in this form. Left unparsed it becomes None+ and `_in_date_range` drops the item, which silently empties the web lane on+ short research windows.++ The label states the result's age **at query time**, so it resolves against+ the clock rather than against the end of the research window. The two differ+ whenever `--as-of` names a past window: a result inside a window ending two+ weeks ago is still labelled with its age as of today, and anchoring to the+ window end would place it a further two weeks back and discard it.++ Sub-day units ("N minutes/hours ago") are subtracted from the clock rather+ than rounded up to the current date, which would name the wrong day across+ midnight: at 00:30 "23 hours ago" is yesterday. Residual slack comes only+ from the timezone `_now()` reads against Google's.++ An age large enough to overflow `timedelta` or run past the end of `date`+ reads as unparseable, like any other junk label, so one malformed value+ cannot abort the whole result loop in `serper_search`.+ """+ anchor = now or _now()+ text = raw.strip().lower()+ if text in _SERPER_RELATIVE_WORDS:+ return (anchor.date() - timedelta(days=_SERPER_RELATIVE_WORDS[text])).isoformat()+ m = _SERPER_RELATIVE_RE.match(text)+ if not m:+ return None+ unit = {"min": "minute", "hr": "hour"}.get(m.group(2), m.group(2))+ try:+ amount = int(m.group(1))+ if unit == "minute":+ return (anchor - timedelta(minutes=amount)).date().isoformat()+ if unit == "hour":+ return (anchor - timedelta(hours=amount)).date().isoformat()+ return (+ anchor.date() - timedelta(days=amount * _SERPER_RELATIVE_DAYS[unit])+ ).isoformat()+ except (OverflowError, ValueError):+ return None+++def _parse_serper_date(raw: str, now: datetime | None = None) -> str | None:if not raw:return Nonenormalized = _normalize_date(raw)@@ -214,7 +271,7 @@ def _parse_serper_date(raw: str) -> str | None:return datetime.strptime(raw.strip(), fmt).date().isoformat()except ValueError:continue- return None+ return _parse_serper_relative(raw, now)diff --git a/tests/test_grounding_v3.py b/tests/test_grounding_v3.pyindex 28cb4da3..2d824c60 100644--- a/tests/test_grounding_v3.py+++ b/tests/test_grounding_v3.py@@ -5,6 +5,7 @@ import unittestimport urllib.errorfrom contextlib import contextmanagerfrom http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from datetime import datetimefrom unittest.mock import patchfrom lib import grounding, parallel_mcp@@ -76,6 +77,141 @@ class SerperSearchTests(unittest.TestCase):self.assertEqual("2026-03-15", items[0]["date"])self.assertEqual("serper", artifact["label"])+ def test_serper_search_keeps_relative_dates(self):+ """Google renders recent results with a relative date, not an absolute one.++ Before these were parsed, every fresh result failed the in-range check+ and the web lane came back empty on short windows.+ """+ mock_response = {+ "organic": [+ {+ "title": "Two Hours Old",+ "link": "https://example.com/hours",+ "snippet": "Fresh enough that Google shows an hour count",+ "date": "2 hours ago",+ },+ {+ "title": "One Day Old",+ "link": "https://example.com/day",+ "snippet": "Yesterday, relative to the window end",+ "date": "1 day ago",+ },+ {+ "title": "Too Old",+ "link": "https://example.com/stale",+ "snippet": "Resolves before the window start",+ "date": "3 weeks ago",+ },+ ]+ }+ with patch("lib.grounding.http.request", return_value=mock_response), \+ patch("lib.grounding._now", return_value=datetime(2026, 3, 27, 12, 0)):+ items, _ = grounding.serper_search("test", ("2026-03-26", "2026-03-27"), "fake-key")+ self.assertEqual(2, len(items))+ self.assertEqual("Two Hours Old", items[0]["title"])+ self.assertEqual("2026-03-27", items[0]["date"])+ self.assertEqual("One Day Old", items[1]["title"])+ self.assertEqual("2026-03-26", items[1]["date"])+++ def test_serper_search_keeps_valid_result_after_overlong_relative_age(self):+ response = {+ "organic": [+ {"title": "Malformed", "link": "https://example.com/bad",+ "date": "9" * 5000 + " days ago"},+ {"title": "Valid", "link": "https://example.com/good",+ "date": "1 day ago"},+ ]+ }+ with patch("lib.grounding.http.request", return_value=response), \+ patch("lib.grounding._now", return_value=datetime(2026, 3, 27, 12, 0)):+ items, artifact = grounding.serper_search(+ "test", ("2026-03-26", "2026-03-27"), "fake-key"+ )+ self.assertEqual(["https://example.com/good"], [item["url"] for item in items])+ self.assertEqual("2026-03-26", items[0]["date"])+ self.assertEqual(1, artifact["resultCount"])+++class SerperRelativeDateTests(unittest.TestCase):+ NOON = datetime(2026, 3, 27, 12, 0)++ def test_absolute_formats_are_unchanged(self):+ for raw, expected in (+ ("Mar 15, 2026", "2026-03-15"),+ ("March 15, 2026", "2026-03-15"),+ ("2026-03-15", "2026-03-15"),+ ):+ with self.subTest(raw=raw):+ self.assertEqual(expected, grounding._parse_serper_date(raw, self.NOON))++ def test_relative_units_resolve_against_query_time(self):+ for raw, expected in (+ ("45 minutes ago", "2026-03-27"),+ ("2 hours ago", "2026-03-27"),+ ("1 day ago", "2026-03-26"),+ ("5 days ago", "2026-03-22"),+ ("2 weeks ago", "2026-03-13"),+ ("3 months ago", "2025-12-27"),+ ("about 2 days ago", "2026-03-25"),+ ("today", "2026-03-27"),+ ("yesterday", "2026-03-26"),+ ):+ with self.subTest(raw=raw):+ self.assertEqual(expected, grounding._parse_serper_date(raw, self.NOON))++ def test_historical_window_dates_against_query_time(self):+ """A relative label describes age at query time, not age at the window end.++ Searching a past window with --as-of still runs today, so Serper labels a+ result from the window with its age as of today. Anchoring to the window+ end would place it before the window and discard it.+ """+ self.assertEqual(+ "2026-03-15",+ grounding._parse_serper_date("12 days ago", now=datetime(2026, 3, 27, 12, 0)),+ )++ def test_unparseable_and_missing_dates_still_return_none(self):+ for raw in ("", "sometime last spring", "ago 2 days", "many hours ago"):+ with self.subTest(raw=raw):+ self.assertIsNone(grounding._parse_serper_date(raw, self.NOON))++ def test_defaults_to_the_current_clock(self):+ with patch("lib.grounding._now", return_value=datetime(2026, 3, 27, 12, 0)):+ self.assertEqual("2026-03-26", grounding._parse_serper_date("1 day ago"))++ def test_sub_day_labels_cross_the_midnight_boundary(self):+ """A sub-day age is exact, so it must not be rounded up to the query date.++ Just after midnight, "23 hours ago" names yesterday. Stamping it with+ today's date both misreports the publication date downstream and lets a+ one-day window admit a result that falls outside it.+ """+ just_after_midnight = datetime(2026, 3, 27, 0, 30)+ for raw, expected in (+ ("23 hours ago", "2026-03-26"),+ ("20 minutes ago", "2026-03-27"),+ ("2 hours ago", "2026-03-26"),+ ):+ with self.subTest(raw=raw):+ self.assertEqual(+ expected, grounding._parse_serper_date(raw, just_after_midnight)+ )++ def test_absurd_ages_are_unparseable_rather_than_fatal(self):+ """One malformed label must not take the whole web lane down with it.++ The amount is unbounded, and a large enough one overflows `timedelta` or+ runs off the end of `date`. That has to read as an unparseable date, the+ same as any other junk value, not as an exception escaping the result+ loop in `serper_search`.+ """+ for raw in ("999999999 years ago", "3000000 days ago", "99999999999 hours ago"):+ with self.subTest(raw=raw):+ self.assertIsNone(grounding._parse_serper_date(raw, self.NOON))+class ExaSearchTests(unittest.TestCase):def test_exa_search_filters_to_in_range_dated_items(self):