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
fix(voice): distinguish session teardown from confirmed disconnect
Adam Eivyc5c7abd0a8bf
diff --git a/server/services/voice/callSession.js b/server/services/voice/callSession.jsindex 30bfd80a09..2661a48dd8 100644--- a/server/services/voice/callSession.js+++ b/server/services/voice/callSession.js@@ -49,6 +49,7 @@ const initialState = () => ({startedAt: null,lastVoiceAt: null,endedReason: null,+ physicalDisconnect: null,transcript: [],// 'user' — PortOS placed this call on request. 'mind' — the Persistent Mind// placed it (voice.call-user). 'inbound' — the user called PortOS and the@@ -136,6 +137,7 @@ const publicState = () => ({hostAttached: Boolean(host),startedAt: session.startedAt,endedReason: session.endedReason,+ physicalDisconnect: session.physicalDisconnect,turns: session.transcript.length,});@@ -261,7 +263,7 @@ export async function pollCall() {return publicState();}- if (observed?.state === 'ended' || observed?.state === 'idle') return endCall('remote-hangup');+ if (observed?.state === 'ended' || observed?.state === 'idle') return endCall('remote-hangup', { disconnectObserved: true });if (observed?.state === 'connected' && session.state === 'dialing') setState('listening');const now = deps.now();@@ -503,7 +505,7 @@ const MIND_TRANSCRIPT_MAX_CHARS = 6_000;* The journal append is best-effort: a failed write must not leave the session* stuck `connected`, because the state is what gates placing the next call.*/-export async function endCall(reason = 'ended') {+export async function endCall(reason = 'ended', { disconnectObserved = false } = {}) {if (session.state === 'idle') return publicState();stopPolling();const transcript = session.transcript;@@ -512,10 +514,23 @@ export async function endCall(reason = 'ended') {session.endedReason = reason;setState('ended');+ // Ending PortOS's audio session is independent of confirming FaceTime's+ // physical disconnect. Always finish cleanup, but retain that distinction.+ let physicalDisconnect = 'confirmed';try {- await deps.hangup();+ // A successful poll already observed idle/ended; pressing a vanished+ // hang-up control would manufacture a failure after a real disconnect.+ if (!disconnectObserved) await deps.hangup();} catch (error) {+ physicalDisconnect = error.code === 'state-unconfirmed' ? 'unconfirmed' : 'failed';console.error(`❌ voice call: hangup failed: ${error.message}`);+ await addNotification({+ type: NOTIFICATION_TYPES.AGENT_WARNING,+ title: 'Check FaceTime to confirm the call disconnected',+ description: 'The PortOS audio session ended, but FaceTime disconnect could not be confirmed. Check FaceTime and end the call there if it is still active.',+ priority: PRIORITY_LEVELS.HIGH,+ metadata: { source: 'voice-facetime-disconnect', outcome: physicalDisconnect },+ }).catch(notificationError => console.error(`❌ voice call: disconnect warning failed: ${notificationError.message}`));}if (transcript.length) {@@ -546,8 +561,8 @@ export async function endCall(reason = 'ended') {const verb = origin === 'mind' ? 'placed' : 'answered';try {await deps.enqueueMindMessage(- `Outcome of the FaceTime Audio call you ${verb} (ended: ${reason}):\n${- transcript.length ? renderTranscript(transcript) : 'The call ended with nothing said.'+ `Outcome of the FaceTime Audio call you ${verb} (PortOS session ended: ${reason}; FaceTime disconnect ${physicalDisconnect}):\n${+ transcript.length ? renderTranscript(transcript) : 'The PortOS session ended with nothing said.'}`.slice(0, MIND_TRANSCRIPT_MAX_CHARS),);} catch (error) {@@ -555,7 +570,7 @@ export async function endCall(reason = 'ended') {}}- session = { ...initialState(), endedReason: reason };+ session = { ...initialState(), endedReason: reason, physicalDisconnect };return emitState();}diff --git a/server/services/voice/callSession.test.js b/server/services/voice/callSession.test.jsindex 7f72055cd8..5fd5e1dfa1 100644--- a/server/services/voice/callSession.test.js+++ b/server/services/voice/callSession.test.js@@ -216,6 +216,54 @@ describe('FaceTime call session', () => {);});+ it.each(['state-unconfirmed', 'action-failed'])('ends the app session but preserves an uncertain physical disconnect (%s)', async code => {+ vi.useFakeTimers();+ attachHost(socket());+ await startCall({ origin: 'mind' });+ recordTurn('caller', 'Goodbye');+ hangup.mockRejectedValue(Object.assign(new Error('native action failed'), { code }));+ const outcome = code === 'state-unconfirmed' ? 'unconfirmed' : 'failed';+ const state = await endCall('user-hangup');+ expect(state).toMatchObject({ state: 'idle', active: false, turns: 0, physicalDisconnect: outcome });+ expect(getCallState()).toMatchObject({ physicalDisconnect: outcome });+ expect(appendJournal).toHaveBeenCalledTimes(1);+ expect(addNotification).toHaveBeenCalledWith(expect.objectContaining({+ title: 'Check FaceTime to confirm the call disconnected',+ metadata: { source: 'voice-facetime-disconnect', outcome },+ }));+ expect(enqueueMindMessage.mock.calls[0][0]).toContain(`FaceTime disconnect ${outcome}`);+ expect(enqueueMindMessage.mock.calls[0][0]).toContain('PortOS session ended');+ const calls = probe.mock.calls.length;+ await vi.advanceTimersByTimeAsync(PROBE_INTERVAL_MS * 2);+ expect(probe).toHaveBeenCalledTimes(calls);+ // Ending an already-ended session neither retries AX nor duplicates records.+ await endCall();+ expect(hangup).toHaveBeenCalledTimes(1);+ expect(appendJournal).toHaveBeenCalledTimes(1);+ hangup.mockResolvedValue({ state: 'ended' });+ await startCall();+ expect(getCallState().physicalDisconnect).toBeNull();+ expect(await endCall()).toMatchObject({ physicalDisconnect: 'confirmed', active: false });+ });++ it('uses an observed remote disconnect without pressing a vanished hangup control', async () => {+ attachHost(socket());+ await startCall({ origin: 'mind' });+ probe.mockResolvedValue({ state: 'idle' });+ expect(await pollCall()).toMatchObject({ active: false, physicalDisconnect: 'confirmed' });+ expect(hangup).not.toHaveBeenCalled();+ expect(addNotification).not.toHaveBeenCalled();+ expect(enqueueMindMessage.mock.calls[0][0]).toContain('FaceTime disconnect confirmed');+ });++ it('finishes teardown even when the disconnect warning cannot be persisted', async () => {+ attachHost(socket());+ await startCall();+ hangup.mockRejectedValue(Object.assign(new Error('unknown state'), { code: 'state-unconfirmed' }));+ addNotification.mockRejectedValueOnce(new Error('notification store unavailable'));+ expect(await endCall()).toMatchObject({ state: 'idle', active: false, physicalDisconnect: 'unconfirmed' });+ });+it('writes nothing for a call where no one said anything', async () => {attachHost(socket());await startCall();