Skip to content

perf(sync): cheaper checksum; undo and no sync rows on local graphs - #13545

Closed
yuxi-liu-wired wants to merge 14 commits into
logseq:masterfrom
yuxi-liu-wired:fix/sync-undo-local-graphs
Closed

yuxi-liu-wired wants to merge 14 commits into
logseq:masterfrom
yuxi-liu-wired:fix/sync-undo-local-graphs

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

This brings #13469, #13470, #13466 and #13478 into 1 pull request, so they merge once. They share test files (db_sync_test.cljs, db_sync_sim_test.cljs, undo_redo_test.cljs) and the undo and pipeline code, so each merge made the others conflict. The branch is on current master. Each part keeps its own commits, changed only where a test conflict had to be resolved, so the review can go commit by commit. The old pull requests are closed with a link here; their review threads stay there.

Commits, in order:

  1. test(sync): pin recompute-checksum to a reference (perf(sync): recompute the checksum in 1 walk of :eavt #13469)
  2. test(sync): give the uuid churn test a real tx id (perf(sync): recompute the checksum in 1 walk of :eavt #13469)
  3. perf(sync): read each entity's datoms once (perf(sync): recompute the checksum in 1 walk of :eavt #13469)
  4. perf(sync): read parent and page uuids from a map (perf(sync): recompute the checksum in 1 walk of :eavt #13469)
  5. perf(sync): walk :eavt once for the full recompute (perf(sync): recompute the checksum in 1 walk of :eavt #13469)
  6. perf(sync): write the checksum in 1 transaction (perf(sync): write the checksum in 1 transaction #13470)
  7. fix(undo): replay the tx datoms on local graphs (fix(undo): replay the tx datoms on local graphs #13466)
  8. perf(sync): keep no sync record on local graphs (perf(sync): keep no sync record on local graphs #13478)
  9. test(sync): fit the checksum tests to local graphs, new: with perf(sync): keep no sync record on local graphs #13478 every graph of the sync tests is marked synced unless it asks to stay local, so the checksum tests of graphs that do not sync (from perf(sync): keep checksum only on remote graphs #13476) ask for a local graph, and the reopen test of fix(db): restore :max-tx of pipeline transactions #13468 opens its reopened graph as a local one, since it is marked synced already through its storage.
  10. test(undo): run the cardinality undo tests synced, new: the 2 tests of fix(outliner): do not revert many to one on undo #13526 check the inverse op of a one-to-many property change, which after fix(undo): replay the tx datoms on local graphs #13466 runs only on a synced graph.

Undo of a one-to-many change on a local graph

This is a behavior difference to decide on. #13526 keeps :db.cardinality/many when a one-to-many change is undone, so that the inverse op does not throw or clear the history. With #13466, undo on a graph that does not sync replays the change's recorded datoms instead of an inverse op: the undo gives the property :db.cardinality/one back, and the block its single value as it was, and redo makes it :many again with the value in a set. Nothing throws and the history stays. On a synced graph the undo goes through the inverse op of #13526 and keeps :many. In the run of #13526's 2 tests on a local graph with this branch, the failing assertions were exactly these, in each test: no inverse op, :one after undo, a single value after undo. In the first test the value's title was still "text 1" and the redo assertions passed; the second test has no redo. If :many should survive undo on local graphs as well, the replay would need an exception for this change.

Tests

The whole unit suite on commits 1 to 9 (master 68a68263b plus these commits), with the db-worker-node target compiled: at TZ=Etc/GMT+12 2585 tests, 12924 assertions; at TZ=Pacific/Kiritimati, the date-dependent namespaces, 1537 tests, 9433 assertions. The only failures were the 2 tests of #13526 described above (3 assertions each, in both zones). With commit 10, frontend.worker.undo-redo-test (62 tests), frontend.worker.db-sync-test (242) and frontend.worker.db-sync-sim-test (21) have 0 failures in both zones.

The descriptions of the parts follow, as each pull request had them.

#13469: perf(sync): recompute the checksum in 1 walk of :eavt

What changes

recompute-checksum (deps/db-sync/src/logseq/db_sync/checksum.cljs) computes the whole-graph sync checksum from scratch. The worker runs it when it opens a graph whose stored checksum does not cover the graph's :max-tx (#13415), before a snapshot upload, and for the checksum diagnostics; update-checksum runs it when the e2ee flag changes or the stored checksum is not valid. On a graph of 157,614 checksum-eligible entities (1,326,567 :eavt datoms, imported from a file graph), the release build took 3.39 s with the graph in memory and 5.83 s with its index nodes still in sqlite. This branch takes that to 0.25 s and 2.81 s, and the recompute returns the same value.

On master, for each entity in the :block/uuid index, the recompute ran 5 d/datoms lookups for the eligibility check, 2 more for the entity's uuid and datoms, and 1 per parent and page tuple for their uuids. It then built a set of [uuid attr value] tuples and a [fnv djb] vector per tuple and per digest step. A CPU profile of the in-memory recompute on master (release build, 1 run, 1 ms sampling, 3,509 ms window) has 1,633 ms in index seeks (-slice and its comparators), 419 ms in hashing and 381 ms in garbage collection.

The commits:

  1. test(sync): pin recompute-checksum to a reference: a reference recompute written from the definition in the test file, compared with recompute-checksum on 37 graphs (listed under Tests), and the value on 1 fixed graph pinned to the one master gives.
  2. test(sync): give the uuid churn test a real tx id: incremental-checksum-matches-recompute-with-duplicate-block-uuid-tx-churn-test gave its datoms tx 200, below datascript's tx0 (536870912). Such a datom is missed by (d/datoms db :eavt 3) and d/entity and returned by a walk of the whole :eavt index, so the test's outcome depended on which read each checksum path used. It now uses the next tx id, and passes on master.
  3. perf(sync): read each entity's datoms once: 1 :eavt read per entity answers the eligibility check and gives the tuples. The eligibility check of update-checksum reads the same way.
  4. perf(sync): read parent and page uuids from a map, filled by 1 walk of the :block/uuid index.
  5. perf(sync): walk :eavt once for the full recompute: an entity's datoms are adjacent in :eavt, so 1 walk of the whole index reads each entity's eligibility and checksum datoms as it passes them, and folds each tuple's digest into 2 running sums, with the entity's uuid prefix folded once per entity. An entity still counts once per :block/uuid datom, as when the recompute walked that index. An entity with a repeated attribute goes through the tuple set as before, since 2 of its datoms can give the same tuple (2 parents with equal uuids).

Recompute time on that graph, standalone db worker, median (range) of 3 runs per build. Cold is the first recompute after the start, with every index node loaded from sqlite. Warm is the later recomputes of the same runs that loaded no index node, 4 to 6 values per build. Each build is the source tree of the commit named.

  • master (16c4ed1a0): release cold 5.83 s (5.42 - 6.59), warm 3.39 s (2.45 - 3.48); dev cold 6.70 s (6.56 - 7.66), warm 4.16 s (4.15 - 4.23).
  • commit 3: release cold 5.07 s (5.06 - 5.20), warm 2.66 s (2.19 - 3.16); dev cold 5.77 s (5.58 - 6.49), warm 2.65 s (2.58 - 4.24).
  • commit 4: release cold 4.56 s (4.56 - 5.09), warm 2.10 s (1.88 - 2.79); dev cold 4.98 s (4.39 - 5.29), warm 3.00 s (2.12 - 3.15).
  • the walk of commit 5 without the digest folding, an intermediate build that is not a commit here: release cold 3.50 s (3.00 - 4.16), warm 1.75 s (1.34 - 2.28); dev cold 3.97 s (3.74 - 4.21), warm 1.88 s (1.49 - 2.41).
  • commit 5, the head: release cold 2.81 s (1.91 - 3.02), warm 0.25 s (0.23 - 0.37); dev cold 2.83 s (1.96 - 2.94), warm 0.50 s (0.26 - 0.67).

Worker start on the graph as imported, which recomputes the checksum at start (fix/restore-max-tx removes that recompute), from the worker's first log line to its ready line, 3 runs per build: master release 5.75 s (5.61 - 7.22) and dev 7.90 s (7.00 - 8.24); this branch release 2.71 s (2.41 - 2.98) and dev 3.10 s (2.02 - 3.27). The cold recompute still loads the graph's 70,194 index nodes from sqlite, which this branch does not change.

Per transaction, update-checksum takes about the same time as on master (release build, median (range) of 3 runs): 0.12 ms (0.12 - 0.13) against 0.14 ms (0.11 - 0.14) on master for a 1-block save, 11.0 ms (10.9 - 11.2) against 10.9 ms (10.5 - 11.0) for a 200-block insert.

The value, on the same graph: every recompute of every build above gave 9b54a1fd6dc28b43, the value master gives. On the head's build that is 24 recomputes in 12 runs, dev and release. After 6 edits on that graph (save, insert, move and delete of 1 block, insert and delete of 200 blocks), the checksum the listener kept incrementally equals a full recompute in all 6 runs on the head's build.

Tests

  • logseq.db-sync.checksum-test, run from deps/db-sync since it reads its fixtures from there: 29 tests, 116 assertions, 0 failures on master's checksum.cljs with this branch's tests, and on commits 3, 4 and 5.
  • The reference test compares on 37 graphs: edge cases (tag-only pages, built-in true and false, a parent or page without a uuid, an entity with a uuid and nothing else), a graph whose datoms bypass cardinality and uniqueness (2 titles, 2 uuids, 2 parents with equal uuids, a shared uuid), the sample graph, the 2 fixture payloads before and after, and 29 generated graphs, e2ee on and off. With the tuple-set path of commit 5 turned off, it fails on the repeated-attribute graph and on 1 fixture graph, and incremental-checksum-matches-recompute-on-rebased-retract-entity-log-repro-test fails as well (3 failures).
  • The churn test with its old tx 200 fails on this branch (1 failure: full bb99d466953b7166 against incremental d74eeb69c5a85c5b). With the next tx id it passes on master and on this branch.
  • frontend.worker.db-sync-test 233 tests, 2222 assertions; frontend.worker.sync.client-op-test 6 tests, 22 assertions: 0 failures.
  • The whole unit suite at TZ=Etc/GMT+12 (2502 tests, 12497 assertions) and its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1469 tests, 9056 assertions): 0 failures, 0 errors. Its runner leaves out logseq.db-sync.*, hence the separate run above.
  • clj-kondo on the 2 changed files, run from deps/db-sync: 0 errors, 0 warnings.

#13470: perf(sync): write the checksum in 1 transaction

What changes

After every transaction, the db listener stores the new sync checksum and the tx it covers in the graph's client-ops database (update-local-checksum, src/main/frontend/worker/sync/client_op.cljs). The 2 keys went in 2 upserts, each its own sqlite autocommit. That database runs in WAL mode with synchronous FULL (read with PRAGMA synchronous on a graph's client-ops file), so each autocommit is a synced write. The 2 upserts now run in 1 transaction through sqlite-with-tx!, which the file already has, so each transaction pays 1 synced commit, and the 2 keys change together.

;; before
(sqlite-set-meta! store :db-sync/checksum checksum)
(sqlite-set-meta! store :db-sync/checksum-covered-tx covered-tx)
;; after
(sqlite-with-tx!
 store
 (fn [tx]
   (sqlite-set-meta! tx :db-sync/checksum checksum)
   (sqlite-set-meta! tx :db-sync/checksum-covered-tx covered-tx)))

In the node worker, sqlite-with-tx! calls the connection wrapper's transaction (wrap-node-sqlite-db in src/main/frontend/worker/platform/node.cljs), which runs the function between BEGIN and COMMIT.

Timings on a graph of 157,614 checksum-eligible entities (imported from a file graph), standalone db worker, median (range) of 3 runs per build:

  • The checksum write alone, 20 writes of changing values per run: master release 16.7 ms (14.0 - 17.0), dev 16.5 ms (15.5 - 17.2); with this change release 8.9 ms (8.8 - 10.5), dev 8.9 ms (8.5 - 11.2).
  • The listener's checksum stage per transaction (update-checksum plus the write), which the dev build logs, master against this change: 1-block insert 17.5 ms (17.0 - 17.8) against 10.0 ms (9.4 - 10.0), 1-block move 17.2 ms (16.4 - 25.0) against 9.2 ms (8.5 - 10.0), 1-block delete 17.2 ms (16.4 - 19.9) against 9.6 ms (9.2 - 10.3), 200-block insert 34.8 ms (33.7 - 36.6) against 28.2 ms (26.1 - 29.3), 200-block delete 33.1 ms (32.7 - 36.2) against 25.0 ms (24.6 - 27.4). The first transaction after the start, a 1-block save, takes 18.0 ms (17.2 - 24.0) on master and 17.7 ms (17.0 - 18.7) with this change.

The builds with this change also carried the commits of perf/checksum-one-walk and fix/restore-max-tx and 1 change to the sqlite read path. A build with the recompute changes and without this commit wrote in 16.8 ms (16.2 - 17.1) release and 17.2 ms (16.6 - 17.6) dev, 3 runs each, the same as master.

Tests

  • New checksum-and-covered-tx-written-in-1-transaction-test in frontend.worker.sync.client-op-test: counts the calls of the database's transaction during update-local-checksum and reads both keys back. On master it fails (1 failure: 0 calls). With this change it passes. The test's database is better-sqlite3 in memory, as in the rest of that namespace.
  • frontend.worker.sync.client-op-test 7 tests, 25 assertions; frontend.worker.db-sync-test 233 tests, 2222 assertions; logseq.db-sync.checksum-test (run from deps/db-sync) 28 tests, 78 assertions: 0 failures.
  • The whole unit suite at TZ=Etc/GMT+12 (2503 tests, 12500 assertions) and its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1469 tests, 9056 assertions): 0 failures, 0 errors.
  • clj-kondo on the 2 changed files: 0 errors, 0 warnings.

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

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

#13478: perf(sync): keep no sync record on local graphs

What changes

This builds on #13466 and is stacked on its branch.

After every local transaction, the db listener stores the change as a client-op row: its normalized and reversed datoms, its forward and inverse outliner ops, pending (persist-local-tx! in src/main/frontend/worker/sync/apply_txs.cljs). That is 1 synced sqlite commit per edit, on every graph. With #13466, undo and redo on a graph without sync (:logseq.kv/graph-remote? not true) replay the tx datoms the undo entry keeps, and on such a graph nothing reads the row any more. Every reader of these rows:

  • The undo entry reads its own row back for the semantic ops (gen-undo-ops!, through history-action-ops-by-tx-id), and apply-history-action! reads a row by tx id for the semantic undo and redo. On a graph without sync, fix(undo): replay the tx datoms on local graphs #13466's undo and redo replay the entry's datoms and use neither.
  • The upload of pending rows (flush-pending!), the rebase of pending rows under remote txs (apply-remote-txs!), the rollback after a reject (rollback-and-mark-failed-txs!) and the acks (mark-pending-txs-false!) run only on a sync connection.
  • A graph's first upload (upload-graph! in sync/upload.cljs) sends a snapshot of the whole graph, then marks every pending row done without sending it (clear-pending-txs!).
  • The rest count or show them: the pending count in the sync status (db-sync-status, which the CLI's sync status prints, and the sync indicator of a live connection), the cleanup every 3 hours (cleanup-finished-history-ops!, which deletes only rows with pending 0, so on a graph that never synced it deletes none), and the client-ops export for debugging.

Today a graph without sync keeps 1 row per edit, forever. On a new graph after the 2 sessions measured below (180 measured edits and their setup): 194 rows, all pending, holding 0.79 MB (insert 2.5 KB, delete 1.7 KB, save 1.6 KB per row; the first row, 413 KB, is the new graph's initial data), in a 1.0 MB file.

With this change, a local transaction on a graph that is not remote after it, the transactions of undo and redo included, gets its undo entry where it makes one and nothing in the client-ops database: no reversed datoms, no derived outliner ops, no row, no pending count. gen-undo-ops! reads no row there; the entry keeps :tx-datoms as in #13466. On a remote graph the path is as before. The condition is the one handle-local-tx! had from de1c58b (rows only on remote graphs) until c3df967, "fix: undo/redo doesn't work for local graphs", which dropped it when undo came to read the row.

A graph uploaded in the middle of a session holds undo entries recorded without rows. On a synced graph undo takes an entry's ops from its row, and without the row apply-history-action! returns :missing-history-action, which logs an error and clears the history at that undo. The transaction that makes a graph remote (the upload's set-graph-sync-metadata!) now clears the undo and redo history in handle-local-tx!, so the history ends at the upload instead of at the first undo after it. ADR 0011 makes the client-op row the source of truth for DB undo history, which it still is on a synced graph, and asks that a stack entry whose row is dropped be invalidated rather than left dangling; the stacks already end at a restart ("Session boundary"). This replaces #13466's rule that entries recorded before an upload take the semantic ops (their rows existed then). Keeping the datom replay for those entries after the upload would record each such undo on the synced graph as a row of datoms without semantic ops, the raw undo row that ADR 0011 moves away from.

Rows written before this change on a graph without sync are left as they are, pending: nothing here deletes them, and the status keeps counting them. The first upload marks them done as today, and the cleanup then deletes them.

A graph syncs once it has the flag or a remote graph id in its client-ops database (local-graph? in undo_redo.cljs reads both). start! can connect sync to a graph without the flag when a remote graph has the same name (<resolve-start-graph-id), and records that graph's id; a graph downloaded before 4ce4aed ("download remote graph should set sync metadata too") has the id and no flag. Both keep their rows as before. The history ends only when the flag is set, at an upload or download.

On a graph without sync the listener builds no upload data: the normalized and reversed datoms are computed only on the synced path. A transaction of attributes sync ignores still makes no undo entry, as before.

Measured on the desktop app, release builds of #13466 (a61ab13b5) and this branch, a new graph without sync each, 2 sessions of 30 cycles per build (the order #13466, this branch, this branch, #13466), median (range) over the 60, key to the next editor focused (delete) or to the editor gone (save):

  • 1-block delete: 92.4 ms (78.2 - 161.0) on fix(undo): replay the tx datoms on local graphs #13466, 84.3 ms (70.1 - 162.2) with this change. Per session: 88.6 and 93.7 against 85.4 and 83.4.
  • 1-block save: 77.8 ms (68.9 - 120.5) against 71.3 ms (54.8 - 124.9). Per session: 77.3 and 79.7 against 71.9 and 71.3.
  • Disk syncs from the key to the end of the edit (an fsync logger preloaded into the app, the same count in 30 of 30 edits of each session): delete and save 5 to 4 (the client-ops database 3 to 2), insert 4 to 3. Sync time per delete, median: 49.6 and 49.7 ms against 43.0 and 43.0 ms.
  • On disk after the sessions: 194 rows on fix(undo): replay the tx datoms on local graphs #13466's graph, none on this branch's.
  • With perf(sync): keep checksum only on remote graphs #13476 (checksum only on graphs that sync) on both builds, 1 session each: delete 75.2 ms (56.4 - 132.4) against 65.3 ms (46.6 - 200.7), save 62.8 ms (52.0 - 96.4) against 53.1 ms (42.6 - 90.1). Syncs: delete and save 3 to 2, insert 2 to 1; a delete or save then syncs the graph and search databases and not the client-ops database.

Tests

  • New in frontend.worker.db-sync-test:
    • local-graph-edit-writes-no-sync-record-test: an edit on a graph without sync writes no row and no pending count; its undo entry holds :tx-datoms, and undo and redo restore the title and write no row either. On fix(undo): replay the tx datoms on local graphs #13466 it fails (4 failures: 1 row after the edit, pending count 1, 3 rows after the undo and redo).
    • local-graph-keeps-existing-sync-records-test: a pending row written before this change stays, pending, after an edit and a cleanup run, and no row joins it. On fix(undo): replay the tx datoms on local graphs #13466 it fails (3 failures: a second pending row).
    • upload-ends-undo-history-recorded-without-sync-record-test: 2 edits and an undo on a graph without sync, then upload-graph! with its network calls replaced, through the app's db listener: after the upload the undo and redo stacks are empty and undo changes nothing; an edit after the upload writes its row, and its undo goes through that row. On fix(undo): replay the tx datoms on local graphs #13466 it fails (6 failures: 3 rows before the upload, both stacks kept, and the undo after the upload undid an edit from before it).
    • remote-graph-edit-writes-sync-record-test: an edit on a remote graph writes its row with the semantic ops, and the undo entry takes them from it. Passes on fix(undo): replay the tx datoms on local graphs #13466 too.
    • graph-with-a-remote-id-writes-sync-records-test: a graph with a remote graph id and no flag (attached by name, or downloaded by an older version) writes its pending row and an entry without :tx-datoms.
    • local-graph-edit-builds-no-upload-data-test: an edit on a graph without sync calls no normalize-tx-data, and makes its undo entry.
  • Tests of synced paths now run on remote graphs, as those paths do in the app. In frontend.worker.db-sync-test, with-datascript-conns marks its graph remote (with this change and without that, 82 of its tests fail: they read the rows of edits made on graphs built without the flag), the 3 new tests of graphs without sync use with-local-graph-conns, and 3 tests that build their tx report before that helper mark the graph first. In frontend.worker.undo-redo-test, 7 tests of the semantic ops an entry takes from its row mark the graph synced. In frontend.worker.db-sync-sim-test, 5 client tests use create-synced-conn. fix(undo): replay the tx datoms on local graphs #13466's synced-graph-undo-replays-semantic-ops-test keeps its synced part; its part about entries recorded before an upload is replaced by the upload test above.
  • with-datascript-conns no longer resets the conns and the undo history when a later test has installed its own conn. In a run of the namespace, the cleanups of 8 async tests that call done before the helper's cleanup ran while a later test was running; the one of pull-ok-without-checksum-is-accepted-test emptied the client-ops conn in the middle of the new upload test, which passed when run alone.
  • The property test of test(undo): add outliner undo/redo property test #13461 (frontend.worker.outliner-undo-property-test from its branch, not in this pull request), on a graph without sync, its known-bug list off, every violation counted: seed 20260926, 300 sequences, 0 violations; seed 4242, 300 sequences of up to 20 operations, 0 violations. fix(undo): replay the tx datoms on local graphs #13466 gives the same 0 and 0 on the same runs.
  • In TZ=Etc/GMT+12 and TZ=Pacific/Kiritimati: frontend.worker.undo-redo-test 56 tests, 301 assertions; frontend.worker.db-sync-test 236 tests, 2255 assertions; frontend.worker.db-sync-sim-test 21 tests, 427 assertions; frontend.worker.sync.client-op-test 6 tests, 22 assertions; frontend.worker.db-listener-test 14 tests, 51 assertions; frontend.worker.pipeline-test 42 tests, 193 assertions; frontend.worker.db-core-test 147 tests, 1601 assertions; frontend.worker.db-worker-test, frontend.worker.sync.restart-test, frontend.undo-redo-test, frontend.handler.history-test: 0 failures, 0 errors.
  • The whole unit suite, 2 processes at a time, at TZ=Etc/GMT+12 (2520 tests, 12614 assertions) and its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1487 tests, 9173 assertions): 0 failures, 0 errors.
  • clj-kondo on the 5 changed files: 0 errors, 0 warnings.

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 found 2 potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +1947 to +1950
(if local-graph?
(record-local-graph-undo! repo tx-report)
(do
(persist-local-tx! repo tx-report normalized reversed-datoms)

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.

🔴 Local edits never reach matched remote graph

When start! matches an unmarked local graph by name, enqueue-local-tx-aux records its edits without pending rows. The sync client uploads pending rows, so those edits never reach the remote graph.

Learn more

A local graph can acquire a sync connection without first setting its graph-remote flag. <resolve-start-graph-id finds a remote graph with the same name, and start! connects to it without marking the local graph remote or uploading a snapshot. New edits then take the no-row branch, while handle-hello! flushes only pending transactions. The remote graph never receives those edits; a subsequent pull can replace conflicting local state.

Example: A local graph named Notes has no remote flag, but the account already contains a remote Notes. After sync starts, editing a block locally writes no pending transaction. Connecting and flushing leaves the remote block unchanged.

Recommended fix: Reconcile graph identity before selecting the no-row path. Either prevent start! from attaching an unmarked graph to an existing remote graph, or make that attachment explicitly set the remote flag and reconcile the graph contents before accepting further edits.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +1936 to +1939
(let [normalized (normalize-tx-data db-after db-before tx-data)
reversed-datoms (reverse-tx-data db-before db-after tx-data)]
local-graph? (worker-undo-redo/local-graph? db-after)
reversed-datoms (when-not local-graph?
(reverse-tx-data db-before db-after tx-data))]

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.

🟡 Local edits still normalize discarded sync data

On a local graph, enqueue-local-tx-aux normalizes every transaction before taking its no-row branch. The normalized data is discarded, so large local edits still pay the sync preparation cost.

Learn more

normalize-tx-data filters and normalizes the transaction's datoms for upload. A local graph writes no client-op row, but this normalization happens before its branch is selected. For a large insert or delete, the worker still processes the entire transaction for an upload payload that is never used.

Example: A paste changes thousands of blocks on a graph without sync. The worker builds normalized operations for the full paste and then only records the original report's datoms for undo.

Recommended fix: Determine whether the graph is local before invoking normalize-tx-data. Only normalize transactions that will be persisted, unless another consumer actually needs the normalized result on local graphs.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 1, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 4, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 6, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
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
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/sync-undo-local-graphs branch from 57728bf to 1282469 Compare October 12, 2026 06:01
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
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
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
recompute-checksum-matches-reference-test compares recompute-checksum
with a reference written from the definition, apart from the
production code paths: for each :block/uuid datom whose entity is
eligible, the entity's [uuid attr value] tuples, parent and page as
their uuids, each digested as FNV-1a and djb2 and summed mod 2^32.

The graphs: edge cases (tag-only pages, built-in true and false, a
parent or page without a uuid, an entity with a uuid and nothing
else), datoms that bypass cardinality and uniqueness (2 titles, 2
uuids, 2 parents with equal uuids, a shared uuid), the sample graph,
2 fixture payloads, and 29 generated graphs, e2ee on and off. The
test also pins the value on 1 fixed graph.
The test's datoms carried tx 200, below datascript's tx0 (536870912).
Such a datom is missed by (d/datoms db :eavt 3), by
(d/datoms db :eavt 3 :block/uuid) and by d/entity, and returned by a
walk of the whole :eavt index or of (d/datoms db :avet :block/uuid).
The test passed only while the recompute and the incremental update
read the uuid the same way. Use the next tx id.
A full recompute checked eligibility with 5 d/datoms lookups per
entity, then read the entity's uuid and its datoms with 2 more. Read
the entity's :eavt datoms once and answer the eligibility check and
the checksum tuples from them. Parent and page uuids are still looked
up per tuple.
A full recompute looked up the uuid of every block's parent and page,
2 index seeks per block. Fill an eid to uuid map from 1 walk of the
:block/uuid index first and read them from it.
The recompute walked the :block/uuid index and sought each entity's
:eavt datoms, then built a tuple set per entity and a [fnv djb] vector
per tuple and per digest step. An entity's datoms are adjacent in
:eavt, so walk the whole index once, read each entity's eligibility
and checksum datoms as the walk passes them, and fold each tuple's
digest into 2 running sums, with the entity uuid prefix folded once
per entity. An entity still counts once per :block/uuid datom, and an
entity with a repeated attribute still goes through the tuple set,
which dedupes.
After every graph commit the checksum and the tx it covers were 2
sqlite autocommits on the client-ops database, each a synced write.
Write both in 1 transaction.
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.
After every local transaction the db listener writes a client-op row
with the change (1 synced sqlite commit per edit), on every graph. On a
graph that does not sync (:logseq.kv/graph-remote? not true) the only
reader of that row was undo, which read the entry's semantic ops back
from it. Undo there now replays the entry's own tx datoms, and the first
upload sends a snapshot of the graph and marks the rows done without
sending them, so nothing reads the row.

On such a graph a local transaction now gets its undo entry and no row:
no normalized and reversed datoms, no semantic ops, no upsert, no pending
count. On a remote graph nothing changes.

The upload's transaction that makes a graph remote ends the undo history:
its entries were recorded without rows, and undo on a synced graph takes
an entry's ops from its row. Rows written before this change stay as
they are.

db_sync_test's graphs are now marked remote by with-datascript-conns, as
the namespace tests sync paths; the new local-graph tests opt out. Its
cleanup no longer resets conns and history that a later async test has
installed.
The no-sync-record change marks every graph of the sync tests synced unless
it asks to stay local. The checksum tests of graphs that do not sync now ask
for a local graph. The reopen test's reopened graph is marked synced already
through its storage, so it asks to stay local: a second mark would be a
transaction past the covered one.
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
A graph counted as local unless it had :logseq.kv/graph-remote?, but start! can attach sync to a graph without the flag when a remote graph has its name, and a graph downloaded before 4ce4aed has the remote id and no flag. Their edits got no pending rows, so the client uploaded none of them. local-graph? now also reads the client-op store's remote graph id. On a graph without sync the listener no longer normalizes and reverses the tx for an upload it never makes.
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/sync-undo-local-graphs branch from 1282469 to 548b46f Compare October 16, 2026 18:34
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Combined with the other PRs that change the same files into #13786.

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