GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(opencode): break auto-compact loop when compaction makes no progress by ZehuaWang · Pull Request #29150 · anomalyco/opencode

Conversation

@ZehuaWang

@ZehuaWang ZehuaWang commented May 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Issue for this PR

Closes #28543

Type of change

  • Bug fix

What does this PR do?

When a model's context limit in models.dev is smaller than what the provider actually serves, every turn reports overflow tokens and auto-compaction fires forever. Once from the pre-model-call isOverflow check, then again from the processor's own finish-step overflow check inside handle.process.

Track the token count from the last auto-compaction in this runLoop invocation. If we'd fire again without the count having dropped by at least 5%, throw ContextOverflowError instead. Reset the tracker when overflow is no longer detected so healthy long sessions aren't affected.

How did you verify your code works?

bun test test/session/compaction.test.ts test/session/prompt.test.ts — all pass. Added unit tests around the 5% boundary and one integration test that seeds an overflow assistant message, calls prompt.loop, and asserts it exits with ContextOverflowError.

Also built the binary and pointed it at a stub openai-compatible server that always reports overflow usage. Original 1.15.10 made 20 LLM calls before timing out at 90s. Patched build exits in ~4s with the typed error.

Checklist

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chenney0552 @claude
fix(opencode): break auto-compact loop when compaction makes no progress
08f74b9
Closes anomalyco#28543

When a model's configured context window is smaller than what the
provider actually serves (e.g. GitHub Copilot's claude-opus-4.7 mapped
at 144K in models.dev when the real ceiling is higher), every successful
turn keeps reporting "overflowing" token counts. Auto-compaction then
fires before each new prompt AND inside the processor on each finish-step,
and we never escape it.

Add a stall detector that compares the reported token count between
consecutive auto-compaction triggers in a single run. If a second
auto-compaction would fire with the token count not having dropped by at
least 5%, throw a typed ContextOverflowError instead of recreating the
compaction task forever.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions

github-actions Bot commented May 25, 2026

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 addressing auto-compaction and context overflow issues:

Potentially Related PRs:

  1. fix(session): break infinite compaction loop #27919 - fix(session): break infinite compaction loop

    • Directly addresses the same infinite compaction loop problem
  2. fix(session): prevent double compaction after auto-compact #26484 - fix(session): prevent double compaction after auto-compact

    • Addresses compaction logic preventing redundant compaction attempts
  3. fix(session): compact finished overflowed turns #27730 - fix(session): compact finished overflowed turns

    • Related to handling overflow detection during session compaction
  4. 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

    • Improves context overflow detection mechanism
  5. 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

    • Adds threshold logic and prevents continuous/infinite compaction
  6. fix: include reasoning tokens in overflow detection #28585 - fix: include reasoning tokens in overflow detection

    • Related to token counting in overflow detection

Why they might be related: These PRs all address aspects of the auto-compaction loop, token overflow detection, and preventing infinite compaction cycles. PR #27919 appears most similar as it also fixes an "infinite compaction loop." You should verify whether #27919 was already attempted, what its status is, and how this PR (#29150) differs in its approach (using a stall detector vs. other mechanisms).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chenney0552 and others added 2 commits May 24, 2026 22:28
@chenney0552 @claude
fix(opencode): treat exactly-5% reduction as progress, not stalled
40506be
Review found the boundary check used >= instead of >, so a reduction that
hit exactly the threshold (e.g. 200K → 190K) was flagged as stalled and
threw ContextOverflowError, contradicting the PR description that requires
"at least 5% reduction" to escape stall. The boundary test name said
"returns false right at the 5% boundary" but the assertion said true —
which made the inconsistency obvious.

Change the comparison to strict `>`. Now exactly-(1-threshold) reduction
counts as progress; only reductions strictly less than the threshold trip
the guard. Update the boundary test name, comment, and assertions to
match.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@chenney0552 @claude
fix(opencode): treat 0 -> 0 token compactions as stalled
18e1120
Switching the percentage check from >= to > closed the boundary
false-positive (200K -> 190K now counts as progress) but opened a
zero-token hole: when the provider directly throws ContextOverflowError,
SessionProcessor.halt sets needsCompaction without running step-finish,
so handle.message.tokens stays at the zero-initialized values. The
percentage check then evaluates 0 > 0 * 0.95 = false on every fire and
the loop keeps recreating compactions.

Add a one-line guard so two consecutive zero-token compactions trip the
stall detector. Unit test added alongside the boundary cases.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions github-actions Bot mentioned this pull request May 25, 2026
📊 AI CLI 工具社区动态日报 2026-05-25 ivanweng2077/big_model_radar#86
Open
@danielxxomg danielxxomg mentioned this pull request Jun 5, 2026
compaction.auto: false is ignored when provider throws a context overflow error #16882
Closed
@danielxxomg

danielxxomg commented Jun 5, 2026

Copy link
Copy Markdown

I was looking into this after #30749 landed. That PR covers the compaction.auto: false overflow path, but this one still seems to address the remaining case where auto-compaction is enabled and compaction does not make enough progress — which looks like the main unresolved failure mode from #28543 and #30680.

I tried applying this locally against current dev. Only prompt.ts needed a small conflict resolution (the autoCompactStalled import line). The rest applied cleanly. Tests pass after that (105 pass, 0 fail, typecheck clean).

Is this PR still intended to move forward? If it mainly needs a rebase, I'd be happy to help with that or prepare a small follow-up if maintainers prefer.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chenney0552
Merge branch 'dev' into fix/auto-compact-loop-guard
5a9b3b8
# Conflicts:
#	packages/opencode/src/session/prompt.ts
@ZehuaWang

ZehuaWang commented Jun 16, 2026

Copy link
Copy Markdown
Author

Thanks @danielxxomg, really appreciate you digging into this 🙏

Yes — still intended to move forward. You've got it exactly right: #30749 only covered the compaction.auto: false path, and this PR targets the remaining case (auto-compaction enabled but not making meaningful progress), which is the loop reported in #28543 and #30680.

I've just merged latest dev and pushed the conflict resolution. One heads-up for anyone else rebasing: since your 2026-06-05 test, dev landed a few more refactors, so it needed more than just the autoCompactStalled import line —

  • slog.warn → Effect.logWarning
  • bus.publish → events.publish
  • MessageV2.ContextOverflowError / MessageV2.Assistant → SessionV1.* (error type + assistant type moved namespaces)

All resolved against current dev; packages/opencode typecheck is clean. Running the full suite now to confirm.

Happy to squash/rebase instead of the merge commit if maintainers prefer — just say the word.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot closed this Jun 25, 2026
@github-actions github-actions Bot added the automated-pr-cleanup label Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

automated-pr-cleanup

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Auto-compact infinite loop with claude-opus-4.7-1m (GitHub Copilot) due to wrong context window

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@ZehuaWang @danielxxomg @chenney0552
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.