|
1 | 1 | import asyncio |
| 2 | +import asyncio.subprocess as aio_subprocess |
2 | 3 | import contextlib |
| 4 | +import json |
3 | 5 | import signal |
4 | 6 | import sys |
| 7 | +import textwrap |
5 | 8 |
|
6 | 9 | import pytest |
7 | 10 |
|
8 | 11 | from acp.transports import spawn_stdio_transport |
9 | 12 |
|
10 | 13 |
|
| 14 | +@pytest.mark.asyncio |
| 15 | +@pytest.mark.parametrize(("runner", "version"), [("v1", 1), ("v2", 2), ("dual", 1), ("dual", 2)]) |
| 16 | +async def test_agent_shutdown_after_stdio_eof(runner: str, version: int) -> None: |
| 17 | + script = textwrap.dedent(""" |
| 18 | + import asyncio |
| 19 | + import sys |
| 20 | +
|
| 21 | + from acp import run_agent, schema |
| 22 | + from acp.experimental import v2 |
| 23 | + from acp.experimental.negotiation import AgentProtocolRouter |
| 24 | +
|
| 25 | + class V1Agent: |
| 26 | + async def initialize(self, protocol_version, **kwargs): |
| 27 | + return schema.InitializeResponse(protocol_version=protocol_version) |
| 28 | +
|
| 29 | + class V2Agent: |
| 30 | + async def initialize(self, protocol_version, **kwargs): |
| 31 | + return v2.schema.InitializeResponse( |
| 32 | + protocol_version=protocol_version, |
| 33 | + info=v2.schema.Implementation(name="test-agent", version="0"), |
| 34 | + ) |
| 35 | +
|
| 36 | + async def main(): |
| 37 | + runner = sys.argv[1] |
| 38 | + if runner == "v1": |
| 39 | + await run_agent(V1Agent()) |
| 40 | + elif runner == "v2": |
| 41 | + await v2.run_agent(V2Agent()) |
| 42 | + else: |
| 43 | + await AgentProtocolRouter(v1=lambda _: V1Agent(), v2=lambda _: V2Agent()).run() |
| 44 | +
|
| 45 | + asyncio.run(main()) |
| 46 | + """) |
| 47 | + params = {"protocolVersion": version} |
| 48 | + if version == 2: |
| 49 | + params = {"protocolVersion": version, "info": {"name": "test-client", "version": "0"}} |
| 50 | + initialize = {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": params} |
| 51 | + |
| 52 | + async with spawn_stdio_transport(sys.executable, "-W", "error::ResourceWarning", "-c", script, runner) as ( |
| 53 | + reader, |
| 54 | + writer, |
| 55 | + process, |
| 56 | + ): |
| 57 | + writer.write(json.dumps(initialize).encode() + b"\n") |
| 58 | + await writer.drain() |
| 59 | + response = json.loads(await asyncio.wait_for(reader.readline(), timeout=5)) |
| 60 | + assert response["id"] == 1 |
| 61 | + assert response["result"]["protocolVersion"] == version |
| 62 | + |
| 63 | + assert process.stderr is not None |
| 64 | + stderr = await asyncio.wait_for(process.stderr.read(), timeout=5) |
| 65 | + assert process.returncode == 0, stderr.decode() |
| 66 | + assert stderr == b"", stderr.decode() |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_agent_shutdown_does_not_hang_on_full_unread_stdout_pipe() -> None: |
| 71 | + if sys.platform == "win32": |
| 72 | + pytest.skip("POSIX pipe backpressure test") |
| 73 | + script = textwrap.dedent(""" |
| 74 | + import asyncio |
| 75 | + from acp import run_agent, schema |
| 76 | +
|
| 77 | + class LargeResponseAgent: |
| 78 | + async def initialize(self, protocol_version, **kwargs): |
| 79 | + return schema.InitializeResponse( |
| 80 | + protocol_version=protocol_version, |
| 81 | + agent_info=schema.Implementation(name="agent", version="x" * (512 * 1024)), |
| 82 | + ) |
| 83 | +
|
| 84 | + asyncio.run(run_agent(LargeResponseAgent())) |
| 85 | + """) |
| 86 | + initialize = {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": 1}} |
| 87 | + process = await asyncio.create_subprocess_exec( |
| 88 | + sys.executable, |
| 89 | + "-W", |
| 90 | + "error::ResourceWarning", |
| 91 | + "-c", |
| 92 | + script, |
| 93 | + stdin=aio_subprocess.PIPE, |
| 94 | + stdout=aio_subprocess.PIPE, |
| 95 | + stderr=aio_subprocess.PIPE, |
| 96 | + ) |
| 97 | + assert process.stdin is not None |
| 98 | + assert process.stdout is not None |
| 99 | + assert process.stderr is not None |
| 100 | + try: |
| 101 | + process.stdin.write(json.dumps(initialize).encode() + b"\n") |
| 102 | + await process.stdin.drain() |
| 103 | + # Let the agent fill the stdout pipe without draining it, then close stdin. |
| 104 | + await asyncio.sleep(0.1) |
| 105 | + process.stdin.close() |
| 106 | + await process.stdin.wait_closed() |
| 107 | + returncode = await asyncio.wait_for(process.wait(), timeout=5) |
| 108 | + stderr = await asyncio.wait_for(process.stderr.read(), timeout=5) |
| 109 | + assert returncode == 0, stderr.decode() |
| 110 | + assert stderr == b"", stderr.decode() |
| 111 | + finally: |
| 112 | + if process.returncode is None: |
| 113 | + process.kill() |
| 114 | + await process.wait() |
| 115 | + |
| 116 | + |
11 | 117 | @pytest.mark.asyncio |
12 | 118 | @pytest.mark.parametrize("ignore_terminate", [False, True]) |
13 | 119 | async def test_shutdown_with_unread_stdin(ignore_terminate: bool) -> None: |
|
0 commit comments