@@ -171,14 +171,17 @@ def activate(
171171 @abstractmethod
172172 def get_payload_codec_with_context (
173173 self ,
174- payload_codec : temporalio .converter .PayloadCodec ,
174+ base_payload_codec : temporalio .converter .PayloadCodec ,
175+ workflow_context_payload_codec : temporalio .converter .PayloadCodec ,
175176 command_seq : Optional [int ],
176177 ) -> temporalio .converter .PayloadCodec :
177178 """Return a payload codec with appropriate serialization context.
178179
179180 Args:
181+ base_payload_codec: The base payload codec to apply context to.
182+ workflow_context_payload_codec: A payload codec that already has workflow context set.
180183 command_seq: Optional sequence number of the associated command. If set, the payload
181- codec will have serialization context set appropriately for that command.
184+ codec will have serialization context set appropriately for that command.
182185
183186 Returns:
184187 The payload codec.
@@ -2103,68 +2106,71 @@ def _converters_with_context(
21032106
21042107 def get_payload_codec_with_context (
21052108 self ,
2106- payload_codec : temporalio .converter .PayloadCodec ,
2109+ base_payload_codec : temporalio .converter .PayloadCodec ,
2110+ workflow_context_payload_codec : temporalio .converter .PayloadCodec ,
21072111 command_seq : Optional [int ],
21082112 ) -> temporalio .converter .PayloadCodec :
21092113 if not isinstance (
2110- payload_codec ,
2114+ base_payload_codec ,
21112115 temporalio .converter .WithSerializationContext ,
21122116 ):
2113- return payload_codec
2114-
2115- workflow_context = temporalio .converter .WorkflowSerializationContext (
2116- namespace = self ._info .namespace ,
2117- workflow_id = self ._info .workflow_id ,
2118- )
2117+ return base_payload_codec
21192118
21202119 if command_seq is None :
21212120 # Use payload codec with workflow context by default (i.e. for payloads not associated
21222121 # with a pending command)
2123- return payload_codec . with_context ( workflow_context )
2122+ return workflow_context_payload_codec
21242123
21252124 if command_seq in self ._pending_activities :
2126- act_handle = self ._pending_activities [command_seq ]
2127- act_context = temporalio .converter .ActivitySerializationContext (
2128- namespace = self ._info .namespace ,
2129- workflow_id = self ._info .workflow_id ,
2130- workflow_type = self ._info .workflow_type ,
2131- activity_type = act_handle ._input .activity ,
2132- activity_task_queue = (
2133- act_handle ._input .task_queue
2134- if isinstance (act_handle ._input , StartActivityInput )
2135- and act_handle ._input .task_queue
2136- else self ._info .task_queue
2137- ),
2138- is_local = isinstance (act_handle ._input , StartLocalActivityInput ),
2125+ # Use the activity's context
2126+ activity_handle = self ._pending_activities [command_seq ]
2127+ return base_payload_codec .with_context (
2128+ temporalio .converter .ActivitySerializationContext (
2129+ namespace = self ._info .namespace ,
2130+ workflow_id = self ._info .workflow_id ,
2131+ workflow_type = self ._info .workflow_type ,
2132+ activity_type = activity_handle ._input .activity ,
2133+ activity_task_queue = (
2134+ activity_handle ._input .task_queue
2135+ if isinstance (activity_handle ._input , StartActivityInput )
2136+ and activity_handle ._input .task_queue
2137+ else self ._info .task_queue
2138+ ),
2139+ is_local = isinstance (
2140+ activity_handle ._input , StartLocalActivityInput
2141+ ),
2142+ )
21392143 )
2140- return payload_codec .with_context (act_context )
21412144
21422145 elif command_seq in self ._pending_child_workflows :
2143- cwf_handle = self ._pending_child_workflows [command_seq ]
2144- wf_context = temporalio .converter .WorkflowSerializationContext (
2145- namespace = self ._info .namespace ,
2146- workflow_id = cwf_handle ._input .id ,
2146+ # Use the child workflow's context
2147+ child_wf_handle = self ._pending_child_workflows [command_seq ]
2148+ return base_payload_codec .with_context (
2149+ temporalio .converter .WorkflowSerializationContext (
2150+ namespace = self ._info .namespace ,
2151+ workflow_id = child_wf_handle ._input .id ,
2152+ )
21472153 )
2148- return payload_codec .with_context (wf_context )
21492154
21502155 elif command_seq in self ._pending_external_signals :
2151- # Use the target workflow's context for external signals
2152- _ , workflow_id = self ._pending_external_signals [command_seq ]
2153- wf_context = temporalio .converter .WorkflowSerializationContext (
2154- namespace = self ._info .namespace ,
2155- workflow_id = workflow_id ,
2156+ # Use the target workflow's context
2157+ _ , target_workflow_id = self ._pending_external_signals [command_seq ]
2158+ return base_payload_codec .with_context (
2159+ temporalio .converter .WorkflowSerializationContext (
2160+ namespace = self ._info .namespace ,
2161+ workflow_id = target_workflow_id ,
2162+ )
21562163 )
2157- return payload_codec .with_context (wf_context )
21582164
21592165 elif command_seq in self ._pending_nexus_operations :
21602166 # Use empty context for nexus operations: users will never want to encrypt using a
21612167 # key derived from caller workflow context because the caller workflow context is
21622168 # not available on the handler side for decryption.
2163- return payload_codec
2169+ return base_payload_codec
21642170
21652171 else :
21662172 # Use payload codec with workflow context for all other payloads
2167- return payload_codec . with_context ( workflow_context )
2173+ return workflow_context_payload_codec
21682174
21692175 def _instantiate_workflow_object (self ) -> Any :
21702176 if not self ._workflow_input :
0 commit comments