
    i)u                     (   S r SSKrSSKrSSKrSSKJrJrJr  SSKJr  SSK	J
r
  SSKJrJrJrJrJr  SSKJr  SSKr\R(                  R+                  S\" \
" \5      R0                  R0                  5      5        SSKJrJr  SS	KJr   SS
KJr   Sr! " S S\5      r#\ " S S5      5       r$\ " S S5      5       r% " S S5      r&Sq'S\&4S jr(SS\S\S\%4S jjr)S\\   S\%4S jr*SS\S\S\%4S jjr+S r,\-S:X  a  \," 5         gg! \" a    Sr!Sr  Nf = f) 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)GeminiExecutor)KimiExecutorTFc                   0    \ rS rSrSrSrSrSrSrSr	Sr
S	rg
)ExecutionMode:   zExecution modes available.singlerwl_seq	rwl_swarmhybridalpha_evolve
agent_team N)__name__
__module____qualname____firstlineno____doc__SINGLERWL_SEQUENTIAL	RWL_SWARMHYBRIDALPHA_EVOLVE
AGENT_TEAM__static_attributes__r       1E:\genesis-system\core\genesis_execution_layer.pyr   r   :   s#    $FNIF!LJr(   r   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \\\\4      \S'   Sr	\
\S'   S	r\\S
'   Sr\
\S'   Sr\
\S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S'   Srg)StoryD   zRWL Story format.idtitledescriptionacceptance_criteria   priorityFpassesr   
iterations   max_iterationsNresulterrorcompleted_atr   )r   r   r   r   r    str__annotations__r	   r   r2   intr3   boolr4   r6   r7   r
   r8   r9   r'   r   r(   r)   r+   r+   D   sz    GJd38n--HcFDJNC FHSM E8C="&L(3-&r(   r+   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   \	\S'   \	\S	'   \
" \S
9r\\\\4      \S'   \
" \S
9r\\   \S'   Srg)ExecutionResultT   zResult from execution layer.successmodestories_completedstories_failedtotal_tokens
total_costelapsed_seconds)default_factoryresultserrorsr   N)r   r   r   r   r    r=   r;   r   r<   floatr   listrI   r	   r   r:   r   rJ   r'   r   r(   r)   r?   r?   T   s]    &M
$)$$?GT$sCx.!?d3FDI3r(   r?   c                   N  ^  \ rS rSrSrSrSrSrSrU 4S jr	S r
S	 r  S*S
\S\S\S\\\\4      4S jjrS\S   4S jr   S+S
\S\\   S\S\S\\\\4      4
S jjr    S,S\\   S\S\S\\   S\S\\\\4      4S jjrS-S\4S jjr   S.S
\S\S\S\S\\\\4      4
S jjr  S/S\\   S\S\S\\\\4      4S jjrS0S\S\4S jjrS1S\S\S\\   4S jjrS \S\4S! jrS \S\4S" jr S0S#\\   S$\S\4S% jjr  S2S\S\S&\ S\4S' jjr!S\\\4   4S( jr"S)r#U =r$$ )3GenesisExecutionLayerb   z
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
Nr1   
   r5   c                    > U R                   c%  [        TU ]	  U 5      U l         SU R                   l        U R                   $ )NF)	_instancesuper__new___initialized)cls	__class__s    r)   rT   GenesisExecutionLayer.__new__t   s3    == !GOC0CM).CMM&}}r(   c                    U R                   (       a  g [        5       U l        [        SS9U l        S U l        SU l        S U l        S U l        S U l	        SU l
        S U l        [        U l        SU l        SU l        SU l        SU l        [%        [&        5      R(                  R(                  S-  S-  U l        U R*                  R(                  R-                  SSS9  SU l         g )NT)use_rate_maximizerr   g        datazexecution_layer.jsonl)parentsexist_ok)rU   r   rate_maximizerr   executor_qwen_client_qwen_enabled
_kimi_fast_kimi_standard	_kimi_max_kimi_enabled_kimi_executorKIMI_EXECUTOR_AVAILABLE_kimi_executor_enabledactive_swarm_sizetotal_executionsrE   rF   r   __file__parentlog_pathmkdirselfs    r)   __init__GenesisExecutionLayer.__init__z   s    13&$? !! "! :>&=# "# ! X--44v=@WW""4$"? r(   c                     U R                   c   SSKJn  U" 5       U l         U R                   $ U R                   $ ! [         a
    SU l         gf = f)zLazy load Qwen client.Nr   )UnifiedQwenClientF)r`   	core.qwenrt   ImportErrorra   )rp   rt   s     r)   _get_qwen_client&GenesisExecutionLayer._get_qwen_client   sV    $7$5$7!    t     %*"s   9 AApromptsystem_prompt
max_tokensreturnc                 \  #    U R                  5       nU(       a  U R                  (       d  g UR                  UUUS9I Sh  vN nUR                  UR                  UR
                  UR                  SS.$  N4! [         a'  nU R                  SS[        U5      05         SnAgSnAff = f7f)z
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)ry   rz   r{   qwen)texttokens_usedexecution_timemodelbackend
qwen_errorr8   )
rw   ra   generater   r   r   r   	Exception_logr:   )rp   ry   rz   r{   clientresponsees          r)   execute_with_qwen'GenesisExecutionLayer.execute_with_qwen   s     " &&(T//	#__+% -  H !'33"*"9"9!!   	IIlWc!f$56	s@   *B,A8 A63A8 5B,6A8 8
B)B$B,$B))B,_KimiExecutorc                     U R                   (       d  gU R                  c"  [        (       d  SU l         g[        5       U l        U R                  $ )z1Lazy-load the KimiExecutor (Moonshot direct API).NF)rh   rf   rg   r   ro   s    r)   _get_kimi_executor(GenesisExecutionLayer._get_kimi_executor   sA    **&**.3+"//D"""r(   r   c           	      z  ^^^^^#    U R                  5       mT(       d  gTR                  5       (       d  U R                  SST05        g [        R                  " 5       nUR                  SUUUUU4S j5      I Sh  vN nUR                  (       d   U R                  SUR                  TS.5        gUR                  UR                  UR                  UR                  UR                  SUR                  UR                  S.$  N! [         a(  nU R                  S[!        U5      TS.5         SnAgSnAff = f7f)	u  
Execute a single task via KimiExecutor (Moonshot API direct).

Use this instead of execute_with_kimi when you want to bypass OpenRouter
and call Moonshot's API directly with the provided MOONSHOT_API_KEY.

Model routing:
  model="fast"     → moonshot-v1-8k    ($0.15/1M — short tasks)
  model="standard" → moonshot-v1-32k   ($0.15/1M — typical stories)
  model="max"      → moonshot-v1-128k  ($0.60/1M — long documents)

Returns:
    Dict with response, tokens, cost, timing — or None if unavailable.
Nkimi_executor_not_configuredr   c                  (   > T R                  TTTTS9$ )N)ry   r   rz   r{   )execute)r_   r{   r   ry   rz   s   r)   <lambda>BGenesisExecutionLayer.execute_with_kimi_executor.<locals>.<lambda>   s!    ((!"/)	 ) r(   kimi_executor_errorr8   r   kimi_executor)r   r   cost_estimater   r   r   prompt_tokenscompletion_tokens)r   is_configuredr   asyncioget_event_looprun_in_executorrA   r8   r   r   r   r   r   r   r   r   r:   )	rp   ry   rz   r{   r   loopr7   excr_   s	    ````   @r)   execute_with_kimi_executor0GenesisExecutionLayer.execute_with_kimi_executor   s    * **,%%''II4w6FG	))+D//  F >>		/6<<RW1XY%11!'!5!5"("7"7*!'!5!5%+%=%=	 	,  	II+s3x%-PQ	sO   AD;2D =D>4D 2D;3AD D;D 
D8D3.D;3D88D;tasksmax_workersc           
        ^^^^^^
#    U R                  5       m
T
(       d  gT
R                  5       (       d  U R                  SST05        g [        R                  " 5       nUR                  SU
UUUUU4S j5      I Sh  vN nUR                  UR                   Vs/ s H=  nUR                  UR                  UR                  UR                  UR                  S.PM?     snUR                  UR                  UR                  UR                   UR"                  STS.	$  Ns  snf ! [$         a(  n	U R                  S['        U	5      TS	.5         Sn	A	gSn	A	ff = f7f)
u  
Execute a parallel swarm via KimiExecutor (Moonshot API direct).

Up to 50 concurrent workers. Tasks returned in submission order.

Args:
    tasks:       List of prompt strings.
    model:       "fast" | "standard" | "max"
    max_workers: Concurrency ceiling (max 50).
    system_prompt: Shared system message.
    max_tokens:  Max tokens per task.

Returns:
    Dict with texts list, aggregate tokens/cost — or None on failure.
N"kimi_executor_swarm_not_configuredr   c                  *   > T R                  TTTTTS9$ )N)r   r   rz   r{   r   )execute_tasks_parallel)r_   r{   r   r   rz   r   s   r)   r   CGenesisExecutionLayer.execute_kimi_executor_swarm.<locals>.<lambda>1  s$    77"/) + 8 r(   )r   tokenscostrA   r   kimi_executor_swarm)	texts	responsesrE   rF   rG   success_countfailure_countr   r   kimi_executor_swarm_errorr   )r   r   r   r   r   r   successful_responsesrI   r   r   r   rA   r   rE   rF   rG   r   r   r   r:   )rp   r   r   r   rz   r{   r   swarm_resultrr   r_   s    `````    @r)   execute_kimi_executor_swarm1GenesisExecutionLayer.execute_kimi_executor_swarm  sD    . **,%%''II:We<LM"	))+D!%!5!5 	" 	L &:: *11	 2 !"

"#-- !#$99!" 2	 !- 9 9*55#/#?#?!-!;!;!-!;!;0% 		$  	II1SXPU3VW	sU   AE3D& ?D D& AD!!=D& ED& !D& &
E0EEEEtierc                     SU 3n[        XS5      c   SSKJn  [        XU" US95        [        XS5      $ ! [         a
    SU l         gf = f)z,Lazy load Kimi K2.5 client for a given tier._kimi_Nr   )	KimiSwarm)r   F)getattrcore.kimi_swarmr   setattrrv   re   )rp   r   attrr   s       r)   _get_kimi_client&GenesisExecutionLayer._get_kimi_clientS  s\    v4t$,5ID$9: t4((  %*"s   6 A
	A
c                 0  #    U R                   (       d  gU R                  U5      nU(       d  gUR                  5       (       d  U R                  SSU05        g UR	                  UUUS9I Sh  vN nUR
                  (       d   U R                  SUR                  US.5        gUR                  UR                  UR                  UR                  UR                  SUS.$  Nq! [         a(  nU R                  S[        U5      US.5         SnAgSnAff = f7f)	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   )ry   systemr{   
kimi_errorr8   r   kimi)r   r   r   r   r   r   r   )re   r   _is_configuredr   execute_asyncrA   r8   r   rE   r   r   r   r   r:   )rp   ry   rz   r{   r   r   r   r   s           r)   execute_with_kimi'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.4C! "D#;C! DC! !
D+D	DDDc           
      |  #    U R                   (       d  gU R                  U5      nU(       d  gUR                  5       (       d  U R                  SSU05        g UR	                  XS9I Sh  vN nUR
                  UR                   Vs/ s H2  nUR                  UR                  UR                  UR                  S.PM4     snUR                  UR                  UR                  UR                  UR                  SUS.	$  Ns  snf ! [         a(  nU R                  S[!        U5      US	.5         SnAgSnAff = f7f)
uB  
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   )r   r   r   rA   
kimi_swarm)	r   r   rE   rF   rG   r   r   r   r   kimi_swarm_errorr   )re   r   r   r   swarm_execute_asyncr   r   r   rE   r   rA   rF   rG   r   r   r   r:   )rp   r   r   r   r   r7   r   r   s           r)   execute_kimi_swarm(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<D +D ,D 	9D=D ?D< D D 
D9D4/D<4D99D<eventr[   c                    [         R                  " 5       R                  5       UU=(       d    0 S.n[        U R                  S5       nUR                  [        R                  " U5      S-   5        SSS5        g! , (       d  f       g= f)zLog execution events.)	timestampr   r[   a
N)r   now	isoformatopenrm   writejsondumps)rp   r   r[   entryfs        r)   r   GenesisExecutionLayer._log  s_     "113JB

 $--%GGDJJu%,- &%%s   )A99
Btaskcontextc                    SU SU S3nU R                   R                  USSS9nUR                  (       d  [        SUSS	 US
SS./SS9/$  UR                  nSU;   a'  UR                  S5      S   R                  S5      S   nO,SU;   a&  UR                  S5      S   R                  S5      S   n[        R                  " UR                  5       5      n[        U5       VVs/ s Hi  u  px[        UR                  SSUS-   S 35      UR                  SS5      UR                  SS5      UR                  S/ 5      UR                  SS5      S9PMk     snn$ s  snnf ! [        R                   a    [        SUSS	 US
SS./SS9/s $ f = f)zp
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   ry   	task_typer{   z	STORY-001Nd   zTask completedmanual)r/   verification   r-   r.   r/   r0   r2   z```jsonz```r   r-   STORY-03dr.    r/   r0   r2   r1   )r_   execute_optimizedrA   r+   r   splitr   loadsstrip	enumerategetJSONDecodeError)	rp   r   r   decomposition_promptr7   r   stories_dataiss	            r)   decompose_to_stories*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	 4DA uuTVAaC9#56%%, !mR 8().CR(HUU:q1 4	 	 	 ## 	4Cj 5EW_%`$a  	s&   BE #A0EE E )F	F	storyc           
         U=R                   S-  sl         SR                  UR                   Vs/ s H  nSUR                  SU5       3PM     sn5      nSUR                   SUR
                   SU S3nU R                  R                  US	S
S9nUR                  (       aQ  SUR                  ;   aA  SUl
        UR                  Ul        [        R                  " 5       R                  5       Ul        O+UR                   =(       d    SUl        UR                  Ul        U =R"                  UR$                  -  sl        U =R&                  UR(                  -  sl        U$ s  snf )za
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)r4   joinr0   r   r.   r/   r_   r   rA   r   r3   r7   r   r   r   r9   r8   rE   r   rF   r   )rp   r   ccriteria_textexecution_promptr7   s         r)   execute_story#GenesisExecutionLayer.execute_story!  sF    	A		..#
. }a()*.#
 
 ;;-      > 00#' 1 
 >>o@EL!??EL!)!9!9!;E ,,I*IEK!??EL 	V///6///m#
s   Ec                 ~   #    [         R                  " 5       R                  SU R                  U5      I Sh  vN $  N7f)z"Async wrapper for story execution.N)r   r   r   r  )rp   r   s     r)   execute_story_async)GenesisExecutionLayer.execute_story_asynca  s8     ++-==$$$e
 
 	
 
s   4=;=storiesmax_parallelc                   #    [         R                   " 5       nU=(       d    U R                  nU R                  R                  5       u  pEU R                  R	                  U R                  R                  5       5      nUR                  SS5      n[        X'S-  [        U5      5      n[        SU5      nU R                  S[        U5      UUS.5        / n	/ n
/ n[        US S9n[        S	[        U5      U5       GH  nXX-    nU Vs/ s H  oR                  U5      PM     nn[        R                  " U6 I S
h  vN nU H  nUR                   (       a  U	R#                  U5        OyUR$                  UR&                  :  aN  U R                  U5      I S
h  vN nUR                   (       a  U	R#                  U5        O#U
R#                  U5        OU
R#                  U5        UR#                  [)        U5      5        M     GM     [         R                   " 5       U-
  nU R                  S[        U	5      [        U
5      US.5        [+        [        U
5      S	:H  [,        R.                  [        U	5      [        U
5      U R0                  U R2                  UUU
 Vs/ s H"  nUR4                  (       d  M  UR4                  PM$     snS9	$ s  snf  GN GN5s  snf 7f)zu
Execute stories as a SWARM with parallel Gemini agents.

This is the PREFERRED execution mode for multi-story work.
rpm_availabler   rP   r   swarm_start)r  
swarm_sizecapacityc                     U R                   $ N)r2   )r   s    r)   r   9GenesisExecutionLayer.execute_rwl_swarm.<locals>.<lambda>  s    

r(   )keyr   Nswarm_complete)	completedfailedelapsed	rA   rB   rC   rD   rE   rF   rG   rI   rJ   )timeDEFAULT_SWARM_SIZEr^   can_execute_researchget_available_capacityget_best_modelr   minlenmaxr   sortedranger
  r   gatherr3   appendr4   r6   r   r?   r   r#   rE   rF   r8   )rp   r  r  
start_timecan_researchreasonr  available_rpmeffective_swarm_sizer  r  rI   r   batchr   r   batch_resultsretry_resultr  r   s                       r)   execute_rwl_swarm'GenesisExecutionLayer.execute_rwl_swarmg  sh     YY[
#>t'>'>  $22GGI&&==..0

 !_c:"<"1Dc'lS"1&:;		-7|. "
 	 	 &:; q#g,(<=Aa67E CHH%--e4%EH")..%"88M&<<$$U+%%(<(<<)-)A)A%)H#HL#**!((6l3MM%(ve}- ' >, ))+
*		"Y&k%
 	 K1$((!)nv;**#%+7VqwwGAGGV7

 
	
5 I8 $I6 8sJ   DKK  K8K9AKKC>KK+K;KKK
force_modec                   #    [         R                   " 5       nU =R                  S-  sl        U R                  SSUSS 05        U(       a  UnOkU R                  X5      n[	        U5      U R
                  :  a  [        R                  nO0[	        U5      S:  a  [        R                  nO[        R                  nU[        R                  :X  a  U R                  R                  USSS9n[        UR                  UUR                  (       a  SOS	UR                  (       a  S	OSUR                  UR                  [         R                   " 5       U-
  S
UR                   0/UR"                  (       a  UR"                  /S9	$ / S9	$ U[        R                  :X  Ga  U R                  X5      n/ n/ n	U HI  n
U R%                  U
5      nUR&                  (       a  UR)                  U5        M8  U	R)                  U5        MK     [        [	        U	5      S	:H  U[	        U5      [	        U	5      U R*                  U R,                  [         R                   " 5       U-
  X-    Vs/ s H  n[/        U5      PM     snU	 Vs/ s H!  oR"                  (       d  M  UR"                  PM#     snS9	$ U R                  X5      nU R1                  U5      I Sh  vN $ s  snf s  snf  N7f)a
  
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  rj   r   r   r"  MIN_TASK_COMPLEXITY_FOR_SWARMr   r#   r"   r!   r_   r   r?   rA   r   r   r   r8   r  r3   r'  rE   rF   r   r0  )rp   r   r   r2  r(  rB   r  r7   r  r  r   executedr   s                r)   execute_task"GenesisExecutionLayer.execute_task  s_    " YY[
"		,ds 45 D //>G7|tAAA$..W!$33$++ =''']]44# 5 F #'-~~!1$*NNq#//!// $		j 8$foo67)/~
 
 <>
 
 ]111//>GIF --e4??$$X.MM(+ ! #Fq("%i."6{!..?? $		j 8,5,>?,>q,>?)/;A77;
 
 //>G//888 @; 9s0   IKK(K.K
K
*K KKc                 F   U R                   R                  5       n0 n SSKJn  U" S5      U" S5      U" S5      S.n0 n U R                  5       nU(       a  UR                  5       nOS	S
S.n U R                  U R                  U R                  U R                  UR                  UR                  UR                  UUSU(       a  SU;  a  SOSUR                  S5      (       a  SOSS.S.
$ ! [         a    SS0n Nf = f! [         a  nS[        U5      0n SnANSnAff = f)zGet execution layer status.r   )kimi_statusfaststandardr#  )r=  r>  r#  r8   zkimi_swarm not importableFznot configured or import failed)	availabler*  Nactive
configurednot_configured)geminikimi_swarm_openrouterkimi_executor_moonshot)
initializedrj   rE   rF   rate_limit_utilization
best_modelrecommendations	kimi_k2_5r   backends)r^   get_utilization_reportr   r<  r   r   
get_statusr:   rU   rj   rE   rF   total_capacity_usedrH  rI  r   )rp   utilizationr<  _kimi_statuskimi_executor_statuskexr   s          r)   rM   GenesisExecutionLayer.get_status  s7   ))@@B 	AC$V,(4#E*K  "	7))+C'*~~'7$5:Fg'h$
  ,, $ 5 5 --//&1&E&E%00*::$1"5@WT_E_el6J6N6N|6\6\(br
 	
  	A"$?@K	A  	7$+SX#6 	7s.   C+ 'C? 'C? +C<;C<?
D 	DD )rU   re   rf   rh   rb   rd   rc   r`   ra   ri   r_   rm   r^   rF   rj   rE   )Nr   )Nr  r>  )r>  rP   Nr  )r>  )Nr   r>  )r>  rP   r  r   )r   N)%r   r   r   r   r    rR   r  MAX_SWARM_SIZEr7  rT   rq   rw   r:   r<   r
   r   r   r   r   r   r	   r   r   r   r   r   r+   r   r  r
  r?   r0  r   r9  rM  r'   __classcell__)rW   s   @r)   rN   rN   b   s    I N$%!!B	! "	$$ $ 	$
 
$sCx.	!$P	#H_$= 	# (,55  }5 	5
 5 
$sCx.	!5t  '+?Cy? ? 	?
  }? ? 
$sCx.	!?F
)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(   rN   r|   c                  0    [         c
  [        5       q [         $ )z+Get the singleton execution layer instance.)_execution_layerrN   r   r(   r)   get_execution_layerrY  >  s     02r(   r   r   c                 T   #    [        5       nUR                  X5      I Sh  vN $  N7f)z3Execute a task through the Genesis Execution Layer.N)rY  r9  )r   r   layers      r)   r9  r9  G  s$     !E##D2222s   (&(r  c                 b  #    [        5       n[        U 5       VVs/ s Hi  u  p#[        UR                  SSUS-   S 35      UR                  SS5      UR                  SS5      UR                  S/ 5      UR                  S	S
5      S9PMk     nnnUR	                  U5      I Sh  vN $ s  snnf  N
7f)z Execute stories as an RWL swarm.r-   r   r   r   r.   r   r/   r0   r2   r1   r   N)rY  r   r+   r   r0  )r  r[  r   r   story_objectss        r)   r0  r0  M  s     !E g&	 'DA 	uuTVAaC9-.%%$mR0 !&;R @UU:q)	
 '  	 ((777	 8s   B/A0B'B/"B-#B/c                 @    [         R                  " [        X5      5      $ )z%Synchronous wrapper for execute_task.)r   runr9  )r   r   s     r)   execute_task_syncr`  ]  s    ;;|D233r(   c                     SS K n U R                  SS9nUR                  S/ SQS9  UR                  S[        SS	9  UR	                  5       n[        5       nUR                  S
:X  a/  UR                  5       n[        [        R                  " USS95        g UR                  S:X  ai  [        S5        [        S5      n[        SUR                   35        [        SUR                  R                   35        [        SUR                   35        g UR                  S:X  a_  UR                   (       d  [        S5        g [        UR                   5      n[        [        R                  " [#        U5      S[        S95        g g )Nr   zGenesis Execution Layer)r/   command)statustestr   )choicesz--taskzTask to execute)typehelprc     )indentrd  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)ri  default)argparseArgumentParseradd_argumentr:   
parse_argsrY  rb  rM  printr   r   r`  rA   rB   valuerE   r   r   )rk  parserargsr[  rc  r7   s         r)   mainrs  c  s5   $$1J$KF
	+HI
s1BCD!E||x!!#djj*+		*+"#OP	&..)*+v{{(()*+,,-./		"yy*+"499-djj3?@ 
#r(   __main__rT  ).r    r   r   r  dataclassesr   r   r   r   pathlibr   typingr   r	   r
   r   r   enumr   syspathinsertr:   rk   rl   core.gemini_rate_maximizerr   r   core.gemini_executorr   core.kimi_executorr   r   rg   rv   r   r+   r?   rN   rX  rY  r9  r0  r`  rs  r   r   r(   r)   <module>r     sL  >    0 0   6 6  
 3tH~,,334 5 D /@"D  ' ' ' 
4 
4 
4U
 U
r  2 3S 33 3 38T$Z 8O 8 4C 4# 4 4A: zF W  #Ms   D 
DD