Repository navigation
Description
Overview
On macOS, sysinfo::available_memory() counts only genuinely-free pages and excludes the reclaimable inactive pool. After a chat model's weights become resident it can report 0.0 GB available on a machine with several GB of reclaimable cache. compute_effective_n_ctx then computes a KV budget of zero and floors n_ctx to N_CTX_FLOOR (4096) — which is below the agent's own system prompt (~6,600 tokens with 14 tools registered). Every tool-using turn fails before the model runs:
WARN Reducing chat context to fit memory: configured=32768 effective=4096
available=0.0GB (KV 2 B/elem) — large model on constrained memory
WARN inference turn failed — Context window exceeded:
Prompt uses 6556 tokens but context window is 4096
This is a hard failure of the whole local-agent surface, not a degradation: the user sees every AI request fail.
The memory is not actually scarce
Measured on a 16 GB M2 Pro while the floor was being hit:
| metric | value |
|---|---|
available_memory() (what the code reads) |
0.0–1.1 GB |
| free pages | 1.17 GB |
| inactive (reclaimable) | 6.65 GB |
| swap used | 0 |
Gemma 4 E4B's KV cache is cheap — 42 layers, GQA 8/2 → 105 KiB/token, so even a full 32K window is ~3.3 GB and 16K is ~1.6 GB. The machine has that. The probe just cannot see it, because macOS holds it as inactive rather than free.
Root cause
packages/nlp-engine/src/chat/mod.rs:
compute_effective_n_ctxreadssys.available_memory()and passes it tofit_n_ctx_to_budget.fit_n_ctx_to_budgetcomputesmin(available − KV_HEADROOM_BYTES, available × KV_BUDGET_PERCENT%).- With
KV_HEADROOM_BYTES = 2 GiB, anyavailableat or below 2 GiB makes the first term zero, so the result clamps toN_CTX_FLOORregardless of the model's real KV cost.
The 2 GiB headroom and 40% cap were tuned against a 12B model OOMing at a 32K window (the adaptive-sizing change that introduced them). E4B pays the same fixed tax despite needing a fraction of the memory, and the pessimistic probe pushes it over the edge.
Impact
- Reproducible on a 16 GB machine with a warm page cache — i.e. normal desktop conditions, not an artificial low-memory state.
- Not limited to test rigs: the same code path runs in the shipped app. Any user whose free-page count reads low when a model loads gets a 4096-token context and a 100% agent failure rate.
- It has already blocked verification work twice: the decomposition PR could not complete a live
aichat-matrixrun for this exact reason, and reproducing it later required temporarily overriding the probe.
Suggested fixes
- Count reclaimable memory. Use free + inactive (+ purgeable) rather than
available_memory()alone on macOS, so the budget reflects memory the OS will actually hand over under pressure. - Never floor below what the prompt needs.
N_CTX_FLOOR(4096) is smaller than the agent's own system prompt (~6,600 tokens). If the computed window cannot hold the system prompt plus a minimal reply, that is a hard error worth surfacing — not a silent clamp that makes every turn fail with a confusing "context window exceeded." - Scale headroom to the model. A fixed 2 GiB subtraction is right for a 12B model at 32K and wrong for E4B; consider deriving it from the measured per-token KV cost.
- KV-cache quantization would independently reduce the pressure (
KvCacheQuantTypeexists inchat/types.rsbut isNoneby default with no override).
Acceptance Criteria
- On a 16 GB machine with a warm page cache, loading
gemma-4-e4b-q4kmyields a context window large enough for the agent system prompt plus a working conversation (not the 4096 floor) - A computed window that cannot fit the system prompt surfaces a clear, actionable error rather than a generic per-turn "Context window exceeded"
-
scripts/aichat-matrix.tsruns end-to-end against E4B on such a machine without any memory-probe override - No regression to the OOM protection the adaptive sizing was introduced for (a large model on a genuinely constrained machine must still be throttled)
Reproduction
cargo build --release -p nodespace-daemon -p nodespace-cli
rm -rf /tmp/nodespaced-test && mkdir -p /tmp/nodespaced-test
NODESPACED_HEADLESS=1 NODESPACED_SOCKET=/tmp/nodespaced-test/daemon.sock \
NODESPACED_DB_PATH=/tmp/nodespaced-test/nodespace.db RUST_LOG=info \
target/release/nodespaced > /tmp/nodespaced-test/daemon.log 2>&1 &
target/release/nodespace --socket /tmp/nodespaced-test/daemon.sock model load gemma-4-e4b-q4km
grep -E "Reducing chat context" /tmp/nodespaced-test/daemon.logThen send any tool-using request (e.g. "Create an invoice tracking database") and observe the per-turn context failure. Compare vm_stat's inactive-page count against the available= figure in the warning.
Activity
malibio commentedon Jul 28, 2026
Baseline (worktree issue-1758-chat-context-memory-probe): Frontend 4091 passed / 166 test files.
malibio commentedon Jul 28, 2026
Correction to the diagnosis in this issue (fixed in #1762).
The stated root cause — that
sysinfo::available_memory()ignores the reclaimable inactive pool — does not hold. sysinfo 0.33.1 already computesfree + inactive + purgeable − compressor(unix/apple/system.rs:200-204), so the proposedhost_statistics64probe (suggested fix 1) would have been a no-op.Instrumented probe during an E4B load on the reporting machine:
The 105 KiB/token figure in the issue is confirmed exactly. But the weights land in wired, not inactive, and the compressor pool is subtracted — which is what drives
availableto ~0. Metal reported ~11.9GB free at the same moment.Two real causes: (1)
n_batchwas pinned to the full context window, so a 32K window reserved a 32K-token compute buffer — the actual memory sink, and the only justification for the 40% cap that starved the window; (2) the budget was keyed offavailable_memory(), which on this machine reports ~0.1-0.4GB whether or not a model is loaded.Suggested fix 4 (KV quantization) was also skipped: E4B reaches its full 32K window at F16, and the tiers that need Q8_0 already set it.
Also filed #1761 for a pre-existing deadlock found while verifying this.