Fix: treat naive as_of/now datetimes as UTC in recency scoring#1009
Fix: treat naive as_of/now datetimes as UTC in recency scoring#1009AdityaPainuli wants to merge 1 commit into
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI (base), Organization UI (inherited) Review profile: ASSERTIVE Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Putting this as a draft as waiting for confirmation on the issue itself. |
Closes #1008
Problem
MCP
context_resolve/context_searchwith a date-only or naive ISOas_of(e.g."2024-06-01") crashes the whole read:_parse_as_of_isoreturns a naive datetime, readers pass it to the ranker as its clock (TaskContext(now=req.as_of)), and_recency_scorenormalizesvalid_atto tz-aware but never normalizesnow— sonow - valid_atraisesTypeError: can't subtract offset-naive and offset-aware datetimesfor any claim carrying avalid_at."2024-06-01"and"2024-06-01T00:00:00Z"are the same instant; one worked, the other crashed.What changed
Two small, layered fixes:
domain/ranking.py: added a module-private_ensure_utc()helper.rank()now normalizes a caller-suppliednow, and_recency_scorereuses the same helper forvalid_at(replacing the previous inline normalization). Naive and aware datetimes can no longer mix inside the ranker, regardless of entry point.adapters/inbound/mcp/server.py:_parse_as_of_isointerprets naive timestamps as UTC and always returns a tz-aware datetime — consistent with how naivevalid_atvalues were already treated. Explicit offsets and theZsuffix are preserved as before.The boundary fix makes MCP input semantics explicit (naive = UTC); the domain fix hardens the ranker for any other caller.
Behavior
as_of="2024-06-01"TypeError, whole read failsas_of="2024-06-01T10:30:00"TypeErroras_of="2024-06-01T00:00:00Z"as_of="2024-06-01T10:30:00+05:30"Testing
nowagainst aware and naivevalid_atno longer crashes, and a naivenowscores identically to its aware-UTC equivalent (same recency breakdown).test_mcp_as_of_parsing.py: every acceptedas_ofform parses tz-aware; naive input maps to UTC; explicit offset andZhandling preserved; blank/None returns None.ruff checkandruff formatclean (repo pre-commit config, v0.14.3).