AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): prevent execution of deleted agents and cleanup orphaned resources (#11243)
Zamil Majdyde70ede54a43
diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.pyindex bf9285c84b..b1a6c9f44e 100644--- a/autogpt_platform/backend/backend/data/graph.py+++ b/autogpt_platform/backend/backend/data/graph.py@@ -5,12 +5,19 @@ from datetime import datetime, timezonefrom typing import TYPE_CHECKING, Any, Literal, Optional, castfrom prisma.enums import SubmissionStatus-from prisma.models import AgentGraph, AgentNode, AgentNodeLink, StoreListingVersion+from prisma.models import (+ AgentGraph,+ AgentNode,+ AgentNodeLink,+ LibraryAgent,+ StoreListingVersion,+)from prisma.types import (AgentGraphCreateInput,AgentGraphWhereInput,AgentNodeCreateInput,AgentNodeLinkCreateInput,+ LibraryAgentWhereInput,StoreListingVersionWhereInput,)from pydantic import BaseModel, Field, create_model@@ -30,6 +37,7 @@ from backend.data.model import ()from backend.integrations.providers import ProviderNamefrom backend.util import type as type_utils+from backend.util.exceptions import GraphNotInLibraryErrorfrom backend.util.json import SafeJsonfrom backend.util.models import Pagination@@ -1102,6 +1110,54 @@ async def delete_graph(graph_id: str, user_id: str) -> int:return entries_count+async def validate_graph_execution_permissions(+ graph_id: str, user_id: str, graph_version: Optional[int] = None+) -> None:+ """+ Validate that a user has permission to execute a specific graph.++ This function performs comprehensive authorization checks and raises specific+ exceptions for different types of failures to enable appropriate error handling.++ Args:+ graph_id: The ID of the graph to check+ user_id: The ID of the user+ graph_version: Optional specific version to check++ Raises:+ GraphNotInLibraryError: If the graph is not in the user's library (deleted/archived)+ NotAuthorizedError: If the user lacks execution permissions for other reasons+ """++ # Step 1: Check library membership (raises specific GraphNotInLibraryError)+ where_clause: LibraryAgentWhereInput = {+ "userId": user_id,+ "agentGraphId": graph_id,+ "isDeleted": False,+ "isArchived": False,+ }++ if graph_version is not None:+ where_clause["agentGraphVersion"] = graph_version++ count = await LibraryAgent.prisma().count(where=where_clause)+ if count == 0:+ raise GraphNotInLibraryError(+ f"Graph #{graph_id} is not accessible in your library"+ )++ # Step 2: Check execution-specific permissions (raises generic NotAuthorizedError)+ # Additional authorization checks beyond library membership:+ # 1. Check if user has execution credits (future)+ # 2. Check if graph is suspended/disabled (future)+ # 3. Check rate limiting rules (future)+ # 4. Check organization-level permissions (future)++ # For now, library membership is sufficient for execution permission+ # Future enhancements can add more granular permission checks here+ # When adding new checks, raise NotAuthorizedError for non-library issues++async def create_graph(graph: Graph, user_id: str) -> GraphModel:async with transaction() as tx:await __create_graph(tx, graph, user_id)diff --git a/autogpt_platform/backend/backend/data/integrations.py b/autogpt_platform/backend/backend/data/integrations.pyindex 82f9d7a8bb..68bdbe085f 100644--- a/autogpt_platform/backend/backend/data/integrations.py+++ b/autogpt_platform/backend/backend/data/integrations.py@@ -1,7 +1,7 @@import loggingfrom typing import AsyncGenerator, Literal, Optional, overload-from prisma.models import IntegrationWebhook+from prisma.models import AgentNode, AgentPreset, IntegrationWebhookfrom prisma.types import (IntegrationWebhookCreateInput,IntegrationWebhookUpdateInput,@@ -15,7 +15,9 @@ from backend.data.includes import (INTEGRATION_WEBHOOK_INCLUDE,MAX_INTEGRATION_WEBHOOKS_FETCH,)+from backend.integrations.creds_manager import IntegrationCredentialsManagerfrom backend.integrations.providers import ProviderName+from backend.integrations.webhooks import get_webhook_managerfrom backend.integrations.webhooks.utils import webhook_ingress_urlfrom backend.server.v2.library.model import LibraryAgentPresetfrom backend.util.exceptions import NotFoundError@@ -237,6 +239,77 @@ async def update_webhook(return Webhook.from_db(_updated_webhook)+async def find_webhooks_by_graph_id(graph_id: str, user_id: str) -> list[Webhook]:+ """+ Find all webhooks that trigger nodes OR presets in a specific graph for a user.++ Args:+ graph_id: The ID of the graph+ user_id: The ID of the user++ Returns:+ list[Webhook]: List of webhooks associated with the graph+ """+ where_clause: IntegrationWebhookWhereInput = {+ "userId": user_id,+ "OR": [+ # Webhooks that trigger nodes in this graph+ {"AgentNodes": {"some": {"agentGraphId": graph_id}}},+ # Webhooks that trigger presets for this graph+ {"AgentPresets": {"some": {"agentGraphId": graph_id}}},+ ],+ }+ webhooks = await IntegrationWebhook.prisma().find_many(where=where_clause)+ return [Webhook.from_db(webhook) for webhook in webhooks]+++async def unlink_webhook_from_graph(+ webhook_id: str, graph_id: str, user_id: str+) -> None:+ """+ Unlink a webhook from all nodes and presets in a specific graph.+ If the webhook has no remaining triggers, it will be automatically deleted+ and deregistered with the provider.++ Args:+ webhook_id: The ID of the webhook+ graph_id: The ID of the graph to unlink from+ user_id: The ID of the user (for authorization)+ """+ # Avoid circular imports+ from backend.data.graph import set_node_webhook+ from backend.server.v2.library.db import set_preset_webhook++ # Find all nodes in this graph that use this webhook+ nodes = await AgentNode.prisma().find_many(+ where={"agentGraphId": graph_id, "webhookId": webhook_id}+ )++ # Unlink webhook from each node+ for node in nodes:+ await set_node_webhook(node.id, None)++ # Find all presets for this graph that use this webhook+ presets = await AgentPreset.prisma().find_many(+ where={"agentGraphId": graph_id, "webhookId": webhook_id, "userId": user_id}+ )++ # Unlink webhook from each preset+ for preset in presets:+ await set_preset_webhook(user_id, preset.id, None)++ # Check if webhook needs cleanup (prune_webhook_if_dangling handles the trigger check)+ webhook = await get_webhook(webhook_id, include_relations=False)+ webhook_manager = get_webhook_manager(webhook.provider)+ creds_manager = IntegrationCredentialsManager()+ credentials = (+ await creds_manager.get(user_id, webhook.credentials_id)+ if webhook.credentials_id+ else None+ )+ await webhook_manager.prune_webhook_if_dangling(user_id, webhook.id, credentials)++async def delete_webhook(user_id: str, webhook_id: str) -> None:deleted = await IntegrationWebhook.prisma().delete_many(where={"id": webhook_id, "userId": user_id}diff --git a/autogpt_platform/backend/backend/executor/database.py b/autogpt_platform/backend/backend/executor/database.pyindex a590501bf0..929a502d7d 100644--- a/autogpt_platform/backend/backend/executor/database.py+++ b/autogpt_platform/backend/backend/executor/database.py@@ -28,6 +28,7 @@ from backend.data.graph import (get_graph,get_graph_metadata,get_node,+ validate_graph_execution_permissions,)from backend.data.notifications import (clear_all_user_notification_batches,@@ -174,6 +175,7 @@ class DatabaseManager(AppService):# Librarylist_library_agents = _(list_library_agents)add_store_agent_to_library = _(add_store_agent_to_library)+ validate_graph_execution_permissions = _(validate_graph_execution_permissions)# Storeget_store_agents = _(get_store_agents)@@ -217,6 +219,7 @@ class DatabaseManagerClient(AppServiceClient):# Librarylist_library_agents = _(d.list_library_agents)add_store_agent_to_library = _(d.add_store_agent_to_library)+ validate_graph_execution_permissions = _(d.validate_graph_execution_permissions)# Storeget_store_agents = _(d.get_store_agents)@@ -272,6 +275,7 @@ class DatabaseManagerAsyncClient(AppServiceClient):# Librarylist_library_agents = d.list_library_agentsadd_store_agent_to_library = d.add_store_agent_to_library+ validate_graph_execution_permissions = d.validate_graph_execution_permissions# Storeget_store_agents = d.get_store_agentsdiff --git a/autogpt_platform/backend/backend/executor/manager_test.py b/autogpt_platform/backend/backend/executor/manager_test.pyindex 0b37c2e6a7..cd543fef4e 100644--- a/autogpt_platform/backend/backend/executor/manager_test.py+++ b/autogpt_platform/backend/backend/executor/manager_test.py@@ -521,6 +521,14 @@ async def test_store_listing_graph(server: SpinTestServer):),user_id=admin_user.id,)++ # Add the approved store listing to the admin user's library so they can execute it+ from backend.server.v2.library.db import add_store_agent_to_library++ await add_store_agent_to_library(+ store_listing_version_id=slv_id, user_id=admin_user.id+ )+alt_test_user = admin_userdata = {"input_1": "Hello", "input_2": "World"}diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.pyindex 56b3d901c0..fdfa78b2d8 100644--- a/autogpt_platform/backend/backend/executor/scheduler.py+++ b/autogpt_platform/backend/backend/executor/scheduler.py@@ -33,8 +33,13 @@ from backend.monitoring import (report_block_error_rates,report_late_executions,)+from backend.util.clients import get_scheduler_clientfrom backend.util.cloud_storage import cleanup_expired_files_async-from backend.util.exceptions import NotAuthorizedError, NotFoundError+from backend.util.exceptions import (+ GraphNotInLibraryError,+ NotAuthorizedError,+ NotFoundError,+)from backend.util.logging import PrefixFilterfrom backend.util.retry import func_retryfrom backend.util.service import (@@ -155,6 +160,14 @@ async def _execute_graph(**kwargs):f"Graph execution {graph_exec.id} took {elapsed:.2f}s to create/publish - "f"this is unusually slow and may indicate resource contention")+ except GraphNotInLibraryError as e:+ elapsed = asyncio.get_event_loop().time() - start_time+ logger.warning(+ f"Scheduled execution blocked for deleted/archived graph {args.graph_id} "+ f"(user {args.user_id}) after {elapsed:.2f}s: {e}"+ )+ # Clean up orphaned schedules for this graph+ await _cleanup_orphaned_schedules_for_graph(args.graph_id, args.user_id)except Exception as e:elapsed = asyncio.get_event_loop().time() - start_timelogger.error(@@ -163,6 +176,33 @@ async def _execute_graph(**kwargs):)+async def _cleanup_orphaned_schedules_for_graph(graph_id: str, user_id: str) -> None:+ """+ Clean up orphaned schedules for a specific graph when execution fails with GraphNotInLibraryError.+ This happens when an agent is deleted but schedules still exist.+ """+ # Use scheduler client to access the scheduler service+ scheduler_client = get_scheduler_client()++ # Find all schedules for this graph and user+ schedules = await scheduler_client.get_execution_schedules(+ graph_id=graph_id, user_id=user_id+ )++ for schedule in schedules:+ try:+ await scheduler_client.delete_schedule(+ schedule_id=schedule.id, user_id=user_id+ )+ logger.info(+ f"Cleaned up orphaned schedule {schedule.id} for deleted/archived graph {graph_id}"+ )+ except Exception:+ logger.exception(+ f"Failed to delete orphaned schedule {schedule.id} for graph {graph_id}"+ )++def cleanup_expired_files():"""Clean up expired files from cloud storage."""# Wait for completiondiff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.pyindex a2a6d429f1..445a9ca7d8 100644--- a/autogpt_platform/backend/backend/executor/utils.py+++ b/autogpt_platform/backend/backend/executor/utils.py@@ -513,6 +513,15 @@ async def validate_and_construct_node_execution_input(if not graph:raise NotFoundError(f"Graph #{graph_id} not found.")+ # Validate that the user has permission to execute this graph+ # This checks both library membership and execution permissions,+ # raising specific exceptions for appropriate error handling+ await gdb.validate_graph_execution_permissions(+ graph_id=graph_id,+ user_id=user_id,+ graph_version=graph.version,+ )+nodes_input_masks = _merge_nodes_input_masks((make_node_credentials_input_map(graph, graph_credentials_inputs)diff --git a/autogpt_platform/backend/backend/integrations/webhooks/_base.py b/autogpt_platform/backend/backend/integrations/webhooks/_base.pyindex 9342a6417b..7daf0dc6de 100644--- a/autogpt_platform/backend/backend/integrations/webhooks/_base.py+++ b/autogpt_platform/backend/backend/integrations/webhooks/_base.py@@ -105,11 +105,15 @@ class BaseWebhooksManager(ABC, Generic[WT]):webhook = await integrations.get_webhook(webhook_id, include_relations=True)if webhook.triggered_nodes or webhook.triggered_presets:# Don't prune webhook if in use+ logger.info(+ f"Webhook #{webhook_id} kept as it has triggers in other graphs"+ )return Falseif credentials:await self._deregister_webhook(webhook, credentials)await integrations.delete_webhook(user_id, webhook.id)+ logger.info(f"Webhook #{webhook_id} deleted as it had no remaining triggers")return True# --8<-- [start:BaseWebhooksManager3]diff --git a/autogpt_platform/backend/backend/server/integrations/router.py b/autogpt_platform/backend/backend/server/integrations/router.pyindex cc509c698e..012bcf9ff4 100644--- a/autogpt_platform/backend/backend/server/integrations/router.py+++ b/autogpt_platform/backend/backend/server/integrations/router.py@@ -1,7 +1,7 @@import asyncioimport loggingfrom datetime import datetime, timedelta, timezone-from typing import TYPE_CHECKING, Annotated, Awaitable, List, Literal+from typing import TYPE_CHECKING, Annotated, List, Literalfrom autogpt_libs.auth import get_user_idfrom fastapi import (@@ -17,9 +17,10 @@ from fastapi import (from pydantic import BaseModel, Field, SecretStrfrom starlette.status import HTTP_500_INTERNAL_SERVER_ERROR, HTTP_502_BAD_GATEWAY-from backend.data.graph import get_graph, set_node_webhook+from backend.data.graph import NodeModel, get_graph, set_node_webhookfrom backend.data.integrations import (WebhookEvent,+ WebhookWithRelations,get_all_webhooks_by_creds,get_webhook,publish_webhook_event,@@ -46,7 +47,13 @@ from backend.server.integrations.models import (get_all_provider_names,)from backend.server.v2.library.db import set_preset_webhook, update_preset-from backend.util.exceptions import MissingConfigError, NeedConfirmation, NotFoundError+from backend.server.v2.library.model import LibraryAgentPreset+from backend.util.exceptions import (+ GraphNotInLibraryError,+ MissingConfigError,+ NeedConfirmation,+ NotFoundError,+)from backend.util.settings import Settingsif TYPE_CHECKING:@@ -369,65 +376,23 @@ async def webhook_ingress_generic(if not (webhook.triggered_nodes or webhook.triggered_presets):return- executions: list[Awaitable] = []await complete_webhook_trigger_step(user_id)- for node in webhook.triggered_nodes:- logger.debug(f"Webhook-attached node: {node}")- if not node.is_triggered_by_event_type(event_type):- logger.debug(f"Node #{node.id} doesn't trigger on event {event_type}")- continue- logger.debug(f"Executing graph #{node.graph_id} node #{node.id}")- executions.append(- add_graph_execution(- user_id=webhook.user_id,- graph_id=node.graph_id,- graph_version=node.graph_version,- nodes_input_masks={node.id: {"payload": payload}},- )- )- for preset in webhook.triggered_presets:- logger.debug(f"Webhook-attached preset: {preset}")- if not preset.is_active:- logger.debug(f"Preset #{preset.id} is inactive")- continue-- graph = await get_graph(preset.graph_id, preset.graph_version, webhook.user_id)- if not graph:- logger.error(- f"User #{webhook.user_id} has preset #{preset.id} for graph "- f"#{preset.graph_id} v{preset.graph_version}, "- "but no access to the graph itself."- )- logger.info(f"Automatically deactivating broken preset #{preset.id}")- await update_preset(preset.user_id, preset.id, is_active=False)- continue- if not (trigger_node := graph.webhook_input_node):- # NOTE: this should NEVER happen, but we log and handle it gracefully- logger.error(- f"Preset #{preset.id} is triggered by webhook #{webhook.id}, but graph "- f"#{preset.graph_id} v{preset.graph_version} has no webhook input node"- )- await set_preset_webhook(preset.user_id, preset.id, None)- continue- if not trigger_node.block.is_triggered_by_event_type(preset.inputs, event_type):- logger.debug(f"Preset #{preset.id} doesn't trigger on event {event_type}")- continue- logger.debug(f"Executing preset #{preset.id} for webhook #{webhook.id}")-- executions.append(- add_graph_execution(- user_id=webhook.user_id,- graph_id=preset.graph_id,- preset_id=preset.id,- graph_version=preset.graph_version,- graph_credentials_inputs=preset.credentials,- nodes_input_masks={- trigger_node.id: {**preset.inputs, "payload": payload}- },- )+ # Execute all triggers concurrently for better performance+ tasks = []+ tasks.extend(+ _execute_webhook_node_trigger(node, webhook, webhook_id, event_type, payload)+ for node in webhook.triggered_nodes+ )+ tasks.extend(+ _execute_webhook_preset_trigger(+ preset, webhook, webhook_id, event_type, payload)- asyncio.gather(*executions)+ for preset in webhook.triggered_presets+ )++ if tasks:+ await asyncio.gather(*tasks, return_exceptions=True)@router.post("/webhooks/{webhook_id}/ping")@@ -456,6 +421,103 @@ async def webhook_ping(return True+async def _execute_webhook_node_trigger(+ node: NodeModel,+ webhook: WebhookWithRelations,+ webhook_id: str,+ event_type: str,+ payload: dict,+) -> None:+ """Execute a webhook-triggered node."""+ logger.debug(f"Webhook-attached node: {node}")+ if not node.is_triggered_by_event_type(event_type):+ logger.debug(f"Node #{node.id} doesn't trigger on event {event_type}")+ return+ logger.debug(f"Executing graph #{node.graph_id} node #{node.id}")+ try:+ await add_graph_execution(+ user_id=webhook.user_id,+ graph_id=node.graph_id,+ graph_version=node.graph_version,+ nodes_input_masks={node.id: {"payload": payload}},+ )+ except GraphNotInLibraryError as e:+ logger.warning(+ f"Webhook #{webhook_id} execution blocked for "+ f"deleted/archived graph #{node.graph_id} (node #{node.id}): {e}"+ )+ # Clean up orphaned webhook trigger for this graph+ await _cleanup_orphaned_webhook_for_graph(+ node.graph_id, webhook.user_id, webhook_id+ )+ except Exception:+ logger.exception(+ f"Failed to execute graph #{node.graph_id} via webhook #{webhook_id}"+ )+ # Continue processing - webhook should be resilient to individual failures+++async def _execute_webhook_preset_trigger(+ preset: LibraryAgentPreset,+ webhook: WebhookWithRelations,+ webhook_id: str,+ event_type: str,+ payload: dict,+) -> None:+ """Execute a webhook-triggered preset."""+ logger.debug(f"Webhook-attached preset: {preset}")+ if not preset.is_active:+ logger.debug(f"Preset #{preset.id} is inactive")+ return++ graph = await get_graph(preset.graph_id, preset.graph_version, webhook.user_id)+ if not graph:+ logger.error(+ f"User #{webhook.user_id} has preset #{preset.id} for graph "+ f"#{preset.graph_id} v{preset.graph_version}, "+ "but no access to the graph itself."+ )+ logger.info(f"Automatically deactivating broken preset #{preset.id}")+ await update_preset(preset.user_id, preset.id, is_active=False)+ return+ if not (trigger_node := graph.webhook_input_node):+ # NOTE: this should NEVER happen, but we log and handle it gracefully+ logger.error(+ f"Preset #{preset.id} is triggered by webhook #{webhook.id}, but graph "+ f"#{preset.graph_id} v{preset.graph_version} has no webhook input node"+ )+ await set_preset_webhook(preset.user_id, preset.id, None)+ return+ if not trigger_node.block.is_triggered_by_event_type(preset.inputs, event_type):+ logger.debug(f"Preset #{preset.id} doesn't trigger on event {event_type}")+ return+ logger.debug(f"Executing preset #{preset.id} for webhook #{webhook.id}")++ try:+ await add_graph_execution(+ user_id=webhook.user_id,+ graph_id=preset.graph_id,+ preset_id=preset.id,+ graph_version=preset.graph_version,+ graph_credentials_inputs=preset.credentials,+ nodes_input_masks={trigger_node.id: {**preset.inputs, "payload": payload}},+ )+ except GraphNotInLibraryError as e:+ logger.warning(+ f"Webhook #{webhook_id} execution blocked for "+ f"deleted/archived graph #{preset.graph_id} (preset #{preset.id}): {e}"+ )+ # Clean up orphaned webhook trigger for this graph+ await _cleanup_orphaned_webhook_for_graph(+ preset.graph_id, webhook.user_id, webhook_id+ )+ except Exception:+ logger.exception(+ f"Failed to execute preset #{preset.id} via webhook #{webhook_id}"+ )+ # Continue processing - webhook should be resilient to individual failures++# --------------------------- UTILITIES ---------------------------- #@@ -496,6 +558,97 @@ async def remove_all_webhooks_for_credentials(logger.warning(f"Webhook #{webhook.id} failed to prune")+async def _cleanup_orphaned_webhook_for_graph(+ graph_id: str, user_id: str, webhook_id: str+) -> None:+ """+ Clean up orphaned webhook connections for a specific graph when execution fails with GraphNotInLibraryError.+ This happens when an agent is deleted but webhook triggers still exist.+ """+ try:+ webhook = await get_webhook(webhook_id, include_relations=True)+ if not webhook or webhook.user_id != user_id:+ logger.warning(+ f"Webhook {webhook_id} not found or doesn't belong to user {user_id}"+ )+ return++ nodes_removed = 0+ presets_removed = 0++ # Remove triggered nodes that belong to the deleted graph+ for node in webhook.triggered_nodes:+ if node.graph_id == graph_id:+ try:+ await set_node_webhook(node.id, None)+ nodes_removed += 1+ logger.info(+ f"Removed orphaned webhook trigger from node {node.id} "+ f"in deleted/archived graph {graph_id}"+ )+ except Exception:+ logger.exception(+ f"Failed to remove webhook trigger from node {node.id}"+ )++ # Remove triggered presets that belong to the deleted graph+ for preset in webhook.triggered_presets:+ if preset.graph_id == graph_id:+ try:+ await set_preset_webhook(user_id, preset.id, None)+ presets_removed += 1+ logger.info(+ f"Removed orphaned webhook trigger from preset {preset.id} "+ f"for deleted/archived graph {graph_id}"+ )+ except Exception:+ logger.exception(+ f"Failed to remove webhook trigger from preset {preset.id}"+ )++ if nodes_removed > 0 or presets_removed > 0:+ logger.info(+ f"Cleaned up orphaned webhook #{webhook_id}: "+ f"removed {nodes_removed} nodes and {presets_removed} presets "+ f"for deleted/archived graph #{graph_id}"+ )++ # Check if webhook has any remaining triggers, if not, prune it+ updated_webhook = await get_webhook(webhook_id, include_relations=True)+ if (+ not updated_webhook.triggered_nodes+ and not updated_webhook.triggered_presets+ ):+ try:+ webhook_manager = get_webhook_manager(+ ProviderName(webhook.provider)+ )+ credentials = (+ await creds_manager.get(user_id, webhook.credentials_id)+ if webhook.credentials_id+ else None+ )+ success = await webhook_manager.prune_webhook_if_dangling(+ user_id, webhook.id, credentials+ )+ if success:+ logger.info(+ f"Pruned orphaned webhook #{webhook_id} "+ f"with no remaining triggers"+ )+ else:+ logger.warning(+ f"Failed to prune orphaned webhook #{webhook_id}"+ )+ except Exception:+ logger.exception(f"Failed to prune orphaned webhook #{webhook_id}")++ except Exception:+ logger.exception(+ f"Failed to cleanup orphaned webhook #{webhook_id} for graph #{graph_id}"+ )++def _get_provider_oauth_handler(req: Request, provider_name: ProviderName) -> "BaseOAuthHandler":diff --git a/autogpt_platform/backend/backend/server/v2/library/db.py b/autogpt_platform/backend/backend/server/v2/library/db.pyindex 6e9082492a..1609929ecd 100644--- a/autogpt_platform/backend/backend/server/v2/library/db.py+++ b/autogpt_platform/backend/backend/server/v2/library/db.py@@ -9,6 +9,7 @@ import prisma.modelsimport prisma.typesimport backend.data.graph as graph_db+import backend.data.integrations as integrations_dbimport backend.server.v2.library.model as library_modelimport backend.server.v2.store.exceptions as store_exceptionsimport backend.server.v2.store.image_gen as store_image_gen@@ -20,6 +21,7 @@ from backend.data.includes import AGENT_PRESET_INCLUDE, library_agent_includefrom backend.data.model import CredentialsMetaInputfrom backend.integrations.creds_manager import IntegrationCredentialsManagerfrom backend.integrations.webhooks.graph_lifecycle_hooks import on_graph_activate+from backend.util.clients import get_scheduler_clientfrom backend.util.exceptions import DatabaseError, NotFoundErrorfrom backend.util.json import SafeJsonfrom backend.util.models import Pagination@@ -546,18 +548,88 @@ async def update_library_agent(async def delete_library_agent(library_agent_id: str, user_id: str, soft_delete: bool = True) -> None:+ # First get the agent to find the graph_id for cleanup+ library_agent = await prisma.models.LibraryAgent.prisma().find_unique(+ where={"id": library_agent_id}, include={"AgentGraph": True}+ )++ if not library_agent or library_agent.userId != user_id:+ raise NotFoundError(f"Library agent #{library_agent_id} not found")++ graph_id = library_agent.agentGraphId++ # Clean up associated schedules and webhooks BEFORE deleting the agent+ # This prevents executions from starting after agent deletion+ await _cleanup_schedules_for_graph(graph_id=graph_id, user_id=user_id)+ await _cleanup_webhooks_for_graph(graph_id=graph_id, user_id=user_id)++ # Delete the library agent after cleanupif soft_delete:deleted_count = await prisma.models.LibraryAgent.prisma().update_many(- where={"id": library_agent_id, "userId": user_id}, data={"isDeleted": True}+ where={"id": library_agent_id, "userId": user_id},+ data={"isDeleted": True},)else:deleted_count = await prisma.models.LibraryAgent.prisma().delete_many(where={"id": library_agent_id, "userId": user_id})+if deleted_count < 1:raise NotFoundError(f"Library agent #{library_agent_id} not found")+async def _cleanup_schedules_for_graph(graph_id: str, user_id: str) -> None:+ """+ Clean up all schedules for a specific graph and user.++ Args:+ graph_id: The ID of the graph+ user_id: The ID of the user+ """+ scheduler_client = get_scheduler_client()+ schedules = await scheduler_client.get_execution_schedules(+ graph_id=graph_id, user_id=user_id+ )++ for schedule in schedules:+ try:+ await scheduler_client.delete_schedule(+ schedule_id=schedule.id, user_id=user_id+ )+ logger.info(f"Deleted schedule {schedule.id} for graph {graph_id}")+ except Exception:+ logger.exception(+ f"Failed to delete schedule {schedule.id} for graph {graph_id}"+ )+++async def _cleanup_webhooks_for_graph(graph_id: str, user_id: str) -> None:+ """+ Clean up webhook connections for a specific graph and user.+ Unlinks webhooks from this graph and deletes them if no other triggers remain.++ Args:+ graph_id: The ID of the graph+ user_id: The ID of the user+ """+ # Find all webhooks that trigger nodes in this graph+ webhooks = await integrations_db.find_webhooks_by_graph_id(+ graph_id=graph_id, user_id=user_id+ )++ for webhook in webhooks:+ try:+ # Unlink webhook from this graph's nodes and presets+ await integrations_db.unlink_webhook_from_graph(+ webhook_id=webhook.id, graph_id=graph_id, user_id=user_id+ )+ logger.info(f"Unlinked webhook {webhook.id} from graph {graph_id}")+ except Exception:+ logger.exception(+ f"Failed to unlink webhook {webhook.id} from graph {graph_id}"+ )++async def delete_library_agent_by_graph_id(graph_id: str, user_id: str) -> None:"""Deletes a library agent for the given userdiff --git a/autogpt_platform/backend/backend/util/exceptions.py b/autogpt_platform/backend/backend/util/exceptions.pyindex 892f14470a..d17dc7487a 100644--- a/autogpt_platform/backend/backend/util/exceptions.py+++ b/autogpt_platform/backend/backend/util/exceptions.py@@ -17,6 +17,12 @@ class NotAuthorizedError(ValueError):"""The user is not authorized to perform the requested operation"""+class GraphNotInLibraryError(NotAuthorizedError):+ """Raised when attempting to execute a graph that is not in the user's library (deleted/archived)."""++ pass++class InsufficientBalanceError(ValueError):user_id: strmessage: str