from pathlib import Path
import argparse, json, os, subprocess, time, urllib.request

parser = argparse.ArgumentParser()
parser.add_argument('--source', required=True)
parser.add_argument('--sha', required=True)
parser.add_argument('--prefix', required=True)
parser.add_argument('--measure', action='store_true')
parser.add_argument('--quality', action='store_true')
parser.add_argument('--image')
parser.add_argument('--installed', action='store_true')
parser.add_argument('--budget-cache')
parser.add_argument('--extra-qualification')
options = parser.parse_args()
root = Path('/home/dev/arbi-serve-vision-reserve-overlap-20261105')
py = '/home/dev/arbi-serve-3814-gemv-route/.venv/bin/python'
image = options.image or 'registry.arbi.work/arbi-serve@sha256:0982b8c718f195486dc34bb9225b23d4afe3f7a33c106db4edc16f4be9a7c94c'
env = dict(os.environ, VISION_TRIAL_SOURCE=options.source, VISION_TRIAL_MODE='exl3', VISION_PROD_CHECKPOINT='/models/Qwen3.8-27B-exl3-4.0bpw-vision6', VISION_PROD_CALIBRATION='/models/Qwen3.8-27B-exl3-4.0bpw-vision6/kv_calibration.json', PYTHONPATH=str(root))
base = ['docker', 'compose', '-f', 'tools/profiling/vision_production_trial.compose.yml']
config = json.loads(subprocess.check_output([*base, 'config', '--format', 'json'], cwd=root, env=env))
args = config['services']['vision-quant-trial']['command']
args = args[args.index('--model'):]
override = Path(options.prefix + '-compose.json')
service = {'image': image, 'working_dir': '/tmp' if options.installed else '/w', 'entrypoint': ['/usr/local/bin/arbi-entrypoint.sh'], 'command': args, 'environment': {'PYTHONPATH': None if options.installed else '/w', 'ARBI_MM_ENCODER_CACHE_MB': None, 'ARBI_VISION_QUERY_CHUNK_TOKENS': None}}
if options.budget_cache:
    service['volumes'] = [str(Path(options.budget_cache).resolve()) + ':/cache/arbi-serve/budget-cache']
override.write_text(json.dumps({'services': {'vision-quant-trial': service}}))
compose = [*base, '-f', str(override)]

def read(suffix):
    with urllib.request.urlopen('http://127.0.0.1:19094/v1/admin/' + suffix, timeout=90) as response:
        return json.load(response)

with open(options.prefix + '-server.log', 'w') as log:
    process = subprocess.Popen([*compose, 'up', '--abort-on-container-exit'], cwd=root, env=env, stdout=log, stderr=subprocess.STDOUT)
    try:
        print('BOOT', options.sha, flush=True)
        for _ in range(1200):
            if process.poll() is not None:
                raise RuntimeError(f'server exited {process.returncode}')
            try:
                with urllib.request.urlopen('http://127.0.0.1:19094/health/ready', timeout=2) as response:
                    if response.status == 200:
                        break
            except Exception:
                pass
            time.sleep(2)
        else:
            raise RuntimeError('readiness deadline')
        result = {'source_sha': options.sha, 'source_mount': options.source, 'runtime_image': image, 'server_info': read('server_info'), 'memory_boot': read('memory/live')}
        assert result['memory_boot']['operator']['closes']
        Path(options.prefix + '-results.json').write_text(json.dumps(result, indent=2) + '\n')
        print('READY', 'KV', result['memory_boot']['kv_mapped'], 'context', result['server_info']['max_context'], flush=True)
        if options.quality:
            for group, filename in [('initial', 'manifest.json'), ('detail', 'detail-balanced-manifest.json')]:
                path = Path(options.prefix + '-' + group + '-qa.jsonl')
                with path.open('w') as output:
                    subprocess.run([py, 'tools/profiling/vision_qa_trial.py', '--manifest', '/tmp/vision-quant-data-20261104/' + filename, '--arm', options.sha, '--max-tokens', '2048'], cwd=root, env=env, stdout=output, check=True)
                rows = [json.loads(line) for line in path.read_text().splitlines()]
                assert all(row['finish_reason'] != 'length' for row in rows)
                print('QUALITY', group, sum(row['correct'] for row in rows), len(rows), flush=True)
        if options.measure:
            for name, argv in [('single', ['--tokens', '256', '--repeats', '3', '--fresh-media']), ('late-prefill', ['--tokens', '1024', '--repeats', '2', '--fresh-media', '--media-lead-fraction', '.6']), ('parallel-eight', ['--tokens', '512', '--text-streams', '8', '--image-side', '4096', '--repeats', '3'])]:
                with open(options.prefix + '-' + name + '.jsonl', 'w') as output:
                    subprocess.run([py, 'tools/profiling/vision_text_latency.py', '--model', 'vision-quant-trial', '--media-namespace', 'reserve-accounting-' + name, *argv], cwd=root, env=env, stdout=output, check=True)
                print('PASSED', name, flush=True)
            with open(options.prefix + '-video-cancel.jsonl', 'w') as output:
                subprocess.run([py, '/tmp/vision_stream_smoke_20261104.py'], cwd=root, env=env, stdout=output, check=True)
            result['memory_after'] = read('memory/live')
            assert result['memory_after']['operator']['closes']
            Path(options.prefix + '-results.json').write_text(json.dumps(result, indent=2) + '\n')
        if options.extra_qualification:
            with open(options.prefix + '-extra-qualification.jsonl', 'w') as output:
                subprocess.run([py, options.extra_qualification, '--arm', options.sha], cwd=root, env=env, stdout=output, check=True)
            result['memory_after'] = read('memory/live')
            assert result['memory_after']['operator']['closes']
            Path(options.prefix + '-results.json').write_text(json.dumps(result, indent=2) + '\n')
            print('PASSED extra-qualification', flush=True)
        print('COMPLETED', flush=True)
    finally:
        subprocess.run(['docker', 'stop', '--timeout', '90', 'arbi-vision-quant-trial'], stdout=subprocess.DEVNULL, check=False)
        process.wait(timeout=120)
        subprocess.run(['docker', 'rm', 'arbi-vision-quant-trial'], stdout=subprocess.DEVNULL, check=False)
