Skip to content

fix(undo): replay the tx datoms on local graphs - #13466

Closed
yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:fix/undo-local-db-record
Closed

yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:fix/undo-local-db-record

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

What changes

On a graph without sync, undo and redo now replay DataScript's own record of each change, as logseq/db-test#1339 proposes: the tx report's datoms, each flipped and last first for undo, as recorded for redo. Synced graphs keep the semantic inverse ops.

Today undo replays an inverse op built from the change's label (build-inverse-* in construct.cljc). Where an operation does more than its label says, undo loses the difference (#13464). Deleting a property or a tag is the clearest case. On a graph without sync (:logseq.kv/graph-remote? not set), nothing can land between a change and its undo, so the exact undo is the change's own datoms, reversed, whatever the operation.

It fixes, on graphs without sync:

Each has a regression test in frontend.worker.undo-redo-test that fails on master. It also fixes the 2 undo bugs the property test of #13461 found once it saw links:

How

  • gen-undo-ops! keeps, on a graph without sync, the tx report's datoms as [e a v added], entity ids unchanged, in DataScript's order (:tx-datoms of the entry). The report is the one ldb/transact! hands to its listeners, which already holds what the worker pipeline added in the same transaction (tag templates, refs, revision stamps). The datoms stay in memory with their entry, like the rest of the undo stack.
  • Undo transacts them flipped, last first; redo transacts them as recorded. The transaction carries the tx meta that apply-history-action! gives a history action, so the undo is recorded as a client-op row as before, plus :undo-redo/replay-tx-datoms?.
  • For that key, transact-pipeline derives nothing: the datoms already hold what it derived for the change. Without this, each redo of a block tagged with a class that has a template inserted the template again. ldb/transact! also skips expand-delete-blocks-tx and update-refs-history for it: the datoms are the whole change, and distinct in expand-delete-blocks-tx dropped a retraction that repeats after its opposite, which left a revision stamp on a deleted block.
  • The pipeline still stamps :block/tx-id on what an undo or redo changes, so the renderer refreshes those blocks. The entry pushed on the opposite stack therefore holds the undo's or redo's own datoms, stamps included, and the next undo or redo is again the exact inverse of the last transaction.
  • A retraction of :block/tx-id retracts the stamp whatever its value: the undo of a later change can restamp an entity of an earlier one (the pages tagged with a tag whose parent changed).
  • A replay that throws has changed nothing. The undo or redo is refused and the history is kept, instead of trying the entry below or clearing the history (ADR 0011: invalidate the whole action, not a part of it).
  • The graph flag is read when the user undoes, so a graph uploaded mid-session takes the semantic ops for its older entries.

Builds on #13462

The first commit of this branch is #13462's. Without it a full undo stack (250 entries) drops its newest half, so the datoms of the entries below the hole no longer match the graph, and a replay that yields an invalid entity is refused. all-core-outliner-ops-local-undo-redo-random-sim-test (1000 operations on a graph without sync) then fails with "undo-all exceeded max steps".

Tests

Allow edits from maintainers.

@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: 2 flags

Not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

One question on replay-recorded-datoms!: the replay tx-meta' is hand-built keeping only :outliner-op from the original tx-meta, while the synced path's apply-history-action goes through undo-redo-action-meta which preserves the whole tx-meta (minus :db-sync/tx-id). Keys like :client-id or :batch-tx? are dropped on the local path. I didn't find a listener reading them today, but the two paths now emit differently-shaped metas — intentional? If not, building on undo-redo-action-meta and adding the replay-specific keys would keep them aligned.

Otherwise this looks solid: atomic replay, op pushed back with :refused? on failure, and the tx-datoms' recompute from the applied tx-report keeps the opposite-stack entry accurate. In a local test run I saw the refusal path exercised end-to-end (redo hitting a :transact/unique conflict on a :block/uuid re-add → refused, entry preserved).

Written by Devin

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Keeping the replay's meta separate is right, for this reason. The replay transacts recorded datoms, not ops, so its meta carries what describes the change to sync and to listeners: :outliner-op, the undo and redo flags, the tx ids, the forward and inverse ops. The keys it drops describe how the original op ran (:client-id, :batch-tx?, :outliner-ops, ...). 1 of them has a reader where the difference shows: the renderer's pipeline (src/main/frontend/modules/outliner/pipeline.cljs:75) refreshes the title of the block being edited when a transaction's :client-id is not this client's. The replay's transaction has no :client-id, so an undo that changes the block being edited shows its new title there. With the original :client-id kept, as on the synced path, that refresh is skipped.

I also rebased the branch on master: it carried #13462's pre-merge commit. The conflicts were in tests only. create-remote-conn from #13476 replaces this PR's create-synced-conn (same body). #13458's inner-ref tests now mark their graph synced (:logseq.kv/graph-remote?): on a local graph undo replays the datoms, and those tests check the inverse ops. frontend.worker.undo-redo-test (58 tests), frontend.worker.db-sync-test (237) and frontend.worker.db-sync-sim-test (21) pass in TZ=Etc/GMT+12 and TZ=Pacific/Kiritimati.

Undo replayed an inverse op built from the change's label, so it lost
whatever the change did beyond its label: the values of a deleted
property on blocks, the tags of a deleted tag's nodes, the references
rewritten to plain text, a child tag's parent. On a graph that does not
sync (no :logseq.kv/graph-remote?), nothing lands between a change and
its undo, so the exact undo is the change's own tx datoms, flipped and
last first.

An undo entry on a local graph now keeps the tx report's datoms as
[e a v added], in order, with the datoms the worker pipeline added.
Undo transacts them flipped and last first, redo as recorded, in one
transaction marked :undo-redo/replay-tx-datoms?: the pipeline derives
nothing more for it, and ldb/transact! neither expands deletes nor
dedupes the datoms. The entry pushed on the opposite stack keeps the
replay's own datoms, whose revision stamps are new; a :block/tx-id
stamp is retracted whatever its value, since an undo of a later change
can restamp an entity. A replay that throws changes nothing; the undo
is refused and the history kept.

Synced graphs, and entries recorded before a graph synced, keep the
semantic ops. Tests that model a synced graph set the flag.
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Continued in #13545 together with #13469, #13470 and #13478: they share files, so they now merge once. The commits are the same, on current master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment