From 01f18acba8cda8544ea5ebc9ab6b9511e86e8982 Mon Sep 17 00:00:00 2001 From: abhi1992002 Date: Mon, 16 Feb 2026 13:51:27 +0530 Subject: [PATCH] fix(library): validate folder existence when updating agent folder ID - Enhanced the `update_library_agent` function to verify that the specified folder ID belongs to the user and is not deleted before updating the agent's folder ID. - This change improves error handling by raising a `NotFoundError` if the folder is not found, ensuring better data integrity and user feedback during agent updates. --- .../backend/backend/api/features/library/db.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/autogpt_platform/backend/backend/api/features/library/db.py b/autogpt_platform/backend/backend/api/features/library/db.py index ea9b97bfd7..e1bcd6f9e1 100644 --- a/autogpt_platform/backend/backend/api/features/library/db.py +++ b/autogpt_platform/backend/backend/api/features/library/db.py @@ -690,7 +690,20 @@ async def update_library_agent( update_fields["settings"] = SafeJson(settings.model_dump()) if folder_id is not None: # Empty string means "move to root" (no folder) - update_fields["folderId"] = None if folder_id == "" else folder_id + if folder_id == "": + update_fields["folderId"] = None + else: + # Verify folder belongs to user + folder = await prisma.models.LibraryFolder.prisma().find_first( + where={ + "id": folder_id, + "userId": user_id, + "isDeleted": False, + } + ) + if not folder: + raise NotFoundError(f"Folder #{folder_id} not found") + update_fields["folderId"] = folder_id try: # If graph_version is provided, update to that specific version