diff --git a/python/semantic_kernel/processes/dapr_runtime/actors/step_actor.py b/python/semantic_kernel/processes/dapr_runtime/actors/step_actor.py index 826ddd1f3dea..7ac58afc73e3 100644 --- a/python/semantic_kernel/processes/dapr_runtime/actors/step_actor.py +++ b/python/semantic_kernel/processes/dapr_runtime/actors/step_actor.py @@ -12,10 +12,7 @@ from dapr.actor.runtime.context import ActorRuntimeContext from semantic_kernel.exceptions.kernel_exceptions import KernelException -from semantic_kernel.exceptions.process_exceptions import ( - ProcessFunctionNotFoundException, - ProcessTargetFunctionNameMismatchException, -) +from semantic_kernel.exceptions.process_exceptions import ProcessFunctionNotFoundException from semantic_kernel.functions.kernel_function import KernelFunction from semantic_kernel.kernel import Kernel from semantic_kernel.processes.dapr_runtime.actors.actor_state_key import ActorStateKeys @@ -317,36 +314,21 @@ async def handle_message(self, message: ProcessMessage): self.inputs[message.function_name][k] = v - invocable_functions = [ - k - for k, v in self.inputs.items() - if v is not None and (v == {} or all(val is not None for val in v.values())) - ] - missing_keys = [ - f"{outer_key}.{inner_key}" - for outer_key, outer_value in self.inputs.items() - for inner_key, inner_value in outer_value.items() - if inner_value is None - ] - - if not invocable_functions: - logger.info(f"No invocable functions, missing keys: {', '.join(missing_keys)}") - return - - target_function = next((name for name in invocable_functions if name == message.function_name), None) + # Only the targeted function can become invocable from this message. A sibling that needs no + # input (or only step context) is always ready and must not trigger a mismatch while we wait. + target_function = message.function_name + arguments = self.inputs.get(target_function, {}) + missing_keys = [f"{target_function}.{key}" for key, value in arguments.items() if value is None] - if not target_function: - raise ProcessTargetFunctionNameMismatchException( - f"A message targeting function `{message.function_name}` has resulted in a different function " - f"`{invocable_functions[0]}` becoming invocable. Check the function names." - ) + if missing_keys: + logger.info(f"Function `{target_function}` is not invocable yet, missing keys: {', '.join(missing_keys)}") + return logger.info( f"Step with Id '{self.id}' received all required input for function [{target_function}] and is executing." ) # Concatenate all inputs and run the function - arguments = self.inputs[target_function] function = self.functions.get(target_function) if function is None: diff --git a/python/semantic_kernel/processes/local_runtime/local_step.py b/python/semantic_kernel/processes/local_runtime/local_step.py index 1b6d6e43c2ed..b2155ab58ba6 100644 --- a/python/semantic_kernel/processes/local_runtime/local_step.py +++ b/python/semantic_kernel/processes/local_runtime/local_step.py @@ -12,10 +12,7 @@ from semantic_kernel import Kernel from semantic_kernel.exceptions import KernelException -from semantic_kernel.exceptions.process_exceptions import ( - ProcessFunctionNotFoundException, - ProcessTargetFunctionNameMismatchException, -) +from semantic_kernel.exceptions.process_exceptions import ProcessFunctionNotFoundException from semantic_kernel.functions import KernelFunction from semantic_kernel.kernel_pydantic import KernelBaseModel from semantic_kernel.processes.kernel_process.kernel_process_edge import KernelProcessEdge @@ -114,36 +111,21 @@ async def handle_message(self, message: LocalMessage): self.inputs[message.function_name][k] = v - invocable_functions = [ - k - for k, v in self.inputs.items() - if v is not None and (v == {} or all(val is not None for val in v.values())) - ] - missing_keys = [ - f"{outer_key}.{inner_key}" - for outer_key, outer_value in self.inputs.items() - for inner_key, inner_value in outer_value.items() - if inner_value is None - ] - - if not invocable_functions: - logger.info(f"No invocable functions, missing keys: {', '.join(missing_keys)}") - return - - target_function = next((name for name in invocable_functions if name == message.function_name), None) + # Only the targeted function can become invocable from this message. A sibling that needs no + # input (or only step context) is always ready and must not trigger a mismatch while we wait. + target_function = message.function_name + arguments = self.inputs.get(target_function, {}) + missing_keys = [f"{target_function}.{key}" for key, value in arguments.items() if value is None] - if not target_function: - raise ProcessTargetFunctionNameMismatchException( - f"A message targeting function `{message.function_name}` has resulted in a different function " - f"`{invocable_functions[0]}` becoming invocable. Check the function names." - ) + if missing_keys: + logger.info(f"Function `{target_function}` is not invocable yet, missing keys: {', '.join(missing_keys)}") + return logger.info( f"Step with Id '{self.id}' received all required input for function [{target_function}] and is executing." ) # Concatenate all inputs and run the function - arguments = self.inputs[target_function] function = self.functions.get(target_function) if function is None: diff --git a/python/tests/unit/processes/dapr_runtime/test_step_actor.py b/python/tests/unit/processes/dapr_runtime/test_step_actor.py index a89f67065af8..d41d6c804fac 100644 --- a/python/tests/unit/processes/dapr_runtime/test_step_actor.py +++ b/python/tests/unit/processes/dapr_runtime/test_step_actor.py @@ -463,3 +463,41 @@ def __init__(self, value): assert emitted.id == "f1.OnResult" assert emitted.data == "out" assert mock_actor_context.inputs["f1"] == {"x": None} + + +async def test_handle_message_partial_input_ignores_other_function_that_needs_no_input(mock_actor_context): + """Partial fill of one function waits even when another function needs no input.""" + mock_actor_context.step_info = DaprStepInfo( + state=KernelProcessStepState(name="S", version="v", id="id"), + inner_step_python_type="Type", + edges={}, + ) + mock_actor_context.step_activated = True + mock_actor_context.functions = { + "add": MagicMock(plugin_name="plug", name="add"), + "ready": MagicMock(plugin_name="plug", name="ready"), + } + mock_actor_context.inputs = {"add": {"a": None, "b": None}, "ready": {}} + mock_actor_context.initial_inputs = {"add": {"a": None, "b": None}, "ready": {}} + + class DummyResult: + value = "out" + + mock_actor_context.invoke_function = AsyncMock(return_value=DummyResult()) + mock_actor_context.emit_event = AsyncMock() + + await mock_actor_context.handle_message( + ProcessMessage(source_id="s", destination_id="d", function_name="add", values={"a": 1}) + ) + mock_actor_context.invoke_function.assert_not_awaited() + mock_actor_context.emit_event.assert_not_awaited() + assert mock_actor_context.inputs["add"] == {"a": 1, "b": None} + + await mock_actor_context.handle_message( + ProcessMessage(source_id="s", destination_id="d", function_name="add", values={"b": 2}) + ) + mock_actor_context.invoke_function.assert_awaited_once() + assert mock_actor_context.invoke_function.call_args.args[0] is mock_actor_context.functions["add"] + assert mock_actor_context.invoke_function.call_args.args[2] == {"a": 1, "b": 2} + assert mock_actor_context.emit_event.call_args.args[0].id == "add.OnResult" + assert mock_actor_context.inputs["add"] == {"a": None, "b": None} diff --git a/python/tests/unit/processes/local_runtime/test_local_process.py b/python/tests/unit/processes/local_runtime/test_local_process.py index 2ac880f9e937..c55462a6806e 100644 --- a/python/tests/unit/processes/local_runtime/test_local_process.py +++ b/python/tests/unit/processes/local_runtime/test_local_process.py @@ -8,6 +8,7 @@ from semantic_kernel.exceptions.kernel_exceptions import KernelException from semantic_kernel.exceptions.process_exceptions import ProcessEventUndefinedException +from semantic_kernel.functions import kernel_function from semantic_kernel.kernel import Kernel from semantic_kernel.processes.kernel_process.kernel_process import KernelProcess from semantic_kernel.processes.kernel_process.kernel_process_edge import KernelProcessEdge @@ -16,10 +17,13 @@ KernelProcessEventVisibility, ) from semantic_kernel.processes.kernel_process.kernel_process_state import KernelProcessState +from semantic_kernel.processes.kernel_process.kernel_process_step import KernelProcessStep from semantic_kernel.processes.kernel_process.kernel_process_step_info import KernelProcessStepInfo +from semantic_kernel.processes.local_runtime.local_kernel_process import start from semantic_kernel.processes.local_runtime.local_message import LocalMessage from semantic_kernel.processes.local_runtime.local_process import LocalProcess from semantic_kernel.processes.local_runtime.local_step import LocalStep +from semantic_kernel.processes.process_builder import ProcessBuilder @pytest.fixture @@ -356,3 +360,38 @@ def mock_process_with_steps(mock_process, mock_kernel, build_model): local_process.steps = [step_1, step_2] return local_process + + +class _RelayStep(KernelProcessStep): + @kernel_function + async def pass_on(self, value: str) -> str: + return value + + +class _AddWithResetStep(KernelProcessStep): + calls: list[tuple] = [] + + @kernel_function + async def reset(self) -> str: + type(self).calls.append(("reset",)) + return "reset" + + @kernel_function + async def add(self, a: str, b: str) -> str: + type(self).calls.append(("add", a, b)) + return f"{a}{b}" + + +async def test_process_waits_for_partial_inputs_when_step_also_has_no_input_function(): + """Fan-in to add(a,b) succeeds even when the same step also has a no-input reset().""" + _AddWithResetStep.calls = [] + builder = ProcessBuilder(name="fan_in") + relay = builder.add_step(_RelayStep) + step = builder.add_step(_AddWithResetStep) + builder.on_input_event("go").send_event_to(step, function_name="add", parameter_name="a") + builder.on_input_event("go").send_event_to(relay, function_name="pass_on", parameter_name="value") + relay.on_function_result("pass_on").send_event_to(step, function_name="add", parameter_name="b") + + await start(process=builder.build(), kernel=Kernel(), initial_event=KernelProcessEvent(id="go", data="x")) + + assert _AddWithResetStep.calls == [("add", "x", "x")] diff --git a/python/tests/unit/processes/local_runtime/test_local_step.py b/python/tests/unit/processes/local_runtime/test_local_step.py index f4487b6deac4..70c27a2678d9 100644 --- a/python/tests/unit/processes/local_runtime/test_local_step.py +++ b/python/tests/unit/processes/local_runtime/test_local_step.py @@ -270,6 +270,88 @@ async def test_handle_message_updates_inputs_and_invokes_function(mocked_process assert step.inputs["func"]["param"] is None +@pytest.mark.parametrize( + "ready_inputs", + [{}, {"context": object()}], + ids=["no_parameters", "step_context_only"], +) +async def test_handle_message_partial_input_ignores_other_function_that_needs_no_input( + mocked_process_step_state, mocked_process_step_info, ready_inputs +): + """Partial fill of one function waits even when another function is already ready.""" + mock_kernel = AsyncMock(spec=Kernel) + mock_kernel.invoke = AsyncMock(return_value=MagicMock(value="result")) + + add_function = AsyncMock(spec=KernelFunction) + add_function.name = "add" + add_function.plugin_name = "test_plugin" + ready_function = AsyncMock(spec=KernelFunction) + ready_function.name = "ready" + ready_function.plugin_name = "test_plugin" + + step = LocalStep( + kernel=mock_kernel, + step_info=mocked_process_step_info, + event_namespace="ns", + step_state=mocked_process_step_state, # type: ignore + factories={}, + functions={"add": add_function, "ready": ready_function}, + inputs={"add": {"a": None, "b": None}, "ready": dict(ready_inputs)}, + initial_inputs={"add": {"a": None, "b": None}, "ready": dict(ready_inputs)}, + initialize_task=True, + ) + + with patch.object(LocalStep, "emit_event") as mock_emit_event: + await step.handle_message( + LocalMessage(source_id="source", destination_id="dest", function_name="add", values={"a": 1}) + ) + + mock_kernel.invoke.assert_not_awaited() + mock_emit_event.assert_not_awaited() + assert step.inputs["add"] == {"a": 1, "b": None} + + await step.handle_message( + LocalMessage(source_id="source", destination_id="dest", function_name="add", values={"b": 2}) + ) + + mock_kernel.invoke.assert_awaited_once_with(add_function, a=1, b=2) + assert mock_emit_event.call_args.args[0].id == "add.OnResult" + assert step.inputs["add"] == {"a": None, "b": None} + + +async def test_handle_message_invokes_function_that_needs_no_input(mocked_process_step_state, mocked_process_step_info): + """A message that targets a no-input function still runs it.""" + mock_kernel = AsyncMock(spec=Kernel) + mock_kernel.invoke = AsyncMock(return_value=MagicMock(value="result")) + + add_function = AsyncMock(spec=KernelFunction) + add_function.name = "add" + add_function.plugin_name = "test_plugin" + ready_function = AsyncMock(spec=KernelFunction) + ready_function.name = "ready" + ready_function.plugin_name = "test_plugin" + + step = LocalStep( + kernel=mock_kernel, + step_info=mocked_process_step_info, + event_namespace="ns", + step_state=mocked_process_step_state, # type: ignore + factories={}, + functions={"add": add_function, "ready": ready_function}, + inputs={"add": {"a": None, "b": None}, "ready": {}}, + initial_inputs={"add": {"a": None, "b": None}, "ready": {}}, + initialize_task=True, + ) + + with patch.object(LocalStep, "emit_event") as mock_emit_event: + await step.handle_message( + LocalMessage(source_id="source", destination_id="dest", function_name="ready", values={}) + ) + + mock_kernel.invoke.assert_awaited_once_with(ready_function) + assert mock_emit_event.call_args.args[0].id == "ready.OnResult" + + async def test_handle_message_raises_target_function_not_found(mocked_process_step_state, mocked_process_step_info): """Test handle_message raises an exception if the target function is not the one that is invocable.""" mock_kernel = AsyncMock(spec=Kernel)