Repository navigation
fix(backend): chat assistant uses user's timezone for conversation times (#6214) - #7736
Conversation
…or chat (#6214) conversations_to_string() hard-coded .astimezone(timezone.utc) + a " UTC" label, so the chat LLM saw every conversation time in UTC and answered users in UTC (e.g. 3h off for an America/Sao_Paulo user). Add an optional `tz` (IANA name) parameter: when provided, timestamps render in that zone and are labelled with it; invalid/missing zones fall back to UTC, so all existing callers are unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… tools (#6214) get_conversations_tool / search_conversations_tool feed conversation text to the chat LLM. Thread the user's timezone (notification_db.get_user_time_zone) so timestamps are localized. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…#6214) get_conversations_text / search_conversations_text (desktop/web tools router) share the same LLM-context path; localize their timestamps too. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…6214) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Greptile SummaryThis PR fixes a user-visible timezone bug (#6214) where chat answers reported conversation timestamps in UTC regardless of the user's configured timezone. The fix threads the user's IANA timezone through
Confidence Score: 4/5Safe to merge; the core change is a backward-compatible default-None parameter addition with correct UTC fallback for missing or invalid zones. The timezone resolution logic is correct and well-tested. The only concern is that the LangChain tool path now fetches the user timezone from Firestore on each individual tool invocation rather than once at session setup, which adds unnecessary round-trips in multi-turn agent sessions. This is a quality concern rather than a functional defect. backend/utils/retrieval/tools/conversation_tools.py — the Important Files Changed
Sequence DiagramsequenceDiagram
participant User as Chat User
participant Agent as LangChain Agent
participant Tool as get/search_conversations_tool
participant NotifDB as notification_db
participant ConvDB as conversations_db
participant Render as conversations_to_string
User->>Agent: "What time did X happen?"
Agent->>Tool: invoke tool(uid, ...)
Tool->>NotifDB: get_user_time_zone(uid)
NotifDB-->>Tool: "America/Sao_Paulo"
Tool->>ConvDB: get_conversations(uid, ...)
ConvDB-->>Tool: raw conversation data
Tool->>Render: "conversations_to_string(..., tz="America/Sao_Paulo")"
Note over Render: _resolve_display_tz("America/Sao_Paulo")<br/>→ ZoneInfo + label
Render-->>Tool: formatted text with local timestamps
Tool-->>Agent: "Conversation at 07:00 America/Sao_Paulo"
Agent-->>User: Answer in user's local time
Reviews (1): Last reviewed commit: "test(backend): cover timezone rendering ..." | Re-trigger Greptile |
| use_transcript=include_transcript, | ||
| include_timestamps=include_timestamps, | ||
| people=people, | ||
| tz=notification_db.get_user_time_zone(uid), |
There was a problem hiding this comment.
Extra Firestore read per tool invocation
notification_db.get_user_time_zone(uid) fires a full Firestore document read every time this tool is invoked by the LangChain agent. In a typical agentic session the agent can call get_conversations_tool and search_conversations_tool several times (pagination, follow-up queries), so the timezone is fetched repeatedly even though it never changes within a session. The same user-id is already threaded through config['configurable']; storing the resolved timezone there (alongside user_id) at session setup time would eliminate the per-call overhead entirely, consistent with how uid itself is handled. The same pattern exists in the search_conversations_tool call at line 489.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| def test_kolkata_half_hour_offset(self): | ||
| # 10:00 UTC -> 15:30 IST (UTC+5:30) | ||
| conv = _make_conversation() | ||
| result = conversations_to_string([conv], tz="Asia/Kolkata") | ||
| assert "15 Jan 2026 at 15:30 Asia/Kolkata" in result |
There was a problem hiding this comment.
The Kolkata test only checks
created_at but does not verify that started_at and finished_at are also converted to the half-hour offset. Given that the Sao Paulo test explicitly covers all three fields, it is worth doing the same here so a regression in the started_at/finished_at path is caught for non-whole-hour offsets.
| def test_kolkata_half_hour_offset(self): | |
| # 10:00 UTC -> 15:30 IST (UTC+5:30) | |
| conv = _make_conversation() | |
| result = conversations_to_string([conv], tz="Asia/Kolkata") | |
| assert "15 Jan 2026 at 15:30 Asia/Kolkata" in result | |
| def test_kolkata_half_hour_offset(self): | |
| # 10:00 UTC -> 15:30 IST (UTC+5:30) | |
| conv = _make_conversation() | |
| result = conversations_to_string([conv], tz="Asia/Kolkata") | |
| assert "15 Jan 2026 at 15:30 Asia/Kolkata" in result | |
| assert "Started: 15 Jan 2026 at 15:30 Asia/Kolkata" in result | |
| assert "Finished: 15 Jan 2026 at 16:00 Asia/Kolkata" in result |
Bug (#6214)
The chat assistant answers time questions in UTC instead of the user's local timezone. A user in
America/Sao_Paulo(UTC−3) asking "what time did X happen?" gets an answer 3 hours off. Users lose trust in timing answers.Root cause
conversations_to_string()(backend/utils/conversations/render.py) — which builds the conversation text fed to the chat LLM — hard-coded.astimezone(timezone.utc)and appended a literal" UTC"label forcreated_at/started_at/finished_at. So every conversation timestamp in the LLM's context was in UTC, regardless of the user's configured timezone. The model then reports those UTC times verbatim.Fix
tz(IANA name, e.g.America/Sao_Paulo) parameter toconversations_to_string(). When provided, timestamps render in that zone and are labelled with it; a missing/invalid zone falls back to UTC. All existing callers are unchanged (defaulttz=None→ UTC behaviour preserved).notification_db.get_user_time_zone(uid)— the same helper already used inutils/llm/chat.py) into the four chat/tools LLM-context call sites:utils/retrieval/tools/conversation_tools.py—get_conversations_tool,search_conversations_tool(agentic chat)utils/retrieval/tool_services/conversations.py—get_conversations_text,search_conversations_text(desktop/web tools router)Non-chat callers of
conversations_to_string(app integrations, post-processing RAG context) intentionally keep UTC — they aren't user-facing time answers.Tests
Added
TestConversationsToStringTimezonetotests/unit/test_conversations_to_string.py(already run bytest.sh): default→UTC,America/Sao_Paulo(10:00 UTC→07:00),Asia/Kolkatahalf-hour offset (→15:30), and invalid-zone fallback. Verified the tz-resolution logic locally;blackclean. pytest isn't installed in the watchdog env, so CI runs the suite — UNVERIFIED at runtime against a live chat session.🤖 automated by hourly watchdog; opened for review, not merged.