-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(ltm): Web UI 删除对话/平台后 LTM 记忆未清理 & unique_session 下群聊记录隔离 (#8386) #8421
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
Open
Ayleovelle
wants to merge
2
commits into
AstrBotDevs:master
Choose a base branch
from
Ayleovelle:fix/ltm-session-cleanup-8386
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,175 @@ | ||||||||||||||||||||||||||||
| """测试 Web UI 删除对话后 LTM session_chats 是否被正确清理 (Issue #8386 Bug 1)""" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||
| import pytest_asyncio | ||||||||||||||||||||||||||||
| from collections import defaultdict | ||||||||||||||||||||||||||||
| from unittest.mock import AsyncMock, MagicMock | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from astrbot.core.conversation_mgr import ConversationManager | ||||||||||||||||||||||||||||
| from astrbot.core.platform.message_type import MessageType | ||||||||||||||||||||||||||||
| from astrbot.builtin_stars.astrbot.long_term_memory import LongTermMemory | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest_asyncio.fixture | ||||||||||||||||||||||||||||
| async def conversation_manager(): | ||||||||||||||||||||||||||||
| db = AsyncMock() | ||||||||||||||||||||||||||||
| db.delete_conversation = AsyncMock() | ||||||||||||||||||||||||||||
| db.get_conversation_by_id = AsyncMock(return_value=None) | ||||||||||||||||||||||||||||
| mgr = ConversationManager(db) | ||||||||||||||||||||||||||||
| return mgr | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest.fixture | ||||||||||||||||||||||||||||
| def ltm(): | ||||||||||||||||||||||||||||
| acm = MagicMock() | ||||||||||||||||||||||||||||
| context = MagicMock() | ||||||||||||||||||||||||||||
| context.get_config = MagicMock(return_value={ | ||||||||||||||||||||||||||||
| "provider_ltm_settings": { | ||||||||||||||||||||||||||||
| "group_message_max_cnt": 300, | ||||||||||||||||||||||||||||
| "image_caption": False, | ||||||||||||||||||||||||||||
| "image_caption_provider_id": "", | ||||||||||||||||||||||||||||
| "active_reply": {"enable": False, "method": "possibility_reply", "possibility_reply": 0.1}, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| "provider_settings": {"image_caption_prompt": ""}, | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| return LongTermMemory(acm, context) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||
| async def test_delete_conversation_triggers_session_deleted_callback(conversation_manager): | ||||||||||||||||||||||||||||
| """验证 delete_conversation 会触发 _on_session_deleted_callbacks""" | ||||||||||||||||||||||||||||
| callback = AsyncMock() | ||||||||||||||||||||||||||||
| conversation_manager.register_on_session_deleted(callback) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| umo = "feishu:group:test_group_123" | ||||||||||||||||||||||||||||
| conversation_manager.session_conversations[umo] = "conv-id-1" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await conversation_manager.delete_conversation( | ||||||||||||||||||||||||||||
| unified_msg_origin=umo, | ||||||||||||||||||||||||||||
| conversation_id="conv-id-1", | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| callback.assert_called_once_with(umo) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||
| async def test_delete_conversation_clears_ltm_session_chats(conversation_manager, ltm): | ||||||||||||||||||||||||||||
| """模拟完整流程:LTM 注册回调后,Web UI 删除对话应清理 session_chats""" | ||||||||||||||||||||||||||||
| umo = "feishu:group:test_group_456" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 模拟群聊中已有 LTM 记录 | ||||||||||||||||||||||||||||
| ltm.session_chats[umo] = [ | ||||||||||||||||||||||||||||
| "[Alice/10:00:00]: 你好", | ||||||||||||||||||||||||||||
| "[Bob/10:01:00]: 你好啊", | ||||||||||||||||||||||||||||
| "[You/10:01:30]: 大家好!", | ||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 注册回调(和 main.py 中的逻辑一致) | ||||||||||||||||||||||||||||
| async def _clear_ltm_session(origin: str) -> None: | ||||||||||||||||||||||||||||
| ltm.session_chats.pop(origin, None) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| conversation_manager.register_on_session_deleted(_clear_ltm_session) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 模拟当前会话指向该对话 | ||||||||||||||||||||||||||||
| conversation_manager.session_conversations[umo] = "conv-id-2" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 执行删除(Web UI 路径) | ||||||||||||||||||||||||||||
| await conversation_manager.delete_conversation( | ||||||||||||||||||||||||||||
| unified_msg_origin=umo, | ||||||||||||||||||||||||||||
| conversation_id="conv-id-2", | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 验证 LTM 内存已清理 | ||||||||||||||||||||||||||||
| assert umo not in ltm.session_chats | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||
| async def test_ltm_on_req_llm_skips_after_session_cleared(conversation_manager, ltm): | ||||||||||||||||||||||||||||
| """删除对话后,on_req_llm 不应再注入已删除的历史到 system_prompt""" | ||||||||||||||||||||||||||||
| umo = "lark:GroupMessage:test_group_789" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 模拟已有 LTM 记录(存储在 group-level key 下) | ||||||||||||||||||||||||||||
| ltm.session_chats[umo] = [ | ||||||||||||||||||||||||||||
| "[User1/09:00:00]: 之前的秘密对话", | ||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| async def _clear_ltm_session(origin: str) -> None: | ||||||||||||||||||||||||||||
| ltm.session_chats.pop(origin, None) | ||||||||||||||||||||||||||||
| parts = origin.split(":") | ||||||||||||||||||||||||||||
| if len(parts) >= 3 and parts[1] == "GroupMessage": | ||||||||||||||||||||||||||||
| group_id = parts[2].split("%")[-1] | ||||||||||||||||||||||||||||
| group_key = f"{parts[0]}:GroupMessage:{group_id}" | ||||||||||||||||||||||||||||
| ltm.session_chats.pop(group_key, None) | ||||||||||||||||||||||||||||
|
Comment on lines
+96
to
+102
Contributor
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
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| conversation_manager.register_on_session_deleted(_clear_ltm_session) | ||||||||||||||||||||||||||||
| conversation_manager.session_conversations[umo] = "conv-id-3" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 删除对话 | ||||||||||||||||||||||||||||
| await conversation_manager.delete_conversation( | ||||||||||||||||||||||||||||
| unified_msg_origin=umo, | ||||||||||||||||||||||||||||
| conversation_id="conv-id-3", | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 模拟后续 LLM 请求 | ||||||||||||||||||||||||||||
| event = MagicMock() | ||||||||||||||||||||||||||||
| event.unified_msg_origin = umo | ||||||||||||||||||||||||||||
| event.get_message_type.return_value = MessageType.GROUP_MESSAGE | ||||||||||||||||||||||||||||
| event.get_group_id.return_value = "test_group_789" | ||||||||||||||||||||||||||||
| event.get_platform_id.return_value = "lark" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| req = MagicMock() | ||||||||||||||||||||||||||||
| req.system_prompt = "You are a helpful assistant." | ||||||||||||||||||||||||||||
| req.prompt = "你好" | ||||||||||||||||||||||||||||
| req.contexts = [] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| await ltm.on_req_llm(event, req) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # system_prompt 不应包含已删除的历史 | ||||||||||||||||||||||||||||
| assert "秘密对话" not in req.system_prompt | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||
| async def test_delete_other_conversation_does_not_affect_unrelated_session(conversation_manager, ltm): | ||||||||||||||||||||||||||||
| """删除某个 session 的对话不应影响其他 session 的 LTM 记录""" | ||||||||||||||||||||||||||||
| umo_a = "feishu:group:group_a" | ||||||||||||||||||||||||||||
| umo_b = "feishu:group:group_b" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ltm.session_chats[umo_a] = ["[A/10:00:00]: hello"] | ||||||||||||||||||||||||||||
| ltm.session_chats[umo_b] = ["[B/10:00:00]: world"] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| async def _clear_ltm_session(origin: str) -> None: | ||||||||||||||||||||||||||||
| ltm.session_chats.pop(origin, None) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| conversation_manager.register_on_session_deleted(_clear_ltm_session) | ||||||||||||||||||||||||||||
| conversation_manager.session_conversations[umo_a] = "conv-a" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 只删除 group_a | ||||||||||||||||||||||||||||
| await conversation_manager.delete_conversation( | ||||||||||||||||||||||||||||
| unified_msg_origin=umo_a, | ||||||||||||||||||||||||||||
| conversation_id="conv-a", | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| assert umo_a not in ltm.session_chats | ||||||||||||||||||||||||||||
| assert ltm.session_chats[umo_b] == ["[B/10:00:00]: world"] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||
| async def test_group_key_ignores_unique_session(ltm): | ||||||||||||||||||||||||||||
| """Bug 3: unique_session 开启时,不同用户的 group_key 应相同(都指向群级别)""" | ||||||||||||||||||||||||||||
| # 用户 A 的 event(unique_session 改写了 unified_msg_origin) | ||||||||||||||||||||||||||||
| event_a = MagicMock() | ||||||||||||||||||||||||||||
| event_a.unified_msg_origin = "lark:GroupMessage:userA%group123" | ||||||||||||||||||||||||||||
| event_a.get_message_type.return_value = MessageType.GROUP_MESSAGE | ||||||||||||||||||||||||||||
| event_a.get_group_id.return_value = "group123" | ||||||||||||||||||||||||||||
| event_a.get_platform_id.return_value = "lark" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 用户 B 的 event | ||||||||||||||||||||||||||||
| event_b = MagicMock() | ||||||||||||||||||||||||||||
| event_b.unified_msg_origin = "lark:GroupMessage:userB%group123" | ||||||||||||||||||||||||||||
| event_b.get_message_type.return_value = MessageType.GROUP_MESSAGE | ||||||||||||||||||||||||||||
| event_b.get_group_id.return_value = "group123" | ||||||||||||||||||||||||||||
| event_b.get_platform_id.return_value = "lark" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # 两者的 group_key 应该相同 | ||||||||||||||||||||||||||||
| assert ltm._group_key(event_a) == ltm._group_key(event_b) | ||||||||||||||||||||||||||||
| assert ltm._group_key(event_a) == "lark:GroupMessage:group123" | ||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
在开启
unique_session时,umo的第三部分(即parts[2])会被改写为user_id%group_id的格式。如果直接使用
parts[2]作为group_key的一部分,那么生成的group_key将会是platform:GroupMessage:user_id%group_id。然而,在
LongTermMemory._group_key中,群聊的 key 是通过event.get_group_id()获取的,即platform:GroupMessage:group_id。这会导致
_clear_ltm_session无法正确清理实际存储在session_chats中的群聊记忆(因为 key 不匹配)。建议通过
parts[2].split("%")[-1]提取出真正的group_id,以确保在unique_session开启和关闭的情况下都能正确清理 LTM 记忆。Uh oh!
There was an error while loading. Please reload this page.
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.
已修复
现在把
parts[2]改成了parts[2].split("%")[-1]。当unique_session关闭时 parts[2] 就是纯group_id,split("%")[-1]结果不变;开启时parts[2]是userId%groupId,split("%")[-1]取到最后一段就是真正的group_id。