Skip to content

⚡ Bolt: optimize SessionManager initialization and queries#58

Open
charles-forsyth wants to merge 1 commit intomainfrom
bolt-optimize-session-manager-5935941829814978723
Open

⚡ Bolt: optimize SessionManager initialization and queries#58
charles-forsyth wants to merge 1 commit intomainfrom
bolt-optimize-session-manager-5935941829814978723

Conversation

@charles-forsyth
Copy link
Owner

💡 What:

Optimized the SessionManager class to reduce database overhead and resolve an N+1 query problem in the session listing logic.

🎯 Why:

  • list_sessions was performing a separate query for every running child session to check its parent's status.
  • Multiple individual UPDATE and commit() calls were being made in a loop for crashed sessions.
  • _init_db (including migrations and PRAGMAs) was running every time a SessionManager was instantiated, which happens frequently in recursive research.
  • Missing indexes were slowing down sorting and filtering by interaction_id, parent_id, and updated_at.

📊 Impact:

  • Reduces list_sessions query count from O(N) to O(1) for fetching session data.
  • Reduces list_sessions update count from O(M) to O(1) for marking crashed sessions.
  • Eliminates redundant database initialization overhead in the same process (average ~0.5ms saved per instantiation).
  • Improves lookup and sorting performance as the database grows via new indexes.

🔬 Measurement:

Verified using tests/test_performance.py, which uses a proxy connection to count exact database queries. Results:

  • list_sessions query count for 5 child sessions: 6 -> 1
  • list_sessions update count for 5 crashed sessions: 5 -> 1
  • Redundant init queries: 3+ -> 0
  • All 33 tests passed in the full suite.

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

This commit implements several performance improvements to the SessionManager:
- Resolves the N+1 query problem in list_sessions using a LEFT JOIN to fetch parent info.
- Batches UPDATE statements for crashed sessions into a single query.
- Adds database indexes to interaction_id, parent_id, and updated_at for faster lookups.
- Implements a class-level cache to skip redundant database initialization/migrations.

A new performance test suite (tests/test_performance.py) is included to verify these improvements.

Co-authored-by: charles-forsyth <54458298+charles-forsyth@users.noreply.github.com>
@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