diff --git a/src/agent_env/agents/agents/codex/Dockerfile b/src/agent_env/agents/agents/codex/Dockerfile index f477158a0..c4cce427a 100644 --- a/src/agent_env/agents/agents/codex/Dockerfile +++ b/src/agent_env/agents/agents/codex/Dockerfile @@ -1,3 +1,17 @@ +# The env's clock: the egress proxy, one static Go binary, and agent-env's libfaketime and its TLS shim, taken from +# agent-env's sources as clock/ and egress/ (shared_sources.toml) and built for this image's libc (Debian bookworm) in +# stages of their own, so no toolchain is left in the image. +FROM public.ecr.aws/docker/library/golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS egress +COPY egress/ /egress/ +RUN cd /egress && go vet ./... && go test -count=1 ./... \ + && CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags=-s -o /out/egress . + +FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 AS clock +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + build-essential patch curl ca-certificates +COPY clock/ /clock/ +RUN sh /clock/build.sh /out + FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 # Node for the Codex CLI's launcher and mcp-remote, checked against its published checksum; install.sh brings the same. ARG NODE_VERSION=22.23.3 @@ -22,12 +36,36 @@ COPY requirements.txt model-requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt -r model-requirements.txt # install.sh and run.sh are install/v1's: in the image, so they stay in its build context. COPY a2a_server.py codex_bridge.py codex_config.py codex_kit.py entrypoint.sh install.sh run.sh ./ -# The CLI's cwd, /app, and its state, fixed so neither follows the uid: Modal runs the image as root. +# Every clocked agent's egress rule, which the bridge imports, and the clock page's format it reads (shared_sources.toml). +COPY agent_clock/agent_egress.py clock/clockpage.py ./ +# The CLI's cwd, /app, and its state, fixed so neither follows the uid. RUN useradd --create-home --uid 10001 agent && mkdir -p /app /home/agent/.codex \ && chown agent:agent /app /home/agent/.codex && chmod 0755 entrypoint.sh +# The env's clock: the follower and the egress proxy, each as its own user, under root's supervisord (root.conf), and +# agent-env's libfaketime and its TLS shim preloaded into every process, faking only the agent's (clock.conf). The +# follower's config is the agent's user's to write (agentenv_protocol.a2a_agent.clock) and the follower's to read. +RUN pip install --no-cache-dir supervisor==4.2.5 \ + && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-clock \ + && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-egress \ + && mkdir -p /usr/local/lib/agentenv-clock /usr/local/lib/agentenv-egress /etc/agentenv \ + && install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock \ + && touch /etc/agentenv/ca.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /usr/local/lib/agentenv-egress/roots.pem +COPY --from=clock /out/libfaketimeMT.so.1 /out/libtls_realtime.so /usr/local/lib/agentenv-clock/ +COPY --from=egress /out/egress /usr/local/lib/agentenv-egress/egress +COPY clock/clockpage.py clock/clock-follower /usr/local/lib/agentenv-clock/ +COPY clock/clock.conf /etc/agentenv/clock.conf +COPY root.conf /etc/agentenv/root.conf +COPY start-agent /usr/local/bin/start-agent +RUN sed -i -e "s/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/" \ + -e "s/^AGENTENV_CLOCK_PAGE_UID=.*/AGENTENV_CLOCK_PAGE_UID=$(id -u agentenv-clock)/" /etc/agentenv/clock.conf \ + && chmod 0755 /usr/local/lib/agentenv-clock/clock-follower /usr/local/bin/start-agent \ + && printf '%s\n' /usr/local/lib/agentenv-clock/libfaketimeMT.so.1 /usr/local/lib/agentenv-clock/libtls_realtime.so \ + > /etc/ld.so.preload ENV A2A_PORT=8000 PYTHONPATH=/opt/agent CODEX_HOME=/home/agent/.codex -USER agent WORKDIR /app EXPOSE 8000 -# CMD rather than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. -CMD ["/opt/agent/entrypoint.sh"] +# Starts as root, which start-agent needs for the clock; the A2A server and the CLI run as the agent's user. CMD rather +# than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. +CMD ["/usr/local/bin/start-agent"] diff --git a/src/agent_env/agents/agents/codex/a2a_server.py b/src/agent_env/agents/agents/codex/a2a_server.py index a29a7f9c4..a3d5fc516 100644 --- a/src/agent_env/agents/agents/codex/a2a_server.py +++ b/src/agent_env/agents/agents/codex/a2a_server.py @@ -698,6 +698,7 @@ def _record_event(event: dict) -> None: task_id=cfg.task_id, event_callback=_record_event, max_turns=cfg.max_turns, + mcp_urls=[info["url"] for info in request.mcp_servers.values()], config_overrides=[*codex_config.role_overrides(request.mcp_servers, cfg.role), *codex_config.builtin_tools_overrides(cfg.builtin_tools), *catalog], diff --git a/src/agent_env/agents/agents/codex/codex_bridge.py b/src/agent_env/agents/agents/codex/codex_bridge.py index 07b4f1639..03dc27e6f 100644 --- a/src/agent_env/agents/agents/codex/codex_bridge.py +++ b/src/agent_env/agents/agents/codex/codex_bridge.py @@ -22,6 +22,7 @@ from datetime import datetime, timezone from pathlib import Path +from agent_egress import clock_armed, egress_env from codex_config import MODEL_HEADERS_ENV, model_headers logger = logging.getLogger(__name__) @@ -93,10 +94,12 @@ def get_last_attribution_snapshot() -> dict[str, str | None]: def cli_env(environ: Mapping[str, str], *, project_id: str | None = None, - task_id: str | None = None) -> dict[str, str]: + task_id: str | None = None, mcp_urls: Sequence[str] = (), clock_armed: bool = False) -> dict[str, str]: """The CLI's environment: this process's, with the endpoint's key also under the name codex's own precheck reads, and the run's attribution, which config.toml's env_http_headers sends as headers on each model call. The - run's tags join the x-litellm-tags [model.headers] names.""" + run's tags join the x-litellm-tags [model.headers] names. On an armed clock (``clock_armed``) the CLI goes out through the + egress proxy (``egress_env``) for the model endpoint and the env's MCP servers (``mcp_urls``), which mcp-remote, + handed the proxy's variables (codex_config._PROXY_ENV), reaches through it too.""" env = dict(environ) if env.get("LITELLM_API_KEY") and not env.get("OPENAI_API_KEY"): env["OPENAI_API_KEY"] = env["LITELLM_API_KEY"] @@ -110,6 +113,7 @@ def cli_env(environ: Mapping[str, str], *, project_id: str | None = None, if name.lower() == _TAGS_HEADER), "") if joined := _join_tags(configured, ",".join(tags)): env["LITELLM_TAGS"] = joined + env.update(egress_env(environ, [env.get("LITELLM_BASE_URL", ""), *mcp_urls], armed=clock_armed)) return env @@ -726,6 +730,7 @@ async def _run_codex_once( config_overrides: Sequence[str] = (), max_turns: int | None = None, prior_tool_calls: int = 0, + mcp_urls: Sequence[str] = (), ) -> CodexResult: # JSON Schema: codex's `--output-schema` flag takes a FILE PATH (unlike # claude-code's inline-JSON `--json-schema`). Write the inner schema to a @@ -774,7 +779,8 @@ async def _run_codex_once( f"inactivity_watchdog={inactivity_s}s)" ) - subprocess_env = cli_env(os.environ, project_id=project_id, task_id=task_id) + subprocess_env = cli_env(os.environ, project_id=project_id, task_id=task_id, mcp_urls=mcp_urls, + clock_armed=clock_armed()) if project_id or task_id: _record_attribution(project_id=project_id, task_id=task_id) logger.info( @@ -900,6 +906,7 @@ async def run_codex( event_callback: Callable[[dict], None] | None = None, config_overrides: Sequence[str] = (), max_turns: int | None = None, + mcp_urls: Sequence[str] = (), ) -> CodexResult: """Run one logical turn with bounded transient recovery. @@ -939,6 +946,7 @@ async def run_codex( event_callback=event_callback, config_overrides=config_overrides, max_turns=max_turns, + mcp_urls=mcp_urls, prior_tool_calls=tool_call_count, ) failure = _resumable_api_failure(result) diff --git a/src/agent_env/agents/agents/codex/codex_config.py b/src/agent_env/agents/agents/codex/codex_config.py index e60d0f358..ef9467c06 100644 --- a/src/agent_env/agents/agents/codex/codex_config.py +++ b/src/agent_env/agents/agents/codex/codex_config.py @@ -30,6 +30,10 @@ # Codex hands a stdio MCP server only a few of its own variables; these tell mcp-remote, a Node client, which # certificates to trust. _TRUST_ENV = ("NODE_EXTRA_CA_CERTS", "NODE_TLS_REJECT_UNAUTHORIZED", "SSL_CERT_FILE") +# And these, which the bridge sets on an armed clock (agent_egress.egress_env), send it through the image's egress +# proxy: on the env's date, a certificate mcp-remote checked itself could be before its issue or past its expiry. +_PROXY_ENV = (*(name for base in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY") for name in (base, base.lower())), + "NODE_USE_ENV_PROXY") # The CLI version CodexTool names the tools of; a test holds it to package.json's @openai/codex. TOOLS_CLI_VERSION = "0.147.0" @@ -244,7 +248,8 @@ def _server_entry(url: str, headers: dict[str, str] | None = None) -> dict[str, """Build a codex `mcp_servers.` config table. Wraps the URL in `mcp-remote` (stdio shim), a Node client that trusts the - certificates `_TRUST_ENV` names in the agent's environment. Each header is passed + certificates `_TRUST_ENV` names in the agent's environment and goes through the + proxy `_PROXY_ENV` names when the bridge sets one. Each header is passed as a `--header "Name: Value"` arg. The `AgentEnv-Role` header, which the env gateway filters tools by, is mcp-remote's `${AGENTENV_ROLE}`: the role the registration's own headers name, from the entry's env, unless a CLI run passes @@ -263,7 +268,7 @@ def _server_entry(url: str, headers: dict[str, str] | None = None) -> dict[str, "command": "mcp-remote", "args": args, "env": {_ROLE_VARIABLE: registered}, - "env_vars": list(_TRUST_ENV), + "env_vars": [*_TRUST_ENV, *_PROXY_ENV], "startup_timeout_sec": 60, # Codex silently drops non-`required` servers that miss its hard-coded # 1s startup grace, which mcp-remote always does — but a `required` diff --git a/src/agent_env/agents/agents/codex/install.sh b/src/agent_env/agents/agents/codex/install.sh index 57fb9d255..c7b2c6deb 100755 --- a/src/agent_env/agents/agents/codex/install.sh +++ b/src/agent_env/agents/agents/codex/install.sh @@ -24,7 +24,7 @@ START_TIMEOUT_S=120 STOP_TIMEOUT_S=10 dockerfile_arg() { sed -n "s/^ARG $1=//p" "$here/Dockerfile"; } NODE_VERSION=$(dockerfile_arg NODE_VERSION) -AGENT_FILES="a2a_server.py codex_bridge.py codex_config.py codex_kit.py requirements.txt run.sh" +AGENT_FILES="a2a_server.py codex_bridge.py codex_config.py codex_kit.py requirements.txt run.sh agent_clock/agent_egress.py clock/clockpage.py" # put SRC DEST replaces DEST by a rename, so a binary an earlier install's agent still runs can be replaced; put_dir SRC # DEST copies a folder to DEST, which isn't there. @@ -144,7 +144,7 @@ put "$stage/agent.env" "$root/agent.env" put "$here/package.json" "$root/npm/package.json" put "$here/package-lock.json" "$root/npm/package-lock.json" for file in $AGENT_FILES; do - put "$here/$file" "$root/app/$file" + put "$here/$file" "$root/app/${file##*/}" done on "chmod 0600 $r/agent.env && chmod 0755 $r/bin/uv $r/bin/codex $r/bin/mcp-remote $r/app/run.sh" diff --git a/src/agent_env/agents/agents/codex/root.conf b/src/agent_env/agents/agents/codex/root.conf new file mode 100644 index 000000000..98b86fa9b --- /dev/null +++ b/src/agent_env/agents/agents/codex/root.conf @@ -0,0 +1,62 @@ +; Root's supervisord (start-agent, FD-5916): the clock follower and the egress proxy, each as its own user, on real +; time, and the A2A server as the agent's user. The server starts with the real-time marker, which agent-env's +; libfaketime takes out of its environment, so the server's own records keep real timestamps and everything it starts, +; the CLI and the model's commands, reads the env's time. Its socket and logs are root's alone, so the agent's user can +; neither stop nor restart any of them. The follower follows nothing until the server writes its config, which +; sync_env_clock has it do, then picks it up within a quarter second. +[supervisord] +nodaemon=true +user=root +logfile=/var/log/agentenv-root/supervisord.log +pidfile=/run/agentenv-root/supervisord.pid +childlogdir=/var/log/agentenv-root + +[unix_http_server] +file=/run/agentenv-root/supervisor.sock +chmod=0700 + +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface + +[supervisorctl] +serverurl=unix:///run/agentenv-root/supervisor.sock + +[program:clock] +command=/usr/local/bin/python3 -I /usr/local/lib/agentenv-clock/clock-follower +user=agentenv-clock +directory=/ +priority=3 +autorestart=true +startsecs=1 +startretries=1000 +redirect_stderr=true +stdout_logfile=/var/log/agentenv-root/clock.log +environment=HOME="/nonexistent",USER="agentenv-clock",LOGNAME="agentenv-clock" + +[program:egress] +command=/usr/local/lib/agentenv-egress/egress serve +user=agentenv-egress +directory=/ +priority=3 +autorestart=true +startsecs=1 +startretries=1000 +redirect_stderr=true +stdout_logfile=/var/log/agentenv-root/egress.log +environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress",AGENTENV_EGRESS_PRIVATE="allow" + +; uvicorn itself, not entrypoint.sh: the marker holds for the process it's given to alone, and a shell that execs +; uvicorn would hand uvicorn an environment without it. +[program:agent] +command=/usr/local/bin/python3 -m uvicorn a2a_server:app --host 0.0.0.0 --port %(ENV_A2A_PORT)s --timeout-graceful-shutdown 5 +user=agent +directory=/app +priority=10 +autorestart=true +stopasgroup=true +killasgroup=true +stopwaitsecs=30 +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +environment=HOME="/home/agent",USER="agent",LOGNAME="agent",AGENTENV_REAL_TIME="1",AGENTENV_EGRESS_PROXY="http://127.0.0.1:3128",NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem",SSL_CERT_FILE="/etc/agentenv/ca-bundle.pem",REQUESTS_CA_BUNDLE="/etc/agentenv/ca-bundle.pem" diff --git a/src/agent_env/agents/agents/codex/shared_sources.toml b/src/agent_env/agents/agents/codex/shared_sources.toml new file mode 100644 index 000000000..6076f3540 --- /dev/null +++ b/src/agent_env/agents/agents/codex/shared_sources.toml @@ -0,0 +1,5 @@ +# What this image's build context takes from agent-env's own sources, at the name its Dockerfile copies it from: one +# copy of each, shared with the other images that take it (utils/docker/context.py). Paths are under src/agent_env. +clock = "utils/faketime_clock" +egress = "utils/egress_proxy" +agent_clock = "utils/agent_clock" diff --git a/src/agent_env/agents/agents/codex/start-agent b/src/agent_env/agents/agents/codex/start-agent new file mode 100755 index 000000000..b813e50d9 --- /dev/null +++ b/src/agent_env/agents/agents/codex/start-agent @@ -0,0 +1,30 @@ +#!/bin/sh +# The image's start (FD-5916). As root, it makes what the agent's user must not touch, the clock page and file (the +# clock user's from their first byte) and the egress proxy's CA (its key the egress user's alone, trusted in the +# system store, agent-env's trust files and every certifi bundle), then starts root's supervisord (root.conf), which +# runs the clock follower and the egress proxy, each as its own user on real time, and the A2A server as the agent's +# user on real time, everything it starts reading the env's time from the page. Until sync_env_clock has the agent +# follow a clock, the page holds real time and the proxy relays TLS untouched. Started as another user, the image runs +# the A2A server alone, as before, on real time. +set -eu +if [ "$(id -u)" != 0 ]; then + exec /opt/agent/entrypoint.sh +fi +install -d -o root -g root -m 0700 /var/log/agentenv-root /run/agentenv-root +install -d -o agentenv-clock -g agentenv-clock -m 0755 /run/agentenv-clock +python3 -I /usr/local/lib/agentenv-clock/clockpage.py create agentenv-clock +AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off \ + /usr/local/lib/agentenv-egress/egress boot > /var/log/agentenv-root/egress-boot.log 2>&1 \ + || { cat /var/log/agentenv-root/egress-boot.log >&2; echo "the egress proxy's boot failed" >&2; exit 2; } +# A provider's own CA bundle stays trusted: Sail, for one, points SSL_CERT_FILE and the like at the bundle it signs +# model requests under, and root.conf points the server's trust at agent-env's bundles instead, so the provider's goes +# into them, and into the egress proxy's roots, which carry those requests once the clock is armed. +for bundle in "${SSL_CERT_FILE:-}" "${REQUESTS_CA_BUNDLE:-}" "${NODE_EXTRA_CA_CERTS:-}" "${CURL_CA_BUNDLE:-}"; do + case "$bundle" in ""|/etc/agentenv/*) continue ;; esac + [ -r "$bundle" ] || continue + for target in /etc/agentenv/ca-bundle.pem /etc/agentenv/ca.pem /usr/local/lib/agentenv-egress/roots.pem; do + grep -qxF "# agentenv: $bundle" "$target" 2>/dev/null && continue + { printf '\n# agentenv: %s\n' "$bundle"; cat "$bundle"; } >> "$target" + done +done +exec supervisord -c /etc/agentenv/root.conf diff --git a/src/agent_env/utils/egress_proxy/leaves.go b/src/agent_env/utils/egress_proxy/leaves.go index 027ecac92..90a74962d 100644 --- a/src/agent_env/utils/egress_proxy/leaves.go +++ b/src/agent_env/utils/egress_proxy/leaves.go @@ -17,6 +17,7 @@ import ( "sort" "strings" "sync" + "time" ) // The longest common name X.509 allows (ub-common-name). @@ -85,7 +86,7 @@ func (l *Leaves) For(host string, sans []string, trusted bool) (*tls.Certificate template := &x509.Certificate{ SerialNumber: serial(), Subject: pkix.Name{CommonName: truncate(host, commonNameLimit)}, - NotBefore: by.cert.NotBefore.Add(LeafInset), + NotBefore: latest(by.cert.NotBefore.Add(LeafInset), LeafFrom), NotAfter: by.cert.NotAfter.Add(-LeafInset), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, @@ -140,6 +141,13 @@ func mintCA(name string) (signer, error) { return signer{cert: cert, key: key}, nil } +func latest(a, b time.Time) time.Time { + if a.After(b) { + return a + } + return b +} + func parseSigner(certPEM, keyPEM []byte) (signer, error) { block, _ := pem.Decode(certPEM) if block == nil { diff --git a/src/agent_env/utils/egress_proxy/main.go b/src/agent_env/utils/egress_proxy/main.go index 20169dd06..9a5f408ab 100644 --- a/src/agent_env/utils/egress_proxy/main.go +++ b/src/agent_env/utils/egress_proxy/main.go @@ -71,6 +71,10 @@ const PrivateEnv = "AGENTENV_EGRESS_PRIVATE" var ( CAFrom = time.Date(1950, 1, 1, 0, 0, 0, 0, time.UTC) CAUntil = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC) + + // The earliest a leaf starts, however early its CA does: rustls (webpki, as in the Codex CLI) refuses any + // certificate dated before the Unix epoch as badly encoded. + LeafFrom = time.Unix(0, 0).UTC() ) func main() { diff --git a/src/agent_env/utils/egress_proxy/proxy_test.go b/src/agent_env/utils/egress_proxy/proxy_test.go index 772afe1f8..53812bcf6 100644 --- a/src/agent_env/utils/egress_proxy/proxy_test.go +++ b/src/agent_env/utils/egress_proxy/proxy_test.go @@ -202,7 +202,7 @@ func TestAUniverseInsideTheSitesCertificateIsSplicedAndOneOutsideIntercepted(t * t.Fatalf("outside: %q %v", body, err) } leaf := resp.TLS.PeerCertificates[0] - if leaf.CheckSignatureFrom(h.ca.cert) != nil || !leaf.NotAfter.After(later) || leaf.NotBefore.Year() != 1950 { + if leaf.CheckSignatureFrom(h.ca.cert) != nil || !leaf.NotAfter.After(later) || !leaf.NotBefore.Equal(LeafFrom) { t.Fatalf("the client's leaf: issuer %s, %s to %s", leaf.Issuer, leaf.NotBefore, leaf.NotAfter) } row := h.lastRow() diff --git a/tst/integration/task_step/test_agent_env_clock_local.py b/tst/integration/task_step/test_agent_env_clock_local.py index 87468d613..6de4f6411 100644 --- a/tst/integration/task_step/test_agent_env_clock_local.py +++ b/tst/integration/task_step/test_agent_env_clock_local.py @@ -89,6 +89,7 @@ class Agent: AGENTS = { "claude-code": Agent(bridge="claude_bridge", version=("claude", "--version"), shows="(Claude Code)", turn=_claude_turn), + "codex": Agent(bridge="codex_bridge", version=("codex", "--version"), shows="codex-cli"), "opencode": Agent(bridge="opencode_bridge", version=("opencode", "--version"), shows="1.17.18"), } diff --git a/tst/unit/agents/codex/agent.py b/tst/unit/agents/codex/agent.py index af4353fdb..7d718ab2f 100644 --- a/tst/unit/agents/codex/agent.py +++ b/tst/unit/agents/codex/agent.py @@ -1,18 +1,24 @@ -"""The Codex agent's modules, imported from its folder as its image runs them: by their own names, from the folder on -the path.""" +"""The Codex agent's modules, imported as its image runs them: by their own names, from its folder and from the folders +its build context takes as agent_clock/ and clock/, whose modules the image copies beside the bridge.""" import importlib import importlib.util +import os import sys from pathlib import Path import agent_env +from tst.util.build_context import sent AGENT_DIR = Path(agent_env.__file__).parent / "agents" / "agents" / "codex" -sys.path.insert(0, str(AGENT_DIR)) +IMPORTED = [str(AGENT_DIR), str(sent(AGENT_DIR, "agent_clock/agent_egress.py").parent), + str(sent(AGENT_DIR, "clock/clockpage.py").parent)] +PYTHONPATH = os.pathsep.join(IMPORTED) +sys.path[:0] = IMPORTED codex_bridge = importlib.import_module("codex_bridge") codex_config = importlib.import_module("codex_config") codex_kit = importlib.import_module("codex_kit") +agent_egress = importlib.import_module("agent_egress") # By its path: Claude Code's a2a_server, imported by the same name, may be loaded already. _spec = importlib.util.spec_from_file_location("codex_a2a_server", AGENT_DIR / "a2a_server.py") a2a_server = importlib.util.module_from_spec(_spec) diff --git a/tst/unit/agents/codex/test_codex_executor_outcome.py b/tst/unit/agents/codex/test_codex_executor_outcome.py index 7fe304d5a..ddb5261ca 100644 --- a/tst/unit/agents/codex/test_codex_executor_outcome.py +++ b/tst/unit/agents/codex/test_codex_executor_outcome.py @@ -183,9 +183,10 @@ async def exercise(): assert seen_thread_ids == [None, "shared-thread"] -def test_every_server_is_required_and_mcp_remote_gets_the_ca_variables(monkeypatch, tmp_path): +def test_every_server_is_required_and_mcp_remote_gets_the_ca_and_proxy_variables(monkeypatch, tmp_path): """Codex drops an optional server that misses its 1s grace, which mcp-remote always does; and codex hands a stdio - server only a few of its variables, so the CA ones are named for it to pass on.""" + server only a few of its variables, so the CA ones, and the egress proxy's an armed clock sets, are named for it to + pass on.""" monkeypatch.setenv("CODEX_HOME", str(tmp_path)) asyncio.run(codex_config.write_mcp_servers( {"envmcp": {"url": "https://gw/mcp", "headers": None}, @@ -193,6 +194,8 @@ def test_every_server_is_required_and_mcp_remote_gets_the_ca_variables(monkeypat servers = tomllib.loads((tmp_path / "config.toml").read_text())["mcp_servers"] assert {name: (server["required"], server["env_vars"]) for name, server in servers.items()} == { - name: (True, ["NODE_EXTRA_CA_CERTS", "NODE_TLS_REJECT_UNAUTHORIZED", "SSL_CERT_FILE"]) + name: (True, ["NODE_EXTRA_CA_CERTS", "NODE_TLS_REJECT_UNAUTHORIZED", "SSL_CERT_FILE", "HTTP_PROXY", "http_proxy", + "HTTPS_PROXY", "https_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy", + "NODE_USE_ENV_PROXY"]) for name in ("envmcp", "peer_agents") } \ No newline at end of file diff --git a/tst/unit/agents/codex/test_config_and_mcp.py b/tst/unit/agents/codex/test_config_and_mcp.py index ded20a4cc..1cc14a250 100644 --- a/tst/unit/agents/codex/test_config_and_mcp.py +++ b/tst/unit/agents/codex/test_config_and_mcp.py @@ -149,7 +149,9 @@ async def exercise(): "command": "mcp-remote", "args": ["https://gw/mcp", "--allow-http", "--header", "X-Tenant: t", "--header", "AgentEnv-Role: ${AGENTENV_ROLE}"], "env": {"AGENTENV_ROLE": ""}, - "env_vars": ["NODE_EXTRA_CA_CERTS", "NODE_TLS_REJECT_UNAUTHORIZED", "SSL_CERT_FILE"], + "env_vars": ["NODE_EXTRA_CA_CERTS", "NODE_TLS_REJECT_UNAUTHORIZED", "SSL_CERT_FILE", + "HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy", + "NODE_USE_ENV_PROXY"], "startup_timeout_sec": 60, "required": True, } diff --git a/tst/unit/agents/codex/test_env_clock.py b/tst/unit/agents/codex/test_env_clock.py new file mode 100644 index 000000000..e8da13dd4 --- /dev/null +++ b/tst/unit/agents/codex/test_env_clock.py @@ -0,0 +1,66 @@ +"""Codex on the env's clock: its bridge hands the shared egress rule (tst/unit/agents/test_agent_egress.py) the model +endpoint's URL and every MCP server's, which mcp-remote reaches with the proxy's variables, so on an armed clock the CLI goes out through the image's egress proxy, and to loopback, the hosts +NO_PROXY names and a plain-HTTP model endpoint directly; unarmed, it goes out as before. What every clocked image shares +is checked in tst/unit/agents/test_agent_clock_images.py.""" + +from __future__ import annotations + +import subprocess + +import pytest + +from tst.unit.agents.codex.agent import AGENT_DIR, agent_egress, codex_bridge + +PROXY = "http://127.0.0.1:3128" + + +def test_on_an_armed_clock_the_cli_goes_out_through_the_proxy(): + env = codex_bridge.cli_env({agent_egress.EGRESS_PROXY_ENV: PROXY, "NO_PROXY": "registry.internal"}, + clock_armed=True) + + assert {env[name] for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "https_proxy")} == {PROXY} + assert {"localhost", "host.docker.internal", "registry.internal"} <= set(env["NO_PROXY"].split(",")) + + +@pytest.mark.parametrize("environ, armed", [({}, True), ({agent_egress.EGRESS_PROXY_ENV: PROXY}, False)]) +def test_without_the_proxy_or_an_armed_clock_the_cli_goes_out_directly_as_before(environ, armed): + assert not {"HTTPS_PROXY", "NO_PROXY"} & set(codex_bridge.cli_env(environ, clock_armed=armed)) + + +@pytest.mark.parametrize("base_url, direct", [("http://10.0.0.5:4000", True), ("https://litellm.example.com", False)]) +def test_a_model_endpoint_over_plain_http_is_reached_directly_on_an_armed_clock(base_url, direct): + env = codex_bridge.cli_env({agent_egress.EGRESS_PROXY_ENV: PROXY, "LITELLM_BASE_URL": base_url}, clock_armed=True) + assert (base_url.split("//")[1].split(":")[0] in env["NO_PROXY"].split(",")) is direct + + +def test_the_envs_plain_http_mcp_servers_are_reached_directly_and_https_ones_through_the_proxy_on_an_armed_clock(): + """mcp-remote, a Node client, checks an HTTPS server's certificate against the env's date, which the proxy, on real + time, doesn't.""" + env = codex_bridge.cli_env({agent_egress.EGRESS_PROXY_ENV: PROXY}, + mcp_urls=["http://gateway:8080/mcp", "https://abc-123.w.modal.host/mcp"], + clock_armed=True) + direct = set(env["NO_PROXY"].split(",")) + + assert "gateway" in direct and "abc-123.w.modal.host" not in direct + assert env["NODE_USE_ENV_PROXY"] == "1" + + +def test_start_agent_keeps_a_providers_ca_bundle_trusted_by_the_server_and_the_proxy(tmp_path): + """Sail points SSL_CERT_FILE and the like at the bundle it signs model requests under; root.conf points the server + at agent-env's bundles, so start-agent adds the provider's to them and to the proxy's roots, once each.""" + start = (AGENT_DIR / "start-agent").read_text() + merge = start[start.index('for bundle in "${SSL_CERT_FILE'):start.index("exec supervisord")] + targets = {"/etc/agentenv/ca-bundle.pem": tmp_path / "ca-bundle.pem", "/etc/agentenv/ca.pem": tmp_path / "ca.pem", + "/usr/local/lib/agentenv-egress/roots.pem": tmp_path / "roots.pem"} + for real, fake in targets.items(): + fake.write_text("SYSTEM ROOTS\n") + merge = merge.replace(real, str(fake)) + sail = tmp_path / "sailbox-ca-bundle.crt" + sail.write_text("-----BEGIN CERTIFICATE-----\nSAIL\n-----END CERTIFICATE-----\n") + + subprocess.run(["sh", "-c", merge], check=True, env={"PATH": "/usr/bin:/bin", "SSL_CERT_FILE": str(sail), + "NODE_EXTRA_CA_CERTS": str(sail), "REQUESTS_CA_BUNDLE": "/etc/agentenv/ca-bundle.pem"}) + + for fake in targets.values(): + text = fake.read_text() + assert text.startswith("SYSTEM ROOTS\n") and text.count("SAIL") == 1, (fake.name, text) diff --git a/tst/unit/agents/codex/test_image_contract.py b/tst/unit/agents/codex/test_image_contract.py index f122eb24e..cf4042455 100644 --- a/tst/unit/agents/codex/test_image_contract.py +++ b/tst/unit/agents/codex/test_image_contract.py @@ -7,10 +7,13 @@ import ast import json import re +import tomllib from tst.unit.agents.codex.agent import AGENT_DIR DOCKERFILE = (AGENT_DIR / "Dockerfile").read_text() +SOURCES = AGENT_DIR.parents[2] +SHARED = tomllib.loads((AGENT_DIR / "shared_sources.toml").read_text()) INSTALL_SH = (AGENT_DIR / "install.sh").read_text() @@ -23,10 +26,22 @@ def _copied() -> list[str]: for name in line.split()] +def _source(name: str): + """Where the build context's ``name`` comes from: the folder, or a folder shared_sources.toml takes from agent-env's + own sources at that name.""" + first, _, rest = name.partition("/") + return SOURCES / SHARED[first] / rest if first in SHARED else AGENT_DIR / name + + +def _in_context(name: str) -> bool: + path = _source(name) + return path.is_dir() if name.endswith("/") else path.is_file() + + def test_every_file_the_image_copies_is_in_the_folder(): copied = _copied() - assert copied and all((AGENT_DIR / name).is_file() for name in copied), copied + assert copied and all(_in_context(name) for name in copied), copied # install.sh and run.sh are install/v1's; a build context pruned to what the Dockerfile reads keeps them so. assert {"a2a_server.py", "codex_bridge.py", "codex_config.py", "codex_kit.py", "entrypoint.sh", "install.sh", "run.sh", "package.json", "package-lock.json"} <= set(copied) @@ -43,10 +58,19 @@ def _imported(path) -> set[str]: def test_the_image_and_an_install_carry_every_module_the_server_imports(): - modules = {path.stem: path for path in AGENT_DIR.glob("*.py")} - needed = {"a2a_server.py"} | {f"{name}.py" for path in modules.values() for name in _imported(path) & set(modules)} + """The folder's own modules and those the build context takes from agent-env's sources, the egress rule and the + clock page's format among them, each by its name in the context.""" + shared = {path.stem: f"{name}/{path.name}" for name, source in SHARED.items() + for path in (SOURCES / source).glob("*.py")} + modules = {**shared, **{path.stem: path.name for path in AGENT_DIR.glob("*.py")}} + needed, pending = set(), ["a2a_server"] + while pending: + if (name := modules[pending.pop()]) not in needed: + needed.add(name) + pending += _imported(_source(name)) & set(modules) installed = set(re.search(r'^AGENT_FILES="([^"]+)"$', INSTALL_SH, re.MULTILINE).group(1).split()) + assert {"agent_clock/agent_egress.py", "clock/clockpage.py"} <= needed assert needed <= set(_copied()) and needed <= installed, (needed, installed) @@ -98,5 +122,5 @@ def test_tls_checks_stay_on(): def test_the_image_starts_through_cmd_which_modal_passes_its_env_to(): - assert 'CMD ["/opt/agent/entrypoint.sh"]' in DOCKERFILE + assert 'CMD ["/usr/local/bin/start-agent"]' in DOCKERFILE assert not re.search(r"^ENTRYPOINT", DOCKERFILE, re.MULTILINE) diff --git a/tst/unit/agents/codex/test_message_contract.py b/tst/unit/agents/codex/test_message_contract.py index cbdeda03f..d93a23549 100644 --- a/tst/unit/agents/codex/test_message_contract.py +++ b/tst/unit/agents/codex/test_message_contract.py @@ -134,7 +134,7 @@ async def exercise(): assert call == { "prompt": "line one", "thread_id": None, "model": "gpt-x", "effort": "high", "system_prompt": "be brief", "output_format": schema, "timeout_s": 42, "max_turns": 45, - "project_id": "p-1", "task_id": "t-1", "config_overrides": [], + "project_id": "p-1", "task_id": "t-1", "config_overrides": [], "mcp_urls": [], }