Skip to content

The agent that grows with you

fix(agent): inherit parent's full tool surface on review fork for cache parity (#103579)

0xAlyDev6a97436e29a5
diff --git a/agent/background_review.py b/agent/background_review.py
index 9ed654f0986..3eaf4db4980 100644
--- a/agent/background_review.py
+++ b/agent/background_review.py
@@ -842,6 +842,29 @@ def _fork_init_kwargs(agent: Any, rt: Dict[str, Any], routed: bool, max_iteratio
return kwargs
+def _inherit_parent_tool_surface(review_agent: Any, agent: Any) -> None:
+ """Copy the parent's advertised tools for a same-model cache-parity fork.
+
+ The parent's array is the last outbound payload: ``agent.tools`` is read
+ directly at request time and is not mutated between review turns because
+ ``_skip_mcp_refresh`` blocks the between-turn MCP refresh. Dispatch remains
+ restricted by the thread whitelist. Copying the whole surface covers every
+ dynamically injected tool (memory-provider, late MCP, and plugin-registered),
+ not just memory tools.
+ """
+ parent_tools = getattr(agent, "tools", None)
+ if not isinstance(parent_tools, (list, tuple)) or not parent_tools:
+ return
+ review_agent.tools = copy.deepcopy(list(parent_tools))
+ review_agent.valid_tool_names = {
+ entry["function"]["name"]
+ for entry in review_agent.tools
+ if isinstance(entry, dict)
+ and isinstance(entry.get("function"), dict)
+ and isinstance(entry["function"].get("name"), str)
+ }
+
+
def build_cache_parity_fork(
agent: Any, task_cfg: Optional[Dict[str, Any]] = None, *, max_iterations: int,
write_origin: str = "background_review",
@@ -887,6 +910,7 @@ def build_cache_parity_fork(
if not _routed:
review_agent._cached_system_prompt = agent._cached_system_prompt
review_agent.session_start = agent.session_start
+ _inherit_parent_tool_surface(review_agent, agent)
_detach_fork_compression(review_agent)
# Compaction bounds a single request; this bounds the WHOLE review (checked in
# conversation_loop via _review_input_budget_exhausted).
diff --git a/tests/run_agent/test_background_review_cache_parity.py b/tests/run_agent/test_background_review_cache_parity.py
index da5f1dcb614..3d8c16a135b 100644
--- a/tests/run_agent/test_background_review_cache_parity.py
+++ b/tests/run_agent/test_background_review_cache_parity.py
@@ -101,6 +101,8 @@ def _make_recorder_class(captured=None, record_on_run=()):
self.suppress_status_output = None
self.session_start = None
self.session_id = None
+ self.tools = None
+ self.valid_tool_names = set()
self.ephemeral_system_prompt = kwargs.get("ephemeral_system_prompt")
def run_conversation(self, *args, **kwargs):
@@ -340,3 +342,68 @@ def test_routed_review_fork_does_not_inherit_reasoning_config():
f"Routed review fork was passed parent-only kwarg {_gated!r}; "
"cache-parity inheritance must stay behind the not-routed gate."
)
+
+
+def test_unrouted_review_fork_inherits_entire_tool_surface():
+ """Unrouted forks preserve all advertised tools and their name index (#103579)."""
+ import run_agent
+ from agent.background_review import build_cache_parity_fork
+
+ agent = _make_agent_stub(run_agent.AIAgent)
+ parent_tools = [
+ {"type": "function", "function": {"name": "base_tool"}},
+ {"type": "function", "function": {"name": "fact_store"}},
+ {"type": "function", "function": {"name": "fact_feedback"}},
+ {"type": "function", "function": {"name": "mcp_late_tool"}},
+ {"malformed": True},
+ ]
+ agent.tools = parent_tools
+
+ _Recorder = _make_recorder_class()
+
+ with patch.object(run_agent, "AIAgent", _Recorder):
+ fork, _rt, routed = build_cache_parity_fork(agent, max_iterations=5)
+ assert not routed
+ assert fork.tools == parent_tools
+ assert fork.tools is not parent_tools
+ assert fork.tools[0] is not parent_tools[0]
+ assert fork.valid_tool_names == {
+ "base_tool", "fact_store", "fact_feedback", "mcp_late_tool"
+ }
+ fork.tools.append({"type": "function", "function": {"name": "new"}})
+ assert len(fork.tools) != len(agent.tools)
+
+
+def test_routed_review_fork_does_not_inherit_tool_surface():
+ """Routed review fork does not inherit parent's tools[] across different models (#103579)."""
+ import run_agent
+ import agent.background_review as bg_review
+ from agent.background_review import build_cache_parity_fork
+
+ agent = _make_agent_stub(run_agent.AIAgent)
+ parent_tools = [
+ {"type": "function", "function": {"name": "parent_tool"}},
+ ]
+ agent.tools = parent_tools
+
+ _Recorder = _make_recorder_class()
+ routed_runtime = {
+ "provider": "openrouter",
+ "model": "aux-cheap-model",
+ "api_key": "test-key",
+ "base_url": None,
+ "api_mode": None,
+ "credential_pool": None,
+ "request_overrides": {},
+ "max_tokens": None,
+ "command": None,
+ "args": [],
+ "routed": True,
+ }
+
+ with patch.object(run_agent, "AIAgent", _Recorder), \
+ patch.object(bg_review, "_resolve_review_runtime", return_value=routed_runtime):
+ fork, _rt, routed = build_cache_parity_fork(agent, max_iterations=5)
+ assert routed
+ assert fork.tools is None
+