I found some discussion/mentions about this behavior here on the forum but the issue seem to remain somewhat open.
This also happens during manual working, sometimes when one steps back in the history the activeLayer will change. It is the way Photoshop behaves, depending on what the user had done just before that hsitorystate the activeLayer may change or not when history is stepped back.
So, when stepping back back in the history one always has to manage the activeLayer also like:
//store the history step
var historyStateStore = activeDocument.activeHistoryState;
var activeLayerStore = activeDocument.activeLayer;
//then step back:
activeDocument.activeHistoryState = historyStateStore;
activeDocument.activeLayer = activeLayerStore;
Timo Autiokari