Repository navigation
Description
Overview
During tonight's investigation (see #1632, #1634, #1637), the native GGUF/llama.cpp path reproducibly hard-OOM'd on Gemma 4 12B (gemma-4-12b-unsloth-q4km) four separate times on a 16GB machine — always the same signature: first turn barely survives at the timeout ceiling, second turn crashes with ggml_metal_synchronize: error... Insufficient Memory (kIOGPUCommandBufferCallbackErrorOutOfMemory), confirmed under genuinely clean conditions (isolated daemon, no other processes, ~12GB free GPU memory at load time per the daemon's own log line).
The same model, over Ollama, never OOM'd once across the whole investigation — same machine, same underlying llama.cpp engine family.
Root Cause
ChatConfig::n_ctx is hardcoded to 32_768 in packages/nlp-engine/src/chat/types.rs and is allocated as a fixed KV cache for the full model-load lifetime, unconditionally, regardless of how much memory is actually free at that moment or how large a context the current conversation actually needs. There is no per-request or memory-pressure-aware sizing on the native path — the daemon requests the full context window up front and holds it for as long as the model stays loaded.
Ollama, by contrast, negotiates context size per request rather than reserving a fixed allocation at model-load time — e.g. a manual ollama run with no explicit options loaded with CONTEXT: 4096 (Ollama's own default), only expanding when a client's request explicitly asks for more via num_ctx. Ollama's serving layer is also built from the ground up to manage memory pressure across multiple concurrent model sessions (see ollama ps, keep_alive, LRU eviction) — a layer the native daemon doesn't have an equivalent of.
Nothing in the underlying llama-cpp-2/llama-cpp-sys-2 crates structurally prevents adaptive sizing — quantized KV cache is already used for some catalog entries (gemma-4-12b-unsloth-q4km uses type_k/type_v: Q8_0 specifically to reduce footprint), so the infrastructure for memory-conscious allocation partially exists. What's missing is deciding n_ctx dynamically (per available memory, or per actual conversation length) rather than as a single hardcoded constant applied to every model at load time.
Problem Statement
Any native-path model whose weights + full 32K KV cache doesn't comfortably fit in available GPU memory is at OOM risk today, independent of the model's own tool-calling capability — this is a resource-management gap, not a model-quality issue. It affects any future model evaluation on this hardware class (this session's investigation into re-evaluating Gemma 4 E4B on the native path, see #1637/#1634 context, was specifically blocked from testing 12B by this issue).
Candidate Fixes (not prescribed — for investigation)
- Size
n_ctxper-request or per-conversation (e.g. start smaller, grow only if the conversation actually needs more context) rather than a single fixed constant for every model. - Check available GPU/system memory before committing to a context size at model-load time, falling back to a smaller context (with a clear warning/limitation) rather than attempting the full 32K and OOM-crashing.
- Investigate whether llama.cpp's context-shift or dynamic KV-cache-resize mechanisms (referenced in some of the upstream PRs found while researching Upgrade llama-cpp-sys-2 when Gemma 4 12B peg-gemma4 parser fixes land upstream #1348) could be adopted here instead of static allocation.
- Look at whether Ollama's actual resource-negotiation logic (open source) offers a directly portable approach.
Related
- ollama_integration test suite has ~17 failing tests due to empty tool-call responses from mistral:7b #1632 — original test-suite triage that surfaced Ollama vs. native-path differences
- mistral:7b (Ollama path) tool-calling reliability degrades sharply past ~6 concurrently-offered tools #1634 — mistral:7b's Ollama-path tool-count ceiling (separate problem, same investigation)
- resolve_query must return a resolved node, not query fragments #1637 — query-decomposition proposal (separate problem, same investigation)
- Upgrade llama-cpp-sys-2 when Gemma 4 12B peg-gemma4 parser fixes land upstream #1348 — Gemma 4 12B peg-gemma4 parser fix (confirmed present in vendored source; this issue's OOM problem is a separate, additional blocker even after that fix, discovered while trying to verify Upgrade llama-cpp-sys-2 when Gemma 4 12B peg-gemma4 parser fixes land upstream #1348 end-to-end)
- ADR-046 (../nodespace-docs/decisions/046-ministral-8b-locked-native-model.md) — locked the native path to Ministral 8B partly due to larger-model failures on this hardware class; this issue provides a more precise root cause (fixed-allocation OOM, not just "doesn't fit") for at least the memory-related portion of that decision
Acceptance Criteria
- Root cause confirmed:
n_ctxallocation strategy on the native path, contrasted precisely against Ollama's per-request negotiation - Decide on an approach (dynamic sizing, memory-check-before-load, or another mechanism)
- Verify fix against the reproduction case documented here:
gemma-4-12b-unsloth-q4kmsurviving more than one conversational turn on a 16GB machine without OOM - No regression to models that already fit comfortably in the current fixed allocation (e.g. Ministral 8B, Gemma 4 E4B)
Activity
malibio commentedon Jul 12, 2026
Frontend baseline: 4031 passed (159 files)
Native llama.cpp path allocates a fixed 32K context at load time; Oll…
Address review: fix search_nodes default limit + stale execute_query …
Collapse execute_query into search_nodes as one agent query tool (closes