From 76fc3d91663ece6ba73160dab16bf5070a233d43 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sat, 28 Nov 2026 13:25:53 -0800 Subject: [PATCH 1/2] feat(protocol): a website extra brings the WebSocket library a site's server needs WebsiteEnvironment.serve() runs uvicorn, and uvicorn without a WebSocket library serves a WebSocket upgrade as a plain GET, which the site answers 404; the page sees the socket close with 1006. The new website extra installs uvicorn with websockets, serve()'s ImportError names it, and the protocol README says to install it in a site's image. A gateway test deploys a site whose mounted app answers a WebSocket, built with the extra, and checks its opening messages and a ping through /website//. Co-Authored-By: Claude Opus 5.5 --- packages/agentenv-protocol/README.md | 7 +++ packages/agentenv-protocol/pyproject.toml | 6 +++ .../agentenv_protocol/website/environment.py | 6 ++- tst/data/socket_website/Dockerfile | 11 +++++ tst/data/socket_website/server.py | 33 ++++++++++++++ tst/integration/env/gateway/gateway_test.py | 45 +++++++++++++++++++ uv.lock | 8 +++- 7 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 tst/data/socket_website/Dockerfile create mode 100644 tst/data/socket_website/server.py diff --git a/packages/agentenv-protocol/README.md b/packages/agentenv-protocol/README.md index c9db29a08..c27b328a2 100644 --- a/packages/agentenv-protocol/README.md +++ b/packages/agentenv-protocol/README.md @@ -502,6 +502,13 @@ async with ToolSession(mcp_url=mcp_url, role="alice@example.com") as session: - **Tools:** `list_tools()` returns the tools the role may use as `EnvironmentTool`, the type a card's `capabilities.tools` holds. Each `inputSchema` is the JSON Schema a model API takes as the tool's parameters. +## Running a website + +`WebsiteEnvironment.serve()` runs a site with uvicorn on `PORT`. Install the `website` extra in the site's image +(`pip install 'agentenv-framework-protocol[website]'`: uvicorn and websockets): a WebSocket a page opens, which a mounted +`app` answers, needs a WebSocket library, and uvicorn without one serves the upgrade as a plain request, which the site +answers 404. + ## Website readiness `agentenv_protocol.website` is the one definition of whether a website env is healthy. A site's card advertises diff --git a/packages/agentenv-protocol/pyproject.toml b/packages/agentenv-protocol/pyproject.toml index 4cf305d97..ddae5d6a8 100644 --- a/packages/agentenv-protocol/pyproject.toml +++ b/packages/agentenv-protocol/pyproject.toml @@ -53,6 +53,12 @@ computer = [ "uvicorn>=0.30", "websockets>=13", ] +# A website's own server: WebsiteEnvironment.serve() runs it with uvicorn, which needs a WebSocket library to answer a +# page's WebSocket at all; without one it serves the upgrade as a plain request, which the site answers 404. +website = [ + "uvicorn>=0.30", + "websockets>=13", +] # A macOS driver on MacOSDriver: ScreenCaptureKit captures the display and Quartz events send its input, through PyObjC. macos = [ "pyobjc-framework-Quartz>=11; sys_platform == 'darwin'", diff --git a/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py b/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py index 2c8cf0f2c..c63d56bdc 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/website/environment.py @@ -260,11 +260,13 @@ def mount(self, app: Any) -> Any: raise TypeError(f"{type(self).__name__} builds its own app with create_app(); set app = ... to serve more") def serve(self) -> None: - """Serve the site on ``PORT`` (8000 by default) with uvicorn, of the ``agent`` or ``app`` extra.""" + """Serve the site on ``PORT`` (8000 by default) with uvicorn, of the ``website`` extra, which brings the + WebSocket library uvicorn needs to answer a page's WebSocket rather than serve the upgrade as a plain request.""" try: import uvicorn except ImportError as e: - raise ImportError("WebsiteEnvironment.serve needs uvicorn: pip install 'uvicorn>=0.30'") from e + raise ImportError("WebsiteEnvironment.serve needs uvicorn: pip install " + "'agentenv-framework-protocol[website]'") from e uvicorn.run(self.asgi or self.create_app(), host=os.environ.get("HOST", "0.0.0.0"), port=int(os.environ.get("PORT", "8000"))) diff --git a/tst/data/socket_website/Dockerfile b/tst/data/socket_website/Dockerfile new file mode 100644 index 000000000..86a3d3f90 --- /dev/null +++ b/tst/data/socket_website/Dockerfile @@ -0,0 +1,11 @@ +FROM mirror.gcr.io/library/python:3.12-slim@sha256:423ed6ab25b1921a477529254bfeeabf5855151dc2c3141699a1bfc852199fbf +RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/* +WORKDIR /app +COPY agentenv-protocol /tmp/agentenv-protocol +RUN pip install --no-cache-dir "/tmp/agentenv-protocol[website]" && rm -rf /tmp/agentenv-protocol +COPY server.py /app/server.py +ENV PORT=8000 +EXPOSE 8000 +RUN useradd --create-home --uid 10001 appuser +USER appuser +CMD ["python", "server.py"] diff --git a/tst/data/socket_website/server.py b/tst/data/socket_website/server.py new file mode 100644 index 000000000..240a91795 --- /dev/null +++ b/tst/data/socket_website/server.py @@ -0,0 +1,33 @@ +"""A website on WebsiteEnvironment whose mounted app answers a WebSocket, for integration-testing that a site installed +with the protocol's website extra serves a page's WebSocket through the gateway's site route: it sends its opening +messages on connect and answers each ping with a pong.""" +from starlette.applications import Starlette +from starlette.routing import WebSocketRoute +from starlette.websockets import WebSocket, WebSocketDisconnect + +from agentenv_protocol import environment_card +from agentenv_protocol.website import WebsiteEnvironment, website + +OPENING = ["hello", "unread:2"] + + +async def socket(websocket: WebSocket) -> None: + await websocket.accept() + for message in OPENING: + await websocket.send_text(message) + try: + while True: + if await websocket.receive_text() == "ping": + await websocket.send_text("pong") + except WebSocketDisconnect: + pass + + +@environment_card(name="sockets") +@website(builtin_routes=False) +class Sockets(WebsiteEnvironment): + app = Starlette(routes=[WebSocketRoute("/socket", socket)]) + + +if __name__ == "__main__": + Sockets().serve() diff --git a/tst/integration/env/gateway/gateway_test.py b/tst/integration/env/gateway/gateway_test.py index 1132db47d..21959a46a 100644 --- a/tst/integration/env/gateway/gateway_test.py +++ b/tst/integration/env/gateway/gateway_test.py @@ -19,6 +19,7 @@ from mcp import ClientSession from mcp.client.streamable_http import streamable_http_client from mcp.shared._httpx_utils import create_mcp_http_client +from websockets.asyncio.client import connect as connect_websocket # Each test deploys a real GatewayEnv + MCP servers on sandbox VMs # (~5-15 min per test). Mark the entire module as slow. @@ -387,6 +388,30 @@ def agentenv_website_env() -> WebsiteEnv: return env +@pytest.fixture(scope="module") +def socket_website_env() -> WebsiteEnv: + """A WebsiteEnvironment site whose mounted app answers a WebSocket, its image installing this checkout's protocol + package with the website extra, as a site's image installs the released one.""" + package = REPO_ROOT / "packages" / "agentenv-protocol" + server_dir = TST_DATA_DIR / "socket_website" + with tempfile.TemporaryDirectory() as build_dir: + bd = Path(build_dir) + shutil.copytree(package, bd / "agentenv-protocol", + ignore=shutil.ignore_patterns("__pycache__", "tests", "examples", ".venv", ".pytest_cache")) + shutil.copy(server_dir / "server.py", bd / "server.py") + shutil.copy(server_dir / "Dockerfile", bd / "Dockerfile") + result = subprocess.run( + ["docker", "build", "--platform", "linux/amd64", "-t", "agentenv-socket-website", str(bd)], + capture_output=True, text=True, + ) + if result.returncode != 0: + raise RuntimeError(f"socket website build failed: {result.stderr}") + image = DockerImageArtifact.put(id="integ-test-socket-website-image", description="A website answering a WebSocket", + image_name="agentenv-socket-website") + return WebsiteEnv.put(id="integ-test-socket-website", backend_docker_image_artifact=image, + environment_name="sockets") + + @pytest.fixture(params=[ pytest.param("scale", id="scale"), pytest.param( @@ -2576,6 +2601,26 @@ async def test_gateway_with_website_env_v1(agentenv_website_env, monkeypatch, tm await env.close() +@pytest.mark.asyncio +@pytest.mark.integration +async def test_gateway_relays_a_website_environments_websocket(socket_website_env): + """A WebSocket a WebsiteEnvironment's mounted app answers reaches a client through the gateway's site route: the + site's opening messages arrive on connect and a ping is answered. Without a WebSocket library in the site's image, + uvicorn serves the upgrade as a plain request and the site answers 404.""" + env = Env.get(socket_website_env.id, version=socket_website_env.version) + try: + result = await env.deploy() + url = "ws" + result.gateway_url.removeprefix("http") + "/website/sockets/socket" + async with connect_websocket(url, open_timeout=60) as websocket: + opening = [await asyncio.wait_for(websocket.recv(), 30) for _ in range(2)] + await websocket.send("ping") + answer = await asyncio.wait_for(websocket.recv(), 30) + finally: + await env.close() + assert opening == ["hello", "unread:2"] + assert answer == "pong" + + async def _load_shell_named_artifact(env, environment_name: str, monkeypatch, tmp_path) -> None: """Load a seed whose file name runs ``touch pwned`` if the shell parses it, from ``tmp_path`` so a run leaves it there.""" artifact = EnvironmentArtifact.put( diff --git a/uv.lock b/uv.lock index 1ff42c91d..091b3efa3 100644 --- a/uv.lock +++ b/uv.lock @@ -339,6 +339,10 @@ macos = [ { name = "pyobjc-framework-quartz", marker = "sys_platform == 'darwin'" }, { name = "pyobjc-framework-screencapturekit", marker = "sys_platform == 'darwin'" }, ] +website = [ + { name = "uvicorn" }, + { name = "websockets" }, +] windows = [ { name = "mss" }, ] @@ -379,13 +383,15 @@ requires-dist = [ { name = "uvicorn", marker = "extra == 'agent'", specifier = ">=0.30" }, { name = "uvicorn", marker = "extra == 'app'", specifier = ">=0.30" }, { name = "uvicorn", marker = "extra == 'computer'", specifier = ">=0.30" }, + { name = "uvicorn", marker = "extra == 'website'", specifier = ">=0.30" }, { name = "websockets", marker = "extra == 'app'", specifier = ">=13" }, { name = "websockets", marker = "extra == 'computer'", specifier = ">=13" }, { name = "websockets", marker = "extra == 'dev'", specifier = ">=13" }, + { name = "websockets", marker = "extra == 'website'", specifier = ">=13" }, { name = "zstandard", marker = "extra == 'app'", specifier = ">=0.23" }, { name = "zstandard", marker = "extra == 'dev'", specifier = ">=0.23" }, ] -provides-extras = ["agent", "app", "computer", "macos", "x11", "windows", "dev"] +provides-extras = ["agent", "app", "computer", "website", "macos", "x11", "windows", "dev"] [[package]] name = "aiofile" From af05d307d206dc691e2e9411c06b972229d076b4 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sat, 28 Nov 2026 13:29:43 -0800 Subject: [PATCH 2/2] test(gateway): name the WebSocket test's timeouts Co-Authored-By: Claude Opus 5.5 --- tst/integration/env/gateway/gateway_test.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tst/integration/env/gateway/gateway_test.py b/tst/integration/env/gateway/gateway_test.py index 21959a46a..adf0d3433 100644 --- a/tst/integration/env/gateway/gateway_test.py +++ b/tst/integration/env/gateway/gateway_test.py @@ -2601,6 +2601,10 @@ async def test_gateway_with_website_env_v1(agentenv_website_env, monkeypatch, tm await env.close() +_SOCKET_OPEN_TIMEOUT_S = 60 +_SOCKET_MESSAGE_TIMEOUT_S = 30 + + @pytest.mark.asyncio @pytest.mark.integration async def test_gateway_relays_a_website_environments_websocket(socket_website_env): @@ -2611,10 +2615,10 @@ async def test_gateway_relays_a_website_environments_websocket(socket_website_en try: result = await env.deploy() url = "ws" + result.gateway_url.removeprefix("http") + "/website/sockets/socket" - async with connect_websocket(url, open_timeout=60) as websocket: - opening = [await asyncio.wait_for(websocket.recv(), 30) for _ in range(2)] + async with connect_websocket(url, open_timeout=_SOCKET_OPEN_TIMEOUT_S) as websocket: + opening = [await asyncio.wait_for(websocket.recv(), _SOCKET_MESSAGE_TIMEOUT_S) for _ in range(2)] await websocket.send("ping") - answer = await asyncio.wait_for(websocket.recv(), 30) + answer = await asyncio.wait_for(websocket.recv(), _SOCKET_MESSAGE_TIMEOUT_S) finally: await env.close() assert opening == ["hello", "unread:2"]