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>
416 lines
26 KiB
Python
416 lines
26 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[pid]=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,
|
|
'adopted':[[p,None,status] for p,status in waits.items()],
|
|
'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],
|
|
'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',
|
|
'stdout_len','stdout_sha256','stderr_len','stderr_sha256','adopted'}
|
|
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'):
|
|
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 and type(row[3]) is not int):
|
|
raise ValueError('invalid descendant record')
|
|
for row in meta['adopted']:
|
|
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 identity')
|
|
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())
|
|
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
|
|
|
|
FIXED_SUPPORT = {'include/linkbuf.h': '#ifndef LINKBUF_H\n#define LINKBUF_H\n#include <stddef.h>\n#include <stdio.h>\n#include <sys/types.h>\n#define LB_MAX_NODES 64\n#define LB_MAX_QUERIES 128\n#define LB_MAX_PATH 128\n#define LB_MAX_TARGET 512\n#define LB_MAX_ROWS 512\n#define LB_LINE 1400\n\ntypedef enum {\n LB_LINK = 1,\n LB_FILE = 2\n} lb_kind;\ntypedef enum {\n LB_OK = 0,\n LB_TRUNCATED = 1,\n LB_MISSING = 2,\n LB_NOTLINK = 3,\n LB_NOMEM = 4\n} lb_status;\ntypedef struct {\n char path[LB_MAX_PATH];\n lb_kind kind;\n unsigned char target[LB_MAX_TARGET];\n size_t length;\n} lb_node;\ntypedef struct {\n size_t initial_capacity;\n size_t maximum_capacity;\n size_t repetitions;\n size_t node_count;\n size_t query_count;\n lb_node nodes[LB_MAX_NODES];\n char queries[LB_MAX_QUERIES][LB_MAX_PATH];\n} lb_graph;\ntypedef struct {\n char path[LB_MAX_PATH];\n lb_status status;\n unsigned char target[LB_MAX_TARGET];\n size_t length;\n} lb_row;\ntypedef struct {\n size_t row_count;\n lb_row rows[LB_MAX_ROWS];\n size_t total_length;\n size_t ok_count;\n size_t truncated_count;\n size_t missing_count;\n size_t notlink_count;\n size_t nomem_count;\n unsigned long checksum;\n} lb_report;\ntypedef struct {\n size_t allocations;\n size_t releases;\n size_t live_blocks;\n size_t live_bytes;\n size_t peak_bytes;\n size_t read_calls;\n size_t bad_releases;\n} lb_metrics;\n\nint lb_normalize(const char *input, char output[LB_MAX_PATH]);\nint lb_load(FILE *stream, lb_graph *graph, char *error, size_t size);\nconst lb_node *lb_find(const lb_graph *graph, const char *path);\nvoid lb_sort_nodes(lb_graph *graph);\nint lb_collect(const lb_graph *graph, lb_report *report);\nvoid lb_summarize(lb_report *report);\nconst char *lb_status_name(lb_status status);\nunsigned long lb_target_checksum(const unsigned char *data, size_t length);\nvoid lb_port_begin(const lb_graph *graph);\nvoid *lb_port_allocate(size_t size);\nvoid lb_port_release(void *pointer);\nssize_t lb_port_readlink(const char *path, void *buffer, size_t capacity);\nlb_metrics lb_port_metrics(void);\nvoid lb_port_end(void);\n#endif\n', 'support/port.c': '#include "linkbuf.h"\n#include <errno.h>\n#include <stdlib.h>\n#include <string.h>\n#define PORT_SLOTS 8192\n\ntypedef struct {\n void *pointer;\n size_t size;\n} port_slot;\nstatic port_slot slots[PORT_SLOTS];\nstatic const lb_graph *active_graph;\nstatic lb_metrics metrics;\n\nvoid lb_port_begin(const lb_graph *graph)\n{\n memset(slots, 0, sizeof(slots));\n memset(&metrics, 0, sizeof(metrics));\n active_graph = graph;\n}\n\nvoid *lb_port_allocate(size_t size)\n{\n void *pointer;\n size_t index = 0;\n while (index < PORT_SLOTS && slots[index].pointer != NULL) {\n index++;\n }\n if (index == PORT_SLOTS || size == 0 || size > LB_MAX_TARGET) {\n return NULL;\n }\n pointer = malloc(size);\n if (pointer == NULL) {\n return NULL;\n }\n memset(pointer, 0xa5, size);\n slots[index].pointer = pointer;\n slots[index].size = size;\n metrics.allocations++;\n metrics.live_blocks++;\n metrics.live_bytes += size;\n if (metrics.live_bytes > metrics.peak_bytes) {\n metrics.peak_bytes = metrics.live_bytes;\n }\n return pointer;\n}\n\nvoid lb_port_release(void *pointer)\n{\n if (pointer == NULL) {\n return;\n }\n for (size_t i = 0; i < PORT_SLOTS; i++) {\n if (slots[i].pointer == pointer) {\n metrics.releases++;\n metrics.live_blocks--;\n metrics.live_bytes -= slots[i].size;\n free(pointer);\n slots[i].pointer = NULL;\n slots[i].size = 0;\n return;\n }\n }\n metrics.bad_releases++;\n}\n\nssize_t lb_port_readlink(const char *path, void *buffer, size_t capacity)\n{\n const lb_node *node;\n size_t count;\n metrics.read_calls++;\n node = lb_find(active_graph, path);\n if (node == NULL) {\n errno = ENOENT;\n return -1;\n }\n if (node->kind != LB_LINK) {\n errno = EINVAL;\n return -1;\n }\n count = node->length < capacity ? node->length : capacity;\n memcpy(buffer, node->target, count);\n return (ssize_t)count;\n}\n\nlb_metrics lb_port_metrics(void)\n{\n return metrics;\n}\n\nvoid lb_port_end(void)\n{\n for (size_t i = 0; i < PORT_SLOTS; i++) {\n if (slots[i].pointer != NULL) {\n free(slots[i].pointer);\n slots[i].pointer = NULL;\n }\n }\n active_graph = NULL;\n}\n', 'support/observe.c': '#include "linkbuf.h"\n#include <stdlib.h>\n\nstatic void hex(const unsigned char *data, size_t size)\n{\n static const char digits[] = "0123456789abcdef";\n putchar(\'"\');\n for (size_t i = 0; i < size; i++) {\n putchar(digits[data[i] >> 4]);\n putchar(digits[data[i] & 15]);\n }\n putchar(\'"\');\n}\n\nstatic void observe(const lb_report *report, lb_metrics m, int rc)\n{\n printf("{\\"rc\\":%d,\\"rows\\":[", rc);\n for (size_t i = 0; i < report->row_count; i++) {\n const lb_row *row = &report->rows[i];\n if (i) {\n putchar(\',\');\n }\n putchar(\'[\');\n hex((const unsigned char *)row->path, __builtin_strlen(row->path));\n printf(",%d,%zu,", row->status, row->length);\n hex(row->target, row->length);\n putchar(\']\');\n }\n printf("],\\"totals\\":[%zu,%zu,%zu,%zu,%zu,%zu,%lu]",\n report->total_length, report->ok_count, report->truncated_count,\n report->missing_count, report->notlink_count, report->nomem_count,\n report->checksum);\n printf(",\\"resources\\":[%zu,%zu,%zu,%zu,%zu,%zu,%zu]}\\n",\n m.allocations, m.releases, m.live_blocks, m.live_bytes,\n m.peak_bytes, m.read_calls, m.bad_releases);\n}\n\nint main(void)\n{\n static lb_graph graph;\n static lb_report report;\n char error[128];\n int loaded;\n while ((loaded = lb_load(stdin, &graph, error, sizeof(error))) > 0) {\n int rc;\n lb_port_begin(&graph);\n rc = lb_collect(&graph, &report);\n observe(&report, lb_port_metrics(), rc);\n lb_port_end();\n }\n if (loaded < 0) {\n fprintf(stderr, "input: %s\\n", error);\n return 65;\n }\n return 0;\n}\n'}
|
|
|
|
CANDIDATE_FILES = ('src/graph.c','src/inventory.c','src/summary.c')
|
|
COMPILE_SECONDS = 12
|
|
CANDIDATE_SECONDS = 8
|
|
CLEANUP_SECONDS = 5
|
|
ABSENCE_SECONDS = 5
|
|
|
|
def normalize(path):
|
|
parts = []
|
|
for part in path.split('/'):
|
|
if part == '..':
|
|
if parts: parts.pop()
|
|
elif part and part != '.': parts.append(part)
|
|
return '/' + '/'.join(parts)
|
|
|
|
def build_cases():
|
|
cases = []
|
|
def add(initial, maximum, repetitions, nodes, queries):
|
|
cases.append(dict(initial=initial,maximum=maximum,repetitions=repetitions,nodes=nodes,queries=queries))
|
|
add(64,256,2,[('LINK','/bin/tool',b'bin/tool-v2'),('LINK','/etc/current',b'config-2026'),('LINK','/run/active',b'worker'),('FILE','/etc/plain',b'not a link')],['/bin/tool','/etc/current','/run/active','/etc/plain','/gone'])
|
|
add(4,32,1,[('LINK','/selected',b'abcdefghijkl'),('LINK','/exact',b'ABCD')],['/selected','/exact'])
|
|
add(1,1,4,[('LINK','/empty',b''),('LINK','/one',b'x')],['/one','/empty','/no'])
|
|
add(3,19,3,[],[])
|
|
add(2,16,2,[('FILE','/',b'')],['/','/a/../','/missing'])
|
|
rng = random.Random(202610250117)
|
|
for index in range(27):
|
|
maximum = [1,2,3,7,16,31,64,127,256,512][index % 10]
|
|
initial = rng.randint(1, maximum)
|
|
lengths = sorted(set([0,1,initial-1,initial, min(512,initial+1),maximum-1,maximum,min(512,maximum+1),512]))
|
|
nodes=[]; queries=[]
|
|
for number,length in enumerate(lengths):
|
|
name='/root/n%02d' % number
|
|
value=bytes(rng.randrange(256) for _ in range(length))
|
|
nodes.append(('LINK',name,value))
|
|
queries.append('/root//branch/.././n%02d/' % number)
|
|
nodes.append(('FILE','/regular',b'file-data'))
|
|
queries += ['/regular','/missing','/../../root/n00','/root/n00']
|
|
rng.shuffle(nodes)
|
|
add(initial,maximum,1+index%4,nodes,queries)
|
|
return cases
|
|
|
|
def serialize(cases):
|
|
lines=[]
|
|
for index,case in enumerate(cases):
|
|
initial,maximum,repetitions=(case[k] for k in ('initial','maximum','repetitions'))
|
|
lines += ['LINKGRAPH 1','LIMITS +000%d 00%d %d' % (initial,maximum,repetitions)]
|
|
for kind,path,data in case['nodes']:
|
|
value=data.hex() or '-'
|
|
if index%2: value=value.upper()
|
|
lines.append('%s\t%s %s' % (kind,path,value))
|
|
lines += ['QUERY '+query for query in case['queries']]
|
|
lines += ['END','']
|
|
return ('\n'.join(lines)+'\n').encode('ascii')
|
|
|
|
def oracle(case):
|
|
nodes={normalize(path):(kind,data) for kind,path,data in case['nodes']}
|
|
rows=[]; totals=[0,0,0,0,0,0,0]
|
|
for _ in range(case['repetitions']):
|
|
for query in case['queries']:
|
|
path=normalize(query); node=nodes.get(path)
|
|
if node is None: status,data=2,b''
|
|
elif node[0]=='FILE': status,data=3,b''
|
|
else:
|
|
data=node[1][:case['maximum']]
|
|
status=1 if len(node[1])>=case['maximum'] else 0
|
|
state=0
|
|
for byte in data: state=(state*33+byte)%65521
|
|
totals[0]+=len(data); totals[1+status]+=1; totals[6]=(totals[6]+state)%65521
|
|
rows.append([path.encode('ascii').hex(),status,len(data),data.hex()])
|
|
return dict(rc=0,rows=rows,totals=totals)
|
|
|
|
def decode_objects(raw):
|
|
text = raw.decode('utf-8')
|
|
decoder = json.JSONDecoder()
|
|
position = 0; result = []
|
|
while position < len(text):
|
|
while position < len(text) and text[position] in ' \t\r\n': position += 1
|
|
if position == len(text): break
|
|
value,position = decoder.raw_decode(text,position)
|
|
result.append(value)
|
|
return result
|
|
|
|
def verify_observations(raw, cases):
|
|
observations = decode_objects(raw)
|
|
if len(observations) != len(cases):
|
|
raise ValueError('document count differs: %d != %d' % (len(observations),len(cases)))
|
|
for number,(actual,case) in enumerate(zip(observations,cases)):
|
|
expected = oracle(case)
|
|
if type(actual) is not dict or set(actual) != {'rc','rows','totals','resources'}:
|
|
raise ValueError('document %d structure' % number)
|
|
if type(actual['rc']) is not int or type(actual['rows']) is not list or type(actual['totals']) is not list:
|
|
raise ValueError('document %d types' % number)
|
|
if len(actual['totals']) != 7 or any(type(v) is not int for v in actual['totals']):
|
|
raise ValueError('document %d totals type' % number)
|
|
for row in actual['rows']:
|
|
if type(row) is not list or len(row)!=4 or type(row[0]) is not str or type(row[1]) is not int or type(row[2]) is not int or type(row[3]) is not str:
|
|
raise ValueError('document %d row type' % number)
|
|
for key in ('rc','rows','totals'):
|
|
if actual[key] != expected[key]:
|
|
raise ValueError('document %d %s first observation difference: actual=%r expected=%r' % (number,key,str(actual[key])[:300],str(expected[key])[:300]))
|
|
metrics=actual['resources']
|
|
if type(metrics) is not list or len(metrics)!=7 or any(type(v) is not int or v<0 for v in metrics):
|
|
raise ValueError('document %d resource structure' % number)
|
|
if metrics[0] != metrics[1] or metrics[2] or metrics[3] or metrics[6]:
|
|
raise ValueError('document %d outstanding resources: %r' % (number,metrics))
|
|
return len(observations)
|
|
|
|
def emit_evidence(evidence, failures, checks):
|
|
# shortcut: bos retains only 4000 stderr characters; retain full metadata compressed,
|
|
# plus actual wire/IO nSHA. Original wire and raw IO bodies are not persisted here.
|
|
data=dict(stages=evidence,exceptions=failures,checks=checks,raw_bodies_stored=False)
|
|
packed=base64.b64encode(bz2.compress(json.dumps(data,separators=(',',':')).encode())).decode('ascii')
|
|
print('RLBMETA '+packed,file=sys.stderr)
|
|
|
|
def main():
|
|
evidence=[]; failures=[]; checks=0
|
|
business_ok=False; cleanup_ok=False; absence_ok=False
|
|
scratch=None
|
|
try:
|
|
scratch=Path(tempfile.mkdtemp(prefix='resource-link-buffer-'))
|
|
work=scratch/'build'
|
|
work.mkdir(mode=0o777)
|
|
os.chmod(work,0o777)
|
|
for name,text in FIXED_SUPPORT.items():
|
|
destination=work/name
|
|
destination.parent.mkdir(parents=True,exist_ok=True)
|
|
destination.write_text(text)
|
|
candidate=Path(sys.argv[1])
|
|
for name in CANDIDATE_FILES:
|
|
destination=work/name
|
|
destination.parent.mkdir(parents=True,exist_ok=True)
|
|
destination.write_bytes((candidate/name).read_bytes())
|
|
# Control-owned support subdirectories need candidate traversal before first child.
|
|
for folder in (work/'src',work/'include',work/'support'):
|
|
os.chmod(folder,0o777)
|
|
cases=build_cases(); payload=serialize(cases)
|
|
compiler=['gcc','-std=c11','-O2','-Wall','-Wextra','-Wpedantic','-Iinclude',
|
|
'src/graph.c','src/inventory.c','src/summary.c','support/port.c','support/observe.c','-o','link-inventory']
|
|
execute_stage('compiler',compiler,work,COMPILE_SECONDS,b'',evidence)
|
|
out,err=execute_stage('candidate',[str(work/'link-inventory')],work,CANDIDATE_SECONDS,payload,evidence)
|
|
checks=verify_observations(out,cases)
|
|
business_ok=True
|
|
except BaseException as error:
|
|
failures.append(['business',type(error).__name__,str(error)])
|
|
finally:
|
|
if scratch is not None:
|
|
try:
|
|
execute_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(scratch)],'/tmp',CLEANUP_SECONDS,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_SECONDS,b'',evidence)
|
|
absence_ok=True
|
|
except BaseException as error:
|
|
failures.append(['absence',type(error).__name__,str(error)])
|
|
emit_evidence(evidence,failures,checks)
|
|
if business_ok and cleanup_ok and absence_ok and not failures:
|
|
print('PASS'); return 0
|
|
print('FAIL '+json.dumps(failures,separators=(',',':'))[:260]); return 1
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|