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

353 lines
19 KiB
Python

"""Independent protocol oracle. C processes expose observations, never expectations."""
import base64
import json
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
CANDIDATES = ['dispatch.c', 'queue.c', 'codec.c', 'dispatch.h']
MOD = 1000000007
def initial():
return dict(mode=0, cursor=0, ticks=0, published=0, blocked=0, invalid=0,
lanes=[dict(enabled=1, credit=1, queue=[], pulses=0, expired=0,
delivered=0, refused=0, cancelled=0, moved_in=0,
moved_out=0) for _ in range(4)],
ports=[dict(period=0, attempts=0, accepted=0, rejected=0,
sum=0, digest=0, last_id=0, last_value=0)
for _ in range(4)])
def parse(line):
if len(line) > 4096:
return ('?', [])
text = line.split('#', 1)[0].strip()
if not text:
return None
parts = text.split()
if len(parts[0]) != 1 or len(parts) > 6:
return ('?', [])
args = []
for token in parts[1:]:
if re.fullmatch(r'[+-]?[0-9]+', token) is None:
return ('?', [])
value = int(token)
if not -2147483648 <= value <= 2147483647:
return ('?', [])
args.append(value)
return parts[0], args
def valid(op, a):
lane = lambda x: 0 <= x < 4
if op in 'RTS' and not a:
return True
if op == 'M':
return len(a) == 1 and a[0] in (0, 1)
if op == 'L':
return (len(a) == 4 and lane(a[0]) and a[1] in (0, 1)
and 0 <= a[2] <= 8 and 0 <= a[3] <= 8)
if op == 'G':
return len(a) == 2 and lane(a[0]) and 0 <= a[1] <= 8
if op == 'D':
return len(a) == 1 and lane(a[0])
if op == 'P':
return (len(a) == 4 and 1 <= a[0] <= 1000000
and -100000 <= a[1] <= 100000 and 1 <= a[2] <= 255
and 1 <= a[3] <= 15)
if op == 'C':
return len(a) == 2 and 1 <= a[0] <= 1000000 and 1 <= a[1] <= 15
if op == 'X':
return (len(a) == 3 and lane(a[0]) and lane(a[1])
and a[0] != a[1] and 0 <= a[2] <= 8)
return False
def apply(s, op, a):
if not valid(op, a):
s['invalid'] += 1
return -1
lanes, ports = s['lanes'], s['ports']
if op == 'R':
s.clear()
s.update(initial())
elif op == 'M':
s['mode'] = a[0]
elif op == 'L':
lanes[a[0]].update(enabled=a[1], credit=a[2])
ports[a[0]]['period'] = a[3]
elif op == 'G':
if lanes[a[0]]['credit'] + a[1] > 8:
return -3
lanes[a[0]]['credit'] += a[1]
elif op == 'D':
lane = lanes[a[0]]
lane['cancelled'] += len(lane['queue'])
lane['queue'] = []
elif op == 'C':
for i, lane in enumerate(lanes):
if a[1] & (1 << i):
kept = [m for m in lane['queue'] if m[0] != a[0]]
lane['cancelled'] += len(lane['queue']) - len(kept)
lane['queue'] = kept
elif op == 'X':
source, target, count = lanes[a[0]], lanes[a[1]], a[2]
if len(source['queue']) < count:
return -4
if len(target['queue']) + count > 8:
s['blocked'] += 1
return -2
target['queue'].extend(source['queue'][:count])
del source['queue'][:count]
source['moved_out'] += count
target['moved_in'] += count
elif op == 'P':
selected = [lane for i, lane in enumerate(lanes) if a[3] & (1 << i)]
if any(len(lane['queue']) == 8 for lane in selected):
s['blocked'] += 1
return -2
for lane in selected:
lane['queue'].append(tuple(a[:3]))
s['published'] += 1
elif op == 'T':
s['ticks'] += 1
for lane in lanes:
lane['pulses'] += 1
old = lane['queue']
lane['queue'] = [(mid, value, ttl - 1) for mid, value, ttl in old if ttl > 1]
lane['expired'] += len(old) - len(lane['queue'])
for i in [(s['cursor'] + offset) % 4 for offset in range(4)]:
lane, port = lanes[i], ports[i]
if not lane['enabled'] or not lane['credit'] or not lane['queue']:
continue
mid, value, _ = lane['queue'][0]
port['attempts'] += 1
port.update(last_id=mid, last_value=value)
if port['period'] and port['attempts'] % port['period'] == 0:
port['rejected'] += 1
lane['refused'] += 1
continue
port['accepted'] += 1
port['sum'] += value
port['digest'] = (port['digest'] * 33 + mid * 17 + value + 100000) % MOD
lane['queue'].pop(0)
lane['credit'] -= 1
lane['delivered'] += 1
if s['mode'] == 0:
break
s['cursor'] = (s['cursor'] + 1) % 4
return 0
def snapshot(s, rc):
rows = [('S', rc, s['mode'], s['cursor'], s['ticks'], s['published'],
s['blocked'], s['invalid'])]
for i, lane in enumerate(s['lanes']):
rows.append(('L', i, lane['enabled'], lane['credit'], len(lane['queue']),
*[lane[k] for k in ('pulses', 'expired', 'delivered', 'refused',
'cancelled', 'moved_in', 'moved_out')]))
rows.extend(('Q', i, j, *m) for j, m in enumerate(lane['queue']))
rows.extend(('V', i, *[p[k] for k in ('period', 'attempts', 'accepted', 'rejected',
'sum', 'digest', 'last_id', 'last_value')])
for i, p in enumerate(s['ports']))
return rows
def expected(lines):
s = initial()
rows = []
for line in lines:
command = parse(line)
if command is not None:
rows.extend(snapshot(s, apply(s, *command)))
return rows
def scenarios():
lines = ['# default replay', '', 'L 0 0 1 0', 'P 41 200 2 1', 'T', 'T', 'S',
'R', 'M 1', 'P 37 -100000 4 15', 'T', 'S',
'R', 'L 0 1 8 1', 'L 1 1 8 2', 'L 2 1 8 0', 'L 3 1 8 3',
'P 1000000 100000 255 15', 'T', 'T', 'M 1', 'T', 'S',
'R', 'L 2 0 0 1', 'P 8 0 1 4', 'T', 'L 2 1 8 0', 'T',
'R', 'L 0 0 0 0']
lines += ['P %d %d 255 1' % (i + 1, -i) for i in range(8)]
lines += ['P 99 1 255 3', 'X 1 2 1', 'X 0 1 8', 'X 1 2 0', 'X 1 2 3',
'X 1 2 6', 'P 1 77 3 15', 'P 1 88 2 15', 'C 1 5', 'D 1',
'G 0 8', 'G 0 1', 'L 0 1 8 0', 'M 1', 'T', 'T', 'T', 'S']
# Ring wraparound, removal from the middle, transfer atomicity and all lease extremes.
for mode in range(2):
lines += ['R', 'M %d' % mode]
for lane in range(4):
lines += ['L %d 1 8 %d' % (lane, lane)]
for i in range(8):
lines += ['P %d %d %d 15' % (i % 3 + 1, i * 151, 255 if i % 2 else 3)]
lines += ['T', 'T', 'C 2 15', 'T', 'G 0 1', 'G 1 1', 'G 2 1', 'G 3 1']
for i in range(6):
lines += ['P %d %d 9 15' % (i + 90, -i * 777), 'T']
lines += ['D 0', 'X 1 0 1', 'T']
invalids = ['Z', 'TT', 'R 1', 'M', 'M 2', 'M -1', 'L -1 0 1 0',
'L 4 0 1 0', 'L 0 2 1 0', 'L 0 1 9 0', 'L 0 1 0 9',
'G 4 0', 'G 0 -1', 'G 0 9', 'D -1', 'D 4',
'P 0 0 1 1', 'P 1000001 0 1 1', 'P 1 -100001 1 1',
'P 1 100001 1 1', 'P 1 0 0 1', 'P 1 0 256 1',
'P 1 0 1 0', 'P 1 0 1 16', 'C 0 1', 'C 1 16',
'X 0 0 0', 'X 4 0 1', 'X 0 1 9', 'X 0 1 -1',
'P + 0 1 1', 'P 1 0x10 1 1', 'P 1 0 1 1 x',
'P 2147483648 0 1 1', 'P -2147483649 0 1 1',
'P -2147483648 0 1 1', 'P 2147483647 0 1 1',
'P 1 1.0 1 1', 'P 1 0 1 1 1 1', 'G 0 1junk']
lines += invalids + [' R # reset', '\tM +1', 'P +1 -0 +255 +15#inline', 'T',
'S' + ' ' * 4093 + '#', 'S']
rng = random.Random(884920)
for section in range(12):
lines += ['R', 'M %d' % (section % 2)]
for _ in range(115):
kind = rng.choice('PPPPTTTLLGDCXS')
lane = rng.randrange(4)
if kind == 'P':
args = [rng.choice([1, 2, 1000000, rng.randrange(1, 1000001)]),
rng.choice([-100000, 0, 100000, rng.randrange(-100000, 100001)]),
rng.choice([1, 2, 3, 8, 255]), rng.randrange(1, 16)]
elif kind == 'L':
args = [lane, rng.randrange(2), rng.randrange(9), rng.randrange(9)]
elif kind == 'G':
args = [lane, rng.randrange(9)]
elif kind == 'D':
args = [lane]
elif kind == 'C':
args = [rng.choice([1, 2, 1000000]), rng.randrange(1, 16)]
elif kind == 'X':
args = [lane, (lane + rng.randrange(1, 4)) % 4, rng.randrange(9)]
else:
args = []
lines.append(' '.join(map(str, [kind] + args)))
# Long mixed sequence followed by the full supported stream bound.
lines += ['R', 'M 1']
for index in range(64):
lines += ['L %d %d 8 %d' % (index % 4, (index // 4) % 2, index % 9),
'P %d %d 255 15' % (index + 700, index * 231 - 7000), 'T']
extension = ['S', 'T', 'P 9999 -32767 8 15', 'T', 'M 1', 'G 3 0', 'C 9999 3', 'D 2']
while len(lines) < 10002:
lines.append(extension[(len(lines) - 1775) % len(extension)])
return lines
def normalize(output):
result = []
for line in output.splitlines():
values = line.split()
if not values:
continue
if values[0] not in ('S', 'L', 'Q', 'V'):
raise ValueError('unknown output record')
result.append((values[0], *(int(value, 10) for value in values[1:])))
return result
# Pure execution only: no task inputs, expected values or behavioral assertions.
EXECUTOR = "import base64, ctypes, json, os, pathlib, signal, subprocess, sys, time\n\n\ndef child_pids():\n found = set()\n pending = [os.getpid()]\n while pending:\n parent = pending.pop()\n try:\n text = pathlib.Path('/proc/%d/task/%d/children' % (parent, parent)).read_text()\n except FileNotFoundError:\n continue\n for item in text.split():\n pid = int(item)\n if pid not in found:\n found.add(pid)\n pending.append(pid)\n return found\n\n\ndef group_absent(group):\n if group is None:\n return True\n try:\n os.killpg(group, 0)\n except ProcessLookupError:\n return True\n return False\n\n\ndef retire(process):\n group = process.pid if process is not None else None\n deadline = time.monotonic() + 3.0\n adopted = []\n seen = set()\n while True:\n children = child_pids()\n seen.update(children)\n if group is not None:\n try:\n os.killpg(group, signal.SIGKILL)\n except ProcessLookupError:\n pass\n for pid in children:\n try:\n os.kill(pid, signal.SIGKILL)\n except ProcessLookupError:\n pass\n while True:\n try:\n pid, status = os.waitpid(-1, os.WNOHANG)\n except ChildProcessError:\n break\n if pid == 0:\n break\n if process is not None and pid == process.pid:\n process.returncode = os.waitstatus_to_exitcode(status)\n else:\n adopted.append(pid)\n remaining = child_pids()\n absent = group_absent(group)\n if not remaining and absent:\n return {'reaped': True, 'group_absent': True, 'remaining_children': [],\n 'observed_children': sorted(seen), 'adopted_reaped': adopted}\n if time.monotonic() >= deadline:\n return {'reaped': False, 'group_absent': absent,\n 'remaining_children': sorted(remaining),\n 'observed_children': sorted(seen), 'adopted_reaped': adopted}\n time.sleep(0.01)\n\n\ndef main():\n request = json.loads(sys.stdin.buffer.read())\n process = None\n out = err = b''\n code = 125\n failure = None\n timed_out = False\n cleanup = {'reaped': False, 'group_absent': False, 'remaining_children': []}\n started = time.monotonic()\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(), 'PR_SET_CHILD_SUBREAPER failed')\n process = subprocess.Popen(request['argv'], cwd=request['cwd'],\n stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE, start_new_session=True)\n try:\n out, err = process.communicate(base64.b64decode(request['input']),\n timeout=request['timeout'])\n code = process.returncode\n except subprocess.TimeoutExpired:\n timed_out = True\n code = 124\n except Exception as error:\n failure = type(error).__name__ + ': ' + str(error)\n finally:\n try:\n cleanup = retire(process)\n if process is not None and timed_out:\n out, err = process.communicate(timeout=1)\n except Exception as error:\n failure = type(error).__name__ + ': ' + str(error)\n if failure is not None or not cleanup['reaped'] or not cleanup['group_absent']:\n code = 125\n metadata = dict(cleanup, uid=os.getuid(), gid=os.getgid(), timeout=request['timeout'],\n elapsed_seconds=time.monotonic() - started, timed_out=timed_out,\n leader_returncode=None if process is None else process.returncode,\n failure=failure)\n print(json.dumps({'code': code, 'stdout': base64.b64encode(out).decode('ascii'),\n 'stderr': base64.b64encode(err).decode('ascii'), 'metadata': metadata}))\n\n\nmain()\n"
def execute_child(argv, cwd, timeout, phase, input_data=b''):
request = dict(argv=argv, cwd=None if cwd is None else str(cwd), timeout=timeout,
input=base64.b64encode(input_data).decode('ascii'))
completed = subprocess.run([sys.executable, '-I', '-c', EXECUTOR],
input=json.dumps(request).encode('utf-8'),
capture_output=True)
if completed.returncode != 0:
raise RuntimeError('executor exit ' + str(completed.returncode) + ': ' +
completed.stderr.decode('utf-8', 'replace')[-1200:])
result = json.loads(completed.stdout)
meta = result['metadata']
print('SDE_EXEC ' + json.dumps(dict(phase=phase, code=result['code'], **meta)), file=sys.stderr)
if meta['reaped'] is not True or meta['group_absent'] is not True:
raise RuntimeError('executor retirement unconfirmed: ' + phase)
return subprocess.CompletedProcess(argv, result['code'],
base64.b64decode(result['stdout']),
base64.b64decode(result['stderr']))
def main():
scratch = None
failure = None
try:
lines = scenarios()
answers = expected(lines)
inputs = '\n'.join(lines).encode('utf-8') # Final EOF without a newline.
context = json.loads(Path(__file__).with_name('context.json').read_text())
candidate = Path(sys.argv[1])
scratch = Path(tempfile.mkdtemp(prefix='state-dispatch-effects-'))
print('scratch=' + str(scratch), file=sys.stderr)
# All trusted input, support and source bytes precede the first child.
for name, value in context.items():
(scratch / name).write_text(value)
for name in CANDIDATES:
(scratch / name).write_bytes((candidate / name).read_bytes())
built = execute_child(['gcc', '-std=c11', '-O2', '-Wall', '-Wextra',
'-Wpedantic', 'dispatch.c', 'queue.c', 'codec.c',
'driver.c', 'port.c', '-o', 'dispatch-demo'],
scratch, 30, 'compile')
print('compile_rc=' + str(built.returncode), file=sys.stderr)
if built.returncode != 0:
raise RuntimeError('compile_failure ' + built.stderr.decode('utf-8', 'replace')[-1500:])
# Parent uses pipes only after the first child, never candidate-controlled files.
result = execute_child([str(scratch / 'dispatch-demo')], scratch, 20,
'candidate', inputs)
print('candidate_rc=' + str(result.returncode), file=sys.stderr)
stderr = result.stderr.decode('utf-8', 'replace')
if stderr:
print('candidate_stderr=' + stderr[-1200:], file=sys.stderr)
if result.returncode != 0:
reason = 'controlled_refusal=grader_unreadable' if (
result.returncode == 71 and 'grader unreadable' in stderr) else 'candidate_exit_failure'
raise RuntimeError(reason)
try:
actual = normalize(result.stdout.decode('utf-8'))
except ValueError as error:
raise RuntimeError('normal_exit_protocol_error=' + str(error)) from error
for index in range(max(len(answers), len(actual))):
got = actual[index] if index < len(actual) else '<missing>'
want = answers[index] if index < len(answers) else '<extra>'
if got != want:
raise RuntimeError('normal_exit_first_observation_difference=%d got=%r expected=%r' %
(index + 1, got, want))
print('observation_rows=' + str(len(answers)), file=sys.stderr)
print('command_lines=' + str(len(lines)), file=sys.stderr)
except Exception as error:
failure = type(error).__name__ + ': ' + str(error)
finally:
if scratch is not None:
try:
cleanup = execute_child([sys.executable, '-I', '-c',
'import shutil,sys; shutil.rmtree(sys.argv[1])',
str(scratch)], None, 10, 'cleanup')
print('cleanup_rc=' + str(cleanup.returncode) + ' scratch=' + str(scratch), file=sys.stderr)
if cleanup.returncode != 0:
raise RuntimeError('candidate_identity_cleanup_failed=' +
cleanup.stderr.decode('utf-8', 'replace')[-1000:])
except Exception as error:
failure = type(error).__name__ + ': ' + str(error)
try:
absence = execute_child([sys.executable, '-I', '-c',
'import os,sys; sys.exit(int(os.path.lexists(sys.argv[1])))',
str(scratch)], None, 5, 'absence')
print('absence_rc=' + str(absence.returncode) + ' scratch=' + str(scratch), file=sys.stderr)
if absence.returncode != 0:
raise RuntimeError('scratch_absence_unconfirmed')
except Exception as error:
failure = type(error).__name__ + ': ' + str(error)
if failure is not None:
print(failure)
return 1
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())