perf: lazy session creation — defer DB write until first message (#314) #449

Merged
Rockachopa merged 1 commits from whip/314-1776127532 into main 2026-04-14 01:08:14 +00:00
Owner

Closes #314

Problem

32.4% of all sessions (3,564 of 10,985) are completely empty — created at agent init but never used. Opus 4.6 has a 56.8% empty rate.

Root Cause

AIAgent.__init__() eagerly calls create_session() for every agent instance, regardless of whether any messages will ever be sent. Agents that time out, fail on API errors, or are instantiated for subagent dispatch that never runs — all produce empty session rows.

Fix

Remove the eager create_session() block from __init__(). Session creation is deferred until _flush_messages_to_session_db() is called with the first batch of messages. The existing ensure_session() fallback (INSERT OR IGNORE) becomes the primary creation path.

Changes

  • run_agent.py: -24 lines (remove eager create_session block), +4 lines (lazy creation comment)
  • Compression-initiated sessions still use create_session() directly — they have messages to write immediately

Impact

Eliminates ~3,564 wasted session records at the source. Zero behavior change for sessions that actually produce messages.

Closes #314 ## Problem 32.4% of all sessions (3,564 of 10,985) are completely empty — created at agent init but never used. Opus 4.6 has a 56.8% empty rate. ## Root Cause `AIAgent.__init__()` eagerly calls `create_session()` for every agent instance, regardless of whether any messages will ever be sent. Agents that time out, fail on API errors, or are instantiated for subagent dispatch that never runs — all produce empty session rows. ## Fix Remove the eager `create_session()` block from `__init__()`. Session creation is deferred until `_flush_messages_to_session_db()` is called with the first batch of messages. The existing `ensure_session()` fallback (INSERT OR IGNORE) becomes the primary creation path. ## Changes - `run_agent.py`: -24 lines (remove eager create_session block), +4 lines (lazy creation comment) - Compression-initiated sessions still use `create_session()` directly — they have messages to write immediately ## Impact Eliminates ~3,564 wasted session records at the source. Zero behavior change for sessions that actually produce messages.
Rockachopa added 1 commit 2026-04-14 00:52:47 +00:00
perf: lazy session creation — defer DB write until first message (closes #314)
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 56s
f35f56e397
Remove eager create_session() call from AIAgent.__init__(). Sessions
are now created lazily on first _flush_messages_to_session_db() call
via ensure_session() which uses INSERT OR IGNORE.

Impact: eliminates 32.4% of sessions (3,564 of 10,985) that were
created at agent init but never received any messages.

The existing ensure_session() fallback in _flush_messages_to_session_db()
already handles this pattern — it was originally designed for recovery
after transient SQLite lock failures. Now it's the primary creation path.

Compression-initiated sessions still use create_session() directly
(line ~5995) since they have messages to write immediately.
Rockachopa merged commit 954fd992eb into main 2026-04-14 01:08:14 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Timmy_Foundation/hermes-agent#449