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): auto-migrate webhook presets to new agent version on pu… (#12753)
abderrahmen bejaoui50cb6c624480
diff --git a/autogpt_platform/backend/backend/api/features/library/db.py b/autogpt_platform/backend/backend/api/features/library/db.pyindex f427f0693f..d886cf8a64 100644--- a/autogpt_platform/backend/backend/api/features/library/db.py+++ b/autogpt_platform/backend/backend/api/features/library/db.py@@ -743,6 +743,15 @@ async def update_graph_in_library(if current_active_version:await on_graph_deactivate(current_active_version, user_id=user_id)+ # Migrate webhook-attached presets to the new version so that+ # existing webhook URLs continue to trigger the latest agent version.+ if created_graph.webhook_input_node:+ await migrate_webhook_presets_to_new_version(+ user_id=user_id,+ graph_id=created_graph.id,+ new_version=created_graph.version,+ )+return created_graph, library_agent@@ -1948,6 +1957,54 @@ async def set_preset_webhook(return library_model.LibraryAgentPreset.from_db(updated)+async def migrate_webhook_presets_to_new_version(+ user_id: str,+ graph_id: str,+ new_version: int,+) -> int:+ """+ Migrates webhook-attached presets for a graph to a newly activated version.++ When a new agent version is activated (i.e. becomes the live version),+ presets with webhooks that were pinned to an older version are updated+ to point to the new active version, so that existing webhook URLs+ continue to trigger the latest agent version.++ Presets pinned to a newer version than ``new_version`` (e.g. manually+ pinned to a future/specific version) are intentionally left untouched.++ Only migrates presets that:+ - Belong to the user+ - Are attached to a webhook (webhookId is not null)+ - Are not deleted+ - Are for the given graph and pinned to a strictly older version++ Args:+ user_id: The owner of the presets.+ graph_id: The graph ID whose presets should be migrated.+ new_version: The newly activated graph version to migrate presets to.++ Returns:+ The number of presets migrated.+ """+ count = await prisma.models.AgentPreset.prisma().update_many(+ where={+ "userId": user_id,+ "agentGraphId": graph_id,+ "agentGraphVersion": {"lt": new_version},+ "webhookId": {"not": None},+ "isDeleted": False,+ },+ data={"agentGraphVersion": new_version},+ )+ if count > 0:+ logger.info(+ f"Migrated {count} webhook preset(s) for graph #{graph_id} "+ f"to version {new_version} (user #{user_id})"+ )+ return count++async def delete_preset(user_id: str, preset_id: str) -> None:"""Soft-deletes a preset by marking it as isDeleted = True.diff --git a/autogpt_platform/backend/backend/api/features/v1.py b/autogpt_platform/backend/backend/api/features/v1.pyindex 9442ea93d2..898aae167d 100644--- a/autogpt_platform/backend/backend/api/features/v1.py+++ b/autogpt_platform/backend/backend/api/features/v1.py@@ -1693,6 +1693,15 @@ async def update_graph(if current_active_version:await on_graph_deactivate(current_active_version, user_id=user_id)+ # Migrate webhook-attached presets to the new version so that+ # existing webhook URLs continue to trigger the latest agent version.+ if new_graph_version.webhook_input_node:+ await library_db.migrate_webhook_presets_to_new_version(+ user_id=user_id,+ graph_id=graph_id,+ new_version=new_graph_version.version,+ )+new_graph_version_with_subgraphs = await graph_db.get_graph(graph_id,new_graph_version.version,@@ -1745,6 +1754,15 @@ async def set_graph_active_version(# Handle deactivation of the previously active versionawait on_graph_deactivate(current_active_graph, user_id=user_id)+ # Migrate webhook-attached presets to the new active version so that+ # existing webhook URLs continue to trigger the latest agent version.+ if new_active_graph.webhook_input_node:+ await library_db.migrate_webhook_presets_to_new_version(+ user_id=user_id,+ graph_id=graph_id,+ new_version=new_active_version,+ )+@v1_router.patch(path="/graphs/{graph_id}/settings",diff --git a/autogpt_platform/backend/test/test_migrate_webhook_presets.py b/autogpt_platform/backend/test/test_migrate_webhook_presets.pynew file mode 100644index 0000000000..54156e5e1b--- /dev/null+++ b/autogpt_platform/backend/test/test_migrate_webhook_presets.py@@ -0,0 +1,359 @@+"""+Unit tests for migrate_webhook_presets_to_new_version.+Mocks prisma to avoid needing a running database.+"""++import logging+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.api.features.library.db import migrate_webhook_presets_to_new_version++# Patch prisma.models.AgentPreset.prisma per the project-wide convention used+# in backend/api/features/library/db_test.py and the rest of the suite.+_PRISMA_PATCH_TARGET = "prisma.models.AgentPreset.prisma"+++def mock_prisma():+ with patch(_PRISMA_PATCH_TARGET) as mock:+ mock_client = AsyncMock()+ mock.return_value = mock_client+ yield mock_client+++async def test_migrate_updates_matching_presets(mock_prisma):+ mock_prisma.update_many = AsyncMock(return_value=3)++ count = await migrate_webhook_presets_to_new_version(+ user_id="user-123", graph_id="graph-abc", new_version=5+ )++ assert count == 3+ mock_prisma.update_many.assert_called_once_with(+ where={+ "userId": "user-123",+ "agentGraphId": "graph-abc",+ "agentGraphVersion": {"lt": 5},+ "webhookId": {"not": None},+ "isDeleted": False,+ },+ data={"agentGraphVersion": 5},+ )+++async def test_migrate_returns_zero_when_no_matches(mock_prisma):+ mock_prisma.update_many = AsyncMock(return_value=0)++ count = await migrate_webhook_presets_to_new_version(+ user_id="user-123", graph_id="graph-abc", new_version=1+ )++ assert count == 0+++async def test_migrate_filters_correctly(mock_prisma):+ mock_prisma.update_many = AsyncMock(return_value=1)++ await migrate_webhook_presets_to_new_version(+ user_id="user-456", graph_id="graph-xyz", new_version=10+ )++ where = mock_prisma.update_many.call_args.kwargs["where"]+ assert where["webhookId"] == {"not": None}+ assert where["isDeleted"] is False+ assert where["userId"] == "user-456"+ assert where["agentGraphId"] == "graph-xyz"+ # Only strictly older versions should be migrated (not equal, not newer).+ assert where["agentGraphVersion"] == {"lt": 10}+++async def test_migrate_logs_when_presets_are_migrated(mock_prisma, caplog):+ """Exercise the ``count > 0`` log branch."""+ mock_prisma.update_many = AsyncMock(return_value=2)++ with caplog.at_level(logging.INFO, logger="backend.api.features.library.db"):+ count = await migrate_webhook_presets_to_new_version(+ user_id="user-789", graph_id="graph-log", new_version=4+ )++ assert count == 2+ # The function logs an INFO message when at least one preset is migrated.+ assert any(+ "Migrated 2 webhook preset(s)" in record.message for record in caplog.records+ )+++async def test_migrate_does_not_log_when_nothing_changes(mock_prisma, caplog):+ """The ``count > 0`` log branch is skipped when no presets matched."""+ mock_prisma.update_many = AsyncMock(return_value=0)++ with caplog.at_level(logging.INFO, logger="backend.api.features.library.db"):+ count = await migrate_webhook_presets_to_new_version(+ user_id="user-789", graph_id="graph-nolog", new_version=4+ )++ assert count == 0+ assert not any("Migrated" in record.message for record in caplog.records)+++# ---------------------------------------------------------------------------+# Call-site tests: verify the migration helper is invoked from every publish+# pathway when the new graph version carries a webhook input node, and is+# skipped when it does not.+# ---------------------------------------------------------------------------+++def _make_graph_mock(*, has_webhook: bool, version: int = 2, is_active: bool = True):+ """Return a stand-in for a GraphModel that satisfies the call-site code."""+ graph = MagicMock()+ graph.id = "graph-xyz"+ graph.version = version+ graph.is_active = is_active+ graph.webhook_input_node = object() if has_webhook else None+ # reassign_ids / validate_graph are sync methods on the real model; keep+ # the MagicMock default (returns MagicMock) so they don't trigger async+ # warnings.+ return graph+++async def test_update_graph_in_library_migrates_when_webhook_node_present(+ mocker,+):+ """``update_graph_in_library`` should call the migration helper."""+ from backend.api.features.library import db as library_db++ new_graph = _make_graph_mock(has_webhook=True)+ incoming = AsyncMock()+ incoming.id = new_graph.id++ mocker.patch.object(library_db.graph_db, "get_graph_all_versions", return_value=[])+ mocker.patch.object(library_db.graph_db, "make_graph_model", return_value=new_graph)+ mocker.patch.object(library_db.graph_db, "create_graph", return_value=new_graph)+ mocker.patch.object(+ library_db, "get_library_agent_by_graph_id", return_value=AsyncMock()+ )+ mocker.patch.object(+ library_db,+ "update_library_agent_version_and_settings",+ return_value=AsyncMock(),+ )+ mocker.patch.object(+ library_db, "on_graph_activate", side_effect=lambda g, user_id: g+ )+ mocker.patch.object(library_db, "on_graph_deactivate", return_value=None)+ mocker.patch.object(library_db.graph_db, "set_graph_active_version")+ migrate_mock = mocker.patch.object(+ library_db,+ "migrate_webhook_presets_to_new_version",+ return_value=1,+ )++ await library_db.update_graph_in_library(graph=incoming, user_id="user-1")++ migrate_mock.assert_awaited_once_with(+ user_id="user-1",+ graph_id=new_graph.id,+ new_version=new_graph.version,+ )+++async def test_update_graph_in_library_skips_when_no_webhook_node(mocker):+ """No migration call when the new graph has no webhook input node."""+ from backend.api.features.library import db as library_db++ new_graph = _make_graph_mock(has_webhook=False)+ incoming = AsyncMock()+ incoming.id = new_graph.id++ mocker.patch.object(library_db.graph_db, "get_graph_all_versions", return_value=[])+ mocker.patch.object(library_db.graph_db, "make_graph_model", return_value=new_graph)+ mocker.patch.object(library_db.graph_db, "create_graph", return_value=new_graph)+ mocker.patch.object(+ library_db, "get_library_agent_by_graph_id", return_value=AsyncMock()+ )+ mocker.patch.object(+ library_db,+ "update_library_agent_version_and_settings",+ return_value=AsyncMock(),+ )+ mocker.patch.object(+ library_db, "on_graph_activate", side_effect=lambda g, user_id: g+ )+ mocker.patch.object(library_db, "on_graph_deactivate", return_value=None)+ mocker.patch.object(library_db.graph_db, "set_graph_active_version")+ migrate_mock = mocker.patch.object(+ library_db,+ "migrate_webhook_presets_to_new_version",+ return_value=0,+ )++ await library_db.update_graph_in_library(graph=incoming, user_id="user-1")++ migrate_mock.assert_not_awaited()+++async def test_v1_update_graph_migrates_when_webhook_node_present(mocker):+ """The PUT /graphs/{id} route triggers migration on the new active version."""+ from backend.api.features import v1++ new_graph = _make_graph_mock(has_webhook=True, version=3)+ incoming = AsyncMock()+ incoming.id = new_graph.id++ existing_version = MagicMock()+ existing_version.version = new_graph.version - 1+ existing_version.is_active = True+ mocker.patch.object(+ v1.graph_db,+ "get_graph_all_versions",+ return_value=[existing_version],+ )+ mocker.patch.object(v1.graph_db, "make_graph_model", return_value=new_graph)+ mocker.patch.object(v1.graph_db, "create_graph", return_value=new_graph)+ mocker.patch.object(v1.graph_db, "set_graph_active_version")+ mocker.patch.object(v1.graph_db, "get_graph", return_value=new_graph)+ mocker.patch.object(+ v1.library_db,+ "update_library_agent_version_and_settings",+ return_value=AsyncMock(),+ )+ migrate_mock = mocker.patch.object(+ v1.library_db,+ "migrate_webhook_presets_to_new_version",+ return_value=1,+ )+ mocker.patch.object(v1, "on_graph_activate", side_effect=lambda g, user_id: g)+ mocker.patch.object(v1, "on_graph_deactivate", return_value=None)++ await v1.update_graph(graph_id=new_graph.id, graph=incoming, user_id="user-1")++ migrate_mock.assert_awaited_once_with(+ user_id="user-1",+ graph_id=new_graph.id,+ new_version=new_graph.version,+ )+++async def test_v1_update_graph_skips_when_no_webhook_node(mocker):+ """No migration call from PUT /graphs/{id} when graph has no webhook node."""+ from backend.api.features import v1++ new_graph = _make_graph_mock(has_webhook=False, version=3)+ incoming = AsyncMock()+ incoming.id = new_graph.id++ existing_version = MagicMock()+ existing_version.version = new_graph.version - 1+ existing_version.is_active = True+ mocker.patch.object(+ v1.graph_db,+ "get_graph_all_versions",+ return_value=[existing_version],+ )+ mocker.patch.object(v1.graph_db, "make_graph_model", return_value=new_graph)+ mocker.patch.object(v1.graph_db, "create_graph", return_value=new_graph)+ mocker.patch.object(v1.graph_db, "set_graph_active_version")+ mocker.patch.object(v1.graph_db, "get_graph", return_value=new_graph)+ mocker.patch.object(+ v1.library_db,+ "update_library_agent_version_and_settings",+ return_value=AsyncMock(),+ )+ migrate_mock = mocker.patch.object(+ v1.library_db,+ "migrate_webhook_presets_to_new_version",+ return_value=0,+ )+ mocker.patch.object(v1, "on_graph_activate", side_effect=lambda g, user_id: g)+ mocker.patch.object(v1, "on_graph_deactivate", return_value=None)++ await v1.update_graph(graph_id=new_graph.id, graph=incoming, user_id="user-1")++ migrate_mock.assert_not_awaited()+++async def test_v1_set_graph_active_version_migrates_when_webhook_node_present(+ mocker,+):+ """PUT /graphs/{id}/versions/active triggers migration on the activated version."""+ from backend.api.features import v1++ target_graph = _make_graph_mock(has_webhook=True, version=4)++ mocker.patch.object(+ v1.graph_db,+ "get_graph",+ side_effect=[target_graph, target_graph],+ )+ mocker.patch.object(v1.graph_db, "set_graph_active_version")+ mocker.patch.object(+ v1.library_db,+ "update_library_agent_version_and_settings",+ return_value=AsyncMock(),+ )+ migrate_mock = mocker.patch.object(+ v1.library_db,+ "migrate_webhook_presets_to_new_version",+ return_value=2,+ )+ mocker.patch.object(v1, "on_graph_activate", return_value=None)+ mocker.patch.object(v1, "on_graph_deactivate", return_value=None)++ body = v1.SetGraphActiveVersion(active_graph_version=target_graph.version)++ await v1.set_graph_active_version(+ graph_id=target_graph.id, request_body=body, user_id="user-1"+ )++ migrate_mock.assert_awaited_once_with(+ user_id="user-1",+ graph_id=target_graph.id,+ new_version=target_graph.version,+ )+++async def test_v1_set_graph_active_version_skips_when_no_webhook_node(mocker):+ """PUT /graphs/{id}/versions/active skips migration when no webhook node."""+ from backend.api.features import v1++ target_graph = _make_graph_mock(has_webhook=False, version=4)++ mocker.patch.object(+ v1.graph_db,+ "get_graph",+ side_effect=[target_graph, target_graph],+ )+ mocker.patch.object(v1.graph_db, "set_graph_active_version")+ mocker.patch.object(+ v1.library_db,+ "update_library_agent_version_and_settings",+ return_value=AsyncMock(),+ )+ migrate_mock = mocker.patch.object(+ v1.library_db,+ "migrate_webhook_presets_to_new_version",+ return_value=0,+ )+ mocker.patch.object(v1, "on_graph_activate", return_value=None)+ mocker.patch.object(v1, "on_graph_deactivate", return_value=None)++ body = v1.SetGraphActiveVersion(active_graph_version=target_graph.version)++ await v1.set_graph_active_version(+ graph_id=target_graph.id, request_body=body, user_id="user-1"+ )++ migrate_mock.assert_not_awaited()