Skip to content

API: introduce InsertPosition enum across proto/gRPC + service (replace overloaded insert_after_node_id) #1216

Closed

Description

@mstomar125

Background

NodeService::create_parent_edge, NodeService::reorder_child, and the underlying move_node (now SqliteStore::move_node after the libsql migration) all currently express insertion position as insert_after_node_id: Option<&str>:

  • Some(id) → "insert after this sibling"
  • None → "insert at beginning of the parent's children"

This shape silently overloads None for callers with opposite intents:

Caller Intent for None
Outliner Enter-at-position-0 (shouldCreateNodeAbove branch) "insert at beginning"
Sync pull-side apply_remote (cloud has_child echo with no order field) "append at end"
reorder_node(_, _, None) move-to-first "insert at beginning"
Future caller wanting "no hint, just place it somewhere reasonable" ambiguous

Today the sync caller has to compute last_child_id explicitly to express "append" (see NodeSpaceAI/nodespace-sync#79) — an extra get_children round-trip per echoed edge — because there is no signal it can pass that the backend would interpret as "end."

⚠️ Updated 2026-05-29 after the nodespaced daemon + SQLite migration. The original issue framed the MCP HTTP handlers as the primary call site. That is no longer accurate — see "Current architecture" below. The ambiguity has since propagated into the gRPC proto wire format (empty string = beginning), which is now the most important surface to fix.

Current architecture (post-nodespaced)

MCP is no longer served over HTTP. The live request path is:

Claude Desktop / Claude Code
  → packages/mcp (TypeScript, stdio transport)
  → `nodespace` CLI (stateless gRPC client)
  → nodespaced daemon (tonic, Unix socket)  ── NodeServiceImpl
  → NodeService (packages/core)
  → SqliteStore

Consequences for this issue:

  • The Rust HTTP MCP server (packages/core/src/mcp/) is dev-only now — constructed solely by packages/dev-tools/src/bin/dev-mcp.rs. Neither the Tauri shell nor nodespaced start it. Its handle_insert_child_at_index / handle_move_child_to_index handlers are still worth migrating for consistency, but they are off the production hot path.
  • The gRPC layer is now the primary consumer and it has re-encoded the same None overload as an empty-string sentinel in the proto (empty = beginning, via empty_to_none in the daemon). Fixing this properly means the proto field should carry intent, not an overloaded string.
  • The TypeScript packages/mcp server does not yet expose reorder/insert-at-index tools, so there is nothing to migrate there today.

Proposal

Introduce an InsertPosition enum that captures intent at the Rust API surface:

pub enum InsertPosition<'a> {
    /// Insert at the beginning of the parent's children list.
    Beginning,
    /// Insert at the end of the parent's children list.
    End,
    /// Insert directly after the named sibling. If the sibling is not
    /// found in the parent's children, fall back to `End` (today's
    /// `move_node` semantic).
    After(&'a str),
}

create_parent_edge and reorder_child take this enum directly:

pub async fn create_parent_edge(
    &self,
    child_id: &str,
    parent_id: &str,
    position: InsertPosition<'_>,
) -> Result<(), NodeServiceError>;

SqliteStore::move_node keeps its underlying Option<&str> for compatibility with the lower layer, but the calculation in move_node exposes both "first_order - 1" and "last_order + 1" branches via the enum at the service layer.

Proto / wire change

The string insert_after_node_id fields in CreateNodeRequest, MoveNodeRequest, and ReorderNodeRequest (packages/proto/proto/node_service.proto) must stop overloading the empty string. Replace with an explicit position that maps to the enum — e.g. a oneof or an enum field carrying BEGINNING | END | AFTER(id). The daemon's empty_to_none translation is deleted in favor of decoding the explicit variant.

Migration / call sites (verified 2026-05-29)

All of these pass position to the ordering APIs and must be migrated:

Proto + daemon gRPC (primary, production):

  • packages/proto/proto/node_service.proto — CreateNodeRequest.insert_after_node_id, MoveNodeRequest.insert_after_node_id, ReorderNodeRequest.insert_after_node_id re-expressed to carry intent
  • packages/daemon/src/services/node_service.rs — create_node, move_node, reorder_node RPCs (currently use empty_to_none)
  • packages/daemon/src/services/import_service.rs — create_node_with_parent call

Core service + storage:

  • InsertPosition enum lives in packages/core/src/services/
  • create_parent_edge, reorder_child, reorder_node accept the enum
  • create_node_with_parent, move_node_unchecked migrated

Tauri frontend bindings:

  • packages/desktop-app/src-tauri/src/commands/nodes.rs + lib.rs — command params
  • TS callers in packages/desktop-app/src/lib/services/ (reactive-node-service, shared-node-store, optimistic-operation-manager, pending-operations, tauri-commands, backend-adapter, tauri-sync-listener, collection-service)

Dev-only Rust MCP (migrate for consistency):

  • packages/core/src/mcp/handlers/nodes.rs — handle_insert_child_at_index (drop the create-then-reorder-to-beginning hack), handle_move_child_to_index (drop the index→sibling translation)

Cross-repo companion:

  • Nodespace-sync PR migrating apply_remote from the explicit last_child_id workaround (#79) to InsertPosition::End

Cleanup unlocked by this

  • handle_insert_child_at_index currently inserts at index 0 on a non-empty list by creating at the end then calling reorder_node(.., None) to move it up — collapses to one InsertPosition::Beginning call.
  • handle_move_child_to_index runs a retry/verify loop to disambiguate beginning-vs-end — removable once intent is explicit.
  • The daemon's empty_to_none sentinel decoding for position fields goes away.
  • Several stale "SurrealDB eventual consistency" / sleep()-based retry comments in the MCP handlers predate the SQLite migration and should be deleted as part of this.

Why this matters

This is the "right" shape the recent nodespace-sync#77 work groped towards via overloads and translations:

  • #1214's original revision had create_parent_edge translate None → last_child at the service layer. That broke the outliner's Enter-at-position-0 UX (new line landed at bottom instead of above the current line). The fix lived at the wrong layer because None couldn't carry intent.
  • The stripped revision of #1214 keeps idempotency only; the receive-side append-at-end semantic moved to nodespace-sync#79 where it can be expressed unambiguously.
  • An InsertPosition::End would let sync express the same intent without the extra get_children round-trip per echo.

Out of scope

  • Bulk-move APIs (move_nodes, batch reorder) — keep InsertPosition per-call.
  • Cross-parent semantics — unchanged; the parent-id argument already covers that.

Acceptance criteria

  • InsertPosition enum exists in packages/core/src/services/
  • create_parent_edge, reorder_child, reorder_node accept the enum
  • Proto position fields re-expressed to carry intent; daemon empty_to_none for position removed
  • All call sites in the migration list above are updated (proto, daemon gRPC, core, Tauri commands, TS services, dev MCP handlers)
  • Nodespace-sync companion PR migrating apply_remote to InsertPosition::End
  • Per-variant tests added; existing tests continue to pass
  • Stale SurrealDB/eventual-consistency comments + create-then-reorder hack + move retry loop removed from the MCP handlers
  • No clippy::needless_match or similar warnings from the new enum

References

  • Root cause for the API confusion: nodespace-sync#77 (the receive-side ordering bug that surfaced both intents passing through None)
  • The pragmatic resolution that shipped: #1214 (idempotency only) + nodespace-sync#79 (sync passes explicit last_child_id)
  • Daemon migration that moved MCP off HTTP and re-encoded the overload in proto: nodespaced + SQLite migration (#1222, #1224)
  • The cleaner long-term shape: this issue

Activity

  1. changed the title [-]API: introduce InsertPosition enum for create_parent_edge / move_node / reorder_child[/-] [+]API: introduce InsertPosition enum across proto/gRPC + service (replace overloaded insert_after_node_id)[/+] on May 29, 2026
  2. self-assigned this
    on May 29, 2026
  3. added 2 commits that reference this issue on Jun 22, 2026
    6385f16
    7d23a6e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions