"""Three near-256K contexts held active; not a quality/JSON benchmark."""
import concurrent.futures, importlib.util, json, os, re, threading, time
from pathlib import Path
os.environ['CUDA_VISIBLE_DEVICES']=''
os.environ['TOKENIZERS_PARALLELISM']='false'
from transformers import AutoTokenizer
root=Path('/home/lab/bench/ornith/modular/sglang')
out=root/'results'/'residency-int8-e5m2.json'
if out.exists():raise RuntimeError('Refusing to overwrite residency evidence')
spec=importlib.util.spec_from_file_location('bench','/home/lab/bench/ornith/modular/shared/bench.py')
bench=importlib.util.module_from_spec(spec);spec.loader.exec_module(bench)
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-sglang','messages':[{'role':'user','content':p[0]}],
           'temperature':0,'max_tokens':2048,'min_tokens':2048,'ignore_eos':True,
           'chat_template_kwargs':{'enable_thinking':False}} for p in prompts]
log=Path(os.environ.get('ORNITH_RESIDENCY_LOG',str(root/'logs'/'full-int8-kve5m2-786k.log')))
offset=log.stat().st_size
start=time.monotonic()
loads=[]; stop=threading.Event()
def sample_loads():
    while not stop.is_set():
        try:
            sample=bench.http('http://127.0.0.1:8130/v1/loads?include=core,memory,queues',timeout=5)
            sample['sample_perf_counter']=time.perf_counter()
            loads.append(sample)
        except Exception as e:
            loads.append({'error':str(e),'time':time.time()})
        stop.wait(1)
monitor=threading.Thread(target=sample_loads,daemon=True);monitor.start()
print('RESIDENCY BEGIN: 3 concurrent requests, total budget262144 each, forced2048 output tokens; not quality.',flush=True)
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as pool:
    results=list(pool.map(lambda p:bench.stream('http://127.0.0.1:8130/v1',p,2400),payloads))
stop.set();monitor.join(timeout=6)
with log.open('rb') as f:f.seek(offset);lines=f.read().decode(errors='replace').splitlines()
samples=[]
for line in lines:
    m=re.search(r'Decode batch, #running-req: (\d+), #full token: (\d+)',line)
    if m:samples.append({'running':int(m[1]),'full_tokens':int(m[2]),'line':line})
three=[s for s in samples if s['running']==3]
peak=max([s['full_tokens'] for s in three],default=0)
load_peak=max([row.get('num_used_tokens',0) for sample in loads for row in sample.get('loads',[]) if row.get('num_running_reqs')==3],default=0)
firsts=[r.get('first_token_monotonic') for r in results]
finishes=[r.get('finished_monotonic') for r in results]
stream_overlap=all(x is not None for x in firsts+finishes) and max(firsts)<min(finishes)
overlap_load_peak=max([row.get('num_used_tokens',0) for sample in loads for row in sample.get('loads',[])
                      if stream_overlap and max(firsts)<=sample['sample_perf_counter']<=min(finishes)
                      and row.get('num_running_reqs')==3],default=0)
record={'wall_seconds':time.monotonic()-start,'estimated_prompt_tokens':[p[2] for p in prompts],
        'prefill_backend':os.environ.get('ORNITH_PREFILL_BACKEND','triton'),'server_log':str(log),
        'max_tokens':2048,'min_tokens':2048,'ignore_eos':True,'results':results,
        'decode_samples':samples,'max_full_tokens_with_three_running':peak,
        'loads':loads,'max_active_tokens_with_three_running':load_peak,
        'all_three_streams_overlap':stream_overlap,'active_tokens_during_three_stream_overlap':overlap_load_peak,
        'three_resident_proven':stream_overlap and overlap_load_peak>=3*(262144-2048-128),
        'all_requests_completed':all(r.get('ok') and r.get('completion_tokens')==2048 for r in results),
        'note':'Capacity/residency only; forced ignore_eos output does not prove answer quality.'}
out.write_text(json.dumps(record,indent=2))
print(json.dumps({k:v for k,v in record.items() if k not in ('results','decode_samples','loads')},indent=2),flush=True)
raise SystemExit(0 if record['three_resident_proven'] and record['all_requests_completed'] else 1)
