The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test: prove concurrent CLI launch wire and restart credential isolation
Teknium84e86a87ec8a
diff --git a/tests/gateway/fixtures/cli_launch_policy_probe.py b/tests/gateway/fixtures/cli_launch_policy_probe.pynew file mode 100644index 00000000000..017e9e5aa90--- /dev/null+++ b/tests/gateway/fixtures/cli_launch_policy_probe.py@@ -0,0 +1,145 @@+"""Ordinary daemon + concurrent classic CLI, loopback-only launch receipts."""+import asyncio+from concurrent.futures import ThreadPoolExecutor+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+import json+import os+from pathlib import Path+import re+import subprocess+import sys+import threading++from tests.gateway.fixtures.local_recovery_probe import daemon, websocket, rpc+++class Model(BaseHTTPRequestHandler):+ def log_message(self, *args):+ pass++ def do_GET(self):+ self.send_error(404)++ def do_POST(self):+ body = json.loads(self.rfile.read(int(self.headers['Content-Length'])))+ if not body.get('messages'):+ self.send_error(404)+ return+ with self.server.lock:+ first = not any(r['body']['model'] == body['model'] for r in self.server.requests)+ self.server.requests.append({'path': self.path, 'auth': self.headers.get('Authorization'), 'body': body})+ if first:+ self.server.barrier.wait(timeout=30)+ message = {'role': 'assistant', 'content': 'LAUNCH_POLICY_OK'}+ choice = {'index': 0, 'message': message, 'finish_reason': 'stop'}+ kind = 'application/json'+ payload = json.dumps({'id': 'local', 'choices': [choice], 'model': body['model'],+ 'usage': {'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15}}).encode()+ if body.get('stream'):+ choice['delta'] = choice.pop('message')+ payload = ('data: ' + json.dumps({'id': 'local', 'choices': [choice]}) + '\n\ndata: [DONE]\n\n').encode()+ kind = 'text/event-stream'+ self.send_response(200)+ self.send_header('Content-Type', kind)+ self.send_header('Content-Length', str(len(payload)))+ self.end_headers()+ self.wfile.write(payload)+++def probe(tmp_path):+ root = Path(__file__).resolve().parents[3]+ home, user = tmp_path / 'state', tmp_path / 'user'+ home.mkdir(mode=0o700)+ user.mkdir()+ peer = ThreadingHTTPServer(('127.0.0.1', 0), Model)+ peer.requests, peer.lock, peer.barrier = [], threading.Lock(), threading.Barrier(2)+ thread = threading.Thread(target=peer.serve_forever, daemon=True)+ thread.start()+ origin = f'http://127.0.0.1:{peer.server_port}'+ cfg = {'model': {'provider': 'custom', 'default': 'system-model', 'base_url': origin + '/system/v1'},+ 'auxiliary': {'title_generation': {'enabled': False}}, 'platform_toolsets': {'cli': []},+ 'memory': {'memory_enabled': False, 'user_profile_enabled': False}}+ config = home / 'config.yaml'+ config.write_text(json.dumps(cfg))+ before = config.read_bytes()+ (home / 'SOUL.md').write_text('SOUL_LAUNCH_MARKER')+ env = {k: os.environ[k] for k in ('PATH', 'LANG', 'TZ', 'SYSTEMROOT') if k in os.environ}+ env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root))+ sessions, pids = {}, []+ keys = {'left': 'private-left-launch-token', 'right': 'private-right-launch-token'}++ def cli(side, resume=None):+ cwd = tmp_path / side+ cwd.mkdir(exist_ok=True)+ (cwd / 'AGENTS.md').write_text('PROJECT_LAUNCH_MARKER_' + side)+ args = dict(query='POLICY_' + side, quiet=True)+ if resume:+ args['resume'] = resume+ else:+ args.update(provider='custom', model='gpt-5-' + side, api_key=keys[side],+ base_url=origin + '/' + side + '/v1', reasoning='high' if side == 'left' else 'low',+ max_turns=1 if side == 'left' else 3, ignore_rules=side == 'left', toolsets='')+ result = subprocess.run([sys.executable, '-c', 'import cli; cli.main(**' + repr(args) + ')'],+ cwd=cwd, env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=70)+ assert result.returncode == 0, result.stdout + result.stderr+ if not resume:+ sessions[side] = re.search(r'Session: (\S+)', result.stderr).group(1)+ return result.stdout++ async def after_restart(desc):+ async with websocket(home, desc) as ws:+ for side, sid in sessions.items():+ result = await rpc(ws, 'session.resume', session_id=sid)+ assert 'result' in result, result+ assert 'LAUNCH_POLICY_OK' in json.dumps(result['result']['messages'])+ replay_epoch = result['result']['replay_epoch']+ result = await rpc(ws, 'prompt.submit', session_id=sid, input_id='restart-' + side, text='NEVER_INFER')+ assert 'result' in result, result+ admission = result['result']['admission_id']+ async with asyncio.timeout(20):+ while True:+ result = await rpc(ws, 'prompt.receipt', session_id=sid, admission_id=admission)+ if result['result']['status'] == 'terminal':+ break+ await asyncio.sleep(.05)+ events = await rpc(ws, 'session.events.since', session_id=sid, last_sequence=0, replay_epoch=replay_epoch)+ assert 'launch_credentials_unavailable' in json.dumps(events), events++ try:+ with daemon(root, home, env, barrier=False) as (proc, desc):+ pids.append(proc.pid)+ with ThreadPoolExecutor(2) as pool:+ assert all('LAUNCH_POLICY_OK' in x for x in pool.map(cli, keys))+ assert config.read_bytes() == before+ cfg['agent'] = {'reasoning_effort': 'none', 'max_turns': 40}+ config.write_text(json.dumps(cfg))+ for side in keys:+ assert 'LAUNCH_POLICY_OK' in cli(side, sessions[side])+ for side in keys:+ captured = [x for x in peer.requests if x['body']['model'] == 'gpt-5-' + side]+ assert len(captured) == 2, captured+ assert all(x['path'] == '/' + side + '/v1/chat/completions' for x in captured)+ assert all(x['auth'] == 'Bearer ' + keys[side] for x in captured)+ bodies = [x['body'] for x in captured]+ assert all(b.get('reasoning_effort') == ('high' if side == 'left' else 'low') for b in bodies), bodies+ systems = [[m for m in b['messages'] if m['role'] in {'system', 'developer'}] for b in bodies]+ assert systems[0] == systems[1]+ text = json.dumps(systems[0])+ assert ('PROJECT_LAUNCH_MARKER_' + side in text) == (side == 'right'), text+ assert ('SOUL_LAUNCH_MARKER' in text) == (side == 'right'), text+ count = len(peer.requests)+ with daemon(root, home, env, barrier=False) as (proc, desc):+ pids.append(proc.pid)+ asyncio.run(after_restart(desc))+ assert len(peer.requests) == count+ leaks = []+ for path in home.rglob('*'):+ if path.is_file() and any(key.encode() in path.read_bytes() for key in keys.values()):+ leaks.append(str(path.relative_to(home)))+ assert not leaks, leaks+ return {'pids': pids, 'requests': count, 'concurrent_policies': True, 'auth_endpoint_model_reasoning': True,+ 'frozen_prefix': True, 'ignore_rules': True, 'restart_fail_closed': True, 'no_durable_keys': True}+ finally:+ peer.shutdown()+ peer.server_close()+ thread.join(timeout=5)diff --git a/tests/gateway/test_session_policy.py b/tests/gateway/test_session_policy.pyindex 98c423d57c1..c3c74148e3d 100644--- a/tests/gateway/test_session_policy.py+++ b/tests/gateway/test_session_policy.py@@ -100,3 +100,18 @@ def test_explicit_key_is_private_and_missing_after_restart_fails_closed(tmp_pathassert dict(os.environ) == before+def test_ordinary_daemon_cli_launch_policy(tmp_path):+ import subprocess+ import sys+ import json+ from pathlib import Path+ root = Path(__file__).resolve().parents[2]+ result = subprocess.run([sys.executable, '-c',+ 'import json,sys; from pathlib import Path; '+ 'from tests.gateway.fixtures.cli_launch_policy_probe import probe; '+ 'print(json.dumps(probe(Path(sys.argv[1]))))', str(tmp_path)], cwd=root,+ stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=180)+ assert result.returncode == 0, result.stdout + result.stderr+ print(json.dumps(json.loads(result.stdout.splitlines()[-1])))++