This mechanism isn't perfect, and sometimes it will guess wrong, but it will help our automation.
The locking code gets its own module, since it's more fundamental than the higher-level locking code. Extracting the logging code was the whole point here. :)