@@ -203,7 +203,9 @@ async def test_immediate_cleanup_integration(self):
203203 # Should still have only 2 sessions (LRU cleanup triggered)
204204 final_count = db ._cleanup_service .get_session_count ()
205205 assert final_count == 2
206- print (f"✅ After immediate LRU cleanup: { final_count } sessions (limit maintained)" )
206+ print (
207+ f"✅ After immediate LRU cleanup: { final_count } sessions (limit maintained)"
208+ )
207209
208210 @pytest .mark .asyncio
209211 async def test_lru_session_cleanup_lifecycle (self ):
@@ -242,7 +244,8 @@ async def test_lru_session_cleanup_lifecycle(self):
242244 )
243245 print (" Session A: Updated with recent activity (now most recently used)" )
244246
245- # Session D: Created fourth (should trigger cleanup, but Session A should be preserved)
247+ # Session D: Created fourth (should trigger cleanup, but Session A
248+ # should be preserved)
246249 await db .save_conversation ("session_D" , "tool1" , "input1" , "output1" )
247250 print (" Session D: Created (cleanup triggered)" )
248251
@@ -252,24 +255,39 @@ async def test_lru_session_cleanup_lifecycle(self):
252255
253256 # Verify automatic LRU cleanup happened (should be at limit of 3)
254257 current_count = db ._cleanup_service .get_session_count ()
255- assert current_count == 3 , f"Expected 3 sessions after automatic cleanup, got { current_count } "
256- print (f"✅ Phase 1: Automatic LRU cleanup maintained limit - { current_count } sessions remain" )
258+ assert current_count == 3 , (
259+ f"Expected 3 sessions after automatic cleanup, got { current_count } "
260+ )
261+ print (
262+ f"✅ Phase 1: Automatic LRU cleanup maintained limit - "
263+ f"{ current_count } sessions remain"
264+ )
257265
258266 # PHASE 2: Verify which sessions remain after automatic cleanup
259267 print ("\n 🔍 PHASE 2: Verifying remaining sessions after automatic cleanup..." )
260268
261269 # Check which sessions still exist
262270 remaining_sessions = []
263- for session_id in ["session_A" , "session_B" , "session_C" , "session_D" , "session_E" ]:
271+ for session_id in [
272+ "session_A" ,
273+ "session_B" ,
274+ "session_C" ,
275+ "session_D" ,
276+ "session_E"
277+ ]:
264278 records = await db .get_session_conversations (session_id )
265279 if records :
266280 remaining_sessions .append (session_id )
267281
268282 print (f" Remaining sessions: { remaining_sessions } " )
269- assert len (remaining_sessions ) == 3 , f"Expected 3 remaining sessions, got { len (remaining_sessions )} "
283+ assert len (remaining_sessions ) == 3 , (
284+ f"Expected 3 remaining sessions, got { len (remaining_sessions )} "
285+ )
270286
271287 # Session A should remain initially (preserved due to recent activity)
272- assert "session_A" in remaining_sessions , "Session A should remain initially (most recently used)"
288+ assert "session_A" in remaining_sessions , (
289+ "Session A should remain initially (most recently used)"
290+ )
273291 print ("✅ Phase 2: Session A initially preserved due to recent activity" )
274292
275293 # PHASE 3: Test that cleanup maintains the limit
@@ -280,8 +298,12 @@ async def test_lru_session_cleanup_lifecycle(self):
280298
281299 # Should still be at limit
282300 final_count = db ._cleanup_service .get_session_count ()
283- assert final_count == 3 , f"Expected 3 sessions after adding new session, got { final_count } "
284- print (f"✅ Phase 3: Session limit maintained at { final_count } after adding new session" )
301+ assert final_count == 3 , (
302+ f"Expected 3 sessions after adding new session, got { final_count } "
303+ )
304+ print (
305+ f"✅ Phase 3: Session limit maintained at { final_count } after adding new session"
306+ )
285307
286308 # PHASE 4: Verify which sessions remain after adding session_F
287309 print ("\n 🔍 PHASE 4: Verifying final remaining sessions..." )
@@ -303,14 +325,17 @@ async def test_lru_session_cleanup_lifecycle(self):
303325 final_remaining_sessions .append (session_id )
304326 last_activity = max (r .timestamp for r in records )
305327 print (
306- f" ✅ { session_id } : { len (records )} records, last activity: { last_activity } "
328+ f" ✅ { session_id } : { len (records )} records, "
329+ f"last activity: { last_activity } "
307330 )
308331 else :
309332 final_deleted_sessions .append (session_id )
310333 print (f" ❌ { session_id } : DELETED (was least recently used)" )
311334
312335 # Verify we have exactly 3 sessions
313- assert len (final_remaining_sessions ) == 3 , f"Expected 3 remaining sessions, got { len (final_remaining_sessions )} "
336+ assert len (final_remaining_sessions ) == 3 , (
337+ f"Expected 3 remaining sessions, got { len (final_remaining_sessions )} "
338+ )
314339
315340 # The 3 most recently used sessions should remain (D, E, F)
316341 # Session A gets removed because even though it was updated,
@@ -321,21 +346,28 @@ async def test_lru_session_cleanup_lifecycle(self):
321346 f"Expected { expected_remaining } , got { actual_remaining } "
322347 )
323348
324- print ("✅ Phase 4: LRU cleanup working correctly - most recent sessions preserved" )
349+ print (
350+ "✅ Phase 4: LRU cleanup working correctly - most recent sessions preserved"
351+ )
325352
326353 # PHASE 5: Verify that LRU cleanup works correctly over time
327354 print ("\n 📊 PHASE 5: Verifying LRU behavior over time..." )
328355
329356 # Session A was initially preserved but then removed when Session F was created
330357 # This demonstrates that LRU is based on actual timestamps, not update sequence
331358 print (" - Session A was initially preserved due to recent activity" )
332- print (" - Session A was later removed when newer sessions (D, E, F) became more recent" )
359+ print (
360+ " - Session A was later removed when newer sessions (D, E, F) "
361+ "became more recent"
362+ )
333363 print (" - This shows LRU is working correctly based on actual timestamps" )
334364
335365 print ("\n 🎯 IMMEDIATE LRU CLEANUP SUMMARY:" )
336366 print (" - Cleanup happens immediately when new sessions are created" )
337367 print (" - Session limit is maintained at all times (no daily delay)" )
338- print (" - Most recently used sessions are preserved based on actual timestamps" )
368+ print (
369+ " - Most recently used sessions are preserved based on actual timestamps"
370+ )
339371 print (" - LRU correctly removes sessions with older last activity times" )
340372 print (" - LRU provides better UX than FIFO by preserving active sessions!" )
341373 print ("✅ Immediate LRU session cleanup lifecycle test PASSED!" )
0 commit comments