
    z7i              	           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  ed      Zedz  dz  Z	edz  d	z  Z
d
ZdZddedededefdZddedededefdZdededee   fdZdededdfdZy)u  
FORGE — Architecture & Build General
======================================
Fires parallel builder agents per story from a PRD.

Usage:
    from core.generals.forge import spawn_forge
    results = spawn_forge("/mnt/e/genesis-system/plans/PRD_VOICE_WIDGET.md", story_count=50)
    N)datetimetimezone)Pathz/mnt/e/genesis-systemdataswarm_progresshiveprogressu  You are a FORGE builder agent for Genesis.

PRD: {prd_path}

Your task: Implement Story {story_index} of {total_stories}

Story title: {story_title}
Story description:
{story_body}

Instructions:
1. Read the full PRD file first to understand the system context
2. Implement ONLY this story — atomic, focused
3. Write black box tests (external behaviour)
4. Write white box tests (internal paths)
5. All tests must pass before marking story VERIFIED
6. Add VERIFICATION_STAMP comment to your implementation
7. Write output to: {output_file}

Constraints:
- NO SQLite — use PostgreSQL (Elestio) for persistence
- ALL files on E: drive only
- If tests fail: iterate up to 30 times before escalating
aW  You are the FORGE Architect for Genesis.

Mission: {mission}

Your role:
1. Design the full system architecture (diagrams, module structure)
2. Create a PRD with minimum 50 atomic stories
3. Each story must have acceptance criteria + test plan
4. Output the PRD to: {output_file}

Follow the PRD structure from GLOBAL_GENESIS_RULES.md Rule 4.
prd_pathstory_countmissionreturnc                    t         j                  dd       t        j                  dd       dt        j                         j
                  dd  }t        |z  }|j                  dd       | rt        |       nd}g }|rA|j                         r1t        ||      }t        dt        |       d|j                          np|dz  }t        j                  |xs | t        |      	      }|d
z  j                  |       t        d|        t!        |      D 	cg c]  }	|	dz   d|	dz    dd }}	|d|xs t        |       dt        |       t        |      t        |      dddt#        j$                  t&        j(                        j+                         t        |      d}
t         | dz  }|D ]  }|d|d   ddz  }t,        j                  | |d   t        |      |j/                  dd|d          |j/                  dd      t        |            }|d|d   ddz  j                  |        t1        ||
       t        d| d t        |       d!       |
S c c}	w )"u  
    Spawn builder agents per story in a PRD.

    Parameters
    ----------
    prd_path : str
        Absolute path to the PRD file (or empty to trigger architect first)
    story_count : int
        Number of story-builder agents to launch (default 50)
    mission : str
        High-level mission (used if no PRD exists yet — triggers architect)

    Returns
    -------
    dict with swarm metadata
    T)parentsexist_okforge_N   z[FORGE] Loaded z stories from zPRD_GENERATED.md)r   output_filezarchitect_prompt.mdu5   [FORGE] No PRD found — architect prompt written to    Story  indextitlebodyFORGEzminimax/minimax-01r   running)swarm_idgeneralr   modelr
   agent_countstories_totalstories_completedstories_completed_this_hourstatus
started_at
output_dirz.jsonstory_r   03dz
_output.mdr   r   zSee PRD for details)r
   story_indextotal_storiesstory_title
story_bodyr   z
_prompt.mdz[FORGE] Swarm u    — z builder agents queued)SWARM_PROGRESS_DIRmkdirHIVE_PROGRESS_DIRuuiduuid4hexr   exists_extract_storiesprintlennameFORGE_ARCHITECT_PROMPTformatstr
write_textranger   nowr   utc	isoformatFORGE_BUILDER_PROMPTget_save_state)r
   r   r   r   r&   prd_filestoriesarch_outputarch_promptiswarm_state
state_filestoryr   prompts                  ,/mnt/e/genesis-system/core/generals/forge.pyspawn_forgerM   <   s   " TD9D48

((!,-.H"X-JTD1!)tH~tHGHOO%"8[9G~^HMM?KL !#55,33'xK( 4 
 
+	+77DEk]STRWXcRdeQQUvacU^RHee +c(m%M7|W'(ll8<<0::<*oK $
%&88J  
R VE'N3+?z#JJ%,,gg,		'VE'N3C+DEyy)>?K( - 
 
uW~c2*=	=II&Q
R 
K(	N8*E#g,7M
NOK fs   Ic                     t        | ||      S )u9   Alias for spawn_forge — used by genesis_hive.py router.)r   r   )rM   )r
   r   r   s      rL   spawnrO      s    x['JJ    rC   limitc                 2   | j                  dd      }g }t        j                  dt        j                  t        j                  z        }|j                  |      D ]  }t        |j                  d            }|j                  d      j                         }|j                  d      j                         }|j                  |||d       t        |      |k\  s n |s2t        |      D 	cg c]  }	|	dz   d	|	dz    d
| j                   dd  }}	|S c c}	w )z,Parse Story blocks from a PRD Markdown file.zutf-8replace)encodingerrorszG###\s+Story\s+(\d+)[:\s]+([^\n]+)\n(.*?)(?=###\s+Story\s+\d+|^##\s+|\Z)r         r   r   z from r   )	read_textrecompileDOTALL	MULTILINEfinditerintgroupstripappendr6   r<   r7   )
rC   rQ   contentrD   patternmatchidxr   r   rG   s
             rL   r4   r4      s     ') DGG jjR
		BLL G !!'* %++a.!A$$&{{1~##%udCDw<5  glmrgstbcQUvacU&-PZ\]ttN us   -#Dpathstatec                 Z    | j                  t        j                  |dt                     y )NrV   )indentdefault)r;   jsondumpsr:   )rf   rg   s     rL   rB   rB      s    OODJJuQ<=rP   )2   r   )r   rm   r   )__doc__rk   rY   r0   r   r   pathlibr   	REPO_ROOTr-   r/   r@   r8   r:   r^   dictrM   rO   listr4   rB    rP   rL   <module>rt      s     	  ' ()	'*:: &3  2 M# MC Ms MD M`KC K3 Kc K4 K
t C DJ 2>d >4 >D >rP   