interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tools / trit / test-daily.py

109 lines · 5231 bytes Raw Blame History
1#!/usr/bin/env python3
2"""External oracle for the native daily interface; no production dependency."""
3import hashlib
4import hmac
5import json
6import os
7from pathlib import Path
8import secrets
9import subprocess
10import tempfile
11
12ROOT = Path(__file__).resolve().parents[2]
13BIN = Path(os.environ.get('BIN', ROOT / 'build/trit/trit')).resolve()
14WORK = Path(tempfile.mkdtemp(prefix='trit-daily-', dir=os.environ.get('TMPDIR')))
15CB = (ROOT / 'tools/codebook/codebook.tsv').read_bytes()
16CBHASH = hashlib.sha256(CB).hexdigest()
17checks = []
18
19def 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
30def 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
38def 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
43def 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
56for 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.
72d = fresh('tamper')
73body = line(d).replace(b'STOP task', b'STOP changed')
74token = put(d, body)
75run('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.
78d = fresh('append')
79first = line(d, tag='task', text='First', digits='101')
80token = put(d, first)
81second = line(d, tag='task', text='Second', digits='101', counter=2)
82whole = first + second
83later = put(d, whole)
84run('consume-silent-with-next-task', d, ['--consume', 'bob', token], 0)
85run('next-task-still-unread', d, ['--inspect', 'bob'], 0, f'bob 1 {later}\n'.encode())
86cursor = d/'inbox/bob.md.cur'
87cur = cursor.read_bytes() if cursor.exists() else None
88run('bad-token', d, ['--consume', 'bob', 'n1:'+'0'*64], 3)
89checks.append({'name':'bad-token-keeps-cursor', 'pass':cur is not None and cursor.exists() and cursor.read_bytes()==cur})
90run('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.
93d = fresh('quiet')
94run('empty', d, ['--inspect', 'bob'], 1)
95put(d, line(d, tag='fyi', text='Status only', signed=False))
96run('informational', d, ['--inspect', 'bob'], 1)
97run('inspect-no-recipient', d, ['--inspect'], 2)
98run('consume-no-token', d, ['--consume', 'bob'], 2)
99run('consume-extra-arg', d, ['--consume', 'bob', 'n0:'+'0'*64, 'extra'], 2)
100run('invalid-recipient', d, ['--inspect', '../bob'], 2)
101token = put(d, line(d, tag='task', text='Work', digits='101'))
102run('multiple-recipients', d, ['--inspect', 'alice', 'bob'], 0, f'bob 1 {token}\n'.encode())
103
104receipt = {'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')
107failed = sum(not c['pass'] for c in checks)
108print(f'DAILY checks={len(checks)} failed={failed} receipt={WORK}/receipt.json')
109raise SystemExit(bool(failed))