From c0e802e848a484f727ee453d88a5251a9b65d1c4 Mon Sep 17 00:00:00 2001 From: Chen-Yang Su Date: Tue, 1 Dec 2026 02:31:12 -0800 Subject: [PATCH] Pass previous result to child workflows in Chain(send_result=True) on SWF --- simpleflow/canvas.py | 13 +++++- tests/test_simpleflow/test_canvas.py | 60 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/simpleflow/canvas.py b/simpleflow/canvas.py index eba86f4c..b7b18ffb 100644 --- a/simpleflow/canvas.py +++ b/simpleflow/canvas.py @@ -6,7 +6,7 @@ from . import futures from .activity import Activity from .base import Submittable, SubmittableContainer -from .task import ActivityTask, WorkflowTask +from .task import ActivityTask, ChildWorkflowTask, WorkflowTask class FuncGroup(SubmittableContainer): @@ -225,6 +225,17 @@ def __init__( # ActivityTask.args is ignored when building swf.ActivityTask (#247) args = [*a.args, previous_result] a = ActivityTask(a.activity, *args, **a.kwargs) + elif isinstance(a, WorkflowTask): + # WorkflowTask.args is ignored when building swf.WorkflowTask + args = [*a.args, previous_result] + task_cls = WorkflowTask if isinstance(a, ChildWorkflowTask) else type(a) + a = task_cls( + a.executor, + a.workflow, + *args, + workflow_task_list=a._task_list, + **a.kwargs, + ) else: a.args.append(previous_result) diff --git a/tests/test_simpleflow/test_canvas.py b/tests/test_simpleflow/test_canvas.py index 890fff17..935168cf 100644 --- a/tests/test_simpleflow/test_canvas.py +++ b/tests/test_simpleflow/test_canvas.py @@ -9,6 +9,10 @@ from simpleflow.constants import HOUR, MINUTE from simpleflow.exceptions import AggregateException, TaskFailed from simpleflow.local.executor import Executor +from simpleflow.swf.executor import Executor as SwfExecutor +from simpleflow.swf.mapper.models import Domain +from simpleflow.swf.mapper.models.history import builder +from simpleflow.swf.mapper.responses import Response from simpleflow.task import ActivityTask, ChildWorkflowTask @@ -260,6 +264,62 @@ def test_propagate_attribute(self): self.assertFalse(inner_a.activities[0].activity.raises_on_failure) self.assertFalse(inner_a.activities[1].activity.raises_on_failure) + def test_send_result_to_child_workflow_on_swf(self): + class Child(MyWorkflow): + name = "child_workflow" + + def get_workflow_id(self, *args, **kwargs): + return "child-" + "-".join(map(str, args)) + + def run(self, *args, **kwargs): + return args + + class ParentWithTask(MyWorkflow): + def run(self): + chain = Chain( + (to_int, "42"), + ChildWorkflowTask(Child, "prefix", flag=True, workflow_task_list="custom_tl"), + send_result=True, + ) + futures.wait(self.submit(chain)) + + class ParentWithClass(MyWorkflow): + def run(self): + chain = Chain( + (to_int, "42"), + Child, + send_result=True, + ) + futures.wait(self.submit(chain)) + + domain = Domain("TestDomain") + for wf_cls, expected_args, expected_kwargs, expected_wf_id, expected_tl in ( + (ParentWithTask, ["prefix", 42], {"flag": True}, "child-prefix-42", "custom_tl"), + (ParentWithClass, [42], {}, "child-42", "test_task_list"), + ): + with self.subTest(workflow=wf_cls.__name__): + history = builder.History(wf_cls, input={}) + swf_executor = SwfExecutor(domain, wf_cls) + decisions = swf_executor.replay(Response(history=history, execution=None)).decisions + activity_id = decisions[0]["scheduleActivityTaskDecisionAttributes"]["activityId"] + history.add_decision_task_completed() + history.add_activity_task( + to_int, + decision_id=history.last_id, + last_state="completed", + activity_id=activity_id, + result=42, + ) + history.add_decision_task_scheduled() + history.add_decision_task_started() + + decisions = swf_executor.replay(Response(history=history, execution=None)).decisions + self.assertEqual(len(decisions), 1) + attrs = decisions[0]["startChildWorkflowExecutionDecisionAttributes"] + self.assertEqual(json.loads(attrs["input"]), {"args": expected_args, "kwargs": expected_kwargs}) + self.assertEqual(attrs["workflowId"], expected_wf_id) + self.assertEqual(attrs["taskList"]["name"], expected_tl) + class TestFuncGroup(unittest.TestCase): def test_previous_value_with_func(self):