From acf39255fcab26ffb7b89893c60e6069be1b9bdd Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 13 Nov 2026 10:48:13 +0000 Subject: [PATCH] feat(orchestrator): radio set with CPU-gated speech, noise burst, modes and log (requirement 004) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- docs/requirements/004-radio-set.md | 50 +- orchestrator/client/signing.py | 4 + orchestrator/kvb_orchestrator/engines.py | 33 +- orchestrator/kvb_orchestrator/errors.py | 9 + orchestrator/kvb_orchestrator/exchange.py | 4 +- orchestrator/kvb_orchestrator/handler.py | 3 +- orchestrator/kvb_orchestrator/hub.py | 8 +- orchestrator/kvb_orchestrator/phrasing.py | 65 +- orchestrator/kvb_orchestrator/settings.py | 72 +-- orchestrator/kvb_orchestrator/speechgate.py | 92 +++ orchestrator/kvb_orchestrator/view.py | 29 +- orchestrator/templates/index.html.j2 | 2 + orchestrator/templates/page-alerts.js | 13 +- orchestrator/templates/page-board.js | 8 +- orchestrator/templates/page-boot.js | 2 +- orchestrator/templates/page-composer.js | 2 +- orchestrator/templates/page-constants.js | 2 + orchestrator/templates/page-load.js | 30 +- orchestrator/templates/page-radioview.js | 232 ++++++- orchestrator/templates/page-settings.js | 64 +- orchestrator/templates/page-speech.js | 228 +++++-- orchestrator/templates/page-state.js | 3 +- orchestrator/templates/radioset.js | 160 +++++ orchestrator/templates/style-ui.css | 49 +- orchestrator/templates/voice.js | 9 +- tests/e2e/test_orchestrator_e2e.py | 83 +-- tests/e2e/test_radio_set_e2e.py | 503 ++++++++++++++++ tests/e2e/test_workbench_w6_e2e.py | 12 +- tests/hub_case.py | 24 + tests/test_hub_bubbles.py | 22 +- tests/test_hub_radioset.py | 635 ++++++++++++++++++++ tests/test_hub_settings.py | 52 +- tests/test_hub_speech.py | 4 + tests/test_hub_speech_gate.py | 283 +++++++++ tests/test_hub_state.py | 2 +- tests/test_hub_surface.py | 11 +- tests/test_hub_voice.py | 32 +- 37 files changed, 2480 insertions(+), 356 deletions(-) create mode 100644 orchestrator/kvb_orchestrator/speechgate.py create mode 100644 orchestrator/templates/radioset.js create mode 100644 tests/e2e/test_radio_set_e2e.py create mode 100644 tests/test_hub_radioset.py create mode 100644 tests/test_hub_speech_gate.py diff --git a/docs/requirements/004-radio-set.md b/docs/requirements/004-radio-set.md index c091161..4da0de0 100644 --- a/docs/requirements/004-radio-set.md +++ b/docs/requirements/004-radio-set.md @@ -21,8 +21,8 @@ that someone transmits without the speech engine loading a busy host. 0.6 s, at the tone's volume and squelch); no server request. Rejected: a recorded sound file. 3. **Modes**: Auto (speech below the threshold, otherwise the burst), Quiet - (the burst only, never speech), Silence (no sound; bubbles and log only). - Default Auto. + (the burst only, never speech), Silence (no sound except the procedure + word of decision 4; bubbles and log only). Default Auto. 4. **Procedure words**: for MAYDAY, PAN-PAN and SÉCURITÉ the procedure word, spoken three times in the sender's voice, always sounds, in every mode and under load; under load or in Quiet the burst replaces the rest of the @@ -47,6 +47,12 @@ that someone transmits without the speech engine loading a busy host. (high effort, no gain). 9. **Order**: builds on W4's resource feed; implementation starts once W4 is on master. +10. **Silence scope** (operator-confirmed 2026-11-13): Silence mutes only the + radio set (speech, bursts, alert beeps); the procedure word of decision 4 + still sounds, and music and the internet station keep playing. Rejected: + Silence also pauses music and the station (not part of 004's scope); + Silence also mutes the procedure word (an emergency call could go + unheard). ## Approach @@ -91,54 +97,54 @@ flowchart TD ### Server gate -- [ ] `POST /api/tts` with an uncached line answers 503 `{"reason": "busy"}` +- [x] `POST /api/tts` with an uncached line answers 503 `{"reason": "busy"}` while the latest `samples.jsonl` row has `cpu` >= the threshold, and the fake Piper records no call. -- [ ] Without a sample younger than 120 s it answers 503 +- [x] Without a sample younger than 120 s it answers 503 `{"reason": "unknown"}`, and the fake Piper records no call. -- [ ] A cached line answers 200 whatever the CPU. -- [ ] Below the threshold an uncached line renders as before. -- [ ] `GET /api/state` carries `speech.gate` with `threshold`, `cpu` and +- [x] A cached line answers 200 whatever the CPU. +- [x] Below the threshold an uncached line renders as before. +- [x] `GET /api/state` carries `speech.gate` with `threshold`, `cpu` and `open`. -- [ ] `speech.threshold` accepts only 30 to 90 in steps of 10, default 50, +- [x] `speech.threshold` accepts only 30 to 90 in steps of 10, default 50, and changes only with a valid passkey signature. ### Sound -- [ ] Auto below the threshold: the page plays the spoken line; on a 503 it +- [x] Auto below the threshold: the page plays the spoken line; on a 503 it plays the burst once and does not retry that row. -- [ ] Quiet: the page sends no `/api/tts` request except for procedure-word +- [x] Quiet: the page sends no `/api/tts` request except for procedure-word lines, and every row plays the burst. -- [ ] Silence: no audio node starts except for a procedure-word line. -- [ ] The burst is WebAudio noise of 0.4 to 0.8 s at the tone's volume times +- [x] Silence: no audio node starts except for a procedure-word line. +- [x] The burst is WebAudio noise of 0.4 to 0.8 s at the tone's volume times the squelch, and it sends no network request. -- [ ] A MAYDAY, PAN-PAN or SÉCURITÉ row requests its procedure-word line +- [x] A MAYDAY, PAN-PAN or SÉCURITÉ row requests its procedure-word line first, in every mode; the second such line for the same voice is a cache hit with no Piper call. ### Panel -- [ ] The display line shows class, mode, CPU % and a TX/RX lamp that is lit +- [x] The display line shows class, mode, CPU % and a TX/RX lamp that is lit while sound plays. -- [ ] Auto, Quiet and Silence form one button group (`aria-pressed`); a press +- [x] Auto, Quiet and Silence form one button group (`aria-pressed`); a press changes the mode without a passkey and survives a reload. -- [ ] Volume per tone, squelch, the read-aloud filters for classes and +- [x] Volume per tone, squelch, the read-aloud filters for classes and agents, and the internet station sit in the panel; the settings dialog has no radio section. -- [ ] The threshold control asks for the passkey once per change and shows +- [x] The threshold control asks for the passkey once per change and shows the server's value. -- [ ] At 1920, 1366 and 390 px the panel has no horizontal scroll, in the +- [x] At 1920, 1366 and 390 px the panel has no horizontal scroll, in the light and the dark palette. ### Log -- [ ] Tabs Sent, Received and All show their counts; Received holds exactly +- [x] Tabs Sent, Received and All show their counts; Received holds exactly the rows addressed to the operator or to all. -- [ ] The chips class, sender, recipient and agent and the text search +- [x] The chips class, sender, recipient and agent and the text search combine with AND; one control clears them all. -- [ ] Rows run newest first with time, direction, class badge, +- [x] Rows run newest first with time, direction, class badge, sender → recipient and text; a click opens the full message. -- [ ] Tab, chips and search survive a reload in the same browser. +- [x] Tab, chips and search survive a reload in the same browser. ### Tests diff --git a/orchestrator/client/signing.py b/orchestrator/client/signing.py index 8b2eb82..9cf698d 100644 --- a/orchestrator/client/signing.py +++ b/orchestrator/client/signing.py @@ -158,6 +158,10 @@ def stream_item(origins: list[str], urls: list[str]) -> dict: return {"kind": "streams", "origins": origins, "urls": urls} +def threshold_item(threshold: int) -> dict: + return {"kind": "speech-threshold", "threshold": threshold} + + def task_item(task: str, argv: list[str]) -> dict: return {"kind": "task", "id": task, "argv": argv} diff --git a/orchestrator/kvb_orchestrator/engines.py b/orchestrator/kvb_orchestrator/engines.py index 283c260..52abc14 100644 --- a/orchestrator/kvb_orchestrator/engines.py +++ b/orchestrator/kvb_orchestrator/engines.py @@ -8,13 +8,14 @@ import subprocess import threading import time import wave +from collections.abc import Callable from dataclasses import dataclass from pathlib import Path from .council import HUMAN from .errors import EngineError, ToolError, Unavailable from .fields import TEXT_LIMIT, bounded, decode, expect -from .phrasing import SPEECH_LIMIT, SPOKEN_LINE, phrase +from .phrasing import PROCEDURE_LINES, SPEECH_LIMIT, SPOKEN_LINE, phrase from .records import digest, read_json from .skill_scripts import SAFE_PATH @@ -22,6 +23,7 @@ MODEL_NAME = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,127}") SPEAKER_LIMIT = 10000 TTS_TIMEOUT = 120 TTS_KEEP = 200 +WORD_CACHE = "word-" STT_RATE = 16000 STT_SECONDS = 120 STT_LIMIT = 44 + STT_RATE * 2 * STT_SECONDS @@ -141,6 +143,7 @@ class Speech: raise ToolError(f"the operator voice {engines.operator_voice!r} is not installed in {engines.voices}") self.operator = engines.operator_voice if self.voices else "" self.pool = [voice for voice in self.voices if voice != self.operator] + self.handed = {self.operator} if self.operator else set() self.speaking = threading.Lock() self.hearing = threading.Lock() self.metering = threading.Lock() @@ -174,7 +177,15 @@ class Speech: return gap or ("" if os.path.isfile(model) else f"the whisper.cpp model {model} is not a file") def voice_of(self, key: str) -> str: - return voice_of(key, self.pool, self.operator) + """Args: + key: the speaker's session id or directory key. + + Returns: + The speaker's voice, which /api/tts accepts from then on. + """ + voice = voice_of(key, self.pool, self.operator) + self.handed.add(voice) + return voice def describe(self) -> dict: operator = self.voice_of(HUMAN) @@ -185,7 +196,15 @@ class Speech: "limit": SPEECH_LIMIT, "rate": STT_RATE, "seconds": STT_SECONDS, } - def synthesize(self, body: bytes) -> bytes: + def synthesize(self, body: bytes, admit: Callable[[str], None]) -> bytes: + """Args: + body: {"text", "voice"} as JSON. + admit: called with a line before it renders and not for a cached + one; it raises to hold the render back. + + Returns: + The WAV of the line, from the cache when rendered before. + """ gap = self.output_gap() if gap: raise Unavailable(f"speech output is off: {gap}") @@ -194,13 +213,16 @@ class Speech: voice = payload["voice"] if not isinstance(voice, str) or voice not in self.voices: raise ToolError("voice must be one of the installed voices") + if voice not in self.handed: + raise ToolError("voice must be one the hub hands out to a speaker") line = " ".join(bounded(payload, "text", SPEECH_LIMIT).split()) if not SPOKEN_LINE.fullmatch(line): raise ToolError(f"text must be 1 to {SPEECH_LIMIT} printable characters") key = digest(f"{voice}\n{line}") - target = self.cache / f"{key}.wav" + target = self.cache / f"{WORD_CACHE if line in PROCEDURE_LINES else ''}{key}.wav" with self.speaking: if not target.is_file(): + admit(line) self.render(voice, line, target) self.trim() os.utime(target) @@ -235,7 +257,8 @@ class Speech: partial.unlink(missing_ok=True) def trim(self) -> None: - kept = sorted(self.cache.glob("*.wav"), key=lambda path: path.stat().st_mtime, reverse=True) + spoken = (path for path in self.cache.glob("*.wav") if not path.name.startswith(WORD_CACHE)) + kept = sorted(spoken, key=lambda path: path.stat().st_mtime, reverse=True) for path in kept[TTS_KEEP:]: path.unlink(missing_ok=True) diff --git a/orchestrator/kvb_orchestrator/errors.py b/orchestrator/kvb_orchestrator/errors.py index e1a8cac..c1a8587 100644 --- a/orchestrator/kvb_orchestrator/errors.py +++ b/orchestrator/kvb_orchestrator/errors.py @@ -25,5 +25,14 @@ class Unavailable(Exception): pass +class Held(Exception): + def __init__(self, reason: str) -> None: + """Args: + reason: why the hub holds the request back, sent as {"reason"}. + """ + super().__init__(reason) + self.reason = reason + + class EngineError(Exception): pass diff --git a/orchestrator/kvb_orchestrator/exchange.py b/orchestrator/kvb_orchestrator/exchange.py index c680407..3523781 100644 --- a/orchestrator/kvb_orchestrator/exchange.py +++ b/orchestrator/kvb_orchestrator/exchange.py @@ -8,7 +8,7 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import parse_qs, urlsplit from . import NAME -from .errors import Conflict, EngineError, Missing, ToolError, Unavailable +from .errors import Conflict, EngineError, Held, Missing, ToolError, Unavailable from .hub import Hub GUARD = "X-Kvb-Orchestrator" @@ -68,6 +68,8 @@ class Exchange(BaseHTTPRequestHandler): self.send_json(HTTPStatus.CONFLICT, error.payload) except Unavailable as error: self.send_text(HTTPStatus.SERVICE_UNAVAILABLE, str(error)) + except Held as error: + self.send_json(HTTPStatus.SERVICE_UNAVAILABLE, {"reason": error.reason}) except EngineError as error: self.send_text(HTTPStatus.BAD_GATEWAY, str(error)) except OSError as error: diff --git a/orchestrator/kvb_orchestrator/handler.py b/orchestrator/kvb_orchestrator/handler.py index 4f96e0a..4cfdc6b 100644 --- a/orchestrator/kvb_orchestrator/handler.py +++ b/orchestrator/kvb_orchestrator/handler.py @@ -69,7 +69,8 @@ ACTION_POSTS = ("/api/rules", "/api/nl", SETTINGS_ROUTE, TTS_ROUTE, STT_ROUTE, S class Handler(Exchange): def speak(self, body: bytes) -> None: headers = {"Content-Security-Policy": PLAIN_CSP} - self.attempt(lambda: self.server.hub.speech.synthesize(body), lambda wav: self.send(HTTPStatus.OK, WAV_TYPE, wav, headers)) + hub = self.server.hub + self.attempt(lambda: hub.speech.synthesize(body, hub.speech_gate.admit), lambda wav: self.send(HTTPStatus.OK, WAV_TYPE, wav, headers)) def hear(self, body: bytes) -> None: self.answer(lambda: self.server.hub.speech.transcribe(body), HTTPStatus.OK) diff --git a/orchestrator/kvb_orchestrator/hub.py b/orchestrator/kvb_orchestrator/hub.py index cfb1126..0fe6cb5 100644 --- a/orchestrator/kvb_orchestrator/hub.py +++ b/orchestrator/kvb_orchestrator/hub.py @@ -1,6 +1,7 @@ from __future__ import annotations import threading +import time from dataclasses import dataclass from pathlib import Path @@ -29,12 +30,14 @@ from .offices import Offices from .presence import SNAPSHOT from .redirects import Redirects from .requesting import Requesting +from .resources import RESOURCES, SAMPLES from .reviewing import Reviewing from .rules import Rules from .settings import Settings from .signed import signing from .skill_scripts import Runner from .sources import Sources +from .speechgate import CpuGate from .tasking import Tasking from .timeline import Timeline from .transcripts import Transcripts @@ -95,6 +98,7 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque settings: Settings redirects: Redirects speech: Speech + speech_gate: CpuGate approvers: Path vendor: Path icons: Markup @@ -117,7 +121,9 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta reviews = Reviews(state) speech = Speech(engines, state / "tts") messages = Ledger(state / "messages.json") + settings = Settings(state / "settings.json") return Hub( + speech_gate=CpuGate(samples=places.kept / RESOURCES / SAMPLES, threshold=lambda: settings.get()["speech"]["threshold"], clock=time.time), vendor=places.vendor, icons=sheet(places.vendor, templates), spool=Spool(places.cleanup).ready(), @@ -133,7 +139,7 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta log=Log(state / "log.jsonl"), batches=Pending(), names=Names(state / "names.json"), - settings=Settings(state / "settings.json"), + settings=settings, redirects=redirects, watch=Watch(), tree=Tree(file=places.tree_file, heartbeat=state / TREE_HEARTBEAT, lock=threading.Lock(), cached=None, stamp=None), diff --git a/orchestrator/kvb_orchestrator/phrasing.py b/orchestrator/kvb_orchestrator/phrasing.py index 039d594..4e78678 100644 --- a/orchestrator/kvb_orchestrator/phrasing.py +++ b/orchestrator/kvb_orchestrator/phrasing.py @@ -10,6 +10,7 @@ SPOKEN_TEXT = 500 SPOKEN_NAME = 60 SPOKEN_GROUP = 120 ALARM_WORDS = {"pan-pan": "PAN-PAN", "security": "say-cure-ee-tay"} +PROCEDURE = {"mayday": "MAYDAY", **ALARM_WORDS} ADDRESSED = ("post", "call", "receipt") CALLS = re.compile(r"^(?:(?:MAYDAY|PAN[ -]?PAN|SECURIT[EÉY])\b[\s:,.;!-]*)+", re.IGNORECASE) HUB_MARK = re.compile(r"\[kvb-orchestrator msg [0-9a-f]{16}\]") @@ -122,12 +123,57 @@ def spoken_text(cls: str, text: str) -> str: body = HUB_MARK.sub("", text) if body != text: body = HUB_FILE.sub("", body) - if cls in ("mayday", *ALARM_WORDS): + if cls in PROCEDURE: body = CALLS.sub("", body.strip()) body = clip(body, SPOKEN_TEXT) return "" if body == "-" else body +def procedure(cls: str) -> str: + """Args: + cls: the radio class of the row. + + Returns: + The procedure word of a MAYDAY, PAN-PAN or SÉCURITÉ row spoken three + times, the same line for every row of the class; "" for any other + class. + """ + word = PROCEDURE.get(cls) + return f"{word} {word} {word}" if word else "" + + +PROCEDURE_LINES = frozenset(procedure(cls) for cls in PROCEDURE) + + +def phrase_parts(cls: str, sender: str, called: list[str], text: str) -> tuple[str, str]: + """Args: + cls: the radio class of the row, one of council.CLASSES or receipt. + sender: the sender's name valid at the row's time. + called: the names of the called stations valid at the row's time. + text: the radio text. + + Returns: + (word, rest): the procedure word line of the row, "" for a routine + class, and the rest of the row in maritime radio phraseology (SMCP + style). + """ + station = clip(sender, SPOKEN_NAME) or "unknown station" + if cls == "mayday": + head, end = f"this is {station} {station} {station}", "over" + elif cls in ALARM_WORDS: + head, end = f"all stations, this is {station}", "over" + elif cls in ADDRESSED and called: + group = clip(" and ".join(clip(name, SPOKEN_NAME) for name in called), SPOKEN_GROUP) + head, end = f"{group}, {group}, this is {station}", "over" + else: + head, end = f"all stations, this is {station}", "out" + return procedure(cls), joined((head, spoken_text(cls, text), end)) + + +def joined(parts: tuple[str, ...]) -> str: + return ", ".join(part for part in parts if part) + + def phrase(cls: str, sender: str, called: list[str], text: str) -> str: """Args: cls: the radio class of the row, one of council.CLASSES or receipt. @@ -136,18 +182,7 @@ def phrase(cls: str, sender: str, called: list[str], text: str) -> str: text: the radio text. Returns: - The row in maritime radio phraseology (SMCP style), one line of at - most SPEECH_LIMIT characters. + The row in maritime radio phraseology (SMCP style), its procedure + word line first, one line of at most SPEECH_LIMIT characters. """ - station = clip(sender, SPOKEN_NAME) or "unknown station" - if cls == "mayday": - head, end = f"MAYDAY MAYDAY MAYDAY, this is {station} {station} {station}", "over" - elif cls in ALARM_WORDS: - word = ALARM_WORDS[cls] - head, end = f"{word} {word} {word}, all stations, this is {station}", "over" - elif cls in ADDRESSED and called: - group = clip(" and ".join(clip(name, SPOKEN_NAME) for name in called), SPOKEN_GROUP) - head, end = f"{group}, {group}, this is {station}", "over" - else: - head, end = f"all stations, this is {station}", "out" - return ", ".join(part for part in (head, spoken_text(cls, text), end) if part) + return joined(phrase_parts(cls, sender, called, text)) diff --git a/orchestrator/kvb_orchestrator/settings.py b/orchestrator/kvb_orchestrator/settings.py index 9e06ab4..87d4da7 100644 --- a/orchestrator/kvb_orchestrator/settings.py +++ b/orchestrator/kvb_orchestrator/settings.py @@ -4,11 +4,9 @@ import copy import json import re import threading -from collections.abc import Callable from pathlib import Path from urllib.parse import urlsplit -from .council import CLASSES from .errors import ToolError from .fields import FIELDS, NAME_LIMIT, expect from .records import read_json, write_atomic @@ -17,7 +15,9 @@ THEMES = ("sky", "light", "dark", "custom") ESTIMATIONS = ("none", "points") TRENDS = ("off", "regression", "velocity") TREND_DAYS = (1, 365) -TONES = ("mayday", "review", "direct", "radio") +THRESHOLDS = tuple(range(30, 91, 10)) +RADIO_MOVED = ("sounds", "squelch") +SPEECH_MOVED = ("on", "classes", "muted") COLOR = re.compile(r"#[0-9a-f]{6}") CLOCK = re.compile(r"(?:[01][0-9]|2[0-3]):[0-5][0-9]") MIDNIGHT = "24:00" @@ -33,16 +33,9 @@ SETTINGS = { "theme": "sky", "colors": {"top": "#0b1020", "bottom": "#2b3a67", "accent": "#8fadff"}, "music": {"on": False, "volume": 0.4}, - "sounds": { - "mayday": {"on": True, "volume": 0.9}, - "review": {"on": True, "volume": 0.6}, - "direct": {"on": True, "volume": 0.5}, - "radio": {"on": False, "volume": 0.25}, - }, - "squelch": 1.0, "notify": True, "streams": [], - "speech": {"on": False, "classes": ["mayday", "pan-pan", "security"], "muted": []}, + "speech": {"threshold": 50}, "space": { "graph": {"axis": "none", "yaw": 30.0, "pitch": 20.0, "zoom": 1.0, "pan_x": 0.0, "pan_y": 0.0}, "sky": {"azimuth": 180.0, "altitude": 0.0, "fov": 180.0}, @@ -56,8 +49,6 @@ SPACE_LIMITS = { "graph": {"yaw": (-180.0, 180.0), "pitch": (-90.0, 90.0), "zoom": (0.25, 4.0), "pan_x": (-2000.0, 2000.0), "pan_y": (-2000.0, 2000.0)}, "sky": {"azimuth": (0.0, 360.0), "altitude": (-30.0, 85.0), "fov": (20.0, 240.0)}, } -MUTED_LIMIT = 200 -LEGACY_CLASSES = {"direct": ("post", "call")} def section(value: object, name: str, keys: tuple[str, ...]) -> dict: @@ -152,27 +143,12 @@ def stream_entry(raw: object, index: int) -> dict: } -def tone_setting(raw: object, kind_name: str) -> dict: - entry = section(raw, f"sounds.{kind_name}", ("on", "volume")) - return {"on": flag(entry["on"], f"sounds.{kind_name}.on"), "volume": number_in(entry["volume"], f"sounds.{kind_name}.volume", 0.0, 1.0)} - - -def distinct_texts(value: object, valid: Callable[[str], bool], limit: int) -> bool: - if not isinstance(value, list) or len(value) > limit or not all(isinstance(item, str) and valid(item) for item in value): - return False - return len(set(value)) == len(value) - - def speech_setting(raw: object) -> dict: - entry = section(raw, "speech", ("on", "classes", "muted")) - classes, muted = entry["classes"], entry["muted"] - if not distinct_texts(classes, lambda name: name in CLASSES, len(CLASSES)): - raise ToolError(f"speech.classes must list distinct classes of {', '.join(CLASSES)}") - if "mayday" not in classes: - raise ToolError("speech.classes must include mayday: a MAYDAY is never quieter than anything else") - if not distinct_texts(muted, FIELDS["agent"][0].fullmatch, MUTED_LIMIT): - raise ToolError(f"speech.muted must list at most {MUTED_LIMIT} distinct agent keys, each one line of 1 to 200 characters") - return {"on": flag(entry["on"], "speech.on"), "classes": [name for name in CLASSES if name in classes], "muted": sorted(muted)} + entry = section(raw, "speech", tuple(SETTINGS["speech"])) + threshold = entry["threshold"] + if isinstance(threshold, bool) or not isinstance(threshold, int) or threshold not in THRESHOLDS: + raise ToolError(f"speech.threshold must be one of {', '.join(map(str, THRESHOLDS))} percent") + return {"threshold": threshold} def space_setting(raw: object) -> dict: @@ -200,7 +176,6 @@ def valid_settings(raw: object) -> dict: data = section(raw, "settings", tuple(SETTINGS)) colors = section(data["colors"], "colors", tuple(SETTINGS["colors"])) music = section(data["music"], "music", ("on", "volume")) - sounds = section(data["sounds"], "sounds", TONES) if not isinstance(data["theme"], str) or data["theme"] not in THEMES: raise ToolError(f"theme must be one of {', '.join(THEMES)}") if not isinstance(data["streams"], list) or len(data["streams"]) > STREAMS_LIMIT: @@ -213,8 +188,6 @@ def valid_settings(raw: object) -> dict: "theme": data["theme"], "colors": {key: color(colors[key], f"colors.{key}") for key in SETTINGS["colors"]}, "music": {"on": flag(music["on"], "music.on"), "volume": number_in(music["volume"], "music.volume", 0.0, 1.0)}, - "sounds": {kind_name: tone_setting(sounds[kind_name], kind_name) for kind_name in TONES}, - "squelch": number_in(data["squelch"], "squelch", 0.0, 1.0), "notify": flag(data["notify"], "notify"), "streams": [stream_entry(entry, index) for index, entry in enumerate(data["streams"])], "speech": speech_setting(data["speech"]), @@ -225,23 +198,32 @@ def valid_settings(raw: object) -> dict: } -def speech_renamed(stored: object) -> object: - speech = stored.get("speech") if isinstance(stored, dict) else None - classes = speech.get("classes") if isinstance(speech, dict) else None - if not isinstance(classes, list) or not any(name in LEGACY_CLASSES for name in classes): +def radio_moved(stored: object) -> object: + """Returns: + The stored settings without the tones, the squelch and the read-aloud + filters, which each browser keeps for itself since requirement 004; + a speech section left empty is dropped so the default threshold + fills it. + """ + if not isinstance(stored, dict): return stored - renamed = [new for name in classes for new in LEGACY_CLASSES.get(name, (name,))] - return {**stored, "speech": {**speech, "classes": list(dict.fromkeys(renamed))}} + kept = {key: value for key, value in stored.items() if key not in RADIO_MOVED} + speech = stored.get("speech") + if not isinstance(speech, dict): + return kept + rest = {key: value for key, value in speech.items() if key not in SPEECH_MOVED} + others = {key: value for key, value in kept.items() if key != "speech"} + return {**others, "speech": rest} if rest else others def migrated(stored: object) -> object: """Returns: - The stored settings in the current shape: legacy speech classes - renamed, and a stream saved before redirect resolution with via []: + The stored settings in the current shape: the radio set moved to the + browser, and a stream saved before redirect resolution with via []: it may have been saved without the passkey, so the hub never dials it unsigned; re-adding it with the passkey resolves its redirects. """ - stored = speech_renamed(stored) + stored = radio_moved(stored) streams = stored.get("streams") if isinstance(stored, dict) else None if not isinstance(streams, list): return stored diff --git a/orchestrator/kvb_orchestrator/speechgate.py b/orchestrator/kvb_orchestrator/speechgate.py new file mode 100644 index 0000000..552ab55 --- /dev/null +++ b/orchestrator/kvb_orchestrator/speechgate.py @@ -0,0 +1,92 @@ +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") diff --git a/orchestrator/kvb_orchestrator/view.py b/orchestrator/kvb_orchestrator/view.py index 65886ce..ec9e6f3 100644 --- a/orchestrator/kvb_orchestrator/view.py +++ b/orchestrator/kvb_orchestrator/view.py @@ -22,7 +22,7 @@ from .graph import graph from .jobs import JOB_OPEN from .messaging import message_row from .orbits import TRACK_STEPS, track -from .phrasing import phrase +from .phrasing import joined, phrase_parts from .presence import presence, read_snapshot from .records import epoch, iso_epoch, read_jsonl from .register import ( @@ -94,7 +94,8 @@ class View: source = directory.key(row["from"], row["ts"]) targets = [directory.key(name, row["ts"]) for name in row["to"]] called = [directory.at(key, row["ts"]) or name for key, name in zip(targets, row["to"], strict=True)] - spoken = {"speech": phrase(row["class"], directory.at(source, row["ts"]) or row["from"], called, row["text"]), "voice": self.speech.voice_of(source)} + word, rest = phrase_parts(row["class"], directory.at(source, row["ts"]) or row["from"], called, row["text"]) + spoken = {"speech": joined((word, rest)), "word": word, "rest": rest, "voice": self.speech.voice_of(source)} radio.append({**row, "source": source, "targets": targets, "now": directory.now(source), **spoken}) grant_rows = [{**grant, "target": directory.key(grant["to"], grant["ts"])} for grant in grants(history, (begin, end), claims, tasks)] self.watch.see(tasks, now) @@ -112,7 +113,7 @@ class View: return { "settings": settings, "media": media_origins(settings), - "speech": self.speech.describe(), + "speech": {**self.speech.describe(), "gate": self.speech_gate.state()}, "people": people, "inbox": inbox, "badges": badges(inbox, claims, self.jobs.listed(), now), @@ -148,19 +149,23 @@ class View: "graph": drawn, } - def stream_change(self, settings: dict, saved: dict) -> dict | None: + def signed_changes(self, settings: dict, saved: dict) -> list[dict]: """Args: settings: valid settings the page wants to save. saved: the settings saved now. Returns: - The item the operator's passkey signs when the settings name a - stream url the saved ones do not, else None: the station origins - and those urls. The hub dials no url before it is signed, and - resolves its redirect targets only after the signature. + The items the operator's passkey signs for this save: the station + origins and the new urls when the settings name a stream url the + saved ones do not, and the speech threshold when it differs from + the saved one, since the hub's speech gate enforces it. The hub + dials no url before it is signed, and resolves its redirect + targets only after the signature. """ urls = sorted({stream["url"] for stream in settings["streams"]} - {stream["url"] for stream in saved["streams"]}) - return signing.stream_item(station_origins(settings), urls) if urls else None + threshold = settings["speech"]["threshold"] + streams = [signing.stream_item(station_origins(settings), urls)] if urls else [] + return streams + ([signing.threshold_item(threshold)] if threshold != saved["speech"]["threshold"] else []) def resolved(self, settings: dict, saved: dict) -> tuple[dict, list[str]]: """Args: @@ -188,8 +193,7 @@ class View: return {**settings, "streams": streams}, problems def sign_settings(self, body: bytes) -> dict: - change = self.stream_change(valid_settings(decode(body)), self.settings.get()) - return self.challenge_for([] if change is None else [change]) + return self.challenge_for(self.signed_changes(valid_settings(decode(body)), self.settings.get())) def save_settings(self, body: bytes) -> dict: """Returns: @@ -201,8 +205,7 @@ class View: proof = payload.pop("proof", None) settings = valid_settings(payload) saved = self.settings.get() - change = self.stream_change(settings, saved) - if change is not None: + for change in self.signed_changes(settings, saved): self.operator_proven(proof, change) resolved, problems = self.resolved(settings, saved) return {"settings": self.settings.put(resolved, saved), "problems": problems} diff --git a/orchestrator/templates/index.html.j2 b/orchestrator/templates/index.html.j2 index 80b799d..a57d4b1 100644 --- a/orchestrator/templates/index.html.j2 +++ b/orchestrator/templates/index.html.j2 @@ -461,6 +461,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi