Skip to content

๐Ÿ” Hierarchy Discovery Integration Guide

Status: ๐ŸŸก PREPARED - NOT YET ENABLED

This guide documents how to enable automatic XX_YY_ZZ hierarchy discovery in the MkDocs container.


๐Ÿ“‹ Overview

The hierarchy discovery script automatically scans the AudioLab repository for directories following the XX_YY_ZZ_Name pattern and generates MkDocs navigation structure.

Current Status: - โœ… Script created and corrected for container environment - โœ… Dockerfile prepared with script support - ๐Ÿ”ด Feature is DISABLED by default


๐Ÿ”ง What Was Fixed

Original Problem

The original discover_hierarchy.py script failed in containers because:

  1. Path assumption: Assumed host filesystem structure

    # โŒ Old (broken in container)
    repo_root = script_path.parents[5]
    

  2. Hardcoded output paths: Not container-aware

    # โŒ Old (broken in container)
    output_file = script_path.parent / 'nav_structure.yml'
    

Container-Optimized Solution

The corrected script now:

  1. Smart path detection:

    # โœ… New (container-aware)
    def find_repo_root(start_path: Path) -> Path:
        # Strategy 1: Container environment detection
        docs_path = Path('/docs')
        if docs_path.exists():
            # /docs is mounted from 07_META/03_documentation
            # So repo root is 2 levels up
            return docs_path.parent.parent
    
        # Strategy 2: Git repository detection
        # ... (fallback strategies)
    

  2. Container-aware output:

    # โœ… New (container paths)
    def get_output_paths() -> Tuple[Path, Path]:
        nav_file = Path('/docs/nav_structure.yml')
        mkdocs_file = Path('/docs/mkdocs.yml')
        return nav_file, mkdocs_file
    

  3. Multiple detection strategies:

  4. Container mount detection (/docs exists)
  5. Git repository detection (.git directory)
  6. Marker file detection (vcpkg.json, expected directories)

๐Ÿ“‚ File Locations

03_06_07_services_stack/
โ”œโ”€โ”€ services/
โ”‚   โ””โ”€โ”€ mkdocs/
โ”‚       โ”œโ”€โ”€ Dockerfile                          # โœ… Prepared (script disabled)
โ”‚       โ””โ”€โ”€ scripts/
โ”‚           โ””โ”€โ”€ discover_hierarchy.py           # โœ… Corrected script
โ””โ”€โ”€ HIERARCHY_DISCOVERY_INTEGRATION.md          # โ† This file

๐Ÿš€ How to Enable (When Ready)

Step 1: Uncomment Script in Dockerfile

Edit services/mkdocs/Dockerfile:

# Copy hierarchy discovery script
# NOTE: Currently DISABLED - uncomment when ready to enable auto-discovery
COPY --chown=audiodev:audiodev scripts/discover_hierarchy.py /opt/scripts/
RUN chmod +x /opt/scripts/discover_hierarchy.py

Change to:

# Copy hierarchy discovery script
COPY --chown=audiodev:audiodev scripts/discover_hierarchy.py /opt/scripts/
RUN chmod +x /opt/scripts/discover_hierarchy.py

Step 2: Update docker-compose.yml Command

Edit docker-compose.yml MkDocs service:

mkdocs:
  # ... other config ...
  command: mkdocs serve --dev-addr=0.0.0.0:8000

Change to:

mkdocs:
  # ... other config ...
  command: >
    sh -c "
    python /opt/scripts/discover_hierarchy.py &&
    mkdocs serve --dev-addr=0.0.0.0:8000 --watch-theme
    "

Step 3: Rebuild and Restart

# Rebuild MkDocs image
docker-compose build mkdocs

# Restart service
docker-compose up -d mkdocs

# View logs to verify
docker-compose logs -f mkdocs

๐Ÿ“Š How It Works

Container Environment

Host Filesystem              Container Filesystem
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€            โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
/path/to/audio-lab/          (not directly accessible)
โ”œโ”€โ”€ 2 - FOUNDATION/
โ”œโ”€โ”€ 06_BRAIN/
โ””โ”€โ”€ 07_META/
    โ””โ”€โ”€ 03_documentation/    โ†’  /docs  (volume mount)
        โ”œโ”€โ”€ mkdocs.yml
        โ”œโ”€โ”€ docs/
        โ””โ”€โ”€ (generated)
            โ””โ”€โ”€ nav_structure.yml

Script Execution Flow

  1. Script starts inside container at /opt/scripts/discover_hierarchy.py

  2. Path detection:

  3. Detects /docs mount point exists
  4. Calculates repo root as /docs/../.. (via volume mount structure)

  5. Hierarchy scan:

  6. Scans repo root for XX_YY_ZZ_Name directories
  7. Finds README files in each component

  8. Navigation generation:

  9. Creates hierarchical nav structure
  10. Writes to /docs/nav_structure.yml
  11. Updates /docs/mkdocs.yml

  12. MkDocs starts:

  13. Loads generated navigation
  14. Serves documentation with auto-discovered structure

๐Ÿงช Testing the Script

Test in Isolation (Before Enabling)

# Access running mkdocs container
docker exec -it audiolab_mkdocs bash

# If script was copied (after uncommenting), run manually:
python /opt/scripts/discover_hierarchy.py

# Expected output:
# ======================================================================
# AudioLab Documentation Hierarchy Discovery
# ======================================================================
#
# ๐Ÿ“ Script location: /opt/scripts/discover_hierarchy.py
# โœ“ Detected container environment
#   /docs mount โ†’ /docs
#   Repo root   โ†’ /docs/../..
# ๐Ÿ“ Repository root: /docs/../..
# ๐Ÿ“ Nav output: /docs/nav_structure.yml
# ๐Ÿ“ MkDocs config: /docs/mkdocs.yml
# ...

Verify Output Files

# Check generated nav structure
docker exec audiolab_mkdocs cat /docs/nav_structure.yml

# Check updated mkdocs config
docker exec audiolab_mkdocs cat /docs/mkdocs.yml

โš ๏ธ Troubleshooting

Issue: Script Not Found

Error: /opt/scripts/discover_hierarchy.py: No such file or directory

Solution: You forgot to uncomment the COPY command in Dockerfile. See Step 1 above.


Issue: IndexError or Path Issues

Error: IndexError: tuple index out of range or similar path errors

Solution: The corrected script handles this. If still occurring:

  1. Check volume mounts are correct in docker-compose.yml:

    volumes:
      - ../../../../07_META/03_documentation:/docs:rw
    

  2. Verify from inside container:

    docker exec audiolab_mkdocs ls -la /docs
    docker exec audiolab_mkdocs python -c "from pathlib import Path; print(Path('/docs').exists())"
    


Issue: No Hierarchy Found

Error: โš  No XX_YY_ZZ hierarchy found!

Possible causes: 1. Repository structure doesn't have XX_YY_ZZ directories 2. Volume mount is incorrect 3. Script is scanning wrong path

Debug:

# Check repo root detection
docker exec audiolab_mkdocs python -c "
from pathlib import Path
from scripts.discover_hierarchy import find_repo_root
print(find_repo_root(Path('/opt/scripts')))
"

# Manually check for XX_YY_ZZ directories
docker exec audiolab_mkdocs find /docs/../.. -type d -name "[0-9][0-9]_[0-9][0-9]_[0-9][0-9]_*" | head


Issue: Permission Denied Writing Files

Error: Permission denied: '/docs/nav_structure.yml'

Solution: Ensure volume mount has write permissions (:rw flag):

volumes:
  - ../../../../07_META/03_documentation:/docs:rw  # โ† Note :rw


๐Ÿ“ Generated Output Format

nav:
  - Level 02:
      - Level 04:
          - Core Interfaces: 2_foundation/04_core/04_01_core_interfaces/README.md
          - Math Primitives: 2_foundation/04_core/04_02_math_primitives/README.md
      - Level 05:
          - Buffer Management: 2_foundation/04_core/04_05_buffer_management/README.md

metadata:
  generated_by: discover_hierarchy.py
  description: Auto-generated navigation from XX_YY_ZZ hierarchy

Updated mkdocs.yml

The script merges the generated nav structure into mkdocs.yml and adds a reference:

# ... existing config ...

GENERATED_NAV_FILE: /docs/nav_structure.yml

nav:
  - Level 02:
      - Level 04:
          # ... auto-generated structure ...

๐Ÿ”„ Re-generation

Automatic Re-generation

The script runs every time the container starts (when enabled in docker-compose.yml).

To force re-generation:

docker-compose restart mkdocs

Manual Re-generation

# Run script manually
docker exec audiolab_mkdocs python /opt/scripts/discover_hierarchy.py

# Reload MkDocs (if using dev server, it auto-reloads)

๐Ÿ“š Integration Workflow

Development Workflow

  1. Create new XX_YY_ZZ component:

    mkdir "2 - FOUNDATION/04_CORE/04_XX_new_component"
    echo "# New Component" > "2 - FOUNDATION/04_CORE/04_XX_new_component/README.md"
    

  2. Restart MkDocs:

    docker-compose restart mkdocs
    

  3. Verify in docs:

  4. Visit http://localhost:8000
  5. New component appears in navigation automatically

CI/CD Integration

# Example GitHub Actions workflow
- name: Generate Documentation Nav
  run: |
    docker-compose run --rm mkdocs python /opt/scripts/discover_hierarchy.py

- name: Build Documentation
  run: |
    docker-compose run --rm mkdocs mkdocs build

๐ŸŽฏ Benefits of Auto-Discovery

โœ… Automatic navigation: No manual mkdocs.yml editing for new components

โœ… Consistency: Navigation structure matches repository structure

โœ… Developer-friendly: Add README.md to component โ†’ automatically in docs

โœ… Hierarchy enforcement: Only XX_YY_ZZ directories included

โœ… Maintenance-free: Documentation scales with codebase


๐Ÿ” Security Considerations

Current setup is safe: - โœ… Script runs as non-root user (audiodev:1000) - โœ… Only writes to /docs (mounted volume) - โœ… Read-only scan of repository structure - โœ… No external network access required

Production recommendations: - Pin Python package versions (already done in Dockerfile) - Validate YAML output before merging to mkdocs.yml - Run in isolated container (already isolated)


๐Ÿ“… Roadmap

Current Status (v1.0)

  • โœ… Basic XX_YY_ZZ pattern detection
  • โœ… README.md file discovery
  • โœ… MkDocs nav generation
  • โœ… Container-optimized paths

Future Enhancements (v2.0)

  • Support for index.md files
  • Metadata extraction from frontmatter
  • Diagram file detection and linking
  • Component dependency visualization
  • Search integration with hierarchy

Future Enhancements (v3.0)

  • Real-time file watching (regenerate on changes)
  • Multi-language documentation support
  • Custom hierarchy patterns beyond XX_YY_ZZ
  • GraphQL API for hierarchy data

๐Ÿ†˜ Support

If you encounter issues:

  1. Check this guide for troubleshooting section
  2. View container logs:
    docker-compose logs mkdocs
    
  3. Test script manually:
    docker exec -it audiolab_mkdocs python /opt/scripts/discover_hierarchy.py
    
  4. Verify paths:
    docker exec audiolab_mkdocs ls -la /docs /opt/scripts
    

Remember: The feature is currently DISABLED by default. Enable only when ready and tested.


Last Updated: 2025-10-04 Status: Ready for activation when needed Script Version: 2.0 (Container-Optimized)