|
| 1 | +import os |
| 2 | +import subprocess |
1 | 3 | import sys |
| 4 | +import tempfile |
2 | 5 | import textwrap |
3 | 6 |
|
4 | 7 | import pytest |
@@ -39,3 +42,59 @@ async def test_spawn_stdio_transport_custom_limit_handles_large_line() -> None: |
39 | 42 | ) as (reader, _writer, _process): |
40 | 43 | line = await reader.readline() |
41 | 44 | assert len(line) == LARGE_LINE_SIZE + 1 |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_run_agent_stdio_buffer_limit() -> None: |
| 49 | + """Test that run_agent with different buffer limits can handle appropriately sized messages.""" |
| 50 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 51 | + # Test 1: Small buffer (1KB) fails with large message (70KB) |
| 52 | + small_agent = os.path.join(tmpdir, "small_agent.py") |
| 53 | + with open(small_agent, "w") as f: |
| 54 | + f.write(""" |
| 55 | +import asyncio |
| 56 | +from acp.core import run_agent |
| 57 | +from acp.interfaces import Agent |
| 58 | +
|
| 59 | +class TestAgent(Agent): |
| 60 | + async def list_capabilities(self): |
| 61 | + return {"capabilities": {}} |
| 62 | +
|
| 63 | +asyncio.run(run_agent(TestAgent(), stdio_buffer_limit_bytes=1024)) |
| 64 | +""") |
| 65 | + |
| 66 | + # Send a 70KB message - should fail with 1KB buffer |
| 67 | + large_msg = '{"jsonrpc":"2.0","method":"test","params":{"data":"' + "X" * LARGE_LINE_SIZE + '"}}\n' |
| 68 | + result = subprocess.run( # noqa: S603 |
| 69 | + [sys.executable, small_agent], input=large_msg, capture_output=True, text=True, timeout=2 |
| 70 | + ) |
| 71 | + |
| 72 | + # Should have errors in stderr about the buffer limit |
| 73 | + assert "Error" in result.stderr or result.returncode != 0, ( |
| 74 | + f"Expected error with small buffer, got: {result.stderr}" |
| 75 | + ) |
| 76 | + |
| 77 | + # Test 2: Large buffer (200KB) succeeds with large message (70KB) |
| 78 | + large_agent = os.path.join(tmpdir, "large_agent.py") |
| 79 | + with open(large_agent, "w") as f: |
| 80 | + f.write(f""" |
| 81 | +import asyncio |
| 82 | +from acp.core import run_agent |
| 83 | +from acp.interfaces import Agent |
| 84 | +
|
| 85 | +class TestAgent(Agent): |
| 86 | + async def list_capabilities(self): |
| 87 | + return {{"capabilities": {{}}}} |
| 88 | +
|
| 89 | +asyncio.run(run_agent(TestAgent(), stdio_buffer_limit_bytes={LARGE_LINE_SIZE * 3})) |
| 90 | +""") |
| 91 | + |
| 92 | + # Same message, but with a buffer 3x the size - should handle it |
| 93 | + result = subprocess.run( # noqa: S603 |
| 94 | + [sys.executable, large_agent], input=large_msg, capture_output=True, text=True, timeout=2 |
| 95 | + ) |
| 96 | + |
| 97 | + # With a large enough buffer, the agent should at least start successfully |
| 98 | + # (it may have other errors from invalid JSON-RPC, but not buffer overrun) |
| 99 | + if "LimitOverrunError" in result.stderr or "buffer" in result.stderr.lower(): |
| 100 | + pytest.fail(f"Large buffer still hit limit error: {result.stderr}") |
0 commit comments