Skip to content

fix: let the macOS app answer the first chat instead of reporting low memory - #200373

Merged
jalehman merged 6 commits into
mainfrom
openclaw/oceanic-prawn
Nov 17, 2026
Merged

jalehman merged 6 commits into
mainfrom
openclaw/oceanic-prawn

Conversation

@jalehman

@jalehman jalehman commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #200340

What Problem This Solves

On the macOS app, the first message after onboarding fails with "This host is low on memory, so it could not start another session", even on a Mac with plenty of free memory. The app's local Gateway runs on Bun. On macOS, Bun's process.availableMemory() counts only truly free pages, about 0.5 GB on a 48 GB Mac in normal use. The 1 GiB Claude CLI live-session guard therefore refuses the session.

The same reading also made optional background work think the process was out of memory on every Mac. Bun reports process.constrainedMemory() as physical RAM, so "available ≤ 20% of the limit" was always true. Cron retention, update checks, GitHub publication scans, post-ready maintenance and startup prewarming were deferred indefinitely.

User Impact

macOS app users get a reply to their first message. Background maintenance and prewarming run again on Macs. Gateway and node host stats report reclaimable memory instead of only free pages. Node runtimes, and Bun on Linux and Windows, are unchanged.

Why This Change Was Made

readProcessAvailableMemoryBytes() in src/infra/host-memory.ts is now the single owner of the "available memory" reading. Under Bun on macOS, it binds host_statistics(HOST_VM_INFO) from libSystem once through Bun's built-in FFI. It returns the same formula Node uses (libuv uv_get_available_memory: free + inactive + purgeable pages × page size). Speculative pages are already part of free_count. Each reading is one Mach call, about 0.1 ms after the first, with no cache.

Callers that read process.availableMemory() directly now use the owner:

  • readHostFreeMemoryBytes(): the CLI live-session guard, doctor rebuild concurrency, system.info, node host stats.
  • isConstrainedProcessLowOnMemory().
  • resolveUpdateHashWorkerCount().
  • scheduleGatewayPrewarm(), which repeated the same 20% check inline. With the shared check, Node/Linux hosts without a cgroup limit also prewarm. Node reports constrainedMemory() as 2^64 there, which the shared check already treats as no limit.

Tests that faked memory pressure by stubbing process.availableMemory() now stub isConstrainedProcessLowOnMemory(). On a Mac under the Bun test runner, the old stub would be bypassed. The threshold itself stays covered in host-memory.test.ts.

Not changed: the guard error is still classified as rate_limit/429. Changing that alters failover and user-copy behavior beyond this fix; it is noted as a follow-up in the issue.

Evidence

Packaged app, before and after (full output). I built and signed the macOS app twice on the affected Mac: from main d29fad4 and from this PR's head 90aa806. On the app's onboarding profile, I sent "hi" eight times through its local Gateway, which runs on the bundled Bun 1.4.4 and uses the Claude CLI live session. Each turn used a new session. A helper held 6 GiB so that truly free pages stayed low throughout, as they do in normal use.

Build Bun raw availableMemory() Reclaimable (Mach) Result
main d29fad4 0.26–0.46 GB 16.3–16.4 GB 0 of 8 replied; each failed with "Host memory is too low for another CLI live session"
This PR 90aa806 0.27–0.42 GB 15.5–16.2 GB 8 of 8 replied; each started a claude-cli live session

memory_pressure reported 58–61% free during both runs. Without the memory hold, main fails only some of the time, because free pages on this Mac swing around 1 GiB.

Helper under the bundled Bun. The final host-memory.ts (sha256 e31ed057…c965) ran under the app's bundled Bun 1.4.4, alternating with Node 26.12.1 at the same moments:

Reading Bytes
Bun raw process.availableMemory() 496,746,496 (0.50 GB)
Fixed helper under Bun 20,420,755,456 (20.42 GB)
Node 26.12.1 process.availableMemory() 20,415,102,976 (20.42 GB)

Under Bun, isConstrainedProcessLowOnMemory() is false (constrainedMemory() = 51,539,607,552 = totalmem()). The first call, including the FFI bind, takes 1.6–5.4 ms; later calls take 0.01–0.13 ms.

Tests:

  • src/infra/host-memory.test.ts simulates Bun on macOS (availableMemory() == freemem() = 512 MiB, constrainedMemory() = 48 GiB) with fake Mach counters. Plentiful reclaimable memory admits a CLI live session through the real ensureCliLiveSessionMemory. Genuinely low Mach memory (256 MiB) still throws cli_live_session_host_memory. Further cases cover pressure, update-hash workers and Node's unchanged reading.
  • Red on base: with origin/main's helper, 3 of the 4 Bun cases fail. The guard fails with the same "Host memory is too low for another CLI live session" as the issue.
  • Touched and consumer suites pass: host-memory, host-stats, system-info, execute-plugin, doctor rebuild concurrency, cron maintenance, update-startup schedule, GitHub publication runtime, runtime-services maintenance, startup prewarm and database page cache. pnpm check:changed passes; its dead-export scan was skipped because of existing failures on main in files this PR does not touch. Test-lines ratchet: +101 of 400.
  • Independent review (OpenClaw subagent) found two P2 issues. Both are fixed: the inline prewarm check, and pressure tests that would read real memory on a Mac.

Gap: I sent the turns through the app's Gateway with its CLI instead of clicking through onboarding in the chat window, because the Mac was in active use. The turns take the same Gateway, CLI-backend and memory-guard path as the app's chat.

🦞 Made with OpenClaw

@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: M labels Nov 17, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. merge-risk: 🚨 automation 🚨 May affect CI, automerge, proof capture, label sync, or maintainer automation. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 17, 2026
@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR centralizes reclaimable-memory readings for Bun on macOS and uses them for session admission, background work, and update-worker sizing.

Example: A 48 GB Mac reports 496,746,496 available bytes under the bundled Bun runtime.

  • Before: The 1 GiB session guard rejects the first Claude CLI conversation.
  • After: The changed helper reports 20,420,755,456 bytes, enough to pass the guard.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The product repair is useful and supported by native helper measurements, but shipped-flow proof is incomplete and the new import breaks wrapper extraction.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🦪 silver shellfish (2/6) 1 actionable review finding remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A healthy Mac rejects the first Claude CLI chat as low on memory, while optional background work can remain deferred.
Reason: The repair restores intended memory accounting on the supported bundled runtime, and the supplied maintainer-authored status establishes direction.

Regression provenance

Possible regression — probable (reviewed change; failure trace). No predecessor PR is attributed.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep open: this PR addresses a source-confirmed macOS first-chat blocker and has clear maintainer value, but the patch needs correction before merge.

Priority: P0
Reviewed head: 90aa8061c71a277175784854f7add75714ea236b

Before merge

  • [P1] Include the new dependency in the PR wrapper inventory (P1) - This import makes every extracted wrapper consumer of worker-pool-sizing depend on host-memory.ts, but scripts/pr-lib/wrapper-components.txt includes only the former. The existing copied-wrapper bootstrap fails with Cannot find module '../../infra/host-memory.js', and the runtime-closure checks identify the same missing file on this exact head. Add src/infra/host-memory.ts to the extraction inventory so wrapper lease acquisition and provisioning can load their dependencies.

Findings

  • [P1] [P1] Include the new dependency in the PR wrapper inventory — src/infra/worker-pool-sizing.ts:2

Tests

  • Missing end-to-end proof: Provide a packaged macOS app fresh-onboarding first-reply recording or redacted Gateway logs under the bundled Bun runtime, showing the changed admission path and resumed background work; redact credentials and non-public endpoints.
Agent review details

How this fits together

The shared host-memory reader translates runtime and operating-system readings into byte counts consumed by session admission, host statistics, background scheduling, and worker sizing.

flowchart TD
  A[macOS Mach statistics] --> C[Shared memory reader]
  B[Runtime memory APIs] --> C
  C --> D[CLI session admission]
  C --> E[Background scheduling]
  C --> F[Host statistics]
  C --> G[Update worker sizing]
Loading

Technical review

Best possible solution:

Keep the shared reclaimable-memory reader and genuine-pressure guards, and include its new dependency in the wrapper extraction inventory.

Do we have a high-confidence way to reproduce the issue?

The reporter's native Bun reading falls below the unchanged 1 GiB guard, and inspection of fetched main plus the pinned Bun source confirms the complete failure path; no reviewer-run macOS reproduction is claimed.

Is this the best way to solve the issue?

Correcting memory semantics in the existing shared owner is a focused repair that preserves pressure protection and fixes its affected consumers without adding settings or persistent state.

Full review comments:

  • [P1] [P1] Include the new dependency in the PR wrapper inventory — src/infra/worker-pool-sizing.ts:2
    This import makes every extracted wrapper consumer of worker-pool-sizing depend on host-memory.ts, but scripts/pr-lib/wrapper-components.txt includes only the former. The existing copied-wrapper bootstrap fails with Cannot find module '../../infra/host-memory.js', and the runtime-closure checks identify the same missing file on this exact head. Add src/infra/host-memory.ts to the extraction inventory so wrapper lease acquisition and provisioning can load their dependencies.
    Confidence: 1

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against f57dbb4ff67e.

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test test/scripts/eager-import-closure.test.ts test/scripts/pr-wrapper-source-closure.test.ts
  • pnpm test test/scripts/pr-worktree-provision.test.ts
  • git diff --check

What I checked:

  • Current-main failure path: Fetched main accepts Bun's positive Darwin availableMemory reading unchanged; the CLI admission owner rejects readings below 1 GiB before starting a new session. (src/infra/host-memory.ts:12, f57dbb4ff67e)
  • Original product reproduction: The linked report documents fresh macOS onboarding, the failed first message, Gateway failure traces, and paired Bun/Node memory readings: [Bug]: macOS app's first message fails with false "host is low on memory" because Bun's process.availableMemory() is free pages only #200340.
  • Affirmative runtime dependency: The runtime manifest pins openclaw/bun commit 191755ec115ece3dd642eba3cb0143cecb5e5d7a, and the patch explicitly imports bun:ffi; the pinned runtime's memory contract directly determines this behavior. (scripts/lib/openclaw-bun.json:3, 90aa8061c71a)
  • Pinned runtime confirms semantic mismatch: Process_availableMemory calls Bun__Os__getFreeMemory; its Darwin implementation returns free_count times page size, excluding inactive and purgeable pages. (src/jsc/bindings/BunProcess.cpp:3673, 191755ec115e)
  • Introduced extraction regression: The new worker-pool-sizing import reaches host-memory.ts, which is absent from scripts/pr-lib/wrapper-components.txt. Exact-head CI reports both the missing inventory entry and a copied-wrapper startup failure with Cannot find module. (src/infra/worker-pool-sizing.ts:2, 90aa8061c71a)
  • Supplied proof and explicit limitation: The full PR body reports native helper measurements under the bundled Bun runtime and simulated guard regression results, but explicitly says no packaged-app first message was exercised; no later discussion supplies that result. (90aa8061c71a)

Likely related people:

  • Peter Steinberger: Raw commit b3196df adds src/infra/worker-pool-sizing.ts:37 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: b3196dfda335; files: src/infra/worker-pool-sizing.ts)

PR surface

Source +62, Tests +101. Total +163 across 11 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 3 70 8 +62
Tests 8 166 65 +101
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 11 236 73 +163

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #200340
Summary: This PR targets the exact Bun memory-reading mismatch documented by the linked issue; the Linux physical-pressure work changes a different condition in the same owner.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

No label changes.

Label justifications:

  • P0: The affected macOS onboarding path cannot complete its first useful conversation, and retrying cannot repair the false memory reading.
  • merge-risk: 🚨 automation: The introduced import breaks the extracted PR wrapper's runtime closure, with an exact-head CI module-resolution failure.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 16, 2026, 10:02 PM ET / November 17, 2026, 03:02 UTC.

@jalehman

Copy link
Copy Markdown
Contributor Author

Packaged macOS app end-to-end proof

I tested the packaged macOS app on the affected Mac, a 48 GB Apple silicon MacBook Pro running macOS 26.5.1. Each turn is a chat message ("hi") to the app's local Gateway with openclaw agent from the app's CLI, using a new session key. The CLI is ~/.openclaw-onboarding/bin/openclaw, profile onboarding. Each turn takes the same path as the app's first chat: the Gateway on the bundled Bun 1.4.4, then the Claude CLI live session (claude-cli/claude-opus-5-5), then the 1 GiB memory guard.

Builds (signed Apple Development builds from scripts/package-mac-app.sh, arm64):

  • Before: main at d29fad4e3e68, app build 2611160101, runtime 2026.11.4-d29fad4e3e68.
  • After: this PR's head 90aa8061c71a, app build 2611170101, runtime 2026.11.4-90aa8061c71a. Signature and Info.plist commit were verified.

To keep free pages consistently low, a helper process held 6 GiB for each run. That matches the Mac's ordinary state: macOS keeps truly free pages low, and most memory is inactive or purgeable. For each turn:

  • bun-raw is Bun's process.availableMemory(), which the old guard used.
  • reclaimable is free + inactive + purgeable pages from Mach VM statistics, the reading this PR uses.
  • free% is the system-wide figure from memory_pressure.

Before (d29fad4): 0 of 8 turns replied. Each one failed with the error from the issue.

00:52:10 turn 1  bun-raw= 0.26 GB  reclaimable=16.43 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
00:52:11 turn 2  bun-raw= 0.35 GB  reclaimable=16.38 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
00:52:11 turn 3  bun-raw= 0.29 GB  reclaimable=16.37 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
00:52:12 turn 4  bun-raw= 0.30 GB  reclaimable=16.39 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
00:52:12 turn 5  bun-raw= 0.32 GB  reclaimable=16.36 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
00:52:12 turn 6  bun-raw= 0.30 GB  reclaimable=16.35 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
00:52:13 turn 7  bun-raw= 0.34 GB  reclaimable=16.32 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
00:52:13 turn 8  bun-raw= 0.46 GB  reclaimable=16.36 GB  free%=61%  FAIL  Host memory is too low for another CLI live session. | cli_live_session_host_memory
SUMMARY before-d29fad4: ok=0 fail=8

After (90aa806, this PR): 8 of 8 turns replied. Bun's raw reading was just as low.

00:53:39 turn 1  bun-raw= 0.34 GB  reclaimable=16.06 GB  free%=59%  OK   What would you like to call me?
00:53:42 turn 2  bun-raw= 0.32 GB  reclaimable=15.46 GB  free%=58%  OK  Hey, I'm your new assistant, just set up in this workspace. What would you like to call me?
00:53:44 turn 3  bun-raw= 0.42 GB  reclaimable=15.96 GB  free%=59%  OK   What would you like to call me?
00:53:46 turn 4  bun-raw= 0.31 GB  reclaimable=16.00 GB  free%=59%  OK  Hey, I'm your new assistant and I just came online. Before we go further, what would you like to call me?
00:53:49 turn 5  bun-raw= 0.32 GB  reclaimable=15.90 GB  free%=59%  OK  Hey, I'm your new assistant, just switched on. Before anything else: what would you like to call me?
00:53:51 turn 6  bun-raw= 0.41 GB  reclaimable=16.24 GB  free%=60%  OK  Hey, I'm your new assistant, and I just came online in this workspace. Before we do anything else: what would
00:53:53 turn 7  bun-raw= 0.28 GB  reclaimable=15.77 GB  free%=59%  OK   What would you like to call me?
00:53:55 turn 8  bun-raw= 0.27 GB  reclaimable=15.46 GB  free%=58%  OK  Hey, I'm your new assistant. I'm brand new, with no name and no memories yet. What would you like to call me?
SUMMARY after-90aa806: ok=8 fail=0

The Gateway log (~/.openclaw-onboarding/logs/openclaw-onboarding-2026-11-17.log, runtimeVersion bun/1.4.4 throughout) shows the same split:

  • Before: each turn logged a FailoverError lane-task error.
  • After: each turn logged cli live session start: provider=claude-cli model=claude-opus-5-5, with activeSessions climbing from 1 to 9.

Without the memory hold, main fails only intermittently. Free pages on this Mac swing around 1 GiB. In one unloaded run, turns 1 and 2 replied at 1.75 and 3.16 GB raw, and turn 3 failed at 2.21 GB raw sampled just before it.

Not covered: I didn't click through onboarding in the app UI. The Mac was in active use, so I sent turns through the app's Gateway instead of driving its chat window. The turns run through the same Gateway agent run, CLI backend and memory guard that the app's chat uses.

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

Labels

gateway Gateway runtime merge-risk: 🚨 automation 🚨 May affect CI, automerge, proof capture, label sync, or maintainer automation. P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. scripts Repository scripts size: M status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: macOS app's first message fails with false "host is low on memory" because Bun's process.availableMemory() is free pages only

1 participant