diff --git a/src/agent_env/agents/agents/letta-code/root.conf b/src/agent_env/agents/agents/letta-code/root.conf index e01a6626c..793be04c5 100644 --- a/src/agent_env/agents/agents/letta-code/root.conf +++ b/src/agent_env/agents/agents/letta-code/root.conf @@ -47,13 +47,14 @@ environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress" ; 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. HOME is the image's, /data, under which the CLI's state lives. -; Not restarted: agent-exit ends the container when it exits. +; Never restarted: see [eventlistener:agent-exit]. [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=false +startretries=0 stopasgroup=true killasgroup=true stopwaitsecs=30 @@ -62,10 +63,12 @@ stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 environment=HOME="/data",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" -; The A2A server is the container, as it was before the clock: when it exits, or never gets going, this stops -; supervisord, which ends the container, rather than have a fresh server, without the dead one's MCP servers, peers and -; tasks, take its place while the dead one's CLI goes on. A stop of the container stops the server without an exit -; event, and the follower and the proxy restart in place. supervisord exits 0 either way. +; The server's exit, for any reason, stops supervisord and with it the container, as when the server was the +; container's main process: one restarted in place would have lost the env's MCP servers, its config, its peers and its +; running tasks, while the CLI the dead one started went on writing. The follower and the proxy restart in place. +; supervisord exits 0 however it stops, so the container's exit status doesn't tell a crash from a stop; its log does. [eventlistener:agent-exit] -command=/usr/local/bin/python3 -I -c "while 1: import os, signal, sys; print('READY', flush=True); header = dict(f.split(':', 1) for f in sys.stdin.readline().split()); event = dict(f.split(':', 1) for f in sys.stdin.read(int(header['len'])).split()); event['processname'] == 'agent' and os.kill(os.getppid(), signal.SIGTERM); print('RESULT 2', end='\nOK', flush=True)" +command=/bin/sh -c 'while echo READY && read -r header; do body=$(head -c "${header##*len:}"); case " $body " in *" processname:agent "*) echo "the A2A server exited ($body): stopping the container" >&2; kill -TERM "$PPID";; esac; printf "RESULT 2\nOK"; done' events=PROCESS_STATE_EXITED,PROCESS_STATE_FATAL +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 diff --git a/tst/unit/agents/letta_code/test_server_exit.py b/tst/unit/agents/letta_code/test_server_exit.py deleted file mode 100644 index d4aeb6ecd..000000000 --- a/tst/unit/agents/letta_code/test_server_exit.py +++ /dev/null @@ -1,81 +0,0 @@ -"""The A2A server is the container, as it was before the image ran supervisord: root.conf restarts the clock follower -and the egress proxy in place, never the server, and its event listener stops supervisord, so the container ends, once -the server exits or fails to start. A restarted server would have lost its MCP servers, peers and tasks while the dead -one's CLI went on in the workspace.""" - -from __future__ import annotations - -import configparser -import json -import shlex -import subprocess -import sys - -from tst.unit.agents.letta_code.agent import AGENT_DIR - -EVENTS = {"PROCESS_STATE_EXITED", "PROCESS_STATE_FATAL"} - - -def _conf() -> configparser.RawConfigParser: - """root.conf as supervisord reads it: a ';' or '#' after whitespace starts a comment.""" - conf = configparser.RawConfigParser(inline_comment_prefixes=(";", "#"), strict=False) - conf.read_string((AGENT_DIR / "root.conf").read_text()) - return conf - - -def _listener() -> configparser.SectionProxy: - listeners = [name for name in _conf().sections() if name.startswith("eventlistener:")] - assert len(listeners) == 1, listeners - return _conf()[listeners[0]] - - -def test_the_server_is_never_restarted_and_the_follower_and_the_proxy_are(): - conf = _conf() - - assert conf["program:agent"]["autorestart"] == "false" - assert conf["program:clock"]["autorestart"] == conf["program:egress"]["autorestart"] == "true" - - -def test_one_listener_hears_every_way_a_program_ends_on_its_own_as_root_which_may_stop_supervisord(): - """A stop, the container's or supervisorctl's, ends a program as STOPPED, which it doesn't hear.""" - listener = _listener() - - assert set(listener["events"].split(",")) == EVENTS - assert "user" not in listener - - -# Plays supervisord to the listener, as its parent, so the listener's signal comes here: each event as the eventlistener -# protocol sends it, then whether the listener sent SIGTERM before it answered. -SUPERVISORD = r""" -import json, signal, subprocess, sys -signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGTERM}) -listener = subprocess.Popen(sys.argv[1:], stdin=subprocess.PIPE, stdout=subprocess.PIPE) -stopped = [] -for name, event, state in json.loads(sys.stdin.read()): - assert listener.stdout.readline() == b"READY\n" - payload = f"processname:{name} groupname:{name} {state}".encode() - header = f"ver:3.0 server:supervisor serial:1 pool:agent-exit poolserial:1 eventname:{event} len:{len(payload)}\n" - listener.stdin.write(header.encode() + payload) - listener.stdin.flush() - assert listener.stdout.read(11) == b"RESULT 2\nOK" - stopped.append(signal.SIGTERM in signal.sigpending()) - if stopped[-1]: - signal.sigwait({signal.SIGTERM}) -listener.kill() -print(json.dumps(stopped)) -""" - - -def test_the_listener_stops_supervisord_when_the_server_exits_or_fails_to_start_and_for_nothing_else(): - exited, fatal = "from_state:RUNNING expected:0 pid:7", "from_state:BACKOFF" - events = [("clock", "PROCESS_STATE_EXITED", exited), ("egress", "PROCESS_STATE_FATAL", fatal), - ("agent", "PROCESS_STATE_EXITED", exited), ("clock", "PROCESS_STATE_FATAL", fatal), - ("agent", "PROCESS_STATE_FATAL", fatal)] - # supervisord expands '%(name)s' in a command, so a bare '%' would break it. - command = shlex.split(_listener()["command"] % {}) - assert command[0] == "/usr/local/bin/python3" - - result = subprocess.run([sys.executable, "-c", SUPERVISORD, sys.executable, *command[1:]], - input=json.dumps(events), capture_output=True, text=True, timeout=60, check=True) - - assert json.loads(result.stdout) == [False, False, True, False, True] diff --git a/tst/unit/agents/test_agent_clock_images.py b/tst/unit/agents/test_agent_clock_images.py index e0eaa9eae..14dda4af8 100644 --- a/tst/unit/agents/test_agent_clock_images.py +++ b/tst/unit/agents/test_agent_clock_images.py @@ -119,7 +119,8 @@ def test_the_a2a_servers_exit_stops_supervisord_and_the_followers_or_the_proxys_ [section] = [name for name in _conf(agent).sections() if name.startswith("eventlistener:")] listener = _conf(agent)[section] assert set(listener["events"].split(",")) == {"PROCESS_STATE_EXITED", "PROCESS_STATE_FATAL"} - assert "%" not in listener["command"] + # As root, which may stop supervisord. + assert "user" not in listener and "%" not in listener["command"] events = [("PROCESS_STATE_EXITED", "processname:clock groupname:clock from_state:RUNNING expected:0 pid:7"), ("PROCESS_STATE_FATAL", "processname:egress groupname:egress from_state:BACKOFF"), ("PROCESS_STATE_EXITED", "processname:agent-exit groupname:agent-exit from_state:RUNNING expected:1 pid:8"),