I've spotted another bug:
Line 371 is
fuction add_robotport(event)
there's an extra t (roboTport instead of roboport), so the function is never being registered as a listener and thus is never called. I have a strong suspicion that this is why the patched version was actually working, because it was never calling the upgrade_roboport_entity() function, practically excluding any mod functionality.
If I'm correct, you should be able to reproduce all the issues after researching a new tier of the Advanced Roboport technology while there are roboports on the map (this will force an upgrade_roboport on each existing roboport)
Tbh, my feeling is that this issue is not related to events, but rather to how my mod handles entities. I'm making a lot of entity swapping behind the scenes, because it's impossible to change the number of charging pads of an existing roboport entity (it's defined during prototype stage), so I have new roboport prototypes with slightly different properties that I silently swap whenever a new roboport is built or a new tier of the Advanced Roboport tech is researched.
To make things manageable, I'm not dealing with new roboport Items. The roboport Item is always a vanilla roboport, which means that anytime a roboport is built, a vanilla roboport is actually created and placed on the map, but this newly created roboport entity gets destroyed and replaced with the correct betterbots-roboport entity right away during the on_built_entity and on_robot_built_entity event handler.
My idea is that your mod listens to those events too and does its work based on the "soon to be obsolete" roboport entity, which causes all the trouble we are seeing.
Unfortunately I have no other way to do what the mod is supposed to do (having a technology that "unlocks" new charging pads for all roboports), they must be handled this way.
A solution that comes to mind would be raising those events again after my mod swaps the entity, so that any mod listening to those events may have a chance to deal with the real entity that is going to persist on the map. This may cause performance issues on maps with a very large amount of roboports.
Another approach could be using the script_raised_built event
https://lua-api.factorio.com/latest/events.html#script_raised_built
to notify that a new entity was placed by the mod, but that requires all other mods to be listening to this rather unusual event. This would be the most "canonically" correct way, I could include a reference to the destroyed roboport in the event parameters letting other mods handle the swap, but would require each mod to make itself compatible with mine which is very undesirable.
I'm going to be looking into this. Let me know if you keep testing or if you have any other input on the subject.
Thanks!