Skip to content

fix(session): avoid full history hydration after compaction - #31638

Open
ualtinok wants to merge 1 commit into
anomalyco:devfrom
ualtinok:fix/filter-compacted-history-load
Open

ualtinok wants to merge 1 commit into
anomalyco:devfrom
ualtinok:fix/filter-compacted-history-load

Conversation

@ualtinok

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #22208

Related: #31525, #25366, #24841, #31527

Type of change

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

What does this PR do?

MessageV2.filterCompactedEffect(sessionID) used to stream the entire session history, then discard messages before the latest completed compaction boundary.

This changes the DB-backed path to page newest-first and stop once the compacted boundary has been found. The in-memory filterCompacted(...) behavior is kept shared through the same scan state, so the returned message list stays equivalent without hydrating pre-compaction history from the database.

Unlike #31527, this does not cache messages across prompt-loop iterations. It only reduces the amount of history loaded for already-compacted sessions.

How did you verify your code works?

  • bun test test/session/messages-pagination.test.ts
  • bun typecheck from packages/opencode
  • pre-push bun turbo typecheck

Screenshots / recordings

N/A — backend performance fix.

Checklist

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

@ualtinok
ualtinok force-pushed the fix/filter-compacted-history-load branch from b17491c to 0e96d57 Compare July 15, 2026 07:18
randomvariable added a commit to randomvariable/opencode that referenced this pull request Aug 3, 2026
Factor the compaction filter into an incremental scan and have
filterCompactedEffect page backward through messages, stopping as soon as a
completed compaction boundary (and any retained tail) is found, instead of
hydrating the entire session history first.

Ported from anomalyco#31638 (already Effect-based; applied as-is).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Qiiks

Qiiks commented Aug 6, 2026

Copy link
Copy Markdown

Cross-linking: PR #39930 (brauliobo, July 31) implements the same fix for the same function (filterCompactedEffect), with a leaner approach worth considering before merging either:

Recommendation: consolidate on #39930 (or port its messageRows-first walk into #31638's shared scan-state structure — that combination is the best of both). Having both open means one will likely be closed as a duplicate by the maintainers anyway.

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.

perf: session loop re-reads all messages from DB every iteration

2 participants