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>
499 lines
26 KiB
Python
499 lines
26 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
|
|
|
|
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]=os.waitstatus_to_exitcode(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_waits':[[p,None,rc] for p,rc 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_waits'}
|
|
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'):
|
|
if type(meta[name]) is not list:
|
|
raise ValueError('invalid list '+name)
|
|
if type(meta['adopted_waits']) is not list:
|
|
raise ValueError('invalid adopted waits')
|
|
for row in meta['adopted_waits']:
|
|
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 identity')
|
|
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:
|
|
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())
|
|
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 random
|
|
import re
|
|
|
|
|
|
def make_cases():
|
|
operations = []
|
|
def add(op, *args):
|
|
operations.append((op, args))
|
|
add('SUMMARY')
|
|
add('ASK', 0, 0, 0)
|
|
for code in range(4):
|
|
add('SET', code, code, code * 71, 4)
|
|
add('ASK', code, 0, 19)
|
|
add('VIEW', code)
|
|
add('ASK', code, 1, code * 71)
|
|
add('ASK', code, 1, code * 71 + 1)
|
|
add('SUMMARY')
|
|
add('ADV', 4)
|
|
for code in range(4):
|
|
add('ASK', code, 0, 0)
|
|
add('ADV', 1)
|
|
for code in range(4):
|
|
add('ASK', code, 1, code * 71)
|
|
add('SET', 31, 1, 65535, 1000)
|
|
add('ASK', 31, 1, 65535)
|
|
add('ASK', 31, 1, 0)
|
|
add('SET', 31, 1, 0, 0)
|
|
add('ASK', 31, 1, 0)
|
|
add('ASK', 31, 1, 65535)
|
|
for args in [(-1,1,1,1),(32,1,1,1),(0,-1,1,1),(0,4,1,1),(0,1,-1,1),(0,1,65536,1),(0,1,1,-1),(0,1,1,1001)]:
|
|
add('SET',*args)
|
|
for args in [(-1,0,0),(32,0,0),(1,-1,0),(1,2,0),(1,1,-1),(1,1,65536)]:
|
|
add('ASK',*args)
|
|
add('ADV', -1)
|
|
add('RESET', 1000001)
|
|
add('RESET', -1)
|
|
add('SUMMARY')
|
|
for device in [0,31,-1,32]:
|
|
add('DROP',device)
|
|
add('DROP',device)
|
|
add('VIEW',device)
|
|
add('RESET', 999999)
|
|
add('SET',0,1,0,1)
|
|
add('ADV',1)
|
|
add('ASK',0,1,0)
|
|
add('ADV',1)
|
|
add('ASK',0,0,0)
|
|
add('SUMMARY')
|
|
add('RESET',0)
|
|
rng = random.Random(147391)
|
|
for cycle in range(12):
|
|
add('RESET',rng.randrange(50))
|
|
for device in range(32):
|
|
add('SET',device,rng.randrange(4),rng.choice([0,1,65535,rng.randrange(65536)]),rng.choice([0,1,7,1000]))
|
|
for step in range(75):
|
|
device = rng.randrange(32)
|
|
kind = rng.randrange(10)
|
|
if kind < 5:
|
|
add('ASK',device,rng.randrange(2),rng.choice([0,1,65535,rng.randrange(65536)]))
|
|
elif kind == 5:
|
|
add('ADV',rng.randrange(5))
|
|
elif kind == 6:
|
|
add('SET',device,rng.randrange(4),rng.randrange(65536),rng.choice([0,1,7,1000]))
|
|
elif kind == 7:
|
|
add('DROP',device)
|
|
elif kind == 8:
|
|
add('VIEW',device)
|
|
else:
|
|
add('SUMMARY')
|
|
add('SUMMARY')
|
|
for device in range(32):
|
|
add('VIEW',device)
|
|
return operations
|
|
|
|
|
|
def expected_rows(operations):
|
|
# Records are immutable tuples: code,evidence,stamp,lifetime,revision,checks,accepted,rejected,last.
|
|
records = {}
|
|
clock = 0
|
|
rows = []
|
|
def snapshot(device):
|
|
return [1,device,*records[device]] if device in records else [0,device,-1,-1,-1,-1,0,0,0,0,-1]
|
|
def totals():
|
|
ordered = sorted(records.items())
|
|
values = [r for _,r in ordered]
|
|
codes = [sum(r[0] == c for r in values) for c in range(4)]
|
|
fresh = sum(clock-r[2] <= r[3] for r in values)
|
|
counters = [sum(r[i] for r in values) for i in (5,6,7,4)]
|
|
weights = (5,7,11,13,17,19,23,29,31)
|
|
checksum = sum((device+1)*(3*device+sum(weight*value for weight,value in zip(weights,record))) for device,record in ordered)
|
|
return [len(records),*codes,fresh,*counters,checksum]
|
|
for op,args in operations:
|
|
if op == 'RESET':
|
|
ok = 0 <= args[0] <= 1000000
|
|
if ok:
|
|
records = {}
|
|
clock = args[0]
|
|
row = [0,int(ok),clock]
|
|
elif op == 'SET':
|
|
device,code,evidence,lifetime = args
|
|
ok = 0 <= device < 32 and 0 <= code <= 3 and 0 <= evidence <= 65535 and 0 <= lifetime <= 1000
|
|
if ok:
|
|
previous = records.get(device,(0,0,0,0,0,0,0,0,-1))
|
|
records[device] = (code,evidence,clock,lifetime,previous[4]+1,*previous[5:])
|
|
row = [1,int(ok),*snapshot(device)]
|
|
elif op == 'ADV':
|
|
ok = 0 <= args[0] <= 1000000-clock
|
|
clock += args[0] if ok else 0
|
|
row = [3,int(ok),clock]
|
|
elif op == 'ASK':
|
|
device,strict,evidence = args
|
|
valid = 0 <= device < 32 and strict in (0,1) and 0 <= evidence <= 65535
|
|
if not valid:
|
|
verdict = 6
|
|
elif device not in records:
|
|
verdict = -1
|
|
else:
|
|
r = records[device]
|
|
if clock-r[2] > r[3]:
|
|
verdict = 5
|
|
elif r[0] != 1:
|
|
verdict = {0:0,2:2,3:3}[r[0]]
|
|
else:
|
|
verdict = 4 if strict and evidence != r[1] else 1
|
|
records[device] = (*r[:5],r[5]+1,r[6]+int(verdict==1),r[7]+int(verdict!=1),verdict)
|
|
row = [2,verdict,device,strict,evidence,*snapshot(device)]
|
|
elif op == 'VIEW':
|
|
row = [4,*snapshot(args[0])]
|
|
elif op == 'DROP':
|
|
ok = args[0] in records
|
|
records.pop(args[0],None)
|
|
row = [5,int(ok),*totals()]
|
|
elif op == 'SUMMARY':
|
|
row = [6,clock,*totals()]
|
|
else:
|
|
raise ValueError(op)
|
|
rows.append(row)
|
|
return rows
|
|
|
|
|
|
def input_bytes(operations):
|
|
lines = []
|
|
for index,(op,args) in enumerate(operations):
|
|
separator = [' ','\t',' \r\t'][index%3]
|
|
strings = [str(value) for value in args]
|
|
if index%7 == 0:
|
|
strings = [('+' if value >= 0 else '-')+'000'+str(abs(value)) for value in args]
|
|
lines.append(separator.join([op,*strings]))
|
|
return ('\n'.join(lines)+'\n').encode('ascii')
|
|
|
|
|
|
def parse_output(raw):
|
|
if not raw.endswith(b'\n'):
|
|
raise ValueError('missing final LF')
|
|
rows = []
|
|
for line in raw[:-1].split(b'\n'):
|
|
tokens = re.split(rb'[ \t\r]+',line.strip(b' \t\r'))
|
|
if not tokens or any(re.fullmatch(rb'[+-]?[0-9]+',token) is None for token in tokens):
|
|
raise ValueError('nondecimal output token')
|
|
values = []
|
|
for token in tokens:
|
|
negative = token[:1] == b'-'
|
|
digits = token.lstrip(b'+-').lstrip(b'0') or b'0'
|
|
if len(digits) > 19:
|
|
raise ValueError('output integer outside int64')
|
|
value = int(digits) * (-1 if negative else 1)
|
|
if not -9223372036854775808 <= value <= 9223372036854775807:
|
|
raise ValueError('output integer outside int64')
|
|
values.append(value)
|
|
rows.append(values)
|
|
return rows
|
|
|
|
|
|
COMPILE_SECONDS = 20
|
|
CANDIDATE_SECONDS = 8
|
|
CLEANUP_SECONDS = 5
|
|
ABSENCE_SECONDS = 5
|
|
OBSERVER = '#include "probe.hpp"\n#include <cstdint>\n#include <iostream>\n#include <limits>\n#include <sstream>\n#include <string>\n#include <vector>\n\nnamespace {\nbool horizontal(char value) {\n return value == \' \' || value == \'\\t\' || value == \'\\r\';\n}\n\nstd::vector<std::string> tokens(const std::string& line) {\n std::vector<std::string> result;\n std::string current;\n for (char value : line) {\n if (horizontal(value)) {\n if (!current.empty()) {\n result.push_back(current);\n current.clear();\n }\n } else {\n current.push_back(value);\n }\n }\n if (!current.empty()) {\n result.push_back(current);\n }\n return result;\n}\n\nbool integer(const std::string& text, std::int64_t& value) {\n if (text.empty()) {\n return false;\n }\n std::size_t index = 0;\n bool negative = false;\n if (text[index] == \'+\' || text[index] == \'-\') {\n negative = text[index] == \'-\';\n ++index;\n }\n if (index == text.size()) {\n return false;\n }\n std::uint64_t magnitude = 0;\n const std::uint64_t limit = negative ? 9223372036854775808ULL : 9223372036854775807ULL;\n for (; index < text.size(); ++index) {\n char digit = text[index];\n if (digit < \'0\' || digit > \'9\') {\n return false;\n }\n unsigned number = static_cast<unsigned>(digit - \'0\');\n if (magnitude > (limit - number) / 10) {\n return false;\n }\n magnitude = magnitude * 10 + number;\n }\n if (negative && magnitude == 9223372036854775808ULL) {\n value = std::numeric_limits<std::int64_t>::min();\n } else {\n value = negative ? -static_cast<std::int64_t>(magnitude) : static_cast<std::int64_t>(magnitude);\n }\n return true;\n}\n\nbool narrow(std::int64_t value) {\n return value >= std::numeric_limits<int>::min() && value <= std::numeric_limits<int>::max();\n}\n\nvoid emit(const std::vector<std::int64_t>& values) {\n for (std::size_t i = 0; i < values.size(); ++i) {\n if (i != 0) {\n std::cout << \' \';\n }\n std::cout << values[i];\n }\n std::cout << \'\\n\';\n}\n\nvoid append(std::vector<std::int64_t>& row, const std::vector<std::int64_t>& extra) {\n row.insert(row.end(), extra.begin(), extra.end());\n}\n\nbool run(probe::Registry& registry, const std::vector<std::string>& words) {\n std::vector<std::int64_t> args;\n for (std::size_t i = 1; i < words.size(); ++i) {\n std::int64_t value;\n if (!integer(words[i], value)) {\n return false;\n }\n args.push_back(value);\n }\n const std::string& command = words[0];\n if (command == "RESET" && args.size() == 1) {\n emit({0, registry.reset(args[0]) ? 1 : 0, registry.now()});\n return true;\n }\n if (command == "ADV" && args.size() == 1) {\n emit({3, registry.advance(args[0]) ? 1 : 0, registry.now()});\n return true;\n }\n for (std::int64_t value : args) {\n if (!narrow(value)) {\n return false;\n }\n }\n if (command == "SET" && args.size() == 4) {\n int id = static_cast<int>(args[0]);\n bool ok = registry.set(id, static_cast<int>(args[1]), static_cast<int>(args[2]), static_cast<int>(args[3]));\n std::vector<std::int64_t> row = {1, ok ? 1 : 0};\n append(row, probe::snapshot_fields(registry.view(id)));\n emit(row);\n return true;\n }\n if (command == "ASK" && args.size() == 3) {\n probe::Query query{static_cast<int>(args[0]), static_cast<int>(args[1]), static_cast<int>(args[2])};\n int verdict = registry.ask(query);\n std::vector<std::int64_t> row = {2, verdict, query.id, query.strict, query.evidence};\n append(row, probe::snapshot_fields(registry.view(query.id)));\n emit(row);\n return true;\n }\n if (command == "VIEW" && args.size() == 1) {\n std::vector<std::int64_t> row = {4};\n append(row, probe::snapshot_fields(registry.view(static_cast<int>(args[0]))));\n emit(row);\n return true;\n }\n if (command == "DROP" && args.size() == 1) {\n bool ok = registry.drop(static_cast<int>(args[0]));\n std::vector<std::int64_t> row = {5, ok ? 1 : 0};\n append(row, probe::total_fields(registry.totals()));\n emit(row);\n return true;\n }\n if (command == "SUMMARY" && args.empty()) {\n std::vector<std::int64_t> row = {6, registry.now()};\n append(row, probe::total_fields(registry.totals()));\n emit(row);\n return true;\n }\n return false;\n}\n}\n\nint main() {\n probe::Registry registry;\n std::string line;\n while (std::getline(std::cin, line)) {\n auto words = tokens(line);\n if (words.empty()) {\n continue;\n }\n if (!run(registry, words)) {\n return 2;\n }\n }\n return std::cin.bad() ? 2 : 0;\n}\n'
|
|
CANDIDATE_FILES = ['include/probe.hpp', 'src/registry.cpp', 'src/decision.cpp', 'src/summary.cpp']
|
|
|
|
def main():
|
|
evidence = []
|
|
failures = []
|
|
accepted = False
|
|
cleanup_ok = False
|
|
absence_ok = False
|
|
scratch = None
|
|
operations = make_cases()
|
|
payload = input_bytes(operations)
|
|
expected = expected_rows(operations)
|
|
expected_bytes = ('\n'.join(' '.join(map(str,row)) for row in expected)+'\n').encode('ascii')
|
|
try:
|
|
scratch = Path(tempfile.mkdtemp(prefix='probe-status-'))
|
|
project = scratch/'project'
|
|
project.mkdir()
|
|
os.chmod(project,0o777)
|
|
for name in CANDIDATE_FILES:
|
|
target = project/name
|
|
target.parent.mkdir(parents=True,exist_ok=True)
|
|
os.chmod(target.parent,0o777)
|
|
target.write_bytes((Path(sys.argv[1])/name).read_bytes())
|
|
os.chmod(target,0o644)
|
|
(project/'observer.cpp').write_text(OBSERVER)
|
|
os.chmod(project/'observer.cpp',0o644)
|
|
# All candidate inputs and public support are ready before the first child.
|
|
compile_argv = ['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic','-Iinclude',
|
|
'src/registry.cpp','src/decision.cpp','src/summary.cpp','observer.cpp','-o','probe-demo']
|
|
execute_stage('compiler',compile_argv,project,COMPILE_SECONDS,b'',evidence)
|
|
out,err = execute_stage('candidate',[str(project/'probe-demo')],project,CANDIDATE_SECONDS,payload,evidence)
|
|
actual = parse_output(out)
|
|
if actual != expected:
|
|
mismatch = next((i for i,(a,b) in enumerate(zip(actual,expected)) if a != b), min(len(actual),len(expected)))
|
|
raise ValueError('behavior difference at row '+str(mismatch)+' actual_rows='+str(len(actual))+' expected_rows='+str(len(expected)))
|
|
accepted = 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)],'/tmp',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)],'/tmp',ABSENCE_SECONDS,b'',evidence)
|
|
absence_ok = True
|
|
except BaseException as error:
|
|
failures.append(['absence',type(error).__name__,str(error)])
|
|
record = dict(evidence=evidence, failures=failures, accepted=accepted,
|
|
cleanup_ok=cleanup_ok,absence_ok=absence_ok,scratch=None if scratch is None else str(scratch),
|
|
input_len=len(payload),input_sha256=hashlib.sha256(payload).hexdigest(),
|
|
expected_rows=len(expected),expected_len=len(expected_bytes),expected_sha256=hashlib.sha256(expected_bytes).hexdigest())
|
|
packed = base64.b85encode(bz2.compress(json.dumps(record,separators=(',',':')).encode('utf-8'))).decode('ascii')
|
|
sys.stderr.write('PROBE_EVIDENCE_B85='+packed+'\n')
|
|
if accepted and cleanup_ok and absence_ok and not failures:
|
|
print('PASS')
|
|
return 0
|
|
print('FAIL: required behavior, execution or recovery rejected')
|
|
return 1
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|