"""Helpers for rendering gateway message timestamps exactly once. Gateway messages need timestamps in the LLM context for temporal awareness, but persisted message content should stay clean so replay does not accumulate ``[timestamp] [timestamp] ...`` prefixes across turns. """ from __future__ import annotations import errno import re import sys import time from datetime import datetime, timezone, UTC from typing import Any, Optional, Tuple from hermes_time import safe_strftime # Leading timestamp prefix, either the current human format # ``[Tue 2026-04-28 13:40:53 CEST]`` or the older ISO one # ``[2026-04-13T17:02:06+0200]`` / ``[...+02:00]`` (human tried first). _TIMESTAMP_PREFIX_RE = re.compile( r"^\[(?:" r"(?P[A-Z][a-z]{2}) " r"(?P\d{4}-\d{2}-\d{2}) " r"(?P