amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

415 lines
17 KiB
Python

"""Independent FIFO/worker model; C subprocesses emit observations only."""
import json
import random
import re
import subprocess
import sys
import tempfile
import time
from pathlib import Path
class ServiceModel:
def __init__(self):
self.reset(1, 4)
def reset(self, workers, capacity):
self.n, self.capacity = workers, capacity
self.phase = 0
self.jobs = []
self.accepted = self.completed = self.cancelled = self.rejected = 0
self.total = self.hash = 0
self.workers = [[0 if i < workers else 3, 0, 0, 0] for i in range(6)]
def wake_one(self):
for worker in self.workers[:self.n]:
if worker[0] == 1:
worker[0] = 0
return
def turn(self, index):
worker = self.workers[index]
if worker[0] != 0:
return
if not self.jobs:
worker[0] = 1 if self.phase == 0 else 2
return
identity, value, lane = self.jobs.pop(0)
result = value * (lane + 1) + identity
self.completed += 1
worker[1] += 1
self.total += result
worker[2] += result
self.hash = (self.hash * 33 + identity * 17 + value + lane) & 0xffffffff
worker[3] = (worker[3] * 33 + identity * 17 + value + lane) & 0xffffffff
def command(self, line):
words = line.split()
if not words:
return None
command, raw = words[0], words[1:]
rc = -1
arguments = None
if all(re.fullmatch(r'[+-]?[0-9]+', token) for token in raw):
arguments = [int(token) for token in raw]
if any(not -2147483648 <= value <= 2147483647 for value in arguments):
arguments = None
arities = dict(I=2, S=3, T=1, A=1, C=1, X=1, N=1, Q=0)
if arguments is not None and len(arguments) == arities.get(command, -1):
a = arguments
if command == 'I' and 1 <= a[0] <= 6 and 1 <= a[1] <= 16:
self.reset(*a)
rc = 0
elif command == 'S' and 1 <= a[0] <= 65535 and -100000 <= a[1] <= 100000 and 0 <= a[2] <= 3:
if self.phase:
rc = -2
elif any(job[0] == a[0] for job in self.jobs):
rc = -4
elif len(self.jobs) == self.capacity:
rc = -3
else:
self.jobs.append(a[:])
self.accepted += 1
self.wake_one()
rc = 0
elif command in ('T', 'N') and 0 <= a[0] < self.n:
rc = 0
if command == 'T':
self.turn(a[0])
elif self.workers[a[0]][0] == 1:
self.workers[a[0]][0] = 0
elif command == 'A' and 0 <= a[0] <= 128:
rc = 0
for _ in range(a[0]):
for index in range(self.n):
self.turn(index)
elif command == 'C' and 1 <= a[0] <= 65535:
rc = -5
for offset, job in enumerate(self.jobs):
if job[0] == a[0]:
self.jobs.pop(offset)
self.cancelled += 1
rc = 0
break
elif command == 'X' and a[0] in (0, 1):
rc = 0
if self.phase == 0:
self.phase = a[0] + 1
if a[0]:
self.cancelled += len(self.jobs)
self.jobs = []
for worker in self.workers[:self.n]:
if worker[0] == 1:
worker[0] = 0
elif command == 'Q':
rc = 0
if rc:
self.rejected += 1
values = [rc, self.phase, self.accepted, self.completed, self.cancelled,
self.rejected, len(self.jobs), self.n, 0, self.total, self.hash]
values += [value for worker in self.workers for value in worker]
values += [value for job in self.jobs for value in job]
values += [0] * ((16 - len(self.jobs)) * 3)
return ' '.join(map(str, values))
def service_cases(seed):
rng = random.Random(seed)
lines = ['Q', 'I 1 1', 'A 1', 'S 41 -7 2', 'Q', 'T 0', 'T 0',
'X 0', 'A 2', 'I 4 3', 'A 1', 'X 1', 'A 2', 'Q',
'I 3 4', 'S 65535 -100000 3', 'S 1 100000 0', 'X 0',
'C 1', 'A 4', 'X 1', 'S 1 0 0', 'Q']
for workers in range(1, 7):
for capacity in (1, 2, 3, 7, 16):
lines += [f'I {workers} {capacity}', 'A 1', 'N 0', 'T 0', 'A 0']
for index in range(capacity):
lines.append(f'S {index + 1} {(-1)**index * (100000-index)} {index % 4}')
lines += ['S 1 0 0', 'S 65535 1 1', 'C 1', 'S 65535 -100000 3',
f'T {workers - 1}', 'A 1', 'N 0', 'A 1', 'C 65535',
'S 1 0 0', 'X 0', 'S 1 0 0', 'A 128', 'Q', 'X 1', 'Q']
lines += [f'I {workers} {capacity}', 'A 1', 'X 1', 'A 4', 'Q']
for _ in range(24):
workers = rng.randint(1, 6)
capacity = rng.randint(1, 16)
lines += [f'I {workers} {capacity}']
for step in range(120):
command = rng.choice('SSSSSTTTAACNNQ')
if command == 'S':
lines.append(f'S {rng.randint(1, 43)} {rng.choice([-100000, 0, 100000, rng.randint(-100000,100000)])} {rng.randrange(4)}')
elif command in ('T', 'N'):
lines.append(f'{command} {rng.randrange(workers)}')
elif command == 'A':
lines.append(f'A {rng.randrange(4)}')
elif command == 'C':
lines.append(f'C {rng.randint(1,43)}')
else:
lines.append('Q')
lines += [f'X {rng.randrange(2)}', 'A 128', 'Q']
lines += ['I 6 16', 'A 1', 'S 65535 100000 3', 'A 1', 'S 65535 -100000 0',
'A 1', 'Q', 'I 0 2', 'I 7 1', 'I 1 0', 'I 1 17', 'I -1 2',
'S 0 0 0', 'S 65536 0 0', 'S 1 -100001 0', 'S 1 100001 0',
'S 1 0 -1', 'S 1 0 4', 'T -1', 'T 6', 'N -1', 'N 6',
'A -1', 'A 129', 'C 0', 'C 65536', 'X -1', 'X 2',
'S 1 0', 'S 1 0 0 7', 'Q 1', 'I 2', 'A', 'T nope',
'S 1 0x1 0', 'S 1 1.5 0', 'I 9999999999999999999999 2',
'bad', 'q', 'T 2147483648', 'Q', ' ', '\t', 'I +1 +1',
'A +0', 'S +1 -0 +0', 'C +1', 'S 1 1 1', 'X 1', 'Q',
'A 128', 'X 0', 'N 0', 'A 1', 'Q', 'I 2 4', 'Q']
return lines
def fifo_cases(seed):
rng = random.Random(seed)
commands = []
for capacity in range(1, 17):
commands += [('J', capacity), ('O',), ('F',), ('K', 0), ('D', 1)]
commands += [('P', index + 1, -100000 + index, index % 4)
for index in range(capacity)]
commands += [('P', 1, 0, 0), ('P', 65535, 100000, 3), ('F',), ('O',),
('P', 65535, 100000, 3), ('V', 65535), ('K', capacity - 1),
('D', 65535), ('E',), ('V', 65535)]
commands += [('J', 0), ('J', 17), ('P', 0, 0, 0), ('P', 65536, 0, 0),
('P', 1, -100001, 0), ('P', 1, 100001, 0), ('P', 1, 0, 4)]
commands += [('J', 7)]
for _ in range(1200):
k = rng.choice('PPPOOKFDVE')
if k == 'P':
commands.append((k, rng.randint(1, 23), rng.randint(-100000, 100000), rng.randrange(4)))
elif k in ('D', 'V'):
commands.append((k, rng.randint(1, 23)))
elif k == 'K':
commands.append((k, rng.randrange(18)))
else:
commands.append((k,))
return commands
def fifo_expected(commands):
queue, capacity = [], 4
rows = []
for event in commands:
key, *a = event
result, job = 0, [77, -777, 7]
if key == 'J':
if 1 <= a[0] <= 16:
capacity, queue = a[0], []
else:
result = -1
elif key == 'P':
identity, value, lane = a
if not (1 <= identity <= 65535 and -100000 <= value <= 100000 and 0 <= lane <= 3):
result = -1
elif len(queue) == capacity or any(x[0] == identity for x in queue):
result = -1
else:
queue.append(a[:])
elif key in ('O', 'F'):
if not queue:
result = -1
else:
job = queue[0][:]
if key == 'O':
queue.pop(0)
elif key == 'D':
result = -1
for index, item in enumerate(queue):
if item[0] == a[0]:
job = queue.pop(index)
result = 0
break
elif key == 'K':
if a[0] >= len(queue):
result = -1
else:
job = queue[a[0]][:]
elif key == 'V':
result = int(any(item[0] == a[0] for item in queue))
elif key == 'E':
result, queue = len(queue), []
fields = [result] + job + [len(queue), capacity, int(not queue), int(len(queue) == capacity), 1]
fields += [value for item in queue for value in item]
fields += [0] * ((16 - len(queue)) * 3)
rows.append(' '.join(map(str, fields)))
return rows
# This helper receives argv, stdin and a deadline only, never oracle expectations.
# The wrapper starts it as the candidate UID so all signal operations stay same-UID.
EXECUTE = r"""
import os
import signal
import subprocess
import sys
limit = float(sys.argv[1])
role = sys.argv[2]
command = sys.argv[3:]
process = None
out = err = b''
result = 125
print('exec start role=%s uid=%d deadline=%.3f' % (role, os.getuid(), limit), file=sys.stderr, flush=True)
try:
data = sys.stdin.buffer.read()
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, start_new_session=True)
try:
out, err = process.communicate(data, timeout=limit)
result = process.returncode if process.returncode >= 0 else 128 - process.returncode
except subprocess.TimeoutExpired as error:
result = 124
print('exec deadline role=%s uid=%d pgid=%d' % (role, os.getuid(), process.pid), file=sys.stderr, flush=True)
os.killpg(process.pid, signal.SIGKILL)
print('exec killpg role=%s uid=%d pgid=%d' % (role, os.getuid(), process.pid), file=sys.stderr, flush=True)
try:
out, err = process.communicate(timeout=2)
except subprocess.TimeoutExpired as tail:
out, err = tail.output or b'', tail.stderr or b''
process.stdout.close()
process.stderr.close()
process.wait(timeout=2)
except Exception as error:
result = 125
print('exec error role=%s: %s' % (role, error), file=sys.stderr, flush=True)
finally:
if process is not None:
try:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
except OSError as error:
result = 125
print('exec group cleanup error role=%s: %s' % (role, error), file=sys.stderr, flush=True)
try:
process.wait(timeout=2)
except subprocess.TimeoutExpired:
result = 125
print('exec reap error role=' + role, file=sys.stderr, flush=True)
sys.stdout.buffer.write(out)
sys.stdout.buffer.flush()
sys.stderr.buffer.write(err)
sys.stderr.buffer.flush()
print('exec end role=%s uid=%d rc=%d' % (role, os.getuid(), result), file=sys.stderr, flush=True)
raise SystemExit(result)
"""
CLEANUP_EXEC = r"""
import os
import shutil
import sys
target = sys.argv[1]
print('cleanup start uid=%d path=%s' % (os.getuid(), target), file=sys.stderr, flush=True)
if os.path.islink(target):
os.unlink(target)
elif os.path.lexists(target):
shutil.rmtree(target)
if os.path.lexists(target):
raise RuntimeError('scratch remains')
print('cleanup confirmed uid=%d path=%s' % (os.getuid(), target), file=sys.stderr, flush=True)
"""
def run_supervised(argv, cwd, stdin=b'', timeout=20, role='candidate'):
# No root-side subprocess timeout: the same-UID helper owns its deadline/killpg.
return subprocess.run([sys.executable, '-I', '-c', EXECUTE, str(timeout), role, *argv],
cwd=cwd, input=stdin, capture_output=True)
def forward_stderr(completed):
if completed.stderr:
print(completed.stderr.decode('utf-8', 'replace')[-2400:], file=sys.stderr, end='')
def compare(kind, completed, answers):
forward_stderr(completed)
if completed.returncode != 0:
print(f'{kind} process failed: {completed.returncode}')
return False
try:
observed = completed.stdout.decode('utf-8').splitlines()
except UnicodeDecodeError:
print(kind + ' stdout is not UTF-8')
return False
if len(observed) != len(answers):
print(f'{kind} observation count mismatch')
return False
for index, (actual, wanted) in enumerate(zip(observed, answers), 1):
if actual != wanted:
print(f'{kind} observation {index} mismatch', file=sys.stderr)
print('observed: ' + actual[:1200], file=sys.stderr)
print('expected: ' + wanted[:1200], file=sys.stderr)
print(f'{kind} observation mismatch')
return False
return True
def main():
config = json.loads(Path(__file__).with_name('support.json').read_text())
service_input = service_cases(config['seed'])
model = ServiceModel()
service_answers = [row for row in (model.command(line) for line in service_input) if row is not None]
queue_input = fifo_cases(config['seed'] + 1)
queue_answers = fifo_expected(queue_input)
stdin_service = ('\n'.join(service_input) + '\n').encode('utf-8')
stdin_queue = ('\n'.join(' '.join(map(str, event)) for event in queue_input) + '\n').encode('utf-8')
candidates = {name: (Path(sys.argv[1]) / name).read_bytes() for name in ('queue.c', 'service.c')}
scratch = Path(tempfile.mkdtemp(prefix='concur-wakeup-queue-'))
print('scratch: ' + str(scratch), file=sys.stderr, flush=True)
programs = [('service', ['queue.c', 'service.c', 'monitor.c', 'cli.c'], stdin_service, service_answers),
('fifo', ['queue.c', 'fifo_probe.c'], stdin_queue, queue_answers)]
passed = False
cleanup_ok = False
budget_end = time.monotonic() + 85
try:
for name, source in config['context'].items():
path = scratch / name
path.parent.mkdir(parents=True, exist_ok=True)
# Prepared before any child: fixed support directories must be removable by its UID.
if path.parent != scratch:
path.parent.chmod(0o777)
path.write_text(source)
for name, source in candidates.items():
(scratch / name).write_bytes(source)
# All files and input/expectation data exist before the first child starts.
# From here the checker uses captured pipes only; cleanup is also a same-UID child.
for kind, sources, stdin, answers in programs:
executable = str(scratch / (kind + '-probe'))
limit = min(30, max(0.001, budget_end - time.monotonic()))
built = run_supervised(['cc', '-std=c11', '-O2', '-Wall', '-Wextra', '-Wpedantic',
*sources, '-o', executable], cwd=scratch,
timeout=limit, role=kind + '-compile')
forward_stderr(built)
if built.returncode:
print(kind + ' build failed: ' + str(built.returncode))
break
limit = min(20, max(0.001, budget_end - time.monotonic()))
completed = run_supervised([executable], cwd=scratch, stdin=stdin,
timeout=limit, role=kind + '-candidate')
if not compare(kind, completed, answers):
break
else:
passed = True
except (OSError, subprocess.SubprocessError) as error:
print('execution failed: ' + str(error))
finally:
try:
cleaned = run_supervised([sys.executable, '-I', '-c', CLEANUP_EXEC, str(scratch)],
cwd='/tmp', timeout=5, role='cleanup')
forward_stderr(cleaned)
cleanup_ok = cleaned.returncode == 0
print('cleanup result: ' + str(cleaned.returncode), file=sys.stderr, flush=True)
except Exception as error:
print('cleanup exception: ' + str(error), file=sys.stderr, flush=True)
if not cleanup_ok:
print('scratch cleanup failed')
return 1
if not passed:
return 1
print('evidence rows: ' + str(len(service_answers) + len(queue_answers)))
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())