Skip to content

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

Closed as not planned

Description

@tobias-weiss-ai-xr

Bug

filterCompactedEffect reads every message from the DB (via paginated SQL) at the start of each runLoop iteration, then discards pre-compaction messages. For compacted sessions with hundreds of messages, this means re-reading and discarding the same pre-compaction messages on every tool call round-trip.

Details

In prompt.ts:1314, filterCompactedEffect(sessionID) is called every loop step. This calls stream(sessionID) which does ceil(N/50) paginated SQL queries for all N messages in the session. Then filterCompacted() scans forward to find the last compaction boundary and discards everything before it.

For a session with 500 messages (300 pre-compaction, 200 post-compaction), each iteration does ~10 SQL queries but only uses ~200 messages. The pre-compaction messages are read and discarded every time.

Suggested Fix

Cache the compaction boundary (first post-compaction message ID/timestamp) per session and pass it as a since parameter to stream() so it adds a WHERE clause to skip older rows. The cache invalidates when a new compaction is created.

This would reduce DB reads by ~60% per iteration for compacted sessions.

  • I've verified this exists in the latest version

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    perfIndicates a performance issue or need for optimization
    on Apr 12, 2026
  2. github-actions commented on Apr 12, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Please review the linked issues and consider whether this adds new information before continuing separately.

  3. removed
    perfIndicates a performance issue or need for optimization
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  4. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions