Elon Musk’s confidence in Tesla’s technology has seemed boundless, but trying to stay ahead of the competition has posed risks.