feat(orchestrator): Gibberlink radio mode with ggwave and a fallback selector (018)

A new radio mode sends each spoken row as ggwave frames of at most 140 bytes: frame 1 carries the full session id, later frames its 8-character prefix, each with n/m. engines.sound_of() gives every speaker one of the four audible ggwave protocols that decode every frame length, plus its own stereo position, by a stable hash. The vendored ggwave 0.4.0 (MIT, LICENSE kept, sha256 pinned in test_hub_artifacts) runs only in the /gibberlink encoder page, sandboxed into an opaque origin with eval and WebAssembly but no connection; the hub page frames it (frame-src 'self') and review pages refuse framing (frame-ancestors 'none'). The radio gear gains a fallback selector (Gibberlink, Morse, Hiss) for rows that speech cannot play.

Accepted MEDUSA false positives (security.py accept after kvb-think-criticize, 2026-11-17): engines.py FTSCAN-231 INF-SCAN-2638 INF-SCAN-2057 STG009 B603; handler.py FTSC-SCAN-089 B105 EXA002 EXA006; view.py WEB-FLASK-004 FINE-043 LO002.

🎯 Fix: 91 %
💥 Break: 4 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 87
🚦 Gate: ✅ 90/5, tokens and velocity <= 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-17 19:05:40 +00:00
1 parent af3555e88b
commit 621dc7536f
23 files changed
+1021 -34

No files matched your search

+2
View File
@@ -142,6 +142,8 @@ FACE = """(async () => {@HELPERS@@RADIO@
gear.click();
await sleep(200);
const groups =Object.fromEntries(all("#radio-gear .chips", spot).map((group) => [group.getAttribute("aria-label"), all(".chip", group).map((chipNode) => [text(chipNode), chipNode.getAttribute("aria-pressed"), chipNode.disabled])]));
all('#radio-gear .chips[aria-label="Fallback"] .chip', spot).find((chipNode) => text(chipNode) === "Hiss").click();
await sleep(150);
const stationsHead = text(spot.querySelector(".radio-station .group-title"));
const ptt = spot.querySelector('form.composer button[type="submit"]');
const pageFits = fits();
+239
View File
@@ -0,0 +1,239 @@
from __future__ import annotations
import json
import os
import shutil
import subprocess
import sys
import tempfile
import time
import unittest
from pathlib import Path
from unittest import mock
HERE = Path(__file__).resolve().parent
sys.path[:0] = [str(HERE.parent), str(HERE)]
import hub_e2e_kit as base
from hub_case import HubCase
from hub_support import HUB, iso
from kvb_orchestrator import engines
from radio_set_kit import CUES, OUT, SPY, WORD, probe
GGWAVE = HUB / "vendor" / "ggwave" / "ggwave.js"
SPOKEN = "Disk at ninety percent"
GIBBER_SPY = """() => {
(@SPY@)();
const captured = window.e2eGibber = [];
const pans = window.e2ePans = [];
const encoded = (part) => {
const raw = new Uint8Array(part.buffer, part.byteOffset, part.byteLength);
let binary = "";
for (let at = 0; at < raw.length; at += 0x8000) {
binary += String.fromCharCode(...raw.subarray(at, at + 0x8000));
}
return btoa(binary);
};
window.addEventListener("message", (event) => {
if (event.data && Array.isArray(event.data.samples)) {
captured.push({rate: event.data.rate, frames: event.data.samples.map(encoded)});
}
});
const panner = AudioContext.prototype.createStereoPanner;
AudioContext.prototype.createStereoPanner = function panned() {
const node = panner.call(this);
pans.push(node);
return node;
};
}""".replace("@SPY@", SPY)
GIBBER_KIT = """
const gear = () => all(".spot-head button", spot).find((button) => name(button) === "Read aloud");
const fallbacks = () => all('#radio-gear .chips[aria-label="Fallback"] .chip', spot);
const kept = () => JSON.parse(localStorage.getItem("kvb-orchestrator.radio") || "{}").fallback || null;
const opened = async (open) => {
if ((gear().getAttribute("aria-expanded") === "true") !== open) {
gear().click();
await sleep(200);
}
};
const sent = async (count) => {
await cue("alarm");
refreshNow();
return Boolean(await until(() => window.e2eGibber.length > count && lamp().dataset.lit === "true", 40000));
};
const stopped = async () => {
const stop = all("button", spot.querySelector(".radio-caption")).find((button) => name(button) === "Stop speaking");
if (stop) {
stop.click();
}
return Boolean(await until(() => lamp().dataset.lit === "false", 3000));
};
"""
GIBBER = """(async () => {@HELPERS@@RADIO@@KIT@
press("gibberlink");
await sleep(200);
const button = spot.querySelector('.radio-modes button[data-mode="gibberlink"]');
const mode = [pressed(), field("mode"), button.title, Boolean(button.querySelector("svg.bi"))];
await opened(true);
const before = fallbacks().map((chip) => [text(chip), chip.getAttribute("aria-pressed")]);
fallbacks().find((chip) => text(chip) === "Morse").click();
await sleep(150);
const morse = [fallbacks().map((chip) => chip.getAttribute("aria-pressed")), kept()];
fallbacks().find((chip) => text(chip) === "Gibberlink").click();
await sleep(150);
const back = [fallbacks().map((chip) => chip.getAttribute("aria-pressed")), kept()];
const label = text(spot.querySelector("#radio-gear .radio-fallback .chips-label"));
const gearFits = fits();
await opened(false);
const at = {captured: window.e2eGibber.length, pans: window.e2ePans.length};
const playing = await sent(at.captured);
await sleep(300);
return {
mode, before, morse, back, label, gearFits, playing, captured: window.e2eGibber.slice(at.captured), pans: window.e2ePans.slice(at.pans).map((node) => node.pan.value),
lamp: [text(lamp()), lamp().dataset.lit], caption: text(spot.querySelector(".radio-caption strong")), fits: fits(), frames: document.querySelectorAll("iframe.gibberlink-encoder").length,
};
})()"""
STOP = """(async () => {@HELPERS@@RADIO@@KIT@
const lit = lamp().dataset.lit;
return {lit, quiet: await stopped(), hidden: spot.querySelector(".radio-caption").hidden};
})()"""
QUIET = """(async () => {@HELPERS@@RADIO@@KIT@
press("quiet");
await sleep(200);
const at = {captured: window.e2eGibber.length, tts: audio().tts.length};
const played = await sent(at.captured);
const quiet = await stopped();
const tts = audio().tts.slice(at.tts).map((entry) => entry.body.text);
await opened(true);
return {played, quiet, tts, captured: window.e2eGibber.slice(at.captured), fits: fits(), violations: window.e2eViolations};
})()"""
DECODE = """
const fs = require("fs");
const factory = require(process.argv[1]);
const captures = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
factory({print: () => undefined, printErr: () => undefined}).then((ggwave) => {
console.log(JSON.stringify(captures.map(({rate, frames}) => {
const parameters = ggwave.getDefaultParameters();
parameters.sampleRateInp = rate;
const listener = ggwave.init(parameters);
const heard = [];
const gap = new Float32Array(Math.round(rate * 0.3));
for (const frame of frames) {
const raw = Buffer.from(frame, "base64");
const parts = [new Float32Array(raw.buffer.slice(raw.byteOffset, raw.byteOffset + raw.length)), gap];
for (const samples of parts) {
for (let at = 0; at < samples.length; at += 1024) {
const got = ggwave.decode(listener, new Int8Array(samples.slice(at, at + 1024).buffer));
if (got && got.length) {
heard.push(Buffer.from(got.buffer, got.byteOffset, got.length).toString("utf8"));
}
}
}
}
ggwave.free(listener);
return heard;
})));
});
"""
def kit(template: str) -> str:
return probe(template).replace("@KIT@", GIBBER_KIT)
@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium")
class TestGibberlink(HubCase):
drive = base.OrchestratorE2ECase.drive
beside = base.OrchestratorE2ECase.beside
def setUp(self):
super().setUp()
OUT.mkdir(parents=True, exist_ok=True)
now = time.time()
self.claim("s1", "Skills session")
self.write_council({"work/s2/claim.json": {"office": "work/s2", "session": "s2", "runtime": "claude", "name": "Stage", "since": iso(now - 7200), "beat": now - 60, "work": "x"}, "news.jsonl": ""})
hub = self.httpd.hub
hub.settings.put({**hub.settings.get(), "theme": "light"}, hub.settings.get())
self.host_cpu(10.0, 0)
self.beside(lambda: self.host_cpu(10.0, 0))
self.sent = 0
heard = hub.voice_notes.get
def get(note_id: str) -> dict:
if note_id != CUES["alarm"]:
return heard(note_id)
self.sent += 1
row = {"ts": time.time(), "from": "Stage", "to": [], "text": f"{SPOKEN} {self.sent}", "class": "security", "v": 2}
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
handle.write(self.jsonl([row]))
return {"id": note_id, "status": "done", "text": "", "error": "", "seconds": None, "created_ts": None}
patch = mock.patch.object(hub.voice_notes, "get", get)
patch.start()
self.addCleanup(patch.stop)
self.base = f"http://localhost:{self.port}/"
def steps(self, browser: str) -> list[dict]:
steps = []
for width, height in base.WIDTHS:
shot = str(OUT / f"{browser}-{width}-radio-gibberlink")
steps += [
{"width": width, "height": height, "url": f"{self.base}?{width}-gibberlink#stream", "wait": 1200, "probe": kit(GIBBER), "shot": f"{shot}-playing.png", "gesture": True},
{"width": width, "height": height, "url": None, "wait": 0, "probe": kit(STOP), "shot": None, "gesture": True},
{"width": width, "height": height, "url": None, "wait": 0, "probe": kit(QUIET), "shot": f"{shot}-fallback.png", "gesture": True},
]
return steps
def decoded(self, captures: list[dict]) -> list[list[str]]:
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as handle:
json.dump(captures, handle)
self.addCleanup(os.unlink, handle.name)
done = subprocess.run(["node", "-e", DECODE, str(GGWAVE), handle.name], capture_output=True, text=True, timeout=600, check=True)
return json.loads(done.stdout)
def check(self, data: dict, browser: str) -> None:
results = iter(data["results"])
sound = engines.sound_of("s2")
for index, (width, _) in enumerate(base.WIDTHS):
played, stop, quiet = next(results), next(results), next(results)
with self.subTest(browser=browser, width=width, part="mode and fallback"):
self.assertNotIn("error", played, played)
self.assertEqual(played["mode"][:2], [["gibberlink"], "Gibberlink"], played["mode"])
self.assertIn("MAYDAY", played["mode"][2])
self.assertTrue(played["mode"][3])
self.assertEqual(played["before"], [["Gibberlink", "true"], ["Morse", "false"], ["Hiss", "false"]])
self.assertEqual((played["morse"], played["back"], played["label"]), ([["false", "true", "false"], "morse"], [["true", "false", "false"], "gibberlink"], "Fallback"))
self.assertEqual((played["gearFits"], played["fits"]), ({"page": True, "panel": True}, {"page": True, "panel": True}))
with self.subTest(browser=browser, width=width, part="frames"):
self.assertTrue(played["playing"], {key: value for key, value in played.items() if key != "captured"})
self.assertEqual(([capture["rate"] for capture in played["captured"]], played["frames"]), ([48000], 1))
self.assertEqual(len(played["pans"]), 1)
self.assertAlmostEqual(played["pans"][0], sound["pan"], places=5)
self.assertEqual(played["lamp"], ["RX", "true"])
self.assertEqual(played["caption"], "Stage speaking")
self.assertEqual(self.decoded(played["captured"]), [[f"s2 1/1 {SPOKEN} {2 * index + 1}"]])
with self.subTest(browser=browser, width=width, part="stop"):
self.assertEqual(stop, {"lit": "true", "quiet": True, "hidden": True})
with self.subTest(browser=browser, width=width, part="fallback in quiet"):
self.assertNotIn("error", quiet, quiet)
self.assertEqual((quiet["played"], quiet["quiet"], quiet["tts"]), (True, True, [WORD]))
self.assertEqual(self.decoded(quiet["captured"]), [[f"s2 1/1 {SPOKEN} {2 * index + 2}"]])
self.assertEqual(quiet["fits"], {"page": True, "panel": True})
self.assertEqual([seen for seen in quiet["violations"] if not seen.endswith("/favicon.ico")], [], "the hub page itself sees no CSP violation")
def test_chromium_sends_every_row_as_decodable_ggwave_frames_from_the_sandboxed_encoder_at_three_widths(self):
data = self.drive(self.steps("chromium"), None, init=GIBBER_SPY)
(OUT / "chromium-radio-gibberlink.json").write_text(json.dumps(data), encoding="utf-8")
self.assertEqual(data["errors"], [])
self.check(data, "chromium")
@unittest.skipUnless(shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox")
def test_firefox_sends_every_row_as_decodable_ggwave_frames_from_the_sandboxed_encoder_at_three_widths(self):
data = self.drive(self.steps("firefox"), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox"), init=GIBBER_SPY)
(OUT / "firefox-radio-gibberlink.json").write_text(json.dumps(data), encoding="utf-8")
self.assertEqual([error for error in data["errors"] if "/favicon.ico" not in error], [])
self.check(data, "firefox")
if __name__ == "__main__":
unittest.main()
+3 -1
View File
@@ -136,7 +136,9 @@ class TestRadioSet(HubCase):
self.assertEqual(face["display"]["mode"], "Auto")
self.assertEqual((face["display"]["cpu"], face["display"]["lamp"]), ("CPU 10 %", ["RX", "false"]), face)
self.assertTrue(face["display"]["cls"], face)
self.assertEqual([mode_[:2] for mode_ in face["modes"]], [["Auto", "true"], ["Quiet", "false"], ["Silence", "false"], ["Morse", "false"]])
self.assertEqual([mode_[:2] for mode_ in face["modes"]], [["Auto", "true"], ["Quiet", "false"], ["Silence", "false"], ["Morse", "false"], ["Gibberlink", "false"]])
hiss = "false" if index == 0 else "true"
self.assertEqual(face["groups"]["Fallback"], [["Gibberlink", "true" if index == 0 else "false", False], ["Morse", "false", False], ["Hiss", hiss, False]])
self.assertTrue(all(mode_[2] and mode_[3] for mode_ in face["modes"]), face["modes"])
self.assertEqual([knob[0] for knob in face["knobs"]], ["MAYDAY / PAN-PAN", "Reviews, asks, decisions", "Post and calls to you", "All radio", "Squelch", "Threshold"])
shown = "50" if index == 0 or not signed else str(THRESHOLDS[base.WIDTHS[index - 1][0]])
+1
View File
@@ -19,6 +19,7 @@ VENDORED = {
"3d-force-graph/3d-force-graph.min.js": ("13088340aad408156064fd120c3bc3a47c93a3cc45b8f132d6284b20e2223be9", "Copyright (c) 2017 Vasco Asturiano"),
"three/three.module.js": ("9052042d676cb0fdc1ddfefe193053f34b7ac0513a616fdac4535d49987812ea", "Copyright © 2010-2026 three.js authors"),
"three/three.core.js": ("9edde002b066a9a05676a6127f67735b62baf399bdea529f2f7e31657da769e6", "Copyright © 2010-2026 three.js authors"),
"ggwave/ggwave.js": ("f3792b5c185345a35a935ca68a5064b97f979d13fbba0062ff16f6b3b31a6113", "Copyright (c) 2020 Georgi Gerganov"),
}
VERSIONS = {"3d-force-graph/3d-force-graph.min.js": "Version 1.80.1", "three/three.core.js": "REVISION = '186'"}
+3 -3
View File
@@ -144,7 +144,7 @@ async function failed(bubbles, item, cancel) {
};
vm.runInNewContext(`${code}\\nsay(item);`, {
Bubbles, speech, fetch, item, Date, settings: {bubbles}, SPEECH_QUEUE: 9, SPEECH_QUIET: 0, radioSet: {},
Voice: {enqueue: (pending, next) => [...pending, next], loudness: () => 0}, RadioSet: {burst: (gain) => ({gain})},
Voice: {enqueue: (pending, next) => [...pending, next], loudness: () => 0}, RadioSet: {KEYED: ["morse", "gibberlink"], FALLBACKS: ["gibberlink", "morse", "burst"], burst: (gain) => ({gain})},
toast: (title, lines) => shown.push(`${title}: ${lines.join(" ")}`),
context: () => ({}), drawBubbles: draw, drawSpeaker: draw, wordsRead: () => 0,
performance: {now: () => 1000}, window: {setTimeout: () => 7, clearTimeout: () => {}},
@@ -195,7 +195,7 @@ async function run(rule, items) {
};
const sandbox = {
Bubbles, speech, Date, settings: {bubbles: true}, radioSet: {}, SPEECH_QUEUE: 9, SPEECH_QUIET: 0, SOUND_SLACK: 2,
Voice: {enqueue: (pending, next) => [...pending, next], loudness: () => 1}, RadioSet: {burst: (gain) => ({gain, seconds: 0.6, frequency: 1800, q: 0.9})},
Voice: {enqueue: (pending, next) => [...pending, next], loudness: () => 1}, RadioSet: {KEYED: ["morse", "gibberlink"], FALLBACKS: ["gibberlink", "morse", "burst"], burst: (gain) => ({gain, seconds: 0.6, frequency: 1800, q: 0.9})},
toast: () => null, context: () => audio, drawBubbles: () => null, wordsRead: () => 0, saidOf: (item) => item.words || Bubbles.words(item.line), performance: {now: () => 1000},
duck: () => {
if (rule === "transmit" && !ducked) {
@@ -343,7 +343,7 @@ async function transmitted(steps, cut, row = ROW, broken = "") {
fetch: async (url, options) => (JSON.parse(options.body).text === "fails"
? {ok: false, status: 500, headers: {get: () => "text/plain"}, text: async () => "tts down"}
: {ok: true, status: 200, headers: {get: () => "audio/wav"}, arrayBuffer: async () => ({})}),
Voice: {enqueue: (pending, next) => [...pending, next], loudness: () => 1}, RadioSet: {burst: (gain) => ({gain, seconds: 0.6, frequency: 1800, q: 0.9})},
Voice: {enqueue: (pending, next) => [...pending, next], loudness: () => 1}, RadioSet: {KEYED: ["morse", "gibberlink"], FALLBACKS: ["gibberlink", "morse", "burst"], burst: (gain) => ({gain, seconds: 0.6, frequency: 1800, q: 0.9})},
};
vm.runInNewContext(`${code}
function drawSpeaker() {
+419
View File
@@ -0,0 +1,419 @@
from __future__ import annotations
import json
import re
import subprocess
import time
import unittest
from unittest import mock
from hub_case import HubCase
from hub_support import HUB
from kvb_orchestrator import council, engines, exchange, settings
from kvb_orchestrator.view import ENCODER_CSP
TEMPLATES = HUB / "templates"
RADIOSET = TEMPLATES / "radioset.js"
GIBBERLINK = TEMPLATES / "gibberlink.js"
MORSE = TEMPLATES / "morse.js"
VOICE = TEMPLATES / "voice.js"
PAGE_SPEECH = TEMPLATES / "page-speech.js"
PAGE_MORSE = TEMPLATES / "page-morse.js"
PAGE_GIBBERLINK = TEMPLATES / "page-gibberlink.js"
GGWAVE = HUB / "vendor" / "ggwave" / "ggwave.js"
KEY = "4f0c2a1e-1111-2222-3333-444455556666"
MARK = "[kvb-orchestrator msg 0123456789abcdef]"
WIDE = "ä€😀"
ALARMS = ("mayday", "pan-pan", "security")
SOUND = {"protocol": "AUDIBLE_FAST", "pan": -0.4}
FRAMES_PROBE = """
const R = require(process.argv[1]);
const input = JSON.parse(process.argv[2]);
console.log(JSON.stringify({
split: input.texts.map(([key, text]) => R.frames(key, text)),
plans: input.plans.map(([row, set, live]) => R.plan(row, set, live)),
kept: input.sets.map((set) => R.valid(set).fallback),
constants: {modes: R.MODES, fallbacks: R.FALLBACKS, frame: R.FRAME, fallback: R.DEFAULTS.fallback, key: R.KEY},
}));
"""
PLAYING_PROBE = """
const fs = require("fs");
const vm = require("vm");
const RadioSet = require(process.argv[1]);
const Morse = require(process.argv[2]);
const Voice = require(process.argv[3]);
const code = process.argv.slice(4, 7).map((path) => fs.readFileSync(path, "utf8")).join("\\n");
const stub = () => undefined;
const settle = async () => {
for (let round = 0; round < 60; round += 1) {
await new Promise((resolve) => setImmediate(resolve));
}
};
const row = (source, pan, cls, text) => ({class: cls, source, from: source, to: [], called: [], speaker: "E", pitch: 600, protocol: "AUDIBLE_FAST", pan, voice: "v", word: "", rest: "x", text, ts: 1000});
function radio(plays, change) {
const run = {buffers: [], gains: [], pans: [], oscillators: [], encoded: []};
class Source {
constructor() {
Object.assign(this, {type: "", frequency: {value: 0}, buffer: null, ended: null, started: null, stops: []});
}
connect(node) {
return node;
}
addEventListener(type, ended) {
this.ended = ended;
}
start(when) {
this.started = when === undefined ? "now" : when;
if (plays) {
setImmediate(() => this.ended && this.ended());
}
}
stop(when) {
this.stops.push(when === undefined ? "now" : when);
if (when === undefined) {
setImmediate(() => this.ended && this.ended());
}
}
}
const made = (list) => () => {
const node = new Source();
list.push(node);
return node;
};
const param = () => ({value: 0, setValueAtTime: stub, linearRampToValueAtTime: stub, exponentialRampToValueAtTime: stub});
const audio = {
currentTime: 0, sampleRate: 8000, destination: {}, createBufferSource: made(run.buffers), createOscillator: made(run.oscillators),
createGain: () => {
const gain = param();
run.gains.push(gain);
return {gain, connect: (node) => node};
},
createStereoPanner: () => {
const pan = param();
run.pans.push(pan);
return {pan, connect: (node) => node};
},
createBuffer: (channels, frames, rate) => ({length: frames, duration: frames / rate, getChannelData: () => new Float32Array(frames)}),
};
const sandbox = {
RadioSet, Morse, Voice, radioSet: RadioSet.valid({mode: "gibberlink", classes: RadioSet.CLASSES, ...change}),
state: {now: 1000, messages: [], speech: {tts: false, gate: {open: false}}},
speech: {pending: [], busy: false, warned: 0, current: null, source: null, loading: null, timer: 0, item: null},
settings: {bubbles: false, music: {on: false, volume: 0.4}, morse: {wpm: 20}}, SPEECH_QUEUE: 20, SPEECH_QUIET: 60000, SOUND_SLACK: 2, HUMAN: "Operator",
Bubbles: {reading: () => 0.001}, wordsRead: () => 0, saidOf: (item) => item.words,
keep: stub, drawRadioSet: stub, drawBubbles: stub, drawSpeaker: stub, duck: stub, clearBubbles: stub, silenceBeeps: stub, toast: stub,
performance: {now: () => 0}, window: {setTimeout: () => 0, clearTimeout: stub}, context: () => audio,
};
vm.runInNewContext(code, sandbox);
sandbox.gibberEncode = async (step) => {
run.encoded.push([step.pan, step.frames.length]);
return {rate: 48000, samples: step.frames.map((frame) => new Float32Array(frame.length * 10))};
};
run.sandbox = sandbox;
run.say = (item) => sandbox.say({voice: item.voice, tone: "direct", cls: item.class, who: item.source, line: item.text, words: [item.text], silent: false, row: item});
run.idle = () => !sandbox.speech.busy && !sandbox.speech.pending.length && sandbox.speech.source === null;
return run;
}
const long = "E ".repeat(200);
(async () => {
const whole = radio(true, {});
whole.say(row("s1", -0.4, "post", long));
await settle();
const quiet = radio(true, {squelch: 0.5});
quiet.say(row("s1", 0.4, "post", "E"));
await settle();
const mute = radio(true, {squelch: 0});
mute.say(row("s1", 0.4, "post", "E"));
await settle();
const stop = radio(false, {});
stop.say(row("s1", -0.4, "post", long));
await settle();
const playing = [stop.buffers.length, stop.sandbox.speech.source === stop.buffers[0]];
stop.sandbox.cancelSpeech();
await settle();
const urgent = radio(false, {});
urgent.say(row("s1", -0.8, "post", long));
await settle();
urgent.say(row("s2", 0.8, "mayday", "E"));
await settle();
const during = {buffers: urgent.buffers.map((node, index) => [urgent.pans[index].value, node.stops.includes("now")]), pending: urgent.sandbox.speech.pending.map((item) => item.cls)};
urgent.buffers[1].ended();
await settle();
const switched = radio(false, {});
switched.say(row("s1", -0.4, "post", long));
await settle();
switched.sandbox.keepRadio({mode: "morse"});
await settle();
console.log(JSON.stringify({
whole: {encoded: whole.encoded, length: whole.buffers.map((node) => node.buffer.length), pan: whole.pans.map((pan) => pan.value), gain: whole.gains.map((gain) => gain.value), idle: whole.idle()},
quiet: quiet.gains.map((gain) => gain.value),
mute: [mute.encoded.length, mute.buffers.length, mute.idle()],
stop: {playing, stops: stop.buffers[0].stops, idle: stop.idle()},
urgent: {during, encoded: urgent.encoded, after: urgent.buffers.map((node, index) => [urgent.pans[index].value, node.started])},
switched: {stops: switched.buffers[0].stops, oscillators: switched.oscillators.length},
}));
})();
"""
ANSWER_PROBE = """
const fs = require("fs");
const vm = require("vm");
const posted = [];
const listeners = [];
const frame = {contentWindow: {postMessage: (message) => posted.push(message)}, addEventListener: (type, loaded) => loaded()};
const sandbox = {
h: () => frame, document: {body: {append: () => undefined}},
window: {addEventListener: (type, heard) => listeners.push(heard), setTimeout: () => 0, clearTimeout: () => undefined},
};
vm.runInNewContext(fs.readFileSync(process.argv[1], "utf8"), sandbox);
const Floats = vm.runInContext("Float32Array", sandbox);
const settle = () => new Promise((resolve) => setImmediate(resolve));
const outcome = (promise) => promise.then((value) => ({rate: value.rate, parts: value.samples.length}), (error) => ({error: error.message}));
(async () => {
const good = outcome(sandbox.gibberEncode({protocol: "AUDIBLE_FAST", frames: ["a", "b"]}));
const bad = outcome(sandbox.gibberEncode({protocol: "AUDIBLE_FAST", frames: ["c"]}));
const failed = outcome(sandbox.gibberEncode({protocol: "MT_FAST", frames: ["d"]}));
await settle();
const answer = (source, data) => listeners.forEach((heard) => heard({source, data}));
answer({}, {id: 1, rate: 1, samples: []});
answer(frame.contentWindow, {id: 1, rate: 48000, samples: [new Floats(2), new Floats(3)]});
answer(frame.contentWindow, {id: 2, rate: "fast", samples: [[0.1]]});
answer(frame.contentWindow, {id: 3, error: "refused"});
console.log(JSON.stringify({posted, listeners: listeners.length, answers: await Promise.all([good, bad, failed])}));
})();
"""
DECODE_PROBE = """
const factory = require(process.argv[1]);
const Gibberlink = require(process.argv[2]);
const R = require(process.argv[3]);
const input = JSON.parse(process.argv[4]);
factory({print: () => undefined, printErr: () => undefined}).then((ggwave) => {
const frames = R.frames(input.key, input.text);
const heard = (parts, rate) => {
const gap = Math.round(rate * 0.3);
const joined = new Float32Array(parts.reduce((sum, part) => sum + part.length + gap, 0));
parts.reduce((at, part) => {
joined.set(part, at);
return at + part.length + gap;
}, 0);
const parameters = ggwave.getDefaultParameters();
parameters.sampleRateInp = rate;
const listener = ggwave.init(parameters);
const texts = [];
for (let at = 0; at < joined.length; at += 1024) {
const got = ggwave.decode(listener, new Int8Array(joined.slice(at, at + 1024).buffer));
if (got && got.length) {
texts.push(new TextDecoder().decode(new Uint8Array(got.buffer, got.byteOffset, got.length)));
}
}
ggwave.free(listener);
return texts;
};
const decoded = {};
const rates = [];
for (const protocol of input.protocols) {
const {rate, samples} = Gibberlink.encode(ggwave, {protocol, frames});
rates.push(rate);
decoded[protocol] = heard(samples, rate);
}
const refused = input.wrong.map((request) => {
try {
Gibberlink.encode(ggwave, request);
return false;
} catch (error) {
return true;
}
});
console.log(JSON.stringify({frames, decoded, rates, refused, ids: Object.keys(ggwave.ProtocolId)}));
});
"""
def node(script: str, *args: str) -> dict:
return json.loads(subprocess.run(["node", "-e", script, *args], check=True, capture_output=True, text=True, timeout=600).stdout)
def row(cls: str, text: str) -> dict:
return {"class": cls, "source": KEY, "voice": "v1", "word": "W" if cls in ALARMS else "", "rest": "R", "text": text, "speaker": "Docs", "called": ["Docs"], "pitch": 600, **SOUND}
def header(index: int, count: int) -> str:
return f"{KEY if index == 1 else KEY[:8]} {index}/{count} "
def payloads(frames: list[str]) -> list[str]:
return [frame[len(header(index, len(frames))) :] for index, frame in enumerate(frames, start=1)]
class TestFrames(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.long = "a" * 300
cls.edges = ["b" * offset + WIDE * 40 for offset in range(8)]
texts = [[KEY, cls.long], [KEY, ""], *[[KEY, text] for text in cls.edges]]
live = {"tts": True, "open": True}
cls.cases = [(mode, fallback, gate, name) for mode in ("gibberlink", "quiet", "auto", "silence") for fallback in ("gibberlink", "morse", "burst") for gate in (True, False) for name in council.CLASSES]
plans = [[row(name, f"hello {MARK}"), {"mode": mode, "fallback": fallback, "classes": list(council.CLASSES), "muted": []}, {**live, "open": gate}] for mode, fallback, gate, name in cls.cases]
plans.append([row("post", "hello"), {"mode": "gibberlink", "fallback": "burst", "classes": list(council.CLASSES), "muted": []}, {"tts": False, "open": False}])
sets = [{}, {"fallback": "morse"}, {"fallback": "burst"}, {"fallback": "hiss"}, {"fallback": 1}, {"mode": "gibberlink"}]
cls.data = node(FRAMES_PROBE, str(RADIOSET), json.dumps({"texts": texts, "plans": plans, "sets": sets}))
def test_a_300_byte_message_splits_into_frames_of_at_most_140_bytes_with_the_key_header_and_joins_back(self):
frames = self.data["split"][0]
self.assertGreaterEqual(len(frames), 3)
for index, frame in enumerate(frames, start=1):
with self.subTest(frame=index):
self.assertLessEqual(len(frame.encode("utf-8")), 140)
self.assertTrue(frame.startswith(header(index, len(frames))), frame)
self.assertEqual("".join(payloads(frames)), self.long)
self.assertEqual(self.data["split"][1], [f"{KEY} 1/1 "], "an empty text still sends its sender")
def test_umlauts_euro_and_emoji_at_every_frame_edge_split_between_characters(self):
for offset, frames in enumerate(self.data["split"][2:]):
with self.subTest(offset=offset):
sizes = [len(frame.encode("utf-8")) for frame in frames]
self.assertTrue(all(size <= 140 for size in sizes), sizes)
self.assertGreater(max(sizes), 136, "the frames fill the budget up to the last whole character")
self.assertEqual("".join(payloads(frames)), self.edges[offset])
def test_gibberlink_mode_gives_the_alarm_classes_their_word_then_the_whole_message_and_every_other_class_the_frames_only(self):
gibberlink = {"kind": "gibberlink", "frames": [f"{KEY} 1/1 hello"], **SOUND}
for case, plan in zip(self.cases, self.data["plans"], strict=False):
mode, _, _, name = case
if mode != "gibberlink":
continue
with self.subTest(case=case):
self.assertEqual(plan, [{"kind": "word", "text": "W"}, gibberlink] if name in ALARMS else [gibberlink])
self.assertEqual(self.data["plans"][-1], [gibberlink], "without the speech engine no procedure word is rendered")
def test_quiet_and_auto_with_the_gate_closed_play_the_fallback_pick_after_the_word(self):
standins = {"gibberlink": {"kind": "gibberlink", "frames": [f"{KEY} 1/1 hello"], **SOUND}, "burst": {"kind": "burst"}}
for case, plan in zip(self.cases, self.data["plans"], strict=False):
mode, fallback, gate, name = case
word = [{"kind": "word", "text": "W"}] if name in ALARMS else []
with self.subTest(case=case):
if mode == "silence":
self.assertEqual(plan, word)
elif mode == "auto" and gate:
self.assertEqual(plan, [*word, {"kind": "speech", "text": "R"}])
elif mode != "gibberlink" and fallback == "morse":
self.assertEqual((plan[:-1], plan[-1]["kind"], plan[-1]["text"]), (word, "morse", "DE Docs hello "))
elif mode != "gibberlink":
self.assertEqual(plan, [*word, standins[fallback]])
def test_the_fallback_pick_is_kept_per_browser_beside_the_mode_and_never_in_the_hub_settings(self):
self.assertEqual(self.data["kept"], ["gibberlink", "morse", "burst", "gibberlink", "gibberlink", "gibberlink"])
constants = self.data["constants"]
self.assertEqual((constants["modes"], constants["fallbacks"], constants["fallback"]), (["auto", "quiet", "silence", "morse", "gibberlink"], ["gibberlink", "morse", "burst"], "gibberlink"))
self.assertEqual((constants["frame"]["bytes"], constants["frame"]["prefix"], constants["key"]), (140, 8, "kvb-orchestrator.radio"))
self.assertNotIn("fallback", settings.SETTINGS)
self.assertNotIn("fallback", json.dumps(settings.SETTINGS))
class TestSpeakerSound(unittest.TestCase):
def test_every_speaker_gets_one_stable_protocol_and_stereo_position_from_its_slot(self):
keys = [f"s{number}" for number in range(200)]
sounds = [engines.sound_of(key) for key in keys]
self.assertEqual(sounds, [engines.sound_of(key) for key in keys])
self.assertTrue(all(sound["protocol"] in engines.GIBBERLINK and sound["pan"] in engines.PANS for sound in sounds))
self.assertEqual(len({(sound["protocol"], sound["pan"]) for sound in sounds}), len(engines.GIBBERLINK) * len(engines.PANS))
with mock.patch.object(engines, "slot", lambda key, count: count - 1):
self.assertEqual(engines.sound_of("s1"), {"protocol": engines.GIBBERLINK[-1], "pan": engines.PANS[-1]})
self.assertEqual(engines.sound_of("s1")["protocol"], engines.GIBBERLINK[engines.slot("s1", 20) % 4])
def test_speakers_in_distinct_slots_get_distinct_protocols_from_the_vendored_source(self):
picks = {}
for number in range(200):
picks.setdefault(engines.slot(f"s{number}", 20) % len(engines.GIBBERLINK), f"s{number}")
self.assertEqual(len({engines.sound_of(key)["protocol"] for key in picks.values()}), len(engines.GIBBERLINK))
self.assertEqual(engines.GIBBERLINK, ("AUDIBLE_NORMAL", "AUDIBLE_FAST", "AUDIBLE_FASTEST", "DT_FASTEST"))
class TestPlaying(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.data = node(PLAYING_PROBE, str(RADIOSET), str(MORSE), str(VOICE), str(PAGE_SPEECH), str(PAGE_MORSE), str(PAGE_GIBBERLINK))
def test_the_frames_play_once_in_the_sender_position_at_the_tone_volume_times_the_squelch(self):
whole = self.data["whole"]
self.assertEqual(whole["encoded"], [[-0.4, 3]])
self.assertEqual((len(whole["length"]), whole["pan"], whole["gain"], whole["idle"]), (1, [-0.4], [0.5], True))
self.assertGreater(whole["length"][0], 399 * 10 + 3 * 48000 * 0.3 - 1, "every frame with a 0.3 second gap at the encoder's 48 kHz")
self.assertEqual(self.data["quiet"], [0.25])
self.assertEqual(self.data["mute"], [0, 0, True], "squelch 0 encodes and plays nothing")
def test_stop_ends_the_frames_at_once(self):
stop = self.data["stop"]
self.assertEqual((stop["playing"], stop["stops"], stop["idle"]), ([1, True], ["now"], True))
def test_a_mayday_cuts_a_post_whose_frames_play_and_the_post_sends_again_whole_after_it(self):
urgent = self.data["urgent"]
self.assertEqual(urgent["during"], {"buffers": [[-0.8, True], [0.8, False]], "pending": ["post"]})
self.assertEqual(urgent["encoded"], [[-0.8, 3], [0.8, 1], [-0.8, 3]])
self.assertEqual(urgent["after"][2], [-0.8, "now"])
def test_a_mode_change_cuts_the_frames_and_replans_the_row(self):
self.assertEqual(self.data["switched"], {"stops": ["now"], "oscillators": 1})
class TestEncoderAnswers(unittest.TestCase):
def test_the_page_takes_samples_only_from_its_own_encoder_frame_and_refuses_anything_else(self):
data = node(ANSWER_PROBE, str(PAGE_GIBBERLINK))
self.assertEqual(data["listeners"], 1, "one encoder frame, one listener, however often the radio encodes")
self.assertEqual(data["posted"], [{"id": 1, "protocol": "AUDIBLE_FAST", "frames": ["a", "b"]}, {"id": 2, "protocol": "AUDIBLE_FAST", "frames": ["c"]}, {"id": 3, "protocol": "MT_FAST", "frames": ["d"]}])
self.assertEqual(data["answers"], [{"rate": 48000, "parts": 2}, {"error": "the Gibberlink encoder answered no samples"}, {"error": "refused"}])
class TestEncoder(unittest.TestCase):
def test_every_protocol_decodes_back_to_the_frame_texts_and_wrong_requests_are_refused(self):
text = f"disk full on {WIDE} " + "rotate the keys " * 15
wrong = [{"protocol": "MT_FAST", "frames": ["x"]}, {"protocol": "values", "frames": ["x"]}, {"protocol": "AUDIBLE_FAST", "frames": [1]}, {"protocol": "AUDIBLE_FAST", "frames": "x"}, {"frames": ["x"]}]
data = node(DECODE_PROBE, str(GGWAVE), str(GIBBERLINK), str(RADIOSET), json.dumps({"key": KEY, "text": text, "protocols": engines.GIBBERLINK, "wrong": wrong}))
self.assertGreaterEqual(len(data["frames"]), 3)
self.assertEqual(list(data["decoded"]), list(engines.GIBBERLINK))
for name, heard in data["decoded"].items():
with self.subTest(protocol=name):
self.assertEqual(heard, data["frames"])
self.assertEqual((set(data["rates"]), data["refused"]), ({48000}, [True] * len(wrong)))
for name in engines.GIBBERLINK:
with self.subTest(protocol=name):
self.assertIn(f"GGWAVE_PROTOCOL_{name}", data["ids"])
class TestEncoderHttp(HubCase):
def test_the_encoder_page_carries_its_own_csp_and_the_hub_page_never_allows_eval(self):
status, headers, body = self.get("/gibberlink")
page = body.decode("utf-8")
nonces = set(re.findall(r'<script nonce="([^"]+)"', page))
self.assertEqual((status, headers["content-type"], len(nonces)), (200, "text/html; charset=utf-8", 1), page)
csp = headers["content-security-policy"]
self.assertEqual(csp, ENCODER_CSP.format(nonces.pop()))
self.assertIn("'unsafe-eval' 'wasm-unsafe-eval'", csp)
self.assertTrue(csp.startswith("sandbox allow-scripts;"), "the server keeps the encoder in an opaque origin even at top level")
self.assertIn("connect-src 'none'", csp)
self.assertIn('src="/vendor/ggwave.js"', page)
self.assertIn("Gibberlink.serve(window, ggwave_factory(", page)
self.assertLess(page.index("window.fetch = () => Promise.reject("), page.index("Gibberlink.serve("), "the frame asks for nothing before ggwave loads its wasm")
self.assertNotEqual(self.get("/gibberlink")[1]["content-security-policy"], csp, "every load gets a fresh nonce")
index = self.get("/")[1]["content-security-policy"]
self.assertNotIn("unsafe-eval", index)
self.assertIn("frame-src 'self'", index)
def test_the_vendor_route_serves_ggwave_under_the_plain_csp(self):
status, headers, body = self.get("/vendor/ggwave.js")
self.assertEqual((status, headers["content-type"], headers["content-security-policy"]), (200, "text/javascript; charset=utf-8", exchange.PLAIN_CSP))
self.assertEqual(body, GGWAVE.read_bytes())
def test_every_radio_row_carries_the_sender_protocol_and_stereo_position(self):
self.claim("s1", "Docs")
self.claim("s2", "Skills")
now = time.time()
self.write_council({"news.jsonl": self.jsonl([
{"ts": now - 30, "from": "Docs", "to": ["Skills"], "text": "gate red", "class": "call", "v": 2},
{"ts": now - 20, "from": "Skills", "to": [], "text": "MAYDAY disk full", "class": "mayday", "v": 2},
])})
rows = [(row["source"], row["protocol"], row["pan"]) for row in self.snapshot()["radio"]]
self.assertEqual(rows, [(key, *engines.sound_of(key).values()) for key in ("s1", "s2")])
if __name__ == "__main__":
unittest.main()
+2 -2
View File
@@ -137,7 +137,7 @@ const ons = (run) => run.gains[0].filter(([kind, value]) => kind === "ramp" && v
const switched = radio(false);
switched.say(row("s1", 640, "security", "E"));
await settle();
switched.sandbox.keepRadio({mode: "quiet"});
switched.sandbox.keepRadio({mode: "quiet", fallback: "burst"});
await settle();
const urgent = radio(false);
urgent.say(row("s1", 600, "post", "E ".repeat(200)));
@@ -231,7 +231,7 @@ class TestMorseCode(unittest.TestCase):
def test_morse_is_a_fourth_mode_and_the_threshold_labels_read_never_and_always_at_the_ends(self):
constants = self.data["constants"]
self.assertEqual((constants["modes"], constants["kept"]), (["auto", "quiet", "silence", "morse"], "morse"))
self.assertEqual((constants["modes"][:4], constants["kept"]), (["auto", "quiet", "silence", "morse"], "morse"))
self.assertEqual(constants["gate"], {"min": settings.THRESHOLDS[0], "max": settings.THRESHOLDS[-1], "step": settings.THRESHOLDS[1] - settings.THRESHOLDS[0]})
self.assertEqual(self.data["gates"], ["never", "speak while CPU < 10 %", "speak while CPU < 50 %", "speak while CPU < 90 %", "always"])
+1 -1
View File
@@ -20,7 +20,7 @@ class TestHubPage(HubCase):
review_id = self.request("feat: csp", "")
status, headers, _ = self.get(f"/reviews/{review_id}/page")
self.assertEqual(status, 200)
self.assertEqual(headers["content-security-policy"], "sandbox allow-scripts")
self.assertEqual(headers["content-security-policy"], "sandbox allow-scripts; frame-ancestors 'none'")
self.assertEqual(headers["x-content-type-options"], "nosniff")
status, headers, body = self.get("/")
csp = headers["content-security-policy"]
+5 -5
View File
@@ -119,7 +119,7 @@ const row = {class: "security", source: "s1", voice: "v1", word: "W", rest: "R",
async function queued(flip) {
const heard = [];
const sandbox = {
RadioSet, radioSet: RadioSet.valid({mode: "auto"}), state: {speech: {tts: true, gate: {open: true}}},
RadioSet, radioSet: RadioSet.valid({mode: "auto", fallback: "burst"}), state: {speech: {tts: true, gate: {open: true}}},
speech: {pending: [], busy: false, current: null, source: null, item: null, timer: 0}, settings: {bubbles: false}, SPEECH_QUEUE: 9,
Voice: {enqueue: (pending, next) => [...pending, next]}, drawBubbles: stub, drawSpeaker: stub,
};
@@ -128,7 +128,7 @@ async function queued(flip) {
sandbox.spoken = async (audio, item, step) => {
heard.push(step.text);
if (heard.length === 1 && flip) {
sandbox.radioSet = RadioSet.valid({mode: flip});
sandbox.radioSet = RadioSet.valid({mode: flip, fallback: "burst"});
}
return "";
};
@@ -248,7 +248,7 @@ function radio({running = true, hold = "", long = "", fail = 0, scale = 1000, bu
decodeAudioData: async (data) => ({duration: data.text === long ? 60 : 1, text: data.text}),
};
const sandbox = {
RadioSet, Voice, radioSet: RadioSet.valid(null), state: {now: 1000, messages: [], speech: {tts: true, gate: {open: true}}},
RadioSet, Voice, radioSet: RadioSet.valid({fallback: "burst"}), state: {now: 1000, messages: [], speech: {tts: true, gate: {open: true}}},
speech: {pending: [], busy: false, warned: 0, current: null, source: null, loading: null, timer: 0, item: null},
settings: {bubbles, music: {on: false, volume: 0.4}}, SPEECH_QUEUE: 20, SPEECH_QUIET: 60000, SOUND_SLACK: 2, HUMAN: "Operator", Bubbles: {reading: () => 0.001},
keep: stub, drawRadioSet: stub, drawBubbles: stub, drawSpeaker: stub, duck: stub, clearBubbles: stub, wordsRead: () => 0, saidOf: (item) => item.words, toast: (title) => run.log.push(["toast", title]),
@@ -437,7 +437,7 @@ def run(calls: dict[str, list[list]]) -> dict:
class TestRadioSetModule(unittest.TestCase):
@classmethod
def setUpClass(cls):
defaults = {"mode": "auto", "classes": ["mayday", "pan-pan", "security"], "muted": ["s9"], "sounds": {}, "squelch": 1, "log": {}}
defaults = {"mode": "auto", "fallback": "burst", "classes": ["mayday", "pan-pan", "security"], "muted": ["s9"], "sounds": {}, "squelch": 1, "log": {}}
modes = ("auto", "quiet", "silence")
lives = ({"tts": True, "open": True}, {"tts": True, "open": False}, {"tts": False, "open": True})
plans = [["plan", ROWS[name], {**defaults, "mode": mode}, live] for mode in modes for live in lives for name in ROWS]
@@ -596,7 +596,7 @@ class TestTransmit(unittest.TestCase):
self.assertEqual(self.data["stalled"], {"before": [False, True], "after": [True, None], "waits": [2500], "stops": 1})
def test_every_mode_button_keeps_the_mode_before_it_resumes_the_audio_context(self):
self.assertEqual(self.data["pressed"], [{"mode": "auto"}, "context", {"mode": "quiet"}, "context", {"mode": "silence"}, "context", {"mode": "morse"}, "context"])
self.assertEqual(self.data["pressed"], [{"mode": "auto"}, "context", {"mode": "quiet"}, "context", {"mode": "silence"}, "context", {"mode": "morse"}, "context", {"mode": "gibberlink"}, "context"])
class TestSpeechModes(unittest.TestCase):