44
55import asyncio
66from typing import Any
7- from unittest .mock import MagicMock
87
98import pytest
109
1110from ddev .ai .agent .types import AgentResponse , ContextUsage , StopReason , TokenUsage , ToolResultMessage
1211from ddev .ai .phases .agentic_phase import AgenticPhase
1312from ddev .ai .phases .checkpoint import CheckpointManager
14- from ddev .ai .phases .config import AgentConfig , PhaseConfig , TaskConfig
13+ from ddev .ai .phases .config import PhaseConfig , TaskConfig
1514from ddev .ai .tools .fs .file_access_policy import FileAccessPolicy
1615from ddev .ai .tools .fs .file_registry import FileRegistry
1716from ddev .ai .tools .registry import ToolRegistry
@@ -86,24 +85,21 @@ async def compact_preserving_last_turn(self) -> AgentResponse | None:
8685 return None
8786
8887
89- def make_agent_factory (mock_agent : MockAgent , captured_kwargs : dict [str , Any ] | None = None ):
90- """Create a callable that replaces AnthropicAgent constructor, returning the given mock.
88+ def make_agent_builder (mock_agent : MockAgent , captured_kwargs : dict [str , Any ] | None = None ):
89+ """Create an agent_builder that returns the given mock and an empty ToolRegistry .
9190
92- If ``captured_kwargs`` is provided, every call updates it with the kwargs passed to
93- the constructor — useful for asserting on system_prompt, tools, etc .
91+ If ``captured_kwargs`` is provided, every call records the system_prompt and
92+ owner_id passed in — useful for asserting on prompt rendering .
9493 """
9594
96- def factory ( ** kwargs : Any ) -> MockAgent :
95+ def builder ( system_prompt : str , owner_id : str ) -> tuple [ MockAgent , ToolRegistry ] :
9796 if captured_kwargs is not None :
98- captured_kwargs .update (kwargs )
99- mock_agent .name = kwargs .get ("name" , "mock" )
100- return mock_agent
97+ captured_kwargs ["system_prompt" ] = system_prompt
98+ captured_kwargs ["owner_id" ] = owner_id
99+ mock_agent .name = owner_id
100+ return mock_agent , ToolRegistry ([])
101101
102- return factory
103-
104-
105- def _empty_registry_from_names (cls , names , * , owner_id , file_registry ):
106- return ToolRegistry ([])
102+ return builder
107103
108104
109105def make_agent_phase (
@@ -116,7 +112,6 @@ def make_agent_phase(
116112 dependencies : list [str ] | None = None ,
117113 tasks : list [TaskConfig ] | None = None ,
118114 checkpoint = None ,
119- agent_tools : list [str ] | None = None ,
120115 flow_variables : dict [str , str ] | None = None ,
121116 runtime_variables : dict [str , str ] | None = None ,
122117 context_compact_threshold_pct : int = 80 ,
@@ -125,31 +120,22 @@ def make_agent_phase(
125120) -> tuple [AgenticPhase , CheckpointManager ]:
126121 """Build an AgenticPhase ready for process_message-driven tests.
127122
128- Patches ``AnthropicAgent`` and ``ToolRegistry.from_names`` so no real LLM or tools
129- are constructed. Pass ``captured_agent_kwargs`` (a dict) to record AnthropicAgent
130- constructor kwargs across calls (e.g. to inspect system_prompt rendering).
123+ Injects a mock agent_builder so no real LLM or tools are constructed. Pass
124+ ``captured_agent_kwargs`` (a dict) to record the rendered system_prompt and owner_id.
131125 """
132- monkeypatch .setattr (
133- "ddev.ai.phases.agentic_phase.AnthropicAgent" ,
134- make_agent_factory (mock_agent , captured_agent_kwargs ),
135- )
136- monkeypatch .setattr (ToolRegistry , "from_names" , classmethod (_empty_registry_from_names ))
137-
138126 config = PhaseConfig (
139127 agent = "writer" ,
140128 tasks = tasks or [TaskConfig (name = "t1" , prompt = "Do the work." )],
141129 checkpoint = checkpoint ,
142130 context_compact_threshold_pct = context_compact_threshold_pct ,
143131 )
144- agent_config = AgentConfig (tools = agent_tools or [])
145132 checkpoint_manager = CheckpointManager (flow_dir / "checkpoints.yaml" )
146133
147134 phase = AgenticPhase (
148135 phase_id = phase_id ,
149136 dependencies = dependencies or [],
150137 config = config ,
151- agent_config = agent_config ,
152- anthropic_client = MagicMock (),
138+ agent_builder = make_agent_builder (mock_agent , captured_agent_kwargs ),
153139 checkpoint_manager = checkpoint_manager ,
154140 runtime_variables = runtime_variables or {},
155141 flow_variables = flow_variables or {},
0 commit comments