Skip to content

C3a: Delete frontend outdent ordering math — trust the daemon's authoritative order via relationship events #1259

Closed

Description

@malibio

Spun off from architectural-audit tracking issue #1228, finding C3 (hierarchy/ordering business logic in the frontend). This is the headline piece: ordering math. Container rules → #C3B; atomic child-transfer-on-split → C3c (deferred).

Background — what's actually still client-side

The audit says "move ordering math + depth into the daemon; return order hints in RPC responses." Investigation shows the write path already delegates ordering to the daemon — the residue is narrower than the finding implies:

  • outdentNode already calls backendAdapter.moveNode(nodeId, version, newParentId, { type: 'after', siblingId: oldParentId }) (reactive-node-service.svelte.ts:1138-1141). The daemon's move_node resolves InsertPosition::After and computes the fractional order server-side via FractionalOrderCalculator (packages/core/src/services/node_service/mod.rs:1621-1628, sqlite_store.rs:1224-1307).
  • The authoritative order is returned from the store as actual_order and emitted in the RelationshipUpdated event as {"order": actual_order} (mod.rs:1631-1641).
  • The frontend already consumes that authoritative order: hierarchy-sync.ts:56 reads payload.order from relationship:updated and applies it via structureTree (C2/C2: Make structureTree single source of truth — unify dual sync paths + invariant check #1250).

So what remains frontend-side is purely the optimistic pre-echo: calculateOutdentInsertOrderPure() (reactive-node-service.svelte.ts:61-73) and its wrapper calculateOutdentInsertOrder() (83-86) re-derive a fractional order client-side to feed the optimistic structureTree.moveInMemoryRelationship(oldParent, newParent, nodeId, insertOrder) at three sites (lines 1033, 1060, 1085). This is a second, divergent implementation of the daemon's ordering rule — exactly the audit's "if the daemon's rules change, the frontend diverges; can't validate end-to-end."

Note FractionalOrderCalculator is now deterministic (jitter removed in B7/#1256), but hierarchy-sync.ts:40 still adds Math.random() * 0.001 on the append fallback — a second place the frontend invents order values. That should also stop.

Target architecture

The frontend stops computing fractional order values entirely. Optimistic placement expresses positional intent (the same InsertPosition semantics the RPC uses), and the authoritative numeric order arrives via the relationship:updated event and reconciles the optimistic placement.

  1. Delete calculateOutdentInsertOrderPure + calculateOutdentInsertOrder and the three insertOrder-passing call sites. The optimistic structureTree.moveInMemoryRelationship(oldParent, newParent, nodeId) keeps placing the node under the new parent for instant feedback, but without a hand-computed numeric order.
  2. Optimistic placement uses relative intent ("insert after oldParentId"), not an absolute float. reactive-structure-tree.svelte.ts already supports order-bearing inserts; add/confirm an insert-after-sibling placement that doesn't require the caller to invent a float. The authoritative float lands when applyHasChildUpdated fires.
  3. Remove the Math.random() jitter append fallback in hierarchy-sync.ts:39-40 — for has_child the daemon always supplies a real order; preserve existing order or log an anomaly, never invent a random float.

On "return order hints in RPC responses": in event-connected modes (Tauri invoke + browser SSE), the relationship:updated event already carries the authoritative order, so a response hint is redundant. Decision: do NOT add fields to ReorderNodeResponse/NodeResponse. Rely on the event as the single reconciliation channel (consistent with C2). Proto stays untouched.

Exact changes

reactive-node-service.svelte.ts:

  • Delete calculateOutdentInsertOrderPure (61-73), calculateOutdentInsertOrder (83-86), and the doc block (41-60).
  • At lines 1033, 1060, 1085: drop the insertOrder argument; optimistic moveInMemoryRelationship(oldParentId, newParentId, nodeId) places under the new parent with relative-after intent.
  • Remove the now-unused sibling-transfer integer-order assignment at line ~1103 (i + 1) if relative-intent placement covers it (the daemon already orders these via the per-sibling moveNode).

hierarchy-sync.ts:

  • Remove the Math.random() * 0.001 jitter append fallback (39-40). If no order is supplied for a has_child create, preserve existing order or log a warning (matching the relationship:updated warn at 58) — don't invent a value.

Daemon/proto: none. InsertPosition (#1216) already expresses Beginning/End/After; move_node already returns the moved node and emits RelationshipUpdated{order}.

Acceptance criteria

  • calculateOutdentInsertOrderPure / calculateOutdentInsertOrder no longer exist anywhere in the frontend.
  • No frontend code computes a fractional order number for has_child placement; optimistic updates use positional intent only; authoritative order arrives via relationship:updated.
  • The Math.random() jitter in hierarchy-sync.ts is gone; no invented order values anywhere.
  • Outdent (single node, node-with-siblings-below, outdent-to-root) produces correct visual ordering optimistically AND after event reconciliation, in both Tauri and browser/HTTP modes.
  • No regression in the in-flight-CREATE outdent paths (lines ~1019-1075).

Testing — the audit's core complaint is "can't validate end-to-end"

  • End-to-end ordering-parity test (required): drive an outdent through the daemon RPC and assert the resulting has_child order sequence is identical to the optimistic frontend placement after relationship:updated reconciliation. Rust integration test on the daemon move path + a Happy-DOM test (bun run test, never bun test) asserting hierarchy-sync applies the daemon-supplied order and the optimistic placement converges to it.
  • Vitest covering the three outdent variants and both in-flight-CREATE branches; assert structureTree child order matches the daemon's FractionalOrderCalculator output for the same scenario.
  • cargo test for move_node with InsertPosition::After(oldParent) returns the expected actual_order (regression-lock the contract the frontend now depends on).
  • bun run check clean; createLogger; no any/{@html}/lint-suppression.

Risks

Coordination

Standard NodeSpace startup / PR sequence

git status + git pull origin main, EnterWorktree({name: "issue-<N>-c3a-outdent-ordering"}), bun install, bun run test baseline, gh:comment/gh:assign/gh:status. Before PR: bun run test:all (no new failures), bun run quality:fix + commit (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 C3 (C3a — ordering math).

Activity

  1. malibio commented on May 30, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 132 files, 3873 tests passed

  2. self-assigned this
    on May 30, 2026
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