From d882e501c19a2025ac8653ca468705b3efa9356f Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 14 Nov 2026 21:23:12 +0000 Subject: [PATCH] fix(orchestrator): queue every speech row of a poll before one redraw MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit speakRows queues all speech rows of a poll before the single redraw, so a MAYDAY behind other rows is never lost, and a draw that throws shows a toast instead of stopping the pump. Late and stale rows both bubble. Tests run once on unify/skills-20261114 after the merge (operator order 2026-11-14). 🎯 Fix: 80 % 💥 Break: 5 % 💸 Tokens: 100 % ⏱️ Velocity: 100 % 🛡️ Security: 100 % 🏆 Score: 76 🚦 Gate: ⛔ fix 80 % < 90 Co-Authored-By: Claude Opus 5.5 --- docs/requirements/003-operator-workbench.md | 40 ++- orchestrator/templates/bubbles.js | 50 +++- orchestrator/templates/page-alerts.js | 9 +- orchestrator/templates/page-bubbles.js | 41 ++- orchestrator/templates/page-constants.js | 1 + orchestrator/templates/page-load.js | 3 +- orchestrator/templates/page-speech.js | 61 +++-- orchestrator/templates/page-state.js | 2 +- tests/e2e/test_hub_bubbles_live_e2e.py | 271 +++++++++++++++++++ tests/test_hub_bubbles.py | 285 +++++++++++++++++++- 10 files changed, 732 insertions(+), 31 deletions(-) create mode 100644 tests/e2e/test_hub_bubbles_live_e2e.py diff --git a/docs/requirements/003-operator-workbench.md b/docs/requirements/003-operator-workbench.md index 94e3d80..c0bf332 100644 --- a/docs/requirements/003-operator-workbench.md +++ b/docs/requirements/003-operator-workbench.md @@ -92,7 +92,26 @@ changed and what it costs, and a gate means something again. TTS the bubbles run at reading pace. Font: vendored Comic Neue (SIL OFL 1.1). Rejected: a ticker, the full text with a karaoke mark, bubbles only with audio, system fonts only, a switch only in the settings, a caption - under the speaker only. + under the speaker only. Superseded by decision 18 (2026-11-14): bubbles + only for the rows read aloud, since with the default read-aloud classes + the bulk of the radio (Post, Broadcast, Newsletter) showed no bubble at + all. +18. **A bubble for every live row** (operator-confirmed 2026-11-14): with the + bubbles on, every radio row that arrives live gets a bubble, whatever its + class, with no per-class choice; reading aloud stays unchanged (default + MAYDAY, PAN-PAN, Sécurité). A row that is not read aloud shows a silent + bubble beside the speech queue, never in it, so it takes no place in the + speech queue: its first line up to 80 characters at reading pace, at most + 3 at once, one per agent, the speech queue first, at most 30 waiting. The + backlog at page load, after a reconnect or after a poll gap of more than + 30 s, and any row older than 30 s, shows none. All rows of one poll reach + the speech queue before its one redraw, and a failing redraw only raises + a toast, so drawing never costs a row its speech. Rejected: a per-class + bubble choice, silent rows in the speech queue (a silent row being read + holds the one speech slot, so even a MAYDAY waits out its reading time, + routine rows read aloud wait behind all of them, and 20 of them fill the + queue and push routine rows out), every bubble at once (a burst of 30 + covers the stage). ## Approach @@ -370,6 +389,16 @@ The work packages meet only through these interfaces: bubble at the size of its earliest. - [ ] With speech off, TTS unavailable or muted, the same rows run through the same queue timed at 180 words per minute, at least 1.5 s each. +- [x] Every radio row that arrives live and is not read aloud shows a silent + bubble beside the speech queue (decision 18): first line up to 80 + characters at reading pace, at most 3 at once, one per agent, at most + 30 waiting; the backlog at page load, after a reconnect or after a poll + gap of more than 30 s, and a row older than 30 s, shows none; it anchors + like every other bubble, at the agent's face or in the caption bar. +- [x] A fault while a row sounds or is read (a throwing redraw, a rejected + sound) still hands the speech queue to the next row; all rows of one + poll reach the speech queue before its one redraw, and a throwing + redraw raises a toast and never costs a row its speech. - [x] Bubble text uses Comic Neue as a vendored woff2 with its SIL OFL 1.1 text in `orchestrator/vendor/`, stack `"Comic Neue", "Comic Sans MS", cursive`; no external font request, the CSP stays unchanged. @@ -402,6 +431,10 @@ The work packages meet only through these interfaces: fold. - [x] Bubble schedule: scale per queue position, word reveal times from a duration, reading-pace duration, one bubble per agent. + - [x] Rows of one poll: a throwing redraw still queues the MAYDAY behind a + Post, 1000 silent rows draw once, silent rows never enter the speech + queue; no silent bubble after a poll gap over 30 s, after a reconnect + or for a row older than 30 s. - Integration: - [x] A scratch repository with installed hooks refuses a commit without a receipt and passes it after a queue gate; refuses a merge below the @@ -423,3 +456,8 @@ The work packages meet only through these interfaces: largest with its words appearing, the second as a smaller `…`; after the first ends the second grows; the setting survives a reload; off hides the layer. + - [ ] Bubbles on, Auto with the CPU gate closed: a Post, a Broadcast and a + Newsletter arriving live each show a silent bubble; the backlog at + load and after a reconnect shows none; a burst of 30 rows shows at + most 3 at once; a Sécurité after 10 silent rows starts speaking + within 5 s of the poll and at most 2 s later than the same row alone. diff --git a/orchestrator/templates/bubbles.js b/orchestrator/templates/bubbles.js index 99ea4ac..b3ca942 100644 --- a/orchestrator/templates/bubbles.js +++ b/orchestrator/templates/bubbles.js @@ -4,6 +4,9 @@ const Bubbles = (() => { const SMALLEST = 0.5; const PACE = 180; const SHORTEST = 1.5; + const QUIET_SCALE = 0.8; + const QUIET_CAP = 3; + const QUIET_WAIT = 30; function scale(position) { return SCALES[position] ?? SMALLEST; @@ -29,20 +32,57 @@ const Bubbles = (() => { return row.class === "mayday" || speech.classes.includes(row.class); } - function plan(head, pending) { + /** + * @param {object|null} head the row that sounds or loads now. + * @param {Array} pending the rows of the speech queue, in order. + * @param {Array} [quiet] the silent bubbles on screen, rows that are not read aloud. + * @returns {Array} who, item and scale of one bubble per agent: the speech queue first, by its position, then the silent bubbles of agents the queue leaves free. + */ + function plan(head, pending, quiet) { const queue = head ? [head, ...pending] : pending; const shift = head ? 0 : 1; const seen = new Set(); - return queue.flatMap((item, index) => { + const once = (item, size) => { if (seen.has(item.who)) { return []; } seen.add(item.who); - return [{who: item.who, item, scale: scale(index + shift)}]; - }); + return [{who: item.who, item, scale: size}]; + }; + return [...queue.flatMap((item, index) => once(item, scale(index + shift))), ...(quiet || []).flatMap((item) => once(item, QUIET_SCALE))]; } - return {SCALES, SMALLEST, PACE, SHORTEST, scale, words, reading, reveals, shown, picked, plan}; + /** + * @param {Array} showing the silent bubbles on screen now. + * @param {Array} waiting the silent bubbles that wait, oldest first. + * @param {Set} taken the agents a speech bubble shows now. + * @returns {object} start: the waiting bubbles that start now, oldest first, so that at most QUIET_CAP show and no agent shows two; waiting: the rest, still waiting. + */ + function admit(showing, waiting, taken) { + const busy = new Set([...taken, ...showing.map((item) => item.who)]); + const start = []; + const rest = []; + for (const item of waiting) { + if (showing.length + start.length < QUIET_CAP && !busy.has(item.who)) { + busy.add(item.who); + start.push(item); + } else { + rest.push(item); + } + } + return {start, waiting: rest}; + } + + /** + * @param {Array} waiting the silent bubbles that wait, oldest first. + * @param {object} item the silent bubble of a row that arrived live. + * @returns {Array} the waiting bubbles with the item last, at most QUIET_WAIT, the oldest dropped first. + */ + function queued(waiting, item) { + return [...waiting, item].slice(-QUIET_WAIT); + } + + return {SCALES, SMALLEST, PACE, SHORTEST, QUIET_SCALE, QUIET_CAP, QUIET_WAIT, scale, words, reading, reveals, shown, picked, plan, admit, queued}; })(); if (typeof module === "object" && module.exports) { module.exports = Bubbles; diff --git a/orchestrator/templates/page-alerts.js b/orchestrator/templates/page-alerts.js index a7cb36d..29771e0 100644 --- a/orchestrator/templates/page-alerts.js +++ b/orchestrator/templates/page-alerts.js @@ -49,7 +49,11 @@ notifyLabel(); } - function announce() { + /** + * @param {boolean} [resumed] whether this state is the first answer after the page was offline. + */ + function announce(resumed) { + const arrived = !resumed && known.now !== null && state.now - known.now <= LIVE_GAP; const alarms = new Map(alarmRows().map((row) => [`${row.ts}|${row.from}`, row])); const radio = new Set(state.radio.map((row) => `${row.ts}|${row.from}|${row.text.slice(0, 40)}`)); if (known.inbox !== null && live()) { @@ -63,7 +67,7 @@ play(kind, loudness(kind)); } const lead = kind ? Math.max(...TONES[kind].map(([, at, length]) => at + length)) : 0; - window.setTimeout(() => speakRows(newRadio), lead * 1000); + window.setTimeout(() => speakRows(newRadio, arrived), lead * 1000); if (settings.notify && "Notification" in window && Notification.permission === "granted") { for (const row of newAlarms) { new Notification(`${row.class === "mayday" ? "Mayday" : "Pan-Pan"} from ${dash(row.from)}`, {body: row.text.slice(0, 200), tag: `alarm-${row.ts}`, requireInteraction: row.class === "mayday"}); @@ -79,6 +83,7 @@ known.inbox = new Set(state.inbox.map(keyOf)); known.alarms = new Set(alarms.keys()); known.radio = radio; + known.now = state.now; for (const batch of state.pending) { if (!known.batches.has(batch.id)) { known.batches.add(batch.id); diff --git a/orchestrator/templates/page-bubbles.js b/orchestrator/templates/page-bubbles.js index 876be1a..d9e54c9 100644 --- a/orchestrator/templates/page-bubbles.js +++ b/orchestrator/templates/page-bubbles.js @@ -1,7 +1,7 @@ const BUBBLE_FONT = "/vendor/comic-neue.woff2"; const BUBBLE_FADE = 400; const BUBBLE_EDGE = 8; - const bubbles = {shown: new Map(), frame: 0, font: null}; + const bubbles = {shown: new Map(), frame: 0, font: null, quiet: [], waiting: [], warned: 0}; function bubbleLayer() { return document.getElementById("bubbles"); @@ -99,16 +99,49 @@ view.words.textContent = text; view.words.scrollTop = view.words.scrollHeight; } - Object.assign(view.node.dataset, {scale: String(entry.scale), state: item.started ? "speaking" : "waiting", words: String(count)}); + Object.assign(view.node.dataset, {scale: String(entry.scale), state: item.quiet ? "quiet" : item.started ? "speaking" : "waiting", words: String(count)}); view.node.style.setProperty("--scale", String(entry.scale)); placeBubble(view, anchorOf(entry.who), entry.scale); } + function startQuiet(spoken) { + const {start, waiting} = Bubbles.admit(bubbles.quiet, bubbles.waiting, new Set(spoken.map((entry) => entry.who))); + bubbles.waiting = waiting; + for (const item of start) { + Object.assign(item, {started: performance.now(), duration: Bubbles.reading(saidOf(item).length)}); + item.timer = window.setTimeout(() => { + bubbles.quiet = bubbles.quiet.filter((other) => other !== item); + drawBubbles(); + }, item.duration * 1000); + } + bubbles.quiet = [...bubbles.quiet, ...start]; + } + + /** + * @param {Array} items the silent bubbles of the rows one poll brought live, oldest first. + */ + function quietBubbles(items) { + bubbles.waiting = items.reduce(Bubbles.queued, bubbles.waiting); + } + function drawBubbles() { + try { + renderBubbles(); + } catch (error) { + if (Date.now() - bubbles.warned > SPEECH_QUIET) { + bubbles.warned = Date.now(); + toast("Drawing speech bubbles failed", [String(error.message || error)], false); + } + } + } + + function renderBubbles() { if (!settings.bubbles) { return; } - const planned = Bubbles.plan(speech.current || speech.loading, speech.pending); + const head = speech.current || speech.loading; + startQuiet(Bubbles.plan(head, speech.pending)); + const planned = Bubbles.plan(head, speech.pending, bubbles.quiet); const kept = new Set(planned.map((entry) => entry.item)); for (const [item, view] of bubbles.shown) { if (!kept.has(item)) { @@ -136,6 +169,8 @@ window.cancelAnimationFrame(bubbles.frame); bubbles.frame = 0; bubbles.shown.clear(); + bubbles.quiet.forEach((item) => window.clearTimeout(item.timer)); + Object.assign(bubbles, {quiet: [], waiting: []}); for (const node of bubbleLayer().querySelectorAll(".speech-bubble")) { node.remove(); } diff --git a/orchestrator/templates/page-constants.js b/orchestrator/templates/page-constants.js index 64990e4..0804f41 100644 --- a/orchestrator/templates/page-constants.js +++ b/orchestrator/templates/page-constants.js @@ -51,6 +51,7 @@ const SPEECH_QUEUE = 20; const SPEECH_QUIET = 60000; const SPEECH_FRESH = 300; + const LIVE_GAP = 30; const SOUND_SLACK = 2; const STATE_TIMEOUT = 15000; const THEME_CHOICES = [["sky", "Sky (follows the light outside)"], ["light", "Always light"], ["dark", "Always dark"], ["custom", "Custom colors"]]; diff --git a/orchestrator/templates/page-load.js b/orchestrator/templates/page-load.js index 2b21442..38c8b85 100644 --- a/orchestrator/templates/page-load.js +++ b/orchestrator/templates/page-load.js @@ -19,6 +19,7 @@ } appliedTicket = ticket; const previous = state.now; + const resumed = ui.offline; state = next; ui.offline = false; if (JSON.stringify(state.settings) !== settingsSig) { @@ -26,7 +27,7 @@ } tuneMusic(FADE / 1000); drawSky(); - announce(); + announce(resumed); render(false); drawTime(); drawAgent(); diff --git a/orchestrator/templates/page-speech.js b/orchestrator/templates/page-speech.js index 6fb75f8..55f714b 100644 --- a/orchestrator/templates/page-speech.js +++ b/orchestrator/templates/page-speech.js @@ -41,8 +41,11 @@ } } - function say(item) { - speech.pending = Voice.enqueue(speech.pending, item, SPEECH_QUEUE); + /** + * @param {...object} items the rows to read aloud. + */ + function say(...items) { + speech.pending = items.reduce((pending, item) => Voice.enqueue(pending, item, SPEECH_QUEUE), speech.pending); pump(); drawBubbles(); } @@ -52,8 +55,11 @@ speech.current = item; speech.timer = window.setTimeout(() => { Object.assign(speech, {current: null, timer: 0}); - drawSpeaker(); - next(); + try { + drawSpeaker(); + } finally { + next(); + } }, item.duration * 1000); drawSpeaker(); } @@ -77,11 +83,14 @@ let timer = 0; const ended = () => { window.clearTimeout(timer); - if (speech.source === source) { - Object.assign(speech, {current: null, source: null}); - drawSpeaker(); + try { + if (speech.source === source) { + Object.assign(speech, {current: null, source: null}); + drawSpeaker(); + } + } finally { + resolve(); } - resolve(); }; source.addEventListener("ended", ended, {once: true}); timer = window.setTimeout(() => { @@ -216,18 +225,26 @@ readSilently(item, next); return; } - const voiced = await transmit(item, audio); + let voiced = false; + try { + voiced = await transmit(item, audio); + } catch (error) { + speechFailed([String(error.message || error)]); + } if (speech.item !== item) { next(); return; } Object.assign(speech, {current: null, source: null, item: null}); - drawSpeaker(); if (!voiced && settings.bubbles && item.words) { readSilently(item, next); return; } - next(); + try { + drawSpeaker(); + } finally { + next(); + } } function cancelSpeech() { @@ -243,14 +260,24 @@ drawSpeaker(); } - function speakRows(rows) { - for (const row of rows.filter((entry) => entry.ts >= state.now - SPEECH_FRESH)) { - const steps = RadioSet.plan(row, radioSet, liveSpeech()); - if (steps.length || (settings.bubbles && Bubbles.picked(row, radioSet))) { - const line = row.text.split("\n").map((part) => part.trim()).find(Boolean) || ""; - say({row, voice: row.voice, tone: Voice.toneOf(row, HUMAN), cls: row.class, who: row.source, sent: Boolean(RadioSet.ledgerOf(row, state.messages)), line: short(line), words: Bubbles.words(row.text), silent: !steps.length}); + /** + * @param {Array} rows the radio rows the page has not announced yet. + * @param {boolean} arrived whether they arrived while the page polled without a gap. + */ + function speakRows(rows, arrived) { + const [spoken, quiet] = [[], []]; + for (const row of rows) { + const fresh = row.ts >= state.now - SPEECH_FRESH; + const steps = fresh ? RadioSet.plan(row, radioSet, liveSpeech()) : []; + const line = short(row.text.split("\n").map((part) => part.trim()).find(Boolean) || ""); + if (fresh && (steps.length || (settings.bubbles && Bubbles.picked(row, radioSet)))) { + spoken.push({row, voice: row.voice, tone: Voice.toneOf(row, HUMAN), cls: row.class, who: row.source, sent: Boolean(RadioSet.ledgerOf(row, state.messages)), line, words: Bubbles.words(row.text), silent: !steps.length}); + } else if (settings.bubbles && arrived && row.ts >= state.now - LIVE_GAP) { + quiet.push({who: row.source, cls: row.class, line, words: Bubbles.words(line), quiet: true}); } } + quietBubbles(quiet); + say(...spoken); } function micGap() { diff --git a/orchestrator/templates/page-state.js b/orchestrator/templates/page-state.js index a66183e..cf049ba 100644 --- a/orchestrator/templates/page-state.js +++ b/orchestrator/templates/page-state.js @@ -37,7 +37,7 @@ const stream = {key: null, events: [], entries: [], last: 0, sessions: {}, busy: false, again: false, dom: null, error: ""}; const tree = {data: null, model: null, busy: false, q: "", open: new Set(), dom: null}; const palette = {items: [], index: 0, input: null, list: null, plan: null, box: null}; - const known = {inbox: null, alarms: null, radio: null, batches: new Set()}; + const known = {inbox: null, alarms: null, radio: null, batches: new Set(), now: null}; const motion = {current: new Map(), from: new Map(), target: new Map(), start: 0, duration: 0, frame: 0, asOf: undefined}; const drawn = {screen: "", sig: ""}; const files = new Map(); diff --git a/tests/e2e/test_hub_bubbles_live_e2e.py b/tests/e2e/test_hub_bubbles_live_e2e.py new file mode 100644 index 0000000..a307e6d --- /dev/null +++ b/tests/e2e/test_hub_bubbles_live_e2e.py @@ -0,0 +1,271 @@ +from __future__ import annotations + +import json +import os +import shutil +import sys +import time +import unittest +from collections.abc import Callable +from pathlib import Path +from unittest import mock + +HERE = Path(__file__).resolve().parent +sys.path[:0] = [str(HERE.parent), str(HERE)] + +import test_orchestrator_e2e as base +from hub_case import HubCase +from hub_support import iso + +OUT = Path(os.environ.get("E2E_OUT", "/tmp/hub-bubbles-live-shots")) +SENDERS = ["Stage", "Docs", "Build", "Team", "Cosmos", "Store"] +CAP = 3 +LATE_MS = 5000 +SLACK_MS = 2000 +GUARD = """() => { + window.e2eRejections = []; + window.addEventListener("unhandledrejection", (event) => window.e2eRejections.push(String((event.reason && event.reason.stack) || event.reason))); + const fetched = window.fetch.bind(window); + window.fetch = (input, init) => (window.e2eOffline && String(input).includes("/api/state") ? Promise.reject(new Error("offline")) : fetched(input, init)); + try { + localStorage.setItem("kvb-orchestrator.radio", JSON.stringify({mode: "auto"})); + } catch (error) { + return; + } +}""" +PROBE = """(async () => {@HELPERS@ + const keys = @KEYS@; + const toggle = document.getElementById("bubbles-toggle"); + const layer = document.getElementById("bubbles"); + const live = () => all(".speech-bubble:not(.leaving)", layer); + const log = []; + let most = 0; + new MutationObserver((records) => { + for (const record of records) { + const nodes = [...record.addedNodes].filter((node) => node instanceof Element && node.matches(".speech-bubble")); + const changed = record.type === "attributes" && record.target.matches(".speech-bubble") ? [record.target] : []; + [...nodes, ...changed].forEach((node) => log.push({t: performance.now(), who: node.dataset.who || null, state: node.dataset.state || null})); + } + most = Math.max(most, live().length); + }).observe(layer, {subtree: true, childList: true, attributes: true, attributeFilter: ["data-state", "class"]}); + const cue = (word) => fetch("/api/tts", {method: "POST", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({text: word, voice: ""}), cache: "no-store"}); + const poll = () => window.dispatchEvent(new Event("focus")); + const cleared = () => until(() => !layer.querySelector(".speech-bubble"), 30000); + document.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true})); + toggle.click(); + const on = Boolean(await until(() => toggle.getAttribute("aria-pressed") === "true" && !layer.hidden, 8000)); + await sleep(1500); + poll(); + await sleep(3000); + const loaded = log.length; + window.e2eOffline = true; + poll(); + await sleep(1200); + await cue("e2e-backlog"); + poll(); + await sleep(1200); + const offline = text(document.getElementById("updated")).startsWith("No connection"); + window.e2eOffline = false; + poll(); + await sleep(4000); + const resumed = log.length - loaded; + let from = log.length; + await cue("e2e-live"); + poll(); + await until(() => [keys.Stage, keys.Docs, keys.Build].every((who) => log.slice(from).some((entry) => entry.who === who && entry.state === "quiet")), 20000); + const classes = Object.fromEntries(["Stage", "Docs", "Build"].map((name) => [name, log.slice(from).some((entry) => entry.who === keys[name] && entry.state === "quiet")])); + await cleared(); + most = 0; + from = log.length; + await cue("e2e-burst"); + poll(); + await sleep(12000); + const burst = {most, agents: new Set(log.slice(from).filter((entry) => entry.state === "quiet").map((entry) => entry.who)).size}; + toggle.click(); + await until(() => toggle.getAttribute("aria-pressed") === "false", 8000); + toggle.click(); + await until(() => toggle.getAttribute("aria-pressed") === "true", 8000); + await cleared(); + const timed = async (word) => { + const start = log.length; + await cue(word); + performance.clearResourceTimings(); + const asked = performance.now(); + poll(); + const spoke = await until(() => log.slice(start).find((entry) => entry.who === keys.Docs && entry.state === "speaking"), 20000); + const quiet = log.slice(start).filter((entry) => entry.state === "quiet" && spoke && entry.t <= spoke.t); + const fetches = performance.getEntriesByType("resource").filter((entry) => /\\/api\\/(state|tts)/.test(entry.name)).map((entry) => [entry.name.includes("/api/tts") ? "tts" : "state", Math.round(entry.startTime - asked), Math.round(entry.requestStart - entry.startTime), Math.round(entry.responseEnd - entry.startTime)]); + await cleared(); + return {delay: spoke ? Math.round(spoke.t - asked) : null, firstQuiet: quiet.length ? Math.round(quiet[0].t - asked) : null, quietBefore: new Set(quiet.map((entry) => entry.who)).size, fetches}; + }; + const speech = await timed("e2e-speech"); + const alone = await timed("e2e-alone"); + return {on, loaded, offline, resumed, classes, burst, speech, alone, rejections: window.e2eRejections}; +})()""" + + +@unittest.skipUnless( + shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium" +) +class TestLiveBubbles(HubCase): + drive = base.TestOrchestratorE2E.drive + beside = base.TestOrchestratorE2E.beside + + def setUp(self): + super().setUp() + OUT.mkdir(parents=True, exist_ok=True) + now = time.time() + claims = { + f"work/s{index}/claim.json": { + "office": f"work/s{index}", + "session": f"s{index}", + "runtime": "claude", + "name": name, + "since": iso(now - 7200), + "beat": now - 60, + "work": "x", + } + for index, name in enumerate(SENDERS[:2], start=2) + } + backlog = [ + { + "ts": now - 5, + "from": name, + "to": [], + "text": f"backlog from {name}", + "class": "broadcast", + "v": 2, + } + for name in SENDERS + ] + self.write_council({**claims, "news.jsonl": self.jsonl(backlog)}) + self.keys = {row["from"]: row["source"] for row in self.snapshot()["radio"]} + self.host_cpu(95.0, 0) + self.beside(lambda: self.host_cpu(95.0, 0)) + hub = self.httpd.hub + spoken = base.tone(1, 8000) + cues = { + "e2e-backlog": [ + ("post", "Stage", ["Docs"], "posted while the page was offline"), + ("broadcast", "Docs", [], "broadcast while the page was offline"), + ], + "e2e-live": [ + ("post", "Stage", ["Docs"], "rebase please, the gate is red"), + ("broadcast", "Docs", [], "docs are green for everyone"), + ("newsletter", "Build", [], "build cache trimmed"), + ], + "e2e-burst": [ + ("post", SENDERS[index % len(SENDERS)], ["Stage"], f"burst row {index}") + for index in range(30) + ], + "e2e-speech": [ + *( + ("post", SENDERS[at], ["Docs"], f"silent row {index}") + for index, at in enumerate((0, 2, 3, 4, 5) * 2) + ), + ("security", "Docs", [], "Disk almost full on the build host"), + ], + "e2e-alone": [ + ("security", "Docs", [], "Disk almost full on the backup host"), + ], + } + + def synthesize(body: bytes, admit: Callable[[str], None]) -> bytes: + word = json.loads(body)["text"] + if word in cues: + stamp = time.time() + rows = [ + { + "ts": stamp + index / 1000, + "from": sender, + "to": to, + "text": text, + "class": cls, + "v": 2, + } + for index, (cls, sender, to, text) in enumerate(cues[word]) + ] + with (self.council / "news.jsonl").open( + "a", encoding="utf-8" + ) as handle: + handle.write(self.jsonl(rows)) + else: + admit(word) + return spoken + + patch = mock.patch.object(hub.speech, "synthesize", synthesize) + patch.start() + self.addCleanup(patch.stop) + self.base = f"http://localhost:{self.port}/" + + def steps(self, browser: str) -> list[dict]: + probe = PROBE.replace("@HELPERS@", base.HELPERS).replace( + "@KEYS@", json.dumps(self.keys) + ) + return [ + { + "width": 1366, + "height": 768, + "url": f"{self.base}#stream", + "wait": 1200, + "probe": probe, + "shot": str(OUT / f"{browser}-1366-live-bubbles.png"), + "gesture": True, + } + ] + + def check(self, data: dict) -> None: + found = data["results"][0] + self.assertNotIn("error", found, found) + self.assertTrue(found["on"], found) + self.assertEqual( + (found["loaded"], found["offline"], found["resumed"]), (0, True, 0), found + ) + self.assertEqual( + found["classes"], {"Stage": True, "Docs": True, "Build": True}, found + ) + self.assertTrue( + found["burst"]["most"] <= CAP and found["burst"]["agents"] >= CAP, found + ) + speech, alone = found["speech"], found["alone"] + self.assertGreaterEqual(speech["quietBefore"], CAP, found) + self.assertEqual(alone["quietBefore"], 0, found) + self.assertLess(speech["delay"], LATE_MS, found) + self.assertLess(alone["delay"], LATE_MS, found) + self.assertLess(speech["delay"] - alone["delay"], SLACK_MS, found) + self.assertEqual(found["rejections"], [], found) + + def test_chromium_bubbles_every_live_row_but_no_backlog_caps_a_burst_and_never_delays_speech( + self, + ): + data = self.drive(self.steps("chromium"), None, init=GUARD) + (OUT / "chromium-live-bubbles.json").write_text( + json.dumps(data, indent=1), encoding="utf-8" + ) + self.assertEqual(data["errors"], []) + self.check(data) + + @unittest.skipUnless( + shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox" + ) + def test_firefox_bubbles_every_live_row_but_no_backlog_caps_a_burst_and_never_delays_speech( + self, + ): + data = self.drive( + self.steps("firefox"), + None, + "bidi.mjs", + os.environ.get("FIREFOX", "firefox"), + init=GUARD, + ) + (OUT / "firefox-live-bubbles.json").write_text( + json.dumps(data, indent=1), encoding="utf-8" + ) + self.assertEqual( + [error for error in data["errors"] if "/favicon.ico" not in error], [] + ) + self.check(data) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hub_bubbles.py b/tests/test_hub_bubbles.py index bbdfc0b..576f38d 100644 --- a/tests/test_hub_bubbles.py +++ b/tests/test_hub_bubbles.py @@ -13,6 +13,9 @@ from kvb_orchestrator import exchange, settings, view BUBBLES = HUB / "templates" / "bubbles.js" LAYER = HUB / "templates" / "page-bubbles.js" SPEECH = HUB / "templates" / "page-speech.js" +ALERTS = HUB / "templates" / "page-alerts.js" +CONSTANTS = HUB / "templates" / "page-constants.js" +VOICE = HUB / "templates" / "voice.js" STYLE = HUB / "templates" / "style-bubbles.css" FONT = HUB / "vendor" / "comic-neue" / "comic-neue-latin-400-normal.woff2" FONT_SHA256 = "b7753f30f8bd4925cf48a2ffe936c52ff1f16fe441f8c0245acf046296ac590f" @@ -21,7 +24,7 @@ BUBBLES_PROBE = """ const B = require(process.argv[1]); const item = (who, n) => ({who, n}); const [a1, b1, c1, a2, b2, d1] = [item("A", 1), item("B", 1), item("C", 1), item("A", 2), item("B", 2), item("D", 1)]; -const planOf = (head, pending) => B.plan(head, pending).map((entry) => [entry.who, entry.item.n, entry.scale]); +const planOf = (head, pending, quiet) => B.plan(head, pending, quiet).map((entry) => [entry.who, entry.item.n, entry.scale]); const speech = {classes: ["security", "post"]}; console.log(JSON.stringify({ scales: [0, 1, 2, 3, 4, 9].map(B.scale), @@ -31,6 +34,96 @@ console.log(JSON.stringify({ words: [B.words(" Disk almost\\nfull\\t now "), B.words(""), B.words(null)], plans: [planOf(a1, [b1, c1, a2, b2, d1]), planOf(null, [b1, a1, b2]), planOf(a1, []), planOf(null, [])], picked: [{class: "mayday"}, {class: "security"}, {class: "post"}, {class: "broadcast"}].map((row) => B.picked(row, speech)), + quiet: planOf(a1, [b1], [item("B", 9), item("E", 1), item("F", 1)]), + admitted: (() => { + const burst = Array.from({length: 30}, (_, index) => item("ABCDEF"[index % 6], index)); + const first = B.admit([], burst, new Set(["A"])); + const later = B.admit(first.start.slice(1), first.waiting, new Set()); + return {first: first.start.map((entry) => [entry.who, entry.n]), left: first.waiting.length, later: later.start.map((entry) => [entry.who, entry.n])}; + })(), + queued: (() => { + let waiting = []; + for (let n = 0; n < 35; n += 1) { + waiting = B.queued(waiting, item("A", n)); + } + return [waiting.length, waiting[0].n, waiting[waiting.length - 1].n]; + })(), +})); +""" +ROWS_PROBE = """ +const fs = require("fs"); +const vm = require("vm"); +const real = require(process.argv[1]); +const Voice = require(process.argv[2]); +const code = process.argv.slice(3).map((path) => fs.readFileSync(path, "utf8")).join("\\n"); +function page(fault) { + const out = {draws: 0, pumped: [], toasts: []}; + const sandbox = { + out, + Bubbles: { + ...real, + plan: () => [], + admit: (...args) => { + out.draws += 1; + if (fault && out.draws === 1) { + throw new Error("draw failed"); + } + return real.admit(...args); + }, + }, + Voice: {...Voice, beep: () => null}, + RadioSet: {plan: (row) => (["mayday", "security"].includes(row.class) ? [{kind: "burst"}] : []), ledgerOf: () => null}, + settings: {bubbles: true, notify: false, music: {}}, + radioSet: {mode: "auto", classes: ["mayday", "pan-pan", "security"], sounds: {}, squelch: 1}, + state: {now: 1000, radio: [], inbox: [], pending: [], messages: [], speech: {tts: false, gate: {open: false}}}, + known: {inbox: null, alarms: null, radio: null, batches: new Set(), now: null}, + speech: {pending: [], busy: false, current: null, loading: null, item: null, source: null, timer: 0, warned: 0}, + alarmRows: () => [], live: () => true, keyOf: (item) => item.id, short: (text) => text, dash: (text) => text, undoToast: () => null, + toast: (title, lines) => out.toasts.push(`${title}: ${lines.join(" ")}`), + document: {currentScript: null}, performance: {now: () => 0}, + window: {matchMedia: () => ({matches: false}), setTimeout: (run, ms) => (ms ? 0 : run()), clearTimeout: () => null, requestAnimationFrame: () => 0, cancelAnimationFrame: () => null}, + }; + vm.createContext(sandbox); + vm.runInContext(`${code} +pump = () => out.pumped.push(speech.pending.map((item) => item.cls)); +function quietLines() { + return [...bubbles.quiet, ...bubbles.waiting].map((item) => item.line); +}`, sandbox); + return sandbox; +} +const row = (line, cls, age) => ({ts: -age, from: line, source: line, to: [], text: line, class: cls}); +function batch(rows, fault) { + const at = page(fault); + at.state.now = 2000; + let thrown = null; + try { + at.speakRows(rows.map((entry) => ({...entry, ts: 2000 + entry.ts})), true); + } catch (error) { + thrown = String(error.message); + } + return {thrown, pumped: at.out.pumped, draws: at.out.draws, toasts: at.out.toasts, bubbles: at.quietLines().length}; +} +function gap() { + const at = page(false); + const poll = (now, rows, resumed) => { + at.state.now = now; + at.state.radio = [...at.state.radio, ...rows.map((entry) => ({...entry, ts: now + entry.ts}))]; + at.announce(resumed); + }; + poll(1000, [], false); + poll(1010, [row("live", "post", 1)], false); + poll(1041, [row("late", "post", 1)], false); + poll(1050, [row("back", "post", 1)], true); + poll(1060, [row("again", "post", 1)], false); + poll(1070, [row("fresh", "post", 29), row("stale", "post", 31)], false); + return at.quietLines(); +} +const posts = (count) => Array.from({length: count}, (_, index) => row(`post ${index % 6}`, "post", 2)); +console.log(JSON.stringify({ + fault: batch([row("post", "post", 2), row("Sec", "mayday", 1)], true), + burst: batch([...posts(1000), row("Sec", "mayday", 1)], false), + speech: batch([...posts(10), row("Docs", "security", 1)], false), + gap: gap(), })); """ SPEECH_PROBE = """ @@ -72,6 +165,89 @@ async function failed(bubbles, item, cancel) { })); })(); """ +STUCK_PROBE = """ +const fs = require("fs"); +const vm = require("vm"); +const Bubbles = require(process.argv[1]); +const code = fs.readFileSync(process.argv[2], "utf8"); +const rejected = []; +process.on("unhandledRejection", (error) => rejected.push(String(error && error.message))); +const settle = async () => { + for (let round = 0; round < 5; round += 1) { + await new Promise((resolve) => setImmediate(resolve)); + } +}; +const part = () => ({gain: {value: 0, setValueAtTime: () => null, exponentialRampToValueAtTime: () => null}, frequency: {}, Q: {}, connect: (next) => next}); +const audio = { + sampleRate: 8000, currentTime: 0, destination: {}, createBuffer: (channels, frames) => ({getChannelData: () => new Float32Array(frames)}), + createBiquadFilter: part, createGain: part, createBufferSource: () => ({...part(), addEventListener: () => null, start: () => null, stop: () => null}), +}; +async function run(rule, items) { + const speech = {pending: [], busy: false, current: null, loading: null, item: null, source: null, timer: 0, warned: 0}; + const timers = new Map(); + const heard = []; + let [fired, ducked, ids] = [false, false, 0]; + const breaks = { + reading: () => speech.busy && !speech.current && !speech.timer, + ended: () => speech.busy && !speech.current && !speech.source && speech.item !== null, + tail: () => speech.busy && speech.item === null, + }; + 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})}, + toast: () => null, context: () => audio, drawBubbles: () => null, performance: {now: () => 1000}, + duck: () => { + if (rule === "transmit" && !ducked) { + ducked = true; + throw new Error("no gain node"); + } + }, + drawSpeaker: () => { + if (speech.current && speech.current.who !== heard[heard.length - 1]) { + heard.push(speech.current.who); + } + if (!fired && breaks[rule] && breaks[rule]()) { + fired = true; + throw new Error("draw failed"); + } + }, + window: { + setTimeout: (next) => { + ids += 1; + timers.set(ids, next); + return ids; + }, + clearTimeout: (id) => timers.delete(id), + }, + }; + vm.runInNewContext(code, sandbox); + items.forEach((item) => sandbox.say(item)); + for (let round = 0; round < 12; round += 1) { + await settle(); + const due = [...timers.values()]; + timers.clear(); + for (const next of due) { + try { + next(); + } catch (error) { + rejected.push(String(error.message)); + } + } + } + await settle(); + return {faulted: fired || ducked, heard, busy: speech.busy, pending: speech.pending.map((item) => item.who)}; +} +(async () => { + const silent = (who) => ({who, line: "Disk almost full", words: ["Disk", "almost", "full"], silent: true}); + const sounded = (who) => ({who, line: "radio check", steps: [{kind: "burst"}], tone: "direct"}); + console.log(JSON.stringify({ + reading: await run("reading", [silent("A"), silent("B")]), + ended: await run("ended", [sounded("A"), sounded("B")]), + tail: await run("tail", [sounded("A"), sounded("B")]), + transmit: await run("transmit", [sounded("A"), sounded("B")]), + })); +})(); +""" STOP_PROBE = """ const fs = require("fs"); const vm = require("vm"); @@ -181,6 +357,84 @@ class TestBubbleSchedule(unittest.TestCase): def test_bubbles_follow_the_read_aloud_classes_and_mayday_always(self): self.assertEqual(self.data["picked"], [True, True, True, False]) + def test_a_silent_bubble_shows_beside_the_speech_queue_only_for_an_agent_the_queue_leaves_free( + self, + ): + self.assertEqual( + self.data["quiet"], + [["A", 1, 1], ["B", 1, 0.8], ["E", 1, 0.8], ["F", 1, 0.8]], + ) + + def test_a_burst_of_thirty_silent_bubbles_shows_at_most_three_at_once_one_per_agent_and_the_rest_wait_in_order( + self, + ): + self.assertEqual( + self.data["admitted"], + {"first": [["B", 1], ["C", 2], ["D", 3]], "left": 27, "later": [["A", 0]]}, + ) + + def test_at_most_thirty_silent_bubbles_wait_and_the_oldest_go_first(self): + self.assertEqual(self.data["queued"], [30, 5, 34]) + + +class TestRowsReachSpeechBeforeAnyDraw(unittest.TestCase): + @classmethod + def setUpClass(cls): + done = subprocess.run( + [ + "node", + "-e", + ROWS_PROBE, + str(BUBBLES), + str(VOICE), + str(CONSTANTS), + str(LAYER), + str(SPEECH), + str(ALERTS), + ], + capture_output=True, + text=True, + timeout=60, + check=True, + ) + cls.data = json.loads(done.stdout) + + def test_a_throwing_redraw_never_costs_the_mayday_of_the_same_batch_its_speech( + self, + ): + self.assertEqual( + self.data["fault"], + { + "thrown": None, + "pumped": [["mayday"]], + "draws": 1, + "toasts": ["Drawing speech bubbles failed: draw failed"], + "bubbles": 1, + }, + ) + + def test_a_batch_of_a_thousand_silent_rows_queues_the_mayday_and_draws_once( + self, + ): + self.assertEqual( + self.data["burst"], + { + "thrown": None, + "pumped": [["mayday"]], + "draws": 1, + "toasts": [], + "bubbles": 30, + }, + ) + + def test_silent_rows_never_enter_the_speech_queue(self): + self.assertEqual(self.data["speech"]["pumped"], [["security"]]) + + def test_no_silent_bubble_after_a_poll_gap_over_30_s_a_reconnect_or_for_a_row_older_than_30_s( + self, + ): + self.assertEqual(self.data["gap"], ["live", "again", "fresh"]) + class TestSpeechFallback(unittest.TestCase): def test_a_failed_tts_request_reads_the_row_at_reading_pace_only_while_bubbles_are_on(self): @@ -199,6 +453,35 @@ class TestSpeechFallback(unittest.TestCase): self.assertEqual(data["unread"], {"current": False, "busy": False, "timer": 0, "started": None, "duration": None, "shown": []}) +class TestQueueSurvivesAFault(unittest.TestCase): + @classmethod + def setUpClass(cls): + done = subprocess.run( + ["node", "-e", STUCK_PROBE, str(BUBBLES), str(SPEECH)], + capture_output=True, + text=True, + timeout=60, + check=True, + ) + cls.data = json.loads(done.stdout) + + def test_a_fault_while_a_row_sounds_or_is_read_still_hands_the_queue_to_the_next_row( + self, + ): + for case, heard in ( + ("reading", ["A", "B"]), + ("ended", ["A", "B"]), + ("tail", ["A", "B"]), + ("transmit", ["B"]), + ): + with self.subTest(case=case): + self.assertEqual( + self.data[case], + {"faulted": True, "heard": heard, "busy": False, "pending": []}, + self.data[case], + ) + + class TestBubbleStop(unittest.TestCase): @classmethod def setUpClass(cls):