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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
perf(session-memory): read darwin swap and pressure in one sysctl call
review4702aea5a18c
diff --git a/CLAUDE.md b/CLAUDE.mdindex 35c36383c..2f4c86b4f 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -6542,8 +6542,8 @@ about which machine they describe. Reading + parsing is `core/session-memory.ts`never zero.- **The pill's swap figure and kernel-pressure tint live in `MemInfo.display`, and nothing but thepill reads it** (audit roadmap #14). darwin: `sysctl vm.swapusage` and- `kern.memorystatus_vm_pressure_level` (1/2/4 → normal/warn/critical), each a separate optional- read inside `darwinMemInfo`; Linux: copied from the same `/proc/meminfo` text. **Never fill+ `kern.memorystatus_vm_pressure_level` (1/2/4 → normal/warn/critical), both in ONE+ `/usr/sbin/sysctl` run inside `darwinMemInfo`, each field optional on its own; Linux: copied from the same `/proc/meminfo` text. **Never fill`swapTotalMb`/`swapFreeMb` on darwin** — `hostUnderMemoryPressure` reads them, and a Mac keepsits swap ~85% used while healthy (the audited one: 7.7 of 9 GB, level flapping 1↔2), so thereaper's swap term would fire all day. The level only RAISES the used-% tint, never lowers it;diff --git a/docs/session-memory.md b/docs/session-memory.mdindex 16ca699d2..c7eb135dd 100644--- a/docs/session-memory.md+++ b/docs/session-memory.md@@ -269,8 +269,10 @@ were swapped and the kernel sat at pressure level 2, and nothing on screen said- **Where.** `MemInfo.display` — `swapUsedMb`, `swapTotalMb`, `pressureLevel`. darwin reads`sysctl vm.swapusage` (`total = 9216.00M used = 7853.62M free = 1362.38M (encrypted)`, parsed by`parseSwapUsage`; K/M/G only) and `kern.memorystatus_vm_pressure_level` (`parsePressureLevel`:- 1 normal, 2 warn, 4 critical, anything else omitted). Each is its own optional read in- `darwinMemInfo`; a failure omits that one field and leaves the `vm_stat` reading untouched. Linux+ 1 normal, 2 warn, 4 critical, anything else omitted). Both come from ONE `/usr/sbin/sysctl` run+ in `darwinMemInfo` (one more synchronous process per 30 s pill read, beside `vm_stat`); each field+ is still optional on its own — an oid with no line omits that field, and a failed run leaves the+ `vm_stat` reading untouched. Linuxcopies its swap from the `/proc/meminfo` text already parsed. Linux has no kernel level; PSI isnot mapped to one because no threshold has been measured.- **Why a sub-object.** `hostUnderMemoryPressure` reads `swapTotalMb`/`swapFreeMb`. Filling those ondiff --git a/src/core/session-memory.test.ts b/src/core/session-memory.test.tsindex 76fc3d797..6353fb3dc 100644--- a/src/core/session-memory.test.ts+++ b/src/core/session-memory.test.ts@@ -593,15 +593,42 @@ describe('parsePressureLevel (darwin, roadmap #14)', () => {})describe('darwinMemInfo display extras', () => {- const sysctl = (answers: Record<string, string | Error>) => (name: string): string => {- const a = answers[name]- if (a === undefined || a instanceof Error) throw a ?? new Error(`unknown oid ${name}`)- return a+ /** One `sysctl <names…>` run: the known oids print their `name: value` lines, in order; an+ * unknown one makes the run exit non-zero, which the caller hands over as the partial stdout+ * (`readMemInfo`'s runner) — so here a run with no known oid at all throws. */+ const sysctl = (answers: Record<string, string>) => (names: string[]): string => {+ const lines = names.filter((n) => answers[n] !== undefined).map((n) => answers[n])+ if (lines.length === 0) throw new Error(`unknown oid ${names.join(' ')}`)+ return lines.join('\n') + '\n'}+ it('reads swap and pressure in ONE sysctl run, never one per field', () => {+ const runs: string[][] = []+ const both = sysctl({+ 'vm.swapusage': SWAPUSAGE.trimEnd(),+ 'kern.memorystatus_vm_pressure_level': 'kern.memorystatus_vm_pressure_level: 4'+ })+ const m = darwinMemInfo(() => VM_STAT, TOTAL_BYTES, (names) => {+ runs.push(names)+ return both(names)+ })+ expect(runs).toEqual([['vm.swapusage', 'kern.memorystatus_vm_pressure_level']])+ expect(m?.display).toEqual({ swapUsedMb: 7854, swapTotalMb: 9216, pressureLevel: 'critical' })+ })++ it('the one run answers each field from its own line, in either order', () => {+ const reversed = (): string =>+ 'kern.memorystatus_vm_pressure_level: 1\n' + SWAPUSAGE+ expect(darwinMemInfo(() => VM_STAT, TOTAL_BYTES, reversed)?.display).toEqual({+ swapUsedMb: 7854,+ swapTotalMb: 9216,+ pressureLevel: 'normal'+ })+ })+it('adds swap and pressure under `display` and NEVER fills the reaper pair', () => {const m = darwinMemInfo(() => VM_STAT, TOTAL_BYTES, sysctl({- 'vm.swapusage': SWAPUSAGE,+ 'vm.swapusage': SWAPUSAGE.trimEnd(),'kern.memorystatus_vm_pressure_level': 'kern.memorystatus_vm_pressure_level: 2'}))expect(m).toEqual({@@ -615,12 +642,12 @@ describe('darwinMemInfo display extras', () => {})it('a failed sysctl omits only its own field; both failing leaves the vm_stat reading untouched', () => {- const swapOnly = darwinMemInfo(() => VM_STAT, TOTAL_BYTES, sysctl({ 'vm.swapusage': SWAPUSAGE }))+ const swapOnly = darwinMemInfo(() => VM_STAT, TOTAL_BYTES, sysctl({ 'vm.swapusage': SWAPUSAGE.trimEnd() }))expect(swapOnly?.display).toEqual({ swapUsedMb: 7854, swapTotalMb: 9216 })const none = darwinMemInfo(() => VM_STAT, TOTAL_BYTES, sysctl({}))expect(none).toEqual({ availableMb: 8951, totalMb: 24576 })// vm_stat failing still wins: no reading at all, never a display-only one.- expect(darwinMemInfo(() => { throw new Error('no vm_stat') }, TOTAL_BYTES, sysctl({ 'vm.swapusage': SWAPUSAGE }))).toBeNull()+ expect(darwinMemInfo(() => { throw new Error('no vm_stat') }, TOTAL_BYTES, sysctl({ 'vm.swapusage': SWAPUSAGE.trimEnd() }))).toBeNull()})it.skipIf(process.platform !== 'darwin')('reads this Mac\'s real swap and pressure (darwin-only: sysctl oids)', () => {diff --git a/src/core/session-memory.ts b/src/core/session-memory.tsindex ab23ed20e..cd6d7ee07 100644--- a/src/core/session-memory.ts+++ b/src/core/session-memory.ts@@ -187,10 +187,14 @@ export function parseTopFootprint(stdout: string): Map<number, number> {* 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.*/+/** The oids `darwinMemInfo` reads, in ONE `sysctl` run. Without `-n`, so each answer carries its+ * own `name:` prefix and is matched by name rather than by line position. */+export const DARWIN_DISPLAY_SYSCTLS = ['vm.swapusage', 'kern.memorystatus_vm_pressure_level'] as const+export function darwinMemInfo(runVmStat: () => string,totalBytes: number,- runSysctl?: (name: string) => string+ runSysctl?: (names: string[]) => string): MemInfo | null {let base: MemInfo | nulltry {@@ -199,24 +203,48 @@ export function darwinMemInfo(return null}if (!base || !runSysctl) return base- // The pill's display extras (roadmap #14). Each is a SEPARATE optional read, the same shape as- // PSI on Linux: a failed sysctl omits its one field and costs the vm_stat reading nothing. Never- // `swapTotalMb`/`swapFreeMb` — see MemInfo.display for why darwin must not fill those.+ // The pill's display extras. Both oids come from ONE `sysctl` run, and each field is still+ // optional on its own, the same shape as PSI on Linux: an oid with no line (or a line that does+ // not parse) omits its one field, and a run that fails outright costs the vm_stat reading+ // nothing. Never `swapTotalMb`/`swapFreeMb` — see MemInfo.display for why darwin must not fill+ // those.const display: MemDisplay = {}+ let out = ''try {- Object.assign(display, parseSwapUsage(runSysctl('vm.swapusage')))+ out = runSysctl([...DARWIN_DISPLAY_SYSCTLS])} catch {- // no swap figure+ // no swap figure, no pressure tint}- try {- const level = parsePressureLevel(runSysctl('kern.memorystatus_vm_pressure_level'))- if (level) display.pressureLevel = level- } catch {- // no pressure tint+ for (const line of out.split('\n')) {+ if (line.startsWith('vm.swapusage:')) Object.assign(display, parseSwapUsage(line))+ else if (line.startsWith('kern.memorystatus_vm_pressure_level:')) {+ const level = parsePressureLevel(line)+ if (level) display.pressureLevel = level+ }}return Object.keys(display).length > 0 ? { ...base, display } : base}+/**+ * One bounded `sysctl` run for `darwinMemInfo`, by absolute path (a GUI app's PATH is not the+ * shell's). An oid the kernel does not know makes sysctl exit non-zero while still printing the+ * others, so a failure hands over whatever stdout it has: each field stays optional on its own.+ * stderr is discarded rather than inherited into main's.+ */+function runDarwinSysctl(names: string[]): string {+ try {+ return execFileSync('/usr/sbin/sysctl', names, {+ encoding: 'utf8',+ timeout: 5_000,+ stdio: ['ignore', 'pipe', 'ignore']+ })+ } catch (e) {+ const partial = (e as { stdout?: unknown } | null)?.stdout+ if (typeof partial === 'string' && partial) return partial+ throw e+ }+}+/*** Parse `sysctl vm.swapusage` (with or without the `vm.swapusage:` prefix). Live shape on the* audited Mac (2026-10-19):@@ -392,12 +420,13 @@ 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') {- // The two sysctls are sub-millisecond reads; they ride on every caller (the reaper too), which- // only ever ignores `display`.+ // The display extras cost one more synchronous process per call (both oids in one `sysctl`),+ // a fork/exec on main beside `vm_stat`. Their caller is the 30 s pill read: on darwin the+ // reaper's `hostMemReader` is null, so it never reaches this.return darwinMemInfo(() => execFileSync('vm_stat', { encoding: 'utf8', timeout: 5_000 }),os.totalmem(),- (name) => execFileSync('sysctl', [name], { encoding: 'utf8', timeout: 5_000 })+ runDarwinSysctl)}try {