Skip to content

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

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1216-insert-position-enum
May 29, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1216-insert-position-enum

Conversation

@malibio

@malibio malibio commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Closes #1216

@malibio

malibio commented May 29, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review — PR #1236: InsertPosition Enum

Summary

This is a well-executed, complete refactor. The core design is sound: the enum cleanly replaces a two-valued Option<&str> with three explicitly-named variants, eliminates the empty_to_none sentinel hack at the proto layer, and threads the change consistently through Rust core, proto/gRPC daemon, Tauri commands, and TypeScript frontend. Coverage is broad and the migration is thorough. The PR is a net positive and should be merged once the issues below are addressed.


Findings

[Improvement] Stale doc comments on move_node and CreateNodeParams

packages/core/src/services/node_service.rs line 4329 — the Arguments block for move_node still reads:

* insert_after_node_id - Optional sibling to insert after (None = insert at beginning)

This argument no longer exists; the parameter is now position: InsertPosition<'_>. A caller reading this doc will be confused.

packages/core/src/services/node_service.rs line 419 — CreateNodeParams.position says:

/// Defaults to InsertPositionOwned::Beginning when not specified.

The field has no Default impl and the actual runtime default for omitted callers is End (commit message confirms this). The doc says the opposite. This will mislead every future caller of this struct.

packages/core/src/services/node_service.rs line 2037 — doc block for create_node_with_parent still references insert_after_node_id by name in the Note: paragraph.

packages/core/src/services/node_service.rs line 9567 — test comment reads to insert_after_node_id.is_none(): the narrowing guard is now matches!(position, InsertPosition::End); the comment describes old code.

These are straightforward fixes, not blockers, but stale doc comments on a freshly-renamed API are especially harmful.

[Improvement] End with no parent silently degrades to Beginning at the store layer

In resolve_insert_position (node_service.rs line 4522):

InsertPosition::End => {
    if let Some(pid) = parent_id {
        let children = self.get_children(pid).await?;
        Ok(children.last().map(|n| n.id.clone()))
    } else {
        Ok(None)   // store interprets None as Beginning
    }
}

When parent_id is None (making a node a root), End resolves to None, and SqliteStore::move_node maps None to before the first sibling (Beginning) at the root level. So move_node_unchecked(id, None, InsertPosition::End) does not actually append at the end of the root list — it prepends. The call sites in tests and benchmarks use InsertPosition::Beginning explicitly for this case, so they are self-consistent, but the semantics of End for root-level nodes are silently wrong. Add a comment documenting that End with no parent falls back to store-layer Beginning, so future callers are not surprised.

[Improvement] Proto bool oneof — bool beginning = 8 / bool end = 9 is an unusual encoding

Using bool as the payload of a oneof variant works but is non-idiomatic and creates a subtle footgun: a client sending {beginning: false} would still set the beginning variant, which the daemon correctly decodes as InsertPositionOwned::Beginning (the value of the bool is ignored). The standard pattern for a unit-type variant in proto is either an empty message (message Beginning {}) or a proto enum. The current approach requires explicit documentation to prevent future proto authors from interpreting the bool value as meaningful. This is not a blocker for a greenfield project but worth tracking.

[Nit] Inline type annotations on insertPosition field accesses in TypeScript

In shared-node-store.svelte.ts lines 1415 and 1497, the inline cast uses { type: string; siblingId?: string } | null. This duplicates structure that the exported InsertPosition type already defines. Import and use it directly to avoid drift if the shape changes.

[Nit] pending-operations.ts line 19 retains an insertAfterNodeId reference in a comment

The race-condition scenario comment still reads which references B as insertAfterNodeId. Should be updated to insertPosition.

[Nit] shared-node-store.svelte.ts line 562 comment

The method docblock at line 562 still reads insertAfterNodeId hint — should be insertPosition.


Acceptance Criteria Status

Checking against issue #1216:

  • InsertPosition enum in packages/core/src/services/ — present, re-exported from crate root
  • create_parent_edge, reorder_child, reorder_node accept the enum
  • Proto position fields re-expressed; daemon empty_to_none for position removed
  • All primary call sites migrated: proto, daemon gRPC, import service, core, Tauri commands, reactive-node-service, shared-node-store, backend-adapter, tauri-commands
  • Per-variant tests — End and Beginning are tested; the gRPC round-trip tests set position: None everywhere, exercising only the End default; explicit daemon tests for Beginning and After proto decoding paths are missing
  • Stale SurrealDB/eventual-consistency comments removed from MCP handlers — moot; the Rust HTTP MCP module was removed in Audit & cleanup: finish daemon + SQLite migrations (purge dead HTTP MCP / SurrealDB / RocksDB from code and docs) #1230; no residue found
  • Create-then-reorder hack and move retry loop removed — confirmed gone
  • Nodespace-sync companion PR (apply_remote to InsertPosition::End) — tracked separately, expected out of scope for this repo
  • optimistic-operation-manager, pending-operations, tauri-sync-listener, collection-service — confirmed these do not call the renamed APIs directly; no migration needed

Verdict

Approve with recommendations. Fix the stale doc comment claiming Defaults to Beginning on CreateNodeParams.position (it says the opposite of the actual behavior) before merge. The End-with-no-parent silent degradation and the gRPC daemon test gap are improvements worth making as a fast follow.

@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.

Reviewed. See full findings in the PR comment above. One doc-accuracy fix recommended before merge (CreateNodeParams.position claims 'Defaults to Beginning' but the actual default is End). Other findings are improvements/nits.

malibio pushed a commit that referenced this pull request May 29, 2026
- Fix wrong doc comment on CreateNodeParams.position (said Beginning, is End)
- Update 3 stale doc/comment references from insert_after_node_id to new API
- Document resolve_insert_position limitation: End with no parent resolves to
  None which store-layer maps to Beginning (all root-move callers already
  pass Beginning explicitly, so no behavior change)
- Add proto oneof documentation explaining bool-variant encoding and why the
  bool value is semantically meaningless (unit variant workaround)
- Add gRPC round-trip test covering Beginning, End, and After proto decoding
  paths via CreateNode + ReorderNode RPCs
- Use imported InsertPosition type in shared-node-store instead of inline
  structural casts
- Update 2 stale insertAfterNodeId comment references in TS files

Skipped: proto bool → empty message redesign — wire format change requires
coordinated client updates; behavior is correct for all internal callers.

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

malibio commented May 29, 2026

Copy link
Copy Markdown
Collaborator Author

Review Feedback Addressed

All 7 reviewer recommendations addressed. 1 skipped with justification.

✅ Addressed

  1. Wrong default in CreateNodeParams.position doc — Fixed to correctly describe End (not Beginning).
  2. 3 stale insert_after_node_id doc/comment references — Updated to use new API names (InsertPosition, position, After).
  3. End with no parent degrades silently — Added explanatory comment in resolve_insert_position documenting the limitation and why it's safe in practice.
  4. Proto oneof bool-variant encoding — Added documentation in all three messages explaining the unit-variant workaround and that the bool value is semantically meaningless.
  5. Missing daemon tests for Beginning and After decoding — Added test_insert_position_beginning_and_after_proto_decoding in grpc_round_trip.rs covering all three variants via CreateNode + ReorderNode RPCs.
  6. TS inline type casts — Replaced { type: string; siblingId?: string } structural casts in shared-node-store.svelte.ts with the imported InsertPosition type.
  7. 2 stale insertAfterNodeId comment references — Updated in pending-operations.ts and shared-node-store.svelte.ts.

⏭️ Skipped

Proto bool → empty message redesign — The wire format change would require coordinated updates to all clients (daemon, Tauri shell, any future gRPC consumers) in a single atomic deploy. The current encoding is correct for all internal callers; the bool-value-is-ignored edge case only matters if an external client sends beginning: false, which doesn't exist. This is now documented in the proto. Revisit if/when an external client appears or if we add a breaking-changes window.

Michael Libio and others added 2 commits May 30, 2026 00:33
#1216)

Replaces the overloaded `insert_after_node_id: Option<&str>` pattern with an
explicit `InsertPosition` enum at every layer:

**Core (packages/core)**
- `InsertPosition<'a>` (borrowed) and `InsertPositionOwned` (for struct storage)
  added to `packages/core/src/services/mod.rs`, re-exported from crate root
- `CreateNodeParams.position: InsertPositionOwned` replaces `insert_after_node_id`
- `create_parent_edge`, `reorder_child`, `reorder_node`, `move_node`,
  `move_node_unchecked` all accept `InsertPosition<'_>` directly
- `resolve_insert_position` helper converts `End` → last-child lookup at the
  service layer so `SqliteStore::move_node` still takes `Option<&str>`
- Default semantic changed: `None`/unset = **End** (append); callers that want
  "insert above" must now pass `Beginning` explicitly

**Proto + daemon (packages/proto, packages/daemon)**
- `CreateNodeRequest`, `MoveNodeRequest`, `ReorderNodeRequest` replace the
  overloaded `string insert_after_node_id` field with a `oneof position`
  carrying `beginning`, `end`, or `after` variants; unset = End
- Daemon `node_service.rs` decodes the oneof and maps to `InsertPosition<'_>`;
  `empty_to_none` translation deleted from the three affected RPCs

**Tauri + TypeScript (packages/desktop-app)**
- `InsertPositionInput` serde enum on the Rust side deserializes
  `{type:"beginning"|"end"|"after", siblingId?:string}` from the frontend
- `CreateNodeInput`, `move_node`, `reorder_node` Tauri commands accept
  `insert_position: Option<InsertPositionInput>` instead of `insert_after_node_id`
- TS `InsertPosition` discriminated union + `insertPosition` factory exported
  from `backend-adapter.ts`; all callers in `reactive-node-service` and
  `shared-node-store` migrated

Co-Authored-By: Claude <[email protected]>
- Fix wrong doc comment on CreateNodeParams.position (said Beginning, is End)
- Update 3 stale doc/comment references from insert_after_node_id to new API
- Document resolve_insert_position limitation: End with no parent resolves to
  None which store-layer maps to Beginning (all root-move callers already
  pass Beginning explicitly, so no behavior change)
- Add proto oneof documentation explaining bool-variant encoding and why the
  bool value is semantically meaningless (unit variant workaround)
- Add gRPC round-trip test covering Beginning, End, and After proto decoding
  paths via CreateNode + ReorderNode RPCs
- Use imported InsertPosition type in shared-node-store instead of inline
  structural casts
- Update 2 stale insertAfterNodeId comment references in TS files

Skipped: proto bool → empty message redesign — wire format change requires
coordinated client updates; behavior is correct for all internal callers.

Co-Authored-By: Claude <[email protected]>
@malibio
malibio force-pushed the worktree-issue-1216-insert-position-enum branch from cfffa9a to 2bef75c Compare May 29, 2026 22:33
@malibio
malibio merged commit 716f0b7 into main May 29, 2026
@malibio
malibio deleted the worktree-issue-1216-insert-position-enum branch May 29, 2026 22:34
malibio added a commit that referenced this pull request May 30, 2026
…ode_ops (closes #1241) (#1244)

- daemon: refactor create_node, update_node, delete_node to delegate to
  node_ops::{create_node,update_node,delete_node} — removes inline
  collection resolution, lifecycle defaulting, and version auto-fetch
  from the gRPC layer (B3+B4 from architectural audit #1228)
- core/node_ops: fix update_node to skip the NodeUpdate call when no
  field changes are present (only collection ops) — prevents "Update
  contains no changes" error on collection-only updates
- daemon/tests: add 3 parity tests asserting create+collection+lifecycle,
  auto-fetch version, and add/remove collection membership via gRPC
- agent/tools: add missing id/position fields to CreateNodeInput struct
  (required after InsertPosition enum was introduced in #1236)

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Jun 22, 2026
…ce overloaded insert_after_node_id) (#1236)

* Introduce InsertPosition enum across proto/gRPC + service layer (closes #1216)

Replaces the overloaded `insert_after_node_id: Option<&str>` pattern with an
explicit `InsertPosition` enum at every layer:

**Core (packages/core)**
- `InsertPosition<'a>` (borrowed) and `InsertPositionOwned` (for struct storage)
  added to `packages/core/src/services/mod.rs`, re-exported from crate root
- `CreateNodeParams.position: InsertPositionOwned` replaces `insert_after_node_id`
- `create_parent_edge`, `reorder_child`, `reorder_node`, `move_node`,
  `move_node_unchecked` all accept `InsertPosition<'_>` directly
- `resolve_insert_position` helper converts `End` → last-child lookup at the
  service layer so `SqliteStore::move_node` still takes `Option<&str>`
- Default semantic changed: `None`/unset = **End** (append); callers that want
  "insert above" must now pass `Beginning` explicitly

**Proto + daemon (packages/proto, packages/daemon)**
- `CreateNodeRequest`, `MoveNodeRequest`, `ReorderNodeRequest` replace the
  overloaded `string insert_after_node_id` field with a `oneof position`
  carrying `beginning`, `end`, or `after` variants; unset = End
- Daemon `node_service.rs` decodes the oneof and maps to `InsertPosition<'_>`;
  `empty_to_none` translation deleted from the three affected RPCs

**Tauri + TypeScript (packages/desktop-app)**
- `InsertPositionInput` serde enum on the Rust side deserializes
  `{type:"beginning"|"end"|"after", siblingId?:string}` from the frontend
- `CreateNodeInput`, `move_node`, `reorder_node` Tauri commands accept
  `insert_position: Option<InsertPositionInput>` instead of `insert_after_node_id`
- TS `InsertPosition` discriminated union + `insertPosition` factory exported
  from `backend-adapter.ts`; all callers in `reactive-node-service` and
  `shared-node-store` migrated

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

* Address review feedback from PR #1236

- Fix wrong doc comment on CreateNodeParams.position (said Beginning, is End)
- Update 3 stale doc/comment references from insert_after_node_id to new API
- Document resolve_insert_position limitation: End with no parent resolves to
  None which store-layer maps to Beginning (all root-move callers already
  pass Beginning explicitly, so no behavior change)
- Add proto oneof documentation explaining bool-variant encoding and why the
  bool value is semantically meaningless (unit variant workaround)
- Add gRPC round-trip test covering Beginning, End, and After proto decoding
  paths via CreateNode + ReorderNode RPCs
- Use imported InsertPosition type in shared-node-store instead of inline
  structural casts
- Update 2 stale insertAfterNodeId comment references in TS files

Skipped: proto bool → empty message redesign — wire format change requires
coordinated client updates; behavior is correct for all internal callers.

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

---------

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Jun 22, 2026
…ode_ops (closes #1241) (#1244)

- daemon: refactor create_node, update_node, delete_node to delegate to
  node_ops::{create_node,update_node,delete_node} — removes inline
  collection resolution, lifecycle defaulting, and version auto-fetch
  from the gRPC layer (B3+B4 from architectural audit #1228)
- core/node_ops: fix update_node to skip the NodeUpdate call when no
  field changes are present (only collection ops) — prevents "Update
  contains no changes" error on collection-only updates
- daemon/tests: add 3 parity tests asserting create+collection+lifecycle,
  auto-fetch version, and add/remove collection membership via gRPC
- agent/tools: add missing id/position fields to CreateNodeInput struct
  (required after InsertPosition enum was introduced in #1236)

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

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

1 participant