#!/usr/bin/env python3
# 自己调度：按 import 图并行调 lean -o，替代 lake（lake 在 17.5 万模块上自身占满 CPU）
import os,re,sys,subprocess,time,threading,heapq,collections
ROOT=os.path.expanduser('~/lean'); SRC=ROOT+'/src'; OUT=ROOT+'/.lake/build/lib/lean'
LEAN=os.path.expanduser('~/.elan/toolchains/leanprover--lean4---v4.33.1/bin/lean')
N=int(sys.argv[1]) if len(sys.argv)>1 else 176
TEST=sys.argv[2] if len(sys.argv)>2 else None
pk=ROOT+'/.lake/packages'
LP=':'.join([f'{pk}/{p}/.lake/build/lib/lean' for p in sorted(os.listdir(pk))]+[OUT])
BASE=['-DmaxHeartbeats=4000000','-DautoImplicit=false','-DrelaxedAutoImplicit=false','-DmaxSynthPendingDepth=3','-Dpp.unicode.fun=true']
FIELDS=('GroundTheory.LocalFields.','GroundTheory.GlobalFields.')
imp=re.compile(r'^\s*(?:public\s+)?(?:meta\s+)?import\s+(?:all\s+)?(\S+)',re.M)
def modname(p): return os.path.relpath(p,SRC)[:-5].replace('/','.')
deps={}
for dp,_,fs in os.walk(SRC):
    for f in fs:
        if f.endswith('.lean'):
            p=os.path.join(dp,f); t=open(p,encoding='utf-8').read()
            # header = up to first non-import/non-comment line; cheap: only first 400 lines
            hdr='\n'.join(t.split('\n')[:400])
            deps[modname(p)]=[d.replace('«','').replace('»','') for d in imp.findall(hdr)]
mods=set(deps)
for m in deps: deps[m]=[d for d in deps[m] if d in mods]
def path(m,ext): return OUT+'/'+m.replace('.','/')+ext
def cmd(m):
    o=list(BASE)
    if m.startswith(FIELDS): o.append('-DsynthInstance.maxHeartbeats=400000')
    return [LEAN,'--root='+SRC,'--tstack=400000','-j','2']+o+[SRC+'/'+m.replace('.','/')+'.lean','-o',path(m,'.olean'),'-i',path(m,'.ilean')]
env=dict(os.environ,LEAN_PATH=LP)
if TEST:
    os.makedirs(os.path.dirname(path(TEST,'.olean')),exist_ok=True)
    c=cmd(TEST); c[c.index('-o')+1]='/tmp/t.olean'; c[c.index('-i')+1]='/tmp/t.ilean'
    t=time.time(); r=subprocess.run(c,env=env,capture_output=True,text=True); print(r.returncode,time.time()-t,r.stdout[-2000:],r.stderr[-2000:]); sys.exit()
done={m for m in mods if os.path.exists(path(m,'.olean')) and os.path.exists(path(m,'.ilean'))}
rdeps=collections.defaultdict(list); need={}
for m in mods:
    if m in done: continue
    need[m]=sum(1 for d in deps[m] if d not in done)
    for d in deps[m]: rdeps[d].append(m)
ready=[m for m,k in need.items() if k==0]
failed=set(); lock=threading.Condition(); running=0; t0=time.time(); nok=0
log=open(ROOT+'/drive.log','a'); errlog=open(ROOT+'/drive_err.log','a')
print(f'total {len(mods)} done {len(done)} todo {len(need)} ready {len(ready)} jobs {N}',file=log,flush=True)
def worker():
    global running,nok
    while True:
        with lock:
            while not ready and running>0: lock.wait()
            if not ready: lock.notify_all(); return
            m=ready.pop(); running+=1
        os.makedirs(os.path.dirname(path(m,'.olean')),exist_ok=True)
        s=time.time(); r=subprocess.run(cmd(m),env=env,capture_output=True,text=True); dt=time.time()-s
        with lock:
            running-=1
            if r.returncode==0:
                nok+=1
                if r.stdout.count('error'):pass
                for x in rdeps[m]:
                    need[x]-=1
                    if need[x]==0: ready.append(x)
            else:
                failed.add(m); errlog.write(f'=== {m} rc={r.returncode} {dt:.0f}s\n{r.stdout[-4000:]}\n{r.stderr[-2000:]}\n'); errlog.flush()
            if nok%500==0 or r.returncode: print(f'{time.time()-t0:.0f}s ok {nok} fail {len(failed)} ready {len(ready)} running {running} last {m} {dt:.0f}s',file=log,flush=True)
            lock.notify_all()
th=[threading.Thread(target=worker) for _ in range(N)]
[t.start() for t in th]; [t.join() for t in th]
blocked=[m for m,k in need.items() if k>0]
print(f'END {time.time()-t0:.0f}s ok {nok} fail {len(failed)} blocked {len(blocked)}',file=log,flush=True)
