Skip to content

fix(cli): compute macOS machine-health memory from vm_stat#990

Open
junmo-kim wants to merge 2 commits into
tiann:mainfrom
junmo-kim:fix/darwin-memory-percent
Open

fix(cli): compute macOS machine-health memory from vm_stat#990
junmo-kim wants to merge 2 commits into
tiann:mainfrom
junmo-kim:fix/darwin-memory-percent

Conversation

@junmo-kim

Copy link
Copy Markdown
Contributor

Problem

On macOS runners, the Machine capacity tooltip in the session sidebar shows "RAM in use" pinned near 99% and displays "High pressure — avoid spawning more here", even when the machine has plenty of headroom and macOS itself reports normal memory pressure.

The runner's computeMemoryPercent() (cli/src/utils/machineHealth.ts) has a dedicated /proc/meminfo branch for Linux but currently falls through to total - os.freemem() on every other platform. On macOS, os.freemem() counts only truly-free pages, and macOS keeps almost all physical memory populated with reclaimable file cache, so used / total sits at ~99% permanently regardless of real usage.

Solution

Add a platform() === 'darwin' branch that reads vm_stat and computes used memory the same way Activity Monitor's "Memory Used" does: App Memory + Wired + Compressed, i.e. the sum of anonymous, wired-down, and compressor-occupied pages. Reclaimable file cache stays out of the sum, so the figure tracks what a user sees in Activity Monitor.

This mirrors the intent of the existing Linux branch, whose total - MemAvailable is byte-identical to what free reports as "used" — reclaimable cache is not treated as pressure on either platform. The Linux branch is unchanged.

Details:

  • Page size is read from the vm_stat header (16 KB on Apple Silicon, 4 KB on Intel) rather than hardcoded.
  • execSync('vm_stat') runs with a 1 s timeout inside a try/catch; any failure or unparseable required field returns undefined and falls through to the existing os.freemem() path, so behavior is unchanged where vm_stat is unavailable.

Tests

  • Unit tests for the new pure readDarwinMemoryUsedPercent(vmStat, totalBytes) parser: a verbatim vm_stat capture from a 16 GB Mac mini (the pre-change total - freemem() path reports 99% for that same snapshot, while the new formula reports 79%, matching top's wired and compressor figures), an Intel 4 KB page-size fixture, and fail-safe cases (missing page-size header, missing page count, non-positive total).
  • Manually verified end-to-end on a live macOS runner: the new branch reports ~80% where the OS's own pressure level is normal, versus the stuck 99% "High pressure" beforehand.

junmo-kim added 2 commits July 3, 2026 13:54
Add readDarwinMemoryUsedPercent, a pure parser that computes macOS used
memory as App Memory + Wired + Compressed (anonymous + wired-down +
occupied-by-compressor pages), matching Activity Monitor's "Memory Used"
figure. Page size is parsed from the vm_stat header rather than hardcoded,
since it differs between Apple Silicon (16KB) and Intel (4KB) Macs.

Not wired up yet; covered by unit tests, including a verbatim vm_stat
capture from a 16GB Mac mini where the pre-fix total - freemem() path
reported 99% (counting reclaimable cache as used) while App+Wired+
Compressed is 79% — the number a user sees in Activity Monitor.
Add a platform() === 'darwin' branch that shells out to vm_stat
(1s timeout, guarded by try/catch) and feeds its output to
readDarwinMemoryUsedPercent. On any failure or undefined result it
falls through to the existing total - freemem() fallback, matching
the Linux branch's structure.

This fixes the Machine capacity tooltip showing a stuck ~99% "High
pressure" warning on macOS runners: os.freemem() there counts
reclaimable file cache as used, so it reports near-total usage.
Summing only App Memory + Wired + Compressed reports the same figure
Activity Monitor shows.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • No issues found.

Summary

  • Review mode: initial
  • Reviewed the full diff for the macOS vm_stat memory calculation and parser tests. Residual risk: I could not execute the test suite in this environment because bun is unavailable.

Testing

  • Not run (automation; bun not installed in this container)

HAPI Bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant