Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(voice): rearm idle unload after rejected start

vastsa8d48bcf34510
diff --git a/apps/desktop/electron/main/voice-service.ts b/apps/desktop/electron/main/voice-service.ts
index 71c0f58bf..2ae68c492 100644
--- a/apps/desktop/electron/main/voice-service.ts
+++ b/apps/desktop/electron/main/voice-service.ts
@@ -25,7 +25,7 @@ type ModelManagerT = import("@pi-desktop/voice-runtime").ModelManager;
// The recommended whisper-large-v3-turbo weights alone hold ~1.5 GiB of
// main-process memory once loaded, and transcription buffers sit on top of
// that. Unloading after a quiet spell keeps an idle app from pinning that
-// memory forever (issue #1496); the model reloads from disk in a few seconds
+// memory forever (issue #1528); the model reloads from disk in a few seconds
// on the next recording.
const MODEL_IDLE_UNLOAD_MS = 10 * 60 * 1000;
@@ -135,17 +135,20 @@ export class VoiceService {
this.cancelModelIdleUnload();
- if (overrides) {
- this.updateSettings(overrides);
- }
-
- await this.ensureRuntime();
- this.releaseCurrentMicrophoneLease();
- this.releaseMicrophoneLease = this.acquireMicrophoneLease(randomUUID());
try {
+ if (overrides) {
+ this.updateSettings(overrides);
+ }
+
+ await this.ensureRuntime();
+ this.releaseCurrentMicrophoneLease();
+ this.releaseMicrophoneLease = this.acquireMicrophoneLease(randomUUID());
await this.controller!.start();
} catch (error) {
this.releaseCurrentMicrophoneLease();
+ // A rejected start may leave the controller in a terminal phase without
+ // emitting stateChange; do not strand the pending idle unload.
+ this.scheduleModelIdleUnload();
throw error;
}
}
diff --git a/apps/desktop/test/voice-service-idle-unload.test.mjs b/apps/desktop/test/voice-service-idle-unload.test.mjs
index fab14bee5..814680726 100644
--- a/apps/desktop/test/voice-service-idle-unload.test.mjs
+++ b/apps/desktop/test/voice-service-idle-unload.test.mjs
@@ -91,6 +91,19 @@ test("starting the next recording cancels the pending idle unload", async (t) =>
assert.equal(modelManager.unloadCalls, 1);
});
+test("a rejected next start re-arms the idle unload", async (t) => {
+ const { service, controller, modelManager, clock } = await startedService(t);
+
+ await service.stop();
+ controller.start = async () => {
+ throw new Error("fixture start failure");
+ };
+
+ await assert.rejects(service.start(), /fixture start failure/);
+ clock.advance(MODEL_IDLE_UNLOAD_MS);
+ assert.equal(modelManager.unloadCalls, 1);
+});
+
test("a recording that began after scheduling is never interrupted by the unload", async (t) => {
const { service, controller, modelManager, clock } = await startedService(t);
diff --git a/docs/spec/03-runtime/live-voice.md b/docs/spec/03-runtime/live-voice.md
index fd18f94d5..1cd748028 100644
--- a/docs/spec/03-runtime/live-voice.md
+++ b/docs/spec/03-runtime/live-voice.md
@@ -181,6 +181,13 @@ underlying Dictation capability remain unchanged and are not deleted or rewritte
by the UI. Old settings with no `liveVoice` value read as disabled with no
bindings.
+When the hidden local Dictation capability loads an STT model, it releases the
+model after ten minutes in a terminal phase without a new recording. Starting a
+recording cancels a pending unload; a later recording lazily reloads the model
+from its on-disk cache. This changes idle memory use and the first-recording
+latency after a long idle period, but does not alter Dictation settings or the
+downloaded model files.
+
Live DTOs and IPC contracts live in `packages/shared`. Pure wire parsing and
PCM math are exported only from `@pi-desktop/voice-runtime/live`; they do not
enter the old local transcription runtime. Electron Main remains an
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 4b4eb023e..bd93638d3 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -37,6 +37,26 @@
and Rust `data_relocation` tests cover rollback and path/filesystem boundaries.
+### E2E-LEGACY-DICTATION-idle-model-release
+
+- **Preconditions:** Exercise the retained local Dictation service through its
+ `VoiceService` interface with a deterministic controller/model-manager stub
+ and a controlled clock. Do not access a physical microphone or download a
+ model.
+- **Steps:** Start and stop a recording, advance time to just before and then
+ to the 10-minute idle boundary, and inspect unload calls. Repeat with a new
+ recording before the boundary, a rejected new start, an error terminal phase,
+ and service disposal.
+- **Expected:** A settled idle model unloads once at the boundary, never early.
+ A new recording cancels the timer and its terminal phase starts a fresh
+ window. A rejected start does not strand the unload. An active phase at the
+ callback or service disposal prevents unloading.
+- **Coverage:** `apps/desktop/test/voice-service-idle-unload.test.mjs` runs the
+ production `VoiceService` lifecycle with only the runtime and clock mocked.
+ The separate Linux RSS measurement verifies that `TranscribeModel.dispose()`
+ returns memory to the OS and that the existing lazy-load path reloads it.
+- **Specs:** [Live Voice](../03-runtime/live-voice.md).
+
### E2E-LIVE-VOICE-public-settings-and-reconnect
- **Preconditions:** A built production Renderer and real Electron/Main/Host,
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 1c6b67558..9e35fe8db 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -9,6 +9,21 @@
---
+### E2E-LEGACY-DICTATION-idle-model-release
+
+- **前提:** 通过 `VoiceService` 接口测试保留的本地 Dictation 服务,使用确定性的
+ Controller/ModelManager stub 和受控时钟。不访问物理麦克风,也不下载模型。
+- **步骤:** 启动并停止一次录音,将时间推进到 10 分钟空闲边界之前和边界时,检查
+ 卸载调用。分别在边界前开始新录音、下一次启动被拒绝、进入 error 终态以及服务
+ dispose 时重复检查。
+- **预期:** 已结束的空闲模型只在边界到达时卸载一次,不会提前卸载。新录音会取消
+ 计时器,其终态会启动新的等待窗口。新录音启动被拒绝时不会遗失卸载计划。回调时
+ 处于活动阶段或服务已 dispose 都不会触发卸载。
+- **覆盖:** `apps/desktop/test/voice-service-idle-unload.test.mjs` 使用真实
+ `VoiceService` 生命周期,仅 mock 运行时边界和时钟。独立 Linux RSS 测量验证
+ `TranscribeModel.dispose()` 会向操作系统归还内存,且现有延迟加载路径可重新加载。
+- **规格:** [实时语音](../03-runtime/live-voice.md)。
+
### E2E-LIVE-VOICE-public-settings-and-reconnect
- **前提:** 生产 Renderer 构建、真实 Electron/Main/Host、隔离数据和测试项目、关闭开发者模式、本地 TLS Realtime fixture 及合成麦克风。仅在测试子进程中信任 fixture CA,不关闭 TLS、sender、沙盒或麦克风权限校验。