Open jhecking opened 8 months ago
You can’t use plain JSON serialization on histories, they’re supposed to be encoded in a proto JSON format.
if you want to save a history you downloaded with a client you’ll need to use https://www.npmjs.com/package/proto3-json-serializer. runReplayHistory supports JSON histories in that format.
Ok. Like I said, the way I've solved this problem is that I use History.fromObject()
to convert the serialised history back into a History
object before replaying it.
I recently added a historyToJSON()
function in common's proto-utils.ts
, to properly encode an existing History object to JSON, for internal purpose.
There's definitely user-side use cases for this, and implementing this correctly is far from trivial, due to bugs in proto3-json-serializer
. We should therefore expose that function explicitly as part of some "Temporal Utils" API.
Is your feature request related to a problem? Please describe.
I'm testing a patched workflow, by replaying an "unpatched" workflow history, to verify that it does not fail with a non-determinism error. To get the workflow history, I'm using the
fetchHistory()
function of the workflow client. I persist the history object to disk usingJSON.serialize
:Then I add another unit tests that does nothing but load the serialized workflow history from disk (using
JSON.parse
) and replay it, e.g.This fails with the following error:
It took me a while to figure out the problem with Antonio's help: https://temporalio.slack.com/archives/C01DKSMU94L/p1708310456162399?thread_ts=1708279751.707509&cid=C01DKSMU94L
I thought I was following the docs here: https://docs.temporal.io/dev-guide/typescript/testing#replay. In particular, this example:
What I didn't realize however, and what I don't think is very obvious, is that the JSON format returned by the Web UI and the Temporal CLI differs from the format returned by the
fetchHistory()
method.Describe the solution you'd like
What I eventually found out is that I can use the
History.fromObject
function to turn the JSON object loaded from disk back into aHistory
object that's accepted byWorker.runReplayHistory
, e.g. this works:I also noticed that the
runReplayHistory
method already has some built-in logic to determine whether thehistory
input value is a JSON object (from the Web UI or CLI) or aHistory
object. What would be nice is if it could also detect where it's being passed aHistory
object serialized into a JSON object. It could just useHistory.fromObject
in that case, just like I am doing now, as IMHO this distinction between the different JSON formats is very easy to miss and the conversion could be handled automatically for the user.Additional context
Filing this feature request on the recommendation of Antonio as per our Slack conversation: https://temporalio.slack.com/archives/C01DKSMU94L/p1708310456162399?thread_ts=1708279751.707509&cid=C01DKSMU94L