Skip to content

⚡ Bolt: Optimize SessionManager database operations#57

Open
charles-forsyth wants to merge 1 commit intomainfrom
bolt-optimize-db-6751822011168121911
Open

⚡ Bolt: Optimize SessionManager database operations#57
charles-forsyth wants to merge 1 commit intomainfrom
bolt-optimize-db-6751822011168121911

Conversation

@charles-forsyth
Copy link
Owner

⚡ Bolt: Optimize SessionManager database operations

💡 What

Implemented a series of optimizations in the SessionManager class to streamline database operations:

  • Redundant Initialization Cache: Added a class-level set _initialized_dbs to track and skip unnecessary schema checks and migrations for databases already processed in the current process.
  • N+1 Query Fix: Updated list_sessions to use a LEFT JOIN on the sessions table itself, fetching parent session information in the primary query instead of executing a separate query for each child session.
  • Batch Updates: Replaced individual UPDATE statements for crashed sessions with a single batched WHERE id IN (...) query, significantly reducing disk I/O and transaction overhead.
  • Database Indexing: Added indexes for interaction_id, parent_id, and updated_at to ensure fast lookups, hierarchical traversal, and sorted listings.

🎯 Why

The SessionManager was performing several inefficient database operations that slowed down the CLI (especially the list command) and the recursive research process (where many agent instances are created). Each PRAGMA check and individual UPDATE commit added latency.

📊 Impact

Measured using benchmark scripts:

  • Redundant Initializations: Reduced from 3 queries to 0 queries per subsequent SessionManager instance.
  • list_sessions Performance: For a typical scenario with 6 sessions (1 parent, 5 children), queries were reduced from 12 down to 2.
  • Scalability: Indexes ensure that performance remains consistent even as the session history grows to hundreds or thousands of entries.

🔬 Measurement

Verified using bench_init.py and bench_list_sessions.py (which proxy sqlite3 to count queries) and by running the full test suite with uv run pytest.


PR created automatically by Jules for task 6751822011168121911 started by @charles-forsyth

This PR implements several performance optimizations for the SessionManager:
1. Avoids redundant DB initialization using a class-level cache.
2. Resolves an N+1 query problem in list_sessions by using a JOIN.
3. Batches status updates for crashed sessions to reduce disk I/O.
4. Adds indexes for frequently queried columns (interaction_id, parent_id, updated_at).

Impact:
- Redundant initializations: 3 queries -> 0 queries.
- list_sessions (6 sessions): 12 queries -> 2 queries.
- Faster lookups and sorting as history grows.

Co-authored-by: charles-forsyth <[email protected]>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant