Skip to content

fix(db-sync): heal stale local checksum on graph open via covered commit - #13415

Merged
tiensonqin merged 1 commit into
masterfrom
devin/1790266543-db-sync-checksum-unclean-heal
Sep 24, 2026
Merged

tiensonqin merged 1 commit into
masterfrom
devin/1790266543-db-sync-checksum-unclean-heal

Conversation

@tiensonqin

@tiensonqin tiensonqin commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the cli-sync-stress flake seen at https://github.com/logseq/logseq/actions/runs/36001797688/job/107639997944 (same signature on ~7/60 recent runs across unrelated branches — pre-existing, not caused by the PR the job ran on).

Root cause. d/transact! stores graph data to db.sqlite inside the commit (store-after-transact!), then fires run-callbacks → the db listener writes the incremental local checksum via update-local-sync-checksum! into the separate client-ops-/db.sqlite. A process kill in that gap (the stress harness's "Outdated worker cleanup" SIGKILL mid-run) commits the graph data but drops the checksum write. The stored checksum then lags the db by that tx, and because later updates are incremental (update-checksum on the stale base), the client can never converge — reproduces exactly the observed local-checksum ≠ remote-checksum with all txs settled and content identical.

Fix — covered-commit marker. client_op.update-local-checksum repo checksum covered-tx now also stores :db-sync/checksum-covered-tx in sync_meta, recording the commit the checksum covers ((:max-tx (:db-after tx-report)) — taken from the tx-report itself, not live @conn, since listener work can be queued). On graph open, db_core calls db-sync/reconcile-local-checksum! after bootstrap/migration and before listen-db-changes!: when covered-tx ≠ (:max-tx @conn) (missed writes, or legacy graphs with no marker), it recomputes the checksum once via sync-checksum/recompute-checksum and stores it with the current marker — one-shot heal, zero cost on clean opens, and also repairs graphs already carrying a stale checksum.

-- on open (db_core <create-or-open-db!, after migrations/bootstrap, before listener):
(db-sync/reconcile-local-checksum! repo conn)
-- when (and checksum (not= covered-tx (:max-tx @conn))):
--   recomputed = sync-checksum/recompute-checksum @conn
--   log :db-sync/checksum-healed-on-open; update-local-checksum(repo, recomputed, current-tx)

Verified end-to-end on the preserved failing state (corrupted client-ops checksum + worker reopen → local-checksum heals to the recomputed value matching remote).

Test script. cli-concurrent-edit-stress.mjs: the final per-client settle loop now always runs validateClientChecksum + validateClientGraph even when waitForSyncSettled throws — in CI the diagnostic was skipped on the failing client, which is why the logs couldn't distinguish bookkeeping drift from real divergence.

Test coverage

  • New: local-checksum-heals-when-covered-commit-lags-test (lost write → reconcile fixes), local-checksum-untouched-when-covered-commit-current-test (consistent marker → no recompute), checksum-covered-tx-roundtrip-test (marker storage).
  • update-local-checksum gained a covered-tx param; all ~21 existing test call sites updated to pass (:max-tx @conn).
  • Ran: bb dev:test -v frontend.worker.db-sync-test (231 tests/2218 assertions), frontend.worker.db-core-test (147/1601), frontend.worker.sync.client-op-test — 0 failures; clj-kondo clean on all touched files.
  • frontend.worker.db-sync-sim-test run pending in background (call-site updates only, no behavior change).

Notes

  • Reproduced locally: instrumented run caught client-3's worker restarted by "Outdated worker cleanup" and its first post-restart tx logging :db-sync/checksum-incremental-drift, with the assert's throw then freezing the stale value for the remaining ~2000 txs.
  • A separate tolerated flake remains (worker cleanup "not published" settle stalls, pending-local stuck with ws-state: stopped) — orthogonal to this fix.

Link to Devin session: https://app.devin.ai/sessions/7f44848565884cf989b5699c0aa980ea
Open in Devin Desktop: https://app.devin.ai/desktop/session/7f44848565884cf989b5699c0aa980ea?variant=devin
Requested by: @tiensonqin


Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration
devin-ai-integration Bot force-pushed the devin/1790266543-db-sync-checksum-unclean-heal branch 2 times, most recently from 9bee4d4 to 7b48efe Compare September 24, 2026 16:36

@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

The stored local checksum is written post-commit to the client-ops
sqlite file while the graph data is stored to db.sqlite inside
d/transact!, so process death between the graph store and the checksum
write leaves the stored checksum permanently stale: every later
incremental update keeps building on the wrong base and the client can
never converge with the server checksum. This caused the
cli-sync-stress flake where a worker killed by outdated-worker cleanup
restarted with local-checksum != remote-checksum while content was
already converged.

update-local-checksum now also records the graph commit (:max-tx) the
checksum covers. On graph open, reconcile-local-checksum! compares the
stored covered commit with the reopened db's :max-tx and recomputes the
checksum once when they disagree, healing bookkeeping drift from lost
checksum writes regardless of shutdown cleanliness. Also run the stress
script's checksum/graph validators even when the final sync-settle
check fails so the recompute diagnostic is always captured.
@tiensonqin
tiensonqin force-pushed the devin/1790266543-db-sync-checksum-unclean-heal branch from 7b48efe to 0634208 Compare September 24, 2026 17:23
@tiensonqin
tiensonqin merged commit 8edb86a into master Sep 24, 2026
4 of 12 checks passed
@tiensonqin
tiensonqin deleted the devin/1790266543-db-sync-checksum-unclean-heal branch September 24, 2026 17:23
@devin-ai-integration

Copy link
Copy Markdown
Contributor

E2E verification of the checksum-heal fix — passed (tested at 063420841f)

heal-evidence

1. Stale-marker heal — works. Simulated the exact post-SIGKILL lost-write state: sync_meta rewound to checksum=5a9c01469515de9a, covered-tx=536870921 while the graph db sat at max-tx=536870948. On next open the worker logged:

:db-sync/checksum-healed-on-open {:stored-checksum "5a9c01469515de9a"
  :recomputed-checksum "3ed4901ddec23c13", :covered-tx 536870921, :current-tx 536870948}

Recomputed checksum equals the true content checksum; sync_meta was repaired to (3ed4901ddec23c13, 536870948) and sync status showed local == remote, pending 0.

2. Clean path — no recompute. A second open with a consistent marker logged no heal line (verified twice).

3. Baseline regression — clean. cli-concurrent-edit-stress.mjs --clients 3 --concurrency 8 --sync --offline --no-e2ee --max-ops 3000 exited 0: all 3 clients converged to recomputed == local == remote == a37a5a71ba89113d with zero worker churn.

Note on earlier "Sync did not settle" failures (environmental)

Three prior runs failed on settle — root-caused to a stale static/logseq-cli.js bundle (rev f8f4763db3) vs worker 7b48efe992/063420841f: the CLI's revision check terminated the graph worker on every command. Rebuilding both bundles at the same HEAD removed all churn; not related to this fix. Post-failure checksum-diagnostics validators from this PR ran correctly on those failing runs (recomputed == local throughout — the stored checksum was never stale).

Verified by Devin session (testing agent, runtime E2E on local db-sync server + JWKS mock).

devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
…mit (#13415)

The stored local checksum is written post-commit to the client-ops
sqlite file while the graph data is stored to db.sqlite inside
d/transact!, so process death between the graph store and the checksum
write leaves the stored checksum permanently stale: every later
incremental update keeps building on the wrong base and the client can
never converge with the server checksum. This caused the
cli-sync-stress flake where a worker killed by outdated-worker cleanup
restarted with local-checksum != remote-checksum while content was
already converged.

update-local-checksum now also records the graph commit (:max-tx) the
checksum covers. On graph open, reconcile-local-checksum! compares the
stored covered commit with the reopened db's :max-tx and recomputes the
checksum once when they disagree, healing bookkeeping drift from lost
checksum writes regardless of shutdown cleanliness. Also run the stress
script's checksum/graph validators even when the final sync-settle
check fails so the recompute diagnostic is always captured.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
…mit (#13415)

The stored local checksum is written post-commit to the client-ops
sqlite file while the graph data is stored to db.sqlite inside
d/transact!, so process death between the graph store and the checksum
write leaves the stored checksum permanently stale: every later
incremental update keeps building on the wrong base and the client can
never converge with the server checksum. This caused the
cli-sync-stress flake where a worker killed by outdated-worker cleanup
restarted with local-checksum != remote-checksum while content was
already converged.

update-local-checksum now also records the graph commit (:max-tx) the
checksum covers. On graph open, reconcile-local-checksum! compares the
stored covered commit with the reopened db's :max-tx and recomputes the
checksum once when they disagree, healing bookkeeping drift from lost
checksum writes regardless of shutdown cleanliness. Also run the stress
script's checksum/graph validators even when the final sync-settle
check fails so the recompute diagnostic is always captured.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
…mit (#13415)

The stored local checksum is written post-commit to the client-ops
sqlite file while the graph data is stored to db.sqlite inside
d/transact!, so process death between the graph store and the checksum
write leaves the stored checksum permanently stale: every later
incremental update keeps building on the wrong base and the client can
never converge with the server checksum. This caused the
cli-sync-stress flake where a worker killed by outdated-worker cleanup
restarted with local-checksum != remote-checksum while content was
already converged.

update-local-checksum now also records the graph commit (:max-tx) the
checksum covers. On graph open, reconcile-local-checksum! compares the
stored covered commit with the reopened db's :max-tx and recomputes the
checksum once when they disagree, healing bookkeeping drift from lost
checksum writes regardless of shutdown cleanliness. Also run the stress
script's checksum/graph validators even when the final sync-settle
check fails so the recompute diagnostic is always captured.
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