From 5d14668d1b9a9357c2b7e17eda683a369dfb37af Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 19 Nov 2026 18:34:05 +0000 Subject: [PATCH 1/4] feat(gateway): serve pgweb and db-mcp at /svc/ routes The gateway serves the env's SQL consoles at /svc/pgweb/ and /svc/db-mcp/ from a new CONSOLE_URLS variable, behind the deployment's control token like its other guarded routes. The deploy records db_web_gateway_url and db_mcp_gateway_url once it has checked the gateway serves both, and keeps db_web_url and db_mcp_url as they are. db-mcp binds every address family on the container path. The CLI prints the gateway URLs, and a website can no longer be named pgweb or db-mcp. --- src/agent_env/cli/env/deploy.py | 9 +- src/agent_env/cli/env/get_instance.py | 8 +- src/agent_env/cli/run.py | 2 +- src/agent_env/cli/task/run.py | 3 +- src/agent_env/env/env.py | 10 +- src/agent_env/env/gateway/constants.py | 3 + src/agent_env/env/gateway/entrypoint.py | 3 +- src/agent_env/env/gateway/gateway.py | 21 ++- .../env_providers/env_gateway_provider.py | 67 ++++++++ .../providers/env_state/env_state_provider.py | 5 + .../providers/env_state/local_postgres.py | 8 +- tst/integration/env/gateway/gateway_test.py | 18 ++- tst/unit/bundle/run_teardown_test.py | 4 +- tst/unit/cli/deployed_env_output_test.py | 25 ++- tst/unit/env/envs/test_multi_env_name.py | 4 + tst/unit/env/gateway/console_routes_test.py | 149 ++++++++++++++++++ tst/unit/env/test_env.py | 7 +- .../env_gateway_provider_test.py | 106 +++++++++++++ .../env_state/test_env_state_provider.py | 3 + 19 files changed, 425 insertions(+), 30 deletions(-) create mode 100644 tst/unit/env/gateway/console_routes_test.py 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..4cd8ba555 100644 --- a/src/agent_env/env/env.py +++ b/src/agent_env/env/env.py @@ -230,6 +230,10 @@ class DeployedGatewayEnv(DeployedSandboxEnv): gateway_mode: str = GatewayMode.PERFORMANCE.value 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 its image + # serves neither. + 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 +252,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 +589,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/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..dcb965ada 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 @@ -239,6 +239,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(os.environ.get(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..31e3460da 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_rest_request(request, url_map=self._consoles) 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) 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..6446555aa 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, @@ -144,6 +145,8 @@ class DeployedGateway: 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 +211,24 @@ 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 can time out + or leave the gateway a 502, so such an answer is asked once more after a pause.""" + answer: int | str = "unasked" + for attempt in range(2): + 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.TimeoutException 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 +569,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}" @@ -702,6 +725,8 @@ async def deploy( 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, @@ -997,6 +1022,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(): @@ -1005,6 +1031,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, + 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, @@ -1088,6 +1115,7 @@ async def _deploy_via_containers( 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,6 +1130,8 @@ 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 @@ -1128,6 +1158,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), @@ -1157,11 +1188,14 @@ async def _deploy_via_containers( 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 +1207,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=30) 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 @@ -1700,6 +1760,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 +1821,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 +1882,7 @@ 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 +_CONSOLE_RETRY_PAUSE_S = 5.0 # 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..7eaddaf45 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. None 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..21fa4e238 100644 --- a/src/agent_env/providers/env_state/local_postgres.py +++ b/src/agent_env/providers/env_state/local_postgres.py @@ -314,6 +314,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 +373,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/gateway/gateway_test.py b/tst/integration/env/gateway/gateway_test.py index 86b1977c9..71d224314 100644 --- a/tst/integration/env/gateway/gateway_test.py +++ b/tst/integration/env/gateway/gateway_test.py @@ -643,8 +643,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 +1243,8 @@ 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) + 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 +1267,8 @@ 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) + 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 +1311,8 @@ 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) + 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 +1676,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 +1688,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 httpx.AsyncClient(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/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/deployed_env_output_test.py b/tst/unit/cli/deployed_env_output_test.py index b3abb0748..05b7694ee 100644 --- a/tst/unit/cli/deployed_env_output_test.py +++ b/tst/unit/cli/deployed_env_output_test.py @@ -42,10 +42,33 @@ 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_url="https://tunnel-8081.example/", db_mcp_url="https://tunnel-18767.example/mcp", + 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_and_never_their_tunnels(): + 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 + assert "tunnel-" not 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/env/envs/test_multi_env_name.py b/tst/unit/env/envs/test_multi_env_name.py index 35d78b764..125dc3eee 100644 --- a/tst/unit/env/envs/test_multi_env_name.py +++ b/tst/unit/env/envs/test_multi_env_name.py @@ -66,6 +66,8 @@ def test_deployed_env_records_the_gateway_name(): 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, + 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 +84,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/gateway/console_routes_test.py b/tst/unit/env/gateway/console_routes_test.py new file mode 100644 index 000000000..2cdc74547 --- /dev/null +++ b/tst/unit/env/gateway/console_routes_test.py @@ -0,0 +1,149 @@ +"""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.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"}) diff --git a/tst/unit/env/test_env.py b/tst/unit/env/test_env.py index a30119dba..d94ba0eb1 100644 --- a/tst/unit/env/test_env.py +++ b/tst/unit/env/test_env.py @@ -274,10 +274,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") @@ -296,6 +299,8 @@ def test_a_gateway_record_is_filled_the_way_it_always_was(): 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", + 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"), 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..cc8be6e51 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, \ @@ -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() @@ -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,104 @@ 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") +_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, 502])}, {"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, _TIMEOUT], [])}, {"db-mcp": _BOTH["db-mcp"]}, id="pgweb-silent"), + pytest.param({"pgweb": ([404], []), "db-mcp": ([404], [])}, {}, id="older-gateway"), + pytest.param({"pgweb": ([200], []), "db-mcp": ([200], [])}, {}, id="open"), + pytest.param({"pgweb": ([_REFUSED], []), "db-mcp": ([_REFUSED], [])}, {}, 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.""" + 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) + 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 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( From 86f2370dad0444f2a226c07b5ff94cec9cbff0c0 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Thu, 19 Nov 2026 11:32:31 -0800 Subject: [PATCH 2/4] fix(gateway): refuse console names up front, log no console writes, retry the console probe - A multi env's computer can't be named pgweb or db-mcp. The gateway serves a computer at /svc//, so such a computer used to pass the env's name checks and stop the gateway at startup, after every sandbox was made. - Requests to a SQL console are forwarded without a write window. Only the control token reaches a console, so a write through one is no role's, and it no longer runs the changelog queries on the event loop. - The deploy asks each console up to four times: a dropped or refused connection is asked again, as a timeout or a 502-504 already was. - _verify_list_tools uses the MCP SDK's client (30 s, redirects), as it did before it took headers, and the tests check the record names db-mcp at the gateway before using it. - A bad CONSOLE_URLS or WEBSITE_URLS entry names its own variable. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/env.py | 4 +-- src/agent_env/env/envs/_multi_computers.py | 7 +++-- src/agent_env/env/gateway/entrypoint.py | 19 ++++++------ src/agent_env/env/gateway/gateway.py | 9 +++++- .../env_providers/env_gateway_provider.py | 10 +++--- .../providers/env_state/env_state_provider.py | 2 +- tst/integration/env/gateway/gateway_test.py | 6 +++- tst/unit/env/envs/test_multi_env_computers.py | 6 +++- tst/unit/env/gateway/console_routes_test.py | 31 +++++++++++++++++++ .../env_gateway_provider_test.py | 11 ++++--- 10 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/agent_env/env/env.py b/src/agent_env/env/env.py index 4cd8ba555..679eb67ec 100644 --- a/src/agent_env/env/env.py +++ b/src/agent_env/env/env.py @@ -230,8 +230,8 @@ class DeployedGatewayEnv(DeployedSandboxEnv): gateway_mode: str = GatewayMode.PERFORMANCE.value 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 its image - # serves neither. + # 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 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/entrypoint.py b/src/agent_env/env/gateway/entrypoint.py index dcb965ada..48d9217f1 100644 --- a/src/agent_env/env/gateway/entrypoint.py +++ b/src/agent_env/env/gateway/entrypoint.py @@ -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,7 +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(os.environ.get(CONSOLE_URLS_ENV, "")), + 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 31e3460da..f6111d804 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -279,7 +279,7 @@ async def list_website_urls() -> list[str]: ) async def proxy_to_service(request: Request) -> Response: if request.path_params["service_name"] in self._consoles: - return await self._proxy_rest_request(request, url_map=self._consoles) + return await self._proxy_console_request(request) return await self._proxy_rest_request(request) if self.rest_proxy_urls: @@ -916,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 6446555aa..511e93e4d 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -212,17 +212,18 @@ def image(self) -> str: 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 can time out - or leave the gateway a 502, so such an answer is asked once more after a pause.""" + """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(2): + 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.TimeoutException as e: + except httpx.TransportError as e: answer = type(e).__name__ except httpx.HTTPError as e: return type(e).__name__ @@ -1883,6 +1884,7 @@ def _seconds(seconds: float) -> str: # time, if startup's didn't complete. _STATE_READ_TIMEOUT_S = 60.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 7eaddaf45..ef031fc56 100644 --- a/src/agent_env/providers/env_state/env_state_provider.py +++ b/src/agent_env/providers/env_state/env_state_provider.py @@ -209,7 +209,7 @@ def render_sidecar_containers( 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. None for a backend without sidecars.""" + network; the gateway serves each behind its control token. Empty for a backend without sidecars.""" return {} def render_compose_containers( diff --git a/tst/integration/env/gateway/gateway_test.py b/tst/integration/env/gateway/gateway_test.py index 71d224314..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. @@ -1243,6 +1244,7 @@ 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}" + 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) @@ -1267,6 +1269,7 @@ 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}" + 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) @@ -1311,6 +1314,7 @@ 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}" + 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: @@ -1688,7 +1692,7 @@ async def _verify_list_tools(mcp_url: str, tool_verifier_callback: callable, hea logger.info(f"Testing gateway via tunnel (attempt {attempt + 1}/{max_retries})...") verified = False try: - async with httpx.AsyncClient(headers=headers) as http_client, \ + 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() 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/gateway/console_routes_test.py b/tst/unit/env/gateway/console_routes_test.py index 2cdc74547..30a0257c3 100644 --- a/tst/unit/env/gateway/console_routes_test.py +++ b/tst/unit/env/gateway/console_routes_test.py @@ -9,6 +9,8 @@ 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" @@ -147,3 +149,32 @@ async def test_consoles_alone_get_the_svc_route(monkeypatch): 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/providers/env_providers/env_gateway_provider_test.py b/tst/unit/providers/env_providers/env_gateway_provider_test.py index cc8be6e51..1cd85e547 100644 --- a/tst/unit/providers/env_providers/env_gateway_provider_test.py +++ b/tst/unit/providers/env_providers/env_gateway_provider_test.py @@ -3120,6 +3120,7 @@ def test_a_site_answering_only_errors_fails_its_health_probe(tmp_path): _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"} @@ -3127,18 +3128,19 @@ def test_a_site_answering_only_errors_fails_its_health_probe(tmp_path): @pytest.mark.asyncio @pytest.mark.parametrize("answers, recorded", [ pytest.param(_GUARDED, _BOTH, id="both-guarded-and-reached"), - pytest.param({**_GUARDED, "db-mcp": ([401], [502, 502])}, {"pgweb": _BOTH["pgweb"]}, id="db-mcp-unreachable"), + 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, _TIMEOUT], [])}, {"db-mcp": _BOTH["db-mcp"]}, id="pgweb-silent"), + 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], []), "db-mcp": ([_REFUSED], [])}, {}, id="unreachable-gateway"), + 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.""" + 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))} @@ -3153,6 +3155,7 @@ def reply(request: httpx.Request) -> httpx.Response: 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 From 0ae41db442c987863baf094ea4f22641167c1cec Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Thu, 19 Nov 2026 11:39:23 -0800 Subject: [PATCH 3/4] refactor(env): name the console probe's request timeout Co-Authored-By: Claude Opus 5.5 --- src/agent_env/providers/env_providers/env_gateway_provider.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 511e93e4d..e07cf87da 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -1230,7 +1230,7 @@ async def served(client: httpx.AsyncClient, key: str) -> bool: return False return True - async with httpx.AsyncClient(timeout=30) as client: + 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} @@ -1883,6 +1883,8 @@ 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 From fd41a3196b30c60dbb4275a3af3c975ed255ff2f Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Thu, 19 Nov 2026 15:09:20 -0800 Subject: [PATCH 4/4] refactor(env): one set of console names, one writer for CONSOLE_URLS - CONSOLE_SERVICE_NAMES, beside the console service names, is the one set the website and computer name checks refuse. - consoles_environment() writes CONSOLE_URLS for both deploy paths, as computers_environment() and the other gateway variables are written. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/envs/_multi_computers.py | 4 ++-- src/agent_env/env/envs/service_db.py | 2 ++ .../env_providers/env_gateway_provider.py | 16 +++++++++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/agent_env/env/envs/_multi_computers.py b/src/agent_env/env/envs/_multi_computers.py index fc4ab9134..86f17f731 100644 --- a/src/agent_env/env/envs/_multi_computers.py +++ b/src/agent_env/env/envs/_multi_computers.py @@ -15,7 +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.envs.service_db import CONSOLE_SERVICE_NAMES from agent_env.env.gateway import ExternalChild from agent_env.providers.sandbox_providers.sandbox_provider import build_sandbox_provider, get_env_sandbox_provider @@ -42,7 +42,7 @@ def name_problems(computers: Iterable[str], *, mcp_servers: Iterable[str] = (), 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)] + for name in dict.fromkeys(computers) if name in CONSOLE_SERVICE_NAMES] return problems diff --git a/src/agent_env/env/envs/service_db.py b/src/agent_env/env/envs/service_db.py index c976e8bf3..a80fac6c6 100644 --- a/src/agent_env/env/envs/service_db.py +++ b/src/agent_env/env/envs/service_db.py @@ -20,6 +20,8 @@ DB_MCP_SERVICE_NAME = "db-mcp" DB_MCP_PORT = 18767 # db-mcp host/tunnel port DB_MCP_CONTAINER_PORT = 8000 # db-mcp's in-container listen port (tunneled as DB_MCP_PORT:DB_MCP_CONTAINER_PORT) +# The env's SQL consoles, each served by the gateway at /svc//, so no child env may take one's name. +CONSOLE_SERVICE_NAMES = frozenset({PGWEB_SERVICE_NAME, DB_MCP_SERVICE_NAME}) @dataclass 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 e07cf87da..4e0d98f9e 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -12,7 +12,7 @@ import shlex import time from collections import Counter -from collections.abc import Iterable +from collections.abc import Iterable, Mapping from dataclasses import dataclass, field, replace from datetime import datetime, timezone from typing import TYPE_CHECKING, Optional @@ -49,6 +49,7 @@ tool_name_clashes_text, ) from agent_env.env.envs.service_db import ( + CONSOLE_SERVICE_NAMES, DATABASE_SERVICE_NAME, DB_MCP_CONTAINER_PORT, DB_MCP_PORT, @@ -570,8 +571,7 @@ 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 consoles_environment(state_provider.console_urls()).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}" @@ -1159,7 +1159,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 {}), + **consoles_environment(consoles), "MCP_SERVER_NAME": mcp_server_name or DEFAULT_MCP_SERVER_NAME, **control_environment(control_token), **clock_followers_environment(external), @@ -1762,7 +1762,7 @@ def _compose_name_problems(mcp_servers: list[MCPServerConfig], website_configs: 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] + for w in website_configs or [] if w.environment_name in CONSOLE_SERVICE_NAMES] return problems @@ -1858,6 +1858,12 @@ def computers_environment(children: Iterable[ExternalChild]) -> dict[str, str]: return {COMPUTERS_ENV: names} if names else {} +def consoles_environment(consoles: Mapping[str, str]) -> dict[str, str]: + """The env's SQL consoles the gateway serves behind its control token, each at its address on the env's network.""" + value = ",".join(f"{name}={url}" for name, url in consoles.items()) + return {CONSOLE_URLS_ENV: value} if value else {} + + def _frontend_urls(gateway_url: str, website_configs: Optional[list[WebsiteConfig]]) -> Optional[dict[str, str]]: """Each synthetic site's page route through the gateway, by its environment name; None with none, as when every site is live."""