When I open Terminal, I get an "I have no name!" prompt
Something in memory got corrupted, and the mapping between your userID (502) and your username (ipd) was lost.  I've seen it happen (usually when I've manually killed hung system processes), although I'm not sure exactly what causes it.  launchd maybe?

Because that mapping is lost, whoami can't convert your ID into a username so returns the ID, and your prompt defaults to the "I have no name!" message because you effectively don't have a name.

Logging out and back in might fixit, but restarting is the best way (as you discovered).

Basically, it's a symptom of another problem, and not a problem in and of itself.