Skip to content

fix(backend): chat assistant uses user's timezone for conversation times (#6214) - #7736

Merged
kodjima33 merged 4 commits into
mainfrom
watchdog/issue-6214-chat-tz
Jun 9, 2026
Merged

kodjima33 merged 4 commits into
mainfrom
watchdog/issue-6214-chat-tz

Conversation

@kodjima33

Copy link
Copy Markdown
Collaborator

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 for created_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

  • Add an optional tz (IANA name, e.g. America/Sao_Paulo) parameter to conversations_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 (default tz=None → UTC behaviour preserved).
  • Thread the user's timezone (notification_db.get_user_time_zone(uid) — the same helper already used in utils/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 TestConversationsToStringTimezone to tests/unit/test_conversations_to_string.py (already run by test.sh): default→UTC, America/Sao_Paulo (10:00 UTC→07:00), Asia/Kolkata half-hour offset (→15:30), and invalid-zone fallback. Verified the tz-resolution logic locally; black clean. 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.

kodjima33 and others added 4 commits June 9, 2026 14:13
…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]>
@greptile-apps

greptile-apps Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This 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 conversations_to_string and all four chat/tools call sites that build LLM context.

  • render.py: adds _resolve_display_tz(tz) helper with correct fallback to UTC for None/invalid zones, and a new tz parameter on conversations_to_string; existing callers are unaffected.
  • tools/conversation_tools.py and tool_services/conversations.py: each of the four modified call sites now fetches the user timezone via notification_db.get_user_time_zone(uid) inline; for the LangChain tool path this adds a Firestore read on every agent tool invocation rather than once per session.
  • Unit tests cover UTC default, whole-hour offset (Sao Paulo), half-hour offset (Kolkata), and invalid-zone fallback; the Kolkata test does not yet assert started_at/finished_at conversion.

Confidence Score: 4/5

Safe 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 notification_db.get_user_time_zone call inside both tool functions adds a Firestore read on every agent invocation.

Important Files Changed

Filename Overview
backend/utils/conversations/render.py Added _resolve_display_tz helper and tz parameter to conversations_to_string; handles None/invalid zones by falling back to UTC; logic is correct.
backend/utils/retrieval/tools/conversation_tools.py Threads notification_db.get_user_time_zone(uid) into both tool call sites; adds one extra Firestore read per tool invocation instead of reusing a session-level value.
backend/utils/retrieval/tool_services/conversations.py Threads notification_db.get_user_time_zone(uid) into both service call sites; called once per API request, so the extra read is acceptable.
backend/tests/unit/test_conversations_to_string.py Good coverage for UTC default, Sao Paulo (whole-hour) offset, Kolkata (half-hour) offset, and invalid-zone fallback; Kolkata test only asserts created_at, leaving started_at/finished_at untested for that case.

Sequence Diagram

sequenceDiagram
    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
Loading

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),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

Comment on lines +143 to +147
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Suggested change
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

@kodjima33
kodjima33 merged commit 94bbf32 into main Jun 9, 2026
3 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-6214-chat-tz branch June 9, 2026 18:28
@undivisible undivisible added the AI label Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants