Skip to content

fix(backend): emit timezone-aware UTC timestamps from conversation search so chat shows correct times (#4643) - #8538

Merged
kodjima33 merged 5 commits into
BasedHardware:mainfrom
ZachL111:zach/search-utc-timestamps
Jun 30, 2026
Merged

kodjima33 merged 5 commits into
BasedHardware:mainfrom
ZachL111:zach/search-utc-timestamps

Conversation

@ZachL111

@ZachL111 ZachL111 commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

What

search_conversations (utils/conversations/search.py) renders each matched Typesense doc's created_at, started_at, and finished_at unix timestamps into ISO strings. It used datetime.utcfromtimestamp(ts).isoformat(), which returns a naive datetime, so the string has no timezone offset (for example 2023-11-14T22:13:20 instead of 2023-11-14T22:13:20+00:00).

These timestamps go to the chat model (search_conversations_tool is what the agent uses for "when did X happen?" event questions) and to API clients. A naive UTC string with no offset can be read as local time, so a conversation can be shown hours off depending on the reader's timezone. This is the same timezone-correctness class as the calendar rendering issue (#4643).

How

  • Add a small _utc_iso(ts) helper that converts a unix timestamp to a timezone-aware UTC ISO 8601 string via datetime.fromtimestamp(ts, tz=timezone.utc).isoformat(), so the offset is explicit.
  • Use it for the three timestamp fields. The conversion stays inside the existing per-record try/except, so one malformed indexed doc still skips just that hit rather than failing the whole search page.

Safe for existing consumers: the one place that re-parses these strings (routers/conversations.py, the overlapping-calendar-event path) already normalizes naive datetimes to UTC, so it now receives an already-aware value and produces the same result. The keyword-search id path does not read the timestamps at all.

Testing

New pure unit test tests/unit/test_search_conversations_utc_timestamps.py (registered in test.sh) covers the helper: the output is timezone-aware with an explicit +00:00 offset, represents the correct UTC instant, is not a naive string (regression guard), and handles float timestamps. All 4 pass. The existing conversation search tests still pass. black and the async blocker lint are clean.

Review in cubic

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

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Positive signal from maintainer automation: this looks like a small, well-scoped backend correctness fix.

What I checked:

  • search_conversations now renders Typesense unix timestamps with an explicit UTC offset via datetime.fromtimestamp(..., tz=timezone.utc).isoformat().
  • The conversion remains inside the existing per-hit malformed-record guard, so a bad timestamp still skips only that hit rather than failing the whole search.
  • New unit coverage checks timezone awareness, the exact UTC instant, regression against naive ISO strings, and float timestamp handling; the test is wired into backend/test.sh.
  • Current GitHub checks are passing.

Maintainer note: this is a narrow fix for conversation-search timestamp serialization. It does not by itself solve the broader timezone preference / local-time interpretation request in #4643, but it should make these search result timestamps safer for downstream chat/API consumers.

No blocking issues found from my review. Because this PR is coming through the maintainer automation guardrails, I’m leaving this as a positive signal rather than a formal approval.

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

Backend bug fix with regression test; CI green. Approving.

@ZachL111

Copy link
Copy Markdown
Contributor Author

Merged latest main to clear a merge conflict. The only conflict was in backend/test.sh (the CI test registry, where every PR appends a line); resolved by keeping both this branch's and main's added registration lines. The timezone-aware timestamp fix itself is unchanged.

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice fix. Verified a couple of things on my side:

  • _utc_iso using datetime.fromtimestamp(ts, tz=timezone.utc).isoformat() is the canonical replacement for the deprecated utcfromtimestamp and produces an explicit +00:00 offset — correct.
  • The three call sites stay inside the existing per-record try/except, which now also catches OverflowError/OSError for out-of-range timestamps — good hardening, and one bad indexed doc still just skips that hit rather than 500-ing the page.
  • Downstream is safe: routers/conversations.py (the overlapping-calendar-event path) already normalizes naive datetimes to UTC, so it now receives an already-aware value and computes the same result. search_conversations_tool and the integration search endpoint don't re-parse these timestamps.
  • Scope is complete — search.py was the only utcfromtimestamp site in backend/utils/conversations/.

The tests are well-aimed (timezone-awareness, correct instant, naive-string regression guard, float handling).

Flagging for maintainer review since this touches timestamps that feed the chat model and API clients, but the change itself looks correct and low-risk. Thanks for the contribution!

@Git-on-my-level Git-on-my-level added the needs-maintainer-review Needs a human maintainer to sign off before merge label Jun 29, 2026
@Git-on-my-level

Copy link
Copy Markdown
Member

Positive signal (post-rebase re-check on ).\n\nIndependent verification of the change:\n- swaps the deprecated (removed in Python 3.12, and which emitted a naive ISO string with no offset) for . Same UTC instant, now with an explicit offset. This is the correct canonical fix.\n- The conversion stays inside the existing per-hit , so a single malformed indexed doc still skips just that hit rather than failing the whole search page.\n- New unit coverage checks timezone awareness, the exact UTC instant, a naive-string regression guard, and float timestamps; the suite is green.\n\nConsumer-safety check (the claim in the PR description):\n- The one downstream re-parser, (), already does followed by . Receiving an already-aware value lands in the same branch and produces the same result. No breakage.\n- reads only uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),105(lxd),988(docker), not timestamps. No other consumer found.\n\nScope note: the in the title references the timezone issue, not a parent PR; this is a sibling of the already-merged #8588 (screen activity) and #8575 (billing date) on the same theme, not a stacked branch sequence.\n\nThis is a small, well-scoped, well-tested correctness fix and the checks pass. Because of the label I'm leaving a positive signal rather than a formal approval.

@Git-on-my-level

Copy link
Copy Markdown
Member

Positive signal (post-rebase re-check on 524a67c).

Independent verification of the change:

  • _utc_iso(ts) swaps the deprecated datetime.utcfromtimestamp(ts) (removed in Python 3.12, and which emitted a naive ISO string with no offset) for datetime.fromtimestamp(ts, tz=timezone.utc).isoformat(). Same UTC instant, now with an explicit +00:00 offset. This is the correct canonical fix.
  • The conversion stays inside the existing per-hit try/except, so a single malformed indexed doc still skips just that hit rather than failing the whole search page.
  • New unit coverage checks timezone awareness, the exact UTC instant, a naive-string regression guard, and float timestamps; the suite is green.

Consumer-safety check (the claim in the PR description):

  • The one downstream re-parser, routers/conversations.py (auto_link_calendar_event), already does datetime.fromisoformat(...) followed by "if tzinfo is None: replace(tzinfo=timezone.utc)". Receiving an already-aware +00:00 value lands in the same branch and produces the same result. No breakage.
  • keyword_search_conversation_ids reads only id, not timestamps. No other consumer found.

Scope note: the (#4643) in the title references the timezone issue, not a parent PR; this is a sibling of the already-merged #8588 (screen activity) and #8575 (billing date) on the same theme, not a stacked branch sequence.

This is a small, well-scoped, well-tested correctness fix and the checks pass. Because of the needs-maintainer-review label I'm leaving a positive signal rather than a formal approval.

@ZachL111

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough verification, David, especially tracing the one downstream re-parser in auto_link_calendar_event to confirm an already-aware +00:00 value lands in the same tzinfo branch and produces the same result. Agreed it is maintainer-gated. Nothing further from me.

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

Backend bug fix, CI green, scoped — merging (confidence 4/5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants