Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: preserve active tuning when daemon is unreachable during context reload (#6200)

[._.]/ Adam Eivy9df10d81ba80
diff --git a/server/services/ollamaManager.js b/server/services/ollamaManager.js
index 25bacfae31..b88fe9c7f8 100644
--- a/server/services/ollamaManager.js
+++ b/server/services/ollamaManager.js
@@ -640,9 +640,20 @@ async function getRuntimeContextLength(selectedModel = null) {
async function ensureContextWindow(contextLength, selectedModel = null) {
const target = Number(contextLength) > 0 ? Math.floor(Number(contextLength)) : null
if (!target) return { applied: false, reason: 'not-configured', contextLength: null }
+
+ // Compose the context-window env ON TOP OF the currently applied launch env
+ // instead of replacing it, so a reload preserves knobs (e.g.
+ // OLLAMA_FLASH_ATTENTION, OLLAMA_KV_CACHE_TYPE) that are not about the window.
+ // `appliedLaunchEnvValues` outlives `appliedLaunchEnv` specifically to answer
+ // "what has PortOS put in front of Ollama that has not been cleared yet", so
+ // an active tuning is preserved even if the daemon was temporarily down.
+ // When a tuning is active mid-sweep, preserving its knobs keeps the sweep's
+ // measurements comparable rather than demoting the daemon to untuned
+ // mid-sweep, while allowing the context window to expand for the harness.
+ const baseEnv = appliedLaunchEnvValues ? { ...appliedLaunchEnvValues } : {}
+ const env = withOllamaContextEnv(baseEnv, target)
+
if (!(await checkOllamaAvailable(true))) {
- const baseEnv = appliedLaunchEnv !== null && appliedLaunchEnvValues ? { ...appliedLaunchEnvValues } : {}
- const env = withOllamaContextEnv(baseEnv, target)
return { ...(await restartWithEnv(env, { tuning: false })), contextLength: target }
}
@@ -673,15 +684,6 @@ async function ensureContextWindow(contextLength, selectedModel = null) {
console.log(`🪟 Reloading Ollama at a ${target}-token context window (was ${runtime ?? 'unknown'})`)
- // Compose the context-window env ON TOP OF the currently applied launch env
- // instead of replacing it, so a reload preserves knobs (e.g.
- // OLLAMA_FLASH_ATTENTION, OLLAMA_KV_CACHE_TYPE) that are not about the window.
- // When a tuning is active mid-sweep, preserving its knobs keeps the sweep's
- // measurements comparable rather than demoting the daemon to untuned
- // mid-sweep, while allowing the context window to expand for the harness.
- const baseEnv = appliedLaunchEnv !== null && appliedLaunchEnvValues ? { ...appliedLaunchEnvValues } : {}
- const env = withOllamaContextEnv(baseEnv, target)
-
// The restart ladder itself — including the rule that a launch-at-login daemon
// is restarted in place rather than un-registered — lives in `restartWithEnv`.
// The checks ABOVE are what is specific to a context window: a resident model
diff --git a/server/services/ollamaManager.test.js b/server/services/ollamaManager.test.js
index 505f7c6df5..7e43fa0b02 100644
--- a/server/services/ollamaManager.test.js
+++ b/server/services/ollamaManager.test.js
@@ -1237,6 +1237,29 @@ describe('ollamaManager.restartWithEnv', () => {
expect(calls).toContain('launchctl setenv OLLAMA_CONTEXT_LENGTH 131072')
})
+ it('preserves an active tuning when ensureContextWindow starts an offline daemon', async () => {
+ const state = { up: false, probesBeforeStop: 0 }
+ stubReachable({ get reachable() { return state.up || state.probesBeforeStop-- > 0 } })
+ const spawn = await stubSpawnRestart(state)
+ const { ensureContextWindow, restartWithEnv } = await loadManager()
+
+ await restartWithEnv({ OLLAMA_FLASH_ATTENTION: '1', OLLAMA_KV_CACHE_TYPE: 'q8_0' })
+
+ // Daemon goes down
+ state.up = false
+ state.probesBeforeStop = 0
+
+ // Calling ensureContextWindow when the daemon is unreachable should still
+ // preserve the tuning knobs and start the daemon with them
+ const result = await ensureContextWindow(131072)
+ expect(result).toMatchObject({ applied: true, contextLength: 131072 })
+
+ const lastSpawn = spawn.mock.calls[spawn.mock.calls.length - 1]
+ expect(lastSpawn[2].env.OLLAMA_FLASH_ATTENTION).toBe('1')
+ expect(lastSpawn[2].env.OLLAMA_KV_CACHE_TYPE).toBe('q8_0')
+ expect(lastSpawn[2].env.OLLAMA_CONTEXT_LENGTH).toBe('131072')
+ })
+
// The domain outlives the daemon, so a stopped Ollama is no reason to leave
// the variables in it — the next login-launched one would inherit them.
it('unsets exported variables even when Ollama is already down', async () => {