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(api): fix the issue that workflow_runs.started_at is overwritten while resuming (#32851)
QuantumGhostd01acfc490d8
diff --git a/api/core/repositories/sqlalchemy_workflow_execution_repository.py b/api/core/repositories/sqlalchemy_workflow_execution_repository.pyindex 649e2f7358f..770df8b050d 100644--- a/api/core/repositories/sqlalchemy_workflow_execution_repository.py+++ b/api/core/repositories/sqlalchemy_workflow_execution_repository.py@@ -194,6 +194,13 @@ class SQLAlchemyWorkflowExecutionRepository(WorkflowExecutionRepository):# Create a new database sessionwith self._session_factory() as session:+ existing_model = session.get(WorkflowRun, db_model.id)+ if existing_model:+ if existing_model.tenant_id != self._tenant_id:+ raise ValueError("Unauthorized access to workflow run")+ # Preserve the original start time for pause/resume flows.+ db_model.created_at = existing_model.created_at+# SQLAlchemy merge intelligently handles both insert and update operations# based on the presence of the primary keysession.merge(db_model)diff --git a/api/migrations/env.py b/api/migrations/env.pyindex 66a4614e80e..3b1fa7bb89d 100644--- a/api/migrations/env.py+++ b/api/migrations/env.py@@ -66,6 +66,7 @@ def run_migrations_offline():context.configure(url=url, target_metadata=get_metadata(), literal_binds=True)+ logger.info("Generating offline migration SQL with url: %s", url)with context.begin_transaction():context.run_migrations()diff --git a/api/pytest.ini b/api/pytest.iniindex 4a9470fa0cb..588dafe7eba 100644--- a/api/pytest.ini+++ b/api/pytest.ini@@ -1,5 +1,6 @@[pytest]-addopts = --cov=./api --cov-report=json+pythonpath = .+addopts = --cov=./api --cov-report=json --import-mode=importlibenv =ANTHROPIC_API_KEY = sk-ant-api11-IamNotARealKeyJustForMockTestKawaiiiiiiiiii-NotBaka-ASkkszAZURE_OPENAI_API_BASE = https://difyai-openai.openai.azure.com@@ -19,7 +20,7 @@ env =GOOGLE_API_KEY = abcdefghijklmnopqrstuvwxyzHUGGINGFACE_API_KEY = hf-awuwuwuwuwuwuwuwuwuwuwuwuwuwuwuwuwuHUGGINGFACE_EMBEDDINGS_ENDPOINT_URL = c- HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL = b+ HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL = bHUGGINGFACE_TEXT_GEN_ENDPOINT_URL = aMIXEDBREAD_API_KEY = mk-aaaaaaaaaaaaaaaaaaaaMOCK_SWITCH = truediff --git a/api/tests/integration_tests/controllers/console/app/test_description_validation.py b/api/tests/integration_tests/controllers/console/app/test_description_validation.pyindex 8160807e482..f36c596eb84 100644--- a/api/tests/integration_tests/controllers/console/app/test_description_validation.py+++ b/api/tests/integration_tests/controllers/console/app/test_description_validation.py@@ -5,14 +5,10 @@ This test module validates the 400-character limit enforcementfor App descriptions across all creation and editing endpoints."""-import osimport sysimport pytest-# Add the API root to Python path for imports-sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", ".."))-class TestAppDescriptionValidationUnit:"""Unit tests for description validation function"""diff --git a/api/tests/test_containers_integration_tests/conftest.py b/api/tests/test_containers_integration_tests/conftest.pyindex d6d2d303057..2a23f1ea7da 100644--- a/api/tests/test_containers_integration_tests/conftest.py+++ b/api/tests/test_containers_integration_tests/conftest.py@@ -10,8 +10,11 @@ more reliable and realistic test scenarios.import loggingimport osfrom collections.abc import Generator+from contextlib import contextmanagerfrom pathlib import Path+from typing import Protocol, TypeVar+import psycopg2import pytestfrom flask import Flaskfrom flask.testing import FlaskClient@@ -31,6 +34,25 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levellogger = logging.getLogger(__name__)+class _CloserProtocol(Protocol):+ """_Closer is any type which implement the close() method."""++ def close(self):+ """close the current object, release any external resouece (file, transaction, connection etc.)+ associated with it.+ """+ pass+++_Closer = TypeVar("_Closer", bound=_CloserProtocol)+++@contextmanager+def _auto_close(closer: _Closer) -> Generator[_Closer, None, None]:+ yield closer+ closer.close()++class DifyTestContainers:"""Manages all test containers required for Dify integration tests.@@ -97,45 +119,28 @@ class DifyTestContainers:wait_for_logs(self.postgres, "is ready to accept connections", timeout=30)logger.info("PostgreSQL container is ready and accepting connections")- # Install uuid-ossp extension for UUID generation- logger.info("Installing uuid-ossp extension...")- try:- import psycopg2-- conn = psycopg2.connect(- host=db_host,- port=db_port,- user=self.postgres.username,- password=self.postgres.password,- database=self.postgres.dbname,- )- conn.autocommit = True- cursor = conn.cursor()- cursor.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')- cursor.close()- conn.close()+ conn = psycopg2.connect(+ host=db_host,+ port=db_port,+ user=self.postgres.username,+ password=self.postgres.password,+ database=self.postgres.dbname,+ )+ conn.autocommit = True+ with _auto_close(conn):+ with conn.cursor() as cursor:+ # Install uuid-ossp extension for UUID generation+ logger.info("Installing uuid-ossp extension...")+ cursor.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')logger.info("uuid-ossp extension installed successfully")- except Exception as e:- logger.warning("Failed to install uuid-ossp extension: %s", e)- # Create plugin database for dify-plugin-daemon- logger.info("Creating plugin database...")- try:- conn = psycopg2.connect(- host=db_host,- port=db_port,- user=self.postgres.username,- password=self.postgres.password,- database=self.postgres.dbname,- )- conn.autocommit = True- cursor = conn.cursor()- cursor.execute("CREATE DATABASE dify_plugin;")- cursor.close()- conn.close()+ # NOTE: We cannot use `with conn.cursor() as cursor:` as it will wrap the statement+ # inside a transaction. However, the `CREATE DATABASE` statement cannot run inside a transaction block.+ with _auto_close(conn.cursor()) as cursor:+ # Create plugin database for dify-plugin-daemon+ logger.info("Creating plugin database...")+ cursor.execute("CREATE DATABASE dify_plugin;")logger.info("Plugin database created successfully")- except Exception as e:- logger.warning("Failed to create plugin database: %s", e)# Set up storage environment variablesos.environ.setdefault("STORAGE_TYPE", "opendal")@@ -258,23 +263,16 @@ class DifyTestContainers:containers = [self.redis, self.postgres, self.dify_sandbox, self.dify_plugin_daemon]for container in containers:if container:- try:- container_name = container.image- logger.info("Stopping container: %s", container_name)- container.stop()- logger.info("Successfully stopped container: %s", container_name)- except Exception as e:- # Log error but don't fail the test cleanup- logger.warning("Failed to stop container %s: %s", container, e)+ container_name = container.image+ logger.info("Stopping container: %s", container_name)+ container.stop()+ logger.info("Successfully stopped container: %s", container_name)# Stop and remove the networkif self.network:- try:- logger.info("Removing Docker network...")- self.network.remove()- logger.info("Successfully removed Docker network")- except Exception as e:- logger.warning("Failed to remove Docker network: %s", e)+ logger.info("Removing Docker network...")+ self.network.remove()+ logger.info("Successfully removed Docker network")self._containers_started = Falselogger.info("All test containers stopped and cleaned up successfully")diff --git a/api/tests/unit_tests/conftest.py b/api/tests/unit_tests/conftest.pyindex d2111ebac8a..3f75fd2851f 100644--- a/api/tests/unit_tests/conftest.py+++ b/api/tests/unit_tests/conftest.py@@ -32,11 +32,6 @@ os.environ.setdefault("OPENDAL_SCHEME", "fs")os.environ.setdefault("OPENDAL_FS_ROOT", "/tmp/dify-storage")os.environ.setdefault("STORAGE_TYPE", "opendal")-# Add the API directory to Python path to ensure proper imports-import sys--sys.path.insert(0, PROJECT_DIR)-from core.db.session_factory import configure_session_factory, session_factoryfrom extensions import ext_redisdiff --git a/api/tests/unit_tests/core/app/apps/test_pause_resume.py b/api/tests/unit_tests/core/app/apps/test_pause_resume.pyindex 44af89601c9..e019a4b977d 100644--- a/api/tests/unit_tests/core/app/apps/test_pause_resume.py+++ b/api/tests/unit_tests/core/app/apps/test_pause_resume.py@@ -1,13 +1,8 @@import sysimport time-from pathlib import Pathfrom types import ModuleType, SimpleNamespacefrom typing import Any-API_DIR = str(Path(__file__).resolve().parents[5])-if API_DIR not in sys.path:- sys.path.insert(0, API_DIR)-import dify_graph.nodes.human_input.entities # noqa: F401from core.app.apps.advanced_chat import app_generator as adv_app_gen_modulefrom core.app.apps.workflow import app_generator as wf_app_gen_modulediff --git a/api/tests/unit_tests/core/repositories/test_sqlalchemy_workflow_execution_repository.py b/api/tests/unit_tests/core/repositories/test_sqlalchemy_workflow_execution_repository.pynew file mode 100644index 00000000000..c66e50437ab--- /dev/null+++ b/api/tests/unit_tests/core/repositories/test_sqlalchemy_workflow_execution_repository.py@@ -0,0 +1,84 @@+from datetime import datetime+from unittest.mock import MagicMock+from uuid import uuid4++from sqlalchemy import create_engine+from sqlalchemy.orm import sessionmaker++from core.repositories.sqlalchemy_workflow_execution_repository import SQLAlchemyWorkflowExecutionRepository+from dify_graph.entities.workflow_execution import WorkflowExecution, WorkflowType+from models import Account, WorkflowRun+from models.enums import WorkflowRunTriggeredFrom+++def _build_repository_with_mocked_session(session: MagicMock) -> SQLAlchemyWorkflowExecutionRepository:+ engine = create_engine("sqlite:///:memory:")+ real_session_factory = sessionmaker(bind=engine, expire_on_commit=False)++ user = MagicMock(spec=Account)+ user.id = str(uuid4())+ user.current_tenant_id = str(uuid4())++ repository = SQLAlchemyWorkflowExecutionRepository(+ session_factory=real_session_factory,+ user=user,+ app_id="app-id",+ triggered_from=WorkflowRunTriggeredFrom.APP_RUN,+ )++ session_context = MagicMock()+ session_context.__enter__.return_value = session+ session_context.__exit__.return_value = False+ repository._session_factory = MagicMock(return_value=session_context)+ return repository+++def _build_execution(*, execution_id: str, started_at: datetime) -> WorkflowExecution:+ return WorkflowExecution.new(+ id_=execution_id,+ workflow_id="workflow-id",+ workflow_type=WorkflowType.WORKFLOW,+ workflow_version="1.0.0",+ graph={"nodes": [], "edges": []},+ inputs={"query": "hello"},+ started_at=started_at,+ )+++def test_save_uses_execution_started_at_when_record_does_not_exist():+ session = MagicMock()+ session.get.return_value = None+ repository = _build_repository_with_mocked_session(session)++ started_at = datetime(2026, 1, 1, 12, 0, 0)+ execution = _build_execution(execution_id=str(uuid4()), started_at=started_at)++ repository.save(execution)++ saved_model = session.merge.call_args.args[0]+ assert saved_model.created_at == started_at+ session.commit.assert_called_once()+++def test_save_preserves_existing_created_at_when_record_already_exists():+ session = MagicMock()+ repository = _build_repository_with_mocked_session(session)++ execution_id = str(uuid4())+ existing_created_at = datetime(2026, 1, 1, 12, 0, 0)+ existing_run = WorkflowRun()+ existing_run.id = execution_id+ existing_run.tenant_id = repository._tenant_id+ existing_run.created_at = existing_created_at+ session.get.return_value = existing_run++ execution = _build_execution(+ execution_id=execution_id,+ started_at=datetime(2026, 1, 1, 12, 30, 0),+ )++ repository.save(execution)++ saved_model = session.merge.call_args.args[0]+ assert saved_model.created_at == existing_created_at+ session.commit.assert_called_once()diff --git a/api/tests/unit_tests/core/workflow/graph_engine/test_mock_iteration_simple.py b/api/tests/unit_tests/core/workflow/graph_engine/test_mock_iteration_simple.pyindex eb449e6d753..8c8e5977c8e 100644--- a/api/tests/unit_tests/core/workflow/graph_engine/test_mock_iteration_simple.py+++ b/api/tests/unit_tests/core/workflow/graph_engine/test_mock_iteration_simple.py@@ -2,15 +2,7 @@Simple test to verify MockNodeFactory works with iteration nodes."""-import sys-from pathlib import Path-from dify_graph.entities.graph_init_params import DIFY_RUN_CONTEXT_KEY--# Add api directory to path-api_dir = Path(__file__).parent.parent.parent.parent.parent.parent-sys.path.insert(0, str(api_dir))-from dify_graph.enums import NodeTypefrom tests.unit_tests.core.workflow.graph_engine.test_mock_config import MockConfigBuilderfrom tests.unit_tests.core.workflow.graph_engine.test_mock_factory import MockNodeFactorydiff --git a/api/tests/unit_tests/core/workflow/graph_engine/test_mock_simple.py b/api/tests/unit_tests/core/workflow/graph_engine/test_mock_simple.pyindex 84d14445858..693cdf92768 100644--- a/api/tests/unit_tests/core/workflow/graph_engine/test_mock_simple.py+++ b/api/tests/unit_tests/core/workflow/graph_engine/test_mock_simple.py@@ -3,14 +3,8 @@ Simple test to validate the auto-mock system without external dependencies."""import sys-from pathlib import Pathfrom dify_graph.entities.graph_init_params import DIFY_RUN_CONTEXT_KEY--# Add api directory to path-api_dir = Path(__file__).parent.parent.parent.parent.parent.parent-sys.path.insert(0, str(api_dir))-from dify_graph.enums import NodeTypefrom tests.unit_tests.core.workflow.graph_engine.test_mock_config import MockConfig, MockConfigBuilder, NodeMockConfigfrom tests.unit_tests.core.workflow.graph_engine.test_mock_factory import MockNodeFactory