Copied and adapted from https://github.com/aanari/dotfiles/blob/master/config/psqlrc.
When a different shell instance writes to the history file, the shell was reloading the history file and thus an up-arrow ran a different command than the one I can see above the current prompt.
Not sure I like this. It makes working in my dotfiles directory "unsafe". Git should not overwrite anything, but if I was committing at the same moment, I might get problems. Also, platform differences might cause problems: $PWD may or may not be a absolute. readlink could be used to ensure I get an absolute directory, but again, platform differences make this non-trivial.