aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / hive-render / test-receive.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Exercise actual forced-command receiver and atomic publication failure paths.""" |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import time |
| 10 | |
| 11 | receiver, renderer = (Path(p).resolve() for p in sys.argv[1:3]) |
| 12 | vulnerable_control = '--expect-vulnerable' in sys.argv[3:] |
| 13 | value = dict(schema_version=1, generated_at='2026-10-26T01:00:00Z', sequence='integration', |
| 14 | poll_ms=1000, stale_after_ms=15000, agents=[], interactions=[], |
| 15 | coverage=dict(label='OMP', roots=0, agents=0, complete=False, notice='Integration oracle')) |
| 16 | raw = (json.dumps(value) + '\n').encode() |
| 17 | with tempfile.TemporaryDirectory(prefix='hive-receiver-integration-') as tmp: |
| 18 | directory = Path(tmp) |
| 19 | board = directory / 'board.html' |
| 20 | fail = directory / 'fail-renderer' |
| 21 | fail.write_text('#!/bin/sh\nprintf incomplete\nexit 7\n') |
| 22 | fail.chmod(0o700) |
| 23 | env = dict(os.environ, SSH_ORIGINAL_COMMAND='hive-feed', HIVE_RENDER=str(fail)) |
| 24 | board.write_bytes(b'old useful board') |
| 25 | result = subprocess.run([receiver, directory], input=raw, env=env, capture_output=True, check=True) |
| 26 | assert board.read_bytes() == b'old useful board' |
| 27 | assert b'retained previous observation' in result.stderr |
| 28 | assert json.loads((directory / 'live.json').read_bytes()) == value |
| 29 | assert not list(directory.glob('.board-tmp-*')) |
| 30 | env['HIVE_RENDER'] = str(renderer) |
| 31 | result = subprocess.run([receiver, directory], input=raw, env=env, capture_output=True, check=True) |
| 32 | assert result.stderr == b'' |
| 33 | expected = subprocess.run([renderer], input=(directory / 'live.json').read_bytes(), capture_output=True, check=True).stdout |
| 34 | assert board.read_bytes() == expected |
| 35 | before = board.read_bytes() |
| 36 | bad = dict(value, schema_version=2) |
| 37 | result = subprocess.run([receiver, directory], input=(json.dumps(bad)+'\n').encode(), env=env, capture_output=True) |
| 38 | assert result.returncode != 0 |
| 39 | assert board.read_bytes() == before |
| 40 | assert json.loads((directory / 'live.json').read_bytes()) == value |
| 41 | # Agent replies only: a snapshot carrying any human-role record is refused whole, |
| 42 | # newer generated_at notwithstanding, and the previous JSON and board stay served. |
| 43 | reply = dict(id='m1', agent_id='a1', role='assistant', text='Agent reply', at='2026-10-26T01:00:00Z', |
| 44 | runtime='OMP', provider='unknown', model='unknown') |
| 45 | for role in ('drake', 'user', 'guest'): |
| 46 | human = dict(reply, id='m2', role=role, text='HUMAN_MESSAGE_CANARY') |
| 47 | leaked = dict(value, generated_at='2026-10-26T02:00:00Z', sequence='human-' + role, interactions=[reply, human]) |
| 48 | result = subprocess.run([receiver, directory], input=(json.dumps(leaked)+'\n').encode(), env=env, capture_output=True) |
| 49 | assert result.returncode != 0, role |
| 50 | assert board.read_bytes() == before, role |
| 51 | assert b'HUMAN_MESSAGE_CANARY' not in (directory / 'live.json').read_bytes(), role |
| 52 | assert json.loads((directory / 'live.json').read_bytes()) == value, role |
| 53 | print('receiver integration passed: failed renderer logs/retains, successful renderer publishes exact bytes, rejected feed and human-role snapshots retain both artifacts') |
| 54 | |
| 55 | with tempfile.TemporaryDirectory(prefix='hive-receiver-overlap-') as tmp: |
| 56 | directory = Path(tmp) |
| 57 | env = dict(os.environ, SSH_ORIGINAL_COMMAND='hive-feed', HIVE_RENDER=str(renderer)) |
| 58 | initial = dict(value, generated_at='2026-10-26T00:00:00Z', sequence='initial') |
| 59 | newest = dict(value, generated_at='2026-10-26T02:00:00Z', sequence='newest') |
| 60 | delayed = dict(value, generated_at='2026-10-26T01:00:00Z', sequence='delayed') |
| 61 | stream = subprocess.Popen([receiver, directory], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) |
| 62 | try: |
| 63 | stream.stdin.write((json.dumps(initial)+'\n').encode()) |
| 64 | stream.stdin.flush() |
| 65 | deadline = time.monotonic() + 10 |
| 66 | while not (directory / 'board.html').exists(): |
| 67 | assert time.monotonic() < deadline, 'initial stream did not publish' |
| 68 | time.sleep(0.01) |
| 69 | # A is alive, waiting for more stdin. B must not be blocked by A's lock. |
| 70 | subprocess.run([receiver, directory], input=(json.dumps(newest)+'\n').encode(), |
| 71 | env=env, capture_output=True, check=True, timeout=10) |
| 72 | newest_board = (directory / 'board.html').read_bytes() |
| 73 | stream.stdin.write((json.dumps(delayed)+'\n').encode()) |
| 74 | stream.stdin.close() |
| 75 | status = stream.wait(timeout=10) |
| 76 | observed = json.loads((directory / 'live.json').read_bytes())['sequence'] |
| 77 | if vulnerable_control: |
| 78 | assert status == 0 and observed == 'delayed', (status, observed) |
| 79 | print('negative control reproduced: older live stream overwrote newer publication') |
| 80 | else: |
| 81 | assert status != 0 and observed == 'newest', (status, observed) |
| 82 | assert (directory / 'board.html').read_bytes() == newest_board |
| 83 | print('overlap check passed: delayed stream refused; latest JSON and board retained; stdin waits unlocked') |
| 84 | finally: |
| 85 | if stream.poll() is None: |
| 86 | stream.kill() |
| 87 | stream.wait() |