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

286 lines
12 KiB
Python

"""Independent semantic oracle; submitted code runs only in child processes."""
import json
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
HERE = Path(__file__).resolve().parent
SENTINEL = [777, 888, 999, -123, 456, 0]
def parse(data):
if not 1 <= len(data) <= 4096:
return -22, None
token = data[:-1] if data.endswith(b'\n') else data
if re.fullmatch(rb'[+-]?[0-9]+', token) is None:
return -22, None
# Strip leading zeroes so Python's decimal safety limit cannot affect the contract.
negative = token.startswith(b'-')
digits = token.lstrip(b'+-').lstrip(b'0') or b'0'
if len(digits) > 10:
return -34, None
value = int(digits) * (-1 if negative else 1)
return (0, value) if -2147483648 <= value <= 2147483647 else (-34, None)
def name_ok(name):
return re.fullmatch(r'[a-z0-9_]{1,24}', name) is not None
def formatting(value, cap, null):
buffer = bytearray(b'#' * 32)
text = str(value).encode() + b'\n\0'
rc = -22 if null else -28 if cap < len(text) else len(text) - 1
if rc >= 0:
buffer[:len(text)] = text
return rc, buffer.hex()
def expected(commands):
attrs = [None] * 8
generations = [0] * 8
observers = []
next_token = 1
output = []
def readable(slot):
return -22 if not 0 <= slot < 8 else -2 if attrs[slot] is None else 0
def counts():
return [sum(item is not None for item in attrs), sum(w['active'] for w in observers)]
def state(slot):
return attrs[slot]['state'][:] if readable(slot) == 0 else SENTINEL[:]
def cancel(token):
for w in observers:
if w['token'] == token and w['active']:
w['active'] = False
return 0
return -2
for command in commands:
op, *a = command
if op == 'I':
one,two,hexed = a
rc,value = parse(bytes.fromhex(hexed))
first = [one,0,1,-2147483648,2147483647,1]
notice = 0
if rc == 0:
rc = len(bytes.fromhex(hexed))
if value != one:
first[0:2] = [value,1]
notice = 1
output.append(['I',0,0,1,rc,notice,0,*first,0,two,0,1,-2147483648,2147483647,1,1,0])
continue
elif op == 'C':
slot, name, minimum, maximum, initial, writable = a
rc = (-22 if not 0 <= slot < 8 else -22 if not name_ok(name)
else -34 if minimum > maximum or not minimum <= initial <= maximum
else -17 if attrs[slot] is not None or any(t is not None and t['name'] == name for t in attrs)
else 0)
if rc == 0:
generations[slot] += 1
attrs[slot] = dict(name=name, state=[initial, 0, generations[slot], minimum, maximum, writable])
elif op == 'D':
slot, = a
rc = readable(slot)
if rc == 0:
attrs[slot] = None
for w in observers:
if w['slot'] == slot:
w['active'] = False
elif op == 'L':
name, = a
rc = -22 if not name_ok(name) else next((i for i,t in enumerate(attrs) if t is not None and t['name'] == name), -2)
elif op == 'S':
slot, = a
output.append(['S', readable(slot), *state(slot)])
continue
elif op == 'H':
slot, cap, null = a
rc = readable(slot)
formatted = formatting(attrs[slot]['state'][0], cap, null) if rc == 0 else (rc, (b'#'*32).hex())
output.append(['H', *formatted])
continue
elif op == 'W':
slot, encoded = a
data = bytes.fromhex(encoded)
rc = readable(slot)
if rc == 0:
current = attrs[slot]['state']
if current[5] == 0:
rc = -13
else:
rc, value = parse(data)
if rc == 0 and not current[3] <= value <= current[4]:
rc = -34
if rc == 0:
rc = len(data)
if value != current[0]:
current[0] = value
current[1] += 1
recipients = [w for w in observers if w['active'] and w['slot'] == slot]
for w in recipients:
if not w['active']:
continue
sr = state(slot)
show_rc, show_hex = formatting(sr[0], 32, 0)
action = -999
probes = [-999]*4
if w['mode'] == 1:
action = cancel(w['target'])
elif w['mode'] == 2:
action = cancel(w['token'])
elif w['mode'] == 3:
probes = [-16]*4
output.append(['N',slot,w['token'],sr[1],sr[2],0,*sr,show_rc,show_hex,action,*probes,*counts(),next((i for i,t in enumerate(attrs) if t is not None and t['name'] == 'counter'), -2)])
elif op == 'B':
slot, mode, target = a
rc = readable(slot)
if rc == 0:
rc = -22 if mode == -1 else -28 if counts()[1] == 16 else next_token
if rc > 0:
observers.append(dict(slot=slot,token=rc,mode=mode,target=target,active=True))
next_token += 1
elif op == 'X':
rc = cancel(a[0])
elif op == 'A':
output.append(['A', *counts()])
continue
elif op == 'P':
rc, value = parse(bytes.fromhex(a[0]))
output.append(['P',rc,777 if rc else value])
continue
elif op == 'F':
output.append(['H',*formatting(*a)])
continue
elif op == 'V':
raw = bytes.fromhex(a[0])
rc = int(re.fullmatch(rb'[a-z0-9_]{1,24}',raw) is not None)
else:
raise ValueError('oracle unknown command')
output.append(['R',rc])
return output
def scenarios(config):
def write(slot, text):
return ['W',slot,text.hex()]
commands = [
['I',-2147483648,2147483647,b'7'.hex()],['I',9,3,b'9'.hex()],['I',0,1,b'bad'.hex()], ['A'], ['S',0], ['S',-1], ['H',8,0,1], ['L',''], ['L','missing'],
['C',-1,'a',0,1,0,1], ['C',0,'Upper',0,1,0,1],
['C',0,'counter',-2147483648,2147483647,0,1], ['C',0,'x',0,1,0,1],
['C',1,'counter',0,1,0,1], ['C',1,'bad_range',2,1,1,1],
['C',1,'initial',0,1,2,1], ['L','counter'], ['S',0],
['B',0,0,0], ['B',0,1,3], ['B',0,0,0], write(0,b'42\n'), ['S',0],
['A'], ['B',0,2,0], write(0,b'-17'), ['A'], ['B',0,3,0],
write(0,b'+0007\n'), write(0,b'7'), ['S',0], ['X',1], ['X',1], ['X',0],
['C',2,'readonly',-20,20,4,0], ['B',2,0,0], write(2,b'bad'), write(2,b'9'),
['D',2], ['X',6], ['A'], ['D',2], ['C',2,'readonly',-20,20,8,1],write(2,b'9'),['S',2],
['B',-1,0,0], ['B',1,0,0], ['B',0,-1,0], ['X',-2],
]
values = [b'', b'\n',b'+',b'-\n',b'1\n\n',b' 1',b'\t1',b'\v1',b'\f2',b'\r3',
b'1 ',b'1\r\n',b'1\x00',b'\xff',b'--1',b'++1',b'+-1',b'0x10',
b'2147483648',b'-2147483649',b'999999999999999999999x', b'-2147483648',
b'2147483647',b'-0',b'+0\n',b'0'*4095+b'7',b'0'*4096,b'0'*4097,
b'0'*4085+b'-2147483648',b'-'+b'0'*4084+b'2147483648',b'9'*4096]
for data in values:
commands.extend([['P',data.hex()],write(0,data),['S',0]])
for value in [-2147483648,-1,0,9,2147483647]:
for cap in [0,1,len(str(value))+1,len(str(value))+2,32]:
commands.extend([['F',value,cap,0],['F',value,cap,1],['H',0,cap,0]])
for name in [b'',b'a',b'_'*24,b'a'*25,b'A',b'a-b',b'a\x00',b'\xff',b'abc_123']:
commands.append(['V',name.hex()])
commands += [['D',0],['S',0],['A'],['C',0,'counter',-10,10,1,1],write(0,b'2'),['S',0]]
for i in range(17):
commands.append(['B',0,0,0])
commands += [['A'],write(0,b'3'),['D',0],['A'],['C',0,'new_name',0,0,0,1],write(0,b'1'),write(0,b'0'),['S',0]]
yield commands
for seed in config['seeds']:
rng = random.Random(seed)
commands = [['C',slot,'lane_'+str(slot),-100,100,slot,slot%2] for slot in range(8)]
for i in range(420):
slot = rng.randrange(-1,9)
op = rng.choice('WWWWBXSCHDLA')
if op == 'W':
text = rng.choice([str(rng.randrange(-110,111)).encode(), b'+00008',b' 5',b'\v8',b'23\n',b'3\x00'])
commands.append(write(slot,text))
elif op == 'B':commands.append(['B',slot,rng.randrange(-1,4),rng.randrange(1,80)])
elif op == 'X':commands.append(['X',rng.randrange(-1,80)])
elif op == 'S':commands.append(['S',slot])
elif op == 'C':commands.append(['C',slot,'lane_'+str(rng.randrange(8)),rng.choice([-100,0]),rng.choice([-1,100]),rng.choice([-100,0,100]),rng.randrange(2)])
elif op == 'H':commands.append(['H',slot,rng.randrange(0,33),rng.randrange(2)])
elif op == 'D':commands.append(['D',slot])
elif op == 'L':commands.append(['L','lane_'+str(rng.randrange(8))])
elif op == 'A':commands.append(['A'])
yield commands
def identical(actual, wanted):
if type(actual) is not type(wanted):
return False
if isinstance(wanted,list):
return len(actual) == len(wanted) and all(identical(a,b) for a,b in zip(actual,wanted))
return actual == wanted
def observations(raw):
text = raw.decode('utf-8')
decoder = json.JSONDecoder()
position = 0
values = []
while position < len(text):
while position < len(text) and text[position] in ' \t\r\n':
position += 1
if position == len(text):
break
value, position = decoder.raw_decode(text, position)
values.append(value)
return values
def fail(reason, diagnostic=b''):
if diagnostic:
sys.stderr.write(diagnostic.decode('utf-8','replace')[-2000:]+'\n')
print('FAIL: '+reason)
return 1
def main():
candidate = Path(sys.argv[1])
config = json.loads((HERE/'cases.json').read_text())
# All candidate reads and scratch writes happen before the first child starts.
names = ['attribute.hpp','attribute_core.cpp','attribute_value.cpp']
sources = {name:(candidate/name).read_bytes() for name in names}
console = (HERE/'console.cpp').read_bytes()
inputs = list(scenarios(config))
answers = [expected(commands) for commands in inputs]
encoded = [(''.join('\t'.join(map(str,row))+'\n' for row in commands)).encode('ascii') for commands in inputs]
scratch = Path(tempfile.mkdtemp(prefix='attribute-check-',dir=Path.cwd()))
for name,data in sources.items():
(scratch/name).write_bytes(data)
(scratch/'console.cpp').write_bytes(console)
binary = scratch/'attribute-console'
build = subprocess.run(['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic',str(scratch/'console.cpp'),str(scratch/'attribute_core.cpp'),str(scratch/'attribute_value.cpp'),'-o',str(binary)],capture_output=True,timeout=25)
if build.returncode != 0:
return fail('candidate build',build.stderr)
checks = 0
for data,wanted in zip(encoded,answers):
completed = subprocess.run([str(binary)],input=data,capture_output=True,timeout=8)
if completed.returncode != 0:
return fail('candidate execution',completed.stderr)
try:
actual = observations(completed.stdout)
except (ValueError,UnicodeDecodeError):
return fail('invalid observation encoding or JSON',completed.stderr)
if not identical(actual,wanted):
mismatch = next((i for i,(a,b) in enumerate(zip(actual,wanted)) if not identical(a,b)),min(len(actual),len(wanted)))
sys.stderr.write('observation mismatch at '+str(mismatch)+'\n')
return fail('behavior mismatch',completed.stderr)
checks += len(wanted)
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())