|
14 | 14 | AssistantMessageInterface, |
15 | 15 | AgentRuntimeError, |
16 | 16 | CancelledException, |
| 17 | + CommandInterface, |
17 | 18 | ) |
18 | 19 |
|
19 | 20 | pytestmark = [pytest.mark.e2e, pytest.mark.asyncio] |
@@ -107,6 +108,149 @@ async def abort_on_first(msg: AssistantMessageInterface): |
107 | 108 | ) |
108 | 109 |
|
109 | 110 |
|
| 111 | +class TestPrePostCommands: |
| 112 | + async def test_pre_command_output_streamed( |
| 113 | + self, client: RuntimeUseClient, make_query_options |
| 114 | + ): |
| 115 | + received: list[AssistantMessageInterface] = [] |
| 116 | + |
| 117 | + async def on_msg(msg: AssistantMessageInterface): |
| 118 | + received.append(msg) |
| 119 | + |
| 120 | + result = await client.query( |
| 121 | + prompt="ECHO:hello", |
| 122 | + options=make_query_options( |
| 123 | + pre_agent_invocation_commands=[ |
| 124 | + CommandInterface(command="echo pre-sentinel") |
| 125 | + ], |
| 126 | + on_assistant_message=on_msg, |
| 127 | + ), |
| 128 | + ) |
| 129 | + |
| 130 | + assert isinstance(result.data, TextResult) |
| 131 | + assert result.data.text == "hello" |
| 132 | + all_text = [block for msg in received for block in msg.text_blocks] |
| 133 | + assert any("pre-sentinel" in t for t in all_text) |
| 134 | + |
| 135 | + async def test_post_command_output_streamed( |
| 136 | + self, client: RuntimeUseClient, make_query_options |
| 137 | + ): |
| 138 | + received: list[AssistantMessageInterface] = [] |
| 139 | + |
| 140 | + async def on_msg(msg: AssistantMessageInterface): |
| 141 | + received.append(msg) |
| 142 | + |
| 143 | + result = await client.query( |
| 144 | + prompt="ECHO:hello", |
| 145 | + options=make_query_options( |
| 146 | + post_agent_invocation_commands=[ |
| 147 | + CommandInterface(command="echo post-sentinel") |
| 148 | + ], |
| 149 | + on_assistant_message=on_msg, |
| 150 | + ), |
| 151 | + ) |
| 152 | + |
| 153 | + assert isinstance(result.data, TextResult) |
| 154 | + assert result.data.text == "hello" |
| 155 | + all_text = [block for msg in received for block in msg.text_blocks] |
| 156 | + assert any("post-sentinel" in t for t in all_text) |
| 157 | + |
| 158 | + async def test_pre_and_post_commands_both_run( |
| 159 | + self, client: RuntimeUseClient, make_query_options |
| 160 | + ): |
| 161 | + received: list[AssistantMessageInterface] = [] |
| 162 | + |
| 163 | + async def on_msg(msg: AssistantMessageInterface): |
| 164 | + received.append(msg) |
| 165 | + |
| 166 | + result = await client.query( |
| 167 | + prompt="ECHO:hello", |
| 168 | + options=make_query_options( |
| 169 | + pre_agent_invocation_commands=[ |
| 170 | + CommandInterface(command="echo pre-sentinel") |
| 171 | + ], |
| 172 | + post_agent_invocation_commands=[ |
| 173 | + CommandInterface(command="echo post-sentinel") |
| 174 | + ], |
| 175 | + on_assistant_message=on_msg, |
| 176 | + ), |
| 177 | + ) |
| 178 | + |
| 179 | + assert isinstance(result.data, TextResult) |
| 180 | + assert result.data.text == "hello" |
| 181 | + all_text = [block for msg in received for block in msg.text_blocks] |
| 182 | + assert any("pre-sentinel" in t for t in all_text) |
| 183 | + assert any("post-sentinel" in t for t in all_text) |
| 184 | + |
| 185 | + async def test_pre_command_with_cwd( |
| 186 | + self, client: RuntimeUseClient, make_query_options |
| 187 | + ): |
| 188 | + received: list[AssistantMessageInterface] = [] |
| 189 | + |
| 190 | + async def on_msg(msg: AssistantMessageInterface): |
| 191 | + received.append(msg) |
| 192 | + |
| 193 | + await client.query( |
| 194 | + prompt="ECHO:ok", |
| 195 | + options=make_query_options( |
| 196 | + pre_agent_invocation_commands=[ |
| 197 | + CommandInterface(command="pwd", cwd="/tmp") |
| 198 | + ], |
| 199 | + on_assistant_message=on_msg, |
| 200 | + ), |
| 201 | + ) |
| 202 | + |
| 203 | + all_text = [block for msg in received for block in msg.text_blocks] |
| 204 | + assert any("/tmp" in t for t in all_text) |
| 205 | + |
| 206 | + async def test_post_command_with_cwd( |
| 207 | + self, client: RuntimeUseClient, make_query_options |
| 208 | + ): |
| 209 | + received: list[AssistantMessageInterface] = [] |
| 210 | + |
| 211 | + async def on_msg(msg: AssistantMessageInterface): |
| 212 | + received.append(msg) |
| 213 | + |
| 214 | + await client.query( |
| 215 | + prompt="ECHO:ok", |
| 216 | + options=make_query_options( |
| 217 | + post_agent_invocation_commands=[ |
| 218 | + CommandInterface(command="pwd", cwd="/tmp") |
| 219 | + ], |
| 220 | + on_assistant_message=on_msg, |
| 221 | + ), |
| 222 | + ) |
| 223 | + |
| 224 | + all_text = [block for msg in received for block in msg.text_blocks] |
| 225 | + assert any("/tmp" in t for t in all_text) |
| 226 | + |
| 227 | + async def test_failed_pre_command_raises_error( |
| 228 | + self, client: RuntimeUseClient, make_query_options |
| 229 | + ): |
| 230 | + with pytest.raises(AgentRuntimeError, match="failed with exit code"): |
| 231 | + await client.query( |
| 232 | + prompt="ECHO:should not reach", |
| 233 | + options=make_query_options( |
| 234 | + pre_agent_invocation_commands=[ |
| 235 | + CommandInterface(command="exit 1") |
| 236 | + ], |
| 237 | + ), |
| 238 | + ) |
| 239 | + |
| 240 | + async def test_failed_post_command_raises_error( |
| 241 | + self, client: RuntimeUseClient, make_query_options |
| 242 | + ): |
| 243 | + with pytest.raises(AgentRuntimeError, match="failed with exit code"): |
| 244 | + await client.query( |
| 245 | + prompt="ECHO:hello", |
| 246 | + options=make_query_options( |
| 247 | + post_agent_invocation_commands=[ |
| 248 | + CommandInterface(command="exit 1") |
| 249 | + ], |
| 250 | + ), |
| 251 | + ) |
| 252 | + |
| 253 | + |
110 | 254 | class TestInvocationFieldsForwarded: |
111 | 255 | async def test_fields_round_trip( |
112 | 256 | self, client: RuntimeUseClient, make_query_options |
|
0 commit comments