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>
277 lines
21 KiB
Python
277 lines
21 KiB
Python
"""Independent pin-state model; only identity-owned child processes execute C."""
|
||
import base64
|
||
import hashlib
|
||
import json
|
||
import math
|
||
import os
|
||
from pathlib import Path
|
||
import random
|
||
import re
|
||
import secrets
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
import zlib
|
||
SUPERVISOR = "import base64\nimport ctypes\nimport hashlib\nimport json\nimport os\nimport signal\nimport subprocess\nimport sys\nimport time\n\n\ndef stat_pid(pid):\n try:\n data = open('/proc/%d/stat' % pid, 'rb').read()\n except FileNotFoundError:\n return None\n rest = data[data.rfind(b')') + 2:].split()\n return dict(pid=pid, state=rest[0].decode('ascii'), ppid=int(rest[1]),\n pgid=int(rest[2]), start=int(rest[19]))\n\n\ndef identity_pid(pid):\n fields = {}\n with open('/proc/%d/status' % pid, 'r', encoding='ascii') as stream:\n for line in stream:\n key, separator, value = line.partition(':')\n if separator and key in ('Uid', 'Gid', 'Groups'):\n fields[key] = [int(x) for x in value.split()]\n return dict(uid=fields['Uid'][1], gid=fields['Gid'][1], groups=fields['Groups'])\n\n\ndef proc_table():\n table = {}\n for entry in os.listdir('/proc'):\n if entry.isascii() and entry.isdecimal():\n row = stat_pid(int(entry))\n if row is not None:\n table[row['pid']] = row\n return table\n\n\ndef relatives(table, supervisor, tracked):\n roots = {supervisor}\n for pid, start in tracked:\n row = table.get(pid)\n if row and row['start'] == start and row['state'] not in ('Z', 'X'):\n roots.add(pid)\n changed = True\n while changed:\n changed = False\n for pid, row in table.items():\n if row['ppid'] in roots and pid not in roots:\n roots.add(pid)\n changed = True\n roots.discard(supervisor)\n return {pid: table[pid] for pid in roots if pid in table}\n\n\ndef signal_token(pid, start, read=stat_pid, send=os.kill):\n # A fresh per-PID stat is mandatory even after discovery and Popen.poll.\n row = read(pid)\n if row is None or row['start'] != start or row['state'] in ('Z', 'X'):\n return False\n send(pid, signal.SIGKILL)\n return True\n\n\ndef run(config):\n begin = time.monotonic()\n errors = []\n tracked = set()\n signals = []\n adopted = []\n timeout = False\n output = b''\n diagnostic = b''\n child = None\n leader_start = 0\n leader_identity = None\n deadline = config['deadline']\n supervisor = os.getpid()\n supervisor_start = stat_pid(supervisor)['start']\n identity = dict(uid=os.geteuid(), gid=os.getegid(), groups=os.getgroups())\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 child = subprocess.Popen(config['argv'], cwd=config['cwd'],\n stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE, start_new_session=True)\n leader = stat_pid(child.pid)\n if leader is None:\n raise RuntimeError('leader stat unavailable before poll')\n leader_start = leader['start']\n leader_identity = identity_pid(child.pid)\n tracked.add((child.pid, leader_start))\n try:\n output, diagnostic = child.communicate(\n base64.b64decode(config['input'], validate=True), timeout=deadline)\n except subprocess.TimeoutExpired as error:\n timeout = True\n output = error.output or b''\n diagnostic = error.stderr or b''\n except BaseException as error:\n errors.append('execute:' + repr(error))\n finally:\n recovery_end = time.monotonic() + 2.0\n while time.monotonic() < recovery_end:\n try:\n # Popen owns its leader; never waitpid(-1) and steal that status.\n if child is not None:\n child.poll()\n table = proc_table()\n found = relatives(table, supervisor, tracked)\n tracked.update((pid, row['start']) for pid, row in found.items())\n for pid, row in found.items():\n if row['state'] not in ('Z', 'X'):\n try:\n if signal_token(pid, row['start']):\n signals.append([pid, row['start']])\n except ProcessLookupError:\n pass\n if child is not None:\n child.poll()\n # Only reap known current adopted children, excluding own leader.\n for pid, start in sorted(tracked):\n if child is not None and pid == child.pid:\n continue\n row = stat_pid(pid)\n if row and row['start'] == start and row['ppid'] == supervisor:\n try:\n got, status = os.waitpid(pid, os.WNOHANG)\n if got:\n adopted.append([pid, start, status])\n except ChildProcessError:\n pass\n current = proc_table()\n remaining = relatives(current, supervisor, tracked)\n if not remaining and (child is None or child.poll() is not None):\n break\n except BaseException as error:\n errors.append('recovery:' + repr(error))\n break\n time.sleep(0.01)\n if child is not None:\n try:\n # communicate caches the complete bytes, including timeout prefixes.\n output, diagnostic = child.communicate(timeout=0.3)\n except subprocess.TimeoutExpired as error:\n output = error.output or output\n diagnostic = error.stderr or diagnostic\n errors.append('pipes did not close after bounded recovery')\n for stream in (child.stdin, child.stdout, child.stderr):\n if stream:\n stream.close()\n except BaseException as error:\n errors.append('drain:' + repr(error))\n final = {}\n try:\n if child is not None:\n child.poll()\n table = proc_table()\n final = relatives(table, supervisor, tracked)\n tracked.update((pid, row['start']) for pid, row in final.items())\n surviving = []\n for pid, start in sorted(tracked):\n row = stat_pid(pid)\n if row and row['start'] == start:\n surviving.append([pid, start, row['state'], row['ppid'], row['pgid']])\n except BaseException as error:\n errors.append('absence:' + repr(error))\n surviving = [['unknown']]\n rc = None if child is None else child.returncode\n facts = dict(nonce=config['nonce'], argv=config['argv'], cwd=config['cwd'],\n deadline=deadline, identity=identity, leader_identity=leader_identity, supervisor=supervisor,\n supervisor_start=supervisor_start,\n leader=None if child is None else child.pid, leader_start=leader_start,\n leader_rc=rc, reaped=child is not None and rc is not None,\n timeout=timeout, controlled_rc=124 if timeout else rc,\n elapsed=time.monotonic()-begin, errors=errors,\n descendants_absent=not surviving and not final,\n pg_absent=not surviving and not final, tracked=[list(x) for x in sorted(tracked)],\n surviving=surviving, signals=signals, adopted=adopted,\n stdout=base64.b64encode(output).decode('ascii'),\n stderr=base64.b64encode(diagnostic).decode('ascii'),\n stdout_len=len(output), stderr_len=len(diagnostic),\n stdout_sha256=hashlib.sha256(output).hexdigest(),\n stderr_sha256=hashlib.sha256(diagnostic).hexdigest())\n return facts\n\n\nif __name__ == '__main__':\n print(json.dumps(run(json.loads(sys.argv[1])), separators=(',', ':')))\n"
|
||
BUILD_DEADLINE = 20
|
||
RUN_DEADLINE = 12
|
||
CLEAN_DEADLINE = 5
|
||
ABSENCE_DEADLINE = 5
|
||
RECOVERY_ALLOWANCE = 3
|
||
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
|
||
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: print('ABSENT')\nelse: print('PRESENT'); raise SystemExit(1)"
|
||
|
||
|
||
def scenarios():
|
||
rows = ["B 3 1 7 1 1 0 0 0 0", "B 2 2 3 0 1 1 1 1 0 19 1 1 1 1 1 0", "B 1 0"]
|
||
rng = random.Random(841703)
|
||
for budget in (1, 2, 4, 8):
|
||
for stage in range(1, 5):
|
||
for busy in (0, budget-1, budget, 8):
|
||
delays = [0]*4
|
||
delays[stage-1] = busy
|
||
rows.append("B %d 1 31 1 %d %d %d %d 0" %
|
||
(budget, delays[1], delays[0], delays[2], delays[3]))
|
||
rows.append("B %d 2 0 0 0 0 0 0 %d 17 1 0 0 0 0 0" % (budget, stage))
|
||
for _ in range(150):
|
||
budget = rng.randint(1, 8)
|
||
pins = rng.sample(range(32), rng.randint(0, 16))
|
||
parts = ['B', str(budget), str(len(pins))]
|
||
for pin in pins:
|
||
parts.extend(map(str, [pin, rng.randrange(2), *[rng.randrange(9) for _ in range(4)], rng.choice([0,0,0,1,2,3,4])]))
|
||
rows.append(' '.join(parts))
|
||
rows += ['', 'B', 'B 0 0', 'B 9 0', 'B 1 17', 'B 1 0 1',
|
||
'B 1 1 32 0 0 0 0 0 0', 'B 1 1 0 2 0 0 0 0 0',
|
||
'B 1 1 0 0 9 0 0 0 0', 'B 1 1 0 0 0 9 0 0 0',
|
||
'B 1 1 0 0 0 0 9 0 0', 'B 1 1 0 0 0 0 0 9 0',
|
||
'B 1 1 0 0 0 0 0 0 5', 'B 1 2 0 0 0 0 0 0 0 0 1 0 0 0 0 0',
|
||
'B +1 0', 'B 1_0 0', 'B 01 00', 'B 1 0x0', 'B 1 0\0',
|
||
'B 1 0', 'B 1 1 0 -0 0 0 0 0 0', 'B 1 0junk', 'B 1 0'+' '*4091,
|
||
'\tB\t02 01 0007 01 01 00 00 00 00\r', 'B 8 0']
|
||
return [row.encode('utf-8') for row in rows]
|
||
|
||
|
||
def parse(row):
|
||
if len(row) > 4096 or any(x not in (9,13,32) and not 33 <= x <= 126 for x in row):
|
||
return None
|
||
tokens = re.split(rb'[ \t\r]+', row.strip(b' \t\r'))
|
||
if len(tokens) < 3 or tokens[0] != b'B':
|
||
return None
|
||
if any(re.fullmatch(rb'[0-9]+', x) is None for x in tokens[1:]):
|
||
return None
|
||
numbers = list(map(int,tokens[1:]))
|
||
budget,count = numbers[:2]
|
||
if not 1 <= budget <= 8 or not 0 <= count <= 16 or len(numbers) != 2+count*7:
|
||
return None
|
||
plans = [numbers[2+7*i:9+7*i] for i in range(count)]
|
||
limits = [31,1,8,8,8,8,4]
|
||
if any(any(x > limit for x,limit in zip(plan,limits)) for plan in plans):
|
||
return None
|
||
if len({p[0] for p in plans}) != count:
|
||
return None
|
||
return budget,plans
|
||
|
||
|
||
def expected(rows):
|
||
output=[]
|
||
for sequence,row in enumerate(rows,1):
|
||
request=parse(row)
|
||
if request is None:
|
||
output.append(('REJECT',sequence))
|
||
continue
|
||
budget,plans=request
|
||
observations=[]
|
||
failed=0
|
||
for pin,value,ready,export,direction,write,fatal in plans:
|
||
counters=[0]*6
|
||
state=[0]*4
|
||
rc=0
|
||
stop=0
|
||
for stage,busy in enumerate((export,ready,direction,write),1):
|
||
calls=1 if fatal==stage else min(busy+1,budget)
|
||
counters[stage-1]=calls
|
||
rc=-5 if fatal==stage else -110 if busy>=budget else 0
|
||
if rc:
|
||
stop=stage
|
||
failed+=1
|
||
if state[0]:
|
||
counters[4]=1
|
||
state=[0]*4
|
||
break
|
||
if stage<=3:
|
||
state[stage-1]=1
|
||
else:
|
||
state[3]=value
|
||
observations.append(('P',sequence,pin,rc,stop,*state,*counters))
|
||
output.append(('R',sequence,-1 if failed else 0,len(plans)-failed,failed))
|
||
output.extend(observations)
|
||
return output
|
||
|
||
|
||
def output_number(token):
|
||
sign = -1 if token.startswith(b'-') else 1
|
||
digits = token.lstrip(b'+-').lstrip(b'0') or b'0'
|
||
if len(digits)>20:
|
||
raise ValueError('stdout value outside observable ranges')
|
||
return sign*int(digits)
|
||
|
||
|
||
def observed(data):
|
||
lines=data.split(b'\n')
|
||
if lines[-1:] == [b'']:
|
||
lines.pop()
|
||
result=[]
|
||
for line in lines:
|
||
tokens=re.split(rb'[ \t]+',line.rstrip(b' \t\r'))
|
||
if not tokens or tokens[0] not in (b'R',b'P',b'REJECT'):
|
||
raise ValueError('stdout row tag')
|
||
if any(re.fullmatch(rb'[+-]?[0-9]+',x) is None for x in tokens[1:]):
|
||
raise ValueError('stdout number must be complete ASCII decimal')
|
||
result.append((tokens[0].decode('ascii'), *map(output_number,tokens[1:])))
|
||
return result
|
||
|
||
|
||
def integer(x):
|
||
return type(x) is int
|
||
|
||
|
||
def validate(meta, config, identity):
|
||
required={'nonce','argv','cwd','deadline','identity','leader_identity','supervisor','supervisor_start','leader','leader_start','leader_rc','reaped','timeout','controlled_rc','elapsed','errors','descendants_absent','pg_absent','tracked','surviving','signals','adopted','stdout','stderr','stdout_len','stderr_len','stdout_sha256','stderr_sha256'}
|
||
if type(meta) is not dict or not required <= meta.keys():
|
||
raise ValueError('missing execution metadata')
|
||
for key in ('nonce','argv','cwd','deadline'):
|
||
if type(meta[key]) is not type(config[key]) or meta[key] != config[key]:
|
||
raise ValueError('execution binding '+key)
|
||
ident=meta['identity']
|
||
if type(ident) is not dict or set(ident) != {'uid','gid','groups'}:
|
||
raise ValueError('identity shape')
|
||
if not integer(ident['uid']) or not integer(ident['gid']) or type(ident['groups']) is not list or any(not integer(g) or g<0 for g in ident['groups']) or ident != identity:
|
||
raise ValueError('identity mismatch')
|
||
leader_ident=meta['leader_identity']
|
||
if type(leader_ident) is not dict or set(leader_ident) != {'uid','gid','groups'} or not integer(leader_ident.get('uid')) or not integer(leader_ident.get('gid')) or type(leader_ident.get('groups')) is not list or any(not integer(g) or g<0 for g in leader_ident['groups']) or leader_ident != identity:
|
||
raise ValueError('leader identity mismatch')
|
||
for key in ('supervisor','supervisor_start','leader','leader_start'):
|
||
if not integer(meta[key]) or meta[key]<=0:
|
||
raise ValueError('invalid PID/start '+key)
|
||
if meta['supervisor']==meta['leader']:
|
||
raise ValueError('leader equals supervisor')
|
||
if not integer(meta['leader_rc']) or not integer(meta['controlled_rc']):
|
||
raise ValueError('invalid return code')
|
||
for key in ('reaped','timeout','descendants_absent','pg_absent'):
|
||
if type(meta[key]) is not bool:
|
||
raise ValueError('invalid boolean '+key)
|
||
if meta['controlled_rc'] != (124 if meta['timeout'] else meta['leader_rc']):
|
||
raise ValueError('return code inconsistency')
|
||
elapsed=meta['elapsed']
|
||
if type(elapsed) not in (int,float) or not math.isfinite(elapsed) or not 0<=elapsed<=config['deadline']+RECOVERY_ALLOWANCE:
|
||
raise ValueError('elapsed outside production deadline/recovery')
|
||
for key in ('errors','tracked','surviving','signals','adopted'):
|
||
if type(meta[key]) is not list:
|
||
raise ValueError('invalid list '+key)
|
||
if meta['errors'] or meta['surviving'] or not meta['reaped'] or not meta['descendants_absent'] or not meta['pg_absent']:
|
||
raise ValueError('execution or cleanup uncertainty')
|
||
for key,width in (('tracked',2),('signals',2),('adopted',3)):
|
||
if any(type(row) is not list or len(row)!=width or any(not integer(x) or x<=0 for x in row[:2]) or (width==3 and not integer(row[2])) for row in meta[key]):
|
||
raise ValueError('invalid process token '+key)
|
||
if [meta['leader'],meta['leader_start']] not in meta['tracked']:
|
||
raise ValueError('leader token not tracked')
|
||
if any(row[:2] not in meta['tracked'] for row in meta['signals']+meta['adopted']):
|
||
raise ValueError('unbound descendant token')
|
||
streams=[]
|
||
for stream in ('stdout','stderr'):
|
||
if type(meta[stream]) is not str or not integer(meta[stream+'_len']) or meta[stream+'_len']<0 or type(meta[stream+'_sha256']) is not str:
|
||
raise ValueError('invalid IO metadata')
|
||
raw=base64.b64decode(meta[stream],validate=True)
|
||
if len(raw)!=meta[stream+'_len'] or hashlib.sha256(raw).hexdigest()!=meta[stream+'_sha256']:
|
||
raise ValueError('IO length/SHA mismatch')
|
||
streams.append(raw)
|
||
if meta['timeout'] or meta['leader_rc']!=0 or meta['controlled_rc']!=0:
|
||
raise ValueError('controlled timeout/nonzero leader')
|
||
return streams
|
||
|
||
|
||
def phase(label, argv, cwd, input_data, deadline, ledger):
|
||
config=dict(nonce=secrets.token_hex(8),argv=argv,cwd=str(cwd),input=base64.b64encode(input_data).decode('ascii'),deadline=deadline)
|
||
record=dict(phase=label, config={k:v for k,v in config.items() if k!='input'}, input_offered_len=len(input_data), input_offered_sha256=hashlib.sha256(input_data).hexdigest())
|
||
ledger.append(record)
|
||
identity=dict(uid=65534,gid=65534,groups=[]) if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534' else dict(uid=os.geteuid(),gid=os.getegid(),groups=os.getgroups())
|
||
wrapper=subprocess.Popen([sys.executable,'-I','-c',SUPERVISOR,json.dumps(config,separators=(',',':'))],stdin=subprocess.DEVNULL,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
|
||
try:
|
||
raw,err=wrapper.communicate(timeout=deadline+5)
|
||
except BaseException as error:
|
||
record['wrapper_exception']=repr(error)
|
||
raise
|
||
record.update(wrapper_rc=wrapper.returncode,wire_len=len(raw),wire_sha256=hashlib.sha256(raw).hexdigest(),wrapper_stderr_len=len(err),wrapper_stderr_sha256=hashlib.sha256(err).hexdigest())
|
||
# Capture wrapper facts before parsing, including deliberately incomplete wires.
|
||
packed=base64.b85encode(zlib.compress(raw)).decode('ascii')
|
||
if len(packed)<=900 and (len(raw)<=600 or wrapper.returncode!=0):
|
||
record['wire_z85']=packed
|
||
meta=json.loads(raw)
|
||
record['meta']={k:v for k,v in meta.items() if k not in ('stdout','stderr')} if type(meta) is dict else meta
|
||
for stream in ('stdout','stderr'):
|
||
if type(meta) is dict and type(meta.get(stream)) is str:
|
||
body=base64.b64decode(meta[stream])
|
||
record[stream+'_preview_b64']=base64.b64encode(body[:240]).decode('ascii')
|
||
if wrapper.returncode!=0 or err:
|
||
raise ValueError('supervisor failed')
|
||
return validate(meta,config,identity)
|
||
|
||
|
||
def main():
|
||
ledger=[]
|
||
exceptions=[]
|
||
scratch=None
|
||
success=False
|
||
cleanup_ok=False
|
||
absence_ok=False
|
||
failure=''
|
||
try:
|
||
context=json.loads(Path(__file__).with_name('context.json').read_text())
|
||
candidate=(Path(sys.argv[1])/'bringup.c').read_bytes()
|
||
rows=scenarios()
|
||
answers=expected(rows)
|
||
stdin=b'\n'.join(rows)+b'\n'
|
||
scratch=Path(tempfile.mkdtemp(prefix='dev-pin-bringup-'))
|
||
for name,body in context.items():
|
||
(scratch/name).write_text(body)
|
||
(scratch/'bringup.c').write_bytes(candidate)
|
||
phase('compiler',['make','-s'],scratch,b'',BUILD_DEADLINE,ledger)
|
||
stdout,stderr=phase('candidate',[str(scratch/'pin-console')],scratch,stdin,RUN_DEADLINE,ledger)
|
||
actual=observed(stdout)
|
||
if actual!=answers:
|
||
index=next((i for i,(a,b) in enumerate(zip(actual,answers)) if a!=b),min(len(actual),len(answers)))
|
||
failure='raw observation mismatch at row '+str(index+1)
|
||
ledger.append(dict(mismatch_row=index+1,actual=repr(actual[index:index+1]),expected=repr(answers[index:index+1])))
|
||
else:
|
||
success=True
|
||
except BaseException as error:
|
||
exceptions.append('main:'+repr(error))
|
||
failure='execution rejected: '+repr(error)
|
||
finally:
|
||
if scratch is not None:
|
||
try:
|
||
phase('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/tmp',b'',CLEAN_DEADLINE,ledger)
|
||
cleanup_ok=True
|
||
except BaseException as error:
|
||
exceptions.append('cleanup:'+repr(error))
|
||
try:
|
||
out,err=phase('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/tmp',b'',ABSENCE_DEADLINE,ledger)
|
||
absence_ok=out==b'ABSENT\n'
|
||
if not absence_ok:
|
||
exceptions.append('absence: unexpected raw output')
|
||
except BaseException as error:
|
||
exceptions.append('absence:'+repr(error))
|
||
facts=dict(ledger=ledger,exceptions=exceptions,success=success,cleanup_ok=cleanup_ok,absence_ok=absence_ok)
|
||
# bos_verify keeps the last 4000 characters; compressed metadata is decoded in author evidence.
|
||
payload=base64.b85encode(zlib.compress(json.dumps(facts,separators=(',',':')).encode())).decode('ascii')
|
||
print('PINFACTS:'+payload,file=sys.stderr)
|
||
if success and cleanup_ok and absence_ok and not exceptions:
|
||
print('PASS')
|
||
return 0
|
||
print(failure or 'cleanup/absence rejected')
|
||
return 1
|
||
|
||
|
||
if __name__=='__main__':
|
||
raise SystemExit(main())
|