Skip to content

Guard the live build_node_tree_recursive against cyclic hierarchy data so a bad edge is an error, not a daemon abort #2417

Closed

Description

@malibio

Problem

build_node_tree_recursive (packages/core/src/services/node_service/mod.rs:1844) walks adjacency_list with no depth limit and no visited set:

children_ids
    .iter()
    .filter_map(|child_id| {
        node_map.get(child_id).map(|child_node| {
            build_node_tree_recursive(child_node, node_map, adjacency_list)
        })
    })

If a has_child cycle ever reaches the relationship table, this recurses until the stack is exhausted. A Rust stack overflow aborts the process rather than unwinding, so it cannot be caught and turned into an error response.

Why the upstream cap does not cover this

get_subtree_with_relationships bounds its WITH RECURSIVE walk with WHERE s.depth < 100. That bound applies to the SQL ID-collection query, which would terminate normally. But the relationship set is queried separately and used to build adjacency_list in memory — a back-edge present there is not filtered by the SQL depth cap. The in-memory rebuild is unguarded.

Blast radius

The call path is user-facing and hot — this runs on opening a page:

backend-adapter.ts:132 → Tauri command (commands/nodes.rs:681) → gRPC daemon (daemon/src/services/node_service.rs:413) → get_children_tree (hierarchy.rs:99) → the unguarded builder

So the symptom is the daemon dying when a user opens an affected page, with no error surfaced.

Likelihood

Low. Cycle prevention exists at write time — hierarchy.rs:661 (is_descendant on move) and relationship.rs:504-516 (validate_no_member_of_cycle). This is defence-in-depth against a write-path bug, a direct DB edit, an import, or a partially-applied write. Low probability, high blast radius, cheap fix.

Suggested fix

Add a depth ceiling and a visited set to the recursion, returning a handled error instead of recursing forever. The chain deleted in #2401 did exactly this and is available in git history as a reference implementation:

Err(anyhow!("Maximum tree depth ({}) exceeded at node '{}'", max_depth, node.id))
Err(anyhow!("Cycle detected: node '{}' appears multiple times", node.id))

Note the current fn returns serde_json::Value, not Result, so the signature needs to change or the guard needs to degrade gracefully (e.g. truncate the branch and log) — worth deciding deliberately, since get_children_tree already returns Result<_, NodeServiceError> and can carry a real error.

Acceptance criteria

  • build_node_tree_recursive cannot recurse unboundedly on cyclic or malformed adjacency_list data
  • A cycle or excessive depth produces a handled error (or a deliberately-chosen graceful degradation), never a stack-overflow abort
  • A regression test constructs an adjacency_list containing a back-edge and asserts the call returns rather than crashing
  • Existing get_children_tree behaviour on well-formed data is unchanged

Context

Surfaced during code review of #2412 (PR for #2401), which deleted an unrelated dead SqliteStore tree-builder that did carry these guards. Noticing what that dead code protected against exposed the gap in the live path. Not a regression from #2401 — the live builder never had these guards.

Activity

  1. malibio commented on Sep 7, 2026

    @malibio
    CollaboratorAuthor

    Baseline established on worktree issue-2417-tree-cycle-guard — Frontend: 232 test files passed, 4863 tests passed (0 failures).

  2. self-assigned this
    on Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions