That looks similar to a problem I had about a year ago:
https://www.mobileread.com/forums/sh...postcount=2373
In that case, davidfor actually updated the plugin to fix whatever the issue was. But, I had an earlier instance where either the names or the objects being pointed to in the TOC were duplicates. IIRC, I got around the issue by making sure any entries that would go into the TOC were unique and having Calibre re-generate the TOC. Unfortunately, it's been so long, I can't remember what I did. Sorry.
EDIT: And, of course, I don't understand the code at all.