
    iU[                        d Z ddlZddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZ ddlmZ ddlZej(                  j+                  d e e
e      j0                  j0                               ddlmZmZ dd	lmZ  G d
 de      Ze G d d             Ze G d d             Z  G d d      Z!da"de!fdZ#ddedede fdZ$dee   de fdZ%ddedede fdZ&d Z'e(dk(  r e'        yy)am  
GENESIS EXECUTION LAYER
========================
The HARDWIRED execution pattern for ALL Genesis agents.

ANY task entering Genesis MUST flow through this layer, which enforces:
1. RWL Swarm execution (Gemini agents as primary workers)
2. Rate Limit Maximizer (stay at 90-95% capacity)
3. Automatic task decomposition into RWL stories
4. Parallel execution where possible

This is the SINGLE SOURCE OF TRUTH for task execution.

Usage (ALL agents must use this):
    from core.genesis_execution_layer import execute_task, execute_rwl_swarm

    # Single task
    result = await execute_task("Implement feature X")

    # PRD/Multi-task
    results = await execute_rwl_swarm(prd_or_tasks)

Entry Points That MUST Use This Layer:
- CLAUDE.md agent briefings
- GEMINI.md agent briefings
- aiva_orchestrator.py
- All swarm orchestrators
- All API endpoints
- All webhook handlers
    N)	dataclassfieldasdict)datetime)Path)DictListOptionalAnyCallable)Enum)GeminiRateMaximizerTaskType)GeminiExecutorc                   (    e Zd ZdZdZdZdZdZdZdZ	y)	ExecutionModezExecution modes available.singlerwl_seq	rwl_swarmhybridalpha_evolve
agent_teamN)
__name__
__module____qualname____doc__SINGLERWL_SEQUENTIAL	RWL_SWARMHYBRIDALPHA_EVOLVE
AGENT_TEAM     5/mnt/e/genesis-system/core/genesis_execution_layer.pyr   r   2   s#    $FNIF!LJr$   r   c                       e Zd ZU dZeed<   eed<   eed<   eeeef      ed<   dZe	ed<   dZ
eed	<   d
Ze	ed<   dZe	ed<   dZee   ed<   dZee   ed<   dZee   ed<   y)StoryzRWL Story format.idtitledescriptionacceptance_criteria   priorityFpassesr   
iterations   max_iterationsNresulterrorcompleted_at)r   r   r   r   str__annotations__r	   r   r-   intr.   boolr/   r1   r2   r
   r3   r4   r#   r$   r%   r'   r'   <   sz    GJd38n--HcFDJNC FHSM E8C="&L(3-&r$   r'   c                       e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   eed<    e	e
	      Zeeeef      ed
<    e	e
	      Zee   ed<   y)ExecutionResultzResult from execution layer.successmodestories_completedstories_failedtotal_tokens
total_costelapsed_seconds)default_factoryresultserrorsN)r   r   r   r   r8   r6   r   r7   floatr   listrC   r	   r   r5   r   rD   r#   r$   r%   r:   r:   L   s]    &M
$)$$?GT$sCx.!?d3FDI3r$   r:   c                       e Zd ZdZdZdZdZdZ fdZd Z	d Z
	 	 d#d	ed
ededeeeef      fdZd$defdZ	 	 	 d%d	ed
edededeeeef      f
dZ	 	 d&dee   dededeeeef      fdZd'dedefdZd(dededee   fdZdedefdZdedefdZ	 d'dee   dedefdZ	 	 d)deded edefd!Zdeeef   fd"Z xZS )*GenesisExecutionLayerz
    The HARDWIRED execution layer for ALL Genesis operations.

    Every task MUST flow through here to ensure:
    - RWL pattern is always used
    - Rate limits are maximized
    - Swarm execution when beneficial
    Nr,   
   r0   c                 ~    | j                   %t        | 	  |       | _         d| j                   _        | j                   S )NF)	_instancesuper__new___initialized)cls	__class__s    r%   rM   zGenesisExecutionLayer.__new__l   s3    == !GOC0CM).CMM&}}r$   c                    | j                   ry t               | _        t        d      | _        d | _        d| _        d | _        d | _        d | _	        d| _
        d| _        d| _        d| _        d| _        t        t               j"                  j"                  dz  dz  | _        | j$                  j"                  j'                  dd       d| _         y )NT)use_rate_maximizerr   g        datazexecution_layer.jsonl)parentsexist_ok)rN   r   rate_maximizerr   executor_qwen_client_qwen_enabled
_kimi_fast_kimi_standard	_kimi_max_kimi_enabledactive_swarm_sizetotal_executionsr?   r@   r   __file__parentlog_pathmkdir)selfs    r%   __init__zGenesisExecutionLayer.__init__r   s    13&$? !! "! "# ! X--44v=@WW""4$"? r$   c                     | j                   	 ddlm}  |       | _         | j                   S | j                   S # t        $ r
 d| _        Y yw xY w)zLazy load Qwen client.Nr   )UnifiedQwenClientF)rX   	core.qwenrg   ImportErrorrY   )rd   rg   s     r%   _get_qwen_clientz&GenesisExecutionLayer._get_qwen_client   sV    $7$5$7!    t     %*"s   8 A
Apromptsystem_prompt
max_tokensreturnc                 L  K   | j                         }|r| j                  sy	 |j                  |||       d{   }|j                  |j                  |j
                  |j                  ddS 7 4# t        $ r'}| j                  ddt        |      i       Y d}~yd}~ww xY ww)a$  
        Execute a task using Qwen/AIVA Ollama.

        Use for:
        - Very long context tasks (>50k tokens)
        - AIVA validation requests
        - Memory consolidation tasks

        Returns:
            Dict with response, tokens, and timing, or None if Qwen unavailable
        N)rk   rl   rm   qwen)texttokens_usedexecution_timemodelbackend
qwen_errorr3   )
rj   rY   generaterq   rr   rs   rt   	Exception_logr5   )rd   rk   rl   rm   clientresponsees          r%   execute_with_qwenz'GenesisExecutionLayer.execute_with_qwen   s     " &&(T//	#__+% -  H !'33"*"9"9!!   	IIlWc!f$56	s>    B$A1 A/3A1 .B$/A1 1	B!:BB$B!!B$tierc                     d| }t        | |d      	 ddlm} t        | | ||             t        | |d      S # t        $ r
 d| _        Y yw xY w)z,Lazy load Kimi K2.5 client for a given tier._kimi_Nr   )	KimiSwarm)rt   F)getattrcore.kimi_swarmr   setattrri   r]   )rd   r~   attrr   s       r%   _get_kimi_clientz&GenesisExecutionLayer._get_kimi_client   sb    v4t$,5dID$9: tT4((  %*"s   ; AAc                   K   | j                   sy| j                  |      }|sy|j                         s| j                  dd|i       y	 |j	                  |||       d{   }|j
                  s | j                  d|j                  |d       y|j                  |j                  |j                  |j                  |j                  d|dS 7 l# t        $ r(}| j                  dt        |      |d       Y d}~yd}~ww xY ww)	u  
        Execute a task using Kimi K2.5 (Moonshot AI).

        Routing guide:
        - tier="fast"     (8K)  — short tasks, rapid Q&A, classification
        - tier="standard" (32K) — typical Genesis stories, code gen, research
        - tier="max"      (128K)— very long documents, multi-file analysis, deep research

        Native PARL: 100 sub-agents per call, 1500 tool calls.

        Returns:
            Dict with response, tokens, cost, and timing, or None if Kimi unavailable
        Nkimi_not_configuredr~   )rk   systemrm   
kimi_errorr3   r~   kimi)rq   rr   cost_estimaters   rt   ru   r~   )r]   r   _is_configuredry   execute_asyncr;   r3   rq   r?   r   rs   rt   rx   r5   )rd   rk   rl   rm   r~   rz   r{   r|   s           r%   execute_with_kimiz'GenesisExecutionLayer.execute_with_kimi   s    ( !!&&t,$$&II+fd^<	#11$% 2  H
 ##		,(..$(OP !'44!)!7!7"*"9"9!! $  	IIlc!fd$CD	sN   AD
C !C"/C D;C DC 	DC<7D<DDtasksmax_workersc           
      ^  K   | j                   sy| j                  |      }|sy|j                         s| j                  dd|i       y	 |j	                  ||       d{   }|j
                  |j                  D cg c]1  }|j                  |j                  |j                  |j                  d3 c}|j                  |j                  |j                  |j                  |j                  d|d	S 7 c c}w # t        $ r(}| j                  dt!        |      |d	       Y d}~yd}~ww xY ww)
u  
        Execute a parallel Kimi swarm — up to 100 concurrent agents.

        Args:
            tasks: List of {"prompt": ..., "system": ..., "max_tokens": ...}
            tier: "fast" | "standard" | "max"
            max_workers: Parallel threads (Kimi supports 100 native PARL)

        Returns:
            Dict with texts list, aggregate tokens/cost, or None on failure
        Nkimi_swarm_not_configuredr~   )r   r   )rq   tokenscostr;   
kimi_swarm)	texts	responsesr?   r@   rA   success_countfailure_countru   r~   kimi_swarm_errorr   )r]   r   r   ry   swarm_execute_asyncr   r   rq   r?   r   r;   r@   rA   r   r   rx   r5   )rd   r   r~   r   rz   r2   rr|   s           r%   execute_kimi_swarmz(GenesisExecutionLayer.execute_kimi_swarm  s     " !!&&t,$$&II1FD>B	!55E{5[[F $-- VVq~~qcdclclm !' 3 3$//#)#9#9!'!5!5!'!5!5'  \  	II(CFD*IJ	sT   AD-
C9  C2!C9 >6C44=C9 1D-2C9 4C9 9	D*D% D-%D**D-eventrS   c                     t        j                         j                         ||xs i d}t        | j                  d      5 }|j                  t        j                  |      dz          ddd       y# 1 sw Y   yxY w)zLog execution events.)	timestampr   rS   a
N)r   now	isoformatopenrb   writejsondumps)rd   r   rS   entryfs        r%   ry   zGenesisExecutionLayer._log2  sh     "113JB

 $--% 	.GGDJJu%,-	. 	. 	.s   (A22A;taskcontextc                    d| d| d}| j                   j                  |dd      }|j                  st        d|dd	 |d
ddgd      gS 	 |j                  }d|v r'|j                  d      d   j                  d      d   }n*d|v r&|j                  d      d   j                  d      d   }t        j                  |j                               }t        |      D cg c]l  \  }}t        |j                  dd|dz   d      |j                  dd      |j                  dd      |j                  dg       |j                  dd            n c}}S c c}}w # t        j                  $ r t        d|dd	 |d
ddgd      gcY S w xY w)z
        Decompose a task into RWL stories using Gemini.

        This is the KEY pattern - every complex task becomes stories.
        zOYou are a Genesis RWL (Ralph Wiggum Loop) task decomposer.

TASK TO DECOMPOSE:
z

CONTEXT:
aT  

Decompose this into discrete, verifiable stories. Each story must:
1. Be completable in a single focused session
2. Have clear acceptance criteria
3. Be independently verifiable

Return JSON array of stories:
```json
[
  {
    "id": "STORY-001",
    "title": "Short descriptive title",
    "description": "Detailed description of what to implement",
    "acceptance_criteria": [
      {"description": "Criterion 1", "verification": "How to verify"},
      {"description": "Criterion 2", "verification": "How to verify"}
    ],
    "priority": 1
  }
]
```

Return ONLY valid JSON, no other text.architecture   rk   	task_typerm   z	STORY-001Nd   zTask completedmanual)r*   verification   r(   r)   r*   r+   r-   z```jsonz```r   r(   STORY-03dr)    r*   r+   r-   r,   )rW   execute_optimizedr;   r'   r{   splitr   loadsstrip	enumerategetJSONDecodeError)	rd   r   r   decomposition_promptr2   r{   stories_dataiss	            r%   decompose_to_storiesz*GenesisExecutionLayer.decompose_to_stories<  s   $   		 
' *> 00'$ 1 
 ~~4Cj 5EW_%`$a  	HH$#>>)4Q7==eDQG("#>>%0399%@C::hnn&67L &l3	 Aq uuTVAaC9#56%%, !mR 8().CR(HUU:q1	 	 	 ## 	4Cj 5EW_%`$a  	s&   BE A1EE E +FFstoryc           
         |xj                   dz  c_         dj                  |j                  D cg c]  }d|j                  d|        c}      }d|j                   d|j
                   d| d}| j                  j                  |d	d
      }|j                  rNd|j                  v r@d|_
        |j                  |_        t        j                         j                         |_        n&|j                   xs d|_        |j                  |_        | xj"                  |j$                  z  c_        | xj&                  |j(                  z  c_        |S c c}w )zy
        Execute a single RWL story with self-verification.

        This is the ATOMIC unit of work in Genesis.
        r   r   z- r*   z)# GENESIS RWL STORY EXECUTION

## Story: z

## Description
z

## Acceptance Criteria
a  

## Instructions
1. Implement the solution completely
2. Verify against EACH acceptance criterion
3. Report PASS or FAIL for each criterion
4. If ALL pass, include: TASK_COMPLETE
5. If ANY fail, explain what needs fixing

## Response Format
```
CRITERION 1: [PASS/FAIL] - [explanation]
CRITERION 2: [PASS/FAIL] - [explanation]
...

IMPLEMENTATION:
[Your implementation details]

STATUS: [TASK_COMPLETE or TASK_INCOMPLETE]
```

Execute now.code_generation    r   TASK_COMPLETETzDid not complete all criteria)r/   joinr+   r   r)   r*   rW   r   r;   r{   r.   r2   r   r   r   r4   r3   r?   rr   r@   r   )rd   r   ccriteria_textexecution_promptr2   s         r%   execute_storyz#GenesisExecutionLayer.execute_story  sA    	A		..#
 }a()*#
 
 ;;-      > 00#' 1 
 >>o@EL!??EL!)!9!9!;E ,,I*IEK!??EL 	V///6///m#
s   Ec                 |   K   t        j                         j                  d| j                  |       d{   S 7 w)z"Async wrapper for story execution.N)asyncioget_event_looprun_in_executorr   )rd   r   s     r%   execute_story_asyncz)GenesisExecutionLayer.execute_story_async  s9     ++-==$$$e
 
 	
 
s   3<:<storiesmax_parallelc                 p  K   t        j                          }|xs | j                  }| j                  j                         \  }}| j                  j	                  | j                  j                               }|j                  dd      }t        ||dz  t        |            }t        d|      }| j                  dt        |      ||d       g }	g }
g }t        |d       }t        d	t        |      |      D ]  }||||z    }|D cg c]  }| j                  |       }}t        j                  |  d
{   }|D ]  }|j                   r|	j#                  |       ns|j$                  |j&                  k  rI| j                  |       d
{   }|j                   r|	j#                  |       n#|
j#                  |       n|
j#                  |       |j#                  t)        |               t        j                          |z
  }| j                  dt        |	      t        |
      |d       t+        t        |
      d	k(  t,        j.                  t        |	      t        |
      | j0                  | j2                  |||
D cg c]  }|j4                  s|j4                   c}	      S c c}w 7 v7 $c c}w w)z
        Execute stories as a SWARM with parallel Gemini agents.

        This is the PREFERRED execution mode for multi-story work.
        rpm_availabler   rI   r   swarm_start)r   
swarm_sizecapacityc                     | j                   S N)r-   )r   s    r%   <lambda>z9GenesisExecutionLayer.execute_rwl_swarm.<locals>.<lambda>  s
    

 r$   )keyr   Nswarm_complete)	completedfailedelapsed	r;   r<   r=   r>   r?   r@   rA   rC   rD   )timeDEFAULT_SWARM_SIZErV   can_execute_researchget_available_capacityget_best_modelr   minlenmaxry   sortedranger   r   gatherr.   appendr/   r1   r   r:   r   r   r?   r@   r3   )rd   r   r   
start_timecan_researchreasonr   available_rpmeffective_swarm_sizer   r   rC   r   batchr   r   batch_resultsretry_resultr   r   s                       r%   execute_rwl_swarmz'GenesisExecutionLayer.execute_rwl_swarm  sx     YY[
#>t'>'>  $22GGIf&&==..0

 !_c:"<"1Dc'lS"1&:;		-7|. "
 	 	 &:; q#g,(<= 	.AAa"667E CHHT--e4HEH")..%"88M& .<<$$U+%%(<(<<)-)A)A%)H#HL#**!((6l3MM%(ve}-.	., ))+
*		"Y&k%
 	 K1$((!)nv;**#%+7qwwAGG7

 
	
5 I8 $I6 8sJ   DJ6J&J65J+6AJ6
J.C3J6>J1J1J6.J61J6
force_modec                 h  K   t        j                          }| xj                  dz  c_        | j                  dd|dd i       |r|}nj| j                  ||      }t	        |      | j
                  k\  rt        j                  }n/t	        |      dkD  rt        j                  }nt        j                  }|t        j                  k(  r| j                  j                  |dd      }t        |j                  ||j                  rdnd	|j                  rd	nd|j                  |j                  t        j                          |z
  d
|j                   ig|j"                  r|j"                  g	      S g 	      S |t        j                  k(  r| j                  ||      }g }g }	|D ]B  }
| j%                  |
      }|j&                  r|j)                  |       2|	j)                  |       D t        t	        |	      d	k(  |t	        |      t	        |	      | j*                  | j,                  t        j                          |z
  ||	z   D cg c]  }t/        |       c}|	D cg c]  }|j"                  s|j"                   c}	      S | j                  ||      }| j1                  |       d{   S c c}w c c}w 7 w)aJ  
        THE MAIN ENTRY POINT for all Genesis task execution.

        This method:
        1. Analyzes task complexity
        2. Decomposes into RWL stories if needed
        3. Executes via swarm (preferred) or sequential
        4. Maximizes rate limit utilization throughout

        ALL agents MUST use this method.
        r   
task_startr   N   generalr   r   r   r{   r   )r   r_   ry   r   r   MIN_TASK_COMPLEXITY_FOR_SWARMr   r   r   r   rW   r   r:   r;   rr   r   r{   r3   r   r.   r   r?   r@   r   r   )rd   r   r   r   r   r<   r   r2   r   r   r   executedr   s                r%   execute_taskz"GenesisExecutionLayer.execute_task$  sc    " YY[
"		,ds 45 D //g>G7|tAAA$..W!$33$++ =''']]44# 5 F #'-~~!1$*NNq#//!// $		j 8$foo67)/~
 
 <>
 
 ]111//g>GIF  ,--e4??$$X.MM(+, #Fq("%i."6{!..?? $		j 8,5,>?q?)/;A177;
 
 //g>G//888 @; 9s0   H9J2;J&J2J+%J+3.J2!J0"J2c           	      @   | j                   j                         }i }	 ddlm}  |d       |d       |d      d}| j
                  | j                  | j                  | j                  |j                  |j                  |j                  |d	S # t        $ r ddi}Y `w xY w)
zGet execution layer status.r   )kimi_statusfaststandardr   )r  r  r   r3   zkimi_swarm not importable)initializedr_   r?   r@   rate_limit_utilization
best_modelrecommendations	kimi_k2_5)rV   get_utilization_reportr   r  rx   rN   r_   r?   r@   total_capacity_usedr
  r  )rd   utilizationr  _kimi_statuss       r%   
get_statusz GenesisExecutionLayer.get_statusz  s    ))@@B 	AC$V,(4#E*K  ,, $ 5 5 --//&1&E&E%00*::$	
 		
  	A"$?@K	As   B BB)Nr   )r  )Nr   r  )r  rI   r   r   )r   N) r   r   r   r   rK   r   MAX_SWARM_SIZEr  rM   re   rj   r5   r7   r
   r   r   r}   r   r   r	   r   ry   r'   r   r   r   r:   r   r   r  r  __classcell__)rP   s   @r%   rH   rH   Z   s    I N$%!!:	! "	$$ $ 	$
 
$sCx.	!$L
)S 
) "44 4 	4
 4 
$sCx.	!4r 	.Dz. . 	.
 
$sCx.	!.`.# .T .P Ps PDK Pd>5 >U >@
u 
 
 !N
eN
 N
 
	N
f $(	T9T9 T9 "	T9
 
T9l
DcN 
r$   rH   rn   c                  .    t         
t               a t         S )z+Get the singleton execution layer instance.)_execution_layerrH   r#   r$   r%   get_execution_layerr    s     02r$   r   r   c                 V   K   t               }|j                  | |       d{   S 7 w)z3Execute a task through the Genesis Execution Layer.N)r  r  )r   r   layers      r%   r  r    s'     !E##D'2222s    )')r   c                 d  K   t               }t        |       D cg c]l  \  }}t        |j                  dd|dz   d      |j                  dd      |j                  dd      |j                  dg       |j                  d	d
            n }}}|j	                  |       d{   S c c}}w 7 
w)z Execute stories as an RWL swarm.r(   r   r   r   r)   r   r*   r+   r-   r,   r   N)r  r   r'   r   r   )r   r  r   r   story_objectss        r%   r   r     s     !E g&	 Aq 	uuTVAaC9-.%%$mR0 !&;R @UU:q)	
	M 	 ((777	 8s   B0A1B(B0#B.$B0c                 @    t        j                  t        | |            S )z%Synchronous wrapper for execute_task.)r   runr  )r   r   s     r%   execute_task_syncr    s    ;;|D'233r$   c                     dd l } | j                  d      }|j                  dg d       |j                  dt        d	       |j	                         }t               }|j                  d
k(  r1|j                         }t        t        j                  |d             y |j                  dk(  rit        d       t        d      }t        d|j                          t        d|j                  j                          t        d|j                          y |j                  dk(  r\|j                   st        d       y t        |j                         }t        t        j                  t#        |      dt                     y y )Nr   zGenesis Execution Layer)r*   command)statustestexecute)choicesz--taskzTask to execute)typehelpr!     )indentr"  zTesting execution layer...z*What is 2 + 2? Reply with just the number.z	Success: zMode: zTokens: r#  zError: --task required)r(  default)argparseArgumentParseradd_argumentr5   
parse_argsr  r   r  printr   r   r  r;   r<   valuer?   r   r   )r*  parserargsr  r!  r2   s         r%   mainr2    s5   $$1J$KF
	+HI
s1BCD!E||x!!#djj*+		*+"#OP	&..)*+v{{(()*+,,-./		"yy*+"499-djj3?@ 
#r$   __main__r  ))r   r   r   r   dataclassesr   r   r   r   pathlibr   typingr   r	   r
   r   r   enumr   syspathinsertr5   r`   ra   core.gemini_rate_maximizerr   r   core.gemini_executorr   r   r'   r:   rH   r  r  r  r   r  r2  r   r#   r$   r%   <module>r=     s!  >    0 0   6 6  
 3tH~,,334 5 D /D  ' ' ' 
4 
4 
4y
 y
z  2 3S 33 3 38T$Z 8O 8 4C 4# 4 4A: zF r$   