Implement approved R4a paired propagation with guarded budgets and synthetic acceptance

This commit is contained in:
Frank14f committed 2026-11-16 19:21:51 +08:00
1 parent f9633bd219
commit 7c85d1a29a
12 files changed
+59816

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
"""Freeze the approved R4(a) metadata contract; never read scientific arrays."""
import argparse
import json
from pathlib import Path
import resource
import time
from gre24_prepare_v2 import sha
from s2_recovery_supervisor import save
def prepare(root):
root.mkdir(parents=True,exist_ok=False)
inventory_path=Path('artifacts/r4a-planning-2026-11-16/input-inventory.json')
inv=json.loads(inventory_path.read_text())
for entry in inv['sources'].values():
if sha(entry['path'])!=entry['sha256']:raise ValueError('Reviewed metadata changed: '+entry['path'])
v5=json.loads(Path(inv['sources']['v5_contract']['path']).read_text())
roster=json.loads(Path(inv['sources']['s2_roster']['path']).read_text())
freeze=json.loads(Path(inv['sources']['v5_preparation_freeze']['path']).read_text())
sources={entry['path']:entry['sha256'] for entry in inv['sources'].values()}
sources[str(inventory_path)]=sha(inventory_path)
sources['docs/r4a-shared-speed-contract-2026-11-16.md']=sha('docs/r4a-shared-speed-contract-2026-11-16.md')
source_code=dict(v5['code_sha256'])
for name,digest in source_code.items():
if sha(name)!=digest:raise ValueError('Frozen v5 source differs: '+name)
names=['r4a_core.py','r4a_part.py','r4a_score.py','run_r4a.py','prepare_r4a.py','test_r4a.py',
's2_custody_common.py','s2_custody_statistics.py','s2_models/features.py','s2_models/objectives.py',
'freeze_g1_components.py','s2_recovery_supervisor.py','s2_recovery_monitoring.py','boundary_distance.py','audit_samples.py']
source_code.update({str(Path('tools')/name):sha(Path('tools')/name) for name in names})
tests=[]
for path in sorted(Path('artifacts/r4a-acceptance-2026-11-16').glob('attempt-*/receipt.json')):
receipt=json.loads(path.read_text());tests.append(dict(path=str(path),sha256=sha(path),**receipt))
sources[str(path)]=sha(path)
if not tests or tests[-1]['status']!='PASS':raise ValueError('Synthetic acceptance missing or failed')
for name,digest in tests[-1]['source_sha256'].items():
if sha(name)!=digest:raise ValueError('Source changed since synthetic acceptance: '+name)
rows=[]
for row in sorted((r for r in roster['parts'] if r['included']),key=lambda r:r['case']):
row=dict(row);row['v5_cache']=freeze['successful'].get(row['case']);rows.append(row)
if len(rows)!=617 or len({r['component_id'] for r in rows})!=209:raise ValueError('Roster changed')
missing=[r['case'] for r in rows if r['v5_cache'] is None]
if missing!=inv['distance_only_missing_parts']:raise ValueError('Cache intersection changed')
setup_cpu=sum(resource.getrusage(resource.RUSAGE_SELF)[:2])
if setup_cpu>60:raise ValueError('Bookkeeping setup reserve insufficient')
contract=dict(version='R4a-shared-cell-speed-v1',authorizing_rulings=['1794819308.674829','1794826344.160199'],
reviewed_git_commit='f9633bd2195cc4a75e888b43a882ba15001fb3d7',created_unix=time.time(),
archive=v5['archive'],archive_stat=v5['archive_stat'],parts=rows,distance_only_missing_parts=missing,
code_sha256=source_code,source_sha256=sources,
label_index=inv['sources']['s2_cached_label_index']['path'],historical_scores=inv['sources']['historical_s2_part_scores']['path'],
worker_cpus=[24,26,28,29],coordinator_cpu=25,compute_worker_cpu_cap=28800,postprocess_worker_cpu_cap=7200,
total_worker_cpu_cap=36000,per_part_cpu_seconds=1800,per_part_wall_seconds=1800,per_part_rss_bytes=4*2**30,
disk_bytes=20*2**30,initial_worker_cpu_seconds=60+sum(t['cpu_seconds'] for t in tests),
initial_charge=dict(synthetic_worker_cpu_seconds=sum(t['cpu_seconds'] for t in tests),
metadata_setup_cpu_observed=setup_cpu,bookkeeping_reserve_seconds=60,
note='60s conservative setup charge within compute cap; actual coordinator/guard CPU separately reported'),
acceptance=tests,zero_retries=True,no_new_labels=True,no_gpu=True,
any_failure_policy='Stop dispatch, terminate active science under live supervisors, retain receipts; no automatic scoring',
bootstrap=dict(seed=2026111408,replicates=2000,interval='percentile95 linear',descriptive=True,no_gate=True),
cache_policy='Validate hash, mesh/IDs/gate/clock and fixed speed construction before reuse; never silent recompute')
save(root/'contract.json',contract)
save(root/'prepared.json',dict(status='READY_NOT_STARTED',contract_sha256=sha(root/'contract.json'),
candidates=611,distance_only_missing=6,total_parts=617,synthetic_checks=tests[-1]['tests'],
scientific_arrays_read=False,scientific_execution=False))
print(json.dumps(dict(root=str(root),contract_sha256=sha(root/'contract.json'),initial_worker_cpu_seconds=contract['initial_worker_cpu_seconds'])))
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();prepare(a.root)
+67
View File
@@ -0,0 +1,67 @@
"""R4(a) shared-cell-speed graph and descriptive grouped statistics."""
from itertools import combinations
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra
from s2_custody_statistics import contrast
def graph_field(g, speed, sources):
speed = np.asarray(speed, dtype=np.float64)
if speed.shape != (len(g['t']),) or not np.isfinite(speed).all() or np.any(speed <= 0):
raise ValueError('Invalid shared cell speed')
edges = np.sort(g['t'][:, list(combinations(range(4), 2))].reshape(-1, 2), axis=1)
incident_speed = np.repeat(speed, 6)
unique, inverse = np.unique(edges, axis=0, return_inverse=True)
fastest = np.zeros(len(unique), dtype=np.float64)
np.maximum.at(fastest, inverse, incident_speed)
weights = np.linalg.norm(g['x'][unique[:, 0]]-g['x'][unique[:, 1]], axis=1)/fastest
if not np.isfinite(weights).all() or np.any(weights <= 0):
raise ValueError('Nonpositive edge length/cost; no repair')
graph = csr_matrix((np.r_[weights, weights],
(np.r_[unique[:, 0], unique[:, 1]], np.r_[unique[:, 1], unique[:, 0]])),
shape=(len(g['x']), len(g['x'])))
field = dijkstra(graph, directed=False, indices=np.asarray(sources), min_only=True)
if not np.isfinite(field).all() or np.any(field < 0) or np.any(field[sources] != 0):
raise ValueError('Invalid/unreachable graph field')
return field, dict(edges=len(unique), minimum_cost=float(weights.min()), maximum_cost=float(weights.max()))
def paired_summary(rows, roster, metric):
by = {r['case']: r for r in rows}
groups = {}
for r in roster:
groups.setdefault(r['component_id'], []).append(r['case'])
values = {'F': {}, 'G': {}}
excluded = {}
for component, cases in sorted(groups.items()):
reasons = []
for case in cases:
for arm in values:
value = by.get(case, {}).get('metrics', {}).get(arm, {}).get(metric, {}).get('value')
if value is None or not np.isfinite(value):
reasons.append(dict(case=case, arm=arm, reason='missing or undefined metric'))
if reasons:
excluded[component] = reasons
continue
for arm in values:
values[arm][component] = float(np.mean([by[case]['metrics'][arm][metric]['value'] for case in cases]))
result = dict(component_values=values, excluded_components=excluded,
paired_components=len(values['F']), requested_components=len(groups),
paired_parts=sum(len(groups[k]) for k in values['F']))
if metric == 'relative_l2':
delta = contrast(values['G'], values['F'], replicates=2000, seed=2026111408)
delta.pop('region', None)
delta['interpretation'] = 'descriptive only; no CI gate or effect-size margin'
result['delta'] = delta
else:
result['means'] = {a: float(np.mean(list(v.values()))) if v else None for a, v in values.items()}
return result
def may_dispatch(spent, active, cap=28800., per_part=1800.):
return spent+(active+1)*(per_part+2.) <= cap
def scoring_allowed(records, requested, stop_reason):
return stop_reason is None and len(records) == requested and all(r['worker']['status'] == 'PASS' for r in records)
+120
View File
@@ -0,0 +1,120 @@
"""One R4(a) label-free paired prediction; no feature/moment/model execution."""
import argparse
import json
import os
from pathlib import Path
import time
import traceback
import hashlib
import numpy as np
from gre24_prepare_v2 import read_geometry, sha
from gre24_features_v2 import mesh, exterior_faces, gate_nodes, cell_flow_speed, solve_eikonal, strict_volume_cdf
from s2_custody_common import prediction_inputs, checked
from s2_recovery_supervisor import save
from r4a_core import graph_field
def contract(root):
c = json.loads((Path(root)/'contract.json').read_text())
for p, h in {**c['code_sha256'], **c['source_sha256']}.items():
checked(p, h)
a = c['archive_stat']; s = Path(c['archive']).stat()
if (s.st_size, s.st_mtime_ns, s.st_ino) != (a['bytes'], a['mtime_ns'], a['inode']):
raise ValueError('Frozen v5 archive identity changed')
return c
def fingerprint(a):
a = np.ascontiguousarray(a)
return dict(shape=list(a.shape), dtype=a.dtype.str, sha256=hashlib.sha256(a.tobytes()).hexdigest())
def exact(a, b, label):
if not np.array_equal(a, b):
raise ValueError('Identity mismatch: '+label)
def worker(root, index):
c = contract(root); row = c['parts'][index]; case = row['case']
out = root/'parts'/case/'output'; out.mkdir(exist_ok=False)
cpu = time.process_time(); start = time.monotonic()
def progress(stage, **kw):
save(out/'progress.json', dict(stage=stage, case=case, pid=os.getpid(), unix=time.time(),
cpu_seconds=time.process_time()-cpu, wall_seconds=time.monotonic()-start, **kw))
try:
progress('GEOMETRY_AND_CACHE_IDENTITY')
x, t, raw, meta = read_geometry(c['archive'], case)
g = mesh(x, t); faces = exterior_faces(t)
sources, gate_mode = gate_nodes(x, raw, meta['inlet_config']['inlet_location'])
prep = row['preparation']; source = Path(prep['preparation_root'])
checked(source/'geometry.npz', prep['output_sha256']['geometry.npz'])
with np.load(source/'geometry.npz', allow_pickle=False) as z:
exact(z['raw_node_ids'], raw, 'S2 raw IDs')
exact(z['points_raw'], x, 'S2 coordinates/units')
exact(z['tetra'], t, 'S2 tet connectivity')
exact(np.sort(z['boundary_triangles'], axis=1)[np.lexsort(np.sort(z['boundary_triangles'], axis=1).T[::-1])],
faces[np.lexsort(faces.T[::-1])], 'wall triangles')
exact(z['gate_location_raw'], meta['inlet_config']['inlet_location'], 'gate location')
query = prediction_inputs(row)
qids = np.searchsorted(raw, query['raw_ids'])
if np.any(qids >= len(raw)): raise ValueError('Query outside retained tet domain')
exact(raw[qids], query['raw_ids'], 'query original IDs')
old = row.get('v5_cache')
if old:
cache = Path(old['output'])
old_result = json.loads(checked(cache/'result.json', old['result_sha256']).read_text())
if old_result['status'] != 'PASS' or old_result['case'] != case: raise ValueError('Cache result identity')
if old_result['geometry'] != meta: raise ValueError('Cache geometry metadata')
loaded = {}
for name in ['Dw', 'Df', 'raw_ids', 'mass', 'all_wall_tet_ids']:
loaded[name] = np.load(checked(cache/(name+'.npy'), old['output_sha256'][name+'.npy']), allow_pickle=False)
exact(loaded['raw_ids'], raw, 'cached raw IDs')
exact(loaded['mass'], g['mass'], 'cached volume measure')
dw, df = loaded['Dw'], loaded['Df']
if float(old_result['t_scale_seconds']) != float(query['t_set']): raise ValueError('Frozen time setting mismatch')
else:
if case not in c['distance_only_missing_parts']: raise ValueError('Unapproved cache fallback')
progress('DISTANCE_ONLY_DW'); dw = solve_eikonal(g, np.unique(faces), 1.)
speed, zero = cell_flow_speed(g, dw, faces)
if old:
f = old_result['features']
exact(loaded['all_wall_tet_ids'], zero, 'all-wall correction list')
exact(f['gate_raw_ids'], raw[sources], 'cached gate source')
if f['wall_mode'] != 'tet_exterior_proxy' or f['precision'] != 'float64' or f['max_iterations'] != 100000:
raise ValueError('Cached scientific settings differ')
if f['speed_min'] != float(speed.min()) or f['speed_max'] != float(speed.max()):
raise ValueError('Cached speed provenance mismatch')
scale = 1./float(speed.min()) if float(speed.min())**2 <= 1e-4 else 1.
if f['Df_coordinate_and_speed_scale'] != scale: raise ValueError('Cached scale mismatch')
else:
progress('DISTANCE_ONLY_DF'); df = solve_eikonal(g, sources, speed)
if df.shape != (len(raw),) or not np.isfinite(df).all() or np.any(df < 0) or np.any(df[sources] != 0):
raise ValueError('Invalid F field')
progress('DIJKSTRA_SHARED_SPEED'); dg, graph_meta = graph_field(g, speed, sources)
progress('FULL_TET_CDF_AND_QUERY_PREDICTIONS')
cf = strict_volume_cdf(df, g['mass']); cg = strict_volume_cdf(dg, g['mass'])
for name, array in dict(Dw=dw, Df=df, Dg=dg, a_K=speed, all_wall_tet_ids=zero,
mass=g['mass'], raw_ids=raw, cdf_F=cf, cdf_G=cg).items():
np.save(out/(name+'.npy'), array)
np.savez(out/'query-predictions.npz', raw_ids=query['raw_ids'], counts=query['counts'],
F=cf[qids]*query['t_set'], G=cg[qids]*query['t_set'])
proof = dict(mesh_x=fingerprint(x), mesh_t=fingerprint(t), wall_faces=fingerprint(faces),
gate=fingerprint(raw[sources]), dw=fingerprint(dw), a_K=fingerprint(speed),
mass=fingerprint(g['mass']), query_raw_ids=fingerprint(query['raw_ids']),
query_counts=fingerprint(query['counts']), t_set=float(query['t_set']))
save(out/'result.json', dict(status='PASS', case=case, component_id=row['component_id'],
nodes=len(raw), query_nodes=len(qids), volume=float(g['volume'].sum()), gate_mode=gate_mode,
reused_frozen_df=bool(old), cache_identity_verified=bool(old), fingerprints=proof, graph=graph_meta,
output_sha256={p.name:sha(p) for p in out.iterdir() if p.suffix in ('.npy', '.npz')},
cpu_seconds=time.process_time()-cpu, wall_seconds=time.monotonic()-start,
decoded_labels=[], models_fitted=0, spatial_moment_computed=False))
progress('PASS')
except Exception as e:
save(out/'failure.json', dict(status='FAILED', case=case, type=type(e).__name__, message=str(e),
traceback=traceback.format_exc(), automatic_retry=False))
raise
if __name__ == '__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int)
a=p.parse_args();worker(a.root,a.index)
+76
View File
@@ -0,0 +1,76 @@
"""Score already frozen R4 predictions against the original S2 custody cache."""
import argparse
import json
from pathlib import Path
import time
import numpy as np
from gre24_prepare_v2 import sha
from r4a_part import contract, exact
from r4a_core import paired_summary, scoring_allowed
from s2_custody_common import checked, prediction_inputs
from s2_custody_statistics import part_metrics
from s2_recovery_supervisor import save
def score(root):
c=contract(root); start=time.process_time();out=root/'postfit';out.mkdir(exist_ok=True)
terminal=json.loads((root/'preparation-complete.json').read_text())
if not scoring_allowed(terminal['records'],len(c['parts']),terminal['stop_reason']):
raise ValueError('Any failure, missing attempt or stop forbids automatic scoring')
freeze=json.loads((root/'predictions-freeze.json').read_text())
if freeze['contract_sha256']!=sha(root/'contract.json') or freeze['preparation_complete_sha256']!=sha(root/'preparation-complete.json'):
raise ValueError('Prediction freeze identity mismatch')
if set(freeze['parts'])!={r['case'] for r in c['parts']}: raise ValueError('Incomplete prediction roster')
# Verify every paired prediction before opening any target cache.
for entry in freeze['parts'].values():
checked(entry['result'],entry['result_sha256'])
checked(entry['predictions'],entry['predictions_sha256'])
labels={x['case']:x for x in json.loads(Path(c['label_index']).read_text())}
rows=[]
for row in c['parts']:
case=row['case']; q=prediction_inputs(row); item=labels[case]
fold=next(a['fold'] for a in row['assignments'] if a['role']=='test')
if item['fold']!=fold:raise ValueError('Custody test-fold mismatch')
with np.load(checked(item['path'],item['sha256']),allow_pickle=False) as z:
exact(z['raw_ids'],q['raw_ids'],'cached truth/query IDs')
exact(z['counts'],q['counts'],'cached truth multiplicity')
truth=z['seconds'].copy()
if not np.isfinite(truth).all() or np.any(truth<0):raise ValueError('Invalid cached truth')
entry=freeze['parts'][case]
with np.load(checked(entry['predictions'],entry['predictions_sha256']),allow_pickle=False) as z:
exact(z['raw_ids'],q['raw_ids'],'prediction IDs');exact(z['counts'],q['counts'],'prediction counts')
metrics={arm:part_metrics(z[arm],truth,q['counts'],q['near_wall'],q['raw_ids'],
q['coordinates_over_Rg'],int(case.split('_')[0])) for arm in ['F','G']}
rows.append(dict(case=case,fold=fold,component_id=row['component_id'],metrics=metrics,label_sha256=item['sha256']))
save(out/'score-progress.json',dict(parts=len(rows),cpu_seconds=time.process_time()-start))
summary={metric:paired_summary(rows,c['parts'],metric) for metric in ['relative_l2','kendall_tau_b','last_location','near_wall']}
historical=json.loads(Path(c['historical_scores']).read_text())
b0={r['case']:r for r in historical if r['arm']=='B0'}
groups={}
for row in c['parts']:groups.setdefault(row['component_id'],[]).append(row['case'])
historical_summary={}
for metric in summary:
values={};missing={}
for component,cases in sorted(groups.items()):
a=[b0.get(case,{}).get(metric,{}).get('value') for case in cases]
if any(v is None or not np.isfinite(v) for v in a):missing[component]='missing or undefined historical B0 metric'
else:values[component]=float(np.mean(a))
paired=summary['relative_l2']['component_values']['F']
shared={k:v for k,v in values.items() if k in paired}
historical_summary[metric]=dict(original_complete_components=len(values),original_mean=float(np.mean(list(values.values()))) if values else None,
on_main_paired_components=len(shared),on_main_paired_mean=float(np.mean(list(shared.values()))) if shared else None,
excluded=missing,interpretation='historical whole distance construction, not the matched graph arm')
save(out/'part-scores.json',dict(rows=rows,predictions_freeze_sha256=sha(root/'predictions-freeze.json')))
results=[json.loads(Path(x['result']).read_text()) for x in freeze['parts'].values()]
save(out/'score-summary.json',dict(status='SCORED_DESCRIPTIVE',summary=summary,historical_B0=historical_summary,
requested_parts=617,requested_components=209,paired_prediction_parts=len(rows),
full_tet_nodes=sum(x['nodes'] for x in results),full_tet_volume=sum(x['volume'] for x in results),
requested_node_coverage=1.,requested_volume_coverage=1.,query_nodes=sum(x['query_nodes'] for x in results),
new_decodes=0,new_fits=0,predictions_freeze_sha256=sha(root/'predictions-freeze.json'),
part_scores_sha256=sha(out/'part-scores.json'),worker_cpu_seconds=time.process_time()-start,
limitations=['Fixed v5/S2 query domain; not fresh blinded test','Shared cell speed and fixed clock only',
'Component-only descriptive percentile intervals; no gate','No full-node paper score or R4b/c']))
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();score(a.root)
+141
View File
@@ -0,0 +1,141 @@
"""One detached R4(a) queue: fail-stop, no retry, no scoring after a failure."""
import argparse
import fcntl
import json
import os
from pathlib import Path
import resource
import signal
import subprocess
import sys
import time
import traceback
import psutil
from gre24_prepare_v2 import sha
from r4a_part import contract
from r4a_core import may_dispatch, scoring_allowed
from s2_recovery_supervisor import save, supervise, stop_group
from s2_recovery_monitoring import disk_usage
def guard(root,index,slot):
c=contract(root);os.sched_setaffinity(0,[c['worker_cpus'][slot]])
dest=root/'parts'/c['parts'][index]['case']
receipt=supervise([sys.executable,'tools/r4a_part.py',str(root),str(index)],dest,'worker',
dict(total_cpu_seconds=1801,job_cpu_seconds=1800,job_wall_seconds=1800,
tree_rss_bytes=4*2**30,disk_bytes=c['disk_bytes'],max_processes=1),poll=.2)
result=dest/'output/result.json'
if receipt['status']=='PASS' and (not result.exists() or json.loads(result.read_text())['status']!='PASS'):
receipt.update(status='STOPPED_OR_FAILED',stop_reason='MISSING_PASS_RESULT')
save(dest/'guard-result.json',dict(index=index,case=c['parts'][index]['case'],worker=receipt,
result_path=str(result) if result.exists() else None,result_sha256=sha(result) if result.exists() else None,
guard_cpu_seconds=sum(resource.getrusage(resource.RUSAGE_SELF)[:2])))
return 0 if receipt['status']=='PASS' else 1
def kill_guard_worker(pid):
# Keep the supervisor alive to reap its worker and write the true failure receipt.
try:
for child in psutil.Process(pid).children():stop_group(child.pid)
except psutil.NoSuchProcess:pass
def fresh(root):
if (root/'started.json').exists() or (root/'complete.json').exists():
raise FileExistsError('Existing attempt: never restart R4 queue')
def run(root):
with (root/'queue.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
return run_locked(root)
def run_locked(root):
c=contract(root);fresh(root);os.sched_setaffinity(0,[25])
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json')))
active={};done=[];next_i=0;reason=None;start=time.monotonic();error=None
def interrupted(*_):
nonlocal reason
reason=reason or 'COORDINATOR_INTERRUPTED'
signal.signal(signal.SIGINT,interrupted);signal.signal(signal.SIGTERM,interrupted)
env=dict(os.environ,OMP_NUM_THREADS='1',OPENBLAS_NUM_THREADS='1',MKL_NUM_THREADS='1',NUMEXPR_NUM_THREADS='1',CUDA_VISIBLE_DEVICES='')
try:
while active or (next_i<len(c['parts']) and reason is None):
for slot,item in list(active.items()):
if item['process'].poll() is None:continue
receipt=root/'parts'/c['parts'][item['index']]['case']/'guard-result.json'
if not receipt.exists():raise RuntimeError('MISSING_GUARD_RECEIPT')
r=json.loads(receipt.read_text());done.append(r);item['log'].close();del active[slot]
if r['worker']['status']!='PASS':reason=reason or 'PART_FAILED_NO_AUTOMATIC_SCORING'
spent=c['initial_worker_cpu_seconds']+sum(x['worker']['cpu_seconds'] for x in done)
used=disk_usage(root)
if used>=c['disk_bytes']:reason=reason or 'GLOBAL_DISK_LIMIT'
if spent>=28800:reason=reason or 'COMPUTE_CPU_LIMIT'
while reason is None and len(active)<4 and next_i<len(c['parts']):
if not may_dispatch(spent,len(active)):
if not active:reason='INSUFFICIENT_COMPUTE_RESERVATION'
break
slot=next(s for s in range(4) if s not in active);index=next_i;next_i+=1
dest=root/'parts'/c['parts'][index]['case'];dest.mkdir(parents=True,exist_ok=False)
log=(dest/'guard.log').open('x')
proc=subprocess.Popen([sys.executable,__file__,str(root),'--guard',str(index),'--slot',str(slot)],
stdout=log,stderr=subprocess.STDOUT,env=env,start_new_session=True)
active[slot]=dict(index=index,process=proc,log=log)
if reason:
for item in active.values():kill_guard_worker(item['process'].pid)
save(root/'progress.json',dict(completed=len(done),attempted=next_i,total=len(c['parts']),
passed=sum(x['worker']['status']=='PASS' for x in done),active={str(s):v['index'] for s,v in active.items()},
settled_worker_cpu_seconds=spent,reserved_active_worker_cpu_seconds=len(active)*1802,
stop_reason=reason,disk_bytes=used,wall_seconds=time.monotonic()-start))
if active:time.sleep(.5)
except Exception as exc:
reason=reason or 'INFRASTRUCTURE_EXCEPTION';error=dict(message=str(exc),traceback=traceback.format_exc())
finally:
for item in active.values():kill_guard_worker(item['process'].pid)
for item in active.values():
item['process'].wait();item['log'].close()
p=root/'parts'/c['parts'][item['index']]['case']/'guard-result.json'
if p.exists():done.append(json.loads(p.read_text()))
active={}
done.sort(key=lambda r:r['index']);spent=c['initial_worker_cpu_seconds']+sum(x['worker']['cpu_seconds'] for x in done)
allowed=scoring_allowed(done,len(c['parts']),reason)
terminal=dict(status='ALL_PASS' if allowed else 'STOPPED',stop_reason=reason,records=done,
never_attempted=list(range(next_i,len(c['parts']))),attempted=next_i,
missing_receipt_indices=sorted(set(range(next_i))-{r['index'] for r in done}),
charged_worker_cpu_seconds=spent,initial_worker_cpu_seconds=c['initial_worker_cpu_seconds'],
error=error,accounting_complete=len(done)==next_i)
save(root/'preparation-complete.json',terminal)
post=None
if allowed:
parts={}
for r in done:
result=Path(r['result_path']);checked_result=json.loads(result.read_text())
if sha(result)!=r['result_sha256']:raise ValueError('Result changed before freeze')
p=result.parent/'query-predictions.npz';digest=checked_result['output_sha256'][p.name]
if sha(p)!=digest:raise ValueError('Prediction changed before freeze')
parts[r['case']]=dict(result=str(result),result_sha256=sha(result),predictions=str(p),predictions_sha256=digest)
save(root/'predictions-freeze.json',dict(contract_sha256=sha(root/'contract.json'),
preparation_complete_sha256=sha(root/'preparation-complete.json'),parts=parts,labels_read=False))
remaining_disk=c['disk_bytes']-disk_usage(root)
if remaining_disk<=0:reason='POSTPROCESS_DISK_EXHAUSTED'
else:
# Inherit a non-v4 CPU; one guarded process for all cache-only scoring.
os.sched_setaffinity(0,[24])
post=supervise([sys.executable,'tools/r4a_score.py',str(root)],root/'postfit','worker',
dict(total_cpu_seconds=7200,job_cpu_seconds=7200,job_wall_seconds=7200,
tree_rss_bytes=4*2**30,disk_bytes=remaining_disk,max_processes=1),poll=.2)
os.sched_setaffinity(0,[25]);spent+=post['cpu_seconds']
if post['status']!='PASS':reason='POSTPROCESS_FAILED'
save(root/'complete.json',dict(status='COMPLETE' if allowed and post and post['status']=='PASS' else 'STOPPED',
stop_reason=reason,charged_worker_cpu_seconds=spent,worker_cpu_cap=36000,
preparation_complete_sha256=sha(root/'preparation-complete.json'),postfit=post,
coordinator_cpu_seconds=sum(resource.getrusage(resource.RUSAGE_SELF)[:2]),
guard_cpu_seconds=sum(x.get('guard_cpu_seconds',0.) for x in done),
wall_seconds=time.monotonic()-start,retries=0,accounting_complete=len(done)==next_i))
return 0 if allowed and post and post['status']=='PASS' else 1
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('--guard',type=int);p.add_argument('--slot',type=int,default=0)
a=p.parse_args();raise SystemExit(run(a.root) if a.guard is None else guard(a.root,a.guard,a.slot))
+127
View File
@@ -0,0 +1,127 @@
"""Bounded synthetic acceptance only; no real archive or labels."""
import json
import resource
import sys
import tempfile
from pathlib import Path
import unittest
from unittest.mock import patch
import numpy as np
from gre24_features_v2 import mesh, exterior_faces, cell_flow_speed, solve_eikonal, strict_volume_cdf
from gre24_prepare_v2 import sha
from r4a_core import graph_field, paired_summary, may_dispatch, scoring_allowed
import r4a_part as part
import r4a_score as scorer
from run_r4a import fresh
from s2_recovery_supervisor import save, supervise
class Checks(unittest.TestCase):
def test_single_constant_tet(self):
x=np.array([[0.,0,0],[1,0,0],[0,1,0],[0,0,1]])
g=mesh(x,[[0,1,2,3]]);field,_=graph_field(g,[2.],[0])
np.testing.assert_allclose(field,[0,.5,.5,.5],rtol=1e-12,atol=1e-12)
def test_tet_chain_agreement(self):
g=mesh([[0.,0,0],[1,0,0],[2,0,0],[1,1,0],[1,0,1]],[[0,1,3,4],[1,2,3,4]])
eik=solve_eikonal(g,[0],1.);graph,_=graph_field(g,np.ones(2),[0])
np.testing.assert_allclose(eik,graph,rtol=1e-10,atol=1e-10)
np.testing.assert_allclose(eik[:3],[0,1,2],rtol=1e-10,atol=1e-10)
def test_planar_front_known_sqrt_two_factor(self):
# Equilateral source face x=0, circumradius1; apex normal distance1.
x=np.array([[0.,1,0],[0,-.5,np.sqrt(3)/2],[0,-.5,-np.sqrt(3)/2],[1,0,0]])
g=mesh(x,[[0,1,2,3]])
eik=solve_eikonal(g,[0,1,2],1.);graph,_=graph_field(g,[1.],[0,1,2])
self.assertAlmostEqual(eik[3],1.,places=10)
self.assertAlmostEqual(graph[3],np.sqrt(2),places=10)
self.assertAlmostEqual(graph[3]/eik[3],np.sqrt(2),places=10)
def test_parallel_incident_minimum_not_sum(self):
g=mesh([[0.,0,0],[1,0,0],[0,1,0],[0,0,1],[0,0,-1]],[[0,1,2,3],[0,1,2,4]])
graph,_=graph_field(g,[1.,4.],[0]);self.assertEqual(graph[1],.25)
def test_all_wall_centroid_shared_and_double_wall_edge_retained(self):
g=mesh([[0.,0,0],[1,0,0],[0,1,0],[0,0,1]],[[0,1,2,3]])
a,zero=cell_flow_speed(g,np.zeros(4),exterior_faces(g['t']))
self.assertGreater(a[0],0);np.testing.assert_array_equal(zero,[0])
graph,_=graph_field(g,a,[0]);self.assertAlmostEqual(graph[1],1/a[0])
eik=solve_eikonal(g,[0],a);np.testing.assert_allclose(eik,graph,rtol=1e-10,atol=1e-10)
def test_cdf_shared_strict_ties_full_volume(self):
f=strict_volume_cdf([0,2,2,3],[1,2,3,4]);g=strict_volume_cdf([0,2,2,3],[1,2,3,4])
np.testing.assert_array_equal(f,g);np.testing.assert_allclose(f,[0,.1,.1,.6]);self.assertLess(f.max(),1)
def test_incomplete_component_not_trimmed_and_percentile_printed(self):
roster=[dict(case='a',component_id='A'),dict(case='b',component_id='A'),dict(case='c',component_id='C')]
def row(case,f,g):return dict(case=case,metrics={'F':{'relative_l2':{'value':f}},'G':{'relative_l2':{'value':g}}})
out=paired_summary([row('a',.1,.2),row('c',.2,.4)],roster,'relative_l2')
self.assertEqual(out['paired_components'],1);self.assertIn('A',out['excluded_components'])
self.assertAlmostEqual(out['delta']['low'],.5);self.assertAlmostEqual(out['delta']['high'],.5)
self.assertNotIn('region',out['delta']);self.assertEqual(out['delta']['seed'],2026111408)
def test_undefined_bootstrap_not_replaced(self):
roster=[dict(case='a',component_id='A')]
out=paired_summary([dict(case='a',metrics={'F':{'relative_l2':{'value':0.}},'G':{'relative_l2':{'value':0.}}})],roster,'relative_l2')
self.assertIsNone(out['delta']['value']);self.assertEqual(out['delta']['reason'],'zero reference mean')
def test_budget_reserves_entire_active_jobs(self):
self.assertTrue(may_dispatch(28800-1802,0));self.assertFalse(may_dispatch(28800-1801,0))
self.assertFalse(may_dispatch(28800-4*1802+1,3))
def test_any_failure_or_missing_or_stop_blocks_score(self):
ok={'worker':{'status':'PASS'}};bad={'worker':{'status':'STOPPED_OR_FAILED'}}
self.assertTrue(scoring_allowed([ok],1,None))
for rows,n,reason in [([ok,bad],2,None),([ok],2,None),([ok],1,'CPU')]:
self.assertFalse(scoring_allowed(rows,n,reason))
with tempfile.TemporaryDirectory() as temp:
r=Path(temp);save(r/'preparation-complete.json',dict(records=[bad],stop_reason=None))
with patch.object(scorer,'contract',return_value={'parts':[{}]}):
with self.assertRaisesRegex(ValueError,'forbids'):scorer.score(r)
def test_restart_and_hash_mismatch_rejected(self):
with tempfile.TemporaryDirectory() as temp:
r=Path(temp);fresh(r);(r/'started.json').write_text('{}')
with self.assertRaises(FileExistsError):fresh(r)
p=r/'cached.npy';np.save(p,np.arange(3.))
with self.assertRaisesRegex(ValueError,'SHA mismatch'):part.checked(p,'0'*64)
def test_real_guard_wall_stop_and_failed_cpu_preserved(self):
with tempfile.TemporaryDirectory() as temp:
r=supervise([sys.executable,'-c','import time; time.sleep(5)'],Path(temp),'worker',
dict(total_cpu_seconds=5,job_cpu_seconds=5,job_wall_seconds=.1,tree_rss_bytes=2**28,disk_bytes=2**20,max_processes=1),poll=.01)
self.assertEqual(r['status'],'STOPPED_OR_FAILED');self.assertIn('wall_limit',r['stop_reason'])
self.assertGreater(r['cpu_seconds'],0);self.assertFalse(r['retry'])
def test_cache_worker_no_solver_no_targets_and_full_provenance(self):
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);case='00000001_synthetic';dest=root/'parts'/case;dest.mkdir(parents=True)
source=root/'source';source.mkdir();cache=root/'cache';cache.mkdir()
x=np.array([[0.,0,0],[1,0,0],[0,1,0],[0,0,1]]);t=np.array([[0,1,2,3]]);raw=np.arange(4)
g=mesh(x,t);faces=exterior_faces(t);dw=np.zeros(4);a,zero=cell_flow_speed(g,dw,faces);df,_=graph_field(g,a,[0])
meta={'inlet_config':{'inlet_location':[0.,0.,0.]}}
np.savez(source/'geometry.npz',points_raw=x,tetra=t,raw_node_ids=raw,boundary_triangles=faces,gate_location_raw=x[0])
for n,v in dict(Dw=dw,Df=df,raw_ids=raw,mass=g['mass'],all_wall_tet_ids=zero).items():np.save(cache/(n+'.npy'),v)
old=dict(status='PASS',case=case,geometry=meta,t_scale_seconds=2.,features=dict(gate_raw_ids=[0],wall_mode='tet_exterior_proxy',
precision='float64',max_iterations=100000,speed_min=float(a.min()),speed_max=float(a.max()),Df_coordinate_and_speed_scale=1.))
save(cache/'result.json',old)
row=dict(case=case,component_id='A',preparation={'preparation_root':str(source),'output_sha256':{'geometry.npz':sha(source/'geometry.npz')}},
v5_cache=dict(output=str(cache),result_sha256=sha(cache/'result.json'),output_sha256={p.name:sha(p) for p in cache.glob('*.npy')}))
q=dict(raw_ids=np.array([1,2,3]),counts=np.array([2,1,1]),t_set=2.)
with patch.object(part,'contract',return_value={'parts':[row],'archive':'SYNTHETIC'}),patch.object(part,'read_geometry',return_value=(x,t,raw,meta)),patch.object(part,'prediction_inputs',return_value=q),patch.object(part,'solve_eikonal',side_effect=AssertionError('Cache route must not solve')):
part.worker(root,0)
result=json.loads((dest/'output/result.json').read_text())
self.assertTrue(result['cache_identity_verified']);self.assertEqual(result['decoded_labels'],[])
self.assertFalse(result['spatial_moment_computed'])
with np.load(dest/'output/query-predictions.npz') as z:np.testing.assert_array_equal(z['F'],z['G'])
if __name__=='__main__':
out=Path(sys.argv[1]);out.mkdir(parents=True,exist_ok=False)
with (out/'tests.log').open('x') as log:
result=unittest.TextTestRunner(stream=log,verbosity=2).run(unittest.defaultTestLoader.loadTestsFromTestCase(Checks))
save(out/'receipt.json',dict(status='PASS' if result.wasSuccessful() else 'FAIL',tests=result.testsRun,
failures=len(result.failures),errors=len(result.errors),
cpu_seconds=sum(resource.getrusage(resource.RUSAGE_SELF)[:2])+sum(resource.getrusage(resource.RUSAGE_CHILDREN)[:2]),
scope='Synthetic only; no real archive/labels',source_sha256={str(p):sha(p) for p in [Path(__file__),Path('tools/r4a_core.py'),Path('tools/r4a_part.py'),Path('tools/r4a_score.py'),Path('tools/run_r4a.py')]}))
print((out/'receipt.json').read_text());raise SystemExit(0 if result.wasSuccessful() else 1)