Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/web-transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/acp/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 29 additions & 0 deletions tests/http/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -258,6 +259,34 @@ def handler(request: httpx2.Request) -> httpx2.Response:
await client.aclose()


@pytest.mark.asyncio
@pytest.mark.parametrize("payload", [b"<html>not an ACP response</html>", 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:
Expand Down