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

274 lines
12 KiB
Python

"""Independent cohort oracle; submitted C++ runs only in subprocesses."""
import json
import random
import re
import subprocess
import sys
import tempfile
from pathlib import Path
class Model:
def __init__(self):
self.ready = 0
self.count = 0
self.mask = 0
self.next_ticket = 1
self.stats = [0] * 8
self.states = {}
self.leases = {}
self.budgets = {}
self.events = []
def cpus(self):
return [cpu for cpu in range(self.count) if self.mask & (1 << cpu)]
def start(self, key, cpu, tag):
self.stats[0] += 1
pair = (key, cpu)
if self.budgets.get(pair, 0):
self.budgets[pair] -= 1
self.stats[1] += 1
self.events.append(('S', key, cpu, 0, -5))
return False
ticket = self.next_ticket
self.next_ticket += 1
self.leases[pair] = (ticket, tag)
self.events.append(('S', key, cpu, ticket, 0))
return True
def stop(self, key, cpu, reason):
lease = self.leases.pop((key, cpu), None)
if lease is not None:
self.stats[2] += 1
self.events.append(('T', key, cpu, lease[0], reason))
def transact(self, jobs):
done = []
for key, cpu, tag in jobs:
if not self.start(key, cpu, tag):
for old_key, old_cpu in reversed(done):
self.stop(old_key, old_cpu, 1)
return -5
done.append((key, cpu))
return 0
def apply(self, line):
self.events = []
parts = line.split()
name = parts[0]
if any(re.fullmatch(r'[+-]?[0-9]+', value) is None for value in parts[1:]):
return -22
try:
args = [int(value, 10) for value in parts[1:]]
except ValueError:
return -22
counts = dict(RESET=2, ARM=3, REG=4, REMOVE=1, UP=1, DOWN=1, SYNC=1, SNAP=0)
if name not in counts or len(args) != counts[name]:
return -22
if any(value < -(1 << 63) or value >= 1 << 63 for value in args):
return -22
if name == 'RESET':
count, mask = args
if not 1 <= count <= 16 or not 0 <= mask < (1 << count):
return -22
self.__init__()
self.ready, self.count, self.mask = 1, count, mask
return 0
if name in ('ARM', 'REG', 'REMOVE', 'SYNC') and not 1 <= args[0] <= 31:
return -22
if name == 'ARM' and not (0 <= args[1] < 16 and 0 <= args[2] <= 4):
return -22
if name == 'REG' and not (-100 <= args[1] <= 100 and 0 <= args[2] <= 65535 and args[3] in (0, 1)):
return -22
if name in ('UP', 'DOWN') and not 0 <= args[0] < 16:
return -22
if not self.ready:
return -19
if name == 'SNAP':
return 0
if name == 'ARM':
key, cpu, budget = args
if cpu >= self.count:
return -22
self.budgets[(key, cpu)] = budget
return 0
if name == 'REG':
key, priority, tag, replay = args
if key in self.states:
return -17
if len(self.states) == 8:
return -28
jobs = [(key, cpu, tag) for cpu in self.cpus()] if replay else []
result = self.transact(jobs)
if result == 0:
self.states[key] = (priority, tag, replay)
self.stats[3] += 1
return result
if name in ('REMOVE', 'SYNC'):
key = args[0]
if key not in self.states:
return -2
if name == 'REMOVE':
for cpu in range(self.count - 1, -1, -1):
self.stop(key, cpu, 3)
del self.states[key]
self.stats[4] += 1
return 0
jobs = [(key, cpu, self.states[key][1]) for cpu in self.cpus() if (key, cpu) not in self.leases]
if not jobs:
return 0
result = self.transact(jobs)
if result == 0:
self.stats[7] += 1
return result
cpu = args[0]
if cpu >= self.count:
return -22
already = bool(self.mask & (1 << cpu))
if (name == 'UP') == already:
return 0
ordered = sorted(self.states, key=lambda key: (self.states[key][0], key))
if name == 'DOWN':
for key in reversed(ordered):
self.stop(key, cpu, 2)
self.mask &= ~(1 << cpu)
self.stats[6] += 1
return 0
jobs = [(key, cpu, self.states[key][1]) for key in ordered]
result = self.transact(jobs)
if result == 0:
self.mask |= 1 << cpu
self.stats[5] += 1
return result
def observe(self, result):
lines = ['R %d' % result]
lines.extend('E %s %d %d %d %d' % event for event in self.events)
fields = [self.ready, self.count, self.mask, bin(self.mask).count('1'), self.next_ticket]
fields += self.stats + [len(self.states)]
lines.append('S ' + ' '.join(map(str, fields)))
for key, state in sorted(self.states.items()):
mask = sum(1 << cpu for owner, cpu in self.leases if owner == key)
lines.append('D %d %d %d %d %d' % (key, *state, mask))
for (key, cpu), (ticket, tag) in sorted(self.leases.items()):
lines.append('L %d %d %d %d' % (key, cpu, ticket, tag))
for (key, cpu), remaining in sorted(self.budgets.items()):
if remaining:
lines.append('F %d %d %d' % (key, cpu, remaining))
lines.append('END')
return lines
def scenarios(seed):
events = ['SNAP', 'REG 1 0 1 1', 'UP 15', 'ARM 1 15 4', 'UP -1', 'REG 0 0 0 1',
'RESET 0 0', 'RESET 16 65536', 'RESET 6 63', 'ARM 7 4 1',
'REG 7 10 418 1', 'REG 7 10 418 1', 'REMOVE 7', 'SNAP']
# Failure at every replay position, empty/sparse/full topology, all tag boundaries.
for count in (1, 2, 6, 16):
for mask in (0, 1, (1 << count) - 1, sum(1 << cpu for cpu in range(0, count, 2))):
for fail_cpu in range(count):
events += ['RESET %d %d' % (count, mask), 'ARM 3 %d 1' % fail_cpu,
'REG 3 -100 65535 1', 'REG 3 100 0 1', 'SYNC 3', 'REMOVE 3', 'REMOVE 3']
# CPU online rollback with first/middle/last failures, tie order and registration disorder.
for failure_key in (2, 5, 9, 12):
for cpu in (0, 7, 15):
events += ['RESET 16 0', 'REG 12 100 5 0', 'REG 9 0 0 1',
'REG 5 0 65535 0', 'REG 2 -100 17 1',
'ARM %d %d 2' % (failure_key, cpu), 'UP %d' % cpu,
'UP %d' % cpu, 'UP %d' % cpu, 'UP %d' % cpu,
'DOWN %d' % cpu, 'DOWN %d' % cpu, 'UP %d' % cpu,
'REMOVE 5', 'DOWN %d' % cpu]
# Partial cohorts, SYNC preserves previously acquired leases and the original replay flag.
for fail_cpu in range(1, 6):
events += ['RESET 6 63', 'REG 31 0 47 0', 'UP 0', 'DOWN 0', 'UP 0',
'ARM 31 %d 1' % fail_cpu, 'SYNC 31', 'SNAP', 'SYNC 31',
'SYNC 31', 'DOWN 5', 'REMOVE 31', 'REG 31 -100 9 1']
# Capacity wins before callback, duplicate wins before capacity; ARM survives REMOVE.
events += ['RESET 2 3'] + ['REG %d %d %d 0' % (key, key, key * 300) for key in range(1, 9)]
events += ['ARM 9 0 4', 'REG 9 0 1 1', 'REG 4 0 1 1', 'REMOVE 1',
'REG 9 0 1 1', 'REG 9 0 1 0', 'REMOVE 9', 'REG 9 0 1 1',
'ARM 9 0 0', 'REG 9 0 1 1', 'RESET 1 0', 'UP 0', 'UP 0', 'DOWN 0', 'DOWN 0']
rng = random.Random(seed)
for _ in range(14):
count = rng.choice((1, 2, 3, 8, 16))
events.append('RESET %d %d' % (count, rng.randrange(1 << count)))
keys = rng.sample(range(1, 32), 11)
for step in range(120):
action = rng.choice(('REG', 'REG', 'ARM', 'UP', 'DOWN', 'SYNC', 'REMOVE', 'SNAP'))
key, cpu = rng.choice(keys), rng.randrange(count)
if action == 'REG':
args = [key, rng.choice((-100, -1, 0, 0, 100)), rng.randrange(65536), rng.randrange(2)]
elif action == 'ARM':
args = [key, cpu, rng.randrange(5)]
elif action in ('UP', 'DOWN'):
args = [cpu]
elif action in ('SYNC', 'REMOVE'):
args = [key]
else:
args = []
events.append(' '.join(map(str, [action] + args)))
events += ['RESET 3 5', 'REG 1 -100 0 1', 'ARM 1 2 4',
'RESET 17 0', 'RESET -1 0', 'RESET 3 -1', 'RESET 3 8',
'ARM 0 0 1', 'ARM 32 0 1', 'ARM 1 -1 1', 'ARM 1 16 1', 'ARM 1 3 1',
'ARM 1 2 -1', 'ARM 1 2 5', 'REG 32 0 0 1', 'REG 2 -101 0 1',
'REG 2 101 0 1', 'REG 2 0 -1 1', 'REG 2 0 65536 1', 'REG 2 0 0 2',
'UP 3', 'DOWN 15', 'UP 16', 'REMOVE 32', 'SYNC 0', 'SNAP 1',
'REG', 'ARM 1 0', 'UP 0 1', 'RESET 1 0 9', 'NONSENSE 1', 'UP x',
'REG 2 0x10 0 1', 'UP 9223372036854775808',
'SNAP 9223372036854775808', 'SNAP -9223372036854775809',
'RESET 1 0 9223372036854775808', 'REG 2 0 0 1 9223372036854775808',
'ARM 1 0 1 -9223372036854775809', 'SNAP +', 'UP 1_0',
'UP -9223372036854775808', 'UP +9223372036854775807',
'SNAP', ' ', 'RESET 16 65535']
return events
def main():
config = json.loads(Path(__file__).with_name('cases.json').read_text())
events = scenarios(config['seed'])
model = Model()
expected = []
operations = 0
for line in events:
if line.split():
expected.extend(model.observe(model.apply(line)))
operations += 1
scratch = Path(tempfile.mkdtemp(prefix='dev-hotplug-cohort-'))
for name, content in config['context'].items():
(scratch / name).write_text(content)
for name in config['candidates']:
(scratch / name).write_bytes((Path(sys.argv[1]) / name).read_bytes())
stdin = '\n'.join(events) + '\n'
# No root access to scratch occurs after untrusted compilation begins.
try:
built = subprocess.run(['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic',
'cohort.cpp', 'cpu_set.cpp', 'ledger.cpp', 'inspect.cpp',
'observer.cpp', '-o', 'cohort-demo'],
cwd=scratch, capture_output=True, text=True, timeout=35)
if built.returncode:
print('build failed: ' + built.stderr[-1800:])
return 1
run = subprocess.run([str(scratch / 'cohort-demo')], cwd=scratch, input=stdin,
capture_output=True, text=True, timeout=25)
if run.returncode:
print('candidate exited %d' % run.returncode)
return 1
actual = run.stdout.splitlines()
for index in range(max(len(actual), len(expected))):
got = actual[index] if index < len(actual) else '<missing>'
want = expected[index] if index < len(expected) else '<extra>'
if got != want:
print('observation %d mismatch: %r != %r' % (index + 1, got[:240], want[:240]))
return 1
print('verified %d operations and %d observation lines' % (operations, len(expected)))
print('PASS')
return 0
finally:
# Wrapper runs this cleanup as the candidate UID too; root follows no child paths.
subprocess.run(['rm', '-rf', '--', str(scratch)], cwd='/', capture_output=True, timeout=10)
if __name__ == '__main__':
raise SystemExit(main())