AI-generated trees can have circular next_node_id references (e.g., node A → B → A). The parent mapping now checks for cycles before assigning parent_node_id, preventing FK deadlocks during insert. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>