From 0bce355041684e39d6c8e4f6d4ec6c8abbf4e7a4 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 13 Nov 2026 06:50:38 +0000 Subject: [PATCH 1/3] fix(gateway): probe a site's health without needing curl in its image The compose health check for a website backend ran curl, so a site image without curl could never turn healthy, even with its server answering. The probe now fetches with whichever of curl, wget, python3 or node the image has. --- .../examples/website/notes/Dockerfile | 1 - .../examples/website/notes/README.md | 2 +- .../env_providers/env_gateway_provider.py | 17 ++++++- .../env_gateway_provider_test.py | 49 +++++++++++++++++-- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/packages/agentenv-protocol/examples/website/notes/Dockerfile b/packages/agentenv-protocol/examples/website/notes/Dockerfile index 095b6a096..ab73e2cbb 100644 --- a/packages/agentenv-protocol/examples/website/notes/Dockerfile +++ b/packages/agentenv-protocol/examples/website/notes/Dockerfile @@ -7,7 +7,6 @@ RUN npm run build FROM mirror.gcr.io/library/python:3.12-slim@sha256:423ed6ab25b1921a477529254bfeeabf5855151dc2c3141699a1bfc852199fbf WORKDIR /app -RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/* RUN pip install --no-cache-dir "agentenv-framework-protocol>=0.1.466" "uvicorn>=0.30" COPY site.py /app/site.py COPY data /app/data diff --git a/packages/agentenv-protocol/examples/website/notes/README.md b/packages/agentenv-protocol/examples/website/notes/README.md index f9edde853..19e4265f0 100644 --- a/packages/agentenv-protocol/examples/website/notes/README.md +++ b/packages/agentenv-protocol/examples/website/notes/README.md @@ -9,7 +9,7 @@ them. The page shows the universe's time. | `site.py` | The server: the `Notes` WebsiteEnvironment, its `@api` routes, and `serve()` on `PORT` (8000). | | `data/seed.json` | The seed data: two users (`ann`, `ben`), their notes, and `current_user`, the acting user. | | `web/` | A minimal Vite app (`index.html`, `src/main.js`, `src/style.css`). Its assets (`base: "./"`) and API calls are relative to the page, so it works at `/` and under the gateway's `/website/notes/` alike; opened at a path without its final slash, such as `/website/notes` or `/website/notes/index.html`, the page first reloads itself at its folder's address. | -| `Dockerfile` | The server image, built with this folder as its context: Node builds `web/`, then Python installs the released protocol (`agentenv-framework-protocol`) with uvicorn. It installs `curl`, which the gateway's compose health check runs against the card. | +| `Dockerfile` | The server image, built with this folder as its context: Node builds `web/`, then Python installs the released protocol (`agentenv-framework-protocol`) with uvicorn. | | `env.toml` | The website env as a bundle entry. | ## What it exercises 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 16437dc0d..d56770678 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -596,8 +596,7 @@ def create_docker_compose( " - PYTHONUNBUFFERED=1", *_settings_lines(wc.settings), " healthcheck:", - f' test: ["CMD-SHELL", "curl -sf -o /dev/null http://localhost:{AGENT_ENV_WEBSITE_BACKEND_PORT}{WELL_KNOWN_PATH}' - f' || curl -sf -o /dev/null http://localhost:{AGENT_ENV_WEBSITE_BACKEND_PORT}{wc.health_path}"]', + f' test: ["CMD-SHELL", {json.dumps(_http_probe(AGENT_ENV_WEBSITE_BACKEND_PORT, WELL_KNOWN_PATH, wc.health_path))}]', " interval: 10s", " timeout: 5s", f" start_period: {_seconds(wc.ready_timeout_s) if wc.ready_timeout_s else '30s'}", @@ -1728,6 +1727,20 @@ def _why_down(row: dict) -> str | None: return None +def _http_probe(port: int, *paths: str) -> str: + """A shell check that passes once any of ``paths`` answers on ``port`` with a success, fetched with whichever of + curl, wget, python3 or node the image has: a site's image is the author's, and many leave curl out. ``$$`` is + compose's escape for a ``$`` the shell reads.""" + fetchers = [ + 'curl -sf --max-time 2 -o /dev/null "$$u"', + 'wget -q -T 2 -O /dev/null "$$u"', + "python3 -c 'import sys, urllib.request; urllib.request.urlopen(sys.argv[1], timeout=2)' \"$$u\"", + "node -e 'fetch(process.argv[1]).then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))' \"$$u\"", + ] + urls = " ".join(f"http://localhost:{port}{path}" for path in paths) + return f"for u in {urls}; do {{ {' || '.join(fetchers)}; }} 2>/dev/null && exit 0; done; exit 1" + + def _network_lines(*networks: str) -> list[str]: return [" networks:", *(f" - {network}" for network in networks)] 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 7ddc4a0d4..09b274198 100644 --- a/tst/unit/providers/env_providers/env_gateway_provider_test.py +++ b/tst/unit/providers/env_providers/env_gateway_provider_test.py @@ -2591,9 +2591,9 @@ def test_a_one_image_site_runs_alone_on_its_port_up_once_it_answers_and_its_rout assert {"mail-website-backend", "shop-website-backend", "shop-website-frontend"} <= set(services) assert "mail-website-frontend" not in services assert services["mail-website-backend"]["healthcheck"]["test"] == [ - "CMD-SHELL", "curl -sf -o /dev/null http://localhost:8000/.well-known/agent-env.json" - " || curl -sf -o /dev/null http://localhost:8000/healthz"] - assert services["shop-website-backend"]["healthcheck"]["test"][1].endswith("http://localhost:8000/api/health") + "CMD-SHELL", env_gateway_provider._http_probe(8000, "/.well-known/agent-env.json", "/healthz")] + assert services["shop-website-backend"]["healthcheck"]["test"] == [ + "CMD-SHELL", env_gateway_provider._http_probe(8000, "/.well-known/agent-env.json", "/api/health")] gateway = dict(item.split("=", 1) for item in services[GATEWAY_SERVICE_NAME]["environment"]) assert gateway["WEBSITE_URLS"] == "mail=http://mail-website-backend:8000,shop=http://shop-website-frontend:80" assert gateway["REST_PROXY_URLS"] == "mail=http://mail-website-backend:8000,shop=http://shop-website-backend:8000" @@ -3041,3 +3041,46 @@ async def children(reach): assert env["INTERNAL_MCP_SERVERS"].split(",")[-1] == "desktop=https://desk.example/mcp" assert env["REST_PROXY_URLS"].split(",")[-1] == "desktop=https://desk.example" assert env["CLOCK_FOLLOWER_TOKENS"] == "desktop=read-desk" + + +def _run_probe(tmp_path, statuses: dict[str, int]) -> int: + """Run the site health probe the way compose does, in a shell that has python3 but no curl, wget or node, against + a server that answers each path with its status in ``statuses`` and every other path with 404.""" + import http.server + import os + import shutil + import subprocess + import sys + import threading + + from pytest_socket import enable_socket + + enable_socket() + + class Handler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(statuses.get(self.path, 404)) + self.end_headers() + + def log_message(self, *args): + pass + + server = http.server.ThreadingHTTPServer(("localhost", 0), Handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + (bin_dir / "python3").symlink_to(sys.executable) + try: + probe = env_gateway_provider._http_probe(server.server_port, "/.well-known/agent-env.json", "/api/health") + return subprocess.run([shutil.which("sh"), "-c", probe.replace("$$", "$")], timeout=30, + env={**os.environ, "PATH": str(bin_dir)}).returncode + finally: + server.shutdown() + + +def test_a_site_image_without_curl_passes_its_health_probe_once_its_health_path_answers(tmp_path): + assert _run_probe(tmp_path, {"/api/health": 200}) == 0 + + +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 From dac0904ecd9c9edd2c9321706ce77de96187c1f7 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 13 Nov 2026 07:15:34 +0000 Subject: [PATCH 2/3] fix(gateway): fetch each health path once, with the image's first HTTP client --- .../env_providers/env_gateway_provider.py | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) 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 d56770678..556854c9f 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -1727,18 +1727,26 @@ def _why_down(row: dict) -> str | None: return None +_PROBE_REQUEST_TIMEOUT_S = 2 +"""Each request of a site's health probe; two paths at most stay inside compose's 5 s check timeout.""" + + def _http_probe(port: int, *paths: str) -> str: - """A shell check that passes once any of ``paths`` answers on ``port`` with a success, fetched with whichever of - curl, wget, python3 or node the image has: a site's image is the author's, and many leave curl out. ``$$`` is - compose's escape for a ``$`` the shell reads.""" - fetchers = [ - 'curl -sf --max-time 2 -o /dev/null "$$u"', - 'wget -q -T 2 -O /dev/null "$$u"', - "python3 -c 'import sys, urllib.request; urllib.request.urlopen(sys.argv[1], timeout=2)' \"$$u\"", - "node -e 'fetch(process.argv[1]).then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))' \"$$u\"", - ] + """A shell check that passes once any of ``paths`` answers on ``port`` with a success. It fetches each path once, + with the first of curl, wget, python3 or node the image has: a site's image is the author's, and many leave curl + out. ``$$`` is compose's escape for a ``$`` the shell reads.""" + t = _PROBE_REQUEST_TIMEOUT_S + fetchers = { + "curl": f'curl -sf --max-time {t} -o /dev/null "$$1"', + "wget": f'wget -q -T {t} -O /dev/null "$$1"', + "python3": f"python3 -c 'import sys, urllib.request; urllib.request.urlopen(sys.argv[1], timeout={t})' \"$$1\"", + "node": (f"node -e 'fetch(process.argv[1], {{signal: AbortSignal.timeout({t * 1000})}})" + f".then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))' \"$$1\""), + } + pick = " el".join(f"if command -v {name} >/dev/null 2>&1; then get() {{ {fetch}; }}; " + for name, fetch in fetchers.items()) urls = " ".join(f"http://localhost:{port}{path}" for path in paths) - return f"for u in {urls}; do {{ {' || '.join(fetchers)}; }} 2>/dev/null && exit 0; done; exit 1" + return f"{pick}else exit 1; fi; for u in {urls}; do get \"$$u\" 2>/dev/null && exit 0; done; exit 1" def _network_lines(*networks: str) -> list[str]: From 78b6ce48ba08381078be954993c05c871149ee70 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 13 Nov 2026 07:41:41 +0000 Subject: [PATCH 3/3] style(gateway): single spaces between the probe's client branches --- src/agent_env/providers/env_providers/env_gateway_provider.py | 2 +- 1 file changed, 1 insertion(+), 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 556854c9f..ec9747648 100644 --- a/src/agent_env/providers/env_providers/env_gateway_provider.py +++ b/src/agent_env/providers/env_providers/env_gateway_provider.py @@ -1743,7 +1743,7 @@ def _http_probe(port: int, *paths: str) -> str: "node": (f"node -e 'fetch(process.argv[1], {{signal: AbortSignal.timeout({t * 1000})}})" f".then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))' \"$$1\""), } - pick = " el".join(f"if command -v {name} >/dev/null 2>&1; then get() {{ {fetch}; }}; " + pick = "el".join(f"if command -v {name} >/dev/null 2>&1; then get() {{ {fetch}; }}; " for name, fetch in fetchers.items()) urls = " ".join(f"http://localhost:{port}{path}" for path in paths) return f"{pick}else exit 1; fi; for u in {urls}; do get \"$$u\" 2>/dev/null && exit 0; done; exit 1"