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

494 lines
29 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
RECOVERY_SECONDS = 3
COMPILE_SECONDS = 20
CANDIDATE_SECONDS = 12
CLEANUP_SECONDS = 6
ABSENCE_SECONDS = 6
EVIDENCE_PATH = None
RECORD_WIRE = False
SUPERVISOR = r'''
import base64,bz2,ctypes,hashlib,json,os,selectors,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
selector=selectors.DefaultSelector(); chunks={1:[],2:[]}; input_offset=0; payload=b''
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.append([pid,None,os.waitstatus_to_exitcode(status)])
def pump(delay):
global input_offset,drained
for key,mask in selector.select(delay):
stream,kind=key.fileobj,key.data
if kind==0:
try:
count=os.write(stream.fileno(),payload[input_offset:input_offset+65536])
input_offset+=count
except (BrokenPipeError,ConnectionResetError):
input_offset=len(payload)
except BlockingIOError:
continue
if input_offset==len(payload):
selector.unregister(stream);stream.close()
else:
try: block=os.read(stream.fileno(),65536)
except BlockingIOError: continue
if block: chunks[kind].append(block)
else: selector.unregister(stream);stream.close()
drained=not any(key.data in (1,2) for key in selector.get_map().values())
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()
for stream,kind in ((leader.stdin,0),(leader.stdout,1),(leader.stderr,2)):
os.set_blocking(stream.fileno(),False)
if kind==0 and not payload: stream.close();continue
selector.register(stream,selectors.EVENT_WRITE if kind==0 else selectors.EVENT_READ,kind)
while True:
pump(0.02)
own_wait()
if reaped and drained: break
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: pump(0.01);own_wait()
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)
output=b''.join(chunks[1]);diagnostic=b''.join(chunks[2]);selector.close()
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':waits,
'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['remaining']:
if type(row) is not list or len(row)!=3 or not all(identity_int(x,1) for x in row[:2]) or type(row[2]) is not str:
raise ValueError('invalid remaining record')
for row in meta['exceptions']:
if type(row) is not list or len(row)!=3 or any(type(x) is not str for x in row):
raise ValueError('invalid exception record')
for row in meta['adopted']:
if type(row) is not list or len(row)!=3 or not identity_int(row[0],1) or row[1] is not None or type(row[2]) is not int:
raise ValueError('invalid adopted wait record')
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 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())
entry['config'] = config
entry['payload_len'] = len(payload)
entry['payload_sha256'] = hashlib.sha256(payload).hexdigest()
if RECORD_WIRE:
entry['wire_ascii'] = raw.stdout.decode('ascii')
entry['wrapper_stderr_bz2'] = base64.b64encode(bz2.compress(raw.stderr)).decode('ascii')
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']
if not RECORD_WIRE: entry['meta'] = meta
validate_meta(meta,config,out,err)
return out,err
import re
import zlib
PUBLIC_OBSERVER = '#include "decimal.h"\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic df_state state;\nstatic df_stream stream;\nstatic unsigned char data[4096];\nstatic unsigned hexvalue(unsigned char c) {\n if(c>=\'0\'&&c<=\'9\') return c-\'0\';\n if(c>=\'a\'&&c<=\'f\') return c-\'a\'+10;\n return c-\'A\'+10;\n}\nstatic size_t unpack(const char *text) {\n size_t n=0;\n if(!text || !strcmp(text,"-")) return 0;\n while(text[0] && text[1] && n<sizeof data) {\n data[n++]=(unsigned char)((hexvalue(text[0])<<4)|hexvalue(text[1]));\n text+=2;\n }\n return n;\n}\nstatic void fields(df_config c) {\n printf("%u %u %u %u",c.seq,c.rate,c.window,c.channel);\n}\nstatic void snapshot(void) {\n fields(state.current);\n printf(" %u %u %d %zu %zu %d %u %u\\n",state.accepted,state.rejected,state.last,\n df_history_size(&state),df_pending(&stream),df_discarding(&stream),stream.bytes,stream.frames);\n}\nint main(void) {\n char line[9000];\n df_init(&state);df_stream_init(&stream);\n while(fgets(line,sizeof line,stdin)) {\n char *op=strtok(line," \\t\\r\\n");\n if(!op) continue;\n if(!strcmp(op,"R")) {df_init(&state);df_stream_init(&stream);snapshot();}\n else if(!strcmp(op,"Q")) snapshot();\n else if(!strcmp(op,"D")) {\n unsigned w=(unsigned)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n unsigned lo=(unsigned)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n unsigned hi=(unsigned)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n uint32_t out=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n size_t n=unpack(strtok(NULL," \\t\\r\\n"));\n int rc=df_number(data,n,w,lo,hi,&out);\n printf("%d %u\\n",rc,out);\n } else if(!strcmp(op,"A")) {\n size_t n=unpack(strtok(NULL," \\t\\r\\n"));\n printf("%d ",df_apply(&state,data,n));snapshot();\n } else if(!strcmp(op,"F")) {\n size_t n=unpack(strtok(NULL," \\t\\r\\n"));\n printf("%zu ",df_feed(&stream,&state,data,n));snapshot();\n } else if(!strcmp(op,"T")) {\n printf("%d ",df_finish(&stream,&state));snapshot();\n } else if(!strcmp(op,"H")) {\n size_t age=(size_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n df_config c={777,777,777,777};\n int rc=df_history(&state,age,&c);\n printf("%d ",rc);fields(c);putchar(\'\\n\');\n } else if(!strcmp(op,"E")) {\n size_t cap=(size_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10),written=777;\n char out[80];memset(out,0xA5,sizeof out);\n int rc=df_encode(&state.current,out,cap,&written);\n printf("%d %zu",rc,written);\n for(size_t i=0;i<sizeof out;i++) printf(" %u",(unsigned char)out[i]);\n putchar(\'\\n\');\n } else if(!strcmp(op,"B")) {\n df_config c;\n c.seq=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n c.rate=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n c.window=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n c.channel=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n size_t cap=(size_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10),written=777;\n char out[80];memset(out,0xA5,sizeof out);\n int rc=df_encode(&c,out,cap,&written);\n printf("%d %zu",rc,written);\n for(size_t i=0;i<sizeof out;i++) printf(" %u",(unsigned char)out[i]);\n putchar(\'\\n\');\n } else if(!strcmp(op,"V")) {\n df_config c;\n c.seq=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n c.rate=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n c.window=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n c.channel=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n printf("%d ",df_validate(&c));fields(c);putchar(\'\\n\');\n } else if(!strcmp(op,"M")) {\n df_config a,b;\n a.seq=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n a.rate=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n a.window=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n a.channel=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n b.seq=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n b.rate=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n b.window=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n b.channel=(uint32_t)strtoul(strtok(NULL," \\t\\r\\n"),NULL,10);\n printf("%u\\n",df_changed_mask(&a,&b));\n } else if(!strcmp(op,"N")) {\n int n=(int)strtol(strtok(NULL," \\t\\r\\n"),NULL,10);\n puts(df_status_name(n));\n } else return 2;\n }\n return ferror(stdin)?3:0;\n}\n'
"""Independent immutable-record Python oracle: no candidate or gold invocation."""
import random
NAMES=['ok','empty','length','char','range','syntax','key','duplicate','sequence','budget','overflow','history','capacity']
BOUNDS={'seq':(4,0,9999),'rate':(4,1,2000),'window':(3,1,128),'channel':(2,0,15)}
def number(data,width,lo,hi,seed):
if not data:return 1,seed
if len(data)>width:return 2,seed
if any(x<48 or x>57 for x in data):return 3,seed
value=sum((x-48)*10**i for i,x in enumerate(reversed(data)))
return (0,value) if lo<=value<=hi else (4,seed)
def validate(c):
if any(not BOUNDS[k][1]<=v<=BOUNDS[k][2] for k,v in zip(BOUNDS,c)):return 4
return 9 if c[1]*c[2]>64000 else 0
def parse(frame,current):
if not frame:return 1,current
if len(frame)>128:return 2,current
values={}
for token in frame.split(b';'):
if token.count(b'=')!=1 or token.startswith(b'='):return 5,current
key,raw=token.split(b'=')
if key not in [x.encode() for x in BOUNDS]:return 6,current
key=key.decode('ascii')
if key in values:return 7,current
rc,value=number(raw,*BOUNDS[key],0)
if rc:return rc,current
values[key]=value
if 'seq' not in values or len(values)<2:return 5,current
if values['seq']!=(current[0]+1)%10000:return 8,current
proposed=tuple(values.get(k,v) for k,v in zip(BOUNDS,current))
rc=validate(proposed)
return (rc,current) if rc else (0,proposed)
def model(commands):
c=(0,25,8,2); accepted=rejected=last=0;history=[];pending=b'';dropping=False; bytecount=frames=0; rows=[]
def snap():return list(c)+[accepted,rejected,last,len(history),len(pending),int(dropping),bytecount,frames]
def apply(data,forced=None):
nonlocal c,accepted,rejected,last,history
rc,proposed=parse(data,c) if forced is None else (forced,c)
last=rc
if rc:rejected+=1
else:c=proposed;accepted+=1;history=(history+[c])[-16:]
return rc
for cmd in commands:
tokens=cmd.split(' '); op=tokens[0]
data=bytes.fromhex(tokens[-1]) if op in ('D','A','F') and tokens[-1]!='-' else b''
if op=='R':
c=(0,25,8,2);accepted=rejected=last=0;history=[];pending=b'';dropping=False;bytecount=frames=0;row=snap()
elif op=='Q':row=snap()
elif op=='D':row=list(number(data,*map(int,tokens[1:5])))
elif op=='A':rc=apply(data);row=[rc]+snap()
elif op=='F':
completed=0
for b in data:
bytecount+=1
if b==10:
apply(pending,10 if dropping else None);pending=b'';dropping=False;frames+=1;completed+=1
elif not dropping:
if len(pending)==128:dropping=True
else:pending+=bytes([b])
row=[completed]+snap()
elif op=='T':
count=int(bool(pending) or dropping)
if count:apply(pending,10 if dropping else 5);frames+=1;pending=b'';dropping=False
row=[count]+snap()
elif op=='H':
age=int(tokens[1]); row=[0]+list(history[-1-age]) if age<len(history) else [11,777,777,777,777]
elif op in ('E','B'):
config=c if op=='E' else tuple(map(int,tokens[1:5]))
capacity=int(tokens[1] if op=='E' else tokens[5]);out=[165]*80;written=777;rc=validate(config)
text=('seq=%04d;rate=%04d;window=%03d;channel=%02d'%config).encode()
if not rc and capacity<=len(text):rc=12
if not rc:written=len(text);out[:written+1]=list(text+b'\x00')
row=[rc,written]+out
elif op=='V':
config=tuple(map(int,tokens[1:]));row=[validate(config)]+list(config)
elif op=='M':
values=list(map(int,tokens[1:]));row=[sum(1<<i for i in range(4) if values[i]!=values[i+4])]
elif op=='N':
n=int(tokens[1]);row=[NAMES[n] if 0<=n<len(NAMES) else 'unknown']
else:raise ValueError(op)
rows.append(row)
return rows
def span(op,data):return op+' '+(data.hex() or '-')
def scenarios():
rng=random.Random(807533);cmds=['R','Q','H 0','H 20']
for mask in range(16):cmds.append('M 0 25 8 2 '+' '.join(str(v+bool(mask&(1<<i))) for i,v in enumerate([0,25,8,2])))
for n in range(-1,15):cmds.append('N '+str(n))
for x in range(256):cmds.append('D 1 0 9 777 '+bytes([x]).hex())
for width in range(1,10):
for data in [b'',b'0',b'9',b'0'*width,b'9'*width,b'0'*(width+1),b'1_0',b'+1',b' 1',b'1\x00',b'\xff']:
cmds.append('D %d 0 999999999 777 %s'%(width,data.hex() or '-'))
for lo,hi in [(1,1),(1,2000),(7,83),(0,0)]:
for value in [0,1,6,7,8,82,83,84,1999,2000,2001]:
cmds.append('D 9 %d %d 123456 %s'%(lo,hi,str(value).encode().hex()))
for c in [(0,25,8,2),(9999,500,128,15),(10000,1,1,0),(1,0,2,2),(1,2001,1,2),(1,1,0,2),(1,1,129,2),(1,1,1,16),(1,2000,32,2),(1,2000,33,2)]:cmds.append('V '+' '.join(map(str,c)))
for config in [(0,25,8,2),(9999,500,128,15),(1,2000,32,0),(1,2000,33,0),(10000,1,1,0),(1,0,1,0),(1,1,129,0),(1,1,1,16),(4294967295,4294967295,4294967295,4294967295)]:
for cap in [0,40,41,80]:cmds.append('B '+' '.join(map(str,config))+' '+str(cap))
for cap in [0,1,37,38,39,40,41,79,80]:cmds.append('E '+str(cap))
bad=[b'',b';',b'seq=1',b'rate=1',b'=1',b'seq',b'seq==1;rate=1',b'seq=1;',b'seq=1;;rate=1',b'Seq=1;rate=1',b'noise=;seq=1',b'seq=1;seq=1;rate=1',b'seq=1;rate=',b'seq=1;rate=00001',b'seq=1;rate=0',b'seq=1;rate=2001',b'seq=1;rate=100;channel=x',b'seq=1;rate=100;window=129',b'seq=1;rate=100;channel=16',b'seq=1;rate=2000;window=33',b'seq=0;rate=2',b'seq=2;rate=2',b'seq=1;rate=1\r',b'seq=1;rate=\x003',b'x'*129]
for frame in bad:cmds+=['R',span('A',frame),'Q','H 0','E 80']
# Each invalid late field must preserve a previously committed nondefault state.
for frame in bad:
cmds+=['R',span('A',b'seq=1;rate=70;window=11;channel=9'),span('A',frame.replace(b'seq=1',b'seq=2')),'Q','H 0','H 1']
cmds+=['R']
for seq in range(1,65):
fields=[('seq',seq),('rate',rng.randrange(1,501)),('window',rng.randrange(1,129)),('channel',rng.randrange(16))]
rng.shuffle(fields);packet=';'.join('%s=%0*d'%(k,BOUNDS[k][0] if seq%3==0 else 1,v) for k,v in fields).encode()
cmds+=[span('A',packet),'H 0','H 15','H 16','E 80']
if seq%4==0:cmds+=[span('A',('seq=%d;rate=99;channel=x'%(seq+1)).encode()),'Q']
cmds+=['R']
# Full sequence wrap exercises 9999 -> 0 without injecting private state.
for seq in range(1,10002):cmds.append(span('A',('seq=%d;channel=%d'%(seq%10000,seq%16)).encode()))
cmds+=['H 0','H 15','E 80','R']
frames=[b'\n',b'seq=1;rate=25\n',b'seq=2;rate=100;channel=x\n',b'seq=2;channel=15\n',b'x'*128+b'\n',b'x'*129+b'\n',b'x'*500+b'\n',b'seq=3;window=9\n',b'seq=4;rate=5\r\n',b'seq=4;rate=6\n']
raw=b''.join(frames)
offset=0
while offset<len(raw):
n=rng.randrange(1,34);cmds.append(span('F',raw[offset:offset+n]));offset+=n
cmds+=['Q','T','T','R',span('F',b'seq=1;rate=4'),'Q','T','Q',span('F',b'x'*129),'T','Q',span('F',b'seq=1;channel=0\n'),'Q']
# Random byte fields and packet ordering, repeatability independent of implementation.
for _ in range(100):
raw=bytes(rng.randrange(256) for _ in range(rng.randrange(0,12)))
cmds.append('D 9 0 999999999 42 '+(raw.hex() or '-'))
return cmds
def decimal_token(token):
if re.fullmatch(rb'[+-]?[0-9]+',token) is None:
raise ValueError('stdout decimal token')
negative=token.startswith(b'-')
digits=token[1:] if token[:1] in (b'+',b'-') else token
significant=digits.lstrip(b'0') or b'0'
if len(significant)>12:
raise ValueError('stdout observable integer range')
value=int(significant)
return -value if negative else value
def observations(raw, expected):
lines=raw.split(b'\n')
if not lines or lines[-1]!=b'':
raise ValueError('stdout requires LF terminated rows')
lines.pop()
if len(lines)!=len(expected):
raise ValueError('stdout row count '+str(len(lines)))
for i,(line,wanted) in enumerate(zip(lines,expected)):
if re.search(rb'[^\x09\x0d\x20-\x7e]',line):
raise ValueError('stdout invalid byte row '+str(i))
tokens=re.split(rb'[ \t\r]+',line.strip(b' \t\r'))
if len(tokens)!=len(wanted):
raise ValueError('stdout token count row '+str(i))
actual=[decimal_token(t) if type(w) is int else t.decode('ascii') for t,w in zip(tokens,wanted)]
if actual!=wanted:
raise ValueError('behavior row %d actual=%r expected=%r'%(i,actual[:13],wanted[:13]))
def main():
evidence=[]; failures=[];scratch=None;behavior_ok=False;cleanup_ok=False;absence_ok=False
commands=scenarios(); expected=model(commands)
payload=('\n'.join(commands)+'\n').encode('ascii')
try:
scratch=Path(tempfile.mkdtemp(prefix='decimal-fields-'))
# All directories and support files are prepared before the first child.
include=scratch/'include';src=scratch/'src'
include.mkdir();src.mkdir()
os.chmod(include,0o777);os.chmod(src,0o777)
candidate=Path(sys.argv[1])
for name in ('include/decimal.h','src/decimal.c','src/config.c','src/stream.c'):
target=scratch/name
target.write_bytes((candidate/name).read_bytes())
os.chmod(target,0o644)
harness=scratch/'observe.c';harness.write_text(PUBLIC_OBSERVER);os.chmod(harness,0o644)
compile_argv=['gcc','-std=c11','-O2','-Wall','-Wextra','-pedantic','-I'+str(include),str(src/'decimal.c'),str(src/'config.c'),str(src/'stream.c'),str(harness),'-o',str(scratch/'observe')]
execute_stage('compiler',compile_argv,scratch,COMPILE_SECONDS,b'',evidence)
out,err=execute_stage('candidate',[str(scratch/'observe')],scratch,CANDIDATE_SECONDS,payload,evidence)
observations(out,expected)
behavior_ok=True
except BaseException as error:
failures.append(['main',type(error).__name__,str(error)])
finally:
if scratch is not None:
try:
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/',CLEANUP_SECONDS,b'',evidence)
cleanup_ok=True
except BaseException as error:
failures.append(['cleanup',type(error).__name__,str(error)])
try:
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/',ABSENCE_SECONDS,b'',evidence)
absence_ok=True
except BaseException as error:
failures.append(['absence',type(error).__name__,str(error)])
summary={'scratch':None if scratch is None else str(scratch),'evidence':evidence,'failures':failures,'behavior_ok':behavior_ok,'cleanup_ok':cleanup_ok,'absence_ok':absence_ok,'rows':len(expected),'input_len':len(payload),'input_sha256':hashlib.sha256(payload).hexdigest()}
if EVIDENCE_PATH:
Path(EVIDENCE_PATH).write_text(json.dumps(summary,separators=(',',':'))+'\n')
# Wrapper preserves only a 4000-character tail: store a compact column-free record.
compact=base64.b85encode(zlib.compress(json.dumps(summary,separators=(',',':')).encode(),9)).decode('ascii')
sys.stderr.write('DFEVIDENCE85:'+compact+'\n')
passed=behavior_ok and cleanup_ok and absence_ok and not failures
print('PASS' if passed else 'FAIL '+json.dumps(failures,separators=(',',':')))
return 0 if passed else 1
if __name__=='__main__':
raise SystemExit(main())