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): 启用意图立即跟随电源释放并复用测试模块

Dash46203f41b23f
diff --git a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts
index 2dd754731a..b8f55c6af8 100644
--- a/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts
+++ b/apps/desktop/src/renderer/voice-input/WebMicAudioEngine.ts
@@ -166,6 +166,7 @@ export function currentPowerReleaseGeneration(): number {
*/
export function disposeVoiceInputAudioModuleForHmr(): Promise<void> {
powerReleaseGeneration += 1;
+ lastVoiceInputUseAt = undefined;
keepAlivePowerReleaseUnsubscribe?.();
keepAlivePowerReleaseUnsubscribe = undefined;
keepAlivePowerReleaseListening = false;
@@ -209,6 +210,8 @@ let keepAliveIdleDeadlineAt: number | undefined;
// task switch would light up a fresh 30-minute window and the OS privacy
// indicator would never go out on a machine the user is merely working in —
// contradicting the promise in settings.voiceInput.fastActivation.hint.
+// The MediaStream and its use window belong to this capture renderer. Another
+// window's first capture is cold; recording here does not open its microphone.
let lastVoiceInputUseAt: number | undefined;
function isSpecificMicrophoneDeviceId(deviceId?: string): deviceId is string {
@@ -425,9 +428,8 @@ function ensureKeepAliveDeviceChangeListener(): void {
* Both mean the user walked away, so holding the capture device buys no
* latency while still lighting the OS privacy indicator and holding an
* idle-sleep assertion through coreaudiod. Mirrors the devicechange listener
- * above: one process-wide subscription, never torn down. Installed lazily by
- * whichever capture starts first — the keep-alive session *or* a direct
- * (keepAlive: false) capture, which relies on it just as much.
+ * above: one subscription per module, released on HMR. Installed at the first
+ * use intent or capture start, including direct (keepAlive: false) capture.
*/
function ensureKeepAlivePowerReleaseListener(): void {
if (keepAlivePowerReleaseListening) return;
@@ -1018,6 +1020,7 @@ function keepAliveMonotonicNow(): number {
* after the idle window has released the device; mounting components never does.
*/
export function noteVoiceInputUse(): void {
+ ensureKeepAlivePowerReleaseListener();
lastVoiceInputUseAt = keepAliveMonotonicNow();
}
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 78b8f00627..5300069778 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,12 +1,13 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { getDefaultVoiceInputSettings, type VoiceInputDataSnapshot } from '../../../shared/voiceInputData';
+import * as mod from '../WebMicAudioEngine';
+import * as pool from '../audioContextPool';
vi.mock('../audioContextPool', () => ({
PCM16K_WORKLET_NAME: 'pcm16k-worklet',
prewarmVoiceInputAudio: vi.fn(),
}));
-type EngineModule = typeof import('../WebMicAudioEngine');
type PowerCallback = (payload: { reason: 'system_suspend' | 'screen_locked' }) => void;
const WORKLET_URL = 'https://app.local/pcm16k-worklet.js';
@@ -33,19 +34,17 @@ function createFakeTrack(): FakeTrack {
}
/**
- * The keep-alive session is module-level state, so every case re-imports the
- * engine to start from a clean slate. Without this the process-wide power
- * listener flag would leak across cases and silently skip the subscription.
+ * Load the engine once. Exercise its real power release and HMR disposal at
+ * teardown so streams, use windows and subscriptions cannot leak across cases.
*/
describe('keep-alive microphone idle window', () => {
- let mod: EngineModule;
let track: FakeTrack;
let sink: { connect: ReturnType<typeof vi.fn>; disconnect: ReturnType<typeof vi.fn>; gain: { value: number } };
let destination: object;
let powerCallback: PowerCallback | undefined;
let getUserMedia: ReturnType<typeof vi.fn>;
- beforeEach(async () => {
+ beforeEach(() => {
vi.useFakeTimers();
track = createFakeTrack();
powerCallback = undefined;
@@ -83,8 +82,6 @@ describe('keep-alive microphone idle window', () => {
port: { close: vi.fn(), postMessage: vi.fn(), onmessage: null },
})));
- vi.resetModules();
- const pool = await import('../audioContextPool');
vi.mocked(pool.prewarmVoiceInputAudio).mockResolvedValue({
context: {
currentTime: 0,
@@ -97,11 +94,11 @@ describe('keep-alive microphone idle window', () => {
workletReady: Promise.resolve(),
workletUrl: WORKLET_URL,
});
- mod = await import('../WebMicAudioEngine');
});
afterEach(async () => {
- await mod.disposeKeepAliveVoiceInputMicrophone('test_cleanup');
+ powerCallback?.({ reason: 'screen_locked' });
+ await mod.disposeVoiceInputAudioModuleForHmr();
vi.useRealTimers();
vi.unstubAllGlobals();
vi.restoreAllMocks();
@@ -167,6 +164,17 @@ describe('keep-alive microphone idle window', () => {
expect(getUserMedia).not.toHaveBeenCalled();
});
+ it('clears enable intent on a power release before the first capture', async () => {
+ // Settings can enable fast activation before any capture engine is built.
+ // That intent must already observe lock/suspend, or a later chat mount
+ // would open the device after the user has walked away.
+ mod.noteVoiceInputUse();
+ expect(powerCallback).toBeDefined();
+ powerCallback?.({ reason: 'screen_locked' });
+ await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });
+ expect(getUserMedia).not.toHaveBeenCalled();
+ });
+
it.each([0, 60 * 1000])('does not relight the microphone %i ms after the idle release', async (delay) => {
mod.noteVoiceInputUse();
await mod.prewarmVoiceInputMicrophone({ workletUrl: WORKLET_URL });
diff --git a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts
index ca704cbb3b..9e1293b9fe 100644
--- a/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts
+++ b/apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.prewarm.test.ts
@@ -62,10 +62,6 @@ describe('prewarmVoiceInputMicrophoneWithAutomaticFallback', () => {
let deviceChangeListener: (() => void) | undefined;
beforeEach(() => {
- // Every case here exercises prewarming for a user who is dictating, so put
- // the process inside the use window first — mounting alone never opens the
- // device (see WebMicAudioEngine.keepAliveIdle.test.ts).
- noteVoiceInputUse();
track = createFakeTrack();
getUserMedia = vi.fn(async () => ({
getAudioTracks: () => [track],
@@ -116,6 +112,9 @@ describe('prewarmVoiceInputMicrophoneWithAutomaticFallback', () => {
workletReady: Promise.resolve(),
workletUrl: 'https://app.local/pcm16k-worklet.js',
});
+ // Establish the renderer before recording intent: it also subscribes to
+ // power releases, even if no microphone has been opened yet.
+ noteVoiceInputUse();
});
afterEach(async () => {
diff --git a/scripts/test-hygiene-baseline.json b/scripts/test-hygiene-baseline.json
index 1cb40e013d..b72792e34c 100644
--- a/scripts/test-hygiene-baseline.json
+++ b/scripts/test-hygiene-baseline.json
@@ -927,9 +927,6 @@
"apps/desktop/src/renderer/voice-input/__tests__/VoiceInputSection.recordingGate.test.ts": {
"sourceReads": 9
},
- "apps/desktop/src/renderer/voice-input/__tests__/WebMicAudioEngine.keepAliveIdle.test.ts": {
- "resetModules": 1
- },
"apps/mobile/src/__tests__/agentCapabilitiesCache.test.ts": {
"resetModules": 1
},