amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

366 lines
15 KiB
Python

"""Independent journal model; all candidate execution occurs in child processes."""
import copy
import errno
import json
from pathlib import Path
import random
import re
import socket
import subprocess
import sys
import tempfile
OK, WAIT, END, FAULT, CLOSED, INVALID, FULL, BUSY = range(8)
ERRORS = {-1: errno.EINTR, -2: errno.EAGAIN, -3: errno.EPIPE, -4: errno.EIO}
RING_RUNNER = r'''#include "stream.h"
#include <stdio.h>
static void hex(const unsigned char *bytes, size_t count) {
size_t i;
putchar('"');
for (i = 0; i < count; ++i) printf("%02x", bytes[i]);
putchar('"');
}
int main(void) {
byte_ring ring;
char op;
unsigned count, seed;
unsigned char bytes[64], logical[64], copied[64];
size_t n, size, i;
ring_reset(&ring);
while (scanf(" %c %u %u", &op, &count, &seed) == 3) {
n = 0;
if (op == 'Z') ring_reset(&ring);
if (op == 'A') {
for (i = 0; i < count; ++i) bytes[i] = (unsigned char)((seed + 29 * i) & 255);
ring_append(&ring, bytes, count);
}
if (op == 'D') ring_drop(&ring, count);
if (op == 'P') n = ring_copy(&ring, copied, count);
size = ring_copy(&ring, logical, 64);
putchar('[');
hex(copied, n);
putchar(',');
hex(logical, size);
printf(",%zu,%zu]\n", ring.size, ring_space(&ring));
}
return 0;
}
'''
def numeric(token):
if re.fullmatch(r'[+-]?[0-9]+', token, re.ASCII) is None:
return None
negative = token.startswith('-')
digits = token.lstrip('+-').lstrip('0') or '0'
if len(digits) > 7:
return None
value = int(digits)
if value > 1000000:
return None
return -value if negative else value
class Model:
def __init__(self):
self.channels = [dict(state=[0] * 8, tx='', rx='') for _ in range(3)]
self.ports = [dict(next=[0, 0], cursor=0, calls=[]) for _ in range(3)]
self.scripts = [[[], []] for _ in range(3)]
self.shutdown = [0, 0, 0]
def call(self, channel, receive, request, data=''):
port = self.ports[channel]
index = int(receive)
script = self.scripts[channel][index]
position = port['next'][index]
if position < len(script):
step = script[position]
port['next'][index] += 1
else:
step = -2 if receive else request
if step < 0:
result, error, output = -1, ERRORS[step], ''
else:
result, error = min(request, step), 0
if receive:
start = port['cursor']
output = bytes((31 + 71 * channel + 17 * i) % 256
for i in range(start, start + result)).hex()
port['cursor'] += result
else:
output = data[:2 * result]
if not receive:
output = data[:2 * request]
port['calls'].append([2 if receive else 1, request,
0 if receive else int(socket.MSG_NOSIGNAL),
result, error, output])
return result, error, output
def execute(self, line):
for port in self.ports:
port['calls'] = []
rc, n, out = INVALID, 0, ''
tokens = re.split('[ \t]+', line.strip(' \t')) if line.strip(' \t') else []
if '\x00' not in line and len(line) <= 8192 and len(tokens) >= 2 and len(tokens[0]) == 1:
channel = numeric(tokens[1])
if channel is not None and 0 <= channel < 3:
rc, n, out = self.command(tokens[0], channel, tokens[2:])
return copy.deepcopy(dict(rc=rc, n=n, out=out, channels=self.channels, ports=self.ports))
def command(self, op, channel, args):
c = self.channels[channel]
s = c['state']
rc, n, out = INVALID, 0, ''
if op in 'OCF' and not args:
if op == 'O':
rc = BUSY if s[1] else OK
if rc == OK:
c.update(state=[s[0] + 1, 1, 0, 0, 0, 0, 0, 0], tx='', rx='')
elif op == 'C':
rc = OK if s[1] else CLOSED
if rc == OK:
c.update(state=[s[0], 0, 0, 0, 0, 0, 0, 0], tx='', rx='')
elif not s[1] or s[3]:
rc = CLOSED
elif c['tx']:
rc = BUSY
else:
error = self.shutdown[channel]
self.ports[channel]['calls'].append([3, 0, 0, -1 if error else 0, error, ''])
rc = FAULT if error else OK
if error:
s[4] = error
else:
s[3] = 1
elif op == 'Q' and len(args) == 1:
text = args[0]
if text == '-':
data = ''
elif len(text) <= 130 and len(text) % 2 == 0 and re.fullmatch('[0-9a-fA-F]+', text):
data = text.lower()
else:
return INVALID, 0, ''
count = len(data) // 2
rc = INVALID if count > 64 else CLOSED if not s[1] or s[3] else FULL if len(c['tx']) // 2 + count > 64 else OK
if rc == OK:
c['tx'] += data
elif op in 'TRDH' and len(args) == 1:
count = numeric(args[0])
if count is None or count < 0:
return INVALID, 0, ''
if op == 'H':
if count in (0, errno.EIO, errno.EAGAIN):
self.shutdown[channel] = count
rc = OK
elif count > 64:
rc = INVALID
elif not s[1]:
rc = CLOSED
elif op == 'D':
n = min(count, len(c['rx']) // 2)
out, c['rx'] = c['rx'][:2 * n], c['rx'][2 * n:]
s[7] += n
rc = OK
elif op == 'T' and s[3]:
rc = CLOSED
elif op == 'R' and s[2]:
rc = END
else:
rc = OK
while n < count and (op == 'R' or c['tx']):
request = min(count - n, 64 - len(c['rx']) // 2 if op == 'R' else len(c['tx']) // 2)
if request == 0:
rc = FULL
break
result, error, data = self.call(channel, op == 'R', request, c['tx'])
if result > 0:
n += result
if op == 'R':
c['rx'] += data
s[6] += result
else:
c['tx'] = c['tx'][2 * result:]
s[5] += result
elif result == 0:
if op == 'R':
s[2] = 1
rc = END
else:
rc = WAIT
break
elif error == errno.EINTR:
continue
elif error == errno.EAGAIN:
rc = WAIT
break
else:
s[4] = error
s[2 if op == 'R' else 3] = 1
rc = FAULT
break
elif op in ('S', 'V') and len(args) <= 64:
steps = [numeric(arg) for arg in args]
if all(step is not None and -4 <= step <= 64 for step in steps):
index = int(op == 'V')
self.scripts[channel][index] = steps
self.ports[channel]['next'][index] = 0
rc = OK
return rc, n, out
def scenarios():
lines = ['O 0', 'V 0 8 0', 'R 0 16', 'D 0 8', 'Q 0 48454c4c4f', 'T 0 5', 'F 0',
'R 0 0', 'T 0 0', 'Q 0 -', 'F 0', 'C 0', 'C 0', 'O 0', 'O 0']
payload = bytes(range(64)).hex()
for channel in range(3):
def add(text):
lines.append(text.replace('@', str(channel)))
for text in ['C @', 'Q @ ' + 'aa' * 65, 'R @ 65', 'T @ 65', 'D @ 65', 'Q @ -',
'T @ 0', 'R @ 0', 'D @ 0', 'F @', 'O @', 'Q @ ' + payload,
'Q @ -', 'Q @ 01', 'S @ 2 -1 3 -2', 'T @ 64', 'T @ 7',
'S @ 0', 'T @ 9', 'S @ 1 -3', 'T @ 64', 'Q @ -', 'F @',
'V @ 1 -1 2 -2', 'R @ 64', 'D @ 1', 'V @ 64 64', 'R @ 64',
'R @ 1', 'D @ 63', 'R @ 1', 'V @ 1 0', 'D @ 64', 'R @ 64',
'R @ 64', 'D @ 64', 'C @', 'O @', 'V @ 1 -4', 'R @ 64',
'Q @ 0102030405', 'S @ 1 1 1 1 1', 'T @ 5', 'H @ 5', 'F @',
'H @ 11', 'F @', 'H @ 0', 'F @', 'R @ 0', 'D @ 64', 'F @',
'C @', 'O @', 'V @ 0', 'R @ 1', 'Q @ 00ff', 'T @ 64', 'F @',
'C @', 'O @', 'S @ ' + ' '.join(['-1'] * 64), 'Q @ ff', 'T @ 1',
'V @ ' + ' '.join(['-1'] * 64), 'R @ 1', 'V @', 'S @']:
add(text)
lines += ['C 0', 'O 0', 'Q 0 00ff01', 'T 0 0', 'R 0 0',
'S 0 1 -4', 'T 0 3', 'R 0 65', 'V 0 2 -3', 'R 0 4', 'D 0 64',
'C 0', 'O 0', 'F 0', 'V 0 3 -2', 'R 0 64', 'D 0 0', 'D 0 64',
'C 0', 'O 0', 'Q 0 ' + 'ff' * 64, 'F 0', 'Q 0 -', 'T 0 64',
'F 0', 'R 0 0', 'V 0 64', 'R 0 64', 'V 0 1', 'R 0 0', 'R 0 1',
'D 0 1', 'R 0 1', 'D 0 64']
rng = random.Random(730219)
for _ in range(900):
c = rng.randrange(3)
op = rng.choice(['O', 'C', 'Q', 'Q', 'T', 'T', 'R', 'R', 'D', 'F', 'H', 'S', 'V'])
if op in 'OCF':
line = f'{op} {c}'
elif op == 'Q':
count = rng.choice([0, 1, 2, 7, 31, 63, 64, 65])
data = bytes(rng.randrange(256) for _ in range(count)).hex() or '-'
line = f'Q {c} {data}'
elif op in 'TRD':
line = f'{op} {c} {rng.choice([0, 1, 2, 31, 63, 64, 65, 1000000])}'
elif op == 'H':
line = f'H {c} {rng.choice([0, 5, 11, 4])}'
else:
steps = [rng.choice([-4, -3, -2, -1, 0, 1, 2, 3, 31, 64]) for _ in range(rng.randrange(9))]
line = f'{op} {c}' + ''.join(f' {step}' for step in steps)
lines.append(line)
lines += ['O 0\x00garbage', 'O 0\x00', '\x00', 'Q 0 aa\x00ff', '', ' ', '\t', 'X 0', 'O', 'O 0 1', 'O -1', 'O 3', 'R 0 -1',
'R 0 1.0', 'R 0 0x10', 'R 0 1\v', 'R 0 1\f', 'O\r 0', 'Q 0 a',
'Q 0 gg', 'S 0 -5', 'V 0 65', 'S 0 ' + ' '.join(['1'] * 65),
'D 0 1000001', 'D 0 +', 'R 0 ' + '9' * 100, 'O 0' + ' ' * 8190,
'C 0', 'O ' + '0' * 8000, 'Q +0000 AaFf', 'T 0000 +0000002',
'C\t1', 'O 1', 'R 1 0000000000000000000000000000000000000']
return lines
def helper_scenarios():
rng = random.Random(88041)
entries, answers, data = [], [], b''
directed = [('Z', 0, 0), ('A', 64, 17), ('P', 0, 0), ('P', 1000, 0),
('D', 63, 0), ('A', 63, 22), ('P', 64, 0), ('D', 64, 0),
('D', 0, 0), ('A', 0, 0), ('P', 65, 0), ('Z', 0, 0)]
for op, count, seed in directed:
if op == 'A':
data += bytes((seed + 29 * i) % 256 for i in range(count))
elif op == 'D':
data = data[count:]
elif op == 'Z':
data = b''
entries.append(f'{op} {count} {seed}')
answers.append([data[:count].hex() if op == 'P' else '', data.hex(), len(data), 64 - len(data)])
for _ in range(300):
op = rng.choice('AAADPPZ')
if op == 'A':
count = rng.randrange(65 - len(data))
seed = rng.randrange(256)
data += bytes((seed + 29 * i) % 256 for i in range(count))
elif op == 'D':
count, seed = rng.randrange(len(data) + 1), 0
data = data[count:]
elif op == 'P':
count, seed = rng.choice([0, 1, 63, 64, 65, 1000]), 0
else:
count, seed, data = 0, 0, b''
copied = data[:count].hex() if op == 'P' else ''
entries.append(f'{op} {count} {seed}')
answers.append([copied, data.hex(), len(data), 64 - len(data)])
return '\n'.join(entries).encode() + b'\n', answers
def same(actual, wanted):
if type(actual) is not type(wanted):
return False
if isinstance(wanted, dict):
return actual.keys() == wanted.keys() and all(same(actual[k], v) for k, v in wanted.items())
if isinstance(wanted, list):
return len(actual) == len(wanted) and all(same(a, b) for a, b in zip(actual, wanted))
return actual == wanted
def parse_result(result, wanted, label):
if result.stderr:
print(result.stderr.decode('utf-8', 'replace')[-2000:], file=sys.stderr)
if result.returncode:
raise ValueError(label + ' child exit ' + str(result.returncode))
try:
observed = [json.loads(line) for line in result.stdout.decode('ascii').splitlines()]
except (UnicodeDecodeError, ValueError) as error:
raise ValueError(label + ' invalid observation') from error
if not same(observed, wanted):
for index in range(max(len(observed), len(wanted))):
if index >= len(observed) or index >= len(wanted) or not same(observed[index], wanted[index]):
raise ValueError(label + ' observation mismatch at ' + str(index + 1))
def main():
config = json.loads(Path(__file__).with_name('context.json').read_text())
sources = {name: (Path(sys.argv[1]) / name).read_bytes() for name in config['candidates']}
lines = scenarios()
model = Model()
answers = [model.execute(line) for line in lines]
stdin = ('\n'.join(lines)).encode('ascii')
ring_stdin, ring_answers = helper_scenarios()
scratch = Path(tempfile.mkdtemp(prefix='socket-progress-', dir=Path.cwd()))
for name, content in config['files'].items():
(scratch / name).write_text(content)
for name, content in sources.items():
(scratch / name).write_bytes(content)
(scratch / 'ring_runner.c').write_text(RING_RUNNER)
# All candidate reads and writes precede the first child process.
commands = [(['make', '-s'], 30),
(['cc', '-std=c11', '-O2', 'ring_runner.c', 'state.c', 'transmit.c', 'receive.c', '-o', 'ring-probe'], 30)]
for command, timeout in commands:
built = subprocess.run(command, cwd=scratch, capture_output=True, timeout=timeout)
if built.stderr:
print(built.stderr.decode('utf-8', 'replace')[-2000:], file=sys.stderr)
if built.returncode:
print('build failed')
return 1
result = subprocess.run([str(scratch / 'socket-console')], cwd=scratch, input=stdin,
capture_output=True, timeout=20)
parse_result(result, answers, 'journal')
result = subprocess.run([str(scratch / 'ring-probe')], cwd=scratch, input=ring_stdin,
capture_output=True, timeout=20)
parse_result(result, ring_answers, 'ring')
result = subprocess.run([str(scratch / 'socket-console')], cwd=scratch, input=b'',
capture_output=True, timeout=10)
parse_result(result, [], 'empty')
print('PASS')
return 0
if __name__ == '__main__':
try:
raise SystemExit(main())
except (ValueError, subprocess.TimeoutExpired) as error:
print(str(error))
raise SystemExit(1)