diff --git a/plugins/memory/hindsight/__init__.py b/plugins/memory/hindsight/__init__.py index 5516f1a1e2..a9a1d57df3 100644 --- a/plugins/memory/hindsight/__init__.py +++ b/plugins/memory/hindsight/__init__.py @@ -541,13 +541,18 @@ def _normalize_observation_scopes(value: Any) -> Any: def _utc_timestamp() -> str: - """Return current UTC timestamp in ISO-8601 with milliseconds and Z suffix.""" + """Return the UTC write/audit time for retain metadata.""" return datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z") def _event_timestamp() -> str: - """Return the configured Hermes-local timestamp for a retained event.""" - return _hermes_now().isoformat(timespec="seconds") + """Return the configured Hermes event time with an explicit UTC offset.""" + event_time = _hermes_now() + # hermes_time.now() guarantees an aware datetime. Keep this fallback so a + # replacement clock cannot silently emit an offset-less Hindsight Event Date. + if event_time.tzinfo is None or event_time.utcoffset() is None: + event_time = event_time.astimezone() + return event_time.isoformat(timespec="seconds") def _embedded_profile_name(config: dict[str, Any]) -> str: @@ -1981,6 +1986,8 @@ class HindsightMemoryProvider(MemoryProvider): self._prefetch_thread.start() def _build_turn_messages(self, user_content: str, assistant_content: str) -> List[Dict[str, str]]: + # Hindsight receives this pair as one conversation turn, so both + # messages intentionally share the same turn-level event timestamp. now = _event_timestamp() return [ { diff --git a/tests/plugins/memory/test_hindsight_provider.py b/tests/plugins/memory/test_hindsight_provider.py index 1da9e72f60..e0e79bb26b 100644 --- a/tests/plugins/memory/test_hindsight_provider.py +++ b/tests/plugins/memory/test_hindsight_provider.py @@ -903,6 +903,16 @@ class TestSyncTurn: assert re.fullmatch(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z", item["metadata"]["retained_at"]) assert item["timestamp"] == event_time.isoformat(timespec="seconds") + def test_retain_timestamp_normalizes_a_naive_clock(self, provider, monkeypatch): + event_time = datetime(2026, 8, 10, 11, 9) + monkeypatch.setattr("plugins.memory.hindsight._hermes_now", lambda: event_time) + + timestamp = provider._build_retain_kwargs("hello")["timestamp"] + parsed = datetime.fromisoformat(timestamp) + + assert parsed.tzinfo is not None + assert parsed.utcoffset() is not None + @pytest.mark.asyncio async def test_retain_timestamp_is_serialized_by_pinned_client(self, provider): from hindsight_client import Hindsight