Skip to content

fix(session): break infinite compaction loop#27919

Open
ranxianglei wants to merge 1 commit into
anomalyco:devfrom
ranxianglei:fix/compaction-loop
Open

fix(session): break infinite compaction loop#27919
ranxianglei wants to merge 1 commit into
anomalyco:devfrom
ranxianglei:fix/compaction-loop

Conversation

@ranxianglei
Copy link
Copy Markdown

@ranxianglei ranxianglei commented May 16, 2026

Issue for this PR

Closes #27924

Type of change

  • Bug fix

What does this PR do?

The session loop in prompt.ts can enter an infinite compaction loop when compression fails to reduce the context below the token limit. The loop spins indefinitely consuming API credits without making progress.

Change in session/prompt.ts:

Add a compactionAttempts counter in the loop:

  • Increment before each compaction attempt (both overflow and compact paths)
  • If counter >= 2, break the loop with a log message
  • Reset counter to 0 when normal processing progresses (i.e., agent/tool call path)

This is a safety guard, not a fix for underlying compaction failure. The counter is per-loop-invocation (reset on each new runLoop call), so it doesn't persist across sessions.

How did you verify your code works?

This fix has been running in our fork (based on v1.14.41) for several days without issues. The change was manually applied to the current dev branch rather than cherry-picked due to significant code divergence.

Verified that:

  • Infinite compaction loop is broken after 2 attempts
  • Normal compaction (when it succeeds) is unaffected
  • No regressions in session flow

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…mpts

When compaction fails to reduce context below the limit (e.g. compression state lost, messages uncompressable), the session loop can enter an infinite cycle: overflow detected -> compact -> still overflow -> compact -> ...

Track consecutive compaction attempts per loop iteration. Break after 2 attempts to prevent the session from spinning forever.

This is a safety guard, not a fix for the underlying compaction failure.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
@github-actions github-actions Bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels May 16, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found several related PRs that address similar session/compaction issues:

Potentially Related PRs:

  1. fix(session): compact finished overflowed turns #27730 - "fix(session): compact finished overflowed turns"

    • Addresses session overflow and compaction handling
  2. fix(session): prevent double compaction after auto-compact #26484 - "fix(session): prevent double compaction after auto-compact"

    • Directly related to preventing unwanted compaction loops/repetition
  3. fix(session): prevent double compaction when task already pending #26235 - "fix(session): prevent double compaction when task already pending"

    • Similar prevention pattern for compaction issues
  4. feat(session): add custom compaction thresholds along with prevention of continuous compaction #10123 - "feat(session): add custom compaction thresholds along with prevention of continuous compaction"

    • Earlier feature addressing continuous compaction prevention
  5. fix: enable auto-compaction for sub-agents and improve context overflow detection #25180 - "fix: enable auto-compaction for sub-agents and improve context overflow detection"

    • Related to overflow detection and compaction handling

Why they're related: These PRs all address compaction loop prevention and session overflow handling in similar ways. PRs #26484 and #26235 are particularly close since they both prevent unwanted compaction repetition, similar to your counter-based approach.

Note: PR #27919 is the current PR, so it's correctly excluded from duplicates.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 16, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(session): infinite compaction loop when compression fails to reduce context

1 participant