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>
This commit is contained in:
1 parent
69693ac2bc
commit
acf39255fc
37 files changed
+2480
-356
No files matched your search
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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))
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
@@ -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}
|
||||
|
||||
Reference in new issue
Block a user