Files
skills/orchestrator/kvb_orchestrator/speechgate.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 86d539a95f feat(hub): radio Morse mode with WPM speed, per-agent pitch and a 0-100 % speech threshold
Morse keys every picked row whole: class prosign (SOS for MAYDAY), DE with the sender's clear name and the full text, after PARIS timing at the hub's morse.wpm (5-40, default 20) and in the sender's own 550-850 Hz tone from the same sha256 slot as its voice. A higher class cuts a keying row, which keys again whole after it. The speech threshold runs 0-100 % in steps of 10, 100 always open, 0 never. The History card moves into page-radiolog.js to keep page-radioview.js under 500 lines.

🎯 Fix: 85 %
💥 Break: 7 %
💸 Tokens: 100 %
⏱️ Velocity: 100 %
🛡️ Security: 100 %
🏆 Score: 79
🚦 Gate: ⛔ fix 85 % < 90, break 7 % > 5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:46:05 +00:00

94 lines
3.1 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
ALWAYS = 100
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 while the latest fresh sample
lies below the threshold, and at a threshold of ALWAYS even
without one, so a threshold of 0 never opens; cpu None without a
fresh sample.
"""
cpu = latest_cpu(self.samples, self.clock())
limit = self.threshold()
return {"threshold": limit, "cpu": cpu, "open": limit >= ALWAYS or (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, while the gate is shut,
it raises Held unknown without a fresh sample and Held busy with
one.
"""
if line in PROCEDURE_LINES:
return
gate = self.state()
if not gate["open"]:
raise Held("unknown" if gate["cpu"] is None else "busy")