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

242 lines
27 KiB
Python

"""Independent list oracle with raw C observations and candidate-identity supervision."""
import base64
import hashlib
import json
import os
from pathlib import Path
import random
import secrets
import subprocess
import sys
import tempfile
import zlib
import lzma
import bz2
import math
SUPPORT = {'segment.h': '#ifndef SEGMENT_H\n#define SEGMENT_H\n#include <stddef.h>\n#include <stdint.h>\n#define ST_MAX_ROWS 32u\n#define ST_MAX_WIDTH 64u\n#define ST_VALUE_LIMIT 1000000\n\ntypedef enum {\n ST_OK = 0,\n ST_INVALID = -1,\n ST_NOMEM = -2,\n ST_RANGE = -3,\n ST_EMPTY = -4\n} StResult;\ntypedef struct {\n void *context;\n void *(*allocate)(void *context, size_t bytes);\n void (*release)(void *context, void *pointer);\n} StAllocator;\ntypedef struct StTable StTable;\ntypedef struct {\n size_t cells;\n int64_t sum;\n int minimum;\n int maximum;\n} StStats;\ntypedef struct {\n size_t row;\n size_t column;\n uint64_t revision;\n} StCursor;\nStResult st_create(StAllocator allocator, StTable **output);\nvoid st_destroy(StTable **table);\nStResult st_clear(StTable *table);\nStResult st_append(StTable *table, unsigned label, const int *values, size_t width);\nStResult st_replace(StTable *table, size_t row, unsigned label, const int *values, size_t width);\nStResult st_erase(StTable *table, size_t first, size_t count);\nStResult st_get(const StTable *table, size_t row, size_t column, int *output);\nStResult st_set(StTable *table, size_t row, size_t column, int value);\nStResult st_shape(const StTable *table, size_t row, unsigned *label, size_t *width);\nStResult st_stats(const StTable *table, size_t first, size_t count, StStats *output);\nStResult st_select(const StTable *table, size_t first, size_t count, int64_t threshold, size_t *output);\nStResult st_rebuild(const StTable *table, size_t first, size_t count, int reverse, StTable **output);\nStResult st_flatten(const StTable *table, int *values, size_t capacity, size_t *needed);\nStResult st_cursor_begin(const StTable *table, StCursor *cursor);\nStResult st_cursor_next(const StTable *table, StCursor *cursor, size_t *row, size_t *column, int *value);\nStResult st_equal(const StTable *left, const StTable *right, int *output);\nStResult st_check(const StTable *table);\nsize_t st_rows(const StTable *table);\nsize_t st_cells(const StTable *table);\nuint64_t st_revision(const StTable *table);\n#endif\n'}
HARNESS = '#include "segment.h"\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\ntypedef struct { void *p; size_t n; } Block;\nstatic Block blocks[20000];\nstatic size_t live, bad, countdown, hit;\nstatic void *acquire(void *ctx, size_t n) {\n size_t i;\n void *p;\n (void)ctx;\n if (countdown != 0 && --countdown == 0) { hit = 1; return NULL; }\n p = malloc(n);\n if (p == NULL) return NULL;\n for (i=0; i<20000; ++i) if (blocks[i].p == NULL) {\n blocks[i].p=p; blocks[i].n=n; ++live; return p;\n }\n free(p); return NULL;\n}\nstatic void release(void *ctx, void *p) {\n size_t i;\n (void)ctx;\n for (i=0; i<20000; ++i) if (p != NULL && blocks[i].p == p) {\n blocks[i].p=NULL; --live; free(p); return;\n }\n ++bad;\n}\nstatic void number(long long n) { printf(",%lld", n); }\nstatic void snapshot(StTable *t) {\n size_t r,c,w=0;\n unsigned label=0;\n int value=0;\n number((long long)st_rows(t)); number((long long)st_cells(t));\n number((long long)st_revision(t));\n for (r=0; r<st_rows(t); ++r) {\n StResult rc=st_shape(t,r,&label,&w);\n number(rc); number(label); number((long long)w);\n for (c=0; c<w; ++c) { rc=st_get(t,r,c,&value); number(rc); number(value); }\n }\n}\nint main(void) {\n StAllocator a={NULL,acquire,release};\n StTable *t=NULL;\n char op;\n while (scanf(" %c",&op)==1) {\n size_t x=0,y=0,z=0,k;\n unsigned label=0;\n int v=0, data[65], reverse=0, rc=0;\n long long threshold=0;\n StStats stats={777,777,777,777};\n size_t result=777, width=777;\n unsigned outlabel=777;\n StTable *copy=NULL;\n printf("[%d",(int)op);\n switch(op) {\n case \'J\': scanf("%zu",&countdown); hit=0; break;\n case \'N\': rc=st_create(a,&t); break;\n case \'D\': st_destroy(&t); st_destroy(&t); break;\n case \'C\': rc=st_clear(t); break;\n case \'A\':\n scanf("%u %zu",&label,&y);\n for(k=0;k<y;++k) scanf("%d",&data[k]);\n rc=st_append(t,label,data,y); break;\n case \'P\':\n scanf("%zu %u %zu",&x,&label,&y);\n for(k=0;k<y;++k) scanf("%d",&data[k]);\n rc=st_replace(t,x,label,data,y); break;\n case \'E\': scanf("%zu %zu",&x,&y); rc=st_erase(t,x,y); break;\n case \'V\': scanf("%zu %zu %d",&x,&y,&v); rc=st_set(t,x,y,v); break;\n case \'Q\':\n scanf("%zu %zu",&x,&y); v=777; rc=st_get(t,x,y,&v); number(v); break;\n case \'H\':\n scanf("%zu",&x); rc=st_shape(t,x,&outlabel,&width);\n number(outlabel); number((long long)width); break;\n case \'S\':\n scanf("%zu %zu",&x,&y); rc=st_stats(t,x,y,&stats);\n number((long long)stats.cells); number(stats.sum); number(stats.minimum); number(stats.maximum); break;\n case \'T\':\n scanf("%zu %zu %lld",&x,&y,&threshold);\n rc=st_select(t,x,y,threshold,&result); number((long long)result); break;\n case \'B\':\n scanf("%zu %zu %d",&x,&y,&reverse);\n rc=st_rebuild(t,x,y,reverse,&copy); snapshot(copy);\n if (copy != NULL) {\n for (k=0;k<st_rows(copy);++k) {\n st_shape(copy,k,&outlabel,&width);\n if (width != 0) { st_set(copy,k,0,1000000); break; }\n }\n }\n st_destroy(&copy); break;\n case \'L\':\n rc=st_rebuild(t,0,st_rows(t),0,&copy); v=777;\n if(rc==0) rc=st_equal(t,copy,&v);\n number(v); st_destroy(&copy); break;\n case \'F\': {\n int flat[2049];\n scanf("%zu",&z);\n for(k=0;k<2049;++k) flat[k]=777;\n rc=st_flatten(t,flat,z,&result); number((long long)result);\n if (rc == 0) for(k=0;k<result;++k) number(flat[k]);\n else for(k=0;k<z;++k) number(flat[k]);\n break;\n }\n case \'I\': {\n StCursor cursor;\n rc=st_cursor_begin(t,&cursor); number(rc);\n if (rc==0) {\n do {\n x=777; y=777; v=777;\n rc=st_cursor_next(t,&cursor,&x,&y,&v);\n number(rc); number((long long)x); number((long long)y); number(v);\n } while(rc==0);\n }\n break;\n }\n case \'U\': {\n StCursor cursor;\n rc=st_cursor_begin(t,&cursor);\n if(rc==0) { st_erase(t,st_rows(t),0); x=777;y=777;v=777;\n rc=st_cursor_next(t,&cursor,&x,&y,&v); number((long long)x);number((long long)y);number(v); }\n break;\n }\n case \'X\': rc=st_check(t); break;\n case \'Z\':\n rc=st_get(t,0,0,NULL); number(rc);\n rc=st_shape(t,0,NULL,&width); number(rc);\n rc=st_stats(t,0,0,NULL); number(rc);\n rc=st_select(t,0,0,0,NULL); number(rc);\n rc=st_rebuild(t,0,0,0,NULL); number(rc);\n rc=st_create(a,NULL); number(rc);\n rc=st_equal(t,t,NULL); number(rc);\n rc=st_flatten(t,NULL,1,&result); number(rc);\n rc=st_cursor_begin(t,NULL); number(rc); break;\n default: return 3;\n }\n number(rc); number((long long)hit); number((long long)bad); number((long long)live);\n snapshot(t); puts("]");\n }\n st_destroy(&t);\n return 0;\n}\n'
EXECUTOR = "import os,sys,json,time,subprocess,selectors,signal,ctypes,base64,hashlib\nrequest=json.loads(sys.stdin.buffer.read())\nstarted=time.monotonic(); errors=[]; tracked={}; reaped=[]; leader=None; leader_rc=None; timed=False\nout=bytearray();err=bytearray(); sel=selectors.DefaultSelector()\ndef proc(pid):\n try:\n s=open('/proc/%d/stat'%pid).read(); tail=s[s.rfind(')')+2:].split()\n return {'pid':pid,'ppid':int(tail[1]),'pgid':int(tail[2]),'start':int(tail[19]),'state':tail[0]}\n except FileNotFoundError: return None\n except ProcessLookupError: return None\n except Exception as e: errors.append('stat:'+repr(e));return None\ndef discover():\n allp={}\n try: names=os.listdir('/proc')\n except Exception as e: errors.append('proc-list:'+repr(e));return\n for n in names:\n if n.isdigit():\n p=proc(int(n))\n if p: allp[p['pid']]=p\n roots={os.getpid()}|{pid for pid,known in tracked.items() if pid in allp and allp[pid]['start']==known['start']}\n changed=True\n while changed:\n changed=False\n for pid,p in allp.items():\n if pid!=os.getpid() and p['ppid'] in roots and pid not in roots:\n roots.add(pid);tracked[pid]=p;changed=True\n for pid,p in list(tracked.items()):\n if pid in allp and allp[pid]['start']==p['start']: tracked[pid]=allp[pid]\ndef alive_tracked():\n alive=[]\n for pid,known in tracked.items():\n current=proc(pid)\n if current is not None and current['start']==known['start']: alive.append(pid)\n return alive\ndef kill_tracked():\n discover()\n for pid,p in list(tracked.items()):\n current=proc(pid)\n if current and current['start']==p['start']:\n try: os.kill(pid,signal.SIGKILL)\n except ProcessLookupError: pass\n except Exception as e: errors.append('kill:'+repr(e))\ndef reap_adopted():\n if leader is not None and leader.poll() is None: return\n while True:\n try: pid,status=os.waitpid(-1,os.WNOHANG)\n except ChildProcessError: break\n except Exception as e: errors.append('reap:'+repr(e));break\n if pid==0: break\n reaped.append([pid,status])\ntry:\n libc=ctypes.CDLL(None,use_errno=True)\n if libc.prctl(36,1,0,0,0)!=0: raise OSError(ctypes.get_errno(),'subreaper')\n leader=subprocess.Popen(request['argv'],cwd=request['cwd'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,start_new_session=True)\n identity=proc(leader.pid)\n if identity is None: raise RuntimeError('leader identity absent')\n tracked[leader.pid]=identity\n pending=memoryview(base64.b64decode(request['input']));written=0\n if pending:\n os.set_blocking(leader.stdin.fileno(),False);sel.register(leader.stdin,selectors.EVENT_WRITE,'i')\n else: leader.stdin.close()\n for stream,tag in [(leader.stdout,'o'),(leader.stderr,'e')]:\n os.set_blocking(stream.fileno(),False);sel.register(stream,selectors.EVENT_READ,tag)\n while True:\n discover()\n leader_rc=leader.poll()\n if time.monotonic()-started>=request['deadline']:\n timed=True;kill_tracked()\n if leader_rc is not None: kill_tracked()\n for key,_ in sel.select(0.01):\n if key.data=='i':\n try: written+=os.write(key.fd,pending[written:written+65536])\n except BlockingIOError: continue\n except BrokenPipeError: written=len(pending)\n if written==len(pending): sel.unregister(key.fileobj);key.fileobj.close()\n continue\n try: data=os.read(key.fd,65536)\n except BlockingIOError: continue\n if data: (out if key.data=='o' else err).extend(data)\n else: sel.unregister(key.fileobj);key.fileobj.close()\n if leader_rc is not None:\n reap_adopted()\n discover()\n alive=alive_tracked()\n if not alive and not sel.get_map(): break\n if time.monotonic()-started>request['deadline']+4:\n errors.append('drain-deadline');break\nexcept BaseException as e: errors.append('execute:'+repr(e))\nfinally:\n kill_tracked()\n if leader is not None:\n try: leader_rc=leader.wait(timeout=2)\n except Exception as e: errors.append('leader-wait:'+repr(e))\n until=time.monotonic()+2\n while time.monotonic()<until:\n reap_adopted();discover()\n alive=alive_tracked()\n if not alive: break\n kill_tracked();time.sleep(0.01)\n remaining=alive_tracked()\n pg_alive=[]\n if leader is not None:\n try: os.killpg(leader.pid,0);pg_alive=[leader.pid]\n except ProcessLookupError: pass\n except Exception as e: errors.append('pg-absence:'+repr(e))\n data={'nonce':request['nonce'],'argv':request['argv'],'deadline':request['deadline'],'uid':os.getuid(),'gid':os.getgid(),'groups':os.getgroups(),'pid':os.getpid(),'elapsed':round(time.monotonic()-started,6),'leader_pid':None if leader is None else leader.pid,'leader_start':None if leader is None else identity['start'],'leader_rc':leader_rc,'leader_reaped':leader is not None and leader.returncode is not None,'timeout':timed,'controlled_rc':124 if timed else leader_rc,'pg_absent':not pg_alive,'descendants_absent':not remaining,'remaining':remaining,'reaped':reaped,'errors':errors,'stdout':base64.b64encode(out).decode(),'stderr':base64.b64encode(err).decode(),'stdout_len':len(out),'stderr_len':len(err),'stdout_sha':hashlib.sha256(out).hexdigest(),'stderr_sha':hashlib.sha256(err).hexdigest()}\n sys.stdout.write(json.dumps(data,separators=(',',':')))\n sys.exit(124 if timed else 0)\n"
DEADLINES = {'compiler':25,'candidate':20,'cleanup':10,'absence':5}
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)"
def scenarios():
events=[['N'],['A',10,2,2,5],['A',20,3,3,4,9],['A',30,1,-1],['S',0,3],['T',0,3,0],['B',0,3,1],['L'],['I'],['F',6],['F',2],['D'],['N'],['A',1,2,3,4],['A',2,1,7],['A',3,0],['T',0,3,0],['T',1,2,0],['T',0,0,-1],['T',0,3,8],['B',0,3,0],['U'],['Z'],['D']]
rng=random.Random(851027)
for cycle in range(8):
events.append(['N'])
for i in range(18):
w=rng.choice([0,1,2,7,64]);values=[rng.randint(-1000000,1000000) for _ in range(w)]
events.append(['A',rng.randrange(2**32),w,*values])
for i in range(15):
row=rng.randrange(18)
events += [['S',row,18-row],['T',0,18,rng.choice([-64000000,0,64000000])],['Q',row,rng.choice([0,63,64])],['H',row],['B',row,18-row,i%2]]
events += [['I'],['L'],['F',2048],['E',3,4],['P',2,88,3,9,-2,5],['V',2,1,1000000],['X'],['C'],['S',0,0],['I'],['D']]
events += [['N']]+[['A',i,0] for i in range(33)]+[['A',0,65,*([1]*65)],['P',0,0,1,1000001],['V',0,0,-1000001],['Q',33,0],['E',33,0],['S',0,33],['B',0,1,2],['D']]
for allocation in range(1,10):
events += [['N'],['A',1,3,1,2,3],['A',2,2,8,9],['J',allocation],['B',0,2,1],['J',0],['D']]
events += [['N'],['A',1,1,7],['J',allocation],['P',0,44,2,11,12],['J',0],['D']]
events += [['N'],['J',allocation],['A',1,2,4,5],['J',0],['D']]
events += [['J',1],['N'],['J',0],['D'],['X'],['Z']]
return events
def snap(table,revision):
out=[len(table),sum(len(v) for _,v in table),revision]
for label,values in table:
out += [0,label,len(values)]
for v in values: out += [0,v]
return out
def judge(events,raw):
# UTF-8 strict is the published numeric stdout protocol; stderr remains arbitrary bytes.
lines=raw.decode('utf-8').splitlines()
if len(lines)!=len(events): return 'observation-count:%d/%d'%(len(lines),len(events))
table=None;revision=0;hit=0
for step,(event,line) in enumerate(zip(events,lines),1):
try: got=json.loads(line)
except Exception: return 'stdout-json:%d'%step
if not isinstance(got,list) or any(type(x) is not int for x in got): return 'stdout-types:%d'%step
op,*a=event; extra=[];rc=0; new=None; newrev=revision
# Extra length may vary only for snapshots; infer base suffix from expected current snapshot.
if op=='J': hit=0
elif op=='N':
rc=-1 if table is not None else 0
if rc==0: new=[];newrev=0
elif op=='D': new=[];newrev=0
elif op=='Z': extra=[-1]*9;rc=-1
elif table is None: rc=-1
elif op=='C': new=[];newrev=revision+1
elif op in ('A','P'):
offset=2 if op=='A' else 3;w=a[offset-1];values=a[offset:]
if w>64 or any(abs(v)>1000000 for v in values): rc=-1
elif op=='P' and a[0]>=len(table) or op=='A' and len(table)==32: rc=-3
else:
new=[(label,list(v)) for label,v in table]
row=(a[0] if op=='A' else a[1],list(values))
if op=='A': new.append(row)
else: new[a[0]]=row
newrev=revision+1
elif op in ('E','S','T','B'):
first,count=a[:2]
if op=='B' and a[2] not in (0,1): rc=-1
elif first>len(table) or count>len(table)-first: rc=-3
elif op=='E': new=table[:first]+table[first+count:];newrev=revision+1
elif op=='S':
values=[x for _,v in table[first:first+count] for x in v]
extra=[len(values),sum(values),min(values,default=0),max(values,default=0)]
elif op=='T':
possible=[i for i in range(first,first+count) if sum(table[i][1])>=a[2]]
if possible: extra=[max(possible,key=lambda i:sum(table[i][1]))]
else: rc=-4;extra=[777]
else:
copy=table[first:first+count]
if a[2]:copy=copy[::-1]
extra=snap(copy,0)
elif op in ('V','Q'):
r,c=a[:2]
if op=='V' and abs(a[2])>1000000: rc=-1
elif r>=len(table) or c>=len(table[r][1]): rc=-3
elif op=='V':
new=[(label,list(v)) for label,v in table];new[r][1][c]=a[2];newrev=revision+1
else:extra=[table[r][1][c]]
elif op=='H':
if a[0]>=len(table): rc=-3;extra=[777,777]
else:extra=[table[a[0]][0],len(table[a[0]][1])]
elif op=='F':
values=[x for _,v in table for x in v]
if a[0]<len(values):rc=-3;extra=[len(values)]+[777]*a[0]
else:extra=[len(values)]+values
elif op=='I':
extra=[0]
for r,(_,values) in enumerate(table):
for c,v in enumerate(values):extra += [0,r,c,v]
extra += [-4,777,777,777];rc=-4
elif op=='U':extra=[777,777,777];rc=-1;newrev=revision+1
elif op=='L':extra=[1]
elif op=='X':pass
else:return 'oracle-unknown-op'
if rc!=0 and op=='Q':extra=[777]
if rc!=0 and op=='S':extra=[777]*4
if rc!=0 and op=='T' and not extra:extra=[777]
if rc!=0 and op=='B':extra=snap([],0)
current=table or []
intended=new if new is not None else current
intended_rev=newrev
# The suffix schema is independent of allocator layout. Detect an observed NOMEM using
# both its exact position and a real one-shot allocator fault reported by the raw adapter.
wanted_prefix=[ord(op)]+extra
position=len(wanted_prefix)
allocating=op in ('N','A','P','B','L') and rc==0
if allocating:
observed=got[position:position+4]
if op=='B':
position_bad=1+len(snap([],0));observed_bad=got[position_bad:position_bad+4]
elif op=='L':position_bad=2;observed_bad=got[position_bad:position_bad+4]
else:position_bad=position;observed_bad=observed
if len(observed_bad)==4 and observed_bad[0]==-2 and observed_bad[1]==1:
rc=-2;new=None;intended=current;intended_rev=revision
if op=='B':extra=snap([],0)
if op=='L':extra=[777]
position=1+len(extra)
head=got[position:position+4]
if len(head)!=4:return 'metadata-shape:%d'%step
actual_rc,actual_hit,bad,live=head
if actual_hit not in (0,1) or (hit==1 and op!='J' and actual_hit!=1):return 'allocator-hit:%d'%step
hit=actual_hit
if bad!=0 or live<0:return 'allocator-invalid-release:%d'%step
if op=='D' and live!=0:return 'resources-live:%d:%d'%(step,live)
wanted=[ord(op)]+extra+[rc,hit,0,live]+snap(intended,intended_rev)
if got!=wanted:return 'behavior:%d:%s:got=%s:expected=%s'%(step,op,str(got)[:180],str(wanted)[:180])
if op=='D':table=None;revision=0
elif op=='N' and rc==0:table=[];revision=0
elif table is not None:table=intended;revision=intended_rev
return None
def pack(value):
for record in value['records']:
if 'raw_wire' not in record: continue
wire=base64.b64decode(record.pop('raw_wire'))
data=json.loads(wire)
streams=[base64.b64decode(data.pop(k)) for k in ('stdout','stderr')]
# Lossless structured encoding avoids expanding repetitive stderr through two base64 layers.
# Preserve exact actual keys and order; verify byte-for-byte reconstruction before logging.
rebuilt=dict(data)
original=json.loads(wire)
for k,v in zip(('stdout','stderr'),streams): original[k]=base64.b64encode(v).decode()
if json.dumps(original,separators=(',',':')).encode()!=wire: raise RuntimeError('wire-codec-roundtrip')
record['raw_wire_parts']={'skeleton':base64.b64encode(wire.replace(('\"stdout\":\"'+json.loads(wire)['stdout']+'\"').encode(),b'\"stdout\":\"@@STDOUT@@\"',1).replace(('\"stderr\":\"'+json.loads(wire)['stderr']+'\"').encode(),b'\"stderr\":\"@@STDERR@@\"',1)).decode(),'streams':[base64.b64encode(bz2.compress(v)).decode() for v in streams]}
raw=json.dumps(value,separators=(',',':')).encode()
choices=[('X',lzma.compress(raw,preset=6)),('B',bz2.compress(raw,compresslevel=9))]
codec,data=min(choices,key=lambda pair:len(pair[1]))
return codec+base64.b85encode(data).decode()
records=[]
def run_stage(stage,argv,cwd,input_bytes=b''):
nonce=secrets.token_hex(12)
request={'nonce':nonce,'argv':argv,'cwd':cwd,'input':base64.b64encode(input_bytes).decode(),'deadline':DEADLINES[stage]}
wrapped=subprocess.run([sys.executable,'-I','-c',EXECUTOR],input=json.dumps(request).encode(),capture_output=True)
wire=wrapped.stdout
# Keep the actual wrapper facts even if its wire is absent, malformed, or intentionally incomplete.
record={'stage':stage,'wrapper_rc':wrapped.returncode,'wire_len':len(wire),'wire_sha':hashlib.sha256(wire).hexdigest(),'wrapper_stderr_len':len(wrapped.stderr),'wrapper_stderr_sha':hashlib.sha256(wrapped.stderr).hexdigest()}
records.append(record)
if globals().get('RAW_WIRE',False): record['raw_wire']=base64.b64encode(wire).decode()
data=json.loads(wire)
required={'nonce','argv','deadline','uid','gid','groups','pid','elapsed','leader_pid','leader_start','leader_rc','leader_reaped','timeout','controlled_rc','pg_absent','descendants_absent','remaining','reaped','errors','stdout','stderr','stdout_len','stderr_len','stdout_sha','stderr_sha'}
if not required<=data.keys():raise RuntimeError(stage+':missing-metadata')
# Store all metadata. Raw streams are captured and bound by actual bytes; they are not claimed
# retained in the 4000-byte Bos stderr tail. Diagnostic copies can enable RAW_WIRE transport.
record['meta']={k:v for k,v in data.items() if k not in ('stdout','stderr')}
stdout=base64.b64decode(data['stdout'],validate=True);stderr=base64.b64decode(data['stderr'],validate=True)
for name,value in [('stdout',stdout),('stderr',stderr)]:
if data[name+'_len']!=len(value) or data[name+'_sha']!=hashlib.sha256(value).hexdigest():raise RuntimeError(stage+':raw-binding')
identity=(65534,65534,[]) if os.environ.get('ROLLOUT_GRADE_ISOLATION')=='uid65534' else (os.getuid(),os.getgid(),os.getgroups())
integer_fields=('uid','gid','pid','leader_pid','leader_start','leader_rc','controlled_rc','stdout_len','stderr_len')
if any(type(data[k]) is not int for k in integer_fields):raise RuntimeError(stage+':integer-fields')
if data['pid']<=0 or data['leader_pid']<=0 or data['leader_start']<=0 or data['pid']==data['leader_pid']:raise RuntimeError(stage+':identity-fields')
if type(data['elapsed']) not in (int,float) or not math.isfinite(data['elapsed']) or not 0<=data['elapsed']<=DEADLINES[stage]+8:raise RuntimeError(stage+':elapsed')
if type(data['timeout']) is not bool:raise RuntimeError(stage+':timeout-type')
if data['controlled_rc']!=(124 if data['timeout'] else data['leader_rc']):raise RuntimeError(stage+':controlled-rc')
if wrapped.returncode!=(124 if data['timeout'] else 0) or wrapped.stderr or data['nonce']!=nonce or data['argv']!=argv or data['deadline']!=DEADLINES[stage]:raise RuntimeError(stage+':wrapper')
if (data['uid'],data['gid'],data['groups'])!=identity:raise RuntimeError(stage+':identity')
if not isinstance(data['errors'],list) or not isinstance(data['remaining'],list) or not isinstance(data['reaped'],list):raise RuntimeError(stage+':list-fields')
if data['leader_reaped'] is not True or data['pg_absent'] is not True or data['descendants_absent'] is not True or data['remaining'] or data['errors'] or data['timeout'] is not False:raise RuntimeError(stage+':execution')
if data['leader_rc']!=0:raise RuntimeError(stage+':leader-rc:'+str(data['leader_rc']))
return stdout
def main():
root=None;failure=None;exceptions=[];business_ok=False;cleanup_ok=False;absence_ok=False
try:
root=Path(tempfile.mkdtemp(prefix='segment-table-'))
build=root/'build';build.mkdir(mode=0o777);os.chmod(build,0o777)
# Only trusted parent-owned support is prepared before the first child.
for name,text in SUPPORT.items():(build/name).write_text(text)
(build/'raw.c').write_text(HARNESS)
(build/'segment.c').write_bytes((Path(sys.argv[1])/'segment.c').read_bytes())
events=scenarios();input_bytes=('\n'.join(' '.join(map(str,e)) for e in events)+'\n').encode()
compile_argv=['gcc','-std=c11','-O2','-Wall','-Wextra','-pedantic','segment.c','raw.c','-o','observe']
run_stage('compiler',compile_argv,str(build))
raw=run_stage('candidate',[str(build/'observe')],str(build),input_bytes)
failure=judge(events,raw)
business_ok=failure is None
except BaseException as error:
exceptions.append('main:'+repr(error));failure=failure or 'execution-failure'
finally:
if root is not None:
try:
run_stage('cleanup',[sys.executable,'-I','-c',CLEANUP,str(root)],'/tmp')
cleanup_ok=True
except BaseException as error:exceptions.append('cleanup:'+repr(error))
try:
run_stage('absence',[sys.executable,'-I','-c',ABSENCE,str(root)],'/tmp')
absence_ok=True
except BaseException as error:exceptions.append('absence:'+repr(error))
evidence={'records':records,'exceptions':exceptions,'business_ok':business_ok,'cleanup_ok':cleanup_ok,'absence_ok':absence_ok,'failure':failure}
encoded=pack(evidence)
sys.stderr.write('SEGMENT_EVIDENCE_C='+encoded+'\n')
if business_ok and cleanup_ok and absence_ok and not exceptions:
print('PASS');return 0
print('FAIL '+str(failure or exceptions));return 1
if __name__=='__main__':sys.exit(main())