diff --git a/src/agent_env/cli/env/deploy.py b/src/agent_env/cli/env/deploy.py index 25e6f3749..708f05958 100644 --- a/src/agent_env/cli/env/deploy.py +++ b/src/agent_env/cli/env/deploy.py @@ -103,10 +103,11 @@ def deploy(env_id: str, env_version: int | None, ttl_seconds: int, gateway_mode: # A record without a gateway has none of the gateway's URLs. if isinstance(deployed_env, DeployedGatewayEnv): click.echo("Env Gateway Url: " + click.style(deployed_env.gateway_url, fg="yellow")) - if deployed_env.db_web_url: - click.echo("Env DB Web Url: " + click.style(deployed_env.db_web_url, fg="yellow")) - if deployed_env.db_mcp_url: - click.echo("Env DB MCP Url: " + click.style(deployed_env.db_mcp_url, fg="yellow")) + # The SQL consoles take the env's control token, as an Authorization: Bearer header. + if deployed_env.db_web_gateway_url: + click.echo("Env DB Web Url: " + click.style(deployed_env.db_web_gateway_url, fg="yellow")) + if deployed_env.db_mcp_gateway_url: + click.echo("Env DB MCP Url: " + click.style(deployed_env.db_mcp_gateway_url, fg="yellow")) if deployed_env.vnc_url: click.echo("VNC Url: " + click.style(deployed_env.vnc_url, fg="yellow")) for svc_name, url in (deployed_env.website_frontend_urls or {}).items(): diff --git a/src/agent_env/cli/env/get_instance.py b/src/agent_env/cli/env/get_instance.py index 4bcfd213b..3a4d05585 100644 --- a/src/agent_env/cli/env/get_instance.py +++ b/src/agent_env/cli/env/get_instance.py @@ -26,10 +26,10 @@ def get_instance(instance_id: str): if gateway: click.echo(f"Env Base Url: {gateway.gateway_url}") click.echo(f"Env MCP Url: {deployed_env.mcp_url}") - if gateway and gateway.db_web_url: - click.echo(f"Env DB Web Url: {gateway.db_web_url}") - if gateway and gateway.db_mcp_url: - click.echo(f"Env DB MCP Url: {gateway.db_mcp_url}") + if gateway and gateway.db_web_gateway_url: + click.echo(f"Env DB Web Url: {gateway.db_web_gateway_url}") + if gateway and gateway.db_mcp_gateway_url: + click.echo(f"Env DB MCP Url: {gateway.db_mcp_gateway_url}") for svc_name, url in ((gateway.website_frontend_urls if gateway else None) or {}).items(): click.echo(f"Website Frontend ({svc_name}): {url}") if isinstance(deployed_env, DeployedSandboxEnv): diff --git a/src/agent_env/cli/run.py b/src/agent_env/cli/run.py index fd1182662..294afaf30 100644 --- a/src/agent_env/cli/run.py +++ b/src/agent_env/cli/run.py @@ -26,7 +26,7 @@ _COLORS = {Outcome.PASSED: "green", Outcome.BELOW_ONE: "yellow", Outcome.UNSCORED: "yellow", Outcome.FAILED: "red", Outcome.CANCELLED: "yellow"} -_ENV_ENDPOINTS = (("mcp", "mcp_url"), ("gateway", "gateway_url"), ("pgweb", "db_web_url"), ("db-mcp", "db_mcp_url"), +_ENV_ENDPOINTS = (("mcp", "mcp_url"), ("gateway", "gateway_url"), ("pgweb", "db_web_gateway_url"), ("db-mcp", "db_mcp_gateway_url"), ("vnc", "vnc_url"), ("expires", "expires_at_utc")) _DRY_RUN = "Dry run: nothing is written or run." diff --git a/src/agent_env/cli/task/run.py b/src/agent_env/cli/task/run.py index feaba615b..64aff5183 100644 --- a/src/agent_env/cli/task/run.py +++ b/src/agent_env/cli/task/run.py @@ -49,7 +49,8 @@ def _format_env(env) -> str: f" env_version: {env.env_version}", *([f" gateway_url: {env.gateway_url}"] if fronted else []), f" mcp_url: {env.mcp_url}", - *([f" db_web_url: {env.db_web_url}", f" db_mcp_url: {env.db_mcp_url}"] if fronted else []), + *([f" db_web_gateway_url: {env.db_web_gateway_url}", f" db_mcp_gateway_url: {env.db_mcp_gateway_url}"] + if fronted else []), ] if isinstance(env, DeployedSandboxEnv): # an env outside our sandboxes has no sandbox to name lines.append(f" sandbox_id: {env.sandbox_id}") diff --git a/src/agent_env/env/env.py b/src/agent_env/env/env.py index bea7e06f4..f6df6b40f 100644 --- a/src/agent_env/env/env.py +++ b/src/agent_env/env/env.py @@ -228,8 +228,14 @@ class DeployedGatewayEnv(DeployedSandboxEnv): env_provider_type: Optional[str] = "gateway" gateway_url: str gateway_mode: str = GatewayMode.PERFORMANCE.value + # pgweb's and db-mcp's direct URLs, which only a record an older deploy wrote carries; nothing writes them now. + # Kept for one release so a consumer can still read an env deployed before then. Remove in the release after. db_web_url: Optional[str] = None db_mcp_url: Optional[str] = None + # pgweb and db-mcp behind the gateway, which take the control token (``control_headers_for``); None when the deploy + # renders no such console, has no control token, or its gateway doesn't guard and reach the console. + db_web_gateway_url: Optional[str] = None + db_mcp_gateway_url: Optional[str] = None website_frontend_urls: Optional[dict[str, str]] = None vnc_url: Optional[str] = None # Ids of the EnvStateInstance record(s) this deploy's state was provisioned into @@ -248,6 +254,8 @@ def _fields_from(cls, data: dict) -> dict: "gateway_mode": data.get("gateway_mode", GatewayMode.PERFORMANCE.value), "db_web_url": data.get("db_web_url"), "db_mcp_url": data.get("db_mcp_url"), + "db_web_gateway_url": data.get("db_web_gateway_url"), + "db_mcp_gateway_url": data.get("db_mcp_gateway_url"), "website_frontend_urls": data.get("website_frontend_urls"), "vnc_url": data.get("vnc_url"), "env_state_instance_ids": data.get("env_state_instance_ids") or [], @@ -583,5 +591,5 @@ def _record_class(data: dict) -> type[DeployedEnv]: # A record carrying any of these is a gateway's. Not gateway_mode: an older kernel writes its default into any record it round-trips. -_GATEWAY_ONLY_FIELDS = ("gateway_url", "db_web_url", "db_mcp_url", "website_frontend_urls", "vnc_url", "env_state_instance_ids", - "child_envs") +_GATEWAY_ONLY_FIELDS = ("gateway_url", "db_web_url", "db_mcp_url", "db_web_gateway_url", "db_mcp_gateway_url", + "website_frontend_urls", "vnc_url", "env_state_instance_ids", "child_envs") diff --git a/src/agent_env/env/envs/_multi_computers.py b/src/agent_env/env/envs/_multi_computers.py index 4503c3889..fc4ab9134 100644 --- a/src/agent_env/env/envs/_multi_computers.py +++ b/src/agent_env/env/envs/_multi_computers.py @@ -15,6 +15,7 @@ from agent_env.env.env import DeployedEnv, DeployedGatewayEnv, Env, gateway_url_of from agent_env.env.envs.computer.utils import clock as computer_clock, sites +from agent_env.env.envs.service_db import DB_MCP_SERVICE_NAME, PGWEB_SERVICE_NAME from agent_env.env.gateway import ExternalChild from agent_env.providers.sandbox_providers.sandbox_provider import build_sandbox_provider, get_env_sandbox_provider @@ -29,8 +30,8 @@ def name_problems(computers: Iterable[str], *, mcp_servers: Iterable[str] = (), websites: Iterable[str] = ()) -> list[str]: """What's wrong with ``computers``, the environment names of a multi env's computers, beside the names of its ``mcp_servers`` and ``websites``: a computer serves its tools under its name and ``_``, which must make a tool prefix, - and the gateway knows a computer by its name alone, which no other child env and no route of the gateway's to an - MCP server (``mcp-``) may have.""" + and the gateway knows a computer by its name alone, which no other child env, no route of the gateway's to an MCP + server (``mcp-``) and no SQL console's /svc key may have.""" computers, mcp_servers = list(computers), list(dict.fromkeys(mcp_servers)) problems = [f"computer {name!r} serves its tools under its name and '_': {problem}" for name in dict.fromkeys(computers) if (problem := tool_prefix_problem(f"{name}_"))] @@ -40,6 +41,8 @@ def name_problems(computers: Iterable[str], *, mcp_servers: Iterable[str] = (), for name in dict.fromkeys(computers) if counts[name] > 1] problems += [f"{name!r} is the gateway's route to MCP server {routes[name]!r}, and the gateway knows a computer by its " "name alone" for name in dict.fromkeys(computers) if name in routes] + problems += [f"{name!r} is the gateway's route to a SQL console, and the gateway knows a computer by its name alone" + for name in dict.fromkeys(computers) if name in (PGWEB_SERVICE_NAME, DB_MCP_SERVICE_NAME)] return problems diff --git a/src/agent_env/env/gateway/constants.py b/src/agent_env/env/gateway/constants.py index 26e3133d9..01ea981c2 100644 --- a/src/agent_env/env/gateway/constants.py +++ b/src/agent_env/env/gateway/constants.py @@ -77,6 +77,9 @@ def data_plane_load_timeout_s(size_bytes: int | None) -> int: # 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 env's SQL consoles (pgweb, db-mcp), each as `key=url` on the env's network, comma-separated. The gateway serves +# each at /svc//, every path of it behind its control token, and none of them when it has no token. +CONSOLE_URLS_ENV = "CONSOLE_URLS" # The route a clock follower long-polls, with its read token. CLOCK_WATCH_PATH = "/clock/watch" CLOCK_FOLLOWERS_PATH = "/clock/followers" diff --git a/src/agent_env/env/gateway/entrypoint.py b/src/agent_env/env/gateway/entrypoint.py index ad7f3c833..48d9217f1 100644 --- a/src/agent_env/env/gateway/entrypoint.py +++ b/src/agent_env/env/gateway/entrypoint.py @@ -8,7 +8,7 @@ import httpx from . import AGENT_ENV_GATEWAY_MCP_PORT, GatewayMode, InternalMCPServer -from .constants import (CLOCK_FOLLOWERS_ENV, COMPUTERS_ENV, CONTROL_TOKEN_CHILDREN_ENV, CONTROL_TOKEN_ENV, DEFAULT_MCP_SERVER_NAME, +from .constants import (CLOCK_FOLLOWERS_ENV, COMPUTERS_ENV, CONSOLE_URLS_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 @@ -165,8 +165,9 @@ async def _site_card(client: httpx.AsyncClient, base: str, deadline: float) -> d await asyncio.sleep(_INTERNAL_SERVER_POLL_S) -def parse_name_url_map(env_str: str) -> dict[str, str]: - # Expected format: 'slack=http://slack-website-backend:8000,email=http://email-website-backend:8000' +def parse_name_url_map(variable: str) -> dict[str, str]: + """The environment variable ``variable`` as a name-to-URL map, from 'slack=http://slack-website-backend:8000,...'.""" + env_str = os.environ.get(variable, "") urls: dict[str, str] = {} if not env_str.strip(): return urls @@ -175,16 +176,16 @@ def parse_name_url_map(env_str: str) -> dict[str, str]: if not entry: continue if "=" not in entry: - raise ValueError(f"Invalid REST_PROXY_URLS entry: '{entry}' (expected 'name=url')") + raise ValueError(f"Invalid {variable} entry: '{entry}' (expected 'name=url')") name, url = entry.split("=", 1) name = name.strip() url = url.strip() if not name: - raise ValueError(f"Empty service name in REST_PROXY_URLS entry: '{entry}'") + raise ValueError(f"Empty service name in {variable} entry: '{entry}'") if not url.startswith(("http://", "https://")): - raise ValueError(f"Invalid URL in REST_PROXY_URLS: '{url}' (must start with http:// or https://)") + raise ValueError(f"Invalid URL in {variable}: '{url}' (must start with http:// or https://)") if name in urls: - raise ValueError(f"Duplicate service name in REST_PROXY_URLS: '{name}'") + raise ValueError(f"Duplicate service name in {variable}: '{name}'") urls[name] = url return urls @@ -223,8 +224,8 @@ def parse_site_names(env_str: str) -> dict[str, tuple[str, ...]]: spec=os.environ.get("INTERNAL_MCP_SERVERS", ""), port=int(os.environ.get("INTERNAL_MCP_PORT", str(AGENT_ENV_GATEWAY_MCP_PORT))), ) - website_urls = parse_name_url_map(os.environ.get("WEBSITE_URLS", "")) - rest_proxy_urls = parse_name_url_map(os.environ.get("REST_PROXY_URLS", "")) + website_urls = parse_name_url_map("WEBSITE_URLS") + rest_proxy_urls = parse_name_url_map("REST_PROXY_URLS") internal_mcp_servers += asyncio.run(website_mcp_servers( {name: rest_proxy_urls.get(name, url) for name, url in website_urls.items()}, taken=frozenset(server.name for server in internal_mcp_servers))) @@ -239,6 +240,7 @@ def parse_site_names(env_str: str) -> dict[str, tuple[str, ...]]: internal_mcp_servers=internal_mcp_servers, website_urls=website_urls, rest_proxy_urls=rest_proxy_urls, + console_urls=parse_name_url_map(CONSOLE_URLS_ENV), gateway_mode=gateway_mode, service_db_url=service_db_url, control_token=os.environ.get(CONTROL_TOKEN_ENV), diff --git a/src/agent_env/env/gateway/gateway.py b/src/agent_env/env/gateway/gateway.py index 32021167f..f6111d804 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -182,6 +182,7 @@ def __init__( clock_followers: Mapping[str, str] | None = None, site_names: dict[str, Iterable[str]] | None = None, listed_by_name: Iterable[str] = (), + console_urls: Mapping[str, str] | None = None, ): """`provider_headers` names the headers the sandbox provider in front of the gateway is sent, its credentials, which the gateway takes off every request it forwards to a service behind it. `clock_followers` names the @@ -191,7 +192,8 @@ def __init__( 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. `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.""" + requests its browser sends take. `console_urls` are the env's SQL consoles by key, each served at /svc// + only with the control token, and not at all without one.""" self.host = host self.port = port self.server_name = server_name @@ -214,6 +216,9 @@ 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._consoles = dict(console_urls or {}) if control_token else {} + if clashes := sorted(self._consoles.keys() & self.rest_proxy_urls.keys()): + raise ValueError(f"console keys {clashes} are also backing servers' /svc keys") 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. @@ -267,14 +272,17 @@ async def list_website_urls() -> list[str]: self._env_get_time_url_cache: str | None = None self._install_role_filters() - if self.rest_proxy_urls: + if self.rest_proxy_urls or self._consoles: @self._mcp.custom_route( "/svc/{service_name}/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"], ) async def proxy_to_service(request: Request) -> Response: + if request.path_params["service_name"] in self._consoles: + return await self._proxy_console_request(request) return await self._proxy_rest_request(request) + if self.rest_proxy_urls: @self._mcp.custom_route(BROWSER_ROUTE, methods=["GET", "HEAD"]) async def browser_discovery(request: Request) -> Response: return await self._browsers.discovery(request) @@ -640,14 +648,17 @@ async def app(scope, receive, send): return app def _takes_control_token(self, scope: dict) -> bool: - """Whether a request takes the deploy's control token: a control route, the env's data plane, or one of - ``token_paths`` behind /svc, by the path as routed or by the one the backing server receives (``reaches``), - after dot-segment normalisation. A site's /website route never reaches its data plane.""" + """Whether a request takes the deploy's control token: a control route, the env's data plane, any path of a SQL + console behind /svc, or one of ``token_paths`` behind /svc, by the path as routed or by the one the backing + server receives (``reaches``), after dot-segment normalisation. A site's /website route never reaches its data + plane.""" path = scope["path"] if path in CONTROL_ROUTES or is_child_mcp_path(path, RPC_PATH): return True prefix, _, rest = path.lstrip("/").partition("/") service, _, rest = rest.partition("/") + if prefix == "svc" and service in self._consoles: + return True guarded = self._token_paths.get(service, ()) if prefix == "svc" else () raw, base = scope.get("raw_path") or path.encode(), self.rest_proxy_urls.get(service, "") return any(reaches(base, raw, path, rest, endpoint) for endpoint in guarded) @@ -905,6 +916,13 @@ async def log_write(status: int) -> None: on_done=log_write, withheld=self._withheld, mcp_endpoint=mcp_endpoint, site_names=site_names) + async def _proxy_console_request(self, request: Request) -> Response: + """Forward a request to one of the env's SQL consoles. Only a holder of the control token reaches one, and what + it writes there is no role's, so it opens no write window.""" + return await forward(request, self._consoles[request.path_params["service_name"]], + role=self._request_role(request), timeout=self.REST_PROXY_TIMEOUT, mcp_server=False, + withheld=self._withheld) + async def _proxy_website_request(self, request: Request) -> Response: """Proxy a website request; with a website or state trigger registered, feed it to the engine and hold barriers. A site's data plane and card, and every path its card advertises for agentenv (``site_reserved_paths``), are diff --git a/src/agent_env/providers/env_providers/env_gateway_provider.py b/src/agent_env/providers/env_providers/env_gateway_provider.py index 2e993e8b8..3cbcc3323 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -37,6 +37,7 @@ from agent_env.env.gateway.constants import ( CLOCK_FOLLOWERS_ENV, COMPUTERS_ENV, + CONSOLE_URLS_ENV, CONTROL_TOKEN_CHILDREN_ENV, DEFAULT_MCP_SERVER_NAME, GATEWAY_PROVIDER_HEADERS_ENV, @@ -49,10 +50,7 @@ ) from agent_env.env.envs.service_db import ( DATABASE_SERVICE_NAME, - DB_MCP_CONTAINER_PORT, - DB_MCP_PORT, DB_MCP_SERVICE_NAME, - DB_WEB_PORT, PGWEB_SERVICE_NAME, ) from agent_env.env.envs.computer.utils.boot import control_environment @@ -142,8 +140,8 @@ class DeployedGateway: gateway_url: str mcp_url: str - db_web_url: str - db_mcp_url: str | None = None + db_web_gateway_url: str | None = None + db_mcp_gateway_url: str | None = None website_frontend_urls: dict[str, str] | None = None mcp_server_name: str | None = None # Ids of the EnvStateInstance record(s) this deploy acquired; the caller records them @@ -208,6 +206,25 @@ def image(self) -> str: return self.image_artifact.image_name +async def _console_answer(client: httpx.AsyncClient, url: str, headers: dict[str, str]) -> int | str: + """The status ``url`` answers, or the name of the error when it doesn't answer. A console still booting, or a + sandbox tunnel dropping a connection, leaves a timeout, a connection error or a 502-504, so such an answer is asked + again, ``_CONSOLE_ATTEMPTS`` times in all.""" + answer: int | str = "unasked" + for attempt in range(_CONSOLE_ATTEMPTS): + if attempt: + await asyncio.sleep(_CONSOLE_RETRY_PAUSE_S) + try: + answer = (await client.get(url, headers=headers)).status_code + if answer not in (502, 503, 504): + return answer + except httpx.TransportError as e: + answer = type(e).__name__ + except httpx.HTTPError as e: + return type(e).__name__ + return answer + + class EnvironmentGatewayProvider(_SandboxEnvironmentProvider): """Provider for creating gateway deployments.""" @@ -548,6 +565,8 @@ def create_docker_compose( gateway_env.append(f" - REST_PROXY_URLS={','.join(rest_proxy_urls)}") gateway_env.append(f" - GATEWAY_MODE={gateway_mode.value}") gateway_env.append(f" - SERVICE_DB_URL={state_provider.base_url(state_instance)}") + if consoles := state_provider.console_urls(): + gateway_env.append(f" - {CONSOLE_URLS_ENV}={','.join(f'{k}={v}' for k, v in consoles.items())}") gateway_env.extend(f" - {key}={value}" for key, value in control_environment(control_token).items()) gateway_env.extend(f" - {key}={value}" for key, value in clock_followers_environment(external_children).items()) gateway_env.extend(f" - {key}={value}" @@ -698,10 +717,10 @@ async def deploy( env_version=env.version, env_provider_type=self.type, gateway_url=deployed.gateway_url, - db_web_url=deployed.db_web_url, sandbox_id=self._sandbox.sandbox_id, sandbox_type=self._sandbox.type, sandbox_ids=self._sandbox_ids(always_name_gateway=topology.names_gateway_on_vm), - db_mcp_url=deployed.db_mcp_url, + db_web_gateway_url=deployed.db_web_gateway_url, + db_mcp_gateway_url=deployed.db_mcp_gateway_url, environment_card_url=f"{deployed.gateway_url}{WELL_KNOWN_PATH}", environment_card=deployed.environment_card, environment_card_read_at_utc=deployed.environment_card_read_at_utc, @@ -858,7 +877,7 @@ async def _deploy_via_vm( if memory_mb is not None: vm_kwargs["memory"] = memory_mb self._sandbox = await sandbox_provider.create_vm( - exposed_ports=[gateway_port, DB_WEB_PORT, DB_MCP_PORT] + [sc.host_port for sc in sidecars], + exposed_ports=[gateway_port] + [sc.host_port for sc in sidecars], timeout=ttl_seconds, disk_size_gb=disk_size_gb, attribution=attribution, @@ -978,13 +997,6 @@ async def _stand_up(_spec): logger.warning(f"{name} container not found") gateway_url = sandbox.tunnel_urls.get(gateway_port) - db_web_base_url = sandbox.tunnel_urls.get(DB_WEB_PORT) if PGWEB_SERVICE_NAME in sidecar_names else None - db_web_url = f"{db_web_base_url}/" if db_web_base_url else None - db_mcp_base_url = sandbox.tunnel_urls.get(DB_MCP_PORT) if DB_MCP_SERVICE_NAME in sidecar_names else None - db_mcp_url = f"{db_mcp_base_url}/mcp" if db_mcp_base_url else None - logger.info(f"pgweb URL: {db_web_url}") - if db_mcp_url: - logger.info(f"DB MCP endpoint: {db_mcp_url}") # Wait for the tunnel to serve the env card, which the deploy records card = await self._wait_for_tunnel(gateway_url, timeout=120) @@ -997,6 +1009,7 @@ async def _stand_up(_spec): await self._refuse_tool_name_clashes(gateway_url) mcp_url = _mcp_url(gateway_url, card) logger.info(f"MCP endpoint: {mcp_url}") + consoles = await self._served_consoles(gateway_url, sidecar_names, control_token) website_frontend_urls = _frontend_urls(gateway_url, website_configs) for svc_name, url in (website_frontend_urls or {}).items(): @@ -1004,7 +1017,7 @@ async def _stand_up(_spec): logger.info(f"Gateway URL: {gateway_url}") return DeployedGateway( - gateway_url=gateway_url, mcp_url=mcp_url, db_web_url=db_web_url, db_mcp_url=db_mcp_url, + gateway_url=gateway_url, mcp_url=mcp_url, db_web_gateway_url=consoles.get(PGWEB_SERVICE_NAME), db_mcp_gateway_url=consoles.get(DB_MCP_SERVICE_NAME), website_frontend_urls=website_frontend_urls, mcp_server_name=mcp_server_name, env_state_instance_ids=[state_instance.instance_id], environment_card=card, environment_card_read_at_utc=card_read_at_utc, control_token=control_token, @@ -1086,8 +1099,7 @@ async def _deploy_via_containers( external: list[ExternalChild] = [] mcp_results: list[tuple[str, "Sandbox"]] = [] - pgweb_sb = None - db_mcp_sb = None + consoles: dict[str, str] = {} errors: list[BaseException] = [] for i, r in enumerate(all_results): if isinstance(r, BaseException): @@ -1102,11 +1114,11 @@ async def _deploy_via_containers( external = r else: spec = sidecar_specs[i - len(mcp_coros)] + if r.private_host: + consoles[spec.name] = f"http://{r.private_host}:{spec.port}" if spec.name == PGWEB_SERVICE_NAME: - pgweb_sb = r self._pgweb_sandbox = r elif spec.name == DB_MCP_SERVICE_NAME: - db_mcp_sb = r self._db_mcp_sandbox = r if errors: @@ -1128,6 +1140,7 @@ async def _deploy_via_containers( *(f"{c.name}={sandbox_provider.url_from_sandbox(c.base_url)}" for c in external)]), "GATEWAY_MODE": gateway_mode.value, "SERVICE_DB_URL": self._state_provider.base_url(state_instance), + **({CONSOLE_URLS_ENV: ",".join(f"{k}={v}" for k, v in consoles.items())} if consoles else {}), "MCP_SERVER_NAME": mcp_server_name or DEFAULT_MCP_SERVER_NAME, **control_environment(control_token), **clock_followers_environment(external), @@ -1155,13 +1168,12 @@ async def _deploy_via_containers( card_read_at_utc = datetime.now(timezone.utc).isoformat() await self._refuse_tool_name_clashes(gateway_url) - db_web_url = pgweb_sb.tunnel_urls[DB_WEB_PORT] + "/" if pgweb_sb else None - db_mcp_url = db_mcp_sb.tunnel_urls[DB_MCP_CONTAINER_PORT] + "/mcp" if db_mcp_sb else None + served = await self._served_consoles(gateway_url, consoles, control_token) return DeployedGateway( gateway_url=gateway_url, mcp_url=_mcp_url(gateway_url, card), - db_web_url=db_web_url, - db_mcp_url=db_mcp_url, + db_web_gateway_url=served.get(PGWEB_SERVICE_NAME), + db_mcp_gateway_url=served.get(DB_MCP_SERVICE_NAME), website_frontend_urls=None, mcp_server_name=mcp_server_name, env_state_instance_ids=[state_instance.instance_id], @@ -1173,6 +1185,32 @@ async def _deploy_via_containers( await self.close() raise + async def _served_consoles(self, gateway_url: str, consoles: Iterable[str], + control_token: str | None) -> dict[str, str]: + """Each of ``consoles`` at the gateway, by key, that the gateway guards and reaches: it refuses a request without + its control token, and with the token it gets the console's answer rather than a 5xx. An older gateway image + answers 404, and a console the gateway can't reach (a server bound to an address family the private network + lacks) leaves it a 502, so the record names neither.""" + keys = list(consoles) + if not keys or not control_token: + return {} + + async def served(client: httpx.AsyncClient, key: str) -> bool: + url = f"{gateway_url}/svc/{key}/" + if (bare := await _console_answer(client, url, {})) != 401: + logger.warning(f"the gateway answered {bare}, not 401, at /svc/{key}/: the record names no {key}") + return False + if not isinstance(answer := await _console_answer(client, url, control_headers(control_token)), int) \ + or answer >= 500: + logger.warning(f"the gateway answered {answer} with its token at /svc/{key}/: it can't reach {key}, so " + f"the record names none") + return False + return True + + async with httpx.AsyncClient(timeout=_CONSOLE_PROBE_TIMEOUT_S) as client: + found = await asyncio.gather(*(served(client, key) for key in keys)) + return {key: f"{gateway_url}/svc/{key}{_CONSOLE_PATHS[key]}" for key, ok in zip(keys, found) if ok} + async def _refuse_tool_name_clashes(self, gateway_url: str) -> None: """Refuse the deploy when the gateway reports tool names more than one of its sources serves, which leaves it serving none of its servers' tools. A gateway whose /state can't be read, or that doesn't report clashes (an @@ -1341,6 +1379,7 @@ async def _provision_sidecar(self, deploy: _ContainerDeploy, spec) -> Sandbox: command=spec.command, **_size_kwargs(deploy.cpu, None), disk_size_gb=deploy.disk_size_gb, timeout=deploy.ttl_seconds, + expose_externally=False, attribution=deploy.attribution, private_network=True, ) @@ -1700,6 +1739,8 @@ def _compose_name_problems(mcp_servers: list[MCPServerConfig], website_configs: problems += [f"two of its services would both be named {name!r}" for name, count in services.items() if count > 1] problems += [f"environment name {name!r} is one a gateway deploy names its own containers " f"({', '.join(sorted(GATEWAY_SERVICE_NAMES))})" for name in services if name in GATEWAY_SERVICE_NAMES] + problems += [f"website name {w.environment_name!r} is the /svc key of a SQL console the gateway serves" + for w in website_configs or [] if w.environment_name in _CONSOLE_PATHS] return problems @@ -1759,6 +1800,10 @@ def _http_probe(port: int, *paths: str) -> str: return f"{pick}else exit 1; fi; for u in {urls}; do get \"$$u\" 2>/dev/null && exit 0; done; exit 1" +# Where each SQL console's users open it under its /svc// route: pgweb's UI, db-mcp's MCP endpoint. +_CONSOLE_PATHS = {PGWEB_SERVICE_NAME: "/", DB_MCP_SERVICE_NAME: "/mcp"} + + def _network_lines(*networks: str) -> list[str]: return [" networks:", *(f" - {network}" for network in networks)] @@ -1816,6 +1861,10 @@ def _seconds(seconds: float) -> str: # Budget for reading the gateway's /state once its card is served: answering it runs tool discovery, one server at a # time, if startup's didn't complete. _STATE_READ_TIMEOUT_S = 60.0 +# Asking the gateway for each SQL console at deploy: each request's budget, and how often and how far apart it asks. +_CONSOLE_PROBE_TIMEOUT_S = 30.0 +_CONSOLE_RETRY_PAUSE_S = 5.0 +_CONSOLE_ATTEMPTS = 4 # Cap on the post-readiness tools/list probe; it only logs, so a slow gateway costs deploy time, never the deploy. _TOOLS_PROBE_TIMEOUT_S = 10.0 diff --git a/src/agent_env/providers/env_state/env_state_provider.py b/src/agent_env/providers/env_state/env_state_provider.py index f0e1ff93d..ef031fc56 100644 --- a/src/agent_env/providers/env_state/env_state_provider.py +++ b/src/agent_env/providers/env_state/env_state_provider.py @@ -207,6 +207,11 @@ def render_sidecar_containers( UIs). Optional: a backend may have none, in which case this returns an empty list.""" return [] + def console_urls(self) -> dict[str, str]: + """The SQL consoles ``render_sidecar_containers`` emits, by service name, at their addresses on the env's + network; the gateway serves each behind its control token. Empty for a backend without sidecars.""" + return {} + def render_compose_containers( self, environment_names: list[str], diff --git a/src/agent_env/providers/env_state/local_postgres.py b/src/agent_env/providers/env_state/local_postgres.py index 154d1e120..3f6d2e005 100644 --- a/src/agent_env/providers/env_state/local_postgres.py +++ b/src/agent_env/providers/env_state/local_postgres.py @@ -21,7 +21,6 @@ from agent_env.env.envs.service_db import ( DATABASE_SERVICE_NAME, DB_MCP_CONTAINER_PORT, - DB_MCP_PORT, DB_MCP_SERVICE_NAME, DB_NAME, DB_PASSWORD, @@ -30,7 +29,6 @@ PGWEB_SERVICE_NAME, SERVICE_DB_PORT, ) -from agent_env.providers.sandbox_providers.sandbox import port_bindings from agent_env.providers.env_state.env_state_provider import ( DatabaseStateProvider, DEFAULT_STATE_TTL_SECONDS, @@ -258,10 +256,8 @@ def render_sidecar_containers( host_ips: tuple[str, ...] = (), ) -> list[str]: """The pgweb + db-mcp direct-SQL browse UIs (VM/compose path), each gated on the - servicedb's health. Beyond the mandatory readiness service.""" - # Opened from the host only, so kept off the bridge address containers reach the host through. - host_ips = host_ips[:1] - publish = host_port or (lambda port: port) + servicedb's health. Beyond the mandatory readiness service. Neither publishes a port: only the gateway reaches + them, on the env's network, behind its control token.""" cfg = self.service_db_config lines: list[str] = [] if cfg.db_web_image: @@ -278,8 +274,6 @@ def render_sidecar_containers( # sidecar_specs for why: Disconnect tore down pgweb's shared # session and 500'd the changelog export on Save & Next). " - PGWEB_LOCK_SESSION=true", - " ports:", - *(f' - "{spec}"' for spec in port_bindings(host_ips, publish(DB_WEB_PORT), DB_WEB_PORT)), " networks:", " - env-network", "", @@ -295,8 +289,6 @@ def render_sidecar_containers( " environment:", f" - DATABASE_URI={self._pg_url(DATABASE_SERVICE_NAME, scheme='postgresql')}", " command: --transport=streamable-http --streamable-http-host=0.0.0.0 --access-mode=unrestricted", - " ports:", - *(f' - "{spec}"' for spec in port_bindings(host_ips, publish(DB_MCP_PORT), DB_MCP_CONTAINER_PORT)), " networks:", " - env-network", "", @@ -314,6 +306,10 @@ def rendered_sidecar_service_names(self) -> list[str]: names.append(DB_MCP_SERVICE_NAME) return names + def console_urls(self) -> dict[str, str]: + ports = {PGWEB_SERVICE_NAME: DB_WEB_PORT, DB_MCP_SERVICE_NAME: DB_MCP_CONTAINER_PORT} + return {name: f"http://{name}:{ports[name]}" for name in self.rendered_sidecar_service_names()} + @staticmethod def _in_image_store(image: str | None) -> bool: """Whether the image store serving ``image`` owns it: the container path pulls with that @@ -369,7 +365,9 @@ def sidecar_specs(self, *, instance: EnvStateInstance | None = None) -> list[Con specs.append(ContainerSpec( name=DB_MCP_SERVICE_NAME, image=cfg.db_mcp_image, port=DB_MCP_CONTAINER_PORT, env={"DATABASE_URI": db_mcp_uri}, - command=["--transport=streamable-http", "--streamable-http-host=0.0.0.0", "--access-mode=unrestricted"], + # An empty host binds every address family. The gateway reaches it over Modal's private network, which + # is IPv6 only, and 0.0.0.0 would serve IPv4 alone. + command=["--transport=streamable-http", "--streamable-http-host=", "--access-mode=unrestricted"], )) elif cfg.db_mcp_image: logger.warning(f"db-mcp image {cfg.db_mcp_image} is not in the image store; skipping db-mcp provisioning") diff --git a/tst/integration/env/custom_env_config_test.py b/tst/integration/env/custom_env_config_test.py index 8e9f31adc..c24afa0a5 100644 --- a/tst/integration/env/custom_env_config_test.py +++ b/tst/integration/env/custom_env_config_test.py @@ -51,7 +51,7 @@ async def deploy(self, **kwargs) -> DeployedEnv: env_id=self.id, env_version=self.version or 1, gateway_url=f"https://gw.example/{self.id}", mcp_url=f"https://mcp.example/{self.id}", - db_web_url=None, sandbox_id=f"sbx-{self.id}", + sandbox_id=f"sbx-{self.id}", metadata={"deployed_by": type(self).__name__, "flavor": self.flavor}, ) diff --git a/tst/integration/env/gateway/gateway_test.py b/tst/integration/env/gateway/gateway_test.py index 86b1977c9..a3a86efb1 100644 --- a/tst/integration/env/gateway/gateway_test.py +++ b/tst/integration/env/gateway/gateway_test.py @@ -18,6 +18,7 @@ from typing import NamedTuple from mcp import ClientSession from mcp.client.streamable_http import streamable_http_client +from mcp.shared._httpx_utils import create_mcp_http_client # Each test deploys a real GatewayEnv + MCP servers on sandbox VMs # (~5-15 min per test). Mark the entire module as slow. @@ -643,8 +644,8 @@ async def assert_generated_data(session, tools_result, tool_names): assert looked_up.instance_id == result.instance_id assert looked_up.gateway_url == result.gateway_url assert looked_up.mcp_url == result.mcp_url - assert looked_up.db_web_url == result.db_web_url - assert looked_up.db_mcp_url == result.db_mcp_url + assert looked_up.db_web_gateway_url == result.db_web_gateway_url + assert looked_up.db_mcp_gateway_url == result.db_mcp_gateway_url assert looked_up.environment_card_url == result.environment_card_url assert looked_up.sandbox_id == result.sandbox_id assert looked_up.gateway_mode == GatewayMode.PERFORMANCE.value @@ -1243,7 +1244,9 @@ async def assert_changelog_empty(session, tools_result, tool_names): logger.info(f"Changelog count after load: {content_text}") assert content_text == "[{'cnt': 0}]", f"Expected empty changelog after load, got {content_text}" - await _verify_list_tools(mcp_url=result.db_mcp_url, tool_verifier_callback=assert_changelog_empty) + assert result.db_mcp_gateway_url, "the deploy recorded no db-mcp at the gateway" + await _verify_list_tools(mcp_url=result.db_mcp_gateway_url, headers=result.control_headers_for(result.db_mcp_gateway_url), + tool_verifier_callback=assert_changelog_empty) # Make a modification via MCP tool async def send_email_for_changelog(session, tools_result, tool_names): @@ -1266,7 +1269,9 @@ async def assert_changelog_has_entry(session, tools_result, tool_names): assert "email" in content_text, f"Expected schema_name 'email' in changelog, got {content_text}" assert "INSERT" in content_text, f"Expected INSERT operation in changelog, got {content_text}" - await _verify_list_tools(mcp_url=result.db_mcp_url, tool_verifier_callback=assert_changelog_has_entry) + assert result.db_mcp_gateway_url, "the deploy recorded no db-mcp at the gateway" + await _verify_list_tools(mcp_url=result.db_mcp_gateway_url, headers=result.control_headers_for(result.db_mcp_gateway_url), + tool_verifier_callback=assert_changelog_has_entry) rehydrated = await MultiEnv.from_deployed_env(result) assert rehydrated._env_provider is not None @@ -1309,7 +1314,9 @@ async def assert_probe_in_changelog(session, tools_result, tool_names): logger.info(f"Post-rehydrate changelog query: {content_text}") assert rehydrate_probe_subject in content_text, f"Expected probe subject in changelog (triggers re-installed via rehydrated provider), got {content_text}" - await _verify_list_tools(mcp_url=result.db_mcp_url, tool_verifier_callback=assert_probe_in_changelog) + assert result.db_mcp_gateway_url, "the deploy recorded no db-mcp at the gateway" + await _verify_list_tools(mcp_url=result.db_mcp_gateway_url, headers=result.control_headers_for(result.db_mcp_gateway_url), + tool_verifier_callback=assert_probe_in_changelog) finally: await multi_env.close() @@ -1673,7 +1680,7 @@ async def _verify_step_api(gateway_url: str, tool_verifier_callback: callable): logger.info("/step test passed!") -async def _verify_list_tools(mcp_url: str, tool_verifier_callback: callable): +async def _verify_list_tools(mcp_url: str, tool_verifier_callback: callable, headers: dict[str, str] | None = None): """Connect to gateway and verify tools are available. Args: @@ -1685,7 +1692,8 @@ async def _verify_list_tools(mcp_url: str, tool_verifier_callback: callable): logger.info(f"Testing gateway via tunnel (attempt {attempt + 1}/{max_retries})...") verified = False try: - async with streamable_http_client(mcp_url) as (read_stream, write_stream, _): + async with create_mcp_http_client(headers=headers) as http_client, \ + streamable_http_client(mcp_url, http_client=http_client) as (read_stream, write_stream, _): async with ClientSession(read_stream, write_stream) as session: await session.initialize() tools_result = await session.list_tools() diff --git a/tst/integration/env/task_step/test_record_step_concurrency.py b/tst/integration/env/task_step/test_record_step_concurrency.py index 85204e6c3..5c2a94aab 100644 --- a/tst/integration/env/task_step/test_record_step_concurrency.py +++ b/tst/integration/env/task_step/test_record_step_concurrency.py @@ -44,7 +44,7 @@ def _assert_journal_matches(instance_id: str, n: int) -> None: def _env(instance_id: str) -> DeployedEnv: return DeployedGatewayEnv( env_id="e", env_version=1, gateway_url="g", mcp_url="m", - db_web_url=None, sandbox_id="s", instance_id=instance_id, + sandbox_id="s", instance_id=instance_id, ) diff --git a/tst/unit/bundle/run_teardown_test.py b/tst/unit/bundle/run_teardown_test.py index b5f1a501c..ef1b8e9f3 100644 --- a/tst/unit/bundle/run_teardown_test.py +++ b/tst/unit/bundle/run_teardown_test.py @@ -489,7 +489,7 @@ def test_the_keep_listing_names_every_sandbox_it_counts_with_the_records_behind_ deployed_sandboxes=[DeployedSandbox(sandbox_name="box", sandbox_id="sb-box", sandbox_mode="vm", sandbox_type="fake", tunnel_urls={"8080": "http://t"})], deployed_envs=[DeployedGatewayEnv(env_id="shop", env_version=1, gateway_url="http://g", mcp_url="http://m", - db_web_url="http://pgweb", sandbox_id="sb-gw", sandbox_type="fake", + db_web_gateway_url="http://gw/svc/pgweb/", sandbox_id="sb-gw", sandbox_type="fake", sandbox_ids={"service_db": {"orders": "sb-db"}})], deployed_agents=[DeployedAgent(agent_name="solver", api_url="http://a", a2a_url="http://a2a", sandbox_id="sb-box", sandbox_type="fake")], @@ -502,7 +502,7 @@ def test_the_keep_listing_names_every_sandbox_it_counts_with_the_records_behind_ "sb-gw fake env shop", " mcp http://m", " gateway http://g", - " pgweb http://pgweb", + " pgweb http://gw/svc/pgweb/", "sb-db fake env shop", ] diff --git a/tst/unit/cli/deploy_vm_sizing_test.py b/tst/unit/cli/deploy_vm_sizing_test.py index 33d86d805..163c91866 100644 --- a/tst/unit/cli/deploy_vm_sizing_test.py +++ b/tst/unit/cli/deploy_vm_sizing_test.py @@ -26,7 +26,7 @@ async def fake_deploy(**kwargs): captured.update(kwargs) return DeployedGatewayEnv( env_id="e1", env_version=1, gateway_url="http://gw", mcp_url="http://mcp", - db_web_url=None, sandbox_id="s1", + sandbox_id="s1", ) fake_env = MagicMock(id="e1", version=1, type="multi") diff --git a/tst/unit/cli/deployed_env_output_test.py b/tst/unit/cli/deployed_env_output_test.py index b3abb0748..7f72da27b 100644 --- a/tst/unit/cli/deployed_env_output_test.py +++ b/tst/unit/cli/deployed_env_output_test.py @@ -42,10 +42,31 @@ def test_env_get_instance_prints_the_gateway_url_only_for_a_gateway(deployed, ga def test_task_run_prints_a_gateway_record_as_before_and_omits_a_missing_gateway(): assert _format_env(_FRONTED).splitlines()[4:8] == [ - " gateway_url: https://gw.example", " mcp_url: https://gw.example/mcp", " db_web_url: None", " db_mcp_url: None"] + " gateway_url: https://gw.example", " mcp_url: https://gw.example/mcp", " db_web_gateway_url: None", + " db_mcp_gateway_url: None"] assert "gateway_url" not in _format_env(_BARE) and " mcp_url: https://srv.example/mcp" in _format_env(_BARE) +_CONSOLES = DeployedGatewayEnv(env_id="e1", env_version=1, gateway_url="https://gw.example", + mcp_url="https://gw.example/mcp", sandbox_id="gw", instance_id="i1", + db_web_gateway_url="https://gw.example/svc/pgweb/", + db_mcp_gateway_url="https://gw.example/svc/db-mcp/mcp") + + +def test_the_cli_prints_the_sql_consoles_at_the_gateway(): + async def fake_deploy(**kwargs): + return _CONSOLES + + with patch("agent_env.cli.env.deploy.Env") as Env, patch("agent_env.providers.build_sandbox_provider"): + Env.get.return_value = MagicMock(id="e1", version=1, type="mcp_server", deploy=fake_deploy) + deployed = CliRunner().invoke(env_cli, ["deploy", "--id", "e1"]).output + with patch("agent_env.env.store.get_env_instance_store") as store: + store.return_value.get.return_value = _CONSOLES + got = CliRunner().invoke(env_cli, ["get-instance", "--id", "i1"]).output + for output in (deployed, got, _format_env(_CONSOLES)): + assert "https://gw.example/svc/pgweb/" in output and "https://gw.example/svc/db-mcp/mcp" in output + + def test_a_record_outside_our_sandboxes_prints_no_sandbox(): with patch("agent_env.env.store.get_env_instance_store") as store: store.return_value.get.return_value = _HOSTED diff --git a/tst/unit/cli/env_state_type_parity_test.py b/tst/unit/cli/env_state_type_parity_test.py index a7587be3e..1bfebefa4 100644 --- a/tst/unit/cli/env_state_type_parity_test.py +++ b/tst/unit/cli/env_state_type_parity_test.py @@ -24,7 +24,7 @@ def _fake_deployed() -> DeployedEnv: return DeployedGatewayEnv( env_id="e1", env_version=1, gateway_url="http://gw", mcp_url="http://mcp", - db_web_url=None, sandbox_id="s1", + sandbox_id="s1", ) diff --git a/tst/unit/env/envs/test_env_card_loads.py b/tst/unit/env/envs/test_env_card_loads.py index 37c385d18..6ca760083 100644 --- a/tst/unit/env/envs/test_env_card_loads.py +++ b/tst/unit/env/envs/test_env_card_loads.py @@ -156,7 +156,7 @@ async def test_a_standalone_env_keeps_the_record_it_deploys(make, module): env = make() provider = MagicMock( spec=EnvironmentGatewayProvider, sandbox=SimpleNamespace(sandbox_id="sb-1", type="vm"), - deploy=AsyncMock(return_value=DeployedGatewayEnv(env_id="e", env_version=1, gateway_url=_GW, mcp_url=f"{_GW}/mcp", db_web_url=None, sandbox_id="sb-1")), + deploy=AsyncMock(return_value=DeployedGatewayEnv(env_id="e", env_version=1, gateway_url=_GW, mcp_url=f"{_GW}/mcp", sandbox_id="sb-1")), _environment_sandboxes={}, ) provider.environment_sandbox.return_value = None @@ -204,7 +204,7 @@ async def test_multi_env_deploy_hands_its_record_to_every_child_env(): container = MagicMock() provider.environment_sandbox.side_effect = lambda name: container if name == "email" else None # container mode: the server's own provider.deploy = AsyncMock(return_value=DeployedGatewayEnv(env_id="multi", env_version=1, gateway_url=_GW, mcp_url=f"{_GW}/mcp", - db_web_url=None, sandbox_id="sb-1")) + sandbox_id="sb-1")) with patch("agent_env.providers.build_env_provider", return_value=provider), \ patch("agent_env.providers.get_env_sandbox_provider", MagicMock()), \ @@ -297,8 +297,7 @@ async def fetch(client, key, base_url): def _record(card: dict | None) -> DeployedEnv: return DeployedGatewayEnv( - env_id="multi", env_version=1, gateway_url=_GW, mcp_url=f"{_GW}/mcp", db_web_url=None, - sandbox_id="sb-1", environment_card_url=f"{_CARD}{WELL_KNOWN_PATH}", environment_card=card, + env_id="multi", env_version=1, gateway_url=_GW, mcp_url=f"{_GW}/mcp", sandbox_id="sb-1", environment_card_url=f"{_CARD}{WELL_KNOWN_PATH}", environment_card=card, ) diff --git a/tst/unit/env/envs/test_mcp_server_container_mode.py b/tst/unit/env/envs/test_mcp_server_container_mode.py index b89ff3b37..db8d77e95 100644 --- a/tst/unit/env/envs/test_mcp_server_container_mode.py +++ b/tst/unit/env/envs/test_mcp_server_container_mode.py @@ -392,7 +392,7 @@ async def deploy_path(sandbox_provider, mcp_servers, mcp_server_images, **kwargs gp._environment_sandboxes = {"email": sandboxes["srv"]} gp._db_sandbox, gp._pgweb_sandbox, gp._db_mcp_sandbox = sandboxes.get("db"), sandboxes.get("pg"), sandboxes.get("dm") gp._container_sandboxes = [sandboxes[i] for i in ("db", "srv", "pg", "dm", "gw") if i in sandboxes] - return DeployedGateway(gateway_url=_GW, mcp_url=f"{_GW}/mcp", db_web_url=None, db_mcp_url=None, mcp_server_name=kwargs["mcp_server_name"], + return DeployedGateway(gateway_url=_GW, mcp_url=f"{_GW}/mcp", mcp_server_name=kwargs["mcp_server_name"], env_state_instance_ids=["st-1"], environment_card=_CARD, environment_card_read_at_utc="2026-09-26T00:00:00+00:00") async def server_path(env, sandbox_provider, **kwargs): diff --git a/tst/unit/env/envs/test_multi_env_computers.py b/tst/unit/env/envs/test_multi_env_computers.py index 93b1f0062..817dd1c63 100644 --- a/tst/unit/env/envs/test_multi_env_computers.py +++ b/tst/unit/env/envs/test_multi_env_computers.py @@ -187,7 +187,11 @@ def test_a_computer_name_of_fifteen_characters_is_taken(): ["'mcp-slack' is the gateway's route to MCP server 'slack', and the gateway knows a computer by its name alone"]), (["mcp-slack"], {"websites": ["slack"]}, []), (["desktop"], {"mcp_servers": ["shop"], "websites": ["shop"]}, []), # an MCP server and a website may share a name -], ids=["two-computers", "a-server", "a-website", "a-servers-route", "a-websites-isnt", "server-and-website"]) + (["pgweb", "db-mcp"], {}, + ["'pgweb' is the gateway's route to a SQL console, and the gateway knows a computer by its name alone", + "'db-mcp' is the gateway's route to a SQL console, and the gateway knows a computer by its name alone"]), +], ids=["two-computers", "a-server", "a-website", "a-servers-route", "a-websites-isnt", "server-and-website", + "a-console"]) def test_a_computer_needs_a_name_no_other_child_env_or_route_of_the_gateways_has(computers, children, problems): assert _multi_computers.name_problems(computers, **children) == problems diff --git a/tst/unit/env/envs/test_multi_env_name.py b/tst/unit/env/envs/test_multi_env_name.py index 35d78b764..2a1eefba2 100644 --- a/tst/unit/env/envs/test_multi_env_name.py +++ b/tst/unit/env/envs/test_multi_env_name.py @@ -57,7 +57,7 @@ def test_random_default_is_env_plus_four_digits(): def test_deployed_env_records_the_gateway_name(): - base = dict(env_id="e", env_version=1, gateway_url="g", mcp_url="m", db_web_url=None, sandbox_id="s") + base = dict(env_id="e", env_version=1, gateway_url="g", mcp_url="m", sandbox_id="s") assert DeployedEnv.from_dict({**base, "mcp_server_name": "env4821"}).mcp_server_name == "env4821" assert DeployedEnv.from_dict(base).mcp_server_name is None @@ -65,7 +65,8 @@ def test_deployed_env_records_the_gateway_name(): @pytest.mark.asyncio async def test_a_deploy_records_the_mcp_url_and_name_its_gateway_card_gives(): card = {"name": "crm", "additionalInterfaces": [{"url": "/mcp", "transport": "mcp"}]} - result = SimpleNamespace(gateway_url="https://gw.example", mcp_url="https://gw.example/mcp", db_web_url=None, db_mcp_url=None, + result = SimpleNamespace(gateway_url="https://gw.example", mcp_url="https://gw.example/mcp", db_web_gateway_url="https://gw.example/svc/pgweb/", + db_mcp_gateway_url="https://gw.example/svc/db-mcp/mcp", website_frontend_urls=None, environment_card=card, environment_card_read_at_utc="t", env_state_instance_ids=[], mcp_server_name="crm", control_token="t0k") @@ -82,3 +83,5 @@ async def deploy_gateway(self, sandbox_provider, **kwargs): record = await MultiEnv(id="e", version=1, mcp_server_envs=[], name="crm").deploy() assert (type(record), record.env_provider_type, record.mcp_url, record.mcp_server_name, record.control_token) == ( DeployedGatewayEnv, "gateway", "https://gw.example/mcp", "crm", "t0k") + assert (record.db_web_gateway_url, record.db_mcp_gateway_url) == ( + "https://gw.example/svc/pgweb/", "https://gw.example/svc/db-mcp/mcp") diff --git a/tst/unit/env/envs/test_multi_env_plugin_provider.py b/tst/unit/env/envs/test_multi_env_plugin_provider.py index f14b1369a..cf677b5e1 100644 --- a/tst/unit/env/envs/test_multi_env_plugin_provider.py +++ b/tst/unit/env/envs/test_multi_env_plugin_provider.py @@ -374,7 +374,7 @@ async def deploy_gateway(self, sandbox_provider, **kwargs): if len(calls) > 1: raise RuntimeError("second boom") self._sandbox = vm - return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url=None) + return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp") env = _multi("gateway", websites=()) with patch.object(EnvironmentGatewayProvider, "_deploy_gateway", deploy_gateway), \ @@ -434,7 +434,7 @@ async def test_close_after_two_successful_gateway_deploys_terminates_both_vms(): async def deploy_gateway(self, sandbox_provider, **kwargs): self._sandbox = next(created) - return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url=None) + return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp") env = _multi("gateway", websites=()) with patch.object(EnvironmentGatewayProvider, "_deploy_gateway", deploy_gateway), \ diff --git a/tst/unit/env/envs/test_multi_env_snapshot_guard.py b/tst/unit/env/envs/test_multi_env_snapshot_guard.py index fb4f479c2..a9fce8945 100644 --- a/tst/unit/env/envs/test_multi_env_snapshot_guard.py +++ b/tst/unit/env/envs/test_multi_env_snapshot_guard.py @@ -85,8 +85,7 @@ async def test_snapshot_capture_gated_off_for_remote_backend(): early with a clear error — not a cryptic 'servicedb container not found' deep in the capture.""" from agent_env.env.snapshot_store import EnvSnapshot - deployed = DeployedGatewayEnv(env_id="env-1", env_version=1, sandbox_id="sb-1", gateway_url="https://gw", mcp_url="https://gw/mcp", - db_web_url=None) + deployed = DeployedGatewayEnv(env_id="env-1", env_version=1, sandbox_id="sb-1", gateway_url="https://gw", mcp_url="https://gw/mcp") inst_store = MagicMock() inst_store.get.return_value = deployed inst_store.get_environment_universe.return_value = {"id": "universe-1", "version": 1} diff --git a/tst/unit/env/gateway/console_routes_test.py b/tst/unit/env/gateway/console_routes_test.py new file mode 100644 index 000000000..30a0257c3 --- /dev/null +++ b/tst/unit/env/gateway/console_routes_test.py @@ -0,0 +1,180 @@ +"""The env's SQL consoles (pgweb, db-mcp) are served behind /svc// only with the deploy's control token, which never +reaches them, and not at all by a gateway started without one.""" +from __future__ import annotations + +import asyncio +from urllib.parse import unquote, urlsplit + +import httpx +import pytest + +from agent_env.env.gateway import rest_proxy as rest_proxy_module +from agent_env.env.gateway.constants import CONSOLE_URLS_ENV +from agent_env.env.gateway.entrypoint import parse_name_url_map +from agent_env.env.gateway.gateway import Gateway + +TOKEN = "s3cret" +CONSOLES = {"pgweb": "http://pgweb:8081", "db-mcp": "http://db-mcp:8000"} +BACKING = {"mcp-svc": "http://svc:18765", "shop": "http://shop-backend:8000"} +_RealAsyncClient = httpx.AsyncClient + +# Every spelling that reaches a console: its root, its query API, db-mcp's MCP endpoint, and the same paths with dot +# segments, percent-encoding, doubled slashes or no trailing slash. +CONSOLE_PATHS = [("GET", "/svc/pgweb/"), ("GET", "/svc/pgweb"), ("GET", "/svc/pgweb/api/query?query=SELECT%201"), + ("POST", "/svc/pgweb/api/query"), ("GET", "/svc/pgweb/static/js/app.js"), + ("GET", "/svc/pgweb/x/../api/query"), ("GET", "/svc/%70gweb/api/query"), + ("GET", "/svc/pgweb//api/query"), ("GET", "/svc/pgweb/public%2Fx/../api/query"), + ("POST", "/svc/db-mcp/mcp"), ("GET", "/svc/db-mcp/mcp"), ("DELETE", "/svc/db-mcp/mcp"), + ("POST", "/svc/db-mcp/./mcp"), ("POST", "/svc/db-%6dcp/mcp"), ("POST", "/svc/db-mcp/%6dcp")] + + +class _Upstream: + """Every service behind the gateway: answers each request as an MCP server would, and keeps what reached it.""" + + def __init__(self, monkeypatch): + self.requests: list[tuple[str, str, str | None]] = [] + transport = httpx.MockTransport(self._answer) + monkeypatch.setattr(rest_proxy_module.httpx, "AsyncClient", lambda **kw: _RealAsyncClient(transport=transport, **kw)) + + def _answer(self, request: httpx.Request) -> httpx.Response: + self.requests.append((request.url.host, request.url.path, request.headers.get("authorization"))) + + async def body(): + yield b'event: message\ndata: {"jsonrpc": "2.0", "id": 1, "result": {}}\n\n' + + return httpx.Response(200, headers={"content-type": "text/event-stream", "mcp-session-id": "abc"}, content=body()) + + @property + def console_hosts(self) -> list[str]: + return [host for host, _, _ in self.requests if host in ("pgweb", "db-mcp")] + + +async def _send(gw: Gateway, method: str, path: str, headers: dict[str, str]) -> tuple[int, dict[bytes, bytes]]: + """One request through the gateway's served app with its path as uvicorn hands it: percent-decoded, its dot + segments and doubled slashes kept. Returns the status and the response headers.""" + url = urlsplit(path) + scope = {"type": "http", "asgi": {"version": "3.0", "spec_version": "2.3"}, "http_version": "1.1", "method": method, + "scheme": "http", "path": unquote(url.path), "raw_path": url.path.encode(), + "query_string": url.query.encode(), "root_path": "", "server": ("gateway", 80), "client": ("agent", 1), + "headers": [(b"content-type", b"application/json"), + *((name.lower().encode(), value.encode()) for name, value in headers.items())]} + received, started = False, [] + + async def receive(): + nonlocal received + if not received: + received = True + return {"type": "http.request", "body": b'{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}', + "more_body": False} + await asyncio.Event().wait() + + async def send(message): + if message["type"] == "http.response.start": + started.append((message["status"], dict(message.get("headers", ())))) + + await gw._asgi_app()(scope, receive, send) + return started[0] + + +def _gateway(token: str | None, consoles: dict[str, str] | None = None) -> Gateway: + return Gateway(host="127.0.0.1", port=0, server_name="unit", internal_mcp_servers=[], control_token=token, + rest_proxy_urls=dict(BACKING), website_urls={"shop": BACKING["shop"]}, + console_urls=CONSOLES if consoles is None else consoles) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("method, path", CONSOLE_PATHS) +@pytest.mark.parametrize("headers", [{}, {"Authorization": "Bearer wrong"}, {"Authorization": TOKEN}], + ids=["none", "wrong", "not-bearer"]) +async def test_a_console_refuses_a_request_without_the_token(monkeypatch, method, path, headers): + upstream = _Upstream(monkeypatch) + status, _ = await _send(_gateway(TOKEN), method, path, headers) + assert status == 401 + assert upstream.requests == [] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("method, path, host, sent", [ + ("GET", "/svc/pgweb/", "pgweb", "/"), ("GET", "/svc/pgweb/api/query", "pgweb", "/api/query"), + ("GET", "/svc/pgweb/static/js/app.js", "pgweb", "/static/js/app.js"), + ("GET", "/svc/pgweb/x/../api/query", "pgweb", "/api/query"), + ("POST", "/svc/db-mcp/mcp", "db-mcp", "/mcp"), ("DELETE", "/svc/db-mcp/mcp", "db-mcp", "/mcp")]) +async def test_a_console_answers_a_request_with_the_token_which_it_never_sees(monkeypatch, method, path, host, sent): + upstream = _Upstream(monkeypatch) + status, headers = await _send(_gateway(TOKEN), method, path, {"Authorization": f"Bearer {TOKEN}"}) + assert status == 200 + assert upstream.requests == [(host, sent, None)] + assert headers[b"mcp-session-id"] == b"abc" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("path", ["/svc/mcp-svc/../pgweb/api/query", "/svc/shop/../db-mcp/mcp", + "/svc/mcp-svc/x%2F..%2F..%2Fpgweb/api/query", "/svc/PGWEB/api/query", + "/svc//pgweb/api/query", "/website/shop/../../svc/pgweb/api/query", + "/svc/pgweb%2Fapi/query"]) +@pytest.mark.parametrize("headers", [{}, {"Authorization": f"Bearer {TOKEN}"}], ids=["none", "token"]) +async def test_no_other_route_reaches_a_console(monkeypatch, path, headers): + upstream = _Upstream(monkeypatch) + await _send(_gateway(TOKEN), "POST", path, headers) + assert upstream.console_hosts == [] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("method, path", CONSOLE_PATHS) +async def test_a_gateway_started_without_a_token_serves_no_console(monkeypatch, method, path): + upstream = _Upstream(monkeypatch) + status, _ = await _send(_gateway(None), method, path, {}) + assert status in (404, 307) # a bare /svc/pgweb is redirected to /svc/pgweb/, which is 404 too + assert upstream.requests == [] + + +@pytest.mark.asyncio +async def test_the_backing_servers_routes_are_unchanged_beside_the_consoles(monkeypatch): + upstream = _Upstream(monkeypatch) + status, _ = await _send(_gateway(TOKEN), "GET", "/svc/shop/api/orders", {}) + assert status == 200 + assert upstream.requests == [("shop-backend", "/api/orders", None)] + + +@pytest.mark.asyncio +async def test_consoles_alone_get_the_svc_route(monkeypatch): + upstream = _Upstream(monkeypatch) + gw = Gateway(host="127.0.0.1", port=0, server_name="unit", internal_mcp_servers=[], control_token=TOKEN, + console_urls=CONSOLES) + assert (await _send(gw, "POST", "/svc/db-mcp/mcp", {}))[0] == 401 + assert (await _send(gw, "POST", "/svc/db-mcp/mcp", {"Authorization": f"Bearer {TOKEN}"}))[0] == 200 + assert upstream.requests == [("db-mcp", "/mcp", None)] + + +def test_a_console_cannot_share_a_backing_servers_key(): + with pytest.raises(ValueError, match="shop"): + _gateway(TOKEN, {"shop": "http://pgweb:8081"}) + + +@pytest.mark.asyncio +async def test_a_write_through_a_console_opens_no_write_window(monkeypatch): + """Only a holder of the control token reaches a console, so what it writes there is credited to no role; a backing + server's write beside it still is.""" + upstream = _Upstream(monkeypatch) + gw = _gateway(TOKEN) + changelog, events = iter(range(1, 100)), [] + + async def log_event(event): + events.append(event) + return "event" + + monkeypatch.setattr(gw, "_query_changelog_id", lambda: next(changelog)) + monkeypatch.setattr(gw, "_log_event", log_event) + for path in ("/svc/db-mcp/mcp", "/svc/pgweb/api/query"): + assert (await _send(gw, "POST", path, {"Authorization": f"Bearer {TOKEN}"}))[0] == 200 + assert (await _send(gw, "POST", "/svc/shop/api/orders", {}))[0] == 200 + assert [(e["event_type"], e["service"]) for e in events] == [("http_request", "shop")] + assert upstream.console_hosts == ["db-mcp", "pgweb"] + + +@pytest.mark.parametrize("value, error", [("pgweb", "Invalid CONSOLE_URLS entry: 'pgweb'"), + ("pgweb=http://a:1,pgweb=http://b:2", "Duplicate service name in CONSOLE_URLS")]) +def test_a_bad_console_entry_names_its_own_variable(monkeypatch, value, error): + monkeypatch.setenv(CONSOLE_URLS_ENV, value) + with pytest.raises(ValueError, match=f"^{error}"): + parse_name_url_map(CONSOLE_URLS_ENV) diff --git a/tst/unit/env/store_test.py b/tst/unit/env/store_test.py index 1a401aa6d..ba1842947 100644 --- a/tst/unit/env/store_test.py +++ b/tst/unit/env/store_test.py @@ -25,9 +25,9 @@ def test_deployed_env_roundtrip_preserves_all_fields(): env_version=3, gateway_url="https://gw.example/", mcp_url="https://gw.example/mcp", - db_web_url="https://gw.example/dbweb/", + db_web_gateway_url="https://gw.example/svc/pgweb/", sandbox_id="sb-abc", - db_mcp_url="https://gw.example/dbmcp/mcp", + db_mcp_gateway_url="https://gw.example/svc/db-mcp/mcp", website_frontend_urls={"a": "https://gw.example/site/a/"}, sandbox_type="modal", vnc_url="vnc://example:5900", diff --git a/tst/unit/env/task_step/test_context_ops.py b/tst/unit/env/task_step/test_context_ops.py index 9e8e75b99..36713528b 100644 --- a/tst/unit/env/task_step/test_context_ops.py +++ b/tst/unit/env/task_step/test_context_ops.py @@ -16,7 +16,7 @@ def _env(instance_id: str = "i1") -> DeployedEnv: return DeployedGatewayEnv( env_id="e", env_version=1, gateway_url="g", mcp_url="m", - db_web_url=None, sandbox_id="s", instance_id=instance_id, + sandbox_id="s", instance_id=instance_id, ) diff --git a/tst/unit/env/task_step/verify_env_card_test.py b/tst/unit/env/task_step/verify_env_card_test.py index 359275014..226a69368 100644 --- a/tst/unit/env/task_step/verify_env_card_test.py +++ b/tst/unit/env/task_step/verify_env_card_test.py @@ -37,7 +37,7 @@ def _make_context(): ctx = TaskStepContext() ctx.deployed_envs = [DeployedGatewayEnv( env_id="e1", env_version=3, gateway_url="https://gw", mcp_url="https://gw/mcp", - db_web_url=None, sandbox_id="sb", environment_card_url=f"https://gw{WELL_KNOWN_PATH}", + sandbox_id="sb", environment_card_url=f"https://gw{WELL_KNOWN_PATH}", )] return ctx @@ -154,7 +154,7 @@ class TestWire: def test_reads_the_card_at_its_address(self, gateway_url): base = gateway_url.rstrip("/") ctx = TaskStepContext(deployed_envs=[DeployedGatewayEnv( - env_id="e1", env_version=3, gateway_url=gateway_url, mcp_url=f"{base}/mcp", db_web_url=None, sandbox_id="sb", + env_id="e1", env_version=3, gateway_url=gateway_url, mcp_url=f"{base}/mcp", sandbox_id="sb", environment_card_url=f"{gateway_url}{WELL_KNOWN_PATH}", )]) sent = [] diff --git a/tst/unit/env/task_step/verify_env_core_protocol_test.py b/tst/unit/env/task_step/verify_env_core_protocol_test.py index 857c516c6..5d7588752 100644 --- a/tst/unit/env/task_step/verify_env_core_protocol_test.py +++ b/tst/unit/env/task_step/verify_env_core_protocol_test.py @@ -28,7 +28,7 @@ def _make_context(): ctx = TaskStepContext() ctx.deployed_envs = [DeployedGatewayEnv( env_id="e1", env_version=3, gateway_url="https://gw", mcp_url="https://gw/mcp", - db_web_url=None, sandbox_id="sb", environment_card_url=f"https://gw{WELL_KNOWN_PATH}", + sandbox_id="sb", environment_card_url=f"https://gw{WELL_KNOWN_PATH}", )] return ctx @@ -154,7 +154,7 @@ class TestWire: def test_reads_the_card_and_probes_the_data_plane_at_the_cards_address(self, gateway_url): base = gateway_url.rstrip("/") ctx = TaskStepContext(deployed_envs=[DeployedGatewayEnv( - env_id="e1", env_version=3, gateway_url=gateway_url, mcp_url=f"{base}/mcp", db_web_url=None, sandbox_id="sb", + env_id="e1", env_version=3, gateway_url=gateway_url, mcp_url=f"{base}/mcp", sandbox_id="sb", environment_card_url=f"{gateway_url}{WELL_KNOWN_PATH}", control_token="s3cret", )]) sent = [] diff --git a/tst/unit/env/test_env.py b/tst/unit/env/test_env.py index a30119dba..59123ed34 100644 --- a/tst/unit/env/test_env.py +++ b/tst/unit/env/test_env.py @@ -73,8 +73,7 @@ async def fetch(client, key, base_url): def _record(card: dict | None, base: str = _BASES[0]) -> DeployedEnv: return DeployedGatewayEnv( - env_id="multi", env_version=1, gateway_url=base, mcp_url=f"{base.rstrip('/')}/mcp", db_web_url=None, - sandbox_id="sb-1", instance_id="inst-1", environment_card_url=f"{base}{WELL_KNOWN_PATH}", environment_card=card, + env_id="multi", env_version=1, gateway_url=base, mcp_url=f"{base.rstrip('/')}/mcp", sandbox_id="sb-1", instance_id="inst-1", environment_card_url=f"{base}{WELL_KNOWN_PATH}", environment_card=card, ) @@ -274,10 +273,13 @@ def test_a_card_without_a_name_leaves_the_server_name_unset(): ({"env_provider_type": "newer", "sandbox_id": "sb"}, DeployedSandboxEnv, "newer"), ({"sandbox_id": "sb"}, DeployedSandboxEnv, None), ({"gateway_url": None, "db_web_url": "https://pg/", "sandbox_id": "sb"}, DeployedGatewayEnv, "gateway"), + ({"gateway_url": None, "db_web_gateway_url": "https://gw/svc/pgweb/", "sandbox_id": "sb"}, DeployedGatewayEnv, + "gateway"), ({}, DeployedSandboxEnv, None), ({"env_provider_type": "newer"}, DeployedEnv, "newer"), ], ids=["gateway", "gateway-type-without-its-url", "before-the-type", "null-type", "newer-type-with-a-gateway", "newer-type-with-a-sandbox", - "sandbox-shape", "gateway-data-without-its-url", "untyped-without-a-sandbox", "newer-type-without-a-sandbox"]) + "sandbox-shape", "gateway-data-without-its-url", "gateway-console-without-its-url", + "untyped-without-a-sandbox", "newer-type-without-a-sandbox"]) def test_a_stored_record_loads_as_its_type_or_else_its_shape_keeping_every_field(doc, cls, provider_type): record = DeployedEnv.from_dict({"env_id": "e", "env_version": 1, "mcp_url": "https://m/mcp", "unknown_key": 1, **doc}) assert (type(record), record.env_provider_type, record.mcp_url) == (cls, provider_type, "https://m/mcp") @@ -287,7 +289,7 @@ def test_a_stored_record_loads_as_its_type_or_else_its_shape_keeping_every_field def test_a_gateway_record_is_filled_the_way_it_always_was(): record = DeployedEnv.from_dict({"env_id": "e", "env_version": 1, "mcp_url": "https://m/mcp", "gateway_url": "https://gw", "sandbox_id": "sb", "sandbox_ids": None, "env_state_instance_ids": None}) - assert (record.gateway_mode, record.sandbox_ids, record.env_state_instance_ids, record.db_web_url) == ("performance", {}, [], None) + assert (record.gateway_mode, record.sandbox_ids, record.env_state_instance_ids, record.db_web_gateway_url) == ("performance", {}, [], None) @pytest.mark.parametrize("record", [ @@ -295,7 +297,9 @@ def test_a_gateway_record_is_filled_the_way_it_always_was(): DeployedSandboxEnv(env_id="e", env_version=1, env_provider_type="server", sandbox_id="srv", sandbox_type="modal", sandbox_ids={"mcp_server": {"slack": "srv"}}, environment_card_url=_CARD_URL, environment_card=_CARD), DeployedGatewayEnv(env_id="e", env_version=1, gateway_url="https://gw.example/sb/x", gateway_mode="consistent", sandbox_id="gw", - sandbox_type="modal_vm", sandbox_ids={"gateway_server": "gw"}, db_web_url="https://pg/", db_mcp_url="https://dm/mcp", + sandbox_type="modal_vm", sandbox_ids={"gateway_server": "gw"}, + db_web_gateway_url="https://gw.example/sb/x/svc/pgweb/", + db_mcp_gateway_url="https://gw.example/sb/x/svc/db-mcp/mcp", website_frontend_urls={"shop": "https://gw/shop/"}, vnc_url="https://vnc", env_state_instance_ids=["st-1"], environment_card_url=_CARD_URL, environment_card=_CARD, environment_card_read_at_utc="2026-09-28T00:00:00+00:00", metadata={"k": "v"}, instance_id="i1", created_at_utc="c", expires_at_utc="x"), @@ -314,6 +318,19 @@ def test_the_gateway_only_fields_are_the_gateway_classs_own(): assert set(_GATEWAY_ONLY_FIELDS) == own - {"gateway_mode", "env_provider_type"} +def test_a_record_an_older_deploy_wrote_keeps_its_console_tunnels(): + """A gateway deployed before the consoles moved behind it is still alive, or is reused by id, and its record names + the consoles only at their tunnels, which a consumer reads until that gateway is gone.""" + old = {"env_id": "e", "env_version": 1, "env_provider_type": "gateway", "sandbox_id": "sb-1", + "gateway_url": "https://gw.example", "db_web_url": "https://pg.example/", + "db_mcp_url": "https://db-mcp.example/mcp"} + record = DeployedEnv.from_dict(old) + assert isinstance(record, DeployedGatewayEnv) + assert (record.db_web_url, record.db_mcp_url) == ("https://pg.example/", "https://db-mcp.example/mcp") + stored = dataclasses.asdict(record) + assert (stored["db_web_url"], stored["db_mcp_url"]) == ("https://pg.example/", "https://db-mcp.example/mcp") + + def test_only_a_gateway_record_has_a_gateway_url(): gateway = DeployedGatewayEnv(env_id="e", env_version=1, gateway_url="https://gw", sandbox_id="gw") bare = DeployedSandboxEnv(env_id="e", env_version=1, sandbox_id="srv") diff --git a/tst/unit/env/test_legacy_protocol.py b/tst/unit/env/test_legacy_protocol.py index d426e3259..682d04df8 100644 --- a/tst/unit/env/test_legacy_protocol.py +++ b/tst/unit/env/test_legacy_protocol.py @@ -229,8 +229,7 @@ def test_card_servers_of_a_leaf_card_is_the_env_itself_and_a_composed_card_servi def _record(card: dict | None, card_url: str | None = "https://env.example/.well-known/agent-env.json"): from agent_env.env.env import DeployedGatewayEnv - return DeployedGatewayEnv(env_id="e", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", db_web_url=None, - sandbox_id="sb", environment_card_url=card_url, environment_card=card) + return DeployedGatewayEnv(env_id="e", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", sandbox_id="sb", environment_card_url=card_url, environment_card=card) @pytest.mark.asyncio diff --git a/tst/unit/providers/compose_host_port_publish_test.py b/tst/unit/providers/compose_host_port_publish_test.py index c463bac02..d54e1078d 100644 --- a/tst/unit/providers/compose_host_port_publish_test.py +++ b/tst/unit/providers/compose_host_port_publish_test.py @@ -17,7 +17,6 @@ from agent_env.env.gateway import AGENT_ENV_GATEWAY_MCP_PORT from agent_env.providers.env_providers import EnvironmentGatewayProvider, MCPServerConfig, SidecarConfig -from agent_env.env.envs.service_db import DB_MCP_CONTAINER_PORT, DB_MCP_PORT, DB_WEB_PORT from agent_env.providers.env_state.local_postgres import LocalPostgresStateProvider SRC = Path(__file__).resolve().parents[3] / "src" @@ -201,9 +200,9 @@ def test_gateway_compose_resolves_every_host_port(stub_postgres, environments, s ) pairs = assert_all_resolved(compose) - # The gateway, pgweb and the DB MCP sidecar are always present; each extra sidecar adds - # one more publication. - assert len(pairs) == 3 + len(sidecars) + # The gateway is always published, and each extra sidecar adds one more publication; pgweb and the DB MCP + # sidecar publish none, since only the gateway reaches them. + assert len(pairs) == 1 + len(sidecars) def test_gateway_compose_defaults_to_identity(stub_postgres): @@ -218,35 +217,26 @@ def test_gateway_compose_defaults_to_identity(stub_postgres): state_instance=_StateInstance(), ) - assert f'"{DB_WEB_PORT}:{DB_WEB_PORT}"' in compose - assert f'"{DB_MCP_PORT}:{DB_MCP_CONTAINER_PORT}"' in compose + assert f'"{AGENT_ENV_GATEWAY_MCP_PORT}:{AGENT_ENV_GATEWAY_MCP_PORT}"' in compose assert '"18768:8000"' in compose -def test_postgres_sidecar_fragment_resolves_host_ports(stub_postgres): - """The state provider renders its own fragment, so it is checked directly as well as - through the full compose above.""" +@pytest.mark.parametrize("host_ips", [(), ("127.0.0.1",)], ids=["every-interface", "loopback"]) +def test_postgres_sidecars_publish_no_port(stub_postgres, host_ips): + """pgweb and db-mcp are reached only through the gateway, on the env's network, so the compose file publishes no + port for either.""" lines = "\n".join( - stub_postgres.render_sidecar_containers(["slack"], host_port=lambda p: p + OFFSET), + stub_postgres.render_sidecar_containers(["slack"], host_port=lambda p: p + OFFSET, host_ips=host_ips), ) - assert f'"{DB_WEB_PORT + OFFSET}:{DB_WEB_PORT}"' in lines - assert f'"{DB_MCP_PORT + OFFSET}:{DB_MCP_CONTAINER_PORT}"' in lines - # Container side untouched: it is the env's internal contract. - assert f'"{DB_WEB_PORT}:{DB_WEB_PORT}"' not in lines - - -def test_postgres_sidecar_fragment_defaults_to_identity(stub_postgres): - lines = "\n".join(stub_postgres.render_sidecar_containers(["slack"])) - - assert f'"{DB_WEB_PORT}:{DB_WEB_PORT}"' in lines - assert f'"{DB_MCP_PORT}:{DB_MCP_CONTAINER_PORT}"' in lines + assert " pgweb:" in lines and " db-mcp:" in lines + assert "ports:" not in lines + assert _PORT_PAIR.findall(lines) == [] @pytest.mark.parametrize("host_ips", [("127.0.0.1",), ("127.0.0.1", "172.17.0.1")], ids=["loopback", "loopback-and-bridge"]) def test_gateway_compose_publishes_only_on_the_host_ips(stub_postgres, host_ips): - """Each port once per host IP, and none on every interface. pgweb and db-mcp, opened only from the - host, stay off the bridge address.""" + """Each port once per host IP, and none on every interface.""" compose = EnvironmentGatewayProvider.create_docker_compose( EnvironmentGatewayProvider.__new__(EnvironmentGatewayProvider), mcp_servers=[MCPServerConfig(image="slack:test", environment_name="slack")], @@ -259,8 +249,6 @@ def test_gateway_compose_publishes_only_on_the_host_ips(stub_postgres, host_ips) ) everywhere = [(AGENT_ENV_GATEWAY_MCP_PORT, AGENT_ENV_GATEWAY_MCP_PORT), (18768, 8000)] - host_only = [(DB_WEB_PORT, DB_WEB_PORT), (DB_MCP_PORT, DB_MCP_CONTAINER_PORT)] assert sorted(_PORT_PAIR.findall(compose)) == sorted( [(ip, str(host + OFFSET), str(container)) for ip in host_ips for host, container in everywhere] - + [(host_ips[0], str(host + OFFSET), str(container)) for host, container in host_only] ) diff --git a/tst/unit/providers/env_providers/env_gateway_provider_test.py b/tst/unit/providers/env_providers/env_gateway_provider_test.py index ab9588196..00eb12294 100644 --- a/tst/unit/providers/env_providers/env_gateway_provider_test.py +++ b/tst/unit/providers/env_providers/env_gateway_provider_test.py @@ -344,6 +344,7 @@ async def _spy_acquire(self, ctx): # Patch _wait_for_tunnel to skip the real HTTP poll; it returns the card it read gp._wait_for_tunnel = AsyncMock(return_value=_CARD) gp._refuse_tool_name_clashes = AsyncMock() + gp._served_consoles = AsyncMock(return_value={}) # Patch Env.get and config to return mock envs with patch("agent_env.env.env.Env.get") as env_get, \ patch("agent_env.config.get_config") as get_cfg, \ @@ -433,8 +434,8 @@ async def _spy_acquire(self, ctx): gp._wait_for_tunnel.assert_awaited_once_with("https://agent-gateway.modal.host", timeout=120) assert result.environment_card == _CARD assert datetime.fromisoformat(result.environment_card_read_at_utc).utcoffset().total_seconds() == 0 - assert result.db_web_url is None - assert result.db_mcp_url is None + assert result.db_web_gateway_url is None + assert result.db_mcp_gateway_url is None @pytest.mark.asyncio @@ -462,6 +463,7 @@ async def fake_create_container(**kwargs): with patch.object(provider, "create_container", side_effect=fake_create_container): gp._wait_for_tunnel = AsyncMock(return_value=_CARD) gp._refuse_tool_name_clashes = AsyncMock() + gp._served_consoles = AsyncMock(return_value={}) with patch("agent_env.env.env.Env.get") as env_get, \ patch("agent_env.config.get_config") as get_cfg: gateway_env = MagicMock() @@ -521,6 +523,7 @@ async def fake_create_container(**kwargs): with patch.object(provider, "create_container", side_effect=fake_create_container): gp._wait_for_tunnel = AsyncMock(return_value=card) gp._refuse_tool_name_clashes = AsyncMock() + gp._served_consoles = AsyncMock(return_value={}) with patch("agent_env.env.env.Env.get") as env_get, patch("agent_env.config.get_config") as get_cfg: env_get.return_value.docker_image_artifact = _image("agent-gateway") get_cfg.return_value = MagicMock(default_gateway_env_id="gw-id") @@ -601,6 +604,7 @@ def _slack_in_containers(gp: EnvironmentGatewayProvider, provider: ModalSandboxP gp._init_service_db_via_exec = AsyncMock() gp._wait_for_tunnel = AsyncMock(return_value=_CARD) gp._refuse_tool_name_clashes = AsyncMock() + gp._served_consoles = AsyncMock(return_value={}) with patch("agent_env.env.env.Env.get") as env_get, \ patch("agent_env.config.get_config") as get_cfg: gateway_env = MagicMock() @@ -1486,6 +1490,8 @@ def _fake_env_get(env_id, *a, **k): sandbox.load_docker_images.assert_awaited_once() loaded = sandbox.load_docker_images.call_args.args[0] assert sidecar_art in loaded, "sidecar image_artifact was not side-loaded onto the VM" + # The VM exposes the gateway and the sidecars, and no port of pgweb's or db-mcp's: the gateway serves both. + assert sandbox_provider.create_vm.call_args.kwargs["exposed_ports"] == [18765, 18768] # --- name injection: SERVICE_NAME + ENVIRONMENT_NAME -------------------------- @@ -1871,7 +1877,7 @@ class _Stop(Exception): def _probed(mcp_url: str = "https://gw.example/mcp") -> DeployedGateway: - return DeployedGateway(gateway_url="https://gw.example", mcp_url=mcp_url, db_web_url=None, environment_card=_PROBE_CARD) + return DeployedGateway(gateway_url="https://gw.example", mcp_url=mcp_url, environment_card=_PROBE_CARD) def _probe_records(caplog) -> list[logging.LogRecord]: @@ -2099,9 +2105,7 @@ async def test_deploy_returns_the_gateway_record_for_the_env(caplog): assert re.fullmatch(r"env\d{4}", compose.call_args.kwargs["mcp_server_name"]) assert compose.call_args.kwargs["host_ips"] == () assert dataclasses.asdict(record) == dataclasses.asdict(DeployedGatewayEnv( - env_id="crm-env", env_version=3, gateway_url="https://vm.example", mcp_url="https://vm.example/mcp", db_web_url=None, - sandbox_id="vm-1", sandbox_type="modal_vm", sandbox_ids={}, db_mcp_url=None, - environment_card_url="https://vm.example/.well-known/agent-env.json", environment_card=card, + env_id="crm-env", env_version=3, gateway_url="https://vm.example", mcp_url="https://vm.example/mcp", sandbox_id="vm-1", sandbox_type="modal_vm", sandbox_ids={}, environment_card_url="https://vm.example/.well-known/agent-env.json", environment_card=card, environment_card_read_at_utc=record.environment_card_read_at_utc, gateway_mode="consistent", env_state_instance_ids=["st-ext"], control_token=record.control_token)) assert len(record.control_token) >= 43 and compose.call_args.kwargs["control_token"] == record.control_token @@ -2156,8 +2160,8 @@ async def test_deploy_records_the_gateways_sidecar_urls_and_its_containers(): async def deploy_gateway(sandbox_provider, **kwargs): gp._sandbox, gp._environment_sandboxes = sandboxes["gw"], {"slack": sandboxes["srv"]} gp._db_sandbox, gp._pgweb_sandbox, gp._db_mcp_sandbox = sandboxes["db"], sandboxes["pg"], sandboxes["dm"] - return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url="https://pg/", db_mcp_url="https://dm/mcp", - environment_card={"name": "env1234"}, env_state_instance_ids=["st-1"]) + return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_gateway_url="https://gw/svc/pgweb/", + db_mcp_gateway_url="https://gw/svc/db-mcp/mcp", environment_card={"name": "env1234"}, env_state_instance_ids=["st-1"]) with patch.object(gp, "_deploy_gateway", side_effect=deploy_gateway), \ patch("agent_env.env.env.Env.get", MagicMock()), \ @@ -2165,7 +2169,8 @@ async def deploy_gateway(sandbox_provider, **kwargs): patch.object(env_gateway_provider, "_tool_names", AsyncMock(return_value={"slack_send"})): record = await gp.deploy(env, ModalSandboxProvider()) - assert (record.gateway_url, record.db_web_url, record.db_mcp_url, record.env_state_instance_ids) == ("https://gw", "https://pg/", "https://dm/mcp", ["st-1"]) + assert (record.gateway_url, record.db_web_gateway_url, record.db_mcp_gateway_url, record.env_state_instance_ids) == ( + "https://gw", "https://gw/svc/pgweb/", "https://gw/svc/db-mcp/mcp", ["st-1"]) assert (record.sandbox_id, record.sandbox_type, record.website_frontend_urls, record.mcp_server_name) == ("gw", "modal", None, "env1234") assert record.sandbox_ids == { "gateway_server": "gw", "mcp_server": {"slack": "srv"}, "service_db": {"servicedb": "db", "pgweb": "pg", "db-mcp": "dm"}} @@ -2196,7 +2201,7 @@ async def test_a_deploy_on_an_external_state_store_needs_no_servicedb_env(make): async def deploy_gateway(sandbox_provider, **kwargs): gp._sandbox = MagicMock(sandbox_id="vm-1", type="modal_vm") - return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url=None, env_state_instance_ids=["st-ext"]) + return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", env_state_instance_ids=["st-ext"]) external = MagicMock(state_type="remote", instance_id="st-ext") with _deploying(gp, deploy_gateway) as deploying, \ @@ -2234,7 +2239,8 @@ async def test_deploy_fronts_a_multi_envs_servers_and_websites_under_its_name(): async def deploy_gateway(sandbox_provider, **kwargs): seen.update(kwargs) gp._sandbox = MagicMock(sandbox_id="vm-1", type="modal_vm") - return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url="https://pg/", website_frontend_urls={"shop": "https://shop"}, + return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_gateway_url="https://gw/svc/pgweb/", + website_frontend_urls={"shop": "https://shop"}, environment_card={"name": "crm"}, env_state_instance_ids=["st-1"]) with _deploying(gp, deploy_gateway) as deploying: @@ -2247,9 +2253,8 @@ async def deploy_gateway(sandbox_provider, **kwargs): assert [i.image_name for i in seen["website_images"]] == ["shop-be", "shop-fe", "blog-be", "blog-fe"] and seen["mcp_server_name"] == "crm" deploying.acquire.assert_awaited_once_with(env_state_type=None, ttl_seconds=60, name_hint="crm-suite", env_state_instance_id="st-1") assert dataclasses.asdict(record) == dataclasses.asdict(DeployedGatewayEnv( - env_id="crm-suite", env_version=2, env_provider_type="gateway", gateway_url="https://gw", db_web_url="https://pg/", - sandbox_id="vm-1", sandbox_type="modal_vm", sandbox_ids={"gateway_server": "vm-1"}, db_mcp_url=None, - environment_card_url="https://gw/.well-known/agent-env.json", environment_card={"name": "crm"}, environment_card_read_at_utc=None, + env_id="crm-suite", env_version=2, env_provider_type="gateway", gateway_url="https://gw", db_web_gateway_url="https://gw/svc/pgweb/", + sandbox_id="vm-1", sandbox_type="modal_vm", sandbox_ids={"gateway_server": "vm-1"}, environment_card_url="https://gw/.well-known/agent-env.json", environment_card={"name": "crm"}, environment_card_read_at_utc=None, website_frontend_urls={"shop": "https://shop"}, gateway_mode="performance", env_state_instance_ids=["st-1"])) assert (record.mcp_url, record.mcp_server_name, deploying.tool_names.await_count) == ("https://gw/mcp", "crm", 1) @@ -2266,7 +2271,7 @@ async def deploy_gateway(sandbox_provider, **kwargs): gp._sandbox = _FakeSandbox("gw", 18765, "https://gw") gp._environment_sandboxes = {name: _FakeSandbox(f"srv-{name}", 18765, "https://srv") for name in servers} gp._db_sandbox = _FakeSandbox("db", SERVICE_DB_PORT, "https://db") - return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url=None) + return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp") with _deploying(gp, deploy_gateway): record = await gp.deploy(MultiEnv(id="crm-suite", version=2, mcp_server_envs=[_mcp(n) for n in servers]), ModalSandboxProvider()) @@ -2280,7 +2285,7 @@ async def test_deploy_fronts_a_website_env(): async def deploy_gateway(sandbox_provider, **kwargs): seen.update(kwargs) gp._sandbox = MagicMock(sandbox_id="vm-1", type="modal_vm") - return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url=None, website_frontend_urls={"shop": "https://shop"}, + return DeployedGateway(gateway_url="https://gw", mcp_url="https://gw/mcp", website_frontend_urls={"shop": "https://shop"}, env_state_instance_ids=["st-1"]) with _deploying(gp, deploy_gateway): @@ -2292,8 +2297,7 @@ async def deploy_gateway(sandbox_provider, **kwargs): def _multi_record(env_state_instance_ids: list[str]) -> DeployedGatewayEnv: - return DeployedGatewayEnv(env_id="crm-suite", env_version=2, gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url=None, - sandbox_id="gw", sandbox_type="modal", env_state_instance_ids=env_state_instance_ids, + return DeployedGatewayEnv(env_id="crm-suite", env_version=2, gateway_url="https://gw", mcp_url="https://gw/mcp", sandbox_id="gw", sandbox_type="modal", env_state_instance_ids=env_state_instance_ids, sandbox_ids={"gateway_server": "gw", "mcp_server": {"slack": "srv"}, "service_db": {"servicedb": "db", "pgweb": "pg"}}) @@ -3052,6 +3056,7 @@ async def children(reach, control_token): gp._init_service_db_via_exec = AsyncMock() gp._wait_for_tunnel = AsyncMock(return_value=_CARD) gp._refuse_tool_name_clashes = AsyncMock() + gp._served_consoles = AsyncMock(return_value={}) with patch.object(provider, "create_container", side_effect=fake_create_container), \ patch("agent_env.env.env.Env.get", side_effect=lambda env_id, *a, **k: gateway_env if env_id == "gw-id" else db_env), \ patch("agent_env.config.get_config", return_value=MagicMock(default_gateway_env_id="gw-id", default_service_db_env_id="db-id")): @@ -3111,3 +3116,109 @@ def test_a_site_image_without_curl_passes_its_health_probe_once_its_health_path_ def test_a_site_answering_only_errors_fails_its_health_probe(tmp_path): assert _run_probe(tmp_path, {"/.well-known/agent-env.json": 500, "/api/health": 503}) != 0 + + +_TIMEOUT = httpx.ReadTimeout("slow") +_REFUSED = httpx.ConnectError("refused") +_DROPPED = httpx.RemoteProtocolError("Server disconnected without sending a response.") +_GUARDED = {"pgweb": ([401], [200]), "db-mcp": ([401], [404])} +_BOTH = {"pgweb": "https://gw.example/svc/pgweb/", "db-mcp": "https://gw.example/svc/db-mcp/mcp"} + + +@pytest.mark.asyncio +@pytest.mark.parametrize("answers, recorded", [ + pytest.param(_GUARDED, _BOTH, id="both-guarded-and-reached"), + pytest.param({**_GUARDED, "db-mcp": ([401], [502] * 3)}, {"pgweb": _BOTH["pgweb"]}, id="db-mcp-unreachable"), + pytest.param({**_GUARDED, "db-mcp": ([401], [502, 404])}, _BOTH, id="db-mcp-up-on-the-second-ask"), + pytest.param({**_GUARDED, "pgweb": ([_TIMEOUT, 401], [200])}, _BOTH, id="pgweb-slow-once"), + pytest.param({**_GUARDED, "pgweb": ([_TIMEOUT] * 3, [])}, {"db-mcp": _BOTH["db-mcp"]}, id="pgweb-silent"), + pytest.param({**_GUARDED, "pgweb": ([_DROPPED, 401], [_REFUSED, 200])}, _BOTH, id="pgweb-tunnel-drops-once"), + pytest.param({"pgweb": ([404], []), "db-mcp": ([404], [])}, {}, id="older-gateway"), + pytest.param({"pgweb": ([200], []), "db-mcp": ([200], [])}, {}, id="open"), + pytest.param({"pgweb": ([_REFUSED] * 3, []), "db-mcp": ([_REFUSED] * 3, [])}, {}, id="unreachable-gateway"), +]) +async def test_the_record_names_each_console_the_gateway_guards_and_reaches(monkeypatch, answers, recorded): + """A console counts when the gateway refuses it without the control token and, with the token, answers for it + rather than with a 5xx: an older gateway image answers 404, and a console bound where the gateway can't reach it + leaves the gateway a 502. A timeout, a dropped connection or a 5xx is asked again, three times in all here.""" + queues = {(key, with_token): list(replies) for key, (bare, token) in answers.items() + for with_token, replies in ((False, bare), (True, token))} + + def reply(request: httpx.Request) -> httpx.Response: + key = request.url.path.split("/")[2] + with_token = request.headers.get("authorization") == "Bearer tok" + answer = queues[key, with_token].pop(0) + if isinstance(answer, Exception): + raise answer + return httpx.Response(answer) + + real = httpx.AsyncClient + monkeypatch.setattr(httpx, "AsyncClient", lambda **kw: real(transport=httpx.MockTransport(reply), **kw)) + monkeypatch.setattr(env_gateway_provider, "_CONSOLE_RETRY_PAUSE_S", 0) + monkeypatch.setattr(env_gateway_provider, "_CONSOLE_ATTEMPTS", 3) + served = await EnvironmentGatewayProvider()._served_consoles("https://gw.example", ["pgweb", "db-mcp"], "tok") + assert served == recorded + + +@pytest.mark.asyncio +@pytest.mark.parametrize("consoles, token", [([], "tok"), (["pgweb", "db-mcp"], None)]) +async def test_a_deploy_without_consoles_or_a_token_asks_the_gateway_nothing(monkeypatch, consoles, token): + monkeypatch.setattr(httpx, "AsyncClient", MagicMock(side_effect=AssertionError("asked the gateway"))) + assert await EnvironmentGatewayProvider()._served_consoles("https://gw.example", consoles, token) == {} + + +@pytest.mark.parametrize("images, consoles", [ + ({"db_web_image": "pgweb:1", "db_mcp_image": "db-mcp:1"}, "pgweb=http://pgweb:8081,db-mcp=http://db-mcp:8000"), + ({"db_web_image": "pgweb:1"}, "pgweb=http://pgweb:8081"), ({}, None)], ids=["both", "pgweb", "none"]) +def test_compose_hands_the_gateway_the_sql_consoles_it_renders_on_their_network(images, consoles): + from agent_env.env.envs.service_db import ServiceDBConfig + + compose = EnvironmentGatewayProvider().create_docker_compose( + mcp_servers=[MCPServerConfig(image="mcp-slack", environment_name="slack")], gateway_image="agent-gateway", + state_provider=LocalPostgresStateProvider(service_db_config=ServiceDBConfig(**images)), + state_instance=LocalPostgresStateProvider.default_instance(), control_token="t0k") + services = yaml.safe_load(compose)["services"] + gateway = dict(item.split("=", 1) for item in services[GATEWAY_SERVICE_NAME]["environment"]) + assert gateway.get("CONSOLE_URLS") == consoles + for console in (consoles or "").split(","): + if console: + name = console.split("=", 1)[0] + assert set(services[name]["networks"]) & set(services[GATEWAY_SERVICE_NAME]["networks"]) + + +@pytest.mark.asyncio +async def test_container_mode_hands_the_gateway_its_consoles_on_the_private_network_and_records_them_there(monkeypatch): + """The gateway reaches pgweb and db-mcp at their private hosts, and the record names them under the gateway's URL + once the gateway refuses them without the token and serves them with it.""" + provider, gp = ModalSandboxProvider(), EnvironmentGatewayProvider() + made: dict[str, tuple[_FakeSandbox, dict]] = {} + + async def create_container(**kwargs): + name = kwargs["image_name"].rsplit("/", 1)[-1] + sandbox = _FakeSandbox(name, kwargs["port"], f"https://{name}.modal.host") + made[name] = (sandbox, kwargs) + return sandbox + + real = httpx.AsyncClient + monkeypatch.setattr(httpx, "AsyncClient", lambda **kw: real(transport=httpx.MockTransport( + lambda request: httpx.Response(404 if not request.url.path.startswith("/svc/") + else 200 if request.headers.get("authorization") == "Bearer t0k" else 401)), + **kw)) + with _slack_in_containers(gp, provider, create_container) as args: + del gp._served_consoles # the real check, asking the gateway + deployed = await gp._deploy_via_containers(sandbox_provider=provider, control_token="t0k", **args) + pgweb, db_mcp = made["pgweb"][0], made["db-mcp"][0] + assert {name: kwargs.get("expose_externally", True) for name, (_, kwargs) in made.items() + if name in ("pgweb", "db-mcp")} == {"pgweb": False, "db-mcp": False} + assert made["agent-gateway"][1]["env"]["CONSOLE_URLS"] == ( + f"pgweb=http://{pgweb.private_host}:8081,db-mcp=http://{db_mcp.private_host}:8000") + assert (deployed.db_web_gateway_url, deployed.db_mcp_gateway_url) == ( + "https://agent-gateway.modal.host/svc/pgweb/", "https://agent-gateway.modal.host/svc/db-mcp/mcp") + + +@pytest.mark.parametrize("name", ["pgweb", "db-mcp"]) +def test_a_website_cant_take_a_consoles_svc_key(name): + from agent_env.providers.env_providers.env_gateway_provider import _compose_name_problems + + site = WebsiteConfig(backend_image="be", frontend_image=None, environment_name=name) + assert any("SQL console" in problem for problem in _compose_name_problems([], [site])) diff --git a/tst/unit/providers/env_state/test_env_state_provider.py b/tst/unit/providers/env_state/test_env_state_provider.py index 995075473..b5afda781 100644 --- a/tst/unit/providers/env_state/test_env_state_provider.py +++ b/tst/unit/providers/env_state/test_env_state_provider.py @@ -278,6 +278,9 @@ def test_sidecar_specs_skip_images_the_image_store_does_not_own(image_store, cap # contributor can't tear down the shared session the changelog export relies on. pgweb_spec = next(s for s in both if s.name == "pgweb") assert pgweb_spec.env["PGWEB_LOCK_SESSION"] == "true" + # The gateway reaches db-mcp over Modal's private network, which is IPv6 only, so it listens on every family. + db_mcp_spec = next(s for s in both if s.name == "db-mcp") + assert "--streamable-http-host=" in db_mcp_spec.command # pgweb from Docker Hub + unset db-mcp → neither, and a warning for the configured one only. none = LocalPostgresStateProvider( diff --git a/tst/unit/store/derived_ids_test.py b/tst/unit/store/derived_ids_test.py index 94f4f99a0..92a7eb170 100644 --- a/tst/unit/store/derived_ids_test.py +++ b/tst/unit/store/derived_ids_test.py @@ -216,7 +216,7 @@ async def snapshot_env_state(**kwargs): monkeypatch.setattr("agent_env.env.env.Env.get", lambda *args: SimpleNamespace(id="crm-suite")) monkeypatch.setattr(SnapshotEnvTaskStep, "snapshot_env_state", staticmethod(snapshot_env_state)) deployed = DeployedGatewayEnv(env_id="crm-suite", env_version=1, gateway_url="https://gw", mcp_url="https://gw/mcp", - db_web_url=None, sandbox_id="sb-1") + sandbox_id="sb-1") assert asyncio.run(series._capture_env_state(TaskStepContext(deployed_envs=[deployed]), {}, 30)) is None (snapshot_id,) = snapshot_ids return snapshot_id diff --git a/tst/unit/store/fakes.py b/tst/unit/store/fakes.py index d36a66011..c50530054 100644 --- a/tst/unit/store/fakes.py +++ b/tst/unit/store/fakes.py @@ -292,7 +292,7 @@ def reattach_for_snapshot(monkeypatch, env_id: str, env_version: int, universe_i """Make ``EnvSnapshot.create`` find ``env_id`` deployed with ``universe_id`` loaded, and reconnect to a ``SnapshotSandbox``, which it returns.""" deployed = DeployedGatewayEnv(env_id=env_id, env_version=env_version, sandbox_id="sb-1", gateway_url="https://gw", - mcp_url="https://gw/mcp", db_web_url=None) + mcp_url="https://gw/mcp") instances = SimpleNamespace(get=lambda instance_id: deployed, get_environment_universe=lambda instance_id: {"id": universe_id, "version": universe_version}) provider = EnvironmentGatewayProvider() diff --git a/tst/unit/store/local_id_acceptance_test.py b/tst/unit/store/local_id_acceptance_test.py index 8f3498a05..97a9190d6 100644 --- a/tst/unit/store/local_id_acceptance_test.py +++ b/tst/unit/store/local_id_acceptance_test.py @@ -350,7 +350,7 @@ async def deploy(self, **kwargs): store.insert("env_state_instances", {"instance_id": f"esi-{self.id}", "state_type": "none"}) store.insert("env_instances", {"instance_id": f"gateway-for-{self.id}", "env_id": "gateway"}) return SimpleNamespace( - instance_id="i", mcp_url="m", gateway_url="g", db_web_url=None, db_mcp_url=None, vnc_url=None, + instance_id="i", mcp_url="m", gateway_url="g", vnc_url=None, website_frontend_urls=None, expires_at_utc=None, a2a_url="a", sandbox_id="s", agent_card={}, ) diff --git a/tst/unit/task/local_run_write_fence_test.py b/tst/unit/task/local_run_write_fence_test.py index 6c62f3ade..b9f022574 100644 --- a/tst/unit/task/local_run_write_fence_test.py +++ b/tst/unit/task/local_run_write_fence_test.py @@ -281,8 +281,7 @@ def exports(monkeypatch): def _deployed(env_id): - return DeployedGatewayEnv(env_id=env_id, env_version=1, gateway_url="https://gw", mcp_url="https://gw/mcp", db_web_url=None, - sandbox_id="sb-1") + return DeployedGatewayEnv(env_id=env_id, env_version=1, gateway_url="https://gw", mcp_url="https://gw/mcp", sandbox_id="sb-1") def _assert_snapshot(universe_id): diff --git a/tst/unit/task_step/attribution_serialization_test.py b/tst/unit/task_step/attribution_serialization_test.py index 57db30565..a5d78c735 100644 --- a/tst/unit/task_step/attribution_serialization_test.py +++ b/tst/unit/task_step/attribution_serialization_test.py @@ -87,7 +87,7 @@ async def deploy(*, attribution=None, **kwargs): passed.update(kwargs, attribution=attribution) return DeployedGatewayEnv( env_id="art-1", env_version=2, gateway_url="http://gw", mcp_url="http://mcp", - db_web_url=None, sandbox_id="s1", metadata=None, + sandbox_id="s1", metadata=None, ) env = MagicMock() @@ -109,7 +109,7 @@ async def deploy(*, attribution=None, **kwargs): passed["attribution"] = attribution return DeployedGatewayEnv( env_id="art-1", env_version=2, gateway_url="http://gw", mcp_url="http://mcp", - db_web_url=None, sandbox_id="s1", metadata=None, + sandbox_id="s1", metadata=None, ) env = MagicMock() diff --git a/tst/unit/task_step/capture_stubs.py b/tst/unit/task_step/capture_stubs.py index c1bc73954..b6135924a 100644 --- a/tst/unit/task_step/capture_stubs.py +++ b/tst/unit/task_step/capture_stubs.py @@ -210,7 +210,6 @@ def context(*, agent: Optional[_StubAgent] = None, env_id: Optional[str] = None) env_version=1, gateway_url="https://gw", mcp_url="https://gw/mcp", - db_web_url=None, sandbox_id="sb-env", ) ] diff --git a/tst/unit/task_step/deploy_env_test.py b/tst/unit/task_step/deploy_env_test.py index d0f47a62a..f878a9000 100644 --- a/tst/unit/task_step/deploy_env_test.py +++ b/tst/unit/task_step/deploy_env_test.py @@ -17,7 +17,6 @@ def _deployed_env(metadata): env_version=2, gateway_url="http://gw", mcp_url="http://mcp", - db_web_url=None, sandbox_id="s1", metadata=metadata, ) diff --git a/tst/unit/task_step/retry_test.py b/tst/unit/task_step/retry_test.py index 62e33bcbd..14d93ec64 100644 --- a/tst/unit/task_step/retry_test.py +++ b/tst/unit/task_step/retry_test.py @@ -73,8 +73,7 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: self.deploy_calls.append(self.id) context.deployed_envs.append( DeployedGatewayEnv( - env_id=self.id, env_version=1, gateway_url="g", mcp_url="m", db_web_url=None, - sandbox_id=f"sb-{self.id}-{len(self.deploy_calls)}", sandbox_type="fake", + env_id=self.id, env_version=1, gateway_url="g", mcp_url="m", sandbox_id=f"sb-{self.id}-{len(self.deploy_calls)}", sandbox_type="fake", ) ) return context @@ -551,7 +550,7 @@ async def execute(self, context): self.runs += 1 context.deployed_envs.append( DeployedGatewayEnv(env_id=self.id, env_version=1, gateway_url="g", mcp_url="m", - db_web_url=None, sandbox_id="") + sandbox_id="") ) await release.wait() return context diff --git a/tst/unit/task_step/sync_env_clock_test.py b/tst/unit/task_step/sync_env_clock_test.py index a615101bc..191247b98 100644 --- a/tst/unit/task_step/sync_env_clock_test.py +++ b/tst/unit/task_step/sync_env_clock_test.py @@ -275,15 +275,13 @@ async def fetch(client, key, base_url): def _cardless() -> DeployedEnv: """A record that kept no card, which a sync reads live from the gateway.""" - return DeployedGatewayEnv(env_id="e1", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", db_web_url=None, - sandbox_id="sb-1") + return DeployedGatewayEnv(env_id="e1", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", sandbox_id="sb-1") def _carded(card: dict) -> DeployedEnv: from agent_env.env.gateway.constants import WELL_KNOWN_PATH - return DeployedGatewayEnv(env_id="e1", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", db_web_url=None, - sandbox_id="sb-1", environment_card_url=f"{_CARD}{WELL_KNOWN_PATH}", environment_card=card) + return DeployedGatewayEnv(env_id="e1", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", sandbox_id="sb-1", environment_card_url=f"{_CARD}{WELL_KNOWN_PATH}", environment_card=card) def _mock_http(monkeypatch, state: dict | None = None, set_time_status: int = 200): diff --git a/tst/unit/task_step/test_apply_server_config.py b/tst/unit/task_step/test_apply_server_config.py index 9df2a67ec..bfb2d6c6d 100644 --- a/tst/unit/task_step/test_apply_server_config.py +++ b/tst/unit/task_step/test_apply_server_config.py @@ -56,7 +56,7 @@ def _ext(uri: str, endpoint: str, method: str = "POST") -> dict: def _make_context(*, env_id="env-x", gateway_url=_GATEWAY, metadata=None): env = DeployedGatewayEnv( env_id=env_id, env_version=1, gateway_url=gateway_url, mcp_url="", - db_web_url=None, sandbox_id="sb-env", + sandbox_id="sb-env", ) return TaskStepContext(deployed_envs=[env], metadata={} if metadata is None else metadata) diff --git a/tst/unit/task_step/test_collect_artifacts.py b/tst/unit/task_step/test_collect_artifacts.py index c439304b1..2b869e6c1 100644 --- a/tst/unit/task_step/test_collect_artifacts.py +++ b/tst/unit/task_step/test_collect_artifacts.py @@ -59,7 +59,7 @@ def _non_cua_context(): instance_id="inst-1", deployed_envs=[DeployedGatewayEnv( env_id="ubuntu-cua", env_version=1, gateway_url="http://gw", - mcp_url="http://gw", db_web_url=None, sandbox_id="vm-1", + mcp_url="http://gw", sandbox_id="vm-1", metadata={"some_other_env": True}, environment_card_url=f"http://gw{WELL_KNOWN_PATH}", environment_card=_gateway_card(), )], @@ -1092,15 +1092,14 @@ def _gateway_card(extensions: list = GATEWAY_EXTENSIONS) -> dict: def _cua_record(extensions: list = GATEWAY_EXTENSIONS) -> DeployedEnv: """An Ubuntu CUA env (the marker CuaEnv.deploy sets) whose stored card is the gateway's.""" - return DeployedGatewayEnv(env_id="ubuntu-cua", env_version=1, gateway_url="http://gw", mcp_url="http://gw", db_web_url=None, - sandbox_id="vm-1", metadata={"cua_vm_sandbox_id": "cua-vm-1"}, + return DeployedGatewayEnv(env_id="ubuntu-cua", env_version=1, gateway_url="http://gw", mcp_url="http://gw", sandbox_id="vm-1", metadata={"cua_vm_sandbox_id": "cua-vm-1"}, environment_card_url=f"http://gw{WELL_KNOWN_PATH}", environment_card=_gateway_card(extensions)) def _macos_record() -> DeployedEnv: """A macOS CUA env: the gateway (CUA MCP server, 18765) plus a distinct osworld sidecar (18768).""" return DeployedGatewayEnv(env_id="macos-cua", env_version=1, gateway_url="http://gw-18765", mcp_url="http://gw-18765/mcp", - db_web_url=None, sandbox_id="vm-1", + sandbox_id="vm-1", metadata={"cua_vm_sandbox_id": "cua-vm-1", "cua_controller_url": "http://ctrl-18768"}, environment_card_url=f"http://gw-18765{WELL_KNOWN_PATH}", environment_card=_gateway_card()) diff --git a/tst/unit/task_step/test_combine_universe_verdicts.py b/tst/unit/task_step/test_combine_universe_verdicts.py index 174de8606..f3db62ac8 100644 --- a/tst/unit/task_step/test_combine_universe_verdicts.py +++ b/tst/unit/task_step/test_combine_universe_verdicts.py @@ -31,7 +31,7 @@ def put(self, **kwargs): def _deployed_env(env_id="multi-x", env_version=3): return DeployedGatewayEnv( env_id=env_id, env_version=env_version, gateway_url="g", mcp_url="m", - db_web_url=None, sandbox_id="sb-1", + sandbox_id="sb-1", ) diff --git a/tst/unit/task_step/test_configure_scheduled_checks.py b/tst/unit/task_step/test_configure_scheduled_checks.py index 8702651e2..045ed37d5 100644 --- a/tst/unit/task_step/test_configure_scheduled_checks.py +++ b/tst/unit/task_step/test_configure_scheduled_checks.py @@ -74,8 +74,7 @@ def _step(**kwargs) -> ConfigureScheduledChecksTaskStep: def _record(extensions: list = GATEWAY_EXTENSIONS) -> DeployedEnv: - return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url="", db_web_url=None, - sandbox_id="sb", environment_card_url=f"{_GATEWAY}{WELL_KNOWN_PATH}", + return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url="", sandbox_id="sb", environment_card_url=f"{_GATEWAY}{WELL_KNOWN_PATH}", environment_card={"name": "gw", "capabilities": {"extensions": extensions}}) diff --git a/tst/unit/task_step/test_deploy_agent_mcp_alias.py b/tst/unit/task_step/test_deploy_agent_mcp_alias.py index 89fa22ba6..597d23adb 100644 --- a/tst/unit/task_step/test_deploy_agent_mcp_alias.py +++ b/tst/unit/task_step/test_deploy_agent_mcp_alias.py @@ -64,8 +64,7 @@ async def test_a_record_without_a_stored_card_relays_no_name_and_warns(monkeypat def _deployed_env(card: dict | None) -> DeployedEnv: return DeployedGatewayEnv( - env_id="crm", env_version=1, gateway_url="https://gw.example", mcp_url=_MCP_URL, db_web_url=None, - sandbox_id="sb-1", environment_card_url="https://gw.example/.well-known/agent-env.json", environment_card=card, + env_id="crm", env_version=1, gateway_url="https://gw.example", mcp_url=_MCP_URL, sandbox_id="sb-1", environment_card_url="https://gw.example/.well-known/agent-env.json", environment_card=card, ) diff --git a/tst/unit/task_step/test_deploy_agent_role_delivery.py b/tst/unit/task_step/test_deploy_agent_role_delivery.py index 3e605df0f..ff4ed9e88 100644 --- a/tst/unit/task_step/test_deploy_agent_role_delivery.py +++ b/tst/unit/task_step/test_deploy_agent_role_delivery.py @@ -285,7 +285,6 @@ def wiring(monkeypatch) -> _Wiring: def _gateway_env() -> DeployedGatewayEnv: return DeployedGatewayEnv( - env_id="crm", env_version=1, gateway_url="https://gw.example", mcp_url=_GATEWAY_MCP_URL, db_web_url=None, - sandbox_id="sb-1", environment_card_url="https://gw.example/.well-known/agent-env.json", + env_id="crm", env_version=1, gateway_url="https://gw.example", mcp_url=_GATEWAY_MCP_URL, sandbox_id="sb-1", environment_card_url="https://gw.example/.well-known/agent-env.json", environment_card={"name": "crm"}, ) diff --git a/tst/unit/task_step/test_deploy_ttl_from_budget.py b/tst/unit/task_step/test_deploy_ttl_from_budget.py index bd1e60eb2..fd5a0cf6b 100644 --- a/tst/unit/task_step/test_deploy_ttl_from_budget.py +++ b/tst/unit/task_step/test_deploy_ttl_from_budget.py @@ -17,7 +17,7 @@ async def _env_deploy_ttl(step_ttl, overrides) -> int: env = MagicMock() env.deploy = AsyncMock(return_value=DeployedGatewayEnv( env_id="art-1", env_version=2, gateway_url="http://gw", mcp_url="http://mcp", - db_web_url=None, sandbox_id="s1", metadata=None, + sandbox_id="s1", metadata=None, )) step = DeployEnvTaskStep(id="d", version=None, env_id="art-1", env_version=2, ttl_seconds=step_ttl) ctx = TaskStepContext(metadata={"user_overrides": overrides}) diff --git a/tst/unit/task_step/test_load_artifact_env_binding.py b/tst/unit/task_step/test_load_artifact_env_binding.py index a4f377c43..b9a64d1b6 100644 --- a/tst/unit/task_step/test_load_artifact_env_binding.py +++ b/tst/unit/task_step/test_load_artifact_env_binding.py @@ -25,7 +25,7 @@ async def _deploy(**_kwargs): await asyncio.sleep(0) return DeployedGatewayEnv( env_id=ENV_ID, env_version=1, gateway_url=f"https://gw-{sandbox_id}", - mcp_url=f"https://{sandbox_id}/mcp", db_web_url=None, sandbox_id=sandbox_id, + mcp_url=f"https://{sandbox_id}/mcp", sandbox_id=sandbox_id, ) env = MagicMock() diff --git a/tst/unit/task_step/test_load_artifact_file.py b/tst/unit/task_step/test_load_artifact_file.py index b4fb13a80..d1d2ed653 100644 --- a/tst/unit/task_step/test_load_artifact_file.py +++ b/tst/unit/task_step/test_load_artifact_file.py @@ -134,7 +134,7 @@ async def from_deployed_env(cls, deployed): context = TaskStepContext() context.deployed_envs = [DeployedGatewayEnv( env_id="tickets", env_version=1, gateway_url="https://gw", mcp_url="https://gw/mcp", - db_web_url=None, sandbox_id="sb-env", + sandbox_id="sb-env", )] step = LoadArtifactTaskStep(id="stage", version=None, env_id="tickets", artifact_id="check", destination_path="/data") context = await step.execute(context) diff --git a/tst/unit/task_step/test_load_artifact_service_universe.py b/tst/unit/task_step/test_load_artifact_service_universe.py index f102e4d75..edc154d86 100644 --- a/tst/unit/task_step/test_load_artifact_service_universe.py +++ b/tst/unit/task_step/test_load_artifact_service_universe.py @@ -75,7 +75,7 @@ async def _fake_load(deployed, universe, destination): def _deployed_env(env_id: str = "multi-slack-email") -> DeployedEnv: return DeployedGatewayEnv( env_id=env_id, env_version=7, gateway_url="http://gw", mcp_url="http://gw/mcp", - db_web_url=None, sandbox_id="sb-env", instance_id="env-inst-1", + sandbox_id="sb-env", instance_id="env-inst-1", ) diff --git a/tst/unit/task_step/test_mcp_cli_builder.py b/tst/unit/task_step/test_mcp_cli_builder.py index af7d6eb74..763351fce 100644 --- a/tst/unit/task_step/test_mcp_cli_builder.py +++ b/tst/unit/task_step/test_mcp_cli_builder.py @@ -207,7 +207,7 @@ def put(**kw): monkeypatch.setattr(build_mcp_cli.Env, "get", staticmethod(lambda id, version=None: SimpleNamespace(environment_name="slack"))) monkeypatch.setattr(build_mcp_cli, "fetch_interface_manifest", no_manifest) monkeypatch.setattr(build_mcp_cli.CliArtifact, "put", staticmethod(put)) - record = DeployedGatewayEnv(env_id="slack-mcp", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", db_web_url=None, sandbox_id="sb") + record = DeployedGatewayEnv(env_id="slack-mcp", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", sandbox_id="sb") await BuildMcpCliTaskStep(id="s1", version=None, env_id="slack-mcp", command_name="slack").execute( TaskStepContext(deployed_envs=[record]) @@ -217,7 +217,7 @@ def put(**kw): def _gateway_record(env_id: str = "slack-mcp") -> DeployedGatewayEnv: - return DeployedGatewayEnv(env_id=env_id, env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", db_web_url=None, sandbox_id="sb") + return DeployedGatewayEnv(env_id=env_id, env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", sandbox_id="sb") def _no_gateway(monkeypatch) -> None: diff --git a/tst/unit/task_step/test_modify_env_tool_access.py b/tst/unit/task_step/test_modify_env_tool_access.py index 450ef5a66..8a3ea58b3 100644 --- a/tst/unit/task_step/test_modify_env_tool_access.py +++ b/tst/unit/task_step/test_modify_env_tool_access.py @@ -58,7 +58,7 @@ def _step(action: str) -> ModifyEnvToolAccessStep: def _record(extensions: list = GATEWAY_EXTENSIONS) -> DeployedEnv: """A gateway deployment whose stored card advertises `extensions` (the gateway's own, by default).""" - return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url="", db_web_url=None, sandbox_id="sb", + return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url="", sandbox_id="sb", environment_card_url=f"{_GATEWAY}{WELL_KNOWN_PATH}", environment_card={"name": "gw", "capabilities": {"extensions": extensions}}) diff --git a/tst/unit/task_step/test_parallel_deploy_env_binding.py b/tst/unit/task_step/test_parallel_deploy_env_binding.py index 5b95f7430..36dd69f15 100644 --- a/tst/unit/task_step/test_parallel_deploy_env_binding.py +++ b/tst/unit/task_step/test_parallel_deploy_env_binding.py @@ -30,7 +30,6 @@ async def _deploy(**_kwargs): env_version=1, gateway_url=f"https://gw-{sandbox_id}", mcp_url=mcp_url, - db_web_url=None, sandbox_id=sandbox_id, ) diff --git a/tst/unit/task_step/test_prompt_agent_triggers.py b/tst/unit/task_step/test_prompt_agent_triggers.py index 0d33d5b9a..b770980b5 100644 --- a/tst/unit/task_step/test_prompt_agent_triggers.py +++ b/tst/unit/task_step/test_prompt_agent_triggers.py @@ -29,7 +29,7 @@ def _env(env_id: str, gateway_url: str, *uris: str): """A record whose stored card is the gateway's, offering only `uris` when given.""" extensions = [e for e in GATEWAY_EXTENSIONS if not uris or e["uri"] in uris] return DeployedGatewayEnv(env_id=env_id, env_version=1, gateway_url=gateway_url, mcp_url="", - db_web_url=None, sandbox_id="sb", environment_card_url=f"{gateway_url}{WELL_KNOWN_PATH}", + sandbox_id="sb", environment_card_url=f"{gateway_url}{WELL_KNOWN_PATH}", environment_card={"capabilities": {"extensions": extensions}}) diff --git a/tst/unit/task_step/test_register_agent_triggers.py b/tst/unit/task_step/test_register_agent_triggers.py index 612ecf933..836ca22ac 100644 --- a/tst/unit/task_step/test_register_agent_triggers.py +++ b/tst/unit/task_step/test_register_agent_triggers.py @@ -34,7 +34,7 @@ def _context(*, card: dict | None = None, env_ids: tuple[str, ...] = ("env-x",), if registered_env_triggers is None: registered_env_triggers = {"env-x": ["v6-rate"]} envs = [DeployedGatewayEnv(env_id=e, env_version=1, gateway_url="http://gw", mcp_url="", - db_web_url=None, sandbox_id="sb") for e in env_ids] + sandbox_id="sb") for e in env_ids] agents = [] if with_agent: agents.append(DeployedAgent(agent_name="stake-1", api_url="http://stakeholder:8000/api", diff --git a/tst/unit/task_step/test_register_env_triggers.py b/tst/unit/task_step/test_register_env_triggers.py index 78f370aee..9659893a4 100644 --- a/tst/unit/task_step/test_register_env_triggers.py +++ b/tst/unit/task_step/test_register_env_triggers.py @@ -353,7 +353,7 @@ def _with_features(*features: str) -> list: def _record(extensions: list = GATEWAY_EXTENSIONS) -> DeployedEnv: """A gateway deployment whose stored card advertises `extensions` (the gateway's own, by default).""" - return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url="", db_web_url=None, sandbox_id="sb", + return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url="", sandbox_id="sb", environment_card_url=f"{_GATEWAY}{WELL_KNOWN_PATH}", environment_card={"name": "gw", "capabilities": {"extensions": extensions}}) diff --git a/tst/unit/task_step/test_run_code.py b/tst/unit/task_step/test_run_code.py index 5527367e1..cb1ba3128 100644 --- a/tst/unit/task_step/test_run_code.py +++ b/tst/unit/task_step/test_run_code.py @@ -46,7 +46,7 @@ def _make_context(*, with_agent=True, metadata=None): ) env = DeployedGatewayEnv( env_id="env-x", env_version=1, gateway_url="", mcp_url="", - db_web_url=None, sandbox_id="sb-env", + sandbox_id="sb-env", ) return TaskStepContext( deployed_agents=[agent] if with_agent else [], diff --git a/tst/unit/task_step/test_snapshot_agent_state.py b/tst/unit/task_step/test_snapshot_agent_state.py index 551f1cd26..c0a372b1e 100644 --- a/tst/unit/task_step/test_snapshot_agent_state.py +++ b/tst/unit/task_step/test_snapshot_agent_state.py @@ -74,7 +74,6 @@ def _ctx(): env_version=1, gateway_url="https://gw", mcp_url="m", - db_web_url=None, sandbox_id="sb-1", ) ] diff --git a/tst/unit/task_step/test_snapshot_env.py b/tst/unit/task_step/test_snapshot_env.py index cf75fb7ca..3720f0d32 100644 --- a/tst/unit/task_step/test_snapshot_env.py +++ b/tst/unit/task_step/test_snapshot_env.py @@ -30,7 +30,6 @@ def _deployed(env_id="env-1", instance_id="env-1-abc123", gateway="https://gw-1" env_version=3, gateway_url=gateway, mcp_url=f"{gateway}/mcp", - db_web_url=None, sandbox_id="sb-1", instance_id=instance_id, environment_card_url=f"{gateway}{WELL_KNOWN_PATH}", @@ -1104,7 +1103,7 @@ def test_snapshot_servers_refuses_an_env_with_none(): def _deployed_without_a_card() -> DeployedEnv: """An older record: no card URL, so its servers are the ones its definition names.""" return DeployedGatewayEnv(env_id="env-1", env_version=3, gateway_url="https://gw-1", mcp_url="https://gw-1/mcp", - db_web_url=None, sandbox_id="sb-1") + sandbox_id="sb-1") def test_resolve_loaded_universe_ref_reads_store(monkeypatch): @@ -1343,8 +1342,7 @@ def _carded(card: dict) -> DeployedEnv: from agent_env.env.gateway.constants import WELL_KNOWN_PATH return DeployedGatewayEnv( - env_id="env-1", env_version=3, gateway_url="https://gw-1", mcp_url="https://gw-1/mcp", db_web_url=None, - sandbox_id="sb-1", environment_card_url=f"{_CARD}{WELL_KNOWN_PATH}", environment_card=card, + env_id="env-1", env_version=3, gateway_url="https://gw-1", mcp_url="https://gw-1/mcp", sandbox_id="sb-1", environment_card_url=f"{_CARD}{WELL_KNOWN_PATH}", environment_card=card, ) diff --git a/tst/unit/task_step/test_snapshot_env_env_binding.py b/tst/unit/task_step/test_snapshot_env_env_binding.py index 990ca7807..4c120cea0 100644 --- a/tst/unit/task_step/test_snapshot_env_env_binding.py +++ b/tst/unit/task_step/test_snapshot_env_env_binding.py @@ -30,7 +30,7 @@ async def _deploy(**_kwargs): await asyncio.sleep(0) return DeployedGatewayEnv( env_id=ENV_ID, env_version=1, gateway_url=f"https://gw-{sandbox_id}", - mcp_url=f"https://{sandbox_id}/mcp", db_web_url=None, sandbox_id=sandbox_id, + mcp_url=f"https://{sandbox_id}/mcp", sandbox_id=sandbox_id, ) env = MagicMock() diff --git a/tst/unit/task_step/test_teardown_sandboxes.py b/tst/unit/task_step/test_teardown_sandboxes.py index a7120fcf9..8638ee882 100644 --- a/tst/unit/task_step/test_teardown_sandboxes.py +++ b/tst/unit/task_step/test_teardown_sandboxes.py @@ -85,8 +85,7 @@ def _context() -> TaskStepContext: ], deployed_envs=[ DeployedGatewayEnv( - env_id="shop", env_version=1, gateway_url=None, mcp_url="http://m", db_web_url=None, - sandbox_id="sb-gw", sandbox_type="modal", + env_id="shop", env_version=1, gateway_url=None, mcp_url="http://m", sandbox_id="sb-gw", sandbox_type="modal", sandbox_ids={"gateway": "sb-gw", "service_db": {"orders": "sb-db"}, "modal_vm": "sb-vm"}, ), ], diff --git a/tst/unit/task_step/test_verify_spec_conformance.py b/tst/unit/task_step/test_verify_spec_conformance.py index 40a2ebedf..6c77aae54 100644 --- a/tst/unit/task_step/test_verify_spec_conformance.py +++ b/tst/unit/task_step/test_verify_spec_conformance.py @@ -622,7 +622,7 @@ def handle(request): sent.append((request.method, str(request.url))) return httpx.Response(200, json={"name": "items"}) if request.url.path.endswith(WELL_KNOWN_PATH) else httpx.Response(404) - record = DeployedGatewayEnv(env_id="e1", env_version=1, gateway_url=gateway_url, mcp_url="", db_web_url=None, sandbox_id="sb", + record = DeployedGatewayEnv(env_id="e1", env_version=1, gateway_url=gateway_url, mcp_url="", sandbox_id="sb", environment_card_url=f"{gateway_url}{WELL_KNOWN_PATH}", environment_card=card) env = MagicMock(environment_name="items") with patch.object(httpx, "AsyncClient", lambda *a, **k: real(transport=httpx.MockTransport(handle))), \ diff --git a/tst/unit/task_step/test_verify_universe_roundtrip.py b/tst/unit/task_step/test_verify_universe_roundtrip.py index 67a8badde..d21675c06 100644 --- a/tst/unit/task_step/test_verify_universe_roundtrip.py +++ b/tst/unit/task_step/test_verify_universe_roundtrip.py @@ -154,7 +154,7 @@ async def fetch(client, key, base_url): def _record(card: dict | None) -> DeployedEnv: - return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url=f"{_GATEWAY}/mcp", db_web_url=None, sandbox_id="sb", + return DeployedGatewayEnv(env_id="env-x", env_version=1, gateway_url=_GATEWAY, mcp_url=f"{_GATEWAY}/mcp", sandbox_id="sb", environment_card_url=f"{_CARD}{WELL_KNOWN_PATH}", environment_card=card)