Repository navigation
perf(sync): keep no sync record on local graphs - #13478
yuxi-liu-wired wants to merge 3 commits into
Conversation
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.
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| (if local-graph? | ||
| (record-local-graph-undo! repo tx-report) |
There was a problem hiding this comment.
🔴 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
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!insrc/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:gen-undo-ops!, throughhistory-action-ops-by-tx-id), andapply-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.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.upload-graph!insync/upload.cljs) sends a snapshot of the whole graph, then marks every pending row done without sending it (clear-pending-txs!).db-sync-status, which the CLI'ssync statusprints, 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-datomsas in #13466. On a remote graph the path is as before. The condition is the onehandle-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'sset-graph-sync-metadata!) now clears the undo and redo history inhandle-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):Tests
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, thenupload-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.frontend.worker.db-sync-test,with-datascript-connsmarks 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 usewith-local-graph-conns, and 3 tests that build their tx report before that helper mark the graph first. Infrontend.worker.undo-redo-test, 7 tests of the semantic ops an entry takes from its row mark the graph synced. Infrontend.worker.db-sync-sim-test, 5 client tests usecreate-synced-conn. fix(undo): replay the tx datoms on local graphs #13466'ssynced-graph-undo-replays-semantic-ops-testkeeps its synced part; its part about entries recorded before an upload is replaced by the upload test above.with-datascript-connsno 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 calldonebefore the helper's cleanup ran while a later test was running; the one ofpull-ok-without-checksum-is-accepted-testemptied the client-ops conn in the middle of the new upload test, which passed when run alone.frontend.worker.outliner-undo-property-testfrom 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.TZ=Etc/GMT+12andTZ=Pacific/Kiritimati:frontend.worker.undo-redo-test56 tests, 301 assertions;frontend.worker.db-sync-test236 tests, 2255 assertions;frontend.worker.db-sync-sim-test21 tests, 427 assertions;frontend.worker.sync.client-op-test6 tests, 22 assertions;frontend.worker.db-listener-test14 tests, 51 assertions;frontend.worker.pipeline-test42 tests, 193 assertions;frontend.worker.db-core-test147 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.TZ=Etc/GMT+12(2520 tests, 12614 assertions) and its time-zone-dependent namespaces atTZ=Pacific/Kiritimati(1487 tests, 9173 assertions): 0 failures, 0 errors.Allow edits from maintainers.