Skip to content

The agent that grows with you

fix: follow-up for salvaged PR #71904 — source raw inbound id, strip persistence key from the wire

Teknium7beb0de676b1
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 258e056612a..a932c46206a 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3065,7 +3065,7 @@ def handle_max_iterations(agent, messages: list, api_call_count: int) -> str:
# timestamp (preserved on gateway user replay entries for the
# stale-confirmation expiry check — #47868 rejection class),
# and every Hermes-internal underscore-prefixed scaffolding key.
- for schema_foreign in ("tool_name", "codex_reasoning_items", "codex_message_items", "timestamp"):
+ for schema_foreign in ("tool_name", "codex_reasoning_items", "codex_message_items", "timestamp", "platform_message_id"):
api_msg.pop(schema_foreign, None)
# api_content (the persist-what-you-send sidecar) carries the
# exact bytes every main-loop call sent for this message —
diff --git a/agent/transports/chat_completions.py b/agent/transports/chat_completions.py
index e2a6707b09a..fb9e7b29c34 100644
--- a/agent/transports/chat_completions.py
+++ b/agent/transports/chat_completions.py
@@ -392,6 +392,7 @@ class ChatCompletionsTransport(ProviderTransport):
or "tool_name" in msg
or "effect_disposition" in msg
or "timestamp" in msg # #47868 — strict providers reject this
+ or "platform_message_id" in msg # gateway dedup id (persistence-only)
or "api_content" in msg # persist-what-you-send sidecar
):
needs_sanitize = True
@@ -463,6 +464,7 @@ class ChatCompletionsTransport(ProviderTransport):
or "tool_name" in msg
or "effect_disposition" in msg
or "timestamp" in msg # #47868 — leak into strict providers
+ or "platform_message_id" in msg # gateway dedup id (persistence-only)
or "api_content" in msg # persist-what-you-send sidecar
):
out_msg = mutable_msg()
@@ -471,6 +473,7 @@ class ChatCompletionsTransport(ProviderTransport):
out_msg.pop("tool_name", None)
out_msg.pop("effect_disposition", None)
out_msg.pop("timestamp", None) # #47868 — leak into strict providers
+ out_msg.pop("platform_message_id", None) # gateway dedup id
out_msg.pop("api_content", None) # persist-what-you-send sidecar
diff --git a/gateway/run.py b/gateway/run.py
index 74be5321042..b235253e3fb 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -6967,9 +6967,10 @@ class TurnRunner:
# user turn so a turn interrupted by a gateway restart is durably
# recorded WITH its id — restart drain-window recovery dedups
# against has_platform_message_id, and without this the
- # interrupted turn is invisible to that check.
- if ctx.event_message_id is not None:
- _conversation_kwargs["persist_user_platform_id"] = str(ctx.event_message_id)
+ # interrupted turn is invisible to that check. Uses the raw
+ # inbound id (NOT event_message_id, which is the reply anchor).
+ if ctx.inbound_message_id is not None:
+ _conversation_kwargs["persist_user_platform_id"] = str(ctx.inbound_message_id)
result = agent.run_conversation(_api_run_message, **_conversation_kwargs)
finally:
unregister_gateway_notify(_approval_session_key)
@@ -22185,6 +22186,9 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
session_key=session_key,
run_generation=run_generation,
event_message_id=self._reply_anchor_for_event(event),
+ inbound_message_id=(
+ str(event.message_id) if event.message_id else None
+ ),
channel_prompt=event.channel_prompt,
moa_config=getattr(event, "_moa_config", None),
persist_user_message=persist_user_message,
@@ -29965,6 +29969,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
run_generation: Optional[int] = None,
_interrupt_depth: int = 0,
event_message_id: Optional[str] = None,
+ inbound_message_id: Optional[str] = None,
channel_prompt: Optional[str] = None,
moa_config: Optional[dict] = None,
persist_user_message: Optional[Any] = None,
@@ -29986,6 +29991,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
message, context_prompt, history, source, session_id,
session_key=session_key, run_generation=run_generation,
_interrupt_depth=_interrupt_depth, event_message_id=event_message_id,
+ inbound_message_id=inbound_message_id,
channel_prompt=channel_prompt, moa_config=moa_config,
persist_user_message=persist_user_message,
persist_user_timestamp=persist_user_timestamp,
@@ -29999,6 +30005,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
message, context_prompt, history, source, session_id,
session_key=session_key, run_generation=run_generation,
_interrupt_depth=_interrupt_depth, event_message_id=event_message_id,
+ inbound_message_id=inbound_message_id,
channel_prompt=channel_prompt, moa_config=moa_config,
persist_user_message=persist_user_message,
persist_user_timestamp=persist_user_timestamp,
@@ -30141,6 +30148,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
run_generation: Optional[int] = None,
_interrupt_depth: int = 0,
event_message_id: Optional[str] = None,
+ inbound_message_id: Optional[str] = None,
channel_prompt: Optional[str] = None,
moa_config: Optional[dict] = None,
persist_user_message: Optional[Any] = None,
@@ -30452,6 +30460,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
run_generation=run_generation,
_interrupt_depth=_interrupt_depth,
event_message_id=event_message_id,
+ inbound_message_id=inbound_message_id,
moa_config=moa_config,
persist_user_message=persist_user_message,
persist_user_timestamp=persist_user_timestamp,
diff --git a/gateway/turn_context.py b/gateway/turn_context.py
index 284875e228d..02fa8d23c96 100644
--- a/gateway/turn_context.py
+++ b/gateway/turn_context.py
@@ -88,6 +88,11 @@ class TurnContext:
process_baseline: frozenset[str] = field(default_factory=frozenset)
_interrupt_depth: int = 0
event_message_id: Optional[str] = None
+ # Raw platform-side id of the INBOUND user message (event.message_id),
+ # distinct from event_message_id which is the reply/thread anchor and can
+ # be the replied-to message on Slack/Mattermost/Buzz or None for Telegram
+ # topics. Used to stamp platform_message_id on the persisted user turn.
+ inbound_message_id: Optional[str] = None
moa_config: Optional[dict] = None
persist_user_message: Optional[Any] = None
persist_user_timestamp: Optional[float] = None