Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5824772
feat(multi): load, snapshot and reset reach a multi env's computers (…
earakely-scale Nov 8, 2026
7e890e8
fix(multi): a record's computers are served only for its own gateway
earakely-scale Nov 8, 2026
77efe5c
feat(multi): a computer in a multi env routes its websites through th…
earakely-scale Nov 8, 2026
71bddd9
fix(multi): env_params that aren't a table go to the computer's own c…
earakely-scale Nov 8, 2026
e47487d
feat(multi): reset_env empties a multi env's MCP servers and websites…
earakely-scale Nov 9, 2026
aea9766
Merge remote-tracking branch 'origin/edgararakelyan/fd-4935-multi-env…
earakely-scale Nov 9, 2026
88f861c
refactor(multi): one owner per server call, one way to name failed ch…
earakely-scale Nov 9, 2026
c0d3b74
Merge 4a's one-owner, named-failure and store-import refactor; site r…
earakely-scale Nov 9, 2026
6f1b9d3
fix(multi): forget the loaded services before a reset empties any, an…
earakely-scale Nov 9, 2026
649d8ef
Merge remote-tracking branch 'origin/edgararakelyan/fd-4935-multi-env…
earakely-scale Nov 9, 2026
d83d1aa
fix(multi): a universe load reads its environments at most once, and …
earakely-scale Nov 9, 2026
ba30fd4
Merge remote-tracking branch 'origin/edgararakelyan/fd-4935-multi-env…
earakely-scale Nov 9, 2026
2020db2
fix(multi): routed websites are selected by environment_name, held ap…
earakely-scale Nov 9, 2026
877bfb8
fix(multi): an older server's reset tool resets with no seed; reset_e…
earakely-scale Nov 9, 2026
8f41c81
Merge remote-tracking branch 'origin/edgararakelyan/fd-4935-multi-env…
earakely-scale Nov 9, 2026
d649c74
Merge main (#542 squash-merged, #541 forward proxy) into the site rou…
earakely-scale Nov 9, 2026
c8c5915
fix(sites): two websites of one env whose cards share a name are each…
earakely-scale Nov 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions src/agent_env/env/envs/_multi_computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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:]:
Expand All @@ -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()}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
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."""
Expand Down
19 changes: 14 additions & 5 deletions src/agent_env/env/envs/computer/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
47 changes: 36 additions & 11 deletions src/agent_env/env/envs/computer/utils/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -465,18 +471,22 @@ 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
declare. A record whose card names no ``browse_url``, as one a gateway made before it did, takes the route from
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/<name>``
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

Expand All @@ -495,24 +505,29 @@ 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:
logger.warning("site routing: the website %s of env %s names no route a browser reaches it at, so it "
"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)


Expand Down Expand Up @@ -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 ``<env_id>-<name>`` for each of them, letters,
digits, ``-`` and ``_`` kept, so every site of the run has a name of its own."""
Expand Down
10 changes: 7 additions & 3 deletions src/agent_env/env/envs/multi_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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``)."""
Expand Down Expand Up @@ -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

Expand Down
Loading
Loading