Skip to content

Remove sleep-based eventual-consistency retry loops from NodeService (SQLite is synchronous/ACID)Β #1237

Closed

Description

@malibio

Background (from architectural audit #1228)

B2 β€” Sleep-based eventual-consistency retry loops survive into the SQLite era πŸ”΄
Comments still cite "SurrealDB eventual consistency" but SQLite is synchronous/ACID, so these retries add 50–2000ms latency and mask whether the path is actually correct.
Proposed action: remove or replace with explicit transaction semantics; confirm the stale-sibling scenario can't occur under SQLite.

Cross-cutting theme: business logic in the wrong layer. This is the residue-removal half β€” dead latency and dead error-handling left over from the SurrealDB era (SurrealDB + RocksDB were fully removed; libsql/SQLite is now the only store and it is synchronous and ACID).

Production retry/sleep sites (verified, with reasoning)

All in packages/core/src/services/node_service.rs. Tests live in the same file from ~line 8900; those are addressed separately below.

Site 1 β€” create_node_with_parent sibling-validity retry (lines 2098–2129)

5x loop with sleep(50ms) validating that a position-sibling still exists and shares the same parent, falling back to End if not. Comment: "Retry up to 5 times with 50ms backoff against transient lookup failures during rapid empty-node creation."

Reasoning: Under SQLite, a node written by a prior awaited call is immediately visible to the next read on the same connection/pool β€” there is no replication lag. A sibling that is genuinely absent on attempt 1 will still be absent on attempt 5; retrying only adds up to 250ms. The "transient lookup failure" this guards against was SurrealDB eventual consistency, which no longer exists.
Action: Remove the loop. Do the sibling-validity check once; if invalid, fall back to End exactly as today. Net behavior is identical minus the latency.

Site 2 β€” create_parent_edge sibling-not-found retry (lines 4604–4642)

10x loop with sleep(100ms) (up to 1s) around store.move_node(...), retrying when the error string contains "Sibling not found".

Reasoning β€” this branch is dead code. SqliteStore::move_node (sqlite_store.rs:1224) never returns a "Sibling not found" error: when the insert_after sibling is absent from the sibling list it silently falls back to last position (sqlite_store.rs:1301–1304). Confirmed: the string "Sibling not found" appears in the entire packages/core/src tree only at node_service.rs:4626 β€” the matcher itself. The retry condition can never fire; the loop always succeeds on attempt 1 or returns a real error immediately.
Action: Delete the retry loop. Call store.move_node(...) once; on Err, return NodeServiceError::query_failed. Remove the now-unused attempt_count/last_error bookkeeping.

Site 3 β€” create_parent_edge position-verification + reorder loop (lines 4644–4721)

A "defensive guard": after the move, a 20x loop (sleep(50ms), up to 1s) re-reads children to confirm the child landed immediately after after_id; if not, it re-issues move_node and runs a nested 10x sleep(50ms) re-verify loop. On exhaustion it logs a warning and proceeds with an unconfirmed order. Worst case ~2s of sleeps.

Reasoning: This is the worst SurrealDB-era artifact β€” it polls for a write to "become visible." Under SQLite the order written by move_node is visible on the very next get_children with no delay. The fractional-order computation in move_node is deterministic and committed before it returns. If the child is in the wrong position after a single awaited move_node, that is a real bug in move_node/ordering, not a visibility lag β€” and a sleep-retry would mask it, not fix it (the audit's exact concern).
Action: Delete the verification/reorder loop entirely. Trust the single move_node result. Before deleting, add a test (see Testing) that creates a parent with several children and inserts via InsertPosition::After(sibling) for each position (first/middle/last), asserting get_children returns the exact expected order with zero retries. If that test passes deterministically, the guard is provably unnecessary. If it ever fails, the bug is in move_node/FractionalOrderCalculator and must be fixed there β€” do not reintroduce the sleep.

"Explicit transaction semantics"

create_parent_edge's body is a single store.move_node call (atomic at the store layer) followed by an event emit. After removing the loops it is already effectively transactional β€” one atomic store mutation, then emit. No multi-statement transaction needs to be introduced here; the store call is the transaction. Document this in the PR (i.e., "explicit transaction semantics" here means: rely on the store's atomic move_node and stop polling, not wrap anything new). If during implementation you find create_node_with_parent issues multiple store mutations that should be atomic together, raise it β€” but do not expand scope speculatively.

Test-section sleeps (lines ~8916–9636)

These are in the in-file test module: sleep(...) calls at 8960, 9065, 9078, 9522, 9535, 9562, 9575, 9617, 9636, with comments at 9080/9194 ("with retry for eventual consistency"). They exist because the tests were written to tolerate SurrealDB lag.

Action: Remove the test-side sleeps/retry-waits after the production retries are gone. Under SQLite, the assertion immediately after an awaited write must hold without waiting. Rewrite these tests to assert directly (no sleep, no retry-until loop). If any test genuinely fails without its sleep, that is a real ordering/visibility bug surfaced by this work β€” investigate and fix the production code; do not re-add the sleep. Keep the assertions; drop only the waiting.

Acceptance criteria

  • Site 1 (2098–2129): retry loop removed; single sibling-validity check with identical End fallback.
  • Site 2 (4604–4642): dead "Sibling not found" retry loop removed; single move_node call; unused bookkeeping removed.
  • Site 3 (4644–4721): position-verification/reorder polling loop removed; result of the single move_node trusted.
  • No tokio::time::sleep / backoff loop remains in production paths of node_service.rs (grep clean for sleep outside the test module).
  • Stale comments referencing "eventual consistency" / "SurrealDB" / "transient lookup" removed or corrected to reflect SQLite synchronous semantics.
  • Test-section sleeps/retry-waits removed; assertions preserved and passing without waiting.
  • The stale-sibling / wrong-position scenario is proven impossible or covered by a test (see below) β€” not just assumed.
  • NO #[allow(dead_code)], NO commented-out loops, NO "kept for safety" shims. Delete outright.
  • cargo clippy --workspace --all-targets -- -D warnings clean.

Testing requirements

The danger of this issue: removing a retry that was masking a real ordering bug. Guard against it:

  • Add a deterministic ordering test (no sleeps): build a parent with children, insert new children via InsertPosition::After(...) at first/middle/last positions and via Beginning/End, assert get_children returns exact expected order on the first read every time. This is the proof that Site 3's guard was unnecessary.
  • Add/keep a test for the stale-sibling fallback (Site 1): position-after a sibling that has been deleted or reparented β†’ new node lands at End, on the first attempt.
  • cargo test -p nodespace-core green with the rewritten (sleep-free) tests.
  • cargo test --workspace green (daemon gRPC create/move round-trips β€” these are the rapid-creation paths Site 1 cited).
  • bun run test:all green, with attention to rapid node-creation flows (Enter, Enter, Enter) and indent/outdent which exercise create_parent_edge. Verify no ordering regressions and note the latency improvement.
  • No new failures vs. baseline.

Coordination with #1238 (NodeService decomposition)

This issue lands FIRST. It is small, deletion-only, and removes exactly the retry/verify machinery that the B1 god-object split would otherwise have to carefully relocate during its Hierarchy-service extraction. #1238 will defer its Hierarchy phase until this PR is merged, then rebase and move the simplified functions. Keep this PR tightly scoped to the retry-loop removal in create_node_with_parent and create_parent_edge (plus the test sleeps) β€” do not start reorganizing the service structure; that is #1238's job.

Startup / PR sequence (NodeSpace standard)

  1. git status + git pull origin main on primary checkout, then EnterWorktree({name: "issue-<N>-remove-eventual-consistency-retries"}).
  2. Inside worktree: bun install, baseline cargo test -p nodespace-core + bun run test, bun run gh:comment <N> "baseline: ...", bun run gh:assign <N> "@me", bun run gh:status <N> "In Progress".
  3. Implement deletions + new ordering tests. Use tracing not println!. No lint suppression.
  4. Before PR: cargo test --workspace, bun run test:all (no new failures), bun run quality:fix then commit (quality:fix runs cargo fmt --all and may touch unrelated files β€” git status and revert strays first).
  5. bun run gh:pr <N>. Run /pragmatic-code-review. After approval: gh pr view <PR#>, ExitWorktree({action: "remove", discard_changes: true}), gh pr merge <PR#> --squash --delete-branch, git pull origin main, bun run gh:status <N> "Done".

Spun off from #1228 (architectural audit), finding B2.

Activity

  1. malibio commented on May 29, 2026

    @malibio
    CollaboratorAuthor

    Baseline: Frontend 131 files / 3924 tests passed. Rust (nodespace-core) 132 passed, 0 failed.

  2. self-assigned this
    on May 29, 2026
  3. added 2 commits that reference this issue on Jun 22, 2026
    af9950e
    afcce16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions