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): a failed vm_stat is no signal, not a fall back to os.freemem()

enes5d80b1aa4856
diff --git a/docs/session-memory.md b/docs/session-memory.md
index d6a347dc3..8abd8e6ac 100644
--- a/docs/session-memory.md
+++ b/docs/session-memory.md
@@ -148,6 +148,16 @@ pill.** `sessionBudgetConfig`'s watermark defaults to 10% of RAM (2457 MB on a 2
reaped idle detached sessions every 10 minutes regardless of how much memory was actually free. One
reader, two consumers: that is the reason they share a definition.
+**A failed `vm_stat` yields NO SIGNAL, not a fallback.** `darwinMemInfo` returns `null` rather than
+dropping through to `os.freemem()` — that value is exactly what caused the bug, so falling back to it
+would restore it on any Mac where `vm_stat` is missing, slow or unparseable. Both consumers degrade
+correctly on `null`: `planReap` treats it as no pressure (absence of evidence never triggers a kill)
+and the pill pulses instead of printing a number it has not earned.
+
+**Confirmed in the field.** The reaper symptom was reported independently as "my sessions keep
+disappearing" on macOS, before the cause was known. Until this ships, the workaround on an affected
+machine is the reaper's own kill switch: `NODETERM_SESSION_REAP_DISABLED=1`.
+
**Unverified:** the fixture in `session-memory.test.ts` is composed from Apple's documented format,
not captured from a real machine. Device checklist item 6 is what closes it.
diff --git a/src/core/session-memory.test.ts b/src/core/session-memory.test.ts
index af54ffd9b..bfbe4131d 100644
--- a/src/core/session-memory.test.ts
+++ b/src/core/session-memory.test.ts
@@ -237,7 +237,7 @@ describe('collectSessionMemory', () => {
})
})
-import { parseVmStat } from './session-memory'
+import { parseVmStat, darwinMemInfo } from './session-memory'
/** Shaped from Apple's documented `vm_stat` format, at a 16 KiB page (Apple Silicon). COMPOSED, not
* captured — nobody in this loop has a Mac. Numbers chosen so the arithmetic is checkable by hand:
@@ -292,3 +292,25 @@ describe('parseVmStat', () => {
expect(tiny?.availableMb).toBe(0)
})
})
+
+describe('darwinMemInfo', () => {
+ it('reports null when vm_stat cannot run — never the os.freemem() value', () => {
+ // The whole point. Falling back to os.freemem() here would restore the bug: on macOS it reads
+ // near zero, the reaper's 10%-of-RAM watermark reads as permanent pressure, and idle detached
+ // sessions get reaped every sweep. A confirmed field symptom ("my sessions keep disappearing").
+ // planReap treats null as NO pressure, so no signal is the safe answer.
+ expect(
+ darwinMemInfo(() => {
+ throw new Error('spawn vm_stat ENOENT')
+ }, TOTAL_BYTES)
+ ).toBeNull()
+ })
+
+ it('reports null on output it cannot parse', () => {
+ expect(darwinMemInfo(() => 'garbage', TOTAL_BYTES)).toBeNull()
+ })
+
+ it('passes a good reading through', () => {
+ expect(darwinMemInfo(() => VM_STAT, TOTAL_BYTES)).toEqual({ availableMb: 8951, totalMb: 24576 })
+ })
+})
diff --git a/src/core/session-memory.ts b/src/core/session-memory.ts
index fd1dc91a6..a49104e20 100644
--- a/src/core/session-memory.ts
+++ b/src/core/session-memory.ts
@@ -105,6 +105,26 @@ export function parseProcessTable(stdout: string): ProcEntry[] {
*
* Lives here rather than in session-budget.ts because two features now read it (the reaper's
* watermark and the system-resource pill) and a second copy would drift. */
+/**
+ * macOS reading, or `null`. **There is deliberately no fallback to `os.freemem()` here.**
+ *
+ * That fallback is the very number this function exists to replace: on macOS it sits near zero, and
+ * the session reaper's watermark (10% of RAM) then reads as permanent memory pressure — which had a
+ * Mac reaping idle detached sessions every 10 minutes regardless of how much memory was free. A
+ * confirmed field symptom, reported as "my sessions keep disappearing".
+ *
+ * So a `vm_stat` we cannot run or cannot parse yields NO SIGNAL, not a wrong one. Both consumers
+ * degrade correctly on null: `planReap` treats it as "no pressure" (absence of evidence never
+ * triggers a kill) and the pill pulses instead of printing a number it has not earned.
+ */
+export function darwinMemInfo(runVmStat: () => string, totalBytes: number): MemInfo | null {
+ try {
+ return parseVmStat(runVmStat(), totalBytes)
+ } catch {
+ return null
+ }
+}
+
/**
* Parse `vm_stat` into the same MemInfo shape, given the machine's total bytes.
*
@@ -166,14 +186,10 @@ export function readMemInfo(): MemInfo | null {
// (the 30 s pill poll and the reaper's 10 min sweep) are far apart, and keeping ONE signature
// means the reaper's watermark is fixed by the same change.
if (process.platform === 'darwin') {
- try {
- const out = execFileSync('vm_stat', { encoding: 'utf8', timeout: 5_000 })
- const parsed = parseVmStat(out, os.totalmem())
- if (parsed) return parsed
- } catch {
- // vm_stat missing or unreadable — fall through to the generic reader below, which is wrong on
- // macOS but is still better than reporting nothing at all.
- }
+ return darwinMemInfo(
+ () => execFileSync('vm_stat', { encoding: 'utf8', timeout: 5_000 }),
+ os.totalmem()
+ )
}
try {
return {