Skip to content

fix(session): bound consecutive overflow compaction cycles in the prompt loop#37584

Open
a692570 wants to merge 1 commit into
anomalyco:devfrom
a692570:fix-session-overflow-compaction-loop
Open

fix(session): bound consecutive overflow compaction cycles in the prompt loop#37584
a692570 wants to merge 1 commit into
anomalyco:devfrom
a692570:fix-session-overflow-compaction-loop

Conversation

@a692570

@a692570 a692570 commented Jul 18, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #27924

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Bounds the overflow compaction retry cycle in the SessionPrompt run loop.

When a provider rejects a turn with a context overflow, the loop requests compaction and retries the turn. There is no attempt limit and no check that compaction reduced the request, so when the summary completes but does not shrink context below the limit, the session compacts forever. I hit this on 1.18.2: my session DB shows 35 consecutive assistant messages with mode "compaction", one every 30 to 90 seconds for over an hour, all finishing stop with no error, with the following turn overflowing each time.

The fix counts consecutive overflow to compaction cycles in the run loop. After 3 with no successful turn in between, it fails the assistant message with ContextOverflowError and stops instead of compacting again. Any result other than "compact" resets the counter, so long sessions that legitimately compact many times are unaffected. This works because the only way the counter reaches 3 is three provider overflows in a row with a compaction between each, which means compaction is not making progress and retrying again cannot succeed.

Note #34029 and #34261 fix related gaps in packages/core, but this loop is in packages/opencode/src/session/prompt.ts, which the server prompt handler uses, so it needs its own guard. Complementary, not a duplicate.

How did you verify your code works?

bun run typecheck passes in packages/opencode. I have been running this patch in a source build since 2026-07-17 on the same cron workloads that previously looped: the loop no longer reproduces and multi-compaction sessions still complete. I did not find a test harness for the SessionPrompt run loop; happy to add a test if you point me at the preferred fixture approach.

Screenshots / recordings

Not a UI change.

Checklist

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

@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 github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

Based on the search results, I found several related PRs but no duplicate. The PR #37584 is the current one being reviewed. Here are the related PRs addressing similar context overflow and compaction issues:

Directly Related (complementary fixes):

Related Compaction/Overflow Handling:

Note: The current PR is a complementary fix to #34029 and #34261, implementing similar overflow cycle bounding in the SessionPrompt loop (packages/opencode/src/session/prompt.ts) rather than the core session runner.

@github-actions github-actions Bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jul 18, 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