The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): an epoch-pinned native event runs with local time, not 1970
Tekniumdd78113137f8
diff --git a/gateway/session_envelope.py b/gateway/session_envelope.pyindex 3c5aabb56f5..4fdb03bcd18 100644--- a/gateway/session_envelope.py+++ b/gateway/session_envelope.py@@ -183,8 +183,12 @@ def restore_native(payload, runner=None):runner = runner or callback_runner()if runner is not None:restore_provenance(runner, source, envelope['provenance'])- event = MessageEvent(text=payload['text'], source=source,- timestamp=datetime.fromisoformat(envelope['timestamp']),+ timestamp = datetime.fromisoformat(envelope['timestamp'])+ if timestamp.tzinfo is not None and timestamp.timestamp() == 0:+ # Epoch 0 pins the retry fingerprint of producers with no provider time (relay, webhook,+ # automation); the turn records local time, as main's arrival-stamped event did.+ timestamp = datetime.now()+ event = MessageEvent(text=payload['text'], source=source, timestamp=timestamp,message_type=MessageType(envelope.get('message_type', 'text')),media_urls=restore_native_media(envelope.get('media', [])),**deepcopy(envelope['event']))diff --git a/tests/gateway/test_native_epoch_timestamp.py b/tests/gateway/test_native_epoch_timestamp.pynew file mode 100644index 00000000000..fa9fad18e44--- /dev/null+++ b/tests/gateway/test_native_epoch_timestamp.py@@ -0,0 +1,49 @@+"""Producers that pin epoch 0 for a stable retry fingerprint still record local time (not 1970)."""+from datetime import UTC, datetime++import pytest++from gateway.config import GatewayConfig, Platform, PlatformConfig+from gateway.platforms.base import SendResult+from gateway.platforms.event import MessageEvent+from gateway.run import GatewayRunner+from gateway.session_authority import initialize_session_authority+from gateway.session_ingress import admit_message+from gateway.session_results import execution_result++CHAT = '[email protected]'+++async def test_epoch_zero_producer_timestamp_runs_with_local_time_and_keeps_retry_identity(tmp_path, monkeypatch):+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter+ monkeypatch.setenv('WHATSAPP_ALLOWED_USERS', '15551234567')+ runner = GatewayRunner(GatewayConfig(sessions_dir=tmp_path / 'sessions'))+ adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={'session_path': str(tmp_path / 'wa')}))+ adapter.gateway_runner, runner.adapters = runner, {Platform.WHATSAPP: adapter}+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='stamp')+ runner._wire_adapter_handlers(adapter)+ stamps = []++ async def send(chat_id, content, **_):+ return SendResult(success=True, message_id='s')++ async def turn(event, *_args, **_kwargs):+ stamps.append(event.timestamp)+ execution_result.get()['result'] = {'final_response': 'ok', 'messages': []}+ return 'ok'++ monkeypatch.setattr(adapter, 'send', send)+ monkeypatch.setattr(runner, '_handle_message_with_agent', turn)+ source = adapter.build_source(chat_id=CHAT, user_id=CHAT, chat_type='dm')+ pinned = lambda: MessageEvent(text='hi', source=source, message_id='p1', # noqa: E731+ timestamp=datetime.fromtimestamp(0, UTC))+ try:+ before = datetime.now()+ assert await admit_message(authority, pinned()) == 'ok'+ assert len(stamps) == 1 and stamps[0].timestamp() >= before.timestamp(), stamps # was 1970-01-01 (persisted as 0)+ # The redelivered provider id is still the same admission: no second turn.+ first = await authority.admit_native(pinned())+ assert first.status == 'terminal' and len(stamps) == 1+ finally:+ authority.db.close()