Skip to content

History token budget undercounts real prompt and refuses to summarize an over-budget conversation #2197

Closed

Description

@mstomar125

Summary

maybe_summarize_history counts only msg.content (lines 2630-2634), so assistant tool-call arguments_json (up to ~2,048 tokens per ReAct round; entire markdown documents for create_nodes_from_markdown), the tool-definitions JSON, and per-message chat-template overhead are never counted; additionally the early return if history_tokens <= history_budget { return Ok(()); } (line 2660) skips summarization even when the first check already established history+system exceeds the window budget, because history_budget is derived from SYSTEM_PROMPT_BUDGET=4,000 while the repo's own measurement (nlp-engine chat/mod.rs N_CTX_MINIMUM comment) puts the tool-registered system prompt at ~6,600 tokens.

Failure scenario

On a 16,384-token effective window: real system prompt 6,600 tokens, counted history 9,000 tokens (real history several thousand more via uncounted tool-call args). Gate: 9,000+6,600=15,600 > total_budget 14,336, so the first check fails; 9,000 <= history_budget 10,336, so the second check returns without summarizing. The templated prompt exceeds 16,384, generate_blocking rejects it with ContextOverflow, and the turn hard-fails. Retries land in the same corridor — the summarizer that exists precisely to prevent this can never fire until counted history alone crosses 10,336, so the user sees repeated turn errors on a long conversation; in the narrower sub-corridor where the prompt still fits with <2,048 tokens of headroom, the same undercounting instead feeds the silent-truncation bug in the llama bridge.

Location

packages/agent/src/local_agent/agent_loop.rs:2660

Severity

important


Found via a full-repo adversarial bug sweep, independently verified by two separate skeptical reviewers reading the actual current code before filing.

Activity

  1. self-assigned this
    on Aug 21, 2026
  2. malibio commented on Aug 21, 2026

    @malibio
    Collaborator

    Frontend: 4794 passed

  3. self-assigned this
    on Aug 21, 2026
  4. mstomar125 commented on Sep 3, 2026

    @mstomar125
    ContributorAuthor

    Independently re-verified before starting today's drain: PR#2225 (commit 1d2690a, "Count tool-call argument JSON in history token budget; remove redundant summarization gate") is merged and confirmed an ancestor of current main. The current maybe_summarize_history correctly counts tool-call arguments_json in the history estimate — confirmed by reading the live code, not just the commit message. Closing — no further work needed.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions