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

258 lines
10 KiB
Python

"""Independent abstract-state oracle; candidate executes only in child processes."""
import copy
import json
from pathlib import Path
import random
import subprocess
import sys
import tempfile
NAMES = ['fork_model.h', 'model_internal.h', 'model_store.c', 'model_buffer.c', 'model_session.c', 'model_lifecycle.c']
def fresh(capacity):
state = {'capacity': capacity,
'processes': [[0, 0, 0, [[-1, ''] for _ in range(3)]] for _ in range(8)],
'descriptions': [[0, 0, 0, ''] for _ in range(3)]}
state['processes'][0][0:2] = [1, 1]
return state
def decimal(word):
sign = -1 if word.startswith("-") else 1
digits = word[1:] if sign < 0 else word
return sign * int(digits.lstrip("0") or "0")
def apply(state, command):
words = command.split()
op = words[0]
args = [decimal(x) for x in words[1:-1]] if op == 'WRITE' else [decimal(x) for x in words[1:]]
if op == 'INIT':
if not 0 <= args[0] <= 512:
return 1, state
return 0, fresh(args[0])
if op == 'FORK':
parent, child = args
if not 0 <= parent < 8 or not 0 <= child < 8:
return 1, state
if not state['processes'][parent][1]:
return 2, state
if state['processes'][child][1]:
return 3, state
target = copy.deepcopy(state['processes'][parent])
target[0:3] = [1, 1, 0]
state['processes'][child] = target
for d, _ in target[3]:
if d >= 0:
state['descriptions'][d][1] += 1
return 0, state
if op == 'EXIT':
p, mode, code = args
if not 0 <= p < 8 or mode not in (0, 1) or not 0 <= code <= 255:
return 1, state
if not state['processes'][p][1]:
return 2, state
trial = copy.deepcopy(state)
for s in range(3):
d, _ = trial['processes'][p][3][s]
if d < 0:
continue
if mode == 0:
rc, trial = apply(trial, 'FLUSH %d %d' % (p, s))
if rc:
return rc, state
trial['descriptions'][d][1] -= 1
trial['processes'][p][3][s] = [-1, '']
trial['processes'][p][1:3] = [0, code]
return 0, trial
p, s = args[:2]
if not 0 <= p < 8 or not 0 <= s < 3:
return 1, state
if op == 'OPEN' and not 0 <= args[2] < 3:
return 1, state
if op == 'SEEK' and not 0 <= args[2] <= state['capacity']:
return 1, state
if not state['processes'][p][1]:
return 2, state
target = state['processes'][p][3][s]
d, pending = target
if op == 'OPEN':
if d >= 0:
return 3, state
target[:] = [args[2], '']
state['descriptions'][args[2]][1] += 1
return 0, state
if d < 0:
return 4, state
if op in ('WRITE', 'NULL'):
if op == 'NULL':
return (0 if args[2] == 0 else 1), state
data = '' if words[-1] == '-' else words[-1].lower()
if len(pending) + len(data) > 128:
return 5, state
target[1] += data
return 0, state
description = state['descriptions'][d]
if op == 'SEEK':
description[0] = args[2]
return 0, state
if op in ('FLUSH', 'CLOSE'):
raw = bytes.fromhex(pending)
if raw:
begin = description[0]
end = begin + len(raw)
if end > state['capacity']:
return 6, state
file_bytes = bytearray.fromhex(description[3])
if end > len(file_bytes):
file_bytes.extend(b'\0' * (end - len(file_bytes)))
file_bytes[begin:end] = raw
description[0] = end
description[2] += 1
description[3] = file_bytes.hex()
target[1] = ''
if op == 'CLOSE':
description[1] -= 1
target[:] = [-1, '']
return 0, state
raise ValueError(op)
def scenarios():
commands = []
def add(*lines):
commands.extend(lines)
add('INIT 128','OPEN 0 0 0','WRITE 0 0 707265','FORK 0 1',
'WRITE 1 0 6b6964','EXIT 1 1 42','EXIT 0 0 0')
add('USE 0','INIT 64','OPEN 0 0 0','WRITE 0 0 a0a1','FORK 0 1',
'USE 1','INIT 32','OPEN 0 0 2','WRITE 0 0 b0b1','FORK 0 7',
'USE 0','FLUSH 0 0','USE 1','FLUSH 7 0','USE 0','INIT 1',
'USE 1','FLUSH 0 0','EXIT 7 1 42','USE 0','OPEN 0 0 1',
'WRITE 0 0 ff','FLUSH 0 0','USE 1','EXIT 0 0 0','USE 0')
full = bytes(range(64)).hex()
add('INIT 512','OPEN 0 0 0','WRITE 0 0 '+full,'FLUSH 0 0','FLUSH 0 0',
'WRITE 0 0 '+full,'CLOSE 0 0','OPEN 0 0 0','FLUSH 0 0','EXIT 0 0 255')
add('INIT 0','OPEN 0 2 2','NULL 0 2 0','WRITE 0 2 -','FLUSH 0 2',
'WRITE 0 2 00','FORK 0 7','EXIT 7 1 255','EXIT 0 0 0','EXIT 0 1 0')
add('INIT 16','OPEN 0 0 1','OPEN 0 1 1','OPEN 0 2 2','WRITE 0 0 0001020304050607',
'WRITE 0 1 000102030405060708090a0b','WRITE 0 2 0102','FORK 0 1',
'EXIT 0 0 0','FLUSH 0 0','EXIT 0 0 0','EXIT 1 1 42','EXIT 0 1 19')
add('INIT 64','OPEN 0 0 0','WRITE 0 0 010203040506','FLUSH 0 0',
'SEEK 0 0 2','WRITE 0 0 aabb','FLUSH 0 0','SEEK 0 0 10',
'FORK 0 1','WRITE 1 0 cc00','FLUSH 1 0','WRITE 0 0 dd','FLUSH 0 0',
'CLOSE 0 0','CLOSE 1 0','OPEN 1 2 0','EXIT 1 0 255',
'FORK 0 1','FORK 1 2','EXIT 0 1 0','EXIT 1 0 0','EXIT 2 0 0')
add('INIT 64','OPEN 0 0 0','OPEN 0 1 0','OPEN 0 2 1',
'WRITE 0 0 aa','WRITE 0 1 bb','WRITE 0 2 cc','FORK 0 1',
'EXIT 1 0 43','EXIT 0 0 0','FORK 0 1')
add('INIT 64','OPEN 0 0 0','WRITE 0 0 dead','FLUSH 0 0',
'WRITE 0 0 dead','FLUSH 0 0','CLOSE 0 0','CLOSE 0 0')
# Range and precedence combinations, followed by valid calls to expose mutations.
add('INIT 512','OPEN 0 0 0','WRITE 0 0 01','NULL 0 0 1','NULL 0 0 0',
'NULL 7 0 1','NULL 0 2 1','WRITE 0 0 '+'ff'*65,
'OPEN 7 0 9','OPEN -1 0 0','OPEN 0 -1 0','OPEN 0 0 -1',
'OPEN 0 0 2','FORK 0 0','FORK 7 7','FORK 0 8','FORK -1 0',
'SEEK 7 0 513','SEEK 0 2 513','SEEK 0 0 -1','SEEK 0 0 512',
'FLUSH 0 0','CLOSE 0 0','EXIT 0 2 1','EXIT 7 2 256',
'EXIT 0 0 256','EXIT 0 0 -1','EXIT 0 -1 1','INIT 513',
'INIT -1','SEEK 0 0 0','FLUSH 0 0','EXIT 0 0 254','EXIT 0 0 0')
rng = random.Random(271064)
for capacity in [0,1,2,63,64,65,127,128,511,512]:
add('INIT %d'%capacity, 'OPEN 0 0 0', 'OPEN 0 1 0','OPEN 0 2 1')
for size in [0,1,63,64,65]:
add('WRITE 0 0 '+(rng.randbytes(size).hex() if size else '-'),
'FLUSH 0 0','FLUSH 0 0')
add('INIT %d'%capacity,'OPEN 0 0 0','OPEN 0 1 0','OPEN 0 2 1')
add('FORK 0 1','FORK 1 2','FORK 2 7','EXIT 7 1 7','FORK 0 7')
for index in range(170):
p = rng.randrange(8)
s = rng.randrange(3)
op = rng.choice(['OPEN','WRITE','WRITE','FLUSH','FLUSH','CLOSE','FORK','EXIT','SEEK','NULL'])
if op == 'OPEN':
add('OPEN %d %d %d'%(p,s,rng.randrange(3)))
elif op == 'WRITE':
size = rng.choice([0,1,2,7,31,63,64,65])
add('WRITE %d %d %s'%(p,s,rng.randbytes(size).hex() if size else '-'))
elif op in ('FLUSH','CLOSE'):
add('%s %d %d'%(op,p,s))
elif op == 'FORK':
add('FORK %d %d'%(rng.choice([0,1,2]),p))
elif op == 'EXIT':
add('EXIT %d %d %d'%(p,rng.randrange(2),rng.choice([0,1,42,254,255])))
elif op == 'SEEK':
add('SEEK %d %d %d'%(p,s,rng.choice([0,1,capacity,capacity+1])))
else:
add('NULL %d %d %d'%(p,s,rng.choice([0,1,64])))
if index % 37 == 0:
add('INIT %d'%capacity,'OPEN 0 0 0','FORK 0 1','OPEN 0 1 0')
# Large legal lexical forms belong to the fixed observer's accepted input.
commands[0] = 'INIT '+ '0'*10000 + '128'
return commands
def same_types(actual, expected):
if type(actual) is not type(expected):
return False
if isinstance(expected, dict):
return actual.keys() == expected.keys() and all(same_types(actual[k],v) for k,v in expected.items())
if isinstance(expected, list):
return len(actual) == len(expected) and all(same_types(a,b) for a,b in zip(actual,expected))
return actual == expected
def main():
candidate = Path(sys.argv[1])
sources = {name: (candidate/name).read_bytes() for name in NAMES}
observer = Path(__file__).with_name('observer.txt').read_bytes()
commands = scenarios()
expected = []
states = [fresh(512), fresh(512)]
selected = 0
for command in commands:
if command.startswith('USE '):
selected = int(command.split()[1])
rc = 0
else:
rc,states[selected] = apply(states[selected],command)
row = copy.deepcopy(states[selected])
row['rc'] = rc
expected.append(row)
# All reads and writes to candidate-accessible paths finish before first child.
scratch = Path(tempfile.mkdtemp(prefix='fork-ledger-',dir=Path.cwd()))
for name,data in sources.items():
(scratch/name).write_bytes(data)
(scratch/'observer.c').write_bytes(observer)
build = subprocess.run(['cc','-std=c11','-D_POSIX_C_SOURCE=200809L','-O2',
'-Wall','-Wextra','-Werror','observer.c',
'model_store.c','model_buffer.c','model_session.c','model_lifecycle.c',
'-o','observe'],cwd=scratch,capture_output=True,timeout=30)
if build.returncode:
sys.stderr.write(build.stderr.decode('utf-8','replace'))
print('FAIL build')
return 1
execution = subprocess.run([str(scratch/'observe')],cwd=scratch,
input=('\n'.join(commands)+'\n').encode('ascii'),
capture_output=True,timeout=20)
if execution.returncode:
sys.stderr.write(execution.stderr.decode('utf-8','replace'))
print('FAIL process')
return 1
try:
rows = [json.loads(line) for line in execution.stdout.decode('utf-8').splitlines()]
except (ValueError,UnicodeError):
print('FAIL observation encoding')
return 1
if len(rows) != len(expected):
print('FAIL observation count')
return 1
for index,(actual,wanted) in enumerate(zip(rows,expected)):
if not same_types(actual,wanted):
sys.stderr.write('observation %d command %s\n'%(index,commands[index]))
print('FAIL state transition')
return 1
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())