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

359 lines
19 KiB
Python

"""Independent packet-spool model and same-identity execution supervisor."""
import base64
import hashlib
import json
import math
import os
from pathlib import Path
import random
import subprocess
import sys
import tempfile
import time
import uuid
import zlib
# Pure execution plumbing. It receives no expected behavior or assertions.
SUPERVISOR = r'''
import base64,ctypes,hashlib,json,os,selectors,signal,subprocess,sys,time
request=json.loads(sys.argv[1]); began=time.monotonic()
meta=dict(phase=request['phase'],nonce=request['nonce'],argv=request['argv'],deadline=request['deadline'],uid=os.getuid(),gid=os.getgid(),groups=os.getgroups(),pid=os.getpid(),start=None,leader=None,leader_rc=None,leader_reaped=False,timed_out=False,pg_absent=False,descendants_absent=False,tracked=[],adopted=[],errors=[])
out=bytearray();err=bytearray();process=None;known={};adopted=[]
def table():
result={}
for name in os.listdir('/proc'):
if not name.isdigit(): continue
try:
raw=open('/proc/'+name+'/stat','rb').read();tail=raw[raw.rfind(b')')+2:].split()
result[int(name)]=(int(tail[1]),int(tail[2]),int(tail[19]),tail[0].decode('ascii'))
except (FileNotFoundError,ProcessLookupError): pass
return result
def token(pid,rows):
row=rows.get(pid)
return (pid,row[2]) if row else None
def closure(rows):
# Dead/reused tracked PIDs never become ancestor roots.
roots={os.getpid()}
roots.update(pid for pid,start in known.items() if token(pid,rows)==(pid,start))
found=set()
change=True
while change:
change=False
for pid,row in rows.items():
if pid!=os.getpid() and row[0] in roots and pid not in roots:
roots.add(pid);found.add(pid);change=True
for pid in found:
known[pid]=rows[pid][2]
return found
def observe():
rows=table();closure(rows)
return rows
def poll_leader():
if process is not None:
code=process.poll()
if code is not None:
meta['leader_rc']=code;meta['leader_reaped']=True
def reap_adopted(rows):
# Popen owns its leader status. Never call waitpid(-1).
poll_leader()
for pid,start in list(known.items()):
if process is not None and pid==process.pid: continue
row=rows.get(pid)
if row is None or row[2]!=start or row[0]!=os.getpid(): continue
try:
waited,status=os.waitpid(pid,os.WNOHANG)
if waited:
adopted.append([pid,start,status])
except ChildProcessError: pass
def kill_owned(rows):
for pid,start in list(known.items()):
if token(pid,rows)!=(pid,start): continue
try: os.kill(pid,signal.SIGKILL)
except ProcessLookupError: pass
except BaseException as error: meta['errors'].append('signal:'+type(error).__name__+':'+str(error))
def drain(selector):
for key,_ in selector.select(0.01):
try: data=os.read(key.fd,65536)
except BlockingIOError: continue
if not data:
selector.unregister(key.fileobj);key.fileobj.close()
elif key.data=='stdout': out.extend(data)
else: err.extend(data)
try:
if ctypes.CDLL(None,use_errno=True).prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')
rows=table();meta['start']=rows[os.getpid()][2]
process=subprocess.Popen(request['argv'],cwd=request.get('cwd'),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
rows=observe();leader=token(process.pid,rows)
if leader is None: raise RuntimeError('leader identity unavailable')
known[process.pid]=leader[1];meta['leader']=list(leader)
selector=selectors.DefaultSelector()
for stream,label in ((process.stdout,'stdout'),(process.stderr,'stderr')):
os.set_blocking(stream.fileno(),False);selector.register(stream,selectors.EVENT_READ,label)
data=base64.b64decode(request.get('stdin',''));offset=0
os.set_blocking(process.stdin.fileno(),False)
deadline=began+request['deadline']
while True:
rows=observe();poll_leader();reap_adopted(rows)
if process.stdin is not None:
try:
if offset<len(data): offset+=os.write(process.stdin.fileno(),data[offset:offset+65536])
if offset==len(data): process.stdin.close();process.stdin=None
except BrokenPipeError: process.stdin.close();process.stdin=None
drain(selector)
if meta['leader_reaped'] and not selector.get_map():
kill_owned(rows)
rows=observe();reap_adopted(rows)
live=[pid for pid,start in known.items() if token(pid,rows)==(pid,start)]
if not live and not selector.get_map(): break
if time.monotonic()>=deadline:
meta['timed_out']=True;break
if meta['timed_out']:
end=time.monotonic()+3
while time.monotonic()<end:
rows=observe();kill_owned(rows);poll_leader();reap_adopted(rows);drain(selector)
rows=observe()
if meta['leader_reaped'] and not any(token(pid,rows)==(pid,start) for pid,start in known.items()) and not selector.get_map(): break
poll_leader()
if meta['leader_reaped']: process.wait(timeout=0)
rows=observe();reap_adopted(rows);rows=observe()
remaining=[[pid,start] for pid,start in known.items() if token(pid,rows)==(pid,start)]
meta['descendants_absent']=not remaining
pg=process.pid
meta['pg_absent']=not any(row[1]==pg for row in rows.values())
if remaining: meta['errors'].append('remaining:'+repr(remaining))
except BaseException as error:
meta['errors'].append(type(error).__name__+':'+str(error))
finally:
if process is not None:
try:
rows=observe();kill_owned(rows);poll_leader();reap_adopted(rows)
except BaseException as error: meta['errors'].append('finally:'+type(error).__name__+':'+str(error))
meta['tracked']=[[pid,start] for pid,start in sorted(known.items())]
meta['adopted']=adopted
meta['elapsed']=round(time.monotonic()-began,6)
meta['stdout_len']=len(out);meta['stderr_len']=len(err)
meta['stdout_sha']=hashlib.sha256(out).hexdigest();meta['stderr_sha']=hashlib.sha256(err).hexdigest()
code=124 if meta['timed_out'] else 125 if meta['errors'] else meta['leader_rc']
wire={'meta':meta,'stdout':base64.b64encode(out).decode(),'stderr':base64.b64encode(err).decode()}
print(json.dumps(wire,separators=(',',':')))
sys.exit(code if isinstance(code,int) and 0<=code<=255 else 125)
'''
CLEANUP = 'import shutil,sys; shutil.rmtree(sys.argv[1])'
ABSENCE = '''import os,sys
try: os.lstat(sys.argv[1])
except FileNotFoundError: raise SystemExit(0)
raise SystemExit(1)
'''
DEADLINES = dict(compile=35,candidate=20,cleanup=12,absence=5)
REQUIRED = {'phase','nonce','argv','deadline','uid','gid','groups','pid','start','leader','leader_rc','leader_reaped','timed_out','pg_absent','descendants_absent','tracked','adopted','errors','elapsed','stdout_len','stderr_len','stdout_sha','stderr_sha'}
def digest(data):
return hashlib.sha256(data).hexdigest()
def execute(phase, argv, cwd=None, data=b''):
request=dict(phase=phase,nonce=uuid.uuid4().hex,argv=argv,cwd=cwd,deadline=DEADLINES[phase],stdin=base64.b64encode(data).decode())
completed=subprocess.run([sys.executable,'-I','-c',SUPERVISOR,json.dumps(request,separators=(',',':'))],capture_output=True,timeout=DEADLINES[phase]+7)
# Preserve observed wrapper rc and exact wire-byte binding BEFORE parsing.
observed=dict(phase=phase,nonce=request['nonce'],wrapper_rc=completed.returncode,wire_len=len(completed.stdout),wire_sha=digest(completed.stdout),wrapper_stderr_len=len(completed.stderr),wrapper_stderr_sha=digest(completed.stderr))
records.append(observed)
wire=json.loads(completed.stdout)
meta=wire['meta'];observed['meta']=meta
stdout=base64.b64decode(wire['stdout'],validate=True);stderr=base64.b64decode(wire['stderr'],validate=True)
if not isinstance(meta,dict) or not REQUIRED<=meta.keys(): raise RuntimeError('missing metadata')
if meta['phase']!=phase or meta['nonce']!=request['nonce'] or meta['argv']!=argv or meta['deadline']!=DEADLINES[phase]: raise RuntimeError('request binding')
identity=(65534,65534,[]) if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534' else (os.getuid(),os.getgid(),os.getgroups())
if (meta['uid'],meta['gid'],meta['groups'])!=identity: raise RuntimeError('execution identity')
if not all(meta[k] is True for k in ('leader_reaped','pg_absent','descendants_absent')): raise RuntimeError('incomplete reaping')
if meta['errors'] != [] or meta['timed_out'] is not False or completed.returncode!=0 or meta['leader_rc']!=0 or completed.stderr: raise RuntimeError('controlled execution failure '+phase)
if not isinstance(meta['leader'],list) or len(meta['leader'])!=2 or not all(type(v) is int and v>0 for v in meta['leader']): raise RuntimeError('leader identity')
if type(meta['pid']) is not int or meta['pid']<=0 or type(meta['start']) is not int or meta['start']<=0 or meta['pid']==meta['leader'][0]: raise RuntimeError('supervisor identity')
if any(type(meta[k]) is not int for k in ('uid','gid','deadline','leader_rc','stdout_len','stderr_len')): raise RuntimeError('integer metadata')
if not isinstance(meta['tracked'],list) or not meta['tracked'] or any(not isinstance(t,list) or len(t)!=2 or any(type(v) is not int or v<=0 for v in t) for t in meta['tracked']): raise RuntimeError('tracked identities')
tracked={tuple(t) for t in meta['tracked']}
if len(tracked)!=len(meta['tracked']) or tuple(meta['leader']) not in tracked or (meta['pid'],meta['start']) in tracked: raise RuntimeError('tracked binding')
if not isinstance(meta['adopted'],list) or any(not isinstance(t,list) or len(t)!=3 or any(type(v) is not int for v in t) or tuple(t[:2]) not in tracked or t[:2]==meta['leader'] for t in meta['adopted']): raise RuntimeError('adopted identities')
if type(meta['elapsed']) not in (int,float) or not math.isfinite(meta['elapsed']) or not 0<=meta['elapsed']<=DEADLINES[phase]+5: raise RuntimeError('elapsed')
if meta['stdout_len']!=len(stdout) or meta['stderr_len']!=len(stderr) or meta['stdout_sha']!=digest(stdout) or meta['stderr_sha']!=digest(stderr): raise RuntimeError('raw IO binding')
return stdout
def packet_hash(packet):
ident,priority,deadline,data=packet
result=2166136261
stream=ident.to_bytes(4,'little')+bytes([priority])+deadline.to_bytes(4,'little')+data
for value in stream:
result=((result^value)*16777619)&0xffffffff
return result
class Model:
def __init__(self):
self.lanes=[[],[],[]];self.cap=[8]*3;self.stats=[0]*7;self.memory=[0]*8;self.countdown=0
def allocate(self,size,kind):
m=self.memory;m[0]+=1
if self.countdown:
self.countdown-=1
if not self.countdown:
m[3]+=1;return False
m[1]+=1;m[5+kind]+=1;m[7]+=size
return True
def release(self,packet):
m=self.memory;m[2]+=2;m[5]-=1;m[6]-=1;m[7]-=65+len(packet[3])
def clear(self,lane):
count=len(self.lanes[lane])
for packet in self.lanes[lane]: self.release(packet)
self.lanes[lane]=[];self.stats[3]+=count
return count
def command(self,line):
t=line.split();command=t[0];a=t[1:];rc=-1;packet=None
def numeric(text,maximum):
if not text or any(c<'0' or c>'9' for c in text): raise ValueError()
value=int(text)
if value>maximum: raise ValueError()
return value
try:
sizes=dict(P=5,O=1,K=1,C=1,E=2,M=3,L=2,F=1,X=0)
if command not in sizes or len(a)!=sizes[command]: raise ValueError()
if command=='P':
lane=numeric(a[0],2);ident=numeric(a[1],999999);priority=numeric(a[2],3);deadline=numeric(a[3],1000000)
if a[4]=='-': data=b''
else:
if len(a[4])>128 or not a[4] or len(a[4])%2 or any(c not in '0123456789abcdefABCDEF' for c in a[4]): raise ValueError()
data=bytes.fromhex(a[4])
q=self.lanes[lane]
if len(q)>=self.cap[lane]: self.stats[5]+=1;rc=-2
elif not self.allocate(64,0): self.stats[6]+=1;rc=-3
elif not self.allocate(len(data)+1,1):
self.memory[2]+=1;self.memory[5]-=1;self.memory[7]-=64;self.stats[6]+=1;rc=-3
else: q.append((ident,priority,deadline,data));self.stats[0]+=1;rc=0
elif command in ('O','K','C'):
lane=numeric(a[0],2);q=self.lanes[lane]
if command=='C': rc=self.clear(lane)
elif not q: rc=-2
else:
packet=q[0];rc=0
if command=='O': q.pop(0);self.release(packet);self.stats[1]+=1
elif command=='E':
lane=numeric(a[0],2);now=numeric(a[1],1000000);old=self.lanes[lane];keep=[];rc=0
for p in old:
if p[2] and p[2]<=now: self.release(p);self.stats[2]+=1;rc+=1
else: keep.append(p)
self.lanes[lane]=keep
elif command=='M':
source=numeric(a[0],2);target=numeric(a[1],2);maximum=numeric(a[2],32);rc=0
if source!=target:
rc=min(maximum,len(self.lanes[source]),self.cap[target]-len(self.lanes[target]))
self.lanes[target].extend(self.lanes[source][:rc]);del self.lanes[source][:rc];self.stats[4]+=rc
elif command=='L':
lane=numeric(a[0],2);capacity=numeric(a[1],32)
if capacity==0: raise ValueError()
rc=-2 if capacity<len(self.lanes[lane]) else 0
if rc==0:self.cap[lane]=capacity
elif command=='F': self.countdown=numeric(a[0],1000);rc=0
else:
for lane in range(3):self.clear(lane)
rc=0
except ValueError: pass
return rc,packet
def snapshot(self):
result=self.stats[:]
for lane,q in enumerate(self.lanes):
total=0
for p in q: total=(total*33+packet_hash(p))&0xffffffff
result.extend([len(q),sum(len(p[3]) for p in q),q[0][0] if q else 0,q[-1][0] if q else 0,total,self.cap[lane]])
return result+self.memory
def scenarios(seed):
lines=['P 0 11 1 0 010203','P 0 12 0 0 ff','O 0','K 0','X','P 1 0 3 7 -','P 1 999999 0 8 00ff','P 1 28 1 0 00','P 1 29 2 7 aa','E 1 7','K 1','O 1','P 1 30 0 7 -','E 1 7','C 1','P 1 1 0 0 -','O 1','P 1 2 0 0 ab','X','F 1','P 0 100 0 0 -','F 2','P 0 101 1 0 01','P 0 102 0 0 -','X']
rng=random.Random(seed)
for _ in range(1100):
cmd=rng.choice('PPPPPOKCEEMMLFXX');lane=rng.randrange(3)
if cmd=='P':
n=rng.choice([0,1,2,8,64]);data=bytes(rng.randrange(256) for _ in range(n)).hex() or '-'
lines.append(f'P {lane} {rng.randrange(1000000)} {rng.randrange(4)} {rng.choice([0,1,6,7,8,1000000])} {data}')
elif cmd in 'OKC':lines.append(f'{cmd} {lane}')
elif cmd=='E':lines.append(f'E {lane} {rng.choice([0,1,7,8,1000000])}')
elif cmd=='M':lines.append(f'M {lane} {rng.randrange(3)} {rng.choice([0,1,2,32])}')
elif cmd=='L':lines.append(f'L {lane} {rng.choice([1,2,8,32])}')
elif cmd=='F':lines.append(f'F {rng.choice([0,1,2,3])}')
else:lines.append('X')
lines+=['X','F 0','L 2 32']+[f'P 2 {i} 3 1000000 '+('ff'*64) for i in range(33)]
lines+=['M 2 0 32','L 0 1','E 2 999999','E 2 1000000','M 0 1 0','M 0 0 32','X']
lines+=['P 0 0 0 0 -','bad','P 3 0 0 0 -','P 0 -1 0 0 -','P 0 1000000 0 0 -','P 0 1 4 0 -','P 0 1 0 1000001 -','P 0 1 0 0 zz','P 0 1 0 0 0','P 0 1 0 0 '+'00'*65,'P 0 1 0 0 00 extra','O 0 extra','L 0 0','E 0 -1','F 1001','M 0 1 33','M 3 0 0','K','P 0 +1 0 0 -','P 0 0002 01 0000 Aa00','P 0 3 0 0 -']
return lines
def expected(lines):
model=Model();output=[]
for step,line in enumerate(lines,1):
rc,packet=model.command(line);row=f'R {step} {rc}'
if rc==0 and line.split()[0] in ('O','K'):
ident,priority,deadline,data=packet;row+=f' {ident} {priority} {deadline} '+(data.hex() or '-')
output.append(row);output.append(f'S {step} '+' '.join(map(str,model.snapshot())))
for lane in range(3):model.clear(lane)
output.append('Z '+' '.join(map(str,model.memory)))
return ('\n'.join(output)+'\n').encode()
records=[]
def main():
scratch=None;errors=[];business_ok=False;cleanup_ok=False;absence_ok=False;raw_difference=None
try:
config=json.loads(Path(__file__).with_name('cases.json').read_text())
lines=scenarios(config['seed']);answer=expected(lines);stdin=('\n'.join(lines)+'\n').encode()
candidate=(Path(sys.argv[1])/'queue.cpp').read_bytes()
scratch=Path(tempfile.mkdtemp(prefix='packet-queue-'))
# Root scratch is already owned by the candidate UID in isolated mode.
for name,text in config['context'].items():
path=scratch/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_text(text)
(scratch/'queue.cpp').write_bytes(candidate)
# chmod only files/directories created by this check before its first child.
for path in sorted(scratch.rglob('*'),key=lambda p:len(p.parts)):
path.chmod(0o755 if path.is_dir() else 0o644)
if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534':os.chown(path,65534,65534)
compile_argv=['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic','queue.cpp','memory.cpp','codec.cpp','service.cpp','-o','packet-demo']
execute('compile',compile_argv,str(scratch))
actual=execute('candidate',[str(scratch/'packet-demo')],str(scratch),stdin)
business_ok=actual==answer
if not business_ok:
aa=actual.splitlines();bb=answer.splitlines()
first=next((i for i,(a,b) in enumerate(zip(aa,bb)) if a!=b),min(len(aa),len(bb)))
raw_difference=dict(line=first+1,actual=(aa[first] if first<len(aa) else b'<missing>').decode('utf-8','replace')[:180],expected=(bb[first] if first<len(bb) else b'<missing>').decode('ascii')[:180],actual_len=len(actual),actual_sha=digest(actual),expected_len=len(answer),expected_sha=digest(answer))
except BaseException as error:
errors.append(['primary',type(error).__name__,str(error)])
finally:
if scratch is not None:
try:
execute('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)])
cleanup_ok=True
except BaseException as error:errors.append(['cleanup',type(error).__name__,str(error)])
try:
execute('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)])
absence_ok=True
except BaseException as error:errors.append(['absence',type(error).__name__,str(error)])
report=dict(scratch=str(scratch) if scratch else None,records=records,errors=errors,business_ok=business_ok,cleanup_ok=cleanup_ok,absence_ok=absence_ok,difference=raw_difference,io_policy='bytes; diagnostics ignored; raw wire/IO bytes not persisted, length/SHA only')
packed=base64.b64encode(zlib.compress(json.dumps(report,separators=(',',':')).encode(),9)).decode()
print('PACKET_TERMINAL_Z '+packed,file=sys.stderr)
success=business_ok and cleanup_ok and absence_ok and not errors
print('PASS' if success else 'FAIL packet behavior or execution; private diagnostics retained')
return 0 if success else 1
if __name__=='__main__':sys.exit(main())