Files
skills/orchestrator/kvb_orchestrator/speechgate.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 acf39255fc feat(orchestrator): radio set with CPU-gated speech, noise burst, modes and log (requirement 004)
The hub gates uncached /api/tts renders on the latest W4 host CPU sample (503 busy or unknown, cached lines always served, signed speech.threshold 30 to 90 in steps of 10, speech.gate in /api/state). The page plays a WebAudio squelch-tail burst when no speech plays, offers Auto, Quiet and Silence per browser, and always sounds the MAYDAY, PAN-PAN and SECURITE procedure word. The radio panel becomes a minimalist VHF faceplate with display line, mode buttons, knobs, PTT send and the internet station; the settings dialog loses its radio section. The log gets Sent, Received and All tabs with class, sender, recipient and agent chips plus text search. Decisions 3 and 10 of 004 now state that the procedure word still sounds in Silence.

Security: MEDUSA-MEX-SCAN-1167 at orchestrator/kvb_orchestrator/hub.py:126 accepted as a false positive after the round 5 skeptic review and the operator pick of 2026-11-13 (regex hit on rce inside RESOURCES; CpuGate reads host CPU samples, no model).

Break risk 6 % is above the 5 % bar; committed and merged on the operator pick of 2026-11-13 for this bundle.

🎯 Fix: 92 %
💥 Break: 6 %
💸 Cost: 100 %
⏱️ Time: 100 %
🛡️ Security: 100 %
🏆 Score: 86
🚦 Gate: ⛔ break 6 % > 5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 10:48:13 +00:00

93 lines
3.0 KiB
Python

from __future__ import annotations
import os
import stat
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from .errors import Held
from .phrasing import PROCEDURE_LINES
from .records import READ_FLAGS, epoch, parse_line
FRESH = 120
TAIL = 1 << 16
def tail_lines(path: Path, size: int) -> list[str]:
"""Args:
path: a file the host sampler appends to; a link, FIFO or other
non-regular file reads as empty without blocking.
size: the most bytes to read from its end.
Returns:
The complete lines within the last size bytes, oldest first; [] when
the file cannot be read.
"""
try:
with open(os.open(path, READ_FLAGS), "rb") as handle:
info = os.fstat(handle.fileno())
if not stat.S_ISREG(info.st_mode):
return []
start = max(info.st_size - size, 0)
handle.seek(start)
data = handle.read(size)
except OSError:
return []
lines = data.decode("utf-8", errors="replace").splitlines()
return lines[1:] if start else lines
def latest_cpu(path: Path, now: float) -> float | None:
"""Args:
path: samples.jsonl of the W4 resource feed.
now: the current time in epoch seconds.
Returns:
The cpu percent of the newest timed row, when that row lies at most
FRESH seconds from now and carries a number from 0 to 100; else None.
"""
for row in map(parse_line, reversed(tail_lines(path, TAIL))):
t = None if row is None else epoch(row.get("t"))
if t is None:
continue
cpu = row.get("cpu")
fresh = abs(now - t) <= FRESH
return float(cpu) if fresh and not isinstance(cpu, bool) and isinstance(cpu, int | float) and 0 <= cpu <= 100 else None
return None
@dataclass(frozen=True)
class CpuGate:
samples: Path
threshold: Callable[[], int]
clock: Callable[[], float]
def state(self) -> dict:
"""Returns:
{"threshold", "cpu", "open"}: open only while the latest fresh
sample lies below the threshold; cpu None without one.
"""
cpu = latest_cpu(self.samples, self.clock())
limit = self.threshold()
return {"threshold": limit, "cpu": cpu, "open": cpu is not None and cpu < limit}
def admit(self, line: str) -> None:
"""Args:
line: the uncached line about to be rendered in a voice the hub
hands out; a procedure word line always renders, it is the
same few lines per voice and outlives the cache trim.
Returns:
None when the line may render; otherwise it raises Held busy
while the latest fresh sample is at or above the threshold and
Held unknown without a fresh sample.
"""
if line in PROCEDURE_LINES:
return
gate = self.state()
if gate["cpu"] is None:
raise Held("unknown")
if not gate["open"]:
raise Held("busy")