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

410 lines
21 KiB
Python

"""Independent resource lifecycle oracle; candidate code runs only in supervised children."""
import base64
import hashlib
import json
import os
from pathlib import Path
import random
import subprocess
import sys
import tempfile
import time
import uuid
import zlib
COMPILE_DEADLINE = 25
CANDIDATE_DEADLINE = 15
CLEANUP_DEADLINE = 8
ABSENCE_DEADLINE = 8
CANDIDATES = ['src/cache.cpp']
# Pure execution supervisor: no cache contract, expected observations or grader reads.
SUPERVISOR = r'''
import base64,ctypes,hashlib,json,os,signal,subprocess,sys,time
argv,limit,encoded,nonce,phase=json.loads(sys.argv[1]),float(sys.argv[2]),sys.argv[3],sys.argv[4],sys.argv[5]
started=time.monotonic()
errors=[]
seen={}
reaped=[]
leader=None
leader_rc=None
leader_start=None
output=b''
diagnostic=b''
timeout=False
leader_reaped=False
def proc(pid):
try:
raw=open('/proc/%d/stat'%pid).read()
fields=raw[raw.rfind(')')+2:].split()
return {'pid':pid,'ppid':int(fields[1]),'pgid':int(fields[2]),'start':int(fields[19]),'state':fields[0]}
except FileNotFoundError:
return None
except ProcessLookupError:
return None
def scan():
allp={}
for name in os.listdir('/proc'):
if name.isdigit():
item=proc(int(name))
if item: allp[item['pid']]=item
family={os.getpid()}
changed=True
while changed:
changed=False
for pid,item in allp.items():
if item['ppid'] in family and pid not in family:
family.add(pid);changed=True
children=[allp[pid] for pid in family if pid!=os.getpid() and pid in allp]
for item in children: seen[(item['pid'],item['start'])]=item
return children,allp
def kill_owned(children):
for item in children:
live=proc(item['pid'])
if live and live['start']==item['start'] and live['state']!='Z':
try: os.kill(item['pid'],signal.SIGKILL)
except ProcessLookupError: pass
except BaseException as error: errors.append('kill:'+type(error).__name__+':'+str(error))
def reap_adopted():
# Never steal Popen's real leader wait status.
if not leader_reaped: return
while True:
try:
pid,status=os.waitpid(-1,os.WNOHANG)
if pid==0: return
reaped.append([pid,status])
except ChildProcessError: return
except BaseException as error:
errors.append('reap:'+type(error).__name__+':'+str(error));return
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(argv,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
live=proc(leader.pid)
if not live: raise RuntimeError('leader identity unavailable')
leader_start=live['start']
seen[(live['pid'],live['start'])]=live
pending=base64.b64decode(encoded)
finished=False
while not finished:
children,_=scan()
leader_rc=leader.poll()
if leader_rc is not None: leader_reaped=True
if time.monotonic()-started>=limit and leader_rc is None: timeout=True
if timeout or leader_reaped: kill_owned(children)
try:
output,diagnostic=leader.communicate(input=pending,timeout=0.04)
pending=None
leader_rc=leader.returncode
leader_reaped=True
finished=True
except subprocess.TimeoutExpired:
pending=None
reap_adopted()
# Re-parented setsid/closed-stdio descendants may survive communicate().
end=time.monotonic()+2.0
while True:
children,_=scan()
kill_owned(children)
reap_adopted()
children,allp=scan()
tracked=[item for item in allp.values() if (item['pid'],item['start']) in seen]
if not children and not tracked: break
if time.monotonic()>end:
errors.append('descendants_not_absent');break
time.sleep(0.01)
except BaseException as error:
errors.append('execute:'+type(error).__name__+':'+str(error))
finally:
try:
children,_=scan();kill_owned(children)
if leader is not None and not leader_reaped:
try:
output,diagnostic=leader.communicate(timeout=2)
leader_rc=leader.returncode;leader_reaped=True
except BaseException as error: errors.append('finalwait:'+type(error).__name__+':'+str(error))
reap_adopted()
children,allp=scan()
descendants_absent=not children and not any((p['pid'],p['start']) in seen for p in allp.values())
group_absent=leader is not None and not any(p['pgid']==leader.pid for p in allp.values())
except BaseException as error:
errors.append('absence:'+type(error).__name__+':'+str(error));descendants_absent=False;group_absent=False
meta={'phase':phase,'nonce':nonce,'argv':argv,'deadline':limit,'elapsed':time.monotonic()-started,
'pid':os.getpid(),'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),
'leader_pid':None if leader is None else leader.pid,'leader_start':leader_start,
'leader_rc':leader_rc,'returncode':124 if timeout else leader_rc,'leader_reaped':leader_reaped,'timeout':timeout,
'pg_absent':group_absent,'descendants_absent':descendants_absent,
'seen':[[k[0],k[1],v['ppid'],v['pgid']] for k,v in sorted(seen.items())],
'adopted_reaped':reaped,'exceptions':errors,
'stdout_len':len(output),'stdout_sha256':hashlib.sha256(output).hexdigest(),
'stderr_len':len(diagnostic),'stderr_sha256':hashlib.sha256(diagnostic).hexdigest()}
print(json.dumps({'meta':meta,'stdout':base64.b64encode(output).decode(),'stderr':base64.b64encode(diagnostic).decode()},separators=(',',':')))
'''
def run_stage(phase, argv, deadline, payload=b''):
nonce = uuid.uuid4().hex
command = [sys.executable, '-I', '-c', SUPERVISOR, json.dumps(argv), str(deadline),
base64.b64encode(payload).decode(), nonce, phase]
completed = subprocess.run(command, capture_output=True)
if completed.returncode != 0:
raise RuntimeError('supervisor rc=%s phase=%s' % (completed.returncode, phase))
wire = json.loads(completed.stdout.decode('utf-8', 'strict'))
if type(wire) is not dict or set(wire) != {'meta', 'stdout', 'stderr'}:
raise RuntimeError('missing wire fields')
meta = wire['meta']
output = base64.b64decode(wire['stdout'], validate=True)
stderr = base64.b64decode(wire['stderr'], validate=True)
required = {'phase','nonce','argv','deadline','elapsed','pid','uid','gid','groups','leader_pid',
'leader_start','leader_rc','returncode','leader_reaped','timeout','pg_absent','descendants_absent',
'seen','adopted_reaped','exceptions','stdout_len','stdout_sha256','stderr_len','stderr_sha256'}
if type(meta) is not dict or set(meta) != required:
raise RuntimeError('missing metadata fields')
identity = (65534, 65534, []) if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else (os.getuid(), os.getgid(), os.getgroups())
if (meta['phase'],meta['nonce'],meta['argv'],meta['deadline']) != (phase,nonce,argv,deadline):
raise RuntimeError('metadata binding')
if (meta['uid'],meta['gid'],meta['groups']) != identity:
raise RuntimeError('metadata identity')
for field in ['pid','leader_pid','leader_start','leader_rc','returncode','stdout_len','stderr_len']:
if type(meta[field]) is not int: raise RuntimeError('metadata integer '+field)
if min(meta['pid'],meta['leader_pid'],meta['leader_start']) <= 0:
raise RuntimeError('metadata process identity')
if meta['returncode'] != (124 if meta['timeout'] else meta['leader_rc']):
raise RuntimeError('metadata returncode consistency')
if type(meta['elapsed']) not in (int,float) or meta['elapsed'] < 0 or meta['elapsed'] > deadline + 6:
raise RuntimeError('metadata elapsed')
for field in ['leader_reaped','timeout','pg_absent','descendants_absent']:
if type(meta[field]) is not bool: raise RuntimeError('metadata boolean '+field)
if not meta['leader_reaped'] or not meta['pg_absent'] or not meta['descendants_absent']:
raise RuntimeError('process absence/reap incomplete')
if type(meta['exceptions']) is not list or meta['exceptions']:
raise RuntimeError('supervisor exceptions '+repr(meta['exceptions']))
if type(meta['seen']) is not list or not any(item[:2] == [meta['leader_pid'],meta['leader_start']] for item in meta['seen']):
raise RuntimeError('leader tracking absent')
if any(type(item) is not list or len(item)!=4 or any(type(v) is not int for v in item) for item in meta['seen']):
raise RuntimeError('invalid tracking')
if type(meta['adopted_reaped']) is not list or any(type(item) is not list or len(item)!=2 or any(type(v) is not int for v in item) for item in meta['adopted_reaped']):
raise RuntimeError('invalid adopted reap')
if (meta['stdout_len'],meta['stdout_sha256'],meta['stderr_len'],meta['stderr_sha256']) != (
len(output),hashlib.sha256(output).hexdigest(),len(stderr),hashlib.sha256(stderr).hexdigest()):
raise RuntimeError('raw IO binding')
return meta, output, stderr
class Oracle:
def __init__(self):
self.source = {}
self.caches = [dict(cap=3,data={},lru=[],stats=[0]*5),dict(cap=2,data={},lru=[],stats=[0]*5)]
self.copies = [None]*3
self.limit = 1048576
self.commands, self.expected, self.clauses = [], [], []
@staticmethod
def key(raw):
parts=raw.split('/')
if len(parts)!=2 or any(not 1<=len(p)<=24 or any(c not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-' for c in p) for p in parts):
return None
return raw.lower()
def accounting(self):
values=[v[1] for c in self.caches for v in c['data'].values()]+[v[1] for v in self.copies if v is not None]
return [len(values),sum(len(v)+1 for v in values),0]
def allocates(self, raw):
return self.accounting()[1]+len(raw)+1 <= self.limit
def remove(self, cache, key):
cache['data'].pop(key)
cache['lru'].remove(key)
def get(self, index, raw):
c=self.caches[index];c['stats'][0]+=1;k=self.key(raw)
if k is None: return [1,'',0]
current=self.source.get(k,[0,b'',False]);old=c['data'].get(k)
if not current[2]:
if old is not None: self.remove(c,k)
c['stats'][2]+=1;return [2,'',0]
if old is not None and old[0]==current[0]:
c['stats'][1]+=1
else:
c['stats'][2]+=1
if not self.allocates(current[1]): return [3,'',0]
if old is not None:
c['stats'][3]+=1
elif len(c['data'])==c['cap']:
self.remove(c,c['lru'][0]);c['stats'][4]+=1
c['data'][k]=current[:2]
if k in c['lru']: c['lru'].remove(k)
c['lru'].append(k)
return [0,current[1].hex(),current[0]]
def owned(self,index):
value=self.copies[index]
return [False,'',0] if value is None else [True,value[1].hex(),value[0]]
def add(self,command,clause):
fields=command.split();op=fields[0];result=None
if op in ['PUT','ERASE']:
key=self.key(fields[1])
if key is None: result=[1,0]
else:
old=self.source.get(key,[0,b'',False])
if op=='PUT': self.source[key]=[old[0]+1,b'' if fields[2]=='-' else bytes.fromhex(fields[2]),True];status=0
elif old[2]: self.source[key]=[old[0]+1,b'',False];status=0
else: status=2
result=[status,self.source.get(key,[0])[0]]
elif op=='GET': result=self.get(int(fields[1]),fields[2])
elif op=='SNAP':
ci,si=int(fields[1]),int(fields[2]);value=self.get(ci,fields[3]);status=value[0]
if status==0:
raw=bytes.fromhex(value[1])
if self.allocates(raw): self.copies[si]=[value[2],raw]
else: status=3
result=[status,self.owned(si)]
elif op=='OWNED': result=self.owned(int(fields[1]))
elif op=='MOVE':
src,dst=map(int,fields[1:])
if src!=dst: self.copies[dst],self.copies[src]=self.copies[src],None
result=[self.owned(src),self.owned(dst)]
elif op=='DROP': self.copies[int(fields[1])]=None;result=self.accounting()
elif op=='LIMIT': self.limit=int(fields[1]);result=self.accounting()
elif op=='END':
for c in self.caches: c['data'].clear();c['lru'].clear()
self.copies=[None]*3;result=self.accounting()
else:
c=self.caches[int(fields[1])]
if op=='STATE': result=[[[k,v[0],v[1].hex()] for k,v in sorted(c['data'].items())],c['lru'][:],c['stats'][:],self.accounting()]
elif op=='CLEAR': c['data'].clear();c['lru'].clear();result=self.accounting()
elif op=='RESET': c.update(cap=int(fields[2]),data={},lru=[],stats=[0]*5);result=self.accounting()
elif op=='INVALIDATE':
k=self.key(fields[2]);status=1 if k is None else 2 if k not in c['data'] else 0
if status==0: self.remove(c,k)
result=[status]
elif op=='RESIZE':
size=int(fields[2]);result=[0 if 1<=size<=8 else 4]
if result==[0]:
c['cap']=size
while len(c['data'])>size: self.remove(c,c['lru'][0]);c['stats'][4]+=1
elif op=='PRUNE':
keys=[k for k,v in c['data'].items() if not self.source.get(k,[0,b'',False])[2] or self.source[k][0]!=v[0]]
for k in keys: self.remove(c,k)
result=[len(keys)]
elif op=='NAMESPACE':
k=self.key(fields[2]+'/x')
if k is None: result=[1,0]
else:
prefix=k[:-1];keys=[key for key in c['data'] if key.startswith(prefix)]
for key in keys: self.remove(c,key)
result=[0,len(keys)]
else: raise ValueError(op)
self.commands.append(command)
self.expected.append(json.loads(json.dumps(result)))
self.clauses.append(clause)
def cases():
c=Oracle();rng=random.Random(2401026)
def add(command,clause='C04'): c.add(command,clause)
for command in ['PUT demo/name 6361636865642d76616c7565','GET 0 demo/name','GET 0 demo/name','CLEAR 0','STATE 0']:
add(command,'C07')
keys=['cfg/a','cfg/b','cfg/c','other/a','A/B','z/empty','x.'+'a'*22+'/'+'q'*24]
for i,key in enumerate(keys):
raw=[b'',b'\0',bytes(range(256)),b'hello',b'old',b'a\0b',b'tail'][i]
add('PUT %s %s'%(key,raw.hex() or '-'),'C03');add('GET 0 '+key);add('GET 1 '+key)
for key in ['a','a/','/b','a/b/c','a'*25+'/b','a/'+ 'b'*25,'a@/b','a/!']:
add('PUT '+key+' 61','C02');add('GET 0 '+key,'C02');add('INVALIDATE 0 '+key,'C02')
for command in ['PUT cfg/a 6f6c64','GET 0 cfg/a','SNAP 0 0 cfg/a','PUT CFG/A 6e657700ff','GET 0 cfg/a','GET 1 cfg/a','OWNED 0','SNAP 0 1 cfg/a','MOVE 0 2','MOVE 2 2','MOVE 2 1','CLEAR 0','OWNED 1','DROP 1','RESET 1 1']:
add(command,'C08')
for command in ['PUT revision/same 61','GET 0 revision/same','PUT revision/same 61','GET 0 revision/same','ERASE revision/same','ERASE revision/same','GET 0 revision/same','PUT revision/same 62','GET 0 revision/same','SNAP 0 2 z/empty','OWNED 2','CLEAR 0','CLEAR 0','OWNED 2','DROP 2']:
add(command,'C03/C07/C08')
# Atomic allocation failures: existing live bytes count before releases.
for command in ['CLEAR 0','CLEAR 1','DROP 0','DROP 2','LIMIT 3','PUT b/k 6162','GET 0 b/k','PUT b/k 63','GET 0 b/k','STATE 0','SNAP 0 0 b/k','LIMIT 5','GET 0 b/k','STATE 0','SNAP 0 0 b/k','SNAP 0 0 b/k','OWNED 0','LIMIT 0','GET 0 b/k','PUT b/new -','GET 0 b/new','ERASE b/k','GET 0 b/k','OWNED 0','DROP 0','LIMIT 1048576']:
add(command,'C05/C08/C09')
for capacity in range(1,9):
add('RESET 0 '+str(capacity),'C12')
for i in range(10):
add('PUT cap/k%d %02x'%(i,i),'C03');add('GET 0 cap/k%d'%i);add('STATE 0','C05/C06')
add('RESIZE 0 1','C12');add('STATE 0','C12');add('RESIZE 0 8','C12');add('RESIZE 0 0','C12');add('RESIZE 0 9','C12')
for command in ['GET 0 cfg/a','GET 0 other/a','PUT cfg/a 78','PRUNE 0','STATE 0','GET 0 cfg/b','GET 0 cfg/c','NAMESPACE 0 CFG','STATE 0','NAMESPACE 0 bad/slash','NAMESPACE 1 absent']:
add(command,'C12/C13')
for i in range(250):
cache=rng.randrange(2);key=rng.choice(keys)
op=rng.choice(['PUT','GET','GET','SNAP','ERASE','INVALIDATE','CLEAR','STATE','PRUNE','RESIZE','NAMESPACE','LIMIT','OWNED','MOVE','DROP'])
if op=='PUT': command='PUT %s %s'%(key,bytes(rng.randrange(256) for _ in range(rng.choice([0,1,2,7,40,4096]))).hex() or '-')
elif op in ['GET','INVALIDATE']: command='%s %d %s'%(op,cache,key)
elif op=='ERASE': command='ERASE '+key
elif op=='SNAP': command='SNAP %d %d %s'%(cache,rng.randrange(3),key)
elif op=='RESIZE': command='RESIZE %d %d'%(cache,rng.randrange(10))
elif op=='NAMESPACE': command='NAMESPACE %d %s'%(cache,rng.choice(['cfg','other','a','x','bad/slash']))
elif op=='LIMIT': command='LIMIT '+str(rng.choice([0,1,2,20,100,1048576]))
elif op in ['OWNED','DROP']: command=op+' '+str(rng.randrange(3))
elif op=='MOVE': command='MOVE %d %d'%(rng.randrange(3),rng.randrange(3))
else: command=op+' '+str(cache)
add(command,'C02-C13');add('STATE '+str(cache),'C05/C06/C09')
for command in ['LIMIT 1048576','PUT end/live 000102ff','GET 0 end/live','SNAP 0 0 end/live','GET 1 end/live','END']:
add(command,'C07/C08')
return c
def main():
evidence=[];failures=[];scratch=None
try:
matrix=cases()
context=json.loads((Path(__file__).resolve().parent/'public-context.json').read_text())
candidate={name:(Path(sys.argv[1])/name).read_bytes() for name in CANDIDATES}
payload=('\n'.join(matrix.commands)+'\n').encode()
scratch=Path(tempfile.mkdtemp(prefix='rkcache-'))
for name,text in context.items():
path=scratch/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_text(text)
for name,raw in candidate.items():
path=scratch/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_bytes(raw)
# Only newly-created, controller-owned children are chmodded before first child.
for path in [scratch/'include',scratch/'src',scratch/'docs']:
path.chmod(0o755)
if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534': os.chown(path,65534,65534)
compiler=['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic','-I'+str(scratch/'include'),
*[str(scratch/'src'/name) for name in ['keys.cpp','store.cpp','pool.cpp','cache.cpp','observe.cpp']],'-o',str(scratch/'observe')]
meta,_,stderr=run_stage('compiler',compiler,COMPILE_DEADLINE)
evidence.append(meta)
if meta['timeout'] or meta['leader_rc']:
failures.append('compiler controlled timeout' if meta['timeout'] else 'compiler rc=%d'%meta['leader_rc'])
else:
meta,stdout,stderr=run_stage('candidate',[str(scratch/'observe')],CANDIDATE_DEADLINE,payload)
evidence.append(meta)
if meta['timeout'] or meta['leader_rc']:
failures.append('candidate controlled timeout' if meta['timeout'] else 'candidate rc=%d'%meta['leader_rc'])
else:
lines=stdout.decode('utf-8','strict').splitlines()
if len(lines)!=len(matrix.expected): failures.append('stdout row count %d expected %d'%(len(lines),len(matrix.expected)))
else:
for i,(line,wanted) in enumerate(zip(lines,matrix.expected)):
actual=json.loads(line)
if json.dumps(actual,separators=(',',':'))!=json.dumps(wanted,separators=(',',':')):
failures.append('behavior row=%d clause=%s observed=%s expected=%s'%(i,matrix.clauses[i],repr(actual)[:180],repr(wanted)[:180]));break
except BaseException as error:
failures.append('check exception '+type(error).__name__+':'+str(error))
finally:
if scratch is not None:
for phase,argv,deadline in [
('cleanup',[sys.executable,'-I','-c','import shutil,sys;shutil.rmtree(sys.argv[1])',str(scratch)],CLEANUP_DEADLINE),
('absence',[sys.executable,'-I','-c','import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nsys.exit(1)',str(scratch)],ABSENCE_DEADLINE)]:
try:
meta,_,_=run_stage(phase,argv,deadline)
evidence.append(meta)
if meta['timeout'] or meta['leader_rc']!=0: failures.append(phase+' unsuccessful rc=%s timeout=%s'%(meta['leader_rc'],meta['timeout']))
except BaseException as error:
failures.append(phase+' exception '+type(error).__name__+':'+str(error))
# Compact lossless metadata survives bos_verify's 4000-character stderr tail.
packed=base64.b85encode(zlib.compress(json.dumps(evidence,separators=(',',':')).encode(),9)).decode()
print('RK_EVIDENCE '+packed,file=sys.stderr)
print('FAILED '+ '; '.join(failures) if failures else 'PASS')
return 1 if failures else 0
if __name__=='__main__':
raise SystemExit(main())