diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index 64fdc3728..821881b21 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -38,7 +38,7 @@ import urllib.request from dataclasses import dataclass, field from pathlib import Path -from typing import Optional +from typing import Awaitable, Optional from urllib.parse import urlsplit, urlunsplit HOSTS_BEGIN = "# agentenv sites" @@ -64,8 +64,8 @@ # log the failure shows. LISTEN_PROBE_S, LISTEN_POLL_S, LOG_TAIL_CHARS = 1.0, 0.2, 2000 CHECK_TIMEOUT_S = 15.0 -#: The checks after each site's: Python through ``SSL_CERT_FILE``, Node and curl. -RUNTIME_CHECKS = 3 +#: The checks after each site's: Python through ``SSL_CERT_FILE``, Node and curl, and the forward port's. +RUNTIME_CHECKS = 4 #: What a JDK's store is opened with unless it says otherwise, how deep a JDK's store is looked for under its folders, #: the folders not looked in, and how long a ``keytool`` call may take. JAVA_STORE_PASSWORD = "changeit" @@ -371,61 +371,62 @@ def __init__(self, config: Config, tls: Optional[ssl.SSLContext] = None): self.upstream_tls = ssl.create_default_context() async def handle(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str) -> None: - await self._serving(reader, writer, scheme, self._requests) + await self._serving(writer, scheme, self._requests(reader, writer, scheme, _peer(writer), forward=False)) async def handle_forward(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: """The forward-proxy port's connection: a ``CONNECT`` to a site's name tunnels to the site as the listeners - serve it, over TLS with its certificate for a name served over HTTPS; a request naming a site's URL is served - as the HTTP listener serves it; any other host, loopback among them, is refused at once and logged.""" - await self._serving(reader, writer, "proxy", self._proxied) - - async def _requests(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str, - peer: str) -> None: - while await self.exchange(reader, writer, scheme, peer): + serve it, over TLS with its certificate for a name served over HTTPS, and any other request is served as the + HTTP listener serves it; ``exchange`` refuses one naming any other host, loopback among them, at once and logs + it.""" + await self._serving(writer, "proxy", self._proxied(reader, writer, _peer(writer))) + + async def _requests(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str, peer: str, *, + forward: bool) -> None: + while await self.exchange(reader, writer, scheme, peer, forward=forward): pass - async def _proxied(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, _: str, peer: str) -> None: + async def _proxied(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, peer: str) -> None: while (head := await read_head(reader)) is not None: - start, headers = parse_head(head) - method, _, target = start.partition(" ") - target = target.rpartition(" ")[0] + method, _, rest = parse_head(head)[0].partition(" ") if method == "CONNECT": - host = split_host(target) - if (site := self.config.site_for(host)) is None: - self._refuse(writer, method, target) - return - writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") - await writer.drain() - tls = site.tls(host.lower()) - if tls: - if self.tls is None: - raise BadRequest(f"{host} is served over HTTPS, and this proxy has no certificate for it") - await writer.start_tls(self.tls) - await self._requests(reader, writer, "https" if tls else "http", peer) + await self._tunnel(reader, writer, peer, rest.rpartition(" ")[0]) return - named = urlsplit(target).netloc if "://" in target else (header(headers, "host") or "") - if self.config.site_for(split_host(named)) is None: - self._refuse(writer, method, named or target) - return - if not await self.exchange(reader, writer, "http", peer, head=head): + if not await self.exchange(reader, writer, "http", peer, head=head, forward=True): return + async def _tunnel(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, peer: str, + authority: str) -> None: + """A ``CONNECT`` to ``authority``: the site it names, whose certificate the tunnel's TLS needs before any + request in it, then each request in it, as the port serves them.""" + host = split_host(authority) + if (site := self.config.site_for(host)) is None: + self._refuse(writer, "CONNECT", authority) + return + writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") + await writer.drain() + tls = site.tls(host.lower()) + if tls: + if self.tls is None: + raise BadRequest(f"{host} is served over HTTPS, and this proxy has no certificate for it") + await writer.start_tls(self.tls) + await self._requests(reader, writer, "https" if tls else "http", peer, forward=True) + @staticmethod def _refuse(writer: asyncio.StreamWriter, method: str, host: str) -> None: print(f"site proxy: refused {method} {host}: no site of this computer", file=sys.stderr, flush=True) writer.write(simple_response("403 Forbidden", f"{host} is no site of this computer, and the browser reaches " "its sites alone\n", FAILED)) - async def _serving(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str, run) -> None: - peer = (writer.get_extra_info("peername") or (LOOPBACK,))[0] + @staticmethod + async def _serving(writer: asyncio.StreamWriter, label: str, serving: Awaitable[None]) -> None: try: - await run(reader, writer, scheme, peer) + await serving except BadRequest as e: writer.write(simple_response("400 Bad Request", f"{e}\n")) except (ConnectionError, asyncio.IncompleteReadError, ssl.SSLError, OSError): pass except Exception as e: - print(f"site proxy: a {scheme} connection failed: {type(e).__name__}: {e}", file=sys.stderr, flush=True) + print(f"site proxy: a {label} connection failed: {type(e).__name__}: {e}", file=sys.stderr, flush=True) finally: try: await writer.drain() @@ -435,8 +436,9 @@ async def _serving(self, reader: asyncio.StreamReader, writer: asyncio.StreamWri pass async def exchange(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str, - peer: str, head: Optional[bytes] = None) -> bool: - """One request, read unless its ``head`` is given, and its answer; whether the connection takes another.""" + peer: str, head: Optional[bytes] = None, *, forward: bool = False) -> bool: + """One request, read unless its ``head`` is given, and its answer; whether the connection takes another. One + naming no site gets 421, or on the forward port (``forward``) the port's refusal, logged.""" head = head or await read_head(reader) if head is None: return False @@ -448,8 +450,11 @@ async def exchange(self, reader: asyncio.StreamReader, writer: asyncio.StreamWri host = (urlsplit(target).netloc if "://" in target else "") or header(headers, "host") or "" site = self.config.site_for(split_host(host)) if host else None if site is None: - writer.write(simple_response("421 Misdirected Request", f"{host or 'no host'} is no site of this " - "computer\n", FAILED)) + if forward: + self._refuse(writer, method, host or target) + else: + writer.write(simple_response("421 Misdirected Request", f"{host or 'no host'} is no site of this " + "computer\n", FAILED)) return False if scheme == "http" and site.tls(split_host(host).lower()): parts = urlsplit(target) @@ -629,6 +634,10 @@ def failed_answer(query: bytes, question: tuple[str, int, int]) -> bytes: return struct.pack("!HHHHHH", ident, 0x8082 | (flags & 0x0100), 1, 0, 0, 0) + query[12:question[2]] +def _peer(writer: asyncio.StreamWriter) -> str: + return (writer.get_extra_info("peername") or (LOOPBACK,))[0] + + def tls_context(config: Config) -> Optional[ssl.SSLContext]: """The HTTPS side's context: each site's certificate, picked by the name the browser asks for.""" contexts = {} @@ -998,7 +1007,8 @@ def install(config_path: str, paths: Paths = Paths()) -> dict: serve_path.write_text(json.dumps(serving)) start(str(serve_path), given.get("log")) checks = checks_as(user, str(serve_path)) - return {"trust": trust, "names": names, "checks": checks["sites"], "runtimes": checks["runtimes"]} + return {"trust": trust, "names": names, "checks": checks["sites"], "runtimes": checks["runtimes"], + **({"forward_port": checks["forward_port"]} if "forward_port" in checks else {})} def start(serve_path: str, log: Optional[str]) -> None: @@ -1051,28 +1061,36 @@ def checks_as(user: Optional[str], serve_path: str) -> dict: def check(config: Config) -> dict: """``sites``: each site's first name loaded, over HTTPS but for a ``.localhost`` name, through the system's - resolver and trust store: its status, or why it didn't load, which an answer the proxy made itself (it marks them - ``FAILED``), such as its 502 for a gateway it couldn't reach, is too. ``runtimes``: the first of them over HTTPS - loaded through each other runtime's own trust (``check_runtimes``).""" + resolver and trust store (``_loaded``). ``runtimes``: the first of them over HTTPS loaded through each other + runtime's own trust (``check_runtimes``). ``forward_port``, with one: the first of them over HTTPS, else the first, + loaded through it as a browser alone loads it.""" opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) results = {} for site in config.sites: name = next((n for n in site.domains if not n.startswith("*.")), None) \ or "agentenv-check." + site.domains[0].removeprefix("*.") - url = f"{'https' if site.tls(name) else 'http'}://{name}/" - try: - with opener.open(url, timeout=CHECK_TIMEOUT_S) as response: - results[name] = {"url": url, "status": response.status} - except urllib.error.HTTPError as e: - if e.headers is not None and e.headers.get(FAILED[0][0]) == FAILED[0][1]: - body = e.read().decode(errors="replace").strip() - results[name] = {"url": url, "error": f"{e.code} {e.reason}: {body}"} - else: - results[name] = {"url": url, "status": e.code} - except (urllib.error.URLError, OSError, ssl.SSLError) as e: - results[name] = {"url": url, "error": str(getattr(e, "reason", e))} + results[name] = _loaded(opener, f"{'https' if site.tls(name) else 'http'}://{name}/") secure = next((result["url"] for result in results.values() if result["url"].startswith("https:")), None) - return {"sites": results, "runtimes": check_runtimes(secure) if secure else {}} + report = {"sites": results, "runtimes": check_runtimes(secure) if secure else {}} + if config.proxy_port and results: + proxy = f"http://{config.listen}:{config.proxy_port}" + forward = urllib.request.build_opener(urllib.request.ProxyHandler({"http": proxy, "https": proxy})) + report["forward_port"] = {"proxy": proxy, **_loaded(forward, secure or next(iter(results.values()))["url"])} + return report + + +def _loaded(opener, url: str) -> dict: + """``url`` loaded by ``opener``: its status, or why it didn't load, which an answer the proxy made itself (it marks + them ``FAILED``), such as its 502 for a gateway it couldn't reach, is too.""" + try: + with opener.open(url, timeout=CHECK_TIMEOUT_S) as response: + return {"url": url, "status": response.status} + except urllib.error.HTTPError as e: + if e.headers is not None and e.headers.get(FAILED[0][0]) == FAILED[0][1]: + return {"url": url, "error": f"{e.code} {e.reason}: {e.read().decode(errors='replace').strip()}"} + return {"url": url, "status": e.code} + except (urllib.error.URLError, OSError, ssl.SSLError) as e: + return {"url": url, "error": str(getattr(e, "reason", e))} def check_runtimes(url: str) -> dict: diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index f3b1a28a7..fc78e2a34 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -447,18 +447,22 @@ async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], net if failed := routing_problems(report): raise RuntimeError("site routing: " + "; ".join(failed)) return {"names": table.to_dict(), "ca": issued.ca_pem, "ca_path": trust_files(os_type)["ca"], - "trust": report["trust"], "checks": report["checks"], "runtimes": report.get("runtimes", {})} + "trust": report["trust"], "checks": report["checks"], "runtimes": report.get("runtimes", {}), + **({"forward_port": report["forward_port"]} if "forward_port" in report else {})} def routing_problems(report: dict) -> list[str]: - """What the install's ``report`` says failed: a site that didn't load, a runtime that didn't trust the CA, and a - JDK's store that didn't take it.""" + """What the install's ``report`` says failed: a site that didn't load, a runtime that didn't trust the CA, a JDK's + store that didn't take it, and the forward port of a browser alone that didn't load a site.""" + forward = report.get("forward_port") or {} return ([f"{result.get('url', name)} didn't load in the computer: {result['error']}" for name, result in report["checks"].items() if "error" in result] + [f"{runtime} in the computer didn't trust the deploy's CA loading {result.get('url')}: {result['error']}" for runtime, result in report.get("runtimes", {}).items() if "error" in result] + [f"the JDK store {store} didn't take the deploy's CA: {result['error']}" - for store, result in report["trust"].get("java", {}).items() if "error" in result]) + for store, result in report["trust"].get("java", {}).items() if "error" in result] + + ([f"the browser's forward port {forward.get('proxy')} didn't load {forward.get('url')}: " + f"{forward['error']}"] if "error" in forward else [])) async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[str]] = None) -> list[dict]: diff --git a/src/agent_env/env/envs/playwright_mcp/entrypoint.sh b/src/agent_env/env/envs/playwright_mcp/entrypoint.sh index 337885849..519027ec5 100644 --- a/src/agent_env/env/envs/playwright_mcp/entrypoint.sh +++ b/src/agent_env/env/envs/playwright_mcp/entrypoint.sh @@ -4,9 +4,13 @@ set -euo pipefail # MCP_PORT and MCP_HOST are injected by docker-compose (gateway convention). # PLAYWRIGHT_MCP_VERSION is baked into the image at build time. -# The recorder serves the browser's card on RECORDER_PORT, restarted whenever it exits; the MCP server starts once -# it listens, so the card is there by the time the server is healthy, or after RECORDER_WAIT_S without it. +# The recorder fronts the MCP port: it serves the browser's card and recording there and passes every other request to +# the MCP server, which listens on loopback PLAYWRIGHT_MCP_PORT behind it. It serves the card on RECORDER_PORT at once, +# opens the MCP port only once the MCP server listens, so the port answering means the server does, and is restarted +# whenever it exits; the MCP server starts once the card is served, or after RECORDER_WAIT_S without it. +export MCP_PORT="${MCP_PORT:-18765}" export RECORDER_PORT="${RECORDER_PORT:-18766}" +export PLAYWRIGHT_MCP_PORT="${PLAYWRIGHT_MCP_PORT:-18767}" (while true; do /opt/recorder/bin/python /app/recorder.py || true; sleep 1; done) & for _ in $(seq 1 "$(( ${RECORDER_WAIT_S:-20} * 10 ))"); do (exec 3<>"/dev/tcp/127.0.0.1/${RECORDER_PORT}") 2>/dev/null && break @@ -18,7 +22,7 @@ exec npx @playwright/mcp@${PLAYWRIGHT_MCP_VERSION} \ --headless \ --isolated \ --no-sandbox \ - --host "${MCP_HOST:-0.0.0.0}" \ - --port "${MCP_PORT:-18765}" \ + --host 127.0.0.1 \ + --port "${PLAYWRIGHT_MCP_PORT}" \ --allowed-hosts '*' \ --caps vision diff --git a/src/agent_env/env/envs/playwright_mcp/recorder.py b/src/agent_env/env/envs/playwright_mcp/recorder.py index c82dbedf2..25ec8bc6e 100644 --- a/src/agent_env/env/envs/playwright_mcp/recorder.py +++ b/src/agent_env/env/envs/playwright_mcp/recorder.py @@ -1,8 +1,11 @@ """The Playwright MCP browser's recorder: computer-recording/v1 of the page Playwright MCP drives, from Chrome DevTools -screencast frames, on a card and port of its own beside the MCP server. It is idle until a recording starts, which -finds the browser through the remote debugging port Playwright opens for an ``--isolated`` Chromium and follows the -page the agent is on. It runs standalone in the browser's image, so it imports only -``agentenv-framework-protocol[computer]`` and, to talk to the browser, ``websockets``.""" +screencast frames. It is the front of the browser's MCP port: it serves the browser's card and the recording there, as +any MCP server serves its card at its own address, and passes every other request, streamed, to Playwright MCP on a +loopback port of its own behind it. It serves the card on ``RECORDER_PORT`` (18766) too, where deploys read it until +they read it at the MCP port. It is idle until a recording starts, which finds the browser through the remote debugging +port Playwright opens for an ``--isolated`` Chromium and follows the page the agent is on. It runs standalone in the +browser's image, so it imports only ``agentenv-framework-protocol[computer]`` and, to talk to the browser, +``websockets``.""" from __future__ import annotations import asyncio @@ -20,7 +23,11 @@ from typing import Any, AsyncContextManager, Optional import httpx +import uvicorn from PIL import Image as PILImage +from starlette.applications import Starlette +from starlette.responses import PlainTextResponse, StreamingResponse +from starlette.routing import Route from agentenv_protocol.agent_env_environment import ( AgentEnvEnvironment, @@ -33,9 +40,18 @@ logger = logging.getLogger("playwright-mcp-recorder") -#: The port the recorder serves its card on, unless RECORDER_PORT says another; the gateway reads the browser's card -#: there (agent_env.env.envs.playwright_mcp.PLAYWRIGHT_MCP_RECORDER_PORT). +#: The port the recorder serves its card on alone, unless RECORDER_PORT says another; deploys read the browser's card +#: there (agent_env.env.envs.playwright_mcp.PLAYWRIGHT_MCP_RECORDER_PORT) until they read it at the MCP port. DEFAULT_PORT = 18766 +#: The browser's MCP port, which the recorder fronts unless MCP_PORT says another, and the loopback port Playwright MCP +#: listens on behind it unless PLAYWRIGHT_MCP_PORT says another. +DEFAULT_MCP_PORT, DEFAULT_PLAYWRIGHT_MCP_PORT = 18765, 18767 +#: How long passing a request to Playwright MCP may wait to connect; its answer may take as long as it does. How often +#: the front looks for Playwright MCP listening before it opens the MCP port. +CONNECT_S, LISTENING_POLL_S = 10.0, 0.2 +#: The headers that belong to one connection, which the front doesn't pass on; Host goes on as the caller sent it. +HOP_HEADERS = frozenset({"connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailer", + "transfer-encoding", "upgrade"}) #: How often the browser is looked for while a recording has none. FIND_S = 0.5 #: A page that hasn't changed is filmed again this often, so a still page doesn't split the recording at a gap. @@ -285,21 +301,95 @@ def stream(self, hints: RecordHints) -> AsyncIterator[RecordedFrame]: return self.driver.frames(hints) -def create_app(recorder: BrowserRecorder) -> Any: - from starlette.applications import Starlette - +def create_app(recorder: BrowserRecorder, behind: Optional[httpx.AsyncClient] = None) -> Any: + """The recorder's card and recording, and, as the front of the MCP port, every other request passed to ``behind``, + a client whose base URL is Playwright MCP's.""" app = Starlette() AgentEnvStarletteApplication(recorder._build_card(), recorder).add_routes_to_app(app) + if behind is not None: + app.router.routes.append(Route("/{path:path}", _passing_to(behind), methods=[ + "GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])) return app -def main() -> None: - import uvicorn +def _passing_to(behind: httpx.AsyncClient) -> Callable: + async def passed(request: Any) -> Any: + """The request passed to Playwright MCP as it came, its body and its answer streamed, or 502 when Playwright + MCP doesn't answer.""" + has_body = request.method not in ("GET", "HEAD", "OPTIONS") or "content-length" in request.headers + sent = behind.build_request( + request.method, request.url.path, params=request.url.query or None, + headers=[(key, value) for key, value in request.headers.items() if key.lower() not in HOP_HEADERS], + content=request.stream() if has_body else None) + try: + answer = await behind.send(sent, stream=True) + except httpx.TransportError as e: + return PlainTextResponse(f"Playwright MCP isn't answering: {type(e).__name__}: {e}\n", status_code=502) + return PassedAnswer(answer) + + return passed + + +class PassedAnswer(StreamingResponse): + """Playwright MCP's answer streamed through as it came, its repeated headers kept apart, and closed when the + response ends, streamed or not: a caller that leaves before the first chunk cancels the body before it starts.""" + + #: Hop-by-hop headers, and those uvicorn adds itself, which passing on would repeat. + DROPPED = frozenset(name.encode() for name in HOP_HEADERS) | {b"date", b"server"} + + def __init__(self, answer: httpx.Response) -> None: + super().__init__(answer.aiter_raw(), status_code=answer.status_code) + self.raw_headers = [(key.lower(), value) for key, value in answer.headers.raw + if key.lower() not in self.DROPPED] + self.answer = answer + async def __call__(self, scope: Any, receive: Callable, send: Callable) -> None: + try: + await super().__call__(scope, receive, send) + finally: + await self.answer.aclose() + + +async def serve() -> None: + """The card alone on ``RECORDER_PORT`` at once, and the front on the MCP port once Playwright MCP listens behind it, + so the MCP port answering, which is what the gateway's health check asks, means Playwright MCP does; one recorder + behind both.""" + host, recorder = os.environ.get("MCP_HOST", "0.0.0.0"), BrowserRecorder() + behind_port = int(os.environ.get("PLAYWRIGHT_MCP_PORT", DEFAULT_PLAYWRIGHT_MCP_PORT)) + behind = httpx.AsyncClient(base_url=f"http://127.0.0.1:{behind_port}", + timeout=httpx.Timeout(None, connect=CONNECT_S)) + card = uvicorn.Server(uvicorn.Config(create_app(recorder), host=host, + port=int(os.environ.get("RECORDER_PORT", DEFAULT_PORT)), log_level="warning")) + front = uvicorn.Server(uvicorn.Config(create_app(recorder, behind), host=host, + port=int(os.environ.get("MCP_PORT", DEFAULT_MCP_PORT)), log_level="warning")) + + async def front_once_behind_listens() -> None: + await _listening(behind_port) + await front.serve() + + try: + await asyncio.gather(card.serve(), front_once_behind_listens()) + finally: + await behind.aclose() + + +async def _listening(port: int) -> None: + """Return once something accepts connections on loopback ``port``.""" + while True: + try: + _, writer = await asyncio.open_connection("127.0.0.1", port) + except OSError: + await asyncio.sleep(LISTENING_POLL_S) + continue + writer.close() + await writer.wait_closed() + return + + +def main() -> None: logging.basicConfig(level=logging.INFO, format="%(asctime)s recorder %(levelname)s %(message)s") logging.getLogger("httpx").setLevel(logging.WARNING) # a request each time it finds a browser - uvicorn.run(create_app(BrowserRecorder()), host=os.environ.get("MCP_HOST", "0.0.0.0"), - port=int(os.environ.get("RECORDER_PORT", DEFAULT_PORT)), log_level="warning") + asyncio.run(serve()) if __name__ == "__main__": diff --git a/tst/integration/computer/browser_computer_test.py b/tst/integration/computer/browser_computer_test.py index ab03d9cad..b093b2fec 100644 --- a/tst/integration/computer/browser_computer_test.py +++ b/tst/integration/computer/browser_computer_test.py @@ -600,6 +600,8 @@ async def test_routing_sites_it_goes_out_only_through_the_forward_port_which_the assert [(tab["url"], tab["title"]) for tab in tabs] == [("https://shop.test/landed", "landed"), ("http://notes.localhost/", "notes")], tabs assert named.strip() == "http://127.0.0.1:3129" + assert deployed.metadata["computer"]["sites"]["forward_port"] == { + "proxy": "http://127.0.0.1:3129", "url": "https://shop.test/", "status": 200} assert pages["https://shop.test/alpha"].startswith("200 ") and pages["https://shop.test/alpha"].endswith( "alpha"), pages assert pages["http://notes.localhost/bravo"].endswith("bravo"), pages diff --git a/tst/integration/computer/ca_trust_test.py b/tst/integration/computer/ca_trust_test.py index 5e525e091..2b175352f 100644 --- a/tst/integration/computer/ca_trust_test.py +++ b/tst/integration/computer/ca_trust_test.py @@ -49,6 +49,7 @@ async def test_every_runtime_of_a_computer_routing_a_site_trusts_its_ca_launched assert at_boot == {"node": "200", "java": "200", "copied java": "200", "curl": "200"}, at_boot assert variables.split() == ["/etc/agentenv/ca-bundle.pem", "/etc/agentenv/ca-bundle.pem", "/etc/agentenv/ca.pem"] assert through_cert_file["exit_code"] == 0 and through_cert_file["stdout"].strip() == "200", through_cert_file + assert "forward_port" not in routed assert routed["ca_path"] == "/etc/agentenv/ca.pem" and routed["ca"].strip() in added and mode.strip() == "644" assert routed["ca"].strip() in bundle and bundle.count("BEGIN CERTIFICATE") > 100 assert {runtime: "status" in result for runtime, result in routed["runtimes"].items()} == { diff --git a/tst/integration/env/envs/playwright_mcp_local_test.py b/tst/integration/env/envs/playwright_mcp_local_test.py new file mode 100644 index 000000000..7ee1710b6 --- /dev/null +++ b/tst/integration/env/envs/playwright_mcp_local_test.py @@ -0,0 +1,201 @@ +"""The Playwright MCP browser's image, built for this machine and run alone: its recorder fronts the MCP port, so the +browser's card is served there, as every MCP server serves its card, and on the recorder's own port too, where deploys +read it until they read it at the MCP port, the card first and the MCP port only once Playwright MCP answers behind it, +which is what the gateway's health check asks; an MCP client lists and calls the browser's tools through the front; a +recording through the card on the MCP port films the page the client opens; and a killed recorder comes back, the front +with it, while the MCP server behind it stays up. Needs Docker.""" + +import asyncio +import json +import subprocess +import time +import uuid +from pathlib import Path + +import httpx +import pytest +from mcp import ClientSession +from mcp.client.streamable_http import streamable_http_client + +from agentenv_protocol import client as protocol_v1 +from agentenv_protocol.computer.card import COMPUTER_RECORDING +from agentenv_protocol.types import WELL_KNOWN_PATH + +from agent_env.env.envs import playwright_mcp + +pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] + +CONTEXT = Path(playwright_mcp.__file__).parent +IMAGE = "agentenv-playwright-mcp:front-test" +#: How long the image may take to answer on its MCP port, its MCP server's first start included. +READY_S = 180.0 +PAGE = "data:text/html,front

front

" +#: The ids of the image's processes whose command line holds the first argument, killed with the second when it's given; +#: the image has no procps, so its recorder's Python reads /proc. +PROCESSES = """import os, sys +found = [] +for pid in (p for p in os.listdir("/proc") if p.isdigit() and int(p) != os.getpid()): + try: + if sys.argv[1] in open(f"/proc/{pid}/cmdline", "rb").read().replace(b"\\0", b" ").decode(errors="replace"): + found.append(pid) + except OSError: + pass +for pid in found if len(sys.argv) > 2 else (): + os.kill(int(pid), int(sys.argv[2])) +print(" ".join(sorted(found, key=int))) +""" + + +#: Run in the image as it starts: watches its card port and MCP port from inside, where nothing answers for a port that +#: isn't listening, and the moment the MCP port first accepts, opens an MCP session through it; prints whether the card +#: answered first and the session's status. +OPENING = """import json, socket, time, urllib.error, urllib.request +def accepts(port): + try: + socket.create_connection(("127.0.0.1", port), 0.5).close() + return True + except OSError: + return False +deadline, card_at, mcp_at = time.monotonic() + 180, None, None +while time.monotonic() < deadline and mcp_at is None: + now = time.monotonic() + if card_at is None and accepts(18766): + card_at = now + if accepts(18765): + mcp_at = now + else: + time.sleep(0.05) +body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { + "protocolVersion": "2025-03-26", "capabilities": {}, "clientInfo": {"name": "opening", "version": "1"}}}).encode() +request = urllib.request.Request("http://127.0.0.1:18765/mcp", body, { + "Content-Type": "application/json", "Accept": "application/json, text/event-stream"}) +try: + status = urllib.request.urlopen(request, timeout=30).status +except urllib.error.HTTPError as e: + status = e.code +print(json.dumps({"card_first": card_at is not None and mcp_at is not None and card_at <= mcp_at, "status": status})) +""" + + +def _docker(*args: str) -> str: + return subprocess.run(["docker", *args], check=True, capture_output=True, text=True).stdout + + +def _processes(container: str, pattern: str, signal: int | None = None) -> list[str]: + return _docker("exec", container, "/opt/recorder/bin/python", "-c", PROCESSES, pattern, + *([str(signal)] if signal is not None else [])).split() + + +@pytest.fixture(scope="module") +def image() -> str: + _docker("build", "-t", IMAGE, str(CONTEXT)) + return IMAGE + + +@pytest.fixture +def browser(image): + """The image run alone, its MCP port and its recorder's port published on loopback: (container, the MCP port's + base URL, the recorder port's).""" + name = f"playwright-mcp-front-{uuid.uuid4().hex[:8]}" + _docker("run", "-d", "--name", name, "-e", "MCP_HOST=0.0.0.0", "-e", "MCP_PORT=18765", "-p", "127.0.0.1::18765", + "-p", "127.0.0.1::18766", image) + try: + mcp, recorder = (f"http://{_docker('port', name, f'{port}/tcp').splitlines()[0]}" for port in (18765, 18766)) + asyncio.run(_answering(mcp)) + yield name, mcp, recorder + finally: + subprocess.run(["docker", "rm", "-f", name], capture_output=True) + + +async def _answering(mcp: str) -> None: + """Wait until the front serves the card and Playwright MCP behind it opens a session.""" + deadline, last = time.monotonic() + READY_S, None + while time.monotonic() < deadline: + try: + async with httpx.AsyncClient(timeout=10) as http: + (await http.get(f"{mcp}{WELL_KNOWN_PATH}")).raise_for_status() + await _tools(mcp) + return + except Exception as e: # noqa: BLE001 - not up yet + last = e + await asyncio.sleep(1) + raise AssertionError(f"the image didn't answer on its MCP port within {READY_S:.0f} s: {last!r}") + + +async def _tools(mcp: str, call: tuple[str, dict] | None = None) -> tuple[list[str], str]: + async with streamable_http_client(f"{mcp}/mcp") as (read, write, _): + async with ClientSession(read, write) as session: + await session.initialize() + names = [tool.name for tool in (await session.list_tools()).tools] + text = "" + if call is not None: + result = await session.call_tool(*call) + text = "".join(getattr(part, "text", "") for part in result.content) + return names, text + + +@pytest.mark.asyncio +async def test_the_card_is_on_the_mcp_port_and_the_recorders_and_tools_pass_through_the_front(browser): + _, mcp, recorder = browser + async with httpx.AsyncClient(timeout=30) as http: + on_mcp_port = (await http.get(f"{mcp}{WELL_KNOWN_PATH}")).json() + on_recorder_port = (await http.get(f"{recorder}{WELL_KNOWN_PATH}")).json() + names, navigated = await _tools(mcp, ("browser_navigate", {"url": PAGE})) + + assert on_mcp_port == on_recorder_port and on_mcp_port["name"] == playwright_mcp.PLAYWRIGHT_MCP_ENVIRONMENT_NAME + assert [extension["uri"] for extension in on_mcp_port["capabilities"]["extensions"]] == [COMPUTER_RECORDING] + assert {"browser_navigate", "browser_snapshot", "browser_take_screenshot"} <= set(names) + assert "front" in navigated + + +def test_the_mcp_port_opens_only_once_playwright_mcp_answers_behind_it_and_the_card_comes_first(image): + name = f"playwright-mcp-opening-{uuid.uuid4().hex[:8]}" + _docker("run", "-d", "--name", name, "-e", "MCP_HOST=0.0.0.0", "-e", "MCP_PORT=18765", image) + try: + opened = _docker("exec", name, "/opt/recorder/bin/python", "-c", OPENING).strip().splitlines()[-1] + finally: + subprocess.run(["docker", "rm", "-f", name], capture_output=True) + + assert json.loads(opened) == {"card_first": True, "status": 200}, opened + + +@pytest.mark.asyncio +async def test_a_recording_through_the_card_on_the_mcp_port_films_the_page_an_agent_opens(browser): + """One MCP session throughout, as an agent holds one: an isolated browser lives as long as its session.""" + _, mcp, _ = browser + async with httpx.AsyncClient(timeout=60) as http, streamable_http_client(f"{mcp}/mcp") as (read, write, _): + async with ClientSession(read, write) as session: + await session.initialize() + card = (await http.get(f"{mcp}{WELL_KNOWN_PATH}")).json() + start, end = (protocol_v1.find_extension_method(card, COMPUTER_RECORDING, op)["endpoint"] + for op in ("start", "end")) + await session.call_tool("browser_navigate", {"url": PAGE}) + started = (await http.post(f"{mcp}{start}", json={"request_id": "front:start", "fps": 5})).json() + for shade in ("red", "green", "blue"): + page = f"data:text/html," + await session.call_tool("browser_navigate", {"url": page}) + await asyncio.sleep(1) + manifest = (await http.post(f"{mcp}{end}", json={"recording_id": started["recording_id"]})).json() + + assert manifest["ended_by"] == "end" and manifest["segments"], manifest + assert sum(segment["frames"] for segment in manifest["segments"]) >= 3, manifest + + +@pytest.mark.asyncio +async def test_a_killed_recorder_comes_back_the_front_with_it_and_the_mcp_server_behind_it_stays_up(browser): + name, mcp, _ = browser + serving = _processes(name, "@playwright/mcp") + assert serving and _processes(name, "/app/recorder.py", 9) + deadline, back = time.monotonic() + 30, None + while time.monotonic() < deadline and back is None: + await asyncio.sleep(1) + try: + async with httpx.AsyncClient(timeout=5) as http: + back = (await http.get(f"{mcp}{WELL_KNOWN_PATH}")).raise_for_status().json() + except httpx.HTTPError: + pass + names, _ = await _tools(mcp) + + assert back is not None and back["name"] == playwright_mcp.PLAYWRIGHT_MCP_ENVIRONMENT_NAME + assert "browser_navigate" in names + assert _processes(name, "@playwright/mcp") == serving diff --git a/tst/integration/env/gateway/gateway_test.py b/tst/integration/env/gateway/gateway_test.py index 255aea263..ae4020400 100644 --- a/tst/integration/env/gateway/gateway_test.py +++ b/tst/integration/env/gateway/gateway_test.py @@ -37,6 +37,10 @@ PLAYWRIGHT_MCP_IMAGE_TAG, PLAYWRIGHT_MCP_ENVIRONMENT_NAME, ) +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.sandbox_providers.local_docker.provider import LocalSandboxProvider 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 @@ -1428,6 +1432,31 @@ 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. + 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) + 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 " + f"{PLAYWRIGHT_MCP_ENVIRONMENT_NAME} /opt/recorder/bin/python " + f"-c \"{read_card}\"") + on_mcp_port = json.loads(served.strip().splitlines()[-1]) + assert on_mcp_port["name"] == PLAYWRIGHT_MCP_ENVIRONMENT_NAME + assert [e["uri"] for e in on_mcp_port["capabilities"]["extensions"]] == [COMPUTER_RECORDING] + start, end = (protocol_v1.find_extension_method(browser, COMPUTER_RECORDING, op)["endpoint"] + for op in ("start", "end")) + async with httpx.AsyncClient(headers=result.control_headers, timeout=120) as client: + started = (await client.post(f"{result.gateway_url}{start}", json={"request_id": "e2e:start", "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) + manifest = (await client.post(f"{result.gateway_url}{end}", + json={"recording_id": started.json()["recording_id"]})).json() + assert manifest["segments"] and sum(segment["frames"] for segment in manifest["segments"]) > 0, manifest + # Verify REST proxy: empty channels before loading data async with httpx.AsyncClient() as client: response = await client.get(f"{result.gateway_url}/svc/slack/api/channels?member_only=false", timeout=30) diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index 4708de923..3b8fa5643 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -334,6 +334,25 @@ async def test_the_forward_port_tunnels_a_connect_to_a_sites_name_through_its_ce "notes.test", "https") +@pytest.mark.asyncio +async def test_a_request_in_a_sites_tunnel_naming_another_host_is_refused_and_logged_as_the_port_refuses(tmp_path, + capsys): + async with Upstream() as upstream: + config, issued = _config(tmp_path, upstream.port) + async with _forward_port(config) as port: + reader, writer = await asyncio.open_connection("127.0.0.1", port) + writer.write(b"CONNECT notes.test:443 HTTP/1.1\r\nHost: notes.test:443\r\n\r\n") + await reader.readuntil(b"\r\n\r\n") + await writer.start_tls(ssl.create_default_context(cadata=issued.ca_pem), server_hostname="notes.test") + start, headers, body, conn = await _exchange(port, b"GET / HTTP/1.1\r\nHost: evil.test\r\n\r\n", + reader_writer=(reader, writer)) + writer.close() + + assert start == "HTTP/1.1 403 Forbidden" and headers["x-agentenv-site-proxy"] == "failed" + assert upstream.requests == [] + assert "site proxy: refused GET evil.test: no site of this computer" in capsys.readouterr().err + + @pytest.mark.asyncio async def test_the_forward_port_serves_a_localhost_name_named_by_its_url_and_sends_a_real_name_to_https(tmp_path): async with Upstream() as upstream: @@ -504,6 +523,34 @@ def open(self, url, timeout): "runtimes": {"node": {"url": "https://notes.test/", "status": 502}}} +def test_a_check_loads_a_site_through_the_forward_port_and_names_one_that_doesnt_answer(monkeypatch): + opened = [] + + class Opener: + def __init__(self, proxies): + self.proxies = proxies + + def open(self, url, timeout): + opened.append((url, self.proxies)) + if self.proxies: + raise urllib.error.URLError(ConnectionRefusedError(111, "Connection refused")) + return contextlib.nullcontext(SimpleNamespace(status=200)) + + monkeypatch.setattr(site_proxy.urllib.request, "build_opener", lambda handler: Opener(handler.proxies)) + monkeypatch.setattr(site_proxy, "check_runtimes", lambda url: {}) + sites_ = [Site("notes", "http://gw/website/notes/", ["notes.localhost"]), + Site("chat", "http://gw/website/chat/", ["chat.test"])] + + alone = site_proxy.check(Config(user=None, sites=sites_, proxy_port=3129)) + desktop = site_proxy.check(Config(user=None, sites=sites_)) + + proxy = "http://127.0.0.1:3129" + assert alone["forward_port"] == {"proxy": proxy, "url": "https://chat.test/", + "error": "[Errno 111] Connection refused"} + assert opened[2] == ("https://chat.test/", {"http": proxy, "https": proxy}) + assert alone["sites"] == desktop["sites"] and "forward_port" not in desktop + + @pytest.mark.asyncio async def test_each_name_is_served_the_certificate_of_its_own_site_and_a_wildcard_covers_one_label(tmp_path): async with Upstream() as upstream: diff --git a/tst/unit/env/envs/test_computer_sites.py b/tst/unit/env/envs/test_computer_sites.py index b00572abf..0cf8c20b7 100644 --- a/tst/unit/env/envs/test_computer_sites.py +++ b/tst/unit/env/envs/test_computer_sites.py @@ -573,6 +573,12 @@ def test_routing_names_each_site_that_didnt_load_runtime_that_didnt_trust_the_ca "problem", "the JDK store /opt/b/lib/security/cacerts didn't take the deploy's CA: keystore password was incorrect"] assert sites.routing_problems({"checks": {}, "trust": {}}) == [] + forward = {"proxy": "http://127.0.0.1:3129", "url": "https://chat.test/", "error": "[Errno 111] Connection refused"} + assert sites.routing_problems({"checks": {}, "trust": {}, "forward_port": forward}) == [ + "the browser's forward port http://127.0.0.1:3129 didn't load https://chat.test/: " + "[Errno 111] Connection refused"] + loaded = {"proxy": "http://127.0.0.1:3129", "url": "https://chat.test/", "status": 200} + assert sites.routing_problems({"checks": {}, "trust": {}, "forward_port": loaded}) == [] def test_the_trust_a_routing_linux_computer_boots_with_is_the_local_providers_so_the_two_always_agree(): diff --git a/tst/unit/env/envs/test_playwright_mcp_recorder.py b/tst/unit/env/envs/test_playwright_mcp_recorder.py index 207c0d45f..748df2c75 100644 --- a/tst/unit/env/envs/test_playwright_mcp_recorder.py +++ b/tst/unit/env/envs/test_playwright_mcp_recorder.py @@ -1,22 +1,30 @@ """The Playwright MCP browser's recorder: it finds the browser Playwright MCP launched, follows the page the agent is on through DevTools target events, holds a still page's frame so the recording doesn't split, and serves -computer-recording/v1 on a card of its own that the gateway nests as the playwright-mcp child env.""" +computer-recording/v1 on a card of its own that the gateway nests as the playwright-mcp child env. It fronts the MCP +port: the card and recording there, every other request passed to Playwright MCP behind it, streamed, and 502 when +Playwright MCP doesn't answer.""" from __future__ import annotations import asyncio import base64 +import contextlib import io import itertools import re +import socket from pathlib import Path import httpx import pytest +import uvicorn av = pytest.importorskip("av") from PIL import Image as PILImage # noqa: E402 +from starlette.applications import Starlette # noqa: E402 +from starlette.responses import JSONResponse # noqa: E402 +from starlette.routing import Route # noqa: E402 from agentenv_protocol import client as protocol_client # noqa: E402 from agentenv_protocol.computer.card import COMPUTER_RECORDING # noqa: E402 from agentenv_protocol.computer.recording import RecordHints # noqa: E402 @@ -221,8 +229,186 @@ async def test_the_gateway_nests_the_card_as_a_child_env_whose_recording_is_invo "end": "/svc/mcp-playwright-mcp/agentenv/ext/computer-recording/end"} -def test_the_image_serves_the_recorder_on_the_port_the_gateway_reads_the_card_on(): +def test_the_image_fronts_the_mcp_port_with_the_recorder_and_serves_the_card_where_deploys_read_it(): entrypoint = (Path(playwright_mcp.__file__).parent / "entrypoint.sh").read_text() - default = re.search(r'RECORDER_PORT="\$\{RECORDER_PORT:-(\d+)\}"', entrypoint).group(1) - assert int(default) == recorder.DEFAULT_PORT == playwright_mcp.PLAYWRIGHT_MCP_RECORDER_PORT + + def default(name: str) -> int: + return int(re.search(rf'{name}="\$\{{{name}:-(\d+)\}}"', entrypoint).group(1)) + + assert default("RECORDER_PORT") == recorder.DEFAULT_PORT == playwright_mcp.PLAYWRIGHT_MCP_RECORDER_PORT + assert default("MCP_PORT") == recorder.DEFAULT_MCP_PORT + assert default("PLAYWRIGHT_MCP_PORT") == recorder.DEFAULT_PLAYWRIGHT_MCP_PORT + assert '--host 127.0.0.1' in entrypoint and '--port "${PLAYWRIGHT_MCP_PORT}"' in entrypoint assert "--isolated" in entrypoint # the remote debugging port the recorder finds comes with it + + +def _behind(app) -> httpx.AsyncClient: + return httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://playwright-mcp") + + +@pytest.mark.asyncio +async def test_the_front_serves_the_card_and_recording_and_passes_everything_else_to_playwright_mcp(): + passed = [] + + async def mcp(request): + passed.append((request.method, request.url.path, request.url.query, await request.body(), + request.headers.get("mcp-session-id"), request.headers.get("host"))) + return JSONResponse({"jsonrpc": "2.0", "id": 1, "result": {}}, headers={"Mcp-Session-Id": "s-2"}) + + playwright = Starlette(routes=[Route("/mcp", mcp, methods=["POST", "DELETE"])]) + async with _behind(playwright) as behind: + front = recorder.create_app(_recorder(_Browser()), behind) + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=front), base_url="http://front") as http: + card = (await http.get("/.well-known/agent-env.json")).json() + answer = await http.post("/mcp?x=1", json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}, + headers={"Mcp-Session-Id": "s-1"}) + closed = await http.delete("/mcp", headers={"Mcp-Session-Id": "s-2"}) + + assert card["name"] == "playwright-mcp" + assert [e["uri"] for e in card["capabilities"]["extensions"]] == [COMPUTER_RECORDING] + assert (answer.status_code, answer.json()["result"], answer.headers["mcp-session-id"]) == (200, {}, "s-2") + assert passed[0][:3] == ("POST", "/mcp", "x=1") and b'"tools/list"' in passed[0][3] and passed[0][4] == "s-1" + assert passed[0][5] == "front" # the Host the caller sent, not Playwright MCP's own address + assert closed.status_code == 200 and (passed[1][0], passed[1][4]) == ("DELETE", "s-2") + + +@pytest.mark.asyncio +async def test_the_front_answers_502_naming_playwright_mcp_when_it_doesnt_answer(): + def refuse(request): + raise httpx.ConnectError("Connection refused", request=request) + + behind = httpx.AsyncClient(transport=httpx.MockTransport(refuse), base_url="http://playwright-mcp") + front = recorder.create_app(_recorder(_Browser()), behind) + async with behind, httpx.AsyncClient(transport=httpx.ASGITransport(app=front), base_url="http://front") as http: + answer = await http.post("/mcp", json={}) + card = await http.get("/.well-known/agent-env.json") + + assert answer.status_code == 502 and answer.text.startswith("Playwright MCP isn't answering: ConnectError") + assert card.status_code == 200 + + +async def _serve(app) -> tuple[uvicorn.Server, int]: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(("127.0.0.1", 0)) + config = uvicorn.Config(app, lifespan="off", log_level="critical", access_log=False) + config.load() + server = uvicorn.Server(config) + server.lifespan = config.lifespan_class(config) + await server.startup(sockets=[sock]) + return server, sock.getsockname()[1] + + +def _free_port() -> int: + with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + return sock.getsockname()[1] + + +async def _accepts(port: int) -> bool: + try: + _, writer = await asyncio.open_connection("127.0.0.1", port) + except OSError: + return False + writer.close() + return True + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("socket_enabled") +async def test_the_card_is_served_at_once_and_the_mcp_port_opens_only_once_playwright_mcp_listens(monkeypatch): + mcp, card, behind = _free_port(), _free_port(), _free_port() + for name, port in (("MCP_PORT", mcp), ("RECORDER_PORT", card), ("PLAYWRIGHT_MCP_PORT", behind)): + monkeypatch.setenv(name, str(port)) + monkeypatch.setenv("MCP_HOST", "127.0.0.1") + serving = asyncio.create_task(recorder.serve()) + playwright = None + try: + for _ in range(100): + if await _accepts(card): + break + await asyncio.sleep(0.05) + await asyncio.sleep(3 * recorder.LISTENING_POLL_S) + card_first, mcp_first = await _accepts(card), await _accepts(mcp) + playwright = await asyncio.start_server(lambda reader, writer: writer.close(), "127.0.0.1", behind) + for _ in range(100): + if await _accepts(mcp): + break + await asyncio.sleep(0.05) + async with httpx.AsyncClient() as http: + served = (await http.get(f"http://127.0.0.1:{mcp}/.well-known/agent-env.json")).json() + finally: + serving.cancel() + with contextlib.suppress(asyncio.CancelledError): + await serving + if playwright is not None: + playwright.close() + + assert (card_first, mcp_first) == (True, False) + assert served["name"] == "playwright-mcp" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("spec_version", ["2.0", "2.4"], ids=["disconnect-message", "send-raises"]) +async def test_an_answer_is_closed_when_its_caller_leaves_before_the_first_chunk(spec_version): + """Under ASGI 2.4 a caller that left shows as ``send`` raising; before it, as a disconnect message.""" + closed = asyncio.Event() + + class Held(httpx.AsyncByteStream): + async def __aiter__(self): + await asyncio.Event().wait() + yield b"" + + async def aclose(self) -> None: + closed.set() + + answer = httpx.Response(200, headers=[("Set-Cookie", "a=1"), ("Set-Cookie", "b=2"), ("Connection", "close")], + stream=Held()) + passed = recorder.PassedAnswer(answer) + + async def receive(): + return {"type": "http.disconnect"} + + async def send(message): + if spec_version == "2.4": + raise OSError("the caller left") + + with contextlib.suppress(Exception): + await asyncio.wait_for(passed({"type": "http", "asgi": {"spec_version": spec_version}}, receive, send), 5) + + assert closed.is_set() + assert [value for key, value in passed.raw_headers if key == b"set-cookie"] == [b"a=1", b"b=2"] + assert b"connection" not in dict(passed.raw_headers) + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("socket_enabled") +async def test_the_front_streams_an_event_stream_as_playwright_mcp_sends_it(): + release = asyncio.Event() + + async def stream(reader, writer): + await reader.readuntil(b"\r\n\r\n") + event = b"event: message\ndata: 1\n\n" + writer.write(b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n" + + f"{len(event):x}\r\n".encode() + event + b"\r\n") + await writer.drain() + await release.wait() + writer.write(b"0\r\n\r\n") + await writer.drain() + writer.close() + + playwright = await asyncio.start_server(stream, "127.0.0.1", 0) + behind = httpx.AsyncClient(base_url=f"http://127.0.0.1:{playwright.sockets[0].getsockname()[1]}") + server, port = await _serve(recorder.create_app(_recorder(_Browser()), behind)) + try: + async with httpx.AsyncClient() as http, http.stream("GET", f"http://127.0.0.1:{port}/mcp") as answer: + lines = answer.aiter_lines() + first = await asyncio.wait_for(anext(lines), 5) + release.set() + rest = [line async for line in lines] + finally: + await server.shutdown() + playwright.close() + await behind.aclose() + + assert answer.headers["content-type"] == "text/event-stream" and first == "event: message" + assert rest[:1] == ["data: 1"]