
    ci                        d Z ddlZddlZddlZddlmZmZ ddlmZ ddlm	Z	  ed      Z
e
dz  dz  Ze
dz  d	z  Ze
d
z  dz  ZeedZddZ	 	 ddedededee   dz  def
dZddededefdZdedee   fdZ	 ddedededee   fdZy)u  
CTM (Commit To Memory) — Knowledge Graph Write Layer
=====================================================
Writes agent discoveries and strategic insights to the Genesis
Knowledge Graph and MEMORY.md auto-memory file.

All outputs land on E: drive under:
  /mnt/e/genesis-system/KNOWLEDGE_GRAPH/entities/
  /mnt/e/genesis-system/KNOWLEDGE_GRAPH/axioms/

Author: Genesis Parallel Builder
Created: 2026-02-26
    N)datetimetimezone)Path)uuid4z/mnt/e/genesis-systemKNOWLEDGE_GRAPHentitiesaxiomsmemoryz	MEMORY.md)entityaxiomreturnc                  `    t         j                  dd       t        j                  dd       y)zEnsure KG directories exist.Tparentsexist_okN)KG_ENTITIES_DIRmkdirKG_AXIOMS_DIR     8/mnt/e/genesis-system/core/gemini_command_centres/ctm.py_ensure_dirsr   $   s(    $6t4r   
agent_namecontentcategorytagsc                 4   t                t        j                  t        j                        j                  d      }d|  d| d}t        j                  |t              }||z  }d|  dt               j                  dd  }|d|  t        j                  t        j                        j                         |||xs g d}	t        |d	d
      5 }
|
j                  t        j                  |	      dz          ddd       dt!        |      |dS # 1 sw Y   xY w)a  
    Write a CTM entry to the Genesis Knowledge Graph.

    Args:
        agent_name: Short name of the originating agent (e.g. "orchestrator").
        content: The insight, decision, or fact to record.
        category: "entity" (default) or "axiom".
        tags: Optional list of tag strings for later filtering.

    Returns:
        dict with {"status": "ok", "file": str, "id": str}
    z%Y_%m_%dgcc__z.jsonlN   zgcc-)idsource	timestampr   typer   autf-8encoding
ok)statusfiler!   )r   r   nowr   utcstrftime_CATEGORY_DIRSgetr   r   hex	isoformatopenwritejsondumpsstr)r   r   r   r   date_strfilename
target_dirfilepathentry_identryfhs              r   	ctm_to_kgr@   *   s   $ N||HLL)22:>Hj\8*F3H##Ho>JH$Hj\57;;r?"34H%\\(,,/99;
E 
hg	. +"
E"T)*+ CMBB+ +s   (DDc                    t         j                  j                  dd       t        j                  t
        j                        j                  d      }d|j                          d| d}| | j                          d}t        t         dd	
      5 }|j                  |       ddd       dt        t               dS # 1 sw Y   xY w)z
    Append a short entry to the auto-memory MEMORY.md file.

    Args:
        content: The memory line to append.
        agent_name: Source agent name (used in header label).

    Returns:
        dict with {"status": "ok", "file": str}
    Tr   z%Y-%m-%d %H:%M UTCz
## GCC-z CTM (z)
r)   r%   r&   r'   Nr*   )r+   r,   )MEMORY_FILEparentr   r   r-   r   r.   r/   upperstripr4   r5   r8   )r   r   r#   headerblockr?   s         r   ctm_to_memoryrH   S   s     TD9X\\*334HII))+,F9+SAFhw}}'r*E	k3	1 R
 C$455 s   B>>Ctextc                     t        j                  dt         j                  t         j                  z        }|j	                  |       }|D cg c]#  }|j                         s|j                         % c}S c c}w )ac  
    Extract all [CTM] ... [/CTM] blocks from an agent response.

    Supports both inline and multiline formats:
        [CTM] single line insight [/CTM]
        [CTM]
        multi-line
        insight
        [/CTM]

    Args:
        text: Raw text response from a Gemini agent.

    Returns:
        List of extracted content strings (stripped).
    z\[CTM\](.*?)\[/CTM\])recompileDOTALL
IGNORECASEfindallrE   )rI   patternmatchesms       r   extract_ctm_blocksrS   j   sR    $ jj0"))bmm2KLGood#G&4!!'')AGGI444s   A6!A6c                 l    t        |       }g }|D ]!  }t        |||      }|j                  |       # |S )a  
    Find all [CTM] blocks in text, write each to the KG.

    Args:
        text: Agent response text.
        agent_name: Originating agent identifier.
        category: KG category ("entity" | "axiom").

    Returns:
        List of write results (one per block).
    )r   r   r   )rS   r@   append)rI   r   r   blocksresultsrG   results          r   process_ctm_blocksrY      sC       %FG j%(Sv Nr   )r   N)r   N)gcc)r   )__doc__r6   osrK   r   r   pathlibr   uuidr   GENESIS_ROOTr   r   rB   r0   r   r8   listdictr@   rH   rS   rY   r   r   r   <module>rb      s    	 	 '   +,!22Z?008;X%3 5 !	&C&C&C &C s)d
	&C
 
&CR63 6C 6D 6.5S 5T#Y 54 
  
$Z	r   