Skip to content

Fix create_nodes_from_markdown hierarchy: Same-level headers incorrectly nested #350

Closed

Description

@malibio

Overview

The create_nodes_from_markdown MCP method incorrectly nests same-level headers under each other instead of making them siblings under their parent. For example, multiple H2 headers should all be children of the H1, but currently H2[2] is a child of H2[1], H2[3] is a child of H2[2], etc.

Current (Incorrect) Structure

From database after MCP import:

Container: # Product Launch Campaign Strategy (H1)
  id: 482fbcf2-916b-48ec-b8db-37cc8a383196
  parent_id: NULL
  container_node_id: NULL

├─ text: "This comprehensive product launch..."
│  parent_id: 482fbcf2... (CORRECT - child of container)
│
├─ ## Launch Overview (H2)
│  id: 7839aca9...
│  parent_id: 482fbcf2... (CORRECT - child of H1)
│
└─ ## Executive Summary (H2) ❌ WRONG
   id: 25be8813...
   parent_id: 7839aca9... (WRONG - should be 482fbcf2, not previous H2!)
   
   └─ ## Target Audience Analysis (H2) ❌ WRONG  
      id: 08f0b97c...
      parent_id: 25be8813... (WRONG - should be 482fbcf2!)
      
      └─ ### Primary Target Segment (H3) ✅ CORRECT
         parent_id: 08f0b97c... (CORRECT - child of H2)

Problem

Same-level headers are being nested sequentially instead of as siblings:

  • H2[1] → child of H1 ✅
  • H2[2] → child of H2[1] ❌ (should be child of H1)
  • H2[3] → child of H2[2] ❌ (should be child of H1)

Expected behavior:

  • All H2 headers should be siblings, all children of the H1 parent
  • H3 headers should be children of their preceding H2
  • H4 headers should be children of their preceding H3

Expected (Correct) Structure

Container: # Product Launch Campaign Strategy (H1)
├─ text: "This comprehensive..."
├─ ## Launch Overview (H2)
│  ├─ text: (any content under Launch Overview)
│  └─ ### Subsection (H3) - if exists
├─ ## Executive Summary (H2)  
│  ├─ text: (any content under Executive Summary)
│  └─ ### Subsection (H3) - if exists
└─ ## Target Audience Analysis (H2)
   ├─ text: (any content)
   └─ ### Primary Target Segment (H3)
      └─ text: demographic info

Investigation Required

Need to analyze the heading hierarchy tracking in parse_markdown() function:

File: packages/core/src/mcp/handlers/markdown.rs

Suspected Issue:

  • heading_stack logic might not be popping correctly
  • When seeing same-level heading (e.g., H2 after H2), should pop to parent (H1)
  • Currently seems to be keeping previous H2 as parent

Key Code Section:
The heading_stack management around line 300-450 in markdown.rs handles heading hierarchy. Need to verify:

  1. When H2 is encountered after H2, does it pop back to H1?
  2. When H3 is encountered after H2, does it correctly use H2 as parent?
  3. When H2 is encountered after H3, does it pop back to H1?

Acceptance Criteria

  • Same-level headers are siblings (same parent)
  • H2 headers all have H1 as parent
  • H3 headers have their preceding H2 as parent
  • H4 headers have their preceding H3 as parent
  • Test with multiple H2s, nested H3s, back to H2, etc.
  • Verify with the Product Launch document structure
  • All 116 nodes have correct parent relationships

Technical Specifications

Reference Files

  • Markdown Parser: packages/core/src/mcp/handlers/markdown.rs
  • Test Database: /Users/malibio/.nodespace/database/nodespace.db

Current Database State

Total nodes: 116

  • Container: 482fbcf2-916b-48ec-b8db-37cc8a383196 (H1)
  • Headers: 20 nodes
  • Text: 89 nodes
  • Date: 1 node (2025-10-24)

Query to see current structure:

SELECT id, node_type, parent_id, SUBSTR(content, 1, 50)
FROM nodes
WHERE container_node_id = '482fbcf2-916b-48ec-b8db-37cc8a383196'
   OR id = '482fbcf2-916b-48ec-b8db-37cc8a383196'
ORDER BY created_at;

Example Incorrect Parent Relationships

  • H1 Container: 482fbcf2... parent: NULL ✅
  • H2 "Launch Overview": 7839aca9... parent: 482fbcf2... ✅
  • H2 "Executive Summary": 25be8813... parent: 7839aca9... ❌ (should be 482fbcf2...)
  • H2 "Target Audience": 08f0b97c... parent: 25be8813... ❌ (should be 482fbcf2...)

Definition of Done

  • Heading hierarchy logic fixed in markdown parser
  • Test import creates correct parent-child relationships
  • Same-level headers are siblings
  • Nested headers have correct parent
  • Product Launch document re-imported with correct structure
  • All tests pass
  • Code review completed

Activity

  1. self-assigned this
    on Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions