At a guess it is because you have undefined as the frist argument for executeAction. It should be charIDToTypeID( "Mk " )
the "undefined" variable comes from fixing the SL code with Xtools - on the other hand even the raw SL code throws the error - and works flawlessy if there's no any snap that shares the same name.
What's strange to me is: how could a SL code coming from registering one particular move (ALT + click in the new Snap icon in the History palette, inserting the name, and clicking Yes to the "Replace?" question) do not work?
Yes, sorry I didn't read the post fully. undefined is a special word in ExtendScript. Although you can redefine it like you have done here I think it would be better to choose a different variable name, something like var eventID = charIDToTypeID( "Mk " );
But I see what you mean about it not working if a historyState with that name already exists. All I can think of is that Photoshop does something that is not recorded by scriptlistner when the 'Replace Snapshot "TEST"?' dialog appears.