diff --git a/src/agent_env/providers/env_providers/env_gateway_provider.py b/src/agent_env/providers/env_providers/env_gateway_provider.py index 234a17ddf..69e0e74c5 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -45,7 +45,6 @@ DB_WEB_PORT, PGWEB_SERVICE_NAME, ) -from agent_env.env.envs.playwright_mcp import PLAYWRIGHT_MCP_RECORDER_PORT from agent_env.providers.env_providers.constants import ( AGENT_ENV_WEBSITE_BACKEND_PORT, AGENT_ENV_WEBSITE_BACKEND_SUFFIX, @@ -109,7 +108,6 @@ class MCPServerConfig: image: str environment_name: str extra_env_vars: dict[str, str] | None = None - card_port: int | None = None # where its env card is served, when not on the MCP port (the website browser's recorder) @dataclass @@ -479,12 +477,10 @@ def create_docker_compose( f" - INTERNAL_MCP_PORT={gateway_port}", " - GATEWAY_TRAJECTORY_FILE=/var/log/agentenv/trajectory.jsonl", ] - # Build REST_PROXY_URLS: mcp-{name} for MCP servers + {name} for website backends - rest_proxy_urls = [] - for server in mcp_servers: - rest_proxy_urls.append( - f"mcp-{server.environment_name}=http://{server.environment_name}:{server.card_port or gateway_port}" - ) + # Build REST_PROXY_URLS: mcp-{name} for MCP servers + {name} for website backends. An MCP server's card and + # extensions are read on its MCP port, the website browser's included, as the containers deploy reads them. + rest_proxy_urls = [f"mcp-{server.environment_name}=http://{server.environment_name}:{gateway_port}" + for server in mcp_servers] if website_configs: website_urls = ",".join( f"{wc.environment_name}=http://{wc.environment_name}-{AGENT_ENV_WEBSITE_FRONTEND_SUFFIX}:80" @@ -777,7 +773,6 @@ async def _deploy_via_vm( browser_server = MCPServerConfig( image=website_browser_env.docker_image_artifact.image_name, environment_name=website_browser_env.environment_name, - card_port=PLAYWRIGHT_MCP_RECORDER_PORT, ) mcp_servers = list(mcp_servers) + [browser_server] mcp_server_images = list(mcp_server_images) + [website_browser_env.docker_image_artifact] diff --git a/tst/integration/env/gateway/gateway_test.py b/tst/integration/env/gateway/gateway_test.py index ae4020400..435a6136c 100644 --- a/tst/integration/env/gateway/gateway_test.py +++ b/tst/integration/env/gateway/gateway_test.py @@ -2,6 +2,7 @@ import json import logging import os +import shlex import shutil import subprocess import sys @@ -13,6 +14,7 @@ import httpx import pytest +import yaml from typing import NamedTuple from mcp import ClientSession from mcp.client.streamable_http import streamable_http_client @@ -39,8 +41,9 @@ ) from agentenv_protocol.computer.card import COMPUTER_RECORDING from agentenv_protocol.types import WELL_KNOWN_PATH -from agent_env.providers.env_providers.constants import GATEWAY_APP_DIR +from agent_env.providers.env_providers.constants import DOCKER_COMPOSE_PATH, GATEWAY_APP_DIR, GATEWAY_SERVICE_NAME from agent_env.providers.sandbox_providers.local_docker.provider import LocalSandboxProvider +from tst.integration.env.envs.playwright_mcp_local_test import PROCESSES from agent_env.env.snapshot_store import compute_env_fingerprint from agent_env.config import get_config from agent_env.task_step.task_steps.prompt_agent import PromptAgentTaskStep @@ -1432,11 +1435,15 @@ async def assert_website_tools_via_step(client, gateway_url, tools, tool_names): gateway_tool_names = [t["name"] for t in gateway_entry["tools"]] assert "list_website_urls" in gateway_tool_names - # The browser is a child of the deploy's card, read where deploys read it, and serves the same card on its MCP - # port, where its recorder fronts Playwright MCP; a recording through the gateway films the page an agent opens. + # The browser is a child of the deploy's card, which the gateway reads on its MCP port as it reads every MCP + # server's, its recorder fronting Playwright MCP there; a recording through the gateway films the page opened. browser = protocol_v1.find_child(result.environment_card, PLAYWRIGHT_MCP_ENVIRONMENT_NAME) assert browser is not None, result.environment_card.get("children_environments") sandbox = await LocalSandboxProvider().get_sandbox(result.sandbox_id) + compose = yaml.safe_load(await sandbox.exec_script(f"cat {DOCKER_COMPOSE_PATH}")) + proxied = dict(item.split("=", 1) for item in compose["services"][GATEWAY_SERVICE_NAME]["environment"]) + assert (f"mcp-{PLAYWRIGHT_MCP_ENVIRONMENT_NAME}=http://{PLAYWRIGHT_MCP_ENVIRONMENT_NAME}:18765" + in proxied["REST_PROXY_URLS"].split(",")), proxied["REST_PROXY_URLS"] card_url = f"http://127.0.0.1:18765{WELL_KNOWN_PATH}" read_card = f"import urllib.request; print(urllib.request.urlopen('{card_url}').read().decode())" served = await sandbox.exec_script(f"cd {GATEWAY_APP_DIR} && docker compose exec -T " @@ -1481,6 +1488,82 @@ async def assert_website_tools_via_step(client, gateway_url, tools, tool_names): await slack_website_env.close() +@pytest.mark.asyncio +@pytest.mark.integration +async def test_the_browsers_card_and_recording_through_the_gateway_survive_its_recorder_and_container_restarting( + slack_website_env): + """The gateway reaches the browser's card and recording on its MCP port, where the recorder fronts Playwright MCP. + The recorder killed comes back with the MCP server behind it untouched, the browser's container restarted comes + back whole, and after each the card answers through the gateway and a recording through it films the page.""" + browser_name = PLAYWRIGHT_MCP_ENVIRONMENT_NAME + result = await slack_website_env.deploy() + try: + sandbox = await LocalSandboxProvider().get_sandbox(result.sandbox_id) + browser = protocol_v1.find_child(result.environment_card, browser_name) + assert browser is not None, result.environment_card.get("children_environments") + start, end = (protocol_v1.find_extension_method(browser, COMPUTER_RECORDING, op)["endpoint"] + for op in ("start", "end")) + compose = f"cd {GATEWAY_APP_DIR} && docker compose" + # The recorder by its interpreter, which nothing else runs: the local sandbox rewrites /app in a script. + recorder_python = "/opt/recorder/bin/python" + + async def processes(pattern: str, signal: int | None = None) -> list[str]: + argv = [pattern, *([str(signal)] if signal is not None else [])] + out = await sandbox.exec_script(f"{compose} exec -T {browser_name} {recorder_python} -c " + f"{shlex.quote(PROCESSES)} {' '.join(map(shlex.quote, argv))}") + return out.strip().splitlines()[-1].split() if out.strip() else [] + + async def card(client: httpx.AsyncClient) -> dict: + deadline, last = time.monotonic() + 90, None + while time.monotonic() < deadline: + try: + response = await client.get(f"{result.gateway_url}/svc/mcp-{browser_name}{WELL_KNOWN_PATH}") + if response.status_code == 200: + return response.json() + last = f"HTTP {response.status_code}" + except httpx.HTTPError as e: + last = repr(e) + await asyncio.sleep(1) + raise AssertionError(f"the browser's card didn't answer through the gateway: {last}") + + async def record(client: httpx.AsyncClient, request_id: str) -> dict: + started = await client.post(f"{result.gateway_url}{start}", json={"request_id": request_id, "fps": 5}) + started.raise_for_status() + await _step_call_tool(client, result.gateway_url, "browser_navigate", + {"url": "http://slack-website-frontend:80"}) + await asyncio.sleep(3) + ended = await client.post(f"{result.gateway_url}{end}", + json={"recording_id": started.json()["recording_id"]}) + ended.raise_for_status() + return ended.json() + + async def started_at() -> str: + return (await sandbox.exec_script(f"{compose} ps -q {browser_name} | xargs docker inspect -f " + "'{{.State.StartedAt}}'")).strip() + + def filmed(manifest: dict) -> bool: + return bool(manifest["segments"]) and sum(segment["frames"] for segment in manifest["segments"]) > 0 + + async with httpx.AsyncClient(headers=result.control_headers, timeout=120) as client: + assert (await card(client))["name"] == browser_name + assert filmed(await record(client, "chaos:before")), "a recording before any restart filmed nothing" + + serving, recorder = await processes("@playwright/mcp"), await processes(recorder_python) + assert serving and recorder and await processes(recorder_python, 9) == recorder + assert (await card(client))["name"] == browser_name + assert (back := await processes(recorder_python)) and back != recorder + assert await processes("@playwright/mcp") == serving + assert filmed(await record(client, "chaos:recorder-killed")), "no film after the recorder came back" + + first_start = await started_at() + await sandbox.exec_script(f"{compose} restart {browser_name}") + assert (await card(client))["name"] == browser_name + assert await started_at() != first_start + assert filmed(await record(client, "chaos:container-restarted")), "no film after the container restarted" + finally: + await slack_website_env.close() + + @pytest.mark.asyncio @pytest.mark.integration async def test_gateway_with_multi_env_including_website(multi_env_with_website, environment_universe_artifact): diff --git a/tst/unit/env/envs/test_multi_env_snapshot_reload_name.py b/tst/unit/env/envs/test_multi_env_snapshot_reload_name.py index 26a5fd4fc..0a306b116 100644 --- a/tst/unit/env/envs/test_multi_env_snapshot_reload_name.py +++ b/tst/unit/env/envs/test_multi_env_snapshot_reload_name.py @@ -26,7 +26,7 @@ def _deployed_compose(browser="website-browser"): """The compose file a deploy with a website writes, its browser named ``browser``.""" return EnvironmentGatewayProvider().create_docker_compose( mcp_servers=[MCPServerConfig(image="mcp-slack", environment_name="slack"), - MCPServerConfig(image="browser:v1", environment_name=browser, card_port=18766)], + MCPServerConfig(image="browser:v1", environment_name=browser)], website_configs=[WebsiteConfig(backend_image="shop-be", frontend_image="shop-fe", environment_name="shop")], gateway_image="gateway:v38", gateway_port=AGENT_ENV_GATEWAY_MCP_PORT, state_provider=LocalPostgresStateProvider(service_db_config=ServiceDBConfig( @@ -81,6 +81,21 @@ async def test_the_reload_starts_the_servicedb_from_the_snapshot_and_keeps_every assert [call.args[0] for call in get.call_args_list] == ["db-id"] # the store it connects to; no image is read +@pytest.mark.asyncio +async def test_the_reload_keeps_the_browsers_card_where_an_older_deploy_read_it(): + """A deploy before the browser's card moved to its MCP port read it, and proxied its REST, on the recorder's 18766, + which the image still serves; a reload of such a deploy leaves that as it was.""" + newer = _deployed_compose("playwright-mcp") + older = newer.replace("mcp-playwright-mcp=http://playwright-mcp:18765", + "mcp-playwright-mcp=http://playwright-mcp:18766") + assert older != newer + env = _env(older) + + await _reload(env) + + assert "mcp-playwright-mcp=http://playwright-mcp:18766" in _written(env) + + @pytest.mark.asyncio async def test_the_reload_recreates_every_service_but_the_servicedb_the_browser_included(): env = _env(_deployed_compose("browseruse-mcp")) diff --git a/tst/unit/providers/env_providers/env_gateway_provider_test.py b/tst/unit/providers/env_providers/env_gateway_provider_test.py index ea8d6f10a..5d6142484 100644 --- a/tst/unit/providers/env_providers/env_gateway_provider_test.py +++ b/tst/unit/providers/env_providers/env_gateway_provider_test.py @@ -1082,14 +1082,14 @@ def test_compose_maps_extra_hosts_into_the_gateway(extra_hosts): assert mapped == ({GATEWAY_SERVICE_NAME: list(extra_hosts)} if extra_hosts else {}) -def test_compose_reads_a_servers_card_on_its_card_port_and_its_mcp_on_the_mcp_port(): - """The website browser serves its card from its recorder's port: the gateway's REST proxy, which it reads the card - through, points there, while its MCP connection and health check stay on the MCP port.""" +def test_compose_reads_every_servers_card_and_mcp_on_its_mcp_port_the_website_browsers_included(): + """The gateway's REST proxy, which it reads each server's card and extensions through, points at the server's MCP + port, where its MCP connection and health check are: the website browser's too, whose recorder fronts that port.""" from agent_env.env.envs.service_db import ServiceDBConfig compose = EnvironmentGatewayProvider().create_docker_compose( mcp_servers=[MCPServerConfig(image="mcp-slack", environment_name="slack"), - MCPServerConfig(image="browser", environment_name="website-browser", card_port=18766)], + MCPServerConfig(image="browser", environment_name="website-browser")], gateway_image="agent-gateway", gateway_port=18765, state_provider=LocalPostgresStateProvider(service_db_config=ServiceDBConfig()), @@ -1098,7 +1098,7 @@ def test_compose_reads_a_servers_card_on_its_card_port_and_its_mcp_on_the_mcp_po services = yaml.safe_load(compose)["services"] gateway = dict(item.split("=", 1) for item in services[GATEWAY_SERVICE_NAME]["environment"]) - assert gateway["REST_PROXY_URLS"] == "mcp-slack=http://slack:18765,mcp-website-browser=http://website-browser:18766" + assert gateway["REST_PROXY_URLS"] == "mcp-slack=http://slack:18765,mcp-website-browser=http://website-browser:18765" assert (gateway["INTERNAL_MCP_SERVERS"], gateway["INTERNAL_MCP_PORT"]) == ("slack,website-browser", "18765") assert "18765" in services["website-browser"]["healthcheck"]["test"][1] @@ -1382,7 +1382,7 @@ def _fake_env_get(env_id, *a, **k): assert set(names) == {"slack", "shop", browser_name} assert compose.call_args.kwargs["mcp_server_name"] == "crm" browser = next(s for s in compose.call_args.kwargs["mcp_servers"] if s.environment_name == browser_name) - assert browser.card_port == 18766 # its card is the recorder's + assert browser == MCPServerConfig(image=browser.image, environment_name=browser_name) # like any MCP server @pytest.mark.asyncio @@ -1693,7 +1693,7 @@ def _render(mcp_servers, website_configs=None): @pytest.mark.parametrize("browser", ["playwright-mcp", "browseruse-mcp"]) def test_compose_takes_a_browser_of_any_name_and_refuses_an_env_named_like_it(browser): - browser_server = MCPServerConfig(image="browser", environment_name=browser, card_port=18766) + browser_server = MCPServerConfig(image="browser", environment_name=browser) shop = WebsiteConfig(backend_image="shop-be", frontend_image="shop-fe", environment_name="slack") services = yaml.safe_load(_render([MCPServerConfig(image="mcp-slack", environment_name="slack"), browser_server],