Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion AGENTS.md

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/agent_env/env/envs/_multi_computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ def _recorded_browser(self, records: Mapping[str, dict]) -> Optional[ComputerEnv

def deployer(self, child_envs: Optional[dict], *, sandbox_type: str | None, ttl_seconds: int,
attribution: Optional[Attribution], websites: Mapping[str, str], live_hosts: Iterable[str] = ()
) -> Callable[[Optional[str]], Awaitable[list[ExternalChild]]]:
) -> Callable[[Optional[str], 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, and the
name and ``_``, its framework-only routes taking ``control_token``, the gateway's, which the gateway passes on to
them, and its network allowing ``reach``, the gateway's address when the gateway has one yet, and the
``live_hosts`` of the multi env's live websites; then each 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."""
Expand All @@ -204,14 +205,14 @@ def deployer(self, child_envs: Optional[dict], *, sandbox_type: str | None, ttl_
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]:
async def deploy(reach: Optional[str], control_token: Optional[str]) -> list[ExternalChild]:
nonlocal tries
if tries:
await self.close()
tries += 1
results = await asyncio.gather(*(env.deploy(
ttl_seconds=ttl_seconds, attribution=attribution, tool_prefix=f"{env.environment_name}_",
reach=reach or "", sites_later=env.environment_name in self._routes,
reach=reach or "", sites_later=env.environment_name in self._routes, control_token=control_token,
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
**({"live_hosts": live} if live else {}),
**deploy_options((child_envs or {}).get(env.environment_name), sandbox_type))
for env in self.every), return_exceptions=True)
Expand Down
12 changes: 7 additions & 5 deletions src/agent_env/env/envs/computer/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,19 @@ async def deploy(self, ttl_seconds: int = 10800, disk_size_gb: float = 10,
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 = "",
sites_later: bool = False, live_hosts: tuple[str, ...] = ()) -> DeployedEnv:
sites_later: bool = False, live_hosts: tuple[str, ...] = (),
control_token: Optional[str] = None) -> 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 the ``live_hosts`` of the run's live websites, 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."""
egress allows that URL's host too, and the ``live_hosts`` of the run's live websites, with ``sites_later``
it routes the sites ``env_params.site_routing`` reaches when ``route_sites`` gives them, and with
``control_token`` its framework-only routes take that token, not one of its own: 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, sites_later=sites_later,
live_hosts=tuple(live_hosts))
live_hosts=tuple(live_hosts), control_token=control_token)

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;
Expand Down
6 changes: 5 additions & 1 deletion src/agent_env/env/gateway/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def data_plane_load_timeout_s(size_bytes: int | None) -> int:
# The computers that follow the gateway's clock, each as `name=read token`, comma-separated; TOKEN in the name keeps
# it out of logged compose files.
CLOCK_FOLLOWERS_ENV = "CLOCK_FOLLOWER_TOKENS"
# The children the gateway passes its control token on to, comma-separated: a multi env's computers, which boot with it.
CONTROL_TOKEN_CHILDREN_ENV = "CONTROL_TOKEN_CHILDREN"
# The route a clock follower long-polls, with its read token.
CLOCK_WATCH_PATH = "/clock/watch"
# How long /clock/watch holds a watch naming the current version before answering it unchanged; a follower's request
# timeout must be longer. The card's watch method advertises it as hold_s.
CLOCK_WATCH_HOLD_S = 25.0
Expand Down Expand Up @@ -252,7 +256,7 @@ def random_mcp_server_name() -> str:
"advance": {"method": "PUT", "endpoint": "/clock/advance", "request": {"type": "object", "properties": {"virtual_time": {"type": "string"}, **_WAIT_FOLLOWERS}, "required": ["virtual_time"]}},
"clear": {"method": "POST", "endpoint": "/clock/clear", "request": {"type": "object", "properties": {**_WAIT_FOLLOWERS}}},
"state": {"method": "GET", "endpoint": "/clock/state", "request": {"type": "object", "properties": {}}},
"watch": {"method": "GET", "endpoint": "/clock/watch", "hold_s": CLOCK_WATCH_HOLD_S, "request": {"type": "object", "properties": {"after": {"type": "string"}, "follower": {"type": "string"}}}}}},
"watch": {"method": "GET", "endpoint": CLOCK_WATCH_PATH, "hold_s": CLOCK_WATCH_HOLD_S, "request": {"type": "object", "properties": {"after": {"type": "string"}, "follower": {"type": "string"}}}}}},
"required": False,
},
{
Expand Down
4 changes: 3 additions & 1 deletion src/agent_env/env/gateway/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import httpx

from . import AGENT_ENV_GATEWAY_MCP_PORT, GatewayMode, InternalMCPServer
from .constants import (CLOCK_FOLLOWERS_ENV, CONTROL_TOKEN_ENV, DEFAULT_MCP_SERVER_NAME,
from .constants import (CLOCK_FOLLOWERS_ENV, CONTROL_TOKEN_CHILDREN_ENV, CONTROL_TOKEN_ENV, DEFAULT_MCP_SERVER_NAME,
GATEWAY_PROVIDER_HEADERS_ENV, MCP_TRANSPORT, WEBSITE_LISTED_BY_NAME_ENV,
WEBSITE_NAMES_ENV, WELL_KNOWN_PATH)
from .gateway import Gateway, card_url
Expand Down Expand Up @@ -242,6 +242,8 @@ def parse_site_names(env_str: str) -> dict[str, tuple[str, ...]]:
gateway_mode=gateway_mode,
service_db_url=service_db_url,
control_token=os.environ.get(CONTROL_TOKEN_ENV),
control_token_children=[name.strip() for name in os.environ.get(CONTROL_TOKEN_CHILDREN_ENV, "").split(",")
if name.strip()],
provider_headers=os.environ.get(GATEWAY_PROVIDER_HEADERS_ENV, "").split(","),
clock_followers=parse_clock_followers(os.environ.get(CLOCK_FOLLOWERS_ENV, "")),
site_names=parse_site_names(os.environ.get(WEBSITE_NAMES_ENV, "")),
Expand Down
24 changes: 19 additions & 5 deletions src/agent_env/env/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
AGENT_ENV_SESSION_META_KEY, # noqa: F401
CARD_FETCH_TIMEOUT_S,
CHANGES_PAGE_DEFAULT,
CLOCK_WATCH_PATH,
CHANGES_PAGE_MAX,
CHILD_DISCOVERY_TIMEOUT_S,
CONTROL_ROUTES,
Expand Down Expand Up @@ -159,6 +160,7 @@ def __init__(
gateway_mode: GatewayMode = GatewayMode.PERFORMANCE,
service_db_url: str | None = None,
control_token: str | None = None,
control_token_children: Iterable[str] = (),
provider_headers: Iterable[str] = (),
clock_followers: Mapping[str, str] | None = None,
site_names: dict[str, Iterable[str]] | None = None,
Expand All @@ -169,7 +171,8 @@ def __init__(
followers of its clock, each with the read token its watch carries. `site_names` are each site's names as its
env stores them, the names its card declares standing in for a site's that stores none: a site reached at one
takes it as its ``Host``. ``list_website_urls`` gives each site `listed_by_name` at its first name, as a browser
computer opens it, and the others at their ``website_urls``."""
computer opens it, and the others at their ``website_urls``. `control_token_children` are the children its
/svc routes pass its control token on to, a multi env's computers, which take it."""
self.host = host
self.port = port
self.server_name = server_name
Expand All @@ -190,6 +193,7 @@ def __init__(
self.gateway_mode = gateway_mode
self._service_db_url = service_db_url
self._control_bearer = f"Bearer {control_token}".encode() if control_token else None
self._control_token_children = frozenset(control_token_children)
self._website_routes: dict[str, list[WebsiteRoute] | None] = {}
self._db_conn: psycopg2.extensions.connection | None = None
if service_db_url:
Expand Down Expand Up @@ -370,7 +374,7 @@ def disarm(body: dict) -> dict:

return await self._clock_write(request, disarm, body_optional=True)

@self._mcp.custom_route("/clock/watch", methods=["GET"])
@self._mcp.custom_route(CLOCK_WATCH_PATH, methods=["GET"])
async def clock_watch(request: Request) -> Response:
received = time.time()
token = request.headers.get("authorization", "").removeprefix("Bearer ").strip()
Expand Down Expand Up @@ -534,6 +538,14 @@ def run(self) -> None:
# by every tools/call of that session (and the role is the one its initialize request carried, which that task
# inherited). Process: child sessions and the time-trigger driver, outer to the MCP session manager's lifespan.

def _keeps_control_token(self, path: str) -> bool:
"""Whether a request to ``path`` keeps the gateway's control token: its clock watch, where a computer it fronts
sends the token as its read token, and its /svc route to one of those computers, which takes the token."""
if path == CLOCK_WATCH_PATH:
return True
child = path.removeprefix("/svc/").split("/", 1)[0] if path.startswith("/svc/") else None
return child in self._control_token_children

def _asgi_app(self):
"""The served app: FastMCP's Starlette app with the request's caller bound around it, the control routes behind
the deploy's token when it has one, and the process lifespan wrapped around the MCP session manager's."""
Expand All @@ -550,9 +562,11 @@ async def app(scope, receive, send):
response = self._json_response({"ok": False, "error": "this route takes the env's control token"}, 401)
await response(scope, receive, send)
return
# The token is the gateway's own: no route reads it, and no service a route proxies to receives it.
scope = {**scope, "headers": [(name, value) for name, value in headers if not (
name == b"authorization" and hmac.compare_digest(value, self._control_bearer))]}
# Only the clock watch reads the token, which the computers the gateway fronts send it, and only they
# receive it, through their /svc routes; every other request goes on without it.
if not self._keeps_control_token(scope["path"]):
scope = {**scope, "headers": [(name, value) for name, value in headers if not (
name == b"authorization" and hmac.compare_digest(value, self._control_bearer))]}
if scope["type"] != "http":
await inner(scope, receive, send)
return
Expand Down
13 changes: 8 additions & 5 deletions src/agent_env/providers/env_providers/env_computer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
app_settings_environment,
boot,
control_environment,
control_token,
control_token as new_control_token,
discard,
first_native,
listed,
Expand Down Expand Up @@ -85,14 +85,16 @@ async def deploy(
reach: str = "",
sites_later: bool = False,
live_hosts: tuple[str, ...] = (),
control_token: Optional[str] = None,
) -> 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, and the
``live_hosts`` of the run's live websites. 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."""
once its driver answers; ``route_sites`` routes them and finishes its boot. With ``control_token``, its
framework-only routes take that token (a multi env's own) rather than one of the computer's own."""
from agent_env.env.envs.computer.env import ComputerEnv

if not isinstance(env, ComputerEnv):
Expand Down Expand Up @@ -125,7 +127,7 @@ async def deploy(
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, sites_later=sites_later,
live_hosts=tuple(live_hosts))
live_hosts=tuple(live_hosts), control_token=control_token)
return await self._run(sandbox_provider, env.id, attempt)

async def route_sites(self, env: ComputerEnv, record: DeployedSandboxEnv, sites: list[dict]) -> None:
Expand Down Expand Up @@ -153,7 +155,8 @@ async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], par
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 = "",
sites_later: bool = False, live_hosts: tuple[str, ...] = ()) -> DeployedSandboxEnv:
sites_later: bool = False, live_hosts: tuple[str, ...] = (),
control_token: Optional[str] = None) -> 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

Expand All @@ -174,7 +177,7 @@ async def _boot(self, env: ComputerEnv, builds: list[tuple[str, str, dict]], par
build = await probe_first_boot(env, form, arch, sandbox_provider)
if build["status"] != "ready":
raise RuntimeError(f"{env.id}'s {form}/{arch} failed its probe on its first boot: {build['reason']}")
computer, name, token = env.computer_artifact(), env.environment_name, control_token()
computer, name, token = env.computer_artifact(), env.environment_name, control_token or new_control_token()
values = _values(computer, values)
app_settings = recorded_app_settings(env.composed_apps(), computer_params.app_settings(env, params.app_settings),
platform_of(env.computer.os))
Expand Down
Loading
Loading