Skip to content

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

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1253-remove-ordering-jitter
May 30, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1253-remove-ordering-jitter

Conversation

@malibio

@malibio malibio commented May 30, 2026 •

Copy link
Copy Markdown
Collaborator

Removes SurrealDB-era jitter from fractional ordering. Under SQLite's serialized writes, concurrent inserts cannot interleave mid-computation, so jitter was dead weight everywhere.

Changes

  • calculate_order is now pure and deterministic — returns exact midpoint/endpoint values
  • generate_jitter() deleted entirely
  • add_to_collection: wrapped SELECT + INSERT in a transaction (closes the one genuine TOCTOU window found during analysis)
  • Doctests assert exact values; approx_eq helper and jitter-uniqueness test replaced with exact-value + strictly-increasing-orders tests

Concurrency analysis per call site

Call site Pattern Window? Verdict
create_child_node_atomic (~line 420) SELECT outside tx, INSERT inside tx Read before tx open Daemon serializes callers; no real-world collision risk
move_node (~lines 1290–1307) Siblings queried in Rust, UPDATE inside tx Same Serialized by daemon
add_to_collection (~line 2579) SELECT + INSERT as two independent statements Yes Fixed: now wrapped in a single transaction
bulk_add_to_collections (~line 2843) Already in transaction None Correct

Conclusion: jitter was not providing collision protection on any path. The one path with a genuine concurrent-write window (add_to_collection) is now protected by a transaction, which is the correct fix.

Closes #1253

…eterministic (closes #1253)

Under SQLite's serialized writes, two inserts cannot interleave mid-computation,
so jitter was dead weight. All calculate_order call sites and the generate_jitter
usage in add_to_collection are removed. Doctest examples now assert exact values;
approx_eq helper and the uniqueness-via-jitter test are replaced with a
deterministic + strictly-increasing-orders test.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio

malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This change correctly removes the SurrealDB-era jitter from fractional ordering. The core logic is sound: calculate_order is now pure and deterministic, all tests pass with exact-value assertions, and cargo clippy -D warnings is clean. It is a net improvement and can merge with one noted concern and one documentation gap addressed below.


Findings

[Improvement] add_to_collection has a genuine read-then-write TOCTOU window (sqlite_store.rs ~2565–2590)

The issue explicitly asked whether a read-base-then-write-order window exists. It does — in add_to_collection:

get_next_member_order(collection_id)   // SELECT … LIMIT 1
    ↓
self.db.execute(INSERT … order = new_order)   // separate statement

Both statements execute against a single libsql::Connection under WAL mode (PRAGMA journal_mode = WAL). In WAL mode SQLite allows concurrent readers alongside writers, but write-write conflicts are serialized at the page level. However, add_to_collection issues two independent statements with no surrounding BEGIN/COMMIT. An async executor could interleave two concurrent callers between the SELECT and the INSERT, causing both to read the same last_order and assign identical new_order values to different members.

The PR description states only "Closes #1253" with no concurrency analysis, which was an explicit acceptance criterion: "The concurrency-collision question is answered with evidence (which case applies, per call site) and recorded in the PR."

The bulk_add_to_collections path correctly uses a transaction, and the create_child_node_atomic path wraps both statements in a transaction. The add_to_collection path is the outlier.

Suggested fix: wrap the SELECT + INSERT in add_to_collection in a self.db.transaction() block, matching the pattern already used by bulk_add_to_collections (line ~2841) and create_child_node_atomic (line ~434).

This existed before this PR (jitter was masking it), but since the issue's scope was specifically to answer this question per call site, it should be addressed or explicitly deferred.

[Improvement] PR description omits the required concurrency analysis

Issue #1253 AC #1 states: "The concurrency-collision question is answered with evidence (which case applied, per call site) and recorded in the PR." The PR body contains only "Closes #1253". The analysis should be documented — even a brief table noting which paths are in transactions (create_child_node_atomic, move_node, bulk_add_to_collections) vs. the unprotected add_to_collection path satisfies the requirement and aids future maintainers.


Nitpicks

  • Nit fractional_ordering.rs:21 — (None, Some(next)) => next - 1.0 can yield order ≤ 0.0 if next is between 0.0 and 1.0 (possible after a rebalance that starts from 1.0 and then a before-first insert). This is a pre-existing edge case not introduced here, but worth tracking. rebalance() currently uses integer values starting at 1.0, so in practice next at index 0 is always ≥ 1.0 after a rebalance. Fine for now, but a comment noting the invariant assumption ("first item order is always ≥ 1.0") would prevent future surprises.

  • Nit test_rapid_sequential_inserts_produce_distinct_orders is a good addition, but it only tests the append path (Some(last), None). A companion case testing the midpoint path (successive between-inserts that will trigger rebalancing) would give fuller coverage of the property the test is meant to validate.


Verdict

The core change — removing jitter from calculate_order, deleting generate_jitter, updating tests to exact-value assertions — is correct and clean. The add_to_collection TOCTOU concern is worth fixing (it's a small change, two-line transaction wrap) before merge, or the author should explicitly document why the concurrent-add case is acceptable (e.g., "collection membership order collisions are benign; duplicates are prevented by the idempotency check"). The PR description gap is easy to address with a comment edit.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review posted as a comment above. Key findings: (1) add_to_collection has a genuine read-then-write TOCTOU window not wrapped in a transaction — this was specifically called out as needing analysis in the acceptance criteria; (2) PR description omits the required per-call-site concurrency analysis from AC #1. Both are straightforward to address. Core jitter removal logic is correct.

… window

The review identified that add_to_collection had an unprotected read-then-write
window: get_next_member_order (SELECT) and the INSERT ran as two independent
statements. Under concurrent async callers both could read the same last_order
and write duplicate order values for different members.

Inlines the order SELECT inside the transaction, matching the pattern used by
bulk_add_to_collections and create_child_node_atomic.

Concurrency analysis per call site:
- create_child_node_atomic: SELECT before tx open, but daemon serializes callers
- move_node: siblings queried in Rust then UPDATE inside tx; serialized by daemon
- add_to_collection: was unprotected — now wrapped in a single transaction
- bulk_add_to_collections: already wrapped in transaction (correct)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio

malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator Author

Review feedback addressed

✅ Addressed: 2 findings

1. add_to_collection TOCTOU window — fixed

Wrapped the idempotency-check SELECT + order SELECT + INSERT in a single self.db.transaction(). The order computation is now inlined against tx (same pattern as bulk_add_to_collections), so no concurrent caller can read the same last_order and write a duplicate order value.

2. Missing concurrency analysis — added to PR description

PR description now has a table documenting the verdict for each calculate_order call site. Short answer: all Rust-side paths are protected by daemon serialization; add_to_collection was the one genuine window and is now transactional; bulk_add_to_collections was already correct.

⏭️ Skipped nitpicks:

  • next - 1.0 edge case when next < 1.0: pre-existing, rebalance() guarantees first item ≥ 1.0, low value to add a comment mid-function
  • Additional midpoint test in test_rapid_sequential_inserts_produce_distinct_orders: that path is already exercised by test_calculate_order_between with exact assertions; the sequential test specifically validates the append path

@malibio
malibio merged commit 8696cd8 into main May 30, 2026
@malibio
malibio deleted the worktree-issue-1253-remove-ordering-jitter branch May 30, 2026 12:17
malibio added a commit that referenced this pull request Jun 22, 2026
…eterministic under SQLite (#1256)

* B7: Remove SurrealDB-era ordering jitter — make fractional ordering deterministic (closes #1253)

Under SQLite's serialized writes, two inserts cannot interleave mid-computation,
so jitter was dead weight. All calculate_order call sites and the generate_jitter
usage in add_to_collection are removed. Doctest examples now assert exact values;
approx_eq helper and the uniqueness-via-jitter test are replaced with a
deterministic + strictly-increasing-orders test.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Address review: wrap add_to_collection in transaction to close TOCTOU window

The review identified that add_to_collection had an unprotected read-then-write
window: get_next_member_order (SELECT) and the INSERT ran as two independent
statements. Under concurrent async callers both could read the same last_order
and write duplicate order values for different members.

Inlines the order SELECT inside the transaction, matching the pattern used by
bulk_add_to_collections and create_child_node_atomic.

Concurrency analysis per call site:
- create_child_node_atomic: SELECT before tx open, but daemon serializes callers
- move_node: siblings queried in Rust then UPDATE inside tx; serialized by daemon
- add_to_collection: was unprotected — now wrapped in a single transaction
- bulk_add_to_collections: already wrapped in transaction (correct)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
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.

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

1 participant