Figured it out: (setq shell-input-autoexpand nil). I spent a long while looking at comint-input-autoexpand which was getting mysteriously set to 'history: this is done by shell-mode based on shell-input-autoexpand. I don't see an easy way to make it prompt-dependent, though.
This is bug#44151, see https://debbugs.gnu.org/44151. I've fixed this in Tramp 22.214.171.124, which will be released later today on GNU ELPA. Note that this bug describes a further problem, which seems to be in gdb-mi.el. This I couldn't fix.
In general, if you don't know the exact name of the buffer, you need to call buffer-list to obtain the list of buffers and filter the result to only act on the desired buffer(s).
(defun erase-buffers-matching (regexp)
"Erase the content of all buffers whose name matches REGEXP."
(interactive "sErase buffers matching: ")