Repository navigation
Description
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:
- A survivor with two
has_childparents. This happens when both nodes are children in different trees. - A survivor holding
member_ofwhile 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_childas a tree:move_nodereplaces the parent, andbulk_create_has_childskips 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_idhappens 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 defectat 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_parentcheckmove_nodeuses. If the survivor holdsmember_of, then either refuse the merge with a clear error or drop the parent edge. Decision needed — see Open Questions.
- 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
- 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_parentmechanism:refresh_for_rootness_in_tx(tx, node, is_root, former_parent)defersrefresh_embedding_for_rootnessuntil after commit. Today the merge call site passesNone, with a comment explaining why. That comment has to change with this fix.
Acceptance Criteria
- After
merge_nodes, the survivor has at most onehas_childparent in every shape (cases 1–3, plus loser-root / survivor-child). - Merging never leaves a node holding
member_ofwhile it has ahas_childparent (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_transactiondeferral should already cover this; assert it). - Each new test is mutation-checked: removing the fix makes it fail.
- Update the
merge_nodes_in_txdoc comment (step 2) and the call-site comment innode_service/conflicts.rsto describe the new edge policy. -
bun run quality:fixis clean, and tests pass through the pre-push gate.
Open Questions
- 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 withmove_node'smember_of_not_rooterror. - 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(therefresh_for_rootness_in_txcall site) - Gain-parent guard:
packages/core/src/db/sqlite_store/nodes.rs,assert_may_gain_parent(used bymove_nodeandbulk_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_testsmodule innode_service/embedding.rs. Itstwo_trees,embed_fresh,is_staleandaggregatefixtures 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_txvariants of the checks (parent-exists andmember_of-exists queries againsttx.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_parentfor the survivor, and letqueue_former_embedding_rootcompare 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; usebun run rust:test:lib/bun run rust:test:core.
Related
- PR Re-queue the old embedding root when a node leaves its tree #2936 / Re-queue the old embedding root when a node leaves its tree #2907: re-queue the old embedding root when a node leaves its tree. It added the
former_parentmechanism and the post-commit tx deferral this fix builds on. - Exclude and re-root a descendant whose access differs from its embedding root's #2895: embedding roots and access boundaries.
- ADR-059 §2 (root-only membership) and §7 (access-boundary descendants).
Activity
malibio commentedon Sep 26, 2026
Frontend baseline: 278 files / 5406 tests passed
Keep merge_nodes inside the tree invariants (closes #2938)