Skip to content

Commit 282e498

Browse files
author
dori
committed
fix: remove reformat
1 parent af6a4c6 commit 282e498

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

src/mcp_as_a_judge/db/cleanup_service.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
removing least recently used sessions when session limits are exceeded.
66
"""
77

8-
9-
108
from sqlalchemy import Engine, func
119
from sqlmodel import Session, select
1210

@@ -46,8 +44,6 @@ def __init__(self, engine: Engine) -> None:
4644
self.engine = engine
4745
self.max_total_sessions = MAX_TOTAL_SESSIONS
4846

49-
50-
5147
def get_session_count(self) -> int:
5248
"""
5349
Get the total number of unique sessions in the database.

src/mcp_as_a_judge/db/providers/sqlite_provider.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class SQLiteProvider(ConversationHistoryDB):
3030
- SQLModel with SQLAlchemy for type safety
3131
- In-memory or file-based SQLite storage
3232
- Two-level cleanup strategy:
33-
1. Session-based LRU cleanup (runs when new sessions are created, removes least recently used)
33+
1. Session-based LRU cleanup (runs when new sessions are created,
34+
removes least recently used)
3435
2. Per-session FIFO cleanup (max 20 records per session, runs on every save)
3536
- Session-based conversation retrieval
3637
"""
@@ -81,12 +82,11 @@ def _create_tables(self) -> None:
8182
SQLModel.metadata.create_all(self.engine)
8283
logger.info("📋 Created conversation_history table with SQLModel")
8384

84-
85-
8685
def _cleanup_excess_sessions(self) -> int:
8786
"""
8887
Remove least recently used sessions when total sessions exceed limit.
89-
This implements LRU cleanup to maintain session limit for better memory management.
88+
This implements LRU cleanup to maintain session limit for better memory
89+
management.
9090
Runs immediately when new sessions are created and limit is exceeded.
9191
"""
9292
return self._cleanup_service.cleanup_excess_sessions()
@@ -105,8 +105,8 @@ def _cleanup_old_messages(self, session_id: str) -> int:
105105
current_count = len(current_records)
106106

107107
logger.info(
108-
f"🧹 FIFO cleanup check for session {session_id}: {current_count} records "
109-
f"(max: {self._max_session_records})"
108+
f"🧹 FIFO cleanup check for session {session_id}: "
109+
f"{current_count} records (max: {self._max_session_records})"
110110
)
111111

112112
if current_count <= self._max_session_records:
@@ -137,7 +137,8 @@ def _cleanup_old_messages(self, session_id: str) -> int:
137137
session.commit()
138138

139139
logger.info(
140-
f"✅ LRU cleanup completed: removed {len(old_records)} records from session {session_id}"
140+
f"✅ LRU cleanup completed: removed {len(old_records)} records "
141+
f"from session {session_id}"
141142
)
142143
return len(old_records)
143144

@@ -187,7 +188,8 @@ async def save_conversation(
187188
logger.info(f"🆕 New session detected: {session_id}, running LRU cleanup")
188189
self._cleanup_excess_sessions()
189190

190-
# Per-session FIFO cleanup: maintain max 20 records per session (runs on every save)
191+
# Per-session FIFO cleanup: maintain max 20 records per session
192+
# (runs on every save)
191193
self._cleanup_old_messages(session_id)
192194

193195
return record_id

tests/test_conversation_history_lifecycle.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)