|
34 | 34 | ) |
35 | 35 | from acp.connection import Connection |
36 | 36 | from acp.core import AgentSideConnection, ClientSideConnection |
| 37 | +from acp.exceptions import RequestError |
37 | 38 | from acp.schema import ( |
38 | 39 | AgentMessageChunk, |
39 | 40 | AllowedOutcome, |
@@ -144,6 +145,154 @@ async def test_session_notifications_flow(connect, client): |
144 | 145 | assert client.notifications[0].session_id == "sess" |
145 | 146 |
|
146 | 147 |
|
| 148 | +@pytest.mark.asyncio |
| 149 | +async def test_response_waits_for_preceding_notification(server): |
| 150 | + notification_started = asyncio.Event() |
| 151 | + release_notification = asyncio.Event() |
| 152 | + |
| 153 | + class _BlockingClient(TestClient): |
| 154 | + async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> None: |
| 155 | + notification_started.set() |
| 156 | + await release_notification.wait() |
| 157 | + await super().session_update(session_id, update, **kwargs) |
| 158 | + |
| 159 | + client = _BlockingClient() |
| 160 | + conn = ClientSideConnection(client, server.client_writer, server.client_reader) |
| 161 | + request = asyncio.create_task( |
| 162 | + conn.prompt(session_id="sess", prompt=[TextContentBlock(type="text", text="question")]) |
| 163 | + ) |
| 164 | + |
| 165 | + request_message = json.loads(await server.server_reader.readline()) |
| 166 | + notification = { |
| 167 | + "jsonrpc": "2.0", |
| 168 | + "method": "session/update", |
| 169 | + "params": { |
| 170 | + "sessionId": "sess", |
| 171 | + "update": { |
| 172 | + "sessionUpdate": "agent_message_chunk", |
| 173 | + "content": {"type": "text", "text": "answer"}, |
| 174 | + }, |
| 175 | + }, |
| 176 | + } |
| 177 | + response = {"jsonrpc": "2.0", "id": request_message["id"], "result": {"stopReason": "end_turn"}} |
| 178 | + server.server_writer.write((json.dumps(notification) + "\n" + json.dumps(response) + "\n").encode()) |
| 179 | + await server.server_writer.drain() |
| 180 | + |
| 181 | + await asyncio.wait_for(notification_started.wait(), timeout=1) |
| 182 | + await asyncio.sleep(0) |
| 183 | + assert not request.done() |
| 184 | + |
| 185 | + release_notification.set() |
| 186 | + prompt_response = await asyncio.wait_for(request, timeout=1) |
| 187 | + assert prompt_response.stop_reason == "end_turn" |
| 188 | + assert len(client.notifications) == 1 |
| 189 | + assert client.notifications[0].session_id == "sess" |
| 190 | + await conn.close() |
| 191 | + |
| 192 | + |
| 193 | +@pytest.mark.asyncio |
| 194 | +async def test_error_response_waits_for_preceding_notification(server): |
| 195 | + notification_started = asyncio.Event() |
| 196 | + release_notification = asyncio.Event() |
| 197 | + |
| 198 | + class _BlockingClient(TestClient): |
| 199 | + async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> None: |
| 200 | + notification_started.set() |
| 201 | + await release_notification.wait() |
| 202 | + |
| 203 | + conn = ClientSideConnection(_BlockingClient(), server.client_writer, server.client_reader) |
| 204 | + request = asyncio.create_task( |
| 205 | + conn.prompt(session_id="sess", prompt=[TextContentBlock(type="text", text="question")]) |
| 206 | + ) |
| 207 | + |
| 208 | + request_message = json.loads(await server.server_reader.readline()) |
| 209 | + notification = { |
| 210 | + "jsonrpc": "2.0", |
| 211 | + "method": "session/update", |
| 212 | + "params": { |
| 213 | + "sessionId": "sess", |
| 214 | + "update": { |
| 215 | + "sessionUpdate": "agent_message_chunk", |
| 216 | + "content": {"type": "text", "text": "partial answer"}, |
| 217 | + }, |
| 218 | + }, |
| 219 | + } |
| 220 | + response = { |
| 221 | + "jsonrpc": "2.0", |
| 222 | + "id": request_message["id"], |
| 223 | + "error": {"code": -32603, "message": "prompt failed"}, |
| 224 | + } |
| 225 | + server.server_writer.write((json.dumps(notification) + "\n" + json.dumps(response) + "\n").encode()) |
| 226 | + await server.server_writer.drain() |
| 227 | + |
| 228 | + await asyncio.wait_for(notification_started.wait(), timeout=1) |
| 229 | + await asyncio.sleep(0) |
| 230 | + assert not request.done() |
| 231 | + |
| 232 | + release_notification.set() |
| 233 | + with pytest.raises(RequestError, match="prompt failed"): |
| 234 | + await asyncio.wait_for(request, timeout=1) |
| 235 | + await conn.close() |
| 236 | + |
| 237 | + |
| 238 | +@pytest.mark.asyncio |
| 239 | +async def test_notification_can_await_nested_request(server): |
| 240 | + notification_finished = asyncio.Event() |
| 241 | + |
| 242 | + class _NestedPromptClient(TestClient): |
| 243 | + def __init__(self) -> None: |
| 244 | + super().__init__() |
| 245 | + self.conn: Agent | None = None |
| 246 | + self.nested_result: PromptResponse | None = None |
| 247 | + |
| 248 | + def on_connect(self, conn: Agent) -> None: |
| 249 | + self.conn = conn |
| 250 | + |
| 251 | + async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> None: |
| 252 | + assert self.conn is not None |
| 253 | + self.nested_result = await self.conn.prompt( |
| 254 | + session_id=session_id, |
| 255 | + prompt=[TextContentBlock(type="text", text="nested question")], |
| 256 | + ) |
| 257 | + notification_finished.set() |
| 258 | + |
| 259 | + client = _NestedPromptClient() |
| 260 | + conn = ClientSideConnection(client, server.client_writer, server.client_reader) |
| 261 | + outer_request = asyncio.create_task( |
| 262 | + conn.prompt(session_id="sess", prompt=[TextContentBlock(type="text", text="outer question")]) |
| 263 | + ) |
| 264 | + outer_message = json.loads(await server.server_reader.readline()) |
| 265 | + |
| 266 | + notification = { |
| 267 | + "jsonrpc": "2.0", |
| 268 | + "method": "session/update", |
| 269 | + "params": { |
| 270 | + "sessionId": "sess", |
| 271 | + "update": { |
| 272 | + "sessionUpdate": "agent_message_chunk", |
| 273 | + "content": {"type": "text", "text": "answer"}, |
| 274 | + }, |
| 275 | + }, |
| 276 | + } |
| 277 | + server.server_writer.write((json.dumps(notification) + "\n").encode()) |
| 278 | + await server.server_writer.drain() |
| 279 | + |
| 280 | + nested_message = json.loads(await asyncio.wait_for(server.server_reader.readline(), timeout=1)) |
| 281 | + nested_response = {"jsonrpc": "2.0", "id": nested_message["id"], "result": {"stopReason": "end_turn"}} |
| 282 | + server.server_writer.write((json.dumps(nested_response) + "\n").encode()) |
| 283 | + await server.server_writer.drain() |
| 284 | + |
| 285 | + await asyncio.wait_for(notification_finished.wait(), timeout=1) |
| 286 | + assert client.nested_result is not None |
| 287 | + assert client.nested_result.stop_reason == "end_turn" |
| 288 | + |
| 289 | + outer_response = {"jsonrpc": "2.0", "id": outer_message["id"], "result": {"stopReason": "end_turn"}} |
| 290 | + server.server_writer.write((json.dumps(outer_response) + "\n").encode()) |
| 291 | + await server.server_writer.drain() |
| 292 | + assert (await asyncio.wait_for(outer_request, timeout=1)).stop_reason == "end_turn" |
| 293 | + await conn.close() |
| 294 | + |
| 295 | + |
147 | 296 | @pytest.mark.asyncio |
148 | 297 | async def test_on_connect_create_terminal_handle(server): |
149 | 298 | class _TerminalAgent(Agent): |
|
0 commit comments