remote: refactor some logic into get_stale_heads()