It seems that InsertTrackAtindex is broken. It does not properly works with undo/redo and there is a big chance of deleting items:
1 - create 2 tracks with items on both
2 - run this script
Code:
reaper.Undo_BeginBlock()
reaper.InsertTrackAtIndex(1, false)
--just to create some undo
reaper.Main_OnCommand( "22", 0 )
reaper.Undo_EndBlock( "Insert track BUG", 0 )
reaper.UpdateArrange()
reaper.TrackList_AdjustWindows(false)
3 - hit Undo
4 - hit Redo
5 - watch items being deleted and not staying in their tracks properly
Also there is a problem of not updating the scrollbar properly as mentioned in another post from 2014.
This is a big one for me, there were times I have been desperate why I have my stuff out of place and missing and I have finally got to the reason. Devs, thanks in advance for fixing this.