#806·yjs

UndoManager: undo/redo deletes too much after a deleted type is restored (redone item merged with neighbour)

Author: wigwugsCreated Sep 27, 2026Updated Sep 27, 2026

Checklist

Describe the bug

Once undo or redo has re-created a deleted type, a later undo/redo can delete more restored content than it should. Text typed into an element, followed by deleting the element, doesn't survive undo (or undo-all → redo-all).

To Reproduce

Plain Yjs, no editor bindings:

javascript
import * as Y from 'yjs'

// UNDO
{
  const doc = new Y.Doc()
  const frag = doc.getXmlFragment('f')
  const p = new Y.XmlElement('p')
  p.insert(0, [new Y.XmlText('ab')])
  frag.insert(0, [p]) // untracked
  const um = new Y.UndoManager(frag, { trackedOrigins: new Set(['me']) })
  const text = () => frag.get(0)?.get(0)?.toString()
  const step = (f) => { doc.transact(f, 'me'); um.stopCapturing() }
  step(() => p.get(0).insert(0, 'x')) // "xab"
  step(() => p.get(0).delete(1, 1))   // "xb"
  step(() => frag.delete(0, 1))       // element deleted
  const got = []
  while (um.undoStack.length) { um.undo(); got.push(text()) }
  console.log(got) // expected [ 'xb', 'xab', 'ab' ], got [ 'xb', 'xab', 'b' ]
}

// REDO
{
  const doc = new Y.Doc()
  const frag = doc.getXmlFragment('f')
  const um = new Y.UndoManager(frag, { trackedOrigins: new Set(['me']) })
  const text = () => frag.get(0)?.get(0)?.toString()
  const step = (f) => { doc.transact(f, 'me'); um.stopCapturing() }
  step(() => { const p = new Y.XmlElement('p'); p.insert(0, [new Y.XmlText('')]); frag.insert(0, [p]) })
  step(() => frag.get(0).get(0).insert(0, 'a'))
  step(() => frag.get(0).get(0).insert(1, 'b'))
  step(() => frag.get(0).get(0).delete(0, 1)) // "b"
  while (um.undoStack.length) um.undo()
  while (um.redoStack.length) um.redo()
  console.log(text()) // expected 'b', got ''
}

Expected behavior

Undo walks back through "xb", "xab", "ab"; undo-all then redo-all ends at "b".

Cause (I think)

In popStackItem, an inserted struct that was redone is resolved with followRedone, and only the start is cut (diff > 0). But the redone copy may since have been merged with its right neighbour: in the undo case, undo #1 recreates x as x', undo #2 recreates a as a' right after it with the next clock, and the two merge into one item "xa". Undo #3 then resolves x → the merged "xa" and deletes both. The redo case is the same thing mirrored (a'' and b'' merge; redoing "delete a" deletes "ab").

Cutting the end as well fixes both cases:

diff
             let { item, diff } = followRedone(store, struct.id)
             if (diff > 0) {
               item = getItemCleanStart(transaction, createID(item.id.client, item.id.clock + diff))
             }
+            if (item.length > struct.length) {
+              getItemCleanStart(transaction, createID(item.id.client, item.id.clock + struct.length))
+            }
             struct = item

We're shipping this as a local patch. It has been through our property-based undo tests (random sequences of insert, delete text, delete element, and move, checked against the exact states undo-all and redo-all should walk through). I have not run the Yjs test suite with it. I'm happy to open a PR if the approach looks right.

Environment Information

  • Node 24, plain Yjs (also seen through y-prosemirror in a browser)
  • Yjs 13.6.31, 13.6.33 and 14.0.0-16 all reproduce; popStackItem has the same code in each.

Additional context

Possibly related to #317 (closed) and #472.