Skip to content

Emit RelationshipDeleted on move-to-root and reparent (move_node) - #1408

Merged
mstomar125 merged 1 commit into
NodeSpaceAI:mainfrom
mstomar125:fix-move-to-root-event
Jun 12, 2026
Merged

mstomar125 merged 1 commit into
NodeSpaceAI:mainfrom
mstomar125:fix-move-to-root-event

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Problem

move_node deletes the old has_child edge in the store, but only emitted a RelationshipUpdated for the new parent (gated on Some(parent)). So:

  • A move-to-root (new_parent = None) emitted no relationship event — consumers never learned the node detached.
  • A reparent left the old parent edge unannounced.

For NodeSpace sync (epic NodeSpaceAI/nodespace-sync#162) this meant outdenting a node to the top level never propagated to other devices — the node stayed parented elsewhere.

Fix

Capture the old parent before the move; when the parent actually changes (move-to-root or reparent), emit a RelationshipDeleted for the old parent edge after the new-parent RelationshipUpdated (insert-before-delete order, so a consumer that inserts-then-deletes never sees the node parentless mid-move). A same-parent position move emits nothing new.

Reuses the existing RelationshipDeleted event (no new variants), node_thing-normalized endpoints like the other emit sites.

Verified

Built the sync daemon against this branch: the daemon now pushes the edge delete on move-to-root (supabase push: has_child edge deleted parent=… child=…). cargo check -p nodespace-core clean. (Full e2e propagation also needs the live tenant's relationships table at REPLICA IDENTITY FULL — already in the tenant schema template — so the realtime DELETE carries the endpoints.)

move_node deletes the old has_child edge in the store, but only emitted a
RelationshipUpdated for the NEW parent (gated on Some). A move-to-root
(new_parent = None) therefore emitted no relationship event at all, so the
detach never reached consumers (cloud sync left the node parented on other
devices); a reparent left the old edge unannounced.

Capture the old parent before the move and, when the parent actually
changes (move-to-root OR reparent), emit a RelationshipDeleted for the old
parent edge after the new-parent RelationshipUpdated — insert-before-delete
order so a consumer never sees the node parentless mid-move. A same-parent
position move emits nothing new.

Needed by nodespace-sync's full-graph sync (epic #162): the daemon pushes
this delete to remove the stale cloud has_child edge.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

REQUEST CHANGES ⏳

Initial review, top commit 6244c11 (against main). no checks reported on the branch — CI status pending/absent, so this is a read-only review; the behaviour change is unvalidated by automated tests (see below).

The fix itself is correct and nicely scoped. I traced each path:

  • move-to-root (new_parent = None, old Some): None != Some(old_id) → emits RelationshipDeleted only. ✅ the bug it targets.
  • reparent (new_parent = Some(other)): emits RelationshipUpdated (new) then RelationshipDeleted (old). ✅ insert-before-delete ordering holds — relationship events are node-keyed None so they bypass BatchState and broadcast immediately in call order (emit_event, mod.rs:1408).
  • same-parent position move (new_parent = Some(old)): Some(old) != Some(old) is false → no delete. ✅
  • already-root → parent (old = None): no delete, just the update. ✅

Event shape is consistent with every sibling site: the id format relationship:{parent}:{child} matches mod.rs:1773 / 1904 / 2127 / 2224 / 2306, and from_id/to_id are node_thing-normalized (parent→child direction), which db/events.rs:92 explicitly notes the inline RelationshipDeleted variant relies on. Good.

One blocking gap:

🟡 No regression test for a sync-critical behaviour change. This PR alters the emit contract of move_node — move-to-root and reparent now emit RelationshipDeleted, and a same-parent reorder must not. That contract is load-bearing for sync epic #162 (the whole point is the detach propagating), and it's exactly the kind of thing a future refactor silently re-gates back to Some(parent)-only — reintroducing the bug with no signal. The harness to lock it down is already here:

  • existing move_node tests in the same module to extend — test_move_node_accepts_container_parent (mod.rs:3090), test_move_node_rejects_non_container_parent (mod.rs:3062);
  • an established event-assertion pattern — service.subscribe_to_events() then a while let Ok(env) = rx.try_recv() drain — used by ~6 tests already (mod.rs:3616, 3665, 3682, 3775, 3813, 3859).

Please add a test (or three) asserting: (a) move-to-root emits a RelationshipDeleted for the old parent edge; (b) reparent emits the new-parent RelationshipUpdated before the old-parent RelationshipDeleted; (c) a same-parent position move emits no RelationshipDeleted. (a) and (c) are the two that guard against regressing this exact fix.

Two non-blocking notes:

🟢 id-format consistency depends on Node.id being bare. The delete id is built from old_parent_id = get_parent(node_id)?.map(|p| p.id) (mod.rs:1888), while the insert-time id (mod.rs:1773/1904) is built from the bare parent_id: &str argument. These only match if get_parent's Node.id is the bare id (not node:-prefixed) — which is the convention, so this is fine, but a consumer that keys deletes off the raw id string (rather than from_id/to_id, which are node_thing-normalized either way) would mis-match if that ever drifts. The test in (a) above would pin it.

🟢 Extra pre-move read. get_parent(node_id).await? (mod.rs:1888) adds a DB round-trip and a new failure point before the store mutation — a get_parent error now aborts the move. Cheap and almost certainly fine (a failing get_parent means the store is already unhealthy), just noting the added ?.

Net: correct, well-reasoned fix with consistent event shape — just needs the regression test that turns "I built the daemon and watched it push the delete" into something CI can keep enforcing for #162.

@mstomar125
mstomar125 merged commit d5c1aeb into NodeSpaceAI:main Jun 12, 2026
malibio pushed a commit that referenced this pull request Jun 22, 2026
)

move_node deletes the old has_child edge in the store, but only emitted a
RelationshipUpdated for the NEW parent (gated on Some). A move-to-root
(new_parent = None) therefore emitted no relationship event at all, so the
detach never reached consumers (cloud sync left the node parented on other
devices); a reparent left the old edge unannounced.

Capture the old parent before the move and, when the parent actually
changes (move-to-root OR reparent), emit a RelationshipDeleted for the old
parent edge after the new-parent RelationshipUpdated — insert-before-delete
order so a consumer never sees the node parentless mid-move. A same-parent
position move emits nothing new.

Needed by nodespace-sync's full-graph sync (epic #162): the daemon pushes
this delete to remove the stale cloud has_child edge.

Co-authored-by: Mayank  Tomar <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[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.

1 participant