-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: prevent session message loss in set_agent_info and clear_session #1724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -305,6 +305,64 @@ def test_update_session_metadata_preserves_messages(self, temp_store): | |||||||||
| session = writer.get_session("session-1") | ||||||||||
| assert session.metadata.get("model") == "gpt-4o-mini" | ||||||||||
| assert session.metadata.get("total_tokens") == 42 | ||||||||||
|
|
||||||||||
| def test_set_agent_info_preserves_messages(self, temp_store): | ||||||||||
| """Agent info updates must not drop messages added by another store instance.""" | ||||||||||
| with tempfile.TemporaryDirectory() as tmpdir: | ||||||||||
| writer = DefaultSessionStore(session_dir=tmpdir) | ||||||||||
| reader = DefaultSessionStore(session_dir=tmpdir) | ||||||||||
|
|
||||||||||
| writer.add_user_message("session-1", "first") | ||||||||||
| reader._load_session("session-1") | ||||||||||
| writer.add_user_message("session-1", "second") | ||||||||||
|
|
||||||||||
| assert reader.set_agent_info("session-1", agent_name="TestBot", user_id="u-1") | ||||||||||
|
|
||||||||||
| writer.invalidate_cache("session-1") | ||||||||||
| history = writer.get_chat_history("session-1") | ||||||||||
| assert len(history) == 2 | ||||||||||
| assert history[1]["content"] == "second" | ||||||||||
|
|
||||||||||
| session = writer.get_session("session-1") | ||||||||||
| assert session.agent_name == "TestBot" | ||||||||||
| assert session.user_id == "u-1" | ||||||||||
|
|
||||||||||
| def test_clear_session_preserves_new_messages(self, temp_store): | ||||||||||
| """Clear must reload from disk so concurrent adds are not lost.""" | ||||||||||
|
Comment on lines
+330
to
+331
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| with tempfile.TemporaryDirectory() as tmpdir: | ||||||||||
| writer = DefaultSessionStore(session_dir=tmpdir) | ||||||||||
| reader = DefaultSessionStore(session_dir=tmpdir) | ||||||||||
|
|
||||||||||
| writer.add_user_message("session-1", "first") | ||||||||||
| reader._load_session("session-1") | ||||||||||
| writer.add_user_message("session-1", "second") | ||||||||||
|
|
||||||||||
| assert reader.clear_session("session-1") | ||||||||||
|
|
||||||||||
| writer.invalidate_cache("session-1") | ||||||||||
| history = writer.get_chat_history("session-1") | ||||||||||
| assert len(history) == 0 | ||||||||||
|
|
||||||||||
| def test_set_gateway_info_preserves_messages(self, temp_store): | ||||||||||
| """Gateway info updates must not drop messages added by another store instance.""" | ||||||||||
| with tempfile.TemporaryDirectory() as tmpdir: | ||||||||||
| writer = DefaultSessionStore(session_dir=tmpdir) | ||||||||||
| reader = DefaultSessionStore(session_dir=tmpdir) | ||||||||||
|
|
||||||||||
| writer.add_user_message("session-1", "first") | ||||||||||
| reader._load_session("session-1") | ||||||||||
| writer.add_user_message("session-1", "second") | ||||||||||
|
|
||||||||||
| assert reader.set_gateway_info("session-1", gateway_session_id="gw-123", agent_id="agent-456") | ||||||||||
|
|
||||||||||
| writer.invalidate_cache("session-1") | ||||||||||
| history = writer.get_chat_history("session-1") | ||||||||||
| assert len(history) == 2 | ||||||||||
| assert history[1]["content"] == "second" | ||||||||||
|
|
||||||||||
| session = writer.get_session("session-1") | ||||||||||
| assert session.gateway_session_id == "gw-123" | ||||||||||
| assert session.agent_id == "agent-456" | ||||||||||
|
|
||||||||||
| def test_concurrent_writes(self, temp_store): | ||||||||||
| """Test concurrent writes to same session.""" | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_load_session_from_disksilently swallowsjson.JSONDecodeErrorandIOErrorwithout any log output, whereas the original_load_sessionlogs a warning. When a session file is corrupt or unreadable, the error will be invisible, making it very hard to diagnose data-loss incidents in production.