105 lines
5.6 KiB
Python
105 lines
5.6 KiB
Python
"""One bounded 624-LOG read-only diagnostic. Never reads VTU/model labels."""
|
|
import argparse
|
|
from collections import Counter
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import resource
|
|
import sys
|
|
import time
|
|
from zipfile import ZipFile
|
|
from moldflow_time_map import parse, diagnose
|
|
from s2_recovery_supervisor import save, supervise
|
|
|
|
|
|
def sha(path):
|
|
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
|
|
|
|
|
def quantiles(values):
|
|
if not values:return None
|
|
v=sorted(values)
|
|
def q(p):
|
|
i=(len(v)-1)*p;j=int(i)
|
|
return v[j]+(v[min(j+1,len(v)-1)]-v[j])*(i-j)
|
|
return dict(n=len(v),min=v[0],p10=q(.1),median=q(.5),p90=q(.9),max=v[-1])
|
|
|
|
|
|
def checked(root):
|
|
c=json.loads((root/'tolerances.json').read_text())
|
|
f=json.loads((root/'code-freeze.json').read_text())
|
|
if sha(root/'tolerances.json')!=f['tolerances_sha256']:raise ValueError('Tolerance mutation')
|
|
for p,h in f['source_sha256'].items():
|
|
if sha(p)!=h:raise ValueError('Source mutation '+p)
|
|
a=c['archive_stat'];st=Path(c['archive']).stat()
|
|
if (st.st_size,st.st_mtime_ns,st.st_ino)!=(a['bytes'],a['mtime_ns'],a['inode']):raise ValueError('Archive changed')
|
|
return c,f
|
|
|
|
|
|
def worker(root):
|
|
c,f=checked(root)
|
|
if (root/'started.json').exists():raise FileExistsError('No repeat LOG pass')
|
|
os.sched_setaffinity(0,[24]);start=time.process_time()
|
|
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),code_freeze=f))
|
|
dest=root/'logs';dest.mkdir(exist_ok=False);records=[]
|
|
with ZipFile(c['archive']) as z:
|
|
members=sorted(n for n in z.namelist() if n.endswith('.log'))
|
|
if len(members)!=624:raise ValueError('LOG count changed')
|
|
if len({Path(n).name for n in members})!=624:raise ValueError('Basename collision')
|
|
for member in members:
|
|
info=z.getinfo(member)
|
|
if info.file_size>c['limits']['per_log_bytes']:raise ValueError('Per-LOG size cap')
|
|
raw=z.read(member);path=dest/Path(member).name;path.write_bytes(raw)
|
|
try:
|
|
p=parse(raw);d=diagnose(p,c)
|
|
except Exception as exc:
|
|
p=dict(parse_error=repr(exc));d=dict(status='PARSE_ERROR_RAW_RETAINED',quantitative_defined=False)
|
|
report=dict(member=member,raw_path=str(path),raw_sha256=sha(path),parsed=p,diagnostic=d)
|
|
out=path.with_suffix('.json');save(out,report)
|
|
records.append(dict(member=member,report=str(out),report_sha256=sha(out),diagnostic=d))
|
|
save(root/'progress.json',dict(logs=len(records),CPU_s=time.process_time()-start))
|
|
valid=[r['diagnostic'] for r in records if r['diagnostic']['quantitative_defined']]
|
|
save(root/'index.json',records)
|
|
flow={}
|
|
for window in ('whole_fill','fixed_5_to_95_percent','before_explicit_VP'):
|
|
a=[r['flow'][window] for r in valid];defined=[r for r in a if r['defined']]
|
|
flow[window]=dict(defined=len(defined),within_tolerance=sum(r['within_fixed_tolerance'] for r in defined),
|
|
undefined_reasons=dict(Counter(r.get('reason') for r in a if not r['defined'])),
|
|
departures=quantiles([r['max_relative_departure'] for r in defined]))
|
|
summary=dict(status='LOG_DIAGNOSTIC_COMPLETE',logs=len(records),parsed=len(valid),
|
|
parse_statuses=dict(Counter(r['diagnostic']['status'] for r in records)),
|
|
tolerance_commit='81dede0bd1775c02712a1e83467a0e4832e93424',tolerances_sha256=sha(root/'tolerances.json'),
|
|
index_sha256=sha(root/'index.json'),printed_100_rows=sum(r['reached_printed_100_percent'] for r in valid),
|
|
linearity_within_tolerance=sum(r['linearity']['within_fixed_tolerance'] for r in valid),
|
|
max_linearity_deviation_pp=quantiles([r['linearity']['max_deviation_percentage_points'] for r in valid]),
|
|
explicit_switch_parts=sum(bool(r['observed_switch_rows']) for r in valid),
|
|
switch_volume_percent=quantiles([r['observed_switch_rows'][0]['fill_percent'] for r in valid if r['observed_switch_rows']]),
|
|
flow=flow,worker_analysis_CPU_s=time.process_time()-start,
|
|
limitations=['Printed LOG rows are rounded, not full simulation fields.',
|
|
'Zero printed flow cannot establish zero actual flow or flatness.',
|
|
'Linearity uses observed window and fixed 1 percentage point tolerance; not exact constant flow.',
|
|
'V/P rows and filling end are distinct from retained-tet labelled endpoint.',
|
|
'No model inputs, labels, fits, frozen predictions or other task sources changed.'])
|
|
save(root/'summary.json',summary)
|
|
return 0
|
|
|
|
|
|
def guard(root):
|
|
c,f=checked(root);limits=c['limits'];sample=json.loads((root/'format-sample-receipt.json').read_text())
|
|
# Include format inspection once in the independent one CPU-hour LOG ledger.
|
|
remaining=limits['worker_CPU_s']-sample['CPU_s']
|
|
os.sched_setaffinity(0,[26])
|
|
receipt=supervise([sys.executable,__file__,str(root),'--worker'],root,'log-worker',dict(
|
|
total_cpu_seconds=remaining,job_cpu_seconds=remaining,job_wall_seconds=limits['wall_s'],
|
|
tree_rss_bytes=limits['RSS_bytes'],disk_bytes=limits['disk_bytes'],max_processes=1),poll=.2)
|
|
save(root/'guard-result.json',dict(worker=receipt,format_sample_CPU_s=sample['CPU_s'],
|
|
cumulative_worker_CPU_s=sample['CPU_s']+receipt['cpu_seconds'],CPU_cap=limits['worker_CPU_s'],
|
|
guard_CPU_s=sum(resource.getrusage(resource.RUSAGE_SELF)[:2])))
|
|
return 0 if receipt['status']=='PASS' else 1
|
|
|
|
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('--worker',action='store_true');a=p.parse_args()
|
|
raise SystemExit(worker(a.root) if a.worker else guard(a.root))
|