amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth
range given to batch 22), each with at least one independent
contract-edge defect and plain public docs. Claude reviewed 100/100
USABLE against current digests; no ID or source overlap with main or
batch 22. Difficulty is unmeasured.

progress.md records the wrap-up: all Codex sessions and Qwen schedulers
stopped for the user's rollout, Qwen results so far (57 HARD of 373),
and what is left on each batch branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:52:13 +08:00

388 lines
22 KiB
Python

from pathlib import Path
import copy
import random
import shutil
import subprocess
import sys
import tempfile
OBSERVER = '#include "reference_ledger.hpp"\n#include <iostream>\n#include <string>\n#include <vector>\n\nusing reference::Number;\nusing reference::View;\n\nvoid print_view(const View &v)\n{\n std::cout << \' \' << v.id << \' \' << v.payload << \' \' << v.incarnation\n << \' \' << v.state << \' \' << v.base << \' \' << v.total << \' \' << v.cpus;\n}\n\nint main()\n{\n reference::Ledger ledgers[2];\n unsigned selected = 0;\n std::string op;\n while (std::cin >> op) {\n auto &ledger = ledgers[selected];\n Number id = 0, cpu = 0, n = 0, nonce = 0;\n if (op == "U") {\n std::cin >> selected;\n std::cout << "U " << selected << \'\\n\';\n } else if (op == "Z") {\n std::cin >> n;\n std::cout << "R " << ledger.reset(n) << \'\\n\';\n } else if (op == "C") {\n std::cin >> id >> n;\n std::cout << "R " << ledger.create(id, n) << \'\\n\';\n } else if (op == "G" || op == "P") {\n std::cin >> id >> cpu >> n;\n int rc = op == "G" ? ledger.get(id, cpu, n) : ledger.put(id, cpu, n);\n std::cout << "R " << rc << \'\\n\';\n } else if (op == "F") {\n std::cin >> id >> cpu;\n std::cout << "R " << ledger.fold_cpu(id, cpu) << \'\\n\';\n } else if (op == "A" || op == "K" || op == "E" || op == "I") {\n std::cin >> id;\n int rc = 0;\n if (op == "A") rc = ledger.fold_all(id);\n if (op == "K") rc = ledger.kill(id);\n if (op == "E") rc = ledger.erase(id);\n if (op == "I") rc = ledger.contains(id);\n std::cout << (op == "I" ? "I " : "R ") << rc << \'\\n\';\n } else if (op == "Q") {\n std::cin >> id >> nonce;\n View value{nonce, nonce, nonce, nonce, nonce, nonce, nonce};\n int rc = ledger.query(id, value);\n std::cout << "Q " << rc;\n print_view(value);\n std::cout << \'\\n\';\n } else if (op == "D") {\n std::cin >> id >> cpu >> nonce;\n reference::CpuView value{nonce, nonce, nonce};\n int rc = ledger.query_cpu(id, cpu, value);\n std::cout << "D " << rc << \' \' << value.local << \' \' << value.folded\n << \' \' << value.pending << \'\\n\';\n } else if (op == "T") {\n std::cin >> id >> nonce;\n int rc = ledger.total(id, nonce);\n std::cout << "T " << rc << \' \' << nonce << \'\\n\';\n } else if (op == "N") {\n std::cout << "N " << ledger.resident_count() << \' \' << ledger.cpu_count() << \'\\n\';\n } else if (op == "L") {\n auto events = ledger.events();\n std::cout << "L " << events.size();\n for (const auto &v : events) {\n std::cout << \' \' << v.sequence << \' \' << v.id << \' \' << v.incarnation << \' \' << v.payload;\n }\n std::cout << \'\\n\';\n } else if (op == "S") {\n auto v = ledger.stats();\n std::cout << "S " << v.active << \' \' << v.killed << \' \' << v.released\n << \' \' << v.total << \' \' << v.pending << \' \' << v.releases << \'\\n\';\n } else if (op == "H") {\n std::cin >> n >> nonce;\n reference::Release value{nonce, nonce, nonce, nonce};\n int rc = ledger.event(n, value);\n std::cout << "H " << rc << \' \' << value.sequence << \' \' << value.id\n << \' \' << value.incarnation << \' \' << value.payload << \'\\n\';\n } else if (op == "V") {\n std::cin >> n >> nonce;\n std::vector<Number> ids(static_cast<std::size_t>(n));\n for (auto &v : ids) std::cin >> v;\n std::vector<View> output{{nonce, nonce, nonce, nonce, nonce, nonce, nonce}};\n int rc = ledger.select(ids, output);\n std::cout << "V " << rc << \' \' << output.size();\n for (const auto &v : output) print_view(v);\n std::cout << \'\\n\';\n } else if (op == "B") {\n std::cin >> n >> nonce;\n std::vector<reference::Op> operations(static_cast<std::size_t>(n));\n for (auto &v : operations) std::cin >> v.kind >> v.id >> v.cpu >> v.amount;\n std::vector<Number> totals{nonce, nonce + 1};\n int rc = ledger.batch(operations, totals);\n std::cout << "B " << rc << \' \' << totals.size();\n for (auto v : totals) std::cout << \' \' << v;\n std::cout << \'\\n\';\n } else {\n return 2;\n }\n if (!std::cin) return 2;\n }\n return std::cin.bad() ? 2 : 0;\n}\n'
CANDIDATES = ['reference_ledger.hpp','reference_ledger.cpp','reference_batch.cpp']
LIMIT = 10**12
LOW = -(2**63)
HIGH = 2**63-1
class Model:
def __init__(self):
self.cpus = 4
self.objects = {}
self.creation = 0
self.releases = []
def find(self, key):
if not 1 <= key <= 1000000: return -22
return 0 if key in self.objects else -2
def base(self, obj):
return obj['total'] - sum(a-b for a,b in zip(obj['local'],obj['folded']))
def view(self, key):
obj = self.objects[key]
return [key,obj['payload'],obj['incarnation'],obj['state'],self.base(obj),obj['total'],self.cpus]
def fold(self, obj, cpu=None):
if cpu is None: obj['folded'] = obj['local'][:]
else: obj['folded'][cpu] = obj['local'][cpu]
def release(self, key):
obj = self.objects[key]
if obj['state'] == 1 and obj['total'] == 0:
self.fold(obj)
obj['state'] = 2
self.releases.append([len(self.releases)+1,key,obj['incarnation'],obj['payload']])
def mutate(self, op, args):
if op == 'Z':
cpus, = args
if cpus < 1 or cpus > 8: return -22
self.__init__()
self.cpus = cpus
return 0
key = args[0]
if op == 'C':
if not 1 <= key <= 1000000: return -22
if key in self.objects: return -17
if len(self.objects) >= 8: return -28
self.creation += 1
self.objects[key] = dict(payload=args[1],incarnation=self.creation,state=0,total=1,
local=[0]*self.cpus,folded=[0]*self.cpus)
return 0
status = self.find(key)
if status: return status
obj = self.objects[key]
if op in ('G','P','F'):
cpu = args[1]
if cpu < 0 or cpu >= self.cpus: return -22
if op in ('G','P'):
amount = args[2]
if amount < 1 or amount > LIMIT: return -34
if obj['state'] == 2 or op == 'G' and obj['state'] != 0: return -108
if op == 'G':
if obj['total'] + amount > LIMIT: return -34
obj['total'] += amount
obj['local'][cpu] += amount
else:
minimum = int(obj['state'] == 0)
if amount > obj['total'] - minimum: return -34
obj['total'] -= amount
obj['local'][cpu] -= amount
self.release(key)
elif op == 'F': self.fold(obj,cpu)
elif op == 'A': self.fold(obj)
elif op == 'K':
if obj['state'] != 0: return -16
obj['state'] = 1
obj['total'] -= 1
self.fold(obj)
self.release(key)
elif op == 'E':
if obj['state'] != 2: return -16
del self.objects[key]
return 0
def output(self, op, a):
if op in ('Z','C','G','P','F','A','K','E'): return ['R',self.mutate(op,a)]
if op == 'I':
status = self.find(a[0])
return ['I',-22 if status == -22 else int(status == 0)]
if op == 'N': return ['N',len(self.objects),self.cpus]
if op == 'L': return ['L',len(self.releases)]+[n for row in self.releases for n in row]
if op == 'S':
counts=[sum(o['state']==state for o in self.objects.values()) for state in range(3)]
pending=sum(sum(x-y for x,y in zip(o['local'],o['folded'])) for o in self.objects.values())
return ['S']+counts+[sum(o['total'] for o in self.objects.values()),pending,len(self.releases)]
if op == 'H':
index,nonce=a
return ['H',0]+self.releases[index] if 0 <= index < len(self.releases) else ['H',-34]+[nonce]*4
if op == 'V':
count,nonce,*ids=a
if count>8: return ['V',-22,1]+[nonce]*7
for key in ids:
status=self.find(key)
if status: return ['V',status,1]+[nonce]*7
return ['V',0,count]+[v for key in ids for v in self.view(key)]
if op == 'B':
count,nonce,*fields=a
if count>64: return ['B',-22,2,nonce,nonce+1]
staged=copy.deepcopy(self)
totals=[]
mapping={1:'G',2:'P',3:'F',4:'A',5:'K',6:'E'}
for i in range(count):
kind,key,cpu,amount=fields[i*4:i*4+4]
if kind not in mapping: return ['B',-22,2,nonce,nonce+1]
command=mapping[kind]
params=[key,cpu,amount] if kind in (1,2) else [key,cpu] if kind==3 else [key]
status=staged.mutate(command,params)
if status: return ['B',status,2,nonce,nonce+1]
totals.append(0 if kind==6 else staged.objects[key]['total'])
self.__dict__=staged.__dict__
return ['B',0,count]+totals
key=a[0]
status=self.find(key)
nonce=a[-1]
if op == 'Q': return ['Q',status]+([nonce]*7 if status else self.view(key))
if op == 'T': return ['T',status,nonce if status else self.objects[key]['total']]
if op == 'D':
cpu=a[1]
if status == 0 and not 0 <= cpu < self.cpus: status=-22
if status: return ['D',status,nonce,nonce,nonce]
obj=self.objects[key]
x,y=obj['local'][cpu],obj['folded'][cpu]
return ['D',0,x,y,x-y]
raise RuntimeError(op)
class Suite:
def __init__(self):
self.models=[Model(),Model()]
self.selected=0
self.commands=[]
self.expected=[]
self.labels=[]
@property
def model(self): return self.models[self.selected]
def add(self,label,op,*args):
self.commands.append(' '.join(map(str,[op]+list(args))))
if op=='U':
self.selected=args[0]
result=['U',self.selected]
else: result=self.model.output(op,list(args))
self.expected.append(' '.join(map(str,result)))
self.labels.append(label)
def inspect(self,label,keys=None):
for op in ('N','S','L'): self.add(label,op)
for idx in (-1,0,len(self.model.releases)-1,len(self.model.releases)):
self.add(label,'H',idx,771)
if keys is None: keys=list(self.model.objects)
for key in keys:
self.add(label,'I',key)
self.add(label,'Q',key,773)
self.add(label,'T',key,775)
for cpu in range(-1,self.model.cpus+1): self.add(label,'D',key,cpu,777)
ids=list(keys)[:8]
self.add(label,'V',len(ids),779,*reversed(ids))
def batch(self,label,operations,nonce=811):
self.add(label,'B',len(operations),nonce,*[x for row in operations for x in row])
def suite():
t=Suite()
t.inspect('R1 R10 R11 R16 default empty',[0,1,1000000,1000001])
for cpus in (1,8):
t.add('R1 CPU reset endpoint','Z',cpus)
t.add('R4 ID payload endpoints','C',1,LOW)
t.add('R4 ID payload endpoints','C',1000000,HIGH)
t.add('R5 get end CPU','G',1,cpus-1,6)
t.add('R1 invalid reset atomic','Z',0)
t.add('R1 invalid reset atomic','Z',9)
t.add('R1 invalid reset atomic','Z',-1)
t.add('R1 signed reset endpoint','Z',LOW)
t.add('R1 signed reset endpoint','Z',HIGH)
t.inspect('R1 reset preserves state on error')
t.add('R1 isolation','U',1)
t.inspect('R1 untouched second ledger',[1,1000000])
t.add('R1 isolation','C',9,-999)
t.add('R1 isolation','U',0)
t.inspect('R1 first ledger retained')
t.add('R1 reset','Z',4)
t.add('smoke fold witness','C',17,9001)
t.add('smoke fold witness','G',17,0,3)
t.add('smoke fold witness','T',17,88)
for i in range(2):
t.add('R7 repeat fold witness','F',17,0)
t.inspect('R7 repeat fold exact watermarks',[17])
t.add('R5 cross-CPU witness','Z',4)
t.add('R5 cross-CPU witness','C',99,42)
t.add('R5 cross-CPU witness','G',99,3,12)
t.add('R5 cross-CPU witness','P',99,0,4)
t.inspect('R5 signed negative pending',[99])
t.add('R5 fold negative','F',99,0)
t.add('R5 more negative after fold','P',99,0,3)
t.add('R5 total owner floor','P',99,1,5)
t.inspect('R5 negative local and watermark',[99])
t.add('R8 kill borrowed','K',99)
t.add('R8 no new acquisition','G',99,0,1)
t.add('R9 final migrated return','P',99,2,0)
t.inspect('R8 killed holds zero borrowed',[99])
t.add('R9 tombstone query','A',99)
t.add('R9 tombstone query','F',99,3)
t.add('R9 repeated kill','K',99)
t.add('R9 repeated put','P',99,3,1)
t.inspect('R9 one callback preserved',[99])
t.add('R9 erase reuse','E',99)
t.add('R4 incarnation reused','C',99,LOW)
t.add('R9 immediate release','K',99)
t.inspect('R4 reused event identity',[99])
t.add('R2 all amount endpoints','Z',8)
t.add('R2 all amount endpoints','C',1,HIGH)
t.add('R6 amount upper inclusive balance overflow','G',1,0,LIMIT)
t.add('R6 total upper exact','G',1,7,LIMIT-1)
t.add('R6 overflow by one','G',1,0,1)
t.add('R6 active owner floor','P',1,6,LIMIT)
t.inspect('R6 failed balance atomic',[1])
t.add('R6 active maximum borrowed','P',1,0,LIMIT-1)
t.add('R6 active owner protected','P',1,7,1)
t.add('R6 get amount lower','G',1,0,1)
t.add('R6 killed amount upper accepted','K',1)
t.add('R6 killed drain','P',1,7,1)
t.inspect('R6 lower upper lifecycle',[1])
# Cross product forces ID/existence/CPU/amount/state/balance priority.
for state in (0,1,2):
t.add('R3 priority setup','Z',2)
t.add('R3 priority setup','C',1,987)
if state==1:
t.add('R3 priority setup','G',1,1,9)
t.add('R3 priority setup','K',1)
if state==2: t.add('R3 priority setup','K',1)
for key in (0,-1,1000001,2,1):
for cpu in (-1,2,0,1):
for amount in (LOW,-1,0,1,LIMIT,LIMIT+1,HIGH):
for op in ('G','P'):
t.add('R3 R6 error precedence',op,key,cpu,amount)
t.add('R7 fold validation','F',key,2)
t.add('R7 fold validation','A',key)
t.add('R8 kill precedence','K',key)
t.add('R9 erase precedence','E',key)
t.inspect('R3 observation preservation after errors',[0,2,1])
t.add('R4 capacity','Z',1)
for key in range(1,9): t.add('R4 capacity','C',key,key*7919)
for key in (0,1,9,1000001): t.add('R4 full duplicate priority','C',key,123)
t.add('R9 live cannot erase','E',1)
t.add('R9 tombstone capacity','K',1)
t.add('R9 tombstone capacity','C',9,999)
t.add('R4 tombstone duplicate','C',1,333)
t.inspect('R4 all resident capacity')
t.add('R9 capacity freed','E',1)
t.add('R4 creation sequence no gaps','C',9,-55)
t.inspect('R4 capacity incarnation')
t.add('R16 select empty','V',0,901)
t.add('R16 select duplicate ordered','V',4,903,9,2,9,8)
t.add('R16 select oversized priority','V',9,905,*([0]*9))
t.add('R16 select invalid second atomic','V',2,907,2,0)
t.add('R16 select missing before invalid','V',2,909,1,0)
t.add('R16 event signed endpoint','H',LOW,911)
t.add('R16 event signed endpoint','H',HIGH,913)
for nonce in (LOW,HIGH):
for key in (0,1,2,9,1000000,1000001):
t.add('R10 R16 nonce endpoints','Q',key,nonce)
t.add('R10 R16 nonce endpoints','D',key,0,nonce)
t.add('R10 R16 nonce endpoints','T',key,nonce)
t.add('R16 nonce endpoints','H',-1,nonce)
t.add('R16 nonce endpoints','V',1,nonce,0)
t.batch('R12 nonce lower endpoint',[[0,0,0,0]],LOW)
t.batch('R12 nonce upper endpoint',[[0,0,0,0]],HIGH-1)
t.add('R5 negative aggregate reset','Z',2)
t.add('R5 negative aggregate create','C',77,123)
t.add('R5 negative aggregate acquire','G',77,1,9)
t.add('R5 fold positive contribution','A',77)
t.add('R5 negative aggregate migrated put','P',77,0,3)
t.inspect('R5 R16 negative pending sum',[77])
t.add('R8 kill remaining borrowed','K',77)
t.add('R9 killed erase forbidden','E',77)
t.add('R8 repeated kill with borrowed','K',77)
t.add('R9 partial killed return','P',77,0,2)
t.inspect('R8 R16 killed negative pending',[77])
t.add('R9 final killed return','P',77,0,4)
t.inspect('R9 exact single event',[77])
t.add('R12 batch reset','Z',4)
t.add('R12 batch create','C',1,111)
t.add('R12 batch create','C',2,222)
t.batch('R12 empty',[])
t.batch('R12 full 64',[[1,1,0,1]]*64)
t.batch('R12 successful fold_cpu unused fields',[[1,1,0,5],[3,1,0,HIGH],[4,1,LOW,HIGH],[2,1,3,5]])
t.batch('R12 oversize 65 priority',[[0,0,-1,0]]*65)
t.inspect('R12 max batch output',[1,2])
t.batch('R12 failed atomic event rollback',[[5,2,HIGH,LOW],[6,2,LOW,HIGH],[0,1,0,1]])
t.inspect('R12 events and erase rollback',[1,2])
t.batch('R12 invalid cpu after valid get rollback',[[1,1,0,5],[3,1,4,0]])
t.batch('R12 invalid amount after valid put rollback',[[2,1,3,2],[1,1,0,0]])
t.batch('R12 valid state sequence',[[2,1,3,64],[4,1,HIGH,LOW],[5,1,0,0],[6,1,HIGH,LOW],[5,2,0,0],[6,2,0,0]])
t.inspect('R12 committed event order',[1,2])
t.add('R4 no aborted sequence changes','C',1,333)
t.add('R9 event after aborted release','K',1)
t.inspect('R12 callback numbering',[1])
rng=random.Random(0xC01DF01D)
for epoch in range(10):
cpus=rng.choice([1,2,4,8])
t.add('random reset','Z',cpus)
for key in range(1,7): t.add('random create','C',key,rng.randint(-10**12,10**12))
for step in range(110):
key=rng.choice([0,1,2,3,4,5,6,7,1000000,1000001])
op=rng.choice(['G','G','P','P','F','A','K','E','C','B'])
cpu=rng.choice([-1,0,cpus-1,cpus])
amount=rng.choice([0,1,2,7,31,LIMIT-1,LIMIT,LIMIT+1])
if op in ('G','P'): t.add('random signed balances',op,key,cpu,amount)
elif op=='F': t.add('random fold',op,key,cpu)
elif op=='C': t.add('random recreate',op,key,rng.randint(LOW,HIGH))
elif op=='B':
operations=[[rng.randint(0,7),rng.randint(1,7),rng.randrange(cpus),rng.randint(1,7)] for _ in range(rng.randint(0,8))]
t.batch('random atomic mixed sequence',operations)
else: t.add('random lifecycle',op,key)
t.add('random direct total','T',key,551)
t.add('random direct CPU','D',key,rng.randrange(cpus),553)
t.add('random direct query','Q',key,555)
if step%17==0: t.inspect('random full observations')
t.inspect('random epoch final')
# deterministic valid drain sequences beyond smoke and random failure-heavy histories
for cpus in (1,2,8):
t.add('full drain reset','Z',cpus)
for key in range(1,9):
t.add('full drain create','C',key,key*101)
t.add('full drain acquire','G',key,cpus-1,key*13)
t.add('full drain return other CPU','P',key,0,key*7)
t.add('full drain fold','F',key,0)
t.add('full drain kill','K',key)
t.inspect('all killed before drain')
for key in (8,1,7,2,6,3,5,4):
t.add('full drain final return','P',key,0,key*6)
t.add('full drain repeated fold','A',key)
t.add('full drain final total','T',key,666)
t.inspect('release ordering all CPUs')
for key in range(1,9): t.add('release erase','E',key)
t.inspect('empty after erase retains events')
return t
def main():
candidate=Path(sys.argv[1])
sources={name:(candidate/name).read_bytes() for name in CANDIDATES}
test=suite()
stdin='\n'.join(test.commands)+'\n'
expected='\n'.join(test.expected)+'\n'
compiler=shutil.which('g++')
if compiler is None: raise RuntimeError('C++17 compiler unavailable')
scratch=Path(tempfile.mkdtemp(prefix='reference-observe-',dir=Path.cwd()))
for name,data in sources.items(): (scratch/name).write_bytes(data)
(scratch/'reference_console.cpp').write_text(OBSERVER)
executable=scratch/'observer'
command=[compiler,'-std=c++17','-O1','-Wall','-Wextra','-pedantic',str(scratch/'reference_ledger.cpp'),str(scratch/'reference_batch.cpp'),str(scratch/'reference_console.cpp'),'-o',str(executable)]
# All trusted file access is complete before the first compiler/candidate process.
build=subprocess.run(command,capture_output=True,text=True,timeout=35)
if build.returncode:
print('BUILD',build.returncode,'RUN NOT_STARTED',file=sys.stderr)
print('build rejected')
return 1
run=subprocess.run([str(executable)],input=stdin,capture_output=True,text=True,timeout=20)
print('BUILD 0 RUN',run.returncode,'COMMANDS',len(test.commands),'STDERR_BYTES',len(run.stderr.encode()),file=sys.stderr)
if run.returncode:
print('candidate execution exit '+str(run.returncode))
return 1
if run.stderr:
print('R3 R13 stderr must be empty')
return 1
if run.stdout != expected:
got=run.stdout.splitlines()
want=test.expected
at=next((i for i,(a,b) in enumerate(zip(got,want)) if a!=b),min(len(got),len(want)))
label=test.labels[at] if at<len(test.labels) else 'R13 extra output'
print('behavior rejected command '+str(at+1)+' '+label)
print('MISMATCH',at+1,'INPUT',test.commands[at] if at<len(test.commands) else '<end>',
'EXPECTED',want[at] if at<len(want) else '<end>','OBSERVED',got[at] if at<len(got) else '<end>',file=sys.stderr)
return 1
print('PASS')
return 0
if __name__=='__main__':
raise SystemExit(main())