"""Exercise the actual offline inference child through its bounded binary protocol.""" from pathlib import Path import argparse import json import struct import subprocess import time ROOT = Path(__file__).resolve().parents[2] OUT = ROOT / 'build/fortune-ai' def exact(stream, size): data = bytearray() while len(data) < size: chunk = stream.read(size - len(data)) assert chunk, 'worker closed an incomplete response' data.extend(chunk) return bytes(data) def response(worker): magic, status, length = struct.unpack(' 160, text assert sum('а' <= c.lower() <= 'я' for c in text) > 80, text assert '' not in text and '<|im_' not in text if index < len(expected): assert all(card in text.lower().replace('ё', 'е') for card in expected[index]), text results.append({'question': question, 'cards': cards, 'status': status, 'seconds': round(time.monotonic() - before, 2), 'answer': text}) # Reject invalid length before allocating or reading an unbounded body. worker.stdin.write(struct.pack('