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

301 lines
13 KiB
Python

"""Independent state oracle; submitted C++ executes only in child processes."""
import copy
import json
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
def cap_valid(c):
t,k,lo,hi=c
return (t==1 and 0<=k<=31 and lo==0 and hi==1 or
t==3 and 0<=k<=7 and -32768<=lo<=hi<=32767)
def normalize(caps):
if any(not cap_valid(c) for c in caps):
return None
if len({tuple(c[:2]) for c in caps})!=len(caps):
return None
return sorted(copy.deepcopy(caps),key=lambda c:c[:2])
def name_valid(name):
return 1<=len(name)<=32 and re.fullmatch(r'[A-Za-z0-9_-]+',name) is not None
class Oracle:
def __init__(self):
self.reset()
def reset(self):
self.slots=[[0,0,'',[],[],[]] for _ in range(4)]
self.totals=[0]*8
self.ledger=[]
def observe(self,rc=0,read=None,normalized=None,help=None):
return copy.deepcopy(dict(rc=rc,read=read,normalized=normalized,help=help,
slots=self.slots,totals=self.totals,ledger=self.ledger))
def apply(self,command,args):
rc=0;read=None;norm=None;help=None
if command=='Z':
self.reset()
return self.observe()
if command=='N':
caps=args
norm=[[1,31,0,1]]
ordered=normalize(caps)
if ordered is None:rc=-1
else:norm=ordered
return self.observe(rc,normalized=norm)
slot=args[0]
valid=0<=slot<4
s=self.slots[slot] if valid else None
if command=='H':
name,*cap=args[1:]
found=valid and any(c[:2]==cap[:2] for c in s[3])
help=list(map(int,[valid,name_valid(name),cap_valid(cap),valid,found]))
elif command=='A':
name=args[1]
if not valid or not name_valid(name):rc=-1
elif s[0]!=0:rc=-2
else:
s[:]=[1,s[1]+1,name,[],[],[]]
self.totals[0]+=1
elif command=='C':
caps=args[1:]
if not valid:rc=-1
elif s[0]!=1:rc=-2
else:
ordered=normalize(caps)
if ordered is None:rc=-1
else:
s[3:]=[ordered,[],[]]
self.totals[2]+=1
elif command=='B':
fail=args[1]
if not valid or fail not in (0,1):rc=-1
elif s[0]!=1:rc=-2
elif fail:rc=-5
else:
s[0]=2;s[4]=[0 if c[0]==1 else c[2] for c in s[3]];s[5]=[]
self.totals[3]+=1
elif command in ('D','X'):
if not valid:rc=-1
else:
if s[0]==2:
s[0]=1;s[4:]=[[],[]];self.totals[4]+=1
if command=='X' and s[0]!=0:
s[:]=[0,s[1],'',[],[],[]];self.totals[1]+=1
elif command in ('E','R'):
t,k=args[1:3]
if not valid:rc=-1
elif s[0]!=2:rc=-2
else:
index=next((i for i,c in enumerate(s[3]) if c[:2]==[t,k]),None)
if index is None:rc=-3
elif command=='R':read=s[4][index]
else:
value=args[3];cap=s[3][index]
if t==1 and value not in (0,1,2) or t==3 and not cap[2]<=value<=cap[3]:rc=-1
else:
repeat=t==1 and value==2
append=repeat or s[4][index]!=value
if append and len(s[5])==64:rc=-4
elif append:
s[5].append([t,k,value]);self.totals[5]+=1
if not repeat:s[4][index]=value
if command=='R' and read is None:read=0
elif command=='S':
if not valid:rc=-1
elif s[0]!=2:rc=-2
elif s[5]:
self.totals[6]+=1;self.totals[7]+=len(s[5])
self.ledger.append([self.totals[6],slot,s[1],copy.deepcopy(s[5])]);s[5]=[]
else:
raise ValueError('oracle command')
return self.observe(rc,read,norm,help)
def parse_console(line):
if len(line.encode('ascii'))>65536:
return None
tokens=[w for w in re.split('[ \t]+',line) if w]
if not tokens:return None
cmd=tokens[0];words=tokens[1:]
def integer(word):
if re.fullmatch('[+-]?[0-9]+',word) is None:raise ValueError()
# Leading zeros have no length cap in the public lexical contract.
sign=-1 if word.startswith('-') else 1
digits=word.lstrip('+-').lstrip('0') or '0'
if len(digits)>10:raise ValueError()
value=sign*int(digits)
if not -2147483648<=value<=2147483647:raise ValueError()
return value
try:
if cmd=='A' and len(words)==2:return cmd,[integer(words[0]),words[1]]
if cmd=='H' and len(words)==6:return cmd,[integer(words[0]),words[1]]+[integer(w) for w in words[2:]]
a=[integer(w) for w in words]
if cmd=='Z' and len(a)==0:return cmd,[]
if cmd in ('D','X','S') and len(a)==1:return cmd,a
if cmd=='B' and len(a)==2:return cmd,a
if cmd=='E' and len(a)==4:return cmd,a
if cmd=='R' and len(a)==3:return cmd,a
if cmd in ('C','N'):
pos=1 if cmd=='C' else 0
if len(a)<=pos or not 0<=a[pos]<=40 or len(a)!=pos+1+a[pos]*4:return None
caps=[a[i:i+4] for i in range(pos+1,len(a),4)]
return cmd,([a[0]]+caps if cmd=='C' else caps)
except ValueError:
return None
return None
def lines_for(seed):
rng=random.Random(seed)
lines=['Z','A 0 panel','C 0 2 3 2 -20 20 1 4 0 1','B 0 0',
'E 0 1 4 1','E 0 1 4 2','E 0 1 4 2','E 0 3 2 7','S 0','S 0',
'R 0 1 4','D 0','D 0','C 0 2 1 4 0 1 3 2 -20 20','C 0 0','C 0 0','B 0 0','E 0 1 4 1','S 0',
'X 0','X 0','A 0 again','B 0 0','S 0','X 0',
'N 0','N 3 3 7 -32768 32767 1 31 0 1 1 0 0 1',
'N 2 1 1 0 1 1 1 0 1','N 2 1 1 0 1 3 8 0 1']
allcaps=[[1,k,0,1] for k in range(32)]+[[3,k,-32768,32767] for k in range(8)]
lines+=['A 1 maximum','C 1 40 '+' '.join(str(x) for c in allcaps for x in c),'B 1 0']
lines += ['E 1 1 0 2']*64
lines += ['E 1 1 0 0','E 1 1 0 1','E 1 1 0 2','R 1 1 0','S 1','S 1']
for k in range(32):lines.append('E 1 1 %d %d'%(k,k%2))
for k in range(8):
lines+=['E 1 3 %d 32767'%k,'R 1 3 %d'%k,'E 1 3 %d -32768'%k]
lines+=['S 1','D 1','C 1 1 3 1 7 7','B 1 1','B 1 0','E 1 3 1 7','S 1','X 1',
'A 1 reuse','C 1 1 1 31 0 1','B 1 0','E 1 1 31 2','S 1']
for episode in range(5):
lines+=['Z']
for slot in range(4):
key=rng.randrange(32);axis=rng.randrange(8)
lines+=['A %d unit_%d'%(slot,episode),'C %d 2 3 %d -11 13 1 %d 0 1'%(slot,axis,key),'B %d 0'%slot]
for _ in range(5):lines.append('E %d 1 %d %d'%(slot,key,rng.randrange(3)))
lines+=['E %d 3 %d 13'%(slot,axis),'S %d'%slot]
for _ in range(80):
slot=rng.choice([-1,0,1,2,3,4]);kind=rng.choice('ABCCDXEESRRHHNN')
if kind=='A':line='A %d %s'%(slot,rng.choice(['ok','Bad!','x'*32,'x'*33]))
elif kind=='B':line='B %d %d'%(slot,rng.choice([-1,0,0,1,2]))
elif kind in 'DXS':line='%s %d'%(kind,slot)
elif kind in 'ER':
line='%s %d %d %d'%(kind,slot,rng.choice([1,1,3,0,4]),rng.randrange(-1,33))
if kind=='E':line+=' '+str(rng.choice([-32769,-32768,-11,0,1,2,13,32767,32768]))
elif kind=='H':line='H %d %s %d %d %d %d'%(slot,rng.choice(['ok','!','a'*32]),rng.choice([1,3,5]),rng.randrange(-1,33),rng.choice([-32768,0,1]),rng.choice([1,0,32767]))
else:
caps=rng.sample(allcaps,rng.randrange(5))
if rng.randrange(5)==0:caps+=[[1,32,0,1]]
if caps and rng.randrange(5)==0:caps.append(caps[0])
line=('%s %d %d'%(kind,slot,len(caps)) if kind=='C' else 'N %d'%len(caps))
if caps:line+=' '+' '.join(str(x) for c in caps for x in c)
lines.append(line)
lines+=['Z','A -1 ok','A 0 bad!','A 4 ok','C 0 1 99 99 0 1','B 0 2','D -1','X 4','S 4','R -1 1 0','E 4 1 0 3',
'A 0 valid','C 0 1 1 0 0 1','C 0 2 1 1 0 1 3 0 7 6','C 0 1 3 0 -32769 7',
'C 0 1 3 0 -7 32768','C 0 1 1 0 1 1','B 0 0','C 0 0','B 0 0','B 0 1',
'E 0 1 0 -1','E 0 1 0 3','E 0 3 0 0','H 0 ok 1 0 0 1','D 0','H 0 ok 1 0 0 1',
'','?','Z 0','A','B 0','E 0 1 0','C 0 -1','C 0 41','C 0 1','N 0 1','S 0 0',
'S +','S 2147483648','S -2147483649','S 0x0','S 0\v','S 0\f',
'S '+'0'*12000,'S '+'0'*65534,'S '+'0'*65535,'\tS\t+0000\t','Z','A 0 tail']
return lines
def console_answers(lines):
model=Oracle();answers=[]
for line in lines:
command=parse_console(line)
answers.append(model.apply(*command) if command is not None else model.observe(-9))
return answers
def direct_answers():
m=Oracle();o=[]
for cmd,args in [('A',[0,'']),('A',[0,'a'*33]),('A',[0,'\xff']),('A',[0,'Direct_1'])]:o.append(m.apply(cmd,args))
caps=[[3,7,-32768,32767],[1,31,0,1],[1,0,0,1]]
caps=normalize(caps);o.append(m.observe(normalized=caps))
caps.append([1,31,0,1]);o.append(m.observe(-1,normalized=caps))
for cmd,args in [('C',[0,[1,31,0,1]]),('B',[0,0]),('E',[0,1,31,2]),('S',[0]),('X',[0]),
('A',[0,'Direct_2']),('C',[0]),('B',[0,0]),('S',[0]),('X',[0])]:o.append(m.apply(cmd,args))
o.append(m.observe(help=[0,1,0,0,1]))
for cmd,args in [('A',[3,'Pointer']),('C',[3,[3,2,-9,9],[1,7,0,1]]),('B',[3,0]),('E',[3,1,7,1])]:o.append(m.apply(cmd,args))
def slot_get(index):
o.append(copy.deepcopy(dict(inspect=index,value=m.slots[index] if 0<=index<4 else None)))
def cap_get(slot,t,k):
value=next((c for c in m.slots[slot][3] if c[:2]==[t,k]),None) if 0<=slot<4 else None
o.append(copy.deepcopy(dict(capability=[slot,t,k],value=value)))
for index in range(4):slot_get(index)
slot_get(-1);slot_get(4)
for args in [(3,3,2),(3,1,7),(3,1,31),(0,3,2)]:cap_get(*args)
o.append(m.observe())
o.append(m.apply('A',[0,'Other']))
o.append(m.apply('C',[0,[3,1,5,5],[1,31,0,1]]))
slot_get(0);cap_get(0,3,1);cap_get(0,1,31)
o.append(m.apply('B',[0,0]));o.append(m.apply('E',[0,1,31,2]))
for index in range(4):slot_get(index)
for args in [(0,3,1),(0,1,31),(3,3,2),(3,1,7),(-1,1,31)]:cap_get(*args)
o.append(m.observe())
o.append(m.apply('X',[3]));slot_get(3);cap_get(3,3,2);o.append(m.observe())
return o
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],wanted[k]) for k in wanted)
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 run_child(argv,scratch,stdin=None,timeout=10):
result=subprocess.run(argv,cwd=scratch,input=stdin,capture_output=True,timeout=timeout)
if result.stderr:print(result.stderr[-2500:].decode('utf-8','replace'),file=sys.stderr)
return result
def compare(result,wanted,label):
if result.returncode:raise ValueError(label+' process exit '+str(result.returncode))
try:lines=result.stdout.decode('utf-8').splitlines()
except UnicodeDecodeError as error:raise ValueError(label+' stdout is not UTF-8') from error
if len(lines)!=len(wanted):raise ValueError(label+' observation count '+str(len(lines))+' expected '+str(len(wanted)))
for index,(line,expect) in enumerate(zip(lines,wanted),1):
try:got=json.loads(line)
except ValueError as error:raise ValueError(label+' invalid JSON at '+str(index)) from error
if not same(got,expect):
raise ValueError(label+' observation '+str(index)+' mismatch: got '+repr(got)[:700]+' expected '+repr(expect)[:700])
def main():
config=json.loads(Path(__file__).with_name('cases.json').read_text())
candidate=Path(sys.argv[1])
scratch=Path(tempfile.mkdtemp(prefix='input-frame-',dir=Path.cwd()))
sources={name:(candidate/name).read_bytes() for name in config['candidate_files']}
sources.update({name:data.encode('utf-8') for name,data in config['context'].items()})
sources['probe.cpp']=config['probe.cpp'].encode('utf-8')
for name,data in sources.items():(scratch/name).write_bytes(data)
plans=[]
for seed in (config['seed'],config['seed']+97,config['seed']+401):
lines=lines_for(seed)
plans.append((('\n'.join(lines)).encode('ascii'),console_answers(lines),'console-'+str(seed)))
probe_expected=direct_answers()
# All candidate reads and scratch writes finish before the first child starts.
common=['g++','-std=c++17','-O2','-Wall','-Wextra','-Wpedantic','state.cpp','registry.cpp','reports.cpp','frames.cpp']
built=run_child(common+['input_console.cpp','-o','input-console'],scratch,timeout=25)
if built.returncode:raise ValueError('console build failed')
built=run_child(common+['probe.cpp','-o','api-probe'],scratch,timeout=25)
if built.returncode:raise ValueError('probe build failed')
for stdin,wanted,label in plans:
compare(run_child([str(scratch/'input-console')],scratch,stdin),wanted,label)
compare(run_child([str(scratch/'api-probe')],scratch),probe_expected,'direct-api')
print('PASS')
return 0
if __name__=='__main__':
try:raise SystemExit(main())
except (ValueError,subprocess.TimeoutExpired) as error:
print(str(error));raise SystemExit(1)