Skip to content

fix(sync): keep children moved out by a merge-delete - #14296

Merged
tiensonqin merged 4 commits into
logseq:masterfrom
prithvihv:fix/merge-delete-keeps-moved-children
Dec 1, 2026
Merged

tiensonqin merged 4 commits into
logseq:masterfrom
prithvihv:fix/merge-delete-keeps-moved-children

Conversation

@prithvihv

@prithvihv prithvihv commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Helllooo Logseq Team 🌈

Thank you so much for working on this amazing tool! I have been using it for a very long time now and am absolutely sure it's the best note taking tool for me.

I ran into this bug often so i started looking into it. I did use claude to learn about the project and understand my change, i write elixir in my day job so i somewhat understand programming i would say 🙃. Please let me know what you think, or if i can contribute to this issue in other ways.

Problem

Screen.Recording.2026-11-30.at.22.54.46.mov

Merging a block that has children into the previous block deletes those children on the sync server and on other clients, while the device that did the merge keeps them.

Steps to reproduce (DB graph with sync):

  1. Create blocks A and B, and give B a child C.
  2. Put the cursor at the start of B and press Backspace. B merges into A, and C becomes A's child.
  3. Edit C.

Expected: the edit syncs. Actual: the edit appears, then reverts. The server rejects every tx that touches C with db transact failed / Nothing found for entity id [:block/uuid C] (:missing-block-uuids [C]), the client rolls it back, and other devices no longer have C or its subtree.

The same path is taken by:

  • Backspace at the start of a block that has children
  • forward-Delete in an empty block that has children
  • forward-Delete at the end of a block whose next block has children

Root cause

The editor runs the merge as one :outliner-op :delete-blocks tx: move the children to the previous block, then delete the merged block. ldb/batch-transact-with-temp-conn! runs those ops in order, so expansion on the uploading client sees the children already moved, then replays the combined datoms with :replay-datoms? true ("already expanded"). transact! drops that flag from tx-meta, so the uploaded tx keeps the :delete-blocks label without it:

[[:db/add [:block/uuid C] :block/parent [:block/uuid A]]
 [:db/retractEntity [:block/uuid B]]]

Delete expansion then treats [:db/retractEntity B] as "delete B's subtree" and looks up that subtree in the db before the tx, where C is still under B. So it adds retracts for C and its descendants. This happens in three places:

  1. Server sanitizer: tx-sanitize/sanitize-tx with :retract-touched-descendants?
  2. ldb/transact! expansion: delete-blocks/expand-delete-blocks-tx, on the server and on every client applying a remote :delete-blocks tx
  3. Client rebase replay: expand-block-retracts-to-descendants in frontend.worker.sync.apply-txs

Fix

New shared helper logseq.db.common.delete-blocks/detached-ids: of the candidate descendants, return only those that have no path to a page once the tx applies.

  • If the tx changes the :block/parent of none of the candidates, the candidates are returned unchanged, so ordinary deletes keep the existing behavior and cost.
  • Otherwise the tx is previewed with d/with, and each candidate is kept if its :block/parent chain still reaches a page.

All three expansion sites now pass their candidate descendants through detached-ids. Children the tx moves out of the deleted block are kept. Children nobody moved, for example ones another client added concurrently, are still deleted, so no orphans are left.

The server can't simply trust the client's expansion and skip its own: the uploader expanded against its own view of the tree, which doesn't include children added concurrently on the server.

Tests

New tests (each checks the merge case and that an unmoved child is still deleted):

  • deps/db: delete-blocks-test/delete-blocks-keeps-children-moved-out-in-same-tx
  • deps/db-sync: tx-sanitize-test/sanitize-delete-blocks-keeps-descendants-moved-out-test
  • deps/db-sync: worker-handler-sync-test/tx-batch-delete-blocks-keeps-children-moved-out-of-deleted-block-test, through handle-tx-batch! with the tx shape the editor uploads, plus a child added concurrently on the server
  • app: db-sync-test/rebase-delete-expansion-keeps-children-moved-out-in-same-tx-test (rebase replay)
  • app: db-sync-test/merge-delete-keeps-moved-children-on-server-and-other-clients-test, end to end. Client A runs the editor's merge ops through outliner-op/apply-ops!, its real pending upload goes through sync-handler/apply-tx-entry!, client B applies what the server journaled (storage/fetch-tx-since, decoded as pull/ok does) through apply-remote-txs!, and the test asserts that A, the server and B have the same tree and checksums.

All new tests fail without the source changes. Without the fix, the end-to-end test shows the server and client B losing the moved child and grandchild, with checksums that differ from client A's.

Results:

Suite Result
deps/db 213 tests, 0 failures
deps/db-sync 250 tests, 0 failures
deps/outliner 172 tests, 0 failures
app (bb dev:lint-and-test) passes

bb dev:lint-and-test (lint and unit tests) passes locally. Not run locally: e2e.

Notes and trade-offs

  • Cost: the d/with preview runs only when a delete tx also moves a block out of the deleted subtree, which in practice means merges.
  • Client copy walks subtrees twice: expand-block-retracts-to-descendants now walks each deleted block's subtree once to compute detached and again in the existing loop that emits retracts. This keeps the diff to that function minimal. It's cheap for the subtrees replayed during rebase, and could be removed by reusing the first walk at the cost of restructuring the loop.
  • Mixed versions: clients on older versions still delete the moved children locally when they receive a merge tx, until they update.

Not addressed here (follow-ups)

  • Blocks already lost stay stuck. handle-tx-reject! only rolls back and marks the tx failed. There is no repair path for :missing-block-uuids when the block still exists locally.
  • handle-tx-batch-ok! ignores the canonical txs the server returns, so server-side rewrites never reach the uploading client. The checksum check that would detect the divergence (verify-sync-checksum!) runs only in dev/test builds.
  • The OCaml db worker on dev ports the same three expansion sites (db_sync_tx_sanitize.ml, delete_blocks.ml, sync_replay.ml) and needs the same change.

Related

Merging a block that has children (Backspace at its start, or forward
Delete into it) moves the children to the previous block and retracts
the merged block in one :delete-blocks tx. Delete expansion looked up
descendants in the db before the tx, so the server, other clients and
rebase replay also deleted the moved children. The uploading client
kept them, and every later edit to them was rejected with "Nothing
found for entity id" and rolled back.

Expansion now keeps descendants that still reach a page once the tx
applies (delete-blocks/detached-ids). Descendants nobody moved, such as
children added concurrently, are still deleted.
@prithvihv
prithvihv marked this pull request as ready for review November 30, 2026 22:15

@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 found 1 potential issue.

Devin Review

The second client now applies the txs the server journaled for the
merge, as a pulling client does, instead of the first client's upload.
@CLAassistant

CLAassistant commented Nov 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Resolve temporary IDs in the transaction preview, keep dependent deletes and moves in the same chunk, and reuse parent and descendant checks.

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

@prithvihv Thanks a lot for the report and the fix, and elixir rocks!

@tiensonqin
tiensonqin merged commit f082a88 into logseq:master Dec 1, 2026
40 of 43 checks passed
prithvihv added a commit to prithvihv/logseq-db-nix that referenced this pull request Dec 1, 2026
* bump logseq to f082a88 and update nixpkgs

Picks up logseq/logseq#14296 (fix(sync): keep children moved out by a
merge-delete), which stops the sync server and other clients from deleting
the children of a block merged into its previous block.

Pinned to the fix's merge commit rather than master tip, which adds a
further journal-title data migration (schema 65.37).

- logseq 22263c3 (2026-10-01) -> f082a88 (2026-12-01); graph schema 65.34 -> 65.36
- pnpm dependency hashes updated for db-sync and the web app; clj deps unchanged
- nixpkgs c59305b (2026-10-01) -> e713fda (2026-11-29)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* logseq-db-app: stop removing forge.config.test.js

Upstream deleted resources/forge.config.test.js in logseq/logseq#14069, so
it no longer lands in static/ and the rm in installPhase failed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants