Skip to content

⚡ Bolt: optimize SessionManager database operations#59

Open
charles-forsyth wants to merge 1 commit intomainfrom
bolt-session-opt-5037856749770711362
Open

⚡ Bolt: optimize SessionManager database operations#59
charles-forsyth wants to merge 1 commit intomainfrom
bolt-session-opt-5037856749770711362

Conversation

@charles-forsyth
Copy link
Owner

💡 What:

  • Optimized SessionManager.list_sessions by using a LEFT JOIN to fetch parent session status and PID in a single query, eliminating the N+1 query problem.
  • Batched UPDATE statements for 'crashed' sessions into a single query using WHERE id IN (...).
  • Added database indexes on interaction_id, parent_id, and updated_at to improve lookup and sorting performance.
  • Refactored append_to_result to use a single SQL UPDATE with concatenation, eliminating a redundant SELECT.
  • Implemented a thread-safe class-level cache to skip redundant _init_db (schema check and migration) calls within the same process.

🎯 Why:

  • The list_sessions method was performing an additional SELECT query for every running child session to check its parent's status, leading to $O(N)$ database round-trips.
  • Frequent schema checks in every SessionManager instantiation were adding unnecessary overhead, especially in recursive research mode where many agents are spawned.
  • Large session histories would eventually slow down due to missing indexes on frequently queried columns.

📊 Impact:

  • Reduces database queries in list_sessions from $2N+1$ to constant $2$ queries in the worst case (1 SELECT, 1 batched UPDATE).
  • Measured a ~9x speedup in list_sessions for 100 sessions (from 0.086s to 0.010s).
  • Improves responsiveness of the list command and overall system efficiency.

🔬 Measurement:

  • Verified with a performance script that counts sqlite3.Connection.execute calls.
  • Confirmed that all existing 30 tests pass (uv run pytest).

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

- Use LEFT JOIN in list_sessions to avoid N+1 queries for parent status.
- Batch UPDATE statements for crashed sessions.
- Add indexes for interaction_id, parent_id, and updated_at.
- Refactor append_to_result to use a single UPDATE query.
- Add thread-safe initialization cache to skip redundant _init_db calls.

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