import argparse
import asyncio
import json
from pathlib import Path
from types import SimpleNamespace

from arbi_serve.admin_console.targets import _openai_client, _probe_request
from arbi_serve.admin_console.vision_concurrency import _image_url, image_request
from arbi_serve.admin_console.vision_quality import measure_vision_quality

parser = argparse.ArgumentParser()
parser.add_argument('--arm', required=True)
args = parser.parse_args()
base = 'http://127.0.0.1:19094'
model = 'vision-quant-trial'

async def main():
    # Recheck the four answers that differed from the older ce1 source.
    old_root = Path('/home/dev/arbi-serve-vision-reserve-overlap-20261105/docs/receipts/data/vision-memory-2026-11-04')
    for group, filename in [('initial', 'manifest.json'), ('detail', 'detail-balanced-manifest.json')]:
        older = [json.loads(s) for s in (old_root / f'candidate-ce1-qa-{group}.jsonl').read_text().splitlines()]
        current = [json.loads(s) for s in Path(f'/tmp/vision-envelope-fixed-20261105-{group}-qa.jsonl').read_text().splitlines()]
        by_key = {(r['image_sha256'], r['question']): r for r in older}
        manifest = json.loads(Path('/tmp/vision-quant-data-20261104/' + filename).read_text())
        selected = []
        for row in current:
            if by_key[(row['image_sha256'], row['question'])]['parsed'] != row['parsed']:
                entry = dict(manifest['evaluation'][row['image']])
                entry['questions'] = [q for q in entry['questions'] if q['question'] == row['question']]
                assert len(entry['questions']) == 1
                selected.append(entry)
        await measure_vision_quality(base, model, selected, arm=args.arm, max_tokens=2048,
            emit=lambda r: print(json.dumps({'test': 'delta_qa', 'group': group, **r}), flush=True))

    image_url, image_hash = _image_url(4096, 'stochastic-memory-qualification')
    async with _openai_client(base, None, timeout=240) as client:
        for width, mixed in [(3, False), (8, False), (3, True)]:
            events = [asyncio.Event() for _ in range(width)]
            async def text(index):
                async def create(**kwargs):
                    kwargs['seed'] = 42 + index
                    kwargs['extra_body']['ignore_eos'] = True
                    stream = await client.chat.completions.create(**kwargs)
                    async def tapped():
                        async for chunk in stream:
                            if chunk.choices and chunk.choices[0].delta.content:
                                events[index].set()
                            yield chunk
                    return tapped()
                proxy = SimpleNamespace(chat=SimpleNamespace(completions=SimpleNamespace(create=create)))
                try:
                    result = await _probe_request(proxy, base, model,
                        f'Run {index}. List the integers from 1 to 1000, one per line. Continue until the output limit.',
                        max_tokens=512, prompt_tokens=0, cache=False,
                        sampler={'temperature': 0.7, 'top_k': 20, 'top_p': 0.9, 'min_p': 0.05, 'enable_thinking': False})
                    assert not result['error'], result
                    assert result['tokens'] == 512, result
                    assert result['accepted'] + result['rejected'] > 0, result
                    return result
                finally:
                    events[index].set()
            tasks = [asyncio.create_task(text(i)) for i in range(width)]
            media = None
            if mixed:
                await asyncio.wait_for(asyncio.gather(*(e.wait() for e in events)), timeout=120)
                assert not all(t.done() for t in tasks), 'media did not overlap text'
                media = await image_request(client, SimpleNamespace(model=model, detail='high'), image_url)
            results = await asyncio.gather(*tasks)
            print(json.dumps({'test':'stochastic_slate', 'width':width, 'mixed':mixed,
                'image_sha256':image_hash if mixed else None, 'texts':results, 'image':media}), flush=True)

asyncio.run(main())
