interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / neuralese / nl1r-task / boundary-check.py

173 lines · 7472 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Compose, compile and exercise the NL1R NL4 boundary adapter (host verification only)."""
3import hashlib, json, os, pathlib, subprocess, sys, tempfile
4
5NB = pathlib.Path('/home/drakestapleton/fix/nl-boundary')
6TD = pathlib.Path('/home/drakestapleton/fix/nl1r-task/native/neuralese/nl1r-task')
7OMC = NB / 'seed/out/omc'
8OMC_SHA = 'd6290c7663c2b4bafb59eb3fdf812476e977f4c144def71095c6248ef9b96b84'
9BUILD = pathlib.Path('/home/drakestapleton/fix/nl1r-task/build/boundary')
10PARTS = [NB / 'build/nl4-deps/h.om', NB / 'neuralese/proposal.om', NB / 'build/nl4-deps/v.om', NB / 'om/tls/sha2.om']
11CTX = NB / 'neuralese/fixtures/nl4-context.bin'
12
13
14def sha(b):
15 return hashlib.sha256(b).hexdigest()
16
17
18def prefix():
19 lines = (NB / 'neuralese/host-proposal.om').read_bytes().split(b'\n')
20 return b'\n'.join(lines[:144]) + b'\n'
21
22
23def compose(boundary_src):
24 return prefix() + boundary_src + b''.join(p.read_bytes() for p in PARTS)
25
26
27def compile_src(name, src):
28 BUILD.mkdir(parents=True, exist_ok=True)
29 s = BUILD / f'{name}.om'
30 s.write_bytes(src)
31 b = BUILD / name
32 with open(s, 'rb') as fi, open(b, 'wb') as fo:
33 r = subprocess.run([str(OMC)], stdin=fi, stdout=fo, stderr=subprocess.PIPE, timeout=120)
34 out = {'name': name, 'compile_exit': r.returncode, 'compile_stderr': r.stderr.decode(), 'source_sha256': sha(src)}
35 if r.returncode == 0:
36 os.chmod(b, 0o755)
37 out['binary_sha256'] = sha(b.read_bytes())
38 return out, b
39
40
41def run(binp, args, fd=3):
42 with tempfile.TemporaryDirectory() as d:
43 f = pathlib.Path(d) / 'frame'
44 argv = ' '.join(["'%s'" % a for a in [binp] + [str(x) for x in args]])
45 cmd = f'exec {argv} {fd}>"{f}" < /dev/null' if fd == 3 else f'exec {argv} < /dev/null'
46 r = subprocess.run(['bash', '-c', cmd], capture_output=True, timeout=20)
47 data = f.read_bytes() if f.exists() else b''
48 return r.returncode, r.stdout.decode(), r.stderr.decode(), data
49
50
51def expected_stdout(bits, task):
52 chunks = 1 if task == 1 else 2
53 out = ''
54 for c in range(chunks):
55 v = (bits >> (2 * c)) & 3
56 out += f'NL4R1 0 3 {v} 0 1 1 0 1 0 {v}\n'
57 return out
58
59
60CASES = [
61 # name, predicted, target, task, abstain, fd, want_exit
62 ('task2-match-6', 6, 6, 2, 0, 3, 0),
63 ('task2-match-9', 9, 9, 2, 0, 3, 0),
64 ('task2-match-0', 0, 0, 2, 0, 3, 0),
65 ('task2-match-15', 15, 15, 2, 0, 3, 0),
66 ('task1-match-1', 1, 1, 1, 0, 3, 0),
67 ('task1-match-0', 0, 0, 1, 0, 3, 0),
68 ('task2-chunk1-wrong', 6, 2, 2, 0, 3, 49),
69 ('task2-chunk0-wrong', 6, 4, 2, 0, 3, 49),
70 ('task1-mismatch', 1, 0, 1, 0, 3, 49),
71 ('task1-oversize-bits', 2, 2, 1, 0, 3, 80),
72 ('task-3-refused', 1, 1, 3, 0, 3, 80),
73 ('predicted-16-refused', 16, 1, 2, 0, 3, 80),
74 ('abstain', 6, 6, 2, 1, 3, 2),
75 ('frame-fd-2-refused', 6, 6, 2, 0, 2, 80),
76]
77
78
79def exercise(binp):
80 rows = []
81 for name, p, t, task, ab, fd, want in CASES:
82 rc, so, se, data = run(binp, [CTX, p, t, task, ab, fd], fd=fd if fd == 3 else 2)
83 exp_out = expected_stdout(p, task) if want == 0 else ''
84 ok = rc == want and (want != 0 or so == exp_out) and (want != 49 or 'NL4R1' not in so)
85 rows.append({'case': name, 'args': [str(CTX), p, t, task, ab, fd], 'want_exit': want, 'exit': rc,
86 'stdout': so, 'stderr': se, 'frame_bytes': len(data), 'frame_sha256': sha(data),
87 'verdict': 'PASS' if ok else 'FAIL'})
88 rc, so, se, data = run(binp, ['/nonexistent-context', 6, 6, 2, 0, 3])
89 rows.append({'case': 'missing-context-refused', 'want_exit': 80, 'exit': rc, 'stdout': so, 'stderr': se,
90 'verdict': 'PASS' if rc == 80 else 'FAIL'})
91 rc, so, se, data = run(binp, [CTX, 6, 6, 2, 0])
92 rows.append({'case': 'argc-refused', 'want_exit': 80, 'exit': rc, 'stdout': so, 'stderr': se,
93 'verdict': 'PASS' if rc == 80 else 'FAIL'})
94 # Envelope negatives: the frame header is copied from the context, so a malformed context must be
95 # refused by the unchanged LTP0 validator before any proposal decode or execution.
96 for name, mutate, want in BAD_CONTEXTS:
97 c = bytearray(CTX.read_bytes())
98 mutate(c)
99 p = BUILD / f'ctx-{name}.bin'
100 p.write_bytes(bytes(c))
101 rc, so, se, data = run(binp, [p, 6, 6, 2, 0, 3])
102 rows.append({'case': 'context-' + name, 'context_sha256': sha(bytes(c)), 'want_exit': want, 'exit': rc,
103 'stdout': so, 'stderr': se, 'verdict': 'PASS' if rc == want and 'NL4R1' not in so else 'FAIL'})
104 return rows
105
106
107def _magic(c):
108 c[0] ^= 0xff
109
110
111def _expiry_before_issue(c):
112 c[72:80] = c[64:72]
113
114
115BAD_CONTEXTS = [
116 ('bad-magic', _magic, 11), # v.om lpvalid: lpbe32 a 0 != 0x4c545030 -> 11
117 ('expiry-equals-issue', _expiry_before_issue, 18), # v.om lpvalid: expiry <= issued -> 18
118]
119
120
121MUTANTS = {
122 # name: (old, new) applied to boundary.om; each must turn at least one case to FAIL.
123 'wrong-chunk-expectation': (b'sh = mul get BCFG 4 2\n', b'sh = mul get BCFG 4 0\n'),
124 'no-receiver-match': (b'if not nlmatch p e\nret 49\n', b'if false\nret 49\n'),
125 'execute-before-all-verified': (
126 b'for i ulong 16\nput props add mul chunk 16 i get p i\nend\nend\n',
127 b'nlexec p\nend\n'),
128 'no-envelope-validation': (b'rc = lpvalid frame part ctx 0 1024 now 0\n', b'rc = 0\n'),
129}
130
131
132def main():
133 src = (TD / 'boundary.om').read_bytes()
134 report = {'omc': str(OMC), 'omc_sha256': sha(OMC.read_bytes()), 'boundary_om_sha256': sha(src),
135 'context_sha256': sha(CTX.read_bytes()), 'parts': [str(p) for p in PARTS],
136 'prefix': 'neuralese/host-proposal.om lines 1..144', 'prefix_sha256': sha(prefix())}
137 if report['omc_sha256'] != OMC_SHA:
138 print('omc pin mismatch', file=sys.stderr)
139 return 3
140 c, b = compile_src('boundary', compose(src))
141 report['compile'] = c
142 if c['compile_exit'] != 0:
143 print(json.dumps(report, indent=2))
144 return 1
145 report['cases'] = exercise(b)
146 report['mutants'] = []
147 for name, (old, new) in MUTANTS.items():
148 if src.count(old) != 1:
149 report['mutants'].append({'mutant': name, 'error': 'anchor not unique'})
150 continue
151 mc, mb = compile_src('mutant-' + name, compose(src.replace(old, new)))
152 entry = {'mutant': name, 'compile': mc}
153 if mc['compile_exit'] == 0:
154 rows = exercise(mb)
155 entry['failed_cases'] = [r['case'] for r in rows if r['verdict'] == 'FAIL']
156 entry['detected'] = bool(entry['failed_cases'])
157 else:
158 entry['detected'] = False
159 report['mutants'].append(entry)
160 report['all_cases_pass'] = all(r['verdict'] == 'PASS' for r in report['cases'])
161 report['all_mutants_detected'] = all(m.get('detected') for m in report['mutants'])
162 out = TD / 'evidence/boundary-exercise.json'
163 out.write_text(json.dumps(report, indent=2) + '\n')
164 for r in report['cases']:
165 print(r['verdict'], r['case'], r['exit'], r['want_exit'], repr(r['stdout'][:90]))
166 for m in report['mutants']:
167 print('MUTANT', m['mutant'], 'compile', m.get('compile', {}).get('compile_exit'), m.get('compile', {}).get('compile_stderr', '').strip(),
168 'detected', m.get('detected'), m.get('failed_cases'))
169 return 0 if report['all_cases_pass'] and report['all_mutants_detected'] else 2
170
171
172if __name__ == '__main__':
173 sys.exit(main())