omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / trit / test-daily.py
| 1 | #!/usr/bin/env python3 |
| 2 | """External oracle for the native daily interface; no production dependency.""" |
| 3 | import hashlib |
| 4 | import hmac |
| 5 | import json |
| 6 | import os |
| 7 | from pathlib import Path |
| 8 | import secrets |
| 9 | import subprocess |
| 10 | import tempfile |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parents[2] |
| 13 | BIN = Path(os.environ.get('BIN', ROOT / 'build/trit/trit')).resolve() |
| 14 | WORK = Path(tempfile.mkdtemp(prefix='trit-daily-', dir=os.environ.get('TMPDIR'))) |
| 15 | CB = (ROOT / 'tools/codebook/codebook.tsv').read_bytes() |
| 16 | CBHASH = hashlib.sha256(CB).hexdigest() |
| 17 | checks = [] |
| 18 | |
| 19 | def fresh(name): |
| 20 | d = WORK / name |
| 21 | (d / 'inbox').mkdir(parents=True) |
| 22 | (d / 'codebook').mkdir() |
| 23 | (d / 'codebook/codebook.tsv').write_bytes(CB) |
| 24 | (d / 'codebook/key').write_bytes(secrets.token_bytes(32)) |
| 25 | (d / 'codebook/key').chmod(0o600) |
| 26 | (d / 'inbox/alice.md').touch() |
| 27 | (d / 'inbox/bob.md').touch() |
| 28 | return d |
| 29 | |
| 30 | def line(d, lane='n', ts='2026-11-16T20:00', sender='alice', to='bob', tag='stop', text='STOP task', counter=1, digits='300', signed=True): |
| 31 | fields = '|'.join((ts, sender, to, tag, text, '-')) |
| 32 | if signed: |
| 33 | payload = f'ocb2|{CBHASH}|{counter}|{lane}|{fields}|{digits}'.encode() |
| 34 | mac = hmac.new((d / 'codebook/key').read_bytes(), payload, hashlib.sha256).hexdigest() |
| 35 | fields += f'|ocb2:{CBHASH}:{counter}:{digits}:{mac}' |
| 36 | return (fields + '\n').encode() |
| 37 | |
| 38 | def put(d, body, lane='n'): |
| 39 | f = d / ('inbox/bob.emergency.md' if lane == 'e' else 'inbox/bob.md') |
| 40 | f.write_bytes(body) |
| 41 | return f'{lane}{len(body)}:{hashlib.sha256(body).hexdigest()}' |
| 42 | |
| 43 | def run(name, d, args, rc, stdout=b'', stderr=b''): |
| 44 | command = [str(BIN), str(d), *args] |
| 45 | p = subprocess.run(command, capture_output=True) |
| 46 | ok = (p.returncode, p.stdout, p.stderr) == (rc, stdout, stderr) |
| 47 | record = {'name': name, 'command': command, 'exit': p.returncode, |
| 48 | 'stdout': p.stdout.decode(errors='replace'), 'stderr': p.stderr.decode(errors='replace'), |
| 49 | 'expected_exit': rc, 'expected_stdout': stdout.decode(), 'pass': ok, |
| 50 | 'inputs': {f.relative_to(d).as_posix(): hashlib.sha256(f.read_bytes()).hexdigest() |
| 51 | for f in (d / 'inbox').glob('*') if f.is_file()}} |
| 52 | checks.append(record) |
| 53 | print(('PASS ' if ok else 'FAIL ') + name) |
| 54 | return p |
| 55 | |
| 56 | for name, kw, lane in [ |
| 57 | ('signed-stop', {}, 'n'), ('old-unseen-stop', {'ts':'2020-01-01T00:00'}, 'n'), |
| 58 | ('key-holder-claims-drake', {'sender':'drake'}, 'n'), |
| 59 | ('unsigned-stop', {'signed':False}, 'n'), ('wrong-recipient', {'to':'other'}, 'n'), |
| 60 | ('signed-emergency', {'lane':'e'}, 'e'), |
| 61 | ('task', {'tag':'task', 'text':'Read task', 'digits':'101'}, 'n')]: |
| 62 | d = fresh(name) |
| 63 | body = line(d, **kw) |
| 64 | token = put(d, body, lane) |
| 65 | run(name, d, ['--inspect', 'bob'], 0, f'bob 1 {token}\n'.encode()) |
| 66 | if name in ('signed-stop', 'old-unseen-stop', 'key-holder-claims-drake', 'signed-emergency'): |
| 67 | run(name+'-legacy-control', d, ['bob'], 0, f'bob 2 {token}\n'.encode()) |
| 68 | run(name+'-consume', d, ['--consume', 'bob', token], 0) |
| 69 | run(name+'-after-consume', d, ['--inspect', 'bob'], 1) |
| 70 | |
| 71 | # A changed signature must still be inspected, never treated as stop authority. |
| 72 | d = fresh('tamper') |
| 73 | body = line(d).replace(b'STOP task', b'STOP changed') |
| 74 | token = put(d, body) |
| 75 | run('tampered-stop', d, ['--inspect', 'bob'], 0, f'bob 1 {token}\n'.encode()) |
| 76 | |
| 77 | # Consuming one token may not emit a receipt or silently consume later work. |
| 78 | d = fresh('append') |
| 79 | first = line(d, tag='task', text='First', digits='101') |
| 80 | token = put(d, first) |
| 81 | second = line(d, tag='task', text='Second', digits='101', counter=2) |
| 82 | whole = first + second |
| 83 | later = put(d, whole) |
| 84 | run('consume-silent-with-next-task', d, ['--consume', 'bob', token], 0) |
| 85 | run('next-task-still-unread', d, ['--inspect', 'bob'], 0, f'bob 1 {later}\n'.encode()) |
| 86 | cursor = d/'inbox/bob.md.cur' |
| 87 | cur = cursor.read_bytes() if cursor.exists() else None |
| 88 | run('bad-token', d, ['--consume', 'bob', 'n1:'+'0'*64], 3) |
| 89 | checks.append({'name':'bad-token-keeps-cursor', 'pass':cur is not None and cursor.exists() and cursor.read_bytes()==cur}) |
| 90 | run('legacy-ack-still-receipt', d, ['--ack', 'bob', later], 0, b'bob 0\n') |
| 91 | |
| 92 | # Empty and informational records do not wake. Multiple recipients remain independent. |
| 93 | d = fresh('quiet') |
| 94 | run('empty', d, ['--inspect', 'bob'], 1) |
| 95 | put(d, line(d, tag='fyi', text='Status only', signed=False)) |
| 96 | run('informational', d, ['--inspect', 'bob'], 1) |
| 97 | run('inspect-no-recipient', d, ['--inspect'], 2) |
| 98 | run('consume-no-token', d, ['--consume', 'bob'], 2) |
| 99 | run('consume-extra-arg', d, ['--consume', 'bob', 'n0:'+'0'*64, 'extra'], 2) |
| 100 | run('invalid-recipient', d, ['--inspect', '../bob'], 2) |
| 101 | token = put(d, line(d, tag='task', text='Work', digits='101')) |
| 102 | run('multiple-recipients', d, ['--inspect', 'alice', 'bob'], 0, f'bob 1 {token}\n'.encode()) |
| 103 | |
| 104 | receipt = {'binary': str(BIN), 'binary_sha256':hashlib.sha256(BIN.read_bytes()).hexdigest(), |
| 105 | 'codebook_sha256':CBHASH, 'scope':'host fixture only; no process control, no runtime authority claimed', 'checks':checks} |
| 106 | (WORK/'receipt.json').write_text(json.dumps(receipt, indent=2)+'\n') |
| 107 | failed = sum(not c['pass'] for c in checks) |
| 108 | print(f'DAILY checks={len(checks)} failed={failed} receipt={WORK}/receipt.json') |
| 109 | raise SystemExit(bool(failed)) |