Repository navigation
Description
Overview
#2172 fixed the symptom of a Metal command-buffer OOM during a large prefill — the poisoned backend context is now torn down and rebuilt (#2226), so the failure is loud and recoverable instead of a silent permanent outage. This issue is about the two root-cause contributors #2226 deliberately left out of scope: the fixed compute-buffer reserve that the OOM slipped past, and the unbounded prompt growth that let a single prefill reach 35 KB / 8,518 tokens in the first place.
Both need their own investigation/measurement before a fix is known, which is why they were filed separately rather than folded into #2226's PR (see the "Scope Discovered Mid-Implementation" guidance in the issue workflow).
Problem Statement
Contributor 1 — the compute-buffer reserve does not model peak prefill cost.
COMPUTE_RESERVE_BYTES (packages/nlp-engine/src/chat/mod.rs) is a fixed 1.5 GiB reserve for activation/command-buffer memory, decoupled from n_ctx per ADR-062. #2172's own arithmetic showed ~4x headroom on the machine where the OOM occurred (7.53 GB budget vs. 1.75 GB actual KV cache), so the reserve is not obviously undersized in the steady state — but a large prefill (an 8.5K-token prompt decoded in four back-to-back 2048-token chunks) still triggered kIOGPUCommandBufferCallbackErrorOutOfMemory 84 times. The peak transient allocation during a large prefill chunk is not currently measured or modeled; the fixed reserve is a guess that happened to be wrong at this prompt size.
ADR-062 explicitly scopes this as implementation tuning, not architecture: "Specific reserve values... are implementation tuning and live in the code, not here. They are empirical and expected to change." It also names this exact risk as a known consequence: "The reserve constants are judgment calls validated on a narrow set of hardware. They are the values most likely to need revision."
Contributor 2 — history is not bounded on the path that produced the oversized prompt.
The matrix run that surfaced #2172 shares one chat node across scenario groups, so history accumulates turn over turn. By turn 10 the prompt was 35,552 characters / 8,518 tokens, and KV-cache prefix reuse contributed only 5 tokens — i.e. maybe_summarize_history (packages/agent/src/local_agent/agent_loop.rs) was not engaging to keep the prompt within a safe working size on this path. Any real user with a long-running agent conversation on a memory-constrained machine reaches the same prompt sizes the same way.
These two are linked: fixing only the reserve treats a symptom that recurs at some larger prompt size; fixing only compaction reduces how often large prefills happen but does not change what happens when one occurs anyway (e.g. a single turn with a large single tool result, before compaction has a chance to run).
Proposed Solution
Part 1: Measure and re-tune the compute-buffer reserve
- Reproduce the OOM using Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172's repro steps (
bun run eval:agentagainst a fresh daemon, 16 GB M-series machine,gemma-4-e4b-q4km). - Instrument or directly measure Metal's peak transient allocation during a single
PREFILL_BATCH_TOKENS-sized decode chunk, at varyingn_past(KV cache depth) — the open question from Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172 is whether compute-buffer size scales with tokens-in-batch alone or also with attention-over-cached-tokens asn_pastgrows. - Based on the measurement, either raise
COMPUTE_RESERVE_BYTES, make it scale withn_ctx/n_pastinstead of staying flat, or reducePREFILL_BATCH_TOKENSfor large-context loads — whichever the data supports. Do not guess at a new constant without a reproduced OOM to validate against.
Part 2: Verify and fix history compaction on the shared-chat-node path
- Confirm why
maybe_summarize_historydid not keep turn 10's prompt under the observed 8.5K-token size — check its trigger threshold and whether it's being invoked at all on this call path (agent_loop::run_turn,packages/agent/src/local_agent/agent_loop.rs). - If summarization is not wired into this path, wire it in. If it is wired in but its threshold is too permissive, tighten it.
- Add a regression test that drives a multi-turn session past the summarization threshold and asserts the resulting prompt size stays bounded (a lighter-weight check than a live-model integration test — this can assert against
maybe_summarize_history's token-budget logic directly, similar to the existingfit_n_ctx_to_budgetunit tests).
Acceptance Criteria
- The OOM in Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172's repro steps no longer reproduces on the same hardware/model after the reserve change (or the investigation concludes the reserve was not the actual gap and documents what was, with evidence)
-
maybe_summarize_historyis confirmed to engage on the shared-chat-node ReAct loop path, keeping prompt size bounded across many turns of accumulated history - A regression test exists asserting bounded prompt growth across a long multi-turn session
- Any changed constants (
COMPUTE_RESERVE_BYTES,PREFILL_BATCH_TOKENS, or a summarization threshold) are justified with the measurement that produced them, not restored from guesswork -
bun run test:allpasses with no new failures -
bun run quality:fixclean
Technical Specifications
Reference Files
packages/nlp-engine/src/chat/mod.rs:COMPUTE_RESERVE_BYTES,PREFILL_BATCH_TOKENS,PREFILL_UBATCH_TOKENS,fit_n_ctx_to_budget— the memory-budget arithmetic this issue tunespackages/agent/src/local_agent/agent_loop.rs:maybe_summarize_history, called fromrun_turn's ReAct loop — the compaction path this issue verifies/fixes- Recreate the chat backend context after a decode failure #2226: the merged fix for the recovery/error-surfacing half of Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172 — this issue is the remaining two "suggested direction" bullets from Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172 that PR deliberately left out of scope
Architecture Impact
No ADR reversal. ADR-062 (../nodespace-docs/decisions/062-context-window-budgeted-against-total-ram.md) governs the shape of the memory budget (total-RAM-based, decoupled batch size) and explicitly scopes reserve constants as implementation tuning expected to change — this issue operates entirely within that decision, adjusting a constant ADR-062 itself flags as likely to need revision. No companion nodespace-docs change is required unless the investigation concludes the reserve needs to become dynamic (scale with n_ctx/n_past) rather than a flat constant, in which case ADR-062's "Decision" section should be updated to describe the new shape.
Dependencies
- Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172 (root-cause investigation this issue continues)
- Recreate the chat backend context after a decode failure #2226 (recovery/error-surfacing fix this issue's scope was split from)
Related Issues
- Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172 (parent investigation)
- Recreate the chat backend context after a decode failure #2226 (merged fix for the other half of Metal OOM on large prefill poisons the backend permanently: agent silently returns zero tool calls until daemon restart #2172's suggested direction)
Activity
malibio commentedon Aug 21, 2026
Frontend: 4794 passed (227 files)
Add a stability-motivated prefill ceiling to history compaction (#2231)