Detecting track has finished

Robin2:
A microswitch with a roller that runs along the wall and the wall keeps the switch pressed.

The robot may fall if it keeps a hold of the wall.

Furthermore, as for the 1st option what type of optical sensor?