Skip to content

fix(undo): restore moves, deletes, tags and properties - #13444

Merged
tiensonqin merged 7 commits into
logseq:masterfrom
yuxi-liu-wired:fix/undo-fuzz-findings
Sep 26, 2026
Merged

tiensonqin merged 7 commits into
logseq:masterfrom
yuxi-liu-wired:fix/undo-fuzz-findings

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

What changes

Five undo bugs, each with a regression test in frontend.worker.undo-redo-test that fails on master. A property-based test found them: random sequences of outliner, page, tag and property operations applied through outliner-op/apply-ops! with the worker's undo history recording, each operation undone one at a time and compared with the graph before it, then shrunk to the shortest failing sequence. Review found a sixth, moving blocks up or down into an embed, and a property case of the tag fix.

Graph in the steps below: page 1 holds a, b (children b1, b2), c. Page 2 holds d. Selections of blocks that are not neighbors come from Ctrl/Cmd+click, which adds 1 block at a time in click order.

Moving blocks that are not adjacent siblings up or down

Steps: Ctrl+click a, then b1. Press Alt+Shift+Down. Undo. The page reads b (a, b1, b2), c: a stays inside b. The same happens for 2 siblings with a block between them (a and c, moved up: undo gives b, a, c).

Cause: the inverse of :move-blocks-up-down was the same move in the other direction (construct.cljc). Moving blocks that are not adjacent siblings gathers them next to each other, so the opposite move cannot put each back.

Fix: for such a selection the inverse is build-inverse-move-blocks, which moves each block back to its old left sibling or parent, as the inverse of :move-blocks already does. A selection of adjacent siblings keeps the opposite move while it stays under its parent (see the embed case below).

Moving blocks up or down into an embed

Steps: paste an embed of d as the block after b. Select b1 and b2 and press Alt+Shift+Down. They go under d on page 2. Undo: b1 and b2 stay under d. The same happens when the embed of d is the block before b and b1, b2 move up.

Cause: moving the last children of a block down puts them into the next block, and moving the first children up puts them into the previous block. When that block is an embed, they go under the embedded block. The opposite move brings them back from the next or previous block, but not from the embedded block.

Fix: the opposite move is the inverse only when a sibling on the side of the move keeps the blocks under their parent. Every other up/down move is undone with build-inverse-move-blocks.

Moving a block together with its grandchild, or a selection made bottom-up

Steps: drag b into a. Ctrl+click a, then b2 (now a's grandchild). Move them to page 2. Undo twice. b2 stays on page 2. The same happens when b2 and b1 are selected bottom-up (b2 first) and moved: after undo, b2 stays on page 2.

Cause: move-blocks moves every selected block whose parent is not selected (filter-top-level-blocks), so the grandchild moves on its own. The inverse took its blocks from selected-block-roots, which drops every block with any selected ancestor, so the grandchild got no restore op. The inverse also restored blocks in selection order: in the bottom-up case b2 was restored next to its old left sibling b1 while b1 was still on page 2.

Fix: for moves, selected-block-roots keeps the blocks filter-top-level-blocks keeps (option :direct-parent?), and build-inverse-move-blocks restores them in page order, so a block's left sibling or parent is back before the block. The delete inverse keeps the ancestor rule, since restoring a deleted block restores its subtree.

Deleting blocks, then moving the block after them

Steps: select a and b, delete. Move c to page 2. Undo twice. Page 1 reads c, a, b.

Cause: the undo of the delete reinserts a and b with their old :block/order values (:keep-block-order?), and get-block-orders took those as they were. The undo of the move had given c a new order from gen-key on the then empty page 1, and that key sorts before the old keys of a and b.

Fix: get-block-orders keeps the old order of a top-level block only while it falls between the target and the next block. Otherwise the top-level blocks get new orders at the target, and their children keep theirs. Only undo sets :keep-block-order?.

Deleting a tag renamed to a page's title

Steps: rename the tag "Topic" to "page 1" (a page "page 1" exists; a tag may share a page's title). Delete the tag. Undo twice. The tag is gone for good, and both undos are skipped without a message.

Cause: the undo of the delete replays :create-page ["page 1" {:uuid <tag uuid> :class? true}]. The replay (apply_txs.cljs) looks the page up by uuid, then by title, finds the ordinary page "page 1", and creates nothing. The following :save-block of the tag finds no entity and raises "invalid rebase op", which undo skips.

Fix: the title fallback reuses only a page of the kind being created, a tag for :class? true and a non-tag page otherwise. A property with the title is never reused (patch from the review), since otherwise replaying the create of an ordinary page, whose uuid is gone, over an older property with the same title returned the property's uuid and created no page.

Deleting a property that has a value on its own page

Steps: set the number property "rating" on the rating property's own page, delete the property, undo twice. The property is gone for good, and the undo history is cleared.

Cause: a number value is a block whose parent is the page holding the value, here the property's own page. The inverse of the delete saved the page's attributes, which refer to that value block, before it reinserted the page's blocks, so the save threw "Nothing found for entity id". The reason became :error, and undo cleared the history.

Fix: the inverse reinserts the page's blocks before it saves the page's attributes.

Tests

  • frontend.worker.undo-redo-test: 8 new tests, each failing on master (17 assertions): undo-move-down-of-blocks-at-different-levels-test, undo-move-up-of-blocks-at-different-levels-test, undo-move-up-of-siblings-that-are-not-adjacent-test, undo-move-of-block-and-its-grandchild-restores-both-test, undo-move-of-blocks-selected-bottom-up-restores-both-test, undo-delete-after-undoing-move-of-block-left-behind-test, undo-delete-of-tag-renamed-to-page-title-restores-tag-test, undo-delete-of-property-valued-on-itself-restores-property-test.
  • frontend.worker.undo-redo-test, added after review: 3 tests that fail on 478e2f8 (12 assertions): undo-move-down-of-last-children-into-next-embed-test, undo-move-up-of-first-children-into-previous-embed-test, replay-create-page-titled-like-property-creates-page-test. 2 tests that pass on 478e2f8 and cover the case the review raised, blocks moved into the next or previous block and back (6 assertions): undo-move-down-of-last-children-into-next-block-test, undo-move-up-of-first-children-into-previous-block-test. The namespace: 42 tests, 218 assertions, 0 failures.
  • The 2 review commits (34f2224, 465488e), at the new head, in TZ=Etc/GMT+12 and TZ=Pacific/Kiritimati: frontend.worker.undo-redo-test, frontend.undo-redo-test, logseq.outliner.op-construct-test and frontend.modules.outliner.core-test: 0 failures.
  • Each of the first 5 commits, in TZ=Etc/GMT+12 and TZ=Pacific/Kiritimati: frontend.worker.undo-redo-test, frontend.undo-redo-test, frontend.worker.db-sync-test, frontend.worker.db-sync-sim-test, logseq.outliner.op-construct-test, logseq.outliner.move-property-value-undo-test, frontend.modules.outliner.core-test and the other outliner namespaces the commit touches: 0 failures beyond those that fail on master.
  • The whole unit suite before the review commits, once with TZ=Etc/GMT+12 and once with TZ=Pacific/Kiritimati: 2484 tests, 12440 assertions each. The only failures are the zone-dependent tests that fix(test): build test dates in local time #13430 fixes (6 assertions at UTC-12, 4 at UTC+14), and at UTC-12 once the CPU-time bound of sync-search-indice-300-new-blocks-performance-when-semantic-search-enabled, which passed when run again.

Allow edits from maintainers.

devin-ai-integration[bot]

This comment was marked as resolved.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Review pass on the undo fixes — the approach looks sound overall. Verified: direct-parent? roots match forward filter-top-level-blocks semantics, the document-order sort restores each moved root's target (left sibling or parent always sorts earlier), consecutive-siblings? is conservative (false negatives just take the general restore path), and moving page-save-op after the root restores fixes the dangling property-value ref. Tests pass: bb dev:test -v frontend.worker.undo-redo-test (37 tests) and the deps/outliner nbb suite (135 tests) against this branch plus the changes below.

One real gap and one nit — I couldn't push to the fork branch (no write access), so here's a patch:

  1. apply_txs.cljs :create-page replay — same-titled property still reused. ldb/get-page can also return a property entity sharing the :block/name (properties set :block/name in outliner/property.cljs). For a non-class create-page op the new check still treats it as the existing page and silently returns the property's uuid. The forward outliner-page/create! only reuses entities tagged with the kind being created (page-exists? with #{Page}/#{Tag}), so replay should exclude properties too.

  2. Nit: (filter top-level? blocks) is computed twice in get-block-orders.

diff --git a/deps/outliner/src/logseq/outliner/core.cljs b/deps/outliner/src/logseq/outliner/core.cljs
--- a/deps/outliner/src/logseq/outliner/core.cljs
+++ b/deps/outliner/src/logseq/outliner/core.cljs
@@ -631,21 +631,22 @@
     (if (and keep-block-order? (every? :block/order blocks))
-      (if (every? at-target? (map :block/order (filter top-level? blocks)))
-        (map :block/order blocks)
-        ;; The kept orders of the top-level blocks no longer fall next to the
-        ;; target, e.g. undo restoring deleted blocks after a sibling moved
-        ;; away and back got a new order: order them at the target and keep
-        ;; the orders of their children.
-        (let [top-level-orders (db-order/gen-n-keys (count (filter top-level? blocks))
-                                                    start-order end-order)]
-          (first
-           (reduce (fn [[orders top-level-orders] block]
-                     (if (top-level? block)
-                       [(conj orders (first top-level-orders)) (rest top-level-orders)]
-                       [(conj orders (:block/order block)) top-level-orders]))
-                   [[] top-level-orders]
-                   blocks))))
+      (let [top-level-blocks (filter top-level? blocks)]
+        (if (every? at-target? (map :block/order top-level-blocks))
+          (map :block/order blocks)
+          ;; The kept orders of the top-level blocks no longer fall next to the
+          ;; target, e.g. undo restoring deleted blocks after a sibling moved
+          ;; away and back got a new order: order them at the target and keep
+          ;; the orders of their children.
+          (let [top-level-orders (db-order/gen-n-keys (count top-level-blocks)
+                                                      start-order end-order)]
+            (first
+             (reduce (fn [[orders top-level-orders] block]
+                       (if (top-level? block)
+                         [(conj orders (first top-level-orders)) (rest top-level-orders)]
+                         [(conj orders (:block/order block)) top-level-orders]))
+                     [[] top-level-orders]
+                     blocks)))))
       (db-order/gen-n-keys (count blocks) start-order end-order))))
diff --git a/src/main/frontend/worker/sync/apply_txs.cljs b/src/main/frontend/worker/sync/apply_txs.cljs
--- a/src/main/frontend/worker/sync/apply_txs.cljs
+++ b/src/main/frontend/worker/sync/apply_txs.cljs
@@ -1526,10 +1526,12 @@
           page-uuid (:uuid opts)
           existing-page (or (when (uuid? page-uuid)
                               (d/entity @conn [:block/uuid page-uuid]))
-                            ;; A page and a tag can share a title; only a
-                            ;; page of the kind being created is this page.
+                            ;; A page, a tag or a property can share a title;
+                            ;; only an entity of the kind being created is
+                            ;; this page.
                             (let [page (ldb/get-page @conn title)]
-                              (when (= (boolean (:class? opts)) (boolean (ldb/class? page)))
+                              (when (and (not (ldb/property? page))
+                                         (= (boolean (:class? opts)) (boolean (ldb/class? page))))
                                 page)))]

clj-kondo clean on all touched files; both changes are in the patch above if you want to apply them (git apply).

Written by Devin

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

The red-flag case does not reproduce on 478e2f8: with P(a, b), Q, moving a, b down puts them first in Q, the opposite move puts them back after the last remaining child of P, and the up direction restores the same way (tests undo-move-down-of-last-children-into-next-block-test and undo-move-up-of-first-children-into-previous-block-test). The concern does hold when the next or previous block is an embed: the move puts the blocks under the embedded block, and the opposite move left them there. 34f2224 therefore uses the opposite move only when a sibling on the side of the move keeps the blocks under their parent, and restores other up/down moves with build-inverse-move-blocks (tests undo-move-down-of-last-children-into-next-embed-test and undo-move-up-of-first-children-into-previous-embed-test, which fail on 478e2f8). 465488e applies your property patch as posted, with replay-create-page-titled-like-property-creates-page-test, which fails without it.

Moving up or down a selection whose blocks are not adjacent siblings,
such as a top-level block and a nested block picked with Ctrl+click, or
2 siblings with a block between them, and then undoing did not put the
blocks back. After a move down of a top-level block and a nested block,
for example, the top-level block ended up inside the nested block's
parent.

The inverse of move-blocks-up-down was a move of the same blocks in the
other direction. That only reverses a move of adjacent siblings: moving
blocks that are not adjacent siblings gathers them next to each other,
so moving them back cannot return each to its own place.

For such a selection the inverse is now a move of each block back to
its left sibling or parent, the inverse move-blocks already uses. A
selection of adjacent siblings keeps the opposite move.

Test: undo-move-down-of-blocks-at-different-levels-test,
undo-move-up-of-blocks-at-different-levels-test and
undo-move-up-of-siblings-that-are-not-adjacent-test. Each fails on
master.
Moving a block together with its grandchild picked by Ctrl+click, or 2
blocks picked bottom one first, and then undoing left a block on the
destination page: the grandchild, or the upper of the 2 blocks.

move-blocks moves each selected block whose parent is not selected
(filter-top-level-blocks), so a grandchild leaves its grandparent's
subtree, and it moves them top first. The inverse restored only the
selected blocks without any selected ancestor, which drops that
grandchild, and restored them in selection order. A block restored next
to its left sibling before that sibling came back went to the
destination page, where the sibling still was.

The inverse now restores each selected block whose parent is not
selected, in page order, so each restore target is back in place first.

Test: undo-move-of-block-and-its-grandchild-restores-both-test and
undo-move-of-blocks-selected-bottom-up-restores-both-test. Each fails
on master.
Deleting blocks, moving the block that followed them to another page,
and then undoing both put the deleted blocks back below that block
instead of above it.

Undo of a delete inserts the blocks at their old left sibling or parent
with their old :block/order values (keep-block-order?). Undo of a move
puts a block back at its old left sibling or parent under a new order
value. The block that followed the deleted ones came back as the first
child with a new order that sorts before the old orders of the deleted
blocks, so those blocks, inserted as the first children, sorted after
it.

insert-blocks now keeps the old order of a top-level block only while
that order falls between the target and the block after it. Otherwise
it orders the top-level blocks at the target and keeps the orders of
their children. Only undo sets keep-block-order?, to put deleted blocks
back.

Test: undo-delete-after-undoing-move-of-block-left-behind-test. It
fails on master.
Renaming a tag to the title of an existing page, deleting the tag, and
then undoing both lost the tag for good: both undos were skipped.

Deleting a tag retracts it, so undo replays a :create-page op with the
tag's uuid and :class? true, then a save of its attributes. The replay
of :create-page reuses an existing page with the same title when no
entity has the uuid, and ldb/get-page found the ordinary page that owns
the title. No tag was created, the save failed on the missing uuid, and
both history actions were dropped as invalid.

The title fallback now reuses only a page of the kind being created: a
tag for a tag, a page that is not a tag otherwise.

Test: undo-delete-of-tag-renamed-to-page-title-restores-tag-test. It
fails on master.
Setting a property on the property's own page, deleting the property,
and then undoing lost the property for good, and the undo history was
cleared.

Deleting a property retracts it, together with the value blocks on its
page. Undo recreates the property, saves its attributes, then inserts
its blocks. The attributes include the property's value on its own
page, a reference to one of those blocks, so the save failed on an
entity that did not exist yet. The failure is not skippable, so undo
cleared the whole history.

The inverse of deleting a tag or property page now inserts the page's
blocks before it saves the page's attributes. Inserting a property
value block already sets the owner's value.

Test: undo-delete-of-property-valued-on-itself-restores-property-test.
It fails on master.
Moving the last children of a block down puts them into the next
block, and moving the first children of a block up puts them into the
previous block. When that block embeds another block, the children go
under the embedded block, and the opposite move left them there. The
inverse now uses the opposite move only when a sibling on the side of
the move keeps the blocks under their parent. Otherwise
build-inverse-move-blocks restores each block to its old left sibling
or parent.

The 2 new tests without an embed pass before this change too. They
cover the case the review raised, where the opposite move does restore
the blocks.
Replaying :create-page when the page's uuid is gone looks the page up
by title, and ldb/get-page can return a property with that title. The
replay then returned the property's uuid and created no page. The title
lookup now skips properties, as outliner-page/create! reuses only an
entity of the kind it creates. get-block-orders now filters the
top-level blocks once.

The change is the patch from the review bot's comment on the pull
request of 2026-09-26, applied as posted. The comment carries no author
header. The test is new.
@tiensonqin
tiensonqin force-pushed the fix/undo-fuzz-findings branch from 465488e to c99732e Compare September 26, 2026 14:43
@tiensonqin
tiensonqin self-requested a review September 26, 2026 14:44

@tiensonqin tiensonqin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QAed all cases and they all worked great!
Thanks for this amazing PR 🚢 👍

@tiensonqin
tiensonqin merged commit 5442f36 into logseq:master Sep 26, 2026
16 checks passed
@yuxi-liu-wired
yuxi-liu-wired deleted the fix/undo-fuzz-findings branch September 30, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants