Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ae1d4be
feat(agents): agents follow the env's clock the way computers do (FD-…
williamloo-scale Nov 16, 2026
eea933c
refactor(agents): leave the Claude Code image to its own PR; agents s…
williamloo-scale Nov 17, 2026
608a93d
Merge origin/main
williamloo-scale Nov 17, 2026
182cef0
feat(agents): the Claude Code image follows the env's clock (FD-5916)
williamloo-scale Nov 17, 2026
bce00d4
fix(agents): agent clock review: new config's status, one clock per a…
williamloo-scale Nov 17, 2026
84381b8
fix(agents): keep each agent's clock record on a key of its own; a re…
williamloo-scale Nov 17, 2026
56dac45
fix(agents): agent clock keys can't collide; one clock env per agent;…
williamloo-scale Nov 17, 2026
c726743
chore: drop test run output committed by mistake
williamloo-scale Nov 17, 2026
5e8c756
Merge #714
williamloo-scale Nov 17, 2026
3b6f499
test(agents): the Claude Code image follows the env's clock end to en…
williamloo-scale Nov 17, 2026
cecc686
fix(agents): a failed agent sync retires its follower; the task's zon…
williamloo-scale Nov 17, 2026
afe55b4
Merge origin/main
williamloo-scale Nov 17, 2026
d523a6e
Merge #714
williamloo-scale Nov 17, 2026
ac0a39e
fix(agents): libfaketime's init can't deadlock the CLI; the CLI goes …
williamloo-scale Nov 17, 2026
c8ed6e1
fix(agents): a later clock sync leaves agents teardown_sandboxes took…
williamloo-scale Nov 17, 2026
b5d3b4b
fix(ci): the agent clock e2e skips as a declared capability gap; the …
williamloo-scale Nov 17, 2026
25af21a
chore: drop test-run output committed again, and ignore where the pro…
williamloo-scale Nov 17, 2026
7216c7c
fix(agents): route the CLI through egress by the clock page's armed f…
williamloo-scale Nov 17, 2026
fc3134b
Merge #714
williamloo-scale Nov 17, 2026
f8bd9ff
feat(a2a-agent): validate clock/v1 by the time the agent's own comman…
earakely-scale Nov 17, 2026
f9af1b6
refactor(a2a-agent): name the clock validation's timeouts and error l…
williamloo-scale Nov 17, 2026
43fdb97
fix(gateway): get_time says the machine's clock may not be synchroniz…
earakely-scale Nov 18, 2026
8bf8e4c
refactor(a2a-agent): move agent_clock to a2a_agent/clock.py, beside t…
earakely-scale Nov 18, 2026
810f42e
Merge #714
williamloo-scale Nov 18, 2026
686fdf1
test(agents): the agent clock e2e imports FOLLOWING_KEY from a2a_agen…
williamloo-scale Nov 18, 2026
9d7d6c0
Merge origin/main (#714 merged)
williamloo-scale Nov 18, 2026
1ff90bd
fix(agents): the Claude Code image installs protocol 0.1.575, the fir…
earakely-scale Nov 18, 2026
ca97b37
fix(computers): the clock follower follows a changed config while its…
earakely-scale Nov 18, 2026
0875e06
fix(agents): an armed clock keeps the agent's own NO_PROXY hosts dire…
williamloo-scale Nov 18, 2026
bb4f07f
test(agents): the agent clock checks run over every clocked agent ima…
williamloo-scale Nov 18, 2026
db76619
feat(agents): the pi image follows the env's clock (FD-5916)
williamloo-scale Nov 18, 2026
fef5804
fix(agents): pi's supervisord gets setuptools, whose pkg_resources su…
williamloo-scale Nov 18, 2026
a3f9e34
Merge main into #726: the clock and egress proxy now live in utils (#…
earakely-scale Nov 18, 2026
3c60fda
refactor(agents): the Claude Code image takes the clock and egress pr…
earakely-scale Nov 18, 2026
c5c4c3f
Merge #726: the clock and egress proxy come from utils
williamloo-scale Nov 18, 2026
150c563
test(agents): Claude Code's contract test takes the folders shared_so…
williamloo-scale Nov 18, 2026
4d17529
refactor(agents): the image takes the clock and egress proxy from uti…
williamloo-scale Nov 18, 2026
35bf07b
Merge #726
williamloo-scale Nov 18, 2026
aed27ee
fix(agents): a private HTTPS model endpoint is reached directly on an…
williamloo-scale Nov 18, 2026
0f4d1fd
fix(agents): a private HTTPS model endpoint is reached directly on an…
williamloo-scale Nov 18, 2026
032d11e
Merge #726
williamloo-scale Nov 18, 2026
590e3b1
test(agents): the clock test's HTTPS model hosts resolve publicly wit…
williamloo-scale Nov 18, 2026
a6f3720
fix(agents): the model host's lookup is bounded and kept, so a slow r…
williamloo-scale Nov 18, 2026
5b241d1
fix(agents): the model host's lookup is bounded and kept, as #726's is
williamloo-scale Nov 18, 2026
6fffd85
Merge #726
williamloo-scale Nov 18, 2026
4cf8922
Merge main (#726 merged)
williamloo-scale Nov 18, 2026
140eda8
refactor(agents): follow the merged Claude Code image (#726): the pro…
williamloo-scale Nov 18, 2026
32b1b56
fix(agents): keep a provider's CA bundle trusted, and send HTTPS MCP …
williamloo-scale Nov 18, 2026
e348853
Merge main: the control token (#780, #784)
williamloo-scale Nov 19, 2026
82c6e22
Merge remote-tracking branch 'origin/main' into wloo/pi-clock-image
williamloo-scale Nov 19, 2026
4f88129
Merge remote-tracking branch 'origin/main' into wloo/pi-clock-image
williamloo-scale Nov 19, 2026
a4e9b4b
refactor(pi): take the shared egress rule (#818)
williamloo-scale Nov 19, 2026
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
45 changes: 43 additions & 2 deletions src/agent_env/agents/agents/pi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
# docker build -t pi-a2a src/agent_env/agents/agents/pi
# 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 (Ubuntu 24.04's, on
# the image's own base) 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 mirror.gcr.io/library/ubuntu:24.04@sha256:534baea6a22c03a63003dbc8dbe78fe34bc0d7e595d9a9dc9834884ff530eb55 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 mirror.gcr.io/library/ubuntu:24.04@sha256:534baea6a22c03a63003dbc8dbe78fe34bc0d7e595d9a9dc9834884ff530eb55
WORKDIR /app
# Named sources: agent-env packs the build context install/v1 copies from the COPY sources.
COPY a2a_server.py changelog.py install.sh peers.py pi_agent.py requirements.txt start.sh ./
# 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 ./
RUN sh install.sh \
&& useradd --create-home agent \
&& mkdir /workspace \
&& chown agent /app /workspace
# 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.
# supervisord goes into the agent's venv, whose bin is on the PATH start-agent runs with.
# supervisor 4.2.5 imports pkg_resources, which a Python 3.12 venv doesn't carry: setuptools before 81 brings it.
RUN /opt/pi-a2a-venv/bin/pip install --no-cache-dir supervisor==4.2.5 'setuptools<81' \
&& 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 PATH="/opt/pi-a2a-venv/bin:/opt/pi/bin:/opt/node/bin:${PATH}"
USER agent
EXPOSE 8000
CMD ["python3", "a2a_server.py"]
# Starts as root, which start-agent needs for the clock; the A2A server and pi 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"]
2 changes: 1 addition & 1 deletion src/agent_env/agents/agents/pi/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Installs Node, pi and this agent's Python environment on Debian or Ubuntu, as root. The image build and
# install/v1 both run it, so an install into a task container matches the image.
set -eu
NODE_VERSION=22.19.0
NODE_VERSION=22.23.3
PI_VERSION=1.1.0
here=$(cd "$(dirname "$0")" && pwd)

Expand Down
11 changes: 10 additions & 1 deletion src/agent_env/agents/agents/pi/pi_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
from starlette.exceptions import HTTPException
from starlette.routing import Route

from agent_egress import clock_armed, egress_env
from changelog import ChangelogCapture, apply as apply_increment
from peers import Peers

Expand Down Expand Up @@ -199,6 +200,14 @@ def _mcp_json(
return {"mcpServers": servers}, env


def cli_env(environ: Mapping[str, str], base_url: str, mcp_servers: Mapping[str, Any] | None = None, *,
clock_armed: bool = False) -> dict[str, str]:
"""The pi CLI's environment, before its run's own variables: this process's, and on an armed clock the egress
proxy (``egress_env``) for the model endpoint and the env's MCP servers, which the CLI calls itself (mcp.json)."""
urls = [base_url, *(str(entry.get("url") or "") for entry in (mcp_servers or {}).values())]
return {**environ, **egress_env(environ, urls, armed=clock_armed)}


def _prompt_text(parts: Sequence[Any], unattached: Sequence[tuple[Path, str]]) -> str:
chunks = []
for part in parts:
Expand Down Expand Up @@ -523,7 +532,7 @@ async def _run_pi(
argv += [f"@{path}" for path in attached]

env = {
**os.environ,
**cli_env(os.environ, base_url, request.mcp_servers, clock_armed=clock_armed()),
**mcp_env,
"PI_CODING_AGENT_DIR": str(agent_dir),
"PI_OFFLINE": "1",
Expand Down
62 changes: 62 additions & 0 deletions src/agent_env/agents/agents/pi/root.conf
Original file line number Diff line number Diff line change
@@ -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/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"

; The server's own interpreter, the venv's, as the image's CMD ran it, not start.sh: the marker holds for the process
; it's given to alone, and a shell that execs the server would hand it an environment without it.
[program:agent]
command=/opt/pi-a2a-venv/bin/python3 a2a_server.py
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"
5 changes: 5 additions & 0 deletions src/agent_env/agents/agents/pi/shared_sources.toml
Original file line number Diff line number Diff line change
@@ -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"
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
agent_clock = "utils/agent_clock"
30 changes: 30 additions & 0 deletions src/agent_env/agents/agents/pi/start-agent
Original file line number Diff line number Diff line change
@@ -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 python3 a2a_server.py
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
3 changes: 3 additions & 0 deletions src/agent_env/agents/agents/pi/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ set -a
. "$here/agent.env"
set +a
export PATH="/opt/pi-a2a-venv/bin:/opt/pi/bin:/opt/node/bin:$PATH"
# The build context keeps the shared egress rule and the clock page's format in the folders it takes them as
# (shared_sources.toml), which the image copies beside the server instead.
export PYTHONPATH="$here/agent_clock:$here/clock${PYTHONPATH:+:$PYTHONPATH}"
cd "$here" && exec python3 a2a_server.py >/tmp/pi-a2a.log 2>&1
1 change: 1 addition & 0 deletions tst/integration/task_step/test_agent_env_clock_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Agent:
"claude-code": Agent(bridge="claude_bridge", version=("claude", "--version"), shows="(Claude Code)",
turn=_claude_turn),
"opencode": Agent(bridge="opencode_bridge", version=("opencode", "--version"), shows="1.17.18"),
"pi": Agent(bridge="pi_agent", version=("pi", "--version"), shows="1.1.0"),
}


Expand Down
12 changes: 9 additions & 3 deletions tst/unit/agents/pi/agent.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
"""The pi agent's modules, imported from its folder as its image runs them: by their own names, from the folder on the
path."""
"""The pi 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" / "pi"
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
changelog = importlib.import_module("changelog")
peers = importlib.import_module("peers")
pi_agent = importlib.import_module("pi_agent")
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("pi_a2a_server", AGENT_DIR / "a2a_server.py")
a2a_server = importlib.util.module_from_spec(_spec)
Expand Down
91 changes: 91 additions & 0 deletions tst/unit/agents/pi/test_env_clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""pi 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 the CLI calls itself, so on an armed clock it goes out through the image's
egress proxy, and to loopback, the hosts NO_PROXY names and plain-HTTP hosts 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 sys
import tarfile

import pytest

from agent_env.utils.docker.context import BuildContext
from tst.unit.agents.pi.agent import AGENT_DIR, agent_egress, pi_agent

PROXY = "http://127.0.0.1:3128"

ENDPOINT = "https://litellm.example.com"


def _direct(env: dict[str, str]) -> set[str]:
return set(env["NO_PROXY"].split(","))


def test_on_an_armed_clock_the_cli_goes_out_through_the_proxy():
env = pi_agent.cli_env({agent_egress.EGRESS_PROXY_ENV: PROXY, "NO_PROXY": "registry.internal", "KEPT": "1"}, ENDPOINT,
clock_armed=True)

assert {env[name] for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "https_proxy")} == {PROXY}
assert env["NODE_USE_ENV_PROXY"] == "1" and env["KEPT"] == "1"
assert {"localhost", "host.docker.internal", "registry.internal"} <= _direct(env)


@pytest.mark.parametrize("environ, armed", [({"KEPT": "1"}, True), ({agent_egress.EGRESS_PROXY_ENV: PROXY, "KEPT": "1"}, False)])
def test_without_the_proxy_or_an_armed_clock_the_cli_goes_out_directly_as_before(environ, armed):
env = pi_agent.cli_env(environ, ENDPOINT, {"env": {"url": "http://10.0.0.7:8080/mcp"}}, clock_armed=armed)

assert env == environ


@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 = pi_agent.cli_env({agent_egress.EGRESS_PROXY_ENV: PROXY}, base_url, clock_armed=True)

assert (base_url.split("//")[1].split(":")[0] in _direct(env)) is direct


def test_the_envs_plain_http_mcp_servers_are_reached_directly_and_https_ones_through_the_proxy_on_an_armed_clock():
servers = {"env": {"url": "http://gateway.internal:8080/mcp"}, "files": {"url": "https://mcp.example.com/mcp"},
pi_agent.PEER_MCP_NAME: {"url": pi_agent.PEER_MCP_URL}}

env = pi_agent.cli_env({agent_egress.EGRESS_PROXY_ENV: PROXY}, ENDPOINT, servers, clock_armed=True)

# Node checks an HTTPS server's certificate against the env's date, which the proxy, on real time, doesn't.
assert {"gateway.internal", "127.0.0.1"} <= _direct(env) and "mcp.example.com" not in _direct(env)
assert "litellm.example.com" not in _direct(env)


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)


def test_an_installed_agent_imports_the_shared_egress_rule_from_the_build_context_it_unpacks(tmp_path):
"""install/v1 unpacks the build context, which keeps the shared modules in the folders it takes them as, and
start.sh puts those folders on the server's path."""
archive = tmp_path / "ctx.tar.gz"
BuildContext.of(AGENT_DIR, AGENT_DIR / "Dockerfile").write(archive)
with tarfile.open(archive) as tar:
tar.extractall(tmp_path / "pi-a2a", filter="data")
start = (AGENT_DIR / "start.sh").read_text()
[path] = [line for line in start.splitlines() if line.startswith("export PYTHONPATH=")]
script = f'here={tmp_path / "pi-a2a"}; {path}; exec "$0" -c "import agent_egress, clockpage"'

subprocess.run(["sh", "-c", script, sys.executable], check=True, env={"PATH": "/usr/bin:/bin"})
Loading