Repository navigation
Description
Spun off from architectural-audit tracking issue #1228, finding B7. Backend-only; no overlap with the in-flight frontend C-cluster work.
Background (from audit #1228)
B7 — Fractional-ordering jitter is a SurrealDB-era artifact 🟡
db/fractional_ordering.rsadds random jitter "to prevent collisions under eventual consistency (SurrealDB/RocksDB)." Under SQLite's serialized writes this is unnecessary and complicates rebalancing.
Action: verify and remove; make ordering deterministic.
SurrealDB + RocksDB were fully removed; libsql/SQLite is now the only store, with serialized writes and synchronous ACID semantics.
Verified current state (the audit framing needs a correction)
packages/core/src/db/fractional_ordering.rs:
calculate_order(prev, next)(line ~21) computes the midpoint/endpoint base then addsgenerate_jitter()(line ~32). Its doc comment now says jitter is "to prevent exact order collisions when multiple insertions happen concurrently" — not the SurrealDB framing the audit quoted. So before deleting, the real question is: can two concurrent inserts under SQLite produce the same base order?generate_jitter()(line ~69) returns[0.0, 0.001)fromtime_nanos + process-counter. Issue Bug: Flaky Rust tests - RocksDB lock and collection ordering #865 comment says it was extracted specifically for use in atomic SQL queries — so it has a second consumer beyondcalculate_order.
Two consumers — verified:
calculate_order(Rust-side order computation) — used atsqlite_store.rs:420, 422, 1290, 1293, 1296, 1299, 1303, 1307, 2549, 2551(insert/move/reorder paths).- Direct
generate_jitter()in an atomic SQL path —sqlite_store.rs:2579-2581:let jitter = generate_jitter(); let new_order = base_order + jitter;.
The actual task
Determine whether jitter is still load-bearing under SQLite, then act:
-
Analyze the concurrency claim. Under SQLite's serialized writes, do the insert/move paths compute
baseorder and persist it within a single serialized transaction, or is there a read-base-then-write-order window where two writers could both read the same neighbors and compute the same base?- If every order computation + write happens inside one serialized write transaction → no two writers can interleave, base orders cannot collide, and jitter is dead weight. Remove it.
- If there IS a read-then-write window (e.g. base computed in Rust from a prior
SELECT, then written in a later statement) → jitter is masking a real TOCTOU. Removing it blindly would reintroduce collisions. In that case, fix the window (compute + write atomically, or compute order in SQL) rather than keep jitter. Document which case applies. - The
sqlite_store.rs:2579atomic-SQL path is the most likely place jitter still does real work — assess it specifically.
-
Remove jitter where it's provably unnecessary.
calculate_ordershould return the deterministic base (0.5,1.5,n+1.0, etc.) — exactly the values its own doctest examples claim ("=> ~0.5", "=> ~1.5"). Make them exact, not approximate. -
Update
needs_rebalancing/rebalanceand tests. The test helperapprox_eq(tolerance 0.01) and comments like "jitter adds up to 0.001" / "jitter ensures uniqueness" exist only to tolerate jitter. With deterministic ordering, assert exact values. Theneeds_rebalancingprecision threshold (0.0001) andrebalancestill serve a real purpose (dense midpoint insertions legitimately shrink gaps) — keep them, but verify their tests don't depend on jitter. -
If jitter must stay for the atomic-SQL concurrency case, then this issue's outcome is: remove it from
calculate_order(the Rust path, where ordering should be deterministic) and keep + correctly documentgenerate_jitteronly for the SQL-atomic path, with the comment corrected from "eventual consistency" to the real reason. Decide per-call-site; do not leave the misleading SurrealDB framing anywhere.
Acceptance criteria
- The concurrency-collision question is answered with evidence (which case applies, per call site) and recorded in the PR.
-
calculate_orderis deterministic (no jitter) OR, if a genuine concurrency window exists, that window is fixed (atomic compute+write) so jitter isn't needed there. - Any
generate_jitterusage that remains is justified (atomic-SQL path only) and its comment no longer references SurrealDB/RocksDB/"eventual consistency." - Doctest examples in
calculate_orderassert exact values, not "~approx (with jitter)". - Test helpers (
approx_eq) and comments asserting "jitter ensures uniqueness" are removed/replaced with exact-value assertions. - No
#[allow(...)], no dead code left behind. Delete what's removed. -
cargo clippy --workspace --all-targets -- -D warningsclean.
Testing
-
cargo test -p nodespace-coregreen with exact-value ordering assertions. - Add/keep a test that rapid sequential inserts at the same position produce strictly increasing, distinct, deterministic orders (the property jitter claimed to provide — prove it holds without jitter under serialized writes).
- If a concurrency window was found and fixed: a test (or documented reasoning) that two inserts at the same anchor don't collide.
-
cargo test --workspacegreen (move/reorder round-trips through daemon).
Coordination
- Backend-only — parallel-safe with the in-flight frontend C1a (C1a: Consolidate the node-CRUD transport layer — fold tauri-commands node half into backendAdapter #1251) and any other frontend work.
- Touches the same ordering subsystem as C3 (move ordering logic to the daemon — not yet filed). C3 isn't started, so no collision today; but whoever picks up C3 should build on this issue's deterministic ordering. Note on Architectural audit (post-daemon + post-SQLite): structural findings & improvement plan #1228.
Standard NodeSpace startup / PR sequence
EnterWorktree({name: "issue-<N>-remove-ordering-jitter"}); bun install; baseline cargo test -p nodespace-core; bun run gh:assign/gh:status. Before PR: cargo test --workspace + bun run test:all (no new failures), bun run quality:fix (revert unrelated cargo fmt churn via git status), bun run gh:pr <N>, /pragmatic-code-review, then ExitWorktree + gh pr merge --squash --delete-branch.
Spun off from #1228 (architectural audit), finding B7.
Activity
malibio commentedon May 30, 2026
Frontend: baseline not applicable (backend-only issue). Rust baseline: cargo test -p nodespace-core — 132 passed, 0 failed.
B7: Remove SurrealDB-era fractional-ordering jitter — make ordering d…
B7: Remove SurrealDB-era fractional-ordering jitter — make ordering d…