The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(background-review): collapse the tool-surface copy to the agent_init shape; 2 tests
kshitijk4poor586a7831d277
diff --git a/agent/background_review.py b/agent/background_review.pyindex fd77d63fc3a..34eb6f29d41 100644--- a/agent/background_review.py+++ b/agent/background_review.py@@ -842,41 +842,23 @@ def _fork_init_kwargs(agent: Any, rt: Dict[str, Any], routed: bool, max_iteratioreturn kwargs-# Sentinel generation above any live registry generation. Mid-review compaction-# boundaries re-run refresh_agent_mcp_tools(content_aware=True); freezing the-# snapshot generation prevents compaction from rebuilding agent.tools and dropping-# inherited provider tools (#103579).-_FROZEN_TOOL_SNAPSHOT_GENERATION: int = 2_147_483_647+# Above any live registry generation: _publish_tool_snapshot refuses an older-generation rebuild,+# so the compaction-boundary refresh_agent_mcp_tools(content_aware=True) cannot rebuild the fork's+# tools[] from the live registry and drop the inherited provider/plugin tools (#103579).+_FROZEN_TOOL_SNAPSHOT_GENERATION = 2_147_483_647def _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.-- Freezes ``_tool_snapshot_generation`` so mid-review compaction boundaries- (which invoke ``refresh_agent_mcp_tools(content_aware=True)``) refuse the- rebuild and do not clobber the inherited tools (#103579).- """- parent_tools = getattr(agent, "tools", None)- if not isinstance(parent_tools, (list, tuple)):- 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)- }+ """Same-model fork: advertise the parent's exact tools[] (its last outbound payload — an+ empty list included) so the request prefix matches byte-for-byte, then freeze the snapshot+ generation. Dispatch stays behind the review whitelist; advertising is not permission."""+ # getattr: /btw and review callers build bare object.__new__ agents in tests without ``tools``.+ review_agent.tools = copy.deepcopy(getattr(agent, "tools", None) or [])+ review_agent.valid_tool_names = {tool["function"]["name"] for tool in review_agent.tools}review_agent._tool_snapshot_generation = _FROZEN_TOOL_SNAPSHOT_GENERATION+def build_cache_parity_fork(agent: Any, task_cfg: Optional[Dict[str, Any]] = None, *, max_iterations: int,write_origin: str = "background_review",diff --git a/tests/run_agent/test_background_review_cache_parity.py b/tests/run_agent/test_background_review_cache_parity.pyindex 92959c9c0b4..91ff789f0af 100644--- a/tests/run_agent/test_background_review_cache_parity.py+++ b/tests/run_agent/test_background_review_cache_parity.py@@ -345,70 +345,6 @@ def test_routed_review_fork_does_not_inherit_reasoning_config():)-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--def test_review_fork_inherited_tools_survive_compaction_refresh():"""Inherited tools survive mid-review compaction refresh (#103579).@@ -419,7 +355,7 @@ def test_review_fork_inherited_tools_survive_compaction_refresh():generation staleness check refuses the rebuild."""import run_agent- from agent.background_review import build_cache_parity_fork+ from agent.background_review import _FROZEN_TOOL_SNAPSHOT_GENERATION, build_cache_parity_forkfrom tools.mcp_tool_agent import refresh_agent_mcp_toolsagent = _make_agent_stub(run_agent.AIAgent)@@ -437,10 +373,10 @@ def test_review_fork_inherited_tools_survive_compaction_refresh():with patch.object(run_agent, "AIAgent", _Recorder):fork, _rt, routed = build_cache_parity_fork(agent, max_iterations=5)assert not routed- assert fork._tool_snapshot_generation == 2_147_483_647- assert [t["function"]["name"] for t in fork.tools] == [- "terminal_command", "read_file", "memory", "fact_store", "fact_feedback"- ]+ assert fork._tool_snapshot_generation == _FROZEN_TOOL_SNAPSHOT_GENERATION+ assert fork.tools == parent_tools+ # Deep copy: the fork's later in-place tool edits must not leak into the parent's array.+ assert fork.tools is not parent_tools and fork.tools[0] is not parent_tools[0]# Simulate mid-review compaction boundary tool refreshadded = refresh_agent_mcp_tools(fork, content_aware=True)@@ -462,7 +398,7 @@ def test_unrouted_review_fork_inherits_empty_tool_surface():mid-review compaction do not break cache parity against the parent's empty surface."""import run_agent- from agent.background_review import build_cache_parity_fork+ from agent.background_review import _FROZEN_TOOL_SNAPSHOT_GENERATION, build_cache_parity_forkfrom tools.mcp_tool_agent import refresh_agent_mcp_toolsagent = _make_agent_stub(run_agent.AIAgent)@@ -483,13 +419,10 @@ def test_unrouted_review_fork_inherits_empty_tool_surface():assert fork.tools == []assert fork.tools is not agent.toolsassert fork.valid_tool_names == set()- assert fork._tool_snapshot_generation == 2_147_483_647+ assert fork._tool_snapshot_generation == _FROZEN_TOOL_SNAPSHOT_GENERATION# Compaction refresh must refuse rebuild on frozen snapshotadded = refresh_agent_mcp_tools(fork, content_aware=True)assert added == set()assert fork.tools == []assert fork.valid_tool_names == set()---