Skip to content

The agent that grows with you

fix(gateway): reuse session-context pin on inherit_channel_pin turns too

chelsealong308987718b7f
diff --git a/gateway/platforms/event.py b/gateway/platforms/event.py
index a861190cade..9ff022d6e5c 100644
--- a/gateway/platforms/event.py
+++ b/gateway/platforms/event.py
@@ -91,11 +91,6 @@ class MessageEvent:
channel_context: Optional[str] = None
# Set for synthetic events (e.g. background-process notifications) that must bypass user authorization.
internal: bool = False
- # Set for synthetic turns that carry no channel inputs of their own (goal continuation,
- # heartbeat due-prompts, /goal resume) so the turn reuses the session's pinned channel
- # prompt/parent instead of overwriting it with this event's own None (#126109). Independent
- # of ``internal``: these events still go through user authorization.
- inherit_channel_pin: bool = False
# Free-form per-event metadata (e.g. ``whatsapp_from_owner=True``); plugins must ``.get()``.
metadata: dict[str, Any] = field(default_factory=dict)
timestamp: datetime = field(default_factory=datetime.now)
@@ -106,6 +101,12 @@ class MessageEvent:
# knows the message was meant for someone else); None means unknown and keeps the visible
# fallback, like True.
reply_expected: Optional[bool] = None
+ # Set for synthetic turns that carry no channel inputs of their own (goal continuation,
+ # heartbeat due-prompts, /goal resume) so the turn reuses the session's pinned channel
+ # prompt/parent instead of overwriting it with this event's own None (#126109). Independent
+ # of ``internal``: these events still go through user authorization. Appended after
+ # ``reply_expected`` for positional compat (see the field-order rule above).
+ inherit_channel_pin: bool = False
# Process-local admission receipt, never routing metadata or execution acknowledgement.
_gateway_accepted: bool = field(default=False, init=False, repr=False, compare=False)
diff --git a/gateway/run_agent_cache.py b/gateway/run_agent_cache.py
index 81db8f0f5e7..e30f7880fe5 100644
--- a/gateway/run_agent_cache.py
+++ b/gateway/run_agent_cache.py
@@ -723,7 +723,9 @@ class GatewayAgentCacheMixin:
it reuses an existing pin verbatim (restored by ``_rehydrate_prompt_pins`` after a restart or an eviction); with
no pin yet it renders and pins as usual. The pin records the ``privacy.redact_pii`` it was
rendered under: bytes from another privacy policy are never reused, even by an internal
- event."""
+ event. Callers pass ``event.internal or event.inherit_channel_pin`` (synthetic
+ goal/heartbeat/resume turns strip ``message_id``) so this pin and ``_pinned_channel_inputs``
+ reuse on the same decision."""
_pin_state = self._peek_session_state(session_key) if session_key else None
_eph_pin = _pin_state.conversation.ephemeral_pin if _pin_state else None
if _eph_pin is not None and _eph_pin[2] != redact_pii:
diff --git a/gateway/run_turn_prepare.py b/gateway/run_turn_prepare.py
index 664a5264f99..21dbec46e40 100644
--- a/gateway/run_turn_prepare.py
+++ b/gateway/run_turn_prepare.py
@@ -639,7 +639,10 @@ class GatewayTurnPrepareMixin:
if source.platform != Platform.LOCAL and event.internal and session_key:
await self._rehydrate_prompt_pins(session_key, session_entry.session_id)
context_prompt = "" if source.platform == Platform.LOCAL else \
- self._pinned_session_context_prompt(context, _redact_pii, session_key, internal=event.internal)
+ self._pinned_session_context_prompt(
+ context, _redact_pii, session_key,
+ internal=event.internal or getattr(event, "inherit_channel_pin", False),
+ )
# Per-turn notes ride the user message via the api_content sidecar, NOT context_prompt
# (appending to the ephemeral system prompt forced a full agent rebuild).