fix(orchestrator): queue every speech row of a poll before one redraw
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
a2a03995cb
commit
d882e501c1
10 files changed
+732
-31
No files matched your search
@@ -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<object>} pending the rows of the speech queue, in order.
|
||||
* @param {Array<object>} [quiet] the silent bubbles on screen, rows that are not read aloud.
|
||||
* @returns {Array<object>} 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<object>} showing the silent bubbles on screen now.
|
||||
* @param {Array<object>} waiting the silent bubbles that wait, oldest first.
|
||||
* @param {Set<string>} 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<object>} waiting the silent bubbles that wait, oldest first.
|
||||
* @param {object} item the silent bubble of a row that arrived live.
|
||||
* @returns {Array<object>} 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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<object>} 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();
|
||||
}
|
||||
|
||||
@@ -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"]];
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<object>} 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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user