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>
363 lines
14 KiB
Python
363 lines
14 KiB
Python
"""Independent integer state model; candidates only execute in subprocesses."""
|
|
from pathlib import Path
|
|
import json
|
|
import random
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
MAX = (1 << 63) - 1
|
|
U64 = (1 << 64) - 1
|
|
U32 = (1 << 32) - 1
|
|
FILES = ["include/timer_ledger.h", "src/timer_internal.h",
|
|
"src/timer_bank.c", "src/timer_schedule.c", "src/timer_dispatch.c",
|
|
"src/timer_cancel.c", "src/timer_audit.c"]
|
|
|
|
|
|
class Model:
|
|
def __init__(self, slots):
|
|
self.now = 0
|
|
self.ticket = 0
|
|
self.serial = 0
|
|
self.timers = [dict(phase=0, generation=0, deadline=0, period=0,
|
|
tag=0, ticket=0, cancel_state=0,
|
|
cancel_generation=0, cancel_serial=0,
|
|
counts=[0] * 6) for _ in range(slots)]
|
|
|
|
def apply(self, op, args):
|
|
result = []
|
|
rc = 0
|
|
n = len(self.timers)
|
|
if op in ("ARM", "BEGIN", "END", "CANCEL", "POLL", "VIEW"):
|
|
if args[0] >= n:
|
|
return self.output(op, -2, self.sentinel(op))
|
|
t = self.timers[args[0]]
|
|
if op == "TIME":
|
|
if args[0] > MAX:
|
|
rc = -5
|
|
elif args[0] < self.now:
|
|
rc = -1
|
|
else:
|
|
self.now = args[0]
|
|
elif op == "ARM":
|
|
_, deadline, period, tag = args
|
|
if deadline > MAX or period > MAX:
|
|
rc = -5
|
|
elif t["phase"] != 0:
|
|
rc = -3
|
|
else:
|
|
t.update(phase=1, generation=t["generation"] + 1,
|
|
deadline=deadline, period=period, tag=tag, ticket=0)
|
|
t["counts"][0] += 1
|
|
elif op in ("NEXT", "TAKE"):
|
|
choices = [(t["deadline"], i) for i, t in enumerate(self.timers)
|
|
if t["phase"] == 1 and
|
|
(op == "NEXT" or t["deadline"] <= self.now)]
|
|
if not choices:
|
|
rc, result = 1, self.sentinel(op)
|
|
else:
|
|
_, i = min(choices)
|
|
t = self.timers[i]
|
|
if op == "NEXT":
|
|
result = [i, t["generation"], t["deadline"],
|
|
t["period"], t["tag"]]
|
|
else:
|
|
self.ticket += 1
|
|
t.update(phase=2, ticket=self.ticket)
|
|
result = [i, t["generation"], self.ticket]
|
|
elif op in ("BEGIN", "END"):
|
|
valid = (args[2] != 0 and args[1] == t["generation"] and
|
|
args[2] == t["ticket"] and t["phase"] in (2, 3, 4))
|
|
if op == "END" and args[3] not in (0, 1):
|
|
rc = -1
|
|
elif not valid:
|
|
rc = -4
|
|
elif op == "BEGIN":
|
|
if t["phase"] != 2:
|
|
rc = -3
|
|
else:
|
|
t["phase"] = 3
|
|
t["counts"][1] += 1
|
|
elif t["phase"] == 2:
|
|
rc = -3
|
|
else:
|
|
repeat = t["phase"] == 3 and args[3] == 1 and t["period"] > 0
|
|
next_due = 0
|
|
if repeat:
|
|
count = (self.now - t["deadline"]) // t["period"] + 1
|
|
next_due = t["deadline"] + count * t["period"]
|
|
if repeat and next_due > MAX:
|
|
rc = -5
|
|
else:
|
|
if t["phase"] == 4:
|
|
t["cancel_state"] = 2
|
|
t.update(phase=1 if repeat else 0, ticket=0)
|
|
t["counts"][2] += 1
|
|
if repeat:
|
|
t["deadline"] = next_due
|
|
t["counts"][3] += 1
|
|
elif op == "CANCEL":
|
|
if t["cancel_state"] != 1:
|
|
self.serial += 1
|
|
t.update(cancel_generation=t["generation"],
|
|
cancel_serial=self.serial)
|
|
if t["phase"] == 3:
|
|
t.update(phase=4, cancel_state=1)
|
|
else:
|
|
if t["phase"] in (1, 2):
|
|
t["counts"][3 + t["phase"]] += 1
|
|
t.update(phase=0, ticket=0, cancel_state=2)
|
|
rc = 2 if t["cancel_state"] == 1 else 3
|
|
result = [args[0], t["cancel_generation"], t["cancel_serial"]]
|
|
elif op == "POLL":
|
|
if (args[2] == 0 or t["cancel_state"] == 0 or
|
|
args[1:] != [t["cancel_generation"], t["cancel_serial"]]):
|
|
rc = -4
|
|
else:
|
|
rc = 2 if t["cancel_state"] == 1 else 3
|
|
elif op == "VIEW":
|
|
result = [t[k] for k in ("phase", "cancel_state", "generation",
|
|
"deadline", "period", "tag", "ticket",
|
|
"cancel_generation", "cancel_serial")] + t["counts"][:]
|
|
elif op == "BANK":
|
|
result = [self.now, n] + [sum(t["phase"] == p for t in self.timers)
|
|
for p in range(5)]
|
|
else:
|
|
raise ValueError(op)
|
|
return self.output(op, rc, result)
|
|
|
|
@staticmethod
|
|
def sentinel(op):
|
|
if op == "VIEW":
|
|
return [U32] * 2 + [U64] * 13
|
|
if op in ("TAKE", "CANCEL"):
|
|
return [U32, U64, U64]
|
|
if op == "NEXT":
|
|
return [U32] + [U64] * 4
|
|
return []
|
|
|
|
@staticmethod
|
|
def output(op, rc, result):
|
|
return " ".join(map(str, [op, rc] + result))
|
|
|
|
|
|
class Session:
|
|
def __init__(self, name, slots):
|
|
self.name = name
|
|
self.model = Model(slots)
|
|
self.commands = [f"INIT {slots}"]
|
|
self.expected = ["INIT 0"]
|
|
|
|
def add(self, op, *args):
|
|
self.commands.append(" ".join(map(str, [op] + list(args))))
|
|
self.expected.append(self.model.apply(op, list(args)))
|
|
return self.expected[-1].split()
|
|
|
|
def inspect(self):
|
|
for i in range(len(self.model.timers)):
|
|
self.add("VIEW", i)
|
|
self.add("BANK")
|
|
|
|
def fire(self, i, mode=1, finish=None):
|
|
token = self.add("TAKE")
|
|
assert int(token[2]) == i
|
|
values = list(map(int, token[2:]))
|
|
self.add("BEGIN", *values)
|
|
if finish is not None:
|
|
self.add("TIME", finish)
|
|
self.add("END", *values, mode)
|
|
|
|
|
|
def scenarios():
|
|
cases = []
|
|
def new(name, slots=2):
|
|
c = Session(name, slots)
|
|
cases.append(c)
|
|
return c
|
|
c = new("C17-smoke")
|
|
for line in ("ARM 0 10 7 41", "TIME 10", "TAKE", "BEGIN 0 1 1",
|
|
"TIME 25", "END 0 1 1 1", "NEXT", "VIEW 0", "BANK"):
|
|
op, *a = line.split()
|
|
c.add(op, *map(int, a))
|
|
c = new("C21-running-cancel")
|
|
for line in ("ARM 0 2 5 99", "TIME 2", "TAKE", "BEGIN 0 1 1",
|
|
"CANCEL 0", "POLL 0 1 1", "TIME 2", "POLL 0 1 1",
|
|
"CANCEL 0", "ARM 0 9 3 77", "NEXT", "TAKE",
|
|
"END 0 1 1 2", "POLL 0 1 1", "END 0 1 2 1",
|
|
"TIME 20", "POLL 0 1 1", "BEGIN 0 1 1",
|
|
"END 0 1 1 1", "POLL 0 1 1", "ARM 0 20 0 77",
|
|
"BEGIN 0 1 1", "END 0 1 1 1", "POLL 0 1 1",
|
|
"TAKE", "BEGIN 0 2 2", "CANCEL 1", "POLL 0 1 1",
|
|
"CANCEL 0", "POLL 0 1 1", "END 0 2 2 0", "POLL 0 2 3"):
|
|
op, *a = line.split()
|
|
c.add(op, *map(int, a))
|
|
c.inspect()
|
|
for n in (1, 8):
|
|
c = new(f"C01-C13-initial-order-{n}", n)
|
|
c.inspect()
|
|
c.add("NEXT"); c.add("TAKE"); c.add("TIME", 0)
|
|
for i in reversed(range(n)):
|
|
c.add("ARM", i, 3 if i % 2 else 7, i, U64 - i)
|
|
c.add("NEXT"); c.add("NEXT"); c.add("TAKE")
|
|
c.add("TIME", 3)
|
|
for _ in range(n + 1):
|
|
c.add("TAKE"); c.add("NEXT"); c.inspect()
|
|
c.add("TIME", 7)
|
|
for _ in range(n + 1):
|
|
c.add("TAKE"); c.inspect()
|
|
for period, delay in ((1, 11), (2, 1), (3, 2), (7, 14), (11, 38),
|
|
(17, 16), (1000, 10000), (MAX // 4, MAX // 3)):
|
|
c = new(f"C17-phase-{period}-{delay}", 1)
|
|
c.add("ARM", 0, 5, period, U64)
|
|
c.add("TIME", 5)
|
|
c.fire(0, finish=5 + delay)
|
|
c.add("NEXT"); c.inspect()
|
|
c = new("C03-C18-range-and-cancel-priority", 1)
|
|
c.add("ARM", 0, MAX - 2, 3, U64)
|
|
c.add("TIME", MAX - 2)
|
|
c.add("TAKE"); c.add("BEGIN", 0, 1, 1)
|
|
c.add("END", 0, 1, 1, 1); c.inspect()
|
|
c.add("END", 0, 1, 1, 0); c.inspect()
|
|
c.add("ARM", 0, MAX - 2, 3, 0)
|
|
c.add("TAKE"); c.add("BEGIN", 0, 2, 2)
|
|
c.add("CANCEL", 0); c.add("END", 0, 2, 2, 1)
|
|
c.add("POLL", 0, 2, 1); c.inspect()
|
|
c.add("TIME", MAX); c.add("ARM", 0, MAX, 0, U64)
|
|
c.fire(0); c.inspect()
|
|
c.add("ARM", 0, 0, MAX, 7); c.add("TAKE")
|
|
c.add("BEGIN", 0, 4, 4); c.add("END", 0, 4, 4, 1)
|
|
c.add("CANCEL", 0); c.add("END", 0, 4, 4, 1); c.inspect()
|
|
c = new("C09-C28-errors-and-atomicity")
|
|
actions = [("TIME", MAX + 1), ("TIME", U64), ("TIME", 1), ("TIME", 0),
|
|
("VIEW", 2), ("CANCEL", U32), ("POLL", U32, U64, U64),
|
|
("ARM", U32, U64, U64, U64), ("ARM", 0, MAX + 1, 0, 0),
|
|
("ARM", 0, 0, MAX + 1, 0), ("ARM", 0, 0, 0, U64),
|
|
("ARM", 0, MAX + 1, 0, 0), ("ARM", 0, 0, 0, 0),
|
|
("BEGIN", 0, 0, 0), ("END", U32, 0, 0, U32),
|
|
("END", 0, 0, 0, U32), ("END", 0, 0, 0, 0), ("TAKE",),
|
|
("END", 0, 1, 1, 0), ("END", 0, 1, 1, 2),
|
|
("BEGIN", 0, 2, 1), ("BEGIN", 0, 1, 2), ("BEGIN", 1, 1, 1),
|
|
("BEGIN", 0, 1, 1), ("BEGIN", 0, 1, 1), ("END", 0, 1, 1, 1),
|
|
("END", 0, 1, 1, 0), ("BEGIN", 0, 1, 1)]
|
|
for op, *a in actions:
|
|
c.add(op, *a); c.inspect()
|
|
c = new("C19-C25-cancel-identity", 8)
|
|
for i in range(8):
|
|
c.add("POLL", i, 0, 0); c.add("CANCEL", i)
|
|
c.add("POLL", i, 0, i + 1)
|
|
c.add("ARM", i, 0, i, U64 - i)
|
|
for i in range(8):
|
|
c.add("TAKE")
|
|
if i % 2:
|
|
c.add("BEGIN", i, 1, i + 1)
|
|
c.add("CANCEL", i)
|
|
c.add("CANCEL", i)
|
|
c.add("BEGIN", i, 1, i + 1)
|
|
c.add("END", i, 1, i + 1, 1)
|
|
c.add("ARM", i, 100, i + 1, i)
|
|
c.add("BEGIN", i, 1, i + 1)
|
|
c.add("END", i, 1, i + 1, 0)
|
|
c.inspect()
|
|
rng = random.Random(0x18caca11)
|
|
for run in range(24):
|
|
c = new(f"random-{run:02d}", rng.choice((1, 2, 5, 8)))
|
|
fire_history = []
|
|
cookies = []
|
|
for step in range(150):
|
|
m = c.model
|
|
i = rng.randrange(len(m.timers))
|
|
t = m.timers[i]
|
|
choice = rng.randrange(10)
|
|
if choice == 0:
|
|
c.add("TIME", rng.choice((m.now, min(MAX, m.now + rng.randrange(20)),
|
|
MAX + 1, max(0, m.now - 1))))
|
|
elif choice == 1:
|
|
c.add("ARM", rng.choice((i, i, U32)),
|
|
rng.choice((0, m.now, min(MAX, m.now + 2), MAX + 1)),
|
|
rng.choice((0, 1, 3, 7, 23, MAX, MAX + 1)), rng.getrandbits(64))
|
|
elif choice == 2:
|
|
r = c.add("TAKE")
|
|
if r[1] == "0": fire_history.append(list(map(int, r[2:])))
|
|
elif choice in (3, 4):
|
|
live = [[j, v["generation"], v["ticket"]]
|
|
for j, v in enumerate(m.timers) if v["ticket"]]
|
|
pool = live * 3 + fire_history + [[i, 0, 0], [U32, U64, U64]]
|
|
token = rng.choice(pool)
|
|
if choice == 3:
|
|
c.add("BEGIN", *token)
|
|
else:
|
|
c.add("END", *token, rng.choice((0, 1, 1, 2, U32)))
|
|
elif choice == 5:
|
|
r = c.add("CANCEL", rng.choice((i, i, U32)))
|
|
if r[1] in ("2", "3"): cookies.append(list(map(int, r[2:])))
|
|
elif choice == 6:
|
|
c.add("POLL", *rng.choice(cookies + [[i, 0, 0], [i, U64, U64],
|
|
[U32, U64, U64]]))
|
|
elif choice == 7:
|
|
c.add("NEXT")
|
|
elif choice == 8:
|
|
c.add("VIEW", rng.choice((i, U32)))
|
|
else:
|
|
c.add("BANK")
|
|
if step % 3 == 0:
|
|
c.inspect()
|
|
c.inspect()
|
|
return cases
|
|
|
|
|
|
def run(candidate):
|
|
cases = scenarios()
|
|
root = Path(__file__).resolve().parent
|
|
source = {name: (candidate / name).read_bytes() for name in FILES}
|
|
observer = (root / "observer.c.txt").read_bytes()
|
|
scratch = Path(tempfile.mkdtemp(prefix="timer-ledger-", dir=Path.cwd()))
|
|
(scratch / "include").mkdir()
|
|
(scratch / "src").mkdir()
|
|
for name, data in source.items():
|
|
(scratch / name).write_bytes(data)
|
|
(scratch / "observer.c").write_bytes(observer)
|
|
prepared = [(case.name, ("\n".join(case.commands) + "\n").encode(),
|
|
case.expected) for case in cases]
|
|
argv = ["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Iinclude", "-Isrc",
|
|
"observer.c"] + [name for name in FILES if name.endswith(".c")] + ["-o", "observer"]
|
|
# shortcut: wrapper owns scratch teardown; check never reopens scratch after a child starts.
|
|
built = subprocess.run(argv, cwd=scratch, capture_output=True, timeout=20)
|
|
print("BUILD " + json.dumps({"argv": argv, "exitcode": built.returncode}), file=sys.stderr)
|
|
if built.returncode:
|
|
print("FAIL build did not link")
|
|
return 1
|
|
failures = []
|
|
commands = 0
|
|
facts = []
|
|
for name, data, expected in prepared:
|
|
completed = subprocess.run([str(scratch / "observer")], cwd=scratch,
|
|
input=data, capture_output=True, timeout=5)
|
|
commands += len(expected)
|
|
matched = False
|
|
if completed.returncode:
|
|
failures.append(f"{name}: process exit {completed.returncode}")
|
|
else:
|
|
got = completed.stdout.decode("ascii", "replace").splitlines()
|
|
matched = completed.stdout == ("\n".join(expected) + "\n").encode("ascii")
|
|
if not matched:
|
|
at = next((i for i, pair in enumerate(zip(got, expected))
|
|
if pair[0] != pair[1]), min(len(got), len(expected)))
|
|
want = expected[at] if at < len(expected) else "<end>"
|
|
observed = got[at] if at < len(got) else "<missing>"
|
|
failures.append(f"{name}: command {at}: got {observed!r}, expected {want!r}")
|
|
facts.append([name, completed.returncode, matched, len(expected)])
|
|
for message in failures[:3]:
|
|
print(message, file=sys.stderr)
|
|
print("RESULTS " + json.dumps({"build_exitcode": built.returncode, "argv": argv,
|
|
"cases": facts, "commands": commands,
|
|
"failures": len(failures)}), file=sys.stderr)
|
|
if failures:
|
|
print("FAIL " + failures[0])
|
|
return 1
|
|
print("PASS")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(run(Path(sys.argv[1])))
|