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>
464 lines
37 KiB
Python
464 lines
37 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_waits':[[p,None,rc] for p,rc in waits.items()],
|
|
'descendants':[[p,t['start'],signals.get((p,t['start']),0)]
|
|
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_waits'}
|
|
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) != 3 or not all(identity_int(x,1) for x in row[:2]) or not identity_int(row[2]):
|
|
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())
|
|
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 = {'include/reverse.h': '#ifndef REVERSE_H\n#define REVERSE_H\n#include <stddef.h>\n#include <stdint.h>\n#define LOG_LIMIT 64\n#define BANK_COUNT 8\n#define CURSOR_SLOTS 2\n#define STEP_DONE 0\n#define STEP_BUDGET 1\n#define STEP_OUTPUT 2\n#define STEP_PORT 3\n#define INVALID_ARGUMENT (-1)\ntypedef struct {\n uint64_t sequence;\n unsigned bank;\n unsigned level;\n int64_t value;\n} Record;\ntypedef struct {\n Record cells[LOG_LIMIT];\n size_t capacity;\n size_t count;\n size_t head;\n uint64_t next_sequence;\n uint64_t overwritten;\n} Journal;\ntypedef struct {\n unsigned bank_mask;\n unsigned minimum_level;\n int64_t minimum_value;\n int64_t maximum_value;\n} Selection;\ntypedef struct {\n uint64_t count;\n int64_t sum;\n int64_t minimum;\n int64_t maximum;\n uint64_t levels[4];\n uint64_t banks[BANK_COUNT];\n uint64_t fingerprint;\n} Summary;\ntypedef struct {\n Record snapshot[LOG_LIMIT];\n size_t snapshot_count;\n size_t low;\n size_t high;\n size_t next;\n int initialized;\n int done;\n Selection selection;\n uint64_t examined;\n uint64_t emitted;\n uint64_t invalid_reads;\n size_t route[LOG_LIMIT];\n size_t route_count;\n size_t route_position;\n} Cursor;\ntypedef struct {\n int reason;\n size_t scanned;\n size_t emitted;\n size_t port_calls;\n Record rows[LOG_LIMIT];\n} Batch;\ntypedef struct {\n int initialized;\n int done;\n size_t snapshot_count;\n uint64_t examined;\n uint64_t emitted;\n uint64_t invalid_reads;\n} CursorInfo;\nint journal_init(Journal *journal, size_t capacity);\nint journal_append(Journal *journal, unsigned bank, unsigned level, int64_t value, uint64_t *sequence);\nint journal_read(const Journal *journal, size_t ordinal, Record *record);\nint journal_copy(const Journal *journal, Record *rows, size_t capacity, size_t *count);\nint selection_valid(const Selection *selection);\nint selection_accepts(const Selection *selection, const Record *record);\nvoid summary_clear(Summary *summary);\nvoid summary_add(Summary *summary, const Record *record);\nint journal_summary(const Journal *journal, const Selection *selection, Summary *summary);\nvoid cursor_clear(Cursor *cursor);\nint cursor_open(Cursor *cursor, const Journal *journal, size_t low, size_t high, const Selection *selection);\nint cursor_step(Cursor *cursor, size_t budget, size_t output_capacity, Batch *batch);\nint cursor_info(const Cursor *cursor, CursorInfo *info);\n#endif\n', 'src/summary.c': '#include "reverse.h"\n#include <string.h>\nvoid summary_clear(Summary *summary)\n{\n if (summary != NULL) {\n memset(summary, 0, sizeof(*summary));\n }\n}\nvoid summary_add(Summary *summary, const Record *record)\n{\n uint64_t term;\n if (summary == NULL || record == NULL) {\n return;\n }\n if (summary->count == 0) {\n summary->minimum = record->value;\n summary->maximum = record->value;\n } else {\n if (record->value < summary->minimum) {\n summary->minimum = record->value;\n }\n if (record->value > summary->maximum) {\n summary->maximum = record->value;\n }\n }\n summary->count++;\n summary->sum += record->value;\n summary->levels[record->level]++;\n summary->banks[record->bank]++;\n term = record->sequence * UINT64_C(131);\n term += (uint64_t)record->bank * UINT64_C(17);\n term += (uint64_t)record->level * UINT64_C(5);\n term += (uint64_t)(record->value + 1000000);\n summary->fingerprint = summary->fingerprint * UINT64_C(65599) + term;\n}\nint journal_summary(const Journal *journal, const Selection *selection, Summary *summary)\n{\n size_t index;\n Record record;\n Summary result;\n if (journal == NULL || summary == NULL || !selection_valid(selection)) {\n return INVALID_ARGUMENT;\n }\n summary_clear(&result);\n for (index = 0; index < journal->count; index++) {\n if (journal_read(journal, index, &record) != 0) {\n return INVALID_ARGUMENT;\n }\n if (selection_accepts(selection, &record)) {\n summary_add(&result, &record);\n }\n }\n *summary = result;\n return 0;\n}\n', 'src/observer.c': '#include "reverse.h"\n#include <inttypes.h>\n#include <limits.h>\n#include <stdio.h>\n#include <string.h>\nstatic int separator(int ch)\n{\n return ch == \' \' || ch == \'\\t\' || ch == \'\\r\' || ch == \'\\n\';\n}\nstatic int next_nonseparator(void)\n{\n int ch;\n do {\n ch = getchar();\n } while (separator(ch));\n return ch;\n}\nstatic int number(int64_t *value)\n{\n int ch = next_nonseparator();\n int negative = 0;\n int digits = 0;\n int invalid = 0;\n uint64_t magnitude = 0;\n uint64_t maximum;\n if (ch == \'+\' || ch == \'-\') {\n negative = ch == \'-\';\n ch = getchar();\n }\n maximum = negative ? (uint64_t)INT64_MAX + 1 : (uint64_t)INT64_MAX;\n while (ch != EOF && !separator(ch)) {\n if (ch < \'0\' || ch > \'9\') {\n invalid = 1;\n } else {\n unsigned digit = (unsigned)(ch - \'0\');\n digits = 1;\n if (magnitude > (maximum - digit) / 10) {\n invalid = 1;\n } else if (!invalid) {\n magnitude = magnitude * 10 + digit;\n }\n }\n ch = getchar();\n }\n if (!digits || invalid) {\n return -1;\n }\n if (negative && magnitude == (uint64_t)INT64_MAX + 1) {\n *value = INT64_MIN;\n } else {\n *value = negative ? -(int64_t)magnitude : (int64_t)magnitude;\n }\n return 0;\n}\nstatic int arguments(int64_t *values, size_t count)\n{\n size_t index;\n for (index = 0; index < count; index++) {\n if (number(&values[index]) != 0) {\n return -1;\n }\n }\n return 0;\n}\nstatic size_t index_value(int64_t value)\n{\n return value < 0 ? SIZE_MAX : (size_t)value;\n}\nstatic unsigned unsigned_value(int64_t value)\n{\n if (value < 0 || (uint64_t)value > UINT_MAX) {\n return UINT_MAX;\n }\n return (unsigned)value;\n}\nstatic Selection make_selection(const int64_t *values)\n{\n Selection result;\n result.bank_mask = unsigned_value(values[0]);\n result.minimum_level = unsigned_value(values[1]);\n result.minimum_value = values[2];\n result.maximum_value = values[3];\n return result;\n}\nstatic void print_record(const Record *record)\n{\n printf(" %" PRIu64 " %u %u %" PRId64,\n record->sequence, record->bank, record->level, record->value);\n}\nstatic void print_info(const Cursor *cursor)\n{\n CursorInfo info;\n cursor_info(cursor, &info);\n printf(" %d %d %zu %" PRIu64 " %" PRIu64 " %" PRIu64,\n info.initialized, info.done, info.snapshot_count,\n info.examined, info.emitted, info.invalid_reads);\n}\nstatic void print_summary(const Summary *summary)\n{\n size_t index;\n printf(" %" PRIu64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIu64,\n summary->count, summary->sum, summary->minimum,\n summary->maximum, summary->fingerprint);\n for (index = 0; index < 4; index++) {\n printf(" %" PRIu64, summary->levels[index]);\n }\n for (index = 0; index < BANK_COUNT; index++) {\n printf(" %" PRIu64, summary->banks[index]);\n }\n}\nstatic void print_journal(const Journal *journal)\n{\n printf(" %zu %zu %" PRIu64 " %" PRIu64,\n journal->capacity, journal->count,\n journal->next_sequence, journal->overwritten);\n}\nint main(void)\n{\n Journal journal;\n Cursor cursors[CURSOR_SLOTS];\n int command;\n int following;\n int rc;\n size_t index;\n int64_t values[7];\n journal_init(&journal, LOG_LIMIT);\n for (index = 0; index < CURSOR_SLOTS; index++) {\n cursor_clear(&cursors[index]);\n }\n while ((command = next_nonseparator()) != EOF) {\n following = getchar();\n if (following != EOF && !separator(following)) {\n return 2;\n }\n switch (command) {\n case \'H\':\n if (arguments(values, 1) != 0) {\n return 2;\n }\n rc = journal_init(&journal, index_value(values[0]));\n if (rc == 0) {\n for (index = 0; index < CURSOR_SLOTS; index++) {\n cursor_clear(&cursors[index]);\n }\n }\n printf("H %d", rc);\n print_journal(&journal);\n break;\n case \'A\': {\n uint64_t sequence = 0;\n if (arguments(values, 3) != 0) {\n return 2;\n }\n rc = journal_append(&journal, unsigned_value(values[0]),\n unsigned_value(values[1]), values[2], &sequence);\n printf("A %d %" PRIu64, rc, sequence);\n print_journal(&journal);\n break;\n }\n case \'V\': {\n Record record;\n if (arguments(values, 1) != 0) {\n return 2;\n }\n rc = journal_read(&journal, index_value(values[0]), &record);\n printf("V %d", rc);\n if (rc == 0) {\n print_record(&record);\n }\n break;\n }\n case \'G\': {\n Selection selection;\n Summary summary;\n if (arguments(values, 4) != 0) {\n return 2;\n }\n selection = make_selection(values);\n rc = journal_summary(&journal, &selection, &summary);\n printf("G %d", rc);\n if (rc == 0) {\n print_summary(&summary);\n }\n break;\n }\n case \'O\': {\n Selection selection;\n if (arguments(values, 7) != 0) {\n return 2;\n }\n selection = make_selection(values + 3);\n rc = INVALID_ARGUMENT;\n if (values[0] >= 0 && values[0] < CURSOR_SLOTS) {\n rc = cursor_open(&cursors[values[0]], &journal,\n index_value(values[1]), index_value(values[2]), &selection);\n }\n printf("O %d", rc);\n if (values[0] >= 0 && values[0] < CURSOR_SLOTS) {\n print_info(&cursors[values[0]]);\n }\n break;\n }\n case \'N\': {\n Batch batch;\n if (arguments(values, 3) != 0) {\n return 2;\n }\n rc = INVALID_ARGUMENT;\n if (values[0] >= 0 && values[0] < CURSOR_SLOTS) {\n rc = cursor_step(&cursors[values[0]], index_value(values[1]),\n index_value(values[2]), &batch);\n }\n printf("N %d", rc);\n if (rc == 0) {\n printf(" %d %zu %zu %zu", batch.reason, batch.scanned,\n batch.emitted, batch.port_calls);\n print_info(&cursors[values[0]]);\n for (index = 0; index < batch.emitted && index < LOG_LIMIT; index++) {\n print_record(&batch.rows[index]);\n }\n } else if (values[0] >= 0 && values[0] < CURSOR_SLOTS) {\n print_info(&cursors[values[0]]);\n }\n break;\n }\n case \'I\':\n case \'C\':\n if (arguments(values, 1) != 0) {\n return 2;\n }\n rc = values[0] >= 0 && values[0] < CURSOR_SLOTS ? 0 : INVALID_ARGUMENT;\n if (rc == 0 && command == \'C\') {\n cursor_clear(&cursors[values[0]]);\n }\n printf("%c %d", command, rc);\n if (rc == 0) {\n print_info(&cursors[values[0]]);\n }\n break;\n default:\n return 2;\n }\n putchar(\'\\n\');\n }\n return ferror(stdin) || ferror(stdout) ? 2 : 0;\n}\n', 'src/journal.c': '#include "reverse.h"\n#include <string.h>\nint journal_init(Journal *journal, size_t capacity)\n{\n if (journal == NULL || capacity == 0 || capacity > LOG_LIMIT) {\n return INVALID_ARGUMENT;\n }\n memset(journal, 0, sizeof(*journal));\n journal->capacity = capacity;\n journal->next_sequence = 1;\n return 0;\n}\nint journal_append(Journal *journal, unsigned bank, unsigned level, int64_t value, uint64_t *sequence)\n{\n size_t position;\n Record record;\n if (journal == NULL || sequence == NULL || journal->capacity == 0) {\n return INVALID_ARGUMENT;\n }\n if (bank >= BANK_COUNT || level > 3 || value < -1000000 || value > 1000000) {\n return INVALID_ARGUMENT;\n }\n if (journal->next_sequence == UINT64_MAX) {\n return INVALID_ARGUMENT;\n }\n record.sequence = journal->next_sequence;\n record.bank = bank;\n record.level = level;\n record.value = value;\n if (journal->count < journal->capacity) {\n position = (journal->head + journal->count) % journal->capacity;\n journal->count++;\n } else {\n position = journal->head;\n journal->head = (journal->head + 1) % journal->capacity;\n journal->overwritten++;\n }\n journal->cells[position] = record;\n journal->next_sequence++;\n *sequence = record.sequence;\n return 0;\n}\nint journal_read(const Journal *journal, size_t ordinal, Record *record)\n{\n size_t position;\n if (journal == NULL || record == NULL || ordinal >= journal->count) {\n return INVALID_ARGUMENT;\n }\n position = (journal->head + ordinal) % journal->capacity;\n *record = journal->cells[position];\n return 0;\n}\nint journal_copy(const Journal *journal, Record *rows, size_t capacity, size_t *count)\n{\n size_t ordinal;\n if (journal == NULL || rows == NULL || count == NULL || capacity < journal->count) {\n return INVALID_ARGUMENT;\n }\n for (ordinal = 0; ordinal < journal->count; ordinal++) {\n if (journal_read(journal, ordinal, &rows[ordinal]) != 0) {\n return INVALID_ARGUMENT;\n }\n }\n *count = journal->count;\n return 0;\n}\nint selection_valid(const Selection *selection)\n{\n if (selection == NULL) {\n return 0;\n }\n if (selection->bank_mask > 255 || selection->minimum_level > 4) {\n return 0;\n }\n if (selection->minimum_value < -1000000 || selection->maximum_value > 1000000) {\n return 0;\n }\n return selection->minimum_value <= selection->maximum_value;\n}\nint selection_accepts(const Selection *selection, const Record *record)\n{\n if (selection == NULL || record == NULL) {\n return 0;\n }\n if ((selection->bank_mask & (1U << record->bank)) == 0) {\n return 0;\n }\n if (record->level < selection->minimum_level) {\n return 0;\n }\n if (record->value < selection->minimum_value) {\n return 0;\n }\n if (record->value > selection->maximum_value) {\n return 0;\n }\n return 1;\n}\n'}
|
|
|
|
COMPILE_SECONDS = 15
|
|
CANDIDATE_SECONDS = 8
|
|
CLEANUP_SECONDS = 6
|
|
ABSENCE_SECONDS = 6
|
|
MASK64 = (1 << 64) - 1
|
|
|
|
class Model:
|
|
def __init__(self):
|
|
self.capacity = 64
|
|
self.rows = []
|
|
self.next_sequence = 1
|
|
self.overwritten = 0
|
|
self.cursors = [None, None]
|
|
|
|
def info(self, slot):
|
|
cursor = self.cursors[slot]
|
|
if cursor is None: return [0,0,0,0,0,0]
|
|
return [1,int(not cursor['remaining']),len(cursor['snapshot']),cursor['examined'],cursor['emitted'],0]
|
|
|
|
def selection_valid(self, values):
|
|
mask,level,minimum,maximum = values
|
|
return 0 <= mask <= 255 and 0 <= level <= 4 and -1000000 <= minimum <= maximum <= 1000000
|
|
|
|
def accepts(self, row, selection):
|
|
_,bank,level,value = row
|
|
mask,minimum_level,minimum,maximum = selection
|
|
return bool(mask & (1 << bank)) and level >= minimum_level and minimum <= value <= maximum
|
|
|
|
def journal_info(self):
|
|
return [self.capacity,len(self.rows),self.next_sequence,self.overwritten]
|
|
|
|
def apply(self, command):
|
|
op,*args = command
|
|
result = [op]
|
|
if op == 'H':
|
|
cap, = args
|
|
valid = 1 <= cap <= 64
|
|
if valid:
|
|
self.capacity=cap; self.rows=[]; self.next_sequence=1; self.overwritten=0; self.cursors=[None,None]
|
|
return result+[0 if valid else -1]+self.journal_info()
|
|
if op == 'A':
|
|
bank,level,value=args
|
|
valid=0 <= bank < 8 and 0 <= level < 4 and -1000000 <= value <= 1000000
|
|
sequence=0
|
|
if valid:
|
|
sequence=self.next_sequence; self.next_sequence+=1
|
|
self.rows.append([sequence,bank,level,value])
|
|
if len(self.rows)>self.capacity:
|
|
self.rows.pop(0); self.overwritten+=1
|
|
return result+[0 if valid else -1,sequence]+self.journal_info()
|
|
if op == 'V':
|
|
index,=args
|
|
return result+([0]+self.rows[index] if 0 <= index < len(self.rows) else [-1])
|
|
if op == 'G':
|
|
if not self.selection_valid(args): return result+[-1]
|
|
chosen=[r for r in self.rows if self.accepts(r,args)]
|
|
levels=[0]*4; banks=[0]*8; fingerprint=0
|
|
for sequence,bank,level,value in chosen:
|
|
levels[level]+=1; banks[bank]+=1
|
|
fingerprint=(fingerprint*65599+sequence*131+bank*17+level*5+value+1000000)&MASK64
|
|
values=[r[3] for r in chosen]
|
|
summary=[len(chosen),sum(values),min(values) if values else 0,max(values) if values else 0,fingerprint]
|
|
return result+[0]+summary+levels+banks
|
|
slot=args[0]
|
|
if slot not in (0,1): return result+[-1]
|
|
if op == 'C': self.cursors[slot]=None
|
|
if op in ('I','C'): return result+[0]+self.info(slot)
|
|
if op == 'O':
|
|
_,low,high,*selection=args
|
|
valid=self.selection_valid(selection) and ((not self.rows and low==high==0) or (0 <= low <= high < len(self.rows)))
|
|
if valid:
|
|
self.cursors[slot]=dict(snapshot=[r[:] for r in self.rows],selection=selection[:],remaining=list(range(high,low-1,-1)) if self.rows else [],examined=0,emitted=0)
|
|
return result+[0 if valid else -1]+self.info(slot)
|
|
if op == 'N':
|
|
_,budget,capacity=args
|
|
cursor=self.cursors[slot]
|
|
if cursor is None or not (0 <= budget <= 64 and 0 <= capacity <= 64):
|
|
return result+[-1]+self.info(slot)
|
|
scanned=0; emitted=[]
|
|
while cursor['remaining'] and scanned < budget and len(emitted) < capacity:
|
|
ordinal=cursor['remaining'].pop(0); row=cursor['snapshot'][ordinal]
|
|
scanned+=1; cursor['examined']+=1
|
|
if self.accepts(row,cursor['selection']):
|
|
emitted.extend([row]); cursor['emitted']+=1
|
|
reason=0 if not cursor['remaining'] else (1 if scanned==budget else 2)
|
|
return result+[0,reason,scanned,len(emitted),scanned]+self.info(slot)+[n for r in emitted for n in r]
|
|
raise ValueError('unknown model command')
|
|
|
|
def cases():
|
|
commands=[]
|
|
def add(op,*args): commands.append([op,*args])
|
|
# The public default and independent filtered example are distinct sessions.
|
|
for filtered in (False,True):
|
|
add('H',8)
|
|
for bank,level,value in [(0,1,40),(1,2,30),(2,3,20),(3,0,10)]: add('A',bank,level,value)
|
|
add('O',0,1 if filtered else 0,3,2 if filtered else 255,0,-1000000,1000000)
|
|
for _ in range(4 if filtered else 2): add('N',0,1 if filtered else 8,8)
|
|
rng=random.Random(0xC0A50E)
|
|
for capacity,count in [(1,0),(1,1),(1,5),(2,2),(3,9),(8,7),(16,16),(64,64),(64,91)]:
|
|
add('H',capacity)
|
|
add('I',0); add('N',0,1,1)
|
|
for n in range(count): add('A',n%8,(n*3)%4,[-1000000,1000000,0,-7,23][n%5])
|
|
retained=min(capacity,count)
|
|
for n in [-1,0,retained//2,retained-1,retained,64]: add('V',n)
|
|
for selection in [(255,0,-1000000,1000000),(0,0,-1000000,1000000),(170,2,-7,23),(255,4,-1000000,1000000)]:
|
|
add('G',*selection)
|
|
add('O',0,0,max(0,retained-1),*selection)
|
|
for _ in range(5): add('N',0,17,9)
|
|
add('N',0,64,64); add('I',0)
|
|
if retained:
|
|
add('O',0,0,retained-1,255,0,-1000000,1000000)
|
|
add('O',1,retained//2,retained-1,85,0,-1000000,1000000)
|
|
add('N',0,0,64); add('N',1,64,0)
|
|
for n in range(capacity+2): add('A',rng.randrange(8),rng.randrange(4),rng.randrange(-100,101))
|
|
for _ in range(5): add('N',1,11,3); add('N',0,13,7)
|
|
add('N',0,64,64); add('N',1,64,64)
|
|
add('C',0); add('I',1); add('N',0,1,1)
|
|
for scenario in range(36):
|
|
capacity=rng.choice([2,3,7,16,31,64]); count=rng.randrange(1,capacity*2+1)
|
|
add('H',capacity)
|
|
for n in range(count): add('A',rng.randrange(8),rng.randrange(4),rng.randrange(-1000000,1000001))
|
|
retained=min(capacity,count); low=rng.randrange(retained); high=rng.randrange(low,retained)
|
|
selection=(rng.randrange(256),rng.randrange(5),-rng.randrange(1000001),rng.randrange(1000001))
|
|
add('G',*selection); add('O',0,low,high,*selection)
|
|
for _ in range(12): add('N',0,rng.choice([0,1,2,7,64]),rng.choice([0,1,2,8,64]))
|
|
add('N',0,64,64); add('N',0,0,0); add('I',0)
|
|
# Invalid API arguments must leave an existing snapshot and its counters intact.
|
|
add('H',4)
|
|
for n in range(4): add('A',n,n,10+n)
|
|
add('O',0,0,3,255,0,-1000000,1000000); add('N',0,1,1)
|
|
for cmd in [('H',0),('H',65),('A',8,0,0),('A',0,4,0),('A',0,0,1000001),('O',0,-1,3,255,0,-1000000,1000000),('O',0,3,1,255,0,-1000000,1000000),('O',0,0,4,255,0,-1000000,1000000),('O',0,0,3,256,0,-1000000,1000000),('O',0,0,3,255,5,-1000000,1000000),('O',0,0,3,255,0,1,-1),('N',0,-1,1),('N',0,65,1),('N',0,1,65),('O',2,0,1,255,0,0,0),('N',2,1,1),('C',-1)]:
|
|
add(*cmd); add('I',0)
|
|
add('N',0,64,64); add('G',255,0,-1000000,1000000)
|
|
model=Model(); expected=[model.apply(c) for c in commands]
|
|
payload=('\n'.join(' '.join(map(str,c)) for c in commands)+'\n').encode('ascii')
|
|
return payload,expected
|
|
|
|
def decimal(token):
|
|
sign=1
|
|
if token[:1] in (b'+',b'-'):
|
|
sign=-1 if token[:1]==b'-' else 1; token=token[1:]
|
|
if not token or any(x < 48 or x > 57 for x in token): raise ValueError('non decimal stdout token')
|
|
token=token.lstrip(b'0') or b'0'
|
|
if len(token)>21: raise ValueError('stdout value outside observable range')
|
|
return sign*int(token)
|
|
|
|
def observations(raw):
|
|
lines=raw.split(b'\n')
|
|
if lines[-1:] != [b'']: raise ValueError('missing output LF')
|
|
result=[]
|
|
for line in lines[:-1]:
|
|
# Only public horizontal separators are permitted within a record.
|
|
for value in line:
|
|
if value not in (9,13,32,43,45) and not 48<=value<=57 and value not in b'HAVGONIC':
|
|
raise ValueError('invalid stdout byte')
|
|
tokens=line.replace(b'\t',b' ').replace(b'\r',b' ').split(b' ')
|
|
tokens=[x for x in tokens if x]
|
|
if not tokens or len(tokens[0])!=1 or tokens[0] not in [bytes([x]) for x in b'HAVGONIC']:
|
|
raise ValueError('invalid command record')
|
|
result.append([tokens[0].decode('ascii')]+[decimal(x) for x in tokens[1:]])
|
|
return result
|
|
|
|
def emit_evidence(evidence, failure, exceptions, scratch, expected_rows):
|
|
record=dict(stages=evidence,failure=failure,exceptions=exceptions,scratch=str(scratch),expected_rows=expected_rows,
|
|
original_wire_io_saved=False,wire_io_evidence='actual captured byte lengths and SHA256; no original wire/IO retained')
|
|
compressed=base64.b64encode(bz2.compress(json.dumps(record,separators=(',',':')).encode())).decode('ascii')
|
|
sys.stderr.write('EVIDENCE_BZ2:'+compressed+'\n')
|
|
|
|
|
|
def main():
|
|
candidate=Path(sys.argv[1])
|
|
scratch=None; evidence=[]; exceptions=[]; failures=[]
|
|
business_ok=False; cleanup_ok=False; absence_ok=False; row_count=0
|
|
try:
|
|
scratch=Path(tempfile.mkdtemp(prefix='reverse-cursor-'))
|
|
build=scratch/'build'; build.mkdir(); build.chmod(0o777)
|
|
for name,text in SUPPORT.items():
|
|
destination=build/name; destination.parent.mkdir(parents=True,exist_ok=True); destination.parent.chmod(0o777)
|
|
destination.write_text(text); destination.chmod(0o644)
|
|
destination=build/'src/cursor.c'; destination.write_bytes((candidate/'src/cursor.c').read_bytes()); destination.chmod(0o644)
|
|
payload,expected=cases(); row_count=len(expected)
|
|
argv=['gcc','-std=c11','-D_POSIX_C_SOURCE=200809L','-O2','-Wall','-Wextra','-Wpedantic','-Iinclude','src/journal.c','src/summary.c','src/cursor.c','src/observer.c','-o','reverse-observer']
|
|
execute_stage('compiler',argv,build,COMPILE_SECONDS,b'',evidence)
|
|
out,err=execute_stage('candidate',[str(build/'reverse-observer')],build,CANDIDATE_SECONDS,payload,evidence)
|
|
observed=observations(out)
|
|
if observed!=expected:
|
|
first=next((i for i,(a,b) in enumerate(zip(observed,expected)) if a!=b),min(len(observed),len(expected)))
|
|
failures.append('observation mismatch at record '+str(first)+'; actual_rows='+str(len(observed)))
|
|
else: business_ok=True
|
|
except BaseException as error:
|
|
exceptions.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:
|
|
exceptions.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:
|
|
exceptions.append(['absence',type(error).__name__,str(error)])
|
|
emit_evidence(evidence,failures,exceptions,scratch,row_count)
|
|
passed=business_ok and cleanup_ok and absence_ok and not failures and not exceptions
|
|
print('PASS' if passed else 'FAIL')
|
|
return 0 if passed else 1
|
|
|
|
if __name__=='__main__':
|
|
sys.exit(main())
|