amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

426 lines
21 KiB
Python

import base64
import bz2
import hashlib
import json
import math
import os
from pathlib import Path
import random
import subprocess
import sys
import tempfile
import time
import re
import shutil
RECOVERY_SECONDS = 3
RECORD_WIRE = False
SUPERVISOR = r'''
import base64,bz2,ctypes,hashlib,json,os,signal,subprocess,sys,time
cfg=json.loads(sys.argv[1]); start=time.monotonic(); errors=[]; tracked={}; signals={}; waits={}
leader=None; leader_start=None; output=b''; diagnostic=b''; timed_out=False; drained=False
reaped=False; leader_rc=None; remaining=[]; pg_absent=False
def fault(op, error):
errors.append([op,type(error).__name__,str(error)])
def exact(pid):
try:
with open('/proc/%d/stat'%pid,'rb') as stream: data=stream.read()
words=data[data.rfind(b')')+2:].split()
return {'pid':pid,'state':words[0].decode('ascii'),'ppid':int(words[1]),'pg':int(words[2]),'start':int(words[19])}
except (FileNotFoundError,ProcessLookupError): return None
def table_now():
result={}
for name in os.listdir('/proc'):
if name.isascii() and name.isdigit():
value=exact(int(name))
if value is not None: result[value['pid']]=value
return result
def closure(table, supervisor, remembered):
roots={supervisor}
for pid,old in remembered.items():
current=table.get(pid)
if current and current['start']==old['start'] and current['state'] not in ('Z','X'):
roots.add(pid)
found={}
changed=True
while changed:
changed=False
for pid,current in table.items():
if pid!=supervisor and pid not in found and (current['ppid'] in roots or pid in roots):
found[pid]=current; roots.add(pid); changed=True
return found
def discover():
table=table_now(); found=closure(table,os.getpid(),tracked)
for pid,current in found.items():
if pid not in tracked or tracked[pid]['start']!=current['start']:
tracked[pid]=dict(current)
return table,found
def send_fresh(pid, token):
current=exact(pid)
if current is None or current['start']!=token['start'] or current['state'] in ('Z','X'): return
os.kill(pid,signal.SIGKILL)
signals[(pid,token['start'])]=signals.get((pid,token['start']),0)+1
def own_wait():
global reaped,leader_rc
if leader is not None:
value=leader.poll()
if value is not None:
leader_rc=leader.wait(timeout=0)
reaped=True
def adopted_wait():
if leader is not None and not reaped: return
while True:
try: pid,status=os.waitpid(-1,os.WNOHANG)
except ChildProcessError: break
if pid==0: break
waits[pid]=status
supervisor_start=exact(os.getpid())['start']
try:
libc=ctypes.CDLL(None,use_errno=True)
if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')
leader=subprocess.Popen(cfg['argv'],cwd=cfg['cwd'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
info=exact(leader.pid)
if info is None: raise RuntimeError('leader identity disappeared before capture')
leader_start=info['start']; tracked[leader.pid]=info
payload=sys.stdin.buffer.read()
first=True
while True:
try:
output,diagnostic=leader.communicate(input=payload if first else None,timeout=0.04)
drained=True; own_wait(); break
except subprocess.TimeoutExpired:
first=False
own_wait()
discover()
if time.monotonic()-start>=cfg['deadline']:
timed_out=True; break
except BaseException as error:
fault('execute',error)
finally:
recovery_end=time.monotonic()+cfg['recovery']
while True:
try: own_wait()
except BaseException as error: fault('own_wait',error)
found={}
try: _,found=discover()
except BaseException as error: fault('discover',error)
for pid,token in list(found.items()):
try: send_fresh(pid,token)
except (FileNotFoundError,ProcessLookupError): pass
except BaseException as error: fault('signal',error)
try: own_wait()
except BaseException as error: fault('own_wait',error)
try: adopted_wait()
except BaseException as error: fault('adopted_wait',error)
if leader is not None and not drained:
try:
output,diagnostic=leader.communicate(timeout=0.03)
drained=True; own_wait()
except subprocess.TimeoutExpired: pass
except BaseException as error: fault('drain',error)
try:
table,found=discover()
remaining=[[p,v['start'],v['state']] for p,v in found.items()]
pg_absent=leader is not None and not any(v['pg']==leader.pid for v in table.values())
except BaseException as error:
fault('absence',error); remaining=[['unknown']]; pg_absent=False
if reaped and drained and not remaining and pg_absent: break
if time.monotonic()>=recovery_end:
fault('recovery',RuntimeError('deadline with unconfirmed closure')); break
time.sleep(0.01)
meta={'phase':cfg['phase'],'nonce':cfg['nonce'],'argv':cfg['argv'],
'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),
'supervisor_pid':os.getpid(),'supervisor_start':supervisor_start,
'leader_pid':None if leader is None else leader.pid,'leader_start':leader_start,
'leader_rc':leader_rc,'leader_reaped':reaped,'timeout':timed_out,
'controlled_rc':124 if timed_out else leader_rc,'elapsed':time.monotonic()-start,
'deadline':cfg['deadline'],'recovery':cfg['recovery'],
'pg_absent':pg_absent,'remaining':remaining,'exceptions':errors,
'adopted':[[p,None,status,os.waitstatus_to_exitcode(status)] for p,status in waits.items()],
'descendants':[[p,t['start'],signals.get((p,t['start']),0),None]
for p,t in tracked.items() if leader is None or p!=leader.pid],
'stdout_len':len(output),'stdout_sha256':hashlib.sha256(output).hexdigest(),
'stderr_len':len(diagnostic),'stderr_sha256':hashlib.sha256(diagnostic).hexdigest()}
wire={'meta':meta,'stdout':base64.b64encode(bz2.compress(output)).decode('ascii'),
'stderr':base64.b64encode(bz2.compress(diagnostic)).decode('ascii')}
sys.stdout.write(json.dumps(wire,separators=(',',':'))+'\n')
'''
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nsys.exit(1)"
def identity_int(value, minimum=0):
return type(value) is int and value >= minimum
def validate_meta(meta, config, out, err):
required = {'phase','nonce','argv','uid','gid','groups','supervisor_pid','supervisor_start',
'leader_pid','leader_start','leader_rc','leader_reaped','timeout','controlled_rc',
'elapsed','deadline','recovery','pg_absent','remaining','exceptions','descendants',
'stdout_len','stdout_sha256','stderr_len','stderr_sha256','adopted'}
if type(meta) is not dict or not required <= meta.keys():
raise ValueError('missing stage metadata')
if meta['phase'] != config['phase'] or meta['nonce'] != config['nonce'] or meta['argv'] != config['argv']:
raise ValueError('stage binding conflict')
for name in ('uid','gid','supervisor_pid','supervisor_start','leader_pid','leader_start'):
if not identity_int(meta[name], 1 if name not in ('uid','gid') else 0):
raise ValueError('invalid identity '+name)
if meta['supervisor_pid'] == meta['leader_pid']:
raise ValueError('supervisor and leader identical')
isolated = os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534'
expected_identity = (65534,65534,[]) if isolated else (os.getuid(),os.getgid(),os.getgroups())
if type(meta['groups']) is not list or any(type(x) is not int or x < 0 for x in meta['groups']):
raise ValueError('invalid groups')
if (meta['uid'],meta['gid'],meta['groups']) != expected_identity:
raise ValueError('execution identity conflict')
if type(meta['deadline']) is not int or meta['deadline'] != config['deadline']:
raise ValueError('deadline conflict')
if type(meta['recovery']) is not int or meta['recovery'] != RECOVERY_SECONDS:
raise ValueError('recovery conflict')
elapsed = meta['elapsed']
if type(elapsed) not in (int,float) or not math.isfinite(elapsed) or not 0 <= elapsed <= config['deadline']+RECOVERY_SECONDS+2:
raise ValueError('invalid elapsed')
for name in ('timeout','leader_reaped','pg_absent'):
if type(meta[name]) is not bool:
raise ValueError('invalid boolean '+name)
for name in ('leader_rc','controlled_rc'):
if type(meta[name]) is not int:
raise ValueError('invalid return code '+name)
if meta['controlled_rc'] != (124 if meta['timeout'] else meta['leader_rc']):
raise ValueError('return code conflict')
for name in ('remaining','exceptions','descendants','adopted'):
if type(meta[name]) is not list:
raise ValueError('invalid list '+name)
for row in meta['descendants']:
if type(row) is not list or len(row) != 4 or not all(identity_int(x,1) for x in row[:2]) or not identity_int(row[2]) or (row[3] is not None and type(row[3]) is not int):
raise ValueError('invalid descendant record')
for row in meta['adopted']:
if (type(row) is not list or len(row)!=4 or not identity_int(row[0],1)
or row[1] is not None or type(row[2]) is not int or type(row[3]) is not int
or os.waitstatus_to_exitcode(row[2])!=row[3]):
raise ValueError('invalid adopted wait identity')
for name,data in (('stdout',out),('stderr',err)):
if not identity_int(meta[name+'_len']) or meta[name+'_len'] != len(data):
raise ValueError('invalid IO length')
if type(meta[name+'_sha256']) is not str or meta[name+'_sha256'] != hashlib.sha256(data).hexdigest():
raise ValueError('invalid IO digest')
if meta['exceptions'] or meta['remaining'] or not meta['leader_reaped'] or not meta['pg_absent']:
raise ValueError('stage recovery or exception failure')
if meta['timeout'] or meta['leader_rc'] != 0 or meta['controlled_rc'] != 0:
raise ValueError('controlled timeout' if meta['timeout'] else 'leader nonzero exit')
def execute_stage(phase, argv, cwd, deadline, payload, evidence):
nonce = os.urandom(8).hex()
config = dict(phase=phase,argv=argv,cwd=str(cwd),deadline=deadline,recovery=RECOVERY_SECONDS,nonce=nonce)
wrapper_argv = [sys.executable,'-I','-c',SUPERVISOR,json.dumps(config,separators=(',',':'))]
raw = subprocess.run(wrapper_argv,input=payload,capture_output=True,timeout=deadline+RECOVERY_SECONDS+5)
# Preserve actual wrapper facts before touching or trusting its wire protocol.
entry = dict(phase=phase,nonce=nonce,wrapper_rc=raw.returncode,
wire_len=len(raw.stdout),wire_sha256=hashlib.sha256(raw.stdout).hexdigest(),
wrapper_stderr_len=len(raw.stderr),wrapper_stderr_sha256=hashlib.sha256(raw.stderr).hexdigest())
if RECORD_WIRE:
protected = Path(__file__).resolve().parent / 'wire-records'
protected.mkdir(mode=0o700,exist_ok=True)
(protected/(nonce+'.wire')).write_bytes(raw.stdout)
(protected/(nonce+'.wrapper-stderr')).write_bytes(raw.stderr)
entry['protected_wire'] = nonce
evidence.append(entry)
if raw.returncode != 0:
raise ValueError(phase+' supervisor nonzero')
wire = json.loads(raw.stdout)
out = bz2.decompress(base64.b64decode(wire['stdout'],validate=True))
err = bz2.decompress(base64.b64decode(wire['stderr'],validate=True))
meta = wire['meta']
entry['meta'] = meta
validate_meta(meta,config,out,err)
return out,err
META_COLUMNS = ['phase','nonce','argv','uid','gid','groups','supervisor_pid','supervisor_start',
'leader_pid','leader_start','leader_rc','leader_reaped','timeout','controlled_rc','elapsed',
'deadline','recovery','pg_absent','remaining','exceptions','descendants','adopted',
'stdout_len','stdout_sha256','stderr_len','stderr_sha256']
ENTRY_COLUMNS = ['phase','nonce','wrapper_rc','wire_len','wire_sha256','wrapper_stderr_len',
'wrapper_stderr_sha256','protected_wire']
def marker(evidence,scratch,failure):
records=[]
for entry in evidence:
row=[entry.get(name) for name in ENTRY_COLUMNS]
meta=entry.get('meta')
row.append([meta.get(name) for name in META_COLUMNS] if type(meta) is dict else meta)
records.append(row)
# Full metadata only; no wire or candidate IO bodies in the bos 4000-character tail.
return json.dumps(dict(v=1,scratch=scratch,failure=failure,stages=records),separators=(',',':'))
OBSERVER = '#define _POSIX_C_SOURCE 200809L\n#include "mode_formula.h"\n#include <inttypes.h>\n#include <stdio.h>\n#include <stdlib.h>\n\nint main(void)\n{\n mf_bank bank;\n char *line=NULL;\n size_t capacity=0;\n ssize_t length;\n mf_reset(&bank);\n while ((length=getline(&line,&capacity,stdin))>=0) {\n mf_row row;\n size_t i;\n size_t used=(size_t)length;\n if (used>0 && line[used-1]==\'\\n\') used--;\n if (!mf_dispatch(&bank,line,used,&row)) continue;\n for (i=0;i<row.length;i++) {\n if (i) putchar(\' \');\n printf("%" PRId64,row.values[i]);\n }\n putchar(\'\\n\');\n }\n free(line);\n if (ferror(stdin) || ferror(stdout)) return 2;\n return 0;\n}\n'
def integer(token):
if not re.fullmatch(rb'[+-]?[0-9]+',token): raise ValueError('decimal token')
negative=token[:1]==b'-'
magnitude=token.lstrip(b'+-').lstrip(b'0') or b'0'
if len(magnitude)>19: raise ValueError('int64 range')
value=int(magnitude)
value=-value if negative else value
if not -(1<<63)<=value<(1<<63): raise ValueError('int64 range')
return value
def oracle(lines):
# Each successful sample is immutable. State queries select the current epoch's journal.
profiles={}; generations=[0]*8; epochs=[0]*8; journal=[]; ticket=1; rows=[]
for line in lines:
words=re.split(rb'[ \t\r]+',line.strip(b' \t\r'))
if words==[b''] or words[0].startswith(b'#'): continue
try: args=[integer(x) for x in words[1:]]
except ValueError: rows.append([7,-1]); continue
op=words[0]; code={b'RESET':0,b'CONFIG':1,b'APPLY':2,b'BATCH':3,b'STATS':4,b'HISTORY':5,b'CLEAR':6,b'PROFILE':8}.get(op)
arity={0:0,1:8,2:2,4:1,5:2,6:1,8:1}
if len(words)>24 or code is None or code!=3 and len(args)!=arity[code] or code==3 and len(args)<2:
rows.append([7,-1]); continue
if code==0:
profiles={}; generations=[0]*8; epochs=[0]*8; journal=[]; ticket=1
rows.append([0,0,1]); continue
c=args[0]
if code==3:
n=args[1]
if not 0<=n<=12: rows.append([3,-2]); continue
if len(args)!=n+2: rows.append([7,-1]); continue
bad=not 0<=c<8
if code==1: bad=bad or any(not -1000<=v<=1000 for v in args[1:4]+args[5:]) or args[4] not in (0,1)
if code==2: bad=bad or not -1000<=args[1]<=1000
if code==3: bad=bad or any(not -1000<=v<=1000 for v in args[2:])
if code==5: bad=bad or args[1]<0
if bad: rows.append([code,-2]); continue
if code==1:
profiles[c]=tuple(args[1:]); generations[c]+=1; epochs[c]+=1
rows.append([1,0,c,generations[c]]); continue
live=[entry[2] for entry in journal if entry[0]==c and entry[1]==epochs[c]]
if code==4:
ys=[e[4] for e in live]
rows.append([4,0,int(c in profiles),generations[c],len(ys),sum(ys),min(ys,default=0),max(ys,default=0),live[-1][0] if live else 0]);continue
if c not in profiles: rows.append([code,-3]);continue
if code==8:
rows.append([8,0,generations[c]]+list(profiles[c]));continue
if code==6:
epochs[c]+=1; rows.append([6,0,c,generations[c]]);continue
if code==5:
offset=args[1]
if offset>=min(16,len(live)): rows.append([5,-4])
else: rows.append([5,0]+list(live[-1-offset]))
continue
xs=[args[1]] if code==2 else args[2:]
gate,base,gain,mode,shift,scale,pivot=profiles[c]
raw=[base+gain*x if gate else 0 for x in xs]
ys=[y if mode==0 else shift+scale*(y-pivot) for y in raw]
events=[(ticket+i,generations[c],x,r,y) for i,(x,r,y) in enumerate(zip(xs,raw,ys))]
journal.extend((c,epochs[c],event) for event in events);ticket+=len(events)
if code==2: rows.append([2,0,events[0][0],raw[0],ys[0]])
else: rows.append([3,0,len(xs),events[0][0] if events else 0,sum(ys),min(ys,default=0),max(ys,default=0)])
return rows
def generate():
lines=[b'RESET',b'STATS 0',b'APPLY 0 2',b'HISTORY 0 0',b'CLEAR 0',
b'CONFIG 0 1 5 2 0 0 1 0',b'APPLY 0 3',b'APPLY 0 -4',
b'CONFIG 1 0 5 2 0 0 1 0',b'BATCH 1 3 -2 0 2',
b'CONFIG 3 -1 4 2 1 7 3 5',b'BATCH 3 4 -2 0 2 5',b'STATS 3',b'HISTORY 3 1']
def add(*parts): lines.append(' '.join(map(str,parts)).encode())
for c in range(8):
add('CONFIG',c,[-1000,-1,0,1,1000][c%5],1000-c,-1000+c,c%2,-7,[-3,0,1,4][c%4],9)
for _ in range(3): add('BATCH',c,12,*range(-6,6))
for offset in (0,1,15,16,36,-1): add('HISTORY',c,offset)
add('PROFILE',c);add('STATS',c);add('BATCH',c,0);add('STATS',c)
add('BATCH',c,3,1,1001,2);add('STATS',c);add('HISTORY',c,0)
add('CLEAR',c);add('HISTORY',c,0);add('STATS',c);add('APPLY',c,-1000)
add('CONFIG',c,0,1000,1000,1,-1000,1000,1000);add('APPLY',c,1000);add('STATS',c)
rng=random.Random(165031)
for i in range(650):
c=rng.randrange(-1,9);op=rng.randrange(9)
if op<2: add('CONFIG',c,rng.choice([-1000,-1,0,1,1000]),rng.randrange(-1000,1001),rng.randrange(-1000,1001),rng.randrange(2),rng.randrange(-1000,1001),rng.randrange(-1000,1001),rng.randrange(-1000,1001))
elif op<4: add('APPLY',c,rng.choice([-1001,-1000,0,1000,1001,rng.randrange(-1000,1001)]))
elif op==4:
n=rng.randrange(13);add('BATCH',c,n,*[rng.randrange(-1000,1001) for _ in range(n)])
elif op==5: add('CLEAR',c)
elif op==6: add('HISTORY',c,rng.choice([-1,0,1,15,16,1000]))
else: add('STATS',c)
lines.extend([b'PROFILE -1',b'PROFILE 8',b'PROFILE 1',b'RESET',b'PROFILE 0',b'CONFIG 0 +0001 -0 0002 0 +0 +01 -00',b'APPLY +0000 -0003',
b'BATCH 0 -0',b'BATCH 0 13',b'BATCH 0 2 1',b'BATCH 0 -1 2',b'BATCH 0 1 1 2',
b'CONFIG 0 1 2 3 2 4 5 6',b'CONFIG 0 1001 2 3 0 4 5 6',b'STATS 0',
b'APPLY 0 1_0',b'APPLY 0 0x10',b'APPLY 0 +',b'APPLY 0 9223372036854775808',
b'APPLY 0 -9223372036854775808',b'APPLY\v0 1',b'APPLY 0\f1',b'apPLY 0 1',b'RESET 1',
b'\t#ignored words\vanything',b' \t\r',b'STATS 0\x00',b'APPLY\t0\r-0',b'STATS 0'])
z=b'0'*5000
lines.extend([b'APPLY '+z+b' +'+z,b'APPLY -'+z+b' -'+z,b'STATS +'+z])
return lines
def parse_output(data):
if data and not data.endswith(b'\n'): raise ValueError('stdout missing LF')
rows=[]
for line in data.split(b'\n')[:-1]:
words=re.split(rb'[ \t\r]+',line.strip(b' \t\r'))
rows.append([integer(word) for word in words])
return rows
def run_check(candidate):
evidence=[];scratch=None;failure=None;matched=False;clean=False;absent=False;errors=[]
try:
scratch=tempfile.mkdtemp(prefix='mf-scratch-')
work=Path(scratch)/'build';work.mkdir(mode=0o755)
if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534': os.chown(work,65534,65534)
# Every support/source/input is prepared before the first untrusted child.
for name in ('mode_formula.h','engine.c','protocol.c'):
shutil.copyfile(candidate/name,work/name)
(work/'observer.c').write_text(OBSERVER)
lines=generate();payload=b'\n'.join(lines)+b'\n';expected=oracle(lines)
execute_stage('compiler',['gcc','-std=c11','-O2','engine.c','protocol.c','observer.c','-o','app'],work,20,b'',evidence)
out,err=execute_stage('candidate',['./app'],work,8,payload,evidence)
actual=parse_output(out)
if actual!=expected:
difference=next((i for i,(a,b) in enumerate(zip(actual,expected)) if a!=b),min(len(actual),len(expected)))
raise ValueError('raw observation difference row '+str(difference))
matched=True
except BaseException as error:
errors.append([type(error).__name__,str(error)])
failure='check exception: '+type(error).__name__+': '+str(error)
finally:
if scratch is not None:
try:
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,scratch],'/tmp',5,b'',evidence)
clean=True
except BaseException as error:
errors.append([type(error).__name__,str(error)])
failure='cleanup exception: '+type(error).__name__+': '+str(error)
try:
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,scratch],'/tmp',5,b'',evidence)
absent=True
except BaseException as error:
errors.append([type(error).__name__,str(error)])
failure='absence exception: '+type(error).__name__+': '+str(error)
success=matched and clean and absent and not errors
print('MF_EVIDENCE '+marker(evidence,scratch,failure),file=sys.stderr)
print('PASS' if success else 'FAIL '+str(failure))
return 0 if success else 1
if __name__=='__main__':
sys.exit(run_check(Path(sys.argv[1])))