Skip to content

Undo on a graph without sync: replay DataScript's own record of each change #1339

Open

Description

@yuxi-liu-wired

Proposal

On a graph that is not synced (:logseq.kv/graph-remote? unset), undo and redo could replay DataScript's own record of each change: the tx report's :tx-data, each datom flipped and in reverse order for undo, as recorded for redo, with the worker pipeline's extra derivation off. Nothing can happen between a change and its undo on such a graph, so this restores the exact previous state for every operation kind, with no per-operation inverse to maintain. Synced graphs keep the current path.

Why

Today undo builds an inverse operation per operation kind (build-inverse-* in construct.cljc), for the sake of rebase (ADR 0011). On a graph that never syncs, those inverses are the only source of undo bugs. A property-based test of random outliner operations with undo and redo found them, all single-user: the ones fixed in logseq/logseq#13444 and logseq/logseq#13458, and the ones reported as #1296 to logseq/logseq#1300, logseq/logseq#1302, logseq/logseq#1304 to logseq/logseq#1306 and logseq/logseq#1309. The test is logseq/logseq#13461.

The stored datoms of a change are not an exact record either. They mix levels: single facts next to :db/retractEntity of whole entities, which makes their replay depend on order. The same was true of undo before ADR 0011: get-reversed-datoms grouped the recorded datoms by entity (group-by :e) and emitted [:db/retractEntity e] for deleted entities next to the other entities' facts.

Example

Block "Buy milk" has the text property note "2 liters". Delete the property note. DataScript's record of the change is 14 datoms, all single facts:

[:db/retract "Buy milk" :user.property/note "2 liters"]
[:db/retract "note" :block/created-at ...]
[:db/retract "note" :block/name "note"]
;; ... every datom of the property entity ...
[:db/retract "note" :db/ident :user.property/note]
[:db/retract "2 liters" :logseq.property/created-from-property "note"]

The client-op row stores:

[:db/retract "Buy milk" :user.property/note "2 liters"]
[:db/retractEntity "note"]

The second record lists a whole (delete the property entity) next to one of its parts (remove its value from a block). The 2 steps do not commute, and the record's order is set by the rewriting (remove-conflict-datoms groups datoms with group-by; reorder-retract-entity moves entity retractions), not by DataScript. A prototype that replayed the stored rows on local graphs hit a wrong order on redo of a property delete, and needed 2 workarounds (pipeline derivation off, properties retracted last), each with a regression test that fails without it. The tx report's datoms are one level, in the order DataScript applied them, so their replay needs no reordering.

Evidence

  • The prototype (stored rows, with the 2 workarounds) takes the property test from 16 kinds of undo/redo failure on master to 0 on a local graph, 300 sequences, with 3 rounds of undo-all and redo-all; the whole unit suite passed apart from 1 CPU-time bound in search-test that passed on rerun (#13446). A version that replays the tx report's datoms is not built yet; a PR follows.
  • The undo-stack cap bug found along the way is a separate PR.

Activity

  1. transferred this issue fromlogseq/logseqon Sep 27, 2026
  2. yuxi-liu-wired commented on Oct 27, 2026

    @yuxi-liu-wired
    Author

    An inventory of the open undo/redo issues on graphs without sync, checked against current master (d06ded9971). Each comes from the same place: the inverse is rebuilt from the operation (deps/outliner/src/logseq/outliner/op/construct.cljc) instead of from the datoms the transaction changed, so whatever the operation did beyond its own entity is not undone. Replaying DataScript's record of each change, as proposed here, restores all of these at once.

    Issue What the operation changed that its inverse leaves out
    #1300 creating a page with a new tag also created the tag; the inverse deletes only the page
    #1310 deleting a tag rewrote every reference to it as plain text (db-refs->page); the inverse restores the tag, not those titles or :block/refs
    #1325 the same for deleting a block: references to it stay plain text
    #1326 the inverse of a block delete re-inserts the display title ([[Movie]]) instead of the stored [[uuid]]
    #1302 deleting a block cleared node property values on other blocks that pointed at it; those are never recorded
    #1324 deleting a property dropped :logseq.property.view/group-by-property on views
    #1311 deleting a property removed it from the tags that listed it (:logseq.property.class/properties)
    #1309 restoring a deleted property's choices puts a choice on the property page itself
    #1312 deleting a parent tag moved its child tags to Root; the inverse does not move them back
    #1305 undoing a tag delete leaves its blocks untagged and gives it an extra Root parent
    #1299 creating a property named like an existing one records the old property's ident, so undo leaves the new one
    #1298 creating ns/child creates two pages; the op finds neither by title, so redo makes new uuids
    #1387 redo re-runs the repeating-task command, so Scheduled/Deadline advance one extra interval

    The two offline-undo issues (#1314, #1315) belong to the synced case and are listed on #1337.

    Every one of these is a fix to one inverse builder, and the next side effect added to an operation will need another. With the transaction's own datoms as the undo record, none of them needs its own fix. The no-sync implementation is in logseq/logseq#13786 (frontend.worker.undo-redo).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions