Skip to content

fix: restore node property values when undoing a block delete - #13457

Open
dannyinit wants to merge 4 commits into
logseq:masterfrom
dannyinit:fix/1302-undo-delete-restores-node-refs
Open

dannyinit wants to merge 4 commits into
logseq:masterfrom
dannyinit:fix/1302-undo-delete-restores-node-refs

Conversation

@dannyinit

@dannyinit dannyinit commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes logseq/db-test#1302

Problem

Deleting a block retracts it with :db/retractEntity (-del in deps/outliner/src/logseq/outliner/core.cljs), which also retracts every ref value other blocks hold on it, such as a node property value. The undo inverse (build-inverse-delete-blocks in deps/outliner/src/logseq/outliner/op/construct.cljc) only reinserts the deleted subtree, so those values stay empty. Steps: set a node property (e.g. related) on block B to block A, delete A, undo. A comes back but B no longer has the related property at all.

Fix

build-inverse-delete-blocks now also receives the delete's tx-data. A new inbound-ref-restore-ops collects the retracted ref datoms whose value is a deleted block and whose entity is outside the deleted subtree, then appends one :save-block per such entity that restores those attributes from db-before, the same way build-inverse-save-block restores attributes. The ops run after the :insert-blocks, so the restored block exists when the values are set. This covers :one and :many cardinalities and any ref attribute, not only one property.

Testing

Added undo-delete-block-restores-node-property-values-pointing-at-it-test in frontend.worker.undo-redo-test. It sets :one and :many node properties on a block to another block, deletes that block, undoes, and asserts both values point at the restored block again, then redoes and asserts the value is gone. It fails on master (both values empty after undo).

  • Full frontend unit test run: 2484 tests, 0 failures
  • deps/outliner tests: 135 tests, 0 failures
  • clj-kondo: 0 errors, 0 warnings
  • Verified in the running web app (:app): with the steps above, after Ctrl+Z the related value points at the restored block again; redo removes it and a second undo restores it again. No console errors.
  • Manually verified in the browser after the fix: set a related node property on a block to another block, deleted that block, pressed Ctrl+Z. The deleted block came back and the related property reappeared under the other block, pointing at the restored block. Before the fix, the deleted block came back but the other block no longer showed the related property at all.

Root cause analysis and fix drafted with Claude Code assistance


Devin Review

devin-ai-integration[bot]

This comment was marked as resolved.

Deleting a block retracts it with :db/retractEntity, which also retracts
ref values other blocks hold on it, such as a node property value. The
undo inverse only reinserted the deleted subtree, so those values stayed
empty. Restore them from the retracted datoms with :save-block.

Fixes logseq/db-test#1302
A history action can hold several :delete-blocks ops whose deleted
blocks share a reference holder. Restore the retracted ref values once,
after all inverse ops, instead of inside each delete inverse, so the
restore never depends on the order the deleted blocks are reinserted.
@tiensonqin
tiensonqin force-pushed the fix/1302-undo-delete-restores-node-refs branch from b4686d5 to c60ebaa Compare September 26, 2026 14:52
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Review: fix looks correct for its intended path — one Important gap

The approach is sound: inbound-ref-restore-ops rebuilds the inbound ref datoms that :db/retractEntity stripped from surviving holders, and appending the restore group after reverse-inverse-entry-groups correctly guarantees all deleted blocks are reinserted before holder saves run. sanitize-ref-value produces resolvable [:block/uuid] lookup refs and assert-no-stale-numeric-ids! guards the new ops. Tests cover the surviving-holder case for both :one and :many cardinality.

Important (pre-existing on master, but same bug class)

When a deleted holder block references another deleted block, undo still fails entirely. The holder's :insert-blocks payload carries [:block/uuid target] lookup refs that are resolved strictly at transact time (entid-strict), and inserts replay in merged-ids order — if the holder inserts before its target, the transact throws, apply-history-action! returns applied? false, and the undo becomes a complete no-op with history cleared.

The new deferred :save-block group only covers holders outside the deleted set (the (not (contains? deleted-ids (:e d))) exclusion), so it can't catch this case.

Reproduced with a worker test (delete holder + its property target in a single action) on both this branch and master — identical failure, so this is not a regression, but it leaves a common case of this bug class unfixed. Suggested direction: strip ref attrs whose values intersect deleted-ids from build-insert-block-payload and drop the :e exclusion so the deferred save group restores them — it already runs last.

Minor / questions

  • inbound-ref-restore-ops only emits :save-block for holders that have :block/uuid; non-block holders (e.g. logseq.property.history entities) are silently skipped. Probably fine today, worth a comment at most.
  • Pre-existing: maybe-rewrite-delete-block-ids canonicalizes each forward :delete-blocks op to contain all deleted uuids, so the recorded inverse contains duplicated insert ops — idempotent, but inflates the op log.

Verified: both new tests pass; no overlap with the recently merged undo fixes (#13335's created-from-property reattachment and 2185a5ae's property-history ref restoration are different mechanisms).

Written by Devin

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Two issues with inbound-ref-restore-ops:

1. The trigger misses :delete-page (Important). The restore ops are only appended when (some #(= :delete-blocks (first %)) forward-ops). But :delete-page also hard-retracts blocks — build-page-retract-tx emits [:db/retractEntity] per :block/_page for today-page truncation and class/property page deletes — which retracts holder refs the same way.

Verified with a repro on this branch: holder block task has :user.property/related pointing at a block on today's journal; [:delete-page [today-uuid {}]] truncates the block and retracts the holder's ref; undo restores the block but the holder's related stays [] — the ref is silently lost.

Suggestion: trigger on the tx-data instead of the op name — e.g. only gate on (seq deleted-ids) (which already derives from :block/uuid retractions), so any forward op that hard-deletes blocks gets the restore ops.

2. Over-restore on mixed txs (Minor). The restore op writes the holder's attr to the full db-before value set via (get ent a). If one tx deletes block X and legitimately removes another value Z from the same holder attr, undo resurrects Z. Restoring only the recorded retracted values for that attr (merge the retract datoms) instead of the whole pre-tx set would avoid it.

Written by Devin

:delete-page hard-deletes blocks too, e.g. when it truncates today's
journal page or deletes a class or property page, which retracts the
ref values other blocks hold on them. Run the inbound ref restore for
:delete-page as well as :delete-blocks.
devin-ai-integration[bot]

This comment was marked as resolved.

A delete can also add values to the attr it retracts a ref from, e.g.
a child class extends Root after its parent class is deleted. The
restore :save-block added the old values next to them, so undo left
the child extending both classes. Retract the restored attrs first.

This branch has not been deployed

No deployments
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.

Undo of deleting a block leaves node property values that pointed at it empty

1 participant