Repository navigation
Make create_parent_edge idempotent on already-existing edges (fixes nodespace-sync#77, alice-side) - #1214
Conversation
…-end Fixes the receive-side half of nodespace-sync#77: when alice creates a new sibling and presses Enter, the cloud LIVE-SELECT echoes the new has_child edge back through the sync pull-side, which then calls NodeService::create_parent_edge(child, parent, None) on the local daemon. Two bugs combined to drag the new sibling to the top of its parent's list in both windows: 1. The pull-side echo re-creates an edge that already exists locally. The old create_parent_edge unconditionally delegated to move_node, which re-computed the order and silently re-positioned the child off its just-computed local order. 2. The cloud relationship row carries no `order` field (sync's `RELATE ... SET rel_type = $rt` doesn't push it), so the pull-side has to pass `None` for `insert_after_node_id`. The store's move_node interprets `None` as "insert at beginning of children" — which is what the MCP `reorder_node(_, _, None)` "move to first position" path needs, but the wrong default for a hierarchical CREATE. For bob (who has never had this relationship before), the new node landed at first_order - 1.0 ≈ -0.998, sorting before every existing sibling. The fix lives at the NodeService layer where the calling convention splits cleanly between CREATE and REORDER: - create_parent_edge is now idempotent: if `child_id` is already a child of `parent_id`, return Ok immediately without touching the edge. - create_parent_edge translates a `None` insert-after hint to the parent's current last-child id, so the underlying move_node call appends at end. move_node's None semantic is preserved for the reorder path. - create_node_with_parent's stale-hint fallback is simplified: when the sibling-validation retry loop times out, clearing the hint to `None` now does the right thing (create_parent_edge translates it to append). The fallback comment promised "fall back to append" but the previous behavior produced "insert at beginning"; the semantic now matches the comment. Three pre-existing tests called move_node(child, parent, None) as shorthand for "actually reorder this child" and depended on the implicit `None = move to beginning` behavior. Updated each to use an explicit `insert_after_sibling` so they exercise a real position change. Added the matching version-bump assertions back so issue #795 coverage is preserved. Test deltas vs origin/main: - 1190 → 1191 passing (3 existing tests rewritten, no new tests) - cargo clippy -p nodespace-core --lib -- -D warnings: clean Manually verified end-to-end against the two-window same-device demo (scripts/run-two-daemons.sh + alice + bob Tauri windows): pressing Enter places the new sibling directly below the current row in both windows. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Pragmatic code review — PR #1214CI: ⏳ No checks reported on Findings🔴 Idempotency guard treats if let Some(existing_parent) = self.get_parent(child_id).await? {
if existing_parent.id == parent_id {
return Ok(());
}
}The guard short-circuits unconditionally when the child is already under the target parent — but the caller may have passed a non-None
Option 1 is the smallest fix and preserves the sync echo behavior the PR is targeting. Recommend at minimum (1) + a test that calls 🟡 match self.get_children(parent_id).await {
Ok(children) => children.last().map(|c| c.id.clone()),
Err(_) => None,
}Every CREATE with no explicit insert-after now does:
For a parent with many children, 🟡 🟡 Idempotency guard's 🟡 Concurrency: read-then-write race between
For sync echo replays this can manifest as out-of-order siblings when the cloud delivers a batch of new edges. The fractional-order math handles it (no collision), but the visual order may not match the cloud-canonical order. Worth either serializing edge creation per-parent (already done elsewhere?) or accepting the eventually-consistent ordering with a doc comment naming it. 🟡 Test rewrites lose coverage of the Nits🟢 Nit: Doc comments name 🟢 Nit: Double blank line in test module ( 🟢 Nit: 🟢 Nit: Idempotency log is 🟢 Nit: 🟢 Nit: PR body says "1191 passed / 0 failed (one rename of an existing test; no net test count change)" — matches the diff. Worth double-checking the rewritten tests still cover Issue #795's actual scenario (the issue is about same-parent reorder preserving SummaryThe two fixes are real and the test rewrites are honest about the semantic shift. The 🔴 is about the idempotency guard's blast radius — narrowing it to the actual sync-echo case ( Verdict: REQUEST CHANGES |
- 🔴 Narrow idempotency to no-explicit-hint calls only. Previously the guard short-circuited every `create_parent_edge(child, parent, *)` call when the edge already existed — including calls that passed `Some(target_sibling)` to explicitly reposition the child. New test `test_create_parent_edge_reorders_when_explicit_hint_against_existing_edge` locks the narrowed contract in place. Today's only known caller of this function that passes a hint against an existing edge would be a future caller; the test future-proofs it. - Propagate `get_children` errors instead of swallowing them. The previous `Err(_) => None` silently degraded to "insert at beginning" — the exact bug nodespace-sync#77 was filed to eliminate. Use `?` and let the caller decide whether to retry. - Document the `get_parent` propagation contract. The idempotency check uses `?` and aborts on transient DB error, distinct from the 10×100ms retry budget on the `move_node` loop below (which is for the sibling-not-found case, a different consistency window). - Document the read-then-write concurrency window between `get_children` and `move_node`. Two concurrent `create_parent_edge(_, parent, None)` calls produce distinct fractional orders (no collision) but the *between-which-siblings* placement reflects whichever transaction landed last. Acceptable for sync echo replay (eventually consistent across windows); callers needing strict ordering should serialise or use `reorder_child` afterwards. - Add `test_move_node_same_parent_no_hint_moves_to_beginning` at the store layer. The previous PR's test rewrites moved the explicit- sibling pattern into three existing tests, which inadvertently left zero coverage for the lower-level `move_node(_, _, None) = "insert at beginning"` semantic. The MCP `reorder_node(_, _, None)` "move to first position" path depends on this; a future refactor that flips the None default to "append at end" at this layer would silently break MCP — this test catches that. - Fix stale comments in two test bodies that described the no-op semantic as living at `move_node`; the idempotency lives at `NodeService::create_parent_edge` (the layer where the calling convention splits cleanly between CREATE and REORDER). Test deltas vs origin/main: - 1191 → 1193 passing (+2 new tests, no removals) - cargo clippy: clean Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Pushed review-feedback addressed in Real concerns
Cost note (acknowledge)
Test deltas
|
Pragmatic code review (re-review) — PR #1214Review Type: Re-Review (scope: commit Previous review summary
Round-1 findings — disposition✅ 🔴 Idempotency blast radius narrowed — guard now gated on service.create_parent_edge(&child1_id, &parent_id, Some(&child2_id)).await.unwrap();
// assertion: after[0] = "Child 2", after[1] = "Child 1"A future regression that widens the guard back to "any same-parent edge" fails this test. Clean fix. ✅ ✅ ✅ Concurrency window — read-then-write race between ✅ Lost store-layer coverage — new ✅ Stale test comments — fixed: comments now correctly attribute the no-op to 🟡 Extra New findings🟢 Nit: SummaryEvery round-1 🔴 and 🟡 is either fixed in code, locked in by a test, or explicitly documented with a rationale. The narrowed idempotency contract is the right shape — sync-echo idempotency lives where the calling convention splits cleanly between CREATE and REORDER, and the test suite now covers both the sync-echo no-op and the explicit-hint reorder. The store-layer test restores the layered contract coverage the rewrites had dropped. Verdict: APPROVE |
The previous version of this PR translated `create_parent_edge`'s
`None` insert-after hint to "append after the parent's last child"
inside `NodeService`. That correctly fixed sync's receive-side
echo path but introduced a regression for the frontend's
Enter-at-position-0 flow:
• User puts cursor at start of node "hello" and presses Enter
• Frontend's `shouldCreateNodeAbove` branch fires → dispatches a
CREATE with `insert_after_node_id: null` meaning "insert above
the current line"
• Frontend optimistic UI shows new empty line ABOVE
• Backend's translation: null → last_child_id → move_node
appends at end
• Backend's RelationshipCreated event arrives ~200ms later with
that "append" order; the frontend updates structureTree → new
empty line visually relocates to the BOTTOM of the parent's
list
Two real callers, two opposite meanings for the same `None`. Backend
can't tell them apart at the API surface, so the receive-side fix
has to live where the intent is unambiguous.
Move the translation to the sync layer in PR D' (`nodespace-sync` —
`apply_remote` for has_child relationships computes the parent's
current last child and passes it explicitly as the hint). Core's
`create_parent_edge` keeps its `None = insert at beginning` semantic
intact, matching `move_node` and `reorder_node(_, _, None)`.
Behavioral surface of this PR after the strip:
• Idempotency guard (no-op when child is already under parent AND
no explicit hint) — KEPT. This is the alice-side echo fix.
• `None`-translation block — REMOVED. Sync receive-side moves to
PR D'.
• Stale-hint fallback in `create_node_with_parent` — kept the
simplified shape; documents the actual behavior (`None` →
insert at beginning).
• `test_insert_at_end_by_default` reverted to
`test_insert_at_beginning_by_default` with the original
assertions.
• `test_create_parent_edge_reorders_when_explicit_hint_against_existing_edge`
updated to seed with an explicit hint (independent of the
default insertion direction).
• `test_move_node_same_parent_no_hint_moves_to_beginning` — kept,
locks the store-level contract used by MCP's reorder path.
Test deltas vs origin/main:
1191 → 1193 passing (no net change from previous revision; the
add and remove balance).
Follow-up: file an issue on core suggesting an
`InsertPosition::{Beginning, End, After(id)}` enum for
`create_parent_edge`'s signature so future callers can express
intent without overloading `Option`.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Force-pushed Why — the translation conflated two callers with opposite intents for the same
The previous revision made the outliner's "insert above current line" flow visually land the new line at the bottom of the parent's children (optimistic above → ~200ms later moves to bottom on backend confirm). Caught by re-reading the frontend's Resolution — resolve at the call site, not at the backend:
API-improvement follow-up — filed #1216 suggesting an Test delta vs origin/main
Updated PR description above reflects the new scope. |
- 🔴 Narrow idempotency to no-explicit-hint calls only. Previously the guard short-circuited every `create_parent_edge(child, parent, *)` call when the edge already existed — including calls that passed `Some(target_sibling)` to explicitly reposition the child. New test `test_create_parent_edge_reorders_when_explicit_hint_against_existing_edge` locks the narrowed contract in place. Today's only known caller of this function that passes a hint against an existing edge would be a future caller; the test future-proofs it. - Propagate `get_children` errors instead of swallowing them. The previous `Err(_) => None` silently degraded to "insert at beginning" — the exact bug nodespace-sync#77 was filed to eliminate. Use `?` and let the caller decide whether to retry. - Document the `get_parent` propagation contract. The idempotency check uses `?` and aborts on transient DB error, distinct from the 10×100ms retry budget on the `move_node` loop below (which is for the sibling-not-found case, a different consistency window). - Document the read-then-write concurrency window between `get_children` and `move_node`. Two concurrent `create_parent_edge(_, parent, None)` calls produce distinct fractional orders (no collision) but the *between-which-siblings* placement reflects whichever transaction landed last. Acceptable for sync echo replay (eventually consistent across windows); callers needing strict ordering should serialise or use `reorder_child` afterwards. - Add `test_move_node_same_parent_no_hint_moves_to_beginning` at the store layer. The previous PR's test rewrites moved the explicit- sibling pattern into three existing tests, which inadvertently left zero coverage for the lower-level `move_node(_, _, None) = "insert at beginning"` semantic. The MCP `reorder_node(_, _, None)` "move to first position" path depends on this; a future refactor that flips the None default to "append at end" at this layer would silently break MCP — this test catches that. - Fix stale comments in two test bodies that described the no-op semantic as living at `move_node`; the idempotency lives at `NodeService::create_parent_edge` (the layer where the calling convention splits cleanly between CREATE and REORDER). Test deltas vs origin/main: - 1191 → 1193 passing (+2 new tests, no removals) - cargo clippy: clean Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…otent Make create_parent_edge idempotent on already-existing edges (fixes nodespace-sync#77, alice-side)
Summary
Backend half of
nodespace-sync#77. MakesNodeService::create_parent_edgeidempotent on already-existing edges so the sync pull-side's echo replay doesn't re-position the just-created child off its local order.What changed
packages/core/src/services/node_service.rs—create_parent_edgenow short-circuits when:insert_after_node_idisNone(no explicit reorder intent), ANDget_parent(child_id)reports the child is already underparent_idThe guard is narrowed via
insert_after_node_id.is_none()so a caller that passes an explicit hint still triggers a real reorder (covered bytest_create_parent_edge_reorders_when_explicit_hint_against_existing_edge).packages/core/src/db/surreal_store.rs— addedtest_move_node_same_parent_no_hint_moves_to_beginningto lock the store-level contract used byreorder_node(_, _, None)(MCP "move to first position" path).The three existing
test_same_parent_*tests were already updated to use explicitinsert_after_siblingfor their reorder calls.Scope note: what this PR does NOT do
An earlier revision of this PR ALSO translated
None → last_child_idinsidecreate_parent_edgeto fix the sync receive-side bug (bob's view of alice's new child landed at the top of the parent's list). That translation has been stripped ine25fa571because it conflated two callers with opposite intents:Noneapply_remoteCore can't disambiguate at the API layer. The fix lives at the call sites instead — sync's pull-side computes
last_child_idexplicitly inNodeSpaceAI/nodespace-sync#79. The outliner's "insert at beginning" UX is preserved by core's unchangedmove_node(_, _, None) = "insert at beginning"semantic.Test plan
cargo test -p nodespace-core --lib— 1191 → 1193 passing (+2 new, no removals)cargo clippy -p nodespace-core --lib -- -D warnings— cleancargo fmt --check— cleannodespace-sync#79for the receive-side correction)Related
nodespace-sync#76). Independent of this PR.#77(tauri-sync-listener fallback + structureTree-based stale check). Independent of this PR.nodespace-sync#79— receive-side companion that passes the explicitlast_child_idhint. Required for the full#77fix.InsertPositionenum so future callers can express intent without overloadingOption.