Undo breaks after a new change that's not undoable

I mean, my document starts like this:

hello google

Then I replace the hello via tr.replaceWith(). Now it becomes,

foobar google

Then let’s say I have a plugin on the link. It adds some text when cursor lands on it. This change is not undoable via tr.add('addToHistory', false).

foobar google.com

OK, now when I undo all the way, I would like to see this:

hello google.com

But I keep seeing duplicates,

hello google.com

hello google.com

How do I tackle this?

Trying to reproduce this, this test passes for me:

    let state = mkState(doc(p("hello ", a("link"))))
    state = state.apply(state.tr.insertText("foobar", 1, 6))
    state = state.apply(state.tr
      .insert(12, state.schema.text(".com", state.doc.resolve(11).marks()))
      .setMeta("addToHistory", false))
    state = command(state, undo)
    ist(state.doc, doc(p("hello ", a("link.com"))), eq)