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

オープン 初心者向け
#1,551 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@tiensonqin がすでに取り組んでいます。

2026年11月21日 から。

  • #14082 @tiensonqin による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
70/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
clojure
領域
database

調査の方向性

deps/db/src/logseq/db.cljs の batch-transact-with-temp-conn!(L273)と、その transact! 呼び出し(L312)から始めてください。再生される Datom レコードが一時的な tx ID を保持している一方で、:max-tx が一度だけ進むことを比較してください。issue にあるコマンドを使って src/test/logseq/db/batch_transact_tx_id_repro_test.cljs の再現テストを実行し、提案された tx なしの ops の transact を適用して再実行してください。両方のアサーションが通り、次のトランザクションの tx ID が未使用であれば完了です。deps/db/src/logseq/db/common/sqlite.cljs の get-storage-conn は関連するコンテキストです。

索引モデルが issue の本文から書いたものです。

説明

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!)
主要言語
言語のデータがありません
スター
29
フォーク
2
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

logseq/db-test のほかの issue

logseq/db-test の issue をすべて見る

似ている issue

Databases の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。