Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions src/agent_env/providers/env_providers/env_gateway_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}",
Expand Down Expand Up @@ -1728,6 +1727,28 @@ 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. 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"{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]:
return [" networks:", *(f" - {network}" for network in networks)]

Expand Down
49 changes: 46 additions & 3 deletions tst/unit/providers/env_providers/env_gateway_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Loading