Skip to content

fix: prevent infinite loop when context overflow compaction fails#2235

Draft
dgageot wants to merge 1 commit intodocker:mainfrom
dgageot:break-endless-loop
Draft

fix: prevent infinite loop when context overflow compaction fails#2235
dgageot wants to merge 1 commit intodocker:mainfrom
dgageot:break-endless-loop

Conversation

@dgageot
Copy link
Member

@dgageot dgageot commented Mar 24, 2026

When a ContextOverflowError occurs and session compaction is enabled, the runtime retries after compacting. If compaction fails to reduce the context enough, the same overflow recurs, causing an infinite loop.

Add a compactionAttempted guard that allows only one compaction attempt per overflow. The guard resets after a successful model call so future overflows can still trigger compaction.

Assisted-By: docker-agent

When a ContextOverflowError occurs and session compaction is enabled, the runtime retries after compacting. If compaction fails to reduce the context enough, the same overflow recurs, causing an infinite loop.

Add a compactionAttempted guard that allows only one compaction attempt per overflow. The guard resets after a successful model call so future overflows can still trigger compaction.

Assisted-By: docker-agent
@dgageot dgageot requested a review from a team as a code owner March 24, 2026 20:43
@dgageot dgageot marked this pull request as draft March 24, 2026 20:43
Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

This PR correctly implements infinite loop prevention for context overflow compaction. The compactionAttempted flag is properly scoped as a local variable within RunStream(), ensuring:

  • ✅ No race conditions (local variable, single goroutine)
  • ✅ Proper reset between invocations (re-initialized on each call)
  • ✅ Correct reset after successful model calls (line 326)
  • ✅ Adequate test coverage with TestContextOverflow_NoInfiniteLoop

The implementation is clean and follows Go best practices. No bugs found in the changed code.

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.

3 participants