Skip to content

The agent that grows with you

fix(gateway): render early Unix timestamps safely on Windows

tank32169526fd72feb
diff --git a/gateway/message_timestamps.py b/gateway/message_timestamps.py
index 7f32fc0b94a..484a14334e6 100644
--- a/gateway/message_timestamps.py
+++ b/gateway/message_timestamps.py
@@ -8,7 +8,7 @@ persisted message content should stay clean so replay does not accumulate
from __future__ import annotations
import re
-from datetime import datetime
+from datetime import datetime, timezone
from typing import Any, Optional, Tuple
from hermes_time import safe_strftime
@@ -83,7 +83,9 @@ def format_message_timestamp(ts_value: Any, tz=None) -> str:
epoch = coerce_message_timestamp(ts_value, tz=tz)
if epoch is None:
return ""
- dt = datetime.fromtimestamp(epoch, tz=tz) if tz is not None else datetime.fromtimestamp(epoch).astimezone()
+ # An early positive epoch can fall in 1969 locally. Starting from aware UTC
+ # avoids Windows' negative-time mktime path in naive astimezone().
+ dt = datetime.fromtimestamp(epoch, tz=tz) if tz is not None else datetime.fromtimestamp(epoch, tz=timezone.utc).astimezone()
return f"[{safe_strftime(dt, '%a %Y-%m-%d %H:%M:%S %Z')}]"
diff --git a/tests/gateway/test_message_timestamps.py b/tests/gateway/test_message_timestamps.py
index f10286c68de..efb98ac2bd6 100644
--- a/tests/gateway/test_message_timestamps.py
+++ b/tests/gateway/test_message_timestamps.py
@@ -1,6 +1,10 @@
-from datetime import datetime
+from datetime import datetime, timezone
from zoneinfo import ZoneInfo
+import pytest
+
+from hermes_time import safe_strftime
+
from gateway.message_timestamps import (
render_user_content_with_timestamp,
)
@@ -13,6 +17,16 @@ def _epoch(year, month, day, hour, minute, second):
return datetime(year, month, day, hour, minute, second, tzinfo=BERLIN).timestamp()
[email protected]("epoch", [1.0, 1_000_000_000.0])
+def test_render_numeric_timestamp_preserves_instant_in_system_timezone(epoch):
+ # Epoch 1 is still in 1969 west of UTC. Windows rejects a naive
+ # astimezone() conversion there, although the Unix timestamp is positive.
+ local = datetime.fromtimestamp(epoch, tz=timezone.utc).astimezone()
+ prefix = safe_strftime(local, "%a %Y-%m-%d %H:%M:%S %Z")
+
+ assert render_user_content_with_timestamp("hello", epoch) == f"[{prefix}] hello"
+
+
def test_render_user_content_deduplicates_existing_timestamp_and_preserves_embedded_time():
db_processing_ts = _epoch(2026, 4, 27, 15, 55, 36)
stored_content = (