Skip to content

fix(backend): render chat tool timestamps in the user's timezone (#6214) - #9719

Merged
kodjima33 merged 1 commit into
mainfrom
issues-improver/6214-chat-tool-timestamps-user-timezone
Jul 15, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
issues-improver/6214-chat-tool-timestamps-user-timezone

Conversation

@kodjima33

@kodjima33 kodjima33 commented Jul 14, 2026 •

Copy link
Copy Markdown
Collaborator

What

Chat/agent tool results that still emitted bare UTC timestamps now render in the user's configured timezone, with an explicit timezone label.

Three surfaces were leaking UTC:

Surface Before After
get_action_items_text (/v1/tools/action-items — desktop, web, MCP agent clients) Due: 2026-06-26 22:00:00 Due: 2026-06-26 19:00:00 America/Sao_Paulo
search_memories_text (same REST surface) date: 2026-06-27 date: 2026-06-26
search_memories_tool (agentic chat) date: 2026-06-27 date: 2026-06-26

Why

From #6214:

When chatting with Omi AI, it confuses hours and gives wrong times because it's not properly handling the user's timezone.
User asks "what time did X happen?" → AI answers with UTC time (off by 3 hours for a UTC-3 user) instead of converting to the user's local timezone (America/Sao_Paulo).

Expected: AI should always use the user's configured timezone when discussing times in chat.

The conversations half of this was fixed in #7736 (conversations_to_string localizes), but the action-item and memory tool results were never converted — so the issue's "always" is still not true today, and the reporter's exact symptom survives on the surfaces above.

The failure mode is specifically the missing timezone label: an unlabelled UTC wall clock reads as local time to the model, so it says "tonight" for a due date that is actually mid-afternoon for the user. Memory dates are worse in a quieter way — the UTC calendar date rolls over at a different instant than the user's, so a memory captured at 21:00 in São Paulo is reported a day late.

Root cause / bug class

The agentic action-item tool already fixed exactly this in #4643, but the fix was applied at one call site rather than the shared boundary. Its REST sibling (tool_services/action_items.py) and both memory paths kept formatting raw strftime output.

To close the class rather than patch one more site, the formatting now has one owner: _format_local is promoted out of action_item_tools.py into utils/conversations/render.py as format_local_time (next to the existing resolve_display_tz), plus a new format_local_date for the date-only case. Both treat a naive timestamp as UTC defensively, so no path can hand the model an unlabelled wall clock.

A timezone lookup failure falls back to labelled UTC and never aborts retrieval.

Verified

Backend env synced with ./scripts/sync-python-deps.sh; ENCRYPTION_SECRET / OPENAI_API_KEY per test.sh.

1. Regression tests fail without the fix. New backend/tests/unit/test_tool_services_timezone.py (15 tests) imports the real modules and swaps collaborators with monkeypatch, so it exercises the production formatters. Reverting only the three production files and re-running:

8 failed, 10 passed      <- pre-fix (behavioural assertions on the rendered string)
15 passed                <- with the fix

2. Real rendered tool output (the string the model actually receives), user tz America/Sao_Paulo, stored due_at = 2026-06-26T22:00:00Z:

BEFORE:   Due: 2026-06-26 22:00:00                        <- no tz; model reads it as 22:00 local
AFTER:    Created: 2026-06-26 19:00:00 America/Sao_Paulo
          Due:     2026-06-26 19:00:00 America/Sao_Paulo

memory stored 2026-06-27T01:30Z
BEFORE:   date: 2026-06-27                                 <- a day late for the user
AFTER:    date: 2026-06-26

That is the reporter's 3-hour skew, gone.

3. Full backend suite — bash test.sh: no collection errors, no regressions. The single failure, test_vad_gate.py::test_concurrent_sessions_no_deadlock, is a pre-existing flake unrelated to this change (it touches none of the changed modules); A/B'd against pristine origin/main, where it fails 3/3 runs in isolation while this branch passes 2/3.

4. make preflight — PR preflight passed: 8 checks.

Covered: conversion correctness, naive-timestamp handling, the label on every emitted timestamp, unset-timezone fallback, and timezone-lookup failure.

Not covered

MemoryDB.get_memories_as_str still renders ... UTC — it is explicitly labelled, so the model can convert it correctly and it is not the mislabelling bug; threading a timezone into that shared model method is a wider change and is left out deliberately.

fixes #6214


Auto-generated from issue feedback by the mini issues-improver. Review before merge.

Review in cubic

The chat/agent tool results that still emitted bare UTC made the model state the
wrong time of day. `get_action_items_text` (the `/v1/tools/*` service behind the
desktop, web, and MCP agent clients) rendered `Due: 2026-06-26 22:00:00` with no
timezone, so the model read that wall clock as local — "tonight" for what was
actually 19:00 for a UTC-3 user. `search_memories_text` and `search_memories_tool`
rendered a bare UTC date, which is a day late for anyone west of Greenwich in the
evening.

Every timestamp handed to the model now carries an explicit timezone label, which
is what the agentic action-item tool already did (#4643); its sibling REST service
and both memory paths were the stragglers. A timezone lookup failure falls back to
labelled UTC rather than aborting retrieval.

The formatting itself now has one owner: `_format_local` is promoted out of
action_item_tools into `utils/conversations/render.py` as `format_local_time`,
alongside a new `format_local_date`, so a third copy of the same conversion is not
added.

fixes #6214

@tianmind-studio tianmind-studio left a comment

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.

I found two integration gaps on d1dc6b11 that should be addressed before merge.

First, the default-memory path still bypasses the new timezone conversion. Both search_memories_text and search_memories_tool compute display_tz, but when search_memory_default_chat_memories_vector_decision_text returns USE_MEMORY, they immediately return its preformatted text. That adapter still formats updated_at with a raw strftime('%Y-%m-%d') in utils/memory/chat_memory_adapter.py, so a 2026-06-27T01:30:00Z memory is still rendered as 2026-06-27 for America/Sao_Paulo. I reproduced this through both public functions; each returned date: 2026-06-27. Please thread the display timezone (or a date formatter) into that adapter path and cover the USE_MEMORY branch in the regression tests.

Second, the new test file is not order-safe with the existing suite. On Windows, pytest tests/unit/test_action_item_date_validation.py tests/unit/test_tool_services_timezone.py -q fails during collection with AttributeError: module 'google.auth' has no attribute 'credentials'. test_action_item_date_validation.py snapshots sys.modules before its real google.auth imports, then restores the package while leaving imported children such as google.auth.credentials cached. Moving _SYS_MODULES_SNAPSHOT = snapshot_sys_modules(...) below the three importable_mod_name imports fixes the leak; the same two-file run then passes all 41 tests. Please include that isolation fix (or an equivalent hermetic approach) so the test can coexist in a single pytest process.

@kodjima33
kodjima33 merged commit 86643ee into main Jul 15, 2026
22 of 25 checks passed
@kodjima33
kodjima33 deleted the issues-improver/6214-chat-tool-timestamps-user-timezone branch July 15, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat assistant gives wrong times due to timezone confusion

2 participants