Implement approved S3-B v0.4 batch flow and bounded solver candidate ten

This commit is contained in:
Frank14f committed 2026-11-15 20:48:24 +08:00
1 parent d9c481d87c
commit 847a6f8ad2
19 files changed
+1700

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
"""Metadata-only v0.4 contract preparation, keeping all original exact evidence."""
from pathlib import Path
import numpy as np
import scipy
from s2_recovery_supervisor import save
from s3b_v04_runtime import ROOT,RULE,CLARIFICATION,OLD_CPU,read,sha,checked
from s3b_v04_math import PARAMETERS
def main():
if ROOT.exists():raise FileExistsError('Never replace v0.4 attempt root')
old=Path('artifacts/s3b-a-ten-part-2026-11-15')
original=read(old/'contract.json');terminal=read(old/'complete.json')
if terminal['status']!='TEN_STOPPED' or terminal['worker_CPU_s']!=OLD_CPU:raise ValueError('Original ledger mismatch')
for p,h in {**original['code_sha256'],**original['source_sha256']}.items():checked(p,h)
receipts=sorted(Path('artifacts/s3b-a-v04-synthetic-2026-11-15').glob('attempt-*/synthetic.receipt.json'))
if not receipts or read(receipts[-1])['status']!='PASS':raise ValueError('No new synthetic acceptance')
references=[]
for i in range(3):
dest=old/'parts'/f'part-{i:02d}'/'output';result=read(dest/'result.json')
if result['status']!='COMPLETE':raise ValueError('Incomplete reference')
references.append(dict(index=i,case=original['ten_parts'][i]['case'],
result=str(dest/'result.json'),result_sha256=sha(dest/'result.json'),
field=str(dest/'event-field.npz'),field_sha256=sha(dest/'event-field.npz')))
sources=[old/'contract.json',old/'complete.json',Path(original['roster']),
Path('docs/s3b-a-v0.4-paper-draft-2026-11-15.md'),
Path('docs/s3b-a-v0.4-approved-contract-2026-11-15.md'),*receipts]
code=[Path('tools')/name for name in ['s3b_v04_math.py','s3b_v04_runtime.py','run_s3b_v04_part.py',
'run_s3b_v04_ten.py','prepare_s3b_v04_ten.py','test_s3b_v04.py',
's3b_graph.py','s3b_runtime.py','test_s3b_graph.py','boundary_distance.py',
's2_recovery_supervisor.py','s2_recovery_monitoring.py']]
c=dict(version='S3B-a-volume-batch-v04-ten-v1',authorizing_ruling=RULE,clarification=CLARIFICATION,
accepted_draft_sha256='429bf06cf8961b42ba52352d1d74feda49854bdb3c426632915a6f8b2ada71d9',
old_stop_commit='d9c481d87c9b53e6af33885e30ce597472d8d5ca',
roster=original['roster'],roster_sha256=original['roster_sha256'],ten_parts=original['ten_parts'],
all_cases=original['all_cases'],total_nodes=original['total_nodes'],exact_references=references,
candidate_order=['splu','gmres_ilu'],parameters=PARAMETERS,worker_cpu=16,coordinator_cpus=[30,31],
old_worker_CPU_s=OLD_CPU,new_synthetic_worker_CPU_s=sum(read(p)['cpu_seconds'] for p in receipts),
limits=dict(total_worker_CPU_s=72000,postfit_reserve_CPU_s=7200,
part_CPU_s=1800,part_wall_s=1800,part_RSS_bytes=4*2**30,
ten_disk_bytes=5*2**30,total_disk_bytes=20*2**30,workers=1,processes_per_worker=1),
numpy_version=np.__version__,scipy_version=scipy.__version__,
label_policy='same exact geometry file allowlist; no labels in prediction',
stage='candidate ten only; then approved reference and full-count cost gates before full617',
source_sha256={str(p):sha(p) for p in sources},code_sha256={str(p):sha(p) for p in code})
checked('docs/s3b-a-v0.4-paper-draft-2026-11-15.md',c['accepted_draft_sha256'])
ROOT.mkdir();save(ROOT/'contract.json',c)
print(dict(root=str(ROOT),contract_sha256=sha(ROOT/'contract.json'),new_synthetic_worker_CPU_s=c['new_synthetic_worker_CPU_s']))
if __name__=='__main__':main()
+109
View File
@@ -0,0 +1,109 @@
"""One immutable geometry-only v0.4 measurement. Never reads labels."""
import argparse
import json
import os
from pathlib import Path
import time
import traceback
import numpy as np
from s2_recovery_supervisor import save
from s3b_v04_runtime import contract, checked, read, sha, restrict_prediction_io
from s3b_graph import edge_geometry, distal_regions
from s3b_v04_math import PARAMETERS, fill_batches
import resource
def run(root, index, method):
c = contract(root); spec = c['ten_parts'][index]
dest = root/'candidates'/method/'parts'/f'part-{index:02d}'/'output'; dest.mkdir(exist_ok=False)
save(dest/'started.json', dict(pid=os.getpid(), case=spec['case'], index=index, unix=time.time(),
solver=method, parameters=PARAMETERS, authorizing_ruling=c['authorizing_ruling'], labels_read=False))
allowed = list(spec['inputs'])
restrict_prediction_io(allowed, dest)
start = time.monotonic(); cpu = time.process_time(); latest = None
cost = {}; geometry = None; last_phase = 'STARTING'
def progress(stage, **kw):
nonlocal last_phase
last_phase=stage
save(dest/'progress.json', dict(stage=stage, case=spec['case'], pid=os.getpid(), unix=time.time(),
worker_CPU_s=time.process_time()-cpu, wall_s=time.monotonic()-start,
peak_RSS_bytes=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024, **kw))
try:
progress('VERIFY_GEOMETRY_ALLOWLIST')
for p, h in spec['inputs'].items(): checked(p, h)
source = Path(spec['preparation_root'])
metadata = read(source/'result.json')
if metadata['base_columns'][5] != 'geodesic_over_Rg':
raise ValueError('Not the original unrestricted geodesic column')
with np.load(source/'geometry.npz', allow_pickle=False) as z:
x=z['points_raw']; tetra=z['tetra']; faces=z['boundary_triangles']
edges=z['edges']; mass=z['node_volume_raw']; raw=z['raw_node_ids']; gate=z['gate_location_raw']
dw = np.load(source/'boundary_distance_raw.npy', allow_pickle=False)
base = np.load(source/'base.npy', allow_pickle=False)
distance = base[:, 5]*metadata['scales']['Rg']; del base
if np.any(np.diff(raw) <= 0): raise ValueError('Original IDs must be increasing')
seed = int(np.argmin(np.linalg.norm(x-gate, axis=1)))
if seed != metadata['base_gate_node']: raise ValueError('Gate source identity differs')
branch_start = time.process_time()
regions = distal_regions(distance, mass, edges, raw)
save(dest/'regions.json', dict(case=spec['case'], definition='strict-geodesic-distal-v0.3',
total_volume=float(mass.sum()), geometry_inputs=spec['inputs'], **regions))
cost['regions_CPU_s'] = time.process_time()-branch_start
def midpoint_progress(done, total):
progress('EXACT_MIDPOINTS', completed=done, total=total)
progress('GEOMETRY_EDGE_CAPACITY')
geometry = edge_geometry(x, tetra, faces, dw, edges, mass, midpoint_progress)
cost.update({k:geometry[k] for k in ['geometry_CPU_s','midpoint_CPU_s']})
np.savez_compressed(dest/'conductance.npz', edges=edges, edge_volume=geometry['edge_volume'],
midpoint_distance=geometry['midpoint'], g=geometry['conductance'],
exact_query_edge_ids=geometry['exact_query_edge_ids'], boundary_edges=geometry['boundary_edges'])
save(dest/'geometry-freeze.json', dict(case=spec['case'], nodes=len(x), total_volume=float(mass.sum()),
exact_midpoint_queries=len(geometry['exact_query_edge_ids']), seed=seed, raw_seed=int(raw[seed]),
boundary_edges=int(geometry['boundary_edges'].sum()), positive_edges=int((geometry['conductance']>0).sum()),
regions_sha256=sha(dest/'regions.json'), conductance_sha256=sha(dest/'conductance.npz'), costs=cost))
progress('BATCH_INITIALIZING', nodes=len(x), events=0, wetted_nodes=1)
last_progress = -1.
event_start = time.process_time()
with (dest/'events.jsonl').open('x', buffering=1) as stream, (dest/'pressure.jsonl').open('x', buffering=1) as pressure_stream:
def event(row):
stream.write(json.dumps(row, allow_nan=False)+'\n')
def state(row, remaining, arrival):
nonlocal latest, last_progress
latest = (row, remaining, arrival)
def stage(phase, **row):
nonlocal last_progress
now=time.monotonic()
if phase != last_phase or now-last_progress>=1:
progress(phase, **row)
last_progress=now
def pressure(row):
row['peak_RSS_bytes']=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024
pressure_stream.write(json.dumps(row, allow_nan=False)+'\n')
arrival, remaining, result = fill_batches(edges, geometry['conductance'], mass, seed, method,
event_callback=event, state_callback=state, stage_callback=stage, pressure_callback=pressure)
cost['events_CPU_s'] = time.process_time()-event_start
np.savez_compressed(dest/'event-field.npz', raw_ids=raw, arrival=arrival, remaining=remaining, mass=mass)
save(dest/'result.json', dict(case=spec['case'], index=index, nodes=len(x),
exact_midpoint_queries=len(geometry['exact_query_edge_ids']), **result, costs=cost,
labels_read=False, GPU=False, new_fits=0, event_batching=True, solver=method, parameters=PARAMETERS,
worker_CPU_s=time.process_time()-cpu, wall_s=time.monotonic()-start,
output_sha256={p.name:sha(p) for p in dest.iterdir() if p.is_file() and p.name!='progress.json'}))
progress(result['status'], event_state=result)
return 0
except Exception as exc:
if latest is not None:
state, remaining, arrival = latest
np.savez_compressed(dest/'last-trusted-state.npz', remaining=remaining, arrival=arrival)
else: state = None
save(dest/'failure.json', dict(status='NUMERICAL_OR_INPUT_FAILURE', case=spec['case'],
type=type(exc).__name__, phase=last_phase, message=str(exc), traceback=traceback.format_exc(),
last_trusted_state=state, remaining_volume=state['remaining_volume'] if state else None,
remaining_state='KNOWN_LAST_STATE' if state else 'UNKNOWN', costs=cost,
worker_CPU_s=time.process_time()-cpu, wall_s=time.monotonic()-start, retry=False))
raise
if __name__ == '__main__':
parser=argparse.ArgumentParser();parser.add_argument('root', type=Path);parser.add_argument('index', type=int)
parser.add_argument('method',choices=['splu','gmres_ilu'])
args=parser.parse_args();raise SystemExit(run(args.root,args.index,args.method))
+93
View File
@@ -0,0 +1,93 @@
"""Sole sequential two-candidate measurement; one corpus solver, shared ledger."""
import argparse
import fcntl
import os
from pathlib import Path
import resource
import signal
import sys
import time
import psutil
from s2_recovery_supervisor import save, supervise, stop_group
from s2_recovery_monitoring import disk_usage
from s3b_v04_runtime import contract, read, sha, reserve_job, failure_classification, may_switch
def run(root):
lock=(root/'batch.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
c=contract(root);lim=c['limits']
if (root/'started.json').exists():raise FileExistsError('Never restart a candidate measurement')
os.sched_setaffinity(0,c['coordinator_cpus'])
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json'),
code_freeze=read(root/'code-freeze.json'),stage='V04_FIXED_TEN',labels_read=False))
interrupted=False
def stop(*_):
nonlocal interrupted
interrupted=True
for child in psutil.Process(os.getpid()).children():stop_group(child.pid)
signal.signal(signal.SIGTERM,stop);signal.signal(signal.SIGINT,stop)
start=time.monotonic();done=[];candidates=[];reason=None;active=None;selected=None
def spent():return c['new_synthetic_worker_CPU_s']+sum(r['worker']['cpu_seconds'] for r in done)
try:
for method in c['candidate_order']:
candidate=[];classification=None
for index,spec in enumerate(c['ten_parts']):
contract(root)
if interrupted:reason='COORDINATOR_INTERRUPTED';break
if not reserve_job(spent(),lim['part_CPU_s']):reason='SHARED_FULL_JOB_RESERVATION';break
if disk_usage(root)>=lim['ten_disk_bytes']:reason='TEN_DISK_LIMIT';break
dest=root/'candidates'/method/'parts'/f'part-{index:02d}'
dest.mkdir(parents=True,exist_ok=False);active=dict(method=method,index=index)
save(root/'progress.json',dict(stage='V04_FIXED_TEN_RUNNING',**active,case=spec['case'],
completed_this_candidate=len(candidate),all_attempts=len(done),
new_settled_worker_CPU_s=spent(),old_worker_CPU_s=c['old_worker_CPU_s'],
remaining_compute_CPU_s=lim['total_worker_CPU_s']-lim['postfit_reserve_CPU_s']-c['old_worker_CPU_s']-spent()))
w=supervise(['taskset','-c',str(c['worker_cpu']),sys.executable,'tools/run_s3b_v04_part.py',str(root),str(index),method],
dest,'worker',dict(total_cpu_seconds=lim['part_CPU_s']+1,job_cpu_seconds=lim['part_CPU_s'],
job_wall_seconds=lim['part_wall_s'],tree_rss_bytes=lim['part_RSS_bytes'],
disk_bytes=lim['ten_disk_bytes']-disk_usage(root),max_processes=1),poll=.2)
paths={n:dest/'output'/f'{n}.json' for n in ['result','failure','progress']}
data={n:read(p) if p.exists() else None for n,p in paths.items()}
classification=failure_classification(w,data['failure'],data['progress'],data['result'])
receipt=dict(method=method,index=index,case=spec['case'],worker=w,classification=classification,
files={n:dict(path=str(p),sha256=sha(p)) for n,p in paths.items() if p.exists()})
save(dest/'guard-result.json',receipt);done.append(receipt);candidate.append(receipt);active=None
print(f'V04_TERMINAL method={method} index={index} status={classification["category"]}',flush=True)
if classification['category']!='PASS':reason=classification['category'];break
candidate_pass=len(candidate)==10 and all(r['classification']['category']=='PASS' for r in candidate)
summary=dict(method=method,status='PASS' if candidate_pass else 'STOPPED',reason=reason,
classification=classification,attempts=candidate,
not_run_cases=[r['case'] for i,r in enumerate(c['ten_parts']) if i not in {v['index'] for v in candidate}])
candidates.append(summary);save(root/f'candidate-{method}.json',summary)
if candidate_pass:
selected=method;reason=None
save(root/'selected-solver.json',dict(method=selected,contract_sha256=sha(root/'contract.json'),
candidate_summary_sha256=sha(root/f'candidate-{method}.json'),
selection_basis='all ten complete within resource and numerical checks; no accuracy/labels read',
reuse_ten=True,reference_gate_not_yet_run=True))
break
if interrupted or reason in {'SHARED_FULL_JOB_RESERVATION','TEN_DISK_LIMIT'}:break
if classification and may_switch(method,classification):
save(root/'fallback-decision.json',dict(from_method=method,to_method='gmres_ilu',
classification=classification,new_worker_CPU_s=spent(),restart_same_fixed_ten_from_index=0))
reason=None
else:break
except Exception as exc:
reason='COORDINATOR_EXCEPTION'
save(root/'coordinator-failure.json',dict(type=type(exc).__name__,message=str(exc),active=active))
raise
finally:
report=dict(status='TEN_PASS_AWAITING_REFERENCE_AND_COST' if selected else 'TEN_STOPPED',
stop_reason=reason,selected_solver=selected,candidates=candidates,attempts=len(done),
new_worker_CPU_s=spent(),old_worker_CPU_s=c['old_worker_CPU_s'],
cumulative_worker_CPU_s=c['old_worker_CPU_s']+spent(),postfit_reserve_CPU_s=lim['postfit_reserve_CPU_s'],
remaining_compute_CPU_s=lim['total_worker_CPU_s']-lim['postfit_reserve_CPU_s']-c['old_worker_CPU_s']-spent(),
coordinator_CPU_s=sum(resource.getrusage(resource.RUSAGE_SELF)[:2]),wall_s=time.monotonic()-start,
account_complete=active is None,no_silent_retries=True,labels_read=False,
note='Reference and full-count cost gates still required; no full617 launch by this coordinator.')
save(root/'complete.json',report);save(root/'progress.json',report)
return 0 if selected else 1
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();raise SystemExit(run(a.root))
+185
View File
@@ -0,0 +1,185 @@
"""Approved volume-batched graph prior; no filesystem or label access."""
import time
import numpy as np
from scipy.sparse import coo_matrix, diags
from scipy.sparse.linalg import splu, spilu, gmres, LinearOperator
from s3b_graph import SOLVER as TOL, NumericalFailure
PARAMETERS = dict(batch_volume_fraction=.005,
splu=dict(permc_spec='COLAMD', diag_pivot_thresh=1.),
gmres=dict(restart=50, maxiter=40, rtol=1e-9, atol=0., callback_type='pr_norm'),
spilu=dict(drop_tol=1e-4, fill_factor=10., drop_rule='basic,area',
permc_spec='COLAMD', diag_pivot_thresh=1.),
maxiter_unit='restart cycles; at most2000 inner steps',
early_resolve='newly full node has positive rate but no unfilled positive-g neighbour')
class SolverFailure(NumericalFailure):
pass
class RoutingFailure(NumericalFailure):
pass
def reroute_once(adjacency, rates, newly_full, remaining):
"""All exhausted nodes handled simultaneously. No occupied volume is sent."""
ids=np.flatnonzero(newly_full & (rates > 0))
recipients=[]
for i in ids:
a,b=adjacency.indptr[i:i+2];j=adjacency.indices[a:b];g=adjacency.data[a:b]
keep=(remaining[j]>0)&(g>0)
if not keep.any():
# No partial transfers. The caller ends this batch and re-solves.
return None, dict(early_resolve=True,dead_end_node=int(i),forwarded_nodes=0)
recipients.append((int(i),j[keep],g[keep]))
result=rates.copy();result[newly_full]=0.
for i,j,g in recipients:
result[j] += rates[i]*g/g.sum()
return result,dict(early_resolve=False,dead_end_node=None,forwarded_nodes=len(ids))
class PressureEngine:
def __init__(self,edges,g,n,method,stage=None,record=None):
if method not in ('splu','gmres_ilu'):raise ValueError('Unfrozen solver')
if not np.isfinite(g).all() or np.any(g<0):raise ValueError('Invalid conductance')
keep=g>0;self.scale=float(g.max()) if keep.any() else 1.
data=g[keep]/self.scale
if np.any(data<=0):raise SolverFailure('Positive g normalization underflow')
e=edges[keep]
self.adj=coo_matrix((np.tile(data,2),(np.r_[e[:,0],e[:,1]],np.r_[e[:,1],e[:,0]])),shape=(n,n)).tocsr()
self.lap=diags(np.asarray(self.adj.sum(axis=1)).ravel())-self.adj
self.previous=np.zeros(n);self.method=method;self.stage=stage;self.record=record
self.calls=0;self.CPU=0.;self.initial=0;self.scheduled=0;self.early=0
def notify(self,phase,**extra):
if self.stage:self.stage(phase,method=self.method,pressure_calls=self.calls,
completed_pressure_CPU_s=self.CPU,**extra)
def has_front(self,wet):
v=np.asarray(self.adj@wet.astype(float));v[wet]=0.
return bool((v>0).any())
def solve(self,wet,seed,Q,reason):
started=time.process_time();wall=time.monotonic();self.calls+=1
if reason=='INITIAL':self.initial+=1
elif reason=='DEAD_END':self.early+=1
else:self.scheduled+=1
self.notify('PRESSURE_MATRIX',wetted_nodes=int(wet.sum()),reason=reason)
ids=np.flatnonzero(wet);a=self.lap[ids][:,ids].tocsc()
b=np.zeros(len(ids));b[np.searchsorted(ids,seed)]=Q
iterations=0;callback_residual=None
def callback(value):
nonlocal iterations,callback_residual
iterations+=1;callback_residual=float(value)
try:
self.notify('PRESSURE_FACTORIZE',wetted_nodes=len(ids),matrix_nnz=a.nnz,reason=reason)
factor_start=time.process_time()
if self.method=='splu':factor=splu(a,**PARAMETERS['splu'])
else:factor=spilu(a,**PARAMETERS['spilu'])
factor_CPU=time.process_time()-factor_start
self.notify('PRESSURE_SOLVE',wetted_nodes=len(ids),factor_CPU_s=factor_CPU,reason=reason)
solve_start=time.process_time()
if self.method=='splu':p=factor.solve(b);info=0
else:
preconditioner=LinearOperator(a.shape,matvec=factor.solve,dtype=np.float64)
p,info=gmres(a,b,x0=self.previous[ids],M=preconditioner,callback=callback,**PARAMETERS['gmres'])
solve_CPU=time.process_time()-solve_start
self.notify('PRESSURE_POSTCHECK',wetted_nodes=len(ids),reason=reason)
residual=float(np.linalg.norm(b-a@p)/np.linalg.norm(b))
if info!=0 or not np.isfinite(p).all() or not np.isfinite(residual) or residual>TOL['acceptance_residual']:
raise SolverFailure(f'Pressure returned invalid: info={info}, raw residual={residual}')
pressure=np.zeros(len(wet));pressure[ids]=p
rates=np.asarray(self.adj@pressure);rates[wet]=0.
if not np.isfinite(rates).all() or np.any(rates < -TOL['negative_flux_tolerance']*Q):
raise SolverFailure('Invalid/negative pressure front flux')
negative=float(-rates[rates<0].sum());rates[rates<0]=0.
imbalance=float(abs(rates.sum()-Q)/Q)
if imbalance>TOL['flux_tolerance']:raise SolverFailure(f'Pressure flux imbalance={imbalance}')
self.previous=pressure
cost=time.process_time()-started;self.CPU+=cost
record=dict(call=self.calls,reason=reason,method=self.method,wetted_nodes=len(ids),matrix_nnz=a.nnz,
factor_CPU_s=factor_CPU,linear_solve_CPU_s=solve_CPU,total_pressure_CPU_s=cost,
wall_s=time.monotonic()-wall,iterations=iterations,callback_residual=callback_residual,
original_residual=residual,flux_imbalance=imbalance,negative_roundoff=negative)
if self.record:self.record(record)
return rates
except SolverFailure:raise
except Exception as exc:
raise SolverFailure(f'{self.method} pressure factor/solve: {type(exc).__name__}: {exc}') from exc
def fill_batches(edges,g,mass,seed,method,event_callback=None,state_callback=None,
stage_callback=None,pressure_callback=None,Q=1.,batch_fraction=None):
"""Batched pressure, exact capacity subevents, and declared early re-solves."""
# Optional fraction is for analytical synthetic fixtures only. Production
# calls omit it and contract pins PARAMETERS; no tuning command-line knob.
fraction=PARAMETERS['batch_volume_fraction'] if batch_fraction is None else batch_fraction
if not 0<fraction<=1 or not np.isfinite(mass).all() or np.any(mass<=0) or Q<=0:
raise ValueError('Invalid capacity/batch domain')
n=len(mass);M=float(mass.sum());initial=float(mass[seed])
engine=PressureEngine(edges,g,n,method,stage_callback,pressure_callback)
remaining=np.array(mass,float);remaining[seed]=0.
wet=np.zeros(n,bool);wet[seed]=True
arrival=np.full(n,np.nan);arrival[seed]=0.
physical_time=0.;rounding=0.;events=0;batches=0;reason='INITIAL'
def summary(status):
return dict(status=status,events=events,batches=batches,filled_nodes=int(wet.sum()),
remaining_volume=float(remaining.sum()),total_volume=M,initial_seed_volume=initial,
graph_time=physical_time,cumulative_rounding_volume=rounding,
pressure_calls=engine.calls,initial_solves=engine.initial,scheduled_solves=engine.scheduled,
early_resolves=engine.early,pressure_CPU_s=engine.CPU,conductance_normalization=engine.scale)
def emit(status):
if state_callback:state_callback(summary(status),remaining,arrival)
emit('INITIALIZED')
while not wet.all():
if not engine.has_front(wet):return arrival,remaining,summary('BLOCKED')
rates=engine.solve(wet,seed,Q,reason);batches+=1
target=min(fraction*M,float(remaining.sum()));occupied=0.
while True:
if stage_callback:stage_callback('SUBEVENT_ROUTING',method=method,pressure_calls=engine.calls,
completed_pressure_CPU_s=engine.CPU,batch=batches,event=events)
active=rates>0
if not active.any() or abs(rates.sum()-Q)>TOL['flux_tolerance']*Q:
raise RoutingFailure('Routing lost/duplicated total Q')
left=target-occupied
if left<=16*np.finfo(float).eps*M:
reason='SCHEDULED';break
dt=min(left/Q,float(np.min(remaining[active]/rates[active])))
if not np.isfinite(dt) or dt<=0 or physical_time+dt<=physical_time:
raise RoutingFailure('Invalid subevent dt')
before=float(remaining.sum());new=remaining-dt*rates
tol=TOL['tie_mass_tolerance']*M
if np.min(new)<-tol:raise RoutingFailure('Capacity overshoot')
full=(~wet)&active&(new<=tol)
correction=float(np.abs(new[full]).sum());new[full]=0.
consumed=before-float(new.sum())
if abs(consumed-Q*dt)>TOL['mass_tolerance']*M:
raise RoutingFailure('Subevent conservation')
next_time=physical_time+dt
if rounding+correction>TOL['mass_tolerance']*M or abs(M-new.sum()-initial-Q*next_time)>TOL['mass_tolerance']*M:
raise RoutingFailure('Cumulative conservation')
remaining=new;wet[full]=True;physical_time=next_time;arrival[full]=physical_time
rounding+=correction;occupied+=consumed;events+=1
record=dict(event=events,batch=batches,newly_wet=int(full.sum()),filled_nodes=int(wet.sum()),
graph_time=physical_time,delta_time=dt,new_occupied_volume=consumed,batch_occupied_volume=occupied,
remaining_volume=float(remaining.sum()),rounding_volume=correction,
rate_sum=float(rates.sum()),front_count=int(active.sum()),early_resolve=False,
forwarded_nodes=0,pressure_calls=engine.calls)
if wet.all():
if event_callback:event_callback(record)
emit('COMPLETE');return arrival,remaining,summary('COMPLETE')
boundary=occupied>=target-16*np.finfo(float).eps*M
if boundary:reason='SCHEDULED'
elif full.any():
updated,route=reroute_once(engine.adj,rates,full,remaining)
record.update(route)
if route['early_resolve']:reason='DEAD_END';boundary=True
else:
rates=updated
if np.any(rates < -TOL['negative_flux_tolerance']*Q) or abs(rates.sum()-Q)>TOL['flux_tolerance']*Q:
raise RoutingFailure('Single-reroute conservation')
if event_callback:event_callback(record)
emit('RUNNING')
if boundary:break
return arrival,remaining,summary('COMPLETE')
+53
View File
@@ -0,0 +1,53 @@
"""Independent v0.4 pins, cumulative budget and narrowly allowed fallback."""
from pathlib import Path
from s3b_runtime import read, sha, checked, restrict_prediction_io
ROOT=Path('artifacts/s3b-a-v04-ten-2026-11-15')
RULE='1794745781.398039'
CLARIFICATION='1794745880.964439'
OLD_CPU=3239.962177
TOTAL=72000.
RESERVE=7200.
PRESSURE_PHASES={'PRESSURE_MATRIX','PRESSURE_FACTORIZE','PRESSURE_SOLVE','PRESSURE_POSTCHECK'}
def contract(root):
c=read(Path(root)/'contract.json')
for p,h in {**c['code_sha256'],**c['source_sha256']}.items():checked(p,h)
freeze=read(Path(root)/'code-freeze.json')
if freeze['contract_sha256']!=sha(Path(root)/'contract.json') or not freeze.get('git_commit'):
raise ValueError('Not Git-frozen')
if (c['authorizing_ruling']!=RULE or c['clarification']!=CLARIFICATION or
c['old_worker_CPU_s']!=OLD_CPU or c['limits']['total_worker_CPU_s']!=TOTAL or
c['limits']['postfit_reserve_CPU_s']!=RESERVE):raise ValueError('Scope/ledger mismatch')
return c
def reserve_job(new_spent,cap=1800.):
return new_spent>=0 and OLD_CPU+new_spent+cap+RESERVE<=TOTAL
def failure_classification(guard,failure=None,progress=None,result=None):
"""A gate failure never becomes evidence for changing the solver."""
failure=failure or {};progress=progress or {};result=result or {}
if guard.get('monitor_errors'):
return dict(category='MONITOR_FAILURE',solver_attributable=False,reason='monitor_errors')
stop=guard.get('stop_reason')
if stop:
attributable=(stop in {'wall_limit','tree_rss_limit','job_cpu_limit',
'wall_limit_after_exit','rss_limit_after_exit','cpu_limit_after_exit'}
and progress.get('stage') in PRESSURE_PHASES)
return dict(category='RESOURCE_STOP',solver_attributable=attributable,reason=stop,
last_phase=progress.get('stage','UNKNOWN'))
if failure:
return dict(category='NUMERICAL_FAILURE' if failure.get('type')=='SolverFailure' else 'ROUTING_OR_INPUT_FAILURE',
solver_attributable=failure.get('type')=='SolverFailure',
reason=failure.get('message','UNKNOWN'),exception_type=failure.get('type','UNKNOWN'))
if guard.get('status')=='PASS' and result.get('status')=='COMPLETE':
return dict(category='PASS',solver_attributable=False,reason=None)
return dict(category='GEOMETRIC_BLOCKED' if result.get('status')=='BLOCKED' else 'UNKNOWN_FAILURE',
solver_attributable=False,reason=result.get('status','missing terminal result'))
def may_switch(method,classification,gate='candidate'):
return method=='splu' and gate=='candidate' and classification.get('solver_attributable',False)
+147
View File
@@ -0,0 +1,147 @@
"""Analytical batch mechanics and authorization/failure boundary tests."""
import unittest
from unittest.mock import patch
from pathlib import Path
import tempfile
import numpy as np
from scipy.sparse import csr_matrix
from s3b_v04_math import fill_batches, reroute_once, SolverFailure, PressureEngine
from s3b_v04_runtime import reserve_job, failure_classification, may_switch
# Retain geometry, strict-region and actual subprocess I/O barrier checks.
from test_s3b_graph import GraphTests
from s2_recovery_supervisor import save
from s3b_v04_runtime import read, OLD_CPU
from run_s3b_v04_ten import run as run_ten
class BatchTests(unittest.TestCase):
def test_chain_both_solvers(self):
for method in ['splu','gmres_ilu']:
a,r,s=fill_batches(np.array([[0,1],[1,2]]),np.array([2.,3.]),np.array([2.,3.,5.]),0,method)
np.testing.assert_allclose(a,[0,3,8],atol=2e-12)
self.assertEqual(s['status'],'COMPLETE');self.assertEqual(r.sum(),0)
self.assertEqual(s['initial_solves'],1)
self.assertEqual(s['pressure_calls'],s['initial_solves']+s['scheduled_solves']+s['early_resolves'])
def test_dead_end_keeps_half_filled_time_and_volume(self):
# A has capacity .01 and dies at t=.02 while B is only .01 filled.
# New pressure sends Q=1 to B; its capacity/time are not reset.
for method in ['splu','gmres_ilu']:
rows=[];states=[];pressures=[]
a,r,s=fill_batches(np.array([[0,1],[0,2]]),np.ones(2),np.array([1.,.01,10.]),0,method,
event_callback=rows.append,pressure_callback=pressures.append,
state_callback=lambda row,r,a:states.append((dict(row),r.copy())))
np.testing.assert_allclose(a,[0,.02,10.01],atol=1e-10)
self.assertEqual(s['early_resolves'],1)
self.assertEqual(pressures[1]['reason'],'DEAD_END')
self.assertTrue(rows[0]['early_resolve'])
self.assertAlmostEqual(rows[0]['new_occupied_volume'],.02)
self.assertAlmostEqual(states[1][1][2],9.99)
self.assertGreater(rows[1]['graph_time'],rows[0]['graph_time'])
self.assertAlmostEqual(rows[1]['batch_occupied_volume'],.005*11.01)
self.assertAlmostEqual(sum(x['new_occupied_volume'] for x in rows),10.01)
def test_single_reroute_simultaneous_sources_and_existing_rate(self):
adj=csr_matrix(np.array([[0,7,1,2],[7,0,3,0],[1,3,0,0],[2,0,0,0]],float))
original=np.array([.3,.4,.1,.2]);full=np.array([True,True,False,False])
new,info=reroute_once(adj,original,full,np.array([0.,0.,3.,4.]))
np.testing.assert_allclose(new,[0,0,.6,.4]);np.testing.assert_allclose(original,[.3,.4,.1,.2])
self.assertEqual(info['forwarded_nodes'],2);self.assertFalse(info['early_resolve'])
def test_dead_end_prevents_partial_transfer(self):
adj=csr_matrix(np.array([[0,0,1],[0,0,0],[1,0,0]],float))
rates=np.array([.4,.6,0.]);full=np.array([True,True,False])
new,info=reroute_once(adj,rates,full,np.array([0.,0.,2.]))
self.assertIsNone(new);self.assertTrue(info['early_resolve'])
np.testing.assert_array_equal(rates,[.4,.6,0.])
def test_diamond_ties_complete_without_final_solve(self):
for method in ['splu','gmres_ilu']:
a,r,s=fill_batches(np.array([[0,1],[0,2],[1,3],[2,3]]),np.ones(4),np.ones(4),0,method,batch_fraction=1.)
np.testing.assert_allclose(a,[0,2,2,3]);self.assertEqual(s['pressure_calls'],1)
self.assertEqual(s['events'],2);self.assertEqual(s['status'],'COMPLETE')
def test_Q_g_scaling_and_permutation(self):
e=np.array([[0,1],[0,2],[1,3],[2,3]]);g=np.array([1.,2.,3.,4.]);m=np.ones(4)
a,_,_=fill_batches(e,g,m,0,'splu')
b,_,_=fill_batches(e,g*13,m,0,'splu',Q=7)
np.testing.assert_allclose(a,b*7,atol=2e-11)
perm=np.array([2,3,1,0]);mm=np.empty_like(m);mm[perm]=m
c,_,_=fill_batches(perm[e[::-1]],g[::-1],mm,int(perm[0]),'splu')
np.testing.assert_allclose(a,c[perm],atol=2e-11)
def test_zero_conductance_capacity_is_retained(self):
for method in ['splu','gmres_ilu']:
a,r,s=fill_batches(np.array([[0,1],[1,2]]),np.array([1.,0.]),np.array([1.,2.,4.]),0,method)
self.assertEqual(s['status'],'BLOCKED');self.assertEqual(s['remaining_volume'],4)
self.assertTrue(np.isnan(a[2]));self.assertEqual(r[2],4)
def test_postcheck_rejects_bad_solver_exit_success(self):
class BadFactor:
def solve(self,b):return np.zeros_like(b)
with patch('s3b_v04_math.splu',return_value=BadFactor()):
with self.assertRaisesRegex(SolverFailure,'raw residual'):
fill_batches(np.array([[0,1]]),np.ones(1),np.ones(2),0,'splu')
with patch('s3b_v04_math.gmres',return_value=(np.zeros(1),0)):
with self.assertRaises(SolverFailure):
fill_batches(np.array([[0,1]]),np.ones(1),np.ones(2),0,'gmres_ilu')
def test_shared_budget_preserves_postfit(self):
self.assertTrue(reserve_job(61560.037823-1800))
self.assertFalse(reserve_job(61560.037823-1799.99))
def test_solver_failure_keeps_reason_and_reference_never_switches(self):
guard=dict(status='STOPPED_OR_FAILED',monitor_errors=0,stop_reason=None)
c=failure_classification(guard,dict(type='SolverFailure',message='raw residual=0.1'))
self.assertEqual(c['category'],'NUMERICAL_FAILURE');self.assertIn('residual',c['reason'])
self.assertTrue(may_switch('splu',c));self.assertFalse(may_switch('gmres_ilu',c))
self.assertFalse(may_switch('splu',c,gate='reference'))
self.assertFalse(may_switch('splu',failure_classification(guard,dict(type='RoutingFailure',message='mass'))))
for phase,expected in [('PRESSURE_FACTORIZE',True),('EXACT_MIDPOINTS',False),('SUBEVENT_ROUTING',False),('UNKNOWN',False)]:
c=failure_classification(dict(guard,stop_reason='wall_limit'),progress={'stage':phase})
self.assertEqual(may_switch('splu',c),expected)
self.assertFalse(may_switch('splu',failure_classification(guard)))
def coordinator_fixture(self,failures):
"""Exercise actual dispatch/fallback/ledger without any scientific input."""
calls=[]
def fake_supervise(command,dest,name,limits,**kw):
method=command[-1];index=int(command[-2]);calls.append((method,index))
out=dest/'output';out.mkdir()
fail=failures.get((method,index))
if fail:
save(out/'failure.json',dict(type=fail,message=method+' original reason'))
else:save(out/'result.json',dict(status='COMPLETE'))
return dict(status='STOPPED_OR_FAILED' if fail else 'PASS',stop_reason=None,
monitor_errors=0,cpu_seconds=2.)
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);save(root/'contract.json',{});save(root/'code-freeze.json',{'git_commit':'synthetic'})
c=dict(coordinator_cpus=[0],worker_cpu=0,old_worker_CPU_s=OLD_CPU,new_synthetic_worker_CPU_s=3.,
candidate_order=['splu','gmres_ilu'],ten_parts=[{'case':str(i)} for i in range(10)],
limits=dict(part_CPU_s=1800,part_wall_s=1800,part_RSS_bytes=4*2**30,
ten_disk_bytes=5*2**30,total_worker_CPU_s=72000,postfit_reserve_CPU_s=7200))
with patch('run_s3b_v04_ten.contract',return_value=c),patch('run_s3b_v04_ten.supervise',side_effect=fake_supervise),patch('run_s3b_v04_ten.os.sched_setaffinity'),patch('run_s3b_v04_ten.signal.signal'):
exitcode=run_ten(root)
return exitcode,read(root/'complete.json'),calls
def test_dispatch_restarts_whole_ten_only_once_and_charges_failures(self):
code,r,calls=self.coordinator_fixture({('splu',2):'SolverFailure'})
self.assertEqual(code,0);self.assertEqual(r['selected_solver'],'gmres_ilu')
self.assertEqual(calls,[('splu',i) for i in range(3)]+[('gmres_ilu',i) for i in range(10)])
self.assertEqual(r['new_worker_CPU_s'],29.)
self.assertEqual(r['cumulative_worker_CPU_s'],OLD_CPU+29.)
self.assertEqual(r['candidates'][0]['classification']['category'],'NUMERICAL_FAILURE')
def test_two_numerical_failures_retained_without_third_candidate(self):
code,r,calls=self.coordinator_fixture({('splu',0):'SolverFailure',('gmres_ilu',1):'SolverFailure'})
self.assertEqual(code,1);self.assertIsNone(r['selected_solver'])
self.assertEqual(calls,[('splu',0),('gmres_ilu',0),('gmres_ilu',1)])
self.assertEqual([c['classification']['reason'] for c in r['candidates']],
['splu original reason','gmres_ilu original reason'])
def test_nonsolver_failure_stops_dispatch_without_fallback(self):
code,r,calls=self.coordinator_fixture({('splu',0):'RoutingFailure'})
self.assertEqual(code,1);self.assertEqual(calls,[('splu',0)])
if __name__=='__main__':unittest.main(verbosity=2)