Main UI JavaScript rule reload causing /rest/items failure

Great you’be found the issue.

Some technical background:

In openHAB Core, Metadata configuration is stored as Map<String, Object>.
When working with Metadata configuration in JS, you will receive a JavaScript Object, where the keys are typically strings and the value can be anything. I think the Object value type for the Java Map was chosen to allow a wide variety of primitives, not real objects. When modifying metadata from Script Actions, the change will be persisted to a JSONDB, which obviously needs serialization. For simpler JavaScript object, this is no real problem I guess, but when passing more complex objects, this will fail.

Putting even the simplest JavaScript object into the value of a configuration key however passes a reference from the JavaScript object, which belongs to the ScriptEngine that executes that script. If you change the helper library or manually reload the script, the reference will be still there in openHAB’s Java layer, but lead to nothing, as the reload destroys the old ScriptEngine and creates a new one.

TL;DR: Putting any objects into metadata configuration is a very bad idea.
I will add some validation to openhab-js to disallow putting objects into metadata configuration: [items] Metadata: Don't allow objects as configuration value by florian-h05 · Pull Request #495 · openhab/openhab-js · GitHub

Apart from that, metadata is not the best way for storing such dynamic data, as any metadata change will cause writes to the JSONDB on disk, possibly wearing out a Pi’s SD card.
Have you considered using the cache for storing the required state?