The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): editing a sent message no longer replies admission_conflict (#140845)
Teknium6483c39f8488
diff --git a/gateway/platforms/event.py b/gateway/platforms/event.pyindex 9ff022d6e5c..8f2b0dc8888 100644--- a/gateway/platforms/event.py+++ b/gateway/platforms/event.py@@ -69,6 +69,9 @@ class MessageEvent:# Platform update id (Telegram ``update_id``): ``/restart`` records it so the new gateway# advances past it even if PTB's shutdown ACK times out.platform_update_id: Optional[int] = None+ # Revision of an edited message (Telegram ``edit_date``). An edit re-sends its message's own+ # ``message_id`` with new text, so native admission keys it apart from the original send.+ edit_revision: Optional[str] = None# Media attachments: local file paths (for vision tool access)media_urls: list[str] = field(default_factory=list)media_types: list[str] = field(default_factory=list)diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex 98de4309dfc..fe9edf5be5c 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -526,6 +526,8 @@ class SessionAuthority:identity = json.dumps([source.profile, source.platform.value, source.chat_id,source.thread_id, source.user_id], separators=(',', ':'))request_id = str(payload['native_text_v1']['event']['message_id'] or uuid.uuid4().hex)+ if revision := payload['native_text_v1']['event'].get('edit_revision'):+ request_id += ':edit:' + revision # an edit is its own input, not a changed replay of the sendcommitted = [ref] # the row's target: the write may retarget a rotated-away reservation# A /retry re-send carries its transcript rewrite: it commits in this admission's own write.from gateway import session_retry_admissiondiff --git a/gateway/session_envelope.py b/gateway/session_envelope.pyindex 567e4a7dfc0..36292919a2e 100644--- a/gateway/session_envelope.py+++ b/gateway/session_envelope.py@@ -24,7 +24,8 @@ _EVENT_FIELDS = ('reply_to_author_name', 'reply_to_is_own_message', 'allow_gateway_control',)# ``reply_anchor_override`` = '' is Telegram topic transfer's "do not quote the root DM" (#139136).-_CONTEXT_FIELDS = ('auto_skill', 'channel_prompt', 'channel_context', 'reply_expected', 'reply_anchor_override')+_CONTEXT_FIELDS = ('auto_skill', 'channel_prompt', 'channel_context', 'reply_expected', 'reply_anchor_override',+ 'edit_revision')# Closed metadata contract: only the keys the Baileys adapter stamps on ordinary inbound# (``WhatsAppAdapter._build_message_event``) cross native admission; any other key refuses._WHATSAPP_METADATA = frozenset({'whatsapp_native_type', 'whatsapp_native', 'whatsapp_from_owner'})diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex 51c90a138d7..6263de61ca0 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -7134,7 +7134,13 @@ class TelegramAdapter(TelegramHeldInboundMixin, TelegramTopicRoutingMixin, BasePmessage_id=str(message.message_id), platform_update_id=update_id,reply_to_message_id=reply_to_id, reply_to_text=reply_to_text, auto_skill=topic_skill,channel_prompt=group_identity_prompt(self, message, channel_prompt),- timestamp=message.date)+ edit_revision=self._edit_revision(message), timestamp=message.date)++ @staticmethod+ def _edit_revision(message) -> Optional[str]:+ """``edit_date`` of an edited message (whole seconds), None for a new send."""+ edit_date = getattr(message, "edit_date", None)+ return str(int(edit_date.timestamp())) if hasattr(edit_date, "timestamp") else None# -- Message reactions (processing lifecycle) --diff --git a/tests/gateway/test_native_admission_edit.py b/tests/gateway/test_native_admission_edit.pynew file mode 100644index 00000000000..1375e0ee007--- /dev/null+++ b/tests/gateway/test_native_admission_edit.py@@ -0,0 +1,64 @@+"""An edited message re-sends its message id with new text: native admission takes it as its own+input, never as a changed replay of the original send (``admission_conflict``, #140845)."""+from datetime import UTC, datetime+from types import SimpleNamespace++import pytest++from gateway.config import GatewayConfig, Platform, PlatformConfig+from gateway.platforms.event import MessageEvent, MessageType+from gateway.run import GatewayRunner+from gateway.session_authority import initialize_session_authority+from hermes_state_runtime import list_session_admissions++CHAT = '[email protected]'+++async def test_edit_of_an_admitted_message_is_its_own_admission(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='edit')+ runner._wire_adapter_handlers(adapter)+ source = adapter.build_source(chat_id=CHAT, user_id=CHAT, chat_type='dm')+ sent = datetime(2026, 10, 18, 2, 5, tzinfo=UTC) # an edit keeps its message's send time++ def send(text, revision=None):+ return authority.admit_native(MessageEvent(text=text, source=source, message_id='m1', timestamp=sent,+ edit_revision=revision))+ try:+ first = await send('helo')+ edit = await send('hello', '1800000010')+ # A redelivered edit is still a replay of that edit, and a second edit is a third input.+ again = await send('hello', '1800000010')+ second = await send('hello!', '1800000020')+ assert again.admission_id == edit.admission_id+ assert len({first.admission_id, edit.admission_id, second.admission_id}) == 3+ rows = list_session_admissions(authority.db, session_id=first.ref.session_id, pending_only=False)+ assert [row['payload']['text'] for row in rows] == ['helo', 'hello', 'hello!']+ finally:+ authority.db.close()+++def test_telegram_edit_carries_its_edit_date_as_revision():+ from plugins.platforms.telegram.adapter import TelegramAdapter+ adapter = object.__new__(TelegramAdapter)+ adapter.platform = Platform.TELEGRAM+ adapter.config = PlatformConfig(enabled=True, token='***', extra={})+ sent = datetime(2026, 10, 18, 2, 5, tzinfo=UTC)++ def message(edit_date):+ return SimpleNamespace(+ message_id=7, text='hello', caption=None, entities=[], caption_entities=[], message_thread_id=None,+ is_topic_message=False, reply_to_message=None, date=sent, edit_date=edit_date, forum_topic_created=None,+ chat=SimpleNamespace(id=111, type='private', full_name='A', title=None, is_forum=False),+ from_user=SimpleNamespace(id=111, full_name='A', first_name='A', is_bot=False))++ original = adapter._build_message_event(message(None), MessageType.TEXT)+ edited = adapter._build_message_event(message(datetime(2026, 10, 18, 2, 6, tzinfo=UTC)), MessageType.TEXT)+ assert original.edit_revision is None+ assert edited.message_id == original.message_id == '7'+ assert edited.edit_revision == str(int(datetime(2026, 10, 18, 2, 6, tzinfo=UTC).timestamp()))