diff --git a/src/agent_env/env/envs/_multi_computers.py b/src/agent_env/env/envs/_multi_computers.py index eda519f88..5af404003 100644 --- a/src/agent_env/env/envs/_multi_computers.py +++ b/src/agent_env/env/envs/_multi_computers.py @@ -13,6 +13,7 @@ from agentenv_protocol.computer import tool_prefix_problem from agent_env.env.env import DeployedEnv, DeployedGatewayEnv, Env +from agent_env.env.envs.computer.utils import sites from agent_env.env.gateway import ExternalChild from agent_env.providers.sandbox_providers.sandbox_provider import build_sandbox_provider, get_env_sandbox_provider @@ -58,21 +59,34 @@ def deploy_options(entry: Optional[dict], sandbox_type: str | None) -> dict: return {"sandbox_type": sandbox_type, **(entry or {})} -def refusal(computer: ComputerEnv, entry: dict, sandbox_type: str | None) -> str | None: - """Why ``computer`` wouldn't deploy in a multi env with ``entry``, its child_envs entry: why it wouldn't alone, a - site it would route, which only a later change routes to the multi env's websites, or a sandbox provider whose URLs - take credentials, which the gateway doesn't send to a child env.""" +def routes_later(entry: Optional[dict]) -> bool: + """Whether a computer with ``entry``, its child_envs entry, routes the multi env's websites, which come up with its + gateway, after it: ``site_routing`` with no ``sites`` of its own.""" + params = (entry or {}).get("env_params") # one that isn't a table is the computer's own params check to refuse + return isinstance(params, dict) and bool(params.get("site_routing")) and not params.get("sites") + + +def refusal(computer: ComputerEnv, entry: dict, sandbox_type: str | None, websites: Mapping[str, str]) -> str | None: + """Why ``computer`` wouldn't deploy in a multi env with ``entry``, its child_envs entry: why it wouldn't alone, + ``site_routing`` naming none of the multi env's ``websites`` (their environment names by env id), or a sandbox + provider whose URLs take credentials, which the gateway doesn't send to a child env.""" options = deploy_options(entry, sandbox_type) params = options.get("env_params") - if isinstance(params, dict) and (routed := [key for key in ("site_routing", "sites") if params.get(key)]): - return (f"env_params.{routed[0]}: a computer in a multi env doesn't route sites yet; deploy it beside the multi " - "env to route them") + if routes_later(entry): + routing = params["site_routing"] + if not websites: + return "env_params.site_routing routes the multi env's websites, and it has none" + # A list of other than ids is the computer's own params check to refuse. + if isinstance(routing, list) and all(isinstance(env_id, str) for env_id in routing) and \ + (others := [env_id for env_id in routing if env_id not in websites]): + return (f"env_params.site_routing names {', '.join(map(repr, others))}, which isn't one of the multi env's " + f"websites ({', '.join(map(repr, websites))})") links = (build_sandbox_provider(options["sandbox_type"]) if options["sandbox_type"] else get_env_sandbox_provider()).links if headed := [type(link).__name__ for link in links if link.REQUEST_HEADERS]: return (f"{', '.join(headed)} takes credentials on each request to a sandbox's URL, which the multi env's gateway " f"doesn't send to a child env; set child_envs.{computer.environment_name}.sandbox_type to a sandbox type " "whose URLs take none") - return computer.deploy_refusal(**options) + return computer.deploy_refusal(**options, sites_later=routes_later(entry)) class Computers: @@ -85,6 +99,7 @@ class Computers: def __init__(self, envs: list[ComputerEnv], owner: str) -> None: self.envs = envs self._owner = owner # the multi env's id, for what's logged + self._routes: dict[str, Optional[list[str]]] = {} # each computer routing sites: the names it routes, or all self._replaced: list[ComputerEnv] = [] self._earlier: Optional[tuple[list[ComputerEnv], list[ComputerEnv]]] = None @@ -119,11 +134,19 @@ def resets(self, deployed: DeployedEnv) -> list[tuple[str, Awaitable[None]]]: for env in self.envs if env.environment_name in records] def deployer(self, child_envs: Optional[dict], *, sandbox_type: str | None, ttl_seconds: int, - attribution: Optional[Attribution]) -> Callable[[Optional[str]], Awaitable[list[ExternalChild]]]: + attribution: Optional[Attribution], websites: Mapping[str, str] + ) -> Callable[[Optional[str]], Awaitable[list[ExternalChild]]]: """The gateway's ``children``: every computer deployed as its child_envs entry says, its tools served under its name and ``_``, and its network allowing ``reach``, the gateway's address when the gateway has one yet; then each - as the gateway fronts it. A gateway deploy tried again deploys them again, after closing the earlier try's.""" + as the gateway fronts it. A computer routing the multi env's ``websites`` (their environment names by env id) + boots with its boot apps held, for ``finish`` to route them once the gateway is up. A gateway deploy tried again + deploys them again, after closing the earlier try's.""" tries = 0 + self._routes = {} + for name, entry in (child_envs or {}).items(): + if routes_later(entry): + routing = entry["env_params"]["site_routing"] + self._routes[name] = None if routing is True else [websites[env_id] for env_id in routing] async def deploy(reach: Optional[str]) -> list[ExternalChild]: nonlocal tries @@ -132,7 +155,8 @@ async def deploy(reach: Optional[str]) -> list[ExternalChild]: tries += 1 results = await asyncio.gather(*(env.deploy( ttl_seconds=ttl_seconds, attribution=attribution, tool_prefix=f"{env.environment_name}_", - reach=reach or "", **deploy_options((child_envs or {}).get(env.environment_name), sandbox_type)) + reach=reach or "", sites_later=env.environment_name in self._routes, + **deploy_options((child_envs or {}).get(env.environment_name), sandbox_type)) for env in self.envs), return_exceptions=True) if failed := [result for result in results if isinstance(result, BaseException)]: for error in failed[1:]: @@ -142,7 +166,20 @@ async def deploy(reach: Optional[str]) -> list[ExternalChild]: for env, record in zip(self.envs, results)] return deploy - async def record(self, deployed: DeployedEnv) -> None: + async def finish(self, deployed: DeployedEnv) -> None: + """Once the gateway is up, before ``deployed``, the multi env's record, is registered: each computer that routes + the multi env's websites routes them, those its ``site_routing`` names or all, once they're ready, and launches + its held apps; then each computer's own record is kept in ``deployed`` (``record``).""" + if self._routes: + # Selected by the name each website is served as, its environment_name, which its card's name needn't be. + routed = {name: await sites.deployed_sites([deployed], services=only) for name, only in self._routes.items()} + await sites.require_ready([deployed]) + await each(self._owner, "route its sites", [(f"computer {env.environment_name!r}", + env.route_sites(routed[env.environment_name])) + for env in self.envs if env.environment_name in routed]) + self.record(deployed) + + def record(self, deployed: DeployedEnv) -> None: """Keep each computer's own record in ``deployed``, the multi env's, before it is registered: whole in its ``child_envs``, for ``reattach``, and its sandbox in its ``sandbox_ids`` under the backend that made it, for teardown.""" diff --git a/src/agent_env/env/envs/computer/env.py b/src/agent_env/env/envs/computer/env.py index 010b86b3d..5abca9a85 100644 --- a/src/agent_env/env/envs/computer/env.py +++ b/src/agent_env/env/envs/computer/env.py @@ -38,7 +38,7 @@ from agent_env.env.envs.computer.utils import params as computer_params from agent_env.env.envs.computer.driver_env import ComputerDriverEnv from agent_env.env.gateway import GatewayMode -from agent_env.env.store import get_env_store +from agent_env.env.store import get_env_store, update_env_instance_metadata from agent_env.store.local_state import awaiting_lock from agent_env.utils.progress import current_progress from agent_env.providers.env_providers.env_computer_provider import end_recordings @@ -278,14 +278,23 @@ async def deploy(self, ttl_seconds: int = 10800, disk_size_gb: float = 10, gateway_mode: GatewayMode = GatewayMode.PERFORMANCE, cpu: float | None = None, memory_mb: int | None = None, sandbox_type: str | None = None, env_state_type: str | None = None, env_state_instance_id: str | None = None, *, attribution: Optional[Attribution] = None, - env_params: Optional[dict] = None, tool_prefix: str = "", reach: str = "") -> DeployedEnv: - """With ``tool_prefix``, its driver serves its tools under it, and with ``reach``, a network that restricts its - egress allows that URL's host too: what a multi env gives each of its computers.""" + env_params: Optional[dict] = None, tool_prefix: str = "", reach: str = "", + sites_later: bool = False) -> DeployedEnv: + """With ``tool_prefix``, its driver serves its tools under it, with ``reach``, a network that restricts its + egress allows that URL's host too, and with ``sites_later`` it routes the sites ``env_params.site_routing`` + reaches when ``route_sites`` gives them: what a multi env gives each of its computers.""" return await deploy_through_provider( self, environment_name=self.environment_name, ttl_seconds=ttl_seconds, sandbox_type=sandbox_type, then=self._give_tools, disk_size_gb=disk_size_gb, gateway_mode=gateway_mode, cpu=cpu, memory_mb=memory_mb, env_state_type=env_state_type, env_state_instance_id=env_state_instance_id, attribution=attribution, - env_params=env_params, tool_prefix=tool_prefix, reach=reach) + env_params=env_params, tool_prefix=tool_prefix, reach=reach, sites_later=sites_later) + + async def route_sites(self, sites: list[dict]) -> None: + """Route ``sites`` on this computer, deployed with ``sites_later``, launch its held apps and finish its boot; + its record, and the instance store's, then keep them.""" + await as_builtin(self._env_provider).route_sites(self, self._deployed, sites) + if self._deployed.instance_id: + update_env_instance_metadata(self._deployed.instance_id, {"computer": self._deployed.metadata["computer"]}) async def run_env_params(self, env_params: Optional[dict], deployed_envs: list[DeployedEnv]) -> Optional[dict]: """With ``site_routing`` and no ``sites``, ``env_params`` naming as its sites the websites the run deployed diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index fc78e2a34..3820c476b 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -24,6 +24,7 @@ import re import shlex import time +from collections.abc import Collection from dataclasses import dataclass, field from datetime import UTC, datetime, timedelta from pathlib import Path, PurePosixPath @@ -284,10 +285,15 @@ def trust_files(os_type: str) -> dict[str, str]: return {"ca": environment["NODE_EXTRA_CA_CERTS"], "bundle": environment["SSL_CERT_FILE"]} -def trust_environment(os_type: str, sites: list[Site]) -> dict[str, str]: +def trust_environment(os_type: str, sites: list[Site], *, later: bool = False) -> dict[str, str]: """The variables naming the trust agentenv adds, for an ``os_type`` computer routing ``sites``: none when every name - is served over plain HTTP, as a ``.localhost`` one is, since no CA is made then to write the files they'd name.""" - return dict(TRUST_ENVIRONMENTS[os_type]) if any(route.tls_names for route in site_table(sites).routes) else {} + is served over plain HTTP, as a ``.localhost`` one is, since no CA is made then to write the files they'd name. + With ``later``, a computer booting before the sites it routes are known, every one: the apps it holds launch after + routing, with the variables it booted with, and its base holds the files from boot, the public roots and no CA + until routing adds one.""" + if later or any(route.tls_names for route in site_table(sites).routes): + return dict(TRUST_ENVIRONMENTS[os_type]) + return {} def browser_environment(display: str) -> dict[str, str]: @@ -465,7 +471,8 @@ def routing_problems(report: dict) -> list[str]: f"{forward['error']}"] if "error" in forward else [])) -async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[str]] = None) -> list[dict]: +async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[str]] = None, *, + services: Optional[Collection[str]] = None) -> list[dict]: """The websites a run has deployed, as ``env_params.sites`` names them: each ``website/v1`` child env of a deployed env's stored card, with the route its composer serves it to a browser at (its params' ``browse_url``, joined onto the env's address), and its names: those its env stores (a website env's ``domains``), else those its params @@ -473,10 +480,13 @@ async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[s its ``website_frontend_urls``. A site those name that the stored card doesn't hold, every site of a record without a card or one whose card the gateway couldn't read when it composed its own, is read live at ``/svc/`` unless its env stores its names, and has no names when it serves no card (404 or no JSON). Reading one fails - otherwise, after ``CARD_ATTEMPTS`` tries, rather than route the site by names it didn't declare. - ``only`` keeps the websites of the envs it names, each of which must be deployed. Sites of different envs that - share a name are each named for their env (``_env_qualified``), so they're routed apart, while two that claim one - domain are still refused.""" + otherwise, after ``CARD_ATTEMPTS`` tries, rather than route the site by names it didn't declare. An env's sites are + named by their cards unless two of its cards share a name, and then each by the name it's served as + (``_served_names``). + ``only`` keeps the websites of the envs it names, each of which must be deployed, and ``services`` those served + as the names it holds, each website's environment_name, which its card's name needn't be. Sites of different + envs that share a name are each named for their env (``_env_qualified``), so they're routed apart, while two + that claim one domain are still refused.""" from agentenv_protocol import client as protocol_v1 from agentenv_protocol.website import WEBSITE_BROWSE_URL_PARAM, WEBSITE_EXTENSION_URI @@ -495,10 +505,14 @@ async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[s if child.find_extension(WEBSITE_EXTENSION_URI) is not None and child.name] stored = _stored_domains(record) if children or frontends else {} named = set() + own: list[tuple[str, dict]] = [] # the env's sites, each with the name it's served as for child in children: params = protocol_v1.extension_params(child.card, WEBSITE_EXTENSION_URI) browse = params.get(WEBSITE_BROWSE_URL_PARAM) service = _served_as(child) + if services is not None and service not in services: + named.add(service) + continue url = (_joined(address, browse) if isinstance(browse, str) and browse and address else frontends.get(service)) if url is None: @@ -506,13 +520,14 @@ async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[s "isn't routed", child.name, record.env_id) continue named.add(service) - found.append((record.env_id, _found(child.name, url, params, stored.get(service)))) + own.append((service, _found(child.name, url, params, stored.get(service)))) for name, url in frontends.items(): - if name not in named: + if name not in named and (services is None or name in services): card = {} if name in stored else await _site_card(protocol_v1, f"{gateway_url_of(record)}/svc/{name}", name) params = protocol_v1.extension_params(card, WEBSITE_EXTENSION_URI) - found.append((record.env_id, _found(name, url, params, stored.get(name)))) + own.append((name, _found(name, url, params, stored.get(name)))) + found += [(record.env_id, site) for site in _served_names(own)] return _env_qualified(found) @@ -554,6 +569,16 @@ def serves_websites(record: DeployedEnv) -> bool: child.find_extension(WEBSITE) is not None and child.name for child in protocol_client.children(card)) +def _served_names(sites: list[tuple[str, dict]]) -> list[dict]: + """One env's ``sites``, each with the name it's served as: named by their cards, unless two cards share a name, as + two websites of one image whose cards name it can; then each is named as it's served, its environment_name, which + the env's gateway gives each a route of its own by.""" + names = [site["name"] for _, site in sites] + if len(set(names)) == len(names): + return [site for _, site in sites] + return [{**site, "name": service} for service, site in sites] + + def _env_qualified(found: list[tuple[str, dict]]) -> list[dict]: """Each site found, a name that sites of different envs share made ``-`` for each of them, letters, digits, ``-`` and ``_`` kept, so every site of the run has a name of its own.""" diff --git a/src/agent_env/env/envs/multi_env.py b/src/agent_env/env/envs/multi_env.py index fe5b4d27c..a4a66ced3 100644 --- a/src/agent_env/env/envs/multi_env.py +++ b/src/agent_env/env/envs/multi_env.py @@ -205,8 +205,8 @@ async def deploy(self, ttl_seconds: int = 10800, disk_size_gb: float = 10, gatew raise ValueError(refusal) self._computers.begin() computers = dict(children=self._computers.deployer(child_envs, sandbox_type=sandbox_type, ttl_seconds=ttl_seconds, - attribution=attribution), - then=self._computers.record) if self.computer_envs else {} + attribution=attribution, websites=self._websites()), + then=self._computers.finish) if self.computer_envs else {} try: deployed_env = await deploy_through_provider( self, environment_name=None, ttl_seconds=ttl_seconds, sandbox_type=sandbox_type, @@ -224,6 +224,10 @@ async def deploy(self, ttl_seconds: int = 10800, disk_size_gb: float = 10, gatew _hand_record_to_child_envs(self, deployed_env) return deployed_env + def _websites(self) -> dict[str, str]: + """Its websites' environment names by env id, the ids a computer's ``site_routing`` may name.""" + return {env.id: env.environment_name for env in self.website_envs} + def save_refusal(self) -> str | None: """Why saving the env would be refused, or None: its env_provider_type can't deploy it. How each computer fits a sandbox is a deploy's question, as it is for a computer saved alone (``deploy_refusal``).""" @@ -259,7 +263,7 @@ def child_envs_refusal(self, child_envs: Optional[dict], *, sandbox_type: str | for name, computer in computers.items(): entry = child_envs.get(name) if entry is None or (isinstance(entry, dict) and set(entry) <= set(CHILD_ENV_OPTIONS)): - if refusal := _multi_computers.refusal(computer, entry or {}, sandbox_type): + if refusal := _multi_computers.refusal(computer, entry or {}, sandbox_type, self._websites()): problems.append(f"child_envs.{name}: {refusal}" if name in child_envs else refusal) return "; ".join(problems) or None diff --git a/src/agent_env/providers/env_providers/env_computer_provider.py b/src/agent_env/providers/env_providers/env_computer_provider.py index 308585643..311d2d3da 100644 --- a/src/agent_env/providers/env_providers/env_computer_provider.py +++ b/src/agent_env/providers/env_providers/env_computer_provider.py @@ -62,6 +62,10 @@ class EnvironmentComputerProvider(_SandboxEnvironmentProvider): type: ClassVar[str] = "computer" record_class = DeployedSandboxEnv + def __init__(self) -> None: + super().__init__() + self._later: dict[str, tuple[Sandbox, int]] = {} # each computer booted with sites_later: its sandbox and CA lifetime + async def deploy( self, env: Env, @@ -75,11 +79,14 @@ async def deploy( env_params: Optional[dict] = None, tool_prefix: str = "", reach: str = "", + sites_later: bool = False, ) -> DeployedSandboxEnv: """The env's first ready build, in its base's order of forms and arches (or of ``env_params.form``), that the sandbox provider runs natively, booted, once what it boots that no compose started is composed; returns the record once the driver is ready with the env's display. With ``tool_prefix``, its driver serves its tools under it; with ``reach``, - a URL this process reaches, a network that restricts the computer's egress allows its host too.""" + a URL this process reaches, a network that restricts the computer's egress allows its host too. With + ``sites_later``, a computer routing sites, whose sites aren't up yet, boots with its boot apps held and returns + once its driver answers; ``route_sites`` routes them and finishes its boot.""" from agent_env.env.envs.computer.env import ComputerEnv if not isinstance(env, ComputerEnv): @@ -91,7 +98,7 @@ async def deploy( problems.append(problem) if problems: raise DeployRefused(f"{env.id}: " + "; ".join(problems)) - if params.site_routing and not params.sites: + if params.site_routing and not params.sites and not sites_later: raise DeployRefused(f"{env.id}: env_params.site_routing reaches the run's websites, which a run's " "deploy_env names in env_params.sites; give them there to deploy it any other way") await env.compose_unstarted(sandbox_provider, params.form) @@ -111,16 +118,29 @@ async def deploy( "computer runs inside a launcher container it can't reach; deploy a container form") attempt = functools.partial(self._boot, env, builds, params, values, ttl_seconds=ttl_seconds, disk_size_gb=disk_size_gb, cpu=cpu, memory_mb=memory_mb, attribution=attribution, - tool_prefix=tool_prefix, reach=reach) + tool_prefix=tool_prefix, reach=reach, sites_later=sites_later) return await self._run(sandbox_provider, env.id, attempt) + async def route_sites(self, env: ComputerEnv, record: DeployedSandboxEnv, sites: list[dict]) -> None: + """Route ``sites`` on the computer ``record`` names, which this provider booted with ``sites_later``, launch its + held apps and finish its boot as a deploy does; its record then keeps them, so a reset routes them again.""" + sandbox, valid_for_s = self._later.pop(record.sandbox_id) + computer = record.metadata["computer"] + params, _, problems = computer_params.checked(env, {**computer["env_params"], "sites": sites}) + if problems: + raise ValueError(f"{env.id}: " + "; ".join(problems)) + computer["env_params"] = params.model_dump(exclude_defaults=True) + if routed := await _finish_boot(env, record, sandbox, params, valid_for_s, computer.get("display")): + computer["sites"] = routed + async def install_changelog_triggers(self, environment_name: str) -> None: """No state store, so no changelog to install.""" async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], params: computer_params.ComputerParams, values: dict, sandbox_provider: SandboxProvider, *, ttl_seconds: int, disk_size_gb: float, cpu: float | None, memory_mb: int | None, - attribution: Optional[Attribution], tool_prefix: str = "", reach: str = "") -> DeployedSandboxEnv: + attribution: Optional[Attribution], tool_prefix: str = "", reach: str = "", + sites_later: bool = False) -> DeployedSandboxEnv: """One attempt on one sandbox provider: the first build it runs natively, booted and readied; its record.""" from agent_env.env.envs.computer.env import ComputerEnv @@ -148,7 +168,8 @@ async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], par policy = _reaching(NetworkPolicy.from_dict(env.network) if env.network else None, sandbox_provider, reach) booted = await boot( sandbox_provider, image=build["image"], port=env.driver.port, network_policy=policy, token=token, - environment=_environment(env, computer, values, params, tool_prefix), timeout=ttl_seconds, + environment=_environment(env, computer, values, params, tool_prefix, sites_later=sites_later), + timeout=ttl_seconds, attribution=attribution, disk_size_gb=disk_size_gb, cpu=cpu, memory_mb=memory_mb, ready_s=ready_timeout(env.composed_apps(), env.computer.os.type), form=form, held=bool(params.site_routing), @@ -172,7 +193,9 @@ async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], par **({"tool_prefix": tool_prefix} if tool_prefix else {}), "env_params": params.model_dump(exclude_defaults=True)}}, ) - if routed := await _finish_boot(env, record, booted.sandbox, params, ttl_seconds, values.get("resolution")): + if sites_later: + self._later[record.sandbox_id] = (booted.sandbox, ttl_seconds) + elif routed := await _finish_boot(env, record, booted.sandbox, params, ttl_seconds, values.get("resolution")): record.metadata["computer"]["sites"] = routed except BaseException: await discard(booted.sandbox) @@ -323,12 +346,14 @@ def _values(computer: ComputerArtifact, values: dict) -> dict: def _environment(env: ComputerEnv, computer: ComputerArtifact, values: dict, - params: computer_params.ComputerParams, tool_prefix: str = "") -> dict[str, str]: + params: computer_params.ComputerParams, tool_prefix: str = "", *, + sites_later: bool = False) -> dict[str, str]: return {**settings_environment(computer, values, environment_name=env.computer_name, driver_settings=params.driver_settings, tool_prefix=tool_prefix), **app_settings_environment(env.composed_apps(), computer_params.app_settings(env, params.app_settings), env.computer.os.type), - **({HOLD_BOOT_LAUNCH_ENV: "1", **sites.trust_environment(env.computer.os.type, params.sites), + **({HOLD_BOOT_LAUNCH_ENV: "1", **sites.trust_environment(env.computer.os.type, params.sites, + later=sites_later), **sites.browser_environment(env.computer.os.display)} if params.site_routing else {})} diff --git a/tst/integration/computer/multi_env_computers_test.py b/tst/integration/computer/multi_env_computers_test.py index e349ee2b9..c30f63ed0 100644 --- a/tst/integration/computer/multi_env_computers_test.py +++ b/tst/integration/computer/multi_env_computers_test.py @@ -10,7 +10,10 @@ import asyncio import json import re +import shutil +import tempfile import uuid +from pathlib import Path import httpx import pytest @@ -21,13 +24,16 @@ from mcp import ClientSession from mcp.client.streamable_http import streamable_http_client -from agent_env.artifact import EnvironmentArtifact, EnvironmentUniverseArtifact, FileArtifact +from agent_env.artifact import DockerImageArtifact, EnvironmentArtifact, EnvironmentUniverseArtifact, FileArtifact +from agent_env.config import get_config, website_browser_env_id from agent_env.env import Env from agent_env.env.env import DeployedEnv from agent_env.env.envs.computer.env import COMPUTER_TOOLS, ComputerEnv from agent_env.env.envs.computer.utils import inside from agent_env.env.envs.mcp_server import MCPServerEnv from agent_env.env.envs.multi_env import MultiEnv +from agent_env.env.envs.playwright_mcp import PLAYWRIGHT_MCP_ENVIRONMENT_NAME +from agent_env.env.envs.website import WebsiteEnv from agent_env.providers.env_providers.env_gateway_provider import EnvironmentGatewayProvider from agent_env.providers.sandbox_providers.local_docker.provider import LocalSandboxProvider from agent_env.store.routing import namespace_routing @@ -57,9 +63,48 @@ def items(desktop): docker_image_artifact=_put_items_image(f"office-items-{uid}")) -def _multi(items, *computers) -> MultiEnv: +def _image(fixture: str, tag: str) -> DockerImageArtifact: + """The image of the ``tst/data`` server ``fixture``, built with this checkout's agentenv_protocol.""" + repo = Path(__file__).resolve().parents[3] + with tempfile.TemporaryDirectory() as build_dir: + context = Path(build_dir) + shutil.copytree(repo / "packages" / "agentenv-protocol" / "src" / "agentenv_protocol", context / "agentenv_protocol", + ignore=shutil.ignore_patterns("__pycache__")) + for name in ("server.py", "Dockerfile"): + shutil.copy(repo / "tst" / "data" / fixture / name, context / name) + built = _docker("build", "-t", tag, str(context)) + assert built.returncode == 0, built.stderr[-2000:] + return DockerImageArtifact.put(id=tag, description="multi env sites e2e", image_name=tag) + + +@pytest.fixture(scope="module") +def shop(items): + """A website whose backend is the small agentenv website, named shop.test, under a local id as ``items`` is. A gateway + in front of websites also runs the website browser's MCP server; a small one stands in for it, since only the + routing of the site is under test here.""" + uid = uuid.uuid4().hex[:8] + with namespace_routing(): + MCPServerEnv.put(id=website_browser_env_id(get_config()), environment_name=PLAYWRIGHT_MCP_ENVIRONMENT_NAME, + docker_image_artifact=_image("cardless_mcp", f"office-browser-{uid}")) + return WebsiteEnv.put(id=f"{items.id.rsplit('/', 1)[0]}/shop", environment_name="shop", domains=["shop.test"], + backend_docker_image_artifact=_image("agentenv_website", f"office-shop-{uid}")) + + +def _multi(items, *computers, websites=()) -> MultiEnv: return MultiEnv.put(id=f"@local/~/office-{uuid.uuid4().hex[:8]}/office", mcp_server_envs=[items], - computer_envs=list(computers), name="office") + website_envs=list(websites), computer_envs=list(computers), name="office") + + +LOAD_TIMEOUT_S = 20 # a site's answer inside the computer +RUN_TIMEOUT_S = LOAD_TIMEOUT_S + 20 # the command that loads it, which must outlast the load + + +async def _reaches(computer, url: str) -> str: + """The HTTP status ``url`` answers inside the computer, through its site proxy, over HTTPS, trusting the deploy's CA + from the system's store.""" + load = f"import sys, urllib.request; print(urllib.request.urlopen(sys.argv[1], timeout={LOAD_TIMEOUT_S}).status)" + answer = await inside.run(computer, ["python3", "-c", load, url], timeout_s=RUN_TIMEOUT_S) + return answer["stdout"].strip() or answer.get("stderr", "")[-300:] async def _tools(deployed) -> tuple[set[str], bool]: @@ -128,11 +173,12 @@ def _gateway(deployed) -> str: @pytest.mark.asyncio -async def test_a_computer_in_a_multi_env_is_served_through_its_gateway_under_its_name_and_torn_down_with_it(desktop, items): +async def test_a_computer_in_a_multi_env_is_served_through_its_gateway_under_its_name_and_torn_down_with_it(desktop, items, + shop): with namespace_routing(): - office = _multi(items, Env.get(desktop.id)) + office = _multi(items, Env.get(desktop.id), websites=[shop]) step = DeployEnvTaskStep(id="office", version=None, env_id=office.id, sandbox_type="local", ttl_seconds=900, - child_envs={"desktop": CONTAINER}) + child_envs={"desktop": {**CONTAINER, "env_params": {"form": "container", "site_routing": True}}}) assert step.preflight() == [] context = TaskStepContext() try: @@ -143,6 +189,7 @@ async def test_a_computer_in_a_multi_env_is_served_through_its_gateway_under_its card = (await client.get(f"{deployed.gateway_url}/svc/desktop{WELL_KNOWN_PATH}")).json() # Load, snapshot and reset reach the computer through the multi env, beside its MCP server. uid, own = uuid.uuid4().hex[:8], DeployedEnv.from_dict(computer) + routed = await _reaches(own, "https://shop.test/api/health") world = _universe(uid, {"Documents/note.txt": b"from the universe"}) await LoadArtifactTaskStep(id="seed", version=None, env_id=office.id, artifact_id=world.id).execute(context) note = (await inside.run(own, ["cat", "Documents/note.txt"], timeout_s=30))["stdout"] @@ -154,6 +201,7 @@ async def test_a_computer_in_a_multi_env_is_served_through_its_gateway_under_its after_reset = (await inside.run(own, ["sh", "-c", "test -e Documents/note.txt && echo kept || echo gone"], timeout_s=30))["stdout"].strip() reset_answers = await _answers(deployed, ["desktop_computer_screenshot"]) + routed_after_reset = await _reaches(own, "https://shop.test/api/health") emptied_items = await _items(deployed) # A reset empties the server too, so the next attempt loads again, every service and the computer. await LoadArtifactTaskStep(id="reseed", version=None, env_id=office.id, artifact_id=world.id).execute(context) @@ -183,6 +231,8 @@ async def test_a_computer_in_a_multi_env_is_served_through_its_gateway_under_its assert note == "from the universe" and "snap-x" in listed_items assert {"items", "desktop"} <= set(snapshot) and snapshot["desktop"]["Documents/note.txt"] == b"from the universe" assert after_reset == "gone" and all(reset_answers.values()), reset_answers + assert computer["metadata"]["computer"]["sites"]["names"]["routes"] == {"shop": ["shop.test"]} + assert routed == routed_after_reset == "200" assert "snap-x" not in emptied_items, emptied_items assert reloaded[0] == "from the universe" and "snap-x" in reloaded[1], reloaded @@ -265,8 +315,7 @@ async def test_a_computer_entry_the_multi_env_refuses_makes_no_sandbox(desktop, office = _multi(items, Env.get(desktop.id)) refused = [ ({"desktop": {**CONTAINER, "env_params": {"site_routing": True}}}, - "child_envs.desktop: env_params.site_routing: a computer in a multi env doesn't route sites yet; deploy it " - "beside the multi env to route them"), + "child_envs.desktop: env_params.site_routing routes the multi env's websites, and it has none"), ({"desktop": {"tools": "standard"}}, "child_envs.desktop: an entry is a table of sandbox_type, cpu, memory_mb, disk_size_gb, env_params, not tools"), ] diff --git a/tst/unit/env/envs/test_computer_env.py b/tst/unit/env/envs/test_computer_env.py index d3896e80c..9cc0cddbb 100644 --- a/tst/unit/env/envs/test_computer_env.py +++ b/tst/unit/env/envs/test_computer_env.py @@ -44,6 +44,7 @@ from agent_env.env.env import DeployedEnv, EnvCapabilityUnsupported from agent_env.env.envs.computer.utils import (boot, builder, composed, composer, gate, inside, packages, params, probing, resolve_in_base) +from agent_env.env.envs.computer.utils import sites as computer_sites from agent_env.env.envs.computer.utils.composer import Composing from agent_env.env.envs.computer.utils.probing import _probe from agent_env.env.envs.computer.env import COMPUTER_TOOLS, ComputerEnv @@ -1034,6 +1035,35 @@ async def test_a_computer_with_site_routing_reaches_the_websites_the_run_deploye assert record["env_params"]["site_routing"] is True and record["env_params"]["sites"][0]["name"] == "notes" +@pytest.mark.asyncio +async def test_a_computer_deployed_to_route_its_sites_later_boots_held_and_routes_them_when_given_as_a_multi_env_does( + base, driver, docker, boots, routes): + env = Env.get(_put(base, driver, display={"width": 1600, "height": 900}).id) + + deployed = await env.deploy(ttl_seconds=3600, env_params={"site_routing": True}, sites_later=True) + held, routed_before = boots.boots[0]["environment"][HOLD_BOOT_LAUNCH_ENV], list(routes) + await env.route_sites([{"name": "notes", "url": "http://localhost:8080/website/notes/", "domains": ["notes.test"]}]) + + assert held == "1" and routed_before == [] + # The trust its held apps launch with, which a boot knowing its sites would give it, before they're known. + assert boots.boots[0]["environment"].items() >= computer_sites.TRUST_ENVIRONMENTS["linux"].items() + ((sandbox, sites, options),) = routes + assert sandbox is boots.sandboxes[0] and [site.name for site in sites] == ["notes"] + assert options["valid_for"] == timedelta(seconds=3600) + record = deployed.metadata["computer"] + assert record["env_params"]["sites"][0]["name"] == "notes" and record["sites"]["valid_for_s"] == 3600 + + +@pytest.mark.asyncio +async def test_a_computer_given_its_sites_later_still_refuses_sites_its_params_cant_hold(base, driver, docker, boots, routes): + env = Env.get(_put(base, driver, display={"width": 1600, "height": 900}).id) + await env.deploy(env_params={"site_routing": True}, sites_later=True) + + with pytest.raises(ValueError, match="sites"): + await env.route_sites([{"name": "notes"}]) + assert routes == [] + + @pytest.mark.asyncio @pytest.mark.parametrize("routing", ["web-env", 1, ["web-env", 2], {"web-env": True}]) async def test_a_site_routing_that_is_neither_true_nor_a_list_of_env_ids_is_refused_before_it_is_read( diff --git a/tst/unit/env/envs/test_computer_sites.py b/tst/unit/env/envs/test_computer_sites.py index 0cf8c20b7..4bc79bf62 100644 --- a/tst/unit/env/envs/test_computer_sites.py +++ b/tst/unit/env/envs/test_computer_sites.py @@ -475,6 +475,38 @@ async def get_card(url, timeout=10, verify=True): assert await sites.deployed_sites([gateway]) == [ {"name": "Notes", "url": "https://gw.example/website/notes/", "domains": ["notes.test"]}] + # Selected by the name it's served as, its environment_name, not its card's. + assert await sites.deployed_sites([gateway], services=["notes"]) == await sites.deployed_sites([gateway]) + assert await sites.deployed_sites([gateway], services=["Notes"]) == [] + + +@pytest.mark.asyncio +async def test_two_websites_of_one_env_whose_cards_share_a_name_are_each_named_as_theyre_served(monkeypatch): + from agentenv_protocol import client as protocol_v1 + + from agent_env.env.env import DeployedGatewayEnv + + async def get_card(url, timeout=10, verify=True): + raise AssertionError(f"read {url} live") + + monkeypatch.setattr(protocol_v1, "get_card", get_card) + + def child(served: str, domain: str) -> dict: + return {"name": "Site", "url": f"/svc/{served}/agentenv", "capabilities": { + "extensions": [{"uri": "urn:agentenv:website/v1", "params": {"domains": [domain]}}]}} + + card = {"name": "env", "children_environments": [child("shop", "shop.test"), child("blog", "blog.test")]} + gateway = DeployedGatewayEnv(env_id="sites", env_version=1, sandbox_id="s", gateway_url="https://gw.example", + environment_card=card, + website_frontend_urls={name: f"https://gw.example/website/{name}/" for name in ("shop", "blog")}) + + found = await sites.deployed_sites([gateway]) + + assert found == [{"name": "shop", "url": "https://gw.example/website/shop/", "domains": ["shop.test"]}, + {"name": "blog", "url": "https://gw.example/website/blog/", "domains": ["blog.test"]}] + assert len(sites.site_table([Site(**site) for site in found]).routes) == 2 # names of their own: both route + assert await sites.deployed_sites([gateway], services=["blog"]) == [ + {"name": "Site", "url": "https://gw.example/website/blog/", "domains": ["blog.test"]}] # alone, its card's name @pytest.mark.asyncio diff --git a/tst/unit/env/envs/test_multi_env_computers.py b/tst/unit/env/envs/test_multi_env_computers.py index 50d112519..4a8a92683 100644 --- a/tst/unit/env/envs/test_multi_env_computers.py +++ b/tst/unit/env/envs/test_multi_env_computers.py @@ -7,6 +7,7 @@ import asyncio import dataclasses import inspect +import re from types import SimpleNamespace from unittest.mock import AsyncMock, MagicMock, patch @@ -43,11 +44,12 @@ class FakeComputer: type = COMPUTER_ENV_TYPE def __init__(self, name: str, *, fails: Exception | None = None, refusal: str | None = None, log: list | None = None, - apps: tuple[str, ...] = (), resets: Exception | None = None, loads: Exception | None = None): + apps: tuple[str, ...] = (), resets: Exception | None = None, routes: Exception | None = None, + loads: Exception | None = None): self.id, self.version, self.environment_name = f"{name}-computer", 1, name self.fails, self.refusal, self.log = fails, refusal, [] if log is None else log self.deploys, self.refusals, self._deployed, self.closes = [], [], None, 0 - self.apps, self.reset_fails, self.load_fails = apps, resets, loads + self.apps, self.reset_fails, self.route_fails, self.load_fails = apps, resets, routes, loads def composed_apps(self): return [SimpleNamespace(environment_name=app) for app in self.apps] @@ -66,6 +68,12 @@ async def load_environment_artifact(self, environment_artifact): inspect.signature(ComputerEnv.load_environment_artifact).bind(self, environment_artifact) self.log.append(("loaded", self.environment_name, [environment_artifact.environment_name])) + async def route_sites(self, sites): + inspect.signature(ComputerEnv.route_sites).bind(self, sites) + self.log.append(("routed", self.environment_name, [site["name"] for site in sites])) + if self.route_fails: + raise self.route_fails + async def reset(self, deployed): inspect.signature(ComputerEnv.reset).bind(self, deployed) await asyncio.sleep(0.01) @@ -101,7 +109,7 @@ async def from_deployed_env(cls, deployed): def _multi(*computers, mcp=(), websites=(), **kwargs) -> MultiEnv: return MultiEnv(id="office", version=1, mcp_server_envs=[MagicMock(environment_name=name) for name in mcp], - website_envs=[MagicMock(environment_name=name, domains=[]) for name in websites], + website_envs=[MagicMock(id=f"{name}-site", environment_name=name, domains=[]) for name in websites], computer_envs=list(computers), **kwargs) @@ -169,8 +177,8 @@ def test_each_computer_is_checked_as_it_would_deploy_alone_on_its_entrys_sandbox refusal = env.deploy_refusal(sandbox_type="modal", child_envs={ "desktop": {"sandbox_type": "modal_vm", "cpu": 4, "env_params": {"form": "disk"}}}) - assert desktop.refusals == [{"sandbox_type": "modal_vm", "cpu": 4, "env_params": {"form": "disk"}}] - assert laptop.refusals == [{"sandbox_type": "modal"}] + assert desktop.refusals == [{"sandbox_type": "modal_vm", "cpu": 4, "env_params": {"form": "disk"}, "sites_later": False}] + assert laptop.refusals == [{"sandbox_type": "modal", "sites_later": False}] assert refusal == "computer env 'laptop-computer': no arch" laptop.refusal = None assert env.child_envs_refusal({"laptop": {"memory_mb": 8192}}) is None @@ -183,21 +191,45 @@ def test_saving_a_multi_env_leaves_how_each_computer_fits_a_sandbox_to_a_deploy_ assert _multi(desktop).save_refusal() is None and desktop.refusals == [] -@pytest.mark.parametrize("child_envs, problem", [ - ({"desktop": {"env_params": {"site_routing": True}}}, "child_envs.desktop: env_params.site_routing: a computer in a multi env " - "doesn't route sites yet; deploy it beside the multi env to route them"), - ({"desktop": {"env_params": {"sites": [{"name": "shop"}]}}}, "child_envs.desktop: env_params.sites: a computer in a multi " - "env doesn't route sites yet; deploy it beside the multi env to " - "route them"), - ({"desktop": {"tools": "standard"}}, "child_envs.desktop: an entry is a table of sandbox_type, cpu, memory_mb, disk_size_gb, " - "env_params, not tools"), - ({"slack": {"cpu": 4}}, "child_envs.slack: slack runs on the multi env's own sandbox, which the step's own fields set, so " - "it takes no entry"), -], ids=["site-routing", "sites", "unknown-key", "mcp-server"]) -def test_a_computer_entry_that_routes_sites_or_holds_what_a_step_doesnt_take_is_refused(providers, child_envs, problem): +@pytest.mark.parametrize("websites, child_envs, problem", [ + ((), {"desktop": {"env_params": {"site_routing": True}}}, + "child_envs.desktop: env_params.site_routing routes the multi env's websites, and it has none"), + (("shop",), {"desktop": {"env_params": {"site_routing": ["shop-site", "blog-site"]}}}, + "child_envs.desktop: env_params.site_routing names 'blog-site', which isn't one of the multi env's websites ('shop-site')"), + ((), {"desktop": {"tools": "standard"}}, "child_envs.desktop: an entry is a table of sandbox_type, cpu, memory_mb, " + "disk_size_gb, env_params, not tools"), + ((), {"slack": {"cpu": 4}}, "child_envs.slack: slack runs on the multi env's own sandbox, which the step's own fields set, " + "so it takes no entry"), +], ids=["site-routing-without-websites", "site-routing-naming-another", "unknown-key", "mcp-server"]) +def test_a_computer_entry_whose_sites_the_multi_env_lacks_or_that_holds_what_a_step_doesnt_take_is_refused( + providers, websites, child_envs, problem): + desktop = FakeComputer("desktop") + assert _multi(desktop, mcp=["slack"], websites=websites).child_envs_refusal(child_envs) == problem + assert desktop.refusals == ([] if "desktop" in child_envs else [{"sandbox_type": None, "sites_later": False}]) + + +def test_env_params_that_arent_a_table_are_left_to_the_computers_own_check_to_refuse(providers): + desktop = FakeComputer("desktop", refusal="computer env 'desktop-computer': env_params is a table") + refusal = _multi(desktop, websites=["shop"]).child_envs_refusal({"desktop": {"env_params": "invalid"}}) + assert refusal == "child_envs.desktop: computer env 'desktop-computer': env_params is a table" + assert desktop.refusals == [{"sandbox_type": None, "env_params": "invalid", "sites_later": False}] + + +def test_a_site_routing_list_of_other_than_ids_is_left_to_the_computers_own_check_to_refuse(providers): + desktop = FakeComputer("desktop", refusal="computer env 'desktop-computer': site_routing is a list of names") + env_params = {"site_routing": [["shop-site"]]} + refusal = _multi(desktop, websites=["shop"]).child_envs_refusal({"desktop": {"env_params": env_params}}) + assert refusal == "child_envs.desktop: computer env 'desktop-computer': site_routing is a list of names" + assert desktop.refusals == [{"sandbox_type": None, "env_params": env_params, "sites_later": True}] + + +@pytest.mark.parametrize("env_params, later", [({"site_routing": True}, True), ({"site_routing": ["shop-site"]}, True), + ({"site_routing": True, "sites": [{"name": "shop"}]}, False)], + ids=["all", "named", "its-own-sites"]) +def test_a_computer_routing_the_multi_envs_websites_is_checked_as_routing_them_later(providers, env_params, later): desktop = FakeComputer("desktop") - assert _multi(desktop, mcp=["slack"]).child_envs_refusal(child_envs) == problem - assert desktop.refusals == ([] if "desktop" in child_envs else [{"sandbox_type": None}]) # a refused entry deploys nothing + assert _multi(desktop, websites=["shop"]).child_envs_refusal({"desktop": {"env_params": env_params}}) is None + assert desktop.refusals == [{"sandbox_type": None, "env_params": env_params, "sites_later": later}] def test_a_computer_on_a_sandbox_provider_whose_urls_take_credentials_is_refused(providers): @@ -257,9 +289,9 @@ async def test_deploy_hands_the_gateway_each_computer_deployed_under_its_name_an child_envs={"desktop": {"sandbox_type": "modal", "cpu": 4, "env_params": {"form": "container"}}}) assert desktop.deploys == [{"ttl_seconds": 600, "attribution": attribution, "tool_prefix": "desktop_", "reach": GATEWAY, - "sandbox_type": "modal", "cpu": 4, "env_params": {"form": "container"}}] + "sites_later": False, "sandbox_type": "modal", "cpu": 4, "env_params": {"form": "container"}}] assert laptop.deploys == [{"ttl_seconds": 600, "attribution": attribution, "tool_prefix": "laptop_", "reach": GATEWAY, - "sandbox_type": "modal_vm"}] + "sites_later": False, "sandbox_type": "modal_vm"}] assert gateway.started == [[ExternalChild("desktop", "https://desktop.example/mcp", "https://desktop.example"), ExternalChild("laptop", "https://laptop.example/mcp", "https://laptop.example")]] assert deployed.child_envs == {name: dataclasses.asdict(_record(name)) for name in ("desktop", "laptop")} @@ -581,3 +613,41 @@ async def test_a_reset_that_cannot_forget_what_loads_put_in_empties_nothing(gate await _multi(FakeComputer("desktop", log=log), mcp=["slack"], websites=["shop"]).reset(stored) assert log == server_resets.log == [] # a resumed load would skip a service a reset had emptied + + +# --- routing the multi env's websites --- + +@pytest.mark.asyncio +async def test_a_computer_routing_sites_boots_held_and_routes_the_multi_envs_websites_once_the_gateway_is_up( + gateway, providers, monkeypatch): + log = gateway.log + found = [{"name": "shop", "url": f"{GATEWAY}/website/shop/"}, {"name": "blog", "url": f"{GATEWAY}/website/blog/"}] + read = AsyncMock(side_effect=lambda records, services=None: log.append(("sites read", records[0].env_id, services)) + or [site for site in found if services is None or site["name"] in services]) + ready = AsyncMock() + monkeypatch.setattr(_multi_computers.sites, "deployed_sites", read) + monkeypatch.setattr(_multi_computers.sites, "require_ready", ready) + desktop, laptop, kiosk = (FakeComputer(name, log=log) for name in ("desktop", "laptop", "kiosk")) + + deployed = await _multi(desktop, laptop, kiosk, websites=["shop", "blog"]).deploy(child_envs={ + "desktop": {"env_params": {"site_routing": True}}, "laptop": {"env_params": {"site_routing": ["blog-site"]}}}) + + assert [deploy["sites_later"] for deploy in (desktop.deploys + laptop.deploys + kiosk.deploys)] == [True, True, False] + assert log[3:5] == [("sites read", "office", None), ("sites read", "office", ["blog"])] # by environment_name + assert [r.env_id for r in ready.await_args.args[0]] == ["office"] + assert sorted(log[5:]) == [("routed", "desktop", ["shop", "blog"]), ("routed", "laptop", ["blog"])] + assert set(deployed.child_envs) == {"desktop", "laptop", "kiosk"} + + +@pytest.mark.asyncio +async def test_a_computer_that_fails_to_route_its_sites_fails_the_deploy_and_closes_everything(gateway, providers, + monkeypatch): + monkeypatch.setattr(_multi_computers.sites, "deployed_sites", AsyncMock(return_value=[{"name": "shop", "url": "u"}])) + monkeypatch.setattr(_multi_computers.sites, "require_ready", AsyncMock()) + desktop = FakeComputer("desktop", log=gateway.log, routes=RuntimeError("shop.test didn't load")) + + with pytest.raises(RuntimeError, match=re.escape( + "multi env 'office': computer 'desktop' failed to route its sites: RuntimeError(\"shop.test didn't load\")")): + await _multi(desktop, websites=["shop"]).deploy(child_envs={"desktop": {"env_params": {"site_routing": True}}}) + + assert gateway.log[-2:] == [("close", "desktop"), ("close", "gateway")]