Finalize tree after on-tree execution

The changes in this PR make the tree frozen after the on_tree stuff was
executed. So, every command which runs on before_finish cannot change
the tree.