`batch-transact-with-temp-conn!` leaves `:max-tx` behind its datoms, so the next transaction reuses a tx id

Đang mở Phù hợp với người mới
#1,551 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

@tiensonqin đang làm issue này rồi.

Từ ngày 21/11/2026.

  • #14082 của @tiensonqin — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
70/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
clojure
Lĩnh vực
database

Hướng nghiên cứu

Bắt đầu tại deps/db/src/logseq/db.cljs, ở batch-transact-with-temp-conn! (L273) và lệnh gọi transact! của nó (L312): so sánh cách các bản ghi Datom được phát lại giữ nguyên tx id tạm thời trong khi :max-tx chỉ tăng một lần. Chạy bài kiểm tra tái hiện tại src/test/logseq/db/batch_transact_tx_id_repro_test.cljs với các lệnh trong issue; áp dụng transact không có tx-ops được đề xuất rồi chạy lại. Hoàn thành khi cả hai assertion đều đạt và tx id của giao dịch tiếp theo chưa được sử dụng; get-storage-conn trong deps/db/src/logseq/db/common/sqlite.cljs là ngữ cảnh liên quan.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Search first
  • I searched and no similar issues were found
What Happened?

Continuing the Opus 5.5 epic from last week. Claude follows.


ldb/batch-transact-with-temp-conn! runs an operation on a temporary conn, possibly as several transactions, and then replays the collected datoms into the real conn with one transact!. The replayed datoms keep the tx ids of the temporary transactions, but the real conn's :max-tx advances only once. When the operation made more than one inner transaction, the conn's :max-tx ends up lower than the tx of datoms it holds, and the next transaction takes a tx id that earlier datoms already carry.

outliner-op/apply-ops! goes through this function, so ordinary edits reach it: in our test an insert followed by a move produced a transaction report whose datoms carried two tx ids while :max-tx covered only the first.

Reproduce the Bug

Save this test as src/test/logseq/db/batch_transact_tx_id_repro_test.cljs, then run

clojure -M:test compile test
node static/tests.js -n logseq.db.batch-transact-tx-id-repro-test
(ns logseq.db.batch-transact-tx-id-repro-test
  (:require [cljs.test :refer [deftest is]]
            [datascript.core :as d]
            [logseq.db :as ldb]))

(deftest batch-transact-with-temp-conn-keeps-tx-ids-unique-test
  (let [conn (d/create-conn {})]
    (d/transact! conn [{:db/id -1 :item/name "a"}])
    (ldb/batch-transact-with-temp-conn!
     conn {}
     (fn [temp-conn _]
       (d/transact! temp-conn [{:db/id -1 :item/name "b"}])
       (d/transact! temp-conn [{:db/id -1 :item/name "c"}])))
    (let [db @conn
          max-datom-tx (reduce max (map :tx (d/datoms db :eavt)))]
      (is (>= (:max-tx db) max-datom-tx))
      (let [new-tx (:tx (first (:tx-data (d/transact! conn [{:db/id -1 :item/name "d"}]))))]
        (is (not-any? #(= new-tx (:tx %)) (d/datoms db :eavt)))))))
Expected Behavior

After the batch, :max-tx is at least the tx of every datom, and the next transaction gets a tx id that no earlier datom carries.

Actual on master logseq/logseq@9af1a190bc:

FAIL expected: (>= (:max-tx db) max-datom-tx)
       actual: (not (>= 536870914 536870915))
FAIL expected: (not-any? (fn* [p1__155722#] (= new-tx (:tx p1__155722#))) (d/datoms db :eavt))
       actual: (not (not-any? #object[Function] (#datascript/Datom [1 :item/name "a" 536870913 true] #datascript/Datom [2 :item/name "b" 536870914 true] #datascript/Datom [3 :item/name "c" 536870915 true])))

The next transaction takes tx 536870915, which datom 3 already carries.

Screenshots

No response

Files

No response

Browser, Desktop or Mobile Platform Information

Unit test on master logseq/logseq@9af1a190bc (2026-11-19), Node 24, Linux x86_64. No UI involved.

Additional Context

Cause. batch-transact-with-temp-conn! (logseq/db.cljs#L273) collects the temporary conn's tx-data as Datom records and passes them to transact! (L312). Datascript inserts a Datom record with its own tx, and increments :max-tx once per transact!.

Related code. get-storage-conn (common/sqlite.cljs#L23) already raises :max-tx on restore to the largest tx of the tail's datoms (tail-max-tx), and its docstring describes these multi-tx entries. That covers a reopen from the tail, but not the conn in memory, and not a full store, which writes the lower :max-tx into the root. logseq/logseq#13911 moves that restore code without changing this.

Possible effects (not verified). The worker pipeline stamps :block/tx-id with (inc (:max-tx db-after)) (pipeline.cljs#L743), so two operations can stamp the same revision. Anything that keys data by tx id can merge two transactions.

Suggested fix. Transact the collected changes as ops without their tx, so the final transact! stamps them all with one new tx id:

(transact! conn
           (mapv (fn [d] [(if (:added d) :db/add :db/retract) (:e d) (:a d) (:v d)]) tx-data)
           (transform-tx-meta tx-meta))

On our fork this makes the test above pass, the whole unit-test suite passes with it (2,744 tests, bb dev:run-test-namespaces), and a storage property test that checks :max-tx after every transaction (100 random sequences with write failures and crashes) no longer finds the problem. I can open a PR with the change and the test.

Are you willing to submit a PR? If you know how to fix the bug.
  • I'm willing to submit a PR (Thank you!)
Ngôn ngữ chính
Không có dữ liệu ngôn ngữ
Star
29
Fork
2
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của logseq/db-test

Tất cả issue của logseq/db-test

Issue tương tự

Thêm issue về Databases

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.