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>
305 lines
22 KiB
Python
305 lines
22 KiB
Python
SUPERVISOR = "import base64, ctypes, errno, hashlib, json, os, selectors, signal, subprocess, sys, time\n\ndef snapshot():\n table = {}\n for name in os.listdir('/proc'):\n if not name.isdecimal():\n continue\n try:\n text = open('/proc/' + name + '/stat').read()\n rest = text[text.rfind(')') + 2:].split()\n table[int(name)] = (int(rest[1]), int(rest[2]), int(rest[19]), rest[0])\n except (FileNotFoundError, ProcessLookupError):\n continue\n return table\n\ndef discover(table, supervisor, tracked):\n # Current real kinship is authoritative; an old token is a root only while\n # the same live (non-zombie/non-dead) identity still exists.\n roots = {supervisor}\n roots.update(pid for pid, start in tracked.items()\n if pid in table and table[pid][2] == start and table[pid][3] not in ('Z', 'X'))\n found = set()\n while True:\n more = {pid for pid, row in table.items() if pid != supervisor and row[0] in roots}\n if more <= found:\n break\n found.update(more)\n roots.update(more)\n return {pid: table[pid][2] for pid in found}\n\ndef identity_alive(table, pid, start):\n return pid in table and table[pid][2] == start\n\ndef run(request):\n start_clock = time.monotonic()\n deadline = request['deadline']\n exceptions = []\n proc = None\n tracked = {}\n out, err = bytearray(), bytearray()\n leader_start = None\n leader_rc = None\n timed_out = False\n leader_reaped = False\n descendants_absent = False\n group_absent = False\n selector = selectors.DefaultSelector()\n supervisor = os.getpid()\n try:\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')\n proc = subprocess.Popen(request['argv'], cwd=request['cwd'],\n stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE, start_new_session=True)\n first = snapshot()\n if proc.pid not in first:\n raise RuntimeError('missing leader identity')\n leader_start = first[proc.pid][2]\n tracked[proc.pid] = leader_start\n pending = memoryview(base64.b64decode(request['input']))\n for stream, kind in ((proc.stdout, 'out'), (proc.stderr, 'err'), (proc.stdin, 'in')):\n os.set_blocking(stream.fileno(), False)\n if kind != 'in' or pending:\n selector.register(stream, selectors.EVENT_WRITE if kind == 'in' else selectors.EVENT_READ, kind)\n else:\n stream.close()\n while True:\n leader_rc = proc.poll()\n current = snapshot()\n tracked.update(discover(current, supervisor, tracked))\n readable = any(key.data != 'in' for key in selector.get_map().values())\n if leader_rc is not None and not readable:\n break\n if time.monotonic() - start_clock >= deadline:\n timed_out = True\n break\n for key, mask in selector.select(0.01):\n stream, kind = key.fileobj, key.data\n if kind == 'in':\n try:\n amount = os.write(stream.fileno(), pending[:65536])\n pending = pending[amount:]\n except BrokenPipeError:\n pending = pending[len(pending):]\n if not pending:\n selector.unregister(stream)\n stream.close()\n else:\n try:\n raw = os.read(stream.fileno(), 65536)\n except BlockingIOError:\n continue\n if raw:\n (out if kind == 'out' else err).extend(raw)\n else:\n selector.unregister(stream)\n stream.close()\n except BaseException as error:\n exceptions.append(type(error).__name__ + ':' + str(error))\n finally:\n # Always poll our own Popen before reaping adopted children: otherwise\n # waitpid(-1) can steal the leader's genuine nonzero exit status.\n recovery_end = time.monotonic() + 3.0\n try:\n while time.monotonic() < recovery_end:\n if proc is not None:\n leader_rc = proc.poll()\n current = snapshot()\n tracked.update(discover(current, supervisor, tracked))\n targets = [(pid, token) for pid, token in tracked.items()\n if identity_alive(current, pid, token)]\n for pid, token in targets:\n now = snapshot()\n if identity_alive(now, pid, token) and now[pid][3] not in ('Z', 'X'):\n try:\n os.kill(pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n if proc is not None:\n leader_rc = proc.poll()\n # Adopted children only, after own leader is observed/reaped.\n if proc is None or leader_rc is not None:\n while True:\n try:\n child, _ = os.waitpid(-1, os.WNOHANG)\n if child == 0:\n break\n except ChildProcessError:\n break\n current = snapshot()\n tracked.update(discover(current, supervisor, tracked))\n if not any(identity_alive(current, pid, token) for pid, token in tracked.items()):\n descendants_absent = True\n break\n time.sleep(0.01)\n if proc is not None:\n leader_rc = proc.poll()\n leader_reaped = leader_rc is not None\n current = snapshot()\n group_absent = proc is not None and not any(row[1] == proc.pid for row in current.values())\n # Drain bytes that arrived just before termination. No stderr cap.\n for key in list(selector.get_map().values()):\n stream, kind = key.fileobj, key.data\n if kind != 'in':\n while True:\n try:\n raw = os.read(stream.fileno(), 65536)\n except BlockingIOError:\n break\n if not raw:\n break\n (out if kind == 'out' else err).extend(raw)\n selector.unregister(stream)\n stream.close()\n except BaseException as error:\n exceptions.append(type(error).__name__ + ':' + str(error))\n selector.close()\n stdout, stderr = bytes(out), bytes(err)\n return {'meta': {'supervisor_pid': supervisor, 'leader_pid': proc.pid if proc else None,\n 'leader_start': leader_start, 'uid': os.getuid(), 'gid': os.getgid(),\n 'groups': os.getgroups(), 'deadline': deadline,\n 'elapsed': time.monotonic() - start_clock, 'leader_rc': leader_rc,\n 'controlled_rc': 124 if timed_out else leader_rc, 'timeout': timed_out,\n 'leader_reaped': leader_reaped, 'descendants_absent': descendants_absent,\n 'group_absent': group_absent, 'exceptions': exceptions,\n 'stdout_len': len(stdout), 'stderr_len': len(stderr),\n 'stdout_sha256': hashlib.sha256(stdout).hexdigest(),\n 'stderr_sha256': hashlib.sha256(stderr).hexdigest()},\n 'stdout': base64.b64encode(stdout).decode(), 'stderr': base64.b64encode(stderr).decode()}\n\nif __name__ == '__main__':\n try:\n request = json.loads(sys.stdin.buffer.read())\n result = run(request)\n sys.stdout.buffer.write(json.dumps(result, separators=(',', ':')).encode())\n except BaseException as error:\n sys.stderr.buffer.write((type(error).__name__ + ':' + str(error)).encode('utf-8', 'replace'))\n raise SystemExit(125)\n"
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import zlib
|
|
|
|
CANDIDATES = ['descriptor_table.c', 'batch.c']
|
|
DEADLINES = {'compiler': 20, 'candidate': 12, 'cleanup': 4, 'absence': 3}
|
|
RECOVERY_ALLOWANCE = 4
|
|
CLEANUP_CODE = 'import shutil,sys; shutil.rmtree(sys.argv[1])'
|
|
ABSENCE_CODE = '''import os,sys
|
|
try:
|
|
os.lstat(sys.argv[1])
|
|
except FileNotFoundError:
|
|
raise SystemExit(0)
|
|
raise SystemExit(1)
|
|
'''
|
|
|
|
class Oracle:
|
|
def __init__(self):
|
|
self.slots = [dict(phase=0, epoch=0, capacity=0, alignment=0, jobs=[]) for _ in range(2)]
|
|
self.creates = self.releases = 0
|
|
self.commands, self.expected, self.labels = [], [], []
|
|
|
|
def emit(self, command, label):
|
|
parts = command.split()
|
|
op, slot = parts[0], int(parts[1])
|
|
values = list(map(int, parts[2:]))
|
|
state = self.slots[slot]
|
|
phase = state['phase']
|
|
rc = 0
|
|
if op == 'B':
|
|
capacity, alignment = values
|
|
if capacity not in range(1, 17) or alignment not in (32, 64, 128):
|
|
rc = 1
|
|
elif phase in (1, 2) or state['epoch'] == 2**32 - 1:
|
|
rc = 2
|
|
else:
|
|
self.creates += 1
|
|
self.releases += int(state['capacity'] != 0)
|
|
state.update(phase=1, epoch=state['epoch']+1, capacity=capacity, alignment=alignment, jobs=[])
|
|
elif op == 'A':
|
|
address, length, control = values
|
|
if phase != 1:
|
|
rc = 2
|
|
elif not 1 <= length <= 1048576 or control > 3 or address + length >= 2**64:
|
|
rc = 1
|
|
elif len(state['jobs']) == state['capacity']:
|
|
rc = 3
|
|
else:
|
|
state['jobs'].append(values)
|
|
elif op == 'S':
|
|
if phase != 1 or not state['jobs']:
|
|
rc = 2
|
|
else:
|
|
state['phase'] = 2
|
|
elif op == 'C':
|
|
if phase != 2:
|
|
rc = 2
|
|
elif values[0] != (state['epoch'] << 32) | len(state['jobs']):
|
|
rc = 5
|
|
else:
|
|
state['phase'] = 3
|
|
elif op in ('X', 'D'):
|
|
self.releases += int(state['capacity'] != 0)
|
|
state.update(phase=0, capacity=0, alignment=0, jobs=[])
|
|
if op == 'D':
|
|
state['epoch'] = 0
|
|
memory = [sum(s['capacity'] != 0 for s in self.slots),
|
|
sum(s['capacity'] * s['alignment'] for s in self.slots), self.creates, self.releases]
|
|
view = [state['phase'], state['epoch'], len(state['jobs']), state['capacity'], state['alignment'], state['alignment']]
|
|
wire = []
|
|
if state['phase'] in (2, 3):
|
|
cookie = (state['epoch'] << 32) | len(state['jobs'])
|
|
for sequence, (address, length, control) in enumerate(state['jobs']):
|
|
raw = struct.pack('<QIIQI', address, length, control, cookie, sequence)
|
|
checksum = 2166136261
|
|
for byte in raw:
|
|
checksum = ((checksum ^ byte) * 16777619) & 0xffffffff
|
|
wire.append([address, length, control, cookie, sequence, checksum])
|
|
self.commands.append(command)
|
|
self.expected.append({'rc':rc, 'view':view, 'memory':memory, 'wire':wire})
|
|
self.labels.append(label)
|
|
|
|
|
|
def cases():
|
|
c, rng = Oracle(), random.Random(79151023)
|
|
def e(text, label='C10'):
|
|
c.emit(text, label)
|
|
for slot in (0, 1):
|
|
e('Q %d' % slot, 'C01'); e('S %d' % slot, 'C05')
|
|
e('C %d 0' % slot, 'C07'); e('A %d 0 0 9' % slot, 'C04')
|
|
for capacity in (0, 17, 2**64-1):
|
|
e('B 0 %d 32' % capacity, 'C02')
|
|
for alignment in (0, 1, 16, 33, 256, 2**64-1):
|
|
e('B 0 1 %d' % alignment, 'C02')
|
|
for align in (32, 64, 128):
|
|
for cap in (1, 2, 3, 16):
|
|
e('B 0 %d %d' % (cap, align), 'C03')
|
|
e('B 0 0 17', 'C02'); e('B 0 2 64', 'C02')
|
|
e('S 0', 'C05')
|
|
for address, length, control in [(0,0,0), (0,1048577,0), (0,1,4), (2**64-1,1,0), (2**64-2,2,0)]:
|
|
e('A 0 %d %d %d' % (address,length,control), 'C04')
|
|
for index in range(cap):
|
|
address = [0, 2**64-1048577, rng.randrange(2**62)][index%3]
|
|
length = [1, 1048576, rng.randrange(1,1048577)][index%3]
|
|
e('A 0 %d %d %d' % (address,length,index%4), 'C04')
|
|
e('A 0 1 1 0', 'C04'); e('A 0 0 0 0', 'C04')
|
|
e('S 0', 'C06'); e('S 0', 'C05'); e('A 0 9 2 1', 'C04')
|
|
current = (c.slots[0]['epoch'] << 32) | cap
|
|
e('C 0 %d' % (current ^ 1), 'C07'); e('C 0 %d' % current, 'C07')
|
|
e('C 0 %d' % current, 'C07'); e('Q 0', 'C06')
|
|
e('B 0 2 64', 'C08'); e('A 0 234 15 3', 'C04'); e('S 0', 'C06')
|
|
e('C 0 %d' % current, 'C07'); e('X 0', 'C08'); e('X 0', 'C08')
|
|
for trial in range(90):
|
|
slot = trial % 2
|
|
state = c.slots[slot]
|
|
previous_cookie = (state['epoch'] << 32) | len(state['jobs'])
|
|
if state['phase'] in (1,2):
|
|
e('X %d' % slot, 'C08')
|
|
cap, align = rng.randrange(1,17), rng.choice((32,64,128))
|
|
e('B %d %d %d' % (slot,cap,align), 'C03')
|
|
count = rng.randrange(1,cap+1)
|
|
for index in range(count):
|
|
e('A %d %d %d %d' % (slot,rng.randrange(2**63),rng.randrange(1,1048577),rng.randrange(4)), 'C06')
|
|
e('Q %d' % (1-slot), 'C09'); e('S %d' % slot, 'C06')
|
|
e('C %d %d' % (slot,previous_cookie), 'C07')
|
|
cookie = (c.slots[slot]['epoch'] << 32) | count
|
|
if trial % 3:
|
|
e('C %d %d' % (slot,cookie), 'C07')
|
|
else:
|
|
e('X %d' % slot, 'C08')
|
|
if trial % 7 == 0:
|
|
e('D %d' % slot, 'C08')
|
|
for slot in (0,1):
|
|
e('X %d' % slot, 'C08'); e('D %d' % slot, 'C08'); e('Q %d' % slot,'C09')
|
|
return c
|
|
|
|
|
|
def positive(value):
|
|
return type(value) is int and value > 0
|
|
|
|
|
|
def validate(wire, deadline, expected_identity):
|
|
if type(wire) is not dict or set(wire) != {'meta','stdout','stderr'}:
|
|
raise ValueError('wire fields')
|
|
meta = wire['meta']
|
|
required = {'supervisor_pid','leader_pid','leader_start','uid','gid','groups','deadline','elapsed',
|
|
'leader_rc','controlled_rc','timeout','leader_reaped','descendants_absent','group_absent',
|
|
'exceptions','stdout_len','stderr_len','stdout_sha256','stderr_sha256'}
|
|
if type(meta) is not dict or set(meta) != required:
|
|
raise ValueError('metadata fields')
|
|
if not all(positive(meta[name]) for name in ('supervisor_pid','leader_pid','leader_start')):
|
|
raise ValueError('PID/start')
|
|
if meta['supervisor_pid'] == meta['leader_pid']:
|
|
raise ValueError('PID conflict')
|
|
if any(type(meta[name]) is not int for name in ('uid','gid')) or type(meta['groups']) is not list:
|
|
raise ValueError('identity types')
|
|
if any(type(item) is not int or item < 0 for item in meta['groups']):
|
|
raise ValueError('groups')
|
|
if [meta['uid'],meta['gid'],sorted(meta['groups'])] != expected_identity:
|
|
raise ValueError('execution identity')
|
|
if type(meta['deadline']) is not int or meta['deadline'] != deadline:
|
|
raise ValueError('deadline')
|
|
elapsed = meta['elapsed']
|
|
if type(elapsed) not in (int,float) or not math.isfinite(elapsed) or not 0 <= elapsed <= deadline + RECOVERY_ALLOWANCE:
|
|
raise ValueError('elapsed')
|
|
if type(meta['timeout']) is not bool:
|
|
raise ValueError('timeout type')
|
|
if type(meta['leader_rc']) is not int or type(meta['controlled_rc']) is not int:
|
|
raise ValueError('returncode type')
|
|
if meta['controlled_rc'] != (124 if meta['timeout'] else meta['leader_rc']):
|
|
raise ValueError('returncode conflict')
|
|
for name in ('leader_reaped','descendants_absent','group_absent'):
|
|
if type(meta[name]) is not bool or not meta[name]:
|
|
raise ValueError(name)
|
|
if type(meta['exceptions']) is not list or meta['exceptions']:
|
|
raise ValueError('supervisor exceptions')
|
|
outputs = []
|
|
for kind in ('stdout','stderr'):
|
|
if type(meta[kind+'_len']) is not int or meta[kind+'_len'] < 0 or type(wire[kind]) is not str:
|
|
raise ValueError('IO length/type')
|
|
raw = base64.b64decode(wire[kind], validate=True)
|
|
if len(raw) != meta[kind+'_len'] or hashlib.sha256(raw).hexdigest() != meta[kind+'_sha256']:
|
|
raise ValueError('IO binding')
|
|
outputs.append(raw)
|
|
if meta['leader_rc'] != 0 or meta['controlled_rc'] != 0 or meta['timeout']:
|
|
raise ValueError('nonzero/timeout')
|
|
return outputs
|
|
|
|
|
|
def phase(name, argv, cwd, payload, log):
|
|
request = {'argv':argv, 'cwd':str(cwd), 'input':base64.b64encode(payload).decode(), 'deadline':DEADLINES[name]}
|
|
# The wrapper itself executes as candidate identity through the unchanged Bos hook.
|
|
wrapper = subprocess.run([sys.executable,'-I','-c',SUPERVISOR], input=json.dumps(request).encode(),
|
|
capture_output=True, timeout=DEADLINES[name]+8)
|
|
entry = {'phase':name, 'wrapper_rc':wrapper.returncode, 'wire_len':len(wrapper.stdout),
|
|
'wire_sha256':hashlib.sha256(wrapper.stdout).hexdigest(),
|
|
'wrapper_stderr_len':len(wrapper.stderr), 'wrapper_stderr_sha256':hashlib.sha256(wrapper.stderr).hexdigest(),
|
|
'request_sha256':hashlib.sha256(json.dumps(request).encode()).hexdigest()}
|
|
log.append(entry) # Capture actual wrapper facts BEFORE parsing possibly missing metadata.
|
|
wire = json.loads(wrapper.stdout)
|
|
entry['meta'] = wire.get('meta') if isinstance(wire,dict) else None
|
|
if wrapper.returncode != 0 or wrapper.stderr:
|
|
raise ValueError('wrapper nonzero/diagnostic')
|
|
identity = [65534,65534,[]] if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else [os.getuid(),os.getgid(),sorted(os.getgroups())]
|
|
return validate(wire, DEADLINES[name], identity)
|
|
|
|
|
|
def compare(matrix, raw):
|
|
lines = raw.decode('utf-8','strict').splitlines()
|
|
if len(lines) != len(matrix.expected):
|
|
raise ValueError('C11 row count got=%d want=%d' % (len(lines),len(matrix.expected)))
|
|
for index, (line, wanted) in enumerate(zip(lines,matrix.expected)):
|
|
actual = json.loads(line)
|
|
if type(actual) is not dict or set(actual) != {'rc','view','memory','wire'}:
|
|
raise ValueError('C11 output fields at %d' % index)
|
|
view = actual['view']
|
|
if type(view) is not list or len(view) != 7 or any(type(v) is not int or v < 0 for v in view):
|
|
raise ValueError('C11 view at %d' % index)
|
|
base = view[6]
|
|
if wanted['view'][3]:
|
|
if base <= 0 or base % wanted['view'][4]:
|
|
raise ValueError('C03 base address at %d' % index)
|
|
elif base:
|
|
raise ValueError('C08 cleared base at %d' % index)
|
|
normalized = dict(actual)
|
|
normalized['view'] = view[:6]
|
|
records = []
|
|
if type(actual['wire']) is not list:
|
|
raise ValueError('C06 wire type')
|
|
for record_index, record in enumerate(actual['wire']):
|
|
if type(record) is not list or len(record) != 7 or any(type(v) is not int for v in record):
|
|
raise ValueError('C06 record type')
|
|
if record[0] != base + record_index*wanted['view'][4]:
|
|
raise ValueError('C03 slot address')
|
|
records.append(record[1:])
|
|
normalized['wire'] = records
|
|
# JSON booleans must not compare equal to integer observations.
|
|
if json.dumps(normalized,sort_keys=True,separators=(',',':')) != json.dumps(wanted,sort_keys=True,separators=(',',':')):
|
|
raise ValueError('%s observation=%d got=%s want=%s' % (matrix.labels[index],index,repr(normalized)[:400],repr(wanted)[:400]))
|
|
|
|
|
|
def main():
|
|
log, errors = [], []
|
|
scratch = None
|
|
success = False
|
|
cleanup_ok = absence_ok = False
|
|
try:
|
|
matrix = cases()
|
|
context = json.loads((Path(__file__).resolve().parent/'context.json').read_text())
|
|
candidates = {name:(Path(sys.argv[1])/name).read_bytes() for name in CANDIDATES}
|
|
scratch = Path(tempfile.mkdtemp(prefix='descriptor-stride-',dir='/tmp'))
|
|
# All source/support/input setup occurs before the first child. The
|
|
# parent performs only pipe/memory operations after that point.
|
|
for name,text in context.items():
|
|
path = scratch/name
|
|
path.parent.mkdir(parents=True,exist_ok=True)
|
|
path.write_text(text)
|
|
for directory in sorted({(scratch/name).parent for name in context}):
|
|
if directory != scratch:
|
|
os.chmod(directory,0o777)
|
|
for name,raw in candidates.items():
|
|
(scratch/name).write_bytes(raw)
|
|
payload = ('\n'.join(matrix.commands)+'\n').encode()
|
|
phase('compiler',['make','--no-print-directory'],scratch,b'',log)
|
|
output, diagnostics = phase('candidate',[str(scratch/'descriptor-observe')],scratch,payload,log)
|
|
compare(matrix,output)
|
|
success = True
|
|
except BaseException as error:
|
|
errors.append(type(error).__name__ + ':' + str(error))
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
phase('cleanup',[sys.executable,'-I','-c',CLEANUP_CODE,str(scratch)],'/tmp',b'',log)
|
|
cleanup_ok = True
|
|
except BaseException as error:
|
|
errors.append('cleanup:' + type(error).__name__ + ':' + str(error))
|
|
try:
|
|
phase('absence',[sys.executable,'-I','-c',ABSENCE_CODE,str(scratch)],'/tmp',b'',log)
|
|
absence_ok = True
|
|
except BaseException as error:
|
|
errors.append('absence:' + type(error).__name__ + ':' + str(error))
|
|
evidence = {'phases':log,'exceptions':errors,'success':success,'cleanup_ok':cleanup_ok,'absence_ok':absence_ok}
|
|
# Compressed complete phase metadata fits Bos's 4000-character tail. Raw
|
|
# inner wires/IO are not retained here: their ACTUAL lengths/SHA are logged.
|
|
packed = base64.b64encode(zlib.compress(json.dumps(evidence,separators=(',',':')).encode(),9)).decode()
|
|
print('EXECUTION_ZLIB_BASE64 '+packed,file=sys.stderr)
|
|
if success and cleanup_ok and absence_ok and not errors:
|
|
print('PASS')
|
|
return 0
|
|
print('FAIL ' + ('; '.join(errors) or 'incomplete execution')[:280])
|
|
return 1
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|