#!/usr/bin/env python3 """CPU-only engine loader/--help preflight, before any model acquisition.""" import json import os from pathlib import Path import socket import subprocess import sys import tempfile import time def main(): binary = str(Path(sys.argv[1]).resolve()) with tempfile.TemporaryDirectory(prefix='strata-engine-probe-') as tmp: root = Path(tmp).resolve() engine, marker, pid_file = [root / n for n in ['engine', 'help-called', 'pid']] config_path = root / 'config.json' with socket.socket() as sock: sock.bind(('127.0.0.1', 0)) port = sock.getsockname()[1] config = {'bind': f'127.0.0.1:{port}', 'engine': {'executable': str(engine), 'args': ['must-not-reach-help']}} assets = [{'manifest': str(root / 'absent.json'), 'directory': str(root / 'output')}] count = 0 def fixture(body): marker.unlink(missing_ok=True) engine.write_text(f'''#!/bin/sh [ "$#" = 1 ] && [ "$1" = --help ] || exit 92 [ "${{CUDA_VISIBLE_DEVICES+x}}" = x ] && [ -z "$CUDA_VISIBLE_DEVICES" ] || exit 93 printf called > "{marker}" printf '%s' "$$" > "{pid_file}" {body} ''') engine.chmod(0o700) def run(command='provision', ok=False, contains=None): nonlocal count config_path.write_text(json.dumps(config)) start = time.monotonic() result = subprocess.run([binary, command, '--config', str(config_path)], env={'PATH':os.defpath, 'CUDA_VISIBLE_DEVICES':'0'}, capture_output=True, text=True, timeout=15) assert (result.returncode == 0) == ok, (result.stdout, result.stderr) if contains: assert contains in result.stderr, result.stderr assert 'private-child-output' not in result.stderr + result.stdout assert not (root / 'output').exists() assert not (root / '.output.strata-stage').exists() with socket.socket() as sock: assert sock.connect_ex(('127.0.0.1',port)) != 0 count += 1 return result, time.monotonic()-start fixture('exit 127') run(ok=True) # default does not execute the probe assert not marker.exists() config['engine_probe'] = True result, _ = run('check-config', ok=True) report = json.loads(result.stdout) assert report['engine_probe_configured'] and not report['engine_probe_checked'] assert not marker.exists() worker = config.pop('engine') run(contains='requires an engine executable') config['engine'] = worker config['engine_probe'] = 'yes' run() config['engine_probe'] = True config['assets'] = assets engine.unlink() for command in ['provision', 'serve']: run(command, contains='could not start') # Permission failure and invalid executable/loader are early failures. fixture('exit 0') engine.chmod(0o600) run(contains='could not start') engine.write_bytes(b'not an executable format\0') engine.chmod(0o700) result, _ = run() assert 'engine' in result.stderr and ('could not start' in result.stderr or 'probe failed' in result.stderr) engine.write_text('#!/nonexistent-strata-test-loader\n') run(contains='could not start') fixture("printf private-child-output; printf private-child-output >&2; exit 127") for command in ['provision', 'serve']: run(command, contains='engine --help probe failed') assert marker.exists() # Successful helper exits before acquisition; no serving arguments pass. fixture('exit 0') for command in ['provision', 'serve']: result, _ = run(command) assert 'engine' not in result.stderr.lower(), result.stderr assert marker.exists() config.pop('assets') run(ok=True) assert marker.exists() # Direct-child timeout cleanup, with no grandchildren or persistent process. config['assets'] = assets fixture('exec /bin/sleep 60') _, elapsed = run(contains='timed out after 10 seconds') assert 9.5 <= elapsed < 14.5, elapsed try: os.kill(int(pid_file.read_text()), 0) except ProcessLookupError: pass else: raise AssertionError('timed-out child alive or unreaped') print(f'{count} engine probe scenarios passed; no model or GPU inference') if __name__ == '__main__': main()