Files
alzeo/tests/test_bench_agent_exits.py
T
Hasan BasbunarandClaude Opus 5.5 2c7d4f61da tests/localports.py: what the second review found, no wrappers
_end() walked the tree under a process it might already have reaped, and
a pid used again by then would have had another program's children
signalled, on a box that runs other work: a server is the process start()
returns now, a wrapper execs it, and only that process is ended. With no
tree walk, a wrapper's child is no longer left running, and a poll reads
the process's own sockets first, the machine's tables only when it has
one. A server that listens on two ports is refused, since which one is
meant cannot be guessed, and one that ends before it listens says what
it wrote on its stderr when start() kept it in a pipe. serve() looked at
every listener of the machine, IPv6-only ones included, which cannot
take an IPv4 port: it reads the IPv4 ones on 127.0.0.1 or any. held()
returns the port alone, the test's cleanup keeping the socket. The
witness that a closed port was anyone's could itself be beaten to it:
the guard's failure on the old tree is the witness.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 11:51:55 +01:00

79 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""bench-agent.py ends a loop the lane stopped answering with exit 3 and a sentence.
A turn read past its timeout, a connection the engine dropped, or a lane that went away
between two turns ended in a Python traceback and exit 1 (found in review, 2026-09-24): only
an HTTP refusal was caught, and the post() written to catch the rest was never called. A
partial loop is not a measurement, which is what exit 3 says elsewhere in this tool.
"""
import http.server
import os
import subprocess
import sys
import threading
import time
import unittest
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import localports # noqa: E402
REPO = Path(__file__).resolve().parents[1]
TOOL = REPO / "bench-agent.py"
MODE = ["hang"]
class Fake(http.server.BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def log_message(self, *a):
pass
def do_POST(self):
self.rfile.read(int(self.headers.get("Content-Length") or 0))
if MODE[0] == "hang":
time.sleep(3) # past --timeout 1
return
self.close_connection = True # "reset": gone without an answer
class TheExits(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Fake)
cls.server.daemon_threads = True
threading.Thread(target=cls.server.serve_forever, daemon=True).start()
@classmethod
def tearDownClass(cls):
cls.server.shutdown()
cls.server.server_close()
def run_tool(self, port):
return subprocess.run([sys.executable, str(TOOL), "--port", str(port), "--model", "fake", "--turns", "2",
"--turn-tokens", "4", "--prefix-tokens", "50", "--timeout", "1"],
capture_output=True, text=True, timeout=60,
env=dict(os.environ, QWEN38_API_KEY="not-the-real-key"))
def test_a_turn_past_its_timeout(self):
MODE[0] = "hang"
r = self.run_tool(self.server.server_address[1])
self.assertEqual(r.returncode, 3, r.stderr)
self.assertNotIn("Traceback", r.stderr)
def test_a_connection_the_engine_dropped(self):
MODE[0] = "reset"
r = self.run_tool(self.server.server_address[1])
self.assertEqual(r.returncode, 3, r.stderr)
self.assertNotIn("Traceback", r.stderr)
def test_a_lane_that_is_gone(self):
port = localports.held(self) # nothing listens there, and no other program can
r = self.run_tool(port)
self.assertEqual(r.returncode, 3, r.stderr)
self.assertNotIn("Traceback", r.stderr)
if __name__ == "__main__":
unittest.main()