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

439 lines
31 KiB
Python

import base64
import bz2
import hashlib
import json
import math
import os
from pathlib import Path
import random
import subprocess
import sys
import tempfile
import time
RECOVERY_SECONDS = 3
RECORD_WIRE = False
SUPERVISOR = r'''
import base64,bz2,ctypes,hashlib,json,os,signal,subprocess,sys,time
cfg=json.loads(sys.argv[1]); start=time.monotonic(); errors=[]; tracked={}; signals={}; waits=[]
leader=None; leader_start=None; output=b''; diagnostic=b''; timed_out=False; drained=False
reaped=False; leader_rc=None; remaining=[]; pg_absent=False
def fault(op, error):
errors.append([op,type(error).__name__,str(error)])
def exact(pid):
try:
with open('/proc/%d/stat'%pid,'rb') as stream: data=stream.read()
words=data[data.rfind(b')')+2:].split()
return {'pid':pid,'state':words[0].decode('ascii'),'ppid':int(words[1]),'pg':int(words[2]),'start':int(words[19])}
except (FileNotFoundError,ProcessLookupError): return None
def table_now():
result={}
for name in os.listdir('/proc'):
if name.isascii() and name.isdigit():
value=exact(int(name))
if value is not None: result[value['pid']]=value
return result
def closure(table, supervisor, remembered):
roots={supervisor}
for pid,old in remembered.items():
current=table.get(pid)
if current and current['start']==old['start'] and current['state'] not in ('Z','X'):
roots.add(pid)
found={}
changed=True
while changed:
changed=False
for pid,current in table.items():
if pid!=supervisor and pid not in found and (current['ppid'] in roots or pid in roots):
found[pid]=current; roots.add(pid); changed=True
return found
def discover():
table=table_now(); found=closure(table,os.getpid(),tracked)
for pid,current in found.items():
if pid not in tracked or tracked[pid]['start']!=current['start']:
tracked[pid]=dict(current)
return table,found
def send_fresh(pid, token):
current=exact(pid)
if current is None or current['start']!=token['start'] or current['state'] in ('Z','X'): return
os.kill(pid,signal.SIGKILL)
signals[(pid,token['start'])]=signals.get((pid,token['start']),0)+1
def own_wait():
global reaped,leader_rc
if leader is not None:
value=leader.poll()
if value is not None:
leader_rc=leader.wait(timeout=0)
reaped=True
def adopted_wait():
if leader is not None and not reaped: return
while True:
try: pid,status=os.waitpid(-1,os.WNOHANG)
except ChildProcessError: break
if pid==0: break
waits.append([pid,None,status])
supervisor_start=exact(os.getpid())['start']
try:
libc=ctypes.CDLL(None,use_errno=True)
if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')
leader=subprocess.Popen(cfg['argv'],cwd=cfg['cwd'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)
info=exact(leader.pid)
if info is None: raise RuntimeError('leader identity disappeared before capture')
leader_start=info['start']; tracked[leader.pid]=info
payload=sys.stdin.buffer.read()
first=True
while True:
try:
output,diagnostic=leader.communicate(input=payload if first else None,timeout=0.04)
drained=True; own_wait(); break
except subprocess.TimeoutExpired:
first=False
own_wait()
discover()
if time.monotonic()-start>=cfg['deadline']:
timed_out=True; break
except BaseException as error:
fault('execute',error)
finally:
recovery_end=time.monotonic()+cfg['recovery']
while True:
try: own_wait()
except BaseException as error: fault('own_wait',error)
found={}
try: _,found=discover()
except BaseException as error: fault('discover',error)
for pid,token in list(found.items()):
try: send_fresh(pid,token)
except (FileNotFoundError,ProcessLookupError): pass
except BaseException as error: fault('signal',error)
try: own_wait()
except BaseException as error: fault('own_wait',error)
try: adopted_wait()
except BaseException as error: fault('adopted_wait',error)
if leader is not None and not drained:
try:
output,diagnostic=leader.communicate(timeout=0.03)
drained=True; own_wait()
except subprocess.TimeoutExpired: pass
except BaseException as error: fault('drain',error)
try:
table,found=discover()
remaining=[[p,v['start'],v['state']] for p,v in found.items()]
pg_absent=leader is not None and not any(v['pg']==leader.pid for v in table.values())
except BaseException as error:
fault('absence',error); remaining=[['unknown']]; pg_absent=False
if reaped and drained and not remaining and pg_absent: break
if time.monotonic()>=recovery_end:
fault('recovery',RuntimeError('deadline with unconfirmed closure')); break
time.sleep(0.01)
meta={'phase':cfg['phase'],'nonce':cfg['nonce'],'argv':cfg['argv'],
'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),
'supervisor_pid':os.getpid(),'supervisor_start':supervisor_start,
'leader_pid':None if leader is None else leader.pid,'leader_start':leader_start,
'leader_rc':leader_rc,'leader_reaped':reaped,'timeout':timed_out,
'controlled_rc':124 if timed_out else leader_rc,'elapsed':time.monotonic()-start,
'deadline':cfg['deadline'],'recovery':cfg['recovery'],
'pg_absent':pg_absent,'remaining':remaining,'exceptions':errors,
'descendants':[[p,t['start'],signals.get((p,t['start']),0),None]
for p,t in tracked.items() if leader is None or p!=leader.pid],
'adopted_waits':waits,'stdout_len':len(output),'stdout_sha256':hashlib.sha256(output).hexdigest(),
'stderr_len':len(diagnostic),'stderr_sha256':hashlib.sha256(diagnostic).hexdigest()}
wire={'meta':meta,'stdout':base64.b64encode(bz2.compress(output)).decode('ascii'),
'stderr':base64.b64encode(bz2.compress(diagnostic)).decode('ascii')}
sys.stdout.write(json.dumps(wire,separators=(',',':'))+'\n')
'''
CLEANUP = "import shutil,sys; shutil.rmtree(sys.argv[1])"
ABSENCE = "import os,sys\ntry: os.lstat(sys.argv[1])\nexcept FileNotFoundError: sys.exit(0)\nsys.exit(1)"
def identity_int(value, minimum=0):
return type(value) is int and value >= minimum
def validate_meta(meta, config, out, err):
required = {'phase','nonce','argv','uid','gid','groups','supervisor_pid','supervisor_start',
'supervisor_start','leader_pid','leader_start','leader_rc','leader_reaped','timeout','controlled_rc',
'elapsed','deadline','recovery','pg_absent','remaining','exceptions','descendants',
'adopted_waits','stdout_len','stdout_sha256','stderr_len','stderr_sha256'}
if type(meta) is not dict or not required <= meta.keys():
raise ValueError('missing stage metadata')
if meta['phase'] != config['phase'] or meta['nonce'] != config['nonce'] or meta['argv'] != config['argv']:
raise ValueError('stage binding conflict')
for name in ('uid','gid','supervisor_pid','supervisor_start','leader_pid','leader_start'):
if not identity_int(meta[name], 1 if name not in ('uid','gid') else 0):
raise ValueError('invalid identity '+name)
if meta['supervisor_pid'] == meta['leader_pid']:
raise ValueError('supervisor and leader identical')
isolated = os.environ.get('ROLLOUT_GRADE_ISOLATION') == 'uid65534'
expected_identity = (65534,65534,[]) if isolated else (os.getuid(),os.getgid(),os.getgroups())
if type(meta['groups']) is not list or any(type(x) is not int or x < 0 for x in meta['groups']):
raise ValueError('invalid groups')
if (meta['uid'],meta['gid'],meta['groups']) != expected_identity:
raise ValueError('execution identity conflict')
if type(meta['deadline']) is not int or meta['deadline'] != config['deadline']:
raise ValueError('deadline conflict')
if type(meta['recovery']) is not int or meta['recovery'] != RECOVERY_SECONDS:
raise ValueError('recovery conflict')
elapsed = meta['elapsed']
if type(elapsed) not in (int,float) or not math.isfinite(elapsed) or not 0 <= elapsed <= config['deadline']+RECOVERY_SECONDS+2:
raise ValueError('invalid elapsed')
for name in ('timeout','leader_reaped','pg_absent'):
if type(meta[name]) is not bool:
raise ValueError('invalid boolean '+name)
for name in ('leader_rc','controlled_rc'):
if type(meta[name]) is not int:
raise ValueError('invalid return code '+name)
if meta['controlled_rc'] != (124 if meta['timeout'] else meta['leader_rc']):
raise ValueError('return code conflict')
for name in ('remaining','exceptions','descendants','adopted_waits'):
if type(meta[name]) is not list:
raise ValueError('invalid list '+name)
for row in meta['descendants']:
if type(row) is not list or len(row) != 4 or not all(identity_int(x,1) for x in row[:2]) or not identity_int(row[2]) or row[3] is not None:
raise ValueError('invalid descendant record')
for row in meta['adopted_waits']:
if type(row) is not list or len(row) != 3 or not identity_int(row[0],1) or row[1] is not None or type(row[2]) is not int:
raise ValueError('invalid adopted wait record')
for name,data in (('stdout',out),('stderr',err)):
if not identity_int(meta[name+'_len']) or meta[name+'_len'] != len(data):
raise ValueError('invalid IO length')
if type(meta[name+'_sha256']) is not str or meta[name+'_sha256'] != hashlib.sha256(data).hexdigest():
raise ValueError('invalid IO digest')
if meta['exceptions'] or meta['remaining'] or not meta['leader_reaped'] or not meta['pg_absent']:
raise ValueError('stage recovery or exception failure')
if meta['timeout'] or meta['leader_rc'] != 0 or meta['controlled_rc'] != 0:
raise ValueError('controlled timeout' if meta['timeout'] else 'leader nonzero exit')
def execute_stage(phase, argv, cwd, deadline, payload, evidence):
nonce = os.urandom(8).hex()
config = dict(phase=phase,argv=argv,cwd=str(cwd),deadline=deadline,recovery=RECOVERY_SECONDS,nonce=nonce)
wrapper_argv = [sys.executable,'-I','-c',SUPERVISOR,json.dumps(config,separators=(',',':'))]
raw = subprocess.run(wrapper_argv,input=payload,capture_output=True,timeout=deadline+RECOVERY_SECONDS+5)
# Preserve actual wrapper facts before touching or trusting its wire protocol.
entry = dict(phase=phase,nonce=nonce,wrapper_rc=raw.returncode,
wire_len=len(raw.stdout),wire_sha256=hashlib.sha256(raw.stdout).hexdigest(),
wrapper_stderr_len=len(raw.stderr),wrapper_stderr_sha256=hashlib.sha256(raw.stderr).hexdigest())
if RECORD_WIRE:
entry['wire_b85'] = base64.b85encode(bz2.compress(raw.stdout)).decode('ascii')
entry['wrapper_stderr_bz2'] = base64.b64encode(bz2.compress(raw.stderr)).decode('ascii')
evidence.append(entry)
if raw.returncode != 0:
raise ValueError(phase+' supervisor nonzero')
wire = json.loads(raw.stdout)
out = bz2.decompress(base64.b64decode(wire['stdout'],validate=True))
err = bz2.decompress(base64.b64decode(wire['stderr'],validate=True))
meta = wire['meta']
if not RECORD_WIRE: entry['meta'] = meta
validate_meta(meta,config,out,err)
return out,err
SUPPORT = {'main.cpp': '#include "ratio.hpp"\n#include <fstream>\n#include <iostream>\n#include <string>\nint main(int argc, char** argv) {\n if (argc == 1) {\n return ratio::run(std::cin, std::cout);\n }\n if (argc != 3 || std::string(argv[1]) != "--input") {\n std::cerr << "usage: ratio-map [--input FILE]\\n";\n return 2;\n }\n std::ifstream input(argv[2], std::ios::binary);\n if (!input) {\n std::cerr << "cannot open input\\n";\n return 2;\n }\n return ratio::run(input, std::cout);\n}\n', 'observer.cpp': '#include "ratio.hpp"\n#include <istream>\n#include <ostream>\nnamespace ratio {\nvoid print_configuration(std::ostream& output, char kind, int slot, const Configuration& value) {\n output << kind << \' \' << slot << \' \' << static_cast<int>(value.result)\n << \' \' << value.generation << \'\\n\';\n}\nvoid print_feed(std::ostream& output, int slot, const Feed& value) {\n output << \'F\' << \' \' << slot << \' \' << static_cast<int>(value.result)\n << \' \' << value.mapped << \' \' << value.weight << \'\\n\';\n}\nvoid print_snapshot(std::ostream& output, int slot, const Snapshot& value) {\n const Statistics& stats = value.stats;\n output << \'S\' << \' \' << slot << \' \' << static_cast<int>(value.result)\n << \' \' << value.generation << \' \' << stats.samples << \' \' << stats.weight\n << \' \' << stats.raw_total << \' \' << stats.mapped_total\n << \' \' << stats.raw_mean() << \' \' << stats.mapped_mean()\n << \' \' << stats.minimum << \' \' << stats.maximum << \' \' << stats.clipped << \'\\n\';\n}\nvoid print_totals(std::ostream& output, const Totals& value) {\n output << \'T\' << \' \' << value.configured << \' \' << value.stats.samples\n << \' \' << value.stats.weight << \' \' << value.stats.raw_total\n << \' \' << value.stats.mapped_total << \' \' << value.stats.clipped << \'\\n\';\n}\nint run(std::istream& input, std::ostream& output) {\n Ledger ledger;\n Parser parser;\n std::string line;\n while (std::getline(input, line)) {\n const Command command = parser.parse(line);\n switch (command.verb) {\n case Verb::empty:\n break;\n case Verb::invalid:\n output << "E 1\\n";\n break;\n case Verb::configure:\n print_configuration(output, \'C\', command.slot, ledger.configure(command.slot, command.profile));\n break;\n case Verb::feed:\n print_feed(output, command.slot, ledger.feed(command.slot, command.raw, command.weight));\n break;\n case Verb::snapshot:\n print_snapshot(output, command.slot, ledger.snapshot(command.slot));\n break;\n case Verb::drop:\n print_configuration(output, \'D\', command.slot, ledger.drop(command.slot));\n break;\n case Verb::clear:\n print_configuration(output, \'Z\', command.slot, ledger.clear(command.slot));\n break;\n case Verb::totals:\n print_totals(output, ledger.totals());\n break;\n }\n }\n return input.bad() || !output ? 2 : 0;\n}\n}\n', 'ratio.hpp': '#ifndef RATIO_HPP\n#define RATIO_HPP\n#include <array>\n#include <cstdint>\n#include <iosfwd>\n#include <string>\n#include <vector>\nnamespace ratio {\nconstexpr int channel_count = 16;\nconstexpr std::int64_t raw_limit = 1000000;\nconstexpr std::int64_t output_limit = 1000000000;\nconstexpr std::uint64_t sample_limit = 256;\nenum class Result : int {\n ok = 0,\n bad_slot = -1,\n bad_value = -2,\n missing = -3,\n full = -4\n};\nstruct Profile {\n std::int64_t raw_low = 0;\n std::int64_t raw_high = 1;\n std::int64_t out_low = 0;\n std::int64_t out_high = 1;\n bool valid() const;\n};\nstruct Mapped {\n std::int64_t value = 0;\n bool clipped = false;\n};\nstd::int64_t rounded_ratio(std::int64_t numerator, std::int64_t denominator);\nMapped map_sample(const Profile& profile, std::int64_t raw);\nstruct Statistics {\n std::uint64_t samples = 0;\n std::uint64_t weight = 0;\n std::int64_t raw_total = 0;\n std::int64_t mapped_total = 0;\n std::int64_t minimum = 0;\n std::int64_t maximum = 0;\n std::uint64_t clipped = 0;\n void clear();\n void add(std::int64_t raw, const Mapped& value, std::uint64_t amount);\n std::int64_t raw_mean() const;\n std::int64_t mapped_mean() const;\n};\nstruct Channel {\n bool configured = false;\n std::uint64_t generation = 0;\n Profile profile;\n Statistics stats;\n};\nstruct Configuration {\n Result result = Result::ok;\n std::uint64_t generation = 0;\n};\nstruct Feed {\n Result result = Result::ok;\n std::int64_t mapped = 0;\n std::uint64_t weight = 0;\n};\nstruct Snapshot {\n Result result = Result::ok;\n std::uint64_t generation = 0;\n Statistics stats;\n};\nstruct Totals {\n std::uint64_t configured = 0;\n Statistics stats;\n};\nclass Ledger {\npublic:\n Ledger();\n Configuration configure(int slot, const Profile& profile);\n Feed feed(int slot, std::int64_t raw, std::int64_t weight);\n Snapshot snapshot(int slot) const;\n Configuration drop(int slot);\n Configuration clear(int slot);\n Totals totals() const;\nprivate:\n std::array<Channel, channel_count> channels_;\n static bool has_slot(int slot);\n};\nenum class Verb { empty, invalid, configure, feed, snapshot, drop, clear, totals };\nstruct Command {\n Verb verb = Verb::invalid;\n int slot = 0;\n Profile profile;\n std::int64_t raw = 0;\n std::int64_t weight = 0;\n};\nclass Parser {\npublic:\n Command parse(const std::string& line) const;\nprivate:\n static bool separator(unsigned char byte);\n static bool tokenize(const std::string& line, std::vector<std::string>& result);\n static bool integer(const std::string& token, std::int64_t& value);\n static bool slot_number(const std::string& token, int& value);\n};\nvoid print_configuration(std::ostream& output, char kind, int slot, const Configuration& value);\nvoid print_feed(std::ostream& output, int slot, const Feed& value);\nvoid print_snapshot(std::ostream& output, int slot, const Snapshot& value);\nvoid print_totals(std::ostream& output, const Totals& value);\nint run(std::istream& input, std::ostream& output);\n}\n#endif\n', 'parser.cpp': '#include "ratio.hpp"\n#include <limits>\nnamespace ratio {\nbool Parser::separator(unsigned char byte) {\n return byte == \' \' || byte == \'\\t\' || byte == \'\\r\';\n}\nbool Parser::tokenize(const std::string& line, std::vector<std::string>& result) {\n if (line.size() > 256) {\n return false;\n }\n std::size_t cursor = 0;\n while (cursor < line.size()) {\n while (cursor < line.size() && separator(static_cast<unsigned char>(line[cursor]))) {\n ++cursor;\n }\n if (cursor == line.size()) {\n break;\n }\n const std::size_t begin = cursor;\n while (cursor < line.size() && !separator(static_cast<unsigned char>(line[cursor]))) {\n const unsigned char byte = static_cast<unsigned char>(line[cursor]);\n if (byte < 33 || byte > 126) {\n return false;\n }\n ++cursor;\n }\n result.push_back(line.substr(begin, cursor - begin));\n if (result.size() > 6) {\n return false;\n }\n }\n return true;\n}\nbool Parser::integer(const std::string& token, std::int64_t& value) {\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 std::uint64_t positive = static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max());\n const std::uint64_t bound = positive + (negative ? 1u : 0u);\n std::uint64_t number = 0;\n for (; cursor < token.size(); ++cursor) {\n const unsigned char byte = static_cast<unsigned char>(token[cursor]);\n if (byte < \'0\' || byte > \'9\') {\n return false;\n }\n const std::uint64_t digit = byte - \'0\';\n if (number > (bound - digit) / 10) {\n return false;\n }\n number = number * 10 + digit;\n }\n if (negative && number == positive + 1) {\n value = std::numeric_limits<std::int64_t>::min();\n } else {\n value = static_cast<std::int64_t>(number);\n if (negative) {\n value = -value;\n }\n }\n return true;\n}\nbool Parser::slot_number(const std::string& token, int& value) {\n std::int64_t wide = 0;\n if (!integer(token, wide)) {\n return false;\n }\n if (wide < std::numeric_limits<int>::min() || wide > std::numeric_limits<int>::max()) {\n return false;\n }\n value = static_cast<int>(wide);\n return true;\n}\nCommand Parser::parse(const std::string& line) const {\n Command response;\n std::vector<std::string> tokens;\n if (!tokenize(line, tokens)) {\n return response;\n }\n if (tokens.empty()) {\n response.verb = Verb::empty;\n return response;\n }\n if (tokens[0] == "TOTAL" && tokens.size() == 1) {\n response.verb = Verb::totals;\n return response;\n }\n if (tokens.size() < 2 || !slot_number(tokens[1], response.slot)) {\n return response;\n }\n if (tokens[0] == "CONFIG" && tokens.size() == 6) {\n if (!integer(tokens[2], response.profile.raw_low) ||\n !integer(tokens[3], response.profile.raw_high) ||\n !integer(tokens[4], response.profile.out_low) ||\n !integer(tokens[5], response.profile.out_high)) {\n return response;\n }\n response.verb = Verb::configure;\n } else if (tokens[0] == "FEED" && tokens.size() == 4) {\n if (!integer(tokens[2], response.raw) || !integer(tokens[3], response.weight)) {\n return response;\n }\n response.verb = Verb::feed;\n } else if (tokens[0] == "SNAP" && tokens.size() == 2) {\n response.verb = Verb::snapshot;\n } else if (tokens[0] == "CLEAR" && tokens.size() == 2) {\n response.verb = Verb::clear;\n } else if (tokens[0] == "DROP" && tokens.size() == 2) {\n response.verb = Verb::drop;\n }\n return response;\n}\n}\n'}
COMPILER_DEADLINE = 20
CANDIDATE_DEADLINE = 8
CLEANUP_DEADLINE = 5
ABSENCE_DEADLINE = 5
def rounded(numerator, denominator):
magnitude, remainder = divmod(abs(numerator), denominator)
return (-1 if numerator < 0 else 1) * (magnitude + (2 * remainder >= denominator))
class Model:
def __init__(self):
self.generations = [0] * 16
self.profiles = {}
self.events = {slot: [] for slot in range(16)}
def stats(self, slot):
records = self.events[slot]
weight = sum(w for raw, mapped, w, clipped in records)
raw_total = sum(raw*w for raw, mapped, w, clipped in records)
mapped_total = sum(mapped*w for raw, mapped, w, clipped in records)
values = [mapped for raw, mapped, w, clipped in records]
return (len(records), weight, raw_total, mapped_total,
rounded(raw_total, weight) if weight else 0,
rounded(mapped_total, weight) if weight else 0,
min(values) if values else 0, max(values) if values else 0,
sum(clipped for raw, mapped, w, clipped in records))
def apply(self, op):
verb = op[0]
if verb == 'E': return ('E', 1)
if verb == 'TOTAL':
selected = [self.stats(slot) for slot in self.profiles]
return ('T',len(selected),*(sum(row[index] for row in selected) for index in (0,1,2,3,8)))
slot = op[1]
valid_slot = 0 <= slot < 16
if verb == 'CONFIG':
lo,hi,low,high = op[2:]
if not valid_slot: return ('C',slot,-1,0)
if not (-1000000 <= lo < hi <= 1000000 and -1000000000 <= low <= 1000000000 and -1000000000 <= high <= 1000000000):
return ('C',slot,-2,0)
self.generations[slot] += 1
self.profiles[slot] = (lo,hi,low,high)
self.events[slot] = []
return ('C',slot,0,self.generations[slot])
if verb == 'FEED':
raw,weight = op[2:]
code = -1 if not valid_slot else -3 if slot not in self.profiles else -2 if not (-1000000 <= raw <= 1000000 and 1 <= weight <= 1000) else -4 if len(self.events[slot]) >= 256 else 0
if code: return ('F',slot,code,0,0)
lo,hi,low,high = self.profiles[slot]
bounded = min(hi,max(lo,raw))
# Independent Fraction model: form the full rational before rounding.
numerator = low*(hi-lo)+(bounded-lo)*(high-low)
mapped = rounded(numerator,hi-lo)
self.events[slot].append((raw,mapped,weight,int(raw!=bounded)))
return ('F',slot,0,mapped,weight)
if verb == 'SNAP':
if not valid_slot: return ('S',slot,-1,0,*([0]*9))
code = 0 if slot in self.profiles else -3
return ('S',slot,code,self.generations[slot],*self.stats(slot))
tag = 'D' if verb == 'DROP' else 'Z'
if not valid_slot: return (tag,slot,-1,0)
if slot not in self.profiles: return (tag,slot,-3,self.generations[slot])
self.events[slot] = []
if verb == 'DROP':
del self.profiles[slot]
self.generations[slot] += 1
return (tag,slot,0,self.generations[slot])
def make_cases():
model = Model()
lines, expected = [], []
def add(op, spelling=None):
lines.append(spelling if spelling is not None else ' '.join(map(str,op)))
expected.append(model.apply(op))
for op in [('CONFIG',0,0,10,0,95),('FEED',0,1,1),('FEED',0,2,1),('SNAP',0),('TOTAL',),
('CONFIG',2,0,100,0,1000),('FEED',2,20,3),('CONFIG',2,0,100,1000,0),('SNAP',2),('FEED',2,75,2),('SNAP',2),('TOTAL',)]: add(op)
# Every channel, negative/descending/flat ranges, exact ties, clamping and weights.
profiles=[(-3,3,-7,10),(-10,10,10,-10),(0,2,-1,0),(0,2,0,1),(-1000000,1000000,-1000000000,1000000000),(0,7,5,5)]
for slot in range(16):
lo,hi,low,high=profiles[slot%len(profiles)]
add(('CONFIG',slot,lo,hi,low,high))
add(('SNAP',slot))
for raw,weight in [(lo-1 if lo>-1000000 else lo,1),(lo,2),((lo+hi)//2,3),(hi,7),(hi+1 if hi<1000000 else hi,1000)]: add(('FEED',slot,raw,weight))
add(('SNAP',slot)); add(('TOTAL',))
# Invalid operations preserve the exact current statistics and generation.
for op in [('CONFIG',-1,0,10,0,10),('CONFIG',16,0,10,0,10),('CONFIG',0,4,4,0,9),('CONFIG',0,-1000001,4,0,9),('CONFIG',0,0,1,0,1000000001),('FEED',0,1000001,1),('FEED',0,0,0),('FEED',0,0,1001),('SNAP',0),('DROP',16),('CLEAR',-1)]:add(op)
for raw in ['', ' \t\r', '\t\t']:
lines.append(raw)
for raw in ['CONFIG 1 0 2 0 3 extra','FEED 0 1_0 1','FEED 0 + 1','FEED 0 1 1\v','TOTAL\f','TOTAL\x00','SNAP 2147483648','FEED 0 9223372036854775808 1','x','x'*257]: add(('E',),raw)
add(('FEED',0,0,1),'FEED\t+0000\r-'+'0'*128+'\t+0001')
add(('SNAP',0),' \tSNAP 000\r')
rng=random.Random(202610250149)
for index in range(420):
slot=rng.randrange(16)
choice=rng.randrange(10)
if choice<5: op=('FEED',slot,rng.randint(-1000000,1000000),rng.randint(1,1000))
elif choice==5:
lo=rng.randint(-10000,10000); hi=lo+rng.randint(1,10000)
op=('CONFIG',slot,lo,hi,rng.randint(-1000000000,1000000000),rng.randint(-1000000000,1000000000))
elif choice==6: op=('DROP',slot)
elif choice==7: op=('CLEAR',slot)
elif choice==8: op=('SNAP',slot)
else: op=('TOTAL',)
add(op)
# Capacity, recovery and generation persistence on a single slot.
add(('CONFIG',15,-1000000,1000000,1000000000,-1000000000))
for index in range(259):add(('FEED',15,index-129,index%1000+1))
for op in [('SNAP',15),('CONFIG',15,0,2,0,1),('SNAP',15),('FEED',15,1,1),('CLEAR',15),('SNAP',15),('FEED',15,2,1),('DROP',15),('SNAP',15),('DROP',15),('CLEAR',15),('FEED',15,1,1),('CONFIG',15,0,1,2,3),('SNAP',15),('TOTAL',)]:add(op)
return ('\n'.join(lines)+'\n').encode('ascii'),expected
def number(token):
if not token: raise ValueError('empty numeric token')
negative = token[:1] == b'-'
digits = token[1:] if token[:1] in (b'+',b'-') else token
if not digits or any(byte<48 or byte>57 for byte in digits):
raise ValueError('not a whole ASCII decimal token')
significant=digits.lstrip(b'0') or b'0'
if len(significant)>19: raise ValueError('numeric value exceeds observable int64 domain')
value=int(significant)
return -value if negative else value
def observations(raw):
lines=raw.split(b'\n')
if lines and not lines[-1]: lines.pop()
output=[]
for line in lines:
words=line.strip(b' \t\r').replace(b'\t',b' ').replace(b'\r',b' ').split(b' ')
words=[word for word in words if word]
if not words or words[0] not in (b'C',b'F',b'S',b'D',b'T',b'Z',b'E'):
raise ValueError('invalid observer label')
output.append((words[0].decode('ascii'),*(number(word) for word in words[1:])))
return output
def publish(evidence, failures, expected, actual):
report=dict(stages=evidence,failures=failures,expected_rows=len(expected),actual_rows=None if actual is None else len(actual))
encoded=base64.b85encode(bz2.compress(json.dumps(report,separators=(',',':')).encode())).decode()
print('EVIDENCE '+encoded,file=sys.stderr)
def main():
evidence=[]; failures=[]; scratch=None; expected=[]; actual=None; business_ok=False; cleanup_ok=False; absence_ok=False
try:
payload,expected=make_cases()
scratch=Path(tempfile.mkdtemp(prefix='ratio-map-'))
work=scratch/'engine'
work.mkdir(mode=0o777)
os.chmod(work,0o777)
candidate=Path(sys.argv[1])
for name,text in SUPPORT.items():
target=work/name; target.parent.mkdir(parents=True,exist_ok=True); target.write_text(text)
for name in ('mapping.cpp','ledger.cpp'):
(work/name).write_bytes((candidate/name).read_bytes())
# Every source and public support file is ready before the first child.
compile_argv=['g++','-std=c++17','-O2','-Wall','-Wextra','-Wpedantic','mapping.cpp','ledger.cpp','parser.cpp','observer.cpp','main.cpp','-o','ratio-map']
execute_stage('compiler',compile_argv,work,COMPILER_DEADLINE,b'',evidence)
output,diagnostic=execute_stage('candidate',[str(work/'ratio-map')],work,CANDIDATE_DEADLINE,payload,evidence)
actual=observations(output)
if actual != expected:
first=next((i for i,pair in enumerate(zip(actual,expected)) if pair[0]!=pair[1]),min(len(actual),len(expected)))
failures.append(['behavior','first_raw_difference',first,
actual[first] if first<len(actual) else None,
expected[first] if first<len(expected) else None])
else: business_ok=True
except BaseException as error:
failures.append(['execute',type(error).__name__,str(error)])
finally:
if scratch is not None:
try:
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/tmp',CLEANUP_DEADLINE,b'',evidence)
cleanup_ok=True
except BaseException as error:
failures.append(['cleanup',type(error).__name__,str(error)])
try:
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/tmp',ABSENCE_DEADLINE,b'',evidence)
absence_ok=True
except BaseException as error:
failures.append(['absence',type(error).__name__,str(error)])
publish(evidence,failures,expected,actual)
passed=business_ok and cleanup_ok and absence_ok and not failures
print('PASS' if passed else 'FAIL '+json.dumps(failures,separators=(',',':'))[:400])
return 0 if passed else 1
if __name__=='__main__':
sys.exit(main())