Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (claude): gate voice-output candidacy on an explicit client signal
[._.]/ Adam Eivy5b47db106e2c
diff --git a/client/src/services/voiceClient.js b/client/src/services/voiceClient.jsindex 6616d43331..a2677ff4f0 100644--- a/client/src/services/voiceClient.js+++ b/client/src/services/voiceClient.js@@ -385,6 +385,15 @@ export const claimVoiceOutput = () => {if (socket.connected) socket.emit('voice:output:claim');};+// Announce this browser tab as a voice-output surface so the server registers+// it as a proactive-audio candidate. Sent unconditionally (even for a+// backgrounded, never-focused tab) so audio still has a home when no tab has+// claimed — but ONLY real browser tabs load this module, so non-playing sockets+// (e.g. a federated peer's Socket.IO relay) never announce and can't be elected.+const announceVoiceOutputAvailable = () => {+ if (socket.connected) socket.emit('voice:output:available');+};+// Subscribe to voice-output ownership changes. Fires immediately with the// current value so a late subscriber (widget mount) reflects state without// waiting for the next handoff. Returns an unsubscribe function.@@ -413,6 +422,9 @@ socket.on('voice:output:detached', () => {if (typeof window !== 'undefined' && typeof document !== 'undefined') {window.addEventListener('focus', claimVoiceOutput);subscribeVisibility((state) => { if (state === 'visible') claimVoiceOutput(); });+ // The socket may already be connected by the time this module loads; the+ // connect handler below re-announces on every (re)connect.+ announceVoiceOutputAvailable();}// voice:transcript marks the start of a new turn's outputs — any pending@@ -497,6 +509,9 @@ socket.on('connect', () => {if (lastDictationRequest.enabled) {socket.emit('voice:dictation:set', lastDictationRequest);}+ // Re-register as a voice-output candidate on every (re)connect — a fresh+ // socket has no server-side candidacy until this tab announces again.+ announceVoiceOutputAvailable();// On (re)connect, re-claim voice output if this tab is the one the user is// looking at — otherwise a reconnect could leave audio pointed at a stale// primary. Only a visible+focused tab claims so a backgrounded reconnectdiff --git a/server/services/voice/proactiveSpeech.test.js b/server/services/voice/proactiveSpeech.test.jsindex e9e43608bf..85c837d103 100644--- a/server/services/voice/proactiveSpeech.test.js+++ b/server/services/voice/proactiveSpeech.test.js@@ -180,11 +180,15 @@ describe('speakProactive', () => {expect(socketEmit).not.toHaveBeenCalled();});+ // The recipient socket may also receive a voice:output:primary notification+ // (lazy promotion), so filter to the voice:speak call specifically.+ const speakCalls = (socketEmit) => socketEmit.mock.calls.filter((c) => c[0] === 'voice:speak');+it('accepts text exactly at MAX_PROACTIVE_TEXT_LEN', async () => {const { io, socketEmit } = makeIo();const r = await speakProactive({ io, text: 'x'.repeat(MAX_PROACTIVE_TEXT_LEN) });expect(r.ok).toBe(true);- expect(socketEmit).toHaveBeenCalledTimes(1);+ expect(speakCalls(socketEmit)).toHaveLength(1);});it('routes voice:speak with audio to the recipient tab (never io.emit)', async () => {@@ -192,9 +196,9 @@ describe('speakProactive', () => {const r = await speakProactive({ io, text: 'Heads up — meeting in five.' });expect(r.ok).toBe(true);expect(emit).not.toHaveBeenCalled(); // no broadcast- expect(socketEmit).toHaveBeenCalledTimes(1);- const [event, payload] = socketEmit.mock.calls[0];- expect(event).toBe('voice:speak');+ const speaks = speakCalls(socketEmit);+ expect(speaks).toHaveLength(1);+ const [, payload] = speaks[0];expect(payload.sentence).toBe('Heads up — meeting in five.');expect(payload.wav).toBeInstanceOf(Buffer);expect(payload.priority).toBe('normal');diff --git a/server/services/voice/voiceOutput.js b/server/services/voice/voiceOutput.jsindex 13f193303a..474162536c 100644--- a/server/services/voice/voiceOutput.js+++ b/server/services/voice/voiceOutput.js@@ -88,13 +88,16 @@ export const releaseVoiceOutput = (socket) => {}};-// The current proactive-output recipient, or null when no tab is connected.-// Lazily promotes when there is no primary (or it has gone stale) so the very-// first proactive line after a connect still reaches exactly one tab even if no-// explicit claim has arrived yet.+// The current proactive-output recipient, or null when no candidate tab is+// registered. Lazily promotes when there is no primary (or it has gone stale) so+// the very first proactive line after a connect still reaches exactly one tab+// even if no explicit claim has arrived yet. Notifies the lazily-elected tab so+// its UI reflects that it is now the speaker (without this, a never-focused tab+// would play proactive audio while its indicator still read "muted").export const getVoiceOutputSocket = () => {if (isLive(primary) && candidates.has(primary)) return primary;primary = pruneAndPickLatest();+ if (primary) primary.emit('voice:output:primary', { primary: true });return primary;};diff --git a/server/services/voice/voiceOutput.test.js b/server/services/voice/voiceOutput.test.jsindex df530020cd..bdabc1f488 100644--- a/server/services/voice/voiceOutput.test.js+++ b/server/services/voice/voiceOutput.test.js@@ -42,6 +42,9 @@ describe('voiceOutput single-recipient routing', () => {expect(emittedEvents(a)).not.toContain('voice:output:primary');// But the sole candidate is the lazy recipient so audio has a home.expect(getVoiceOutputSocket()).toBe(a);+ // And lazy promotion notifies the elected tab so its UI reflects it's now+ // the speaker (it never claimed, so this is the only signal it gets).+ expect(emittedEvents(a)).toContain('voice:output:primary');});it('emitVoiceOutput routes to the single primary, not other candidates', () => {diff --git a/server/sockets/voice.js b/server/sockets/voice.jsindex 1f6e2e5728..ba168f3c29 100644--- a/server/sockets/voice.js+++ b/server/sockets/voice.js@@ -1,7 +1,7 @@// Per-socket voice handlers.// Inbound: voice:turn | voice:text | voice:interrupt | voice:reset// | voice:dictation:set | voice:ui:index | voice:screenshot:result-// | voice:ui:read-response | voice:output:claim+// | voice:ui:read-response | voice:output:available | voice:output:claim// Outbound: voice:transcript | voice:llm:delta | voice:llm:done | voice:tts:audio// | voice:tool | voice:dictation | voice:navigate// | voice:ui:click | voice:ui:fill | voice:ui:select | voice:ui:check@@ -93,12 +93,6 @@ export const registerVoiceHandlers = (socket) => {};registerEchoBuffer(state.recentTts);- // Register this tab as a possible recipient of proactive (server-initiated)- // voice output. It does NOT become the sole recipient until it claims output- // (below) — proactive audio is routed to exactly ONE tab so a reminder doesn't- // play on every open tab/machine at once. See services/voice/voiceOutput.js.- registerVoiceOutputCandidate(socket);-const pushHistory = (role, content) => {if (!content) return;state.history.push({ role, content });@@ -337,6 +331,17 @@ export const registerVoiceHandlers = (socket) => {}});+ // A real browser tab announces itself as a possible recipient of proactive+ // (server-initiated) voice output on connect. Only sockets that send this are+ // eligible — registering EVERY connection would make non-playing sockets+ // (e.g. a federated peer's Socket.IO relay client, which lands on the same+ // io.on('connection')) eligible, and electing one as primary would silently+ // route proactive audio into a socket that never plays it. It does NOT become+ // the sole recipient until it claims output (below). See voiceOutput.js.+ socket.on('voice:output:available', () => {+ registerVoiceOutputCandidate(socket);+ });+// The user's active tab claims proactive voice output — whichever tab is// focused/visible (or where the user explicitly clicked "speak here") becomes// the single recipient of `voice:speak`. Last claim wins; the previous holderdiff --git a/server/sockets/voice.test.js b/server/sockets/voice.test.jsindex 57b1a02fbf..b34b2f2aeb 100644--- a/server/sockets/voice.test.js+++ b/server/sockets/voice.test.js@@ -201,19 +201,39 @@ describe('voice:text validation', () => {describe('voice:output single-recipient wiring', () => {beforeEach(() => __resetVoiceOutput());- it('registers the voice:output:claim handler and a candidate on connect', () => {+ it('registers a candidate only after the tab announces voice:output:available', () => {const socket = makeFakeSocket();registerVoiceHandlers(socket);+ expect(socket.has('voice:output:available')).toBe(true);expect(socket.has('voice:output:claim')).toBe(true);- // Registered as the sole candidate → lazily eligible to receive output.+ // A bare connection is NOT a candidate — a non-browser socket (e.g. a peer+ // relay) that never announces must never be elected to receive audio.+ expect(getVoiceOutputSocket()).toBe(null);+ // After announcing, it becomes the (sole) lazily-eligible recipient.+ socket.fire('voice:output:available');expect(getVoiceOutputSocket()).toBe(socket);});+ it('a connection that never announces is never elected primary', () => {+ const relay = makeFakeSocket(); // stands in for a peer-relay Socket.IO client+ const tab = makeFakeSocket();+ registerVoiceHandlers(relay);+ registerVoiceHandlers(tab);+ tab.fire('voice:output:available');+ // Even though the relay connected, output routes to the real tab.+ const io = { emit: () => { throw new Error('must not broadcast'); } };+ emitVoiceOutput(io, 'voice:speak', { sentence: 'ping' });+ expect(relay.emitted.filter((e) => e.event === 'voice:speak')).toHaveLength(0);+ expect(tab.emitted.filter((e) => e.event === 'voice:speak')).toHaveLength(1);+ });+it('claiming routes proactive output to the claiming tab, not another', () => {const a = makeFakeSocket();const b = makeFakeSocket();registerVoiceHandlers(a);registerVoiceHandlers(b);+ a.fire('voice:output:available');+ b.fire('voice:output:available');// b is the active tab and claims output.b.fire('voice:output:claim');@@ -223,11 +243,13 @@ describe('voice:output single-recipient wiring', () => {expect(b.emitted.filter((e) => e.event === 'voice:speak')).toHaveLength(1);});- it('disconnecting the primary promotes another live tab', () => {+ it('disconnecting the primary promotes another announced tab', () => {const a = makeFakeSocket();const b = makeFakeSocket();registerVoiceHandlers(a);registerVoiceHandlers(b);+ a.fire('voice:output:available');+ b.fire('voice:output:available');a.fire('voice:output:claim');expect(getVoiceOutputSocket()).toBe(a);