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

239 lines
20 KiB
Python

CONTEXT = '#include "blink.hpp"\n#include <iostream>\n#include <sstream>\n#include <memory>\n#include <array>\n#include <iomanip>\n\nnamespace {\nclass Device : public blink::Platform {\npublic:\n int next_gpio = 10;\n int next_timer = 100;\n int gpio = -1;\n int timer = -1;\n int physical = -1;\n int fault = 0;\n int immediate = 0;\n int reserved = 0;\n int writes = 0;\n int arms = 0;\n bool pending = false;\n bool flight = false;\n bool first = false;\n blink::Callback callback = nullptr;\n void *context = nullptr;\n std::vector<std::vector<long long>> events;\n\n int request(int pin) override\n {\n int result = fault == 1 ? -5 : next_gpio++;\n events.push_back({1, pin, result});\n if (result >= 0) gpio = result;\n return result;\n }\n int configure(int handle, int value) override\n {\n int result = fault == 2 ? -5 : 0;\n events.push_back({2, handle, value, result});\n if (result == 0 && handle == gpio) physical = value;\n return result;\n }\n void write(int handle, int value) override\n {\n events.push_back({3, handle, value});\n ++writes;\n if (handle == gpio && gpio >= 0) physical = value;\n }\n void release(int handle) override\n {\n events.push_back({4, handle});\n if (handle == gpio) gpio = -1;\n }\n int create_timer(blink::Callback function, void *argument) override\n {\n int result = fault == 3 ? -5 : next_timer++;\n events.push_back({5, result});\n if (result >= 0) {\n timer = result;\n callback = function;\n context = argument;\n first = true;\n pending = false;\n flight = false;\n }\n return result;\n }\n int arm(int handle, std::uint32_t period) override\n {\n bool initial = first;\n first = false;\n int result = initial && fault == 4 ? -5 : 0;\n ++arms;\n events.push_back({6, handle, period, result});\n pending = true;\n if (initial && immediate) fire();\n if (initial && reserved) hold();\n return result;\n }\n void cancel(int handle) override\n {\n events.push_back({7, handle});\n pending = false;\n }\n void drain(int handle) override\n {\n events.push_back({8, handle, flight ? 1 : 0});\n complete();\n }\n void destroy_timer(int handle) override\n {\n events.push_back({9, handle});\n if (timer == handle) {\n timer = -1;\n pending = false;\n flight = false;\n callback = nullptr;\n context = nullptr;\n }\n }\n void fire()\n {\n if (timer < 0 || !pending || flight) return;\n pending = false;\n events.push_back({10, timer});\n if (callback) callback(context);\n }\n void hold()\n {\n if (timer < 0 || !pending || flight) return;\n pending = false;\n flight = true;\n events.push_back({11, timer});\n }\n void complete()\n {\n if (timer < 0 || !flight) return;\n flight = false;\n events.push_back({10, timer});\n if (callback) callback(context);\n }\n};\n\nvoid numbers(const std::vector<long long> &values)\n{\n std::cout << \'[\';\n bool separator = false;\n for (auto number : values) {\n if (separator) std::cout << \',\';\n std::cout << number;\n separator = true;\n }\n std::cout << \']\';\n}\n\nbool decode(const std::string &input, std::string &output)\n{\n output.clear();\n if (input == "-") return true;\n if (input.size() % 2) return false;\n auto digit = [](char value) {\n if (value >= \'0\' && value <= \'9\') return value - \'0\';\n if (value >= \'a\' && value <= \'f\') return value - \'a\' + 10;\n if (value >= \'A\' && value <= \'F\') return value - \'A\' + 10;\n return -1;\n };\n for (std::size_t at = 0; at < input.size(); at += 2) {\n int high = digit(input[at]);\n int low = digit(input[at + 1]);\n if (high < 0 || low < 0) return false;\n output.push_back(static_cast<char>(high * 16 + low));\n }\n return true;\n}\n\nvoid observe(int result, blink::Session &session, Device &device,\n const blink::Config *config, const std::string *raw)\n{\n auto s = session.snapshot();\n std::cout << "{\\"r\\":" << result << ",\\"s\\":";\n numbers({s.phase, s.pin, s.gpio, s.timer, s.logical, s.period,\n static_cast<long long>(s.pulses), static_cast<long long>(s.generation),\n static_cast<long long>(s.callbacks)});\n std::cout << ",\\"b\\":";\n numbers({device.gpio >= 0, device.timer >= 0, device.pending,\n device.flight, device.physical, device.writes, device.arms});\n std::cout << ",\\"log\\":[";\n bool separator = false;\n for (const auto &event : device.events) {\n if (separator) std::cout << \',\';\n numbers(event);\n separator = true;\n }\n std::cout << \']\';\n if (config != nullptr) {\n std::cout << ",\\"c\\":";\n numbers({config->pin, config->period, config->active_low, config->initial});\n std::cout << ",\\"text\\":" << std::quoted(blink::format_config(*config));\n static const char digits[] = "0123456789abcdef";\n std::cout << ",\\"input\\":\\"";\n for (unsigned char byte : *raw) {\n std::cout << digits[byte >> 4] << digits[byte & 15];\n }\n std::cout << \'"\';\n }\n std::cout << "}\\n";\n}\n}\n\nint main()\n{\n Device devices[2];\n blink::Session first(devices[0]);\n blink::Session second(devices[1]);\n blink::Session *sessions[2] = {&first, &second};\n int selected = 0;\n std::string line;\n while (std::getline(std::cin, line)) {\n std::istringstream input(line);\n std::string op;\n if (!(input >> op)) continue;\n if (op == "U") {\n if (!(input >> selected) || selected < 0 || selected > 1) return 2;\n }\n Device &device = devices[selected];\n blink::Session &session = *sessions[selected];\n device.events.clear();\n int result = 0;\n blink::Config config;\n const blink::Config *parsed = nullptr;\n std::string raw;\n if (op == "U" || op == "Q") {\n } else if (op == "I") {\n int seed = 0;\n if (!(input >> seed)) return 2;\n device.next_gpio = seed + 10;\n device.next_timer = seed + 100;\n } else if (op == "M") {\n if (!(input >> device.fault >> device.immediate >> device.reserved)) return 2;\n } else if (op == "O") {\n int commit = 0;\n if (!(input >> config.pin >> config.period >> config.active_low >> config.initial >> commit)) return 2;\n result = session.open(config, commit);\n } else if (op == "S") {\n result = session.stop();\n } else if (op == "P") {\n std::uint32_t period = 0;\n if (!(input >> period)) return 2;\n result = session.change_period(period);\n } else if (op == "L") {\n int logical = 0;\n if (!(input >> logical)) return 2;\n result = session.set_level(logical);\n } else if (op == "V") {\n result = session.toggle();\n } else if (op == "T") {\n unsigned count = 0;\n if (!(input >> count)) return 2;\n while (count--) device.fire();\n } else if (op == "H") {\n device.hold();\n } else if (op == "F") {\n device.complete();\n } else if (op == "D") {\n if (!(input >> config.pin >> config.period >> config.active_low >> config.initial)) return 2;\n result = blink::validate(config);\n } else if (op == "C") {\n std::string hex;\n std::string text;\n if (!(input >> config.pin >> config.period >> config.active_low >> config.initial >> hex)) return 2;\n if (!decode(hex, text)) return 2;\n raw = text;\n result = blink::parse_config(raw, config);\n parsed = &config;\n } else {\n return 2;\n }\n observe(result, session, device, parsed, &raw);\n }\n return std::cin.bad() ? 2 : 0;\n}\n'
CANDIDATES = ['blink.hpp', 'blink_config.cpp', 'gpio_lease.cpp', 'pulse_timer.cpp', 'blink_session.cpp', 'blink_cleanup.cpp']
import json
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
class Oracle:
def __init__(self):
self.next_gpio=10; self.next_timer=100
self.gpio=-1; self.timer=-1; self.physical=-1
self.fault=0; self.inline=0; self.reserve=0
self.pending=False; self.flight=False; self.first=False
self.writes=0; self.arms=0; self.phase=0
self.pin=-1; self.period=0; self.low=0; self.logical=0
self.pulses=0; self.generation=0; self.callbacks=0
self.events=[]
def event(self,*args): self.events.append(list(args))
def write(self,logical):
self.logical=logical; self.physical=logical ^ self.low
self.writes+=1; self.event(3,self.gpio,self.physical)
def callback(self):
self.event(10,self.timer); self.callbacks+=1
if self.phase==2:
self.write(1-self.logical); self.pulses+=1
self.arm(False)
def arm(self,initial):
result=-5 if initial and self.fault==4 else 0
self.event(6,self.timer,self.period,result); self.arms+=1
self.pending=True
if initial and self.inline:
self.pending=False; self.callback()
if initial and self.reserve and self.pending and not self.flight:
self.pending=False; self.flight=True; self.event(11,self.timer)
return result
def clear(self):
self.phase=0; self.pin=-1; self.period=0
self.low=0; self.logical=0; self.pulses=0
def retire(self):
self.phase=3
if self.timer>=0:
self.event(7,self.timer); self.pending=False
self.event(8,self.timer,int(self.flight))
if self.flight:
self.flight=False; self.callback()
self.event(9,self.timer); self.timer=-1
self.pending=False; self.flight=False
if self.gpio>=0:
self.write(0); self.event(4,self.gpio); self.gpio=-1
self.clear()
def open(self,pin,period,low,initial,commit):
if self.phase: return -16
if not (0<=pin<=1023 and 1<=period<=1000000 and low in (0,1) and initial in (0,1) and commit in (0,-5)): return -22
self.phase=1; self.pin=pin; self.period=period; self.low=low; self.logical=initial; self.pulses=0
result=-5 if self.fault==1 else self.next_gpio
self.event(1,pin,result)
if result<0:
self.clear(); return result
self.gpio=result; self.next_gpio+=1
result=-5 if self.fault==2 else 0
self.event(2,self.gpio,initial ^ low,result)
if result<0:
self.event(4,self.gpio); self.gpio=-1; self.clear(); return result
self.physical=initial ^ low
result=-5 if self.fault==3 else self.next_timer
self.event(5,result)
if result<0:
self.retire(); return result
self.timer=result; self.next_timer+=1
self.pending=False; self.flight=False; self.phase=2
result=self.arm(True)
if result or commit:
self.retire(); return result or commit
self.generation+=1
return 0
def state(self):
return [self.phase,self.pin,self.gpio,self.timer,self.logical,self.period,self.pulses,self.generation,self.callbacks]
def backend(self):
return [int(self.gpio>=0),int(self.timer>=0),int(self.pending),int(self.flight),self.physical,self.writes,self.arms]
def operation(self,op,args):
if op=='I': self.next_gpio=args[0]+10; self.next_timer=args[0]+100
elif op=='M': self.fault,self.inline,self.reserve=args
elif op=='O': return self.open(*args)
elif op=='S':
if self.phase: self.retire()
elif op=='P':
if self.phase!=2: return -2
if not 1<=args[0]<=1000000: return -22
self.period=args[0]; self.arm(False)
elif op=='L':
if self.phase!=2: return -2
if args[0] not in (0,1): return -22
self.write(args[0])
elif op=='V':
if self.phase!=2: return -2
self.write(1-self.logical)
elif op=='T':
for _ in range(args[0]):
if self.timer>=0 and self.pending and not self.flight:
self.pending=False; self.callback()
elif op=='H':
if self.timer>=0 and self.pending and not self.flight:
self.pending=False; self.flight=True; self.event(11,self.timer)
elif op=='F':
if self.timer>=0 and self.flight:
self.flight=False; self.callback()
return 0
def parse_config(text,previous):
if len(text)>128: return -22,previous
fields={}
for token in re.split(r'[ \t\r\n]+',text.strip(' \t\r\n')):
if not token: continue
match=re.fullmatch(r'(pin|period|low|initial)=([0-9]+)',token)
if not match or match[1] in fields: return -22,previous
value=int(match[2])
if value>2**32-1: return -22,previous
fields[match[1]]=value
values=[fields.get('pin',-1),fields.get('period',500),fields.get('low',0),fields.get('initial',0)]
if not (0<=values[0]<=1023 and 1<=values[1]<=1000000 and values[2] in (0,1) and values[3] in (0,1)): return -22,previous
return 0,values
class Suite:
def __init__(self):
self.models=[Oracle(),Oracle()]; self.selected=0
self.commands=[]; self.expected=[]; self.labels=[]
def add(self,command,label,clauses):
words=command.split(); op=words[0]
if op=='U': self.selected=int(words[1])
model=self.models[self.selected]; model.events=[]; extra={}
if op=='C':
previous=list(map(int,words[1:5])); raw=bytes.fromhex(words[5]) if words[5]!='-' else b''
result,config=parse_config(raw.decode('ascii'),previous)
extra={'c':config,'text':'pin=%d period=%d low=%d initial=%d'%tuple(config),'input':raw.hex()}
elif op=='D':
pin,period,low,initial=map(int,words[1:])
result=0 if 0<=pin<=1023 and 1<=period<=1000000 and low in (0,1) and initial in (0,1) else -22
else: result=model.operation(op,list(map(int,words[1:])))
self.commands.append(command)
self.expected.append(dict(r=result,s=model.state(),b=model.backend(),log=list(model.events),**extra))
self.labels.append((label,clauses))
def config(self,text,label='config'):
self.add('C 997 23 1 1 '+(text.encode('ascii').hex() or '-'),label,'B18-B19,B23')
def cases():
s=Suite(); rng=random.Random(0xB11A575)
def c(command,label='regression',clauses='B04-B17,B20-B24'): s.add(command,label,clauses)
c('I '+str(rng.randrange(100,10000)))
c('U 1'); c('I '+str(rng.randrange(100,10000))); c('U 0')
for instance in (0,1):
c(f'U {instance}')
for fault in range(5):
for inline in (0,1):
for reserve in (0,1):
for commit in (0,-5):
c('S'); c(f'M {fault} {inline} {reserve}')
pin=rng.choice([0,7,511,1023]); period=rng.choice([1,500,1000000])
c(f'O {pin} {period} {rng.randrange(2)} {rng.randrange(2)} {commit}','open-combinations','B04-B13')
c('Q'); c('T 0'); c('T 2'); c('H'); c('P 17'); c('F')
c('L 1'); c('V'); c('H'); c('S','stop-flight','B10,B12-B14')
c('F'); c('T 3'); c('S'); c('Q')
c('M 0 0 0')
for command in ('P 0','P 1000001','L 2','V','S','T 100','H','F'):
c(command,'idle-no-effects','B14-B17')
for params in ('-1 1 0 0 0','1024 1 0 0 0','0 0 0 0 0','0 1000001 0 0 0','0 1 2 0 0','0 1 0 -1 0','0 1 0 2 0','0 1 0 0 1'):
c('O '+params,'invalid-open','B04-B07'); c('Q')
c('O 1023 1000000 1 1 0')
for command in ('O -1 0 2 -1 1','P 0','P 1000001','L -1','L 2','P 1','T 1','H','P 1000000','F','L 0','V','T 100','Q','S'):
c(command,'boundaries-priority','B04-B17')
for _ in range(350):
if rng.randrange(7)==0: c('U '+str(rng.randrange(2)),'multi-instance','B17')
op=rng.choice(['M','O','S','P','L','V','T','H','F','Q'])
if op=='M': command=f'M {rng.randrange(5)} {rng.randrange(2)} {rng.randrange(2)}'
elif op=='O': command=f'O {rng.choice([-1,0,17,1023,1024])} {rng.choice([0,1,37,1000000,1000001])} {rng.randrange(3)} {rng.randrange(3)} {rng.choice([0,0,-5])}'
elif op=='P': command=f'P {rng.choice([0,1,99,1000000,1000001])}'
elif op=='L': command=f'L {rng.randrange(-1,3)}'
elif op=='T': command=f'T {rng.randrange(5)}'
else: command=op
c(command,'seeded-sequences','B04-B17')
c('U 0'); c('S'); c('U 1'); c('S')
texts=['pin=0','pin=1023 period=1000000 low=1 initial=1',' initial=0\tlow=1\r\nperiod=1 pin=0007 ','pin=7 period=500','pin=0'+' '*123]
texts+=['',' ','pin=','pin=-1','pin=1024','pin=1 pin=1','period=3','pin=0 low=2','pin=0 initial=2','pin=0 period=0','pin=0 period=1000001','pin=4294967296','pin=0 x=1','pin=+1','pin=0 period=1=2','pin=1\x00','pin=1\v','pin=1\f','pin=1'+' '*124]
for text in texts: s.config(text)
for values in ('0 1 0 0','1023 1000000 1 1','-1 1 0 0','1024 1 0 0','0 0 0 0','0 1000001 0 0','0 1 2 0','0 1 0 -1'):
c('D '+values,'validate-direct','B04-B05,B19')
c('C -3 0 -5 9 -','format-unvalidated','B18-B19,B23')
for _ in range(80):
fields=[f'pin={rng.randrange(1024)}',f'period={rng.randrange(1,1000001)}',f'low={rng.randrange(2)}',f'initial={rng.randrange(2)}']
rng.shuffle(fields)
s.config(rng.choice([' ','\t','\r\n']).join(fields),'config-permutations')
return s
def semantic_equal(actual,expected):
if isinstance(expected,dict):
return isinstance(actual,dict) and actual.keys()==expected.keys() and all(semantic_equal(actual[key],value) for key,value in expected.items())
if isinstance(expected,list):
return isinstance(actual,list) and len(actual)==len(expected) and all(semantic_equal(a,b) for a,b in zip(actual,expected))
if isinstance(expected,int):
return isinstance(actual,(int,float)) and not isinstance(actual,bool) and actual==expected
return type(actual) is type(expected) and actual==expected
def main():
suite=cases()
scratch=Path(tempfile.mkdtemp(prefix='embedded-blink-session-',dir=Path.cwd()))
(scratch/'smoke.cpp').write_text(CONTEXT)
for name in CANDIDATES: (scratch/name).write_bytes((Path(sys.argv[1])/name).read_bytes())
wire='\n'.join(suite.commands)+'\n'
# All inputs and source files are ready before any candidate subprocess runs.
built=subprocess.run(['g++','-std=c++17','-O2','-Wall','-Wextra','-pedantic','smoke.cpp',*CANDIDATES[1:],'-o','observe'],cwd=scratch,capture_output=True,timeout=30)
if built.returncode:
print('check=build clauses=B02 build failed '+built.stderr.decode('utf-8','replace')[-1200:].replace('\n',' ')); return 1
# Scratch belongs to bos_verify outer temporary root; its teardown removes it after check exits.
observed=subprocess.run([str(scratch/'observe')],input=wire.encode('utf-8'),cwd=scratch,capture_output=True,timeout=15)
if observed.returncode:
print('check=execution clauses=B20-B24 exit='+str(observed.returncode)); return 1
try: actual=observed.stdout.decode('utf-8').splitlines()
except UnicodeDecodeError:
print('check=output clauses=B24 invalid UTF-8 JSON'); return 1
if len(actual)!=len(suite.expected):
print('check=observations clauses=B21,B24 count='+str(len(actual))+' expected='+str(len(suite.expected))); return 1
failures=[]
for index,(line,expected) in enumerate(zip(actual,suite.expected)):
try: value=json.loads(line)
except ValueError: value=None
if not semantic_equal(value,expected):
label,clauses=suite.labels[index]
failures.append((index,label,clauses,value,expected))
if failures:
for index,label,clauses,actual,expected in failures[:10]:
print('check=%s clauses=%s observation=%d actual=%r expected=%r'%(label,clauses,index,actual,expected))
print('mismatches=%d observations=%d'%(len(failures),len(suite.expected))); return 1
print('PASS'); return 0
if __name__=='__main__': raise SystemExit(main())