From 3dcb06db0154318e72d2e315b2a89aadbc981647 Mon Sep 17 00:00:00 2001 From: Charles Yao Date: Thu, 3 Sep 2026 23:45:15 -0700 Subject: [PATCH] fix(dify-builder): exclude in-batch delete_node ids from apply_repair idempotency filter apply_repair's already-present filter (added for interrupted-step Retry idempotency) compared create_node/connect intents against before_graph alone, with no awareness of delete_node intents in the same batch. A from-scratch build sends delete_node(placeholder_start) + create_node(same id) in one apply_repair call to replace the canvas's default placeholder start with the generator's own start node -- the filter dropped the create_node as "already present" while the delete_node still ran, deleting the start node with no re-create, so the following connect raised ValueError: node not found. Mirror handlers_build.py's existing M2 guard: subtract in-batch delete_node targets from the present-node/edge sets before filtering, so a delete+recreate of the same id in one batch survives. Co-Authored-By: Claude Opus 4.8 --- api/services/dify_builder/dify_port.py | 19 +++++-- .../services/dify_builder/test_dify_port.py | 49 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/api/services/dify_builder/dify_port.py b/api/services/dify_builder/dify_port.py index 17a9a132d28..6943133960c 100644 --- a/api/services/dify_builder/dify_port.py +++ b/api/services/dify_builder/dify_port.py @@ -211,10 +211,23 @@ class WorkflowServiceDifyPort: # present so the re-apply is a clean no-op. Uses the ORIGINAL draft graph # (before_graph) as the reference, so an in-batch duplicate of a NEW id # still reaches graph_ops and raises (validation preserved). - _present_node_ids = {n.get("id") for n in before_graph.get("nodes", [])} - _present_edges = {(e.get("source"), e.get("target")) for e in before_graph.get("edges", [])} + # + # Mirror handlers_build.py's M2 guard: ids targeted by a delete_node in + # THIS SAME batch must not count as "already present" for the filter + # above. A from-scratch build sends delete_node(placeholder_start) + + # create_node(same id) in one batch (recreating the id the placeholder + # occupied); without this exclusion the create_node would be dropped as + # already-present while the delete_node still runs, deleting the node + # with no re-create. + _deleted_ids = {i.args.get("node_id") for i in intents if i.op == "delete_node"} + _present_node_ids = {n.get("id") for n in before_graph.get("nodes", [])} - _deleted_ids + _present_edges = { + (e.get("source"), e.get("target")) + for e in before_graph.get("edges", []) + if e.get("source") not in _deleted_ids and e.get("target") not in _deleted_ids + } - def _already_present(intent) -> bool: + def _already_present(intent: MutationIntent) -> bool: if intent.op == "create_node": return intent.args.get("node_id") in _present_node_ids if intent.op == "connect": diff --git a/api/tests/unit_tests/services/dify_builder/test_dify_port.py b/api/tests/unit_tests/services/dify_builder/test_dify_port.py index 2055a8cdf3a..20cf2a28896 100644 --- a/api/tests/unit_tests/services/dify_builder/test_dify_port.py +++ b/api/tests/unit_tests/services/dify_builder/test_dify_port.py @@ -469,6 +469,55 @@ def test_apply_repair_skips_already_present_create_and_connect(mock_session: Mag mock_ws_cls.return_value.sync_draft_workflow.assert_not_called() +def test_apply_repair_survives_delete_and_recreate_of_same_id_in_one_batch(mock_session: MagicMock): + """A from-scratch build (handlers_build.handle_plan_approval) sends + delete_node(placeholder_start) + create_node(same id) in ONE batch, to + replace the canvas's default placeholder start with the generator's own + start node of the same id. The already-present filter above must NOT + treat "start" as already-present just because it's in before_graph -- + that would drop the create_node while the delete_node still runs, + deleting the start node with no re-create, and the subsequent connect + would then raise ValueError('node not found: start').""" + account = SimpleNamespace(id="acc-1") + app = SimpleNamespace(id="app-1", tenant_id="tenant-1") + _configure_session_get(mock_session, account=account, app=app) + + # Draft has only the canvas's default placeholder start node "start". + existing_graph = { + "nodes": [{"id": "start", "data": {"type": "start"}}], + "edges": [], + } + workflow = SimpleNamespace( + graph_dict=existing_graph, + unique_hash="h0", + features_dict={}, + conversation_variables=[], + ) + updated_workflow = SimpleNamespace(unique_hash="h1") + intents = [ + MutationIntent(op="delete_node", args={"node_id": "start"}), + MutationIntent(op="create_node", args={"node_type": "start", "node_id": "start", "config": {}}), + MutationIntent(op="create_node", args={"node_type": "llm", "node_id": "llm_1", "config": {}}), + MutationIntent(op="connect", args={"from_node": "start", "to_node": "llm_1"}), + ] + + with patch("services.dify_builder.dify_port.WorkflowService") as mock_ws_cls: + mock_ws_cls.return_value.get_draft_workflow.return_value = workflow + mock_ws_cls.return_value.sync_draft_workflow.return_value = updated_workflow + + # Must not raise -- the recreate of "start" must survive the filter. + result = WorkflowServiceDifyPort().apply_repair("app-1", _actor(), intents) + + assert result.new_hash == "h1" + _, kwargs = mock_ws_cls.return_value.sync_draft_workflow.call_args + synced_nodes = {n["id"] for n in kwargs["graph"]["nodes"]} + synced_edges = {(e["source"], e["target"]) for e in kwargs["graph"]["edges"]} + # "start" must still be present (recreated, not left deleted) alongside the new node. + assert synced_nodes == {"start", "llm_1"} + # The connect from the recreated "start" to "llm_1" must have applied. + assert ("start", "llm_1") in synced_edges + + # ---- run_draft --------------------------------------------------------------