@@ -113,15 +113,17 @@ public static function record(
113113 $ runCodec = is_string ($ run ->payload_codec ) && $ run ->payload_codec !== ''
114114 ? $ run ->payload_codec
115115 : null ;
116- $ serializedSuccessfulResult = null ;
117- $ resultCodec = null ;
116+ $ encodedSuccessfulResult = null ;
118117
119118 if ($ throwable === null ) {
120- $ serializedSuccessfulResult = self ::serializeWithCodec ($ result , $ codec , $ runCodec );
121- $ resultCodec = self ::payloadCodec ($ codec , $ runCodec );
119+ $ encodedSuccessfulResult = self ::serializeWithCodec (
120+ $ result ,
121+ $ codec ,
122+ self ::preferredPayloadCodec ($ lockedExecution , $ runCodec ),
123+ );
122124
123125 StructuralLimits::logWarning (
124- StructuralLimits::warnApproachingPayloadSize ($ serializedSuccessfulResult ),
126+ StructuralLimits::warnApproachingPayloadSize ($ encodedSuccessfulResult [ ' blob ' ] ),
125127 [
126128 'workflow_run_id ' => $ run ->id ,
127129 'workflow_type ' => $ run ->workflow_type ,
@@ -133,23 +135,25 @@ public static function record(
133135 );
134136
135137 try {
136- StructuralLimits::guardPayloadSize ($ serializedSuccessfulResult );
138+ StructuralLimits::guardPayloadSize ($ encodedSuccessfulResult [ ' blob ' ] );
137139 } catch (StructuralLimitExceededException $ limitExceeded ) {
138140 $ throwable = $ limitExceeded ;
139- $ serializedSuccessfulResult = null ;
140- $ resultCodec = null ;
141+ $ encodedSuccessfulResult = null ;
141142 }
142143 }
143144
144145 if (in_array ($ run ->status , [RunStatus::Completed, RunStatus::Failed], true )) {
145146 $ lockedExecution ->forceFill ([
146147 'status ' => $ throwable === null ? ActivityStatus::Completed : ActivityStatus::Failed,
147148 'result ' => $ throwable === null
148- ? $ serializedSuccessfulResult
149+ ? $ encodedSuccessfulResult [ ' blob ' ]
149150 : $ lockedExecution ->result ,
151+ 'payload_codec ' => $ throwable === null
152+ ? $ encodedSuccessfulResult ['codec ' ]
153+ : $ lockedExecution ->payload_codec ,
150154 'exception ' => $ throwable === null
151155 ? $ lockedExecution ->exception
152- : self ::serializeWithCodec (self ::failurePayload ($ throwable , $ codec ), null , $ runCodec ),
156+ : self ::serializeWithCodec (self ::failurePayload ($ throwable , $ codec ), null , $ runCodec )[ ' blob ' ] ,
153157 'closed_at ' => $ lockedExecution ->closed_at ?? now (),
154158 ])->save ();
155159
@@ -178,7 +182,8 @@ public static function record(
178182 if ($ throwable === null ) {
179183 $ lockedExecution ->forceFill ([
180184 'status ' => ActivityStatus::Completed,
181- 'result ' => $ serializedSuccessfulResult ,
185+ 'result ' => $ encodedSuccessfulResult ['blob ' ],
186+ 'payload_codec ' => $ encodedSuccessfulResult ['codec ' ],
182187 'exception ' => null ,
183188 'closed_at ' => now (),
184189 ])->save ();
@@ -191,7 +196,7 @@ public static function record(
191196 'sequence ' => $ lockedExecution ->sequence ,
192197 'attempt_number ' => $ attemptCount ,
193198 'result ' => $ lockedExecution ->result ,
194- 'payload_codec ' => $ resultCodec ,
199+ 'payload_codec ' => $ encodedSuccessfulResult [ ' codec ' ] ,
195200 'activity ' => ActivitySnapshot::fromExecution ($ lockedExecution ),
196201 ], $ parallelMetadata ?? []), $ task );
197202
@@ -214,7 +219,7 @@ public static function record(
214219
215220 $ lockedExecution ->forceFill ([
216221 'status ' => ActivityStatus::Pending,
217- 'exception ' => self ::serializeWithCodec ($ exceptionPayload , null , $ runCodec ),
222+ 'exception ' => self ::serializeWithCodec ($ exceptionPayload , null , $ runCodec )[ ' blob ' ] ,
218223 'last_heartbeat_at ' => null ,
219224 ])->save ();
220225
@@ -293,7 +298,7 @@ public static function record(
293298
294299 $ lockedExecution ->forceFill ([
295300 'status ' => ActivityStatus::Failed,
296- 'exception ' => self ::serializeWithCodec ($ exceptionPayload , null , $ runCodec ),
301+ 'exception ' => self ::serializeWithCodec ($ exceptionPayload , null , $ runCodec )[ ' blob ' ] ,
297302 'closed_at ' => now (),
298303 ])->save ();
299304
@@ -509,25 +514,32 @@ private static function shouldRetry(
509514 * codec (with a chooseCodecForData PHP-only fallback), then the package
510515 * default.
511516 *
512- * The encoded blob is stamped on the activity row; the workflow side
513- * later decodes it with a codec sniff so a Y-encoded fallback round-
514- * trips even when the run's codec tag says Avro. Keeps parity with
515- * activity-argument scheduling (see WorkflowExecutor::scheduleActivity
516- * and #429).
517+ * @return array{blob: string, codec: string}
517518 */
518- private static function serializeWithCodec (mixed $ value , ?string $ workerCodec , ?string $ runCodec ): string
519+ private static function serializeWithCodec (mixed $ value , ?string $ workerCodec , ?string $ preferredCodec ): array
519520 {
520521 if (is_string ($ workerCodec ) && $ workerCodec !== '' && is_string ($ value )) {
521- return $ value ;
522+ return [
523+ 'blob ' => $ value ,
524+ 'codec ' => CodecRegistry::canonicalize ($ workerCodec ),
525+ ];
522526 }
523527
524- if (is_string ($ runCodec ) && $ runCodec !== '' ) {
525- $ chosenCodec = Serializer::chooseCodecForData ($ runCodec , $ value );
528+ if (is_string ($ preferredCodec ) && $ preferredCodec !== '' ) {
529+ $ chosenCodec = Serializer::chooseCodecForData ($ preferredCodec , $ value );
526530
527- return Serializer::serializeWithCodec ($ chosenCodec , $ value );
531+ return [
532+ 'blob ' => Serializer::serializeWithCodec ($ chosenCodec , $ value ),
533+ 'codec ' => $ chosenCodec ,
534+ ];
528535 }
529536
530- return Serializer::serialize ($ value );
537+ $ chosenCodec = Serializer::chooseCodecForData (CodecRegistry::defaultCodec (), $ value );
538+
539+ return [
540+ 'blob ' => Serializer::serializeWithCodec ($ chosenCodec , $ value ),
541+ 'codec ' => $ chosenCodec ,
542+ ];
531543 }
532544
533545 /**
@@ -549,10 +561,10 @@ private static function failurePayload(Throwable $throwable, ?string $workerCode
549561 return $ payload ;
550562 }
551563
552- private static function payloadCodec (? string $ workerCodec , ?string $ runCodec ): string
564+ private static function preferredPayloadCodec ( ActivityExecution $ execution , ?string $ runCodec ): ? string
553565 {
554- if (is_string ($ workerCodec ) && $ workerCodec !== '' ) {
555- return $ workerCodec ;
566+ if (is_string ($ execution -> payload_codec ) && $ execution -> payload_codec !== '' ) {
567+ return $ execution -> payload_codec ;
556568 }
557569
558570 if (is_string ($ runCodec ) && $ runCodec !== '' ) {
0 commit comments