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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(daemons): address review findings for deadband hysteresis and keepLoaded
[._.]/ Adam Eivy00899acd99ac
diff --git a/server/lib/managedDaemon.js b/server/lib/managedDaemon.jsindex 44cc1ad787..54dab0a029 100644--- a/server/lib/managedDaemon.js+++ b/server/lib/managedDaemon.js@@ -400,13 +400,21 @@ export function evaluateMemoryPressurePolicy({}const free = memoryStats.free;- if (free >= thresholdBytes) {+ const wasUnderPressure = Boolean(options.wasUnderPressure ?? (lastReleasedAt && (now - lastReleasedAt < calmDownMs * 2)));+ const exitThresholdBytes = thresholdBytes + deadBandBytes;+ const isRelieved = wasUnderPressure+ ? free >= exitThresholdBytes+ : free >= thresholdBytes;++ if (isRelieved) {return {shouldRelease: false,target: null,reason: 'host memory not under pressure',free,thresholdBytes,+ exitThresholdBytes,+ wasUnderPressure,};}@@ -536,7 +544,7 @@ export async function reapIdleDaemons(now = Date.now(), options = {}) {const daemonList = [];for (const [name, entry] of idleDaemons) {- const isPinned = await Promise.resolve(entry.isPinned?.()).catch(() => false);+ const isPinned = await Promise.resolve(entry.isPinned?.()).catch(() => true);const isRunning = entry.isRunning? await Promise.resolve(entry.isRunning()).catch(() => false): true;diff --git a/server/lib/managedDaemon.pressure.test.js b/server/lib/managedDaemon.pressure.test.jsindex 1c67fd1848..44f6a63bc0 100644--- a/server/lib/managedDaemon.pressure.test.js+++ b/server/lib/managedDaemon.pressure.test.js@@ -52,17 +52,39 @@ describe('managedDaemon memory pressure policy', () => {expect(result.reason).toBe('memory stats unavailable');});- it('returns shouldRelease: false when free memory is above threshold', () => {+ it('returns shouldRelease: false when memory is above threshold', () => {const result = evaluateMemoryPressurePolicy({daemons: [{ name: 'mtplx', lastUsedAt: now - 10_000 }],memoryStats: { total: 64 * GB, used: 50 * GB, free: 14 * GB },- history: [{ at: now, free: 14 * GB }],now,});expect(result.shouldRelease).toBe(false);expect(result.reason).toBe('host memory not under pressure');});+ it('stays under pressure when within dead-band hysteresis window', () => {+ // Threshold is 4GB, dead-band is 2GB, exit threshold is 6GB+ // When wasUnderPressure is true, 5GB free is still under pressure+ const resultInDeadBand = evaluateMemoryPressurePolicy({+ daemons: [{ name: 'mtplx', lastUsedAt: now - 10_000 }],+ memoryStats: { total: 64 * GB, used: 59 * GB, free: 5 * GB },+ history: [{ at: now - 40_000, free: 2 * GB }, { at: now, free: 5 * GB }],+ options: { wasUnderPressure: true, sustainedDurationMs: 0 },+ now,+ });+ expect(resultInDeadBand.shouldRelease).toBe(true);++ // Above threshold + dead-band (7GB >= 6GB), pressure is relieved+ const resultAboveDeadBand = evaluateMemoryPressurePolicy({+ daemons: [{ name: 'mtplx', lastUsedAt: now - 10_000 }],+ memoryStats: { total: 64 * GB, used: 57 * GB, free: 7 * GB },+ options: { wasUnderPressure: true },+ now,+ });+ expect(resultAboveDeadBand.shouldRelease).toBe(false);+ expect(resultAboveDeadBand.reason).toBe('host memory not under pressure');+ });+it('returns shouldRelease: false when within the calm-down window', () => {const result = evaluateMemoryPressurePolicy({daemons: [{ name: 'mtplx', lastUsedAt: now - 10_000 }],@@ -250,6 +272,26 @@ describe('managedDaemon memory pressure policy', () => {expect(daemonReleaseReason('daemon-pinned')).toBeNull();});+ it('fail-safes to pinned when isPinned throws or rejects', async () => {+ const stop = vi.fn().mockResolvedValue(undefined);+ registerIdleDaemon({+ name: 'daemon-flaky-pin',+ getIdleMs: () => 60 * MINUTE,+ isPinned: () => Promise.reject(new Error('transient read failure')),+ stop,+ });++ const now = Date.now();+ const stopped = await reapIdleDaemons(now, {+ memoryStats: { total: 64 * GB, used: 62 * GB, free: 2 * GB },+ history: [{ at: now - 35_000, free: 2 * GB }, { at: now, free: 2 * GB }],+ sustainedDurationMs: 30_000,+ });++ expect(stopped).toEqual([]);+ expect(stop).not.toHaveBeenCalled();+ });+it('clears releaseReason when markDaemonUsed is called upon server restart', async () => {const stop = vi.fn().mockResolvedValue(undefined);registerIdleDaemon({ name: 'daemon-a', getIdleMs: () => 60 * MINUTE, stop });diff --git a/server/services/llamaServerManager.js b/server/services/llamaServerManager.jsindex 14ca50d08f..f00186fc40 100644--- a/server/services/llamaServerManager.js+++ b/server/services/llamaServerManager.js@@ -669,14 +669,15 @@ export async function startLlamaServer(options = {}) {// feature existed — never a rejected launch. `effectiveIdleMinutes` is what// actually reached the process, so the status card reports the truth rather// than the request.- const effectiveIdleMinutes = requestedIdleMinutes > 0 && await supportsSleepIdle(binaryPath)+ const isPinned = await configuredSleepIdleKeepLoaded();+ const effectiveIdleMinutes = !isPinned && requestedIdleMinutes > 0 && await supportsSleepIdle(binaryPath)? requestedIdleMinutes: 0;if (effectiveIdleMinutes > 0) args.push(SLEEP_IDLE_FLAG, String(effectiveIdleMinutes * 60));lastExitError = null;daemon.resetLogs();- if (requestedIdleMinutes > 0 && effectiveIdleMinutes === 0) {+ if (!isPinned && requestedIdleMinutes > 0 && effectiveIdleMinutes === 0) {appendLog(`This llama-server build has no ${SLEEP_IDLE_FLAG} — the model stays resident while idle`);}if (droppedSpecTypes.length > 0) {diff --git a/server/services/llamaServerManager.test.js b/server/services/llamaServerManager.test.jsindex 730f9da3b1..2aa128a1ad 100644--- a/server/services/llamaServerManager.test.js+++ b/server/services/llamaServerManager.test.js@@ -11,6 +11,7 @@ import {getLlamaServerUpdateStatus,upgradeLlamaServer,_resetLlamaServerStateForTests,+ _setLlamaKeepLoadedOverrideForTests,LLAMA_APP,} from './llamaServerManager.js';import * as processEnv from '../lib/processEnv.js';@@ -1527,6 +1528,15 @@ describe('llamaServerManager', () => {expect(startArgs()).not.toContain('--sleep-idle-seconds');});+ it('leaves the flag off when keepLoaded is configured', async () => {+ mockHelp('--sleep-idle-seconds SECONDS number of seconds of idleness');+ _setLlamaKeepLoadedOverrideForTests(true);++ await startLlamaServer({ model: modelPath, draftModel: null, specType: '', port: 8080, sleepIdleMinutes: 30 });++ expect(startArgs()).not.toContain('--sleep-idle-seconds');+ });+// The compatibility guarantee: an install on an older llama.cpp must keep// starting. Emitting an unknown flag would make the daemon exit immediately.it('omits the flag on a build that does not advertise it, rather than failing the start', async () => {