Skip to content

Sibling node order not preserved after page refresh #657

Description

@malibio

Overview

Nodes created via Enter key display in correct order during the session, but after page refresh they appear in a different order. This indicates the sibling ordering information is not being persisted to the database during node creation.

Problem Statement

User Experience:

  1. User creates nodes A, B, C, D via Enter key (as children of node A)
  2. During session: Nodes display as A > B, C, D (correct order)
  3. After refresh: Nodes display as A > C, D, B (B moved to end)

Database State:

  • Nodes are created with parentId but missing insertAfterNodeId
  • Backend doesn't know where to insert new nodes in sibling order
  • Database stores incorrect order values

Root Cause

Location: packages/desktop-app/src/lib/services/reactive-node-service.svelte.ts (createNode function, line 242-267)

The createNode function creates a Node object with parentId but does NOT include insertAfterNodeId. When the node is persisted via sharedNodeStore.setNode() (line 267), the backend receives only the parent but no sibling positioning information.

Why it happens:

  • Frontend calculates correct order for ReactiveStructureTree (lines 274-301) for immediate UI display
  • But this order is NOT sent to backend during persistence
  • Backend has no insertAfterNodeId to determine where to insert the node among siblings
  • Result: Backend appends to end or uses default ordering

Evidence

Commit 3241e710 on old fix/multiple-bugs branch had the fix:

  • Added insertAfterNodeId field to CreateNodeInput interface
  • ReactiveNodeService.createNode() calculated and passed insertAfterNodeId
  • Backend received correct positioning information

This fix was NOT included in PR #655 merge (commit dbe232ef), causing a regression.

Proposed Solution

Add insertAfterNodeId to the Node object created in ReactiveNodeService.createNode() and ensure it's passed to the backend during persistence.

Changes Required

1. Modify ReactiveNodeService.createNode() (reactive-node-service.svelte.ts):

// Calculate insertAfterNodeId for backend
const insertAfterNodeId = insertAtBeginning ? null : afterNodeId;

// Create Node with insertAfterNodeId
const newNode: Node & { insertAfterNodeId?: string | null } = {
  id: nodeId,
  nodeType: nodeType,
  content: initialContent,
  createdAt: new Date().toISOString(),
  parentId: newParentId,
  insertAfterNodeId: insertAfterNodeId,  // ADD THIS
  // ... rest of fields
};

2. Update backend adapter (backend-adapter.ts):

  • Ensure CreateNodeInput interface has insertAfterNodeId field
  • Pass this field through to backend create_node call

3. Verify backend handling (dev-proxy.rs, commands/nodes.rs):

  • Backend should already support insertAfterNodeId (from earlier commits)
  • Verify it's being used in create_parent_edge() / move_node() calls

Acceptance Criteria

  • Nodes created via Enter key persist in correct order
  • After page refresh, sibling order matches creation order
  • Database has_child edges have correct order values
  • No regression in existing Enter key functionality
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • ReactiveNodeService: packages/desktop-app/src/lib/services/reactive-node-service.svelte.ts

    • createNode function (lines 175-380)
    • Line 242: Node object creation (needs insertAfterNodeId)
    • Line 267: Persistence via sharedNodeStore.setNode()
  • BackendAdapter: packages/desktop-app/src/lib/services/backend-adapter.ts

    • CreateNodeInput interface
    • HttpAdapter.createNode() method
    • TauriAdapter.createNode() method

Reference Commit

  • Commit 3241e710 on fix/multiple-bugs branch: "Fix: Node ordering after refresh (remove before_sibling_id legacy code)"
    • Shows exact implementation of insertAfterNodeId passing
    • Can be cherry-picked or used as reference

Testing Requirements

  1. Create nodes A, B, C, D via Enter key
  2. Verify order: A > B, C, D
  3. Refresh page
  4. Verify order unchanged: A > B, C, D
  5. Check database has_child edges have sequential order values (1, 2, 3, 4)

Related Issues

Investigation Notes

Troubleshooting performed:

  • Confirmed UI shows correct order during session (structure tree calculation works)
  • Confirmed order is wrong after refresh (database state mismatch)
  • Checked reactive-node-service.svelte.ts createNode() - no insertAfterNodeId field
  • Verified commit 3241e710 had the fix but wasn't in PR Fix: Enter key, indent/outdent, and SSE sync issues #655

Hypothesis:
PR #655 was based on commit 3eda1bf2 from main branch, which had the Enter key UI fix but not the backend ordering fix. Commit 3241e710 was on the old experimental branch and didn't make it into the final PR.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingui

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions