From ae687dd5fbb6b077c5fe7c82ea00d79beba7d43e Mon Sep 17 00:00:00 2001 From: "praisonai-triage-agent[bot]" <272766704+praisonai-triage-agent[bot]@users.noreply.github.com> Date: Fri, 17 Apr 2026 07:21:44 +0000 Subject: [PATCH] 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 --- .../praisonaiagents/session/store.py | 2 +- .../tests/unit/session/test_session_store.py | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/praisonai-agents/praisonaiagents/session/store.py b/src/praisonai-agents/praisonaiagents/session/store.py index 617574627..69e1fdbe6 100644 --- a/src/praisonai-agents/praisonaiagents/session/store.py +++ b/src/praisonai-agents/praisonaiagents/session/store.py @@ -490,7 +490,7 @@ def list_sessions(self, limit: int = 50) -> List[Dict[str, Any]]: pass # Sort by updated_at descending - sessions.sort(key=lambda x: x.get("updated_at", ""), reverse=True) + sessions.sort(key=lambda x: x.get("updated_at") or "", reverse=True) return sessions[:limit] # ── Agent-Level Queries (Gap S4) ────────────────────────────────── diff --git a/src/praisonai-agents/tests/unit/session/test_session_store.py b/src/praisonai-agents/tests/unit/session/test_session_store.py index 4d78850cb..a74bc71ab 100644 --- a/src/praisonai-agents/tests/unit/session/test_session_store.py +++ b/src/praisonai-agents/tests/unit/session/test_session_store.py @@ -332,6 +332,42 @@ def test_invalidate_cache(self, temp_store): temp_store.invalidate_cache("session-1") history = temp_store.get_chat_history("session-1") assert len(history) == 2 + + def test_list_sessions_with_none_updated_at(self, temp_store): + """Test list_sessions handles None updated_at values without crashing. + + Regression test for issue #1445 where sessions with updated_at=None + caused TypeError in sorting. + """ + # Create a session with a regular message + temp_store.add_user_message("session-1", "Hello") + + # Create another session and manually set updated_at to None + temp_store.add_user_message("session-2", "Hi") + filepath = os.path.join(temp_store.session_dir, "session-2.json") + + # Read the session file and set updated_at to None + with open(filepath, "r") as f: + data = json.load(f) + data["updated_at"] = None # Explicit None value + with open(filepath, "w") as f: + json.dump(data, f) + + # Clear cache to ensure file is re-read + temp_store.invalidate_cache("session-2") + + # This should not crash with TypeError + sessions = temp_store.list_sessions(limit=50) + + # Should return both sessions + assert len(sessions) == 2 + session_ids = [s["session_id"] for s in sessions] + assert "session-1" in session_ids + assert "session-2" in session_ids + + # Session with None updated_at should appear last (empty string sorts before timestamps) + assert sessions[-1]["session_id"] == "session-2" + assert sessions[-1]["updated_at"] is None class TestAgentSessionIntegration: