I've attached a patch that corrects the behaviour. For the type of lines mentioned, the patch makes the history behave more like Bash history.
I have my doubts that you've really fixed anything here since Bash is a line-oriented shell while psql is a statement-oriented one. This is a feature.
What you are observing is, I think, a side-effect of that fact that comments cannot terminate statements. That seems reasonable. In short, your BEFORE results make sense and don't require fixing.