From d8a3eca374294bbad22bf200241030725e3b643b Mon Sep 17 00:00:00 2001 From: Kip Date: Wed, 4 Nov 2026 22:45:17 +0800 Subject: [PATCH 1/3] Python: continue declarative workflows after an If with an empty then Add a ThenPassthrough continuation node for empty then branches, mirroring the existing ElsePassthrough path, and cover graph wiring and condition routing with regression tests. Co-Authored-By: Claude Opus 5 --- .../_workflows/_declarative_builder.py | 16 ++++- .../declarative/tests/test_graph_coverage.py | 67 +++++++++++++++++++ .../tests/test_workflow_generated_ids.py | 4 +- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py index 9e7a18711e6..c60d29bb15f 100644 --- a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py +++ b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py @@ -619,6 +619,13 @@ def _create_if_structure( # Create then branch then_actions = action_def.get("then", action_def.get("actions", [])) then_entry = self._create_executors_for_actions(then_actions, builder, branch_context) + then_passthrough = None + if then_entry is None: + # Empty then branch - create a passthrough for continuation when condition is true + then_passthrough = JoinExecutor( + {"kind": "ThenPassthrough"}, id=self._allocate_generated_id(f"{action_id}_then_pass") + ) + self._executors[then_passthrough.id] = then_passthrough # Create else branch else_actions = action_def.get("else", []) @@ -641,6 +648,12 @@ def _create_if_structure( target=then_target, condition=lambda msg: isinstance(msg, ConditionResult) and msg.branch_index == 0, ) + elif then_passthrough: + builder.add_edge( + source=evaluator, + target=then_passthrough, + condition=lambda msg: isinstance(msg, ConditionResult) and msg.branch_index == 0, + ) if else_entry: else_target = self._get_structure_entry(else_entry) builder.add_edge( @@ -656,7 +669,7 @@ def _create_if_structure( ) # Get branch exit executors for later wiring to successor - then_exit = self._get_branch_exit(then_entry) + then_exit = self._get_branch_exit(then_entry) if then_entry else then_passthrough else_exit = self._get_branch_exit(else_entry) if else_entry else else_passthrough # Collect all branch exits (for wiring to successor) @@ -672,6 +685,7 @@ def __init__(self) -> None: self.id = action_id self.evaluator = evaluator # The entry point for this structure self.then_entry = then_entry + self.then_passthrough = then_passthrough self.else_entry = else_entry self.else_passthrough = else_passthrough self.branch_exits = branch_exits # All exits that need wiring to successor diff --git a/python/packages/declarative/tests/test_graph_coverage.py b/python/packages/declarative/tests/test_graph_coverage.py index 7187b317f8c..c1a0f0da70e 100644 --- a/python/packages/declarative/tests/test_graph_coverage.py +++ b/python/packages/declarative/tests/test_graph_coverage.py @@ -4,6 +4,7 @@ # pyright: reportPrivateUsage=false, reportUnknownVariableType=false # pyright: reportGeneralTypeIssues=false +import asyncio from dataclasses import dataclass from typing import Any, cast from unittest.mock import AsyncMock, MagicMock @@ -2572,6 +2573,72 @@ def test_foreach_advance_edge_with_if_as_last_body_action(self): assert ("else_action", "loop_next") in edges assert ("step_1", "loop_next") not in edges + def test_empty_then_branch_wires_continuation_passthrough(self): + """An empty then branch must still continue to the following action.""" + from agent_framework_declarative._workflows import DeclarativeWorkflowBuilder + + yaml_def = { + "name": "empty_then_continuation", + "actions": [ + {"kind": "If", "id": "check", "condition": "=true", "then": []}, + {"kind": "SendActivity", "id": "after", "activity": {"text": "after"}}, + ], + } + + workflow = DeclarativeWorkflowBuilder(yaml_def).build() + edges = {(e.source_id, e.target_id) for group in workflow.edge_groups for e in group.edges} + + assert ("check_eval", "check_then_pass") in edges + assert ("check_then_pass", "after") in edges + assert ("check_eval", "check_else_pass") in edges + assert ("check_else_pass", "after") in edges + + from agent_framework_declarative._workflows._executors_control_flow import ELSE_BRANCH_INDEX + + then_edge = next( + edge + for group in workflow.edge_groups + for edge in group.edges + if edge.source_id == "check_eval" and edge.target_id == "check_then_pass" + ) + else_edge = next( + edge + for group in workflow.edge_groups + for edge in group.edges + if edge.source_id == "check_eval" and edge.target_id == "check_else_pass" + ) + then_branch = ConditionResult(matched=True, branch_index=0, value=True) + else_branch = ConditionResult(matched=False, branch_index=ELSE_BRANCH_INDEX, value=False) + + assert asyncio.run(then_edge.should_route(then_branch)) is True + assert asyncio.run(then_edge.should_route(else_branch)) is False + assert asyncio.run(else_edge.should_route(else_branch)) is True + assert asyncio.run(else_edge.should_route(then_branch)) is False + + def test_non_empty_then_branch_does_not_add_passthrough(self): + """A non-empty then branch should keep wiring directly to its action.""" + from agent_framework_declarative._workflows import DeclarativeWorkflowBuilder + + yaml_def = { + "name": "non_empty_then", + "actions": [ + { + "kind": "If", + "id": "check", + "condition": "=true", + "then": [{"kind": "SendActivity", "id": "then_action", "activity": {"text": "then"}}], + }, + {"kind": "SendActivity", "id": "after", "activity": {"text": "after"}}, + ], + } + + workflow = DeclarativeWorkflowBuilder(yaml_def).build() + edges = {(e.source_id, e.target_id) for group in workflow.edge_groups for e in group.edges} + + assert ("check_eval", "then_action") in edges + assert ("then_action", "after") in edges + assert ("check_then_pass", "after") not in edges + def test_wire_to_target_with_if_structure(self): """Test wiring to an If structure routes to evaluator.""" from agent_framework import WorkflowBuilder diff --git a/python/packages/declarative/tests/test_workflow_generated_ids.py b/python/packages/declarative/tests/test_workflow_generated_ids.py index 3951c2de301..e2169f34be9 100644 --- a/python/packages/declarative/tests/test_workflow_generated_ids.py +++ b/python/packages/declarative/tests/test_workflow_generated_ids.py @@ -271,7 +271,7 @@ async def test_nullable_inactive_if_branch_remains_supported(null_then: bool) -> }) expected_ids = {"_workflow_entry", "branch_eval", "child"} - if not null_then: - expected_ids.add("branch_else_pass") + # An empty branch gets a passthrough so it can continue to the next action. + expected_ids.add("branch_then_pass" if null_then else "branch_else_pass") assert set(workflow.executors) == expected_ids assert (await workflow.run({})).get_outputs() == ["inside"] From 92c901c12c632bac08192b078a000473aaac03d0 Mon Sep 17 00:00:00 2001 From: Aman Agnihotri Date: Mon, 9 Nov 2026 09:40:42 +0000 Subject: [PATCH 2/3] Wire empty ConditionGroup cases to the continuation (cherry picked from commit be2f39da5217bdf7994af6a38f639e3741b90a83) --- .../_workflows/_declarative_builder.py | 7 ++++ .../declarative/tests/test_graph_coverage.py | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py index c60d29bb15f..7f39b0c93fc 100644 --- a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py +++ b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py @@ -748,6 +748,13 @@ def _create_condition_group_structure( branch_exit = self._get_branch_exit(branch_entry) if branch_exit: branch_exits.append(branch_exit) + else: + case_passthrough = JoinExecutor( + {"kind": "CasePassthrough"}, id=self._allocate_generated_id(f"{action_id}_case{i}_pass") + ) + self._executors[case_passthrough.id] = case_passthrough + branch_entries.append((i, case_passthrough)) + branch_exits.append(case_passthrough) else_actions = action_def.get("elseActions", []) default_entry = None diff --git a/python/packages/declarative/tests/test_graph_coverage.py b/python/packages/declarative/tests/test_graph_coverage.py index c1a0f0da70e..73ef95b74dd 100644 --- a/python/packages/declarative/tests/test_graph_coverage.py +++ b/python/packages/declarative/tests/test_graph_coverage.py @@ -2615,6 +2615,38 @@ def test_empty_then_branch_wires_continuation_passthrough(self): assert asyncio.run(else_edge.should_route(else_branch)) is True assert asyncio.run(else_edge.should_route(then_branch)) is False + def test_empty_condition_group_case_wires_continuation_passthrough(self): + """An empty ConditionGroup case branch must still continue to the following action.""" + from agent_framework_declarative._workflows import DeclarativeWorkflowBuilder + + yaml_def = { + "name": "empty_condition_group_case", + "actions": [ + { + "kind": "ConditionGroup", + "id": "route", + "conditions": [{"condition": "=true", "actions": []}], + }, + {"kind": "SendActivity", "id": "after", "activity": {"text": "after"}}, + ], + } + + workflow = DeclarativeWorkflowBuilder(yaml_def).build() + edges = {(e.source_id, e.target_id) for group in workflow.edge_groups for e in group.edges} + + assert ("route_eval", "route_case0_pass") in edges + assert ("route_case0_pass", "after") in edges + + case_edge = next( + edge + for group in workflow.edge_groups + for edge in group.edges + if edge.source_id == "route_eval" and edge.target_id == "route_case0_pass" + ) + matched_case = ConditionResult(matched=True, branch_index=0, value=True) + + assert asyncio.run(case_edge.should_route(matched_case)) is True + def test_non_empty_then_branch_does_not_add_passthrough(self): """A non-empty then branch should keep wiring directly to its action.""" from agent_framework_declarative._workflows import DeclarativeWorkflowBuilder From 50f78541b9cc0bfddddf5e9e3ce34d728aba7d6f Mon Sep 17 00:00:00 2001 From: Kip Date: Tue, 10 Nov 2026 17:05:51 +0800 Subject: [PATCH 3/3] Python: only add conditional passthroughs for empty authored branches Base the passthrough decision on the authored action list rather than on whether an executor was created. Non-empty branches containing only unsupported actions no longer continue as empty branches. Preserve the existing empty-branch passthroughs and the ConditionGroup fix from @its-amann. Co-Authored-By: Claude Fable 5 --- .../_workflows/_declarative_builder.py | 4 +- .../declarative/tests/test_graph_coverage.py | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py index 7f39b0c93fc..fcd7b0731be 100644 --- a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py +++ b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_builder.py @@ -620,7 +620,7 @@ def _create_if_structure( then_actions = action_def.get("then", action_def.get("actions", [])) then_entry = self._create_executors_for_actions(then_actions, builder, branch_context) then_passthrough = None - if then_entry is None: + if not then_actions: # Empty then branch - create a passthrough for continuation when condition is true then_passthrough = JoinExecutor( {"kind": "ThenPassthrough"}, id=self._allocate_generated_id(f"{action_id}_then_pass") @@ -748,7 +748,7 @@ def _create_condition_group_structure( branch_exit = self._get_branch_exit(branch_entry) if branch_exit: branch_exits.append(branch_exit) - else: + elif not branch_actions: case_passthrough = JoinExecutor( {"kind": "CasePassthrough"}, id=self._allocate_generated_id(f"{action_id}_case{i}_pass") ) diff --git a/python/packages/declarative/tests/test_graph_coverage.py b/python/packages/declarative/tests/test_graph_coverage.py index 73ef95b74dd..abece235ce6 100644 --- a/python/packages/declarative/tests/test_graph_coverage.py +++ b/python/packages/declarative/tests/test_graph_coverage.py @@ -5,6 +5,7 @@ # pyright: reportGeneralTypeIssues=false import asyncio +import logging from dataclasses import dataclass from typing import Any, cast from unittest.mock import AsyncMock, MagicMock @@ -2671,6 +2672,62 @@ def test_non_empty_then_branch_does_not_add_passthrough(self): assert ("then_action", "after") in edges assert ("check_then_pass", "after") not in edges + def test_non_empty_then_with_only_unknown_actions_does_not_add_passthrough(self, caplog): + """A non-empty then with only unknown actions must not be treated as empty.""" + from agent_framework_declarative._workflows import DeclarativeWorkflowBuilder + + yaml_def = { + "name": "unknown_then_action", + "actions": [ + { + "kind": "If", + "id": "check", + "condition": "=true", + "then": [{"kind": "UnknownAction", "id": "typo"}], + }, + {"kind": "SendActivity", "id": "after", "activity": {"text": "after"}}, + ], + } + + with caplog.at_level(logging.WARNING): + workflow = DeclarativeWorkflowBuilder(yaml_def).build() + + edges = {(e.source_id, e.target_id) for group in workflow.edge_groups for e in group.edges} + + assert ("check_eval", "check_then_pass") not in edges + assert ("check_then_pass", "after") not in edges + assert "Unknown action kind 'UnknownAction'" in caplog.text + + def test_non_empty_condition_group_case_with_only_unknown_actions_does_not_add_passthrough( + self, + caplog, + ): + """A non-empty case with only unknown actions must not be treated as empty.""" + from agent_framework_declarative._workflows import DeclarativeWorkflowBuilder + + yaml_def = { + "name": "unknown_condition_group_case", + "actions": [ + { + "kind": "ConditionGroup", + "id": "route", + "conditions": [ + {"condition": "=true", "actions": [{"kind": "UnknownAction", "id": "typo"}]}, + ], + }, + {"kind": "SendActivity", "id": "after", "activity": {"text": "after"}}, + ], + } + + with caplog.at_level(logging.WARNING): + workflow = DeclarativeWorkflowBuilder(yaml_def).build() + + edges = {(e.source_id, e.target_id) for group in workflow.edge_groups for e in group.edges} + + assert ("route_eval", "route_case0_pass") not in edges + assert ("route_case0_pass", "after") not in edges + assert "Unknown action kind 'UnknownAction'" in caplog.text + def test_wire_to_target_with_if_structure(self): """Test wiring to an If structure routes to evaluator.""" from agent_framework import WorkflowBuilder