Skip to content
24 changes: 19 additions & 5 deletions src/agent_env/bundle/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,8 @@ def _check_multi_names(self, closure: set[_Key], store_latest: dict[tuple[Entity
website as its backend's and frontend's. No two of a multi's envs may name one container, and a provider
other than the core's, which gives a multi one env card, can't tell an MCP server and a website of one name
apart. Nor may two of its websites claim one name. A computer runs on a sandbox of its own, which the gateway
knows by its name alone, and serves its tools under it (``_multi_computers.name_problems``)."""
knows by its name alone, and serves its tools under it (``_multi_computers.name_problems``); its apps' names
are no other child env's, since loads and snapshots find an environment by its name."""
for key in sorted(closure, key=self.rank.__getitem__):
source = self.nodes[key]
if isinstance(source, BundleImage) or source.entry.kind is not BundleKind.ENV:
Expand All @@ -907,6 +908,7 @@ def _check_multi_names(self, closure: set[_Key], store_latest: dict[tuple[Entity
taken: dict[str, str] = {} # each container a child runs as, by the field naming the child
named: dict[str, set[str]] = {} # the environment_names of each list's envs
computers: list[str] = [] # the computers' environment_names, in order
apps: dict[str, set[str]] = {} # each computer's apps' environment_names, when they're known
claimed = [sites.Claim(domain, sites.SYNTHETIC, ref.where) for ref in source.references
if ref.where.startswith("website_envs[")
for domain in dict.fromkeys(self._domains(ref, store_latest))]
Expand All @@ -921,6 +923,8 @@ def _check_multi_names(self, closure: set[_Key], store_latest: dict[tuple[Entity
named.setdefault(group, set()).add(name)
if group == "computer_envs":
computers.append(name)
if (held := self._server_names(ref, store_latest)) is not None:
apps[name] = held - {name}
continue
containers = [name] if group == "mcp_server_envs" else [
f"{name}-{AGENT_ENV_WEBSITE_BACKEND_SUFFIX}", f"{name}-{AGENT_ENV_WEBSITE_FRONTEND_SUFFIX}"]
Expand All @@ -932,6 +936,8 @@ def _check_multi_names(self, closure: set[_Key], store_latest: dict[tuple[Entity
taken.update(dict.fromkeys(containers, ref.where))
self.problems += [f"{where}: computer_envs: {problem}" for problem in _multi_computers.name_problems(
computers, mcp_servers=named.get("mcp_server_envs", ()), websites=named.get("website_envs", ()))]
self.problems += [f"{where}: computer_envs: {problem}" for problem in _multi_computers.app_name_problems(
apps, [*named.get("mcp_server_envs", ()), *named.get("website_envs", ()), *computers])]
provider_type = source.config.get("env_provider_type", EnvironmentGatewayProvider.type)
try:
provider = _env_provider_class(provider_type)
Expand Down Expand Up @@ -1012,13 +1018,19 @@ def _check_loaded_environments(self, closure: set[_Key],

def _server_names(self, ref: Reference, store_latest: dict[tuple[EntityKind, str], int] | None) -> set[str] | None:
"""The environment_names of the servers the env ``ref`` names runs: an MCP server's or a website's own, each
of a multi's envs', or a computer's own and each of its apps'. None when they aren't all known."""
of a multi's envs', its computers' apps' too, or a computer's own and each of its apps'. None when they aren't
all known."""
if isinstance(ref.local, BundleEntry):
cls = get_env_registry().get(ref.local.type)
children = self.nodes[_key(ref.local)].references
if cls is not None and issubclass(cls, MultiEnv):
names = {self._environment_name(child, store_latest) for child in children
if child.kind is EntityKind.ENV}
names = set()
for child in children:
if child.kind is not EntityKind.ENV:
continue
held = (self._server_names(child, store_latest) if child.where.startswith("computer_envs[") else
{self._environment_name(child, store_latest)})
names |= held if held is not None else {None}
else:
names = {self.accepted.get(_key(ref.local), {}).get("environment_name")}
if cls is not None and issubclass(cls, ComputerEnv):
Expand All @@ -1032,7 +1044,9 @@ def _server_names(self, ref: Reference, store_latest: dict[tuple[EntityKind, str
except (NotFoundError, ValueError, KeyError, TypeError):
return None # reported by the store ref check
if isinstance(env, MultiEnv):
return {child.environment_name for child in [*env.mcp_server_envs, *env.website_envs]}
return {*(child.environment_name for child in [*env.mcp_server_envs, *env.website_envs]),
*(name for computer in env.computer_envs
for name in (computer.environment_name, *(app.environment_name for app in computer.composed_apps())))}
if isinstance(env, ComputerEnv):
return {env.environment_name, *(app.environment_name for app in env.composed_apps())}
name = getattr(env, "environment_name", None)
Expand Down
42 changes: 41 additions & 1 deletion src/agent_env/env/envs/_deployment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""The env side of the environment-provider contract: deploying an env through its provider, the options a provider takes,
the checks on a plugin's record, loading into a deployment a plugin's provider made, and closing a deployment."""
the checks on a plugin's record, loading into a deployment a plugin's provider made, emptying a server's data, and closing
a deployment."""

from __future__ import annotations

Expand All @@ -10,9 +11,11 @@
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, Optional

import httpx
from agentenv_protocol import DATA_OBJECTS_EXTENSION_URI, METHOD_ADD, METHOD_RESET, RPC_PATH, FilePart, client as protocol_v1
from agent_env.env import legacy_protocol
from agent_env.env.env import DeployedSandboxEnv, gateway_url_of
from agent_env.env.gateway.constants import DATA_PLANE_LOAD_TIMEOUT_S
from agent_env.env.store import register_env_instance
from agent_env.store.base import ObjectNotFoundError
from agent_env.utils.paths import validate_file_name
Expand Down Expand Up @@ -142,6 +145,43 @@ async def load_by_signed_url(env: MCPServerEnv | WebsiteEnv, environment_artifac
await protocol_v1.add_data(base_url, file_parts(environment_artifact, files, urls), timeout=timeout)


async def reset_server_data(env: MCPServerEnv | WebsiteEnv, *, mcp: bool) -> bool:
"""Empty what loads put into ``env``, an MCP server or with ``mcp=False`` a website, as each load does before it adds:
through ``data/reset``, or an older server's ``/api/reset``, or else its ``<name>_reset`` tool, which with no seed
puts back the data the server boots with; then, behind a gateway of ours, its changelog started afresh, as after a
load. ``env`` is attached to its deployment, which may be a MultiEnv's. False, with nothing sent, for one serving no
reset, which takes no load either: a card declaring operations without ``data/reset``, a plugin's card offering it
no data plane, or an older server answering 404 that offers no ``<name>_reset`` tool."""
name = env.environment_name
if env._deployed is None and env._gateway_url is None:
raise RuntimeError("Environment not deployed - call deploy() first")
builtin = as_builtin(env._env_provider)
base_url = await legacy_protocol.v1_base_url(env._deployed, env._gateway_url, name, mcp=mcp)
if base_url is not None:
child = env._deployed.child_env(name, mcp=mcp) if env._deployed is not None and env._deployed.environment_card else None
if child is not None and child.serves(METHOD_RESET) is False:
return False
await protocol_v1.reset_data(base_url, timeout=DATA_PLANE_LOAD_TIMEOUT_S)
elif builtin is None:
return False
else:
try:
await legacy_protocol.reset_via_rest(legacy_protocol.environment_base_url(env._gateway_url, name, mcp=mcp),
timeout=60)
except httpx.HTTPStatusError as e:
if e.response.status_code != 404:
raise
mcp_url = f"{env._gateway_url.rstrip('/')}/mcp"
if not (mcp and await legacy_protocol.offers_tool(mcp_url, f"{name}_reset")):
return False
if (answer := await legacy_protocol.reset_via_mcp_tool(mcp_url, name)).isError:
said = "".join(part.text for part in answer.content if hasattr(part, "text"))
raise RuntimeError(f"MCP server '{name}': its {name}_reset tool failed: {said}") from None
if builtin is not None:
await builtin.install_changelog_triggers(name)
return True


def file_parts(environment_artifact: EnvironmentArtifact, files: dict[str, FileArtifact], uris: dict[str, str]) -> list[FilePart]:
"""The parts ``data/add`` takes ``environment_artifact``'s ``files`` as, each fetched from its uri in ``uris``: one file
as the one part it has always been sent as, each of several with its path in the data-objects marker."""
Expand Down
46 changes: 44 additions & 2 deletions src/agent_env/env/envs/_multi_computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import dataclasses
import logging
from collections import Counter
from collections.abc import Awaitable, Callable, Iterable
from typing import TYPE_CHECKING, Optional
from collections.abc import Awaitable, Callable, Iterable, Mapping
from typing import TYPE_CHECKING, Any, Optional

from agentenv_protocol.computer import tool_prefix_problem

Expand All @@ -17,6 +17,7 @@
from agent_env.providers.sandbox_providers.sandbox_provider import build_sandbox_provider, get_env_sandbox_provider

if TYPE_CHECKING:
from agent_env.artifact import EnvironmentArtifact
from agent_env.attribution import Attribution
from agent_env.env.envs.computer.env import ComputerEnv

Expand All @@ -40,6 +41,18 @@ def name_problems(computers: Iterable[str], *, mcp_servers: Iterable[str] = (),
return problems


def app_name_problems(apps: Mapping[str, Iterable[str]], others: Iterable[str]) -> list[str]:
"""What's wrong with the names of each computer's apps, ``apps`` by the computer's name, beside ``others``, every
other name the multi env's child envs go by: loads and snapshots find an environment by its name, so an app's must
be no other's."""
apps = {computer: list(dict.fromkeys(names)) for computer, names in apps.items()}
taken = Counter(set(others))
for names in apps.values():
taken.update(names)
return [f"app {name!r} of computer {computer!r} has the name of another of its child envs, and loads and snapshots "
"find an environment by its name" for computer, names in apps.items() for name in names if taken[name] > 1]


def deploy_options(entry: Optional[dict], sandbox_type: str | None) -> dict:
"""A computer's deploy options from its child_envs entry: on ``sandbox_type``, the multi env's, unless it names one."""
return {"sandbox_type": sandbox_type, **(entry or {})}
Expand Down Expand Up @@ -88,6 +101,23 @@ def commit(self) -> None:
(earlier, replaced), self._earlier = self._earlier, None
self._replaced = [*replaced, *(earlier if self.envs is not earlier else [])]

def holding(self, environment_name: str) -> Optional[ComputerEnv]:
"""The computer a load of ``environment_name`` reaches: the one of that name, or with an app of it."""
return next((env for env in self.envs if env.holds(environment_name)), None)

async def load(self, environments: list[EnvironmentArtifact]) -> None:
"""Load each computer's of ``environments`` into it, in its own order, the computers at once (``each``)."""
await each(self._owner, "load", [(f"computer {env.environment_name!r}", env.load_environments(held))
for env in self.envs
if (held := [e for e in environments if env.holds(e.environment_name)])])

def resets(self, deployed: DeployedEnv) -> list[tuple[str, Awaitable[None]]]:
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
"""Each computer ``deployed``, the multi env's record, holds, by name, with its reset from its own record, as it
resets alone; none is awaited yet."""
records = getattr(deployed, "child_envs", None) or {}
return [(env.environment_name, env.reset(DeployedEnv.from_dict(records[env.environment_name])))
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]]]:
"""The gateway's ``children``: every computer deployed as its child_envs entry says, its tools served under its
Expand Down Expand Up @@ -142,3 +172,15 @@ async def close(self) -> None:
logger.warning(f"multi env '{self._owner}': failed to close computer {env.environment_name!r}: {result!r}")
elif isinstance(result, BaseException):
raise result


async def each(owner: str, what: str, calls: list[tuple[str, Awaitable[Any]]]) -> list[Any]:
"""Await ``calls``, each a child env's under its label, at once, and once all have ended, what each returned; or,
when any failed to ``what``, a RuntimeError of the multi env ``owner`` naming each that did."""
results = await asyncio.gather(*(call for _, call in calls), return_exceptions=True)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if interrupted := next((r for r in results if isinstance(r, BaseException) and not isinstance(r, Exception)), None):
raise interrupted
if failed := [(child, result) for (child, _), result in zip(calls, results) if isinstance(result, Exception)]:
raise RuntimeError(f"multi env '{owner}': " + "; ".join(f"{child} failed to {what}: {error!r}"
for child, error in failed)) from failed[0][1]
return results
11 changes: 10 additions & 1 deletion src/agent_env/env/envs/computer/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,16 @@ async def load_environment_universe_artifact(self, environment_universe_artifact
if environment.environment_name not in names]:
logger.warning(f"[{self.id}] loading only {[environment.environment_name for environment in known]} from "
f"'{environment_universe_artifact.id}'; leaving out {others}")
await self._load(sorted(known, key=lambda environment: environment.environment_name != self.computer_name))
await self.load_environments(known)

def holds(self, environment_name: str) -> bool:
"""Whether a load reaches ``environment_name`` in this computer: its own, or one of its apps'."""
return environment_name in self._environment_names()

async def load_environments(self, environments: list[EnvironmentArtifact]) -> None:
"""Load ``environments``, each named after the computer or one of its apps: its own first, then its apps', so
what an app keeps ends as its own export has it."""
await self._load(sorted(environments, key=lambda environment: environment.environment_name != self.computer_name))

async def reset(self, deployed: DeployedEnv) -> None:
"""Return the computer to boot state: its recordings ended, its container replaced with a fresh one of the same
Expand Down
9 changes: 8 additions & 1 deletion src/agent_env/env/envs/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from agent_env.env import legacy_protocol
from agent_env.env.envs._deployment import (
as_builtin, builtin_provider_for, close_deployed, close_replaced, deploy_refusal, deploy_through_provider, host_staging_refusal,
file_parts, legacy_load_file, load_by_signed_url, plugin_provider_like_a_builtin, provider_or_class, staged_paths,
file_parts, legacy_load_file, load_by_signed_url, plugin_provider_like_a_builtin, provider_or_class, reset_server_data,
staged_paths,
)
from agent_env.env.gateway import GatewayMode
from agent_env.providers.sandbox_providers.sandbox import VmSandbox
Expand Down Expand Up @@ -136,6 +137,12 @@ async def from_deployed_env(cls, deployed: DeployedEnv) -> MCPServerEnv:
env._deployed = deployed
return env

async def reset(self, deployed: DeployedEnv) -> None:
"""Empty what loads put into the server, attached to ``deployed`` as a load is, so a task loads it again after;
NotImplementedError for one serving no reset, which takes no load either."""
if not await reset_server_data(await type(self).from_deployed_env(deployed), mcp=True):
raise NotImplementedError(f"MCP server '{self.environment_name}' serves no reset")

async def close(self) -> None:
await close_replaced(self)
if self._env_provider is not None:
Expand Down
Loading
Loading