Repository navigation
fix(backend): honor user timezone in conversation metadata + rendering (wrong times/dates in chat) - #10012
Conversation
Two timezone-correctness defects fed wrong dates/times into the chat LLM and, for timezone-less users, silently broke conversation metadata extraction. Both are the "AI states the wrong time/date" class from BasedHardware#4643 and BasedHardware#6214. 1. utils/llm/chat.py — retrieve_metadata_from_message / _from_text / _fields_from_transcript ground "today" with `created_at.astimezone(ZoneInfo(tz))`. `tz` originates from `get_user_time_zone(uid) or ''` (save_structured_vector), so a user who has not set a timezone yields `tz == ''` and `ZoneInfo('')` raises ValueError — before the LLM call — on the DEFAULT conversation path (_fields_from_transcript). That user's people/topics/entities/dates vector metadata is never written, degrading search. The raw astimezone also reinterprets a naive created_at as server-local time. Fix: delegate to the existing `date_in_tz(created_at, tz)` helper, which already falls back to UTC for a missing/invalid tz and treats a naive datetime as UTC. 2. utils/conversations/render.py — conversations_to_string rendered created_at/started_at/finished_at with `.astimezone(display_tz)` without the module's own `_as_utc()` guard (which format_local_time / format_local_date already apply for exactly this reason). A naive stored timestamp is then presented to the model as server-local wall clock, so any non-UTC backend host shows the wrong time in the conversation text fed to chat. Fix: wrap each timestamp in `_as_utc()`. Tests: - test_conversations_to_string.py: TestConversationsToStringNaiveTimestamps forces a non-UTC process TZ (America/Los_Angeles) so the naive-as-local defect is deterministic, and asserts naive timestamps render as UTC and convert correctly to a user tz. Fails on the pre-fix code. - test_temporal_date_in_tz.py: locks the date_in_tz contract the chat.py fix relies on — empty/None/invalid tz fall back to UTC without raising (the exact input that crashed the old expression), and naive datetimes are read as UTC. Verification: python -m pytest tests/unit/test_conversations_to_string.py \ tests/unit/test_temporal_date_in_tz.py -> 19 passed Reverting the render _as_utc fix makes the two naive tests fail. black --line-length 120 --check: 4 files unchanged. Relates to BasedHardware#4643, BasedHardware#6214 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks @arhxam — this is a genuinely well-scoped first contribution. Clear root-cause write-up, the fix lands exactly where it should, and the contract tests are the right shape. Leaving a positive signal rather than a formal approval for the reasons at the end.
What this fixes, and why it's correct
- chat.py metadata prompts:
created_at.astimezone(ZoneInfo(tz))raisedValueErrorwhenevertz == ''(users with no saved timezone — the default conversation path), which aborted metadata extraction before the LLM call and silently dropped people/topics/entities/dates vector metadata for those users. It also re-interpreted naive timestamps as server-local. Delegating todate_in_tz(created_at, tz)fixes both: missing/None/invalid tz falls back to UTC, naive datetimes are read as UTC, and it never raises. I confirmeddate_in_tzin utils/llm/temporal.py has exactly those guarantees. - render.py
conversations_to_string: wrapping created_at/started_at/finished_at with_as_utc(...)before.astimezone(display_tz)stops naive stored timestamps being read as the host's local wall clock._as_utcalready exists in render.py and is the same helper used byformat_local_time/format_local_date, so this just makes the module consistent.
Verification notes
- All three chat.py call sites now use
date_in_tz(created_at, tz); all three render.py lines use_as_utc(...). Diff matches the description. from zoneinfo import ZoneInfois still used elsewhere in both files, so there's no dead-import regression.- The new tests (test_temporal_date_in_tz.py + TestConversationsToStringNaiveTimestamps) lock the exact invariants the fixes rely on, and forcing
TZ=America/Los_Angelesmakes the naive-as-local failure deterministic regardless of where the suite runs.
One thing for a human maintainer before merge
- The only CI signal on this PR is
cubic(neutral); the backend pytest suite does not appear to have actually executed, so the new tests haven't been exercised by automation yet. They look correct by inspection, but please confirm the suite runs green (ideally including the two new files) before merging.
No changes requested from my side — flagging needs-maintainer-review purely because this touches user-facing times and the default metadata-extraction path, and a maintainer should be the one to push the merge button. Nice work.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
|
Thanks @arhxam — this is a clean, well-scoped fix for a real defect class. Quick maintainer notes after reviewing against What this fixes (verified)
Both Tests
Coverage matches the fix surface and would fail on the pre-fix code. Status Two non-blocking notes for a follow-up, not a reason to block or split this PR:
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
Problem
Two timezone bugs feed the chat/LLM layer wrong dates and times — the "AI mentions the wrong time of day / wrong date" class reported in #4643 (and referenced in the code around #6214). One of them also silently breaks metadata extraction for any user who hasn't set a timezone.
Root cause & fix
1.
utils/llm/chat.py— metadata date-grounding crashes for timezone-less usersretrieve_metadata_from_message/retrieve_metadata_from_text/retrieve_metadata_fields_from_transcriptground the model's notion of "today" with:tzcomes fromsave_structured_vector:So a user who has not set a timezone produces
tz == '', andZoneInfo('')raisesValueErrorbefore the LLM call:retrieve_metadata_fields_from_transcriptis the default path for regular conversations, so those users'people/topics/entities/datesvector metadata is never written — degrading search/retrieval. The rawastimezoneon a possibly-naivecreated_atalso reinterprets it as server-local time.Fix: delegate to the module's existing purpose-built helper
date_in_tz(created_at, tz)(utils/llm/temporal.py), which already falls back to UTC for a missing/invalid tz and treats naive datetimes as UTC.2.
utils/conversations/render.py— naive timestamps rendered as server-localconversations_to_stringrenderscreated_at/started_at/finished_atwith.astimezone(display_tz)but skips the module's own_as_utc()guard — the guard thatformat_local_time/format_local_datealready apply for exactly this reason:created_at/started_at/finished_atare plaindatetime(often naive UTC), so on any non-UTC backend host the conversation text handed to the chat LLM shows the wrong wall clock.Fix: wrap each timestamp in
_as_utc()before.astimezone(display_tz).Tests
test_conversations_to_string.py→TestConversationsToStringNaiveTimestampsforces a non-UTC process timezone (America/Los_Angeles) viaTZ+time.tzset()so the naive-as-local defect is deterministic regardless of where CI runs, and asserts naive timestamps render as UTC and convert correctly to a user tz. These fail on the pre-fix code.test_temporal_date_in_tz.pylocks thedate_in_tzcontract thechat.pyfix now relies on: empty /None/ invalid tz fall back to UTC without raising (the exact''input that crashed the old expression), and naive datetimes are read as UTC.Verification
Note: I ran the affected backend unit tests and
blackdirectly (in a fork). The repo's maintainer-only preflight tooling (make preflight,scripts/failure-class) requires the full monorepo checkout/toolchain and was not run here.Relates to #4643, #6214
🤖 Generated with Claude Code