Skip to content

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

address review (claude): keep focus recency while voice is disabled

[._.]/ Adam Eivyf542b696c844
diff --git a/server/services/voice/voiceOutput.js b/server/services/voice/voiceOutput.js
index 5082465643..dc9ecb658e 100644
--- a/server/services/voice/voiceOutput.js
+++ b/server/services/voice/voiceOutput.js
@@ -76,12 +76,19 @@ export const registerVoiceOutputCandidate = (socket) => {
// calls this (via the `voice:output:claim` socket event) on focus / visibility
// / explicit request. The previous primary is told it lost the binding so its
// UI can reflect the change; the new primary is told it now owns output.
-export const claimVoiceOutput = (socket) => {
- if (!socket) return null;
+// Stamp claim recency so a later election or promotion picks the
+// most-recently-focused tab (not merely the newest connection), without making
+// it primary. Used directly while voice is disabled so focus history survives
+// until voice is enabled.
+export const noteVoiceOutputFocus = (socket) => {
+ if (!socket) return;
candidates.add(socket);
- // Stamp claim recency so a later disconnect promotes the most-recently-focused
- // survivor (not merely the newest connection). Re-stamped on every claim.
claimSeq.set(socket, ++claimCounter);
+};
+
+export const claimVoiceOutput = (socket) => {
+ if (!socket) return null;
+ noteVoiceOutputFocus(socket);
if (primary === socket) return socket;
const prev = primary;
primary = socket;
diff --git a/server/sockets/voice.js b/server/sockets/voice.js
index 719aae564c..f37fee34f4 100644
--- a/server/sockets/voice.js
+++ b/server/sockets/voice.js
@@ -24,6 +24,7 @@ import { registerEchoBuffer, unregisterEchoBuffer } from '../services/voice/echo
import {
registerVoiceOutputCandidate,
claimVoiceOutput,
+ noteVoiceOutputFocus,
releaseVoiceOutput,
} from '../services/voice/voiceOutput.js';
import { isIsoDate } from '../services/brainJournal.js';
@@ -416,15 +417,20 @@ export const registerVoiceHandlers = (socket) => {
// focused/visible (or where the user explicitly clicked "speak here") becomes
// the single recipient of `voice:speak`. Last claim wins; the previous holder
// is notified via voice:output:detached so its UI can reflect the handoff.
- // Ignored while voice is disabled: every tab claims on each focus/visibility
- // change, and proactive speech is refused when disabled anyway. Candidates
- // stay registered, so enabling voice later still lazily elects a recipient.
+ // While voice is disabled a claim only records focus recency: every tab
+ // claims on each focus/visibility change and proactive speech is refused
+ // anyway, so electing (and logging) a primary would be noise. The recency
+ // stamp lets the first election after enabling voice pick the focused tab.
// Re-check the connection after the await: a tab that disconnected mid-read
- // was already released and must not be re-added as primary.
+ // was already released and must not be re-added.
socket.on('voice:output:claim', async () => {
try {
const cfg = await getVoiceConfig();
- if (!cfg.enabled || socket.connected === false) return;
+ if (socket.connected === false) return;
+ if (!cfg.enabled) {
+ noteVoiceOutputFocus(socket);
+ return;
+ }
claimVoiceOutput(socket);
} catch (err) {
console.error(`❌ voice: output claim failed: ${err.message}`);
diff --git a/server/sockets/voice.test.js b/server/sockets/voice.test.js
index c60ad8ec06..40a0885a08 100644
--- a/server/sockets/voice.test.js
+++ b/server/sockets/voice.test.js
@@ -335,14 +335,44 @@ describe('voice:output single-recipient wiring', () => {
expect(getVoiceOutputSocket()).toBe(b);
});
- it('ignores output claims while voice is disabled', async () => {
- getVoiceConfig.mockResolvedValueOnce({ enabled: false });
+ it('does not elect a primary while voice is disabled, but remembers focus for later', async () => {
+ getVoiceConfig.mockResolvedValue({ enabled: false });
+ const focused = makeFakeSocket();
+ const background = makeFakeSocket();
+ registerVoiceHandlers(focused);
+ registerVoiceHandlers(background);
+ focused.fire('voice:output:available');
+ background.fire('voice:output:available');
+ focused.fire('voice:output:claim');
+ await flush();
+ expect(focused.emitted.filter((e) => e.event === 'voice:output:primary')).toHaveLength(0);
+
+ // Voice enabled with no further focus change: the first election picks the
+ // tab the user focused, not the later-connected background tab.
+ getVoiceConfig.mockResolvedValue({ enabled: true });
+ expect(getVoiceOutputSocket()).toBe(focused);
+ });
+
+ it('drops a claim from a tab that disconnected while the config was read', async () => {
+ const a = makeFakeSocket();
+ registerVoiceHandlers(a);
+ a.fire('voice:output:claim');
+ a.connected = false;
+ await flush();
+ expect(a.emitted.filter((e) => e.event === 'voice:output:primary')).toHaveLength(0);
+ expect(getVoiceOutputSocket()).toBe(null);
+ });
+
+ it('survives a config read failure without electing a primary', async () => {
+ getVoiceConfig.mockRejectedValueOnce(new Error('settings unreadable'));
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const a = makeFakeSocket();
registerVoiceHandlers(a);
- a.fire('voice:output:available');
a.fire('voice:output:claim');
await flush();
expect(a.emitted.filter((e) => e.event === 'voice:output:primary')).toHaveLength(0);
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('settings unreadable'));
+ errSpy.mockRestore();
});
});