Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(agent): complete ownership-aware deletion lifecycle (#40888)
盐粒 Yanli013b328555d1
diff --git a/api/controllers/console/app/workflow.py b/api/controllers/console/app/workflow.pyindex d506a443a3a..4d90161f52d 100644--- a/api/controllers/console/app/workflow.py+++ b/api/controllers/console/app/workflow.py@@ -95,7 +95,6 @@ from services.errors.app import IsDraftWorkflowError, WorkflowHashNotEqualError,from services.errors.llm import InvokeRateLimitErrorfrom services.workflow_ref_service import WorkflowRefServicefrom services.workflow_service import DraftWorkflowDeletionError, WorkflowInUseError, WorkflowService-from tasks.collect_agent_resources_task import enqueue_agent_resource_collectionlogger = logging.getLogger(__name__)@@ -1303,7 +1302,7 @@ class PublishedWorkflowApi(Resource):workflow_service = WorkflowService()with sessionmaker(db.engine).begin() as session:- workflow, retirement_candidates = workflow_service.publish_workflow(+ workflow = workflow_service.publish_workflow(session=session,app_model=app_model,account=current_user,@@ -1320,16 +1319,6 @@ class PublishedWorkflowApi(Resource):workflow_created_at = TimestampField().format(workflow.created_at)- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(- tenant_id=app_model.tenant_id,- agent_ids=retirement_candidates,- account_id=current_user.id,- )- enqueue_agent_resource_collection(- tenant_id=app_model.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )return {"result": "success","created_at": workflow_created_at,@@ -1620,10 +1609,11 @@ class WorkflowByIdApi(Resource):@login_required@account_initialization_required@get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW])+ @with_current_user@edit_permission_required@rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_EDIT)@console_ns.response(204, "Workflow deleted successfully")- def delete(self, app_model: App, workflow_id: str):+ def delete(self, current_user: Account, app_model: App, workflow_id: str):"""Delete workflow"""@@ -1633,7 +1623,7 @@ class WorkflowByIdApi(Resource):# Create a session and manage the transactionwith sessionmaker(db.engine).begin() as session:try:- workflow_service.delete_workflow(+ retirement_candidates = workflow_service.delete_workflow(session=session,workflow_ref=workflow_ref,)@@ -1644,6 +1634,11 @@ class WorkflowByIdApi(Resource):except ValueError as e:raise NotFound(str(e))+ WorkflowAgentRetirementService.retire_unowned(+ tenant_id=app_model.tenant_id,+ agent_ids=retirement_candidates,+ account_id=current_user.id,+ )return None, 204diff --git a/api/controllers/console/datasets/rag_pipeline/rag_pipeline_workflow.py b/api/controllers/console/datasets/rag_pipeline/rag_pipeline_workflow.pyindex 63a14083c85..7c5e0cff893 100644--- a/api/controllers/console/datasets/rag_pipeline/rag_pipeline_workflow.py+++ b/api/controllers/console/datasets/rag_pipeline/rag_pipeline_workflow.py@@ -61,6 +61,7 @@ from models import Accountfrom models.dataset import Pipelinefrom models.model import EndUserfrom models.workflow import Workflow+from services.agent.retirement_service import WorkflowAgentRetirementServicefrom services.dataset_service import DatasetServicefrom services.errors.app import IsDraftWorkflowError, WorkflowHashNotEqualError, WorkflowNotFoundErrorfrom services.errors.llm import InvokeRateLimitError@@ -770,8 +771,9 @@ class RagPipelineByIdApi(Resource):@account_initialization_required@edit_permission_required@rbac_permission_required(RBACResourceScope.DATASET, RBACPermission.DATASET_EDIT)+ @with_current_user@get_rag_pipeline- def delete(self, pipeline: Pipeline, workflow_id: str):+ def delete(self, current_user: Account, pipeline: Pipeline, workflow_id: str):"""Delete a published workflow version that is not currently active on the pipeline."""@@ -783,7 +785,7 @@ class RagPipelineByIdApi(Resource):with sessionmaker(db.engine).begin() as session:try:- workflow_service.delete_workflow(+ retirement_candidates = workflow_service.delete_workflow(session=session,workflow_ref=workflow_ref,)@@ -794,6 +796,11 @@ class RagPipelineByIdApi(Resource):except ValueError as e:raise NotFound(str(e))+ WorkflowAgentRetirementService.retire_unowned(+ tenant_id=pipeline.tenant_id,+ agent_ids=retirement_candidates,+ account_id=current_user.id,+ )return None, 204diff --git a/api/controllers/console/snippets/snippet_workflow.py b/api/controllers/console/snippets/snippet_workflow.pyindex 0c24cb74fea..309a9d9ea1a 100644--- a/api/controllers/console/snippets/snippet_workflow.py+++ b/api/controllers/console/snippets/snippet_workflow.py@@ -57,13 +57,11 @@ from libs.helper import TimestampFieldfrom libs.login import current_account_with_tenant, login_requiredfrom models import Accountfrom models.snippet import CustomizedSnippet-from services.agent.retirement_service import WorkflowAgentRetirementServicefrom services.agent.workflow_publish_service import WorkflowAgentPublishServicefrom services.errors.app import IsDraftWorkflowError, WorkflowHashNotEqualError, WorkflowNotFoundErrorfrom services.errors.workflow_service import DraftWorkflowDeletionError, WorkflowInUseErrorfrom services.snippet_generate_service import SnippetGenerateServicefrom services.snippet_service import SnippetService-from tasks.collect_agent_resources_task import enqueue_agent_resource_collectionlogger = logging.getLogger(__name__)@@ -299,9 +297,8 @@ class SnippetPublishedWorkflowApi(Resource):with Session(db.engine) as session:snippet = session.merge(snippet)- tenant_id = snippet.tenant_idtry:- workflow, retirement_candidates = snippet_service.publish_workflow(+ workflow = snippet_service.publish_workflow(session=session,snippet=snippet,account=current_user,@@ -311,16 +308,6 @@ class SnippetPublishedWorkflowApi(Resource):except ValueError as e:return {"message": str(e)}, 400- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(- tenant_id=tenant_id,- agent_ids=retirement_candidates,- account_id=current_user.id,- )- enqueue_agent_resource_collection(- tenant_id=tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )return {"result": "success","created_at": workflow_created_at,diff --git a/api/services/agent/composer_service.py b/api/services/agent/composer_service.pyindex 064a6dc8e56..5ca7d670ad6 100644--- a/api/services/agent/composer_service.py+++ b/api/services/agent/composer_service.py@@ -279,16 +279,11 @@ class AgentComposerService:)state["validation"] = cls.collect_validation_findings(payload=payload)session.commit()- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id=tenant_id,agent_ids=retirement_candidates,account_id=account_id,)- enqueue_agent_resource_collection(- tenant_id=tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )return state@classmethoddiff --git a/api/services/agent/deletion_service.py b/api/services/agent/deletion_service.pynew file mode 100644index 00000000000..0801b8bb0e7--- /dev/null+++ b/api/services/agent/deletion_service.py@@ -0,0 +1,103 @@+"""Hard-delete archived Agent aggregates after external resources are collected."""++from __future__ import annotations++from collections.abc import Iterable++from sqlalchemy import delete, select++from core.db.session_factory import session_factory+from models.agent import (+ Agent,+ AgentConfigDraft,+ AgentConfigRevision,+ AgentConfigSnapshot,+ AgentDebugConversation,+ AgentHomeSnapshot,+ AgentStatus,+ AgentWorkingResourceStatus,+ AgentWorkspaceBinding,+)+++class AgentDeletionInvariantError(RuntimeError):+ """An archived Agent no longer satisfies the hard-deletion contract."""+++class AgentDeletionService:+ """Delete archived Agent aggregates after their external resources are gone.++ The aggregate includes Agent-owned configuration, debug, Home, and Workspace+ Binding rows. Workflow-owned binding soft references are outside the+ aggregate and may remain dangling after deletion.+ """++ @classmethod+ def purge_archived_agents(cls, *, tenant_id: str, agent_ids: Iterable[str]) -> None:+ """Idempotently hard-delete eligible archived Agent aggregates.++ Missing targets are a no-op. Every stored target must be ``ARCHIVED``,+ have no ACTIVE Workspace Binding or Home Snapshot, and all dependent rows+ and Agents are deleted and committed in one transaction; an exception+ before commit leaves the transaction to roll back without a partial+ aggregate deletion.+ """+ candidates = tuple(sorted({agent_id for agent_id in agent_ids if agent_id}))+ if not candidates:+ return++ with session_factory.create_session() as session:+ agents = session.scalars(select(Agent).where(Agent.tenant_id == tenant_id, Agent.id.in_(candidates))).all()+ if not agents:+ return++ stored_ids = [agent.id for agent in agents]+ non_archived_ids = [agent.id for agent in agents if agent.status != AgentStatus.ARCHIVED]+ if non_archived_ids:+ raise AgentDeletionInvariantError(+ f"Agents must be ARCHIVED before deletion: {', '.join(non_archived_ids)}"+ )++ active_binding_id = session.scalar(+ select(AgentWorkspaceBinding.id)+ .where(+ AgentWorkspaceBinding.tenant_id == tenant_id,+ AgentWorkspaceBinding.agent_id.in_(stored_ids),+ AgentWorkspaceBinding.status == AgentWorkingResourceStatus.ACTIVE,+ )+ .limit(1)+ )+ if active_binding_id is not None:+ raise AgentDeletionInvariantError(f"Agent aggregate still has ACTIVE Binding {active_binding_id}")++ active_home_id = session.scalar(+ select(AgentHomeSnapshot.id)+ .where(+ AgentHomeSnapshot.tenant_id == tenant_id,+ AgentHomeSnapshot.agent_id.in_(stored_ids),+ AgentHomeSnapshot.status == AgentWorkingResourceStatus.ACTIVE,+ )+ .limit(1)+ )+ if active_home_id is not None:+ raise AgentDeletionInvariantError(f"Agent aggregate still has ACTIVE Home Snapshot {active_home_id}")++ for model in (+ AgentDebugConversation,+ AgentConfigRevision,+ AgentConfigDraft,+ AgentConfigSnapshot,+ AgentHomeSnapshot,+ AgentWorkspaceBinding,+ ):+ session.execute(+ delete(model).where(+ model.tenant_id == tenant_id,+ model.agent_id.in_(stored_ids),+ )+ )+ session.execute(delete(Agent).where(Agent.tenant_id == tenant_id, Agent.id.in_(stored_ids)))+ session.commit()+++__all__ = ["AgentDeletionInvariantError", "AgentDeletionService"]diff --git a/api/services/agent/home_snapshot_service.py b/api/services/agent/home_snapshot_service.pyindex 54b0eb2591e..9e2113c56a6 100644--- a/api/services/agent/home_snapshot_service.py+++ b/api/services/agent/home_snapshot_service.py@@ -15,7 +15,6 @@ from libs.uuid_utils import uuidv7from models.agent import (Agent,AgentConfigDraft,- AgentConfigSnapshot,AgentConfigVersionKind,AgentHomeSnapshot,AgentStatus,@@ -108,13 +107,13 @@ class AgentHomeSnapshotService:select(AgentHomeSnapshot).where(AgentHomeSnapshot.tenant_id == tenant_id,AgentHomeSnapshot.agent_id == agent_id,- AgentHomeSnapshot.status == AgentWorkingResourceStatus.ACTIVE,)).all()now = naive_utc_now()for row in rows:- row.status = AgentWorkingResourceStatus.RETIRED- row.retired_at = now+ if row.status == AgentWorkingResourceStatus.ACTIVE:+ row.status = AgentWorkingResourceStatus.RETIRED+ row.retired_at = nowreturn [row.id for row in rows]@classmethod@@ -129,13 +128,6 @@ class AgentHomeSnapshotService:)if snapshot is None:return- referenced = session.scalar(- select(AgentConfigDraft.id).where(AgentConfigDraft.home_snapshot_id == home_snapshot_id).limit(1)- ) or session.scalar(- select(AgentConfigSnapshot.id).where(AgentConfigSnapshot.home_snapshot_id == home_snapshot_id).limit(1)- )- if referenced is not None:- returnsnapshot_ref = snapshot.snapshot_refcls.delete(snapshot_ref=snapshot_ref)with session_factory.create_session() as session:diff --git a/api/services/agent/retirement_service.py b/api/services/agent/retirement_service.pyindex 25b133e14f9..c800a8ea51b 100644--- a/api/services/agent/retirement_service.py+++ b/api/services/agent/retirement_service.py@@ -5,7 +5,7 @@ from __future__ import annotationsimport loggingfrom collections.abc import Iterable-from sqlalchemy import or_, select+from sqlalchemy import delete, selectfrom sqlalchemy.orm import Sessionfrom core.db.session_factory import session_factory@@ -15,20 +15,22 @@ from models.agent import (AgentScope,AgentStatus,AgentWorkingResourceStatus,+ AgentWorkspace,AgentWorkspaceBinding,WorkflowAgentNodeBinding,)-from models.enums import AppStatus-from models.model import App+from models.model import App, AppModefrom models.workflow import Workflowfrom services.agent.home_snapshot_service import AgentHomeSnapshotServicefrom services.agent.workspace_service import AgentWorkspaceService+from tasks.collect_agent_resources_task import enqueue_agent_resource_collection+from tasks.remove_app_and_related_data_task import remove_app_and_related_data_tasklogger = logging.getLogger(__name__)class WorkflowAgentRetirementService:- """Archive workflow-only Agents once no effective binding owns them."""+ """Delete workflow-only Agent aggregates after their last Workflow owner is gone."""@classmethoddef retire_unowned(@@ -37,13 +39,25 @@ class WorkflowAgentRetirementService:tenant_id: str,agent_ids: Iterable[str],account_id: str | None,- ) -> tuple[list[str], list[str]]:- """Re-check ownership, archive orphans, and commit their resource retirement."""+ ) -> None:+ """Retire unowned workflow-only Agents in an independent transaction.++ This method returns ``None``. It archives orphan Agents, retires their+ working resources, and deletes their hidden Apps before committing. It+ then publishes every hidden-App cleanup before publishing the Agent+ resource collector; database and task-publication errors propagate.++ Archived Agents, missing hidden App rows, and already-retired resources+ remain cleanup candidates. A retry can therefore publish duplicate+ cleanup tasks, which are expected to be idempotent.+ """candidates = tuple(sorted({agent_id for agent_id in agent_ids if agent_id}))if not candidates:- return [], []+ return+ backing_app_ids: list[str] = []retired_bindings: list[str] = []+ retired_workspaces: list[str] = []retired_snapshots: list[str] = []try:with session_factory.create_session() as session:@@ -53,22 +67,43 @@ class WorkflowAgentRetirementService:agent_ids=candidates,account_id=account_id,)+ retired_agents = session.scalars(+ select(Agent).where(+ Agent.tenant_id == tenant_id,+ Agent.id.in_(retired_agent_ids),+ )+ ).all()+ backing_app_ids = sorted({agent.backing_app_id for agent in retired_agents if agent.backing_app_id})+ for app_id in backing_app_ids:+ AgentWorkspaceService.retire_all_for_app(+ session=session,+ tenant_id=tenant_id,+ app_id=app_id,+ )+ retired_workspaces.extend(+ session.scalars(+ select(AgentWorkspace.id).where(+ AgentWorkspace.tenant_id == tenant_id,+ AgentWorkspace.app_id == app_id,+ AgentWorkspace.status == AgentWorkingResourceStatus.RETIRED,+ )+ ).all()+ )for agent_id in retired_agent_ids:bindings = session.scalars(select(AgentWorkspaceBinding).where(AgentWorkspaceBinding.tenant_id == tenant_id,AgentWorkspaceBinding.agent_id == agent_id,- AgentWorkspaceBinding.status == AgentWorkingResourceStatus.ACTIVE,)).all()for binding in bindings:- binding_id = AgentWorkspaceService.retire_binding(- session=session,- tenant_id=tenant_id,- binding_id=binding.id,- )- if binding_id is not None:- retired_bindings.append(binding_id)+ if binding.status == AgentWorkingResourceStatus.ACTIVE:+ AgentWorkspaceService.retire_binding(+ session=session,+ tenant_id=tenant_id,+ binding_id=binding.id,+ )+ retired_bindings.append(binding.id)retired_snapshots.extend(AgentHomeSnapshotService.retire_all_for_agent(session=session,@@ -76,6 +111,14 @@ class WorkflowAgentRetirementService:agent_id=agent_id,))+ if backing_app_ids:+ session.execute(+ delete(App).where(+ App.tenant_id == tenant_id,+ App.id.in_(backing_app_ids),+ App.mode == AppMode.AGENT,+ )+ )session.commit()except Exception:logger.exception(@@ -85,8 +128,24 @@ class WorkflowAgentRetirementService:"agent_ids": candidates,},)- return [], []- return retired_bindings, retired_snapshots+ raise++ for app_id in backing_app_ids:+ try:+ remove_app_and_related_data_task.delay(tenant_id=tenant_id, app_id=app_id)+ except Exception:+ logger.exception(+ "Failed to enqueue hidden Agent App cleanup",+ extra={"tenant_id": tenant_id, "app_id": app_id},+ )+ raise+ enqueue_agent_resource_collection(+ tenant_id=tenant_id,+ workspace_ids=retired_workspaces,+ binding_ids=retired_bindings,+ home_snapshot_ids=retired_snapshots,+ purge_agent_ids=retired_agent_ids,+ )@classmethoddef archive_unowned(@@ -97,7 +156,7 @@ class WorkflowAgentRetirementService:agent_ids: Iterable[str],account_id: str | None,) -> list[str]:- """Archive active orphans and return every orphan eligible for Home cleanup."""+ """Archive active orphans and return complete aggregate purge candidates."""candidates = tuple(sorted({agent_id for agent_id in agent_ids if agent_id}))if not candidates:return []@@ -109,7 +168,7 @@ class WorkflowAgentRetirementService:Agent.status.in_((AgentStatus.ACTIVE, AgentStatus.ARCHIVED)),)).all()- effective_agent_ids = cls._effective_agent_ids(+ retained_agent_ids = cls.retained_agent_ids(session=session,tenant_id=tenant_id,agent_ids=[agent.id for agent in agents],@@ -117,7 +176,7 @@ class WorkflowAgentRetirementService:now = naive_utc_now()cleanup_candidates: list[str] = []for agent in agents:- if agent.id in effective_agent_ids:+ if agent.id in retained_agent_ids:continueif agent.status == AgentStatus.ACTIVE:agent.status = AgentStatus.ARCHIVED@@ -130,33 +189,32 @@ class WorkflowAgentRetirementService:return cleanup_candidates@staticmethod- def _effective_agent_ids(+ def retained_agent_ids(*,session: Session,tenant_id: str,agent_ids: list[str],) -> set[str]:+ """Return Agents that still have an exact persisted Workflow owner.++ The owner key is tenant, App, Workflow, and Workflow version. Draft and+ every published version, whether current or historical, count equally;+ the App's current-Workflow pointer is not part of ownership.+ """if not agent_ids:return set()values = session.scalars(select(WorkflowAgentNodeBinding.agent_id).join(Workflow,- Workflow.id == WorkflowAgentNodeBinding.workflow_id,+ (Workflow.tenant_id == WorkflowAgentNodeBinding.tenant_id)+ & (Workflow.app_id == WorkflowAgentNodeBinding.app_id)+ & (Workflow.id == WorkflowAgentNodeBinding.workflow_id)+ & (Workflow.version == WorkflowAgentNodeBinding.workflow_version),)- .join(App, App.id == WorkflowAgentNodeBinding.app_id).where(WorkflowAgentNodeBinding.tenant_id == tenant_id,WorkflowAgentNodeBinding.agent_id.in_(agent_ids),- Workflow.tenant_id == tenant_id,- Workflow.app_id == WorkflowAgentNodeBinding.app_id,- Workflow.version == WorkflowAgentNodeBinding.workflow_version,- App.tenant_id == tenant_id,- App.status == AppStatus.NORMAL,- or_(- Workflow.version == Workflow.VERSION_DRAFT,- App.workflow_id == Workflow.id,- ),).distinct()).all()diff --git a/api/services/agent/roster_service.py b/api/services/agent/roster_service.pyindex af7bed03879..d63056209ef 100644--- a/api/services/agent/roster_service.py+++ b/api/services/agent/roster_service.py@@ -23,8 +23,6 @@ from models.agent import (AgentScope,AgentSource,AgentStatus,- AgentWorkingResourceStatus,- AgentWorkspaceBinding,AgentWorkspaceOwnerType,WorkflowAgentBindingType,WorkflowAgentNodeBinding,@@ -42,7 +40,6 @@ from services.agent.errors import (AgentNotFoundError,AgentVersionNotFoundError,)-from services.agent.home_snapshot_service import AgentHomeSnapshotServicefrom services.agent.workspace_service import AgentWorkspaceNotFoundError, AgentWorkspaceService, WorkspaceOwnerScopefrom services.app_service import AppService, CreateAppParamsfrom services.enterprise.enterprise_service import EnterpriseService@@ -1261,41 +1258,6 @@ class AgentRosterService:raise AgentNameConflictError() from excreturn self.get_roster_agent_detail(tenant_id=tenant_id, agent_id=agent_id)- def archive_roster_agent(self, *, tenant_id: str, agent_id: str, account_id: str) -> None:- agent = self._get_agent(tenant_id=tenant_id, agent_id=agent_id, roster_only=True)- retired_binding_ids: list[str] = []- if agent.status != AgentStatus.ARCHIVED:- agent.status = AgentStatus.ARCHIVED- agent.archived_by = account_id- agent.archived_at = naive_utc_now()- agent.updated_by = account_id- bindings = self._session.scalars(- select(AgentWorkspaceBinding).where(- AgentWorkspaceBinding.tenant_id == tenant_id,- AgentWorkspaceBinding.agent_id == agent_id,- AgentWorkspaceBinding.status == AgentWorkingResourceStatus.ACTIVE,- )- ).all()- for binding in bindings:- retired_id = AgentWorkspaceService.retire_binding(- session=self._session,- tenant_id=tenant_id,- binding_id=binding.id,- )- if retired_id is not None:- retired_binding_ids.append(retired_id)- retired_snapshot_ids = AgentHomeSnapshotService.retire_all_for_agent(- session=self._session,- tenant_id=tenant_id,- agent_id=agent_id,- )- self._session.commit()- enqueue_agent_resource_collection(- tenant_id=tenant_id,- binding_ids=retired_binding_ids,- home_snapshot_ids=retired_snapshot_ids,- )-@staticmethoddef _visible_version_operations(agent: Agent) -> set[AgentConfigRevisionOperation]:if agent.source == AgentSource.AGENT_APP or (diff --git a/api/services/agent/workflow_publish_service.py b/api/services/agent/workflow_publish_service.pyindex 954a3819774..117d8f12ef0 100644--- a/api/services/agent/workflow_publish_service.py+++ b/api/services/agent/workflow_publish_service.py@@ -24,7 +24,6 @@ from models.agent_config_entities import (WorkflowNodeJobConfig,WorkflowPreviousNodeOutputRef,)-from models.model import Appfrom models.workflow import Workflowfrom services.agent.composer_validator import ComposerConfigValidatorfrom services.agent.prompt_mentions import (@@ -575,32 +574,18 @@ class WorkflowAgentPublishService:session: Session,draft_workflow: Workflow,published_workflow: Workflow,- ) -> set[str]:- current_workflow_id = session.scalar(- select(App.workflow_id).where(- App.tenant_id == draft_workflow.tenant_id,- App.id == draft_workflow.app_id,- )- )- retirement_candidates: set[str] = set()- if current_workflow_id:- retirement_candidates = {- agent_id- for agent_id in session.scalars(- select(WorkflowAgentNodeBinding.agent_id).where(- WorkflowAgentNodeBinding.tenant_id == draft_workflow.tenant_id,- WorkflowAgentNodeBinding.app_id == draft_workflow.app_id,- WorkflowAgentNodeBinding.workflow_id == current_workflow_id,- WorkflowAgentNodeBinding.binding_type == WorkflowAgentBindingType.INLINE_AGENT,- )- ).all()- if agent_id- }+ ) -> None:+ """Copy all draft Roster and inline bindings to a published version.++ Only copied inline bindings add owners for workflow-only Agents.+ Publishing does not release existing draft or historical inline owners,+ produces no retirement candidates, and returns ``None``.+ """node_ids = {node_id for node_id, _node_data in WorkflowAgentNodeValidator.iter_agent_v2_nodes(draft_workflow.graph_dict)}if not node_ids:- return retirement_candidates+ returnbindings = session.scalars(select(WorkflowAgentNodeBinding).where(@@ -612,25 +597,17 @@ class WorkflowAgentPublishService:)).all()if not bindings:- return retirement_candidates-- agents_by_id = {- agent.id: agent- for agent in session.scalars(- select(Agent).where(- Agent.tenant_id == draft_workflow.tenant_id,- Agent.id.in_({binding.agent_id for binding in bindings if binding.agent_id}),- )- ).all()- }+ returnfor binding in bindings:- agent = agents_by_id.get(binding.agent_id) if binding.agent_id else None- current_snapshot_id = (- agent.active_config_snapshot_id- if agent is not None and binding.binding_type == WorkflowAgentBindingType.ROSTER_AGENT- else binding.current_snapshot_id- )+ current_snapshot_id = binding.current_snapshot_id+ if binding.binding_type == WorkflowAgentBindingType.ROSTER_AGENT and binding.agent_id:+ _, current_snapshot_id = cls._resolve_roster_agent_graph_binding(+ session=session,+ draft_workflow=draft_workflow,+ node_id=binding.node_id,+ agent_id=binding.agent_id,+ )copied = WorkflowAgentNodeBinding(tenant_id=binding.tenant_id,app_id=binding.app_id,@@ -645,7 +622,6 @@ class WorkflowAgentPublishService:updated_by=binding.updated_by,)session.add(copied)- return retirement_candidates@classmethoddef restore_agent_node_bindings_to_draft(@@ -671,9 +647,6 @@ class WorkflowAgentPublishService:for binding in existingif binding.binding_type == WorkflowAgentBindingType.INLINE_AGENT and binding.agent_id}- for binding in existing:- session.delete(binding)-source_bindings = session.scalars(select(WorkflowAgentNodeBinding).where(WorkflowAgentNodeBinding.tenant_id == source_workflow.tenant_id,@@ -682,6 +655,19 @@ class WorkflowAgentPublishService:WorkflowAgentNodeBinding.workflow_version == source_workflow.version,)).all()+ for source in source_bindings:+ if source.binding_type == WorkflowAgentBindingType.ROSTER_AGENT and source.agent_id:+ cls._resolve_roster_agent_graph_binding(+ session=session,+ draft_workflow=draft_workflow,+ node_id=source.node_id,+ agent_id=source.agent_id,+ )++ for binding in existing:+ session.delete(binding)+ session.flush()+for source in source_bindings:agent_id = source.agent_idsnapshot_id = source.current_snapshot_iddiff --git a/api/services/agent/workspace_service.py b/api/services/agent/workspace_service.pyindex 2cc6c34e881..cdf2736b523 100644--- a/api/services/agent/workspace_service.py+++ b/api/services/agent/workspace_service.py@@ -13,7 +13,7 @@ from dataclasses import dataclassfrom dify_agent.client import Clientfrom dify_agent.protocol import CreateExecutionBindingRequest, DestroyExecutionBindingRequest-from sqlalchemy import select+from sqlalchemy import delete, selectfrom sqlalchemy.orm import Sessionfrom clients.agent_backend.factory import create_agent_backend_client@@ -364,46 +364,68 @@ class AgentWorkspaceService:.order_by(AgentWorkspaceBinding.created_at)).all()if not bindings:- logger.error(- "RETIRED Workspace has no Binding available for physical collection",- extra={"tenant_id": tenant_id, "workspace_id": workspace_id},+ raise AgentWorkspaceError(+ f"RETIRED Workspace has no RETIRED Binding: tenant_id={tenant_id}, workspace_id={workspace_id}")- returnanchor = bindings[0]- remaining_ids = [binding.id for binding in bindings[1:]]+ remaining = [(binding.id, binding.backend_binding_ref) for binding in bindings[1:]]workspace_ref = workspace.backend_workspace_refbinding_ref = anchor.backend_binding_refanchor_id = anchor.id++ failures: list[str] = []+ first_error: Exception | None = Nonewith cls._client() as client:- client.destroy_execution_binding_sync(- DestroyExecutionBindingRequest(- binding_ref=binding_ref,- workspace_ref=workspace_ref,- destroy_workspace=True,+ targets = [(anchor_id, binding_ref, workspace_ref, True)] + [+ (binding_id, backend_binding_ref, None, False) for binding_id, backend_binding_ref in remaining+ ]+ for binding_id, backend_binding_ref, target_workspace_ref, destroy_workspace in targets:+ try:+ client.destroy_execution_binding_sync(+ DestroyExecutionBindingRequest(+ binding_ref=backend_binding_ref,+ workspace_ref=target_workspace_ref,+ destroy_workspace=destroy_workspace,+ )+ )+ except Exception as exc:+ failures.append(binding_id)+ if first_error is None:+ first_error = exc+ logger.exception(+ "Failed to destroy retired Agent Workspace Binding",+ extra={+ "tenant_id": tenant_id,+ "workspace_id": workspace_id,+ "binding_id": binding_id,+ "destroy_workspace": destroy_workspace,+ },+ )+ if failures:+ if len(failures) == 1 and first_error is not None:+ raise first_error+ raise AgentWorkspaceError(+ f"Failed to destroy {len(failures)} RETIRED Workspace Binding(s): {', '.join(failures)}"+ ) from first_error++ binding_ids = [anchor_id, *(binding_id for binding_id, _binding_ref in remaining)]+ with session_factory.create_session() as session:+ session.execute(+ delete(AgentWorkspaceBinding).where(+ AgentWorkspaceBinding.id.in_(binding_ids),+ AgentWorkspaceBinding.tenant_id == tenant_id,+ AgentWorkspaceBinding.workspace_id == workspace_id,+ AgentWorkspaceBinding.status == AgentWorkingResourceStatus.RETIRED,))- with session_factory.create_session() as session:- stored_workspace = session.scalar(- select(AgentWorkspace).where(+ session.execute(+ delete(AgentWorkspace).where(AgentWorkspace.id == workspace_id,AgentWorkspace.tenant_id == tenant_id,AgentWorkspace.status == AgentWorkingResourceStatus.RETIRED,))- stored_anchor = session.scalar(- select(AgentWorkspaceBinding).where(- AgentWorkspaceBinding.id == anchor_id,- AgentWorkspaceBinding.tenant_id == tenant_id,- AgentWorkspaceBinding.status == AgentWorkingResourceStatus.RETIRED,- )- )- if stored_workspace is not None:- session.delete(stored_workspace)- if stored_anchor is not None:- session.delete(stored_anchor)session.commit()- for remaining_id in remaining_ids:- cls.collect_retired_binding(tenant_id=tenant_id, binding_id=remaining_id)@staticmethoddef validate_binding_generation(diff --git a/api/services/app_dsl_service.py b/api/services/app_dsl_service.pyindex 98fe5f9d758..48c9cd7cfc3 100644--- a/api/services/app_dsl_service.py+++ b/api/services/app_dsl_service.py@@ -64,7 +64,6 @@ from services.errors.app import WorkflowNotFoundErrorfrom services.plugin.dependencies_analysis import DependenciesAnalysisServicefrom services.workflow_draft_variable_service import WorkflowDraftVariableServicefrom services.workflow_service import WorkflowService-from tasks.collect_agent_resources_task import enqueue_agent_resource_collectionlogger = logging.getLogger(__name__)@@ -583,16 +582,11 @@ class AppDslService:draft_workflow=draft_workflow,)self._session.commit()- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id=app.tenant_id,agent_ids=retirement_candidates,account_id=account.id,)- enqueue_agent_resource_collection(- tenant_id=app.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )case AppMode.CHAT | AppMode.AGENT_CHAT | AppMode.COMPLETION:# Initialize model configmodel_config = data.get("model_config")diff --git a/api/services/app_service.py b/api/services/app_service.pyindex d7788ef6bf4..b6571937341 100644--- a/api/services/app_service.py+++ b/api/services/app_service.py@@ -7,7 +7,7 @@ from typing import Any, Literal, NotRequired, TypedDict, cast, overrideimport sqlalchemy as safrom pydantic import BaseModel, Field-from sqlalchemy import ColumnElement, select+from sqlalchemy import ColumnElement, delete, selectfrom sqlalchemy.exc import IntegrityErrorfrom sqlalchemy.orm import Session@@ -36,6 +36,8 @@ from models.agent import (AgentStatus,AgentWorkingResourceStatus,AgentWorkspaceBinding,+ WorkflowAgentBindingType,+ WorkflowAgentNodeBinding,)from models.model import App, AppMode, AppModelConfig, IconType, Site, load_annotation_reply_configfrom models.workflow import Workflow@@ -741,7 +743,7 @@ class AppService:role: NotRequired[str | None]@staticmethod- def _get_backing_agent_for_update(app: App, *, session: Session) -> Agent | None:+ def _get_backing_agent(app: App, *, session: Session) -> Agent | None:if app.mode != AppMode.AGENT:return Nonereturn session.scalar(@@ -784,7 +786,7 @@ class AppService:Role omission is intentional: ``role=None`` preserves the backingAgent's current role, while ``role=""`` explicitly clears it."""- agent = self._get_backing_agent_for_update(app, session=session)+ agent = self._get_backing_agent(app, session=session)if agent is None:return@@ -988,21 +990,49 @@ class AppService:return appdef delete_app(self, app: App, *, session: Session) -> None:- """- Delete app- :param app: App instance+ """Delete an App and commit the passed session.++ The transaction releases all of a Workflow App's binding owners across+ draft and published versions, archives a backing Roster Agent, retires+ its resources, and deletes the App. Deleting a Roster Agent's backing+ App does not remove bindings owned by external Workflows.++ After commit, the main App cleanup is published first, followed by+ workflow-only Agent retirement and the Roster resource collector. Any+ publication failure propagates."""app_was_deleted.send(app)- backing_agent = self._get_backing_agent_for_update(app, session=session)- workflow_agent_ids = session.scalars(- select(Agent.id).where(- Agent.tenant_id == app.tenant_id,- Agent.app_id == app.id,- Agent.scope == AgentScope.WORKFLOW_ONLY,- Agent.status == AgentStatus.ACTIVE,+ backing_agent = self._get_backing_agent(app, session=session)+ workflow_agent_ids = set(+ session.scalars(+ select(Agent.id).where(+ Agent.tenant_id == app.tenant_id,+ Agent.app_id == app.id,+ Agent.scope == AgentScope.WORKFLOW_ONLY,+ Agent.status == AgentStatus.ACTIVE,+ )+ ).all()+ )+ if app.mode in (AppMode.WORKFLOW, AppMode.ADVANCED_CHAT):+ workflow_agent_ids.update(+ agent_id+ for agent_id in session.scalars(+ select(WorkflowAgentNodeBinding.agent_id).where(+ WorkflowAgentNodeBinding.tenant_id == app.tenant_id,+ WorkflowAgentNodeBinding.app_id == app.id,+ WorkflowAgentNodeBinding.binding_type == WorkflowAgentBindingType.INLINE_AGENT,+ WorkflowAgentNodeBinding.agent_id.is_not(None),+ )+ ).all()+ if agent_id+ )+ session.execute(+ delete(WorkflowAgentNodeBinding).where(+ WorkflowAgentNodeBinding.tenant_id == app.tenant_id,+ WorkflowAgentNodeBinding.app_id == app.id,+ ))- ).all()account_id = current_user.id if current_user else Noneif backing_agent is not None:now = naive_utc_now()@@ -1019,17 +1049,16 @@ class AppService:select(AgentWorkspaceBinding).where(AgentWorkspaceBinding.tenant_id == app.tenant_id,AgentWorkspaceBinding.agent_id == backing_agent.id,- AgentWorkspaceBinding.status == AgentWorkingResourceStatus.ACTIVE,)).all()for binding in bindings:- binding_id = AgentWorkspaceService.retire_binding(- session=session,- tenant_id=app.tenant_id,- binding_id=binding.id,- )- if binding_id is not None:- retired_binding_ids.append(binding_id)+ if binding.status == AgentWorkingResourceStatus.ACTIVE:+ AgentWorkspaceService.retire_binding(+ session=session,+ tenant_id=app.tenant_id,+ binding_id=binding.id,+ )+ retired_binding_ids.append(binding.id)retired_snapshot_ids = AgentHomeSnapshotService.retire_all_for_agent(session=session,tenant_id=app.tenant_id,@@ -1044,7 +1073,16 @@ class AppService:session.delete(app)session.commit()- workflow_binding_ids, workflow_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ try:+ remove_app_and_related_data_task.delay(tenant_id=app.tenant_id, app_id=app.id)+ except Exception:+ logger.exception(+ "Failed to enqueue App cleanup",+ extra={"tenant_id": app.tenant_id, "app_id": app.id},+ )+ raise++ WorkflowAgentRetirementService.retire_unowned(tenant_id=app.tenant_id,agent_ids=workflow_agent_ids,account_id=account_id,@@ -1052,8 +1090,9 @@ class AppService:enqueue_agent_resource_collection(tenant_id=app.tenant_id,workspace_ids=retired_workspace_ids,- binding_ids=[*retired_binding_ids, *workflow_binding_ids],- home_snapshot_ids=[*retired_snapshot_ids, *workflow_snapshot_ids],+ binding_ids=retired_binding_ids,+ home_snapshot_ids=retired_snapshot_ids,+ purge_agent_ids=[backing_agent.id] if backing_agent is not None else [],)# clean up web app settings@@ -1063,9 +1102,6 @@ class AppService:if dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD:BillingService.clean_billing_info_cache(app.tenant_id)- # Trigger asynchronous deletion of app and related data- remove_app_and_related_data_task.delay(tenant_id=app.tenant_id, app_id=app.id)-@staticmethoddef get_app_code_by_id(app_id: str, *, session: Session) -> str:"""diff --git a/api/services/data_migration/import_service.py b/api/services/data_migration/import_service.pyindex 48ff50ac655..c051be05d7a 100644--- a/api/services/data_migration/import_service.py+++ b/api/services/data_migration/import_service.py@@ -26,7 +26,6 @@ from models import Account, ApiToken, Tenant, TenantAccountJoin, TenantAccountRofrom models.enums import ApiTokenTypefrom models.model import Appfrom models.tools import ApiToolProvider, MCPToolProvider, WorkflowToolProvider-from services.agent.retirement_service import WorkflowAgentRetirementServicefrom services.app_dsl_service import AppDslServicefrom services.data_migration.dependency_discovery_service import DependencyDiscoveryServicefrom services.data_migration.entities import (@@ -48,7 +47,6 @@ from services.tools.api_tools_manage_service import ApiToolManageServicefrom services.tools.mcp_tools_manage_service import MCPToolManageServicefrom services.tools.workflow_tools_manage_service import WorkflowToolManageServicefrom services.workflow_service import WorkflowService-from tasks.collect_agent_resources_task import enqueue_agent_resource_collection@dataclass(frozen=True)@@ -713,7 +711,7 @@ class MigrationImportService:raise MigrationDataError(f"Referenced workflow app was not found in target tenant: {app_id}")if account_in_session is None:raise MigrationDataError(f"Operator account not found: {account.id}")- workflow, retirement_candidates = workflow_service.publish_workflow(+ workflow = workflow_service.publish_workflow(session=session,app_model=app_in_session,account=account_in_session,@@ -723,16 +721,6 @@ class MigrationImportService:app_in_session.workflow_id = workflow.idapp_in_session.updated_by = account.idapp_in_session.updated_at = naive_utc_now()- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(- tenant_id=target.tenant_id,- agent_ids=retirement_candidates,- account_id=account.id,- )- enqueue_agent_resource_collection(- tenant_id=target.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )def _import_mcp_tools(self,diff --git a/api/services/rag_pipeline/rag_pipeline.py b/api/services/rag_pipeline/rag_pipeline.pyindex 452e958ba6c..00daaa2bdd0 100644--- a/api/services/rag_pipeline/rag_pipeline.py+++ b/api/services/rag_pipeline/rag_pipeline.py@@ -301,7 +301,11 @@ class RagPipelineService:return workflowdef get_published_workflow_by_id(self, pipeline: Pipeline, workflow_id: str) -> Workflow | None:- """Fetch a published workflow snapshot by ID for restore operations."""+ """Fetch and lock a published Workflow snapshot for restoration.++ The source lock is held until the service transaction ends, preventing+ concurrent deletion while restore copies its Workflow snapshot fields.+ """workflow = self._session.scalar(select(Workflow).where(@@ -310,6 +314,7 @@ class RagPipelineService:Workflow.id == workflow_id,).limit(1)+ .with_for_update())if workflow and workflow.version == Workflow.VERSION_DRAFT:raise IsDraftWorkflowError("source workflow must be published")@@ -419,7 +424,8 @@ class RagPipelineService:Pipelines reuse the shared draft-restore field copy helper, but still ownthe pipeline-specific flush/link step that wires a newly created draft- back onto ``pipeline.workflow_id``.+ back onto ``pipeline.workflow_id``. The source version remains locked+ through snapshot-field copy and commit."""source_workflow = self.get_published_workflow_by_id(pipeline=pipeline, workflow_id=workflow_id)if not source_workflow:diff --git a/api/services/snippet_dsl_service.py b/api/services/snippet_dsl_service.pyindex 22f495a2370..b7d45d5caba 100644--- a/api/services/snippet_dsl_service.py+++ b/api/services/snippet_dsl_service.py@@ -32,7 +32,6 @@ from services.entities.dsl_entities import ()from services.plugin.dependencies_analysis import DependenciesAnalysisServicefrom services.snippet_service import SNIPPET_FORBIDDEN_NODE_TYPES, SnippetService-from tasks.collect_agent_resources_task import enqueue_agent_resource_collectionlogger = logging.getLogger(__name__)@@ -491,16 +490,11 @@ class SnippetDslService:self._session.commit()if workflow_data:- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id=snippet.tenant_id,agent_ids=retirement_candidates,account_id=account.id,)- enqueue_agent_resource_collection(- tenant_id=snippet.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )return snippetdef export_snippet_dsl(self, snippet: CustomizedSnippet, include_secret: bool = False) -> str:diff --git a/api/services/snippet_service.py b/api/services/snippet_service.pyindex aebe33f6e2b..cf0809dd520 100644--- a/api/services/snippet_service.py+++ b/api/services/snippet_service.py@@ -19,10 +19,11 @@ from models.agent import (Agent,AgentScope,AgentStatus,+ WorkflowAgentBindingType,WorkflowAgentNodeBinding,)from models.enums import WorkflowRunTriggeredFrom-from models.model import App, AppMode, UploadFile+from models.model import UploadFilefrom models.snippet import CustomizedSnippet, SnippetTypefrom models.tools import WorkflowToolProviderfrom models.workflow import (@@ -46,7 +47,6 @@ from services.workflow_node_execution_trace_service import (assemble_workflow_node_execution_traces,)from services.workflow_restore import apply_published_workflow_snapshot_to_draft-from tasks.collect_agent_resources_task import enqueue_agent_resource_collectionlogger = logging.getLogger(__name__)@@ -361,51 +361,51 @@ class SnippetService:snippet: CustomizedSnippet,account_id: str | None = None,) -> bool:- """- Delete a snippet.+ """Stage Snippet deletion in the caller's transaction.++ Workflow rows and all of their binding owners are deleted in that+ transaction. A single ``after_commit`` callback performs Agent+ retirement, so rollback does not trigger cleanup.:param session: Database session:param snippet: Snippet to delete:return: True if deleted successfully"""SnippetService._delete_draft_variable_files(session=session, snippet=snippet)- owned_agents = session.scalars(- select(Agent).where(- Agent.tenant_id == snippet.tenant_id,- Agent.app_id == snippet.id,- Agent.scope == AgentScope.WORKFLOW_ONLY,- Agent.source.in_(WORKFLOW_ONLY_AGENT_SOURCES),- Agent.status == AgentStatus.ACTIVE,- )- ).all()- now = datetime.now(UTC).replace(tzinfo=None)- backing_app_ids = {agent.backing_app_id for agent in owned_agents if agent.backing_app_id}- for agent in owned_agents:- agent.status = AgentStatus.ARCHIVED- agent.archived_by = account_id- agent.archived_at = now- agent.updated_by = account_id or agent.updated_by- agent.updated_at = now-- if backing_app_ids:- session.execute(- delete(App)- .where(- App.tenant_id == snippet.tenant_id,- App.id.in_(backing_app_ids),- App.mode == AppMode.AGENT,+ candidate_agent_ids = {+ agent_id+ for agent_id in session.scalars(+ select(WorkflowAgentNodeBinding.agent_id).where(+ WorkflowAgentNodeBinding.tenant_id == snippet.tenant_id,+ WorkflowAgentNodeBinding.app_id == snippet.id,+ WorkflowAgentNodeBinding.binding_type == WorkflowAgentBindingType.INLINE_AGENT,+ WorkflowAgentNodeBinding.agent_id.is_not(None),)- .execution_options(synchronize_session=False)- )+ ).all()+ if agent_id+ }+ candidate_agent_ids.update(+ session.scalars(+ select(Agent.id).where(+ Agent.tenant_id == snippet.tenant_id,+ Agent.app_id == snippet.id,+ Agent.scope == AgentScope.WORKFLOW_ONLY,+ Agent.source.in_(WORKFLOW_ONLY_AGENT_SOURCES),+ Agent.status == AgentStatus.ACTIVE,+ )+ ).all()+ )+ if candidate_agent_ids:tenant_id = snippet.tenant_id- def cleanup_backing_apps(_session: Session) -> None:- from tasks.remove_app_and_related_data_task import remove_app_and_related_data_task-- for app_id in backing_app_ids:- remove_app_and_related_data_task.delay(tenant_id=tenant_id, app_id=app_id)+ def collect_agent_resources(_session: Session) -> None:+ WorkflowAgentRetirementService.retire_unowned(+ tenant_id=tenant_id,+ agent_ids=candidate_agent_ids,+ account_id=account_id,+ )- event.listen(session, "after_commit", cleanup_backing_apps, once=True)+ event.listen(session, "after_commit", collect_agent_resources, once=True)session.execute(delete(WorkflowAgentNodeBinding)@@ -621,16 +621,11 @@ class SnippetService:)self._commit_if_owned(session)if self._session is None:- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id=snippet.tenant_id,agent_ids=retirement_candidates,account_id=account.id,)- enqueue_agent_resource_collection(- tenant_id=snippet.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )return workflowdef restore_published_workflow_to_draft(@@ -680,16 +675,11 @@ class SnippetService:)self._commit_if_owned(session)if self._session is None:- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id=snippet.tenant_id,agent_ids=retirement_candidates,account_id=account.id,)- enqueue_agent_resource_collection(- tenant_id=snippet.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )return draft_workflowdef publish_workflow(@@ -698,7 +688,7 @@ class SnippetService:session: Session,snippet: CustomizedSnippet,account: Account,- ) -> tuple[Workflow, set[str]]:+ ) -> Workflow:"""Publish the draft workflow as a new version.@@ -749,7 +739,7 @@ class SnippetService:kind=WorkflowKind.SNIPPET.value,)session.add(workflow)- retirement_candidates = WorkflowAgentPublishService.copy_agent_node_bindings_to_published(+ WorkflowAgentPublishService.copy_agent_node_bindings_to_published(session=session,draft_workflow=draft_workflow,published_workflow=workflow,@@ -762,7 +752,7 @@ class SnippetService:snippet.updated_by = account.idsession.add(snippet)- return workflow, retirement_candidates+ return workflowdef get_all_published_workflows(self,diff --git a/api/services/workflow_service.py b/api/services/workflow_service.pyindex c821e7b7c80..5cad8988888 100644--- a/api/services/workflow_service.py+++ b/api/services/workflow_service.py@@ -80,6 +80,7 @@ from graphon.variables.input_entities import VariableEntityTypefrom graphon.variables.variables import Variablefrom libs.datetime_utils import naive_utc_nowfrom models import Account+from models.agent import WorkflowAgentBindingType, WorkflowAgentNodeBindingfrom models.human_input import HumanInputFormRecipient, RecipientTypefrom models.model import App, AppModefrom models.tools import WorkflowToolProvider@@ -93,7 +94,6 @@ from services.errors.app import (WorkflowHashNotEqualError,WorkflowNotFoundError,)-from tasks.collect_agent_resources_task import enqueue_agent_resource_collection@dataclass(frozen=True)@@ -280,14 +280,21 @@ class WorkflowService:.with_for_update())- def get_published_workflow_by_id(self, app_model: App, workflow_id: str, *, session: Session) -> Workflow | None:- """- fetch published workflow by workflow_id+ def get_published_workflow_by_id(+ self,+ app_model: App,+ workflow_id: str,+ *,+ session: Session,+ for_update: bool = False,+ ) -> Workflow | None:+ """Fetch a published workflow by ID in the caller's transaction.- Reuses the caller's active session so workflow reads stay in the same- transaction as the surrounding request or task.+ With ``for_update=True``, the source version stays locked until that+ transaction ends. Restore uses the lock while copying Agent bindings so+ a concurrent delete cannot release the same owner."""- workflow = session.scalar(+ stmt = (select(Workflow).where(Workflow.tenant_id == app_model.tenant_id,@@ -296,6 +303,9 @@ class WorkflowService:).limit(1))+ if for_update:+ stmt = stmt.with_for_update()+ workflow = session.scalar(stmt)if not workflow:return Noneif workflow.version == Workflow.VERSION_DRAFT:@@ -491,16 +501,11 @@ class WorkflowService:# commit db session changesif commit:session.commit()- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id=app_model.tenant_id,agent_ids=retirement_candidates,account_id=account.id,)- enqueue_agent_resource_collection(- tenant_id=app_model.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )# trigger app workflow eventsif commit:@@ -624,7 +629,10 @@ class WorkflowService:published workflow so the normal draft sync flow stays stateless."""source_workflow = self.get_published_workflow_by_id(- app_model=app_model, workflow_id=workflow_id, session=session+ app_model=app_model,+ workflow_id=workflow_id,+ session=session,+ for_update=True,)if not source_workflow:raise WorkflowNotFoundError("Workflow not found.")@@ -656,16 +664,11 @@ class WorkflowService:)session.commit()- binding_ids, home_snapshot_ids = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id=app_model.tenant_id,agent_ids=retirement_candidates,account_id=account.id,)- enqueue_agent_resource_collection(- tenant_id=app_model.tenant_id,- binding_ids=binding_ids,- home_snapshot_ids=home_snapshot_ids,- )app_draft_workflow_was_synced.send(app_model, synced_draft_workflow=draft_workflow)return draft_workflow@@ -678,7 +681,7 @@ class WorkflowService:account: Account,marked_name: str = "",marked_comment: str = "",- ) -> tuple[Workflow, set[str]]:+ ) -> Workflow:draft_workflow_stmt = select(Workflow).where(Workflow.tenant_id == app_model.tenant_id,Workflow.app_id == app_model.id,@@ -743,7 +746,7 @@ class WorkflowService:# commit db session changessession.add(workflow)- retirement_candidates = WorkflowAgentPublishService.copy_agent_node_bindings_to_published(+ WorkflowAgentPublishService.copy_agent_node_bindings_to_published(session=session,draft_workflow=draft_workflow,published_workflow=workflow,@@ -753,7 +756,7 @@ class WorkflowService:app_published_workflow_was_updated.send(app_model, published_workflow=workflow)# return new workflow- return workflow, retirement_candidates+ return workflowdef _validate_workflow_credentials(self, workflow: Workflow, *, session: Session) -> None:"""@@ -1888,21 +1891,29 @@ class WorkflowService:return workflow- def delete_workflow(self, *, session: Session, workflow_ref: WorkflowRef) -> bool:- """- Delete a workflow+ def delete_workflow(self, *, session: Session, workflow_ref: WorkflowRef) -> list[str]:+ """Stage a published Workflow and its binding owners for deletion.++ The exact owner key is tenant, App, Workflow, and Workflow version. The+ Workflow row lock serializes source-version reads and restoration with+ deletion. The caller must commit successfully before retiring the+ returned, sorted and deduplicated inline Agent candidates.:param session: SQLAlchemy database session:param workflow_ref: Owner-bound workflow reference- :return: True if successful+ :return: Inline Agent IDs whose owner binding is staged for deletion:raises: ValueError if workflow not found:raises: WorkflowInUseError if workflow is in use:raises: DraftWorkflowDeletionError if workflow is a draft version"""- stmt = select(Workflow).where(- Workflow.id == workflow_ref.workflow_id,- Workflow.tenant_id == workflow_ref.tenant_id,- Workflow.app_id == workflow_ref.owner_id,+ stmt = (+ select(Workflow)+ .where(+ Workflow.id == workflow_ref.workflow_id,+ Workflow.tenant_id == workflow_ref.tenant_id,+ Workflow.app_id == workflow_ref.owner_id,+ )+ .with_for_update())workflow = session.scalar(stmt)@@ -1934,8 +1945,25 @@ class WorkflowService:# Cannot delete a workflow that's published as a toolraise WorkflowInUseError("Cannot delete workflow that is published as a tool")+ bindings = session.scalars(+ select(WorkflowAgentNodeBinding).where(+ WorkflowAgentNodeBinding.tenant_id == workflow.tenant_id,+ WorkflowAgentNodeBinding.app_id == workflow.app_id,+ WorkflowAgentNodeBinding.workflow_id == workflow.id,+ WorkflowAgentNodeBinding.workflow_version == workflow.version,+ )+ ).all()+ retirement_candidates = sorted(+ {+ binding.agent_id+ for binding in bindings+ if binding.binding_type == WorkflowAgentBindingType.INLINE_AGENT and binding.agent_id+ }+ )+ for binding in bindings:+ session.delete(binding)session.delete(workflow)- return True+ return retirement_candidatesdef _setup_variable_pool(diff --git a/api/tasks/collect_agent_resources_task.py b/api/tasks/collect_agent_resources_task.pyindex bfec87b7bf3..54addc2f45a 100644--- a/api/tasks/collect_agent_resources_task.py+++ b/api/tasks/collect_agent_resources_task.py@@ -1,4 +1,10 @@-"""Asynchronously collect retired Agent working resources."""+"""Collect retired Agent data under a two-phase task contract.++Phase one attempts every explicitly identified RETIRED working resource. Phase+two purges the requested archived Agent aggregates only after the whole first+phase succeeds. Any collection failure skips aggregate purge and is included in+the error raised after all explicit resources have been attempted.+"""from __future__ import annotations@@ -7,6 +13,7 @@ from collections.abc import Iterablefrom celery import shared_task+from services.agent.deletion_service import AgentDeletionServicefrom services.agent.home_snapshot_service import AgentHomeSnapshotServicefrom services.agent.workspace_service import AgentWorkspaceService@@ -20,8 +27,14 @@ def collect_agent_resources(binding_ids: list[str],workspace_ids: list[str],home_snapshot_ids: list[str],+ purge_agent_ids: list[str] | None = None,) -> None:- """Collect only the explicitly identified RETIRED resources."""+ """Collect the explicit RETIRED batch, then purge Agents only on full success.++ Collection is best-effort across the complete explicit batch so one failed+ resource does not hide later failures. If any resource fails, aggregate+ purge is skipped and one summary error is raised after all attempts.+ """collectors = ((workspace_ids, "workspace_id", AgentWorkspaceService.collect_retired_workspace),@@ -32,20 +45,30 @@ def collect_agent_resources(AgentHomeSnapshotService.collect_retired_home_snapshot,),)+ failures: list[str] = []+ first_error: Exception | None = Nonefor resource_ids, argument_name, collector in collectors:for resource_id in resource_ids:try:collector(tenant_id=tenant_id, **{argument_name: resource_id})- except Exception:+ except Exception as exc:+ resource_type = argument_name.removesuffix("_id")+ failures.append(f"{resource_type}:{resource_id}")+ if first_error is None:+ first_error = exclogger.exception("Failed to collect retired Agent resource",extra={"tenant_id": tenant_id,- "resource_type": argument_name.removesuffix("_id"),+ "resource_type": resource_type,"resource_id": resource_id,},)- raise+ if failures:+ raise RuntimeError(+ f"Failed to collect {len(failures)} retired Agent resource(s): {', '.join(failures)}"+ ) from first_error+ AgentDeletionService.purge_archived_agents(tenant_id=tenant_id, agent_ids=purge_agent_ids or ())def enqueue_agent_resource_collection(@@ -54,13 +77,15 @@ def enqueue_agent_resource_collection(binding_ids: Iterable[str] = (),workspace_ids: Iterable[str] = (),home_snapshot_ids: Iterable[str] = (),+ purge_agent_ids: Iterable[str] = (),) -> None:- """Best-effort enqueue of physical collection after retire has committed."""+ """Enqueue physical collection after retirement has committed."""payload = {"binding_ids": sorted({resource_id for resource_id in binding_ids if resource_id}),"workspace_ids": sorted({resource_id for resource_id in workspace_ids if resource_id}),"home_snapshot_ids": sorted({resource_id for resource_id in home_snapshot_ids if resource_id}),+ "purge_agent_ids": sorted({agent_id for agent_id in purge_agent_ids if agent_id}),}if not any(payload.values()):return@@ -71,6 +96,7 @@ def enqueue_agent_resource_collection("Failed to enqueue retired Agent resource collection",extra={"tenant_id": tenant_id, **payload},)+ raise__all__ = ["collect_agent_resources", "enqueue_agent_resource_collection"]diff --git a/api/tasks/remove_app_and_related_data_task.py b/api/tasks/remove_app_and_related_data_task.pyindex 4562b7d1d90..f8af48e0990 100644--- a/api/tasks/remove_app_and_related_data_task.py+++ b/api/tasks/remove_app_and_related_data_task.py@@ -40,6 +40,7 @@ from models import (TraceAppConfig,WorkflowSchedulePlan,)+from models.agent import WorkflowAgentNodeBindingfrom models.tools import WorkflowToolProviderfrom models.trigger import WorkflowPluginTrigger, WorkflowTriggerLog, WorkflowWebhookTriggerfrom models.web import PinnedConversation, SavedMessage@@ -70,6 +71,7 @@ def remove_app_and_related_data_task(self, tenant_id: str, app_id: str):_delete_recommended_apps(tenant_id, app_id)_delete_app_annotation_data(tenant_id, app_id)_delete_app_dataset_joins(tenant_id, app_id)+ _delete_workflow_agent_node_bindings(tenant_id, app_id)_delete_app_workflows(tenant_id, app_id)_delete_app_workflow_runs(tenant_id, app_id)_delete_app_workflow_node_executions(tenant_id, app_id)@@ -262,6 +264,17 @@ def _delete_app_workflows(tenant_id: str, app_id: str):)+def _delete_workflow_agent_node_bindings(tenant_id: str, app_id: str) -> None:+ with session_factory.create_session() as session:+ session.execute(+ delete(WorkflowAgentNodeBinding).where(+ WorkflowAgentNodeBinding.tenant_id == tenant_id,+ WorkflowAgentNodeBinding.app_id == app_id,+ )+ )+ session.commit()++def _delete_app_workflow_runs(tenant_id: str, app_id: str):"""Delete all workflow runs for an app using the service repository."""session_maker = sessionmaker(bind=db.engine)diff --git a/api/tests/test_containers_integration_tests/services/test_workflow_service.py b/api/tests/test_containers_integration_tests/services/test_workflow_service.pyindex 05697997511..c9d8ee51c72 100644--- a/api/tests/test_containers_integration_tests/services/test_workflow_service.py+++ b/api/tests/test_containers_integration_tests/services/test_workflow_service.py@@ -871,13 +871,12 @@ class TestWorkflowService:from unittest.mock import patchwith patch("flask_login.utils._get_user", return_value=account, autospec=True):- result, retirement_candidates = workflow_service.publish_workflow(+ result = workflow_service.publish_workflow(session=db_session_with_containers, app_model=app, account=account)# Assertassert result is not None- assert retirement_candidates == set()assert result.version != Workflow.VERSION_DRAFT# Version should be a timestamp format like '2025-08-22 00:10:24.722051'assert isinstance(result.version, str)@@ -1435,7 +1434,7 @@ class TestWorkflowService:)# Assert- assert result is True+ assert result == []# Verify workflow is actually deleteddeleted_workflow = db_session_with_containers.query(Workflow).filter_by(id=workflow.id).first()diff --git a/api/tests/test_containers_integration_tests/services/workflow/test_workflow_deletion.py b/api/tests/test_containers_integration_tests/services/workflow/test_workflow_deletion.pyindex d2fc873cf51..3c70dea134f 100644--- a/api/tests/test_containers_integration_tests/services/workflow/test_workflow_deletion.py+++ b/api/tests/test_containers_integration_tests/services/workflow/test_workflow_deletion.py@@ -116,7 +116,7 @@ class TestWorkflowDeletion:workflow_ref=WorkflowRef(tenant_id=tenant.id, owner_id=app.id, workflow_id=workflow_id),)- assert result is True+ assert result == []db_session_with_containers.expire_all()assert db_session_with_containers.get(Workflow, workflow_id) is Nonediff --git a/api/tests/unit_tests/controllers/console/app/test_workflow.py b/api/tests/unit_tests/controllers/console/app/test_workflow.pyindex 3677b0cbb80..f01ddcaba64 100644--- a/api/tests/unit_tests/controllers/console/app/test_workflow.py+++ b/api/tests/unit_tests/controllers/console/app/test_workflow.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport inspectimport json+from contextlib import contextmanager, nullcontextfrom datetime import datetimefrom types import SimpleNamespacefrom typing import cast@@ -79,6 +80,99 @@ def _make_workflow(**overrides):return workflow+def test_publish_workflow_returns_success(+ app: Flask,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ current_user = SimpleNamespace(id="account-1")+ app_model = SimpleNamespace(id="app-1", tenant_id="tenant-1")+ workflow = SimpleNamespace(id="published-workflow", created_at=datetime(2026, 8, 17, 12, 0, 0))+ session = Mock()+ session.get.return_value = app_model+ monkeypatch.setattr(+ workflow_module,+ "WorkflowService",+ Mock(return_value=SimpleNamespace(publish_workflow=Mock(return_value=workflow))),+ )+ monkeypatch.setattr(+ workflow_module,+ "sessionmaker",+ lambda _engine: SimpleNamespace(begin=lambda: nullcontext(session)),+ )+ monkeypatch.setattr(workflow_module, "db", SimpleNamespace(engine=object()))+ with app.test_request_context("/apps/app-1/workflows/publish", method="POST", json={}):+ response = inspect.unwrap(workflow_module.PublishedWorkflowApi.post)(+ workflow_module.PublishedWorkflowApi(),+ current_user,+ app_model,+ )++ assert response["result"] == "success"++[email protected]("transaction_fails", [False, True], ids=["commit-succeeds", "commit-fails"])+def test_delete_workflow_retires_candidates_only_after_transaction_exit(+ app: Flask,+ monkeypatch: pytest.MonkeyPatch,+ transaction_fails: bool,+) -> None:+ current_user = SimpleNamespace(id="account-1")+ app_model = SimpleNamespace(id="app-1", tenant_id="tenant-1")+ session = Mock()+ events: list[str] = []+ error = RuntimeError("commit failed")+ workflow_service = SimpleNamespace(+ delete_workflow=Mock(side_effect=lambda **_kwargs: events.append("delete") or ["inline-agent"])+ )++ @contextmanager+ def transaction():+ events.append("transaction-enter")+ yield session+ events.append("transaction-exit")+ if transaction_fails:+ raise error++ monkeypatch.setattr(workflow_module, "WorkflowService", Mock(return_value=workflow_service))+ monkeypatch.setattr(+ workflow_module,+ "sessionmaker",+ lambda _engine: SimpleNamespace(begin=transaction),+ )+ monkeypatch.setattr(workflow_module, "db", SimpleNamespace(engine=object()))+ retire_unowned = Mock(side_effect=lambda **_kwargs: events.append("retire"))+ monkeypatch.setattr(workflow_module.WorkflowAgentRetirementService, "retire_unowned", retire_unowned)++ with app.test_request_context("/apps/app-1/workflows/workflow-1", method="DELETE"):+ if transaction_fails:+ with pytest.raises(RuntimeError) as exc_info:+ inspect.unwrap(workflow_module.WorkflowByIdApi.delete)(+ workflow_module.WorkflowByIdApi(),+ current_user,+ app_model,+ "workflow-1",+ )+ assert exc_info.value is error+ else:+ response = inspect.unwrap(workflow_module.WorkflowByIdApi.delete)(+ workflow_module.WorkflowByIdApi(),+ current_user,+ app_model,+ "workflow-1",+ )+ assert response == (None, 204)++ assert events == ["transaction-enter", "delete", "transaction-exit"] + ([] if transaction_fails else ["retire"])+ if transaction_fails:+ retire_unowned.assert_not_called()+ else:+ retire_unowned.assert_called_once_with(+ tenant_id=app_model.tenant_id,+ agent_ids=["inline-agent"],+ account_id=current_user.id,+ )++def test_parse_file_no_config(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(workflow_module.FileUploadConfigManager, "convert", lambda *_args, **_kwargs: None)workflow = SimpleNamespace(features_dict={}, tenant_id="t1")diff --git a/api/tests/unit_tests/controllers/console/datasets/rag_pipeline/test_rag_pipeline_workflow_apis.py b/api/tests/unit_tests/controllers/console/datasets/rag_pipeline/test_rag_pipeline_workflow_apis.pyindex caef20a3d69..81b58c32339 100644--- a/api/tests/unit_tests/controllers/console/datasets/rag_pipeline/test_rag_pipeline_workflow_apis.py+++ b/api/tests/unit_tests/controllers/console/datasets/rag_pipeline/test_rag_pipeline_workflow_apis.py@@ -3,7 +3,8 @@from __future__ import annotationsimport json-from collections.abc import Iterator+from collections.abc import Generator, Iterator+from contextlib import contextmanagerfrom dataclasses import dataclassfrom datetime import datetimefrom inspect import unwrap@@ -671,13 +672,33 @@ class TestRagPipelineByIdApi:result, status = method(api, WorkflowUpdatePayload(), user, pipeline, "w1")assert status == 400- def test_delete_success(self, app: Flask) -> None:+ @pytest.mark.parametrize("transaction_fails", [False, True], ids=["commit-succeeds", "commit-fails"])+ def test_delete_retires_candidates_only_after_transaction_exit(+ self,+ app: Flask,+ transaction_fails: bool,+ ) -> None:api = RagPipelineByIdApi()method = unwrap(api.delete)pipeline = make_pipeline(tenant_id="t1", workflow_id="active-workflow")+ user = make_account()+ events: list[str] = []+ error = RuntimeError("commit failed")workflow_service = MagicMock()+ workflow_service.delete_workflow.side_effect = lambda **_kwargs: events.append("delete") or ["inline-agent"]+ transaction_factory = MagicMock()++ @contextmanager+ def transaction() -> Generator[Session]:+ events.append("transaction-enter")+ yield MagicMock(spec=Session)+ events.append("transaction-exit")+ if transaction_fails:+ raise error++ transaction_factory.begin.side_effect = transactionwith (app.test_request_context("/", method="DELETE"),@@ -685,21 +706,45 @@ class TestRagPipelineByIdApi:"controllers.console.datasets.rag_pipeline.rag_pipeline_workflow.WorkflowService",return_value=workflow_service,),+ patch(+ "controllers.console.datasets.rag_pipeline.rag_pipeline_workflow.sessionmaker",+ return_value=transaction_factory,+ ),+ patch(+ "controllers.console.datasets.rag_pipeline.rag_pipeline_workflow."+ "WorkflowAgentRetirementService.retire_unowned"+ ) as retire_unowned,):- result = method(api, pipeline, "old-workflow")+ retire_unowned.side_effect = lambda **_kwargs: events.append("retire")+ if transaction_fails:+ with pytest.raises(RuntimeError) as exc_info:+ method(api, user, pipeline, "old-workflow")+ assert exc_info.value is error+ else:+ result = method(api, user, pipeline, "old-workflow")+ assert result == (None, 204)workflow_service.delete_workflow.assert_called_once()- assert result == (None, 204)+ assert events == ["transaction-enter", "delete", "transaction-exit"] + ([] if transaction_fails else ["retire"])+ if transaction_fails:+ retire_unowned.assert_not_called()+ else:+ retire_unowned.assert_called_once_with(+ tenant_id=pipeline.tenant_id,+ agent_ids=["inline-agent"],+ account_id=user.id,+ )def test_delete_active_workflow_rejected(self, app: Flask) -> None:api = RagPipelineByIdApi()method = unwrap(api.delete)pipeline = make_pipeline(tenant_id="t1", workflow_id="active-workflow")+ user = make_account()with app.test_request_context("/", method="DELETE"):with pytest.raises(BadRequest, match="currently in use by pipeline"):- method(api, pipeline, "active-workflow")+ method(api, user, pipeline, "active-workflow")class TestRagPipelineWorkflowLastRunApi:diff --git a/api/tests/unit_tests/controllers/console/snippets/test_snippet_workflow.py b/api/tests/unit_tests/controllers/console/snippets/test_snippet_workflow.pyindex 06b984799ff..ad4de5468ab 100644--- a/api/tests/unit_tests/controllers/console/snippets/test_snippet_workflow.py+++ b/api/tests/unit_tests/controllers/console/snippets/test_snippet_workflow.py@@ -197,6 +197,33 @@ def test_published_workflow_post_returns_400_when_publish_fails(assert snippet.name == "Snippet"[email protected]("sqlite_session", [(CustomizedSnippet,)], indirect=True)+def test_published_workflow_post_returns_success(+ app: Flask,+ monkeypatch: pytest.MonkeyPatch,+ sqlite_engine: Engine,+ sqlite_session: Session,+) -> None:+ user = _account("account-1")+ snippet = _snippet()+ sqlite_session.add(snippet)+ sqlite_session.commit()+ workflow = SimpleNamespace(created_at=datetime(2026, 8, 17, 12, 0, 0))+ monkeypatch.setattr(snippet_workflow_module, "db", SimpleNamespace(engine=sqlite_engine))+ monkeypatch.setattr(+ snippet_workflow_module,+ "_snippet_service",+ lambda: SimpleNamespace(publish_workflow=Mock(return_value=workflow)),+ )++ api = snippet_workflow_module.SnippetPublishedWorkflowApi()+ handler = unwrap(api.post)+ with app.test_request_context("/snippets/snippet-1/workflows/publish", method="POST", json={}):+ response = handler(api, user, snippet)++ assert response["result"] == "success"++def test_default_block_configs_delegates_to_service(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:get_default_block_configs = Mock(return_value=[{"type": "llm"}])monkeypatch.setattr(diff --git a/api/tests/unit_tests/services/agent/test_agent_services.py b/api/tests/unit_tests/services/agent/test_agent_services.pyindex 2042dbee794..c3b70798924 100644--- a/api/tests/unit_tests/services/agent/test_agent_services.py+++ b/api/tests/unit_tests/services/agent/test_agent_services.py@@ -25,7 +25,6 @@ from models.agent import (AgentScope,AgentSource,AgentStatus,- AgentWorkspaceBinding,AgentWorkspaceOwnerType,WorkflowAgentBindingType,WorkflowAgentNodeBinding,@@ -562,14 +561,8 @@ def test_save_workflow_composer_commits_before_retiring_replaced_inline_agent(def retire_unowned(**kwargs):assert kwargs["agent_ids"] == {"old-inline-agent"}events.append("retire")- return ["binding-1"], ["home-1"]monkeypatch.setattr(composer_service.WorkflowAgentRetirementService, "retire_unowned", retire_unowned)- monkeypatch.setattr(- composer_service,- "enqueue_agent_resource_collection",- MagicMock(side_effect=lambda **_kwargs: events.append("enqueue")),- )payload = ComposerSavePayload.model_validate({"variant": ComposerVariant.WORKFLOW,@@ -589,7 +582,7 @@ def test_save_workflow_composer_commits_before_retiring_replaced_inline_agent(payload=payload,)- assert events == ["commit", "retire", "enqueue"]+ assert events == ["commit", "retire"]def test_save_workflow_composer_rejects_agent_app_variant(sqlite_session: Session):@@ -3878,7 +3871,7 @@ def test_reference_counts_include_draft_and_published_bindings_once_per_app(sqliassert result == {"agent-1": 1}-def test_roster_update_archive_versions_and_detail(monkeypatch: pytest.MonkeyPatch, sqlite_session: Session):+def test_roster_update_versions_and_detail(monkeypatch: pytest.MonkeyPatch, sqlite_session: Session):session = sqlite_sessionlisted_version = AgentConfigSnapshot(id="version-4",@@ -3933,8 +3926,6 @@ def test_roster_update_archive_versions_and_detail(monkeypatch: pytest.MonkeyPatsession.add_all([agent, listed_version, older_listed_version, revision, listed_revision])session.commit()service = AgentRosterService(session)- retire_snapshots = MagicMock(return_value=[])- monkeypatch.setattr(AgentHomeSnapshotService, "retire_all_for_agent", retire_snapshots)monkeypatch.setattr(service,"get_roster_agent_detail",@@ -3947,13 +3938,10 @@ def test_roster_update_archive_versions_and_detail(monkeypatch: pytest.MonkeyPataccount_id="account-1",payload=roster_service.RosterAgentUpdatePayload(description="new"),)- service.archive_roster_agent(tenant_id="tenant-1", agent_id="agent-1", account_id="account-1")versions = service.list_agent_versions(tenant_id="tenant-1", agent_id="agent-1")detail = service.get_agent_version_detail(tenant_id="tenant-1", agent_id="agent-1", version_id="version-2")assert updated["description"] == "new"- assert agent.status == AgentStatus.ARCHIVED- retire_snapshots.assert_called_once_with(session=session, tenant_id="tenant-1", agent_id="agent-1")assert versions[0]["id"] == "version-4"assert versions[0]["version"] == 2assert versions[0]["display_version"] == 2@@ -3970,69 +3958,6 @@ def test_roster_update_archive_versions_and_detail(monkeypatch: pytest.MonkeyPatassert detail["revisions"][0]["created_at"] == int(revision_created_at.timestamp())-def test_roster_archive_retires_then_commits_before_enqueue(- monkeypatch: pytest.MonkeyPatch, sqlite_session: Session-) -> None:- session = sqlite_session- service = AgentRosterService(session)- agent = _agent()- binding = AgentWorkspaceBinding(- id="binding-1",- tenant_id=agent.tenant_id,- app_id="app-1",- workspace_id="workspace-1",- agent_id=agent.id,- agent_config_version_id="version-1",- agent_config_version_kind=AgentConfigVersionKind.SNAPSHOT,- backend_binding_ref="backend-binding-1",- )- session.add_all([agent, binding])- session.commit()- events: list[str] = []- monkeypatch.setattr(- AgentWorkspaceService,- "retire_binding",- MagicMock(side_effect=lambda **_kwargs: events.append("retire-binding") or "binding-1"),- )- monkeypatch.setattr(- AgentHomeSnapshotService,- "retire_all_for_agent",- MagicMock(side_effect=lambda **_kwargs: events.append("retire-home") or ["home-1"]),- )- event.listen(session, "after_commit", lambda _session: events.append("commit"))- monkeypatch.setattr(- roster_service,- "enqueue_agent_resource_collection",- MagicMock(side_effect=lambda **_kwargs: events.append("enqueue")),- )-- service.archive_roster_agent(tenant_id="tenant-1", agent_id="agent-1", account_id="account-1")-- assert events == ["retire-binding", "retire-home", "commit", "enqueue"]---def test_roster_archive_commit_failure_does_not_enqueue(- monkeypatch: pytest.MonkeyPatch, sqlite_session: Session-) -> None:- session = sqlite_session- service = AgentRosterService(session)- session.add(_agent())- session.commit()- monkeypatch.setattr(AgentHomeSnapshotService, "retire_all_for_agent", MagicMock(return_value=["home-1"]))- event.listen(- session,- "before_commit",- lambda _session: (_ for _ in ()).throw(RuntimeError("commit failed")),- )- enqueue_collection = MagicMock()- monkeypatch.setattr(roster_service, "enqueue_agent_resource_collection", enqueue_collection)-- with pytest.raises(RuntimeError, match="commit failed"):- service.archive_roster_agent(tenant_id="tenant-1", agent_id="agent-1", account_id="account-1")-- enqueue_collection.assert_not_called()--def test_roster_create_detail_and_lookup_helpers(monkeypatch: pytest.MonkeyPatch, sqlite_session: Session):session = sqlite_sessionservice = AgentRosterService(session)diff --git a/api/tests/unit_tests/services/agent/test_deletion_service.py b/api/tests/unit_tests/services/agent/test_deletion_service.pynew file mode 100644index 00000000000..26e9c472a6b--- /dev/null+++ b/api/tests/unit_tests/services/agent/test_deletion_service.py@@ -0,0 +1,373 @@+from collections.abc import Generator+from contextlib import contextmanager, nullcontext+from decimal import Decimal+from typing import cast+from unittest.mock import MagicMock++import pytest+from sqlalchemy import Table+from sqlalchemy.orm import Session, sessionmaker+from sqlalchemy.sql.dml import Delete++from models.agent import (+ Agent,+ AgentConfigDraft,+ AgentConfigDraftType,+ AgentConfigRevision,+ AgentConfigRevisionOperation,+ AgentConfigSnapshot,+ AgentConfigVersionKind,+ AgentDebugConversation,+ AgentHomeSnapshot,+ AgentKind,+ AgentScope,+ AgentSource,+ AgentStatus,+ AgentWorkingResourceStatus,+ AgentWorkspace,+ AgentWorkspaceBinding,+ AgentWorkspaceOwnerType,+ WorkflowAgentBindingType,+ WorkflowAgentNodeBinding,+)+from models.agent_config_entities import AgentSoulConfig+from models.enums import AppStatus, ConversationFromSource, ConversationStatus+from models.model import App, AppMode, Conversation, Message+from services.agent.deletion_service import AgentDeletionInvariantError, AgentDeletionService+++def _archived_agent(+ *,+ agent_id: str = "agent-1",+ tenant_id: str = "tenant-1",+ status: AgentStatus = AgentStatus.ARCHIVED,+) -> Agent:+ return Agent(+ id=agent_id,+ tenant_id=tenant_id,+ name="Agent",+ description="",+ role="",+ agent_kind=AgentKind.DIFY_AGENT,+ scope=AgentScope.ROSTER,+ source=AgentSource.AGENT_APP,+ status=status,+ )+++def test_purge_archived_agent_deletes_complete_aggregate_and_preserves_workflow_binding(+ monkeypatch: pytest.MonkeyPatch,+ sqlite_session: Session,+ sqlite_session_factory: sessionmaker[Session],+) -> None:+ agent = _archived_agent()+ snapshot = AgentConfigSnapshot(+ id="snapshot-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ version=1,+ config_snapshot=AgentSoulConfig(),+ )+ dangling_binding = WorkflowAgentNodeBinding(+ id="workflow-binding-1",+ tenant_id=agent.tenant_id,+ app_id="missing-app",+ workflow_id="missing-workflow",+ workflow_version="old-version",+ node_id="agent-node",+ binding_type=WorkflowAgentBindingType.ROSTER_AGENT,+ agent_id=agent.id,+ current_snapshot_id=snapshot.id,+ node_job_config={},+ )+ rows = [+ agent,+ snapshot,+ AgentConfigDraft(+ id="draft-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ draft_type=AgentConfigDraftType.DRAFT,+ draft_owner_key="",+ config_snapshot=AgentSoulConfig(),+ ),+ AgentConfigDraft(+ id="build-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ draft_type=AgentConfigDraftType.DEBUG_BUILD,+ account_id="account-1",+ draft_owner_key="account-1",+ config_snapshot=AgentSoulConfig(),+ ),+ AgentConfigRevision(+ id="revision-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ current_snapshot_id=snapshot.id,+ revision=1,+ operation=AgentConfigRevisionOperation.CREATE_VERSION,+ ),+ AgentDebugConversation(+ id="debug-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ app_id="app-1",+ account_id="account-1",+ draft_type=AgentConfigDraftType.DRAFT,+ conversation_id="conversation-1",+ ),+ AgentHomeSnapshot(+ id="home-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ snapshot_ref="home-ref",+ status=AgentWorkingResourceStatus.RETIRED,+ ),+ AgentWorkspaceBinding(+ id="binding-1",+ tenant_id=agent.tenant_id,+ app_id="app-1",+ workspace_id="workspace-1",+ agent_id=agent.id,+ agent_config_version_id=snapshot.id,+ agent_config_version_kind=AgentConfigVersionKind.SNAPSHOT,+ backend_binding_ref="binding-ref",+ status=AgentWorkingResourceStatus.RETIRED,+ ),+ ]+ sibling = _archived_agent(agent_id="agent-2")+ other_tenant = _archived_agent(agent_id="agent-3", tenant_id="tenant-2")+ unrelated_app = App(+ id="unrelated-app",+ tenant_id=agent.tenant_id,+ name="Unrelated",+ mode=AppMode.WORKFLOW,+ status=AppStatus.NORMAL,+ enable_site=True,+ enable_api=True,+ )+ conversation = Conversation(+ id="conversation-1",+ app_id=unrelated_app.id,+ mode=AppMode.AGENT_CHAT,+ name="Preserved conversation",+ _inputs={},+ status=ConversationStatus.NORMAL,+ from_source=ConversationFromSource.CONSOLE,+ from_account_id="account-1",+ )+ preserved_rows = [+ sibling,+ other_tenant,+ AgentConfigDraft(+ id="sibling-draft",+ tenant_id=sibling.tenant_id,+ agent_id=sibling.id,+ draft_type=AgentConfigDraftType.DRAFT,+ draft_owner_key="",+ config_snapshot=AgentSoulConfig(),+ ),+ AgentHomeSnapshot(+ id="other-home",+ tenant_id=other_tenant.tenant_id,+ agent_id=other_tenant.id,+ snapshot_ref="other-home-ref",+ status=AgentWorkingResourceStatus.RETIRED,+ ),+ unrelated_app,+ AgentWorkspace(+ id="workspace-1",+ tenant_id=agent.tenant_id,+ app_id=unrelated_app.id,+ owner_type=AgentWorkspaceOwnerType.CONVERSATION,+ owner_id=conversation.id,+ owner_scope_key="root",+ backend_workspace_ref="workspace-ref",+ status=AgentWorkingResourceStatus.ACTIVE,+ active_guard=1,+ ),+ conversation,+ Message(+ id="message-1",+ app_id=unrelated_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",+ from_source=ConversationFromSource.CONSOLE,+ from_account_id="account-1",+ ),+ ]+ sqlite_session.add_all([*rows, dangling_binding, *preserved_rows])+ sqlite_session.commit()+ monkeypatch.setattr(+ "services.agent.deletion_service.session_factory.create_session",+ sqlite_session_factory,+ )++ AgentDeletionService.purge_archived_agents(tenant_id=agent.tenant_id, agent_ids=[agent.id])++ with sqlite_session_factory() as observer_session:+ for row in rows:+ assert observer_session.get(type(row), row.id) is None+ preserved_binding = observer_session.get(WorkflowAgentNodeBinding, dangling_binding.id)+ assert preserved_binding is not None+ assert preserved_binding.agent_id == agent.id+ for row in preserved_rows:+ assert observer_session.get(type(row), row.id) is not None+++def test_purge_bulk_deletes_aggregate_dependencies_before_agent(monkeypatch: pytest.MonkeyPatch) -> None:+ context = MagicMock()+ session = context.__enter__.return_value+ session.scalars.return_value.all.return_value = [_archived_agent()]+ session.scalar.side_effect = [None, None]+ deleted_tables: list[str] = []++ def record_bulk_delete(statement: object) -> None:+ if isinstance(statement, Delete):+ deleted_tables.append(cast(Table, statement.table).name)++ session.execute.side_effect = record_bulk_delete+ monkeypatch.setattr("services.agent.deletion_service.session_factory.create_session", lambda: context)+ AgentDeletionService.purge_archived_agents(tenant_id="tenant-1", agent_ids=["agent-1"])++ assert deleted_tables == [+ cast(Table, model.__table__).name+ for model in (+ AgentDebugConversation,+ AgentConfigRevision,+ AgentConfigDraft,+ AgentConfigSnapshot,+ AgentHomeSnapshot,+ AgentWorkspaceBinding,+ Agent,+ )+ ]+++ ("invariant", "expected_error"),+ [+ ("non_archived", "must be ARCHIVED"),+ ("active_binding", "still has ACTIVE Binding"),+ ("active_home", "still has ACTIVE Home Snapshot"),+ ],+)+def test_purge_rejects_invalid_aggregate_invariants(+ monkeypatch: pytest.MonkeyPatch,+ sqlite_session: Session,+ invariant: str,+ expected_error: str,+) -> None:+ agent = _archived_agent(status=AgentStatus.ACTIVE if invariant == "non_archived" else AgentStatus.ARCHIVED)+ related: AgentWorkspaceBinding | AgentHomeSnapshot | None = None+ if invariant == "active_binding":+ related = AgentWorkspaceBinding(+ id="binding-1",+ tenant_id=agent.tenant_id,+ app_id="app-1",+ workspace_id="workspace-1",+ agent_id=agent.id,+ agent_config_version_id="snapshot-1",+ agent_config_version_kind=AgentConfigVersionKind.SNAPSHOT,+ backend_binding_ref="binding-ref",+ status=AgentWorkingResourceStatus.ACTIVE,+ )+ elif invariant == "active_home":+ related = AgentHomeSnapshot(+ id="home-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ snapshot_ref="home-ref",+ status=AgentWorkingResourceStatus.ACTIVE,+ )+ sqlite_session.add(agent)+ if related is not None:+ sqlite_session.add(related)+ sqlite_session.commit()+ monkeypatch.setattr(+ "services.agent.deletion_service.session_factory.create_session",+ lambda: nullcontext(sqlite_session),+ )++ with pytest.raises(AgentDeletionInvariantError, match=expected_error):+ AgentDeletionService.purge_archived_agents(tenant_id=agent.tenant_id, agent_ids=[agent.id])++ assert sqlite_session.get(Agent, agent.id) is not None+ if related is not None:+ assert sqlite_session.get(type(related), related.id) is not None+++def test_purge_is_idempotent_for_empty_missing_and_repeated_ids(+ monkeypatch: pytest.MonkeyPatch, sqlite_session: Session+) -> None:+ agent = _archived_agent()+ sqlite_session.add(agent)+ sqlite_session.commit()+ monkeypatch.setattr(+ "services.agent.deletion_service.session_factory.create_session",+ lambda: nullcontext(sqlite_session),+ )++ agent_id = agent.id++ AgentDeletionService.purge_archived_agents(tenant_id=agent.tenant_id, agent_ids=[])+ assert sqlite_session.get(Agent, agent_id) is not None++ AgentDeletionService.purge_archived_agents(tenant_id=agent.tenant_id, agent_ids=["missing-agent"])+ assert sqlite_session.get(Agent, agent_id) is not None++ AgentDeletionService.purge_archived_agents(tenant_id=agent.tenant_id, agent_ids=[agent_id, agent_id])+ assert sqlite_session.get(Agent, agent_id) is None++ AgentDeletionService.purge_archived_agents(tenant_id=agent.tenant_id, agent_ids=[agent_id])+ assert sqlite_session.get(Agent, agent_id) is None++[email protected]("failure_stage", ["delete", "commit"])+def test_purge_failure_rolls_back_complete_aggregate(+ monkeypatch: pytest.MonkeyPatch,+ sqlite_session: Session,+ sqlite_session_factory: sessionmaker[Session],+ failure_stage: str,+) -> None:+ agent = _archived_agent()+ draft = AgentConfigDraft(+ id="draft-1",+ tenant_id=agent.tenant_id,+ agent_id=agent.id,+ draft_type=AgentConfigDraftType.DRAFT,+ draft_owner_key="",+ config_snapshot=AgentSoulConfig(),+ )+ sqlite_session.add_all([agent, draft])+ sqlite_session.commit()+ agent_id = agent.id+ draft_id = draft.id+ error = RuntimeError(f"{failure_stage} failed")++ @contextmanager+ def failing_session() -> Generator[Session]:+ with sqlite_session_factory() as service_session:+ failure_method = failure_stage if failure_stage == "commit" else "execute"+ monkeypatch.setattr(service_session, failure_method, MagicMock(side_effect=error))+ yield service_session++ monkeypatch.setattr(+ "services.agent.deletion_service.session_factory.create_session",+ failing_session,+ )++ with pytest.raises(RuntimeError) as exc_info:+ AgentDeletionService.purge_archived_agents(tenant_id=agent.tenant_id, agent_ids=[agent_id])++ assert exc_info.value is error+ with sqlite_session_factory() as observer_session:+ assert observer_session.get(Agent, agent_id) is not None+ assert observer_session.get(AgentConfigDraft, draft_id) is not Nonediff --git a/api/tests/unit_tests/services/agent/test_home_snapshot_service.py b/api/tests/unit_tests/services/agent/test_home_snapshot_service.pyindex abe565aaa94..b3476f1c120 100644--- a/api/tests/unit_tests/services/agent/test_home_snapshot_service.py+++ b/api/tests/unit_tests/services/agent/test_home_snapshot_service.py@@ -148,6 +148,42 @@ def test_home_snapshot_collection_database_failure_propagates(monkeypatch: pytesassert exc_info.value is error[email protected]("snapshot_state", ["missing", "active"])[email protected]("sqlite_session", [(AgentHomeSnapshot,)], indirect=True)+def test_home_snapshot_collection_non_retired_target_is_idempotent_noop(+ monkeypatch: pytest.MonkeyPatch,+ sqlite_session: Session,+ snapshot_state: str,+) -> None:+ if snapshot_state == "active":+ sqlite_session.add(+ AgentHomeSnapshot(+ id="home-1",+ tenant_id="tenant-1",+ agent_id="agent-1",+ snapshot_ref="snapshot-ref-1",+ status=AgentWorkingResourceStatus.ACTIVE,+ )+ )+ sqlite_session.commit()+ delete = MagicMock()+ commit = MagicMock(wraps=sqlite_session.commit)+ monkeypatch.setattr(+ "services.agent.home_snapshot_service.session_factory.create_session",+ lambda: nullcontext(sqlite_session),+ )+ monkeypatch.setattr(AgentHomeSnapshotService, "delete", delete)+ monkeypatch.setattr(sqlite_session, "commit", commit)++ AgentHomeSnapshotService.collect_retired_home_snapshot(+ tenant_id="tenant-1",+ home_snapshot_id="home-1",+ )++ delete.assert_not_called()+ commit.assert_not_called()++@pytest.mark.parametrize("sqlite_session",[(AgentHomeSnapshot, AgentConfigDraft, AgentConfigSnapshot)],@@ -185,6 +221,50 @@ def test_home_snapshot_collection_backend_failure_propagates_and_preserves_retirassert stored_snapshot.status is AgentWorkingResourceStatus.RETIRED+ "sqlite_session",+ [(AgentHomeSnapshot, AgentConfigDraft, AgentConfigSnapshot)],+ indirect=True,+)+def test_home_snapshot_collection_ignores_config_references(+ monkeypatch: pytest.MonkeyPatch, sqlite_session: Session+) -> None:+ snapshot = AgentHomeSnapshot(+ id="home-1",+ tenant_id="tenant-1",+ agent_id="agent-1",+ snapshot_ref="snapshot-ref-1",+ status=AgentWorkingResourceStatus.RETIRED,+ )+ draft = _build_draft(home_snapshot_id=snapshot.id)+ config_snapshot = AgentConfigSnapshot(+ id="config-1",+ tenant_id="tenant-1",+ agent_id="agent-1",+ version=1,+ home_snapshot_id=snapshot.id,+ config_snapshot=AgentSoulConfig(),+ )+ sqlite_session.add_all([snapshot, draft, config_snapshot])+ sqlite_session.commit()+ delete = MagicMock()+ monkeypatch.setattr(+ "services.agent.home_snapshot_service.session_factory.create_session",+ lambda: nullcontext(sqlite_session),+ )+ monkeypatch.setattr(AgentHomeSnapshotService, "delete", delete)++ AgentHomeSnapshotService.collect_retired_home_snapshot(+ tenant_id="tenant-1",+ home_snapshot_id=snapshot.id,+ )++ delete.assert_called_once_with(snapshot_ref=snapshot.snapshot_ref)+ assert sqlite_session.get(AgentHomeSnapshot, snapshot.id) is None+ assert sqlite_session.get(AgentConfigDraft, draft.id) is not None+ assert sqlite_session.get(AgentConfigSnapshot, config_snapshot.id) is not None++@pytest.mark.parametrize("sqlite_session",[(AgentHomeSnapshot, AgentConfigDraft, AgentConfigSnapshot)],diff --git a/api/tests/unit_tests/services/agent/test_retirement_service.py b/api/tests/unit_tests/services/agent/test_retirement_service.pyindex 71362eecfe3..698f5a2a799 100644--- a/api/tests/unit_tests/services/agent/test_retirement_service.py+++ b/api/tests/unit_tests/services/agent/test_retirement_service.py@@ -28,10 +28,9 @@ from services.agent.retirement_service import WorkflowAgentRetirementServicefrom services.agent.workspace_service import AgentWorkspaceService-def test_retire_unowned_commits_resource_retirement(monkeypatch: pytest.MonkeyPatch) -> None:+def test_retire_unowned_failure_propagates(monkeypatch: pytest.MonkeyPatch) -> None:context = MagicMock()- session = context.__enter__.return_value- session.scalars.return_value.all.return_value = [SimpleNamespace(id="binding-1")]+ error = RuntimeError("retirement failed")monkeypatch.setattr("services.agent.retirement_service.session_factory.create_session",lambda: context,@@ -39,30 +38,20 @@ def test_retire_unowned_commits_resource_retirement(monkeypatch: pytest.MonkeyPamonkeypatch.setattr(WorkflowAgentRetirementService,"archive_unowned",- MagicMock(return_value=["agent-1"]),- )- monkeypatch.setattr(- AgentWorkspaceService,- "retire_binding",- MagicMock(return_value="binding-1"),- )- monkeypatch.setattr(- AgentHomeSnapshotService,- "retire_all_for_agent",- MagicMock(return_value=["home-1"]),+ MagicMock(side_effect=error),)- result = WorkflowAgentRetirementService.retire_unowned(- tenant_id="tenant-1",- agent_ids=["agent-1"],- account_id="account-1",- )+ with pytest.raises(RuntimeError) as exc_info:+ WorkflowAgentRetirementService.retire_unowned(+ tenant_id="tenant-1",+ agent_ids=["agent-1"],+ account_id="account-1",+ )- assert result == (["binding-1"], ["home-1"])- session.commit.assert_called_once_with()+ assert exc_info.value is error-def _workflow_only_agent() -> Agent:+def _workflow_only_agent(*, backing_app_id: str | None = None) -> Agent:return Agent(id="agent-1",tenant_id="tenant-1",@@ -73,17 +62,29 @@ def _workflow_only_agent() -> Agent:scope=AgentScope.WORKFLOW_ONLY,source=AgentSource.WORKFLOW,status=AgentStatus.ACTIVE,+ backing_app_id=backing_app_id,)@pytest.mark.parametrize(- "sqlite_session",- [(Agent, App, Workflow, WorkflowAgentNodeBinding)],- indirect=True,+ ("workflow_version", "pointer_to_owner", "mismatched_key", "expected_status"),+ [+ pytest.param(Workflow.VERSION_DRAFT, False, None, AgentStatus.ACTIVE, id="draft-owner"),+ pytest.param("current-version", True, None, AgentStatus.ACTIVE, id="current-published-owner"),+ pytest.param("historical-version", False, None, AgentStatus.ACTIVE, id="historical-published-owner"),+ pytest.param("v1", True, "tenant_id", AgentStatus.ARCHIVED, id="tenant-mismatch"),+ pytest.param("v1", True, "app_id", AgentStatus.ARCHIVED, id="app-mismatch"),+ pytest.param("v1", True, "workflow_id", AgentStatus.ARCHIVED, id="workflow-mismatch"),+ pytest.param("v1", True, "workflow_version", AgentStatus.ARCHIVED, id="version-mismatch"),+ ],)-def test_retire_unowned_keeps_effectively_owned_agent_active(+def test_retire_unowned_requires_an_exact_persisted_workflow_owner_key(monkeypatch: pytest.MonkeyPatch,sqlite_session: Session,+ workflow_version: str,+ pointer_to_owner: bool,+ mismatched_key: str | None,+ expected_status: AgentStatus,) -> None:agent = _workflow_only_agent()app = App(@@ -96,10 +97,10 @@ def test_retire_unowned_keeps_effectively_owned_agent_active(enable_api=True,)workflow = Workflow.new(- tenant_id="tenant-1",+ tenant_id="workflow-tenant" if mismatched_key == "tenant_id" else "tenant-1",app_id=app.id,type=WorkflowType.WORKFLOW.value,- version=Workflow.VERSION_DRAFT,+ version=workflow_version,graph="{}",features="{}",created_by="account-1",@@ -107,11 +108,22 @@ def test_retire_unowned_keeps_effectively_owned_agent_active(conversation_variables=[],rag_pipeline_variables=[],)+ app.workflow_id = workflow.id if pointer_to_owner else "another-current-workflow"+ binding_key = {+ "tenant_id": "tenant-1",+ "app_id": workflow.app_id,+ "workflow_id": workflow.id,+ "workflow_version": workflow.version,+ }+ mismatched_values = {+ "app_id": "app-2",+ "workflow_id": "workflow-2",+ "workflow_version": "other-version",+ }+ if mismatched_key is not None and mismatched_key != "tenant_id":+ binding_key[mismatched_key] = mismatched_values[mismatched_key]binding = WorkflowAgentNodeBinding(- tenant_id="tenant-1",- app_id=app.id,- workflow_id=workflow.id,- workflow_version=workflow.version,+ **binding_key,node_id="agent-node",binding_type=WorkflowAgentBindingType.INLINE_AGENT,agent_id=agent.id,@@ -124,17 +136,21 @@ def test_retire_unowned_keeps_effectively_owned_agent_active("services.agent.retirement_service.session_factory.create_session",lambda: nullcontext(sqlite_session),)-- result = WorkflowAgentRetirementService.retire_unowned(+ celery_delay = MagicMock()+ monkeypatch.setattr("tasks.collect_agent_resources_task.collect_agent_resources.delay", celery_delay)+ WorkflowAgentRetirementService.retire_unowned(tenant_id="tenant-1",agent_ids=[agent.id],account_id="account-1",)- assert result == ([], [])stored_agent = sqlite_session.get(Agent, agent.id)assert stored_agent is not None- assert stored_agent.status is AgentStatus.ACTIVE+ assert stored_agent.status is expected_status+ if expected_status is AgentStatus.ACTIVE:+ celery_delay.assert_not_called()+ else:+ celery_delay.assert_called_once()@pytest.mark.parametrize(@@ -146,7 +162,16 @@ def test_retire_unowned_archives_orphan_and_retires_resources(monkeypatch: pytest.MonkeyPatch,sqlite_session: Session,) -> None:- agent = _workflow_only_agent()+ agent = _workflow_only_agent(backing_app_id="hidden-app-1")+ hidden_app = App(+ id="hidden-app-1",+ tenant_id="tenant-1",+ name="Inline Agent runtime",+ mode=AppMode.AGENT,+ status=AppStatus.NORMAL,+ enable_site=True,+ enable_api=True,+ )home = AgentHomeSnapshot(id="home-1",tenant_id="tenant-1",@@ -157,7 +182,7 @@ def test_retire_unowned_archives_orphan_and_retires_resources(workspace = AgentWorkspace(id="workspace-1",tenant_id="tenant-1",- app_id="app-1",+ app_id=hidden_app.id,owner_type=AgentWorkspaceOwnerType.CONVERSATION,owner_id="conversation-1",owner_scope_key="root",@@ -168,7 +193,7 @@ def test_retire_unowned_archives_orphan_and_retires_resources(binding = AgentWorkspaceBinding(id="binding-1",tenant_id="tenant-1",- app_id="app-1",+ app_id=hidden_app.id,workspace_id=workspace.id,agent_id=agent.id,base_home_snapshot_id=home.id,@@ -177,20 +202,26 @@ def test_retire_unowned_archives_orphan_and_retires_resources(backend_binding_ref="binding-ref",status=AgentWorkingResourceStatus.ACTIVE,)- sqlite_session.add_all([agent, home, workspace, binding])+ sqlite_session.add_all([agent, hidden_app, home, workspace, binding])sqlite_session.commit()monkeypatch.setattr("services.agent.retirement_service.session_factory.create_session",lambda: nullcontext(sqlite_session),)+ cleanup_app = MagicMock()+ enqueue_collection = MagicMock()+ monkeypatch.setattr("services.agent.retirement_service.remove_app_and_related_data_task.delay", cleanup_app)+ monkeypatch.setattr(+ "services.agent.retirement_service.enqueue_agent_resource_collection",+ enqueue_collection,+ )- result = WorkflowAgentRetirementService.retire_unowned(+ WorkflowAgentRetirementService.retire_unowned(tenant_id="tenant-1",agent_ids=[agent.id],account_id="account-1",)- assert result == ([binding.id], [home.id])stored_agent = sqlite_session.get(Agent, agent.id)stored_binding = sqlite_session.get(AgentWorkspaceBinding, binding.id)stored_workspace = sqlite_session.get(AgentWorkspace, workspace.id)@@ -199,7 +230,165 @@ def test_retire_unowned_archives_orphan_and_retires_resources(assert stored_binding is not Noneassert stored_workspace is not Noneassert stored_home is not None+ assert sqlite_session.get(App, hidden_app.id) is Noneassert stored_agent.status is AgentStatus.ARCHIVEDassert stored_binding.status is AgentWorkingResourceStatus.RETIREDassert stored_workspace.status is AgentWorkingResourceStatus.RETIREDassert stored_home.status is AgentWorkingResourceStatus.RETIRED+ cleanup_app.assert_called_once_with(tenant_id="tenant-1", app_id=hidden_app.id)+ enqueue_collection.assert_called_once_with(+ tenant_id="tenant-1",+ workspace_ids=[workspace.id],+ binding_ids=[binding.id],+ home_snapshot_ids=[home.id],+ purge_agent_ids=[agent.id],+ )+++def test_hidden_app_enqueue_failure_prevents_agent_purge_enqueue(monkeypatch: pytest.MonkeyPatch) -> None:+ context = MagicMock()+ session = context.__enter__.return_value+ session.scalars.side_effect = [+ SimpleNamespace(+ all=MagicMock(+ return_value=[+ SimpleNamespace(backing_app_id="hidden-app-1"),+ SimpleNamespace(backing_app_id="hidden-app-2"),+ ]+ )+ ),+ SimpleNamespace(all=MagicMock(return_value=[])),+ SimpleNamespace(all=MagicMock(return_value=[])),+ SimpleNamespace(all=MagicMock(return_value=[])),+ SimpleNamespace(all=MagicMock(return_value=[])),+ ]+ monkeypatch.setattr(+ "services.agent.retirement_service.session_factory.create_session",+ lambda: context,+ )+ monkeypatch.setattr(+ WorkflowAgentRetirementService,+ "archive_unowned",+ MagicMock(return_value=["agent-1", "agent-2"]),+ )+ monkeypatch.setattr(AgentWorkspaceService, "retire_all_for_app", MagicMock(return_value=[]))+ monkeypatch.setattr(AgentHomeSnapshotService, "retire_all_for_agent", MagicMock(return_value=[]))+ error = RuntimeError("broker unavailable")+ cleanup_app = MagicMock(side_effect=[None, error])+ monkeypatch.setattr("services.agent.retirement_service.remove_app_and_related_data_task.delay", cleanup_app)+ enqueue_collection = MagicMock()+ monkeypatch.setattr(+ "services.agent.retirement_service.enqueue_agent_resource_collection",+ enqueue_collection,+ )++ with pytest.raises(RuntimeError) as exc_info:+ WorkflowAgentRetirementService.retire_unowned(+ tenant_id="tenant-1",+ agent_ids=["agent-1", "agent-2"],+ account_id="account-1",+ )++ assert exc_info.value is error+ assert [call.kwargs["app_id"] for call in cleanup_app.call_args_list] == ["hidden-app-1", "hidden-app-2"]+ enqueue_collection.assert_not_called()+++def test_retire_unowned_retry_after_hidden_app_enqueue_failure_preserves_full_collector_payload(+ monkeypatch: pytest.MonkeyPatch,+ sqlite_session: Session,+) -> None:+ agent = _workflow_only_agent(backing_app_id="hidden-app-1")+ hidden_app = App(+ id="hidden-app-1",+ tenant_id="tenant-1",+ name="Inline Agent runtime",+ mode=AppMode.AGENT,+ status=AppStatus.NORMAL,+ enable_site=False,+ enable_api=False,+ )+ home = AgentHomeSnapshot(+ id="home-1",+ tenant_id="tenant-1",+ agent_id=agent.id,+ snapshot_ref="home-ref",+ status=AgentWorkingResourceStatus.ACTIVE,+ )+ workspace = AgentWorkspace(+ id="workspace-1",+ tenant_id="tenant-1",+ app_id=hidden_app.id,+ owner_type=AgentWorkspaceOwnerType.CONVERSATION,+ owner_id="conversation-1",+ owner_scope_key="root",+ backend_workspace_ref="workspace-ref",+ status=AgentWorkingResourceStatus.ACTIVE,+ active_guard=1,+ )+ binding = AgentWorkspaceBinding(+ id="binding-1",+ tenant_id="tenant-1",+ app_id=hidden_app.id,+ workspace_id=workspace.id,+ agent_id=agent.id,+ base_home_snapshot_id=home.id,+ agent_config_version_id="config-1",+ agent_config_version_kind=AgentConfigVersionKind.SNAPSHOT,+ backend_binding_ref="binding-ref",+ status=AgentWorkingResourceStatus.ACTIVE,+ )+ sqlite_session.add_all([agent, hidden_app, home, workspace, binding])+ sqlite_session.commit()+ agent_id = agent.id+ hidden_app_id = hidden_app.id+ home_id = home.id+ workspace_id = workspace.id+ binding_id = binding.id+ error = RuntimeError("broker unavailable")+ cleanup_app = MagicMock(side_effect=[error, None])+ enqueue_collection = MagicMock()+ monkeypatch.setattr("services.agent.retirement_service.remove_app_and_related_data_task.delay", cleanup_app)+ monkeypatch.setattr(+ "services.agent.retirement_service.enqueue_agent_resource_collection",+ enqueue_collection,+ )++ with pytest.raises(RuntimeError) as exc_info:+ WorkflowAgentRetirementService.retire_unowned(+ tenant_id="tenant-1",+ agent_ids=[agent_id],+ account_id="account-1",+ )++ assert exc_info.value is error+ sqlite_session.expire_all()+ stored_agent = sqlite_session.get(Agent, agent_id)+ stored_workspace = sqlite_session.get(AgentWorkspace, workspace_id)+ stored_binding = sqlite_session.get(AgentWorkspaceBinding, binding_id)+ stored_home = sqlite_session.get(AgentHomeSnapshot, home_id)+ assert stored_agent is not None+ assert stored_workspace is not None+ assert stored_binding is not None+ assert stored_home is not None+ assert stored_agent.status is AgentStatus.ARCHIVED+ assert sqlite_session.get(App, hidden_app_id) is None+ assert stored_workspace.status is AgentWorkingResourceStatus.RETIRED+ assert stored_binding.status is AgentWorkingResourceStatus.RETIRED+ assert stored_home.status is AgentWorkingResourceStatus.RETIRED+ enqueue_collection.assert_not_called()++ WorkflowAgentRetirementService.retire_unowned(+ tenant_id="tenant-1",+ agent_ids=[agent_id],+ account_id="account-1",+ )++ assert cleanup_app.call_count == 2+ enqueue_collection.assert_called_once_with(+ tenant_id="tenant-1",+ workspace_ids=[workspace_id],+ binding_ids=[binding_id],+ home_snapshot_ids=[home_id],+ purge_agent_ids=[agent_id],+ )diff --git a/api/tests/unit_tests/services/agent/test_workflow_publish_service.py b/api/tests/unit_tests/services/agent/test_workflow_publish_service.pyindex 87f376e1c4b..66f06b5ab78 100644--- a/api/tests/unit_tests/services/agent/test_workflow_publish_service.py+++ b/api/tests/unit_tests/services/agent/test_workflow_publish_service.py@@ -5,7 +5,7 @@ import pytestfrom sqlalchemy import selectfrom sqlalchemy.orm import Session-from models.agent import Agent, WorkflowAgentBindingType, WorkflowAgentNodeBinding+from models.agent import Agent, AgentScope, WorkflowAgentBindingType, WorkflowAgentNodeBindingfrom models.enums import AppStatusfrom models.model import App, AppModefrom models.workflow import Workflow, WorkflowType@@ -66,6 +66,38 @@ def test_inline_binding_from_another_node_is_cloned(monkeypatch: pytest.MonkeyPaassert binding.node_job_config.workflow_prompt == "Summarize the input"+def test_draft_sync_resolves_roster_agents() -> None:+ draft_workflow = _workflow()+ draft_workflow.graph = (+ '{"nodes":['+ '{"id":"node-b","data":{"type":"agent","version":"2","agent_node_kind":"dify_agent",'+ '"agent_binding":{"binding_type":"roster_agent","agent_id":"agent-b"}}},'+ '{"id":"node-a","data":{"type":"agent","version":"2","agent_node_kind":"dify_agent",'+ '"agent_binding":{"binding_type":"roster_agent","agent_id":"agent-a"}}}'+ '],"edges":[]}'+ )+ session = Mock()+ session.scalars.return_value = SimpleNamespace(all=lambda: [])+ agents = {+ agent_id: SimpleNamespace(+ id=agent_id,+ scope=AgentScope.ROSTER,+ active_config_snapshot_id=f"{agent_id}-snapshot",+ )+ for agent_id in ("agent-a", "agent-b")+ }+ session.scalar.side_effect = [agents["agent-b"], agents["agent-a"]]++ WorkflowAgentPublishService.sync_agent_bindings_for_draft(+ session=session,+ draft_workflow=draft_workflow,+ account_id="account-1",+ )++ assert session.scalar.call_count == 2+ assert {call.args[0].agent_id for call in session.add.call_args_list} == {"agent-a", "agent-b"}++def test_restore_replaces_bindings_and_returns_only_replaced_inline_agent() -> None:existing_inline = WorkflowAgentNodeBinding(tenant_id="tenant-1",@@ -108,6 +140,11 @@ def test_restore_replaces_bindings_and_returns_only_replaced_inline_agent() -> NSimpleNamespace(all=lambda: [existing_inline, existing_roster]),SimpleNamespace(all=lambda: [source]),]+ session.scalar.return_value = SimpleNamespace(+ id="roster-agent",+ scope=AgentScope.ROSTER,+ active_config_snapshot_id="published-snapshot",+ )retirement_candidates = WorkflowAgentPublishService.restore_agent_node_bindings_to_draft(session=session,source_workflow=_workflow(workflow_id="published-workflow", version="2026-07-13 00:00:00"),@@ -126,16 +163,54 @@ def test_restore_replaces_bindings_and_returns_only_replaced_inline_agent() -> Nassert restored.agent_id == "roster-agent"assert restored.current_snapshot_id == "published-snapshot"assert restored.node_job_config.workflow_prompt == "Use the roster agent"- session.flush.assert_called_once()assert retirement_candidates == {"old-inline-agent"}+def test_publish_copy_uses_current_roster_snapshot() -> None:+ draft_workflow = _workflow()+ draft_workflow.graph = (+ '{"nodes":[{"id":"agent-node","data":{"type":"agent","version":"2",'+ '"agent_node_kind":"dify_agent"}}],"edges":[]}'+ )+ published_workflow = _workflow(workflow_id="published", version="published")+ binding = WorkflowAgentNodeBinding(+ tenant_id="tenant-1",+ app_id="app-1",+ workflow_id="workflow-1",+ workflow_version=Workflow.VERSION_DRAFT,+ node_id="agent-node",+ binding_type=WorkflowAgentBindingType.ROSTER_AGENT,+ agent_id="roster-agent",+ current_snapshot_id="old-snapshot",+ node_job_config={},+ created_by="account-1",+ )+ session = Mock()+ active_agent = SimpleNamespace(+ id="roster-agent",+ scope=AgentScope.ROSTER,+ active_config_snapshot_id="active-snapshot",+ )+ session.scalar.return_value = active_agent+ session.scalars.return_value = SimpleNamespace(all=lambda: [binding])++ WorkflowAgentPublishService.copy_agent_node_bindings_to_published(+ session=session,+ draft_workflow=draft_workflow,+ published_workflow=published_workflow,+ )++ copied = session.add.call_args.args[0]+ assert copied.agent_id == "roster-agent"+ assert copied.current_snapshot_id == "active-snapshot"++@pytest.mark.parametrize("sqlite_session",[(App, Agent, WorkflowAgentNodeBinding)],indirect=True,)-def test_publish_binding_replacement_returns_only_previous_inline_agent(+def test_publish_binding_copy_keeps_previous_published_owner(sqlite_session: Session,) -> None:draft_workflow = _workflow()@@ -198,13 +273,15 @@ def test_publish_binding_replacement_returns_only_previous_inline_agent()sqlite_session.add_all([app, previous_inline_binding, previous_roster_binding, draft_binding])sqlite_session.commit()- retirement_candidates = WorkflowAgentPublishService.copy_agent_node_bindings_to_published(+ result = WorkflowAgentPublishService.copy_agent_node_bindings_to_published(session=sqlite_session,draft_workflow=draft_workflow,published_workflow=published_workflow,)- assert retirement_candidates == {"previous-inline-agent"}+ assert result is None+ assert sqlite_session.get(WorkflowAgentNodeBinding, previous_inline_binding.id) is previous_inline_binding+ assert sqlite_session.get(WorkflowAgentNodeBinding, previous_roster_binding.id) is previous_roster_bindingcopied = sqlite_session.scalar(select(WorkflowAgentNodeBinding).where(WorkflowAgentNodeBinding.workflow_id == "published-new",@@ -306,7 +383,6 @@ def test_resolve_existing_inline_binding_agent_returns_valid_agent_or_none(monkedef test_resolve_roster_binding_rejects_unpublished_agent() -> None:session = Mock()session.scalar.return_value = None-with pytest.raises(ValueError, match="unavailable or unpublished roster agent"):WorkflowAgentPublishService._resolve_roster_agent_graph_binding(session=session,diff --git a/api/tests/unit_tests/services/agent/test_workspace_service.py b/api/tests/unit_tests/services/agent/test_workspace_service.pyindex d678a92106d..8191f04cbfc 100644--- a/api/tests/unit_tests/services/agent/test_workspace_service.py+++ b/api/tests/unit_tests/services/agent/test_workspace_service.py@@ -15,7 +15,12 @@ from models.agent import (AgentWorkspaceBinding,AgentWorkspaceOwnerType,)-from services.agent.workspace_service import AgentWorkspaceNotFoundError, AgentWorkspaceService, WorkspaceOwnerScope+from services.agent.workspace_service import (+ AgentWorkspaceError,+ AgentWorkspaceNotFoundError,+ AgentWorkspaceService,+ WorkspaceOwnerScope,+)def _scope() -> WorkspaceOwnerScope:@@ -412,9 +417,11 @@ def test_collect_workspace_destroys_workspace_then_remaining_bindings(sqlite_session.add_all([workspace, anchor, remaining])sqlite_session.commit()client = MagicMock()+ commit = MagicMock(wraps=sqlite_session.commit)monkeypatch.setattr("services.agent.workspace_service.session_factory.create_session", lambda: nullcontext(sqlite_session))+ monkeypatch.setattr(sqlite_session, "commit", commit)monkeypatch.setattr(AgentWorkspaceService, "_client", lambda: nullcontext(client))AgentWorkspaceService.collect_retired_workspace(tenant_id="tenant-1", workspace_id=workspace.id)@@ -429,6 +436,73 @@ def test_collect_workspace_destroys_workspace_then_remaining_bindings(assert sqlite_session.get(AgentWorkspace, workspace.id) is Noneassert sqlite_session.get(AgentWorkspaceBinding, anchor.id) is Noneassert sqlite_session.get(AgentWorkspaceBinding, remaining.id) is None+ commit.assert_called_once()++[email protected]("sqlite_session", [(AgentWorkspace, AgentWorkspaceBinding)], indirect=True)+def test_collect_workspace_remaining_failure_preserves_ledgers_and_replay_converges(+ monkeypatch: pytest.MonkeyPatch, sqlite_session: Session+) -> None:+ workspace = _workspace(status=AgentWorkingResourceStatus.RETIRED)+ anchor = _binding(status=AgentWorkingResourceStatus.RETIRED)+ remaining = [+ _binding(+ binding_id=f"binding-{index}",+ agent_id=f"agent-{index}",+ status=AgentWorkingResourceStatus.RETIRED,+ )+ for index in (2, 3)+ ]+ anchor.created_at = datetime(2026, 7, 23, 10)+ for index, binding in enumerate(remaining, start=1):+ binding.created_at = anchor.created_at + timedelta(minutes=index)+ sqlite_session.add_all([workspace, anchor, *remaining])+ sqlite_session.commit()+ workspace_id = workspace.id+ binding_ids = [anchor.id, *(binding.id for binding in remaining)]+ error = RuntimeError("middle Binding destroy failed")+ client = MagicMock()+ client.destroy_execution_binding_sync.side_effect = [None, error, None]+ monkeypatch.setattr(+ "services.agent.workspace_service.session_factory.create_session", lambda: nullcontext(sqlite_session)+ )+ monkeypatch.setattr(AgentWorkspaceService, "_client", lambda: nullcontext(client))++ with pytest.raises(RuntimeError) as exc_info:+ AgentWorkspaceService.collect_retired_workspace(tenant_id="tenant-1", workspace_id=workspace_id)++ assert exc_info.value is error+ assert client.destroy_execution_binding_sync.call_count == 3+ first_attempt = [call.args[0] for call in client.destroy_execution_binding_sync.call_args_list]+ assert [request.destroy_workspace for request in first_attempt] == [True, False, False]+ assert sqlite_session.get(AgentWorkspace, workspace_id) is not None+ assert all(sqlite_session.get(AgentWorkspaceBinding, binding_id) is not None for binding_id in binding_ids)++ client.reset_mock()+ client.destroy_execution_binding_sync.side_effect = None+ AgentWorkspaceService.collect_retired_workspace(tenant_id="tenant-1", workspace_id=workspace_id)++ assert client.destroy_execution_binding_sync.call_count == 3+ assert sqlite_session.get(AgentWorkspace, workspace_id) is None+ assert all(sqlite_session.get(AgentWorkspaceBinding, binding_id) is None for binding_id in binding_ids)+++def test_collect_retired_workspace_without_retired_binding_raises(+ monkeypatch: pytest.MonkeyPatch, sqlite_session: Session+) -> None:+ workspace = _workspace(status=AgentWorkingResourceStatus.RETIRED)+ sqlite_session.add(workspace)+ sqlite_session.commit()+ monkeypatch.setattr(+ "services.agent.workspace_service.session_factory.create_session",+ lambda: nullcontext(sqlite_session),+ )++ with pytest.raises(AgentWorkspaceError, match="tenant_id=tenant-1, workspace_id=workspace-1"):+ AgentWorkspaceService.collect_retired_workspace(+ tenant_id="tenant-1",+ workspace_id=workspace.id,+ )def test_binding_collection_database_failure_propagates(monkeypatch: pytest.MonkeyPatch) -> None:diff --git a/api/tests/unit_tests/services/data_migration/test_import_service.py b/api/tests/unit_tests/services/data_migration/test_import_service.pyindex 02a897a8f20..a5a1fdfd172 100644--- a/api/tests/unit_tests/services/data_migration/test_import_service.py+++ b/api/tests/unit_tests/services/data_migration/test_import_service.py@@ -1,5 +1,8 @@+from contextlib import nullcontextfrom dataclasses import dataclass+from types import SimpleNamespacefrom typing import cast+from unittest.mock import Mockimport pytestimport yaml@@ -489,6 +492,30 @@ def test_workflow_tool_import_publishes_referenced_app_before_create(assert events == [("published", app_id), ("created", app_id)]+def test_ensure_workflow_app_is_published_updates_current_workflow(+ monkeypatch: pytest.MonkeyPatch,+ database: Database,+) -> None:+ _, account = _persist_tenant_account(database.session)+ app_id = "00000000-0000-0000-0000-000000000001"+ _persist_app(database.session, app_id=app_id)+ publish = Mock(return_value=SimpleNamespace(id="published-workflow"))+ monkeypatch.setattr(import_service, "WorkflowService", Mock(return_value=SimpleNamespace(publish_workflow=publish)))+ monkeypatch.setattr(+ import_service,+ "sessionmaker",+ lambda _engine: SimpleNamespace(begin=lambda: nullcontext(database.session)),+ )+ MigrationImportService()._ensure_workflow_app_is_published(+ ImportTarget("tenant-1", "target", "account-1", "[email protected]"),+ account,+ app_id,+ session=database.session,+ )++ assert database.session.get(App, app_id).workflow_id == "published-workflow"++@pytest.mark.parametrize("id_strategy", [IdStrategy.PRESERVE_ID, IdStrategy.GENERATE_NEW_ID])def test_workflow_tool_import_id_follows_id_strategy(monkeypatch: pytest.MonkeyPatch, database: Database, id_strategy: IdStrategydiff --git a/api/tests/unit_tests/services/rag_pipeline/test_rag_pipeline_service.py b/api/tests/unit_tests/services/rag_pipeline/test_rag_pipeline_service.pyindex 831bb49dc90..cea5bc86a02 100644--- a/api/tests/unit_tests/services/rag_pipeline/test_rag_pipeline_service.py+++ b/api/tests/unit_tests/services/rag_pipeline/test_rag_pipeline_service.py@@ -6,6 +6,7 @@ from types import SimpleNamespaceimport pytestfrom pytest_mock import MockerFixture+from sqlalchemy.dialects import postgresqlfrom sqlalchemy.orm import Session, sessionmakerfrom core.app.entities.app_invoke_entities import InvokeFrom@@ -69,6 +70,22 @@ class MockRepo:pass+def test_get_published_workflow_by_id_locks_restore_source(mocker: MockerFixture) -> None:+ session = mocker.Mock(spec=Session)+ workflow = _make_workflow()+ workflow.version = "v1"+ session.scalar.return_value = workflow+ service = RagPipelineService.__new__(RagPipelineService)+ service._session = session++ result = service.get_published_workflow_by_id(_make_pipeline(), workflow.id)++ stmt = session.scalar.call_args.args[0]+ sql = str(stmt.compile(dialect=postgresql.dialect()))+ assert result is workflow+ assert "FOR UPDATE" in sql++def _make_account(account_id: str = "u1", tenant_id: str = "t1") -> Account:account = Account(name="Test User", email=f"{account_id}@example.com")account.id = account_iddiff --git a/api/tests/unit_tests/services/test_app_dsl_service.py b/api/tests/unit_tests/services/test_app_dsl_service.pyindex 7afbb0fa042..afb681cdf44 100644--- a/api/tests/unit_tests/services/test_app_dsl_service.py+++ b/api/tests/unit_tests/services/test_app_dsl_service.py@@ -301,6 +301,59 @@ def test_create_or_update_app_flushes_new_model_config_before_signal(assert sqlite_session.in_transaction()+def test_create_or_update_app_forwards_imported_agent_purge_ids(monkeypatch: pytest.MonkeyPatch) -> None:+ session = cast(Session, SimpleNamespace(add=Mock(), flush=Mock(), commit=Mock(), get=Mock()))+ service = AppDslService(session=session)+ app = SimpleNamespace(+ id="app-1",+ tenant_id="tenant-1",+ name="Workflow",+ description="",+ icon_type=IconType.EMOJI,+ icon="robot",+ icon_background="#FFFFFF",+ )+ workflow = SimpleNamespace(id="workflow-1")+ workflow_service = SimpleNamespace(+ get_draft_workflow=Mock(return_value=None),+ sync_draft_workflow=Mock(return_value=workflow),+ )+ monkeypatch.setattr("services.app_dsl_service.WorkflowService", Mock(return_value=workflow_service))+ monkeypatch.setattr(+ "services.app_dsl_service.AgentDslService.graph_without_package_bindings",+ Mock(return_value={"nodes": [], "edges": []}),+ )+ monkeypatch.setattr(+ "services.app_dsl_service.AgentDslService.import_workflow_packages",+ Mock(return_value=(workflow, [], {"retired-agent"})),+ )+ monkeypatch.setattr(+ "services.app_dsl_service.WorkflowAgentPublishService.validate_agent_nodes_for_draft_sync",+ Mock(),+ )+ retire_unowned = Mock()+ monkeypatch.setattr(+ "services.app_dsl_service.WorkflowAgentRetirementService.retire_unowned",+ retire_unowned,+ )++ service._create_or_update_app(+ app=cast(App, app),+ data={+ "app": {"mode": AppMode.WORKFLOW.value},+ "workflow": {"graph": {"nodes": [], "edges": []}},+ "agent_packages": {"package-1": {}},+ },+ account=Mock(id="account-1"),+ )++ retire_unowned.assert_called_once_with(+ tenant_id="tenant-1",+ agent_ids={"retired-agent"},+ account_id="account-1",+ )++def test_export_dsl_loads_model_config_and_annotation_reply_with_request_session(monkeypatch: pytest.MonkeyPatch,sqlite_session_factory: sessionmaker[Session],diff --git a/api/tests/unit_tests/services/test_app_service.py b/api/tests/unit_tests/services/test_app_service.pyindex f33c3082e52..223be9f678b 100644--- a/api/tests/unit_tests/services/test_app_service.py+++ b/api/tests/unit_tests/services/test_app_service.py@@ -14,8 +14,17 @@ from enums import DeploymentEditionfrom graphon.model_runtime.entities.model_entities import ModelTypefrom models import Account, Tenantfrom models.account import TenantAccountJoin, TenantAccountRole-from models.agent import Agent, AgentIconType, AgentScope, AgentSource, AgentStatus+from models.agent import (+ Agent,+ AgentIconType,+ AgentScope,+ AgentSource,+ AgentStatus,+ WorkflowAgentBindingType,+ WorkflowAgentNodeBinding,+)from models.model import App, AppMode, AppModelConfig, IconType+from models.workflow import Workflow, WorkflowTypefrom services.agent.errors import AgentAccessNotReadyError, AgentNameConflictErrorfrom services.app_service import AppListParams, AppService, CreateAppParams@@ -615,6 +624,31 @@ class TestAgentAppType:def test_delete_agent_app_archives_backing_agent(self, sqlite_session: Session):app, backing_agent = _persist_agent_app(sqlite_session)+ workflow_app = _persist_app(sqlite_session, tenant_id=app.tenant_id, name="Workflow")+ workflow_app.mode = AppMode.WORKFLOW+ referencing_workflow = Workflow.new(+ tenant_id=app.tenant_id,+ app_id=workflow_app.id,+ type=WorkflowType.WORKFLOW.value,+ version=Workflow.VERSION_DRAFT,+ graph="{}",+ features="{}",+ created_by="account-1",+ environment_variables=[],+ conversation_variables=[],+ rag_pipeline_variables=[],+ )+ workflow_binding = WorkflowAgentNodeBinding(+ tenant_id=app.tenant_id,+ app_id=workflow_app.id,+ workflow_id=referencing_workflow.id,+ workflow_version=referencing_workflow.version,+ node_id="agent-node",+ binding_type=WorkflowAgentBindingType.ROSTER_AGENT,+ agent_id=backing_agent.id,+ current_snapshot_id="snapshot-1",+ node_job_config={},+ )workflow_agents = [Agent(tenant_id=app.tenant_id,@@ -630,7 +664,7 @@ class TestAgentAppType:)for index in range(2)]- sqlite_session.add_all(workflow_agents)+ sqlite_session.add_all([referencing_workflow, workflow_binding, *workflow_agents])sqlite_session.commit()account_id = str(uuid4())events: list[str] = []@@ -642,7 +676,10 @@ class TestAgentAppType:patch("services.app_service.BillingService"),patch("services.app_service.EnterpriseService"),patch("services.app_service.FeatureService"),- patch("services.app_service.remove_app_and_related_data_task"),+ patch(+ "services.app_service.remove_app_and_related_data_task.delay",+ side_effect=lambda **_kwargs: events.append("enqueue-app-cleanup"),+ ),patch("services.app_service.AgentHomeSnapshotService.retire_all_for_agent",return_value=["home-1"],@@ -653,9 +690,7 @@ class TestAgentAppType:) as mock_retire_workspaces,patch("services.app_service.WorkflowAgentRetirementService.retire_unowned",- side_effect=lambda **_kwargs: (- events.append("retire-workflow-agents") or (["workflow-binding-1"], ["workflow-home-1"])- ),+ side_effect=lambda **_kwargs: events.append("retire-workflow-agents"),) as mock_workflow_retirement,patch("services.app_service.enqueue_agent_resource_collection",@@ -664,7 +699,13 @@ class TestAgentAppType:):AppService().delete_app(app, session=sqlite_session)- assert events == ["retire-app-workspaces", "commit", "retire-workflow-agents", "enqueue"]+ assert events == [+ "retire-app-workspaces",+ "commit",+ "enqueue-app-cleanup",+ "retire-workflow-agents",+ "enqueue",+ ]sqlite_session.expire_all()persisted_agent = sqlite_session.get(Agent, backing_agent.id)assert persisted_agent is not None@@ -672,9 +713,12 @@ class TestAgentAppType:assert persisted_agent.status == AgentStatus.ARCHIVEDassert persisted_agent.archived_by == account_idassert persisted_agent.archived_at is not None+ persisted_workflow_binding = sqlite_session.get(WorkflowAgentNodeBinding, workflow_binding.id)+ assert persisted_workflow_binding is not None+ assert persisted_workflow_binding.agent_id == backing_agent.idmock_workflow_retirement.assert_called_once_with(tenant_id=app.tenant_id,- agent_ids=[agent.id for agent in workflow_agents],+ agent_ids={agent.id for agent in workflow_agents},account_id=account_id,)mock_retire_workspaces.assert_called_once_with(@@ -690,8 +734,9 @@ class TestAgentAppType:mock_enqueue_collection.assert_called_once_with(tenant_id=app.tenant_id,workspace_ids=["workspace-1"],- binding_ids=["workflow-binding-1"],- home_snapshot_ids=["home-1", "workflow-home-1"],+ binding_ids=[],+ home_snapshot_ids=["home-1"],+ purge_agent_ids=[backing_agent.id],)def test_delete_app_commit_failure_does_not_retire_workflow_agents_or_enqueue(self, sqlite_session: Session):@@ -722,9 +767,100 @@ class TestAgentAppType:patch("services.app_service.AgentWorkspaceService.retire_all_for_app", return_value=["workspace-1"]),patch("services.app_service.WorkflowAgentRetirementService.retire_unowned") as retire_unowned,patch("services.app_service.enqueue_agent_resource_collection") as enqueue_collection,+ patch("services.app_service.remove_app_and_related_data_task.delay") as enqueue_app_cleanup,):with pytest.raises(RuntimeError, match="commit failed"):AppService().delete_app(app, session=sqlite_session)retire_unowned.assert_not_called()enqueue_collection.assert_not_called()+ enqueue_app_cleanup.assert_not_called()++ def test_delete_workflow_app_releases_all_bindings_before_retirement(self, sqlite_session: Session):+ app = _persist_app(sqlite_session, tenant_id=str(uuid4()))+ app.mode = AppMode.WORKFLOW+ workflow = Workflow.new(+ tenant_id=app.tenant_id,+ app_id=app.id,+ type=WorkflowType.WORKFLOW.value,+ version="historical-version",+ graph="{}",+ features="{}",+ created_by="account-1",+ environment_variables=[],+ conversation_variables=[],+ rag_pipeline_variables=[],+ )+ inline_binding = WorkflowAgentNodeBinding(+ tenant_id=app.tenant_id,+ app_id=app.id,+ workflow_id=workflow.id,+ workflow_version=workflow.version,+ node_id="inline-node",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id="inline-agent",+ current_snapshot_id="snapshot-1",+ node_job_config={},+ )+ roster_binding = WorkflowAgentNodeBinding(+ tenant_id=app.tenant_id,+ app_id=app.id,+ workflow_id=workflow.id,+ workflow_version=workflow.version,+ node_id="roster-node",+ binding_type=WorkflowAgentBindingType.ROSTER_AGENT,+ agent_id="roster-agent",+ current_snapshot_id="snapshot-2",+ node_job_config={},+ )+ sqlite_session.add_all([workflow, inline_binding, roster_binding])+ sqlite_session.commit()+ events: list[str] = []++ def retire_unowned(**kwargs):+ events.append("retire")+ assert sqlite_session.get(WorkflowAgentNodeBinding, inline_binding.id) is None+ assert sqlite_session.get(WorkflowAgentNodeBinding, roster_binding.id) is None+ assert kwargs["agent_ids"] == {"inline-agent"}++ with (+ patch("services.app_service.current_user", _account_identity(str(uuid4()))),+ patch("services.app_service.app_was_deleted.send"),+ patch("services.app_service.FeatureService"),+ patch("services.app_service.BillingService"),+ patch("services.app_service.EnterpriseService"),+ patch("services.app_service.AgentWorkspaceService.retire_all_for_app", return_value=[]),+ patch(+ "services.app_service.remove_app_and_related_data_task.delay",+ side_effect=lambda **_kwargs: events.append("enqueue-app-cleanup"),+ ),+ patch(+ "services.app_service.WorkflowAgentRetirementService.retire_unowned",+ side_effect=retire_unowned,+ ),+ patch("services.app_service.enqueue_agent_resource_collection"),+ ):+ AppService().delete_app(app, session=sqlite_session)++ assert events == ["enqueue-app-cleanup", "retire"]+ assert sqlite_session.get(WorkflowAgentNodeBinding, inline_binding.id) is None+ assert sqlite_session.get(WorkflowAgentNodeBinding, roster_binding.id) is None++ def test_delete_app_cleanup_enqueue_failure_propagates_before_retirement(self, sqlite_session: Session):+ app = _persist_app(sqlite_session, tenant_id=str(uuid4()))+ error = RuntimeError("broker unavailable")++ with (+ patch("services.app_service.current_user", _account_identity(str(uuid4()))),+ patch("services.app_service.app_was_deleted.send"),+ patch("services.app_service.AgentWorkspaceService.retire_all_for_app", return_value=[]),+ patch("services.app_service.remove_app_and_related_data_task.delay", side_effect=error),+ patch("services.app_service.WorkflowAgentRetirementService.retire_unowned") as retire_unowned,+ patch("services.app_service.enqueue_agent_resource_collection") as enqueue_collection,+ ):+ with pytest.raises(RuntimeError) as exc_info:+ AppService().delete_app(app, session=sqlite_session)++ assert exc_info.value is error+ retire_unowned.assert_not_called()+ enqueue_collection.assert_not_called()diff --git a/api/tests/unit_tests/services/test_snippet_dsl_service.py b/api/tests/unit_tests/services/test_snippet_dsl_service.pyindex a9bbdc98495..b706da797da 100644--- a/api/tests/unit_tests/services/test_snippet_dsl_service.py+++ b/api/tests/unit_tests/services/test_snippet_dsl_service.py@@ -533,12 +533,17 @@ def test_create_or_update_snippet_updates_existing_snippet_and_syncs_workflow(momonkeypatch.setattr("services.snippet_dsl_service.SnippetService", lambda *_args, **_kwargs: snippet_service)monkeypatch.setattr("services.snippet_dsl_service.WorkflowAgentPublishService.sync_agent_bindings_for_draft",- Mock(return_value=set()),+ Mock(return_value={"retired-agent"}),)monkeypatch.setattr("services.snippet_dsl_service.WorkflowAgentPublishService.validate_agent_nodes_for_draft_sync",Mock(),)+ retire_unowned = Mock()+ monkeypatch.setattr(+ "services.snippet_dsl_service.WorkflowAgentRetirementService.retire_unowned",+ retire_unowned,+ )result = service._create_or_update_snippet(snippet=snippet,@@ -561,6 +566,11 @@ def test_create_or_update_snippet_updates_existing_snippet_and_syncs_workflow(moassert snippet.icon_info == {"icon": "x"}snippet_service.sync_draft_workflow.assert_called_once()session.commit.assert_called_once()+ retire_unowned.assert_called_once_with(+ tenant_id="tenant-1",+ agent_ids={"retired-agent"},+ account_id="account-1",+ )def test_create_or_update_snippet_creates_new_snippet_and_flushes(monkeypatch: pytest.MonkeyPatch):diff --git a/api/tests/unit_tests/services/test_snippet_service.py b/api/tests/unit_tests/services/test_snippet_service.pyindex b883d7eeb6f..caac531bdbc 100644--- a/api/tests/unit_tests/services/test_snippet_service.py+++ b/api/tests/unit_tests/services/test_snippet_service.py@@ -6,7 +6,7 @@ from types import SimpleNamespacefrom unittest.mock import Mockimport pytest-from sqlalchemy import select+from sqlalchemy import event, selectfrom sqlalchemy.orm import Session, sessionmakerfrom enums import DeploymentEdition@@ -14,9 +14,16 @@ from extensions.storage.storage_type import StorageTypefrom graphon.variables.segments import StringSegmentfrom graphon.variables.types import SegmentTypefrom models.account import Account-from models.agent import Agent, AgentScope, AgentSource, AgentStatus-from models.enums import CreatorUserRole-from models.model import UploadFile+from models.agent import (+ Agent,+ AgentScope,+ AgentSource,+ AgentStatus,+ WorkflowAgentBindingType,+ WorkflowAgentNodeBinding,+)+from models.enums import AppStatus, CreatorUserRole+from models.model import App, AppMode, UploadFilefrom models.snippet import CustomizedSnippet, SnippetTypefrom models.workflow import (Workflow,@@ -219,6 +226,15 @@ def test_sync_draft_workflow_creates_draft_and_updates_input_fields() -> None:service = SnippetService(session_maker=sqlite_session_factory)monkeypatch.setattr(service, "get_draft_workflow", Mock(return_value=None))+ monkeypatch.setattr(+ "services.agent.workflow_publish_service.WorkflowAgentPublishService.sync_agent_bindings_for_draft",+ Mock(return_value={"retired-agent"}),+ )+ retire_unowned = Mock()+ monkeypatch.setattr(+ "services.snippet_service.WorkflowAgentRetirementService.retire_unowned",+ retire_unowned,+ )snippet = _snippet()account = _account()@@ -239,6 +255,11 @@ def test_sync_draft_workflow_creates_draft_and_updates_input_fields(assert stored_workflow is not Noneassert stored_snippet is not Noneassert stored_snippet.input_fields_list == [{"variable": "query"}]+ retire_unowned.assert_called_once_with(+ tenant_id=snippet.tenant_id,+ agent_ids={"retired-agent"},+ account_id=account.id,+ )def test_sync_draft_workflow_raises_when_hash_mismatches(@@ -467,6 +488,15 @@ def test_restore_published_snippet_workflow_to_draft_copies_source_snapshot(monkeypatch.setattr(service, "get_published_workflow_by_id", Mock(return_value=source_workflow))monkeypatch.setattr(service, "get_draft_workflow", Mock(return_value=draft_workflow))+ monkeypatch.setattr(+ "services.agent.workflow_publish_service.WorkflowAgentPublishService.restore_agent_node_bindings_to_draft",+ Mock(return_value={"retired-agent"}),+ )+ retire_unowned = Mock()+ monkeypatch.setattr(+ "services.snippet_service.WorkflowAgentRetirementService.retire_unowned",+ retire_unowned,+ )result = service.restore_published_workflow_to_draft(snippet=snippet,@@ -482,6 +512,11 @@ def test_restore_published_snippet_workflow_to_draft_copies_source_snapshot(stored = sqlite_session.get(Workflow, draft_workflow.id)assert stored is not Noneassert stored.graph_dict == source_graph+ retire_unowned.assert_called_once_with(+ tenant_id=snippet.tenant_id,+ agent_ids={"retired-agent"},+ account_id=account.id,+ )def test_restore_published_snippet_workflow_to_draft_raises_when_source_missing(@@ -607,7 +642,7 @@ def test_publish_workflow_creates_snapshot_and_updates_snippet(Mock(return_value=set()),)- result, retirement_candidates = service.publish_workflow(+ result = service.publish_workflow(session=sqlite_session,snippet=snippet,account=_account(),@@ -621,7 +656,6 @@ def test_publish_workflow_creates_snapshot_and_updates_snippet(sqlite_session.flush()assert sqlite_session.get(Workflow, result.id) is resultassert sqlite_session.get(CustomizedSnippet, snippet.id).workflow_id == result.id- assert retirement_candidates == set()def test_get_all_published_workflows_returns_empty_without_current_workflow(unbound_session: Session) -> None:@@ -681,11 +715,12 @@ def test_delete_snippet_removes_related_records(assert observer.get(Workflow, workflow.id) is None-def test_delete_snippet_archives_owned_agents_and_schedules_backing_app_cleanup(+def test_delete_snippet_releases_last_owner_and_retries_archived_agent_cleanup_after_commit(monkeypatch: pytest.MonkeyPatch,sqlite_session: Session,) -> None:snippet = _snippet()+ archived_at = datetime(2025, 1, 1)agent = Agent(id="agent-1",tenant_id=snippet.tenant_id,@@ -696,13 +731,57 @@ def test_delete_snippet_archives_owned_agents_and_schedules_backing_app_cleanup(source=AgentSource.WORKFLOW,app_id=snippet.id,backing_app_id="backing-app-1",- status=AgentStatus.ACTIVE,- updated_by="creator-1",+ workflow_id="workflow-1",+ workflow_node_id="agent-node",+ status=AgentStatus.ARCHIVED,+ archived_by="original-account",+ archived_at=archived_at,+ updated_by="original-account",+ )+ hidden_app = App(+ id="backing-app-1",+ tenant_id=snippet.tenant_id,+ name="Snippet Agent runtime",+ mode=AppMode.AGENT,+ status=AppStatus.NORMAL,+ enable_site=False,+ enable_api=False,+ )+ workflow = _create_workflow(+ workflow_id="workflow-1",+ version=Workflow.VERSION_DRAFT,+ graph={"nodes": []},+ features={},)- sqlite_session.add_all([snippet, agent])+ owner_binding = WorkflowAgentNodeBinding(+ id="snippet-inline-binding",+ tenant_id=snippet.tenant_id,+ app_id=snippet.id,+ workflow_id=workflow.id,+ workflow_version=workflow.version,+ node_id="agent-node",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id=agent.id,+ current_snapshot_id="snapshot-1",+ node_job_config={},+ )+ sqlite_session.add_all([snippet, agent, hidden_app, workflow, owner_binding])sqlite_session.flush()- cleanup_delay = Mock()- monkeypatch.setattr("tasks.remove_app_and_related_data_task.remove_app_and_related_data_task.delay", cleanup_delay)+ hidden_app_id = hidden_app.id+ workflow_id = workflow.id+ owner_binding_id = owner_binding.id+ events: list[str] = []+ event.listen(sqlite_session, "after_commit", lambda _session: events.append("commit"), once=True)+ cleanup_delay = Mock(side_effect=lambda **_kwargs: events.append("cleanup-hidden-app"))+ enqueue_collection = Mock(side_effect=lambda **_kwargs: events.append("enqueue-agent-purge"))+ monkeypatch.setattr(+ "services.agent.retirement_service.remove_app_and_related_data_task.delay",+ cleanup_delay,+ )+ monkeypatch.setattr(+ "services.agent.retirement_service.enqueue_agent_resource_collection",+ enqueue_collection,+ )result = SnippetService.delete_snippet(session=sqlite_session,@@ -711,13 +790,100 @@ def test_delete_snippet_archives_owned_agents_and_schedules_backing_app_cleanup()assert result is True- assert agent.status == "archived"- assert agent.archived_by == "account-1"- assert agent.archived_at is not None- assert agent.updated_by == "account-1"+ assert agent.status == AgentStatus.ARCHIVED+ assert (+ sqlite_session.scalar(select(WorkflowAgentNodeBinding).where(WorkflowAgentNodeBinding.id == owner_binding.id))+ is None+ )+ cleanup_delay.assert_not_called()+ enqueue_collection.assert_not_called()sqlite_session.commit()- assert sqlite_session.get(Agent, agent.id).status == AgentStatus.ARCHIVED+ sqlite_session.expire_all()+ stored_agent = sqlite_session.get(Agent, agent.id)+ assert stored_agent is not None+ assert stored_agent.status == AgentStatus.ARCHIVED+ assert stored_agent.archived_by == "original-account"+ assert stored_agent.archived_at == archived_at+ assert stored_agent.updated_by == "original-account"+ assert sqlite_session.get(App, hidden_app_id) is None+ assert sqlite_session.get(Workflow, workflow_id) is None+ assert sqlite_session.get(WorkflowAgentNodeBinding, owner_binding_id) is None+ assert events == ["commit", "cleanup-hidden-app", "enqueue-agent-purge"]cleanup_delay.assert_called_once_with(tenant_id=snippet.tenant_id, app_id="backing-app-1")+ enqueue_collection.assert_called_once_with(+ tenant_id=snippet.tenant_id,+ workspace_ids=[],+ binding_ids=[],+ home_snapshot_ids=[],+ purge_agent_ids=[agent.id],+ )+++def test_delete_snippet_keeps_agent_with_persisted_external_owner(+ monkeypatch: pytest.MonkeyPatch,+ sqlite_session: Session,+) -> None:+ snippet = _snippet()+ agent = Agent(+ id="agent-1",+ tenant_id=snippet.tenant_id,+ name="Shared workflow Agent",+ description="",+ role="",+ scope=AgentScope.WORKFLOW_ONLY,+ source=AgentSource.WORKFLOW,+ app_id=snippet.id,+ backing_app_id="backing-app-1",+ status=AgentStatus.ACTIVE,+ )+ workflow_app = App(+ id="app-1",+ tenant_id=snippet.tenant_id,+ name="Workflow",+ mode=AppMode.WORKFLOW,+ status=AppStatus.NORMAL,+ enable_site=True,+ enable_api=True,+ )+ workflow = Workflow(+ id="workflow-1",+ tenant_id=snippet.tenant_id,+ app_id=workflow_app.id,+ type=WorkflowType.WORKFLOW,+ version=Workflow.VERSION_DRAFT,+ graph="{}",+ features="{}",+ created_by="account-1",+ environment_variables=[],+ conversation_variables=[],+ rag_pipeline_variables=[],+ )+ external_binding = WorkflowAgentNodeBinding(+ tenant_id=snippet.tenant_id,+ app_id=workflow_app.id,+ workflow_id=workflow.id,+ workflow_version=workflow.version,+ node_id="agent-node",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id=agent.id,+ current_snapshot_id="snapshot-1",+ node_job_config={},+ )+ sqlite_session.add_all([snippet, agent, workflow_app, workflow, external_binding])+ sqlite_session.flush()+ cleanup_delay = Mock()+ monkeypatch.setattr(+ "services.agent.retirement_service.remove_app_and_related_data_task.delay",+ cleanup_delay,+ )++ SnippetService.delete_snippet(session=sqlite_session, snippet=snippet, account_id="account-1")+ sqlite_session.commit()+ sqlite_session.expire_all()++ assert sqlite_session.get(Agent, agent.id).status == AgentStatus.ACTIVE # type: ignore[union-attr]+ assert sqlite_session.get(WorkflowAgentNodeBinding, external_binding.id) is not None+ cleanup_delay.assert_not_called()def test_delete_draft_variable_files_removes_storage_objects(diff --git a/api/tests/unit_tests/services/test_workflow_service.py b/api/tests/unit_tests/services/test_workflow_service.pyindex 4c6e223719f..2fff05a4ed9 100644--- a/api/tests/unit_tests/services/test_workflow_service.py+++ b/api/tests/unit_tests/services/test_workflow_service.py@@ -39,10 +39,22 @@ from graphon.variables import StringVariablefrom graphon.variables.input_entities import VariableEntityTypefrom libs.datetime_utils import naive_utc_nowfrom models.account import Account+from models.agent import (+ Agent,+ AgentConfigSnapshot,+ AgentKind,+ AgentScope,+ AgentSource,+ AgentStatus,+ WorkflowAgentBindingType,+ WorkflowAgentNodeBinding,+)+from models.agent_config_entities import AgentSoulConfigfrom models.human_input import HumanInputFormRecipient, RecipientTypefrom models.model import App, AppModefrom models.tools import BuiltinToolProvider, WorkflowToolProviderfrom models.workflow import Workflow, WorkflowType+from services.agent.retirement_service import WorkflowAgentRetirementServicefrom services.errors.app import IsDraftWorkflowError, TriggerNodeLimitExceededError, WorkflowHashNotEqualErrorfrom services.errors.workflow_service import DraftWorkflowDeletionError, WorkflowInUseErrorfrom services.workflow_ref_service import WorkflowRef@@ -326,6 +338,24 @@ class TestWorkflowService:assert result is workflow+ def test_get_published_workflow_by_id_can_lock_restore_source(self, workflow_service: WorkflowService):+ app = TestWorkflowAssociatedDataFactory.create_app()+ workflow = TestWorkflowAssociatedDataFactory.create_workflow(version="v1")+ session = MagicMock(spec=Session)+ session.scalar.return_value = workflow++ result = workflow_service.get_published_workflow_by_id(+ app,+ workflow.id,+ session=session,+ for_update=True,+ )++ stmt = session.scalar.call_args.args[0]+ sql = str(stmt.compile(dialect=postgresql.dialect()))+ assert result is workflow+ assert "FOR UPDATE" in sql+def test_get_published_workflow_by_id_raises_error_for_draft(self, workflow_service: WorkflowService, sqlite_session: Session):@@ -419,7 +449,14 @@ class TestWorkflowService:graph = TestWorkflowAssociatedDataFactory.create_valid_workflow_graph()features = {"file_upload": {"enabled": False}}- with patch("services.workflow_service.app_draft_workflow_was_synced"):+ with (+ patch("services.workflow_service.app_draft_workflow_was_synced"),+ patch(+ "services.agent.workflow_publish_service.WorkflowAgentPublishService.sync_agent_bindings_for_draft",+ return_value={"retired-agent"},+ ),+ patch("services.workflow_service.WorkflowAgentRetirementService.retire_unowned") as retire_unowned,+ ):result = workflow_service.sync_draft_workflow(app_model=app,graph=graph,@@ -435,6 +472,11 @@ class TestWorkflowService:assert persisted_workflow is resultassert result.graph_dict == graphassert result.features_dict == features+ retire_unowned.assert_called_once_with(+ tenant_id=app.tenant_id,+ agent_ids={"retired-agent"},+ account_id=account.id,+ )def test_sync_draft_workflow_updates_existing_draft(self, workflow_service: WorkflowService, sqlite_session: Session@@ -759,7 +801,19 @@ class TestWorkflowService:sqlite_session.add_all([source_workflow, draft_workflow])sqlite_session.commit()- with patch("services.workflow_service.app_draft_workflow_was_synced"):+ with (+ patch("services.workflow_service.app_draft_workflow_was_synced"),+ patch.object(+ workflow_service,+ "get_published_workflow_by_id",+ wraps=workflow_service.get_published_workflow_by_id,+ ) as get_published_workflow_by_id,+ patch(+ "services.agent.workflow_publish_service.WorkflowAgentPublishService.restore_agent_node_bindings_to_draft",+ return_value={"retired-agent"},+ ),+ patch("services.workflow_service.WorkflowAgentRetirementService.retire_unowned") as retire_unowned,+ ):result = workflow_service.restore_published_workflow_to_draft(app_model=app,workflow_id=source_workflow.id,@@ -772,6 +826,138 @@ class TestWorkflowService:assert draft_workflow.serialized_features == json.dumps(legacy_features)sqlite_session.refresh(draft_workflow)assert draft_workflow.serialized_features == json.dumps(legacy_features)+ get_published_workflow_by_id.assert_called_once_with(+ app_model=app,+ workflow_id=source_workflow.id,+ session=sqlite_session,+ for_update=True,+ )+ retire_unowned.assert_called_once_with(+ tenant_id=app.tenant_id,+ agent_ids={"retired-agent"},+ account_id=account.id,+ )++ def test_restore_historical_inline_agent_after_current_pointer_moves_uses_real_clone(+ self,+ workflow_service: WorkflowService,+ sqlite_session: Session,+ ) -> None:+ app = TestWorkflowAssociatedDataFactory.create_app(workflow_id=None)+ account = TestWorkflowAssociatedDataFactory.create_account()+ graph = {+ "nodes": [+ {+ "id": "agent-node",+ "data": {+ "type": "agent",+ "version": "2",+ "agent_node_kind": "dify_agent",+ },+ }+ ],+ "edges": [],+ }+ historical = TestWorkflowAssociatedDataFactory.create_workflow(+ workflow_id="historical-workflow",+ version="historical-version",+ graph=graph,+ )+ current = TestWorkflowAssociatedDataFactory.create_workflow(+ workflow_id="current-workflow",+ version="current-version",+ graph=graph,+ )+ draft = TestWorkflowAssociatedDataFactory.create_workflow(+ workflow_id="draft-workflow",+ version=Workflow.VERSION_DRAFT,+ )+ source_agent = Agent(+ id="historical-agent",+ tenant_id=app.tenant_id,+ name="Historical inline Agent",+ description="",+ role="",+ agent_kind=AgentKind.DIFY_AGENT,+ scope=AgentScope.WORKFLOW_ONLY,+ source=AgentSource.WORKFLOW,+ app_id=app.id,+ workflow_id=historical.id,+ workflow_node_id="agent-node",+ active_config_snapshot_id="historical-snapshot",+ active_config_has_model=False,+ active_config_is_published=True,+ status=AgentStatus.ACTIVE,+ created_by=account.id,+ updated_by=account.id,+ )+ source_snapshot = AgentConfigSnapshot(+ id="historical-snapshot",+ tenant_id=app.tenant_id,+ agent_id=source_agent.id,+ version=1,+ config_snapshot=AgentSoulConfig(config_note="historical soul"),+ created_by=account.id,+ )+ historical_binding = WorkflowAgentNodeBinding(+ id="historical-binding",+ tenant_id=app.tenant_id,+ app_id=app.id,+ workflow_id=historical.id,+ workflow_version=historical.version,+ node_id="agent-node",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id=source_agent.id,+ current_snapshot_id=source_snapshot.id,+ node_job_config={},+ created_by=account.id,+ )+ sqlite_session.add_all([app, historical, current, draft, source_agent, source_snapshot, historical_binding])+ app.workflow_id = historical.id+ sqlite_session.commit()++ app.workflow_id = current.id+ sqlite_session.commit()++ WorkflowAgentRetirementService.retire_unowned(+ tenant_id=app.tenant_id,+ agent_ids=[source_agent.id],+ account_id=account.id,+ )+ sqlite_session.expire_all()+ retained_agent = sqlite_session.get(Agent, source_agent.id)+ assert retained_agent is not None+ assert retained_agent.status is AgentStatus.ACTIVE++ with patch("services.workflow_service.app_draft_workflow_was_synced"):+ restored_draft = workflow_service.restore_published_workflow_to_draft(+ app_model=app,+ workflow_id=historical.id,+ account=account,+ session=sqlite_session,+ )++ restored_binding = sqlite_session.scalar(+ select(WorkflowAgentNodeBinding).where(+ WorkflowAgentNodeBinding.workflow_id == draft.id,+ WorkflowAgentNodeBinding.workflow_version == Workflow.VERSION_DRAFT,+ WorkflowAgentNodeBinding.node_id == "agent-node",+ )+ )+ assert restored_draft is draft+ assert app.workflow_id == current.id+ assert sqlite_session.get(Workflow, historical.id) is historical+ assert sqlite_session.get(WorkflowAgentNodeBinding, historical_binding.id) is historical_binding+ assert restored_binding is not None+ assert restored_binding.agent_id not in (None, source_agent.id)+ assert restored_binding.current_snapshot_id not in (None, source_snapshot.id)+ restored_agent = sqlite_session.get(Agent, restored_binding.agent_id)+ restored_snapshot = sqlite_session.get(AgentConfigSnapshot, restored_binding.current_snapshot_id)+ assert restored_agent is not None+ assert restored_agent.workflow_id == draft.id+ assert restored_agent.workflow_node_id == "agent-node"+ assert restored_snapshot is not None+ assert restored_snapshot.config_snapshot_dict == source_snapshot.config_snapshot_dict# ==================== Workflow Validation Tests ====================# These tests verify graph structure and feature configuration validation@@ -1068,7 +1254,7 @@ class TestWorkflowService:patch("services.workflow_service.app_published_workflow_was_updated"),patch("services.workflow_service.dify_config.DEPLOYMENT_EDITION", DeploymentEdition.COMMUNITY),):- result, retirement_candidates = workflow_service.publish_workflow(+ result = workflow_service.publish_workflow(session=sqlite_session,app_model=app,account=account,@@ -1081,7 +1267,6 @@ class TestWorkflowService:assert result.version != Workflow.VERSION_DRAFTassert result.marked_name == "Version 1"assert result.marked_comment == "Initial release"- assert retirement_candidates == set()def test_publish_workflow_numbers_versions_from_one(self, workflow_service: WorkflowService, sqlite_session: Session@@ -1107,8 +1292,8 @@ class TestWorkflowService:DeploymentEdition.COMMUNITY,),):- first, _ = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)- second, _ = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)+ first = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)+ second = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)assert first.version_number == 1assert second.version_number == 2@@ -1137,12 +1322,12 @@ class TestWorkflowService:DeploymentEdition.COMMUNITY,),):- published, _ = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)+ published = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)sqlite_session.flush()sqlite_session.delete(published)sqlite_session.flush()- republished, _ = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)+ republished = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)assert republished.version_number == 2@@ -1176,7 +1361,7 @@ class TestWorkflowService:DeploymentEdition.COMMUNITY,),):- workflow, _ = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)+ workflow = workflow_service.publish_workflow(session=sqlite_session, app_model=app, account=account)published.append(workflow)assert [workflow.version_number for workflow in published] == [1, 1]@@ -1467,14 +1652,76 @@ class TestWorkflowService:workflow = TestWorkflowAssociatedDataFactory.create_workflow(workflow_id=workflow_id, tenant_id=tenant_id, app_id=app_id, version="v1")- sqlite_session.add(workflow)+ inline_binding = WorkflowAgentNodeBinding(+ id="inline-binding",+ tenant_id=tenant_id,+ app_id=app_id,+ workflow_id=workflow_id,+ workflow_version="v1",+ node_id="inline-node",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id="inline-agent",+ current_snapshot_id="snapshot-1",+ node_job_config={},+ )+ roster_binding = WorkflowAgentNodeBinding(+ id="roster-binding",+ tenant_id=tenant_id,+ app_id=app_id,+ workflow_id=workflow_id,+ workflow_version="v1",+ node_id="roster-node",+ binding_type=WorkflowAgentBindingType.ROSTER_AGENT,+ agent_id="roster-agent",+ current_snapshot_id="snapshot-2",+ node_job_config={},+ )+ non_target_bindings = [+ WorkflowAgentNodeBinding(+ id=f"non-target-{key}",+ tenant_id="other-tenant" if key == "tenant" else tenant_id,+ app_id="other-app" if key == "app" else app_id,+ workflow_id="other-workflow" if key == "workflow" else workflow_id,+ workflow_version="other-version" if key == "version" else workflow.version,+ node_id=f"{key}-node",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id=f"{key}-inline-agent",+ current_snapshot_id=f"{key}-snapshot",+ node_job_config={},+ )+ for key in ("tenant", "app", "workflow", "version")+ ]+ sqlite_session.add_all([workflow, inline_binding, roster_binding, *non_target_bindings])sqlite_session.commit()result = workflow_service.delete_workflow(session=sqlite_session, workflow_ref=workflow_ref)sqlite_session.flush()- assert result is True+ assert result == ["inline-agent"]assert sqlite_session.get(Workflow, workflow_id) is None+ assert sqlite_session.get(WorkflowAgentNodeBinding, inline_binding.id) is None+ assert sqlite_session.get(WorkflowAgentNodeBinding, roster_binding.id) is None+ for binding in non_target_bindings:+ assert sqlite_session.get(WorkflowAgentNodeBinding, binding.id) is binding++ def test_delete_workflow_locks_source_until_caller_commits(self, workflow_service: WorkflowService):+ workflow = TestWorkflowAssociatedDataFactory.create_workflow(version="v1")+ workflow_ref = WorkflowRef(+ tenant_id=workflow.tenant_id,+ owner_id=workflow.app_id,+ workflow_id=workflow.id,+ )+ session = MagicMock(spec=Session)+ session.scalar.side_effect = [workflow, None, None]+ session.scalars.return_value.all.return_value = []++ result = workflow_service.delete_workflow(session=session, workflow_ref=workflow_ref)++ stmt = session.scalar.call_args_list[0].args[0]+ sql = str(stmt.compile(dialect=postgresql.dialect()))+ assert result == []+ assert "FOR UPDATE" in sql+ session.delete.assert_called_once_with(workflow)def test_delete_workflow_with_ref_scopes_lookup_to_app(self, workflow_service: WorkflowService, sqlite_session: Sessiondiff --git a/api/tests/unit_tests/tasks/test_collect_agent_resources_task.py b/api/tests/unit_tests/tasks/test_collect_agent_resources_task.pyindex eb751061f17..d660ec489fe 100644--- a/api/tests/unit_tests/tasks/test_collect_agent_resources_task.py+++ b/api/tests/unit_tests/tasks/test_collect_agent_resources_task.py@@ -1,8 +1,9 @@from typing import Protocol, cast-from unittest.mock import MagicMock+from unittest.mock import MagicMock, callimport pytest+from services.agent.deletion_service import AgentDeletionServicefrom services.agent.home_snapshot_service import AgentHomeSnapshotServicefrom services.agent.workspace_service import AgentWorkspaceServicefrom tasks.collect_agent_resources_task import (@@ -29,6 +30,7 @@ def test_enqueue_deduplicates_ids_and_skips_empty_input(monkeypatch: pytest.Monktenant_id="tenant-1",binding_ids=["binding-2", "binding-1", "binding-2"],workspace_ids=["workspace-1"],+ purge_agent_ids=["agent-2", "", "agent-1", "agent-2"],)delay.assert_called_once_with(@@ -36,6 +38,7 @@ def test_enqueue_deduplicates_ids_and_skips_empty_input(monkeypatch: pytest.Monkbinding_ids=["binding-1", "binding-2"],workspace_ids=["workspace-1"],home_snapshot_ids=[],+ purge_agent_ids=["agent-1", "agent-2"],)@@ -56,67 +59,126 @@ def test_collection_runs_in_workspace_binding_snapshot_order(monkeypatch: pytest"collect_retired_home_snapshot",lambda **_kwargs: calls.append("home"),)+ purge = MagicMock(side_effect=lambda **_kwargs: calls.append("purge"))+ monkeypatch.setattr(AgentDeletionService, "purge_archived_agents", purge)collect_agent_resources.run(tenant_id="tenant-1",workspace_ids=["workspace-1"],binding_ids=["binding-1"],home_snapshot_ids=["home-1"],+ purge_agent_ids=["agent-1"],)- assert calls == ["workspace", "binding", "home"]+ assert calls == ["workspace", "binding", "home", "purge"]+ purge.assert_called_once_with(tenant_id="tenant-1", agent_ids=["agent-1"])-def test_collection_failure_propagates_and_stops_task(monkeypatch: pytest.MonkeyPatch) -> None:+def test_collection_failure_propagates_after_attempting_remaining_resources(monkeypatch: pytest.MonkeyPatch) -> None:calls: list[str] = []- error = RuntimeError("workspace failed")+ first_error = RuntimeError("workspace-1 failed")+ errors = {+ "workspace-1": first_error,+ "workspace-2": RuntimeError("workspace-2 failed"),+ "binding-1": RuntimeError("binding-1 failed"),+ "home-2": RuntimeError("home-2 failed"),+ }log_exception = MagicMock()- def collect_workspace(**_kwargs: object) -> None:- calls.append("workspace")- raise error+ def collect_workspace(*, workspace_id: str, **_kwargs: object) -> None:+ calls.append(f"workspace:{workspace_id}")+ if error := errors.get(workspace_id):+ raise error++ def collect_binding(*, binding_id: str, **_kwargs: object) -> None:+ calls.append(f"binding:{binding_id}")+ if error := errors.get(binding_id):+ raise error++ def collect_home(*, home_snapshot_id: str, **_kwargs: object) -> None:+ calls.append(f"home:{home_snapshot_id}")+ if error := errors.get(home_snapshot_id):+ raise errormonkeypatch.setattr(AgentWorkspaceService, "collect_retired_workspace", collect_workspace)- monkeypatch.setattr(- AgentWorkspaceService,- "collect_retired_binding",- lambda **_kwargs: calls.append("binding"),+ monkeypatch.setattr(AgentWorkspaceService, "collect_retired_binding", collect_binding)+ monkeypatch.setattr(AgentHomeSnapshotService, "collect_retired_home_snapshot", collect_home)+ monkeypatch.setattr("tasks.collect_agent_resources_task.logger.exception", log_exception)+ purge = MagicMock()+ monkeypatch.setattr(AgentDeletionService, "purge_archived_agents", purge)++ with pytest.raises(RuntimeError) as exc_info:+ collect_agent_resources.run(+ tenant_id="tenant-1",+ workspace_ids=["workspace-1", "workspace-2", "workspace-3"],+ binding_ids=["binding-1", "binding-2"],+ home_snapshot_ids=["home-1", "home-2", "home-3"],+ purge_agent_ids=["agent-1"],+ )++ assert exc_info.value.__cause__ is first_error+ assert str(exc_info.value) == (+ "Failed to collect 4 retired Agent resource(s): "+ "workspace:workspace-1, workspace:workspace-2, binding:binding-1, home_snapshot:home-2")- monkeypatch.setattr(- AgentHomeSnapshotService,- "collect_retired_home_snapshot",- lambda **_kwargs: calls.append("home"),+ assert calls == [+ "workspace:workspace-1",+ "workspace:workspace-2",+ "workspace:workspace-3",+ "binding:binding-1",+ "binding:binding-2",+ "home:home-1",+ "home:home-2",+ "home:home-3",+ ]+ purge.assert_not_called()+ log_exception.assert_has_calls(+ [+ call(+ "Failed to collect retired Agent resource",+ extra={+ "tenant_id": "tenant-1",+ "resource_type": resource_type,+ "resource_id": resource_id,+ },+ )+ for resource_type, resource_id in (+ ("workspace", "workspace-1"),+ ("workspace", "workspace-2"),+ ("binding", "binding-1"),+ ("home_snapshot", "home-2"),+ )+ ],+ any_order=False,)+ assert log_exception.call_count == 4+++def test_enqueue_failure_propagates(monkeypatch: pytest.MonkeyPatch) -> None:+ error = RuntimeError("queue unavailable")+ delay = MagicMock(side_effect=error)+ log_exception = MagicMock()+ monkeypatch.setattr(collect_agent_resources, "delay", delay)monkeypatch.setattr("tasks.collect_agent_resources_task.logger.exception", log_exception)with pytest.raises(RuntimeError) as exc_info:- collect_agent_resources.run(+ enqueue_agent_resource_collection(tenant_id="tenant-1",- workspace_ids=["workspace-1"],binding_ids=["binding-1"],+ workspace_ids=["workspace-1"],home_snapshot_ids=["home-1"],+ purge_agent_ids=["agent-2", "agent-1", "agent-2"],)assert exc_info.value is error- assert calls == ["workspace"]+ payload = {+ "binding_ids": ["binding-1"],+ "workspace_ids": ["workspace-1"],+ "home_snapshot_ids": ["home-1"],+ "purge_agent_ids": ["agent-1", "agent-2"],+ }+ delay.assert_called_once_with(tenant_id="tenant-1", **payload)log_exception.assert_called_once_with(- "Failed to collect retired Agent resource",- extra={- "tenant_id": "tenant-1",- "resource_type": "workspace",- "resource_id": "workspace-1",- },- )---def test_enqueue_failure_is_best_effort(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(- collect_agent_resources,- "delay",- MagicMock(side_effect=RuntimeError("queue unavailable")),- )-- enqueue_agent_resource_collection(- tenant_id="tenant-1",- binding_ids=["binding-1"],+ "Failed to enqueue retired Agent resource collection",+ extra={"tenant_id": "tenant-1", **payload},)diff --git a/api/tests/unit_tests/tasks/test_remove_app_and_related_data_task.py b/api/tests/unit_tests/tasks/test_remove_app_and_related_data_task.pyindex 3f0ce2475ef..125e447a10e 100644--- a/api/tests/unit_tests/tasks/test_remove_app_and_related_data_task.py+++ b/api/tests/unit_tests/tasks/test_remove_app_and_related_data_task.py@@ -6,9 +6,12 @@ from uuid import uuid4import pytestfrom sqlalchemy.orm import Session+import tasks.remove_app_and_related_data_task as remove_app_task_module+from enums import DeploymentEditionfrom graphon.enums import WorkflowExecutionStatusfrom libs.archive_storage import ArchiveStorageNotConfiguredErrorfrom models import AppStar+from models.agent import WorkflowAgentBindingType, WorkflowAgentNodeBindingfrom models.enums import CreatorUserRole, WorkflowRunTriggeredFromfrom models.workflow import WorkflowArchiveLogfrom tasks.remove_app_and_related_data_task import (@@ -17,10 +20,104 @@ from tasks.remove_app_and_related_data_task import (_delete_archived_workflow_run_files,_delete_draft_variable_offload_data,_delete_draft_variables,+ _delete_workflow_agent_node_bindings,delete_draft_variables_batch,)+def test_delete_workflow_agent_node_bindings_is_scoped_to_tenant_and_app(sqlite_session: Session) -> None:+ target = WorkflowAgentNodeBinding(+ tenant_id="tenant-1",+ app_id="app-1",+ workflow_id="workflow-1",+ workflow_version="draft",+ node_id="node-1",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id="agent-1",+ current_snapshot_id="snapshot-1",+ node_job_config={},+ )+ kept = WorkflowAgentNodeBinding(+ tenant_id="tenant-1",+ app_id="app-2",+ workflow_id="workflow-2",+ workflow_version="draft",+ node_id="node-2",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id="agent-2",+ current_snapshot_id="snapshot-2",+ node_job_config={},+ )+ other_tenant = WorkflowAgentNodeBinding(+ tenant_id="tenant-2",+ app_id="app-1",+ workflow_id="workflow-3",+ workflow_version="draft",+ node_id="node-3",+ binding_type=WorkflowAgentBindingType.INLINE_AGENT,+ agent_id="agent-3",+ current_snapshot_id="snapshot-3",+ node_job_config={},+ )+ sqlite_session.add_all([target, kept, other_tenant])+ sqlite_session.commit()+ target_id = target.id+ kept_id = kept.id+ other_tenant_id = other_tenant.id++ _delete_workflow_agent_node_bindings("tenant-1", "app-1")++ sqlite_session.expire_all()+ assert sqlite_session.get(WorkflowAgentNodeBinding, target_id) is None+ assert sqlite_session.get(WorkflowAgentNodeBinding, kept_id) is not None+ assert sqlite_session.get(WorkflowAgentNodeBinding, other_tenant_id) is not None+++def test_app_cleanup_removes_agent_bindings_before_workflows(monkeypatch: pytest.MonkeyPatch) -> None:+ events: list[str] = []+ monkeypatch.setattr(remove_app_task_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.COMMUNITY)+ other_cleanup_names = (+ "_delete_app_model_configs",+ "_delete_app_site",+ "_delete_app_mcp_servers",+ "_delete_app_api_tokens",+ "_delete_installed_apps",+ "_delete_app_stars",+ "_delete_recommended_apps",+ "_delete_app_annotation_data",+ "_delete_app_dataset_joins",+ "_delete_app_workflow_runs",+ "_delete_app_workflow_node_executions",+ "_delete_app_workflow_app_logs",+ "_delete_app_conversations",+ "_delete_app_messages",+ "_delete_workflow_tool_providers",+ "_delete_app_tag_bindings",+ "_delete_end_users",+ "_delete_trace_app_configs",+ "_delete_conversation_variables",+ "_delete_draft_variables",+ "_delete_app_triggers",+ "_delete_workflow_plugin_triggers",+ "_delete_workflow_webhook_triggers",+ "_delete_workflow_schedule_plans",+ "_delete_workflow_trigger_logs",+ )+ for name in other_cleanup_names:+ monkeypatch.setattr(remove_app_task_module, name, MagicMock())++ delete_bindings = MagicMock(side_effect=lambda *_args: events.append("bindings"))+ delete_workflows = MagicMock(side_effect=lambda *_args: events.append("workflows"))+ monkeypatch.setattr(remove_app_task_module, "_delete_workflow_agent_node_bindings", delete_bindings)+ monkeypatch.setattr(remove_app_task_module, "_delete_app_workflows", delete_workflows)++ remove_app_task_module.remove_app_and_related_data_task.run(tenant_id="tenant-1", app_id="app-1")++ assert events == ["bindings", "workflows"]+ delete_bindings.assert_called_once_with("tenant-1", "app-1")+ delete_workflows.assert_called_once_with("tenant-1", "app-1")++class TestDeleteDraftVariablesBatch:def test_delete_draft_variables_batch_invalid_batch_size(self):"""Test that invalid batch size raises ValueError."""diff --git a/dify-agent/docs/dify-agent/concepts/runtime-resources/index.md b/dify-agent/docs/dify-agent/concepts/runtime-resources/index.mdindex c3bae6fb3c6..50d47929951 100644--- a/dify-agent/docs/dify-agent/concepts/runtime-resources/index.md+++ b/dify-agent/docs/dify-agent/concepts/runtime-resources/index.md@@ -118,10 +118,12 @@ product use without performing network I/O inside the caller's transaction.Product lifecycle paths commit this transition synchronously. After thetransaction commits, one Celery task asks Dify Agent to destroy the physicalresources. A successful collector deletes the corresponding ledger row. If a-collector raises, the task logs the tenant, resource type, and resource ID,-re-raises the exception so collection stops and Celery records the task as-failed, and leaves the RETIRED row intact. No automatic retry or reconciliation-is performed.+collector raises, the task logs the tenant, resource type, and resource ID and+continues with the other independent resources in the batch. After all resources+have been attempted, any failure makes the Celery task fail and prevents Agent+aggregate deletion. Failed RETIRED rows remain available for a later retry. A+failure to publish the Celery task is also propagated to the product caller. No+automatic retry or reconciliation is performed.The unified `collect_agent_resources` task is registered on normal Celeryworkers and explicitly uses the existing `retention` queue. Standard workers@@ -130,14 +132,26 @@ is required. At a Workflow terminal event, the graph layer synchronously retiresand commits the run's Workspaces before enqueueing collection. When a Workflowchange may orphan Workflow-only Agents, the main product transaction commitsfirst; a fresh session then rechecks effective ownership and retires only Agents-that remain unowned.+that remain unowned. An effective reference is a binding in a normal App's+current draft or current published Workflow. This ownership check applies only+to implicit retirement of Workflow-only Agents. Explicit deletion of a roster+Agent or Agent App proceeds even while Workflows reference it.Retiring a final Binding also retires its Workspace. Workspace collectiondestroys the physical Workspace through one Binding and then collects remainingmaterialized Homes. Home Snapshots are retired when their owning Agent is-retired and are collected only after no draft or config snapshot references-them. Celery performs physical collection only; it does not decide or perform-the initial retirement. Dify Agent itself remains stateless.+retired. `RETIRED` is the sole physical-deletion condition for a Home Snapshot;+Draft and Config Snapshot references are historical pointers and do not keep it+alive. After every external resource in a deletion batch succeeds, Dify API+hard-deletes the archived Agent together with its Drafts, Config Snapshots,+Config Revisions, debug-conversation mappings, and resource ledgers in one+database transaction. Workflow Agent bindings belong to+their Workflows and remain unchanged, so they may hold a dangling Agent ID after+explicit deletion. Dify Agent itself remains stateless.++A `RETIRED` Workspace without a `RETIRED` Binding cannot identify a backend+participant through which to destroy the Workspace. That state is a lifecycle+invariant violation and fails collection instead of being logged as success.There is currently no age-based TTL, periodic GC, or global orphan reconciler.Backend destroy operations are idempotent where supported. Dify API does not