Skip to content

Re-queue the old embedding root when a node leaves its tree #2907

Closed

Description

@malibio

Overview

When a node leaves a tree, nothing re-queues the tree it left. The old root's embedding keeps the departed subtree's text until someone next edits that tree. Leaving includes:

  • outdenting to root;
  • moving under a parent in another tree;
  • deleting its has_child edge.

Joining a tree works: the tree gained is re-queued.

Problem Statement

The embedding queue follows has_child. Every path that changes a node's parent calls refresh_for_rootness(node_id, is_root). That function fixes the moved node's title and its own embedding, then calls queue_root_for_embedding(node_id). That resolves the node's new embedding root. The old root, whose aggregate just lost the node's subtree, is never queued.

Deleting a node does not have this problem. delete_node resolves the embedding root before the delete and queues it afterwards.

Two consequences:

  • Stale search. The old root still ranks for text it no longer contains, until an unrelated edit to that tree re-queues it.
  • Access leak through a legitimate flow. A user outdents a line so it becomes its own root. They then file it into a restricted collection, which is allowed because it is now a root. The old open root's vector still carries the line's text, so semantic search surfaces its meaning to readers outside the collection. This is the leak ADR-059 §7 exists to prevent, and it happens with no invariant bypassed.

member_of plays no part in the fix. Embeddings follow has_child only. The trigger is the node's position in the tree, not its classification.

Proposed Solution

In each path that removes or replaces a node's has_child parent, resolve the node's embedding root before the edge changes (NodeService::get_embedding_root_id). After the change, queue that old root when it differs from the new one. This is the same capture-before, queue-after pattern delete_node already uses.

Paths to cover:

  • NodeService::move_node and move_node_unchecked (packages/core/src/services/node_service/hierarchy.rs), for outdent-to-root and reparent across trees.
  • The has_child relationship-delete paths in packages/core/src/services/node_service/relationship.rs, the ones that call refresh_for_rootness(target_id, true) / refresh_for_rootness_in_tx(.., true).
  • move_children_to_parent and any other refresh_for_rootness call site that detaches a node. Audit every call site; each one that can change the parent must queue the old root.

A move within the same tree resolves the old and new roots to the same id, so no extra queue is written.

Acceptance Criteria

  • Outdenting a node to root re-queues its former tree root. A test asserts the old root's embedding is marked stale, or gets a stale marker.
  • Moving a node under a parent in a different tree re-queues both the old and the new root.
  • Deleting a node's has_child edge through the relationship API re-queues the former tree root.
  • A move within one tree queues that tree once, and nothing else.
  • The outdent-then-file case is covered: after outdenting and filing a line into a restricted collection, the former root is queued. Its rebuilt aggregate excludes the line.
  • Each new test is mutation-checked: removing the old-root queue makes it fail.
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Technical Specifications

Reference Files

  • Rootness refresh: packages/core/src/services/node_service/crud.rs, refresh_for_rootness / refresh_for_rootness_in_tx
  • Move paths: packages/core/src/services/node_service/hierarchy.rs, move_node, move_node_unchecked
  • Relationship delete: packages/core/src/services/node_service/relationship.rs, the has_child detach call sites
  • Pattern to copy: packages/core/src/services/node_service/crud.rs, delete_node (the root_id_for_embedding capture and queue)
  • Embedding root: packages/core/src/services/node_service/embedding.rs, get_embedding_root_id, queue_root_for_embedding

Implementation Notes

  • Queueing is #[cfg(feature = "nlp")] on the service paths. The stale-marker assertions need the store-level marker (has_embeddings / stale state), not a running NLP engine.
  • The _in_tx relationship path writes no embedding state inside the transaction (see refresh_for_rootness_in_tx). Queue the old root after commit, as the non-tx path does, rather than inside the transaction.
  • Do not run bare cargo test -p nodespace-core; use bun run rust:test:lib / bun run rust:test:core.

Estimate: small, about half a day. It touches roughly 3–4 call sites, applies one existing pattern, and adds one test module.

Related Issues

Activity

  1. malibio commented on Sep 25, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 262 files / 5269 tests passed

  2. self-assigned this
    on Sep 25, 2026
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