Skip to content

B7: Remove SurrealDB-era fractional-ordering jitter — make ordering deterministic under SQLite #1253

Closed

Description

@malibio

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.rs adds 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 adds generate_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) from time_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 beyond calculate_order.

Two consumers — verified:

  1. calculate_order (Rust-side order computation) — used at sqlite_store.rs:420, 422, 1290, 1293, 1296, 1299, 1303, 1307, 2549, 2551 (insert/move/reorder paths).
  2. 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:

  1. Analyze the concurrency claim. Under SQLite's serialized writes, do the insert/move paths compute base order 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:2579 atomic-SQL path is the most likely place jitter still does real work — assess it specifically.
  2. Remove jitter where it's provably unnecessary. calculate_order should 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.

  3. Update needs_rebalancing / rebalance and tests. The test helper approx_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. The needs_rebalancing precision threshold (0.0001) and rebalance still serve a real purpose (dense midpoint insertions legitimately shrink gaps) — keep them, but verify their tests don't depend on jitter.

  4. 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 document generate_jitter only 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_order is 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_jitter usage that remains is justified (atomic-SQL path only) and its comment no longer references SurrealDB/RocksDB/"eventual consistency."
  • Doctest examples in calculate_order assert 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 warnings clean.

Testing

  • cargo test -p nodespace-core green 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 --workspace green (move/reorder round-trips through daemon).

Coordination

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

  1. malibio commented on May 30, 2026

    @malibio
    CollaboratorAuthor

    Frontend: baseline not applicable (backend-only issue). Rust baseline: cargo test -p nodespace-core — 132 passed, 0 failed.

  2. self-assigned this
    on May 30, 2026
  3. added a commit that references this issue on May 30, 2026
    8696cd8
  4. added a commit that references this issue on Jun 22, 2026
    47c4c8e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions