22
33from dataclasses import dataclass , field
44from enum import Enum
5- from typing import Dict , List , Optional , Callable
5+ from typing import Dict , List , Optional , Callable , TypedDict , Literal
66
77from cadence .api .v1 import (
88 decision_pb2 as decision ,
@@ -81,12 +81,18 @@ def __str__(self) -> str:
8181@dataclass
8282class StateTransition :
8383 """Represents a state transition with associated actions."""
84- next_state : DecisionState
84+ next_state : Optional [ DecisionState ]
8585 action : Optional [Callable [['BaseDecisionStateMachine' , history .HistoryEvent ], None ]] = None
8686 condition : Optional [Callable [['BaseDecisionStateMachine' , history .HistoryEvent ], bool ]] = None
8787
8888
89- decision_state_transition_map = {
89+ class TransitionInfo (TypedDict ):
90+ type : Literal ["initiated" , "started" , "completion" , "canceled" , "cancel_initiated" , "cancel_failed" , "initiation_failed" ]
91+ decision_type : DecisionType
92+ transition : StateTransition
93+
94+
95+ decision_state_transition_map : Dict [str , TransitionInfo ] = {
9096 "activity_task_scheduled_event_attributes" : {
9197 "type" : "initiated" ,
9298 "decision_type" : DecisionType .ACTIVITY ,
@@ -247,6 +253,10 @@ class BaseDecisionStateMachine:
247253 Subclasses are responsible for mapping workflow history events into state
248254 transitions and producing the next set of decisions when queried.
249255 """
256+
257+ # Common fields that subclasses may use
258+ scheduled_event_id : Optional [int ] = None
259+ started_event_id : Optional [int ] = None
250260
251261 def get_id (self ) -> str :
252262 raise NotImplementedError
@@ -890,12 +900,12 @@ def handle_history_event(self, event: history.HistoryEvent) -> None:
890900 if transition_info :
891901 event_type = transition_info ["type" ]
892902 # Route to all relevant machines using the new unified handle_event method
893- for m in list (self .activities .values ()):
894- m .handle_event (event , event_type )
895- for m in list (self .timers .values ()):
896- m .handle_event (event , event_type )
897- for m in list (self .children .values ()):
898- m .handle_event (event , event_type )
903+ for activity_machine in list (self .activities .values ()):
904+ activity_machine .handle_event (event , event_type )
905+ for timer_machine in list (self .timers .values ()):
906+ timer_machine .handle_event (event , event_type )
907+ for child_machine in list (self .children .values ()):
908+ child_machine .handle_event (event , event_type )
899909
900910 # ----- Decision aggregation -----
901911
@@ -907,11 +917,11 @@ def collect_pending_decisions(self) -> List[decision.Decision]:
907917 decisions .extend (machine .collect_pending_decisions ())
908918
909919 # Timers
910- for machine in list (self .timers .values ()):
911- decisions .extend (machine .collect_pending_decisions ())
920+ for timer_machine in list (self .timers .values ()):
921+ decisions .extend (timer_machine .collect_pending_decisions ())
912922
913923 # Children
914- for machine in list (self .children .values ()):
915- decisions .extend (machine .collect_pending_decisions ())
924+ for child_machine in list (self .children .values ()):
925+ decisions .extend (child_machine .collect_pending_decisions ())
916926
917927 return decisions
0 commit comments