Skip to content

Auto-compaction loops forever when a session's irreducible context exceeds the model's usable window #48827

Closed as not planned

Description

@whitesm2000

Auto-compaction loops forever when a session's irreducible context exceeds the model's usable window

Summary

When a session's post-compaction floor (system prompt + retained recent tail) is larger than the model's usable context window, the session loop auto-compacts after every single step, forever — compaction → "Continue if you have next steps…" → assistant step → overflow → compaction → … The UI spins with no error, the agent keeps executing tool calls autonomously on every cycle, and each cycle burns a full session's worth of tokens until it trips the provider's rate limits.

Observed on desktop v1.18.30 with cerebras/qwen-3.8-27b on a ~60k-token session.

Root cause

session/overflow.ts:

usable = context - min(limit.output, OUTPUT_TOKEN_MAX)   // qwen-3.8-27b: 65536 - 32000 = 33536
isOverflow = tokens.total >= usable

Compaction summarizes old messages but always retains the recent tail. If that retained tail alone exceeds usable, then:

  1. assistant step finishes with total > usable → needsCompaction (processor.ts) / isOverflow check (prompt.ts)
  2. auto-compaction runs and injects the synthetic "Continue if you have next steps…" user message
  3. the very next assistant step starts already over the threshold and finishes over it again
  4. → goto 1, indefinitely

Nothing bounds the retry, and nothing is ever surfaced to the user.

Evidence

Session logs from the desktop app (server run across ~20 seconds, then manually cancelled) — note the strict alternation and the token counts pinned just above the 33,536 threshold, never decreasing:

step=1 stream cerebras/qwen-3.8-27b agent=compaction   (total 77,246)
step=2 stream cerebras/qwen-3.8-27b agent=build        (total 50,840)
step=3 stream cerebras/qwen-3.8-27b agent=compaction   (total 10,416)
step=4 stream cerebras/qwen-3.8-27b agent=build        (total 47,492)
step=5 stream cerebras/qwen-3.8-27b agent=compaction   (total  8,528)
step=6 stream cerebras/qwen-3.8-27b agent=build        (cancelled by user)

An earlier unattended run ping-ponged for 20+ cycles in about a minute and ended only by tripping Cerebras's tokens-per-minute rate limit (AI_APICallError: Tokens per minute limit exceeded). Because each cycle's build step runs the agent loop, the model also executed tool calls (read-only bash verification commands) on its own each time — the auto-continue makes the session work unattended while it burns quota.

This is not provider-specific: any model whose usable window is smaller than a session's irreducible tail hits it. It's just most visible on small-context models (64k) with large sessions.

Proposed fix

PR linked below: a no-progress guard. Count consecutive automatic compactions whose first finished (non-summary) successor step still overflows — i.e. compactions that demonstrably failed to restore headroom. When that streak reaches 2, stop the loop and surface a ContextOverflowError ("start a new session or switch to a model with a larger context window") instead of compacting again.

Judging by the first successor step (rather than any later step) keeps legitimate flows working: a session that overflows again only after pulling in a new 50k-token tool result gets compacted normally, because its first post-compaction step is under the threshold. Real user turns and manual /compact reset the streak.

Activity

  1. github-actions commented on Sep 13, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    If this is distinct from those (e.g. the specific condition of the retained tail alone exceeding the usable window, rather than compaction failing to summarize), please clarify how it differs from the above.

  2. thdxr commented on Oct 11, 2026

    @thdxr
    Member

    Thanks for the report! OpenCode 2.0 is now out and development is focused on V2, so we're closing issues filed against OpenCode 1. Please upgrade (opencode upgrade --major, details: https://anoma.ly/notes/opencode-2-0), and if this is still relevant on 2.0, reopen this issue or open a new one with your 2.0 version.

    — from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

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

Metadata

Metadata

Assignees

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