Skip to content

fix(session): prevent double compaction after auto-compact#26484

Closed
sgfvamll wants to merge 1 commit into
anomalyco:devfrom
sgfvamll:fix/double-compaction-guard
Closed

fix(session): prevent double compaction after auto-compact#26484
sgfvamll wants to merge 1 commit into
anomalyco:devfrom
sgfvamll:fix/double-compaction-guard

Conversation

@sgfvamll

@sgfvamll sgfvamll commented May 9, 2026

Copy link
Copy Markdown

Issue for this PR

Resolves #26230
Resolves #20621

Type of change

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

What does this PR do?

After auto-compaction, filterCompacted retains tail assistant messages that carry stale cumulative token counts (~100K). The backward scan in the prompt loop picks up these retained tails as lastFinished, and the overflow pre-check immediately fires a second compaction — every single time. This wastes tokens and doubles Copilot request consumption.

Root cause: The backward scan at prompt.ts:1418-1426 searches from the end of filterCompacted's output and breaks as soon as it finds lastUser + lastFinished. After compaction, the output is:

[0]  compaction_user        (COMPACTION part with tail_start_id)
[1]  compaction_summary     summary=true, finish=stop
[2-20] retained tail assistants  tokens=97K-103K, finish=tool-calls (stale)
[21] auto-continue user     tokens=0

The scan finds lastUser=[21] and lastFinished=[20] (a retained tail), then breaks — never reaching the compaction part at [0]. Since tasks is empty, the compaction task handler is skipped. The pre-check then sees lastFinished with stale 103K tokens, isOverflow returns true, and a second compaction fires.

Fix: Add a guard that only triggers the overflow pre-check when lastFinished is newer than the last compaction user message. Retained tails have IDs older than the compaction user, so they fail this check.

4-line production change in packages/opencode/src/session/prompt.ts.

How did you verify your code works?

  • Reproduced the bug empirically against a real session (587 messages, 19 compaction pairs — all doubled)
  • Imported the actual filterCompacted function and traced the exact output ordering and backward scan behavior
  • bun typecheck — clean
  • bun test test/session/prompt.test.ts --test-name-pattern "double-compact" — 1 pass
  • Regression test seeds a post-compaction state (compaction pair + auto-continue user) and asserts no second compaction is created and the model is called exactly once

Screenshots / recordings

N/A — no UI changes.

Checklist

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

@sgfvamll sgfvamll requested a review from adamdotdevin as a code owner May 9, 2026 06:56
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label May 9, 2026
@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

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

Potential Related PR Found:

This PR appears to be related as it also addresses double compaction issues in sessions. You should verify if PR #26235 overlaps with the fix in PR #26484 or if they address different scenarios of the same problem.

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

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

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

@sgfvamll sgfvamll force-pushed the fix/double-compaction-guard branch from e0aeead to 5c8b307 Compare May 9, 2026 07:00
After compaction, filterCompacted retains tail assistant messages with
stale cumulative token counts. The backward scan picks up these retained
tails as lastFinished, and the overflow pre-check fires a second
compaction immediately.

Add a guard: only trigger auto-compaction when lastFinished is newer
than the last compaction user message. Retained tails have older IDs,
so they fail this check.

Resolves anomalyco#26230
Resolves anomalyco#20621
@sgfvamll sgfvamll force-pushed the fix/double-compaction-guard branch from 5c8b307 to d5316de Compare May 9, 2026 07:05
@sgfvamll

sgfvamll commented May 13, 2026

Copy link
Copy Markdown
Author

I'm not sure if the root cause analysis is right. But it fixed the issue I encountered. May serve as a workround.
I'm not sure if this fix might cause other issues in some corner cases.

@github-actions

github-actions Bot commented Jun 9, 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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GitHub Copilot - Requests are double counted when compaction occurs Double compaction for Copilot Opus 4.7

1 participant