Skip to content

Commit ae687dd

Browse files
fix: handle None updated_at in DefaultSessionStore.list_sessions()
- Fix TypeError when sessions have updated_at=None by using 'or' operator - Add regression test for None updated_at values in session store - Ensures sessions with None updated_at sort last (as intended) Fixes #1445
1 parent 220d0d5 commit ae687dd

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/praisonai-agents/praisonaiagents/session/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def list_sessions(self, limit: int = 50) -> List[Dict[str, Any]]:
490490
pass
491491

492492
# Sort by updated_at descending
493-
sessions.sort(key=lambda x: x.get("updated_at", ""), reverse=True)
493+
sessions.sort(key=lambda x: x.get("updated_at") or "", reverse=True)
494494
return sessions[:limit]
495495

496496
# ── Agent-Level Queries (Gap S4) ──────────────────────────────────

src/praisonai-agents/tests/unit/session/test_session_store.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,42 @@ def test_invalidate_cache(self, temp_store):
332332
temp_store.invalidate_cache("session-1")
333333
history = temp_store.get_chat_history("session-1")
334334
assert len(history) == 2
335+
336+
def test_list_sessions_with_none_updated_at(self, temp_store):
337+
"""Test list_sessions handles None updated_at values without crashing.
338+
339+
Regression test for issue #1445 where sessions with updated_at=None
340+
caused TypeError in sorting.
341+
"""
342+
# Create a session with a regular message
343+
temp_store.add_user_message("session-1", "Hello")
344+
345+
# Create another session and manually set updated_at to None
346+
temp_store.add_user_message("session-2", "Hi")
347+
filepath = os.path.join(temp_store.session_dir, "session-2.json")
348+
349+
# Read the session file and set updated_at to None
350+
with open(filepath, "r") as f:
351+
data = json.load(f)
352+
data["updated_at"] = None # Explicit None value
353+
with open(filepath, "w") as f:
354+
json.dump(data, f)
355+
356+
# Clear cache to ensure file is re-read
357+
temp_store.invalidate_cache("session-2")
358+
359+
# This should not crash with TypeError
360+
sessions = temp_store.list_sessions(limit=50)
361+
362+
# Should return both sessions
363+
assert len(sessions) == 2
364+
session_ids = [s["session_id"] for s in sessions]
365+
assert "session-1" in session_ids
366+
assert "session-2" in session_ids
367+
368+
# Session with None updated_at should appear last (empty string sorts before timestamps)
369+
assert sessions[-1]["session_id"] == "session-2"
370+
assert sessions[-1]["updated_at"] is None
335371

336372

337373
class TestAgentSessionIntegration:

0 commit comments

Comments
 (0)