Sorry for the noisy messages. It's Saturday and God knows I have the day off.
In the end, it was ansi-osc-directory-tracker resetting default-directory without regard to the shell being in Tramp-space.
I advised ansi-osc-directory-tracker to manipulate the default-directory to keep it in Tramp-space. It's a bit of a circuitous thing because ansi-osc-directory-tracker has no easy way to know if the buffer is a Tramp buffer. I can't use with-connection-local-variables as-is since it depends on default-directory which isn't in Tramp-space. And there are no buffer locals set directly by Tramp to test a shell buffer being Tramped (were there ever? should there be at least one little tiny one?).
Without either, in a shell-mode-hook, if a Tramp file, I squirreled away a copy of a Tramp vec from (tramp-dissect-file-name default-directory 'nodefault) in a buffer local. Inside ansi-osc-directory-tracker I can reconstruct a Tramp-space default-directory as
(when squirreled-vec
(setq default-directory
(tramp-make-tramp-file-name squirreled-vec default-directory)))
(I don't really call it a squirrel--it's more like a rat). Storing the vec is convenient vs. just a flag for I'd have to go make a vec anyway.
My mystery is solved for now. But people who use "modern" ansi shell directory tracking may find this issue in the future.
I'm all ears for a better solution that doesn't involve shell-hook trickery. If this kind of thing is to be fixed in Emacs core, it could be in shell-mode proper so no hook required. Would either need a Tramp local or shell-mode can just do what I did.
Other approaches?
-Stephane