Skip to content

Prefill OOM risk on large prompts: re-tune the compute-buffer reserve and engage history compaction on the shared-chat-node path #2227

Closed

Description

@malibio

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

Part 2: Verify and fix history compaction on the shared-chat-node path

  • Confirm why maybe_summarize_history did 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 existing fit_n_ctx_to_budget unit 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_history is 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:all passes with no new failures
  • bun run quality:fix clean

Technical Specifications

Reference Files

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

Related Issues

Activity

  1. added
    bug
    on Aug 21, 2026
  2. malibio commented on Aug 21, 2026

    @malibio
    CollaboratorAuthor

    Frontend: 4794 passed (227 files)

  3. self-assigned this
    on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

backendbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions