Skip to content

The agent that grows with you

refactor(bot-mode): re-authorize message_agent on staged snapshots without copying the agent

kshitijk4poor57f05e2142f7
diff --git a/tools/bot_mode_dm.py b/tools/bot_mode_dm.py
index cd56958e645..2151b898c0e 100644
--- a/tools/bot_mode_dm.py
+++ b/tools/bot_mode_dm.py
@@ -108,6 +108,23 @@ def message_agent_tool_schema() -> dict:
}
+def message_agent_authorized(agent: Any) -> bool:
+ """The ``message_agent`` gate: a protocol-enabled agent whose session is a managed
+ Bot-Mode canonical Bot Chat. Session-stable, so it is prompt-cache safe to re-evaluate
+ on every tool-snapshot rebuild. Never raises."""
+ try:
+ if not getattr(agent, "_bot_mode_protocol", True):
+ return False
+ from tools.bot_mode_probe import BOT_CHAT_TITLE, is_bot_mode_managed
+
+ # Managed-install check, NOT section non-emptiness: a SOUL.md carrying the
+ # legacy protocol text gets an empty section but must still get the tool.
+ return _session_title(agent) == BOT_CHAT_TITLE and is_bot_mode_managed(_agent_home(agent))
+ except Exception: # pragma: no cover — must never break a turn
+ logger.debug("message_agent_authorized failed", exc_info=True)
+ return False
+
+
def ensure_message_agent_tool(agent: Any) -> bool:
"""Inject the ``message_agent`` schema into a Bot Chat agent's tool list (once per turn).
Idempotent and deterministic for the session's life (the gate is stable from the
@@ -121,11 +138,7 @@ def ensure_message_agent_tool(agent: Any) -> bool:
for t in tools
):
return True
- from tools.bot_mode_probe import BOT_CHAT_TITLE, is_bot_mode_managed
-
- # Managed-install check, NOT section non-emptiness: a SOUL.md carrying the
- # legacy protocol text gets an empty section but must still get the tool.
- if _session_title(agent) != BOT_CHAT_TITLE or not is_bot_mode_managed(_agent_home(agent)):
+ if not message_agent_authorized(agent):
return False
if agent.tools is None:
agent.tools = []
diff --git a/tools/mcp_tool_agent.py b/tools/mcp_tool_agent.py
index d707add5544..b355ce99bf2 100644
--- a/tools/mcp_tool_agent.py
+++ b/tools/mcp_tool_agent.py
@@ -2,7 +2,6 @@
AIAgent's tools/tool names, preserving the cached tools[] prefix across rebuilds,
and re-injecting post-build tools."""
-import copy
import logging
import json
import threading
@@ -197,18 +196,16 @@ def _merge_preserving_prefix(current_defs: list, new_defs: list, registered_name
def _reinject_authorized_dynamic_tools(agent, tools_list: list, name_set: set) -> None:
- """Re-authorize session capabilities against a staged snapshot before publication."""
- try:
- from tools.bot_mode_dm import MESSAGE_AGENT_TOOL_NAME, ensure_message_agent_tool
-
- tools_list[:] = [entry for entry in tools_list if _def_name(entry) != MESSAGE_AGENT_TOOL_NAME]
- name_set.discard(MESSAGE_AGENT_TOOL_NAME)
- staged_agent = copy.copy(agent)
- staged_agent.tools = tools_list
- staged_agent.valid_tool_names = name_set
- ensure_message_agent_tool(staged_agent)
- except Exception:
- logger.debug("Dynamic tool re-injection skipped", exc_info=True)
+ """``message_agent`` is injected by an auth gate, never registered, so a registry-derived
+ rebuild drops it. Scrub any stale copy from the STAGED pair and re-add it only when the live
+ gate re-authorizes, so the publisher exposes a coherent ``(tools, valid_tool_names)``."""
+ from tools.bot_mode_dm import MESSAGE_AGENT_TOOL_NAME, message_agent_authorized, message_agent_tool_schema
+
+ tools_list[:] = [entry for entry in tools_list if _def_name(entry) != MESSAGE_AGENT_TOOL_NAME]
+ name_set.discard(MESSAGE_AGENT_TOOL_NAME)
+ if message_agent_authorized(agent):
+ tools_list.append(message_agent_tool_schema())
+ name_set.add(MESSAGE_AGENT_TOOL_NAME)
def _reinject_post_build_tools(agent, tools_list: list, name_set: set) -> set: