Skip to content

fix: wire device timezone into AI chat for relative times (Fixes #4643) - #13857

Merged
kodjima33 merged 12 commits into
BasedHardware:mainfrom
apoorvdarshan:cursor/chat-timezone-4643-a4ea
Sep 15, 2026
Merged

kodjima33 merged 12 commits into
BasedHardware:mainfrom
apoorvdarshan:cursor/chat-timezone-4643-a4ea

Conversation

@apoorvdarshan

@apoorvdarshan apoorvdarshan commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4643

Problem

AI chat often defaulted to UTC because the user's IANA timezone was only persisted when FCM push registration ran. Without that, relative phrases like "tonight" and "this afternoon" did not match the user's local day.

Solution

  • Accept optional time_zone on POST /v2/messages, validate as IANA, persist on the user profile when it changes, and pass the resolved zone through chat streaming (client_tz) so the datetime block and tools share one timezone.
  • Add PUT /v1/users/time-zone so clients can sync timezone without push registration.
  • Clarify the injected <current_datetime> block: relative time words must be interpreted in the user's timezone.
  • Mobile: send time_zone on every chat message; sync timezone after auth via the new endpoint (fire-and-forget so sign-in is not blocked).
  • Web: send time_zone on each chat message.

Review follow-ups (cc9f425)

  • Fail-open Firestore persist in sync_user_time_zone_from_client (chat send proceeds with request tz).
  • Offload chat timezone sync via run_blocking(db_executor, ...) inside the stream generator.
  • PUT /v1/users/time-zone uses sync_user_time_zone_from_client; BYOK policy validated_when_headers_present.
  • set_user_time_zone uses get_firestore_client() injectable seam.
  • Flutter: unawaited timezone sync on token refresh; omit time_zone when device TZ lookup fails.

Verification

cd backend && source .venv/bin/activate && pytest tests/unit/test_chat_timezone_sync.py tests/unit/test_prompt_metadata_safety.py -q

14 passed (includes cc9f425 regressions).

PR checks

Line-Count-Exception: backend/routers/chat.py | 2175 -> 2182 | client timezone sync via run_blocking in stream + review fixes
Line-Count-Exception: backend/utils/llm/chat.py | 1514 -> 1535 | sync helper + relative-time prompt guidance for #4643

Failure-Class: none

Review in cubic

apoorvdarshan and others added 9 commits September 8, 2026 22:29
chore: sync fork main from BasedHardware/omi
)

Add optional time_zone on SendMessageRequest, persist device-reported IANA
zones via PUT /v1/users/time-zone, and pass resolved tz through chat streaming
so relative time language matches the user. Extend the datetime prompt block
with explicit relative-time guidance.

Verification: pytest tests/unit/test_chat_timezone_sync.py tests/unit/test_prompt_metadata_safety.py

Co-authored-by: Apoorv Darshan <[email protected]>
Include IANA time_zone on each chat message, sync timezone on auth without
requiring FCM registration, and regenerate app-client TypeScript types.

Co-authored-by: Apoorv Darshan <[email protected]>
Regenerate app-client OpenAPI from FastAPI export and update TS/Swift
generators. Register PUT /v1/users/time-zone in route_policy_manifest.yaml.

Co-authored-by: Apoorv Darshan <[email protected]>
Move sync_user_time_zone_from_client to utils/chat_time_zone.py and only
invoke it when the client sends time_zone, preserving hermetic chat router
tests that stub database.notifications.

Co-authored-by: Apoorv Darshan <[email protected]>
Move sync_user_time_zone_from_client to database.notifications and wire the
shared chat router harness stub so loading routers.chat does not import real
Firestore protos.

Co-authored-by: Apoorv Darshan <[email protected]>
Avoid importing utils.llm.chat in test_chat_timezone_sync, which exceeded the
1s per-test CI duration gate when loading the full chat module.

Co-authored-by: Apoorv Darshan <[email protected]>
@apoorvdarshan
apoorvdarshan marked this pull request as ready for review September 14, 2026 17:10

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 21 files

You’re at about 92% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact [email protected].

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread backend/database/notifications.py Resolved
Comment thread app/lib/providers/auth_provider.dart Resolved
Comment thread app/lib/backend/http/api/messages.dart Resolved
Comment thread backend/route_policy_manifest.yaml Resolved
Comment thread backend/routers/chat.py Resolved
Comment thread backend/routers/notifications.py Resolved
Comment thread backend/database/notifications.py Resolved
cursoragent and others added 3 commits September 14, 2026 17:33
- Catch Firestore failures in sync_user_time_zone_from_client; still return request_tz
- Offload chat send timezone sync via run_blocking(db_executor, ...)
- PUT /v1/users/time-zone uses sync_user_time_zone_from_client; BYOK policy fix
- set_user_time_zone uses get_firestore_client injectable seam
- Flutter: unawaited timezone sync on sign-in; omit time_zone when TZ lookup fails

Verification: backend/.venv/bin/python -m pytest tests/unit/test_chat_timezone_sync.py -q (8 passed)

Co-authored-by: Apoorv Darshan <[email protected]>
Build request body with conditional map entry instead of post-hoc body['time_zone']
assignment so app-client schema inventory stays clean.

Verification: inventory_app_client_schemas.py --operation-id send_message_v2_messages_post; test_app_client_schema_inventory.py

Co-authored-by: Apoorv Darshan <[email protected]>
Listen Pusher Stack Gauntlet failed with httpx.ReadTimeout in
_rest_finalization_survives_listener_restart; PR diff does not touch
listen/pusher stack paths.

Co-authored-by: Apoorv Darshan <[email protected]>
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @apoorvdarshan — well-scoped fix for #4643, and the cc9f425 follow-ups (fail-open persist, offloaded sync, injectable Firestore seam) address the right things. I verified the wiring end-to-end on the current head; all checks are green.

What looks good, file by file:

  • backend/database/notifications.py — sync_user_time_zone_from_client validates via ZoneInfo, skips the write when the stored zone matches, and fails open (logs, returns the request tz) when the Firestore persist fails, so chat sends are never blocked by tz sync. The firestore_client seam on set_user_time_zone keeps it testable.
  • backend/routers/chat.py — the sync runs via run_blocking(db_executor, ...) inside the stream generator, so the event loop isn't blocked, and it costs at most one Firestore get per message (set only on change).
  • backend/models/chat.py / backend/models/other.py — both request models validate time_zone as IANA and normalize whitespace; the stored value is re-validated on read via resolve_user_timezone, so nothing unvalidated reaches the <current_datetime> prompt block.
  • backend/utils/retrieval/graph.py — client_tz or await run_blocking(...) also removes a redundant Firestore read per streamed message when the client supplies the zone. Nice side benefit.
  • backend/routers/notifications.py + backend/route_policy_manifest.yaml — PUT /v1/users/time-zone is authenticated per-uid and writes only the caller's own field; the manifest entry mirrors the sibling location-context-consent route.
  • app/lib/backend/http/api/messages.dart / app/lib/providers/auth_provider.dart — tz lookup failures omit the field (chat send unaffected), and the post-auth sync is unawaited so sign-in isn't blocked. flutter_timezone is already a dependency, so this adds no packages.
  • Tests: backend/tests/unit/test_chat_timezone_sync.py covers valid/invalid zones, write-skip-when-unchanged, persist-failure fail-open, and the injectable client seam; the database.notifications stubs in _chat_router_test_harness.py mirror the real semantics; test_prompt_metadata_safety.py pins the new prompt guidance.
  • The five generated clients (omiApi.generated.*, OmiApi.generated.swift) and docs/api-reference/app-client-openapi.json are consistent mechanical regens for the new field/endpoint.

Two non-blocking observations:

  1. SendMessageRequest._validate_time_zone raises 422 on a malformed zone, which fails the whole chat send. The mobile path deliberately omits the field when the lookup fails, but web/app/src/lib/api.ts sends Intl.DateTimeFormat().resolvedOptions().timeZone unconditionally. Browsers return IANA identifiers in practice, so this is theoretical — but coercing invalid → None instead of raising would match the fail-open philosophy the rest of the PR follows.
  2. sendVoiceMessageStreamServer in messages.dart doesn't send time_zone, so voice-initiated chats fall back to the stored zone (now synced at auth via the new endpoint). Fine as-is; just noting the fix doesn't cover that path yet.

Leaving for human maintainer review rather than formal approval: this adds a new authenticated first-party endpoint and user-profile write plus a change to the model-facing <current_datetime> guidance, which deserves maintainer sign-off on the route and the prompt wording.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added needs-maintainer-review Needs a human maintainer to sign off before merge backend Backend Task (python) labels Sep 14, 2026
huiyuansun28-cmyk added a commit to huiyuansun28-cmyk/omi that referenced this pull request Sep 15, 2026
@huiyuansun28-cmyk

Copy link
Copy Markdown
Contributor

PR: #14102

AUTO (AI Agent).

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wires device timezone into chat for relative-time phrases; fixes #4643, validated IANA tz, extensive new tests, CI green. Confidence 5/5.

@kodjima33
kodjima33 merged commit bfd9789 into BasedHardware:main Sep 15, 2026
45 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) needs-maintainer-review Needs a human maintainer to sign off before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix AI chat timezone confusion by allowing users to choose or auto-detect the timezone, preventing incorrect event time mentions.

5 participants