Skip to content

fix(undo): restore deleted blocks with inner refs - #13458

Merged
tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:fix/undo-delete-inner-refs
Sep 30, 2026
Merged

tiensonqin merged 1 commit into
logseq:masterfrom
yuxi-liu-wired:fix/undo-delete-inner-refs

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

What changes

Undo of a block delete did nothing when one deleted block referred to another deleted block, and it cleared the undo history. The property-based test that found the bugs of #13444 found it: random sequences of outliner operations applied through outliner-op/apply-ops! with the worker's undo history recording, each undone and compared with the graph before it. Both cases below have a regression test in frontend.worker.undo-redo-test that fails on master.

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

Steps

  1. Set the node property "related" (many values) of a to b1. Select a and b, delete. Undo: nothing comes back, and page 1 still reads c.
  2. Apply the template tmpl under e. This inserts a copy whose used-template points at tmpl. Ctrl+click the copy, then tmpl, and delete. Undo: nothing comes back.

After either undo, the undo history is empty, so no further undo works.

Cause

The inverse of :delete-blocks (build-inverse-delete-blocks, construct.cljc:795) restores each selected root with its own :insert-blocks op, in selection order. A restored block's payload keeps its ref values as [:block/uuid ...] lookups. When a block in one op refers to a block that a later op restores, the first insert fails with "Nothing found for entity id". In case 1, a's related value is b1, which comes back with b. In case 2, the copy's used-template is tmpl. Refs inside 1 op resolve, because insert-blocks transacts every inserted uuid first (core.cljs:1032). The replay (apply-history-action!) fails with reason :error, and undo clears the history (undo_redo.cljs:241).

Reordering the ops cannot fix case 1. b's restore target is its old left sibling a, so a must come back before b, while a's value needs b1.

Fix

build-inverse-delete-blocks removes from the insert payloads every ref value that points at a block restored by a later insert op. After all the inserts, :save-block ops set those values again (defer-refs-to-later-plans). For a many-valued property, only the values that point at later blocks move. The other values stay in the insert. The inverse of a delete without such refs is unchanged.

Related: logseq/db-test#1302 covers a different case, where a block that survives keeps a reference to a deleted block and undo does not restore it. I found no db-test report of this bug. I searched for "undo delete", "undo delete template", "undo delete property reference", "undo delete multiple blocks", "undo delete blocks node property", "undo does nothing delete", "used-template" and "undo selected blocks".

Tests

  • frontend.worker.undo-redo-test: 2 new tests, undo-delete-of-blocks-referring-to-each-other-test (it also redoes and undoes again) and undo-delete-of-template-and-its-applied-copy-test. On master they have 13 failures in 17 assertions. With the fix they pass: the namespace ran 31 tests with 184 assertions and 0 failures.
  • In TZ=Etc/GMT+12, frontend.undo-redo-test, logseq.outliner.op-construct-test (copied into the main test build for the run) and frontend.modules.outliner.core-test had 0 failures. frontend.worker.db-sync-test ran 231 tests with 1 failure. That failure is outliner-upload-chunks-preserve-entities-and-acknowledgment-test, an intermittent upload-chunking failure that also happens on master. This change doesn't touch upload.
  • The whole unit suite at TZ=Etc/GMT+12 (2478 tests, 12424 assertions) and the time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1448 tests, 8998 assertions): the only failures are the zone-dependent tests that fix(test): build test dates in local time #13430 fixes, the upload-chunks flake that fix(sync): keep value replacements in one request #13432 fixes, and the CPU-time bound of combine-results-large-result-benchmark (Unit tests: assert on work done instead of elapsed time #13446), all of which fail on master too.

Allow edits from maintainers.


Devin Review

@devin-ai-integration devin-ai-integration Bot 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

The same entity-id/missing failure this PR fixes still exists on the :delete-page path: build-inverse-delete-page's today-page and class/property branches emit multi-plan to-insert-op sequences but don't route root-plans through defer-refs-to-later-plans.

Verified with a repro on this branch: create today's journal with two top-level blocks x and y where x.related → y, [:delete-page [today-page-uuid {}]], then undo →

  • inverse ops insert x's plan first; its :user.property/related #{[:block/uuid y]} payload throws Nothing found for entity id [:block/uuid y]
  • the action returns {:applied? false, :reason :error}, which hits the else branch in apply-history-action → clear-history! — the undo is refused and the entire undo stack is wiped, so nothing is restored and no further undo is possible

Suggestion: route root-plans through defer-refs-to-later-plans in both build-inverse-delete-page branches, or hoist the deferral so it covers any multi-plan insert sequence regardless of the producing op.

(Also heads-up: this PR currently has a merge conflict with master.)

Written by Devin

@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/undo-delete-inner-refs branch from d7cc7e1 to 1be2a41 Compare September 28, 2026 21:19
@tiensonqin
tiensonqin force-pushed the fix/undo-delete-inner-refs branch from 1be2a41 to d28a8d5 Compare September 30, 2026 03:32
@tiensonqin tiensonqin self-assigned this Sep 30, 2026
@tiensonqin
tiensonqin self-requested a review September 30, 2026 03:57

@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.

Can reproduce the issue and the PRs works great! 🚢 👍

The inverse of a block delete restores each selected root with its own
insert op. When a block of one op refers to a block that a later op
restores (a node property value, a used template), the insert fails on
the missing lookup ref, the replay reports :error and undo clears the
history. Take those refs out of the insert payloads and set them with
save-block ops after all inserts.
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Confirmed on master: undoing the delete of today's journal or of a tag page whose blocks refer to each other is refused and clears the history. Fixed in #13528 (logseq/db-test#1379).

yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
@yuxi-liu-wired
yuxi-liu-wired deleted the fix/undo-delete-inner-refs branch September 30, 2026 20:02
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 1, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 4, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
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