I'm posting this here in the hope that somebody may have experienced the same issue and/or have a solution. I reported it to ESRI as a bug, but I cannot reliably recreate it, so couldn't live demo it to them.
The issue generally occurs within an aprx, where I am making active edits to a python script which is embedded in a toolbox (which lives on a shared server), using the right-click and "edit" option in the Pro catalog pane.
Generally, having made the edits I will save the changes and close the idle window and I will want to run the tool immediately afterwards to check if my edits have been successful. If the tool subsequently fails, and I got back to edit it again, often (but not always) the entire contents of the script have been replaced by a single line of text referencing a path to a temporary directory on my pc which also doesn't exist.
This is really concerning and frustrating behaviour, as it totally overwrites a saved script thereby deleting a lot of work. I appreciate I could save the script as a standalone py file and point the tool at it, but I thought that embedding was supposed to make things more efficient in terms of sharing (i.e. the python always lived with the tool, so if the toolbox was moved or shared then the meat of the script would move with it).