Skip to content

Node merge bypasses tree invariants: survivor gains a second parent or breaks root-only membership #2938

Closed

Description

@malibio

Overview

NodeService::merge_nodes (user-initiated from the Conflicts view) moves every edge from the loser to the survivor with a plain endpoint UPDATE inside SqliteStore::merge_nodes_in_tx. That re-point skips the tree invariants that every other has_child write path enforces. There are two confirmed results:

  1. A survivor with two has_child parents. This happens when both nodes are children in different trees.
  2. A survivor holding member_of while also having a parent. This breaks ADR-059 §2, which lets only a root hold collection membership. It happens when the survivor is a root filed into a collection and the loser is a child.

Stale embeddings are one visible symptom, but the underlying defect is structural.

Evidence (probe run against main @ 6823d80)

A throwaway test (not committed) built each shape and merged survivor=Y, loser=LINE. ROOT_A holds LINE; ROOT_B is a separate tree. Both roots were given fresh, non-stale embeddings first.

Case Shape Result
1 Y is a root, LINE is a child of A ✅ Y's parents: [A]. A is re-queued (stale=true), B is untouched. Correct.
2 Y is a child of B, LINE is a child of A ❌ Y's parents: [A, B]. embedding_root_id(Y) = A. A is stale, B is not. aggregate(A) = "SURVIVOR_IN_B". The loser is archived and has no parents.
3 Both are children of A ✅ A duplicate A→Y edge is dropped (dropped=1) and Y's parents are [A]. A is re-queued.
4 Y is a root filed in a restricted collection, LINE is a child of open A ❌ The control move_node(Y → A) is refused with member_of_not_root … (ADR-059 §2). merge_nodes succeeds (repointed=1) and Y's parents are [A]. ADR-059 §7's fallback contains the embedding: embedding_root_id(Y) = Y and aggregate(A) = None. The outline, however, now shows restricted content under an open tree.

Case 5 is inferred from the code and was not probed. Here the loser is a root filed in a collection and the survivor is a child. The loser's member_of edge is moved onto a child survivor, which breaks §2 from the other direction.

Why this matters

  • Single-parent tree invariant. Every other path treats has_child as a tree: move_node replaces the parent, and bulk_create_has_child skips children that already have a parent. Several functions read exactly one parent (get_parent, embedding_root_id, collect_subtree_ids, rootness and title derivation, and the frontend hierarchy sync). With two parents, which one they pick is arbitrary. The outline also renders the node under two trees.
  • Embeddings. Since PR Re-queue the old embedding root when a node leaves its tree #2936, the tx path re-queues the survivor's embedding root after commit. That fixes case 1, which queued nothing before. In case 2 only the parent that embedding_root_id happens to follow gets queued.
  • Access (ADR-059). In case 4 a restricted node now has an open parent. §7's defect fallback keeps it out of the open root's vector and logs ADR-059 §7 defect at error level. But §2 exists precisely so that shape can't be reached, and merge is currently a way to reach it through the UI.

Proposed Solution

Make merge preserve the invariants instead of bypassing them. Decide the policy per edge type in merge_nodes_in_tx step 2:

  • Loser's inbound has_child (its parent edge):
    • If the survivor already has a parent, drop the loser's parent edge. The survivor keeps its own position, and the dropped edge counts in edges_dropped.
    • If the survivor is a root, re-point it as today (case 1). Before doing so, run the same assert_may_gain_parent check move_node uses. If the survivor holds member_of, then either refuse the merge with a clear error or drop the parent edge. Decision needed — see Open Questions.
  • Loser's outbound has_child (its children): re-point as today. The children join the survivor's tree.
  • Loser's member_of: re-point only when the survivor is (and stays) a root. Otherwise refuse the merge or drop the edge, following the same policy as above.
  • Embedding re-queue after commit: also queue the loser's former embedding root, resolved before the transaction from the loser's parent, whenever it differs from the survivor's resulting root. That tree lost the loser's text, and in the drop-parent case nothing else re-queues it. Use the existing former_parent mechanism: refresh_for_rootness_in_tx(tx, node, is_root, former_parent) defers refresh_embedding_for_rootness until after commit. Today the merge call site passes None, with a comment explaining why. That comment has to change with this fix.

Acceptance Criteria

  • After merge_nodes, the survivor has at most one has_child parent in every shape (cases 1–3, plus loser-root / survivor-child).
  • Merging never leaves a node holding member_of while it has a has_child parent (case 4 and case 5). Either the merge is refused with an error naming ADR-059 §2, or the conflicting edge is dropped and counted, per the chosen policy.
  • Every tree whose aggregate changed is re-queued after commit: the survivor's resulting root and the loser's former root when they differ. A test asserts both are stale for case 2.
  • A rolled-back merge queues nothing (the existing with_transaction deferral should already cover this; assert it).
  • Each new test is mutation-checked: removing the fix makes it fail.
  • Update the merge_nodes_in_tx doc comment (step 2) and the call-site comment in node_service/conflicts.rs to describe the new edge policy.
  • bun run quality:fix is clean, and tests pass through the pre-push gate.

Open Questions

  1. When the merge would break §2, refuse or drop? Refusing is safer and more explicit: the user must un-file the node first, the same as with move_node. Dropping keeps merge always successful but silently changes collection membership or outline position. Leaning towards refuse, for consistency with move_node's member_of_not_root error.
  2. Should the Conflicts view explain which position the survivor keeps when the loser's parent edge is dropped (case 2)?

Technical Specifications

Reference Files

  • Merge store transaction: packages/core/src/db/sqlite_store/conflicts.rs, merge_nodes_in_tx (step 2, edge re-pointing)
  • Merge service: packages/core/src/services/node_service/conflicts.rs, merge_nodes (the refresh_for_rootness_in_tx call site)
  • Gain-parent guard: packages/core/src/db/sqlite_store/nodes.rs, assert_may_gain_parent (used by move_node and bulk_create_has_child)
  • Root-only membership guard: packages/core/src/db/sqlite_store/relationships.rs, assert_root_only_membership
  • Former-root re-queue: packages/core/src/services/node_service/crud.rs, refresh_for_rootness / refresh_embedding_for_rootness / refresh_for_rootness_in_tx; embedding.rs, queue_former_embedding_root
  • Post-commit deferral: packages/core/src/services/node_service/mod.rs, NodeServiceTx::defer_embedding_refresh, with_transaction
  • Test helpers to reuse: the former_embedding_root_tests module in node_service/embedding.rs. Its two_trees, embed_fresh, is_stale and aggregate fixtures make these cases a few lines each.

Implementation Notes

  • The guards run through the store's own connection, but merge works on a transaction connection. You'll need _in_tx variants of the checks (parent-exists and member_of-exists queries against tx.conn()), not calls into the non-tx guards, which would deadlock on the write guard.
  • Resolve the loser's former embedding root before step 2 re-points its parent edge. Afterwards the loser has no parent and resolves to itself. Alternatively, capture the loser's parent id and pass it as former_parent for the survivor, and let queue_former_embedding_root compare roots after commit. That's the same after-the-write resolution PR Re-queue the old embedding root when a node leaves its tree #2936 uses.
  • Do not run bare cargo test -p nodespace-core; use bun run rust:test:lib / bun run rust:test:core.

Related

Activity

  1. malibio commented on Sep 26, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 278 files / 5406 tests passed

  2. self-assigned this
    on Sep 26, 2026
  3. added a commit that references this issue on Sep 26, 2026
    f928c5b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions