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

address review (agy): dead-band starvation, stale idle re-stop, pin fail-safe

[._.]/ Adam Eivyef7cd1287b5a
diff --git a/server/lib/managedDaemon.js b/server/lib/managedDaemon.js
index 54dab0a029..df84aac946 100644
--- a/server/lib/managedDaemon.js
+++ b/server/lib/managedDaemon.js
@@ -428,6 +428,11 @@ export function evaluateMemoryPressurePolicy({
}
if (sustainedDurationMs > 0) {
+ // Track "under pressure" against the same bar isRelieved just used above —
+ // when wasUnderPressure, that's the higher exit threshold, not the base
+ // threshold — otherwise samples sitting in the dead-band (below exitThresholdBytes
+ // but above thresholdBytes) never count as sustained and eviction starves.
+ const activeThresholdBytes = wasUnderPressure ? exitThresholdBytes : thresholdBytes;
const validSamples = (history || [])
.map((s) => ({
at: s.at ?? s.timestamp ?? s.time ?? now,
@@ -438,7 +443,7 @@ export function evaluateMemoryPressurePolicy({
let earliestUnderPressureAt = null;
for (let i = validSamples.length - 1; i >= 0; i--) {
- if (validSamples[i].free < thresholdBytes) {
+ if (validSamples[i].free < activeThresholdBytes) {
earliestUnderPressureAt = validSamples[i].at;
} else {
break;
@@ -501,9 +506,20 @@ export async function reapIdleDaemons(now = Date.now(), options = {}) {
// 1. Normal idle timeout pass
for (const [name, entry] of idleDaemons) {
- const isPinned = await Promise.resolve(entry.isPinned?.()).catch(() => false);
+ // Fail-safe to pinned on a transient read error, matching the pressure-aware
+ // pass below — assuming "not pinned" here would risk stopping a keepLoaded
+ // daemon on a flaky settings read.
+ const isPinned = await Promise.resolve(entry.isPinned?.()).catch(() => true);
if (isPinned) continue; // Pinned servers are exempt
+ // Already stopped (by the pressure-aware pass, or externally) — skip so this
+ // pass doesn't overwrite its release reason and doesn't preempt the
+ // pressure-aware pass below via the early return once its idle window re-elapses.
+ const isRunning = entry.isRunning
+ ? await Promise.resolve(entry.isRunning()).catch(() => true)
+ : true;
+ if (!isRunning) continue;
+
// Resolved per sweep, so lowering the window in Settings applies to the very
// next beat rather than to the next server restart.
const windowMs = await Promise.resolve(entry.getIdleMs()).catch(() => null);
diff --git a/server/lib/managedDaemon.pressure.test.js b/server/lib/managedDaemon.pressure.test.js
index 44f6a63bc0..edcd69a759 100644
--- a/server/lib/managedDaemon.pressure.test.js
+++ b/server/lib/managedDaemon.pressure.test.js
@@ -85,6 +85,26 @@ describe('managedDaemon memory pressure policy', () => {
expect(resultAboveDeadBand.reason).toBe('host memory not under pressure');
});
+ it('treats dead-band samples as sustained pressure when wasUnderPressure', () => {
+ // Threshold 4GB, dead-band 2GB, exit threshold 6GB. Free has held at 5GB —
+ // inside the dead-band (below the exit threshold, above the base threshold)
+ // — for well over the default 30s sustained window while wasUnderPressure.
+ const deadBandHistory = [
+ { at: now - 40_000, free: 5 * GB },
+ { at: now - 20_000, free: 5 * GB },
+ { at: now, free: 5 * GB },
+ ];
+ const result = evaluateMemoryPressurePolicy({
+ daemons: [{ name: 'mtplx', lastUsedAt: now - 10_000 }],
+ memoryStats: { total: 64 * GB, used: 59 * GB, free: 5 * GB },
+ history: deadBandHistory,
+ options: { wasUnderPressure: true },
+ now,
+ });
+ expect(result.shouldRelease).toBe(true);
+ expect(result.reason).toBe('host memory pressure');
+ });
+
it('returns shouldRelease: false when within the calm-down window', () => {
const result = evaluateMemoryPressurePolicy({
daemons: [{ name: 'mtplx', lastUsedAt: now - 10_000 }],
@@ -292,6 +312,42 @@ describe('managedDaemon memory pressure policy', () => {
expect(stop).not.toHaveBeenCalled();
});
+ it('idle-timeout pass skips an already-stopped daemon instead of re-stopping it', async () => {
+ let running = true;
+ const stop = vi.fn().mockImplementation(() => {
+ running = false;
+ return Promise.resolve();
+ });
+ registerIdleDaemon({
+ name: 'daemon-already-stopped',
+ getIdleMs: () => 5 * MINUTE,
+ isRunning: () => running,
+ stop,
+ });
+
+ const now1 = Date.now();
+ // Idle window (5m) hasn't elapsed since registration, so this falls through
+ // to the pressure-aware pass, which stops it and records why.
+ const stoppedByPressure = await reapIdleDaemons(now1, {
+ memoryStats: { total: 64 * GB, used: 62 * GB, free: 2 * GB },
+ history: [{ at: now1 - 40_000, free: 2 * GB }, { at: now1, free: 2 * GB }],
+ sustainedDurationMs: 30_000,
+ });
+ expect(stoppedByPressure).toEqual(['daemon-already-stopped']);
+ expect(stop).toHaveBeenCalledTimes(1);
+ const pressureReason = daemonReleaseReason('daemon-already-stopped');
+ expect(pressureReason).toContain('host memory pressure');
+
+ // The pressure stop reset lastUsedAt to now1 — advance past the idle
+ // window without the daemon ever restarting.
+ const now2 = now1 + 6 * MINUTE;
+ const stoppedByIdle = await reapIdleDaemons(now2, { memoryStats: false });
+
+ expect(stoppedByIdle).toEqual([]);
+ expect(stop).toHaveBeenCalledTimes(1);
+ expect(daemonReleaseReason('daemon-already-stopped')).toBe(pressureReason);
+ });
+
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 });