Avoid unnecessary upgrade mode (v1.20)

This is rehashed backport of various changes from trunk which ensure the machine agent doesn't enter upgrade mode unless it actually needs to.