I believe that Middleman rebuilds or, at the very least, partially updates the metadata of resources when
files.changed is fired, which is why the metadata set might be getting cleared. When you say it is unstable, what are the issues you are having?
For build, it seems that
files.changed only gets called once in this mode. In addition, when the first time that
files.changed is called, it seems that the list of sitemap.resources is not yet fully populated with resources from external pipelines. Therefore, please try and see if this hybrid approach works for you:
# your loops to update metadata
Hope that helps.