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

372 lines
33 KiB
Python

"""Independent scoped-ledger oracle; candidate identity execution and evidence."""
import base64
import hashlib
import json
import math
import os
from pathlib import Path
import random
import re
import subprocess
import sys
import tempfile
import uuid
import zlib
CONTEXT = {'include/ledger.hpp': "#ifndef SCOPED_LEDGER_HPP\n#define SCOPED_LEDGER_HPP\n#include <array>\n#include <cstdint>\n#include <iosfwd>\n#include <string>\n#include <vector>\n\nnamespace scoped {\nconstexpr int group_limit = 32;\nconstexpr int owner_limit = 8;\nconstexpr int weight_limit = 1000;\n\nstruct GateView {\n int owner = 0;\n std::uint64_t depth = 0;\n std::uint64_t acquisitions = 0;\n std::uint64_t releases = 0;\n std::uint64_t denied = 0;\n std::uint64_t invalid_releases = 0;\n std::uint64_t maximum_depth = 0;\n};\n\nclass Gate {\npublic:\n bool acquire(int owner);\n bool release(int owner);\n void reset();\n GateView view() const;\nprivate:\n GateView state_;\n};\n\nclass Scope {\npublic:\n Scope(Gate& gate, int owner);\n ~Scope();\n Scope(const Scope&) = delete;\n Scope& operator=(const Scope&) = delete;\n bool acquired() const;\nprivate:\n Gate& gate_;\n int owner_;\n bool acquired_;\n};\n\nstruct Result {\n int code = 0;\n std::int64_t total = 0;\n int visited = 0;\n int probes = 0;\n int stolen = 0;\n};\n\nstruct Group {\n int id = -1;\n int parent = -1;\n int weight = 0;\n};\n\nstruct Slot {\n bool live = false;\n int parent = -1;\n int weight = 0;\n};\n\nclass Ledger {\npublic:\n Ledger();\n void reset();\n Result add(int id, int parent, int weight);\n Result move(int id, int parent);\n Result set_weight(int id, int weight);\n Result erase(int id);\n Result report(int owner, int id);\n Result adjust(int owner, int id, int delta, int other);\n Gate& gate();\n const Gate& gate() const;\n std::vector<Group> snapshot() const;\nprivate:\n bool live(int id) const;\n bool parent_ok(int parent) const;\n bool descendant(int id, int ancestor) const;\n std::vector<int> children(int id) const;\n std::vector<int> subtree(int id) const;\n Result report_node(int owner, int id, int level);\n Result adjust_node(int owner, int id, int level, int delta, int other);\n std::array<Slot, group_limit> groups_;\n Gate gate_;\n};\n\nstruct Command {\n char kind = '?';\n std::vector<int> values;\n bool valid = false;\n};\n\nCommand parse_command(const std::string& line);\nResult dispatch(Ledger& ledger, const Command& command);\nvoid observe(std::ostream& output, const Result& result, const Ledger& ledger);\nint run_wire(std::istream& input, std::ostream& output);\nint run_demo(std::ostream& output);\n}\n#endif\n", 'support/main.cpp': '#include "ledger.hpp"\n#include <iostream>\n#include <string>\n\nint main(int argc, char** argv) {\n if (argc == 1) {\n return scoped::run_demo(std::cout);\n }\n if (argc == 2 && std::string(argv[1]) == "--wire") {\n return scoped::run_wire(std::cin, std::cout);\n }\n std::cerr << "usage: scope-demo [--wire]\\n";\n return 2;\n}\n', 'support/observer.cpp': '#include "ledger.hpp"\n#include <ostream>\n\nnamespace scoped {\nvoid observe(std::ostream& output, const Result& result, const Ledger& ledger) {\n const auto state = ledger.gate().view();\n const auto groups = ledger.snapshot();\n output << result.code << \' \'\n << result.total << \' \'\n << result.visited << \' \'\n << result.probes << \' \'\n << result.stolen << \' \'\n << state.owner << \' \'\n << state.depth << \' \'\n << state.acquisitions << \' \'\n << state.releases << \' \'\n << state.denied << \' \'\n << state.invalid_releases << \' \'\n << state.maximum_depth << \' \'\n << groups.size();\n for (const auto& group : groups) {\n output << \' \' << group.id\n << \' \' << group.parent\n << \' \' << group.weight;\n }\n output << \'\\n\';\n}\n\nint run_demo(std::ostream& output) {\n Ledger ledger;\n const auto root = ledger.add(0, -1, 7);\n if (root.code != 0) {\n return 1;\n }\n const auto middle = ledger.add(1, 0, 4);\n if (middle.code != 0) {\n return 1;\n }\n const auto leaf = ledger.add(2, 1, -2);\n if (leaf.code != 0) {\n return 1;\n }\n const auto report = ledger.report(3, 0);\n output << "report code=" << report.code\n << " total=" << report.total\n << " visited=" << report.visited << \'\\n\';\n const auto state = ledger.gate().view();\n output << "gate owner=" << state.owner\n << " depth=" << state.depth\n << " acquired=" << state.acquisitions\n << " returned=" << state.releases << \'\\n\';\n return 0;\n}\n}\n', 'support/wire.cpp': '#include "ledger.hpp"\n#include <climits>\n#include <istream>\n#include <ostream>\n\nnamespace scoped {\nnamespace {\nbool horizontal(char value) {\n return value == \' \' || value == \'\\t\' || value == \'\\r\';\n}\n\nbool decimal(const std::string& token, int& output) {\n if (token.empty()) {\n return false;\n }\n std::size_t cursor = 0;\n bool negative = false;\n if (token[cursor] == \'+\' || token[cursor] == \'-\') {\n negative = token[cursor] == \'-\';\n ++cursor;\n }\n if (cursor == token.size()) {\n return false;\n }\n const unsigned long long maximum = negative\n ? static_cast<unsigned long long>(INT_MAX) + 1\n : static_cast<unsigned long long>(INT_MAX);\n unsigned long long value = 0;\n for (; cursor < token.size(); ++cursor) {\n const char character = token[cursor];\n if (character < \'0\' || character > \'9\') {\n return false;\n }\n const unsigned digit = static_cast<unsigned>(character - \'0\');\n if (value > (maximum - digit) / 10) {\n return false;\n }\n value = value * 10 + digit;\n }\n if (negative && value == static_cast<unsigned long long>(INT_MAX) + 1) {\n output = INT_MIN;\n } else {\n output = negative ? -static_cast<int>(value) : static_cast<int>(value);\n }\n return true;\n}\n\nint arity(char kind) {\n switch (kind) {\n case \'R\':\n case \'Q\':\n return 0;\n case \'D\':\n case \'L\':\n case \'X\':\n return 1;\n case \'M\':\n case \'W\':\n case \'F\':\n return 2;\n case \'A\':\n return 3;\n case \'U\':\n return 4;\n default:\n return -1;\n }\n}\n}\n\nCommand parse_command(const std::string& line) {\n Command command;\n std::vector<std::string> tokens;\n std::size_t cursor = 0;\n while (cursor < line.size()) {\n while (cursor < line.size() && horizontal(line[cursor])) {\n ++cursor;\n }\n if (cursor == line.size()) {\n break;\n }\n const auto begin = cursor;\n while (cursor < line.size() && !horizontal(line[cursor])) {\n ++cursor;\n }\n tokens.push_back(line.substr(begin, cursor - begin));\n }\n if (tokens.empty() || tokens.front().size() != 1) {\n return command;\n }\n command.kind = tokens.front()[0];\n const int expected = arity(command.kind);\n if (expected < 0 || tokens.size() != static_cast<std::size_t>(expected + 1)) {\n return command;\n }\n for (std::size_t index = 1; index < tokens.size(); ++index) {\n int value = 0;\n if (!decimal(tokens[index], value)) {\n return command;\n }\n command.values.push_back(value);\n }\n command.valid = true;\n return command;\n}\n\nResult dispatch(Ledger& ledger, const Command& command) {\n if (!command.valid) {\n Result result;\n result.code = -1;\n return result;\n }\n const auto& values = command.values;\n switch (command.kind) {\n case \'R\':\n ledger.reset();\n return Result{};\n case \'Q\':\n return Result{};\n case \'A\':\n return ledger.add(values[0], values[1], values[2]);\n case \'M\':\n return ledger.move(values[0], values[1]);\n case \'W\':\n return ledger.set_weight(values[0], values[1]);\n case \'D\':\n return ledger.erase(values[0]);\n case \'F\':\n return ledger.report(values[0], values[1]);\n case \'U\':\n return ledger.adjust(values[0], values[1], values[2], values[3]);\n case \'L\':\n case \'X\': {\n const bool accepted = command.kind == \'L\'\n ? ledger.gate().acquire(values[0])\n : ledger.gate().release(values[0]);\n Result result;\n result.code = accepted ? 0 : -2;\n return result;\n }\n default: {\n Result result;\n result.code = -1;\n return result;\n }\n }\n}\n\nint run_wire(std::istream& input, std::ostream& output) {\n Ledger ledger;\n std::string line;\n while (std::getline(input, line)) {\n const auto result = dispatch(ledger, parse_command(line));\n observe(output, result, ledger);\n }\n return 0;\n}\n}\n'}
SUPERVISOR = "import base64, ctypes, hashlib, json, os, selectors, signal, subprocess, sys, time\ncfg = json.loads(sys.argv[1])\nbegan = time.monotonic()\nerrors = []\ntracked = set()\nsignals_sent = []\nadopted_reaped = []\nleader = None\nleader_start = None\nleader_rc = None\nexpired = False\nout = bytearray()\nerr = bytearray()\nselector = selectors.DefaultSelector()\n\ndef problem(where, error):\n errors.append([where, type(error).__name__, str(error)])\n\ndef stat(pid):\n try:\n with open('/proc/%d/stat' % pid) as stream:\n raw = stream.read()\n except (FileNotFoundError, ProcessLookupError):\n return None\n tail = raw[raw.rfind(')') + 2:].split()\n return (int(tail[19]), int(tail[1]), int(tail[2]), tail[0])\n\ndef table():\n result = {}\n for name in os.listdir('/proc'):\n if name.isascii() and name.isdigit():\n pid = int(name)\n value = stat(pid)\n if value is not None:\n result[pid] = value\n return result\n\ndef closure(rows, own, known):\n roots = {own}\n for pid, start in known:\n item = rows.get(pid)\n if item is not None and item[0] == start and item[3] not in ('Z', 'X'):\n roots.add(pid)\n found = set(roots)\n changed = True\n while changed:\n changed = False\n for pid, item in rows.items():\n if pid not in found and item[1] in found:\n found.add(pid)\n changed = True\n return {pid for pid in found if pid != own and pid in rows and rows[pid][3] not in ('Z', 'X')}\n\ndef discover():\n rows = table()\n current = closure(rows, os.getpid(), tracked)\n for pid in current:\n tracked.add((pid, rows[pid][0]))\n return rows, current\n\ndef fresh_signal(pid, start):\n value = stat(pid)\n if value is not None and value[0] == start and value[3] not in ('Z', 'X'):\n try:\n os.kill(pid, signal.SIGKILL)\n signals_sent.append([pid, start])\n except ProcessLookupError:\n pass\n\ndef reap():\n global leader_rc\n if leader is not None:\n leader_rc = leader.poll()\n if leader is None or leader_rc is not None:\n while True:\n try:\n pid, status = os.waitpid(-1, os.WNOHANG)\n except ChildProcessError:\n break\n if pid == 0:\n break\n adopted_reaped.append([pid, status])\n\ndef drain_io(delay):\n for key, mask in selector.select(delay):\n try:\n block = os.read(key.fd, 65536)\n except BlockingIOError:\n continue\n if block:\n (out if key.data == 'out' else err).extend(block)\n else:\n selector.unregister(key.fileobj)\n key.fileobj.close()\n\ndef finish():\n until = time.monotonic() + 4.0\n while True:\n reap()\n rows, current = discover()\n for pid in current:\n fresh_signal(pid, rows[pid][0])\n drain_io(0.005)\n reap()\n rows, current = discover()\n if leader_rc is not None and not current and not selector.get_map():\n return\n if leader is None and not current and not selector.get_map():\n return\n if time.monotonic() >= until:\n errors.append(['drain', 'Deadline', 'descendant or pipe remained'])\n return\n\ntry:\n if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0:\n raise OSError(ctypes.get_errno(), 'subreaper')\n leader = subprocess.Popen(cfg['argv'], cwd=cfg['cwd'], stdin=subprocess.PIPE,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE,\n start_new_session=True)\n item = stat(leader.pid)\n if item is None:\n raise RuntimeError('leader start token unavailable')\n leader_start = item[0]\n tracked.add((leader.pid, leader_start))\n # Small prepared input fits a pipe on Linux; use a nonblocking writer for all sizes.\n payload = memoryview(base64.b64decode(cfg['input']))\n os.set_blocking(leader.stdin.fileno(), False)\n for stream, name in ((leader.stdout, 'out'), (leader.stderr, 'err')):\n os.set_blocking(stream.fileno(), False)\n selector.register(stream, selectors.EVENT_READ, name)\n cursor = 0\n while True:\n if leader.stdin is not None:\n if cursor == len(payload):\n leader.stdin.close()\n leader.stdin = None\n else:\n try:\n cursor += os.write(leader.stdin.fileno(), payload[cursor:cursor + 65536])\n except BlockingIOError:\n pass\n except BrokenPipeError:\n leader.stdin.close()\n leader.stdin = None\n drain_io(0.005)\n reap()\n discover()\n if leader_rc is not None:\n break\n if time.monotonic() - began >= cfg['deadline']:\n expired = True\n break\nexcept BaseException as error:\n problem('execute', error)\nfinally:\n try:\n if leader is not None and leader.stdin is not None:\n leader.stdin.close()\n leader.stdin = None\n finish()\n except BaseException as error:\n problem('drain', error)\n # A discovery/IO failure cannot skip bounded fresh discovery and reaping.\n recovery = time.monotonic() + 4.0\n while time.monotonic() < recovery:\n try:\n reap()\n rows, current = discover()\n for pid in current:\n fresh_signal(pid, rows[pid][0])\n drain_io(0.005)\n reap()\n rows, current = discover()\n if (leader is None or leader_rc is not None) and not current and not selector.get_map():\n break\n except BaseException as again:\n problem('recovery', again)\n time.sleep(0.01)\ntry:\n reap()\n rows, current = discover()\n group_absent = leader is not None and not any(v[2] == leader.pid and v[3] not in ('Z', 'X') for v in rows.values())\n descendants_absent = not current\nexcept BaseException as error:\n problem('absence', error)\n group_absent = descendants_absent = False\nmeta = dict(nonce=cfg['nonce'], argv=cfg['argv'], deadline=cfg['deadline'],\n supervisor_pid=os.getpid(), supervisor_start=stat(os.getpid())[0],\n leader_pid=leader.pid if leader is not None else None, leader_start=leader_start,\n uid=os.getuid(), gid=os.getgid(), groups=os.getgroups(),\n leader_rc=leader_rc, reaped=leader is not None and leader_rc is not None,\n timeout=expired, controlled_rc=124 if expired else leader_rc,\n elapsed=time.monotonic()-began, pg_absent=group_absent,\n descendants_absent=descendants_absent, pipes_closed=not selector.get_map(), exceptions=errors,\n stdout_length=len(out), stdout_sha256=hashlib.sha256(out).hexdigest(),\n stderr_length=len(err), stderr_sha256=hashlib.sha256(err).hexdigest(),\n discovered=[list(token) for token in sorted(tracked)], signals=signals_sent, adopted_reaped=adopted_reaped)\nwire = dict(meta=meta, stdout=base64.b64encode(out).decode('ascii'), stderr=base64.b64encode(err).decode('ascii'))\nsys.stdout.write(json.dumps(wire, separators=(',', ':')))\n"
COMPILER_DEADLINE = 35
CANDIDATE_DEADLINE = 15
CLEANUP_DEADLINE = 8
ABSENCE_DEADLINE = 8
REAP_ALLOWANCE = 10
CANDIDATES = ['scope_lock.cpp', 'ledger.cpp']
def sha(raw):
return hashlib.sha256(raw).hexdigest()
def evidence(value):
raw = json.dumps(value, sort_keys=True, separators=(',', ':')).encode()
print('E120 ' + base64.b85encode(zlib.compress(raw, 9)).decode(), file=sys.stderr)
def ascii_int(raw, maximum_digits=20):
if re.fullmatch(rb'[+-]?[0-9]+', raw) is None:
raise ValueError('nondecimal token')
negative = raw[:1] == b'-'
digits = raw[1:] if raw[:1] in (b'+', b'-') else raw
digits = digits.lstrip(b'0') or b'0'
if len(digits) > maximum_digits:
raise ValueError('out of observable numeric range')
return (-1 if negative else 1) * int(digits)
def commands():
rows = ['Q', 'A 0 -1 7', 'A 1 0 4', 'A 2 1 -2', 'F 3 0',
'U 3 0 2 6', 'L 3', 'L 3', 'F 3 0', 'U 3 1 -1 5',
'L 6', 'X 6', 'X 3', 'Q', 'X 3', 'Q', 'X 3',
'M 0 2', 'M 2 -1', 'M 1 2', 'F 2 2', 'D 2', 'Q',
'A 0 -1 4', 'D 31', 'F 0 0', 'U 3 0 0 3', 'R']
rows += ['A %d %d %d' % (i, i - 1, (-1 if i % 2 else 1) * (i + 1)) for i in range(32)]
rows += ['L 8', 'F 8 0', 'U 8 0 3 1', 'X 8', 'U 3 31 999 2',
'W 31 -1000', 'U 4 31 -1 2', 'F 4 31', 'M 31 0', 'F 7 0', 'R']
rows += ['', ' ', 'Q 0', 'R\v', 'A 0 -1 1_0', 'A 0 -1 0x1',
'A 0 -1 2147483648', 'F -2147483649 0', 'L +', 'L --1',
'A +000 -001 -' + '0' * 5000, 'L +' + '0' * 5000 + '1',
'F 0001 -' + '0' * 5000, 'X 1', 'Q\f', 'R',
'\t A\t+00\r-1 0001000\r ', 'U 1 0 1 2', 'U 1 0 -1000 2', 'Q']
rng = random.Random(12020261024)
for batch in range(8):
rows.append('R')
for i in range(20):
rows.append('A %d %d %d' % (i, -1 if i == 0 or rng.randrange(5) == 0 else rng.randrange(i), rng.randrange(-80, 81)))
for _ in range(80):
op = rng.choice('FFFFUUUUAWMDQLX')
if op == 'F':
args = [rng.randrange(0, 10), rng.randrange(-1, 34)]
elif op == 'U':
args = [rng.randrange(0, 10), rng.randrange(-1, 34), rng.choice([-1000, -5, 0, 7, 1000]), rng.randrange(0, 10)]
elif op == 'A':
args = [rng.randrange(-1, 34), rng.randrange(-1, 34), rng.choice([-1001, -1000, 0, 1000, 1001])]
elif op in 'WM':
args = [rng.randrange(-1, 34), rng.randrange(-1001, 1002) if op == 'W' else rng.randrange(-1, 34)]
elif op == 'D':
args = [rng.randrange(-1, 34)]
elif op in 'LX':
args = [rng.randrange(-1, 10)]
else:
args = []
rows.append(op + ''.join(' ' + str(x) for x in args))
return rows
class Model:
def __init__(self):
self.nodes = {}
self.g = [0] * 7
def acquire(self, owner):
if owner not in range(1, 9) or self.g[1] and self.g[0] != owner:
self.g[4] += 1
return False
self.g[0] = owner
self.g[1] += 1
self.g[2] += 1
self.g[6] = max(self.g[6], self.g[1])
return True
def release(self, owner):
if owner not in range(1, 9) or not self.g[1] or self.g[0] != owner:
self.g[5] += 1
return False
self.g[1] -= 1
self.g[3] += 1
if not self.g[1]:
self.g[0] = 0
return True
def path(self, node):
path = []
while node != -1:
path.append(node)
node = self.nodes[node][0]
return path
def operation(self, row):
result = [0] * 5
tokens = re.split(r'[ \t\r]+', row.strip(' \t\r'))
arity = dict(R=0, Q=0, A=3, M=2, W=2, D=1, F=2, U=4, L=1, X=1)
kind = tokens[0]
try:
if kind not in arity or len(tokens) != arity[kind] + 1:
raise ValueError('syntax')
args = [ascii_int(t.encode('ascii'), 10) for t in tokens[1:]]
if any(not -2147483648 <= x <= 2147483647 for x in args):
raise ValueError('int range')
except (ValueError, UnicodeError):
result[0] = -1
return self.view(result)
if kind == 'R':
self.__init__()
elif kind in ('L', 'X'):
result[0] = 0 if (self.acquire(args[0]) if kind == 'L' else self.release(args[0])) else -2
elif kind == 'A':
node, parent, weight = args
if not 0 <= node < 32 or node in self.nodes or parent != -1 and parent not in self.nodes or not -1000 <= weight <= 1000:
result[0] = -1
else:
self.nodes[node] = [parent, weight]
elif kind == 'M':
node, parent = args
if node not in self.nodes or parent != -1 and (parent not in self.nodes or node in self.path(parent)):
result[0] = -1
else:
self.nodes[node][0] = parent
elif kind == 'W':
node, weight = args
if node not in self.nodes or not -1000 <= weight <= 1000:
result[0] = -1
else:
self.nodes[node][1] = weight
elif kind == 'D':
if args[0] not in self.nodes:
result[0] = -1
else:
dead = [n for n in self.nodes if args[0] in self.path(n)]
for node in dead:
del self.nodes[node]
elif kind in ('F', 'U'):
owner, root = args[:2]
delta, other = args[2:] if kind == 'U' else (0, 0)
invalid = owner not in range(1, 9) or root not in self.nodes
if kind == 'U':
invalid |= other not in range(1, 9) or owner == other or not -1000 <= delta <= 1000
if not invalid:
invalid = any(not -1000 <= self.nodes[n][1] + delta <= 1000 for n in self.nodes if root in self.path(n))
if invalid:
result[0] = -1
else:
# Parent paths define an independent ordered enter/exit event schedule.
schedule = []
def walk(node, level):
schedule.append((node, level, True))
for child in sorted(n for n in self.nodes if self.nodes[n][0] == node):
walk(child, level + 1)
schedule.append((node, level, False))
walk(root, 0)
held = 0
for node, level, enter in schedule:
if enter:
if not self.acquire(owner):
result[0] = -2
break
held += 1
if kind == 'U':
self.nodes[node][1] += delta
result[1] += self.nodes[node][1] * (level + 1)
result[2] += 1
else:
if kind == 'U':
result[3] += 1
if self.acquire(other):
result[4] += 1
self.release(other)
self.release(owner)
held -= 1
for _ in range(held):
self.release(owner)
return self.view(result)
def view(self, result):
return result + self.g[:] + [len(self.nodes)] + [v for n in sorted(self.nodes) for v in [n, *self.nodes[n]]]
META_FIELDS = set('nonce argv deadline supervisor_pid supervisor_start leader_pid leader_start uid gid groups leader_rc reaped timeout controlled_rc elapsed pg_absent descendants_absent pipes_closed exceptions stdout_length stdout_sha256 stderr_length stderr_sha256 discovered signals adopted_reaped'.split())
def validate(wire, cfg, expected_identity):
if type(wire) is not dict or set(wire) != {'meta', 'stdout', 'stderr'}:
raise ValueError('wire shape')
m = wire['meta']
if type(m) is not dict or set(m) != META_FIELDS:
raise ValueError('metadata fields')
for key in ('supervisor_pid', 'supervisor_start', 'leader_pid', 'leader_start'):
if type(m[key]) is not int or m[key] <= 0:
raise ValueError('metadata positive int ' + key)
if m['supervisor_pid'] == m['leader_pid']:
raise ValueError('leader identity contradiction')
for key in ('uid', 'gid'):
if type(m[key]) is not int or m[key] != expected_identity[key]:
raise ValueError('identity ' + key)
if type(m['groups']) is not list or any(type(v) is not int for v in m['groups']) or m['groups'] != expected_identity['groups']:
raise ValueError('identity groups')
if type(m['nonce']) is not str or m['nonce'] != cfg['nonce'] or type(m['argv']) is not list or m['argv'] != cfg['argv']:
raise ValueError('execution binding')
if type(m['deadline']) is not int or m['deadline'] != cfg['deadline']:
raise ValueError('deadline binding')
if type(m['elapsed']) not in (int, float) or not math.isfinite(m['elapsed']) or not 0 <= m['elapsed'] <= cfg['deadline'] + REAP_ALLOWANCE:
raise ValueError('elapsed')
for key in ('timeout', 'reaped', 'pg_absent', 'descendants_absent', 'pipes_closed'):
if type(m[key]) is not bool:
raise ValueError('metadata bool ' + key)
for key in ('leader_rc', 'controlled_rc'):
if type(m[key]) is not int:
raise ValueError('metadata rc ' + key)
if m['controlled_rc'] != (124 if m['timeout'] else m['leader_rc']):
raise ValueError('controlled rc contradiction')
for name in ('discovered', 'signals'):
if type(m[name]) is not list or any(type(pair) is not list or len(pair) != 2 or any(type(v) is not int or v <= 0 for v in pair) for pair in m[name]):
raise ValueError('process token metadata')
if any(pair not in m['discovered'] for pair in m['signals']):
raise ValueError('signal token binding')
if type(m['adopted_reaped']) is not list or any(type(pair) is not list or len(pair) != 2 or type(pair[0]) is not int or pair[0] <= 0 or pair[0] == m['leader_pid'] or type(pair[1]) is not int or pair[1] < 0 for pair in m['adopted_reaped']):
raise ValueError('adopted wait metadata')
if type(m['exceptions']) is not list or m['exceptions']:
raise ValueError('execution exceptions')
if not all(m[k] for k in ('reaped', 'pg_absent', 'descendants_absent', 'pipes_closed')):
raise ValueError('execution not reaped/absent/closed')
output = []
for key in ('stdout', 'stderr'):
if type(m[key + '_length']) is not int or m[key + '_length'] < 0 or type(m[key + '_sha256']) is not str:
raise ValueError('IO metadata type')
if type(wire[key]) is not str:
raise ValueError('IO base64 type')
raw = base64.b64decode(wire[key], validate=True)
if len(raw) != m[key + '_length'] or sha(raw) != m[key + '_sha256']:
raise ValueError('IO binding')
output.append(raw)
return m, output[0], output[1]
def execute(stage, argv, cwd, payload, deadline):
cfg = dict(nonce=uuid.uuid4().hex, argv=argv, cwd=str(cwd), input=base64.b64encode(payload).decode(), deadline=deadline)
identity = dict(uid=65534, gid=65534, groups=[]) if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else dict(uid=os.getuid(), gid=os.getgid(), groups=os.getgroups())
wrapped = subprocess.run([sys.executable, '-I', '-c', SUPERVISOR, json.dumps(cfg, separators=(',', ':'))], capture_output=True, timeout=deadline + REAP_ALLOWANCE + 3)
observed = dict(stage=stage, nonce=cfg['nonce'], argv=argv, deadline=deadline,
stdin_length=len(payload), stdin_sha256=sha(payload), wrapper_rc=wrapped.returncode, wire_length=len(wrapped.stdout), wire_sha256=sha(wrapped.stdout),
wrapper_stderr_length=len(wrapped.stderr), wrapper_stderr_sha256=sha(wrapped.stderr))
# Record the real wrapper return and wire bytes digest before attempting to parse.
try:
if wrapped.returncode != 0 or wrapped.stderr:
raise ValueError('wrapper process failure')
wire = json.loads(wrapped.stdout)
observed['metadata_actual'] = wire.get('meta') if type(wire) is dict else None
m, out, err = validate(wire, cfg, identity)
observed['stdout_preview_b64'] = base64.b64encode(out[:80]).decode()
observed['stderr_tail_b64'] = base64.b64encode(err[-240:]).decode()
return m, out, err
finally:
evidence(observed)
def successful(meta):
return meta['leader_rc'] == 0 and meta['controlled_rc'] == 0 and meta['timeout'] is False and meta['exceptions'] == []
def compare(raw, expected):
if not raw.endswith(b'\n'):
return 'stdout requires final LF'
lines = raw[:-1].split(b'\n')
if len(lines) != len(expected):
return 'record count got=%d expected=%d' % (len(lines), len(expected))
for index, (line, wanted) in enumerate(zip(lines, expected), 1):
try:
fields = re.split(rb'[ \t\r]+', line.strip(b' \t\r'))
actual = [ascii_int(token) for token in fields]
except ValueError as error:
return 'record %d lexical %s' % (index, error)
if actual != wanted:
return 'record %d raw=%r expected=%r' % (index, line[:180], wanted[:18])
return None
REMOVE = "import shutil,sys;shutil.rmtree(sys.argv[1])"
ABSENT = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: raise SystemExit(0)\nraise SystemExit(1)"
def main():
scratch = None
failures = []
matched = False
try:
scratch = Path(tempfile.mkdtemp(prefix='recursive-scope-'))
work = scratch / 'work'
work.mkdir()
os.chmod(work, 0o777)
for name, content in CONTEXT.items():
target = work / name
target.parent.mkdir(parents=True, exist_ok=True)
if target.parent != work:
os.chmod(target.parent, 0o777)
target.write_text(content)
for name in CANDIDATES:
(work / name).write_bytes((Path(sys.argv[1]) / name).read_bytes())
rows = commands()
model = Model()
expected = [model.operation(row) for row in rows]
payload = ('\n'.join(rows) + '\n').encode('ascii')
argv = ['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-Wpedantic', '-I' + str(work / 'include'),
str(work / 'scope_lock.cpp'), str(work / 'ledger.cpp'), str(work / 'support/wire.cpp'),
str(work / 'support/observer.cpp'), str(work / 'support/main.cpp'), '-o', str(work / 'scope-demo')]
built, _, _ = execute('compiler', argv, work, b'', COMPILER_DEADLINE)
if not successful(built):
failures.append('compiler rc=%r timeout=%r' % (built['leader_rc'], built['timeout']))
else:
ran, stdout, _ = execute('candidate', [str(work / 'scope-demo'), '--wire'], work, payload, CANDIDATE_DEADLINE)
if not successful(ran):
failures.append('candidate controlled rc=%r leader=%r timeout=%r' % (ran['controlled_rc'], ran['leader_rc'], ran['timeout']))
else:
difference = compare(stdout, expected)
if difference is not None:
failures.append('observation difference: ' + difference)
else:
matched = True
except BaseException as error:
failures.append('checker exception %s: %s' % (type(error).__name__, error))
finally:
if scratch is not None:
try:
cleaned, _, _ = execute('cleanup', [sys.executable, '-I', '-c', REMOVE, str(scratch)], '/', b'', CLEANUP_DEADLINE)
if not successful(cleaned):
failures.append('cleanup rc/timeout failure')
except BaseException as error:
failures.append('cleanup exception %s: %s' % (type(error).__name__, error))
try:
absent, _, _ = execute('absence', [sys.executable, '-I', '-c', ABSENT, str(scratch)], '/', b'', ABSENCE_DEADLINE)
if not successful(absent):
failures.append('absence rc/timeout failure')
except BaseException as error:
failures.append('absence exception %s: %s' % (type(error).__name__, error))
if failures or not matched:
print('FAIL ' + '; '.join(failures or ['no successful observation']))
return 1
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())