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>
460 lines
29 KiB
Python
460 lines
29 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,os.waitstatus_to_exitcode(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',
|
|
'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['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 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 and type(row[3]) is not int):
|
|
raise ValueError('invalid descendant 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_ascii'] = 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']
|
|
entry['meta'] = meta
|
|
validate_meta(meta,config,out,err)
|
|
return out,err
|
|
|
|
SUPPORT = {'text.h': '#ifndef TEXT_H\n#define TEXT_H\n#include <stddef.h>\n#include <stdint.h>\n#define TEXT_CAPACITY 96\n#define TEXT_RECORDS 16\n#define TEXT_KEY 16\n\nenum text_kind {\n TEXT_CSTR = 1,\n TEXT_COUNTED = 2\n};\nenum text_status {\n TEXT_OK = 0,\n TEXT_INVALID = -1,\n TEXT_MISSING = -2,\n TEXT_FULL = -3\n};\nstruct text_record {\n char key[TEXT_KEY];\n unsigned char data[TEXT_CAPACITY];\n size_t stored;\n enum text_kind kind;\n int occupied;\n};\nstruct text_store {\n struct text_record records[TEXT_RECORDS];\n size_t count;\n uint32_t revision;\n};\nstruct text_measure {\n size_t stored;\n size_t logical;\n size_t wire;\n uint32_t digest;\n enum text_kind kind;\n};\nstruct text_report {\n size_t selected;\n size_t logical;\n size_t wire;\n uint32_t digest;\n};\nint text_key_valid(const char *key);\nvoid text_store_init(struct text_store *store);\nvoid text_store_reset(struct text_store *store);\nint text_store_set(struct text_store *store, const char *key,\n enum text_kind kind, const unsigned char *data, size_t length);\nint text_store_delete(struct text_store *store, const char *key);\nint text_store_copy(struct text_store *store, const char *source, const char *target);\nconst struct text_record *text_store_find(const struct text_store *store, const char *key);\nsize_t text_extent(const unsigned char *data, size_t length, enum text_kind kind);\nuint32_t text_hash_byte(uint32_t hash, unsigned char value);\nuint32_t text_hash_bytes(uint32_t hash, const unsigned char *data, size_t length);\nsize_t text_wire_size(const unsigned char *data, size_t length);\nint text_measure(const struct text_store *store, const char *key, struct text_measure *result);\nint text_report(const struct text_store *store, const char *prefix, size_t limit,\n struct text_report *result);\n#endif\n', 'cli.c': '#include "text.h"\n#include <inttypes.h>\n#include <stdio.h>\n#include <string.h>\n\nstatic int horizontal(char value)\n{\n return value == \' \' || value == \'\\t\' || value == \'\\r\' || value == \'\\n\';\n}\n\nstatic int split(char *line, char **words, size_t capacity)\n{\n size_t used = 0;\n char *cursor = line;\n while (*cursor != 0) {\n while (horizontal(*cursor)) {\n cursor += 1;\n }\n if (*cursor == 0) {\n break;\n }\n if (used == capacity) {\n return -1;\n }\n words[used++] = cursor;\n while (*cursor != 0 && !horizontal(*cursor)) {\n cursor += 1;\n }\n if (*cursor != 0) {\n *cursor++ = 0;\n }\n }\n return (int)used;\n}\n\nstatic int hex_digit(char value)\n{\n if (value >= \'0\' && value <= \'9\') {\n return value - \'0\';\n }\n if (value >= \'a\' && value <= \'f\') {\n return value - \'a\' + 10;\n }\n if (value >= \'A\' && value <= \'F\') {\n return value - \'A\' + 10;\n }\n return -1;\n}\n\nstatic int bytes(const char *word, unsigned char *data, size_t *length)\n{\n size_t size;\n size_t index;\n if (strcmp(word, "-") == 0) {\n *length = 0;\n return 1;\n }\n size = strlen(word);\n if (size > TEXT_CAPACITY * 2 || size % 2 != 0) {\n return 0;\n }\n for (index = 0; index < size; index += 2) {\n int high = hex_digit(word[index]);\n int low = hex_digit(word[index + 1]);\n if (high < 0 || low < 0) {\n return 0;\n }\n data[index / 2] = (unsigned char)(high * 16 + low);\n }\n *length = size / 2;\n return 1;\n}\n\nstatic int limit_value(const char *word, size_t *result)\n{\n size_t value = 0;\n if (*word == \'+\') {\n word += 1;\n }\n if (*word == 0) {\n return 0;\n }\n while (*word != 0) {\n if (*word < \'0\' || *word > \'9\') {\n return 0;\n }\n value = value * 10 + (size_t)(*word - \'0\');\n if (value > TEXT_RECORDS) {\n return 0;\n }\n word += 1;\n }\n *result = value;\n return 1;\n}\n\nstatic void mutation(const char *name, int status, const struct text_store *store)\n{\n printf("%s %d %zu %" PRIu32 "\\n", name, status, store->count, store->revision);\n}\n\nstatic void get(const struct text_store *store, const char *key)\n{\n struct text_measure result;\n int status = text_measure(store, key, &result);\n printf("GET %d %d %zu %zu %zu %" PRIu32 "\\n", status, (int)result.kind,\n result.stored, result.logical, result.wire, result.digest);\n}\n\nstatic void view(const struct text_store *store, const char *prefix, size_t limit)\n{\n struct text_report result;\n int status = text_report(store, prefix, limit, &result);\n printf("VIEW %d %zu %zu %zu %" PRIu32 "\\n", status, result.selected,\n result.logical, result.wire, result.digest);\n}\n\nstatic void dispatch(struct text_store *store, char **words, int count)\n{\n unsigned char data[TEXT_CAPACITY];\n size_t length;\n size_t limit;\n int status;\n if (count == 0) {\n return;\n }\n if (count == 1 && strcmp(words[0], "RESET") == 0) {\n text_store_reset(store);\n mutation("RESET", 0, store);\n return;\n }\n if (count == 4 && strcmp(words[0], "SET") == 0) {\n enum text_kind kind;\n if (strcmp(words[2], "C") == 0) {\n kind = TEXT_CSTR;\n } else if (strcmp(words[2], "B") == 0) {\n kind = TEXT_COUNTED;\n } else {\n puts("ERR -1");\n return;\n }\n if (!bytes(words[3], data, &length)) {\n puts("ERR -1");\n return;\n }\n status = text_store_set(store, words[1], kind, data, length);\n mutation("SET", status, store);\n return;\n }\n if (count == 2 && strcmp(words[0], "GET") == 0) {\n get(store, words[1]);\n return;\n }\n if (count == 2 && strcmp(words[0], "DEL") == 0) {\n status = text_store_delete(store, words[1]);\n mutation("DEL", status, store);\n return;\n }\n if (count == 3 && strcmp(words[0], "COPY") == 0) {\n status = text_store_copy(store, words[1], words[2]);\n mutation("COPY", status, store);\n return;\n }\n if (count == 3 && strcmp(words[0], "VIEW") == 0) {\n if (!limit_value(words[2], &limit)) {\n puts("ERR -1");\n return;\n }\n view(store, strcmp(words[1], "-") == 0 ? "" : words[1], limit);\n return;\n }\n puts("ERR -1");\n}\n\nint main(void)\n{\n struct text_store store;\n char line[1024];\n char *words[5];\n text_store_init(&store);\n while (fgets(line, sizeof(line), stdin) != NULL) {\n int count = split(line, words, 5);\n if (count < 0) {\n puts("ERR -1");\n } else {\n dispatch(&store, words, count);\n }\n }\n return ferror(stdin) || ferror(stdout) ? 1 : 0;\n}\n'}
|
|
|
|
COMPILER_DEADLINE = 20
|
|
CANDIDATE_DEADLINE = 10
|
|
CLEANUP_DEADLINE = 5
|
|
ABSENCE_DEADLINE = 5
|
|
|
|
def model(lines):
|
|
import re
|
|
store = {}
|
|
revision = 0
|
|
answer = []
|
|
def key(value):
|
|
return re.fullmatch(r'[A-Za-z][A-Za-z0-9_]{0,14}',value) is not None
|
|
def logical(record):
|
|
kind,data = record
|
|
return data.split(b'\0',1)[0] if kind == 1 else data
|
|
def digest(data):
|
|
value = 2166136261
|
|
for byte in data:
|
|
value = ((value ^ byte) * 16777619) & 0xffffffff
|
|
return value
|
|
def wire(data):
|
|
return sum(1 if 32 <= byte <= 126 and byte not in (92,124) else 4 for byte in data)
|
|
def mutation(name,status):
|
|
answer.append((name,status,len(store),revision))
|
|
for line in lines:
|
|
words = re.findall(r'[^ \t\r\n]+',line)
|
|
if not words:
|
|
continue
|
|
name = words[0]
|
|
if name == 'RESET' and len(words) == 1:
|
|
store = {}; revision = (revision + 1) & 0xffffffff
|
|
mutation(name,0)
|
|
elif name == 'SET' and len(words) == 4:
|
|
_,label,kind,encoded = words
|
|
if kind not in ('C','B') or (encoded != '-' and (len(encoded)>192 or len(encoded)%2 or not re.fullmatch('[0-9a-fA-F]+',encoded))):
|
|
answer.append(('ERR',-1)); continue
|
|
data = b'' if encoded == '-' else bytes.fromhex(encoded)
|
|
status = -1 if not key(label) or kind == 'C' and b'\0' not in data else -3 if label not in store and len(store)==16 else 0
|
|
if status == 0:
|
|
store[label] = (1 if kind=='C' else 2,data); revision = (revision+1)&0xffffffff
|
|
mutation(name,status)
|
|
elif name == 'DEL' and len(words) == 2:
|
|
label=words[1]
|
|
status=-1 if not key(label) else -2 if label not in store else 0
|
|
if status==0:
|
|
del store[label]; revision=(revision+1)&0xffffffff
|
|
mutation(name,status)
|
|
elif name == 'COPY' and len(words) == 3:
|
|
source,target=words[1:]
|
|
status=-1 if not key(source) or not key(target) else -2 if source not in store else -3 if target not in store and len(store)==16 else 0
|
|
if status==0:
|
|
store[target]=store[source]; revision=(revision+1)&0xffffffff
|
|
mutation(name,status)
|
|
elif name == 'GET' and len(words) == 2:
|
|
label=words[1]
|
|
status=-1 if not key(label) else -2 if label not in store else 0
|
|
if status:
|
|
answer.append((name,status,0,0,0,0,0))
|
|
else:
|
|
kind,data=store[label]; text=logical(store[label])
|
|
answer.append((name,0,kind,len(data),len(text),wire(text),digest(text)))
|
|
elif name == 'VIEW' and len(words) == 3:
|
|
prefix,limit=words[1:]
|
|
token=limit[1:] if limit.startswith('+') else limit
|
|
if not token or not re.fullmatch('[0-9]+',token):
|
|
answer.append(('ERR',-1)); continue
|
|
effective=token.lstrip('0') or '0'
|
|
if len(effective)>2 or int(effective)>16:
|
|
answer.append(('ERR',-1)); continue
|
|
limit=int(effective); prefix='' if prefix=='-' else prefix
|
|
if prefix and not key(prefix):
|
|
answer.append((name,-1,0,0,0,0)); continue
|
|
selected=sorted(k for k in store if k.startswith(prefix))[:limit]
|
|
text=[logical(store[k]) for k in selected]
|
|
aggregate=b''.join(k.encode('ascii')+b'\0'+v+b'\xff' for k,v in zip(selected,text))
|
|
answer.append((name,0,len(selected),sum(map(len,text)),sum(len(k)+2+wire(v) for k,v in zip(selected,text)),digest(aggregate)))
|
|
else:
|
|
answer.append(('ERR',-1))
|
|
return answer
|
|
|
|
def corpus():
|
|
lines=['VIEW - 16','GET absent','DEL absent','COPY absent copy',
|
|
'SET banner C 726561647900aabbccddeeff','GET banner','COPY banner backup','VIEW - 16',
|
|
'SET packet B 4100425c7c00ff','GET packet','VIEW - 16']
|
|
for length in [0,1,2,3,5,7,8,9,15,16,31,63,95,96]:
|
|
data=bytes((index*37+length)%256 for index in range(length))
|
|
lines += ['SET binary B '+(data.hex() or '-'),'GET binary','VIEW b 16']
|
|
for position in sorted({0,length//2,max(0,length-1)}):
|
|
if length:
|
|
text=bytearray(b'Z'*length); text[position]=0
|
|
lines += ['SET string C '+text.hex(),'GET string','COPY string clone','GET clone','VIEW - 4']
|
|
lines += ['RESET']
|
|
for number in reversed(range(16)):
|
|
data=bytes([number,0,255-number,92,124])
|
|
lines += [f'SET k{number:02d} B {data.hex()}']
|
|
lines += ['SET overflow B 0102','COPY k00 overflow','SET k00 C 580059','GET k00',
|
|
'COPY k00 k00','VIEW - 0','VIEW - +00016','VIEW k0 7','VIEW k1 16',
|
|
'DEL k07','SET fresh B -','GET fresh','COPY k01 k02','VIEW - 16',
|
|
'SET bad C 414243','GET bad','SET 1bad B 00','SET k00 Q 00','SET k00 B zz',
|
|
'SET k00 B 0','SET k00 B '+('ab'*97),'GET 1bad','DEL !','COPY k00 !',
|
|
'VIEW ! 3','VIEW - 17','VIEW - -1','VIEW - 1_0','VIEW - +','RESET extra',
|
|
'UNKNOWN','GET','COPY k00','SET k00 C -','VIEW - 16','RESET','VIEW - 16']
|
|
rng=random.Random(731924)
|
|
keys=['x','xA','xB','y','Zed','alpha','alpha_2','Long_key_12345']
|
|
for index in range(160):
|
|
label=rng.choice(keys); action=rng.randrange(6)
|
|
if action<2:
|
|
length=rng.randrange(0,97); data=bytearray(rng.randrange(256) for _ in range(length))
|
|
kind='B' if action==0 else 'C'
|
|
if kind=='C' and length and rng.randrange(5): data[rng.randrange(length)]=0
|
|
lines.append(f'SET {label} {kind} '+(data.hex() or '-'))
|
|
elif action==2: lines.append('GET '+label)
|
|
elif action==3: lines.append('COPY '+label+' '+rng.choice(keys))
|
|
elif action==4: lines.append('DEL '+label)
|
|
else: lines.append('VIEW '+rng.choice(['-','x','alpha','Z'])+' '+str(rng.randrange(17)))
|
|
lines.append('GET '+label)
|
|
lines.append('VIEW - 16')
|
|
# Every possible byte is observed in counted data, in capacity-sized pieces.
|
|
for begin in range(0,256,64):
|
|
lines += ['SET allbytes B '+bytes(range(begin,begin+64)).hex(),'GET allbytes','VIEW all 1']
|
|
lines += ['VIEW\t-\t+00000000000000000000000016\r','\t \r']
|
|
# Additional replacement/copy-independent transactions, not the first diagnostic replay.
|
|
lines.append('RESET')
|
|
for index in range(32):
|
|
data=bytes((index*19+j*11)%256 for j in range((index*7)%97))
|
|
label='stage'+str(index%4)
|
|
lines += ['SET '+label+' B '+(data.hex() or '-'),'GET '+label]
|
|
if index%8==7:
|
|
lines.append('VIEW stage 3')
|
|
lines.append('VIEW - 16')
|
|
return lines
|
|
|
|
def parse_output(data):
|
|
import re
|
|
if any(byte not in b' \t\r\n+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_' for byte in data):
|
|
raise ValueError('invalid stdout byte')
|
|
tokens=re.findall(rb'[^ \t\r\n]+',data)
|
|
rows=[]; cursor=0
|
|
widths={'SET':3,'DEL':3,'COPY':3,'RESET':3,'GET':6,'VIEW':5,'ERR':1}
|
|
while cursor<len(tokens):
|
|
label=tokens[cursor].decode('ascii'); cursor+=1
|
|
if label not in widths:
|
|
raise ValueError('invalid observation label')
|
|
count=widths[label]
|
|
if len(tokens)-cursor<count:
|
|
raise ValueError('short observation')
|
|
numbers=[]
|
|
for token in tokens[cursor:cursor+count]:
|
|
if re.fullmatch(rb'[+-]?[0-9]+',token) is None:
|
|
raise ValueError('invalid decimal output token')
|
|
sign=-1 if token.startswith(b'-') else 1
|
|
digits=(token[1:] if token[:1] in (b'+',b'-') else token).lstrip(b'0') or b'0'
|
|
if len(digits)>10:
|
|
raise ValueError('output value exceeds public observable bounds')
|
|
numbers.append(sign*int(digits))
|
|
cursor+=count
|
|
rows.append(tuple([label]+numbers))
|
|
return rows
|
|
|
|
def main():
|
|
evidence=[]; failures=[]; scratch=None; behavior_ok=False; cleaned=False; absent=False
|
|
try:
|
|
scratch=Path(tempfile.mkdtemp(prefix='protocol-text-extent-'))
|
|
build=scratch/'build'; build.mkdir(); build.chmod(0o777)
|
|
candidate=Path(sys.argv[1])
|
|
for name in ('store.c','extent.c','report.c'):
|
|
(build/name).write_bytes((candidate/name).read_bytes())
|
|
(build/name).chmod(0o644)
|
|
for name,data in SUPPORT.items():
|
|
(build/name).write_text(data); (build/name).chmod(0o644)
|
|
lines=corpus(); expected=model(lines)
|
|
payload=('\n'.join(lines)+'\n').encode('ascii')
|
|
compiler=['gcc','-std=c11','-O2','-Wall','-Wextra','-Werror','-pedantic','store.c','extent.c','report.c','cli.c','-o','observer']
|
|
execute_stage('compiler',compiler,build,COMPILER_DEADLINE,b'',evidence)
|
|
out,err=execute_stage('candidate',[str(build/'observer')],build,CANDIDATE_DEADLINE,payload,evidence)
|
|
actual=parse_output(out)
|
|
if actual!=expected:
|
|
first=next((i for i,(a,b) in enumerate(zip(actual,expected)) if a!=b),min(len(actual),len(expected)))
|
|
raise ValueError('observation mismatch row %d actual=%r expected=%r counts=%r'%(first,actual[first:first+1],expected[first:first+1],(len(actual),len(expected))))
|
|
behavior_ok=True
|
|
except BaseException as error:
|
|
failures.append(['main',type(error).__name__,str(error)])
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/',CLEANUP_DEADLINE,b'',evidence)
|
|
cleaned=True
|
|
except BaseException as error:
|
|
failures.append(['cleanup',type(error).__name__,str(error)])
|
|
try:
|
|
execute_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(scratch)],'/',ABSENCE_DEADLINE,b'',evidence)
|
|
absent=True
|
|
except BaseException as error:
|
|
failures.append(['absence',type(error).__name__,str(error)])
|
|
facts=dict(scratch=None if scratch is None else str(scratch),stages=evidence,
|
|
failures=failures,behavior_ok=behavior_ok,cleaned=cleaned,absent=absent)
|
|
marker=base64.b64encode(bz2.compress(json.dumps(facts,separators=(',',':')).encode())).decode()
|
|
sys.stderr.write('EVIDENCE64:'+marker+'\n')
|
|
passed=behavior_ok and cleaned and absent and not failures
|
|
print('PASS' if passed else 'FAIL')
|
|
return 0 if passed else 1
|
|
|
|
if __name__=='__main__':
|
|
sys.exit(main())
|