"""Finish the approved benchmark, waiting for existing workers without polling.

Training, selection, refit, fixed holdout evaluation, packaging and integrity
checks form one resumable job. No active model entry or annotation DB is written.
"""
import argparse
from concurrent.futures import ThreadPoolExecutor
import ctypes
from datetime import datetime, timezone
import fcntl
import hashlib
import json
import os
from pathlib import Path
import select
import subprocess
import sys
import traceback

PROJECT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(PROJECT))
from pallet.pose_benchmark import ROOT, atomic_json,ACCELERATION,freeze_refinement_protocol
from run_pose_benchmark import GPUS


def state(stage, **fields):
    record=dict(stage=stage,updated_at=datetime.now(timezone.utc).isoformat(),**fields)
    atomic_json(ROOT/'pipeline_state.json',record)
    print(json.dumps(record),flush=True)


def open_pidfd(pid):
    if hasattr(os,'pidfd_open'):
        return os.pidfd_open(pid)
    # Python built against older glibc headers may omit its wrapper even when
    # the running Linux >=5.3 kernel implements this syscall.
    if os.uname().machine not in ('x86_64','aarch64'):
        raise RuntimeError('No known pidfd syscall number for this architecture')
    libc=ctypes.CDLL(None,use_errno=True)
    libc.syscall.restype=ctypes.c_long
    handle=libc.syscall(ctypes.c_long(434),ctypes.c_int(pid),ctypes.c_uint(0))
    if handle<0:
        error=ctypes.get_errno()
        raise OSError(error,os.strerror(error))
    return handle


def wait_existing(pids):
    handles=[]
    for pid in pids:
        try:
            handles.append(open_pidfd(pid))
        except ProcessLookupError:
            pass
    state('waiting_for_existing_workers',pids=pids)
    try:
        while handles:
            ready,_,_=select.select(handles,[],[])
            for handle in ready:
                os.close(handle);handles.remove(handle)
    finally:
        for handle in handles:os.close(handle)


def run(label, args, gpu=None, cwd=PROJECT):
    env=dict(os.environ,OPENBLAS_NUM_THREADS='1',OMP_NUM_THREADS='2',
             PYTHONPATH='/tmp/opencode/pose_benchmark_small_deps:/tmp/opencode/pose_benchmark_deps')
    if gpu:env['CUDA_VISIBLE_DEVICES']=gpu
    log=ROOT/'logs'/('pipeline_'+label+'.log');log.parent.mkdir(exist_ok=True)
    print(json.dumps(dict(step=label,args=args,log=str(log))),flush=True)
    with log.open('w') as stream:
        process=subprocess.run([sys.executable,'-u',*args],cwd=cwd,env=env,
                               stdout=stream,stderr=subprocess.STDOUT)
    if process.returncode:
        raise RuntimeError(f'{label} failed ({process.returncode}); see {log}')


def freeze_sources():
    files=[*PROJECT.glob('pallet/pose_benchmark*.py'),
           *PROJECT.glob('scripts/*pose_benchmark*.py'),
           PROJECT/'configs/pallet_pose_benchmark_v3.json',
           PROJECT/'configs/pallet_pose_benchmark_v3_acceleration.json',
           PROJECT/'docs/pallet_pose_benchmark_v3.md',PROJECT/'tests/test_pose_benchmark.py']
    root=ROOT/'final_source'
    manifest={}
    for path in files:
        name=str(path.relative_to(PROJECT));dest=root/name;dest.parent.mkdir(parents=True,exist_ok=True)
        raw=path.read_bytes();manifest[name]=hashlib.sha256(raw).hexdigest()
        if dest.exists() and dest.read_bytes()!=raw:
            raise ValueError('Final source already frozen with different contents: '+name)
        if not dest.exists():dest.write_bytes(raw)
    atomic_json(ROOT/'final_source_manifest.json',manifest)


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--wait-pids',type=int,nargs='*',default=[])
    args=parser.parse_args()
    ROOT.mkdir(exist_ok=True)
    with (ROOT/'pipeline.lock').open('a') as lock:
        fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
        try:
            wait_existing(args.wait_pids)
            if ACCELERATION.exists():freeze_refinement_protocol()
            freeze_sources()
            state('shortlist')
            run('shortlist',['-m','pallet.pose_benchmark_results','--stage','shortlist'])
            state('three_seed_two_route_refinement')
            run('refinement',['scripts/run_pose_benchmark.py','--stage','refine'])
            state('freeze_selection_train_only')
            run('selection',['-m','pallet.pose_benchmark_results','--stage','select'])
            state('refit_and_fixed_holdout_evaluation')
            with ThreadPoolExecutor(max_workers=2) as pool:
                futures=[pool.submit(run,'evaluation_'+branch,
                    ['-m','pallet.pose_benchmark_results','--stage','evaluate','--branch',branch],GPUS[i])
                    for i,branch in enumerate(('metric','native_unknown'))]
                for future in futures:future.result()
            state('report_and_package')
            run('report',['-m','pallet.pose_benchmark_results','--stage','report'])
            run('delivery',['scripts/deliver_pose_benchmark.py'])
            state('final_verification')
            run('inference',['scripts/verify_pose_benchmark_inference.py'],GPUS[0])
            run('integrity',['scripts/verify_pose_benchmark.py'])
            run('regressions',['-m','pytest','tests/test_pose_benchmark.py','tests/test_pose_refinement.py',
                               'tests/test_pose_review.py','tests/test_pose_model.py','-q'])
            run('portable',['scripts/verify_pose_benchmark_portable.py'],GPUS[0])
            run('result_audit',['scripts/audit_pose_benchmark_results.py'])
            decision=json.loads((ROOT/'deployment_decision.json').read_text())
            delivery=json.loads((ROOT/'delivery_manifest.json').read_text())
            state('complete',deployment_decision=decision,archive=delivery,
                  live_annotations_modified=False,active_entry_modified=False)
        except Exception:
            state('failed',error=traceback.format_exc())
            raise


if __name__=='__main__':main()
