diff --git a/docs/web-transport.md b/docs/web-transport.md index ce77eba4..8798c470 100644 --- a/docs/web-transport.md +++ b/docs/web-transport.md @@ -56,6 +56,10 @@ header, then opens the connection-scoped SSE stream. When a new `sessionId` appears it opens that session-scoped stream too. A single SSE attempt is made per stream; reconnect/retry is the caller's responsibility (v1 of the RFD). +When a server returns an inline JSON response in a `200 OK` POST body, invalid +JSON raises `json.JSONDecodeError` in the sending call. The request fails promptly +and the connection remains available for subsequent requests. + ### Loading an existing session Both HTTP and WebSocket support `load_session()` when the agent advertises diff --git a/src/acp/http/client.py b/src/acp/http/client.py index 5087bcf9..b7366959 100644 --- a/src/acp/http/client.py +++ b/src/acp/http/client.py @@ -164,8 +164,7 @@ async def _send_post(self, message: dict[str, Any]) -> None: raise AcpHttpStatusError(response.status_code, f"POST {method} failed") # Some servers may answer initialize-like 200 bodies; for 200 with a body enqueue it. if response.status_code == 200 and response.content: - with contextlib.suppress(Exception): - self._handle_incoming(response.json()) + self._handle_incoming(response.json()) def _open_stream(self, *, session_id: str | None) -> None: if self._closed: diff --git a/tests/http/test_http_client.py b/tests/http/test_http_client.py index 38ddb78b..b97bd46e 100644 --- a/tests/http/test_http_client.py +++ b/tests/http/test_http_client.py @@ -10,6 +10,7 @@ import pytest from acp._sse import serialize_sse_event +from acp.connection import Connection from acp.http.client import AcpHttpStatusError, create_http_stream from acp.http.protocol import CONNECTION_ID_HEADER, CONTENT_TYPE_JSON, SESSION_ID_HEADER @@ -258,6 +259,34 @@ def handler(request: httpx2.Request) -> httpx2.Response: await client.aclose() +@pytest.mark.asyncio +@pytest.mark.parametrize("payload", [b"not an ACP response", b"{invalid"]) +async def test_invalid_post_json_rejects_request_without_losing_connection(payload: bytes) -> None: + server = FakeServer() + + def handler(request: httpx2.Request) -> httpx2.Response: + if request.method == "POST": + message = json.loads(request.content) + if message.get("method") == "broken": + return httpx2.Response(200, content=payload) + if message.get("method") == "after": + return httpx2.Response(200, json={"jsonrpc": "2.0", "id": message["id"], "result": "ok"}) + return server.handler(request) + + async def noop(method: str, params: Any, is_notification: bool) -> None: + pass + + async with httpx2.AsyncClient(transport=httpx2.MockTransport(handler)) as client: + transport = create_http_stream("http://testserver/acp", client=client) + await transport.send({"jsonrpc": "2.0", "id": "init", "method": "initialize", "params": {}}) + await asyncio.wait_for(transport.receive(), timeout=1) + + async with Connection(noop, transport) as connection: + with pytest.raises(json.JSONDecodeError): + await asyncio.wait_for(connection.send_request("broken", {}), timeout=1) + assert await asyncio.wait_for(connection.send_request("after", {}), timeout=1) == "ok" + + @pytest.mark.asyncio @pytest.mark.parametrize("result", [{}, None]) async def test_load_opens_session_stream_from_request_id(result: Any) -> None: