Skip to content

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

fix(models): clear stale local model selections after model removal (#1312)

ambbone7753f61f3d6b
diff --git a/src/components/SettingsPage.tsx b/src/components/SettingsPage.tsx
index 51c885848..fc447f348 100644
--- a/src/components/SettingsPage.tsx
+++ b/src/components/SettingsPage.tsx
@@ -100,7 +100,7 @@ import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
import { startMigration, useMigration } from "../stores/noteStore.js";
import { syncService } from "../services/SyncService.js";
import { formatBytes } from "../utils/formatBytes";
-import { useSettingsStore } from "../stores/settingsStore";
+import { clearMissingLocalModelSelections, useSettingsStore } from "../stores/settingsStore";
import { canManageSystemAudioInApp } from "../utils/systemAudioAccess";
import WorkspaceSection from "./settings/WorkspaceSection";
import { WORKSPACES_ENABLED } from "../lib/features";
@@ -1283,6 +1283,8 @@ export default function SettingsPage({
description: t("settingsPage.developer.removeModels.failedDescription"),
});
} else {
+ // Every local model is gone, so no local selection can still resolve.
+ clearMissingLocalModelSelections(() => false);
window.dispatchEvent(new Event("openwhispr-models-cleared"));
showAlertDialog({
title: t("settingsPage.developer.removeModels.successTitle"),
diff --git a/src/helpers/localModelSelections.js b/src/helpers/localModelSelections.js
new file mode 100644
index 000000000..796c16727
--- /dev/null
+++ b/src/helpers/localModelSelections.js
@@ -0,0 +1,12 @@
+// Store keys of every inference scope pointing at a local model that is no
+// longer installed. Only "local" scopes are candidates — in every other mode the
+// selected id names a remote model, which the local model cache knows nothing
+// about. `scopes` is Object.values(INFERENCE_SCOPES).
+export function findStaleLocalModelKeys(scopes, settings, isInstalled) {
+ return scopes
+ .filter(({ storeKeys }) => {
+ const selected = settings[storeKeys.model];
+ return settings[storeKeys.mode] === "local" && !!selected && !isInstalled(selected);
+ })
+ .map(({ storeKeys }) => storeKeys.model);
+}
diff --git a/src/helpers/noteFormattingOverrides.js b/src/helpers/noteFormattingOverrides.js
index 1b8e62edd..87dd4139a 100644
--- a/src/helpers/noteFormattingOverrides.js
+++ b/src/helpers/noteFormattingOverrides.js
@@ -22,7 +22,15 @@ export function buildNoteFormattingOverrides(noteFormatting, isCloudMode, custom
};
}
- const provider = mode === "providers" ? noteFormatting?.provider || undefined : undefined;
+ // Local must pin its provider for the same reason: an empty local selection
+ // resolves to the cleanup scope's model, and processText would derive a cloud
+ // provider from that id — sending note content off-device.
+ const provider =
+ mode === "local"
+ ? "local"
+ : mode === "providers"
+ ? noteFormatting?.provider || undefined
+ : undefined;
const isCustom = provider === "custom";
return {
provider,
diff --git a/src/hooks/useModelDownload.ts b/src/hooks/useModelDownload.ts
index 58fb53628..344613172 100644
--- a/src/hooks/useModelDownload.ts
+++ b/src/hooks/useModelDownload.ts
@@ -10,6 +10,7 @@ import type {
WhisperDownloadProgressData,
WhisperModelResult,
} from "../types/electron";
+import { clearMissingLocalModelSelections } from "../stores/settingsStore";
import "../types/electron";
const PROGRESS_THROTTLE_MS = 100;
@@ -523,7 +524,11 @@ export function useModelDownload({
});
}
} else {
- await window.electronAPI?.modelDelete?.(modelId);
+ // model-delete reports failure by resolving, not throwing — leaving the
+ // model on disk, so the scopes pointing at it must stay untouched.
+ const result = await window.electronAPI?.modelDelete?.(modelId);
+ if (!result?.success) throw new Error(result?.error ?? "");
+ clearMissingLocalModelSelections((id) => id !== modelId);
toast({
title: t("hooks.modelDownload.modelDeleted.title"),
description: t("hooks.modelDownload.modelDeleted.description"),
diff --git a/src/stores/settingsStore.ts b/src/stores/settingsStore.ts
index d605dc0cb..b0acb390d 100644
--- a/src/stores/settingsStore.ts
+++ b/src/stores/settingsStore.ts
@@ -10,6 +10,7 @@ import type { LocalTranscriptionProvider, InferenceMode, SelfHostedType } from "
import type { GoogleCalendarAccount } from "../types/calendar";
import { PROMPT_KIND_LIST, type PromptKind } from "../config/prompts/registry";
import { deriveReasoningMode, buildReasoningScopePatches } from "../helpers/reasoningRouting";
+import { findStaleLocalModelKeys } from "../helpers/localModelSelections";
import {
INFERENCE_SCOPES,
type InferenceScope,
@@ -2089,6 +2090,32 @@ export function getSettings() {
return useSettingsStore.getState();
}
+/**
+ * Drops any local model selection the model cache no longer backs — a scope left
+ * pointing at a deleted model fails at inference time with an error the user has
+ * no way to act on. Cleared rather than repointed so the picker asks again.
+ */
+export function clearMissingLocalModelSelections(isInstalled: (modelId: string) => boolean): void {
+ const settings = useSettingsStore.getState() as unknown as Record<string, unknown>;
+ const staleKeys: string[] = findStaleLocalModelKeys(
+ Object.values(INFERENCE_SCOPES),
+ settings,
+ isInstalled
+ );
+ for (const key of staleKeys) {
+ setStringSetting(key as keyof SettingsState, "");
+ }
+}
+
+/** Reconciles every scope against the models actually on disk. */
+export async function reconcileLocalModelSelections(): Promise<void> {
+ if (!isBrowser || !window.electronAPI?.modelGetAll) return;
+
+ const models = await window.electronAPI.modelGetAll();
+ const installed = new Set(models.filter((model) => model.isDownloaded).map((model) => model.id));
+ clearMissingLocalModelSelections((modelId) => installed.has(modelId));
+}
+
export function getEffectiveCleanupModel() {
const state = useSettingsStore.getState();
if (selectIsCloudCleanupMode(state)) {
@@ -2448,6 +2475,16 @@ export async function initializeSettings(): Promise<void> {
);
}
+ try {
+ await reconcileLocalModelSelections();
+ } catch (err) {
+ logger.warn(
+ "Failed to reconcile local model selections on startup",
+ { error: (err as Error).message },
+ "settings"
+ );
+ }
+
// Only after a successful DB↔cache reconcile. If the read failed, the cache
// may still be stale — writing it via setCustomDictionary would wipe SQLite.
if (dictionarySyncSucceeded) {
diff --git a/test/helpers/localModelSelections.test.js b/test/helpers/localModelSelections.test.js
new file mode 100644
index 000000000..30e8bf4e8
--- /dev/null
+++ b/test/helpers/localModelSelections.test.js
@@ -0,0 +1,59 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+const load = () => import("../../src/helpers/localModelSelections.js");
+
+const SCOPES = [
+ { storeKeys: { mode: "cleanupMode", model: "cleanupModel" } },
+ { storeKeys: { mode: "chatAgentMode", model: "chatAgentModel" } },
+];
+
+const installed = (...ids) => {
+ const set = new Set(ids);
+ return (id) => set.has(id);
+};
+
+test("flags a local scope whose model is gone", async () => {
+ const { findStaleLocalModelKeys } = await load();
+ const settings = {
+ cleanupMode: "local",
+ cleanupModel: "qwen2.5-3b",
+ chatAgentMode: "local",
+ chatAgentModel: "llama-3.2-3b",
+ };
+ assert.deepEqual(findStaleLocalModelKeys(SCOPES, settings, installed("llama-3.2-3b")), [
+ "cleanupModel",
+ ]);
+});
+
+test("leaves installed models alone", async () => {
+ const { findStaleLocalModelKeys } = await load();
+ const settings = { cleanupMode: "local", cleanupModel: "qwen2.5-3b" };
+ assert.deepEqual(findStaleLocalModelKeys(SCOPES, settings, installed("qwen2.5-3b")), []);
+});
+
+test("ignores scopes that are not on local mode", async () => {
+ const { findStaleLocalModelKeys } = await load();
+ const settings = { cleanupMode: "providers", cleanupModel: "gpt-5.5" };
+ assert.deepEqual(findStaleLocalModelKeys(SCOPES, settings, installed()), []);
+});
+
+test("ignores a scope with nothing selected", async () => {
+ const { findStaleLocalModelKeys } = await load();
+ const settings = { cleanupMode: "local", cleanupModel: "" };
+ assert.deepEqual(findStaleLocalModelKeys(SCOPES, settings, installed()), []);
+});
+
+test("flags every local scope sharing the removed model", async () => {
+ const { findStaleLocalModelKeys } = await load();
+ const settings = {
+ cleanupMode: "local",
+ cleanupModel: "qwen2.5-3b",
+ chatAgentMode: "local",
+ chatAgentModel: "qwen2.5-3b",
+ };
+ assert.deepEqual(findStaleLocalModelKeys(SCOPES, settings, installed()), [
+ "cleanupModel",
+ "chatAgentModel",
+ ]);
+});
diff --git a/test/helpers/noteFormattingOverrides.test.js b/test/helpers/noteFormattingOverrides.test.js
index 44067f4c3..090285da7 100644
--- a/test/helpers/noteFormattingOverrides.test.js
+++ b/test/helpers/noteFormattingOverrides.test.js
@@ -72,7 +72,7 @@ test("providers with a first-party cloud provider passes provider only, no key/b
});
});
-test("local mode passes no provider overrides (uses model-derived local provider)", async () => {
+test("local mode pins the local provider and leaks no key", async () => {
const { buildNoteFormattingOverrides } = await load();
const overrides = buildNoteFormattingOverrides(
{ mode: "local", model: "qwen2.5-3b" },
@@ -80,9 +80,16 @@ test("local mode passes no provider overrides (uses model-derived local provider
"irrelevant"
);
assert.deepEqual(overrides, {
- provider: undefined,
+ provider: "local",
baseUrl: undefined,
customApiKey: undefined,
lanUrl: undefined,
});
});
+
+test("local mode with no model stays local (regression: inherited cloud id hit the cloud)", async () => {
+ const { buildNoteFormattingOverrides } = await load();
+ const overrides = buildNoteFormattingOverrides({ mode: "local", model: "" }, false, "sk-cloud");
+ assert.equal(overrides.provider, "local");
+ assert.equal(overrides.customApiKey, undefined);
+});