Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(reasoning): stop the shared llama-server only when no scope needs it (#2328)

Chad Piha671198d5686d
diff --git a/preload.js b/preload.js
index 8c30b06c7..1a36bc1bc 100644
--- a/preload.js
+++ b/preload.js
@@ -708,7 +708,6 @@ contextBridge.exposeInMainWorld("electronAPI", {
// llama-server
llamaServerStart: (modelId) => ipcRenderer.invoke("llama-server-start", modelId),
- llamaServerStop: () => ipcRenderer.invoke("llama-server-stop"),
llamaServerStatus: () => ipcRenderer.invoke("llama-server-status"),
llamaGpuReset: () => ipcRenderer.invoke("llama-gpu-reset"),
diff --git a/src/components/ReasoningModelSelector.tsx b/src/components/ReasoningModelSelector.tsx
index 3eea578e6..cb8813673 100644
--- a/src/components/ReasoningModelSelector.tsx
+++ b/src/components/ReasoningModelSelector.tsx
@@ -499,10 +499,6 @@ export default function ReasoningModelSelector({
setLocalReasoningProvider("");
setReasoningModel("");
}
-
- if (newMode === "cloud") {
- window.electronAPI?.llamaServerStop?.();
- }
};
const handleCloudProviderChange = (provider: string) => {
diff --git a/src/components/settings/InferenceConfigEditor.tsx b/src/components/settings/InferenceConfigEditor.tsx
index 02b74f815..9d951e9c1 100644
--- a/src/components/settings/InferenceConfigEditor.tsx
+++ b/src/components/settings/InferenceConfigEditor.tsx
@@ -148,11 +148,6 @@ export default function InferenceConfigEditor({
patch.model = "";
}
setResolvedLLMConfig(scope, patch);
-
- if (mode === "openwhispr" || mode === "self-hosted" || mode === "enterprise") {
- window.electronAPI?.llamaServerStop?.();
- }
-
onModeChange?.(mode);
},
[scope, config.provider, effectiveMode, isSignedIn, onModeChange, isModeAllowed]
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 990f5c3f0..ee5642050 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -78,7 +78,7 @@ const diarizationHost = (endpoint) => {
} catch {}
return null;
};
-const { resolveLocalServerNeeds } = require("./localServerPolicy");
+const { resolveLocalServerNeeds, shouldStopLocalServer } = require("./localServerPolicy");
const autoStart = require("./autoStart");
const { getRelaunchOptions, getRelaunchWaiter } = require("./autoStartPolicy");
const HyprlandShortcutManager = require("./hyprlandShortcut");
@@ -5221,34 +5221,44 @@ class IPCHandlers {
});
}
- const localServer = resolveLocalServerNeeds(prefs);
-
- if (localServer.cleanup) {
- setVars.CLEANUP_PROVIDER = "local";
- setVars.LOCAL_CLEANUP_MODEL = localServer.cleanup;
- } else {
- clearVars.push("CLEANUP_PROVIDER", "LOCAL_CLEANUP_MODEL");
- }
// TODO: drop legacy REASONING_PROVIDER / LOCAL_REASONING_MODEL clears once
// the read fallback is removed (~2 releases after this lands).
clearVars.push("REASONING_PROVIDER", "LOCAL_REASONING_MODEL");
- if (localServer.dictationAgent) {
- setVars.DICTATION_AGENT_PROVIDER = "local";
- setVars.LOCAL_DICTATION_AGENT_MODEL = localServer.dictationAgent;
- } else {
- clearVars.push("DICTATION_AGENT_PROVIDER", "LOCAL_DICTATION_AGENT_MODEL");
- }
+ // A signed-in window whose workspace policy is still loading reports
+ // unclamped modes, so it neither pre-warms nor stops the shared
+ // llama-server; signed out, the policy never loads.
+ if (prefs.policySettled || !this._hasActiveAccountScope()) {
+ const localServer = resolveLocalServerNeeds(prefs);
- // Stop the shared llama-server only when neither scope still needs it, so
- // the active scope keeps its server when the other one switches away.
- if (localServer.stopServer) {
+ if (localServer.cleanup) {
+ setVars.CLEANUP_PROVIDER = "local";
+ setVars.LOCAL_CLEANUP_MODEL = localServer.cleanup;
+ } else {
+ clearVars.push("CLEANUP_PROVIDER", "LOCAL_CLEANUP_MODEL");
+ }
+
+ if (localServer.dictationAgent) {
+ setVars.DICTATION_AGENT_PROVIDER = "local";
+ setVars.LOCAL_DICTATION_AGENT_MODEL = localServer.dictationAgent;
+ } else {
+ clearVars.push("DICTATION_AGENT_PROVIDER", "LOCAL_DICTATION_AGENT_MODEL");
+ }
+
+ // Stop the shared llama-server only when no scope still needs the model
+ // it holds, so the active scopes keep their server when another leaves.
const modelManager = require("./modelManagerBridge").default;
- modelManager.stopServer().catch((err) => {
- debugLogger.error("Failed to stop llama-server on provider switch", {
- error: err.message,
+ if (shouldStopLocalServer(localServer, modelManager.currentServerModelId)) {
+ if (modelManager.getServerStatus().running) {
+ debugLogger.debug("Stopping llama-server: no scope needs its model", {
+ loadedModel: modelManager.currentServerModelId,
+ neededModels: localServer.models,
+ });
+ }
+ modelManager.stopServer().catch((err) => {
+ debugLogger.error("Failed to stop llama-server", { error: err.message });
});
- });
+ }
}
this._syncStartupEnv(setVars, clearVars);
@@ -5448,16 +5458,6 @@ class IPCHandlers {
}
});
- ipcMain.handle("llama-server-stop", async () => {
- try {
- const modelManager = require("./modelManagerBridge").default;
- await modelManager.stopServer();
- return { success: true };
- } catch (error) {
- return { success: false, error: error.message };
- }
- });
-
ipcMain.handle("llama-server-status", async () => {
try {
const modelManager = require("./modelManagerBridge").default;
diff --git a/src/helpers/llamaServer.js b/src/helpers/llamaServer.js
index dae65d0cd..7010ed40d 100644
--- a/src/helpers/llamaServer.js
+++ b/src/helpers/llamaServer.js
@@ -184,8 +184,12 @@ class LlamaServerManager {
this.modelPath === modelPath &&
this.draftModelPath === requestedDraftPath &&
requestedContextSize <= (this.contextSize || 0)
- )
+ ) {
+ // Streaming chat reaches the port directly and never goes through
+ // inference(), so asking for the running server is its only activity.
+ this.resetIdleTimer();
return;
+ }
if (this.process) {
await this.stop();
@@ -607,13 +611,24 @@ class LlamaServerManager {
resetIdleTimer() {
this.clearIdleTimer();
- this.idleTimer = setTimeout(() => {
+ const timer = setTimeout(async () => {
+ // Streaming chat talks to the port directly, so only the server knows
+ // whether an answer that outlived the timeout is still being generated.
+ const slots = await this._requestJson("/slots");
+ if (this.idleTimer !== timer) return;
+ if (Array.isArray(slots) && slots.some((slot) => slot.is_processing)) {
+ this.resetIdleTimer();
+ return;
+ }
debugLogger.info("llama-server idle timeout reached, stopping to free VRAM", {
timeoutMs: IDLE_TIMEOUT_MS,
model: this.modelPath ? path.basename(this.modelPath) : null,
});
this.stop();
}, IDLE_TIMEOUT_MS);
+ // Freeing an idle server is housekeeping; it must never hold the process open.
+ timer.unref();
+ this.idleTimer = timer;
}
clearIdleTimer() {
diff --git a/src/helpers/localServerPolicy.js b/src/helpers/localServerPolicy.js
index 3d7a9d8be..5d8f7118d 100644
--- a/src/helpers/localServerPolicy.js
+++ b/src/helpers/localServerPolicy.js
@@ -1,9 +1,17 @@
// Which model each LLM scope needs pre-warmed in the shared llama-server, and
-// whether that server can be stopped.
+// whether that server can be stopped. This is the single rule for stopping it:
+// every scope that can run locally shares the one server, so it may only stop
+// once none of them needs the model it holds.
//
// A scope stores its local selection as a catalog family id (qwen, gemma, …),
// never the literal "local", so only the scope's mode says whether it runs
// locally. A scope that is switched off never runs, so it needs no server.
+// Modes and models must be the resolved, policy-effective ones (see
+// selectLocalServerPrefs), since those are what the request path runs on.
+// dictationAgentVision is absent because it can never be local.
+const localModelOf = (enabled, mode, model) =>
+ enabled && mode === "local" ? model?.trim() || "" : "";
+
export function resolveLocalServerNeeds({
useCleanupModel,
cleanupMode,
@@ -11,10 +19,29 @@ export function resolveLocalServerNeeds({
useDictationAgent,
dictationAgentMode,
dictationAgentModel,
+ noteFormattingMode,
+ noteFormattingModel,
+ chatAgentMode,
+ chatAgentModel,
+ useDictationTranslation,
+ translationMode,
+ translationModel,
}) {
- const cleanup = useCleanupModel && cleanupMode === "local" ? cleanupModel?.trim() || "" : "";
- const dictationAgent =
- useDictationAgent && dictationAgentMode === "local" ? dictationAgentModel?.trim() || "" : "";
+ const cleanup = localModelOf(useCleanupModel, cleanupMode, cleanupModel);
+ const dictationAgent = localModelOf(useDictationAgent, dictationAgentMode, dictationAgentModel);
+ const models = [
+ cleanup,
+ dictationAgent,
+ localModelOf(true, noteFormattingMode, noteFormattingModel),
+ localModelOf(true, chatAgentMode, chatAgentModel),
+ localModelOf(useDictationTranslation, translationMode, translationModel),
+ ].filter((model, index, all) => model && all.indexOf(model) === index);
+
+ return { cleanup, dictationAgent, models };
+}
- return { cleanup, dictationAgent, stopServer: !cleanup && !dictationAgent };
+// The server holds one model, so it can also go when no scope needs the model
+// it has loaded: the next local request reloads it with the right one anyway.
+export function shouldStopLocalServer({ models }, loadedModelId) {
+ return loadedModelId ? !models.includes(loadedModelId) : models.length === 0;
}
diff --git a/src/hooks/useSettings.ts b/src/hooks/useSettings.ts
index b5bd29b64..3bf54e187 100644
--- a/src/hooks/useSettings.ts
+++ b/src/hooks/useSettings.ts
@@ -1,5 +1,10 @@
-import React, { createContext, useCallback, useContext, useEffect, useRef } from "react";
-import { useSettingsStore, initializeSettings } from "../stores/settingsStore";
+import React, { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
+import { useShallow } from "zustand/react/shallow";
+import {
+ useSettingsStore,
+ initializeSettings,
+ selectLocalServerPrefs,
+} from "../stores/settingsStore";
import logger from "../utils/logger";
import { useLocalStorage } from "./useLocalStorage";
import type {
@@ -13,8 +18,10 @@ import {
effectiveAudioRetentionDays,
effectiveLocalHistoryEnabled,
isLocalHistoryPolicyResolved,
+ isPolicySettled,
} from "../stores/policyRules";
import { usePolicyStore } from "../stores/policyStore";
+import { usePolicySnapshot } from "./usePolicy";
export interface TranscriptionSettings {
uiLanguage: string;
@@ -223,13 +230,24 @@ function useSettingsInternal() {
parakeetModel,
cohereModel,
preferredLanguage,
- useCleanupModel,
- cleanupMode,
- cleanupModel,
- useDictationAgent,
- dictationAgentMode,
- dictationAgentModel,
} = store;
+ // Every window runs this sync, and the main process stops the shared
+ // llama-server from it, so it must see every scope's resolved local model.
+ const policySnapshot = usePolicySnapshot();
+ const localServerPrefs = useSettingsStore(
+ useShallow((state) => selectLocalServerPrefs(state, policySnapshot))
+ );
+ const policySettled = isPolicySettled(policySnapshot);
+ // A sign-out before the policy fetch starts leaves the policy idle, so only
+ // the cleared account scope says this window's deferred sync can now apply.
+ const [signOuts, setSignOuts] = useState(0);
+ useEffect(
+ () =>
+ window.electronAPI?.onActiveAccountScopeChanged?.((scope) => {
+ if (!scope) setSignOuts((count) => count + 1);
+ }),
+ []
+ );
useEffect(() => {
if (typeof window === "undefined" || !window.electronAPI?.syncStartupPreferences) return;
@@ -246,12 +264,8 @@ function useSettingsInternal() {
localTranscriptionProvider,
model: model || undefined,
language: preferredLanguage || undefined,
- useCleanupModel,
- cleanupMode,
- cleanupModel,
- useDictationAgent,
- dictationAgentMode,
- dictationAgentModel,
+ ...localServerPrefs,
+ policySettled,
})
.catch((err) =>
logger.warn(
@@ -267,12 +281,9 @@ function useSettingsInternal() {
parakeetModel,
cohereModel,
preferredLanguage,
- useCleanupModel,
- cleanupMode,
- cleanupModel,
- useDictationAgent,
- dictationAgentMode,
- dictationAgentModel,
+ localServerPrefs,
+ policySettled,
+ signOuts,
]);
return {
diff --git a/src/stores/policyRules.ts b/src/stores/policyRules.ts
index d92a6e2e0..44ac3b7f1 100644
--- a/src/stores/policyRules.ts
+++ b/src/stores/policyRules.ts
@@ -105,6 +105,15 @@ export function isLocalHistoryPolicyResolved(state: PolicyDecisionSnapshot): boo
return state.status === "managed" || state.status === "unmanaged";
}
+/**
+ * Whether this window has applied whatever policy it will get. `idle` and
+ * `loading` may still be a signed-in window before its fetch lands, while
+ * `error` applies no policy and is as final as `managed` or `unmanaged`.
+ */
+export function isPolicySettled(state: PolicyDecisionSnapshot): boolean {
+ return state.status !== "idle" && state.status !== "loading";
+}
+
/** The org-forced local history value, or null when the user may choose. */
export function lockedLocalHistoryValue(state: PolicyDecisionSnapshot): boolean | null {
const mode = managedPolicy(state)?.dataRetention.localHistoryMode;
diff --git a/src/stores/settingsStore.ts b/src/stores/settingsStore.ts
index 7f0104d87..72c73fa88 100644
--- a/src/stores/settingsStore.ts
+++ b/src/stores/settingsStore.ts
@@ -9,6 +9,7 @@ import type {
ChineseScriptPreference,
LocalTranscriptionProvider,
InferenceMode,
+ LocalServerPrefs,
SelfHostedType,
} from "../types/electron";
import type { CalendarAccount } from "../types/calendar";
@@ -2957,6 +2958,36 @@ export function isCloudChatAgentMode() {
return selectIsCloudChatAgentMode(getSettings());
}
+// What resolveLocalServerNeeds (main process) decides the shared llama-server
+// from. The policy-effective resolved configs carry fallback inheritance,
+// enterprise overrides and policy clamps, so they match what requests run on.
+export function selectLocalServerPrefs(
+ rawState: SettingsState,
+ policyState: PolicyDecisionSnapshot
+): LocalServerPrefs {
+ const state = selectPolicyEffectiveSettings(rawState, policyState);
+ const cleanup = selectResolvedLLMConfig(state, "dictationCleanup");
+ const dictationAgent = selectResolvedLLMConfig(state, "dictationAgent");
+ const noteFormatting = selectResolvedLLMConfig(state, "noteFormatting");
+ const chat = selectResolvedLLMConfig(state, "chatIntelligence");
+ const translation = selectResolvedLLMConfig(state, "dictationTranslation");
+ return {
+ useCleanupModel: state.useCleanupModel,
+ cleanupMode: cleanup.mode,
+ cleanupModel: cleanup.model,
+ useDictationAgent: state.useDictationAgent,
+ dictationAgentMode: dictationAgent.mode,
+ dictationAgentModel: dictationAgent.model,
+ noteFormattingMode: noteFormatting.mode,
+ noteFormattingModel: noteFormatting.model,
+ chatAgentMode: chat.mode,
+ chatAgentModel: chat.model,
+ useDictationTranslation: state.useDictationTranslation,
+ translationMode: translation.mode,
+ translationModel: translation.model,
+ };
+}
+
// --- Convenience getters for non-React code ---
interface TranscriptionContextKeys {
diff --git a/src/types/electron.ts b/src/types/electron.ts
index c2f6b260f..dcd44df01 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -23,6 +23,23 @@ export type ChineseScriptPreference = "simplified" | "traditional" | "as-transcr
export type InferenceMode = "openwhispr" | "providers" | "local" | "self-hosted" | "enterprise";
+/** Each LLM scope's resolved mode and model, from which the main process decides the shared llama-server. */
+export interface LocalServerPrefs {
+ useCleanupModel: boolean;
+ cleanupMode: InferenceMode;
+ cleanupModel: string;
+ useDictationAgent: boolean;
+ dictationAgentMode: InferenceMode;
+ dictationAgentModel: string;
+ noteFormattingMode: InferenceMode;
+ noteFormattingModel: string;
+ chatAgentMode: InferenceMode;
+ chatAgentModel: string;
+ useDictationTranslation: boolean;
+ translationMode: InferenceMode;
+ translationModel: string;
+}
+
export type SelfHostedType = "openai-compatible" | "lan";
export type TranscriptionStatus = "completed" | "failed" | "pending" | "discarded";
@@ -1673,18 +1690,15 @@ declare global {
saveUiLanguage: (language: string) => Promise<{ success: boolean; language: string }>;
setUiLanguage: (language: string) => Promise<{ success: boolean; language: string }>;
saveAllKeysToEnv: () => Promise<{ success: boolean; path: string }>;
- syncStartupPreferences: (prefs: {
- useLocalWhisper: boolean;
- localTranscriptionProvider: LocalTranscriptionProvider;
- model?: string;
- language?: string;
- useCleanupModel: boolean;
- cleanupMode: InferenceMode;
- cleanupModel?: string;
- useDictationAgent: boolean;
- dictationAgentMode: InferenceMode;
- dictationAgentModel?: string;
- }) => Promise<void>;
+ syncStartupPreferences: (
+ prefs: LocalServerPrefs & {
+ useLocalWhisper: boolean;
+ localTranscriptionProvider: LocalTranscriptionProvider;
+ model?: string;
+ language?: string;
+ policySettled: boolean;
+ }
+ ) => Promise<void>;
// Clipboard operations
checkAccessibilityPermission: (silent?: boolean) => Promise<boolean>;
@@ -1923,7 +1937,6 @@ declare global {
llamaServerStart: (
modelId: string
) => Promise<{ success: boolean; port?: number; error?: string }>;
- llamaServerStop: () => Promise<{ success: boolean; error?: string }>;
llamaServerStatus: () => Promise<LlamaServerStatus>;
llamaGpuReset: () => Promise<{ success: boolean; error?: string }>;
detectVulkanGpu?: () => Promise<VulkanGpuResult>;
diff --git a/test/helpers/llamaServerIdle.test.js b/test/helpers/llamaServerIdle.test.js
new file mode 100644
index 000000000..035b9b7e6
--- /dev/null
+++ b/test/helpers/llamaServerIdle.test.js
@@ -0,0 +1,99 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+const LlamaServerManager = require("../../src/helpers/llamaServer.js");
+
+const MINUTE = 60 * 1000;
+
+// The idle callback awaits the server's /slots answer before it decides.
+const settle = () => new Promise(setImmediate);
+
+// A manager whose spawn is stubbed at the _doStart boundary but still arms the
+// idle timer the way a real start does, whose /slots answer is scripted, and
+// whose stop is recorded.
+function makeManager() {
+ const manager = new LlamaServerManager();
+ const stops = [];
+ manager.processing = false;
+ manager._requestJson = async (path) =>
+ path === "/slots" ? [{ is_processing: false }, { is_processing: manager.processing }] : null;
+ manager._doStart = async (modelPath, options = {}) => {
+ manager.ready = true;
+ manager.modelPath = modelPath;
+ manager.draftModelPath = options.draftModelPath || null;
+ manager.resetIdleTimer();
+ };
+ manager.stop = async () => {
+ stops.push(Date.now());
+ manager.clearIdleTimer();
+ };
+ return { manager, stops };
+}
+
+// Streaming chat (the Voice Assistant panel, typed chat) asks for the running
+// server before every turn and then talks to its port directly, so that ask is
+// the only activity the server sees. Before this, an active conversation was
+// stopped five minutes after the server first started.
+test("asking for the already running server postpones the idle stop", async (t) => {
+ t.mock.timers.enable({ apis: ["setTimeout", "Date"] });
+ const { manager, stops } = makeManager();
+
+ await manager.start("/models/main.gguf");
+ t.mock.timers.tick(4 * MINUTE);
+ await manager.start("/models/main.gguf");
+ t.mock.timers.tick(4 * MINUTE);
+ await settle();
+
+ assert.equal(stops.length, 0, "an active server must not idle out");
+
+ t.mock.timers.tick(1 * MINUTE + 1);
+ await settle();
+ assert.equal(stops.length, 1, "it still stops once really idle");
+});
+
+// A single streamed answer can outlast the timeout with no new request.
+test("a server still generating an answer is not stopped mid-stream", async (t) => {
+ t.mock.timers.enable({ apis: ["setTimeout", "Date"] });
+ const { manager, stops } = makeManager();
+
+ await manager.start("/models/main.gguf");
+ manager.processing = true;
+ t.mock.timers.tick(5 * MINUTE + 1);
+ await settle();
+ assert.equal(stops.length, 0, "a busy slot must postpone the stop");
+
+ manager.processing = false;
+ t.mock.timers.tick(5 * MINUTE + 1);
+ await settle();
+ assert.equal(stops.length, 1, "it stops once the answer is done");
+});
+
+test("a server that cannot answer /slots is stopped as before", async (t) => {
+ t.mock.timers.enable({ apis: ["setTimeout", "Date"] });
+ const { manager, stops } = makeManager();
+ manager._requestJson = async () => null;
+
+ await manager.start("/models/main.gguf");
+ t.mock.timers.tick(5 * MINUTE + 1);
+ await settle();
+
+ assert.equal(stops.length, 1);
+});
+
+test("a request that arrives while /slots is checked keeps the server", async (t) => {
+ t.mock.timers.enable({ apis: ["setTimeout", "Date"] });
+ const { manager, stops } = makeManager();
+ let answerSlots;
+ manager._requestJson = () =>
+ new Promise((resolve) => {
+ answerSlots = resolve;
+ });
+
+ await manager.start("/models/main.gguf");
+ t.mock.timers.tick(5 * MINUTE + 1);
+ await manager.start("/models/main.gguf");
+ answerSlots([{ is_processing: false }]);
+ await settle();
+
+ assert.equal(stops.length, 0, "the newer request's timer owns the decision now");
+});
diff --git a/test/helpers/localServerPolicy.test.js b/test/helpers/localServerPolicy.test.js
index 657f1bad9..cfb7bb5ec 100644
--- a/test/helpers/localServerPolicy.test.js
+++ b/test/helpers/localServerPolicy.test.js
@@ -3,13 +3,28 @@ const assert = require("node:assert/strict");
const load = () => import("../../src/helpers/localServerPolicy.js");
-const LOCAL_CLEANUP = {
+// Every scope off local: the baseline each test switches one scope away from.
+const NOTHING_LOCAL = {
useCleanupModel: true,
- cleanupMode: "local",
- cleanupModel: "qwen3-8b-q4_k_m",
- useDictationAgent: false,
+ cleanupMode: "openwhispr",
+ cleanupModel: "",
+ useDictationAgent: true,
dictationAgentMode: "openwhispr",
dictationAgentModel: "",
+ noteFormattingMode: "openwhispr",
+ noteFormattingModel: "",
+ chatAgentMode: "openwhispr",
+ chatAgentModel: "",
+ useDictationTranslation: false,
+ translationMode: "openwhispr",
+ translationModel: "",
+};
+
+const LOCAL_CLEANUP = {
+ ...NOTHING_LOCAL,
+ useDictationAgent: false,
+ cleanupMode: "local",
+ cleanupModel: "qwen3-8b-q4_k_m",
};
test("a local scope pre-warms the model it selected", async () => {
@@ -18,7 +33,7 @@ test("a local scope pre-warms the model it selected", async () => {
assert.deepEqual(resolveLocalServerNeeds(LOCAL_CLEANUP), {
cleanup: "qwen3-8b-q4_k_m",
dictationAgent: "",
- stopServer: false,
+ models: ["qwen3-8b-q4_k_m"],
});
});
@@ -31,7 +46,7 @@ test("a local selection is recognized by its mode, not its provider id", async (
const needs = resolveLocalServerNeeds({ ...LOCAL_CLEANUP, cleanupProvider: "qwen" });
assert.equal(needs.cleanup, "qwen3-8b-q4_k_m");
- assert.equal(needs.stopServer, false);
+ assert.deepEqual(needs.models, ["qwen3-8b-q4_k_m"]);
});
test("a switched-off scope needs no server even with a local model selected", async () => {
@@ -40,7 +55,7 @@ test("a switched-off scope needs no server even with a local model selected", as
assert.deepEqual(resolveLocalServerNeeds({ ...LOCAL_CLEANUP, useCleanupModel: false }), {
cleanup: "",
dictationAgent: "",
- stopServer: true,
+ models: [],
});
});
@@ -50,17 +65,16 @@ test("local mode without a downloaded model pre-warms nothing", async () => {
const needs = resolveLocalServerNeeds({ ...LOCAL_CLEANUP, cleanupModel: " " });
assert.equal(needs.cleanup, "");
- assert.equal(needs.stopServer, true);
+ assert.deepEqual(needs.models, []);
});
test("the shared server survives one scope leaving while the other stays local", async () => {
const { resolveLocalServerNeeds } = await load();
const needs = resolveLocalServerNeeds({
- useCleanupModel: true,
+ ...NOTHING_LOCAL,
cleanupMode: "providers",
cleanupModel: "gpt-5-mini",
- useDictationAgent: true,
dictationAgentMode: "local",
dictationAgentModel: "gemma-4-e4b-it-q4_k_m",
});
@@ -68,23 +82,109 @@ test("the shared server survives one scope leaving while the other stays local",
assert.deepEqual(needs, {
cleanup: "",
dictationAgent: "gemma-4-e4b-it-q4_k_m",
- stopServer: false,
+ models: ["gemma-4-e4b-it-q4_k_m"],
});
});
-test("the server stops once no scope runs locally", async () => {
+test("the server has no consumer once no scope runs locally", async () => {
const { resolveLocalServerNeeds } = await load();
for (const mode of ["openwhispr", "providers", "self-hosted", "enterprise"]) {
const needs = resolveLocalServerNeeds({
- useCleanupModel: true,
+ ...NOTHING_LOCAL,
+ useDictationTranslation: true,
cleanupMode: mode,
cleanupModel: "gpt-5-mini",
- useDictationAgent: true,
dictationAgentMode: mode,
dictationAgentModel: "gpt-5-mini",
+ noteFormattingMode: mode,
+ noteFormattingModel: "gpt-5-mini",
+ chatAgentMode: mode,
+ chatAgentModel: "gpt-5-mini",
+ translationMode: mode,
+ translationModel: "gpt-5-mini",
});
- assert.deepEqual(needs, { cleanup: "", dictationAgent: "", stopServer: true }, mode);
+ assert.deepEqual(needs, { cleanup: "", dictationAgent: "", models: [] }, mode);
}
});
+
+// Before the shared rule, only cleanup and the Voice Assistant counted, so
+// moving cleanup to the cloud stopped a server that typed chat, note
+// formatting or translation was still using.
+for (const [scope, overrides] of [
+ ["chat", { chatAgentMode: "local", chatAgentModel: "qwen3-8b-q4_k_m" }],
+ ["note formatting", { noteFormattingMode: "local", noteFormattingModel: "qwen3-8b-q4_k_m" }],
+ [
+ "translation",
+ {
+ useDictationTranslation: true,
+ translationMode: "local",
+ translationModel: "qwen3-8b-q4_k_m",
+ },
+ ],
+]) {
+ test(`a local ${scope} scope still needs the server after cleanup leaves`, async () => {
+ const { resolveLocalServerNeeds } = await load();
+
+ const needs = resolveLocalServerNeeds({ ...NOTHING_LOCAL, ...overrides });
+
+ assert.deepEqual(needs.models, ["qwen3-8b-q4_k_m"]);
+ // Pre-warm targets stay limited to the two startup scopes.
+ assert.equal(needs.cleanup, "");
+ assert.equal(needs.dictationAgent, "");
+ });
+}
+
+test("translation switched off needs no server even with a local model selected", async () => {
+ const { resolveLocalServerNeeds } = await load();
+
+ const needs = resolveLocalServerNeeds({
+ ...NOTHING_LOCAL,
+ translationMode: "local",
+ translationModel: "qwen3-8b-q4_k_m",
+ });
+
+ assert.deepEqual(needs.models, []);
+});
+
+test("scopes sharing a model list it once", async () => {
+ const { resolveLocalServerNeeds } = await load();
+
+ const needs = resolveLocalServerNeeds({
+ ...LOCAL_CLEANUP,
+ useDictationAgent: true,
+ dictationAgentMode: "local",
+ dictationAgentModel: "qwen3-8b-q4_k_m",
+ chatAgentMode: "local",
+ chatAgentModel: "gemma-4-e4b-it-q4_k_m",
+ });
+
+ assert.deepEqual(needs.models, ["qwen3-8b-q4_k_m", "gemma-4-e4b-it-q4_k_m"]);
+});
+
+test("the server keeps running while its loaded model is still needed", async () => {
+ const { shouldStopLocalServer } = await load();
+
+ const needs = { models: ["qwen3-8b-q4_k_m", "gemma-4-e4b-it-q4_k_m"] };
+
+ assert.equal(shouldStopLocalServer(needs, "gemma-4-e4b-it-q4_k_m"), false);
+});
+
+test("the server stops when no scope needs the model it has loaded", async () => {
+ const { shouldStopLocalServer } = await load();
+
+ // The next request for the remaining local model reloads the server anyway,
+ // so holding the stale model only keeps its memory in use.
+ assert.equal(
+ shouldStopLocalServer({ models: ["qwen3-8b-q4_k_m"] }, "gemma-4-e4b-it-q4_k_m"),
+ true
+ );
+});
+
+test("with no model loaded, the server stops only when nothing is needed", async () => {
+ const { shouldStopLocalServer } = await load();
+
+ assert.equal(shouldStopLocalServer({ models: [] }, null), true);
+ assert.equal(shouldStopLocalServer({ models: ["qwen3-8b-q4_k_m"] }, null), false);
+});
diff --git a/test/helpers/localServerSyncIpc.test.js b/test/helpers/localServerSyncIpc.test.js
new file mode 100644
index 000000000..4cb1406fb
--- /dev/null
+++ b/test/helpers/localServerSyncIpc.test.js
@@ -0,0 +1,143 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const Module = require("node:module");
+
+const handlersModulePath = require.resolve("../../src/helpers/ipcHandlers");
+const originalLoad = Module._load;
+const handlers = new Map();
+
+const MODEL = "qwen3.5-4b-q4_k_m";
+const modelManager = {
+ currentServerModelId: null,
+ stops: 0,
+ getServerStatus: () => ({ running: true }),
+ stopServer: async () => {
+ modelManager.stops += 1;
+ },
+};
+
+const electronStub = {
+ app: {
+ getPath: () => "",
+ getName: () => "test",
+ getVersion: () => "0.0.0",
+ isPackaged: false,
+ on: () => {},
+ requestSingleInstanceLock: () => true,
+ },
+ ipcMain: {
+ handle: (channel, handler) => handlers.set(channel, handler),
+ on: () => {},
+ removeHandler: () => {},
+ },
+ net: { fetch: async () => ({ ok: true, status: 200, json: async () => ({}) }) },
+ BrowserWindow: class BrowserWindow {
+ static getAllWindows() {
+ return [];
+ }
+
+ static fromWebContents() {
+ return null;
+ }
+ },
+ shell: {},
+ dialog: {},
+ screen: { getPrimaryDisplay: () => ({ workAreaSize: { width: 0, height: 0 } }) },
+ systemPreferences: { getMediaAccessStatus: () => "granted" },
+ session: { fromPartition: () => ({}) },
+ clipboard: {},
+ nativeImage: {},
+ globalShortcut: {},
+ utilityProcess: {},
+ MessageChannelMain: class {},
+};
+
+Module._load = function loadWithStubs(request, parent, isMain) {
+ if (request === "electron") return electronStub;
+ if (parent?.filename === handlersModulePath && request === "./debugLogger") {
+ return new Proxy({}, { get: () => () => {} });
+ }
+ if (parent?.filename === handlersModulePath && request === "./modelManagerBridge") {
+ return { default: modelManager };
+ }
+ return originalLoad.call(this, request, parent, isMain);
+};
+
+function anything() {
+ return new Proxy(function () {}, {
+ get: (_target, property) => {
+ if (property === Symbol.toPrimitive || property === "toString") return () => "";
+ if (property === "then") return undefined;
+ return anything();
+ },
+ apply: () => anything(),
+ });
+}
+
+let target;
+test.before(() => {
+ delete require.cache[handlersModulePath];
+ const IPCHandlers = require(handlersModulePath);
+ const Ctor = IPCHandlers.default || IPCHandlers;
+ target = { signedIn: false, envKeys: [] };
+ target._syncStartupEnv = (setVars, clearVars) => {
+ target.envKeys = [...Object.keys(setVars), ...clearVars];
+ };
+ target._hasActiveAccountScope = () => target.signedIn;
+ Ctor.prototype.setupHandlers.call(
+ new Proxy(target, {
+ get: (value, property) => (property in value ? value[property] : anything()),
+ })
+ );
+ assert.ok(handlers.get("sync-startup-preferences"), "sync handler must be registered");
+});
+
+test.after(() => {
+ Module._load = originalLoad;
+});
+
+function sync({ loaded, signedIn = false, policySettled = true, ...scopes }) {
+ modelManager.currentServerModelId = loaded;
+ modelManager.stops = 0;
+ target.signedIn = signedIn;
+ return handlers.get("sync-startup-preferences")(
+ {},
+ {
+ useLocalWhisper: false,
+ useCleanupModel: true,
+ cleanupMode: "openwhispr",
+ useDictationAgent: true,
+ dictationAgentMode: "openwhispr",
+ noteFormattingMode: "openwhispr",
+ chatAgentMode: "openwhispr",
+ useDictationTranslation: false,
+ translationMode: "openwhispr",
+ policySettled,
+ ...scopes,
+ }
+ );
+}
+
+test("a window sync keeps the server while another scope still needs its model", async () => {
+ await sync({ loaded: MODEL, chatAgentMode: "local", chatAgentModel: MODEL });
+ assert.equal(modelManager.stops, 0);
+});
+
+test("a window sync stops the server once no scope needs its model", async () => {
+ await sync({ loaded: MODEL, chatAgentMode: "local", chatAgentModel: "gemma-4-e2b" });
+ assert.equal(modelManager.stops, 1);
+});
+
+test("a signed-in window waits for its workspace policy before touching the server", async () => {
+ await sync({ loaded: MODEL, signedIn: true, policySettled: false });
+ assert.equal(modelManager.stops, 0);
+ assert.equal(target.envKeys.includes("CLEANUP_PROVIDER"), false, "no pre-warm change either");
+
+ await sync({ loaded: MODEL, signedIn: true, policySettled: true });
+ assert.equal(modelManager.stops, 1);
+});
+
+test("signed out, the policy never loads, so an unresolved policy does not block the stop", async () => {
+ await sync({ loaded: MODEL, signedIn: false, policySettled: false });
+ assert.equal(modelManager.stops, 1);
+});
diff --git a/test/helpers/policyRules.test.js b/test/helpers/policyRules.test.js
index 97d8d0da0..cfea4542b 100644
--- a/test/helpers/policyRules.test.js
+++ b/test/helpers/policyRules.test.js
@@ -1015,6 +1015,18 @@ test("the enterprise transcription tile is offered only to a managed policy snap
);
});
+test("a failed policy fetch is settled, but idle and loading are not", async () => {
+ const { isPolicySettled } = await load();
+ const snapshot = (status) => ({ status, policy: null, appVersion: null });
+
+ for (const status of ["managed", "unmanaged", "error"]) {
+ assert.equal(isPolicySettled(snapshot(status)), true, status);
+ }
+ for (const status of ["idle", "loading"]) {
+ assert.equal(isPolicySettled(snapshot(status)), false, status);
+ }
+});
+
test("the local-history policy is resolved only once the fetch has settled", async () => {
const { isLocalHistoryPolicyResolved } = await load();
const snapshot = (status) => ({ status, policy: null, appVersion: null });
diff --git a/test/hooks/localServerSyncWiring.test.js b/test/hooks/localServerSyncWiring.test.js
new file mode 100644
index 000000000..5968158dc
--- /dev/null
+++ b/test/hooks/localServerSyncWiring.test.js
@@ -0,0 +1,83 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const React = require("react");
+const {
+ createRendererServer,
+ installBrowserGlobals,
+ installHookDom,
+} = require("../lib/rendererTestHarness");
+
+// The main process decides the shared llama-server from what each window
+// syncs, so this pins the renderer half: the policy-settled flag it sends and
+// the resync after a sign-out that leaves the policy idle. SSR cannot run
+// useEffect, so the settings provider is mounted for real.
+async function mountSettings(t) {
+ // Registered before the browser globals so it runs before their teardown.
+ const rootRef = { current: null };
+ t.after(async () => {
+ if (rootRef.current) await React.act(async () => rootRef.current.unmount());
+ });
+
+ const syncs = [];
+ const listeners = {};
+ const known = {
+ syncStartupPreferences: async (prefs) => {
+ syncs.push(prefs);
+ },
+ // Startup seeds the agent name into the dictionary. Left pending, the write
+ // never reaches its cloud push, which imports a module after teardown.
+ applyDictionaryChanges: () => new Promise(() => {}),
+ onActiveAccountScopeChanged: (callback) => {
+ listeners.scope = callback;
+ return () => {
+ delete listeners.scope;
+ };
+ },
+ };
+ // Every other bridge call the provider makes is irrelevant here.
+ const electronAPI = new Proxy(known, {
+ get: (target, name) =>
+ name in target
+ ? target[name]
+ : String(name).startsWith("on")
+ ? () => () => {}
+ : async () => undefined,
+ });
+
+ installBrowserGlobals(t, { window: { electronAPI } });
+ const container = installHookDom(t);
+ const vite = await createRendererServer(t, {
+ cachePrefix: "openwhispr-local-server-sync-wiring-test-",
+ });
+ const [{ SettingsProvider }, { usePolicyStore }] = await Promise.all([
+ vite.ssrLoadModule("/hooks/useSettings.ts"),
+ vite.ssrLoadModule("/stores/policyStore.ts"),
+ ]);
+
+ const { createRoot } = require("react-dom/client");
+ const root = createRoot(container);
+ await React.act(async () => root.render(React.createElement(SettingsProvider, null, null)));
+ rootRef.current = root;
+
+ return { syncs, listeners, usePolicyStore };
+}
+
+test("a window reports its policy as unsettled until the fetch lands", async (t) => {
+ const { syncs, usePolicyStore } = await mountSettings(t);
+
+ assert.equal(syncs.at(-1).policySettled, false, "idle is not settled");
+
+ await React.act(async () => usePolicyStore.setState({ status: "unmanaged" }));
+ assert.equal(syncs.at(-1).policySettled, true);
+});
+
+test("a sign-out resyncs even though the policy stays idle", async (t) => {
+ const { syncs, listeners } = await mountSettings(t);
+ const before = syncs.length;
+
+ await React.act(async () => listeners.scope({ accountId: "a", authGeneration: 1 }));
+ assert.equal(syncs.length, before, "a sign-in waits for its policy instead");
+
+ await React.act(async () => listeners.scope(null));
+ assert.equal(syncs.length, before + 1);
+});
diff --git a/test/stores/localServerPrefs.test.js b/test/stores/localServerPrefs.test.js
new file mode 100644
index 000000000..d2c6fac7e
--- /dev/null
+++ b/test/stores/localServerPrefs.test.js
@@ -0,0 +1,122 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const path = require("node:path");
+const { createRendererServer, installBrowserGlobals } = require("../lib/rendererTestHarness");
+
+const MODEL = "qwen3.5-4b-q4_k_m";
+
+const UNMANAGED = { status: "unmanaged", policy: null, appVersion: null };
+
+const ALL_CLOUD = {
+ _llmScopeKeysMigrated: "1",
+ _dictationAgentSeeded: "1",
+ cleanupMode: "openwhispr",
+ dictationAgentMode: "openwhispr",
+ noteFormattingMode: "openwhispr",
+ chatAgentMode: "openwhispr",
+ translationMode: "openwhispr",
+};
+
+async function loadStore(t, initialStorage, cachePrefix) {
+ installBrowserGlobals(t, { initialStorage });
+ const vite = await createRendererServer(t, {
+ cachePrefix,
+ resolveAlias: { "@": path.resolve(__dirname, "../../src") },
+ });
+ await vite.ssrLoadModule("/models/ModelRegistry.ts");
+ const store = await vite.ssrLoadModule("/stores/settingsStore.ts");
+ const policy = await vite.ssrLoadModule("/helpers/localServerPolicy.js");
+ return { ...store, ...policy };
+}
+
+test("typed chat on a local model keeps the server once cleanup moves to the cloud", async (t) => {
+ const s = await loadStore(
+ t,
+ {
+ ...ALL_CLOUD,
+ cleanupMode: "local",
+ cleanupProvider: "qwen",
+ cleanupModel: MODEL,
+ chatAgentMode: "local",
+ chatAgentProvider: "qwen",
+ chatAgentModel: MODEL,
+ },
+ "openwhispr-local-server-prefs-chat-test-"
+ );
+
+ s.setResolvedLLMConfig("dictationCleanup", { mode: "openwhispr", cloudMode: "openwhispr" });
+ const needs = s.resolveLocalServerNeeds(
+ s.selectLocalServerPrefs(s.useSettingsStore.getState(), UNMANAGED)
+ );
+
+ assert.deepEqual(needs.models, [MODEL]);
+ assert.equal(s.shouldStopLocalServer(needs, MODEL), false);
+});
+
+test("note formatting counts the local model it inherits from cleanup", async (t) => {
+ const s = await loadStore(
+ t,
+ {
+ ...ALL_CLOUD,
+ cleanupMode: "local",
+ cleanupProvider: "qwen",
+ cleanupModel: MODEL,
+ noteFormattingMode: "local",
+ },
+ "openwhispr-local-server-prefs-notes-test-"
+ );
+
+ const prefs = s.selectLocalServerPrefs(s.useSettingsStore.getState(), UNMANAGED);
+
+ assert.equal(prefs.noteFormattingMode, "local");
+ assert.equal(prefs.noteFormattingModel, MODEL);
+});
+
+test("the server stops once the last local scope leaves", async (t) => {
+ const s = await loadStore(
+ t,
+ { ...ALL_CLOUD, dictationAgentMode: "local", dictationAgentModel: MODEL },
+ "openwhispr-local-server-prefs-last-test-"
+ );
+
+ s.setResolvedLLMConfig("dictationAgent", { mode: "openwhispr", cloudMode: "openwhispr" });
+ const needs = s.resolveLocalServerNeeds(
+ s.selectLocalServerPrefs(s.useSettingsStore.getState(), UNMANAGED)
+ );
+
+ assert.deepEqual(needs.models, []);
+ assert.equal(s.shouldStopLocalServer(needs, MODEL), true);
+});
+
+test("a policy that forbids local inference reports the clamped mode", async (t) => {
+ const s = await loadStore(
+ t,
+ { ...ALL_CLOUD, cleanupMode: "local", cleanupProvider: "qwen", cleanupModel: MODEL },
+ "openwhispr-local-server-prefs-policy-test-"
+ );
+ const cloudOnly = {
+ status: "managed",
+ policy: {
+ version: 1,
+ transcription: { allowedModes: ["openwhispr"], allowedByokProviders: [] },
+ llm: {
+ allowedModes: ["openwhispr"],
+ allowedByokProviders: [],
+ allowedEnterpriseProviders: [],
+ },
+ features: { agentEnabled: true, webSearchEnabled: true, screenContextEnabled: true },
+ sharing: { externalLinkSharing: "allowed" },
+ dataRetention: {
+ audioRetentionMaxDays: null,
+ localHistoryMode: "user_choice",
+ cloudBackupAllowed: true,
+ },
+ minAppVersion: null,
+ },
+ };
+
+ const prefs = s.selectLocalServerPrefs(s.useSettingsStore.getState(), cloudOnly);
+
+ assert.notEqual(prefs.cleanupMode, "local");
+ assert.deepEqual(s.resolveLocalServerNeeds(prefs).models, []);
+});