Thanks for the thorough review.

I have updated the change to include a check of the head tree as well and added another unit test that I think captures the scenario you outlined that was failing but now passes.