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>
321 lines
18 KiB
Python
321 lines
18 KiB
Python
"""Independent numeric schedule model; candidate children only emit raw protocol observations."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
SUPPORT = '#include "phase.h"\nint main(void)\n{\n return timer_cli(stdin, stdout);\n}\n'
|
|
CANDIDATES = ['phase.h', 'phase_math.c', 'timer_bank.c', 'wire.c', 'cli.c']
|
|
LIMIT = 1000000000
|
|
|
|
|
|
class Model:
|
|
def __init__(self):
|
|
self.now = 0
|
|
self.channels = {}
|
|
|
|
def next_at(self, channel):
|
|
anchor = channel['epoch'] + channel['phase']
|
|
return anchor + ((self.now - anchor) // channel['period'] + 1) * channel['period']
|
|
|
|
def state(self, ident):
|
|
c = self.channels[ident]
|
|
return 'STATE {} {} {} {} {} {} {} {} {}'.format(
|
|
ident, c['period'], c['phase'], c['epoch'], c['mode'],
|
|
c['enabled'], c['next'], c['fired'], c['skipped'])
|
|
|
|
def execute(self, line):
|
|
words = line.split()
|
|
if len(line) > 255:
|
|
return ['ERR syntax']
|
|
if not words:
|
|
return []
|
|
name = words[0]
|
|
arities = {'INIT': 2, 'PUT': 6, 'PEEK': 2, 'ENABLE': 3, 'SHIFT': 3,
|
|
'DROP': 2, 'TICK': 2, 'LIST': 1, 'STATS': 1}
|
|
if name not in arities or len(words) != arities[name]:
|
|
return ['ERR syntax']
|
|
tokens = words[1:5] if name == 'PUT' else words[1:]
|
|
args = []
|
|
for token in tokens:
|
|
if re.fullmatch(r'[+-]?[0-9]+', token) is None:
|
|
return ['ERR syntax']
|
|
number = int(token)
|
|
if not -(2**63) <= number <= 2**63-1:
|
|
return ['ERR range']
|
|
args.append(number)
|
|
if name == 'PUT' and words[-1] not in ('COUNT', 'LATEST'):
|
|
return ['ERR syntax']
|
|
if name == 'INIT':
|
|
if abs(args[0]) > LIMIT:
|
|
return ['ERR range']
|
|
self.now = args[0]
|
|
self.channels.clear()
|
|
return ['OK INIT {}'.format(self.now)]
|
|
if name == 'TICK':
|
|
target = args[0]
|
|
if abs(target) > LIMIT:
|
|
return ['ERR range']
|
|
if target < self.now:
|
|
return ['ERR backward']
|
|
events = []
|
|
for ident, c in sorted(self.channels.items()):
|
|
if not c['enabled'] or c['next'] > target:
|
|
continue
|
|
due = 1 + (target - c['next']) // c['period']
|
|
first = c['next']
|
|
last = first + (due-1)*c['period']
|
|
skipped = 0
|
|
if c['mode'] == 'LATEST':
|
|
first = last
|
|
skipped = due-1
|
|
c['next'] = last+c['period']
|
|
c['fired'] += due
|
|
c['skipped'] += skipped
|
|
events.append('EVT {} {} {} {} {} {}'.format(
|
|
ident, due, first, last, skipped, c['next']))
|
|
self.now = target
|
|
return ['BEGIN {} {}'.format(target,len(events))]+events+['END {}'.format(target)]
|
|
if name == 'LIST':
|
|
return ['LIST {}'.format(len(self.channels))]+[self.state(i) for i in sorted(self.channels)]
|
|
if name == 'STATS':
|
|
return ['STATS {} {} {} {} {}'.format(
|
|
self.now, len(self.channels), sum(c['enabled'] for c in self.channels.values()),
|
|
sum(c['fired'] for c in self.channels.values()),
|
|
sum(c['skipped'] for c in self.channels.values()))]
|
|
ident = args[0]
|
|
if not 0 <= ident < 16:
|
|
return ['ERR range']
|
|
if name == 'PUT':
|
|
_, period, phase, epoch = args
|
|
if not 1 <= period <= 1000000 or abs(phase)>LIMIT or abs(epoch)>LIMIT:
|
|
return ['ERR range']
|
|
c = dict(period=period, phase=phase % period, epoch=epoch,
|
|
mode=words[5], enabled=1, fired=0, skipped=0)
|
|
c['next'] = self.next_at(c)
|
|
self.channels[ident] = c
|
|
return ['OK PUT {}'.format(ident)]
|
|
if name == 'ENABLE' and args[1] not in (0,1):
|
|
return ['ERR range']
|
|
if name == 'SHIFT' and abs(args[1])>LIMIT:
|
|
return ['ERR range']
|
|
if ident not in self.channels:
|
|
return ['ERR missing']
|
|
c = self.channels[ident]
|
|
if name == 'PEEK':
|
|
return [self.state(ident)]
|
|
if name == 'DROP':
|
|
del self.channels[ident]
|
|
return ['OK DROP {}'.format(ident)]
|
|
if name == 'ENABLE':
|
|
c['enabled'] = args[1]
|
|
c['next'] = self.next_at(c) if c['enabled'] else 0
|
|
return ['OK ENABLE {} {}'.format(ident,args[1])]
|
|
c['phase'] = (c['phase']+args[1]) % c['period']
|
|
c['next'] = self.next_at(c) if c['enabled'] else 0
|
|
return ['OK SHIFT {}'.format(ident)]
|
|
|
|
|
|
def scenarios():
|
|
# First case is the published COUNT reproduction, next isolates LATEST.
|
|
cases = [
|
|
('public-count',['INIT -8','PUT 3 3 -7 0 COUNT','PEEK 3','TICK -2','STATS']),
|
|
('public-latest',['INIT 0','PUT 4 5 0 0 LATEST','TICK 17','PEEK 4']),
|
|
('independent-latest',['INIT -40','PUT 12 7 2 -3 LATEST','PEEK 12','TICK 100','TICK 100','TICK 102','STATS']),
|
|
('empty-and-errors',['',' ','LIST','STATS','PEEK 0','DROP 15','ENABLE 1 0',
|
|
'SHIFT 2 -1','TICK -1','INIT -1','TICK -1','TICK 0','LIST']),
|
|
('strict-boundary',['INIT -1000000000','PUT 0 1 -1000000000 1000000000 COUNT',
|
|
'PEEK 0','TICK -1000000000','TICK 1000000000','PEEK 0','STATS']),
|
|
('rearm-and-replace',['INIT -97','PUT 15 11 -37 23 LATEST','TICK -32','ENABLE 15 0',
|
|
'PEEK 15','TICK 17','SHIFT 15 -29','PEEK 15','ENABLE 15 1','PEEK 15',
|
|
'TICK 79','ENABLE 15 1','TICK 79','PUT 15 2 1 -1 COUNT','PEEK 15',
|
|
'STATS','TICK 83','DROP 15','LIST','STATS']),
|
|
('invalid-atomicity',['PUT 2 7 -12 -9 COUNT','PEEK 2','PUT 2 0 2 3 COUNT',
|
|
'PUT 2 1000001 0 0 COUNT','PUT 2 4 1000000001 0 COUNT',
|
|
'PUT 2 4 0 -1000000001 COUNT','PUT 16 1 0 0 COUNT','PEEK -1',
|
|
'ENABLE 2 2','SHIFT 2 -1000000001','TICK -1','INIT 1000000001',
|
|
'PUT 2 7 4 3 other','PUT 2 7 0 0 COUNT extra','PEEK 2','STATS',
|
|
'SHIFT 2 x','PUT 2 7 4 0.5 COUNT','TICK 9223372036854775808',
|
|
'PEEK 2','TICK 23','PEEK 2','STATS','UNKNOWN','INIT','LIST extra']),
|
|
('whitespace-line-limits',['\tINIT +00002 ','PUT +1 7 -00012 0 COUNT',
|
|
' PEEK\t1','\tTICK\t+30\t','X'*256,'PEEK 1','LIST','STATS'])
|
|
]
|
|
full = ['INIT -123456789']
|
|
for ident in range(15,-1,-1):
|
|
full.append('PUT {} {} {} {} {}'.format(ident, [1,2,3,7,997,1000000][ident%6],
|
|
-1000000000+ident*717, 1000000000-ident*37, 'LATEST' if ident%2 else 'COUNT'))
|
|
full += ['LIST','TICK -123450000','LIST','STATS','TICK 1000000000','LIST','STATS']
|
|
cases.append(('all-ids-large-gap',full))
|
|
rng=random.Random(0x74696D6572)
|
|
for case in range(18):
|
|
now=rng.randint(-1000000,1000000)
|
|
lines=['INIT {}'.format(now)]
|
|
for step in range(110):
|
|
ident=rng.randrange(16)
|
|
choice=rng.randrange(10)
|
|
if choice < 3:
|
|
period=rng.choice([1,2,3,5,17,997,1000000,rng.randint(1,1000000)])
|
|
lines.append('PUT {} {} {} {} {}'.format(ident,period,rng.randint(-LIMIT,LIMIT),
|
|
rng.randint(-LIMIT,LIMIT),rng.choice(['COUNT','LATEST'])))
|
|
elif choice==3:
|
|
lines.append('SHIFT {} {}'.format(ident,rng.randint(-LIMIT,LIMIT)))
|
|
elif choice==4:
|
|
lines.append('ENABLE {} {}'.format(ident,rng.randrange(2)))
|
|
elif choice==5:
|
|
lines.append('DROP {}'.format(ident))
|
|
elif choice==6:
|
|
lines.append('PEEK {}'.format(ident))
|
|
elif choice<9:
|
|
now=min(LIMIT,now+rng.choice([0,1,2,7,997,1000000]))
|
|
lines.append('TICK {}'.format(now))
|
|
else:
|
|
lines.extend(['LIST','STATS'])
|
|
lines.extend(['TICK 1000000000','LIST','STATS'])
|
|
cases.append(('generated-{}'.format(case),lines))
|
|
return cases
|
|
|
|
|
|
|
|
EXECUTOR = "import ctypes,json,os,signal,subprocess,sys,time\nphase=sys.argv[1]\ndeadline=float(sys.argv[2])\ncommand=sys.argv[3:]\nmarker=b'\\nTIMER_EXEC_META '\nleader=None\nstdout=b''\nstderr=b''\ncode=125\nexpired=False\ndetail=''\nreaped=0\n\ndef children():\n with open('/proc/self/task/{}/children'.format(os.getpid())) as stream:\n return [int(value) for value in stream.read().split()]\n\ndef terminate_owned():\n global reaped\n until=time.monotonic()+2\n if leader is not None and leader.poll() is None:\n try: os.killpg(leader.pid,signal.SIGKILL)\n except ProcessLookupError: pass\n while True:\n if leader is not None: leader.poll()\n owned=children()\n if not owned:\n return\n for pid in owned:\n try: os.kill(pid,signal.SIGKILL)\n except ProcessLookupError: pass\n for pid in owned:\n if leader is not None and pid==leader.pid:\n leader.poll()\n continue\n try:\n waited,_=os.waitpid(pid,os.WNOHANG)\n if waited: reaped+=1\n except ChildProcessError: pass\n if time.monotonic()>=until:\n raise RuntimeError('owned descendants not reaped')\n time.sleep(0.005)\n\ntry:\n libc=ctypes.CDLL(None,use_errno=True)\n if libc.prctl(36,1,0,0,0)!=0:\n raise OSError(ctypes.get_errno(),'subreaper setup failed')\n data=sys.stdin.buffer.read()\n leader=subprocess.Popen(command,stdin=subprocess.PIPE,stdout=subprocess.PIPE,\n stderr=subprocess.PIPE,start_new_session=True)\n try:\n stdout,stderr=leader.communicate(data,timeout=deadline)\n code=leader.returncode\n except subprocess.TimeoutExpired:\n expired=True\n terminate_owned()\n stdout,stderr=leader.communicate(timeout=2)\n code=124\n terminate_owned()\nexcept BaseException as error:\n code=125\n detail=type(error).__name__+': '+str(error)\n try: terminate_owned()\n except BaseException as cleanup_error:\n detail+='; '+type(cleanup_error).__name__+': '+str(cleanup_error)\nmetadata={'phase':phase,'pid':os.getpid(),'uid':os.getuid(),'gid':os.getgid(),\n 'deadline':deadline,'rc':code,'leader_rc':None if leader is None else leader.returncode,\n 'timeout':expired,'reaped':reaped,'detail':detail}\ntry:\n metadata['tree_absent']=not children()\nexcept BaseException as error:\n metadata['tree_absent']=False\n metadata['detail']+='; '+type(error).__name__+': '+str(error)\nif not metadata['tree_absent']:\n code=125\n metadata['rc']=code\nsys.stdout.buffer.write(stdout)\nsys.stderr.buffer.write(stderr+marker+json.dumps(metadata,sort_keys=True).encode()+b'\\n')\nsys.stdout.buffer.flush()\nsys.stderr.buffer.flush()\nraise SystemExit(code if code>=0 else 128-code)\n"
|
|
COMPILE_SECONDS = 30
|
|
CANDIDATE_SECONDS = 5
|
|
CLEANUP_SECONDS = 10
|
|
ABSENCE_SECONDS = 5
|
|
CLEANUP = 'import os,shutil,sys; print("cleanup_uid={} cleanup_gid={} path={}".format(os.getuid(),os.getgid(),sys.argv[1])); shutil.rmtree(sys.argv[1])'
|
|
ABSENCE = 'import json,os,sys; absent=not os.path.lexists(sys.argv[1]); print(json.dumps({"uid":os.getuid(),"gid":os.getgid(),"path":sys.argv[1],"absent":absent})); raise SystemExit(0 if absent else 9)'
|
|
|
|
|
|
def execute(phase, argv, records, timeout, data=''):
|
|
completed = subprocess.run([sys.executable, '-I', '-c', EXECUTOR, phase,
|
|
str(timeout), *argv], input=data.encode('utf-8'),
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
marker = b'\nTIMER_EXEC_META '
|
|
raw, found, terminal = completed.stderr.rpartition(marker)
|
|
if not found:
|
|
raise RuntimeError('execution metadata missing phase=' + phase)
|
|
metadata = json.loads(terminal.decode('utf-8', 'strict'))
|
|
records.append(metadata)
|
|
expected_uid = 65534 if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else os.getuid()
|
|
expected_gid = 65534 if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else os.getgid()
|
|
if metadata.get('phase') != phase or metadata.get('uid') != expected_uid or metadata.get('gid') != expected_gid:
|
|
raise RuntimeError('execution identity metadata mismatch')
|
|
if metadata.get('tree_absent') is not True:
|
|
raise RuntimeError('execution descendants remain phase=' + phase)
|
|
completed.stderr = raw.decode('utf-8', 'replace')
|
|
completed.stdout = completed.stdout.decode('utf-8', 'strict').replace('\r\n', '\n').replace('\r', '\n')
|
|
return completed
|
|
|
|
def main():
|
|
scratch = None
|
|
failure = None
|
|
checks = 0
|
|
records = []
|
|
cleanup_rc = None
|
|
absence_rc = None
|
|
try:
|
|
scratch = Path(tempfile.mkdtemp(prefix='timer-negative-phase-'))
|
|
print('scratch_created='+str(scratch), file=sys.stderr)
|
|
compiler = shutil.which('gcc')
|
|
if compiler is None:
|
|
raise RuntimeError('required compiler gcc unavailable')
|
|
candidate=Path(sys.argv[1])
|
|
# All input snapshots and construction are covered by finally.
|
|
source={name:(candidate/name).read_bytes() for name in CANDIDATES}
|
|
testcases=[]
|
|
for label, lines in scenarios():
|
|
model=Model()
|
|
expected=[]
|
|
evidence=[]
|
|
for command_index,line in enumerate(lines):
|
|
observations=model.execute(line)
|
|
expected.extend(observations)
|
|
evidence.extend([(command_index,line)]*len(observations))
|
|
testcases.append((label,'\n'.join(lines)+'\n',expected,evidence))
|
|
src=scratch/'src'
|
|
build=scratch/'build'
|
|
src.mkdir()
|
|
build.mkdir()
|
|
# These controller-owned directories are prepared before the first child.
|
|
src.chmod(0o777)
|
|
build.chmod(0o777)
|
|
for name,data in source.items():
|
|
(src/name).write_bytes(data)
|
|
(src/name).chmod(0o644)
|
|
(src/'main.c').write_text(SUPPORT)
|
|
(src/'main.c').chmod(0o644)
|
|
executable=str(build/'timer')
|
|
argv=[compiler,'-std=c11','-O2','-Wall','-Wextra','-Wpedantic',
|
|
str(src/'main.c'),*[str(src/n) for n in CANDIDATES if n.endswith('.c')],'-o',executable]
|
|
compilation=execute('compile',argv,records,COMPILE_SECONDS)
|
|
print('compile_rc={}'.format(compilation.returncode),file=sys.stderr)
|
|
if compilation.returncode:
|
|
print(compilation.stderr[-1000:],file=sys.stderr)
|
|
failure='compile rc={}'.format(compilation.returncode)
|
|
# After the first child the parent does not access the shared scratch tree.
|
|
if failure is None:
|
|
for label,data,expected,evidence in testcases:
|
|
observed=execute('candidate',[executable],records,CANDIDATE_SECONDS,data)
|
|
print('candidate_case={} candidate_rc={}'.format(label,observed.returncode),file=sys.stderr)
|
|
if observed.returncode:
|
|
failure='candidate rc={} case={}'.format(observed.returncode,label)
|
|
print(observed.stderr[-1000:],file=sys.stderr)
|
|
break
|
|
actual=observed.stdout.splitlines()
|
|
if observed.stdout != '\n'.join(expected)+'\n':
|
|
first=next((i for i,(a,b) in enumerate(zip(actual,expected)) if a!=b),
|
|
min(len(actual),len(expected)))
|
|
detail=evidence[first] if first<len(evidence) else ('EOF','EOF')
|
|
failure='observation difference case={} line={} command={}'.format(label,first,detail)
|
|
print('first_raw_difference '+json.dumps({'case':label,'output_index':first,
|
|
'command_index':detail[0],'command':detail[1],
|
|
'expected':expected[first] if first<len(expected) else '<EOF>',
|
|
'actual':actual[first] if first<len(actual) else '<EOF>',
|
|
'stdout_ends_newline':observed.stdout.endswith('\n')}),file=sys.stderr)
|
|
break
|
|
checks+=len(expected)
|
|
except Exception as error:
|
|
failure=type(error).__name__+': '+str(error)
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
cleanup=execute('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],
|
|
records,CLEANUP_SECONDS)
|
|
cleanup_rc=cleanup.returncode
|
|
print(cleanup.stdout.strip(),file=sys.stderr)
|
|
print('candidate_identity_cleanup_rc={}'.format(cleanup_rc),file=sys.stderr)
|
|
if cleanup_rc != 0:
|
|
failure='candidate identity cleanup rc={}'.format(cleanup_rc)
|
|
print(cleanup.stderr[-1000:],file=sys.stderr)
|
|
except Exception as error:
|
|
failure='cleanup '+type(error).__name__+': '+str(error)
|
|
# Independent same-identity absence runs even if deletion failed.
|
|
try:
|
|
absence=execute('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],
|
|
records,ABSENCE_SECONDS)
|
|
absence_rc=absence.returncode
|
|
print('candidate_identity_absence='+absence.stdout.strip(),file=sys.stderr)
|
|
if absence_rc != 0:
|
|
failure='candidate identity absence rc={}'.format(absence_rc)
|
|
except Exception as error:
|
|
failure='absence '+type(error).__name__+': '+str(error)
|
|
summary={'scratch':None if scratch is None else str(scratch),'cleanup_rc':cleanup_rc,
|
|
'absence_rc':absence_rc,'observations':checks,
|
|
'executions':[[x['phase'],x['pid'],x['uid'],x['gid'],x['deadline'],x['rc'],
|
|
x['timeout'],x['tree_absent'],x['reaped']] for x in records]}
|
|
print('TIMER_WORK_SUMMARY '+json.dumps(summary,separators=(',',':')),file=sys.stderr)
|
|
if failure is not None:
|
|
print('FAIL '+failure[:500])
|
|
return 1
|
|
print('observations_checked={}'.format(checks),file=sys.stderr)
|
|
print('PASS')
|
|
return 0
|
|
|
|
|
|
if __name__=='__main__':
|
|
raise SystemExit(main())
|