Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(models): F3 Nachbesserung 6 - der verdraengte Anbieter behaelt seinen Schluessel

purple7cedea5002fe
diff --git a/src/api/providers/types.ts b/src/api/providers/types.ts
index 9fe113e1..d41a184a 100644
--- a/src/api/providers/types.ts
+++ b/src/api/providers/types.ts
@@ -49,6 +49,19 @@ export interface ProviderConfig {
// then reads DISABLED instead of STANDBY, which is the button that was
// pressed (Nebenbefund 3, R12/R13 re-measure 2026-08-30).
disabledByUser?: boolean
+ // Opus-Review Nachbesserung 6 (3.0.1, F3): the pushed-out backend's own
+ // API key, so a takeover does not just stop LEAKING it into the new
+ // occupant's field (the original F3 fix) but also stops DESTROYING it —
+ // handing the slot back used to come back with no key and a silent 401.
+ // Same "obfuscated" representation `apiKey` itself carries, an opaque
+ // blob this app never needs to read as text outside providerStore.ts.
+ // Session-only on purpose: providerStore.ts's `partialize` strips this
+ // field unconditionally before every persist, because there is no vault
+ // entry reserved for a PARKED backend (the OS keychain has exactly one
+ // slot per ProviderId, already spoken for by whichever backend is
+ // active). A key parked here survives Enable/Disable within the running
+ // session; it does not survive a restart, same as before this fix.
+ apiKey?: string
}
}
diff --git a/src/components/settings/ProviderConfig.tsx b/src/components/settings/ProviderConfig.tsx
index 4422f86a..e3d416f4 100644
--- a/src/components/settings/ProviderConfig.tsx
+++ b/src/components/settings/ProviderConfig.tsx
@@ -1,6 +1,6 @@
import { useState, useEffect, useRef } from 'react'
import { Wifi, WifiOff, Loader2, Eye, EyeOff, ChevronDown, Plus, Power, Play, Trash2 } from 'lucide-react'
-import { useProviderStore } from '../../stores/providerStore'
+import { useProviderStore, deobfuscate } from '../../stores/providerStore'
import { providerRowIds, isReturnableRow } from '../../lib/provider-visibility'
import {
slotTakeoverUpdate,
@@ -241,6 +241,20 @@ export function ProviderSettings() {
run()
}
+ // Opus-Review Nachbesserung 6 (3.0.1, F3): every path below that hands the
+ // shared `openai` slot to a REMEMBERED backend (Remove on the occupant,
+ // Enable on the standby card, Disable swapping it back in) must restore
+ // that backend's own parked key too, not just its name/URL. Through
+ // setProviderApiKey, not a plain field merge: that is the call that keeps
+ // the OS keychain in step with the store, and it has to run AFTER
+ // setProviderConfig so it is the last word on `openai`'s key field. No
+ // parked key (an older `displaced` record, written before this fix, or a
+ // backend that never had one) clears it instead of leaving the key of
+ // whichever backend just left sitting there under a new name.
+ function restoreParkedApiKey(parkedObfuscated: string | undefined) {
+ setProviderApiKey('openai', parkedObfuscated !== undefined ? deobfuscate(parkedObfuscated) : '')
+ }
+
// Remove on the backend that holds the shared local slot: the slot goes back
// to what it held before the takeover, and the removed backend is forgotten
// instead of parked on standby. Offered only where `displaced` knows a state
@@ -249,7 +263,9 @@ export function ProviderSettings() {
function removeOccupant() {
const update = slotRemoveOccupantUpdate(providers.openai)
if (!update) return
+ const parked = providers.openai.displaced?.apiKey
setProviderConfig('openai', update)
+ restoreParkedApiKey(parked)
setStatuses(prev => ({ ...prev, openai: 'idle' }))
setExpandedProvider('openai')
}
@@ -269,7 +285,9 @@ export function ProviderSettings() {
function handBackSlot() {
const update = slotHandbackUpdate(providers.openai)
if (!update) return
+ const parked = providers.openai.displaced?.apiKey
setProviderConfig('openai', update)
+ restoreParkedApiKey(parked)
setStatuses(prev => ({ ...prev, openai: 'idle' }))
setExpandedProvider('openai')
}
@@ -343,7 +361,9 @@ export function ProviderSettings() {
if (!nextEnabled && id === 'openai') {
const handback = slotDisableOccupantUpdate(providers.openai)
if (handback) {
+ const parked = providers.openai.displaced?.apiKey
setProviderConfig('openai', handback)
+ restoreParkedApiKey(parked)
setStatuses(prev => ({ ...prev, openai: 'idle' }))
return
}
diff --git a/src/lib/__tests__/builtin-card-survives-add-provider.test.ts b/src/lib/__tests__/builtin-card-survives-add-provider.test.ts
index 65f07404..83454bbf 100644
--- a/src/lib/__tests__/builtin-card-survives-add-provider.test.ts
+++ b/src/lib/__tests__/builtin-card-survives-add-provider.test.ts
@@ -241,4 +241,44 @@ describe('the wiring, so the rule reaches the screen', () => {
expect(end).toBeGreaterThan(start)
expect(pane.slice(start, end)).not.toMatch(/[–—]/)
})
+
+ // Opus-Review Nachbesserung 6 (3.0.1, F3): Enable on the standby card used
+ // to come back with no key and a silent 401 — the slot's name/URL were
+ // restored through slotHandbackUpdate, but nothing ever restored the
+ // parked backend's own apiKey. Every path that hands the slot to a
+ // REMEMBERED backend must read `displaced.apiKey` and push it through
+ // setProviderApiKey (store + keychain), not just merge the plain patch.
+ it('handBackSlot restores the parked key through setProviderApiKey, not a plain merge', () => {
+ const start = pane.indexOf('function handBackSlot()')
+ const end = pane.indexOf('\n }', start)
+ const body = pane.slice(start, end)
+ expect(body).toMatch(/const parked = providers\.openai\.displaced\?\.apiKey/)
+ expect(body).toMatch(/restoreParkedApiKey\(parked\)/)
+ // Reads the parked value BEFORE overwriting `providers.openai` with the
+ // handback patch — read-after-write here would read the NEW (wrong) slot.
+ expect(body.indexOf('const parked')).toBeLessThan(body.indexOf("setProviderConfig('openai', update)"))
+ })
+
+ it('removeOccupant restores the parked key too, same as Enable on standby', () => {
+ const start = pane.indexOf('function removeOccupant()')
+ const end = pane.indexOf('\n }', start)
+ const body = pane.slice(start, end)
+ expect(body).toMatch(/const parked = providers\.openai\.displaced\?\.apiKey/)
+ expect(body).toMatch(/restoreParkedApiKey\(parked\)/)
+ })
+
+ it('Disable swapping the built-in engine back in restores the parked key too', () => {
+ const start = pane.indexOf('const handback = slotDisableOccupantUpdate(providers.openai)')
+ const end = pane.indexOf('\n }', start)
+ const body = pane.slice(start, end)
+ expect(body).toMatch(/const parked = providers\.openai\.displaced\?\.apiKey/)
+ expect(body).toMatch(/restoreParkedApiKey\(parked\)/)
+ })
+
+ it('restoreParkedApiKey decodes the parked value and clears when nothing was parked', () => {
+ const start = pane.indexOf('function restoreParkedApiKey(')
+ const end = pane.indexOf('\n }', start)
+ const body = pane.slice(start, end)
+ expect(body).toMatch(/setProviderApiKey\('openai', parkedObfuscated !== undefined \? deobfuscate\(parkedObfuscated\) : ''\)/)
+ })
})
diff --git a/src/lib/__tests__/openai-slot-handover-api-key.test.ts b/src/lib/__tests__/openai-slot-handover-api-key.test.ts
index 738c9d41..319eec63 100644
--- a/src/lib/__tests__/openai-slot-handover-api-key.test.ts
+++ b/src/lib/__tests__/openai-slot-handover-api-key.test.ts
@@ -10,7 +10,7 @@
* Run: npx vitest run src/lib/__tests__/openai-slot-handover-api-key.test.ts
*/
import { describe, it, expect } from 'vitest'
-import { takeoverClearsApiKey, type HandoverSlot } from '../openai-slot-handover'
+import { takeoverClearsApiKey, slotTakeoverUpdate, slotHandbackUpdate, type HandoverSlot } from '../openai-slot-handover'
const jan: HandoverSlot = {
enabled: true, name: 'Jan', baseUrl: 'http://localhost:1337/v1', isLocal: true, managed: false,
@@ -39,3 +39,54 @@ describe('F3 — a slot takeover clears the inherited API key', () => {
expect(takeoverClearsApiKey(builtin, incoming)).toBe(true)
})
})
+
+/**
+ * Opus-Review Nachbesserung 6 (3.0.1, F3): closing the leak by clearing the
+ * key on takeover was only half the fix — the DISPLACED backend's own key
+ * has to survive somewhere, or handing the slot back later comes with no key
+ * and a silent 401. `displaced.apiKey` is that somewhere; these tests pin the
+ * pure carry-through (the actual restore into the store + keychain is
+ * ProviderConfig.tsx's job, see das there and background-shutdown-adjacent
+ * tests for the store-level partialize stripping).
+ */
+describe('Nachbesserung 6 — the displaced backend keeps its own key', () => {
+ it('a real takeover remembers the OUTGOING backend key in displaced.apiKey', () => {
+ const janWithKey: HandoverSlot = { ...jan, apiKey: 'obf-jan-key' }
+ const incoming = { name: 'My Server', baseUrl: 'http://localhost:5001/v1', isLocal: true, managed: false }
+ const patch = slotTakeoverUpdate(janWithKey, incoming)
+ expect(patch.displaced?.apiKey).toBe('obf-jan-key')
+ })
+
+ it('re-selecting the same backend does not create a displaced record at all, key included', () => {
+ const janWithKey: HandoverSlot = { ...jan, apiKey: 'obf-jan-key' }
+ const incoming = { name: jan.name, baseUrl: jan.baseUrl, isLocal: jan.isLocal, managed: jan.managed }
+ const patch = slotTakeoverUpdate(janWithKey, incoming)
+ expect(patch.displaced).toBeUndefined()
+ })
+
+ it('slotHandbackUpdate hands the parked key back out through standbyOccupant', () => {
+ const slotWithDisplacedKey: HandoverSlot = {
+ enabled: true, name: 'Jan', baseUrl: 'http://localhost:1337/v1', isLocal: true, managed: false, apiKey: 'obf-jan-active',
+ displaced: { name: 'Built-in Engine', baseUrl: 'http://127.0.0.1:8127/v1', isLocal: true, managed: true, apiKey: 'obf-builtin-parked' },
+ }
+ const update = slotHandbackUpdate(slotWithDisplacedKey)
+ // The base patch itself never carries an apiKey field — ProviderConfig.tsx
+ // reads the parked value straight off `providers.openai.displaced.apiKey`
+ // BEFORE calling this, exactly so a plain merge of this patch cannot
+ // corrupt the store's separately-obfuscated apiKey field (see the
+ // handBackSlot/removeOccupant comment there for why).
+ expect(update?.apiKey).toBeUndefined()
+ // And the backend now being pushed OUT (Jan) gets its own key parked in
+ // turn, so a second handback later still has something to give back.
+ expect(update?.displaced?.apiKey).toBe('obf-jan-active')
+ })
+
+ it('a backend with no key parks nothing, and a handback restores nothing (not a stale one)', () => {
+ const slotNoKey: HandoverSlot = {
+ enabled: true, name: 'Jan', baseUrl: 'http://localhost:1337/v1', isLocal: true, managed: false,
+ displaced: { name: 'Built-in Engine', baseUrl: 'http://127.0.0.1:8127/v1', isLocal: true, managed: true },
+ }
+ const update = slotHandbackUpdate(slotNoKey)
+ expect(update?.displaced?.apiKey).toBeUndefined()
+ })
+})
diff --git a/src/lib/openai-slot-handover.ts b/src/lib/openai-slot-handover.ts
index 93fa0a19..537d0b0f 100644
--- a/src/lib/openai-slot-handover.ts
+++ b/src/lib/openai-slot-handover.ts
@@ -61,6 +61,17 @@ export interface SlotOccupant {
* the card can tell them apart, because the slot itself looks identical.
*/
disabledByUser?: boolean
+ /**
+ * Opus-Review Nachbesserung 6 (3.0.1, F3): carried through untouched, the
+ * same obfuscated representation `ProviderConfig.apiKey` itself uses. This
+ * module never decodes it — it only moves the value between the slot and
+ * its `displaced` memory, so a takeover stops destroying the pushed-out
+ * backend's key along with clearing the leak the original F3 fix closed.
+ * See ProviderConfig.tsx's `applyPreset`/`handBackSlot` for the actual
+ * store-and-keychain restore, which needs the plain key and therefore
+ * happens OUTSIDE this pure module.
+ */
+ apiKey?: string
}
/** The part of the `openai` slot this decision reads. */
@@ -138,6 +149,11 @@ export function slotTakeoverUpdate(
baseUrl: slot.baseUrl,
isLocal: slot.isLocal,
managed: slot.managed,
+ // Nachbesserung 6: the outgoing backend's own key travels with it now,
+ // instead of being left to rot in the slot's field for the incoming
+ // backend to inherit (that leak is what `takeoverClearsApiKey` closes)
+ // or simply vanishing the moment something else takes the slot.
+ apiKey: slot.apiKey,
},
}
}
diff --git a/src/stores/__tests__/providerStore.keychain.test.ts b/src/stores/__tests__/providerStore.keychain.test.ts
index 4d46f16a..29dff375 100644
--- a/src/stores/__tests__/providerStore.keychain.test.ts
+++ b/src/stores/__tests__/providerStore.keychain.test.ts
@@ -158,4 +158,46 @@ describe('providerStore keychain (H5)', () => {
expect(raw).toContain(obf('sk-fail-fallback')) // retained as fallback, NOT stripped
expect(useStore.getState().getProviderApiKey('anthropic')).toBe('sk-fail-fallback')
})
+
+ // Opus-Review Nachbesserung 6 (3.0.1, F3): a parked `displaced.apiKey` has
+ // no vault entry of its own (the OS keychain holds one credential per
+ // ProviderId, already spoken for by whichever backend is active), so it
+ // must never reach localStorage in the clear — on ANY platform, keychain
+ // active or not, unlike the active `apiKey` field which at least gets the
+ // localStorage fallback when the vault write fails.
+ it('a parked displaced.apiKey never reaches persisted localStorage, keychain active', async () => {
+ secretGet.mockResolvedValue(null)
+ secretSet.mockResolvedValue(undefined)
+ const useStore = await freshStore()
+ await useStore.getState().hydrateProviderKeys() // keychainReady = true
+
+ useStore.getState().setProviderConfig('openai', {
+ displaced: { name: 'Built-in Engine', baseUrl: 'http://127.0.0.1:8127/v1', isLocal: true, managed: true, apiKey: obf('sk-parked-secret') },
+ })
+
+ const raw = localStorage.getItem('lu-providers') || ''
+ expect(raw).not.toContain('sk-parked-secret')
+ expect(raw).not.toContain(obf('sk-parked-secret'))
+ // The rest of the parked record (name/baseUrl/managed) is not a secret
+ // and still needs to survive a restart — only the key is stripped.
+ expect(raw).toContain('Built-in Engine')
+ })
+
+ it('a parked displaced.apiKey never reaches persisted localStorage, no keychain on this platform either', async () => {
+ // Never probed / probe found nothing usable — keychainReady stays false,
+ // the same path Linux and the web build take. The ACTIVE apiKey field
+ // keeps its localStorage fallback here (unchanged behavior); the parked
+ // one still must not, because there is no vault to have "failed" into.
+ secretGet.mockRejectedValue(new Error('no vault on this platform'))
+ const useStore = await freshStore()
+ await useStore.getState().hydrateProviderKeys() // keychainReady stays false
+
+ useStore.getState().setProviderConfig('openai', {
+ displaced: { name: 'Jan', baseUrl: 'http://localhost:1337/v1', isLocal: true, apiKey: obf('sk-parked-linux') },
+ })
+
+ const raw = localStorage.getItem('lu-providers') || ''
+ expect(raw).not.toContain('sk-parked-linux')
+ expect(raw).not.toContain(obf('sk-parked-linux'))
+ })
})
diff --git a/src/stores/providerStore.ts b/src/stores/providerStore.ts
index 014d7bcb..4c2fa6b0 100644
--- a/src/stores/providerStore.ts
+++ b/src/stores/providerStore.ts
@@ -38,7 +38,12 @@ function obfuscate(key: string): string {
}
}
-function deobfuscate(encoded: string): string {
+// Exported for ProviderConfig.tsx's handback/removal paths (Opus-Review
+// Nachbesserung 6): `displaced.apiKey` carries the same obfuscated form this
+// function decodes for every other read of a provider's key, and restoring
+// it into the active slot has to go through `setProviderApiKey` (store AND
+// keychain), which takes the PLAIN key.
+export function deobfuscate(encoded: string): string {
if (!encoded) return ''
try {
return atob(encoded).split('').reverse().join('')
@@ -393,15 +398,24 @@ export const useProviderStore = create<ProviderState>()(
// When the OS keychain is active (H5), strip apiKey so the secret never
// touches localStorage; otherwise keep the obfuscated key as before.
partialize: (state) => ({
- providers: keychainReady
- ? (Object.fromEntries(
- Object.entries(state.providers).map(([id, p]) =>
- // Strip the key (it lives in the vault) UNLESS the vault write
- // failed for this id — then keep the obfuscated fallback.
- _keychainFailed.has(id as ProviderId) ? [id, p] : [id, { ...p, apiKey: '' }]
- )
- ) as Record<ProviderId, ProviderConfig>)
- : state.providers,
+ providers: Object.fromEntries(
+ Object.entries(state.providers).map(([id, p]) => {
+ // Strip the ACTIVE key (it lives in the vault) UNLESS the vault
+ // write failed for this id — then keep the obfuscated fallback.
+ const stripActive = keychainReady && !_keychainFailed.has(id as ProviderId)
+ const withActive = stripActive ? { ...p, apiKey: '' } : p
+ // Opus-Review Nachbesserung 6: a PARKED key on the `displaced`
+ // memory has no vault entry of its own — the OS keychain holds
+ // exactly one credential per ProviderId, already spoken for by
+ // whichever backend is active. Persisting it in the clear would
+ // put a secret into localStorage with none of the protection
+ // `apiKey` itself gets, so it never survives a persist, on any
+ // platform. It still restores correctly within the same running
+ // session (ProviderConfig.tsx reads it before this ever runs).
+ if (!withActive.displaced?.apiKey) return [id, withActive]
+ return [id, { ...withActive, displaced: { ...withActive.displaced, apiKey: undefined } }]
+ })
+ ) as Record<ProviderId, ProviderConfig>,
hideBackendSelector: state.hideBackendSelector,
}),
}