"""Read-only llama capacity test; no configuration/process/cache changes."""
import concurrent.futures
import json
import os
import threading
import time
from pathlib import Path

os.environ['CUDA_VISIBLE_DEVICES'] = ''
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
import bench
from transformers import AutoTokenizer


def main():
    root = Path('/home/lab/bench/ornith/modular/shared')
    out = root / 'llama-residency-256k.json'
    if out.exists():
        raise RuntimeError('Refusing to overwrite evidence')
    tok = AutoTokenizer.from_pretrained('/home/lab/models/Ornith-1.5-9B-original', local_files_only=True)
    prompts = [bench.make_prompt(tok, 262144, 2048, 193517 + i, 'throughput') for i in range(3)]
    payloads = [{'model': 'ornith-local', 'messages': [{'role': 'user', 'content': p[0]}],
                 'temperature': 0, 'max_tokens': 2048, 'ignore_eos': True,
                 'chat_template_kwargs': {'enable_thinking': False}} for p in prompts]
    samples = []
    stop = threading.Event()

    def monitor():
        keys = ('id', 'id_task', 'is_processing', 'n_ctx', 'n_prompt_tokens',
                'n_prompt_tokens_cache', 'n_prompt_tokens_processed')
        while not stop.is_set():
            try:
                slots = bench.http('http://127.0.0.1:8120/slots', timeout=5)
                samples.append({'monotonic': time.perf_counter(),
                                'slots': [{k: slot.get(k) for k in keys} for slot in slots]})
            except Exception as exc:
                samples.append({'monotonic': time.perf_counter(), 'error': str(exc)})
            stop.wait(1)

    thread = threading.Thread(target=monitor, daemon=True)
    thread.start()
    started = time.perf_counter()
    print('Llama: capacity-only test, three distinct total-context262144 requests; output2048 forced.', flush=True)
    try:
        with concurrent.futures.ThreadPoolExecutor(max_workers=3) as pool:
            results = list(pool.map(lambda p: bench.stream('http://127.0.0.1:8120/v1', p, 1800), payloads))
    finally:
        stop.set()
        thread.join(timeout=6)
    completed = all(r.get('ok') and r.get('completion_tokens') == 2048 for r in results)
    firsts = [r.get('first_token_monotonic') for r in results]
    ends = [r.get('finished_monotonic') for r in results]
    overlap = all(x is not None for x in firsts + ends) and max(firsts) < min(ends)
    actual = [r.get('usage', {}).get('prompt_tokens') for r in results]
    proof = []
    if overlap and all(isinstance(n, int) for n in actual):
        for sample in samples:
            if not max(firsts) <= sample['monotonic'] <= min(ends):
                continue
            ready = [s for s in sample.get('slots', []) if s.get('is_processing')
                     and (s.get('n_prompt_tokens_processed') or 0) + (s.get('n_prompt_tokens_cache') or 0) >= min(actual)]
            if len(ready) == 3:
                proof.append(sample)
    report = {'seconds': time.perf_counter() - started, 'total_context_budget': 262144,
              'output_reservation': 2048, 'estimated_prompt_tokens': [p[2] for p in prompts],
              'actual_prompt_tokens': actual, 'all_completed': completed,
              'three_stream_overlap': bool(overlap), 'three_resident_proven': completed and bool(proof),
              'proof_samples': proof, 'samples': samples, 'results': results,
              'note': 'Capacity only. Forced ignore_eos output is not quality evidence. No server configuration or cache flush changed.'}
    out.write_text(json.dumps(report, indent=2) + '\n')
    print(json.dumps({k: v for k, v in report.items() if k not in ('samples', 'proof_samples', 'results')}, indent=2), flush=True)
    raise SystemExit(0 if report['three_resident_proven'] else 1)


if __name__ == '__main__':
    main()
