From 6590fcb76f65f4d34747d43e7f477d17fc587b35 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Tue, 2 Dec 2025 21:31:36 +0700 Subject: [PATCH] fix(backend): fix broken update_agent_version_in_library and reduce the method code duplication (#11514) ## Summary Fix broken `update_agent_version_in_library` functionality by eagerly loading `AgentGraph` while loading the library, also consolidating duplicate code that updates agent version in library and configures HITL safe mode settings. ## Problem The `update_agent_version_in_library` is currently failed with this error: ``` File "/Users/abhi/Documents/AutoGPT/autogpt_platform/backend/backend/server/v2/library/model.py", line 110, in from_db raise ValueError("Associated Agent record is required.") ValueError: Associated Agent record is required. ``` also logic was duplicated across two router endpoints with identical implementations, creating maintenance burden and potential for inconsistencies. ## Changes Made ### Created Helper Method - Add `_update_library_agent_version_and_settings()` helper function - Fixes broken `update_agent_version_in_library` by centralizing the logic - Uses proper error handling and settings merging with `model_copy()` ### Replaced Duplicate Code - **In `update_graph` function** (v1.py:863) - replaced 13 lines with single helper call - **In `set_graph_active_version` function** (v1.py:920) - replaced 13 lines with single helper call ### Benefits - **Fixes broken functionality**: Centralizes `update_agent_version_in_library` logic - **DRY Principle**: Eliminates code duplication across two router endpoints - **Maintainability**: Single place to modify the library agent update logic - **Consistency**: Ensures both endpoints use identical logic for HITL safe mode configuration - **Readability**: Cleaner, more focused endpoint implementations ## Technical Details The helper method fixes broken `update_agent_version_in_library` by handling: 1. Updating agent version in library via `update_agent_version_in_library()` 2. Conditionally setting `human_in_the_loop_safe_mode: true` if graph has HITL blocks and setting is not already configured 3. Proper settings merging to preserve existing configuration ## Testing - [x] Code compiles and passes type checking - [x] Pre-commit hooks pass (linting, formatting, type checking) - [x] Both affected endpoints maintain same functionality with cleaner implementation Fixes broken duplicate code identified in v1.py router endpoints for `update_agent_version_in_library`. Co-authored-by: Claude --- .../backend/backend/server/routers/v1.py | 48 +++++++++---------- .../backend/backend/server/v2/library/db.py | 1 + 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/autogpt_platform/backend/backend/server/routers/v1.py b/autogpt_platform/backend/backend/server/routers/v1.py index 7ad812af8a..de4d8137c4 100644 --- a/autogpt_platform/backend/backend/server/routers/v1.py +++ b/autogpt_platform/backend/backend/server/routers/v1.py @@ -143,6 +143,28 @@ async def hide_activity_summary_if_disabled( return execution +async def _update_library_agent_version_and_settings( + user_id: str, agent_graph: graph_db.GraphModel +) -> library_db.library_model.LibraryAgent: + # Keep the library agent up to date with the new active version + library = await library_db.update_agent_version_in_library( + user_id, agent_graph.id, agent_graph.version + ) + # If the graph has HITL node, initialize the setting if it's not already set. + if ( + agent_graph.has_human_in_the_loop + and library.settings.human_in_the_loop_safe_mode is None + ): + await library_db.update_library_agent_settings( + user_id=user_id, + agent_id=library.id, + settings=library.settings.model_copy( + update={"human_in_the_loop_safe_mode": True} + ), + ) + return library + + # Define the API routes v1_router = APIRouter() @@ -838,18 +860,7 @@ async def update_graph( if new_graph_version.is_active: # Keep the library agent up to date with the new active version - library = await library_db.update_agent_version_in_library( - user_id, graph.id, graph.version - ) - if ( - new_graph_version.has_human_in_the_loop - and library.settings.human_in_the_loop_safe_mode is None - ): - await library_db.update_library_agent_settings( - user_id=user_id, - agent_id=library.id, - settings=GraphSettings(human_in_the_loop_safe_mode=True), - ) + await _update_library_agent_version_and_settings(user_id, new_graph_version) # Handle activation of the new graph first to ensure continuity new_graph_version = await on_graph_activate(new_graph_version, user_id=user_id) @@ -906,18 +917,7 @@ async def set_graph_active_version( ) # Keep the library agent up to date with the new active version - library = await library_db.update_agent_version_in_library( - user_id, new_active_graph.id, new_active_graph.version - ) - if ( - new_active_graph.has_human_in_the_loop - and library.settings.human_in_the_loop_safe_mode is None - ): - await library_db.update_library_agent_settings( - user_id=user_id, - agent_id=library.id, - settings=GraphSettings(human_in_the_loop_safe_mode=True), - ) + await _update_library_agent_version_and_settings(user_id, new_active_graph) if current_active_graph and current_active_graph.version != new_active_version: # Handle deactivation of the previously active version diff --git a/autogpt_platform/backend/backend/server/v2/library/db.py b/autogpt_platform/backend/backend/server/v2/library/db.py index aae6e4cc65..17a0efa7be 100644 --- a/autogpt_platform/backend/backend/server/v2/library/db.py +++ b/autogpt_platform/backend/backend/server/v2/library/db.py @@ -523,6 +523,7 @@ async def update_agent_version_in_library( }, }, }, + include={"AgentGraph": True}, ) if lib is None: raise NotFoundError(f"Library agent {library_agent.id} not found")