anomalyco / anomalyco/opencode

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

Open
#48,827 1 comment 0 reactions 1 assignee View on GitHub

@rekram1-node is already working on this.

Since Sep 13, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

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 > usableneedsCompaction (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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.