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

fix([issue-12598]): ignore call-host audio unless a call is listening (#12718)

[._.]/050f56c58b47
diff --git a/server/routes/hostControlRunsVoice.test.js b/server/routes/hostControlRunsVoice.test.js
index b5ae806915..35b4d00fd3 100644
--- a/server/routes/hostControlRunsVoice.test.js
+++ b/server/routes/hostControlRunsVoice.test.js
@@ -54,7 +54,9 @@ const callHost = vi.hoisted(() => ({ socket: null }));
vi.mock('../services/voice/callSession.js', () => ({
attachHost: (socket) => { callHost.socket = socket; return { ok: true, state: {} }; },
getCallHost: () => callHost.socket, isCallActive: () => true,
- getCallState: () => ({}), getCallContext: () => null,
+ // Call-host frames are dropped unless the session is already in a turn.
+ // These cases exercise tool authority on that path, not the idle gate.
+ getCallState: () => ({ state: 'listening', active: true }), getCallContext: () => null,
peekCallOpeningLine: () => null, clearCallOpeningLine: vi.fn(),
detachHost: vi.fn(), endCall: vi.fn(), markListening: vi.fn(), markSpeaking: vi.fn(),
noteCallerSpeech: vi.fn(), recordTurn: vi.fn(), setCallStateListener: vi.fn(),
diff --git a/server/services/voice/callSession.js b/server/services/voice/callSession.js
index c61fb58815..9db1fc5f0c 100644
--- a/server/services/voice/callSession.js
+++ b/server/services/voice/callSession.js
@@ -266,10 +266,19 @@ export async function pollCall() {
// The probe can outlast the interval; the call may have ended meanwhile.
if (!publicState().active) return publicState();
if (observed?.state === 'ended' || observed?.state === 'idle') return endCall('remote-hangup', { disconnectObserved: true });
- if (observed?.state === 'connected' && session.state === 'dialing') setState('listening');
+ if (observed?.state === 'connected' && session.state === 'dialing') {
+ // Ringback is not the caller. Start the silence clock at pickup so a
+ // long ring neither expires the call while it is still dialing nor
+ // hangs up on the same tick the far end answers.
+ session.lastVoiceAt = deps.now();
+ setState('listening');
+ }
const now = deps.now();
- if (now - (session.lastVoiceAt ?? session.startedAt) >= SILENCE_HANGUP_MS) return endCall('caller-silent');
+ if (session.state !== 'dialing'
+ && now - (session.lastVoiceAt ?? session.startedAt) >= SILENCE_HANGUP_MS) {
+ return endCall('caller-silent');
+ }
if (now - session.startedAt >= await maxCallMs()) return endCall('max-duration');
return publicState();
}
@@ -500,8 +509,16 @@ export function noteCallerSpeech() {
return publicState();
}
-export const markSpeaking = () => (publicState().active ? setState('speaking') : publicState());
-export const markListening = () => (publicState().active ? setState('listening') : publicState());
+// `dialing` is active, but ringback is not a turn, so these must not promote
+// it. pollCall leaves dialing when the helper reports the far end connected
+// (or when the ring ends / the duration cap hits). `connected` stays eligible
+// so the opening line can still move the answered handoff into speaking.
+const inLiveTurn = () => session.state === 'connected'
+ || session.state === 'listening'
+ || session.state === 'speaking';
+
+export const markSpeaking = () => (inLiveTurn() ? setState('speaking') : publicState());
+export const markListening = () => (inLiveTurn() ? setState('listening') : publicState());
const renderTranscript = (transcript) => transcript
.map((turn) => `${turn.speaker === 'assistant' ? 'PortOS' : 'Caller'}: ${turn.text}`)
diff --git a/server/services/voice/callSession.test.js b/server/services/voice/callSession.test.js
index 9c066de8f7..2ef6aaf4a8 100644
--- a/server/services/voice/callSession.test.js
+++ b/server/services/voice/callSession.test.js
@@ -36,6 +36,7 @@ import {
endCall,
getCallContext,
getCallState,
+ markListening,
markSpeaking,
noteCallerSpeech,
pollCall,
@@ -114,6 +115,47 @@ describe('FaceTime call session', () => {
expect(getCallState()).toMatchObject({ state: 'listening', active: true });
});
+ it('does not let markSpeaking or markListening leave dialing', async () => {
+ // Ringback used to be promoted straight to speaking, so the opening line
+ // fired before anyone answered. Dialing now waits for the probe.
+ attachHost(socket());
+ probe.mockResolvedValue({ state: 'dialing' });
+ await startCall();
+
+ expect(markSpeaking().state).toBe('dialing');
+ expect(markListening().state).toBe('dialing');
+ await pollCall();
+ expect(getCallState().state).toBe('dialing');
+
+ probe.mockResolvedValue({ state: 'connected' });
+ await pollCall();
+ expect(getCallState().state).toBe('listening');
+ expect(markSpeaking().state).toBe('speaking');
+ expect(markListening().state).toBe('listening');
+ });
+
+ it('does not hang up a long ring as caller silence, and starts that clock at pickup', async () => {
+ attachHost(socket());
+ probe.mockResolvedValue({ state: 'dialing' });
+ await startCall({ openingLine: 'Hi' });
+
+ clock += SILENCE_HANGUP_MS + 1_000;
+ await pollCall();
+ expect(getCallState()).toMatchObject({ state: 'dialing', active: true });
+
+ probe.mockResolvedValue({ state: 'connected' });
+ await pollCall();
+ expect(getCallState().state).toBe('listening');
+
+ clock += SILENCE_HANGUP_MS - 1_000;
+ await pollCall();
+ expect(getCallState().state).toBe('listening');
+
+ clock += 2_000;
+ await pollCall();
+ expect(getCallState().endedReason).toBe('caller-silent');
+ });
+
it('refuses a second call while one is up', async () => {
attachHost(socket());
await startCall();
diff --git a/server/sockets/voice.js b/server/sockets/voice.js
index 078ce4c2d3..36f727163a 100644
--- a/server/sockets/voice.js
+++ b/server/sockets/voice.js
@@ -563,6 +563,15 @@ export const registerVoiceHandlers = (socket) => {
const view = toInt16Samples(pcm);
if (asCallHost) {
+ // The host page streams from attach until the tab closes, including
+ // while idle and while the far end is still ringing. Only a connected
+ // turn may endpoint into runTurn; drop everything else and reset so
+ // audio buffered before connect cannot finish as the first utterance.
+ const phase = getCallState().state;
+ if (phase !== 'listening' && phase !== 'speaking') {
+ call.endpointer = createCallEndpointer();
+ return;
+ }
const wasSpeaking = Boolean(call.endpointer.speaking);
const utterance = call.endpointer.push(view);
// Barge-in: caller speech onset cancels playback on the host socket
diff --git a/server/sockets/voice.test.js b/server/sockets/voice.test.js
index 06037a87c2..3099e2def4 100644
--- a/server/sockets/voice.test.js
+++ b/server/sockets/voice.test.js
@@ -677,6 +677,18 @@ describe('voice:capture socket handlers (meeting capture)', () => {
const socket = makeFakeSocket();
registerVoiceHandlers(socket);
await socket.fire('voice:call:attach');
+ // Idle and dialing frames never start a turn. Barge-in is a property of
+ // an in-flight reply, so the call has to be listening first.
+ __setCallSessionDeps({
+ probe: vi.fn(async () => ({ state: 'connected' })),
+ call: vi.fn(async () => ({ state: 'dialing' })),
+ hangup: vi.fn(async () => ({ state: 'ended' })),
+ appendJournal: vi.fn(async () => ({})),
+ enqueueMindMessage: vi.fn(async () => ({})),
+ });
+ await startCall();
+ await pollCall();
+ expect(getCallState().state).toBe('listening');
let capturedSignal;
let releaseFirstTurn;
@@ -711,6 +723,18 @@ describe('voice:capture socket handlers (meeting capture)', () => {
const socket = makeFakeSocket();
registerVoiceHandlers(socket);
await socket.fire('voice:call:attach');
+ // Idle frames are dropped, so the onset edge is only observable once the
+ // call is listening — the same connected phase the in-flight barge-in uses.
+ __setCallSessionDeps({
+ probe: vi.fn(async () => ({ state: 'connected' })),
+ call: vi.fn(async () => ({ state: 'dialing' })),
+ hangup: vi.fn(async () => ({ state: 'ended' })),
+ appendJournal: vi.fn(async () => ({})),
+ enqueueMindMessage: vi.fn(async () => ({})),
+ });
+ await startCall();
+ await pollCall();
+ expect(getCallState().state).toBe('listening');
runTurn.mockResolvedValueOnce({ text: 'Hello there' });
@@ -775,6 +799,17 @@ describe('voice:call:audio frame alignment', () => {
const socket = makeFakeSocket();
registerVoiceHandlers(socket);
await socket.fire('voice:call:attach');
+ // The handler drops frames unless the call is listening or speaking, so
+ // alignment has to be observed on a live call — an idle push never happens.
+ __setCallSessionDeps({
+ probe: vi.fn(async () => ({ state: 'connected' })),
+ call: vi.fn(async () => ({ state: 'dialing' })),
+ hangup: vi.fn(async () => ({ state: 'ended' })),
+ appendJournal: vi.fn(async () => ({})),
+ enqueueMindMessage: vi.fn(async () => ({})),
+ });
+ await startCall();
+ await pollCall();
return socket;
};
@@ -835,3 +870,93 @@ describe('voice:call:audio frame alignment', () => {
expect(Array.from(pushed[0])).toEqual(Array.from(sourceSamples()));
});
});
+
+describe('voice:call:audio does not run a turn unless the call is up', () => {
+ // Same frame contract as the capture tests: ~20 ms at 16 kHz, loud enough
+ // to clear RMS 0.015, and long enough over 14 frames to clear the 250 ms
+ // minimum. 36 silent frames clear the 700 ms trailing-silence endpoint.
+ const FRAME_SAMPLES = 320;
+ const loudFrame = () => {
+ const frame = new Int16Array(FRAME_SAMPLES);
+ for (let i = 0; i < FRAME_SAMPLES; i += 1) frame[i] = Math.round(Math.sin(i / 8) * 20000);
+ return frame;
+ };
+ const silentFrame = () => new Int16Array(FRAME_SAMPLES);
+ const flushMicrotasks = () => new Promise((resolve) => setImmediate(resolve));
+
+ const burst = (socket) => {
+ for (let i = 0; i < 14; i += 1) socket.fire('voice:call:audio', { pcm: loudFrame() });
+ for (let i = 0; i < 36; i += 1) socket.fire('voice:call:audio', { pcm: silentFrame() });
+ };
+
+ let probe;
+
+ beforeEach(() => {
+ __resetCallSession();
+ runTurn.mockClear();
+ probe = vi.fn(async () => ({ state: 'dialing' }));
+ __setCallSessionDeps({
+ probe,
+ call: vi.fn(async () => ({ state: 'dialing' })),
+ hangup: vi.fn(async () => ({ state: 'ended' })),
+ appendJournal: vi.fn(async () => ({})),
+ enqueueMindMessage: vi.fn(async () => ({})),
+ });
+ });
+
+ afterEach(() => __resetCallSession());
+
+ const attach = async () => {
+ const socket = makeFakeSocket();
+ registerVoiceHandlers(socket);
+ await socket.fire('voice:call:attach');
+ return socket;
+ };
+
+ it('never calls runTurn for speech while a host is attached and no call is up', async () => {
+ const socket = await attach();
+ expect(getCallState().state).toBe('idle');
+
+ burst(socket);
+ await flushMicrotasks();
+
+ expect(runTurn).not.toHaveBeenCalled();
+ expect(getCallState().state).toBe('idle');
+
+ // Audio buffered while idle must not complete once the far end connects.
+ await startCall();
+ probe.mockResolvedValue({ state: 'connected' });
+ await pollCall();
+ expect(getCallState().state).toBe('listening');
+ for (let i = 0; i < 36; i += 1) socket.fire('voice:call:audio', { pcm: silentFrame() });
+ await flushMicrotasks();
+ expect(runTurn).not.toHaveBeenCalled();
+ });
+
+ it('never calls runTurn while dialing, and stays dialing until the probe sees a connect', async () => {
+ const socket = await attach();
+ await startCall();
+ expect(getCallState().state).toBe('dialing');
+
+ burst(socket);
+ await flushMicrotasks();
+ await pollCall();
+
+ expect(runTurn).not.toHaveBeenCalled();
+ expect(getCallState().state).toBe('dialing');
+
+ probe.mockResolvedValue({ state: 'connected' });
+ await pollCall();
+ expect(getCallState().state).toBe('listening');
+
+ // Silence alone must not finish speech that arrived while the line was ringing.
+ for (let i = 0; i < 36; i += 1) socket.fire('voice:call:audio', { pcm: silentFrame() });
+ await flushMicrotasks();
+ expect(runTurn).not.toHaveBeenCalled();
+
+ burst(socket);
+ await flushMicrotasks();
+ expect(runTurn).toHaveBeenCalledTimes(1);
+ expect(getCallState().state).toBe('listening');
+ });
+});