mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
fix: reclaim conversation resources on deletion (#40792)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
fc7b51634d
commit
dfac3e524e
15 files changed
+785
-52
No files matched your search
@@ -162,6 +162,8 @@ class TestCelerySSLConfiguration:
|
||||
|
||||
# Mock all the scheduler configs
|
||||
mock_config.CELERY_BEAT_SCHEDULER_TIME = 1
|
||||
mock_config.ENABLE_CONVERSATION_CLEANUP_TASK = False
|
||||
mock_config.CONVERSATION_CLEANUP_TASK_INTERVAL = 5
|
||||
mock_config.ENABLE_CLEAN_EMBEDDING_CACHE_TASK = False
|
||||
mock_config.ENABLE_CLEAN_UNUSED_DATASETS_TASK = False
|
||||
mock_config.ENABLE_CREATE_TIDB_SERVERLESS_TASK = False
|
||||
@@ -195,7 +197,7 @@ class TestCelerySSLConfiguration:
|
||||
assert "redis_backend_use_ssl" in celery_app.conf
|
||||
assert celery_app.conf["redis_backend_use_ssl"] is not None
|
||||
|
||||
def test_celery_init_applies_global_keyprefix_and_registers_agent_resource_collector(self):
|
||||
def test_celery_init_registers_required_agent_and_conversation_tasks(self):
|
||||
mock_config = MagicMock()
|
||||
mock_config.BROKER_USE_SSL = False
|
||||
mock_config.REDIS_KEY_PREFIX = "enterprise-a"
|
||||
@@ -210,6 +212,8 @@ class TestCelerySSLConfiguration:
|
||||
mock_config.CELERY_TASK_ANNOTATIONS = {}
|
||||
|
||||
mock_config.CELERY_BEAT_SCHEDULER_TIME = 1
|
||||
mock_config.ENABLE_CONVERSATION_CLEANUP_TASK = True
|
||||
mock_config.CONVERSATION_CLEANUP_TASK_INTERVAL = 5
|
||||
mock_config.ENABLE_CLEAN_EMBEDDING_CACHE_TASK = False
|
||||
mock_config.ENABLE_CLEAN_UNUSED_DATASETS_TASK = False
|
||||
mock_config.ENABLE_CREATE_TIDB_SERVERLESS_TASK = False
|
||||
@@ -241,3 +245,7 @@ class TestCelerySSLConfiguration:
|
||||
assert celery_app.conf["broker_transport_options"]["global_keyprefix"] == "enterprise-a:"
|
||||
assert celery_app.conf["result_backend_transport_options"]["global_keyprefix"] == "enterprise-a:"
|
||||
assert "tasks.collect_agent_resources_task" in celery_app.conf["imports"]
|
||||
assert "tasks.delete_conversation_task" in celery_app.conf["imports"]
|
||||
assert celery_app.conf["beat_schedule"]["conversation_cleanup_sweeper"]["task"] == (
|
||||
"tasks.delete_conversation_task.sweep_deleted_conversations"
|
||||
)
|
||||
@@ -91,11 +91,11 @@ def _seed_agent(*, tenant_id: str = TENANT, agent_id: str = AGENT) -> None:
|
||||
session.commit()
|
||||
|
||||
|
||||
def _seed_tool_file(*, user_id: str = USER, name: str = "f.txt") -> str:
|
||||
def _seed_tool_file(*, user_id: str = USER, name: str = "f.txt", conversation_id: str | None = None) -> str:
|
||||
tool_file = ToolFile(
|
||||
user_id=user_id,
|
||||
tenant_id=TENANT,
|
||||
conversation_id=None,
|
||||
conversation_id=conversation_id,
|
||||
file_key=f"tools/{TENANT}/{name}",
|
||||
mimetype="text/plain",
|
||||
name=name,
|
||||
@@ -156,6 +156,30 @@ def test_commit_then_manifest_lists_the_entry():
|
||||
)
|
||||
|
||||
|
||||
def test_commit_owned_tool_file_detaches_conversation_ownership():
|
||||
conversation_id = "44444444-4444-4444-4444-444444444444"
|
||||
tool_file_id = _seed_tool_file(conversation_id=conversation_id)
|
||||
|
||||
_commit("data/report.txt", tool_file_id, owned=True)
|
||||
|
||||
with session_factory.create_session() as session:
|
||||
tool_file = session.get(ToolFile, tool_file_id)
|
||||
assert tool_file is not None
|
||||
assert tool_file.conversation_id is None
|
||||
|
||||
|
||||
def test_commit_shared_tool_file_keeps_conversation_ownership():
|
||||
conversation_id = "44444444-4444-4444-4444-444444444444"
|
||||
tool_file_id = _seed_tool_file(conversation_id=conversation_id)
|
||||
|
||||
_commit("data/report.txt", tool_file_id, owned=False)
|
||||
|
||||
with session_factory.create_session() as session:
|
||||
tool_file = session.get(ToolFile, tool_file_id)
|
||||
assert tool_file is not None
|
||||
assert tool_file.conversation_id == conversation_id
|
||||
|
||||
|
||||
def test_commit_skill_row_persists_metadata_and_lists_catalog() -> None:
|
||||
tf = _seed_tool_file(name="SKILL.md")
|
||||
AgentDriveService().commit(
|
||||
|
||||
@@ -188,13 +188,16 @@ def test_delete_retires_then_commits_before_enqueue(monkeypatch: pytest.MonkeyPa
|
||||
"enqueue_agent_resource_collection",
|
||||
MagicMock(side_effect=lambda **_kwargs: events.append("enqueue")),
|
||||
)
|
||||
monkeypatch.setattr(conversation_service.delete_conversation_related_data, "delay", MagicMock())
|
||||
delete_related = MagicMock()
|
||||
monkeypatch.setattr(conversation_service.delete_conversation_related_data, "delay", delete_related)
|
||||
|
||||
ConversationService.delete(app, conversation.id, None, session=sqlite_session)
|
||||
|
||||
assert events == ["retire", "commit", "enqueue"]
|
||||
assert conversation.is_deleted is True
|
||||
assert get_binding.call_args.kwargs["binding_id"] == "conversation-binding-1"
|
||||
assert retire_binding.call_args.kwargs["binding_id"] == "conversation-binding-1"
|
||||
delete_related.assert_called_once_with(conversation.id)
|
||||
|
||||
|
||||
def test_delete_commit_failure_does_not_enqueue(monkeypatch: pytest.MonkeyPatch, sqlite_session: Session) -> None:
|
||||
@@ -202,7 +205,7 @@ def test_delete_commit_failure_does_not_enqueue(monkeypatch: pytest.MonkeyPatch,
|
||||
conversation = ConversationServiceTestDataFactory.create_conversation()
|
||||
conversation.agent_workspace_binding_id = "binding-1"
|
||||
sqlite_session.add(conversation)
|
||||
sqlite_session.flush()
|
||||
sqlite_session.commit()
|
||||
rollback_events: list[str] = []
|
||||
event.listen(sqlite_session, "after_rollback", lambda _session: rollback_events.append("rollback"))
|
||||
|
||||
@@ -226,10 +229,32 @@ def test_delete_commit_failure_does_not_enqueue(monkeypatch: pytest.MonkeyPatch,
|
||||
ConversationService.delete(app, conversation.id, None, session=sqlite_session)
|
||||
|
||||
assert rollback_events == ["rollback"]
|
||||
assert conversation.is_deleted is False
|
||||
enqueue_collection.assert_not_called()
|
||||
delete_related.assert_not_called()
|
||||
|
||||
|
||||
def test_delete_keeps_soft_deleted_marker_when_dispatch_fails(
|
||||
monkeypatch: pytest.MonkeyPatch, sqlite_session: Session
|
||||
) -> None:
|
||||
app = ConversationServiceTestDataFactory.create_app()
|
||||
conversation = ConversationServiceTestDataFactory.create_conversation()
|
||||
sqlite_session.add(conversation)
|
||||
sqlite_session.flush()
|
||||
monkeypatch.setattr(ConversationService, "get_conversation", MagicMock(return_value=conversation))
|
||||
monkeypatch.setattr(
|
||||
conversation_service.delete_conversation_related_data,
|
||||
"delay",
|
||||
MagicMock(side_effect=RuntimeError("broker unavailable")),
|
||||
)
|
||||
|
||||
ConversationService.delete(app, conversation.id, None, session=sqlite_session)
|
||||
|
||||
persisted = sqlite_session.get(Conversation, conversation.id)
|
||||
assert persisted is not None
|
||||
assert persisted.is_deleted is True
|
||||
|
||||
|
||||
class TestConversationServicePagination:
|
||||
"""Test conversation pagination operations."""
|
||||
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.app.entities.app_invoke_entities import InvokeFrom
|
||||
from core.workflow.human_input_adapter import DeliveryMethodType
|
||||
from core.workflow.nodes.human_input.enums import HumanInputFormKind, HumanInputFormStatus
|
||||
from graphon.file import FileTransferMethod, FileType
|
||||
from models import (
|
||||
AgentDebugConversation,
|
||||
AppMode,
|
||||
Conversation,
|
||||
ConversationVariable,
|
||||
HumanInputForm,
|
||||
HumanInputFormUploadFile,
|
||||
HumanInputFormUploadToken,
|
||||
Message,
|
||||
MessageAgentThought,
|
||||
MessageAnnotation,
|
||||
MessageChain,
|
||||
MessageFeedback,
|
||||
MessageFile,
|
||||
PinnedConversation,
|
||||
SavedMessage,
|
||||
)
|
||||
from models.agent import AgentConfigDraftType, AgentDriveFile, AgentDriveFileKind
|
||||
from models.enums import (
|
||||
ConversationFromSource,
|
||||
ConversationStatus,
|
||||
CreatorUserRole,
|
||||
FeedbackFromSource,
|
||||
FeedbackRating,
|
||||
MessageChainType,
|
||||
)
|
||||
from models.human_input import HumanInputDelivery, HumanInputFormRecipient, RecipientType
|
||||
from models.tools import ToolConversationVariables, ToolFile
|
||||
from tasks.delete_conversation_task import _cleanup_conversation_related_data, sweep_deleted_conversations
|
||||
|
||||
TENANT_ID = "11111111-1111-1111-1111-111111111111"
|
||||
APP_ID = "22222222-2222-2222-2222-222222222222"
|
||||
ACCOUNT_ID = "33333333-3333-3333-3333-333333333333"
|
||||
CONVERSATION_ID = "44444444-4444-4444-4444-444444444444"
|
||||
OTHER_CONVERSATION_ID = "55555555-5555-5555-5555-555555555555"
|
||||
MESSAGE_ID = "66666666-6666-6666-6666-666666666666"
|
||||
AGENT_ID = "77777777-7777-7777-7777-777777777777"
|
||||
|
||||
|
||||
def _conversation(conversation_id: str, *, deleted: bool) -> Conversation:
|
||||
return Conversation(
|
||||
id=conversation_id,
|
||||
app_id=APP_ID,
|
||||
mode=AppMode.CHAT,
|
||||
name="Test conversation",
|
||||
inputs={},
|
||||
status=ConversationStatus.NORMAL,
|
||||
from_source=ConversationFromSource.CONSOLE,
|
||||
from_account_id=ACCOUNT_ID,
|
||||
is_deleted=deleted,
|
||||
)
|
||||
|
||||
|
||||
def _message() -> Message:
|
||||
return Message(
|
||||
id=MESSAGE_ID,
|
||||
app_id=APP_ID,
|
||||
conversation_id=CONVERSATION_ID,
|
||||
inputs={},
|
||||
query="hello",
|
||||
message={"role": "user", "content": "hello"},
|
||||
answer="world",
|
||||
message_unit_price=Decimal(0),
|
||||
answer_unit_price=Decimal(0),
|
||||
currency="USD",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
from_source=ConversationFromSource.CONSOLE,
|
||||
from_account_id=ACCOUNT_ID,
|
||||
)
|
||||
|
||||
|
||||
def _tool_file(*, name: str, conversation_id: str | None = CONVERSATION_ID) -> ToolFile:
|
||||
return ToolFile(
|
||||
user_id=ACCOUNT_ID,
|
||||
tenant_id=TENANT_ID,
|
||||
conversation_id=conversation_id,
|
||||
file_key=f"tools/{TENANT_ID}/{name}",
|
||||
mimetype="text/plain",
|
||||
name=name,
|
||||
size=5,
|
||||
)
|
||||
|
||||
|
||||
def test_cleanup_removes_owned_resources_and_preserves_drive_files(sqlite_session: Session) -> None:
|
||||
conversation = _conversation(CONVERSATION_ID, deleted=True)
|
||||
other_conversation = _conversation(OTHER_CONVERSATION_ID, deleted=False)
|
||||
message = _message()
|
||||
owned_file = _tool_file(name="owned.txt")
|
||||
drive_file = _tool_file(name="drive.txt")
|
||||
other_file = _tool_file(name="other.txt", conversation_id=OTHER_CONVERSATION_ID)
|
||||
sqlite_session.add_all([conversation, other_conversation, message, owned_file, drive_file, other_file])
|
||||
sqlite_session.flush()
|
||||
|
||||
message_chain = MessageChain(message_id=MESSAGE_ID, type=MessageChainType.SYSTEM, input=None, output=None)
|
||||
form = HumanInputForm(
|
||||
tenant_id=TENANT_ID,
|
||||
app_id=APP_ID,
|
||||
workflow_run_id=None,
|
||||
conversation_id=CONVERSATION_ID,
|
||||
form_kind=HumanInputFormKind.RUNTIME,
|
||||
node_id="ask-human",
|
||||
form_definition="{}",
|
||||
rendered_content="form",
|
||||
status=HumanInputFormStatus.WAITING,
|
||||
expiration_time=datetime.now(UTC) + timedelta(hours=1),
|
||||
)
|
||||
sqlite_session.add_all([message_chain, form])
|
||||
sqlite_session.flush()
|
||||
|
||||
delivery = HumanInputDelivery(
|
||||
form_id=form.id,
|
||||
delivery_method_type=DeliveryMethodType.WEBAPP,
|
||||
delivery_config_id=None,
|
||||
channel_payload="{}",
|
||||
)
|
||||
sqlite_session.add(delivery)
|
||||
sqlite_session.flush()
|
||||
|
||||
upload_token = HumanInputFormUploadToken(
|
||||
tenant_id=TENANT_ID,
|
||||
app_id=APP_ID,
|
||||
form_id=form.id,
|
||||
recipient_id="88888888-8888-8888-8888-888888888888",
|
||||
token="upload-token",
|
||||
)
|
||||
sqlite_session.add(upload_token)
|
||||
sqlite_session.flush()
|
||||
|
||||
related_rows = [
|
||||
MessageAgentThought(
|
||||
message_id=MESSAGE_ID,
|
||||
position=1,
|
||||
created_by_role=CreatorUserRole.ACCOUNT,
|
||||
created_by=ACCOUNT_ID,
|
||||
message_chain_id=message_chain.id,
|
||||
),
|
||||
MessageFile(
|
||||
message_id=MESSAGE_ID,
|
||||
type=FileType.DOCUMENT,
|
||||
transfer_method=FileTransferMethod.REMOTE_URL,
|
||||
created_by_role=CreatorUserRole.ACCOUNT,
|
||||
created_by=ACCOUNT_ID,
|
||||
url="https://example.com/file.txt",
|
||||
),
|
||||
SavedMessage(
|
||||
app_id=APP_ID,
|
||||
message_id=MESSAGE_ID,
|
||||
created_by_role=CreatorUserRole.ACCOUNT,
|
||||
created_by=ACCOUNT_ID,
|
||||
),
|
||||
MessageAnnotation(
|
||||
app_id=APP_ID,
|
||||
question="hello",
|
||||
content="world",
|
||||
account_id=ACCOUNT_ID,
|
||||
conversation_id=CONVERSATION_ID,
|
||||
message_id=MESSAGE_ID,
|
||||
),
|
||||
MessageFeedback(
|
||||
app_id=APP_ID,
|
||||
conversation_id=CONVERSATION_ID,
|
||||
message_id=MESSAGE_ID,
|
||||
rating=FeedbackRating.LIKE,
|
||||
from_source=FeedbackFromSource.ADMIN,
|
||||
from_account_id=ACCOUNT_ID,
|
||||
),
|
||||
ToolConversationVariables(
|
||||
user_id=ACCOUNT_ID,
|
||||
tenant_id=TENANT_ID,
|
||||
conversation_id=CONVERSATION_ID,
|
||||
variables_str="{}",
|
||||
),
|
||||
ConversationVariable(
|
||||
id="99999999-9999-9999-9999-999999999999",
|
||||
conversation_id=CONVERSATION_ID,
|
||||
app_id=APP_ID,
|
||||
data="{}",
|
||||
),
|
||||
PinnedConversation(
|
||||
app_id=APP_ID,
|
||||
conversation_id=CONVERSATION_ID,
|
||||
created_by_role=CreatorUserRole.ACCOUNT,
|
||||
created_by=ACCOUNT_ID,
|
||||
),
|
||||
AgentDebugConversation(
|
||||
tenant_id=TENANT_ID,
|
||||
agent_id=AGENT_ID,
|
||||
app_id=APP_ID,
|
||||
account_id=ACCOUNT_ID,
|
||||
draft_type=AgentConfigDraftType.DEBUG_BUILD,
|
||||
conversation_id=CONVERSATION_ID,
|
||||
),
|
||||
AgentDriveFile(
|
||||
tenant_id=TENANT_ID,
|
||||
agent_id=AGENT_ID,
|
||||
key="drive.txt",
|
||||
file_kind=AgentDriveFileKind.TOOL_FILE,
|
||||
file_id=drive_file.id,
|
||||
value_owned_by_drive=False,
|
||||
is_skill=False,
|
||||
),
|
||||
HumanInputFormRecipient(
|
||||
form_id=form.id,
|
||||
delivery_id=delivery.id,
|
||||
recipient_type=RecipientType.CONSOLE,
|
||||
recipient_payload="{}",
|
||||
),
|
||||
HumanInputFormUploadFile(
|
||||
tenant_id=TENANT_ID,
|
||||
app_id=APP_ID,
|
||||
form_id=form.id,
|
||||
upload_file_id="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
|
||||
upload_token_id=upload_token.id,
|
||||
),
|
||||
]
|
||||
sqlite_session.add_all(related_rows)
|
||||
sqlite_session.commit()
|
||||
form_id = form.id
|
||||
owned_file_id = owned_file.id
|
||||
owned_file_key = owned_file.file_key
|
||||
drive_file_id = drive_file.id
|
||||
other_file_id = other_file.id
|
||||
|
||||
with patch("tasks.delete_conversation_task.storage") as storage_mock:
|
||||
assert _cleanup_conversation_related_data(CONVERSATION_ID) is True
|
||||
|
||||
storage_mock.delete.assert_called_once_with(owned_file_key)
|
||||
sqlite_session.expire_all()
|
||||
assert sqlite_session.get(Conversation, CONVERSATION_ID) is None
|
||||
assert sqlite_session.get(Message, MESSAGE_ID) is None
|
||||
assert (
|
||||
sqlite_session.scalar(select(MessageAgentThought).where(MessageAgentThought.message_id == MESSAGE_ID)) is None
|
||||
)
|
||||
assert sqlite_session.scalar(select(HumanInputForm).where(HumanInputForm.id == form_id)) is None
|
||||
assert sqlite_session.get(ToolFile, owned_file_id) is None
|
||||
preserved_drive_file = sqlite_session.get(ToolFile, drive_file_id)
|
||||
assert preserved_drive_file is not None
|
||||
assert preserved_drive_file.conversation_id is None
|
||||
preserved_drive_entry = sqlite_session.scalar(select(AgentDriveFile).where(AgentDriveFile.file_id == drive_file_id))
|
||||
assert preserved_drive_entry is not None
|
||||
assert preserved_drive_entry.value_owned_by_drive is True
|
||||
assert sqlite_session.get(ToolFile, other_file_id) is not None
|
||||
assert sqlite_session.get(Conversation, OTHER_CONVERSATION_ID) is not None
|
||||
|
||||
|
||||
def test_cleanup_storage_failure_retains_marker_and_file_key(sqlite_session: Session) -> None:
|
||||
conversation = _conversation(CONVERSATION_ID, deleted=True)
|
||||
tool_file = _tool_file(name="retry.txt")
|
||||
sqlite_session.add_all([conversation, tool_file])
|
||||
sqlite_session.commit()
|
||||
|
||||
with patch("tasks.delete_conversation_task.storage") as storage_mock:
|
||||
storage_mock.delete.side_effect = RuntimeError("storage unavailable")
|
||||
storage_mock.exists.return_value = True
|
||||
with pytest.raises(RuntimeError, match="storage unavailable"):
|
||||
_cleanup_conversation_related_data(CONVERSATION_ID)
|
||||
|
||||
sqlite_session.expire_all()
|
||||
persisted_conversation = sqlite_session.get(Conversation, CONVERSATION_ID)
|
||||
assert persisted_conversation is not None
|
||||
assert persisted_conversation.is_deleted is True
|
||||
assert sqlite_session.get(ToolFile, tool_file.id) is not None
|
||||
|
||||
|
||||
def test_cleanup_skips_active_conversation(sqlite_session: Session) -> None:
|
||||
conversation = _conversation(CONVERSATION_ID, deleted=False)
|
||||
tool_file = _tool_file(name="active.txt")
|
||||
sqlite_session.add_all([conversation, tool_file])
|
||||
sqlite_session.commit()
|
||||
|
||||
with patch("tasks.delete_conversation_task.storage") as storage_mock:
|
||||
assert _cleanup_conversation_related_data(CONVERSATION_ID) is False
|
||||
storage_mock.delete.assert_not_called()
|
||||
|
||||
assert sqlite_session.get(Conversation, CONVERSATION_ID) is not None
|
||||
assert sqlite_session.get(ToolFile, tool_file.id) is not None
|
||||
|
||||
|
||||
def test_sweeper_dispatches_only_soft_deleted_conversations(sqlite_session: Session) -> None:
|
||||
sqlite_session.add_all(
|
||||
[
|
||||
_conversation(CONVERSATION_ID, deleted=True),
|
||||
_conversation(OTHER_CONVERSATION_ID, deleted=False),
|
||||
]
|
||||
)
|
||||
sqlite_session.commit()
|
||||
|
||||
with patch("tasks.delete_conversation_task.delete_conversation_related_data.delay", MagicMock()) as delay:
|
||||
assert sweep_deleted_conversations.run() == 1
|
||||
|
||||
delay.assert_called_once_with(CONVERSATION_ID)
|
||||
Reference in new issue
Block a user