Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(core): silence the host memory leg on darwin (freemem lies there)

enes4392789e00bf
diff --git a/src/core/memory-pressure.test.ts b/src/core/memory-pressure.test.ts
index ffd5c34bc..b78ce8b43 100644
--- a/src/core/memory-pressure.test.ts
+++ b/src/core/memory-pressure.test.ts
@@ -1,5 +1,6 @@
import { describe, it, expect, vi } from 'vitest'
-import { createMemoryPressureMonitor } from './memory-pressure'
+import { createMemoryPressureMonitor, hostMemReader } from './memory-pressure'
+import { readMemInfo } from './session-budget'
const mem = (availableMb: number, totalMb = 16000) => () => ({ availableMb, totalMb })
@@ -49,6 +50,20 @@ describe('memory pressure monitor', () => {
expect(on).toHaveBeenCalledTimes(1)
vi.useRealTimers()
})
+ it('the default host reader is SILENT on darwin (os.freemem there is vm_stat free_count)', () => {
+ const read = hostMemReader('darwin')
+ expect(read()).toBeNull()
+ // Host leg silent → a healthy Mac is never classified from "free" memory…
+ const quiet = createMemoryPressureMonitor({ readMem: read, selfRssMb: () => 500, onPressure: vi.fn() })
+ expect(quiet.check()).toBeNull()
+ // …but the self-RSS leg still carries the monitor on its own.
+ const loud = createMemoryPressureMonitor({ readMem: read, selfRssMb: () => 9000, onPressure: vi.fn() })
+ expect(loud.check()).toBe('critical')
+ })
+ it('the default host reader is the real one everywhere else', () => {
+ expect(hostMemReader('linux')).toBe(readMemInfo)
+ expect(hostMemReader('win32')).toBe(readMemInfo)
+ })
it('a throwing consumer does not break the monitor', () => {
vi.useFakeTimers()
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
diff --git a/src/core/memory-pressure.ts b/src/core/memory-pressure.ts
index 953e45ec4..d9018cb3c 100644
--- a/src/core/memory-pressure.ts
+++ b/src/core/memory-pressure.ts
@@ -1,6 +1,7 @@
// Two-signal memory pressure (cmux's MemoryPressureMonitor pattern, Electron-free): host
-// available-memory watermarks (readMemInfo — the same source session-budget trusts) plus this
-// process's own RSS, because the host signal alone can't tell you YOU are the problem.
+// available-memory watermarks (readMemInfo — the same source session-budget trusts, except on
+// darwin, where it is not measuring what its name says; see hostMemReader) plus this process's
+// own RSS, because the host signal alone can't tell you YOU are the problem.
// Consumers hang reclaim levers off onPressure; all levers must be idempotent — the monitor
// re-fires a held severity at most once per RE_FIRE_FLOOR_MS.
import { readMemInfo, type MemInfo } from './session-budget'
@@ -12,6 +13,24 @@ export const RE_FIRE_FLOOR_MS = 60_000
export const SELF_RSS_WARN_MB = 4096
export const SELF_RSS_CRIT_MB = 8192
+/**
+ * The DEFAULT host-memory reader — platform-aware, because `readMemInfo`'s non-Linux fallback is
+ * `os.freemem()`, and on **darwin** that is vm_stat's `free_count` alone: it excludes inactive,
+ * purgeable and compressor pages, all of which macOS hands back on demand. A perfectly healthy
+ * 16 GB Mac idles at a few hundred MB "free", i.e. under BOTH watermarks — the monitor would sit
+ * permanently CRITICAL on the primary desktop platform, disposing every parked terminal and
+ * sweeping the reaper once a minute forever. Per the repo rule, a guess must degrade to NOTHING
+ * rather than to something wrong: on darwin the host leg is silent (`null` is explicitly "no
+ * pressure signal", not "no pressure") and the self-RSS leg carries the monitor alone.
+ *
+ * Follow-up: give macOS a REAL signal (the memory-pressure/compressor state, swap-in rate, or
+ * Electron's `app.getAppMetrics`) and drop the special case — the platform is not un-measurable,
+ * `os.freemem()` is simply the wrong instrument for it.
+ */
+export function hostMemReader(platform: NodeJS.Platform = process.platform): () => MemInfo | null {
+ return platform === 'darwin' ? (): null => null : readMemInfo
+}
+
export interface MemoryPressureMonitor {
start(): void
stop(): void
@@ -25,8 +44,10 @@ export function createMemoryPressureMonitor(opts: {
intervalMs?: number
onPressure: (s: PressureSeverity) => void
}): MemoryPressureMonitor {
- const readMem = opts.readMem ?? readMemInfo
- const selfRss = opts.selfRssMb ?? ((): number => Math.round(process.memoryUsage().rss / 1048576))
+ const readMem = opts.readMem ?? hostMemReader()
+ // `memoryUsage.rss()` reads just the one number; `memoryUsage()` builds the whole stats object
+ // (heap walk included) every 30 s for a field we throw away.
+ const selfRss = opts.selfRssMb ?? ((): number => Math.round(process.memoryUsage.rss() / 1048576))
let timer: ReturnType<typeof setInterval> | null = null
let lastFired = 0