Skip to content

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

Closed
yuxi-liu-wired wants to merge 3 commits into
logseq:masterfrom
yuxi-liu-wired:perf/local-graphs-no-sync-record
Closed

yuxi-liu-wired wants to merge 3 commits into
logseq:masterfrom
yuxi-liu-wired:perf/local-graphs-no-sync-record

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

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.

Left as it is: start! can connect sync to a graph without the flag when a remote graph has the same name (<resolve-start-graph-id). Such a graph now keeps no rows of its local edits to upload, as before c3df967.

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

A full undo or redo stack kept its oldest half and dropped the newest
half, including the entry just pushed. After the 250th undoable change,
changes 126 to 250 had no entries: undo stepped back to change 251, then
jumped to change 125 while 126 to 250 stayed applied. Keep the newest
half instead.
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.

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

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

Devin Review

Comment on lines +1947 to +1948
(if local-graph?
(record-local-graph-undo! repo tx-report)

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! connects a local graph by matching its name, local-graph? omits every new edit from pending transactions. Sync uploads only pending transactions, so those edits never reach the remote graph.

Learn more

Sync can connect a graph with no remote flag when its name matches a remote graph. <resolve-start-graph-id resolves that remote ID without marking the local graph remote. The new local branch writes no client-op row, while pending-txs is the source of changes uploaded through the sync connection. Thus local edits made before or after connection cannot be uploaded.

Example: A local graph named “Work” matches an existing remote “Work.” After connecting, editing a block to “Draft” changes the local database but creates no pending row; syncing cannot send “Draft” to the remote graph.

Recommended fix: Before connecting a name-matched graph, establish a safe reconciliation or upload path for its local state. Ensure edits are recorded as pending when the graph has an active sync connection, and account for edits made before connection that have no rows.

Devin Review


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

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Continued in #13545 together with #13469, #13470 and #13466: 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant