
    HמiN(                         d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZ  G d d      Zdedefd	Zd
ededefdZd
ede
e   fdZy)ua  
scripts/knowledge/mermaid_generator.py — Living Mermaid diagram generator.

Scans the Genesis codebase and produces self-contained Mermaid diagrams for:
  - System architecture (directory-level flowchart)
  - Python module import graph
  - Memory pipeline sequence diagram

Usage:
    from scripts.knowledge.mermaid_generator import MermaidGenerator
    gen = MermaidGenerator()
    diagrams = gen.generate_all("/mnt/e/genesis-system/obsidian-vault/diagrams")

# VERIFICATION_STAMP
# Story: 12.03 — MermaidGenerator
# Verified By: parallel-builder
# Verified At: 2026-02-25
# Tests: 9/9
# Coverage: 100%
    N)Path)DictListOptionalSetTuplec                       e Zd ZU dZg dZeeeef      ed<   g dZ	eeeeef      ed<   ddeddfd	Z
dd
ee   deeef   fdZdefdZdefdZdefdZy)MermaidGeneratorz
    Generates living Mermaid diagrams from the Genesis codebase structure.
    All diagrams are returned as raw Mermaid text blocks that render in
    GitHub, Obsidian, and any Mermaid-compatible renderer.
    ))corezCore Execution Layer)mcp-serverszMCP Servers)KNOWLEDGE_GRAPHzKnowledge Graph)SunaivazSunaiva Products)RECEPTIONISTAIReceptionistAI)TRADIESTradiesVoice)scriptszScripts & Tools)databasezDatabase Layer)loopzRWL Loop)deployzDeploy / Static)AIVAz
AIVA Queen)infraInfrastructure
ARCH_NODES))r   r   z	writes KG)r   r   z
tool calls)r   r   zdispatches tasks)r   r   zfeeds results)r   r   zingests data)r   r   ztriggers execution)r   r   connects)r   r   voice pipeline)r   r   r   )r   r   zproduct APIs)r   r   zstatic assets)r   r   zpersistent state)r   r   zagent commands)r   r   hosts
ARCH_EDGES	repo_pathreturnNc                 $    t        |      | _        y N)r   r   )selfr   s     </mnt/e/genesis-system/scripts/knowledge/mermaid_generator.py__init__zMermaidGenerator.__init__F   s    i    
output_dirc                 R   | j                         | j                         | j                         d}|rtt        |      }|j	                  dd       |j                         D ]C  \  }}|| dz  j                  d|j                  dd      j                          d| d	d
       E |S )z
        Generate all three diagrams.

        Args:
            output_dir: If given, write each diagram to a .md file there.

        Returns:
            {"architecture": "...", "module_map": "...", "memory_flow": "..."}
        )architecture
module_mapmemory_flowT)parentsexist_okz.mdz# _ z
 Diagram


utf-8)encoding)	generate_architecturegenerate_module_mapgenerate_memory_flowr   mkdiritems
write_textreplacetitle)r#   r'   diagramsoutnamecontents         r$   generate_allzMermaidGenerator.generate_allM   s     !668224446
 z"CIIdTI2!)!1 g$s|#//c3/5578WIRP$ 0  r&   c           	          t               }| j                  D ]4  \  }}| j                  |z  j                         s$|j	                  |       6 ddg}| j                  D ]-  \  }}||v st        |      }|j                  d| d| d       / |j                  d       | j                  D ]@  \  }}}||v s||v st        |      }	t        |      }
|j                  d|	 d| d|
        B |j                  d	       d
j                  |      S )z
        Scan the repository's top-level directories and produce a Mermaid
        flowchart of the system architecture with data-flow edges.

        Returns:
            Raw Mermaid block (```mermaid ... ```)
        
```mermaidzflowchart TD    [""] z -->|"z"| ```r0   )	setr   r   existsadd_mermaid_idappendr   join)r#   presentnode_idr.   lineslabelsafe_idsrcdstsds              r$   r3   z&MermaidGenerator.generate_architectureh   s     E// 	%JGQ(002G$	% ).9 #oo 	:NGU'!%g.tG9BugR89	:
 	R  $ 	<OCeg~#.$$tA3fUG3qc:;		< 	Uyyr&   c                    | j                   dz  }i }|j                         rt        |j                  d            D ]  }t	        ||      }t        |      }t               }|D ]Z  }|j                  d      s|j                  d      s&|j                  d      j                  dd      }|sJ|j                  |       \ |s|s|||<    ddg}	t        |j                               }
|j                         D ]  }|
j                  |        t        |
      dd	 }t        |      }|D ]%  }t        |      }|	j                  d
| d| d       ' |	j                  d       d}t        |j!                               D ]W  \  }}||vrt        |      D ]?  }||v s|dk  st        |      }t        |      }|	j                  d
| d|        |dz  }A Y |	j                  d       dj#                  |	      S )z
        Scan core/**/*.py for import statements and produce a Mermaid graph
        of module dependencies.

        Returns:
            Raw Mermaid block (```mermaid ... ```)
        r   z*.pyzcore..rE   rA   zgraph LRN(   rB   rC   rD   r   <   z -->    rF   r0   )r   rH   sortedrglob_py_to_module_name_extract_importsrG   
startswithlstripr9   rI   keysvaluesupdaterJ   rK   r7   rL   )r#   core_dirimports_mappy_filemodule_namedepsinternal_depsdepcleanrO   	all_nodesrepresentative_nodesnode_setnodesafe
edge_countmodulerT   rU   s                      r$   r4   z$MermaidGenerator.generate_module_map   s    >>F*+-??!(.."89 =0(C'0*-% 5C~~g.#..2E #

3 7 7 D )--e4	5
 !K/<K,= )*5 "+"2"2"45	&&( 	#DT"	#  &i0"5+, ) 	2Dt$DLL4vRvR01	2 	R 
";#4#4#67 	$LFDX%d| $(?zB#F+A#C(ALL4s%s!34!OJ$	$ 	Uyyr&   c                 
    d}|S )u   
        Produce a Mermaid sequence diagram of the Genesis memory pipeline:
        Conversation → Aggregation → Distillation → KG Write → Qdrant → Query.

        Returns:
            Raw Mermaid block (```mermaid ... ```)
        aU  ```mermaid
sequenceDiagram
    participant U as User
    participant C as Claude Orchestrator
    participant G as Gemini Swarm
    participant D as memory_digestion.py
    participant KG as Knowledge Graph (JSONL)
    participant Q as Qdrant Vector Store
    participant PG as PostgreSQL (Elestio)

    U->>C: Sends message / task
    C->>G: Dispatches RWL stories
    G-->>C: Returns results
    C->>D: aggregate_session(transcript)
    D->>D: Distil key entities & axioms
    D->>KG: Write entity JSONL record
    D->>KG: Write axiom JSONL record
    D->>Q: Embed & upsert vectors
    D->>PG: Persist structured state
    Note over KG,Q: Living Memory Updated

    U->>C: Follow-up query
    C->>Q: semantic_search(query)
    Q-->>C: Top-K relevant chunks
    C->>KG: Load linked entities
    KG-->>C: Entity context
    C-->>U: Grounded response
``` )r#   diagrams     r$   r5   z%MermaidGenerator.generate_memory_flow   s    : r&   )z/mnt/e/genesis-systemr"   )__name__
__module____qualname____doc__r   r   r   str__annotations__r   r%   r   r   r?   r3   r4   r5   rt   r&   r$   r
   r
      s    )JU38_%  .JU3S=)* ")# )D )x} S#X 6  s   D8 S 8 t%c %r&   r
   r=   r    c                 p    t        j                  dd|       }|r|d   j                         rd|z   }|xs dS )zc
    Convert a string to a valid Mermaid node identifier
    (alphanumeric + underscore only).
    z[^A-Za-z0-9_]r.   r   nro   )resubisdigit)r=   rp   s     r$   rJ   rJ      s:    
 66"C.DQ!Tz>6r&   rf   base_dirc                    	 | j                  |      }t        |j                        }|d   j	                  d      r|d   dd |d<   |d   dk(  r|dd }|rdj                  |      S | j                  S # t        $ r | }Y mw xY w)u|   
    Convert a Path like core/genesis_executor.py → "genesis_executor".
    Strips the base directory and .py suffix.
    z.pyNr%   rW   )relative_to
ValueErrorlistpartsendswithrL   stem)rf   r   relr   s       r$   r]   r]      s    
!!(+ OERy% "IcrNb	RyJcr
#388E?55  s   A3 3B Bc                    t               }	 | j                  dd      }	 t        j                  |t        |             }t        j                  |      D ]  }t        |t        j                        r-|j                  D ]  }|j                  |j                          Jt        |t        j                        se|j                  srd|j                  xs dz  }|j                  ||j                  z           |S # t        $ r |cY S w xY w# t        $ r Y nw xY wt!        j"                  d|t         j$                        D ]"  }|j                  |j'                  d             $ |S )	z
    Parse a Python file with ast and return a set of imported module names.
    Falls back to regex parsing if ast fails (e.g. syntax errors).
    r1   r9   )r2   errors)filenamerW   r   z^\s*(?:from|import)\s+([\w.]+)rZ   )rG   	read_textOSErrorastparserz   walk
isinstanceImportnamesrI   r=   
ImportFromrr   levelSyntaxErrorr~   finditer	MULTILINEgroup)rf   importssourcetreero   aliasprefixmatchs           r$   r^   r^     s4   
 G""GI"F
yy#g,7HHTN 	6D$

+!ZZ ,EKK

+,D#..1;; DJJO!4FKK 45	6      )62<< $ 	EKKN#$ Ns/   C< BD :D 4D <D
	D
	DD)ry   r   osr~   pathlibr   typingr   r   r   r   r   r
   rz   rJ   r]   r^   rt   r&   r$   <module>r      so   *  	 	  3 3L Lf	c 	c 	6 6 6 6"d s3x r&   