The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): voice-live status fetch is scoped to profile and write
Maciej Lisiewski57c952ddcbe5
diff --git a/apps/desktop/src/store/voice-live.test.ts b/apps/desktop/src/store/voice-live.test.tsindex 1fb5ee66be2..02ecb3d9317 100644--- a/apps/desktop/src/store/voice-live.test.ts+++ b/apps/desktop/src/store/voice-live.test.ts@@ -1,24 +1,99 @@-import { describe, expect, it, vi } from 'vitest'+import { beforeEach, describe, expect, it, vi } from 'vitest'-const request = vi.fn(async (_profile: string, _method: string, _params?: Record<string, unknown>) => undefined)+import type { VoiceLiveStatus } from '@/lib/voice-live'++const { fetches, request, route } = vi.hoisted(() => ({+ fetches: [] as Array<{ resolve: (status: null | VoiceLiveStatus) => void }>,+ request: vi.fn(async (_profile: string, _method: string, _params?: Record<string, unknown>) => undefined),+ route: { connectionId: 'conn-a', profile: 'work' }+}))vi.mock('@/store/gateway', () => ({- activeGatewayProfileKey: () => 'work',+ activeGatewayConnectionId: () => route.connectionId,+ activeGatewayProfileKey: () => route.profile,requestGatewayForProfile: request}))vi.mock('@/lib/voice-live', () => ({- fetchVoiceLiveStatus: async () => null+ fetchVoiceLiveStatus: () =>+ new Promise<null | VoiceLiveStatus>(resolve => {+ fetches.push({ resolve })+ })}))-const { setVoiceChatMode } = await import('./voice-live')+const { $voiceLiveStatus, refreshVoiceLiveStatus, setVoiceChatMode } = await import('./voice-live')++function status(mode: VoiceLiveStatus['mode']): VoiceLiveStatus {+ return { available: true, mode, model: 'gpt-live-1', reason: null, voice: 'alloy' }+}++// Settle every outstanding fetch so no in-flight slot leaks into the next test.+async function drainFetches(): Promise<void> {+ while (fetches.length) {+ fetches.shift()?.resolve(null)+ await new Promise(resolve => setTimeout(resolve, 0))+ }+}++beforeEach(async () => {+ await drainFetches()+ route.connectionId = 'conn-a'+ route.profile = 'work'+ $voiceLiveStatus.set(null)+})describe('setVoiceChatMode', () => {it('routes the write through the viewed profile, not the bare active socket', async () => {- await setVoiceChatMode('gpt-live')+ const pending = setVoiceChatMode('gpt-live')++ await vi.waitFor(() => expect(fetches).toHaveLength(1))+ fetches[0].resolve(null)+ await pending// `config.set` is profile-scoped on the backend: an unscoped write on the// shared-primary route edits the LAUNCH profile's config.yaml (#125969 class).expect(request).toHaveBeenCalledWith('work', 'config.set', { key: 'voice.voice_chat_mode', value: 'gpt-live' })})++ it('re-reads the status after the write instead of joining a pre-write fetch', async () => {+ void refreshVoiceLiveStatus()+ const pending = setVoiceChatMode('gpt-live')++ // Let the mocked config.set settle so the post-write re-read is issued.+ await new Promise(resolve => setTimeout(resolve, 0))+ expect(fetches).toHaveLength(2)++ fetches[0].resolve(status('chained'))+ fetches[1].resolve(status('gpt-live'))++ expect((await pending)?.mode).toBe('gpt-live')+ expect($voiceLiveStatus.get()?.mode).toBe('gpt-live')+ })+})++describe('refreshVoiceLiveStatus', () => {+ it('does not let the previous profile publish its status after a switch', async () => {+ const previous = refreshVoiceLiveStatus()+ route.profile = 'home'+ const current = refreshVoiceLiveStatus()++ expect(fetches).toHaveLength(2)+ fetches[1].resolve(status('chained'))+ await current+ fetches[0].resolve(status('gpt-live'))+ await previous++ expect($voiceLiveStatus.get()?.mode).toBe('chained')+ })++ it('shares one fetch between callers on the same scope', async () => {+ const first = refreshVoiceLiveStatus()+ const second = refreshVoiceLiveStatus()++ expect(fetches).toHaveLength(1)+ fetches[0].resolve(status('gpt-live'))++ expect(await first).toEqual(await second)+ expect($voiceLiveStatus.get()?.mode).toBe('gpt-live')+ })})diff --git a/apps/desktop/src/store/voice-live.ts b/apps/desktop/src/store/voice-live.tsindex fc62c1ea7ca..d4792592ac1 100644--- a/apps/desktop/src/store/voice-live.ts+++ b/apps/desktop/src/store/voice-live.ts@@ -1,7 +1,8 @@+import { backendScopeKey } from '@hermes/shared'import { atom } from 'nanostores'import { fetchVoiceLiveStatus, type VoiceLiveStatus } from '@/lib/voice-live'-import { activeGatewayProfileKey, requestGatewayForProfile } from '@/store/gateway'+import { activeGatewayConnectionId, activeGatewayProfileKey, requestGatewayForProfile } from '@/store/gateway'/*** `voice.voice_chat_mode` as the backend resolves it, plus whether GPT-Live can@@ -11,24 +12,47 @@ import { activeGatewayProfileKey, requestGatewayForProfile } from '@/store/gatew*/export const $voiceLiveStatus = atom<null | VoiceLiveStatus>(null)-let inflight: null | Promise<null | VoiceLiveStatus> = null+// Bumped by every mode write: a fetch issued before the write reads the old+// mode, so it must neither be joined nor allowed to publish afterwards.+let writeEpoch = 0++let inflight: null | { key: string; promise: Promise<null | VoiceLiveStatus> } = null++// The single-flight slot is shared only within one (connection, profile, write)+// scope; a switch or a write strands the older fetch instead of joining it.+function voiceLiveScopeKey(): string {+ return `${backendScopeKey(activeGatewayConnectionId(), activeGatewayProfileKey())}::${writeEpoch}`+}export async function refreshVoiceLiveStatus(): Promise<null | VoiceLiveStatus> {- if (inflight) {- return inflight+ const key = voiceLiveScopeKey()++ if (inflight?.key === key) {+ return inflight.promise}- inflight = fetchVoiceLiveStatus()- .then(status => {- $voiceLiveStatus.set(status)+ const flight = {+ key,+ promise: fetchVoiceLiveStatus()+ .then(status => {+ // A late answer for a previous scope still resolves for its caller but+ // must not overwrite the status the current profile/connection reads.+ if (voiceLiveScopeKey() === key) {+ $voiceLiveStatus.set(status)+ }- return status- })- .finally(() => {- inflight = null- })+ return status+ })+ .finally(() => {+ if (inflight === flight) {+ inflight = null+ }+ })+ }++ inflight = flight- return inflight+ return flight.promise}/** Selected mode. `chained` until the backend answers, or when the backend predates the mode. */@@ -49,5 +73,7 @@ export function selectedVoiceChatMode(status: null | VoiceLiveStatus = $voiceLivexport async function setVoiceChatMode(mode: 'chained' | 'gpt-live'): Promise<null | VoiceLiveStatus> {await requestGatewayForProfile(activeGatewayProfileKey(), 'config.set', { key: 'voice.voice_chat_mode', value: mode })+ writeEpoch += 1+return refreshVoiceLiveStatus()}