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>
343 lines
34 KiB
Python
343 lines
34 KiB
Python
"""Independent binary64 frame model; raw public observer, candidate-UID supervisors."""
|
|
import base64
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import zlib
|
|
|
|
SUPERVISOR = "import base64, ctypes, hashlib, json, os, selectors, signal, subprocess, sys, time, zlib\n\ndef proc(pid):\n try:\n with open('/proc/%d/stat' % pid, 'rb') as f:\n fields = f.read().rsplit(b') ', 1)[1].split()\n return (int(fields[1]), int(fields[2]), int(fields[19]), fields[0].decode('ascii'))\n except (FileNotFoundError, ProcessLookupError):\n return None\n\ndef members(table, own, tracked):\n roots = {own}\n owned = set()\n for pid, start in tracked.items():\n row = table.get(pid)\n if row and row[2] == start and row[3] not in ('Z', 'X'):\n roots.add(pid)\n owned.add(pid)\n changed = True\n while changed:\n changed = False\n for pid, row in table.items():\n if pid != own and row[0] in roots and pid not in roots:\n roots.add(pid)\n owned.add(pid)\n changed = True\n return {pid: table[pid] for pid in owned}\n\ndef error(errors, stage, exc):\n errors.append([stage, type(exc).__name__, str(exc)])\n\ndef scan(own, tracked, errors):\n table = {}\n for name in os.listdir('/proc'):\n if name.isdecimal():\n pid = int(name)\n try:\n row = proc(pid)\n if row is not None:\n table[pid] = row\n except Exception as exc:\n error(errors, 'stat', exc)\n found = members(table, own, tracked)\n for pid, row in found.items():\n tracked[pid] = row[2]\n return found\n\ndef fresh_signal(pid, start, sent):\n row = proc(pid)\n if row is not None and row[2] == start and row[3] not in ('Z', 'X'):\n try:\n os.kill(pid, signal.SIGKILL)\n sent.append([pid, start, signal.SIGKILL])\n except (FileNotFoundError, ProcessLookupError):\n pass\n\ndef pack(data):\n return base64.b85encode(zlib.compress(data, 9)).decode('ascii')\n\ndef main():\n request = json.loads(sys.argv[1])\n deadline = request['deadline']\n started = time.monotonic()\n own = os.getpid()\n own_stat = proc(own)\n errors, sent, adopted = [], [], []\n tracked = {}\n leader = None\n leader_start = None\n leader_rc = None\n timed_out = False\n out, err = bytearray(), bytearray()\n selector = selectors.DefaultSelector()\n input_data = base64.b64decode(request['input'])\n written = 0\n remaining = {}\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(request['argv'], cwd=request['cwd'],\n stdin=subprocess.PIPE, stdout=subprocess.PIPE,\n stderr=subprocess.PIPE, start_new_session=True)\n row = proc(leader.pid)\n if row is None:\n raise RuntimeError('leader identity disappeared before observation')\n leader_start = row[2]\n tracked[leader.pid] = leader_start\n for stream, label in ((leader.stdout, 'out'), (leader.stderr, 'err')):\n os.set_blocking(stream.fileno(), False)\n selector.register(stream, selectors.EVENT_READ, label)\n if input_data:\n os.set_blocking(leader.stdin.fileno(), False)\n selector.register(leader.stdin, selectors.EVENT_WRITE, 'in')\n else:\n leader.stdin.close()\n while True:\n leader_rc = leader.poll()\n remaining = scan(own, tracked, errors)\n if leader_rc is not None and not selector.get_map():\n break\n if time.monotonic() - started >= deadline:\n timed_out = True\n break\n for key, event in selector.select(0.015):\n stream = key.fileobj\n if key.data == 'in':\n try:\n written += os.write(stream.fileno(), input_data[written:written + 65536])\n except BrokenPipeError:\n written = len(input_data)\n if written == len(input_data):\n selector.unregister(stream)\n stream.close()\n else:\n try:\n data = os.read(stream.fileno(), 65536)\n except BlockingIOError:\n continue\n if data:\n (out if key.data == 'out' else err).extend(data)\n else:\n selector.unregister(stream)\n stream.close()\n except Exception as exc:\n error(errors, 'execute', exc)\n finally:\n recover_until = time.monotonic() + 3.0\n while time.monotonic() < recover_until:\n try:\n if leader is not None:\n leader_rc = leader.poll()\n except Exception as exc:\n error(errors, 'poll', exc)\n try:\n remaining = scan(own, tracked, errors)\n except Exception as exc:\n error(errors, 'discover', exc)\n remaining = None\n if remaining is not None:\n for pid, row in list(remaining.items()):\n try:\n fresh_signal(pid, row[2], sent)\n except Exception as exc:\n error(errors, 'signal', exc)\n # Popen owns leader status; reap only other adopted children.\n for pid, row in list(remaining.items()):\n if leader is not None and pid == leader.pid:\n continue\n try:\n current = proc(pid)\n if current and current[2] == row[2]:\n reaped, status = os.waitpid(pid, os.WNOHANG)\n if reaped:\n adopted.append([reaped, row[2], status])\n except ChildProcessError:\n pass\n except Exception as exc:\n error(errors, 'reap', exc)\n try:\n if leader is not None:\n leader_rc = leader.poll()\n except Exception as exc:\n error(errors, 'poll-after-reap', exc)\n try:\n remaining = scan(own, tracked, errors)\n except Exception as exc:\n error(errors, 'rediscover', exc)\n remaining = None\n try:\n for key, event in selector.select(0):\n if key.data == 'in':\n selector.unregister(key.fileobj)\n key.fileobj.close()\n continue\n data = os.read(key.fileobj.fileno(), 65536)\n if data:\n (out if key.data == 'out' else err).extend(data)\n else:\n selector.unregister(key.fileobj)\n key.fileobj.close()\n except Exception as exc:\n error(errors, 'drain', exc)\n if remaining == {} and leader is not None and leader_rc is not None:\n # All writers have been reaped; drain until actual EOF.\n if not selector.get_map():\n break\n time.sleep(0.005)\n try:\n remaining = scan(own, tracked, errors)\n except Exception as exc:\n error(errors, 'final-discover', exc)\n remaining = None\n try:\n if leader is not None:\n leader_rc = leader.poll()\n except Exception as exc:\n error(errors, 'final-poll', exc)\n try:\n selector.close()\n except Exception as exc:\n error(errors, 'selector-close', exc)\n elapsed = time.monotonic() - started\n absent = remaining == {}\n controlled_rc = 125 if errors else 124 if timed_out else leader_rc\n result = dict(supervisor=own, supervisor_start=own_stat[2],\n leader=None if leader is None else leader.pid, leader_start=leader_start,\n uid=os.getuid(), gid=os.getgid(), groups=os.getgroups(),\n argv=request['argv'], deadline=deadline, elapsed=elapsed,\n leader_rc=leader_rc, reaped=leader_rc is not None,\n timeout=timed_out, controlled_rc=controlled_rc,\n descendants_absent=absent,\n owned_pg_absent=absent,\n exceptions=errors, tracked=sorted(tracked.items()),\n signals=sent, adopted=adopted,\n stdout_len=len(out), stderr_len=len(err),\n stdout_sha=hashlib.sha256(out).hexdigest(),\n stderr_sha=hashlib.sha256(err).hexdigest(),\n stdout=pack(out), stderr=pack(err))\n sys.stdout.write(json.dumps(result, separators=(',', ':')) + '\\n')\n return 0\n\nif __name__ == '__main__':\n raise SystemExit(main())\n"
|
|
CONTEXT = {'include/engine.hpp': '#ifndef QUANT_ENGINE_HPP\n#define QUANT_ENGINE_HPP\n#include <array>\n#include <cstddef>\n#include <cstdint>\n#include <vector>\n\nnamespace quant {\nconstexpr std::size_t channel_count = 8;\nconstexpr std::size_t frame_capacity = 32;\nenum class Policy { keep = 0, reject = 1, clamp = 2 };\nstruct Profile {\n bool present = false;\n std::int32_t zero = 0;\n double scale = 1;\n double bias = 0;\n double lower = -1;\n double upper = 1;\n std::uint64_t revision = 0;\n};\nstruct Sample {\n unsigned channel = 0;\n std::int32_t code = 0;\n};\nstruct Value {\n unsigned channel = 0;\n std::int32_t code = 0;\n double decoded = 0;\n double delivered = 0;\n bool outside = false;\n std::uint64_t revision = 0;\n};\nstruct Statistics {\n std::uint64_t count = 0;\n std::uint64_t outside = 0;\n double sum = 0;\n double minimum = 0;\n double maximum = 0;\n double weighted = 0;\n};\nstruct State {\n std::uint64_t configured = 0;\n std::uint64_t epoch = 0;\n std::uint64_t accepted = 0;\n std::uint64_t rejected = 0;\n std::uint64_t canceled = 0;\n std::uint64_t samples = 0;\n std::uint64_t outside = 0;\n bool active = false;\n std::uint64_t frame = 0;\n std::size_t pending = 0;\n std::uint64_t last_frame = 0;\n std::size_t last_size = 0;\n};\nclass Engine {\npublic:\n Engine();\n void reset();\n int configure(unsigned, std::int32_t, double, double, double, double);\n int erase(unsigned);\n int begin(std::uint64_t);\n int append(unsigned, std::int32_t);\n int commit(Policy);\n int cancel();\n State state() const;\n Profile profile(unsigned) const;\n Statistics statistics(unsigned) const;\n const std::vector<Value>& last() const;\nprivate:\n double convert(std::int32_t, const Profile&) const;\n void accumulate(Statistics&, const Value&);\n void close_pending();\n std::array<Profile, channel_count> profiles_;\n std::array<Statistics, channel_count> stats_;\n std::vector<Sample> pending_;\n std::vector<Value> last_;\n State state_;\n};\n}\n#endif\n', 'src/observer.cpp': '#include "engine.hpp"\n#include <cerrno>\n#include <cmath>\n#include <cstdlib>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <sstream>\n#include <string>\n#include <vector>\n\nnamespace {\nbool integer(const std::string& token, long long& value)\n{\n if (token.empty()) {\n return false;\n }\n std::size_t index = 0;\n bool negative = false;\n if (token[index] == \'+\' || token[index] == \'-\') {\n negative = token[index] == \'-\';\n ++index;\n }\n if (index == token.size()) {\n return false;\n }\n unsigned long long magnitude = 0;\n const auto limit = static_cast<unsigned long long>(std::numeric_limits<long long>::max());\n for (; index < token.size(); ++index) {\n const char digit = token[index];\n if (digit < \'0\' || digit > \'9\') {\n return false;\n }\n const auto part = static_cast<unsigned>(digit - \'0\');\n if (magnitude > (limit - part) / 10) {\n return false;\n }\n magnitude = magnitude * 10 + part;\n }\n value = negative ? -static_cast<long long>(magnitude) : static_cast<long long>(magnitude);\n return true;\n}\n\nbool decimal(const std::string& token, double& value)\n{\n std::size_t i = 0;\n if (i < token.size() && (token[i] == \'+\' || token[i] == \'-\')) {\n ++i;\n }\n bool digits = false;\n while (i < token.size() && token[i] >= \'0\' && token[i] <= \'9\') {\n digits = true;\n ++i;\n }\n if (i < token.size() && token[i] == \'.\') {\n ++i;\n while (i < token.size() && token[i] >= \'0\' && token[i] <= \'9\') {\n digits = true;\n ++i;\n }\n }\n if (!digits) {\n return false;\n }\n if (i < token.size() && (token[i] == \'e\' || token[i] == \'E\')) {\n ++i;\n if (i < token.size() && (token[i] == \'+\' || token[i] == \'-\')) {\n ++i;\n }\n const auto exponent = i;\n while (i < token.size() && token[i] >= \'0\' && token[i] <= \'9\') {\n ++i;\n }\n if (i == exponent) {\n return false;\n }\n }\n if (i != token.size()) {\n return false;\n }\n char* end = nullptr;\n errno = 0;\n value = std::strtod(token.c_str(), &end);\n return end == token.c_str() + token.size() && std::isfinite(value);\n}\n\nstd::vector<std::string> tokens(const std::string& line)\n{\n std::vector<std::string> result;\n std::istringstream stream(line);\n std::string token;\n while (stream >> token) {\n result.push_back(token);\n }\n return result;\n}\n\nbool bounded(const std::vector<std::string>& args, std::size_t index,\n long long lower, long long upper, long long& result)\n{\n if (index >= args.size()) {\n return false;\n }\n return integer(args[index], result) && result >= lower && result <= upper;\n}\n\nint dispatch(quant::Engine& engine, const std::vector<std::string>& args)\n{\n if (args.empty()) {\n return -9;\n }\n long long a = 0;\n long long b = 0;\n const auto& command = args.front();\n if (command == "R" && args.size() == 1) {\n engine.reset();\n return 0;\n }\n if (command == "P" && args.size() == 7) {\n double scale = 0;\n double bias = 0;\n double lower = 0;\n double upper = 0;\n if (!bounded(args, 1, 0, 7, a)) {\n return -1;\n }\n if (!bounded(args, 2, -2147483648LL, 2147483647LL, b)) {\n return -1;\n }\n if (!decimal(args[3], scale) || !decimal(args[4], bias)) {\n return -1;\n }\n if (!decimal(args[5], lower) || !decimal(args[6], upper)) {\n return -1;\n }\n return engine.configure(static_cast<unsigned>(a), static_cast<std::int32_t>(b),\n scale, bias, lower, upper);\n }\n if (command == "D" && args.size() == 2) {\n if (!bounded(args, 1, 0, 7, a)) {\n return -1;\n }\n return engine.erase(static_cast<unsigned>(a));\n }\n if (command == "B" && args.size() == 2) {\n if (!bounded(args, 1, 1, 1000000000, a)) {\n return -1;\n }\n return engine.begin(static_cast<std::uint64_t>(a));\n }\n if (command == "A" && args.size() == 3) {\n if (!bounded(args, 1, 0, 7, a)) {\n return -1;\n }\n if (!bounded(args, 2, -2147483648LL, 2147483647LL, b)) {\n return -1;\n }\n return engine.append(static_cast<unsigned>(a), static_cast<std::int32_t>(b));\n }\n if (command == "C" && args.size() == 2) {\n if (!bounded(args, 1, 0, 2, a)) {\n return -1;\n }\n return engine.commit(static_cast<quant::Policy>(a));\n }\n if (command == "X" && args.size() == 1) {\n return engine.cancel();\n }\n if (command == "S" && args.size() == 1) {\n return 0;\n }\n return -9;\n}\n\nvoid emit(std::size_t step, int result, const quant::Engine& engine)\n{\n const auto state = engine.state();\n std::cout << "S " << step << \' \' << result;\n std::cout << \' \' << state.configured << \' \' << state.epoch;\n std::cout << \' \' << state.accepted << \' \' << state.rejected;\n std::cout << \' \' << state.canceled << \' \' << state.samples;\n std::cout << \' \' << state.outside << \' \' << state.active;\n std::cout << \' \' << state.frame << \' \' << state.pending;\n std::cout << \' \' << state.last_frame << \' \' << state.last_size << \'\\n\';\n for (unsigned channel = 0; channel < quant::channel_count; ++channel) {\n const auto profile = engine.profile(channel);\n const auto stats = engine.statistics(channel);\n std::cout << "P " << step << \' \' << channel;\n std::cout << \' \' << profile.present << \' \' << profile.zero;\n std::cout << \' \' << profile.scale << \' \' << profile.bias;\n std::cout << \' \' << profile.lower << \' \' << profile.upper;\n std::cout << \' \' << profile.revision << \'\\n\';\n std::cout << "T " << step << \' \' << channel;\n std::cout << \' \' << stats.count << \' \' << stats.outside;\n std::cout << \' \' << stats.sum << \' \' << stats.minimum;\n std::cout << \' \' << stats.maximum << \' \' << stats.weighted << \'\\n\';\n }\n const auto& values = engine.last();\n for (std::size_t index = 0; index < values.size(); ++index) {\n const auto& value = values[index];\n std::cout << "V " << step << \' \' << index;\n std::cout << \' \' << value.channel << \' \' << value.code;\n std::cout << \' \' << value.decoded << \' \' << value.delivered;\n std::cout << \' \' << value.outside << \' \' << value.revision << \'\\n\';\n }\n}\n}\n\nint main()\n{\n std::cout << std::setprecision(17);\n quant::Engine engine;\n std::string line;\n std::size_t step = 0;\n while (std::getline(std::cin, line)) {\n ++step;\n const auto args = tokens(line);\n const int result = dispatch(engine, args);\n emit(step, result, engine);\n }\n return std::cin.bad() ? 2 : 0;\n}\n'}
|
|
COMPILE_DEADLINE = 20
|
|
RUN_DEADLINE = 8
|
|
CLEAN_DEADLINE = 4
|
|
ABSENCE_DEADLINE = 4
|
|
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
|
|
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: raise SystemExit(0)\nraise SystemExit(1)"
|
|
EVIDENCE = []
|
|
|
|
def unpack(text):
|
|
return zlib.decompress(base64.b85decode(text))
|
|
|
|
def ascii_integer(token):
|
|
if not re.fullmatch(rb'[+-]?[0-9]+', token):
|
|
raise ValueError('nondecimal integer token')
|
|
negative = token.startswith(b'-')
|
|
digits = token.lstrip(b'+-').lstrip(b'0') or b'0'
|
|
if len(digits) > 12:
|
|
raise ValueError('integer outside observable domain')
|
|
return -int(digits) if negative else int(digits)
|
|
|
|
def floating(token):
|
|
if not re.fullmatch(rb'[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?', token):
|
|
raise ValueError('nondecimal floating token')
|
|
value = float(token)
|
|
if not math.isfinite(value):
|
|
raise ValueError('nonfinite observation')
|
|
return value
|
|
|
|
def cases():
|
|
lines = ['P 0 -1000000000 .001 0 -10000000 10000000', 'B 1', 'A 0 2000000000', 'C 0', 'S', 'R']
|
|
# Full signed differences, float range, subnormal-in-float and precision cases.
|
|
profiles = [(-2147483648,1e90,0,-1e110,1e110), (2147483647,1e-80,0,-1,1),
|
|
(0,0.100000000003,0.000000000123,-2e10,2e10), (17,1.0,0,-3,3),
|
|
(-9,1e100,1e99,-1e110,1e110), (3,1e-100,-1e-99,-1,1),
|
|
(0,0.125,0.25,-1,1), (0,1,0,0,0)]
|
|
for ch, (zero,scale,bias,lower,upper) in enumerate(profiles):
|
|
lines.append('P %d %d %.17g %.17g %.17g %.17g' % (ch,zero,scale,bias,lower,upper))
|
|
for frame, policy in enumerate([0,2,1,0,2], 10):
|
|
lines.append('B %d' % frame)
|
|
for ch in range(8):
|
|
for q in [-2147483648,2147483647,0,17]:
|
|
lines.append('A %d %d' % (ch,q))
|
|
lines.extend(['A 0 1', 'C %d' % policy])
|
|
lines.extend(['B 50','C 0','B 51','X','X','C 0','A 0 1','B 52',
|
|
'P 0 0 1 0 -1 1','D 0','B 53','C 3','A 8 1','C 0',
|
|
'D 0','D 0','B 54','A 0 2','X','P 0 0 1 0 -1 1'])
|
|
rng = random.Random(12620261023)
|
|
for cycle in range(45):
|
|
if cycle % 9 == 0:
|
|
lines.append('R')
|
|
channel = rng.randrange(8)
|
|
zero = rng.choice([-2147483648,2147483647,-1000000000,0,rng.randrange(-100000,100000)])
|
|
scale = rng.choice([1e-100,1e-60,0.001,0.125,0.100000000003,3.14159265358979,1e40,1e100])
|
|
bias = rng.choice([0,0.00314159265358,-1e-90,1e80])
|
|
lower, upper = (-1e110,1e110) if cycle % 3 else (-0.5,0.5)
|
|
lines.append('P %d %d %.17g %.17g %.17g %.17g' % (channel,zero,scale,bias,lower,upper))
|
|
lines.append('B %d' % (100+cycle))
|
|
for _ in range(rng.randrange(1,20)):
|
|
lines.append('A %d %d' % (rng.randrange(8),rng.choice([-2147483648,2147483647,rng.randrange(-2147483648,2147483648)])))
|
|
lines.append('X' if cycle % 7 == 0 else 'C %d' % rng.randrange(3))
|
|
lines.append('S')
|
|
lines.extend(['R','P 0 0 1 0 -1 1','B +000001','A 0 -00000001','A 0 +00000001','C 1',
|
|
'P 7 +000 1e-100 0 -1 1', 'D 7','B 1000000001','B -1','A 0 2147483648',
|
|
'P 0 0 0 0 -1 1','P 0 0 1e101 0 -1 1','P 0 0 1 1e101 -1 1',
|
|
'P 0 0 1 0 2 1','P 0 0 nan 0 -1 1','P 0 0 1_0 0 -1 1',
|
|
'P 0 1_0 1 0 -1 1','P 8 0 1 0 -1 1','C -1','R 1','Q','','S'])
|
|
return lines
|
|
|
|
def expected(lines):
|
|
output = []
|
|
profiles = []
|
|
history = []
|
|
last = []
|
|
pending = []
|
|
state = {}
|
|
def reset():
|
|
nonlocal profiles, history, last, pending, state
|
|
profiles = [None]*8
|
|
history = [[] for _ in range(8)]
|
|
last, pending = [], []
|
|
state = dict(configured=0,epoch=0,accepted=0,rejected=0,canceled=0,
|
|
samples=0,outside=0,active=0,frame=0,last_frame=0)
|
|
reset()
|
|
integer_pattern = re.compile(r'[+-]?[0-9]+\Z', re.ASCII)
|
|
decimal_pattern = re.compile(r'[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\Z', re.ASCII)
|
|
def number(token, low, high):
|
|
if not integer_pattern.fullmatch(token):
|
|
raise ValueError()
|
|
digits = token.lstrip('+-').lstrip('0') or '0'
|
|
if len(digits)>12:
|
|
raise ValueError()
|
|
value = int(('-' if token.startswith('-') else '') + digits)
|
|
if not low<=value<=high:
|
|
raise ValueError()
|
|
return value
|
|
def decimal(token):
|
|
if not decimal_pattern.fullmatch(token):
|
|
raise ValueError()
|
|
value = float(token)
|
|
if not math.isfinite(value):
|
|
raise ValueError()
|
|
return value
|
|
def ordered_sum(values):
|
|
result=0.0
|
|
for value in values: result+=value
|
|
return result
|
|
for step,line in enumerate(lines,1):
|
|
args = re.findall(r'[^ \t\r\v\f]+', line)
|
|
rc = 0
|
|
name = args[0] if args else ''
|
|
sizes = dict(R=1,P=7,D=2,B=2,A=3,C=2,X=1,S=1)
|
|
if name not in sizes or len(args)!=sizes[name]:
|
|
rc=-9
|
|
else:
|
|
try:
|
|
if name=='R':
|
|
reset()
|
|
elif name=='P':
|
|
channel=number(args[1],0,7); zero=number(args[2],-2147483648,2147483647)
|
|
scale,bias,lower,upper=map(decimal,args[3:])
|
|
if not (1e-100<=scale<=1e100 and abs(bias)<=1e100 and abs(lower)<=1e110 and abs(upper)<=1e110 and lower<=upper):
|
|
raise ValueError()
|
|
if state['active']: rc=-2
|
|
else:
|
|
state['configured']+=profiles[channel] is None
|
|
state['epoch']+=1
|
|
profiles[channel]=(zero,scale,bias,lower,upper,state['epoch'])
|
|
elif name=='D':
|
|
channel=number(args[1],0,7)
|
|
if state['active']: rc=-2
|
|
elif profiles[channel] is not None:
|
|
profiles[channel]=None;state['configured']-=1;state['epoch']+=1
|
|
elif name=='B':
|
|
frame=number(args[1],1,1000000000)
|
|
if state['active']:rc=-2
|
|
else:state.update(active=1,frame=frame);pending=[]
|
|
elif name=='A':
|
|
channel=number(args[1],0,7);code=number(args[2],-2147483648,2147483647)
|
|
if not state['active']:rc=-2
|
|
elif profiles[channel] is None:rc=-3
|
|
elif len(pending)==32:rc=-4
|
|
else:pending.append((channel,code))
|
|
elif name=='C':
|
|
policy=number(args[1],0,2)
|
|
if not state['active']:rc=-2
|
|
else:
|
|
values=[]
|
|
for channel,code in pending:
|
|
zero,scale,bias,lower,upper,revision=profiles[channel]
|
|
decoded=scale*(code-zero)+bias
|
|
outside=int(decoded<lower or decoded>upper)
|
|
delivered=min(upper,max(lower,decoded)) if policy==2 else decoded
|
|
values.append((channel,code,decoded,delivered,outside,revision))
|
|
if policy==1 and any(v[4] for v in values):
|
|
rc=-5;state['rejected']+=1
|
|
else:
|
|
state['accepted']+=1;state['last_frame']=state['frame'];last=values
|
|
for v in values:
|
|
history[v[0]].append((v[3],v[4]))
|
|
state['samples']+=1;state['outside']+=v[4]
|
|
pending=[];state.update(active=0,frame=0)
|
|
elif name=='X':
|
|
if not state['active']:rc=-2
|
|
else:state['canceled']+=1;pending=[];state.update(active=0,frame=0)
|
|
except (ValueError,OverflowError):
|
|
rc=-1
|
|
output.append(['S',step,rc]+[int(state[x]) for x in ('configured','epoch','accepted','rejected','canceled','samples','outside','active','frame')]+[len(pending),state['last_frame'],len(last)])
|
|
for channel in range(8):
|
|
profile=profiles[channel]
|
|
output.append(['P',step,channel]+([1]+list(profile) if profile else [0,0,1.0,0.0,-1.0,1.0,0]))
|
|
items=history[channel];values=[v for v,o in items]
|
|
output.append(['T',step,channel,len(items),sum(o for v,o in items),
|
|
ordered_sum(values), min(values,default=0.0),max(values,default=0.0),
|
|
ordered_sum(v*(i+1) for i,v in enumerate(values))])
|
|
for index,value in enumerate(last):
|
|
output.append(['V',step,index]+list(value))
|
|
return output
|
|
|
|
def compare(raw, answers):
|
|
rows = raw.split(b'\n')
|
|
if rows[-1:] != [b'']:
|
|
return 'stdout lacks final LF'
|
|
rows.pop()
|
|
for index,(line,wanted) in enumerate(zip(rows,answers),1):
|
|
if line.endswith(b'\r'):
|
|
line=line[:-1]
|
|
tokens=re.split(rb'[ \t]+',line.strip(b' \t'))
|
|
if len(tokens)!=len(wanted) or tokens[0]!=wanted[0].encode('ascii'):
|
|
return 'row %d shape/tag' % index
|
|
try:
|
|
for token,value in zip(tokens[1:],wanted[1:]):
|
|
if type(value) is float:
|
|
observed=floating(token)
|
|
if not math.isclose(observed,value,rel_tol=3e-12,abs_tol=1e-14):
|
|
return 'row %d numeric difference' % index
|
|
elif ascii_integer(token)!=value:
|
|
return 'row %d integer difference' % index
|
|
except (ValueError,OverflowError):
|
|
return 'row %d lexical/domain error' % index
|
|
if len(rows)!=len(answers):
|
|
return 'row count %d != %d' % (len(rows),len(answers))
|
|
return None
|
|
|
|
def validate(meta, argv, deadline, out, err):
|
|
def integer(name, positive=False):
|
|
value=meta[name]
|
|
if type(value) is not int or (positive and value<=0):
|
|
raise ValueError('metadata integer '+name)
|
|
return value
|
|
for key in ('supervisor','supervisor_start','leader','leader_start'):
|
|
integer(key,True)
|
|
if meta['supervisor']==meta['leader']:
|
|
raise ValueError('leader/supervisor identity conflict')
|
|
uid=65534 if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534' else os.getuid()
|
|
gid=65534 if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534' else os.getgid()
|
|
if integer('uid')!=uid or integer('gid')!=gid:
|
|
raise ValueError('execution identity')
|
|
groups=meta['groups']
|
|
if type(groups) is not list or any(type(v) is not int for v in groups):
|
|
raise ValueError('groups type')
|
|
expected_groups=[] if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534' else os.getgroups()
|
|
if groups!=expected_groups:
|
|
raise ValueError('supplementary groups')
|
|
for key in ('elapsed','deadline'):
|
|
v=meta[key]
|
|
if type(v) not in (int,float) or not math.isfinite(v):
|
|
raise ValueError('time metadata type/range')
|
|
if meta['deadline']!=deadline or not 0<=meta['elapsed']<=deadline+4:
|
|
raise ValueError('deadline inconsistency')
|
|
if type(meta['timeout']) is not bool:
|
|
raise ValueError('timeout type')
|
|
actual=integer('leader_rc');controlled=integer('controlled_rc')
|
|
if meta['argv']!=argv:
|
|
raise ValueError('argv binding')
|
|
if controlled != (125 if meta['exceptions'] else 124 if meta['timeout'] else actual):
|
|
raise ValueError('controlled/actual status inconsistency')
|
|
for key in ('reaped','descendants_absent','owned_pg_absent'):
|
|
if type(meta[key]) is not bool or not meta[key]:
|
|
raise ValueError('incomplete recovery '+key)
|
|
if type(meta['exceptions']) is not list or meta['exceptions']:
|
|
raise ValueError('supervisor exceptions')
|
|
for key,data in (('stdout',out),('stderr',err)):
|
|
if integer(key+'_len')<0 or meta[key+'_len']!=len(data):
|
|
raise ValueError('IO length binding')
|
|
if meta[key+'_sha']!=hashlib.sha256(data).hexdigest():
|
|
raise ValueError('IO digest binding')
|
|
for field in ('tracked','signals','adopted'):
|
|
if type(meta[field]) is not list:
|
|
raise ValueError('recovery inventory type')
|
|
if meta['timeout'] or actual!=0 or controlled!=0:
|
|
raise ValueError('controlled exit %s actual %s timeout %s' % (controlled,actual,meta['timeout']))
|
|
|
|
def stage(label, argv, scratch, data, deadline):
|
|
request=dict(argv=argv,cwd=str(scratch),input=base64.b64encode(data).decode('ascii'),deadline=deadline)
|
|
result=subprocess.run([sys.executable,'-I','-c',SUPERVISOR,json.dumps(request,separators=(',',':'))],capture_output=True,timeout=deadline+8)
|
|
record=dict(stage=label,argv=argv,deadline=deadline,input_len=len(data),input_sha=hashlib.sha256(data).hexdigest(),wrapper_rc=result.returncode,wire_len=len(result.stdout),
|
|
wire_sha=hashlib.sha256(result.stdout).hexdigest(),wrapper_stderr_len=len(result.stderr),
|
|
wrapper_stderr_sha=hashlib.sha256(result.stderr).hexdigest())
|
|
# Save raw wire before parsing whenever its compressed representation fits.
|
|
packed=base64.b85encode(zlib.compress(result.stdout,9)).decode('ascii')
|
|
if len(packed)<=2500:
|
|
record['wire_z85']=packed
|
|
EVIDENCE.append(record)
|
|
if result.returncode!=0:
|
|
raise ValueError(label+' wrapper nonzero')
|
|
meta=json.loads(result.stdout)
|
|
if 'wire_z85' not in record:
|
|
record['meta']={k:v for k,v in meta.items() if k not in ('stdout','stderr')}
|
|
out,err=unpack(meta['stdout']),unpack(meta['stderr'])
|
|
validate(meta,argv,deadline,out,err)
|
|
return out,err
|
|
|
|
def main():
|
|
scratch=None
|
|
failures=[]
|
|
behavior_ok=False
|
|
cleanup_ok=False
|
|
absence_ok=False
|
|
lines=cases()
|
|
answers=expected(lines)
|
|
try:
|
|
scratch=Path(tempfile.mkdtemp(prefix='quant-wide-decode-'))
|
|
for name,text in CONTEXT.items():
|
|
target=scratch/name
|
|
target.parent.mkdir(parents=True,exist_ok=True)
|
|
target.write_text(text)
|
|
(scratch/'src/engine.cpp').write_bytes((Path(sys.argv[1])/'src/engine.cpp').read_bytes())
|
|
# Scratch root already belongs to candidate UID under bos_verify's hook.
|
|
# Set controller-owned children before launching the first child.
|
|
for folder in (scratch/'src',scratch/'include'):
|
|
os.chmod(folder,0o755)
|
|
if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534':
|
|
os.chown(folder,65534,65534)
|
|
command=['g++','-std=c++17','-O2','-Wall','-Wextra','-Wpedantic','-Iinclude','src/engine.cpp','src/observer.cpp','-o','quant-demo']
|
|
stage('compiler',command,scratch,b'',COMPILE_DEADLINE)
|
|
raw,diagnostics=stage('candidate',[str(scratch/'quant-demo')],scratch,('\n'.join(lines)+'\n').encode('ascii'),RUN_DEADLINE)
|
|
mismatch=compare(raw,answers)
|
|
if mismatch is not None:
|
|
failures.append('behavior: '+mismatch)
|
|
else:
|
|
behavior_ok=True
|
|
except Exception as exc:
|
|
failures.append('execution: '+type(exc).__name__+':'+str(exc))
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],scratch.parent,b'',CLEAN_DEADLINE)
|
|
cleanup_ok=True
|
|
except Exception as exc:
|
|
failures.append('cleanup: '+type(exc).__name__+':'+str(exc))
|
|
try:
|
|
stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],scratch.parent,b'',ABSENCE_DEADLINE)
|
|
absence_ok=True
|
|
except Exception as exc:
|
|
failures.append('absence: '+type(exc).__name__+':'+str(exc))
|
|
evidence=dict(scratch=None if scratch is None else str(scratch),rows=len(answers),
|
|
phases=EVIDENCE,failures=failures,behavior=behavior_ok,cleanup=cleanup_ok,absence=absence_ok)
|
|
packed=base64.b85encode(zlib.compress(json.dumps(evidence,separators=(',',':')).encode(),9)).decode('ascii')
|
|
print('EVIDENCE85 '+packed,file=sys.stderr)
|
|
if behavior_ok and cleanup_ok and absence_ok and not failures:
|
|
print('PASS');return 0
|
|
print('FAIL '+(';'.join(failures)[:250] or 'incomplete required phases'));return 1
|
|
|
|
if __name__=='__main__':
|
|
raise SystemExit(main())
|