"""Three long contexts held active. This proves residency, not output quality."""
import argparse,concurrent.futures,importlib.util,json,os,pathlib,re,threading,time,urllib.request
os.environ['CUDA_VISIBLE_DEVICES']='';os.environ['TOKENIZERS_PARALLELISM']='false'
from transformers import AutoTokenizer
p=argparse.ArgumentParser();p.add_argument('--id',required=True);p.add_argument('--context',type=int,default=131072);p.add_argument('--out',default='residency.json');a=p.parse_args()
r=pathlib.Path('/home/lab/bench/ornith/modular/vllm/runs')/a.id;out=r/a.out
assert not out.exists(),'Refusing to overwrite 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,a.context,2048,193517+i,'throughput') for i in range(3)]
payloads=[{'model':'ornith-vllm','messages':[{'role':'user','content':x[0]}],'temperature':0,'max_tokens':2048,'min_tokens':2048,'ignore_eos':True,'chat_template_kwargs':{'enable_thinking':False}} for x in prompts]
capacity=int(re.findall(r'GPU KV cache size: ([\d,]+) tokens',(r/'server.log').read_text(errors='replace'))[-1].replace(',',''))
samples=[];stop=threading.Event()
def monitor():
 while not stop.is_set():
  try:
   with urllib.request.urlopen('http://127.0.0.1:8140/metrics',timeout=5) as response:lines=response.read().decode().splitlines()
   values={}
   for key in ('num_requests_running','num_requests_waiting','kv_cache_usage_perc','num_preemptions_total'):
    selected=[line for line in lines if not line.startswith('#') and line.startswith('vllm:'+key)]
    if selected:values[key]=sum(float(line.rsplit(' ',1)[1]) for line in selected)
   samples.append({'time':time.perf_counter(),**values})
  except Exception as exc:samples.append({'error':str(exc),'time':time.perf_counter()})
  stop.wait(1)
thread=threading.Thread(target=monitor,daemon=True);thread.start();start=time.monotonic()
print('RESIDENCY:3 different long prompts, forced2048 output each; NOT a quality benchmark.',flush=True)
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as pool:results=list(pool.map(lambda body:bench.stream('http://127.0.0.1:8140/v1',body,2400),payloads))
stop.set();thread.join(timeout=6)
firsts=[x.get('first_token_monotonic') for x in results];finishes=[x.get('finished_monotonic') for x in results]
overlap=all(x is not None for x in firsts+finishes) and max(firsts)<min(finishes)
three=[s for s in samples if overlap and max(firsts)<=s['time']<=min(finishes) and s.get('num_requests_running')==3]
cache_peak=max((s.get('kv_cache_usage_perc',0) for s in three),default=0)
estimated=capacity*cache_peak
preemptions=max((s.get('num_preemptions_total',0) for s in samples),default=0)
record={'context_budget':a.context,'capacity_tokens':capacity,'actual_prompt_tokens':[x.get('usage',{}).get('prompt_tokens') for x in results],'forced_completion_tokens':2048,'all_three_streams_overlap':overlap,'overlap_seconds':min(finishes)-max(firsts) if overlap else 0,'peak_cache_fraction_during_overlap':cache_peak,'estimated_cached_tokens_during_overlap':estimated,'cache_estimate_caveat':'Runtime capacity times block-usage fraction; block padding makes this an occupancy estimate, not exact token count.','preemptions_counter':preemptions,'three_resident_proven':bool(overlap and three and estimated>=3*(a.context-2048-128) and preemptions==0),'all_requests_completed':all(x.get('ok') and x.get('completion_tokens')==2048 for x in results),'wall_seconds':time.monotonic()-start,'results':results,'samples':samples,'note':'Forced ignore_eos output proves capacity/residency only, not output 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','samples')},indent=2),flush=True)
raise SystemExit(not(record['three_resident_proven'] and record['all_requests_completed']))
