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>
370 lines
35 KiB
Python
370 lines
35 KiB
Python
CONTEXT = {'service_port.hpp': '#ifndef SERVICE_PORT_HPP\n#define SERVICE_PORT_HPP\n#include <cstdint>\n#include <vector>\nnamespace timerlab {\nusing Tick = std::uint64_t;\nusing Token = std::uint64_t;\nstruct Reply {\n int status = 0;\n std::int64_t value = 0;\n};\nstruct ClientView {\n Token token = 0;\n bool busy = false;\n Tick ready = 0;\n};\nstruct PortView {\n Tick clock = 0;\n unsigned capacity = 0;\n unsigned live = 0;\n unsigned busy = 0;\n std::uint64_t created = 0;\n std::uint64_t destroyed = 0;\n std::uint64_t submitted = 0;\n std::uint64_t replied = 0;\n std::uint64_t canceled = 0;\n std::uint64_t exhausted = 0;\n std::uint64_t errors = 0;\n};\nclass ServicePort {\npublic:\n explicit ServicePort(unsigned capacity);\n Token create();\n int destroy(Token client);\n int send(Token client, std::uint64_t sequence, std::int64_t payload,\n unsigned delay, bool failure, int gain, unsigned route);\n int receive(Token client, Reply &reply);\n int cancel(Token client);\n void advance(Tick delta);\n Tick now() const;\n PortView view() const;\n std::vector<ClientView> clients() const;\nprivate:\n struct Slot {\n Token generation = 0;\n bool live = false;\n bool busy = false;\n Tick ready = 0;\n int status = 0;\n std::int64_t result = 0;\n };\n Slot *lookup(Token client);\n std::vector<Slot> slots_;\n PortView totals_;\n};\n}\n#endif\n', 'service_port.cpp': '#include "service_port.hpp"\n#include <stdexcept>\nnamespace timerlab {\nServicePort::ServicePort(unsigned capacity) : slots_(capacity)\n{\n if (capacity < 1 || capacity > 16) {\n throw std::invalid_argument("capacity");\n }\n totals_.capacity = capacity;\n}\nServicePort::Slot *ServicePort::lookup(Token client)\n{\n if (client == 0) {\n ++totals_.errors;\n return nullptr;\n }\n const unsigned index = static_cast<unsigned>((client - 1) % 16);\n const Token generation = (client - 1) / 16;\n if (index >= slots_.size()) {\n ++totals_.errors;\n return nullptr;\n }\n Slot &slot = slots_[index];\n if (!slot.live || slot.generation != generation) {\n ++totals_.errors;\n return nullptr;\n }\n return &slot;\n}\nToken ServicePort::create()\n{\n for (unsigned index = 0; index < slots_.size(); ++index) {\n Slot &slot = slots_[index];\n if (slot.live) {\n continue;\n }\n ++slot.generation;\n slot.live = true;\n slot.busy = false;\n slot.ready = 0;\n slot.status = 0;\n slot.result = 0;\n ++totals_.created;\n return slot.generation * 16 + index + 1;\n }\n ++totals_.exhausted;\n return 0;\n}\nint ServicePort::destroy(Token client)\n{\n Slot *slot = lookup(client);\n if (slot == nullptr) {\n return -1;\n }\n if (slot->busy) {\n ++totals_.errors;\n return -2;\n }\n slot->live = false;\n slot->ready = 0;\n slot->result = 0;\n ++totals_.destroyed;\n return 0;\n}\nint ServicePort::send(Token client, std::uint64_t sequence,\n std::int64_t payload, unsigned delay, bool failure,\n int gain, unsigned route)\n{\n Slot *slot = lookup(client);\n if (slot == nullptr) {\n return -1;\n }\n if (slot->busy) {\n ++totals_.errors;\n return -2;\n }\n slot->busy = true;\n slot->ready = totals_.clock + delay;\n slot->status = failure ? 1 : 0;\n slot->result = failure ? 0 : payload * gain + route * 101 + sequence;\n ++totals_.submitted;\n return 0;\n}\nint ServicePort::receive(Token client, Reply &reply)\n{\n Slot *slot = lookup(client);\n if (slot == nullptr) {\n return -1;\n }\n if (!slot->busy) {\n ++totals_.errors;\n return -2;\n }\n if (totals_.clock < slot->ready) {\n return 1;\n }\n reply.status = slot->status;\n reply.value = slot->result;\n slot->busy = false;\n ++totals_.replied;\n return 0;\n}\nint ServicePort::cancel(Token client)\n{\n Slot *slot = lookup(client);\n if (slot == nullptr) {\n return -1;\n }\n if (!slot->busy) {\n ++totals_.errors;\n return -2;\n }\n slot->busy = false;\n ++totals_.canceled;\n return 0;\n}\nvoid ServicePort::advance(Tick delta)\n{\n totals_.clock += delta;\n}\nTick ServicePort::now() const\n{\n return totals_.clock;\n}\nPortView ServicePort::view() const\n{\n PortView result = totals_;\n for (const Slot &slot : slots_) {\n if (slot.live) {\n ++result.live;\n if (slot.busy) {\n ++result.busy;\n }\n }\n }\n return result;\n}\nstd::vector<ClientView> ServicePort::clients() const\n{\n std::vector<ClientView> result;\n for (unsigned index = 0; index < slots_.size(); ++index) {\n const Slot &slot = slots_[index];\n if (!slot.live) {\n continue;\n }\n ClientView client;\n client.token = slot.generation * 16 + index + 1;\n client.busy = slot.busy;\n client.ready = slot.ready;\n result.push_back(client);\n }\n return result;\n}\n\n}\n', 'timer_console.cpp': '#include "timer_adapter.hpp"\n#include <charconv>\n#include <iostream>\n#include <memory>\n#include <sstream>\n#include <string>\n#include <vector>\nnamespace {\nusing namespace timerlab;\nstd::vector<std::string> words(const std::string &line)\n{\n std::vector<std::string> result;\n std::string token;\n std::istringstream input(line);\n while (input >> token) {\n result.push_back(token);\n }\n return result;\n}\nbool decimal(const std::string &text, std::int64_t &result)\n{\n if (text.empty()) {\n return false;\n }\n const char *first = text.data();\n const char *last = first + text.size();\n const auto parsed = std::from_chars(first, last, result);\n return parsed.ec == std::errc{} && parsed.ptr == last;\n}\nbool arguments(const std::vector<std::string> &tokens,\n unsigned size, std::vector<std::int64_t> &values)\n{\n if (tokens.size() != size + 1) {\n return false;\n }\n values.clear();\n for (unsigned index = 1; index < tokens.size(); ++index) {\n std::int64_t value = 0;\n if (!decimal(tokens[index], value)) {\n return false;\n }\n values.push_back(value);\n }\n return true;\n}\nvoid adapter_view(const TimerAdapter &adapter)\n{\n const AdapterView v = adapter.view();\n std::cout << "S " << v.running << \' \' << v.pending\n << \' \' << v.next_sequence << \' \' << v.callbacks\n << \' \' << v.launched << \' \' << v.skipped\n << \' \' << v.rejected << \' \' << v.succeeded\n << \' \' << v.failed << \' \' << v.timed_out\n << \' \' << v.canceled << \'\\n\';\n}\nvoid port_view(const ServicePort &port)\n{\n const PortView v = port.view();\n std::cout << "P " << v.clock << \' \' << v.capacity\n << \' \' << v.live << \' \' << v.busy\n << \' \' << v.created << \' \' << v.destroyed\n << \' \' << v.submitted << \' \' << v.replied\n << \' \' << v.canceled << \' \' << v.exhausted\n << \' \' << v.errors << \'\\n\';\n}\nvoid client_view(const ServicePort &port)\n{\n const auto clients = port.clients();\n std::cout << "L " << clients.size() << \'\\n\';\n for (const ClientView &client : clients) {\n std::cout << client.token << \' \' << client.busy << \' \'\n << client.ready << \'\\n\';\n }\n}\nvoid result_view(const TimerAdapter &adapter)\n{\n const auto &results = adapter.results();\n std::cout << "O " << results.size() << \'\\n\';\n for (const Result &r : results) {\n std::cout << r.sequence << \' \' << r.payload << \' \'\n << r.status << \' \' << r.value << \'\\n\';\n }\n}\n}\nint main()\n{\n std::unique_ptr<ServicePort> port;\n std::unique_ptr<TimerAdapter> adapter;\n std::string line;\n while (std::getline(std::cin, line)) {\n const auto tokens = words(line);\n if (tokens.empty()) {\n continue;\n }\n std::vector<std::int64_t> a;\n const std::string &op = tokens[0];\n if (op == "N" && arguments(tokens, 1, a) && a[0] >= 1 && a[0] <= 16) {\n adapter.reset();\n port = std::make_unique<ServicePort>(static_cast<unsigned>(a[0]));\n adapter = std::make_unique<TimerAdapter>(*port);\n std::cout << "N 0\\n";\n continue;\n }\n if (!adapter) {\n return 2;\n }\n if (op == "B" && arguments(tokens, 3, a) && a[0] >= -10000 && a[0] <= 10000 &&\n a[1] >= -10000 && a[1] <= 10000 && a[2] >= -10000 && a[2] <= 10000) {\n std::cout << "B " << adapter->start(static_cast<int>(a[0]),\n static_cast<int>(a[1]), static_cast<int>(a[2])) << \'\\n\';\n } else if (op == "T" && arguments(tokens, 3, a) && a[0] >= -2000000 &&\n a[0] <= 2000000 && a[1] >= -10000 && a[1] <= 10000 &&\n a[2] >= -10000 && a[2] <= 10000) {\n std::cout << "T " << adapter->callback(a[0], static_cast<int>(a[1]),\n static_cast<int>(a[2])) << \'\\n\';\n } else if (op == "A" && arguments(tokens, 1, a) && a[0] >= 0 && a[0] <= 1000000) {\n port->advance(static_cast<Tick>(a[0]));\n adapter->collect();\n std::cout << "A 0\\n";\n } else if (op == "X" && arguments(tokens, 0, a)) {\n adapter->stop();\n std::cout << "X 0\\n";\n } else if (op == "S" && arguments(tokens, 0, a)) {\n adapter_view(*adapter);\n } else if (op == "P" && arguments(tokens, 0, a)) {\n port_view(*port);\n } else if (op == "L" && arguments(tokens, 0, a)) {\n client_view(*port);\n } else if (op == "O" && arguments(tokens, 0, a)) {\n result_view(*adapter);\n } else {\n return 2;\n }\n }\n return std::cin.bad() ? 2 : 0;\n}\n'}
|
|
CANDIDATES = ['timer_adapter.hpp', 'timer_adapter.cpp']
|
|
SUPERVISOR = "import base64\nimport ctypes\nimport hashlib\nimport json\nimport os\nimport selectors\nimport signal\nimport subprocess\nimport sys\nimport time\n\n\ndef proc_stat(pid):\n try:\n with open('/proc/%d/stat' % pid, 'rb') as stream:\n raw = stream.read()\n except (FileNotFoundError, ProcessLookupError):\n return None\n fields = raw[raw.rfind(b')') + 2:].split()\n return {'p': pid, 's': int(fields[19]), 'parent': int(fields[1]),\n 'state': fields[0].decode('ascii'), 'pg': int(fields[2])}\n\n\ndef closure(table, root, tracked):\n roots = {root}\n for pid, start in tracked.items():\n row = table.get(pid)\n if row and row['s'] == start and row['state'] not in ('Z', 'X'):\n roots.add(pid)\n found = set()\n while True:\n extra = {pid for pid, row in table.items()\n if pid != root and (pid in roots or row['parent'] in roots)} - found\n if not extra:\n return found\n found.update(extra)\n roots.update(extra)\n\n\ndef main():\n spec = json.loads(sys.argv[1])\n started = time.monotonic()\n errors = []\n tracked = {}\n signals = []\n adopted = []\n observations = {}\n output = bytearray()\n stderr = bytearray()\n leader = None\n leader_start = None\n leader_rc = None\n timed_out = False\n absence = False\n remaining = None\n poller = selectors.DefaultSelector()\n channels = {}\n pending = memoryview(base64.b64decode(spec['input']))\n self_pid = os.getpid()\n self_start = proc_stat(self_pid)['s']\n def error(where, exc):\n errors.append([where, type(exc).__name__, str(exc)])\n def discover():\n table = {}\n for name in os.listdir('/proc'):\n if name.isascii() and name.isdigit():\n row = proc_stat(int(name))\n if row:\n table[row['p']] = row\n own = closure(table, self_pid, tracked)\n for pid in own:\n row = table[pid]\n tracked[pid] = row['s']\n observations[(pid, row['s'])] = row\n return table, own\n def reap():\n nonlocal leader_rc\n if leader is not None:\n leader_rc = leader.poll()\n # Popen owns its leader. Only reap adopted children after it has reaped it.\n if leader is None or leader_rc is not None:\n discover()\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.append([pid, os.waitstatus_to_exitcode(status)])\n def drain(wait):\n nonlocal pending\n for key, mask in poller.select(wait):\n fd = key.fd\n kind = channels[fd]\n try:\n if kind == 'in':\n if pending:\n wrote = os.write(fd, pending[:65536])\n pending = pending[wrote:]\n if not pending:\n poller.unregister(fd)\n del channels[fd]\n leader.stdin.close()\n else:\n data = os.read(fd, 65536)\n if not data:\n poller.unregister(fd)\n del channels[fd]\n key.fileobj.close()\n elif kind == 'out':\n output.extend(data)\n else:\n stderr.extend(data)\n except BrokenPipeError:\n poller.unregister(fd)\n del channels[fd]\n key.fileobj.close()\n except BlockingIOError:\n pass\n def send(pid, start):\n # A discovery snapshot never authorizes a later signal.\n row = proc_stat(pid)\n if row is None or row['s'] != start or row['state'] in ('Z', 'X'):\n return\n os.kill(pid, signal.SIGKILL)\n signals.append([pid, start, 9])\n try:\n libc = ctypes.CDLL(None, use_errno=True)\n if libc.prctl(36, 1, 0, 0, 0) != 0:\n raise OSError(ctypes.get_errno(), 'subreaper')\n leader = subprocess.Popen(spec['argv'], cwd=spec['cwd'],\n stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE, start_new_session=True)\n row = proc_stat(leader.pid)\n if row is None:\n raise RuntimeError('missing leader token')\n leader_start = row['s']\n tracked[leader.pid] = leader_start\n for stream, kind, event in ((leader.stdout, 'out', selectors.EVENT_READ),\n (leader.stderr, 'err', selectors.EVENT_READ),\n (leader.stdin, 'in', selectors.EVENT_WRITE)):\n os.set_blocking(stream.fileno(), False)\n channels[stream.fileno()] = kind\n poller.register(stream, event)\n while True:\n reap()\n discover()\n if leader_rc is not None and not any(v != 'in' for v in channels.values()):\n break\n if time.monotonic() - started >= spec['deadline']:\n timed_out = True\n break\n drain(0.01)\n except BaseException as exc:\n error('execute', exc)\n finally:\n until = time.monotonic() + 4\n # Each operation has its own handler. A transient failure must not stop recovery.\n while time.monotonic() < until:\n try:\n reap()\n except BaseException as exc:\n error('reap', exc)\n owned = set()\n try:\n table, owned = discover()\n except BaseException as exc:\n error('discover', exc)\n for pid in sorted(owned, reverse=True):\n try:\n send(pid, tracked[pid])\n except (ProcessLookupError, FileNotFoundError):\n pass\n except BaseException as exc:\n error('signal:%d:%d' % (pid, tracked[pid]), exc)\n try:\n drain(0.005)\n except BaseException as exc:\n error('drain', exc)\n try:\n reap()\n except BaseException as exc:\n error('reap', exc)\n try:\n _, final = discover()\n remaining = [[p, tracked[p]] for p in sorted(final)]\n if not final and (leader is None or leader_rc is not None):\n absence = True\n break\n except BaseException as exc:\n error('absence', exc)\n if not absence:\n errors.append(['recovery', 'RuntimeError', 'descendants or leader remain'])\n for key in list(poller.get_map().values()):\n try:\n poller.unregister(key.fileobj)\n key.fileobj.close()\n except BaseException as exc:\n error('close', exc)\n poller.close()\n controlled = 124 if timed_out else 125 if errors or not absence else leader_rc\n meta = {'stage': spec['stage'], 'nonce': spec['nonce'], 'argv': spec['argv'],\n 'cwd': spec['cwd'], 'deadline': spec['deadline'], 'input_n': len(base64.b64decode(spec['input'])),\n 'input_sha': hashlib.sha256(base64.b64decode(spec['input'])).hexdigest(),\n 'uid': os.getuid(), 'gid': os.getgid(), 'groups': os.getgroups(),\n 'supervisor': self_pid, 'supervisor_start': self_start,\n 'leader': leader.pid if leader else None, 'leader_start': leader_start,\n 'leader_rc': leader_rc, 'reaped': leader is not None and leader_rc is not None,\n 'timeout': timed_out, 'rc': controlled, 'absence': absence, 'remaining': remaining,\n 'elapsed': time.monotonic() - started, 'errors': errors,\n 'tracked': [[p, s] for p, s in sorted(tracked.items())],\n 'signals': signals, 'adopted': adopted,\n 'pg': [[r['p'], r['s'], r['pg']] for r in observations.values()],\n 'out_n': len(output), 'out_sha': hashlib.sha256(output).hexdigest(),\n 'err_n': len(stderr), 'err_sha': hashlib.sha256(stderr).hexdigest()}\n wire = {'meta': meta, 'out': base64.b64encode(output).decode('ascii'),\n 'err': base64.b64encode(stderr).decode('ascii')}\n sys.stdout.buffer.write(json.dumps(wire, separators=(',', ':')).encode('ascii'))\n\nif __name__ == '__main__':\n main()\n"
|
|
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
|
|
|
|
COMPILE_DEADLINE = 20
|
|
CANDIDATE_DEADLINE = 8
|
|
CLEANUP_DEADLINE = 5
|
|
ABSENCE_DEADLINE = 5
|
|
|
|
|
|
def scenarios():
|
|
events = []
|
|
def add(*event):
|
|
events.append(list(event))
|
|
events.extend([['S'], ['P'], ['L']])
|
|
add('N', 1)
|
|
add('B', 10, 3, 2)
|
|
for i in range(100):
|
|
add('T', i - 51, 1, i % 7 == 0)
|
|
add('A', 1)
|
|
add('O')
|
|
add('X')
|
|
add('B', 7, -100, 15)
|
|
add('T', -1000000, 1000, 0)
|
|
add('X')
|
|
add('O')
|
|
add('B', 3, 0, 0)
|
|
add('T', 1000000, 3, 0)
|
|
add('A', 3)
|
|
add('T', -1, 4, 1)
|
|
add('A', 3)
|
|
add('O')
|
|
rng = random.Random(852391)
|
|
for capacity in (1, 2, 16):
|
|
add('N', capacity)
|
|
for session in range(5):
|
|
add('B', rng.choice([1, 2, 7, 1000]), rng.choice([-100, -3, 0, 1, 100]), rng.randrange(16))
|
|
for i in range(38):
|
|
k = rng.choice('TTTTAAAXB')
|
|
if k == 'T':
|
|
add('T', rng.randint(-1000000, 1000000), rng.choice([0, 1, 2, 7, 1000]), rng.randrange(2))
|
|
elif k == 'A':
|
|
add('A', rng.choice([0, 1, 2, 8, 1000]))
|
|
elif k == 'X':
|
|
add('X')
|
|
else:
|
|
add('B', rng.choice([0, 1, 7, 1001]), rng.choice([-101, -100, 100, 101]), rng.choice([-1, 0, 15, 16]))
|
|
if i % 13 == 0:
|
|
add('O')
|
|
add('X')
|
|
add('X')
|
|
add('O')
|
|
for event in [('B', 0, 1, 0), ('B', 1001, 1, 0), ('B', 1, -101, 0),
|
|
('B', 1, 101, 0), ('B', 1, 0, -1), ('B', 1, 0, 16),
|
|
('T', -1000001, 0, 0), ('T', 1000001, 0, 0),
|
|
('T', 0, -1, 0), ('T', 0, 1001, 0),
|
|
('T', 0, 0, -1), ('T', 0, 0, 2), ('T', 0, 0, 0)]:
|
|
add(*event)
|
|
return [[int(x) if type(x) is bool else x for x in e] for e in events]
|
|
|
|
|
|
class Oracle:
|
|
def __init__(self):
|
|
self.now = 0
|
|
self.capacity = 1
|
|
self.total = [0, 0, 0]
|
|
self.reset()
|
|
def reset(self):
|
|
self.running = False
|
|
self.pending = None
|
|
self.next = 1
|
|
self.stats = [0] * 8
|
|
self.journal = []
|
|
def finish(self, status):
|
|
seq, payload, ready, deadline, failure = self.pending
|
|
value = payload * self.gain + self.route * 101 + seq if status == 0 else 0
|
|
self.journal.append([seq, payload, status, value])
|
|
self.stats[4 + status] += 1
|
|
self.total[1 if status < 2 else 2] += 1
|
|
self.pending = None
|
|
def collect(self):
|
|
if self.pending:
|
|
seq, payload, ready, deadline, failure = self.pending
|
|
if self.now >= ready:
|
|
self.finish(1 if failure else 0)
|
|
elif self.now >= deadline:
|
|
self.finish(2)
|
|
def event(self, event):
|
|
op, *a = event
|
|
if op == 'N':
|
|
self.__init__()
|
|
self.capacity = a[0]
|
|
return [['N', 0]]
|
|
if op == 'B':
|
|
timeout, gain, route = a
|
|
if not (1 <= timeout <= 1000 and -100 <= gain <= 100 and 0 <= route <= 15):
|
|
rc = -2
|
|
elif self.running:
|
|
rc = -3
|
|
else:
|
|
self.reset()
|
|
self.running = True
|
|
self.timeout, self.gain, self.route = a
|
|
rc = 0
|
|
return [['B', rc]]
|
|
if op == 'T':
|
|
payload, delay, failure = a
|
|
if not (-1000000 <= payload <= 1000000 and 0 <= delay <= 1000 and failure in (0, 1)):
|
|
rc = -2
|
|
else:
|
|
self.stats[0] += 1
|
|
self.collect()
|
|
if not self.running:
|
|
rc = 2
|
|
elif self.pending:
|
|
self.stats[2] += 1
|
|
rc = 1
|
|
else:
|
|
self.pending = [self.next, payload, self.now + delay, self.now + self.timeout, failure]
|
|
self.next += 1
|
|
self.stats[1] += 1
|
|
self.total[0] += 1
|
|
rc = 0
|
|
return [['T', rc]]
|
|
if op == 'A':
|
|
self.now += a[0]
|
|
self.collect()
|
|
return [['A', 0]]
|
|
if op == 'X':
|
|
if self.pending:
|
|
self.finish(3)
|
|
self.running = False
|
|
return [['X', 0]]
|
|
if op == 'S':
|
|
return [['S', int(self.running), int(self.pending is not None), self.next, *self.stats]]
|
|
if op == 'O':
|
|
return [['O', len(self.journal)], *[row[:] for row in self.journal]]
|
|
return []
|
|
|
|
|
|
def number(raw):
|
|
if re.fullmatch(rb'[+-]?[0-9]+', raw) is None:
|
|
raise ValueError('nondecimal output token')
|
|
negative = raw.startswith(b'-')
|
|
digits = raw.lstrip(b'+-').lstrip(b'0') or b'0'
|
|
# All public trace observables are bounded by clock/command/payload limits.
|
|
if len(digits) > 14:
|
|
raise ValueError('observable outside public trace bounds')
|
|
result = int(digits)
|
|
return -result if negative else result
|
|
|
|
|
|
def rows(raw):
|
|
if not raw.endswith(b'\n'):
|
|
raise ValueError('missing final LF')
|
|
result = []
|
|
for line in raw[:-1].split(b'\n'):
|
|
if line.endswith(b'\r'):
|
|
line = line[:-1]
|
|
fields = re.split(rb'[ \t]+', line.strip(b' \t'))
|
|
if not fields or fields == [b'']:
|
|
raise ValueError('empty output row')
|
|
parsed = []
|
|
for token in fields:
|
|
parsed.append(token.decode('ascii') if token in (b'N', b'B', b'T', b'A', b'X', b'S', b'P', b'L', b'O') else number(token))
|
|
result.append(parsed)
|
|
return result
|
|
|
|
|
|
def compare(events, raw):
|
|
observed = rows(raw)
|
|
model = Oracle()
|
|
index = 0
|
|
actual_live = 0
|
|
for step, event in enumerate(events):
|
|
op = event[0]
|
|
if op == 'P':
|
|
if index >= len(observed):
|
|
raise ValueError('missing port observation')
|
|
row = observed[index]
|
|
index += 1
|
|
if len(row) != 12 or row[0] != 'P':
|
|
raise ValueError('port shape at %d' % step)
|
|
clock, cap, live, busy, made, destroyed, submitted, replied, canceled, exhausted, errors = row[1:]
|
|
if not (clock == model.now and cap == model.capacity and
|
|
0 <= live <= int(model.running) and busy == int(model.pending is not None) and
|
|
live >= busy and made >= destroyed >= 0 and made - destroyed == live and
|
|
[submitted, replied, canceled] == model.total and exhausted == errors == 0):
|
|
raise ValueError('port behavior at %d: %r' % (step, row))
|
|
actual_live = live
|
|
elif op == 'L':
|
|
if index >= len(observed) or observed[index] != ['L', actual_live]:
|
|
raise ValueError('live table count at %d' % step)
|
|
index += 1
|
|
for _ in range(actual_live):
|
|
if index >= len(observed):
|
|
raise ValueError('missing client row')
|
|
row = observed[index]
|
|
index += 1
|
|
if (len(row) != 3 or any(type(x) is not int for x in row) or row[0] <= 0 or
|
|
row[1] != int(model.pending is not None) or
|
|
(model.pending is not None and row[2] != model.pending[2])):
|
|
raise ValueError('live table behavior at %d' % step)
|
|
else:
|
|
for expected in model.event(event):
|
|
if index >= len(observed) or observed[index] != expected:
|
|
raise ValueError('observation %d at command %d: %r != %r' %
|
|
(index, step, observed[index:index + 1], expected))
|
|
index += 1
|
|
if index != len(observed):
|
|
raise ValueError('extra stdout rows')
|
|
return index
|
|
|
|
|
|
def require_meta(meta, spec, out, err):
|
|
required = {'stage','nonce','argv','cwd','deadline','input_n','input_sha','uid','gid','groups',
|
|
'supervisor','supervisor_start','leader','leader_start','leader_rc','reaped','timeout',
|
|
'rc','absence','remaining','elapsed','errors','tracked','signals','adopted','pg','out_n','out_sha','err_n','err_sha'}
|
|
if type(meta) is not dict or set(meta) != required:
|
|
raise ValueError('metadata fields')
|
|
for key in ('supervisor','supervisor_start','leader','leader_start'):
|
|
if type(meta[key]) is not int or meta[key] <= 0:
|
|
raise ValueError('metadata identity token')
|
|
if meta['supervisor'] == meta['leader']:
|
|
raise ValueError('same supervisor/leader')
|
|
for key in ('uid', 'gid'):
|
|
if type(meta[key]) is not int:
|
|
raise ValueError('identity integer type')
|
|
if type(meta['groups']) is not list or any(type(x) is not int for x in meta['groups']):
|
|
raise ValueError('groups type')
|
|
identity = (65534, 65534, []) if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534' else (os.getuid(), os.getgid(), os.getgroups())
|
|
if (meta['uid'], meta['gid'], meta['groups']) != identity:
|
|
raise ValueError('execution identity')
|
|
elapsed = meta['elapsed']
|
|
if type(elapsed) not in (int, float) or not math.isfinite(elapsed) or not 0 <= elapsed <= spec['deadline'] + 8:
|
|
raise ValueError('elapsed range/type')
|
|
for key in ('timeout','reaped','absence'):
|
|
if type(meta[key]) is not bool:
|
|
raise ValueError('metadata bool type')
|
|
if type(meta['leader_rc']) is not int or type(meta['rc']) is not int:
|
|
raise ValueError('returncode type')
|
|
if meta['rc'] != (124 if meta['timeout'] else 125 if meta['errors'] or not meta['absence'] else meta['leader_rc']):
|
|
raise ValueError('controlled returncode consistency')
|
|
for key in ('errors','tracked','signals','adopted','pg'):
|
|
if type(meta[key]) is not list:
|
|
raise ValueError('metadata list type')
|
|
tokens = meta['tracked']
|
|
if (any(type(row) is not list or len(row) != 2 or
|
|
any(type(x) is not int or x <= 0 for x in row) for row in tokens) or
|
|
len({tuple(row) for row in tokens}) != len(tokens) or
|
|
[meta['leader'], meta['leader_start']] not in tokens):
|
|
raise ValueError('tracked token structure')
|
|
for row in meta['signals']:
|
|
if (type(row) is not list or len(row) != 3 or any(type(x) is not int for x in row) or
|
|
row[2] != 9 or row[:2] not in tokens):
|
|
raise ValueError('signal binding')
|
|
for row in meta['adopted']:
|
|
if (type(row) is not list or len(row) != 2 or any(type(x) is not int for x in row) or
|
|
row[0] <= 0 or row[0] == meta['leader']):
|
|
raise ValueError('adopted binding')
|
|
for row in meta['pg']:
|
|
if (type(row) is not list or len(row) != 3 or
|
|
any(type(x) is not int or x <= 0 for x in row) or row[:2] not in tokens):
|
|
raise ValueError('process group binding')
|
|
if type(meta['remaining']) is not list or (meta['absence'] and meta['remaining']):
|
|
raise ValueError('remaining closure consistency')
|
|
for key, value in [('stage',spec['stage']),('nonce',spec['nonce']),('argv',spec['argv']),('cwd',spec['cwd']),('deadline',spec['deadline'])]:
|
|
if type(meta[key]) is not type(value) or meta[key] != value:
|
|
raise ValueError('binding mismatch '+key)
|
|
for kind, data in [('input',base64.b64decode(spec['input'])),('out',out),('err',err)]:
|
|
if type(meta[kind+'_n']) is not int or meta[kind+'_n'] < 0 or meta[kind+'_n'] != len(data):
|
|
raise ValueError('IO length type/value')
|
|
if meta[kind+'_sha'] != hashlib.sha256(data).hexdigest():
|
|
raise ValueError('IO digest')
|
|
if not meta['reaped'] or not meta['absence'] or meta['timeout'] or meta['errors'] or meta['leader_rc'] != 0 or meta['rc'] != 0:
|
|
raise ValueError('execution rejected')
|
|
|
|
|
|
def main():
|
|
history = []
|
|
failures = []
|
|
scratch = None
|
|
count = 0
|
|
def execute(stage, argv, cwd, data, deadline):
|
|
spec = dict(stage=stage, argv=argv, cwd=cwd, input=base64.b64encode(data).decode('ascii'),
|
|
deadline=deadline, nonce=uuid.uuid4().hex)
|
|
wrapper = subprocess.run([sys.executable, '-I', '-c', SUPERVISOR, json.dumps(spec)],
|
|
capture_output=True, timeout=deadline + 12)
|
|
record = {'stage':stage, 'wrapper_rc':wrapper.returncode,
|
|
'wire_n':len(wrapper.stdout), 'wire_sha':hashlib.sha256(wrapper.stdout).hexdigest(),
|
|
'wrapper_stderr_n':len(wrapper.stderr), 'wrapper_stderr_sha':hashlib.sha256(wrapper.stderr).hexdigest()}
|
|
# Preserve wrapper facts before any decode/parse failure.
|
|
history.append(record)
|
|
if wrapper.returncode != 0 or wrapper.stderr:
|
|
if len(wrapper.stdout) < 2000:
|
|
record['wire_raw_b64'] = base64.b64encode(wrapper.stdout).decode('ascii')
|
|
raise ValueError('supervisor wrapper failed')
|
|
try:
|
|
wire = json.loads(wrapper.stdout)
|
|
if type(wire) is not dict or set(wire) != {'meta','out','err'}:
|
|
raise ValueError('wire fields')
|
|
out = base64.b64decode(wire['out'], validate=True)
|
|
err = base64.b64decode(wire['err'], validate=True)
|
|
record['meta'] = wire['meta']
|
|
# Raw byte summaries always bind actual capture; short IO is kept verbatim.
|
|
for label, raw in [('out',out),('err',err)]:
|
|
record[label+'_n'] = len(raw)
|
|
record[label+'_sha'] = hashlib.sha256(raw).hexdigest()
|
|
if len(raw) <= 1000:
|
|
record[label+'_raw_b64'] = base64.b64encode(raw).decode('ascii')
|
|
require_meta(wire['meta'], spec, out, err)
|
|
return out
|
|
except BaseException:
|
|
if len(wrapper.stdout) < 2000:
|
|
record['wire_raw_b64'] = base64.b64encode(wrapper.stdout).decode('ascii')
|
|
raise
|
|
try:
|
|
scratch = tempfile.mkdtemp(prefix='timer-client-reuse-')
|
|
work = Path(scratch) / 'work'
|
|
work.mkdir()
|
|
for name, text in CONTEXT.items():
|
|
(work / name).write_text(text)
|
|
candidate = Path(sys.argv[1])
|
|
for name in CANDIDATES:
|
|
(work / name).write_bytes((candidate / name).read_bytes())
|
|
work.chmod(0o777)
|
|
if os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534':
|
|
os.chown(work, 65534, 65534)
|
|
events = scenarios()
|
|
data = ''.join(' '.join(map(str, e))+'\n' for e in events).encode('ascii')
|
|
execute('compiler', ['g++','-std=c++17','-O2','timer_console.cpp','service_port.cpp','timer_adapter.cpp','-o','program'], str(work), b'', COMPILE_DEADLINE)
|
|
raw = execute('candidate', [str(work/'program')], str(work), data, CANDIDATE_DEADLINE)
|
|
count = compare(events, raw)
|
|
except BaseException as exc:
|
|
failures.append(['body',type(exc).__name__,str(exc)])
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
cleanup = "import shutil,sys;shutil.rmtree(sys.argv[1])"
|
|
execute('cleanup', [sys.executable,'-I','-c',cleanup,scratch], '/', b'', CLEANUP_DEADLINE)
|
|
except BaseException as exc:
|
|
failures.append(['cleanup',type(exc).__name__,str(exc)])
|
|
try:
|
|
absence = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nsys.exit(1)"
|
|
execute('absence', [sys.executable,'-I','-c',absence,scratch], '/', b'', ABSENCE_DEADLINE)
|
|
except BaseException as exc:
|
|
failures.append(['absence',type(exc).__name__,str(exc)])
|
|
record = {'scratch':scratch, 'rows':count, 'errors':failures, 'stages':history}
|
|
# Only compact factual metadata crosses bos_verify's 4000-character private tail.
|
|
packed = base64.b85encode(zlib.compress(json.dumps(record,separators=(',',':')).encode(),9)).decode()
|
|
sys.stderr.write('TCR1 '+packed+'\n')
|
|
print('PASS' if not failures and count > 0 else 'FAIL '+json.dumps(failures,separators=(',',':'))[:250])
|
|
return 0 if not failures and count > 0 else 1
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|