Skip to content

armgit / dify

Public Imported from GitHub
0

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.

test: persist human input forms across graph pause and resume (#43205)

Asuka Minatob4ae43986c28
diff --git a/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py b/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py
index c5cc7a37cbd..27d0b452456 100644
--- a/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py
+++ b/api/tests/test_containers_integration_tests/core/workflow/test_human_input_resume_node_execution.py
@@ -1,7 +1,5 @@
import time
import uuid
-from datetime import timedelta
-from unittest.mock import MagicMock
import pytest
from sqlalchemy import delete, select
@@ -10,7 +8,7 @@ from sqlalchemy.orm import Session
from core.app.app_config.entities import WorkflowUIBasedAppConfig
from core.app.entities.app_invoke_entities import InvokeFrom, WorkflowAppGenerateEntity
from core.app.workflow.layers import PersistenceWorkflowInfo, WorkflowPersistenceLayer
-from core.repositories.human_input_repository import HumanInputFormEntity, HumanInputFormRepository
+from core.repositories.human_input_repository import HumanInputFormRepository, HumanInputFormRepositoryImpl
from core.repositories.sqlalchemy_workflow_execution_repository import SQLAlchemyWorkflowExecutionRepository
from core.repositories.sqlalchemy_workflow_node_execution_repository import SQLAlchemyWorkflowNodeExecutionRepository
from core.workflow.nodes.human_input.callback import (
@@ -33,40 +31,12 @@ from libs.datetime_utils import naive_utc_now
from models import Account
from models.account import AccountStatus, Tenant, TenantAccountJoin, TenantAccountRole, TenantStatus
from models.enums import CreatorUserRole, WorkflowRunTriggeredFrom
+from models.human_input import HumanInputDelivery, HumanInputForm, HumanInputFormRecipient
from models.model import App, AppMode, IconType
from models.workflow import Workflow, WorkflowNodeExecutionModel, WorkflowNodeExecutionTriggeredFrom, WorkflowRun
from tests.workflow_test_utils import build_test_graph_init_params
-def _mock_form_repository_without_submission() -> HumanInputFormRepository:
- repo = MagicMock(spec=HumanInputFormRepository)
- form_entity = MagicMock(spec=HumanInputFormEntity)
- form_entity.id = "test-form-id"
- form_entity.submission_token = "test-form-token"
- form_entity.recipients = []
- form_entity.rendered_content = "rendered"
- form_entity.submitted = False
- repo.create_form.return_value = form_entity
- repo.get_form.return_value = None
- return repo
-
-
-def _mock_form_repository_with_submission(action_id: str) -> HumanInputFormRepository:
- repo = MagicMock(spec=HumanInputFormRepository)
- form_entity = MagicMock(spec=HumanInputFormEntity)
- form_entity.id = "test-form-id"
- form_entity.submission_token = "test-form-token"
- form_entity.recipients = []
- form_entity.rendered_content = "rendered"
- form_entity.submitted = True
- form_entity.selected_action_id = action_id
- form_entity.submitted_data = {}
- form_entity.status = HumanInputFormStatus.WAITING
- form_entity.expiration_time = naive_utc_now() + timedelta(hours=1)
- repo.get_form.return_value = form_entity
- return repo
-
-
def _build_runtime_state(workflow_execution_id: str, app_id: str, workflow_id: str, user_id: str) -> GraphRuntimeState:
variable_pool = VariablePool.from_bootstrap(
system_variables=build_system_variables(
@@ -248,6 +218,16 @@ class TestHumanInputResumeNodeExecutionIntegration:
yield
+ form_ids = select(HumanInputForm.id).where(
+ HumanInputForm.tenant_id == self.tenant.id, HumanInputForm.app_id == self.app.id
+ )
+ self.session.execute(delete(HumanInputFormRecipient).where(HumanInputFormRecipient.form_id.in_(form_ids)))
+ self.session.execute(delete(HumanInputDelivery).where(HumanInputDelivery.form_id.in_(form_ids)))
+ self.session.execute(
+ delete(HumanInputForm).where(
+ HumanInputForm.tenant_id == self.tenant.id, HumanInputForm.app_id == self.app.id
+ )
+ )
self.session.execute(delete(WorkflowNodeExecutionModel))
self.session.execute(delete(WorkflowRun))
self.session.execute(delete(Workflow).where(Workflow.id == self.workflow.id))
@@ -310,27 +290,42 @@ class TestHumanInputResumeNodeExecutionIntegration:
workflow_id=self.workflow.id,
user_id=self.account.id,
)
- pause_repo = _mock_form_repository_without_submission()
+ form_repository = HumanInputFormRepositoryImpl(
+ tenant_id=self.tenant.id, app_id=self.app.id, workflow_execution_id=execution_id
+ )
paused_graph = _build_graph(
runtime_state,
self.tenant.id,
self.app.id,
self.workflow.id,
self.account.id,
- pause_repo,
+ form_repository,
)
self._run_graph(paused_graph, runtime_state, execution_id)
snapshot = runtime_state.dumps()
resumed_state = GraphRuntimeState.from_snapshot(snapshot)
- resume_repo = _mock_form_repository_with_submission(action_id="continue")
+ form = self.session.scalar(
+ select(HumanInputForm).where(
+ HumanInputForm.tenant_id == self.tenant.id,
+ HumanInputForm.workflow_run_id == execution_id,
+ HumanInputForm.node_id == "human",
+ )
+ )
+ assert form is not None
+ assert form.status == HumanInputFormStatus.WAITING
+ form.selected_action_id = "continue"
+ form.submitted_data = "{}"
+ form.submitted_at = naive_utc_now()
+ form.status = HumanInputFormStatus.SUBMITTED
+ self.session.commit()
resumed_graph = _build_graph(
resumed_state,
self.tenant.id,
self.app.id,
self.workflow.id,
self.account.id,
- resume_repo,
+ form_repository,
)
self._run_graph(resumed_graph, resumed_state, execution_id)
diff --git a/api/tests/unit_tests/core/workflow/test_response_stream_filter_pause_resume.py b/api/tests/unit_tests/core/workflow/test_response_stream_filter_pause_resume.py
index e3267cc8466..02993ac9043 100644
--- a/api/tests/unit_tests/core/workflow/test_response_stream_filter_pause_resume.py
+++ b/api/tests/unit_tests/core/workflow/test_response_stream_filter_pause_resume.py
@@ -6,14 +6,14 @@ answer nodes downstream of a pre-pause branch never unlock for streaming on
resume, even though the graph executes correctly.
"""
-from datetime import timedelta
-from unittest.mock import MagicMock
+from pytest_mock import MockerFixture
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
from core.app.entities.app_invoke_entities import InvokeFrom, UserFrom
from core.repositories.human_input_repository import (
- HumanInputFormEntity,
- HumanInputFormRecipientEntity,
HumanInputFormRepository,
+ HumanInputFormRepositoryImpl,
)
from core.workflow.nodes.human_input.callback import DifyHITLCallback
from core.workflow.nodes.human_input.entities import HumanInputNodeData, UserActionConfig
@@ -36,40 +36,12 @@ from graphon.nodes.start.start_node import StartNode
from graphon.runtime import GraphRuntimeState, VariablePool
from graphon.utils.condition.entities import Condition
from libs.datetime_utils import naive_utc_now
+from models.human_input import HumanInputForm
from tests.workflow_test_utils import build_test_graph_init_params
WORKFLOW_EXECUTION_ID = "wf-exec-38525"
-def _mock_repo_paused() -> HumanInputFormRepository:
- repo = MagicMock(spec=HumanInputFormRepository)
- form = MagicMock(spec=HumanInputFormEntity)
- form.id = "form-1"
- form.submission_token = "token-1"
- form.recipients = list[HumanInputFormRecipientEntity]()
- form.rendered_content = "rendered"
- form.submitted = False
- repo.create_form.return_value = form
- repo.get_form.return_value = None
- return repo
-
-
-def _mock_repo_resumed(action_id: str = "continue") -> HumanInputFormRepository:
- repo = MagicMock(spec=HumanInputFormRepository)
- form = MagicMock(spec=HumanInputFormEntity)
- form.id = "form-1"
- form.submission_token = "token-1"
- form.recipients = list[HumanInputFormRecipientEntity]()
- form.rendered_content = "rendered"
- form.submitted = True
- form.selected_action_id = action_id
- form.submitted_data = dict[str, object]()
- form.status = HumanInputFormStatus.WAITING
- form.expiration_time = naive_utc_now() + timedelta(hours=1)
- repo.get_form.return_value = form
- return repo
-
-
def _build_graph(runtime_state: GraphRuntimeState, form_repository: HumanInputFormRepository) -> Graph:
params = build_test_graph_init_params(
workflow_id="wf",
@@ -169,10 +141,19 @@ def _build_runtime_state() -> GraphRuntimeState:
return GraphRuntimeState(variable_pool=variable_pool, start_at=0.0)
-def test_if_else_human_input_pause_resume_answer_chunks_survive_resume() -> None:
+def test_if_else_human_input_pause_resume_answer_chunks_survive_resume(
+ sqlite_session_factory: sessionmaker[Session],
+ mocker: MockerFixture,
+) -> None:
+ mocker.patch(
+ "core.repositories.human_input_repository.session_factory.create_session", side_effect=sqlite_session_factory
+ )
+ repository = HumanInputFormRepositoryImpl(
+ tenant_id="tenant", app_id="app", workflow_execution_id=WORKFLOW_EXECUTION_ID
+ )
# ---- Phase 1: run to GraphRunPausedEvent ----
runtime_state_1 = _build_runtime_state()
- graph_1 = _build_graph(runtime_state_1, _mock_repo_paused())
+ graph_1 = _build_graph(runtime_state_1, repository)
engine_1 = GraphEngine(
workflow_id="wf",
graph=graph_1,
@@ -196,9 +177,18 @@ def test_if_else_human_input_pause_resume_answer_chunks_survive_resume() -> None
response_filter_snapshot = filter_1.dumps()
runtime_snapshot = runtime_state_1.dumps()
+ with sqlite_session_factory.begin() as session:
+ form = session.scalar(select(HumanInputForm).where(HumanInputForm.workflow_run_id == WORKFLOW_EXECUTION_ID))
+ assert form is not None
+ assert form.status == HumanInputFormStatus.WAITING
+ form.selected_action_id = "continue"
+ form.submitted_data = "{}"
+ form.submitted_at = naive_utc_now()
+ form.status = HumanInputFormStatus.SUBMITTED
+
# ---- Phase 2: rebuild engine + filter from snapshots, resume to completion ----
runtime_state_2 = GraphRuntimeState.from_snapshot(runtime_snapshot)
- graph_2 = _build_graph(runtime_state_2, _mock_repo_resumed(action_id="continue"))
+ graph_2 = _build_graph(runtime_state_2, repository)
engine_2 = GraphEngine(
workflow_id="wf",
graph=graph_2,