Skip to content

The agent that grows with you

fix(gateway): stop synthetic goal/heartbeat turns from stomping channel_pin

chelsealong994df5e6de8c
diff --git a/gateway/platforms/event.py b/gateway/platforms/event.py
index 54ecff5519e..a861190cade 100644
--- a/gateway/platforms/event.py
+++ b/gateway/platforms/event.py
@@ -91,6 +91,11 @@ 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)
diff --git a/gateway/run_agent_cache.py b/gateway/run_agent_cache.py
index aa8fc266700..81db8f0f5e7 100644
--- a/gateway/run_agent_cache.py
+++ b/gateway/run_agent_cache.py
@@ -744,10 +744,12 @@ class GatewayAgentCacheMixin:
"""``(channel_prompt, source)`` for this turn's agent run.
The ephemeral system prompt also appends ``channel_prompt`` and the ``channel_overrides``
- prompt (looked up by chat/thread/``parent_chat_id``). Internal events carry
- ``channel_prompt=None`` and a source without ``parent_chat_id``, so they dropped both and
- toggled the system prompt like the context pin did. Human turns record their inputs;
- internal turns reuse them (``_rehydrate_prompt_pins`` restores both after a restart)."""
+ prompt (looked up by chat/thread/``parent_chat_id``). Events with no channel inputs of
+ their own (internal events, and synthetic goal/heartbeat/resume turns — see
+ ``MessageEvent.inherit_channel_pin``) carry ``channel_prompt=None`` and a source without
+ ``parent_chat_id``, so they dropped both and toggled the system prompt like the context pin
+ did. Human turns record their inputs; turns that ask to reuse the pin get them back
+ (``_rehydrate_prompt_pins`` restores both after a restart)."""
if not session_key:
return channel_prompt, source
if not internal:
diff --git a/gateway/run_goals.py b/gateway/run_goals.py
index 72208ae5b21..822b2f7e2ec 100644
--- a/gateway/run_goals.py
+++ b/gateway/run_goals.py
@@ -108,7 +108,10 @@ class GatewayGoalsMixin:
would quote that stale message (Telegram DM topics route anchorless via the topic id).
"""
source = dataclasses.replace(source, message_id=None) if getattr(source, "message_id", None) else source
- return MessageEvent(text=text, message_type=MessageType.TEXT, source=source, internal=internal)
+ return MessageEvent(
+ text=text, message_type=MessageType.TEXT, source=source, internal=internal,
+ inherit_channel_pin=True,
+ )
def _register_heartbeat_watch(self, quick_key: str, source: Any, session_id: str) -> None:
"""Track the canonical route and start the restart-recoverable poller."""
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index cceaf8e2427..6b00733fafd 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -257,9 +257,11 @@ class GatewayTurnMixin(GatewayTurnPrepareMixin, GatewayTurnHygieneMixin, Gateway
# Admission/typing is not execution. All routing, authorization and
# turn preparation gates have passed when the agent runner is entered.
event._heartbeat_execution_started = True
- # Internal events reuse the last human turn's channel inputs (see _pinned_channel_inputs).
+ # Internal and pin-inheriting synthetic events reuse the last human turn's channel
+ # inputs (see _pinned_channel_inputs).
_turn_channel_prompt, _turn_source = self._pinned_channel_inputs(
- session_key, event.channel_prompt, source, internal=event.internal,
+ session_key, event.channel_prompt, source,
+ internal=event.internal or getattr(event, "inherit_channel_pin", False),
)
if not event.internal:
# Persist the coherent context+channel pair before execution: a crash during the
@@ -1922,7 +1924,8 @@ class GatewayTurnMixin(GatewayTurnPrepareMixin, GatewayTurnHygieneMixin, Gateway
next_message_id = self._reply_anchor_for_event(pending_event)
next_inbound_id = str(pending_event.message_id) if getattr(pending_event, "message_id", None) else None
next_channel_prompt, next_source = self._pinned_channel_inputs(
- next_session_key, pending_event.channel_prompt, next_source, internal=pending_event.internal,
+ next_session_key, pending_event.channel_prompt, next_source,
+ internal=pending_event.internal or getattr(pending_event, "inherit_channel_pin", False),
)
if not pending_event.internal:
# A drained human turn re-pins its channel inputs; make them durable like a first turn.
diff --git a/gateway/slash_commands_goals.py b/gateway/slash_commands_goals.py
index 03e7cca3254..61d58976179 100644
--- a/gateway/slash_commands_goals.py
+++ b/gateway/slash_commands_goals.py
@@ -93,6 +93,7 @@ class GatewayGoalCommandsMixin:
message_id=event.message_id if kickoff else None,
channel_prompt=event.channel_prompt if kickoff else None,
auto_skill=event.auto_skill if kickoff else None,
+ inherit_channel_pin=not kickoff,
)
self._enqueue_fifo(quick_key, turn, adapter)
except Exception as exc: