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.

7 changes: 6 additions & 1 deletion src/agent_env/env/envs/computer/utils/site_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
SITE_ROOT_HEADER = "AgentEnv-Site-Root"
FORWARDED = frozenset({"x-forwarded-host", "x-forwarded-proto", "x-forwarded-for", "x-forwarded-port",
"x-forwarded-prefix", "forwarded", SITE_ROOT_HEADER.lower()})
#: Who a request is the gateway's to say: the computer it came from goes upstream from the config alone, and no page
#: names a role, so its request takes the role driving the computer.
ROLE_HEADER, COMPUTER_HEADER = "AgentEnv-Role", "AgentEnv-Computer"
AGENTENV_IDENTITY = frozenset({ROLE_HEADER.lower(), COMPUTER_HEADER.lower()})
RESPONSE_DROP = frozenset({"connection", "keep-alive", "proxy-connection", "alt-svc"})
UPSTREAM_HEADERS_FILE = "upstream_headers.json"
HEAD_LIMIT = 1 << 20
Expand Down Expand Up @@ -442,7 +446,8 @@ def upstream_request(method: str, target: str, headers: list[tuple[str, str]], s
parts = urlsplit(target)
target = urlunsplit(("", "", parts.path or "/", parts.query, ""))
provided = {key.lower() for key in site.upstream_headers}
kept = [(key, value) for key, value in headers if key.lower() not in HOP_BY_HOP | FORWARDED | provided]
kept = [(key, value) for key, value in headers
if key.lower() not in HOP_BY_HOP | FORWARDED | AGENTENV_IDENTITY | provided]
kept += [("Host", site.netloc), ("X-Forwarded-Host", host), ("X-Forwarded-Proto", scheme),
("X-Forwarded-For", peer), (SITE_ROOT_HEADER, f"{scheme}://{host}"), *site.upstream_headers.items()]
kept += [("Connection", "Upgrade"), ("Upgrade", upgrade)] if upgrade else [("Connection", "close")]
Expand Down
5 changes: 4 additions & 1 deletion src/agent_env/env/envs/computer/utils/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

from agent_env.env.env import Env, EnvCapabilityUnsupported, gateway_url_of
from agent_env.env.envs.computer.utils.site_proxy import is_local, matches
from agent_env.env.gateway.constants import AGENT_ENV_COMPUTER_HEADER
from agent_env.providers.sandbox_providers.local_docker.provider import LOCAL_TRUST_ENV
from agent_env.providers.sandbox_providers.sandbox import NetworkPolicy, VmSandbox
from agent_env.store.base import NotFoundError
Expand Down Expand Up @@ -457,7 +458,9 @@ async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], net
from agent_env.providers.sandbox_providers.sandbox_provider import sandbox_request_headers_for_url

issued = issue(table.routes, label=label)
upstream_headers = {r.site: sandbox_request_headers_for_url(r.url) for r in table.routes}
# Each request names the computer it came from, so the gateway gives it the role driving that computer now.
upstream_headers = {r.site: {**sandbox_request_headers_for_url(r.url), AGENT_ENV_COMPUTER_HEADER: label}
for r in table.routes}
channel = RootChannel(sandbox)
await channel.private_folder(FOLDER)
await channel.write(PROXY, _PROXY_SOURCE.read_text())
Expand Down
14 changes: 9 additions & 5 deletions src/agent_env/env/gateway/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
from __future__ import annotations

import heapq
from typing import Any
from typing import Any, Callable, Container


class ChangeAttribution:
"""The write windows (changelog ids before and after a call) of the trajectory events logged since start."""
"""The write windows (changelog ids before and after a call) of the trajectory events logged since start. A call
to a tool `unwindowed` names, a computer's, claims none: a computer writes the env's data only through its
browser's requests, which carry windows of their own, while its calls may run for seconds as others write."""

def __init__(self) -> None:
def __init__(self, unwindowed: Callable[[], Container[str]] = frozenset) -> None:
self._calls: dict[str, dict] = {}
self._windows: list[tuple[int, int, int, dict]] = [] # (before, after, order, actor)
self._unwindowed = unwindowed

def observe(self, event: dict) -> None:
"""Take one trajectory event as `_log_event` writes it."""
Expand All @@ -21,10 +24,11 @@ def observe(self, event: dict) -> None:
**_who(event)}
elif kind in ("tool_call_result", "tool_call_failed"):
actor = self._calls.pop(event.get("tool_call_event_id"), None)
if actor is not None and "changelog_id_before" in event: # a gateway-native tool's result has no window
if (actor is not None and "changelog_id_before" in event # a gateway-native tool's result has no window
and actor["tool"] not in self._unwindowed()):
failed = {"failed": True} if kind == "tool_call_failed" else {}
self._add(event["changelog_id_before"], event.get("changelog_id"), {**actor, **failed})
elif kind == "internal_tool_call":
elif kind == "internal_tool_call" and event.get("tool") not in self._unwindowed():
self._add(event.get("changelog_id_before"), event.get("changelog_id_after"), {
"kind": "trigger_action", "event_id": event["event_id"], "trigger_id": event.get("trigger_id"),
"action_index": event.get("action_index"), "tool": event.get("tool"), "role": event.get("as")})
Expand Down
5 changes: 5 additions & 0 deletions src/agent_env/env/gateway/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def data_plane_load_timeout_s(size_bytes: int | None) -> int:
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 gateway's computers, comma-separated: a multi env's, each a child on a sandbox of its own, whose input calls set
# who drives it, the role its browser's requests take.
COMPUTERS_ENV = "COMPUTERS"
# 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
Expand Down Expand Up @@ -162,6 +165,8 @@ def sources(names: list[str]) -> str:
)

AGENT_ENV_ROLE_HEADER = "AgentEnv-Role"
# Sent by a computer's site proxy: the computer whose browser made the request, which takes the role driving it now.
AGENT_ENV_COMPUTER_HEADER = "AgentEnv-Computer"
# Sent by a computer's site proxy, which serves a website's route at the root of the site's own name.
AGENT_ENV_SITE_ROOT_HEADER = "AgentEnv-Site-Root"
GATEWAY_PROVIDER_HEADERS_ENV = "AGENTENV_PROVIDER_REQUEST_HEADERS"
Expand Down
3 changes: 2 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_CHILDREN_ENV, CONTROL_TOKEN_ENV, DEFAULT_MCP_SERVER_NAME,
from .constants import (CLOCK_FOLLOWERS_ENV, COMPUTERS_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 @@ -244,6 +244,7 @@ def parse_site_names(env_str: str) -> dict[str, tuple[str, ...]]:
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()],
computers=[name.strip() for name in os.environ.get(COMPUTERS_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
47 changes: 41 additions & 6 deletions src/agent_env/env/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

# Relative import — gateway runs as a standalone container with no agent_env.*
from .constants import (
AGENT_ENV_COMPUTER_HEADER,
AGENT_ENV_HIDDEN_BY_DEFAULT_META_KEY,
AGENT_ENV_ROLE_HEADER,
AGENT_ENV_ROLE_META_KEY, # noqa: F401 (the server module serves the whole vocabulary, used in this file or not)
Expand Down Expand Up @@ -135,6 +136,12 @@ def __init__(self, clashes: dict[str, list[str]]):
super().__init__(f"Duplicate tool name{'s' if len(clashes) > 1 else ''} {tool_name_clashes_text(clashes)}")



def _read_only(tool: MCPTool) -> bool:
"""Whether a tool says it only reads (``readOnlyHint``), as a computer's screenshots and waits do."""
annotations = getattr(tool, "annotations", None)
return annotations is not None and getattr(annotations, "readOnlyHint", None) is True

class Gateway:
"""Gateway that aggregates MCP tools and REST proxies for website backends."""

Expand Down Expand Up @@ -163,6 +170,7 @@ def __init__(
service_db_url: str | None = None,
control_token: str | None = None,
control_token_children: Iterable[str] = (),
computers: Iterable[str] = (),
provider_headers: Iterable[str] = (),
clock_followers: Mapping[str, str] | None = None,
site_names: dict[str, Iterable[str]] | None = None,
Expand All @@ -174,7 +182,9 @@ def __init__(
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``. `control_token_children` are the children its
/svc routes pass its control token on to, a multi env's computers, which take it."""
/svc routes pass its control token on to, a multi env's computers, which take it. `computers` are its children
that are computers, a multi env's: an input call to one's tools makes its caller the role driving it, which the
requests its browser sends take."""
self.host = host
self.port = port
self.server_name = server_name
Expand All @@ -197,6 +207,12 @@ def __init__(
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._computers = frozenset(computers)
# The computer each computer tool is one of, the computer tools that take input rather than read, and the role
# driving each computer now: the last to make an input call to its tools.
self._computer_of: dict[str, str] = {}
self._input_tools: frozenset[str] = frozenset()
self._drivers: dict[str, str] = {}
self._website_routes: dict[str, list[WebsiteRoute] | None] = {}
self._db_conn: psycopg2.extensions.connection | None = None
if service_db_url:
Expand All @@ -216,7 +232,7 @@ def __init__(

self._event_counter = 0
self._event_lock = asyncio.Lock()
self._changes = ChangeAttribution()
self._changes = ChangeAttribution(lambda: self._computer_of)
Path(GATEWAY_TRAJECTORY_FILE).parent.mkdir(parents=True, exist_ok=True)
self._trajectory_file = open(GATEWAY_TRAJECTORY_FILE, "a")
atexit.register(self._close_trajectory_file)
Expand Down Expand Up @@ -912,10 +928,24 @@ async def _fire_website_triggers(self, request: Request, app: str,
logger.exception("Trigger evaluation failed (fail-open)")
return [], None

@staticmethod
def _request_role(request: HTTPConnection) -> str:
"""The role the gateway attributes a REST request to: the first AgentEnv-Role header, else the default role."""
return request.headers.get(AGENT_ENV_ROLE_HEADER, "").strip() or DEFAULT_ROLE
def _request_role(self, request: HTTPConnection) -> str:
"""The role the gateway attributes a REST request to: the first AgentEnv-Role header; else, from a computer's
site proxy (AgentEnv-Computer), the role driving that computer as the request arrived; else the default role.
Read once per request, so its forwarding, its write and its triggers name one role, whoever drives after."""
known = getattr(request.state, "agentenv_role", None)
if known is None:
role = request.headers.get(AGENT_ENV_ROLE_HEADER, "").strip()
computer = request.headers.get(AGENT_ENV_COMPUTER_HEADER, "").strip()
known = role or self._drivers.get(computer) or DEFAULT_ROLE
request.state.agentenv_role = known
return known

def _drive(self, role: str, tool_name: str) -> None:
"""An input call to a computer's tools makes its caller that computer's driver as it starts, so a request the
page sends before the call returns is the caller's too. A read (a screenshot, a wait) changes nothing."""
if tool_name in self._input_tools:
self._drivers[self._computer_of[tool_name]] = role


async def _read_trigger_website_routes(self, body: Any) -> None:
"""Read the routes of each website a website trigger in a register `body` names an `op` of, so registration
Expand Down Expand Up @@ -1092,6 +1122,7 @@ async def _call_child_tool(self, server_url: str, tool_name: str, arguments: dic
unattributed trigger action), on its child's session (see child_sessions). Discovery is not redone on a
reconnect; the registered proxies look their child up by URL."""
meta = caller.meta() if caller is not None else None
self._drive(caller.role if caller is not None else DEFAULT_ROLE, tool_name)
return await self._sessions.call(server_url, tool_name, arguments, meta, timeout=self.TOOL_CALL_TIMEOUT_S)

async def _ensure_tools_discovered(self) -> None:
Expand Down Expand Up @@ -1154,6 +1185,10 @@ async def _discover_and_register_tools(self, sessions: dict[str, ClientSession])
logger.debug(f"Registered proxy tool: {tool.name}")
for server, tools in listed:
self._server_tools[server.name] = tools
computer_tools = [(server.name, tool) for server, tools in listed if server.name in self._computers
for tool in tools]
self._computer_of = {tool.name: computer for computer, tool in computer_tools}
self._input_tools = frozenset(tool.name for _, tool in computer_tools if not _read_only(tool))
self._hidden_by_default = {
tool.name for _, tools in listed for tool in tools
if (tool.meta or {}).get(AGENT_ENV_HIDDEN_BY_DEFAULT_META_KEY) is True}
Expand Down
10 changes: 10 additions & 0 deletions src/agent_env/providers/env_providers/env_gateway_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from agent_env.env.gateway import AGENT_ENV_GATEWAY_MCP_PORT, ExternalChild, GatewayMode
from agent_env.env.gateway.constants import (
CLOCK_FOLLOWERS_ENV,
COMPUTERS_ENV,
CONTROL_TOKEN_CHILDREN_ENV,
DEFAULT_MCP_SERVER_NAME,
GATEWAY_PROVIDER_HEADERS_ENV,
Expand Down Expand Up @@ -551,6 +552,7 @@ def create_docker_compose(
gateway_env.extend(f" - {key}={value}" for key, value in clock_followers_environment(external_children).items())
gateway_env.extend(f" - {key}={value}"
for key, value in control_token_children_environment(external_children, control_token).items())
gateway_env.extend(f" - {key}={value}" for key, value in computers_environment(external_children).items())
if provider_headers := sandbox_request_header_names():
gateway_env.append(f" - {GATEWAY_PROVIDER_HEADERS_ENV}={','.join(provider_headers)}")
publish = host_port or (lambda port: port)
Expand Down Expand Up @@ -1130,6 +1132,7 @@ async def _deploy_via_containers(
**control_environment(control_token),
**clock_followers_environment(external),
**control_token_children_environment(external, control_token),
**computers_environment(external),
**({GATEWAY_PROVIDER_HEADERS_ENV: ",".join(names)} if (names := sandbox_request_header_names()) else {}),
}
gateway_sb = await sandbox_provider.create_container(
Expand Down Expand Up @@ -1781,6 +1784,13 @@ def control_token_children_environment(children: Iterable[ExternalChild], contro
return {CONTROL_TOKEN_CHILDREN_ENV: names} if names else {}


def computers_environment(children: Iterable[ExternalChild]) -> dict[str, str]:
"""The gateway's computers: every child it fronts on a sandbox of its own (``children``), a multi env's computers,
with or without a control token."""
names = ",".join(child.name for child in children)
return {COMPUTERS_ENV: names} if names else {}


def _frontend_urls(gateway_url: str, website_configs: Optional[list[WebsiteConfig]]) -> Optional[dict[str, str]]:
"""Each synthetic site's page route through the gateway, by its environment name; None with none, as when every
site is live."""
Expand Down
34 changes: 34 additions & 0 deletions tst/integration/computer/computer_triggers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from agent_env.task_step.task_steps.configure_scheduled_checks import ConfigureScheduledChecksTaskStep
from agent_env.task_step.task_steps.register_env_triggers import RegisterEnvTriggersStep
from tst.integration.computer.conftest import CONTAINER, multi_env
from tst.integration.computer.multi_env_computers_test import _reaches, shop # noqa: F401 shop is a fixture
from tst.integration.task_step.test_register_env_triggers_local import _Executor

pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow]
Expand Down Expand Up @@ -187,3 +188,36 @@ async def _computer_shows(computer: DeployedEnv, virtual_time: str) -> bool:
if time.monotonic() > deadline:
return False
await asyncio.sleep(0.5)


@pytest.mark.asyncio
async def test_a_computers_browser_requests_take_the_role_driving_it_now(desktop, items, shop): # noqa: F811
routed = {**CONTAINER, "env_params": {"form": "container", "site_routing": True}}
health = {"id": "health", "actions": [], "when": {"type": "website", "app": "shop", "method": "GET",
"path": {"regex": "^/?api/health$"}, "repeat": True}}
with namespace_routing():
office = multi_env(items, Env.get(desktop.id), websites=[shop])
context = TaskStepContext()
try:
deployed = await office.deploy(sandbox_type="local", ttl_seconds=1800, child_envs={"desktop": routed})
context = TaskStepContext(deployed_envs=[deployed], metadata={})
computer = DeployedEnv.from_dict(deployed.child_envs["desktop"])
await RegisterEnvTriggersStep(id="seen", version=None, env_id=office.id, triggers=[health],
watch_roles=["person", "helper"]).execute(context)
async with httpx.AsyncClient(base_url=deployed.gateway_url, timeout=120,
headers=deployed.control_headers) as client:
loads = []
for role in ("person", "helper"):
moved = await client.post("/step", json={"action": "call_tool", "arguments": {"x": 10, "y": 10},
"tool_name": "desktop_computer_pointer_move"},
headers={AGENT_ENV_ROLE_HEADER: role})
assert moved.status_code == 200, moved.text
loads.append(await _reaches(computer, "https://shop.test/api/health"))
await _quiet(client)
state = await _triggers(client)
finally:
report = await teardown_run(context)

assert not report.failed and loads == ["200", "200"]
assert [e["provoking"]["role"] for e in state["events"] if e["kind"] == "detected"] == ["person", "helper"]

Loading
Loading