feat(orchestrator): S8 shared speech service and sealed model transport

Requirement 001 S8: the hub can use a shared OpenAI-style speech service (KVB_ORCHESTRATOR_SPEECH_URL, key from a mounted file) instead of the local piper and whisper engines, and the image takes its pinned artefacts through an optional INFINITO_ARTEFACT_BASE cache. A new transport serves speech and the LLM: one wall-clock deadline over DNS, connect, TLS handshake and body, certificate and hostname always verified, no redirect followed. A key travels only over https or to loopback; both the speech and the LLM start refuse it otherwise. Key files are capped at 4096 bytes. Streamed WAV answers with placeholder sizes are accepted only when the data chunk runs to the end, and are rewritten before caching. Accepted MEDUSA false positives (41, independent review, operator pick): raw client socket patterns in transport.py, AIC-EXFIL-005 on the sealed key path, JB-SCAN-2237 on the hub's own system prompt, STG009 on the RIFF pad byte and LLM01 at language.py:111, where agent-written titles reach the NL prompt but the model only proposes a plan the operator confirms.

🎯 Fix: 93 %
💥 Break: 4 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 89
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-13 16:57:52 +00:00
1 parent 590b430d37
commit 6d2bca77f7
16 files changed
+1253 -56

No files matched your search

+1 -1
View File
@@ -55,7 +55,7 @@ def write_engines(base: Path) -> server.Engines:
(voices / "broken.onnx").write_bytes(b"onnx")
model = base / "ggml-base.bin"
model.write_bytes(b"ggml")
return server.Engines(piper=bin_dir / "piper", voices=voices, operator_voice=OPERATOR_VOICE, whisper=bin_dir / "whisper-cli", whisper_model=model, scratch=scratch)
return server.Engines(piper=bin_dir / "piper", voices=voices, operator_voice=OPERATOR_VOICE, whisper=bin_dir / "whisper-cli", whisper_model=model, scratch=scratch, service=server.Service(url="", key=""))
def wav_bytes(channels: int, width: int, rate: int, frames: bytes) -> bytes:
+4 -6
View File
@@ -33,7 +33,6 @@ from hub_support import (
verifier,
zero_led,
)
from kvb_orchestrator import language
PASSKEY_PAGE = HUB / "templates" / "page-passkey.js"
PASSKEY_PROBE = """
@@ -198,11 +197,10 @@ class TestApproval(unittest.TestCase):
self.addCleanup(server.server_close)
threading.Thread(target=server.serve_forever, daemon=True).start()
self.addCleanup(server.shutdown)
for name, opener in (("client", hub_api.OPENER), ("llm", language.LLM_OPENER)):
request = urllib.request.Request(f"{hub_api.base_url(server.server_address[1])}/api/asks")
with self.subTest(name=name), self.assertRaises(urllib.error.HTTPError) as caught:
opener.open(request, timeout=5)
self.assertEqual(caught.exception.code, 302)
request = urllib.request.Request(f"{hub_api.base_url(server.server_address[1])}/api/asks")
with self.assertRaises(urllib.error.HTTPError) as caught:
hub_api.OPENER.open(request, timeout=5)
self.assertEqual(caught.exception.code, 302)
def test_attestation_parsing_yields_the_pinned_key_of_each_algorithm(self):
for alg in (-7, -8, -257):
+133
View File
@@ -0,0 +1,133 @@
from __future__ import annotations
import contextlib
import dataclasses
import http.client
import http.server
import io
import threading
import time
import unittest
from unittest import mock
from hub_case import HubCase
from hub_support import server
from kvb_orchestrator import errors, language, transport
KEY = "sk-model-0123456789abcdef"
MALFORMED = "the model endpoint key must be printable ASCII without whitespace: check KVB_ORCHESTRATOR_LLM_KEY_FILE"
PLAIN = "the model endpoint key travels only over https or to 127.0.0.1, localhost or ::1: set an https KVB_ORCHESTRATOR_LLM_URL or empty KVB_ORCHESTRATOR_LLM_KEY_FILE"
class Endpoint(http.server.ThreadingHTTPServer):
def __init__(self) -> None:
super().__init__(("127.0.0.1", 0), Answer)
self.mode = "error"
threading.Thread(target=self.serve_forever, args=(0.05,), daemon=True).start()
def handle_error(self, request, client_address):
return None
class Answer(http.server.BaseHTTPRequestHandler):
def do_POST(self):
self.rfile.read(int(self.headers["Content-Length"]))
if self.server.mode == "trickle":
self.send_response(200)
self.send_header("Content-Length", "100")
self.end_headers()
for _ in range(100):
self.wfile.write(b" ")
time.sleep(0.2)
return
redirect = self.server.mode == "redirect"
body = b"" if redirect else f"upstream broke, your key {self.headers['Authorization']}".encode()
self.send_response(302 if redirect else 500)
if redirect:
self.send_header("Location", "/elsewhere")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, format, *args):
return None
class TestModelKey(unittest.TestCase):
def test_a_key_that_is_no_printable_ascii_token_is_refused_and_never_shown(self):
for key in ("sk-SECRET\n# rotated 2026-11-13", "sk SECRET", "sk-\tSECRET", "sk-SECRET\r", "sk-SÉCRET", "sk-\x7fSECRET", "sk-\x00SECRET"):
with self.subTest(key=key), self.assertRaises(errors.ToolError) as refused:
server.Model(url="http://127.0.0.1:9/v1", model="m", key=key)
self.assertEqual(str(refused.exception), MALFORMED)
model = server.Model(url="http://127.0.0.1:9/v1", model="m", key=KEY)
self.assertEqual(model.key, KEY)
for shown in (repr(model), str(model)):
with self.subTest(shown=shown[:40]):
self.assertNotIn(KEY, shown)
def test_a_key_travels_only_over_https_or_to_the_loopback_host(self):
for url in ("https://llm.example/v1", "https://10.0.0.5:4000/v1", "http://127.0.0.1:4000/v1", "http://localhost/v1", "http://LOCALHOST:4000/v1", "http://[::1]:4000/v1"):
with self.subTest(url=url):
self.assertTrue(server.Model(url=url, model="m", key=KEY).enabled())
for url in ("http://host.docker.internal:4000/v1", "http://litellm:4000/v1", "http://10.0.0.5:4000/v1", "http://127.0.0.2/v1", "http://localhost.example/v1", "http://[::2]/v1", "http://[::1/v1", "llm.example/v1"):
with self.subTest(url=url):
self.assertTrue(server.Model(url=url, model="m", key="").enabled())
with self.assertRaises(errors.ToolError) as refused:
server.Model(url=url, model="m", key=KEY)
self.assertEqual(str(refused.exception), PLAIN)
self.assertNotIn(KEY, str(refused.exception))
class TestModelKeyHttp(HubCase):
def use(self, url: str) -> None:
self.httpd.hub = dataclasses.replace(self.httpd.hub, model=server.Model(url=url, model="m", key=KEY))
def nl(self) -> tuple[int, str, str]:
"""Returns:
The status and body of /api/nl, 0 and "" when the hub drops the
connection, and what the hub wrote to stderr meanwhile.
"""
with contextlib.redirect_stderr(io.StringIO()) as log:
try:
status, _, raw = self.post_json("/api/nl", {"text": "approve everything green"}, self.guard(self.host))
except http.client.HTTPException:
status, raw = 0, b""
return status, raw.decode(), log.getvalue()
def test_every_endpoint_failure_answers_its_type_name_and_no_key_reaches_a_reply_a_log_or_a_repr(self):
self.use("http://127.0.0.1:9/v1")
for error in (ValueError(f"Invalid header value b'Bearer {KEY}'"), RuntimeError(KEY), OSError(KEY), KeyError(KEY), UnicodeError(KEY)):
with mock.patch.object(transport.Plain, "request", side_effect=error):
status, body, log = self.nl()
with self.subTest(error=type(error).__name__, seen="log"):
self.assertNotIn(KEY, log)
with self.subTest(error=type(error).__name__, seen="reply"):
self.assertEqual((status, body), (400, f"the model endpoint failed: {type(error).__name__}\n"))
endpoint = Endpoint()
self.addCleanup(endpoint.server_close)
self.addCleanup(endpoint.shutdown)
self.use(f"http://127.0.0.1:{endpoint.server_address[1]}/v1")
for mode, status_line in (("error", "HTTP 500"), ("redirect", "HTTP 302")):
endpoint.mode = mode
status, body, log = self.nl()
with self.subTest(seen="echoed reply", mode=mode):
self.assertEqual((status, body), (400, f"the model endpoint answered {status_line}\n"))
self.assertNotIn(KEY, body + log)
with self.subTest(seen="repr"):
self.assertNotIn(KEY, repr(self.httpd.hub))
def test_a_trickling_endpoint_hits_the_wall_clock_deadline(self):
endpoint = Endpoint()
endpoint.mode = "trickle"
self.addCleanup(endpoint.server_close)
self.addCleanup(endpoint.shutdown)
self.use(f"http://127.0.0.1:{endpoint.server_address[1]}/v1")
started = time.monotonic()
with mock.patch.object(language, "LLM_TIMEOUT", 0.5):
status, body, _ = self.nl()
self.assertEqual((status, body), (400, "the model endpoint took longer than 0.5 seconds\n"))
self.assertLess(time.monotonic() - started, 2.0)
if __name__ == "__main__":
unittest.main()
+30 -1
View File
@@ -183,13 +183,14 @@ class TestHubSpeech(HubCase):
"--llm-key-file": os.devnull, "--llm-url": "", "--llm-model": "",
"--piper": str(self.engines.piper), "--voices": str(self.engines.voices), "--operator-voice": "en_US-test-high:9", "--whisper": str(self.engines.whisper),
"--whisper-model": str(self.engines.whisper_model), "--scratch": str(self.engines.scratch), "--approvers": str(self.approvers),
"--speech-url": "", "--speech-key-file": os.devnull,
}
def run(given: dict[str, str]) -> subprocess.CompletedProcess:
argv = [part for pair in given.items() for part in pair]
return subprocess.run([sys.executable, str(HUB / "server.py"), *argv], capture_output=True, text=True, timeout=60, check=False)
for name in ("--piper", "--voices", "--operator-voice", "--whisper", "--whisper-model", "--scratch", "--approvers", "--vendor", "--cleanup", "--offices"):
for name in ("--piper", "--voices", "--operator-voice", "--whisper", "--whisper-model", "--scratch", "--approvers", "--vendor", "--cleanup", "--offices", "--speech-url", "--speech-key-file"):
with self.subTest(name=name):
done = run({key: value for key, value in args.items() if key != name})
self.assertEqual(done.returncode, 2, done.stderr)
@@ -197,6 +198,34 @@ class TestHubSpeech(HubCase):
done = run(args)
self.assertEqual(done.returncode, 2, done.stderr)
self.assertIn("the operator voice 'en_US-test-high:9' is not installed", done.stderr)
secret = "op:hunter2"
done = run({**args, "--operator-voice": OPERATOR_VOICE, "--speech-url": f"https://{secret}@speech.example/v1"})
self.assertEqual(done.returncode, 2, done.stderr)
self.assertIn("the speech service URL must be http or https with a host and without userinfo", done.stderr)
self.assertNotIn(secret, done.stderr + done.stdout)
key_file = self.outside / "speech.key"
key_file.write_text("sk-plain-http-0123456789\n", encoding="utf-8")
done = run({**args, "--operator-voice": OPERATOR_VOICE, "--speech-url": "http://svc-ai-speech:8000/v1", "--speech-key-file": str(key_file)})
self.assertEqual(done.returncode, 2, done.stderr)
self.assertIn("the speech service key travels only over https or to 127.0.0.1, localhost or ::1", done.stderr)
self.assertNotIn("sk-plain-http", done.stderr + done.stdout)
done = run({**args, "--operator-voice": OPERATOR_VOICE, "--llm-url": "http://host.docker.internal:4000/v1", "--llm-model": "m", "--llm-key-file": str(key_file)})
self.assertEqual(done.returncode, 2, done.stderr)
self.assertIn("the model endpoint key travels only over https or to 127.0.0.1, localhost or ::1", done.stderr)
self.assertNotIn("sk-plain-http", done.stderr + done.stdout)
key_file.write_text("sk-SECRET-0123\n# rotated 2026-11-13\n", encoding="utf-8")
for key_path, message in ((key_file, "the speech service key must be printable ASCII without whitespace"), (self.outside, f"the speech service key file {self.outside} is unreadable")):
with self.subTest(key_path=key_path):
done = run({**args, "--operator-voice": OPERATOR_VOICE, "--speech-url": "https://speech.example/v1", "--speech-key-file": str(key_path)})
self.assertEqual(done.returncode, 2, done.stderr)
self.assertIn(message, done.stderr)
self.assertNotIn("SECRET", done.stderr + done.stdout)
for key_path, message in ((key_file, "the model endpoint key must be printable ASCII without whitespace"), (self.outside, f"the model endpoint key file {self.outside} is unreadable")):
with self.subTest(llm_key_path=key_path):
done = run({**args, "--operator-voice": OPERATOR_VOICE, "--llm-url": "https://llm.example/v1", "--llm-model": "m", "--llm-key-file": str(key_path)})
self.assertEqual(done.returncode, 2, done.stderr)
self.assertIn(message, done.stderr)
self.assertNotIn("SECRET", done.stderr + done.stdout)
class TestSpeechImage(unittest.TestCase):
+414
View File
@@ -0,0 +1,414 @@
from __future__ import annotations
import dataclasses
import http.client
import http.server
import json
import socket
import struct
import tempfile
import threading
import time
import unittest
from pathlib import Path
from unittest import mock
import hub_case
from hub_case import HubCase
from hub_engines import log_rows, wav_bytes, write_engines
from kvb_orchestrator import engines, errors, keys, speechservice, transport
from kvb_orchestrator.speechservice import Service
KEY = "sk-speech-0123456789abcdef"
VOICE = "en_US-test-high:2"
LINE = "all stations, this is Docs, disk at ninety percent, out"
MAYDAY = "MAYDAY MAYDAY MAYDAY"
SPOKEN = wav_bytes(1, 2, 22050, b"\x01\x00" * 2205)
OVERFLOW = struct.pack("<4sI", b"RIFF", len(SPOKEN) - 108) + SPOKEN[8:-100]
CLIP = wav_bytes(1, 2, engines.STT_RATE, b"\x01\x00" * engines.STT_RATE)
MALFORMED = "the speech service key must be printable ASCII without whitespace: check KVB_ORCHESTRATOR_SPEECH_KEY_FILE"
STREAMING = 0xFFFFFFFF
DATA = SPOKEN.index(b"data")
def streamed(riff: int, data: int, body: bytes = SPOKEN) -> bytes:
at = body.index(b"data", 12)
return body[:4] + struct.pack("<I", riff) + body[8 : at + 4] + struct.pack("<I", data) + body[at + 8 :]
class FakeService(http.server.ThreadingHTTPServer):
def __init__(self) -> None:
super().__init__(("127.0.0.1", 0), FakeHandler)
self.mode = "ok"
self.requests: list[dict] = []
self.thread = threading.Thread(target=self.serve_forever, args=(0.05,), daemon=True)
self.thread.start()
@property
def url(self) -> str:
return f"http://127.0.0.1:{self.server_address[1]}/v1"
def stop(self) -> None:
self.shutdown()
self.server_close()
self.thread.join(10)
def handle_error(self, request, client_address):
return None
class FakeHandler(http.server.BaseHTTPRequestHandler):
def log_message(self, format, *args):
return None
def answer(self, status: int, kind: str, body: bytes, headers: dict[str, str]) -> None:
self.send_response(status)
self.send_header("Content-Type", kind)
self.send_header("Content-Length", str(len(body)))
for name, value in headers.items():
self.send_header(name, value)
self.end_headers()
self.wfile.write(body)
def record(self, method: str, body: bytes) -> None:
self.server.requests.append({"method": method, "path": self.path, "headers": {name.lower(): value for name, value in self.headers.items()}, "body": body})
def do_GET(self):
self.record("GET", b"")
self.answer(200, "audio/wav", SPOKEN, {})
def trickle(self, data: bytes) -> None:
for byte in data:
self.wfile.write(bytes([byte]))
time.sleep(0.2)
def do_POST(self):
body = self.rfile.read(int(self.headers["Content-Length"]))
self.record("POST", body)
mode = "trickle-body" if b"trickle" in body else self.server.mode
speech = self.path.endswith(speechservice.SPEECH_PATH)
if mode == "trickle-body":
self.send_response(200)
self.send_header("Content-Length", str(len(SPOKEN)))
self.end_headers()
self.trickle(SPOKEN)
elif mode == "trickle-head":
self.trickle(b"HTTP/1.1 200 OK\r\n" * 40)
elif mode == "short":
self.send_response(200)
self.send_header("Content-Length", str(len(SPOKEN)))
self.end_headers()
self.wfile.write(SPOKEN[:1000])
self.close_connection = True
elif mode == "truncated":
self.answer(200, "audio/wav", SPOKEN[:-100], {})
elif mode == "overflow":
self.answer(200, "audio/wav", OVERFLOW, {})
elif mode == "error":
self.answer(500, "text/plain", f"upstream broke, your key {self.headers['Authorization']}".encode(), {})
elif mode == "redirect":
self.answer(302, "text/plain", b"", {"Location": "/elsewhere"})
elif mode == "garbage":
self.answer(200, "audio/wav", b"RIFF no wave here", {})
elif mode == "notext":
self.answer(200, "application/json", b'{"words": []}', {})
elif mode == "slow":
time.sleep(1.5)
self.answer(200, "audio/wav", SPOKEN, {})
elif mode == "big":
self.answer(200, "audio/wav", SPOKEN + b"\x00" * 4096, {})
elif mode == "streamed":
self.answer(200, "audio/wav", streamed(STREAMING, STREAMING), {})
elif mode == "streamed-short":
self.answer(200, "audio/wav", streamed(STREAMING, len(SPOKEN) - DATA - 8)[:-100], {})
elif speech:
self.answer(200, "audio/wav", SPOKEN, {})
else:
self.answer(200, "application/json", json.dumps({"text": " hello [BLANK_AUDIO] from the service "}).encode(), {})
class TestServiceUrl(unittest.TestCase):
def test_only_a_plain_http_or_https_base_is_accepted_and_empty_keeps_the_engines(self):
self.assertFalse(Service(url="", key="").enabled())
for url in ("http://127.0.0.1:8000/v1", "https://speech.example/v1", "http://svc-ai-speech/v1", "https://[::1]:8443/v1"):
with self.subTest(url=url):
self.assertTrue(Service(url=url, key="").enabled())
for url in (
"ftp://speech.example/v1", "speech.example/v1", "https:///v1", "https://user:pass@speech.example/v1", "https://speech.example/v1?key=1",
"https://speech.example/v1#top", "https://speech.example:0/v1", "https://speech.example:99999/v1", "https://speech.example/v 1",
"https://speech.example/%761", "https://speech.example/v1\n", "https://spéech.example/v1", "file:///etc/passwd",
):
with self.subTest(url=url), self.assertRaises(errors.ToolError) as refused:
Service(url=url, key=KEY)
self.assertNotIn("pass", str(refused.exception))
def test_a_key_travels_only_over_https_or_to_the_loopback_host(self):
for url in ("https://speech.example/v1", "https://10.0.0.5:8443/v1", "http://127.0.0.1:8000/v1", "http://localhost/v1", "http://LOCALHOST:9000/v1", "http://[::1]:8000/v1"):
with self.subTest(url=url):
self.assertTrue(Service(url=url, key=KEY).enabled())
for url in ("http://svc-ai-speech/v1", "http://10.0.0.5:8000/v1", "http://127.0.0.2/v1", "http://localhost.example/v1", "http://[::2]/v1", "http://speech.example/v1"):
with self.subTest(url=url):
self.assertTrue(Service(url=url, key="").enabled())
with self.assertRaises(errors.ToolError) as refused:
Service(url=url, key=KEY)
self.assertEqual(str(refused.exception), "the speech service key travels only over https or to 127.0.0.1, localhost or ::1: set an https KVB_ORCHESTRATOR_SPEECH_URL or empty KVB_ORCHESTRATOR_SPEECH_KEY_FILE")
self.assertNotIn(KEY, str(refused.exception))
class TestServiceKey(unittest.TestCase):
def test_a_key_that_is_no_printable_ascii_token_is_refused_and_never_shown(self):
for key in ("sk-SECRET\n# rotated 2026-11-13", "sk SECRET", "sk-\tSECRET", "sk-SECRET\r", "sk-SÉCRET", "sk-\x7fSECRET", "sk-\x00SECRET"):
with self.subTest(key=key), self.assertRaises(errors.ToolError) as refused:
Service(url="https://speech.example/v1", key=key)
self.assertEqual(str(refused.exception), MALFORMED)
service = Service(url="https://speech.example/v1", key=KEY)
built = engines.Engines(piper=Path("p"), voices=Path("v"), operator_voice="", whisper=Path("w"), whisper_model=Path("m"), scratch=Path("s"), service=service)
for shown in (repr(service), str(service), repr(built)):
with self.subTest(shown=shown[:40]):
self.assertNotIn(KEY, shown)
def test_the_key_file_is_read_strictly_and_an_unreadable_one_stops_the_start(self):
folder = tempfile.TemporaryDirectory()
self.addCleanup(folder.cleanup)
base = Path(folder.name)
(base / "key").write_text(f" {KEY}\n", encoding="utf-8")
(base / "binary").write_bytes(b"\xffsk-SECRET")
self.assertEqual((keys.key_from(Path("/dev/null"), "speech service"), keys.key_from(base / "key", "speech service")), ("", KEY))
for path in (base, base / "missing", base / "binary"):
with self.subTest(path=path), self.assertRaises(errors.ToolError) as refused:
keys.key_from(path, "speech service")
self.assertEqual(str(refused.exception), f"the speech service key file {path} is unreadable")
self.assertIsNone(refused.exception.__cause__)
def test_a_key_file_over_the_limit_stops_the_start_without_reading_on(self):
folder = tempfile.TemporaryDirectory()
self.addCleanup(folder.cleanup)
base = Path(folder.name)
(base / "full").write_text("k" * keys.KEY_LIMIT, encoding="utf-8")
(base / "huge").write_text("k" * (keys.KEY_LIMIT + 1), encoding="utf-8")
self.assertEqual(keys.key_from(base / "full", "speech service"), "k" * keys.KEY_LIMIT)
for path in (base / "huge", Path("/dev/zero")):
with self.subTest(path=path):
with self.assertRaises(errors.ToolError) as refused:
keys.key_from(path, "speech service")
self.assertEqual(str(refused.exception), f"the speech service key file {path} holds more than {keys.KEY_LIMIT} bytes")
def test_every_failure_of_the_request_becomes_its_type_name_only(self):
service = Service(url="http://127.0.0.1:9/v1", key=KEY)
for error in (ValueError(f"Invalid header value b'Bearer {KEY}'"), RuntimeError(KEY), http.client.InvalidURL(KEY), OSError(KEY)):
with self.subTest(error=type(error).__name__), mock.patch.object(http.client.HTTPConnection, "request", side_effect=error):
with self.assertRaises(errors.EngineError) as failed:
service.speak(VOICE, LINE)
self.assertEqual(str(failed.exception), f"the speech service failed: {type(error).__name__}")
self.assertTrue(failed.exception.__suppress_context__)
class TestTransportDeadline(unittest.TestCase):
def speak(self, url: str) -> tuple[str, float]:
started = time.monotonic()
with mock.patch.object(speechservice, "SERVICE_TIMEOUT", 0.5), self.assertRaises(errors.EngineError) as failed:
Service(url=url, key="").speak(VOICE, LINE)
return str(failed.exception), time.monotonic() - started
def test_a_stalled_name_lookup_ends_at_the_deadline(self):
with mock.patch.object(transport.socket, "getaddrinfo", side_effect=lambda *args, **kwargs: time.sleep(3)):
message, took = self.speak("http://speech.example/v1")
self.assertEqual(message, "the speech service took longer than 0.5 seconds")
self.assertLess(took, 1.5)
def test_a_stalled_connect_gets_only_the_time_left_across_every_address(self):
timeouts: list[float] = []
def stall(sock: socket.socket, address: tuple) -> None:
timeouts.append(sock.gettimeout())
time.sleep(sock.gettimeout())
raise TimeoutError
addresses = [(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", ("127.0.0.1", 9))] * 3
with mock.patch.object(transport.Call, "resolve", return_value=addresses), mock.patch.object(socket.socket, "connect", stall):
message, took = self.speak("http://speech.example/v1")
self.assertEqual(message, "the speech service took longer than 0.5 seconds")
self.assertLess(took, 1.5)
self.assertTrue(timeouts and sum(timeouts) <= 0.5, timeouts)
def test_a_stalled_tls_handshake_ends_at_the_deadline(self):
listener = socket.create_server(("127.0.0.1", 0))
self.addCleanup(listener.close)
message, took = self.speak(f"https://127.0.0.1:{listener.getsockname()[1]}/v1")
self.assertEqual(message, "the speech service took longer than 0.5 seconds")
self.assertLess(took, 1.5)
def test_a_watchdog_that_cannot_start_fails_with_its_type_name(self):
with mock.patch.object(transport.threading.Timer, "start", side_effect=RuntimeError(KEY)):
message, _ = self.speak("http://127.0.0.1:9/v1")
self.assertEqual(message, "the speech service failed: RuntimeError")
class TestWholeWav(unittest.TestCase):
def test_only_a_wav_whose_sizes_match_the_bytes_received_counts(self):
chunks = struct.pack("<4sI", b"LIST", 1) + b"x\x00" + SPOKEN[12:]
odd = struct.pack("<4sI4s", b"RIFF", len(chunks) + 4, b"WAVE") + chunks
self.assertEqual([engines.whole_wav(body) for body in (SPOKEN, odd)], [SPOKEN, odd])
empty = wav_bytes(1, 2, 22050, b"")
for body in (SPOKEN[:-100], OVERFLOW, SPOKEN + b"\x00", SPOKEN[:-1], b"", b"RIFF", empty, SPOKEN.replace(b"WAVE", b"AVI ", 1)):
with self.subTest(size=len(body)):
self.assertIsNone(engines.whole_wav(body))
def test_a_streamed_wav_counts_only_if_its_data_chunk_is_last_and_runs_to_the_end(self):
real = len(SPOKEN) - DATA - 8
chunks = struct.pack("<4sI", b"LIST", 1) + b"x\x00" + SPOKEN[12:]
odd = struct.pack("<4sI4s", b"RIFF", len(chunks) + 4, b"WAVE") + chunks
for riff, data in ((STREAMING, STREAMING), (0, 0), (STREAMING, 0), (0, STREAMING), (STREAMING, real), (0, real)):
with self.subTest(riff=riff, data=data):
self.assertEqual(engines.whole_wav(streamed(riff, data)), SPOKEN)
self.assertEqual(engines.whole_wav(streamed(STREAMING, STREAMING, odd)), odd)
trailer = struct.pack("<4sI", b"LIST", 2) + b"xy"
for name, body in (
("data not last", streamed(STREAMING, real) + trailer),
("truncated with a real size", streamed(STREAMING, real)[:-100]),
("real size short of the end", streamed(STREAMING, real - 2)),
("no audio", streamed(STREAMING, STREAMING)[: DATA + 8]),
("no data chunk", streamed(STREAMING, STREAMING)[:DATA]),
("real RIFF, placeholder data", streamed(len(SPOKEN) - 8, STREAMING)),
("real RIFF, zero data", streamed(len(SPOKEN) - 8, 0)),
("mismatched RIFF, placeholder data", streamed(len(SPOKEN), STREAMING)),
("placeholder RIFF, truncated fmt", streamed(STREAMING, STREAMING)[:30]),
):
with self.subTest(name=name):
self.assertIsNone(engines.whole_wav(body))
class TestServiceEngines(unittest.TestCase):
def test_with_the_service_no_local_piper_or_whisper_is_needed_but_the_voices_are(self):
folder = tempfile.TemporaryDirectory()
self.addCleanup(folder.cleanup)
base = Path(folder.name)
built = write_engines(base)
gone = {"piper": base / "missing-piper", "whisper": base / "missing-whisper", "whisper_model": base / "missing.bin"}
remote = engines.Speech(dataclasses.replace(built, service=Service(url="http://127.0.0.1:9/v1", key=""), **gone), base / "remote")
local = engines.Speech(dataclasses.replace(built, **gone), base / "local")
self.assertEqual([remote.describe()[key] for key in ("tts", "stt", "tts_off", "stt_off")], [True, True, "", ""])
self.assertEqual([local.describe()[key] for key in ("tts", "stt")], [False, False])
voiceless = engines.Speech(dataclasses.replace(built, service=Service(url="http://127.0.0.1:9/v1", key=""), voices=base / "none"), base / "voiceless")
self.assertEqual((voiceless.describe()["tts"], voiceless.describe()["stt"]), (False, True))
class TestSpeechServiceHttp(HubCase):
def setUp(self):
self.service = FakeService()
self.addCleanup(self.service.stop)
original = hub_case.write_engines
def remote(base: Path) -> engines.Engines:
return dataclasses.replace(original(base), service=Service(url=self.service.url, key=KEY))
with mock.patch.object(hub_case, "write_engines", remote):
super().setUp()
self.hand_out(VOICE)
def tts(self, text: str) -> tuple[int, dict, bytes]:
return self.post_json("/api/tts", {"text": text, "voice": VOICE}, self.guard(self.host))
def stt(self, clip: bytes) -> tuple[int, dict, bytes]:
return self.call("POST", "/api/stt", clip, {**self.guard(self.host), "Content-Type": "audio/wav"})
def local_calls(self) -> list[dict]:
return log_rows(self.engines.scratch / "piper.jsonl") + log_rows(self.engines.scratch / "whisper.jsonl")
def test_speech_output_renders_at_the_service_behind_the_cpu_gate_and_the_cache(self):
self.host_cpu(10.0, 5)
status, headers, body = self.tts(LINE)
self.assertEqual((status, headers["content-type"], body), (200, "audio/wav", SPOKEN))
[sent] = self.service.requests
self.assertEqual((sent["method"], sent["path"], sent["headers"]["authorization"], sent["headers"]["content-type"]), ("POST", "/v1/audio/speech", f"Bearer {KEY}", "application/json"))
self.assertEqual(json.loads(sent["body"]), {"model": speechservice.SPEECH_MODEL, "input": LINE, "voice": VOICE, "response_format": "wav"})
self.assertEqual(self.tts(LINE)[::2], (200, SPOKEN))
self.assertEqual(len(self.service.requests), 1)
for cpu, age, reason in ((99.0, 5, "busy"), (None, 5, "unknown"), (10.0, 900, "unknown")):
with self.subTest(cpu=cpu, age=age):
self.host_cpu(cpu, age)
status, _, body = self.tts(f"{LINE} again")
self.assertEqual((status, json.loads(body)), (503, {"reason": reason}))
self.assertEqual(self.tts(LINE)[::2], (200, SPOKEN))
self.assertEqual(len(self.service.requests), 1)
self.host_cpu(99.0, 5)
self.assertEqual(self.tts(MAYDAY)[0], 200)
self.assertEqual(json.loads(self.service.requests[-1]["body"])["input"], MAYDAY)
self.assertEqual(self.local_calls(), [])
def test_a_streamed_wav_with_placeholder_sizes_is_served_and_cached_with_the_real_sizes(self):
self.host_cpu(10.0, 5)
self.service.mode = "streamed"
self.assertEqual(self.tts(LINE)[::2], (200, SPOKEN))
self.assertEqual(self.tts(LINE)[::2], (200, SPOKEN))
self.assertEqual(len(self.service.requests), 1)
def test_speech_input_transcribes_at_the_service_within_the_hub_limits(self):
status, headers, body = self.stt(CLIP)
self.assertEqual((status, headers["content-type"], json.loads(body)), (200, "application/json", {"text": "hello from the service"}))
[sent] = self.service.requests
kind = sent["headers"]["content-type"]
boundary = kind.removeprefix("multipart/form-data; boundary=")
self.assertEqual((sent["path"], sent["headers"]["authorization"]), ("/v1/audio/transcriptions", f"Bearer {KEY}"))
self.assertNotEqual(boundary, kind)
parts = sent["body"].split(f"--{boundary}".encode())
self.assertEqual(parts[-1], b"--\r\n")
self.assertIn(f'name="model"\r\n\r\n{speechservice.HEARING_MODEL}\r\n'.encode(), parts[1])
self.assertIn(b'name="file"; filename="recording.wav"\r\nContent-Type: audio/wav\r\n\r\n' + CLIP + b"\r\n", sent["body"])
self.assertEqual(self.stt(b"not a wav")[0], 400)
with mock.patch.object(engines, "HEARING_RATE", 1):
self.assertEqual(self.stt(CLIP)[0], 503)
self.assertEqual(len(self.service.requests), 1)
self.assertEqual(self.local_calls(), [])
def test_a_failing_service_answers_502_without_the_key_and_leaves_no_partial_file(self):
self.host_cpu(10.0, 5)
cases = (
("error", "tts", "the speech service answered HTTP 500"),
("redirect", "tts", "the speech service answered HTTP 302"),
("garbage", "tts", "the speech service answered no complete WAV"),
("truncated", "tts", "the speech service answered no complete WAV"),
("overflow", "tts", "the speech service answered no complete WAV"),
("streamed-short", "tts", "the speech service answered no complete WAV"),
("short", "tts", "the speech service answered a truncated reply"),
("big", "tts", f"the speech service answered more than {len(SPOKEN)} bytes"),
("slow", "tts", "the speech service took longer than 0.5 seconds"),
("error", "stt", "the speech service answered HTTP 500"),
("notext", "stt", "the speech service answered no transcript"),
("short", "stt", "the speech service answered a truncated reply"),
)
for number, (mode, route, message) in enumerate(cases):
with self.subTest(mode=mode, route=route), mock.patch.object(speechservice, "SPOKEN_REPLY", len(SPOKEN)), mock.patch.object(speechservice, "SERVICE_TIMEOUT", 0.5):
self.service.mode = mode
status, _, body = self.tts(f"{LINE} {number}") if route == "tts" else self.stt(CLIP)
self.assertEqual((status, body.decode()), (502, f"{message}\n"))
self.assertNotIn(KEY, body.decode())
self.assertNotIn("GET", [sent["method"] for sent in self.service.requests])
self.assertEqual([path.name for path in (self.state / "tts").iterdir()], [])
self.service.stop()
self.service.mode = "ok"
self.assertEqual(self.tts(f"{LINE} refused")[::2], (502, b"the speech service failed: ConnectionRefusedError\n"))
self.assertEqual(self.local_calls(), [])
def test_a_trickling_service_hits_the_wall_clock_deadline_and_frees_the_speech_lock(self):
self.host_cpu(10.0, 5)
late = b"the speech service took longer than 1 seconds\n"
with mock.patch.object(speechservice, "SERVICE_TIMEOUT", 1.0):
started = time.monotonic()
outcome: list[tuple[int, bytes]] = []
stuck = threading.Thread(target=lambda: outcome.append(self.tts(f"{LINE} trickle")[::2]))
stuck.start()
time.sleep(0.3)
self.assertEqual(self.tts(MAYDAY)[::2], (200, SPOKEN))
stuck.join(10)
self.assertEqual(outcome, [(502, late)])
self.assertLess(time.monotonic() - started, 3.0)
self.service.mode = "trickle-head"
started = time.monotonic()
self.assertEqual(self.stt(CLIP)[::2], (502, late))
self.assertLess(time.monotonic() - started, 3.0)
self.assertEqual(sorted(path.name.startswith(engines.WORD_CACHE) for path in (self.state / "tts").iterdir()), [True])
if __name__ == "__main__":
unittest.main()
+150
View File
@@ -0,0 +1,150 @@
from __future__ import annotations
import re
import unittest
from hub_support import HUB
NAME = "INFINITO_ARTEFACT_BASE"
EXPRESSION = re.compile(r"\$\{([A-Za-z_]\w*)(?::([-+])([^}]*))?\}")
COMMIT = "d1be6fde11ac6e0407606b4e42fe72d34add8037"
VOICE = "huggingface.co/rhasspy/piper-voices/resolve/e7229f7d33a10e145e109b886d661984201ac3c4/en/en_US/libritts/high/en_US-libritts-high.onnx"
PINNED = {
VOICE: "9127a559e11603f10b366d1a20ac7426826081dbc521de4c2420c57728d73f0f",
f"{VOICE}.json": "2efdc6d7f954588b8180132cbd9b8001933fdd00932c92bc92fd0d2028a9eb3d",
"huggingface.co/ggerganov/whisper.cpp/resolve/5359861c739e955e79d9a303bcbc70fb988958b1/ggml-base-q8_0.bin": "c577b9a86e7e048a0b7eada054f4dd79a56bbfa911fbdacf900ac5b567cbb7d9",
}
ARCHIVE = (f"codeload.github.com/ggml-org/whisper.cpp/tar.gz/{COMMIT}", "645445da884df6dbd52b30f82d7be7c01d1767c938714e477e786b88d529e9f4")
CLONE = ("clone", "https://github.com/ggml-org/whisper.cpp", f"v1.9.5@{COMMIT}")
def expand(text: str, values: dict[str, str]) -> str:
"""Args:
text: one instruction.
values: the build args in scope; any other name expands as unset.
Returns:
text with every ${name}, ${name:-word} and ${name:+word} expanded as
BuildKit does, an empty value counting as unset.
"""
def value(match: re.Match) -> str:
name, operator, word = match.groups()
current = values.get(name, "")
if operator == "-":
return current or word
if operator == "+":
return word if current else ""
return current
return EXPRESSION.sub(value, text)
def instructions(text: str) -> list[str]:
joined = text.replace("\\\n", " ")
return [" ".join(line.split()) for line in joined.splitlines() if line.strip() and not line.lstrip().startswith("#")]
def stages(text: str) -> tuple[set[str], list[dict]]:
"""Returns:
The global ARG names and every stage as {"name", "base", "steps"} in
file order; a stage without AS is named by its index.
"""
shared: set[str] = set()
found: list[dict] = []
for step in instructions(text):
word, _, rest = step.partition(" ")
if word == "FROM":
parts = rest.split()
found.append({"name": parts[2] if parts[1:2] == ["AS"] else str(len(found)), "base": parts[0], "steps": []})
elif found:
found[-1]["steps"].append(step)
elif word == "ARG":
shared.add(rest.split("=")[0])
return shared, found
def rendered(build_args: dict[str, str]) -> tuple[list[tuple[str, str, str]], list[str]]:
"""Args:
build_args: the --build-arg values; a missing name is unset.
Returns:
Every fetch of the stages the final image needs, as (kind, url, pin),
sorted, and every step of those stages after expansion.
"""
shared, found = stages((HUB / "Dockerfile").read_text(encoding="utf-8"))
index = {stage["name"]: number for number, stage in enumerate(found)}
wanted, seen, fetches, steps = [len(found) - 1], set(), [], []
while wanted:
number = wanted.pop()
if number in seen:
continue
seen.add(number)
stage = found[number]
base = expand(stage["base"], {name: build_args[name] for name in shared if name in build_args})
wanted += [index[base]] if base in index else []
scope: dict[str, str] = {}
for raw in stage["steps"]:
if raw.startswith("ARG "):
name = raw[4:].split("=")[0]
scope[name] = build_args.get(name, "")
continue
step = expand(raw, scope)
steps.append(step)
source = re.search(r"--from=(\S+)", step)
wanted += [index[source[1]]] if step.startswith("COPY ") and source else []
added = re.fullmatch(r"ADD --checksum=sha256:([0-9a-f]{64}) (?:--chmod=\S+ )?(\S+) (\S+)", step)
if added:
fetches.append(("add", added[2], added[1]))
cloned = re.search(r"git clone --depth 1 --branch (\S+) (\S+) /src && test \"\$\(cd /src && git rev-parse HEAD\)\" = ([0-9a-f]{40})", step)
if cloned:
fetches.append(("clone", cloned[2], f"{cloned[1]}@{cloned[3]}"))
return sorted(fetches), steps
def through(base: str) -> list[tuple[str, str, str]]:
return sorted([("add", f"{base}/{ARCHIVE[0]}", ARCHIVE[1]), *(("add", f"{base}/{path}", pin) for path, pin in PINNED.items())])
class TestArtefactBase(unittest.TestCase):
def test_unset_the_build_fetches_the_upstream_urls_and_clones_as_before(self):
upstream = sorted([CLONE, *(("add", f"https://{path}", pin) for path, pin in PINNED.items())])
for build_args in ({}, {NAME: ""}):
with self.subTest(build_args=build_args):
self.assertEqual(rendered(build_args)[0], upstream)
def test_set_every_fetch_goes_through_the_base_with_the_same_pins(self):
for base in ("http://10.0.0.5", "http://10.0.0.5:8081/cache", "https://cache.example"):
with self.subTest(base=base):
fetches, steps = rendered({NAME: base})
self.assertEqual(fetches, through(base))
self.assertEqual([step for step in steps if "https://" in step.replace(base, "") or "git clone" in step], [])
self.assertEqual(rendered({NAME: "https:/"})[0], through("https:/"))
self.assertEqual(through("https:/"), sorted([("add", f"https://{ARCHIVE[0]}", ARCHIVE[1]), *(("add", f"https://{path}", pin) for path, pin in PINNED.items())]))
def test_every_add_is_pinned_and_reads_the_base_and_each_stage_declares_it(self):
shared, found = stages((HUB / "Dockerfile").read_text(encoding="utf-8"))
self.assertIn(NAME, shared)
for stage in found:
adds = [step for step in stage["steps"] if step.startswith("ADD ")]
with self.subTest(stage=stage["name"]):
for step in adds:
self.assertRegex(step, r"^ADD --checksum=sha256:[0-9a-f]{64} (?:--chmod=\S+ )?\$\{" + NAME + r":-https:/\}/\S+ /\S+$")
declared = [number for number, other in enumerate(stage["steps"]) if other == f"ARG {NAME}"]
self.assertTrue(declared and declared[0] < stage["steps"].index(step), step)
self.assertEqual(sum(step.startswith("ADD ") for stage in found for step in stage["steps"]), len(PINNED) + 1)
class TestComposeWiring(unittest.TestCase):
def test_compose_hands_the_base_to_the_build_and_the_speech_service_to_the_hub(self):
compose = (HUB / "compose.yml").read_text(encoding="utf-8")
self.assertRegex(compose, r"(?m)^ build:\n context: \.\n args:\n - " + NAME + "$")
self.assertIn(' - --speech-url\n - "${KVB_ORCHESTRATOR_SPEECH_URL}"\n - --speech-key-file\n - /run/kvb-orchestrator/speech.key\n', compose)
self.assertIn(' source: "${KVB_ORCHESTRATOR_SPEECH_KEY_FILE:-/dev/null}"\n target: /run/kvb-orchestrator/speech.key\n read_only: true\n', compose)
env = (HUB / ".env").read_text(encoding="utf-8").splitlines()
self.assertLessEqual({"KVB_ORCHESTRATOR_SPEECH_URL=", "KVB_ORCHESTRATOR_SPEECH_KEY_FILE="}, set(env))
self.assertNotIn(NAME, "\n".join(env))
if __name__ == "__main__":
unittest.main()