Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(voice): clean abandoned profiles and hydrate first mobile recording
DavidShenf3fbcca9c212
diff --git a/apps/desktop/src/main/voice-input/speaker/__tests__/speakerProfileStore.test.ts b/apps/desktop/src/main/voice-input/speaker/__tests__/speakerProfileStore.test.tsindex 44b55b4403..1785e0f87a 100644--- a/apps/desktop/src/main/voice-input/speaker/__tests__/speakerProfileStore.test.ts+++ b/apps/desktop/src/main/voice-input/speaker/__tests__/speakerProfileStore.test.ts@@ -1,4 +1,4 @@-import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import fs from 'node:fs';import os from 'node:os';import path from 'node:path';@@ -18,6 +18,7 @@ beforeEach(() => {});afterEach(() => {+ vi.restoreAllMocks();fs.rmSync(dir, { recursive: true, force: true });});@@ -94,4 +95,77 @@ describe('SpeakerProfileStore', () => {expect(fs.existsSync(file)).toBe(false);expect(store.read()).toBeNull();});++ it.each(['write', 'rename'] as const)('removes the temporary voiceprint after a failed %s and keeps the committed profile', failure => {+ const file = path.join(dir, 'profile.json');+ const store = new SpeakerProfileStore(() => file, fakeCipher());+ store.write(Float32Array.of(1, 0));+ const previous = fs.readFileSync(file, 'utf8');+ const originalWrite = fs.writeFileSync;+ if (failure === 'write') {+ vi.spyOn(fs, 'writeFileSync').mockImplementationOnce((target, data, options) => {+ originalWrite(target, data, options);+ throw new Error('disk error');+ });+ } else {+ vi.spyOn(fs, 'renameSync').mockImplementationOnce(() => { throw new Error('disk error'); });+ }+ expect(() => store.write(Float32Array.of(0, 1))).toThrow('disk error');+ expect(fs.existsSync(`${file}.${process.pid}.tmp`)).toBe(false);+ expect(fs.readFileSync(file, 'utf8')).toBe(previous);+ expect(Array.from(store.read()!.embedding)).toEqual([1, 0]);+ });++ it('reclaims crashed writers on read without touching live writers or unrelated files', () => {+ const file = path.join(dir, 'profile.json');+ const deadPid = process.pid + 1;+ const old = `${file}.${deadPid}.tmp`;+ const live = `${file}.${process.pid + 2}.tmp`;+ const unrelated = path.join(dir, 'other-profile.json.123.tmp');+ for (const target of [old, live, unrelated, `${file}.notes.tmp`]) fs.writeFileSync(target, 'fixture');+ vi.spyOn(process, 'kill').mockImplementation(pid => {+ if (pid === deadPid) throw Object.assign(new Error('no process'), { code: 'ESRCH' });+ return true;+ });+ expect(new SpeakerProfileStore(() => file, fakeCipher()).read()).toBeNull();+ expect(fs.existsSync(old)).toBe(false);+ expect(fs.existsSync(live)).toBe(true);+ expect(fs.existsSync(unrelated)).toBe(true);+ expect(fs.existsSync(`${file}.notes.tmp`)).toBe(true);+ });++ it('explicit deletion removes all matching temporary files even without a committed profile', () => {+ const file = path.join(dir, 'profile.json');+ const otherAccount = path.join(dir, 'other-account');+ fs.mkdirSync(otherAccount);+ const otherFile = path.join(otherAccount, 'profile.json.123.tmp');+ fs.writeFileSync(otherFile, 'other account');+ for (const pid of [123, 456, process.pid]) fs.writeFileSync(`${file}.${pid}.tmp`, 'ciphertext fixture');+ new SpeakerProfileStore(() => file, fakeCipher()).delete();+ expect(fs.readdirSync(dir)).toEqual(['other-account']);+ expect(fs.readFileSync(otherFile, 'utf8')).toBe('other account');+ });++ it('attempts every deletion, reports a locked temporary file and allows retry', () => {+ const file = path.join(dir, 'profile.json');+ const store = new SpeakerProfileStore(() => file, fakeCipher());+ store.write(Float32Array.of(1, 0));+ const locked = `${file}.123.tmp`;+ const other = `${file}.456.tmp`;+ fs.writeFileSync(locked, 'ciphertext fixture');+ fs.writeFileSync(other, 'ciphertext fixture');+ const originalRm = fs.rmSync;+ const spy = vi.spyOn(fs, 'rmSync').mockImplementation((target, options) => {+ if (target === locked) throw new Error('file locked');+ originalRm(target, options);+ });+ expect(() => store.delete()).toThrow('Speaker profile cleanup failed');+ expect(fs.existsSync(file)).toBe(false);+ expect(fs.existsSync(other)).toBe(false);+ expect(fs.existsSync(locked)).toBe(true);+ spy.mockRestore();+ store.delete();+ expect(fs.existsSync(locked)).toBe(false);+ expect(store.read()).toBeNull();+ });});diff --git a/apps/desktop/src/main/voice-input/speaker/speakerProfileStore.ts b/apps/desktop/src/main/voice-input/speaker/speakerProfileStore.tsindex 03623d9180..e25bcd4463 100644--- a/apps/desktop/src/main/voice-input/speaker/speakerProfileStore.ts+++ b/apps/desktop/src/main/voice-input/speaker/speakerProfileStore.ts@@ -49,6 +49,9 @@ export class SpeakerProfileStore {read(): SpeakerProfile | null {const file = this.filePath();if (this.cache?.file === file) return this.cache.profile;+ // Recover abandoned atomic writes after a crash. A busy/locked file can be+ // retried later; it must not make the committed profile unreadable.+ try { this.removeTemporaryFiles(file, true); } catch { /* Retried on delete/write. */ }const profile = this.load(file);// A failed read (e.g. keychain still locked) is retried next time.this.cache = profile ? { file, profile } : null;@@ -69,9 +72,16 @@ export class SpeakerProfileStore {};const file = this.filePath();fs.mkdirSync(path.dirname(file), { recursive: true });+ try { this.removeTemporaryFiles(file, true); } catch { /* Explicit delete reports failures. */ }const tmp = `${file}.${process.pid}.tmp`;- fs.writeFileSync(tmp, JSON.stringify(stored), { mode: 0o600 });- fs.renameSync(tmp, file);+ try {+ fs.writeFileSync(tmp, JSON.stringify(stored), { mode: 0o600 });+ fs.renameSync(tmp, file);+ } finally {+ // Includes a partial write or failed rename; preserve the original I/O+ // error if cleanup also fails. Startup/delete retry any remaining file.+ try { fs.rmSync(tmp, { force: true }); } catch { /* Retried on read/write/delete. */ }+ }const profile = { embedding: Float32Array.from(embedding), createdAt };this.cache = { file, profile };return profile;@@ -79,8 +89,40 @@ export class SpeakerProfileStore {delete(): void {const file = this.filePath();- fs.rmSync(file, { force: true });this.cache = null;+ const errors: unknown[] = [];+ try { fs.rmSync(file, { force: true }); } catch (error) { errors.push(error); }+ try { this.removeTemporaryFiles(file, false); } catch (error) { errors.push(error); }+ if (errors.length) throw new AggregateError(errors, 'Speaker profile cleanup failed');+ }++ private removeTemporaryFiles(file: string, staleOnly: boolean): void {+ const dir = path.dirname(file);+ const prefix = `${path.basename(file)}.`;+ let names: string[];+ try {+ names = fs.readdirSync(dir);+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') return;+ throw error;+ }+ const errors: unknown[] = [];+ for (const name of names) {+ if (!name.startsWith(prefix) || !name.endsWith('.tmp')) continue;+ const pidText = name.slice(prefix.length, -4);+ if (!/^[1-9]\d*$/.test(pidText)) continue;+ const pid = Number(pidText);+ if (staleOnly && pid !== process.pid) {+ try {+ process.kill(pid, 0);+ continue; // Another instance may be in the middle of its atomic write.+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== 'ESRCH') continue;+ }+ }+ try { fs.rmSync(path.join(dir, name), { force: true }); } catch (error) { errors.push(error); }+ }+ if (errors.length) throw new AggregateError(errors, 'Speaker profile temporary cleanup failed');}private load(file: string): SpeakerProfile | null {diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsxindex 1556126048..5f99476bfd 100644--- a/apps/mobile/app/sessions/[sessionId].tsx+++ b/apps/mobile/app/sessions/[sessionId].tsx@@ -5501,6 +5501,7 @@ function SessionScreenContent() {credential.settings?.language,CINDY_MANAGED_REFINER_PROVIDER,);+ const audioGate = await createMobileSpeakerGate();if (voiceStartupSeqRef.current !== startupSeq) {// This run never opened the microphone; release only its claimed ASR.void prewarmedVoice?.asr.stop().catch(() => undefined);@@ -5515,7 +5516,7 @@ function SessionScreenContent() {const controller = createMobileVoiceControllerSession({credential,// "Recognize my voice": undefined unless enabled, enrolled and the models are on disk.- audioGate: createMobileSpeakerGate(),+ audioGate,...(prewarmedVoice ? { asr: prewarmedVoice.asr } : {}),connectionProvider: (providerId: string) => voiceContext.createAsrConnection(providerId),refinerTargetProvider: (providerId: string, options?: { refreshAccessToken?: boolean }) =>diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsxindex fce7dfed2d..ec19ed5f79 100644--- a/apps/mobile/app/sessions/new.tsx+++ b/apps/mobile/app/sessions/new.tsx@@ -3861,6 +3861,7 @@ export default function NewRemoteSessionScreen() {credential.settings?.language,CINDY_MANAGED_REFINER_PROVIDER,);+ const audioGate = await createMobileSpeakerGate();if (voiceStartupSeqRef.current !== startupSeq) {// This run never opened the microphone; release only its claimed ASR.void prewarmedVoice?.asr.stop().catch(() => undefined);@@ -3874,7 +3875,7 @@ export default function NewRemoteSessionScreen() {const controller = createMobileVoiceControllerSession({credential,// "Recognize my voice": undefined unless enabled, enrolled and the models are on disk.- audioGate: createMobileSpeakerGate(),+ audioGate,...(prewarmedVoice ? { asr: prewarmedVoice.asr } : {}),connectionProvider: (providerId: string) => voiceContext.createAsrConnection(providerId),refinerTargetProvider: (providerId: string, options?: { refreshAccessToken?: boolean }) =>diff --git a/apps/mobile/src/__tests__/composerSpeakerGateStartup.test.ts b/apps/mobile/src/__tests__/composerSpeakerGateStartup.test.tsnew file mode 100644index 0000000000..58bd54e8e5--- /dev/null+++ b/apps/mobile/src/__tests__/composerSpeakerGateStartup.test.ts@@ -0,0 +1,100 @@+import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';+import ts from 'typescript';+import { describe, expect, it, vi } from 'vitest';++// Execute each production start callback, with the native/network boundaries+// mocked. In particular, an already-prewarmed ASR must not hide a slow keychain.+function readStart(page: string) {+ const source = ts.createSourceFile(page, readFileSync(resolve(process.cwd(), 'app/sessions', page), 'utf8'),+ ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);+ let callback = '';+ function visit(node: ts.Node) {+ if (ts.isVariableDeclaration(node) && node.name.getText(source) === 'startVoiceRecording'+ && node.initializer && ts.isCallExpression(node.initializer)) {+ callback = node.initializer.arguments[0].getText(source);+ }+ ts.forEachChild(node, visit);+ }+ visit(source);+ if (!callback) throw new Error(`Missing ${page} start callback`);+ const compiled = ts.transpileModule(`return (${callback});`,+ { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;+ return (bindings: Record<string, unknown>) =>+ new Function(...Object.keys(bindings), compiled)(...Object.values(bindings)) as () => Promise<void>;+}++describe.each(['new.tsx', '[sessionId].tsx'])('%s speaker gate startup', page => {+ const start = readStart(page);+ function setup() {+ let finishGate!: (gate: object | undefined) => void;+ const pendingGate = new Promise<object | undefined>(resolve => { finishGate = resolve; });+ const createGate = vi.fn(() => pendingGate);+ const controller = { start: vi.fn(async () => {}), cancel: vi.fn(async () => {}) };+ const createController = vi.fn((_options: Record<string, unknown>) => controller);+ const prewarmed = { credential: { settings: {} }, voiceContext: {}, asr: { stop: vi.fn(async () => {}) } };+ const startupSeq = { current: 0 };+ const setVoiceError = vi.fn();+ const bindings: Record<string, unknown> = {+ voiceState: 'idle', voiceIsProcessing: false,+ voicePermissionRequestInFlightRef: { current: false },+ voicePermissionRequestSeqRef: { current: 0 }, voicePermissionRequestAbortRef: { current: null },+ voiceStartupInFlightRef: { current: false }, voiceStartupSeqRef: startupSeq,+ voiceStopInFlightRef: { current: false }, voiceRecordingActiveRef: { current: false },+ voiceStopAfterStartRef: { current: false }, voiceLongPressActiveRef: { current: false },+ voiceControllerSessionRef: { current: null }, finishVoiceRecordingRef: { current: null },+ voiceStopGestureSelectionGuardRef: { current: false }, voiceSelectionUserOwnedRef: { current: false },+ voicePendingSelectionEchoesRef: { current: [] },+ deviceId: 'fixture-host', selectedDeviceId: 'fixture-host',+ setVoiceError, setVoiceState: vi.fn(), setVoiceReleaseToSendActive: vi.fn(),+ isMobileRealtimeAudioAvailable: () => true,+ AppState: { currentState: 'active' },+ getRecordingPermissionsAsync: vi.fn(), requestRecordingPermissionsAsync: vi.fn(),+ resolveMobileVoiceRecordingPermission: async () => 'granted',+ openLink: async () => {}, refreshMobileVoiceDictionary: async () => {},+ takePrewarmedMobileVoiceAsr: () => Promise.resolve(prewarmed),+ getMobileVoiceInputHistoryForHost: async () => [], hydrateMobileVoiceDictionary: async () => {},+ createMobileSpeakerGate: createGate, createMobileVoiceControllerSession: createController,+ draftRef: { current: '' }, composerDocumentRef: { current: {} }, composerInputRef: { current: null },+ firstMessageRef: { current: '' }, firstMessageSelectionRef: { current: { start: 0, end: 0 } },+ takeRefinementContextTail: (value: string) => value,+ buildMobileVoiceSessionRefinementContext: () => ({}), renderItems: [],+ formatRemoteError: String,+ };+ return { run: start(bindings), createGate, createController, controller, startupSeq, finishGate, prewarmed, setVoiceError };+ }++ it('waits for hydration before constructing and starting the first controller', async () => {+ const test = setup();+ const pending = test.run();+ await vi.waitFor(() => expect(test.createGate).toHaveBeenCalledOnce());+ expect(test.createController).not.toHaveBeenCalled();+ const gate = { fixture: 'saved speaker gate' };+ test.finishGate(gate);+ await pending;+ expect(test.createController).toHaveBeenCalledWith(expect.objectContaining({ audioGate: gate }));+ expect(test.controller.start).toHaveBeenCalledOnce();+ expect(test.setVoiceError).toHaveBeenCalledExactlyOnceWith(null);+ });++ it('releases the claimed ASR without opening the mic if cancelled during hydration', async () => {+ const test = setup();+ const pending = test.run();+ await vi.waitFor(() => expect(test.createGate).toHaveBeenCalledOnce());+ test.startupSeq.current += 1; // Page teardown/background cancels this startup.+ test.finishGate({});+ await pending;+ expect(test.createController).not.toHaveBeenCalled();+ expect(test.prewarmed.asr.stop).toHaveBeenCalledOnce();+ expect(test.setVoiceError).toHaveBeenCalledExactlyOnceWith(null);+ });++ it('still records when the gate is disabled or unavailable', async () => {+ const test = setup();+ test.finishGate(undefined);+ await test.run();+ expect(test.createController).toHaveBeenCalledWith(expect.objectContaining({ audioGate: undefined }));+ expect(test.controller.start).toHaveBeenCalledOnce();+ expect(test.setVoiceError).toHaveBeenCalledExactlyOnceWith(null);+ });+});diff --git a/apps/mobile/src/__tests__/mobileSpeakerFilter.test.ts b/apps/mobile/src/__tests__/mobileSpeakerFilter.test.tsindex 08ef0d1659..5e4aee625a 100644--- a/apps/mobile/src/__tests__/mobileSpeakerFilter.test.ts+++ b/apps/mobile/src/__tests__/mobileSpeakerFilter.test.ts@@ -1,4 +1,5 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { accountVaultKey } from '@cindy/auth-client';import { buildSpeakerEnrollment, SPEAKER_MODEL_ASSETS, SpeakerGate, speakerModelAsset, speakerModelLocalName } from '@cindy/voice-input-core';import AsyncStorage from '@react-native-async-storage/async-storage';import * as FileSystem from 'expo-file-system/legacy';@@ -225,7 +226,7 @@ describe('mobile speaker filter', () => {}service.warmMobileSpeakerFilter();await vi.waitFor(() => expect(loadSpeakerModels).toHaveBeenCalledTimes(2));- expect(service.createMobileSpeakerGate()).toBeInstanceOf(SpeakerGate);+ expect(await service.createMobileSpeakerGate()).toBeInstanceOf(SpeakerGate);} finally {unsubscribe();}@@ -322,7 +323,7 @@ describe('mobile speaker filter', () => {await signIn('user-a');const state = service.getMobileSpeakerFilterSnapshot();expect(state).toMatchObject({ hydrated: true, enabled: false, strictness: 'standard', enrolled: false });- expect(service.createMobileSpeakerGate()).toBeUndefined();+ expect(await service.createMobileSpeakerGate()).toBeUndefined();});it('builds a gate only when on, enrolled and the models are on disk', async () => {@@ -331,10 +332,69 @@ describe('mobile speaker filter', () => {const { getMobileAuthOwner } = await import('@/auth/authOwnerGeneration');storeProfile(getMobileAuthOwner().accountKey);await service.hydrateMobileSpeakerFilter();- expect(service.createMobileSpeakerGate()).toBeUndefined();+ expect(await service.createMobileSpeakerGate()).toBeUndefined();await service.setMobileSpeakerFilterEnabled(true);- expect(service.createMobileSpeakerGate()).toBeInstanceOf(SpeakerGate);+ expect(await service.createMobileSpeakerGate()).toBeInstanceOf(SpeakerGate);+ });++ it.each(['cold start', 'account switch'] as const)('waits for saved state on the first recording after %s', async scenario => {+ installModels();+ if (scenario === 'account switch') await signIn('user-b');+ const key = accountVaultKey('global', 'user-a');+ storeProfile(key);+ asyncStore.set(service.__testing.prefsKey(key), JSON.stringify({ version: 1, enabled: true }));+ const read = vi.mocked(getSecureItem).getMockImplementation()!;+ let release!: () => void;+ vi.mocked(getSecureItem).mockImplementationOnce(async name => {+ await new Promise<void>(resolve => { release = resolve; });+ return read(name);+ });+ setMobileAuthOwner('user-a', 'global');+ const gate = service.createMobileSpeakerGate();+ let completed = false;+ void gate.then(() => { completed = true; });+ await vi.waitFor(() => expect(release).toBeTypeOf('function'));+ expect(completed).toBe(false);+ expect(service.getMobileSpeakerFilterSnapshot().enabled).toBe(false);+ release();+ expect(await gate).toBeInstanceOf(SpeakerGate);+ expect(loadSpeakerModels).not.toHaveBeenCalled(); // State only; native load stays lazy.+ });++ it('does not give an old startup the next account\'s gate', async () => {+ installModels();+ for (const key of [accountVaultKey('global', 'user-a'), accountVaultKey('global', 'user-b')]) {+ storeProfile(key);+ asyncStore.set(service.__testing.prefsKey(key), JSON.stringify({ version: 1, enabled: true }));+ }+ let release!: (value: string | null) => void;+ vi.mocked(getSecureItem).mockImplementationOnce(() => new Promise(resolve => { release = resolve; }));+ setMobileAuthOwner('user-a', 'global');+ const oldGate = service.createMobileSpeakerGate();+ await vi.waitFor(() => expect(release).toBeTypeOf('function'));+ setMobileAuthOwner('user-b', 'global');+ expect(await service.createMobileSpeakerGate()).toBeInstanceOf(SpeakerGate);+ release(secureStore.get(service.__testing.profileKey(accountVaultKey('global', 'user-a')))!);+ expect(await oldGate).toBeUndefined();+ });++ it('bounds a stalled keychain read and uses the recovered state on the next run', async () => {+ installModels();+ const key = accountVaultKey('global', 'user-a');+ storeProfile(key);+ asyncStore.set(service.__testing.prefsKey(key), JSON.stringify({ version: 1, enabled: true }));+ let release!: (value: string | null) => void;+ vi.mocked(getSecureItem).mockImplementationOnce(() => new Promise(resolve => { release = resolve; }));+ setMobileAuthOwner('user-a', 'global');+ vi.useFakeTimers();+ const gate = service.createMobileSpeakerGate();+ await vi.advanceTimersByTimeAsync(1_500);+ expect(await gate).toBeUndefined();+ expect(vi.getTimerCount()).toBe(0);+ release(secureStore.get(service.__testing.profileKey(key))!);+ expect(await service.createMobileSpeakerGate()).toBeInstanceOf(SpeakerGate);+ expect(vi.getTimerCount()).toBe(0);});it('ignores a voiceprint made by another model', async () => {@@ -362,7 +422,7 @@ describe('mobile speaker filter', () => {await signIn('user-b');expect(service.getMobileSpeakerFilterSnapshot()).toMatchObject({ enabled: false, strictness: 'standard', enrolled: false });- expect(service.createMobileSpeakerGate()).toBeUndefined();+ expect(await service.createMobileSpeakerGate()).toBeUndefined();});it('restoring the default strictness drops the override', async () => {@@ -413,7 +473,7 @@ describe('mobile speaker filter', () => {expect(secureStore.has(service.__testing.profileKey(other))).toBe(true);expect(files.size).toBe(SPEAKER_MODEL_ASSETS.length);expect(service.getMobileSpeakerFilterSnapshot()).toMatchObject({ enabled: false, enrolled: false });- expect(service.createMobileSpeakerGate()).toBeUndefined();+ expect(await service.createMobileSpeakerGate()).toBeUndefined();});it('does not restore a logged-out voiceprint on sign-in until cleanup succeeds', async () => {diff --git a/apps/mobile/src/session/mobileSpeakerFilter.ts b/apps/mobile/src/session/mobileSpeakerFilter.tsindex e09310bcc7..e019240ce4 100644--- a/apps/mobile/src/session/mobileSpeakerFilter.ts+++ b/apps/mobile/src/session/mobileSpeakerFilter.ts@@ -37,9 +37,8 @@ import { logMobileVoice } from './mobileVoiceDiagnostics';* "Recognize my voice" on mobile. Same models, thresholds and gate as desktop;* inference runs through ONNX Runtime in the cindy-speaker-onnx module.*- * Everything dictation start needs is kept in memory so building the gate- * never adds an await before the microphone opens. When anything is not- * ready the run is simply not filtered.+ * Dictation start waits for saved preferences and the voiceprint before+ * choosing its gate. Model loading stays lazy; failures still pass through.** The voiceprint is biometric data: it stays on this device, in the OS* keychain/keystore through SecureStore, one per account, and is never@@ -73,6 +72,7 @@ type Profile = { embedding: Float32Array; createdAt: number };const MAX_EMBEDDING_DIM = 4096;const DOWNLOAD_STALL_MS = 60_000;+const HYDRATION_START_TIMEOUT_MS = 1_500;let snapshot: MobileSpeakerFilterSnapshot = initialSnapshot();let profile: Profile | null = null;@@ -486,12 +486,27 @@ export function warmMobileSpeakerFilter(): void {}/**- * The gate for one dictation run, or undefined when the filter is off or not- * ready. Synchronous by design: it never delays opening the microphone.+ * The gate for one dictation run. Await saved state so the first run after+ * launch/account switch uses the user's setting. Bound storage stalls without+ * waiting for model download or native model loading before opening the mic.*/-export function createMobileSpeakerGate(): SpeakerGate | undefined {- void hydrateMobileSpeakerFilter();+export async function createMobileSpeakerGate(): Promise<SpeakerGate | undefined> {const owner = getMobileAuthOwner();+ let timer: ReturnType<typeof setTimeout> | undefined;+ try {+ await Promise.race([+ hydrateMobileSpeakerFilter(),+ new Promise<never>((_, reject) => {+ timer = setTimeout(() => reject(new Error('speaker hydration timed out')), HYDRATION_START_TIMEOUT_MS);+ }),+ ]);+ } catch {+ logMobileVoice('warn', 'speaker gate passthrough', { reason: 'hydration_timeout' });+ return undefined;+ } finally {+ clearTimeout(timer);+ }+ if (!isMobileAuthOwnerCurrent(owner)) return undefined;if (!snapshot.supported || !snapshot.enabled || hydratedFor !== owner.accountKey || !profile) return undefined;if (snapshot.modelState !== 'ready') {// The OS may have purged the cached models; bring them back for next time.