Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(voice): 排空失败时仍完成取消清理并更新启动回归测试
Dash47a6fa48a0a8
diff --git a/apps/mobile/src/__tests__/composerSpeakerGateStartup.test.ts b/apps/mobile/src/__tests__/composerSpeakerGateStartup.test.tsindex ef31f28d03..5d962f1d82 100644--- a/apps/mobile/src/__tests__/composerSpeakerGateStartup.test.ts+++ b/apps/mobile/src/__tests__/composerSpeakerGateStartup.test.ts@@ -26,11 +26,15 @@ function readStart(page: string) {describe.each(['new.tsx', '[sessionId].tsx'])('%s speaker gate startup', page => {const start = readStart(page);- function setup() {+ function setup(pendingStart = false) {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 () => {}) };+ let finishStart!: () => void;+ const startReady = pendingStart+ ? new Promise<void>(resolve => { finishStart = resolve; })+ : Promise.resolve();+ const controller = { start: vi.fn(() => startReady), 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 };@@ -62,31 +66,34 @@ describe.each(['new.tsx', '[sessionId].tsx'])('%s speaker gate startup', page =>formatRemoteError: String,prewarmAgentOnTyping: () => {},};- return { run: start(bindings), createGate, createController, controller, startupSeq, finishGate, prewarmed, setVoiceError };+ return { run: start(bindings), createGate, createController, controller, startupSeq, finishGate, pendingGate, finishStart, prewarmed, setVoiceError };}- it('waits for hydration before constructing and starting the first controller', async () => {+ // PR #7126: opening capture now overlaps hydration; the real controller tests+ // enforce filtering before ASR. These unit cases execute both page callbacks+ // to protect the promise handoff and cancellation of an in-flight startup.+ it('starts the first controller with pending hydration instead of waiting for it', async () => {const test = setup();const pending = test.run();await vi.waitFor(() => expect(test.createGate).toHaveBeenCalledOnce());- expect(test.createController).not.toHaveBeenCalled();+ expect(test.createController).toHaveBeenCalledWith(expect.objectContaining({ audioGate: test.pendingGate }));+ expect(test.controller.start).toHaveBeenCalledOnce();const gate = { fixture: 'saved speaker gate' };test.finishGate(gate);await pending;- expect(test.createController).toHaveBeenCalledWith(expect.objectContaining({ audioGate: gate }));- expect(test.controller.start).toHaveBeenCalledOnce();+ await expect(test.pendingGate).resolves.toBe(gate);expect(test.setVoiceError).toHaveBeenCalledExactlyOnceWith(null);});- it('releases the claimed ASR without opening the mic if cancelled during hydration', async () => {- const test = setup();+ it('cancels the opened controller when the page abandons startup during hydration', async () => {+ const test = setup(true);const pending = test.run();- await vi.waitFor(() => expect(test.createGate).toHaveBeenCalledOnce());+ await vi.waitFor(() => expect(test.controller.start).toHaveBeenCalledOnce());test.startupSeq.current += 1; // Page teardown/background cancels this startup.test.finishGate({});+ test.finishStart();await pending;- expect(test.createController).not.toHaveBeenCalled();- expect(test.prewarmed.asr.stop).toHaveBeenCalledOnce();+ expect(test.controller.cancel).toHaveBeenCalledOnce();expect(test.setVoiceError).toHaveBeenCalledExactlyOnceWith(null);});@@ -94,7 +101,8 @@ describe.each(['new.tsx', '[sessionId].tsx'])('%s speaker gate startup', page =>const test = setup();test.finishGate(undefined);await test.run();- expect(test.createController).toHaveBeenCalledWith(expect.objectContaining({ audioGate: undefined }));+ expect(test.createController).toHaveBeenCalledWith(expect.objectContaining({ audioGate: test.pendingGate }));+ await expect(test.pendingGate).resolves.toBeUndefined();expect(test.controller.start).toHaveBeenCalledOnce();expect(test.setVoiceError).toHaveBeenCalledExactlyOnceWith(null);});diff --git a/apps/mobile/src/__tests__/mobileVoiceController.test.ts b/apps/mobile/src/__tests__/mobileVoiceController.test.tsindex 64c417a020..bc81682f53 100644--- a/apps/mobile/src/__tests__/mobileVoiceController.test.ts+++ b/apps/mobile/src/__tests__/mobileVoiceController.test.ts@@ -165,6 +165,30 @@ function credential(): StoredMobileVoiceCredential {}describe('mobileVoiceController', () => {+ // PR #7126 / discussion_r4396776891: a rejected native drain used to skip+ // controller cancellation. Existing cancel cases only resolved native stop.+ // Unit: real session.cancel() must abort the gate and close ASR even on error.+ it('cleans the speaker filter and ASR when cancelling after native drain fails', async () => {+ const asr = new FinalOnlyAsrProvider();+ const abort = vi.fn();+ const gate: AudioGate = {+ begin: vi.fn(), push: vi.fn(), finish: async () => undefined, abort,+ };+ const session = createMobileVoiceControllerSession({+ credential: credential(), asr, refiner: null, initialDraft: '',+ audioGate: gate, onDraftChanged: vi.fn(),+ startAudio: async () => async () => { throw new Error('native drain overflow'); },+ });+ await session.start();+ abort.mockClear();+ const stopsBeforeCancel = asr.stopCalls;++ await expect(session.cancel()).rejects.toThrow('native drain overflow');++ expect(abort).toHaveBeenCalledOnce();+ expect(asr.stopCalls).toBeGreaterThan(stopsBeforeCancel);+ });+it('fails capture if pending speaker settings reject instead of uploading unfiltered audio', async () => {let reject!: (error: Error) => void;const asr = new FinalOnlyAsrProvider();diff --git a/apps/mobile/src/session/mobileVoiceController.ts b/apps/mobile/src/session/mobileVoiceController.tsindex 1f8d4ffc26..097e58ce3f 100644--- a/apps/mobile/src/session/mobileVoiceController.ts+++ b/apps/mobile/src/session/mobileVoiceController.ts@@ -686,12 +686,17 @@ export function createMobileVoiceControllerSession(// opening later) is handled by BufferedAsrProvider when controller.cancel()// reaches its stop().runPhase = 'cancelled';- if (stopAudio) {- const stop = stopAudio;- stopAudio = null;- await stop();+ try {+ if (stopAudio) {+ const stop = stopAudio;+ stopAudio = null;+ await stop();+ }+ } finally {+ // Native drain can reject (overflow / missing history). Cancellation+ // still owns filter disposal and closing the ASR connection.+ await controller.cancel();}- await controller.cancel();},currentDraft() {return readCurrentDraft();