Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(voice-input): 保留启动所有权并同步开关使用意图

Dash6fc70406f649
diff --git a/apps/desktop/src/renderer/hooks/useVoiceInputSettings.ts b/apps/desktop/src/renderer/hooks/useVoiceInputSettings.ts
index 0d0dedf2c2..ead9d1f883 100644
--- a/apps/desktop/src/renderer/hooks/useVoiceInputSettings.ts
+++ b/apps/desktop/src/renderer/hooks/useVoiceInputSettings.ts
@@ -133,7 +133,13 @@ export async function adviseAndRecordVoiceInputDictionaryLearning(
export function subscribeVoiceInputSettings(
callback: (settings: VoiceInputSettings) => void,
): () => void {
+ // Every renderer receives the existing main-owned broadcast. Only a live
+ // off-to-on transition is user intent; subscribing with an already-enabled
+ // snapshot (or receiving history/language updates) must not renew the window.
+ let fastActivationEnabled = getVoiceInputSettings().fastActivationEnabled;
return window.electronAPI.voiceInput.onDataChanged((snapshot: VoiceInputDataSnapshot) => {
+ if (!fastActivationEnabled && snapshot.settings.fastActivationEnabled) noteVoiceInputUse();
+ fastActivationEnabled = snapshot.settings.fastActivationEnabled;
callback(snapshot.settings);
});
}
@@ -261,14 +267,7 @@ export function useVoiceInputSettings(): {
);
const setFastActivationEnabled = useCallback(
- (fastActivationEnabled: boolean) => {
- // Flipping this switch on is an explicit statement of intent, so it counts
- // as use and lets the immediate prewarm open the microphone. Without it the
- // setting would stay inert until the first dictation, which is not what the
- // user just asked for.
- if (fastActivationEnabled) noteVoiceInputUse();
- return updateSettings({ fastActivationEnabled });
- },
+ (fastActivationEnabled: boolean) => updateSettings({ fastActivationEnabled }),
[updateSettings],
);
diff --git a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts
index bfb7205882..2dd754731a 100644
--- a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts
+++ b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts
@@ -210,10 +210,6 @@ let keepAliveIdleDeadlineAt: number | undefined;
// indicator would never go out on a machine the user is merely working in —
// contradicting the promise in settings.voiceInput.fastActivation.hint.
let lastVoiceInputUseAt: number | undefined;
-// A recording claim that never reached activate() is a leak: the session stays
-// isBusy() forever, so neither the idle window nor "setting turned off" is
-// allowed to touch it and the microphone can never be reclaimed.
-const STALE_RECORDING_RESERVATION_MS = 60 * 1000;
function isSpecificMicrophoneDeviceId(deviceId?: string): deviceId is string {
return Boolean(deviceId && deviceId !== 'default');
@@ -476,9 +472,6 @@ class KeepAliveMicSession {
// session, and a flag would let whichever finishes first clear the other's
// claim — handing the still-live session to a replacing prewarm.
private recordingReservations = 0;
- // When the current claim was taken. Only used to detect a claim that never
- // reached activate() and never got released.
- private reservationClaimedAt: number | undefined;
constructor(options: KeepAliveSessionOptions) {
this.options = options;
@@ -692,28 +685,11 @@ class KeepAliveMicSession {
* stop(), or on any startup path that fails after claiming.
*/
reserveForRecording(): void {
- if (this.recordingReservations === 0) this.reservationClaimedAt = keepAliveMonotonicNow();
this.recordingReservations += 1;
}
releaseRecordingReservation(): void {
this.recordingReservations = Math.max(0, this.recordingReservations - 1);
- if (this.recordingReservations === 0) this.reservationClaimedAt = undefined;
- }
-
- /**
- * A claim that has outlived any plausible startup.
- *
- * start() takes the claim before its first await and stop() drops it, but a
- * startup that dies in an unforeseen way would keep it forever: the session
- * then reads as busy, so neither the idle window nor "fast activation turned
- * off" may dispose it — the microphone stays open with no path back. Callers
- * use this to reclaim such a session instead of trusting the claim blindly.
- */
- hasStaleRecordingReservation(): boolean {
- if (this.active || this.recordingReservations === 0) return false;
- if (this.reservationClaimedAt === undefined) return false;
- return keepAliveMonotonicNow() - this.reservationClaimedAt > STALE_RECORDING_RESERVATION_MS;
}
/** Someone other than the caller still needs this session live. */
@@ -934,16 +910,10 @@ export async function prewarmVoiceInputMicrophone(options: WebMicAudioEngineOpti
// track the user is speaking into when the device/profile changed. stop()
// already owns both re-arming the idle window and honouring the new config.
if (keepAliveSession?.isBusy()) {
- if (keepAliveSession.hasStaleRecordingReservation()) {
- // Self-heal a leaked claim (see hasStaleRecordingReservation): without this
- // the session is busy forever and no later prewarm can ever reclaim it.
- await disposeKeepAliveVoiceInputMicrophone('stale_recording_reservation');
- } else {
- if (keepAliveSession.key !== keepAliveKey(normalized)) {
- keepAliveSession.markStaleForReplacement();
- }
- return;
+ if (keepAliveSession.key !== keepAliveKey(normalized)) {
+ keepAliveSession.markStaleForReplacement();
}
+ return;
}
if (!isWithinVoiceInputUseWindow()) {
// Nobody asked to dictate inside the retention window, so mounting a
diff --git a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts
index 2dbe225451..78b8f00627 100644
--- a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts
+++ b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { getDefaultVoiceInputSettings, type VoiceInputDataSnapshot } from '../../../shared/voiceInputData';
vi.mock('../audioContextPool', () => ({
PCM16K_WORKLET_NAME: 'pcm16k-worklet',
@@ -236,25 +237,75 @@ describe('keep-alive microphone idle window', () => {
expect(getUserMedia).toHaveBeenCalledTimes(2);
});
- it('reclaims a session whose recording claim leaked', async () => {
+ it('does not replace a recording still awaiting microphone permission after 60 seconds', async () => {
mod.noteVoiceInputUse();
- // Startup hangs on the device handshake and its owner never stops: the claim
- // outlives any plausible startup, so the session would read as busy forever
- // and neither the idle window nor a settings change could reclaim it.
- getUserMedia.mockImplementationOnce(() => new Promise(() => undefined));
+ let resolveStream: (value: unknown) => void = () => undefined;
+ getUserMedia.mockImplementationOnce(() => new Promise((resolve) => {
+ resolveStream = resolve;
+ }));
const engine = new mod.WebMicAudioEngine({
workletUrl: WORKLET_URL,
keepAlive: true,
onInterrupted: vi.fn(),
});
- void engine.start().catch(() => undefined);
+ const starting = engine.start();
+ void starting.catch(() => undefined);
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(2 * 60 * 1000);
await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });
- // The leaked session is reclaimed and a fresh warm-up owns the device again.
- expect(getUserMedia).toHaveBeenCalledTimes(2);
+ // A permission prompt has no fixed response deadline. A mount must neither
+ // replace the owner's pending request nor cause a later cold fallback.
+ expect(getUserMedia).toHaveBeenCalledTimes(1);
+ resolveStream({ getAudioTracks: () => [track], getTracks: () => [track] });
+ await starting;
+ expect(track.stopped).toBe(false);
+ expect(getUserMedia).toHaveBeenCalledTimes(1);
+ await engine.stop();
+ });
+
+ it('warms on an enable broadcast but not on repeated or newly mounted settings snapshots', async () => {
+ const { subscribeVoiceInputSettings } = await import('../../hooks/useVoiceInputSettings');
+ let settings = { ...getDefaultVoiceInputSettings('darwin'), fastActivationEnabled: false };
+ const listeners = new Set<(snapshot: VoiceInputDataSnapshot) => void>();
+ Object.assign(window.electronAPI.voiceInput, {
+ getDataSnapshot: () => ({ settings, history: [] }),
+ onDataChanged: (listener: (snapshot: VoiceInputDataSnapshot) => void) => {
+ listeners.add(listener);
+ return () => listeners.delete(listener);
+ },
+ });
+ const warming: Promise<void>[] = [];
+ const subscribe = () => subscribeVoiceInputSettings(() => {
+ warming.push(mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL }));
+ });
+ const emit = async (enabled: boolean) => {
+ settings = { ...settings, fastActivationEnabled: enabled };
+ listeners.forEach((listener) => listener({ settings, history: [] } as VoiceInputDataSnapshot));
+ await Promise.all(warming.splice(0));
+ };
+ let unsubscribe = subscribe();
+ try {
+ await emit(true);
+ expect(getUserMedia).toHaveBeenCalledTimes(1);
+ await vi.advanceTimersByTimeAsync(IDLE_TTL_MS);
+ expect(track.stopped).toBe(true);
+ await emit(true);
+ expect(getUserMedia).toHaveBeenCalledTimes(1);
+
+ // A renderer subscribing after the switch is already on must not infer a
+ // new activation from that initial snapshot or a history-only broadcast.
+ unsubscribe();
+ unsubscribe = subscribe();
+ await emit(true);
+ expect(getUserMedia).toHaveBeenCalledTimes(1);
+ await emit(false);
+ await emit(true);
+ expect(getUserMedia).toHaveBeenCalledTimes(2);
+ } finally {
+ unsubscribe();
+ }
});
it('does not extend the idle window when prewarm re-asserts keep-alive intent', async () => {