perf: lazy session creation — defer DB write until first message (#314) #449
Reference in New Issue
Block a user
Delete Branch "whip/314-1776127532"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 callscreate_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 existingensure_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)create_session()directly — they have messages to write immediatelyImpact
Eliminates ~3,564 wasted session records at the source. Zero behavior change for sessions that actually produce messages.