Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 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")
)
self._executors[then_passthrough.id] = then_passthrough

# Create else branch
else_actions = action_def.get("else", [])
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -734,6 +748,13 @@ def _create_condition_group_structure(
branch_exit = self._get_branch_exit(branch_entry)
if branch_exit:
branch_exits.append(branch_exit)
elif not branch_actions:
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
Expand Down
156 changes: 156 additions & 0 deletions python/packages/declarative/tests/test_graph_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# pyright: reportPrivateUsage=false, reportUnknownVariableType=false
# pyright: reportGeneralTypeIssues=false

import asyncio
import logging
from dataclasses import dataclass
from typing import Any, cast
from unittest.mock import AsyncMock, MagicMock
Expand Down Expand Up @@ -2572,6 +2574,160 @@ 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_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

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_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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Loading