
    i                     b   d Z ddlZddl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mZmZmZ ddlmZmZ  e	d      Zej&                  j)                  d ee             ddlmZ  ej0                  d	      Ze G d
 d             Ze G d d             Z G d d      Zedk(  r e       Zyy)ag  
Antigravity Workflow Engine
===========================
The core execution engine for autonomous genesis system development.
Synthesizes high-level goals into executable steps.

Architecture:
- Plan: Synthesized by Gemini (Pro/Flash)
- Execute: Step-by-step implementation with verification
- Memory: Recursive integration of results into the knowledge base
    N)datetimetimezone)Path)AnyDictListOptional)	dataclassasdictz/mnt/e/genesis-system)GeminiExecutorzcore.antigravity_workflowc                   b    e Zd ZU eed<   eed<   eed<   dZeed<   dZee   ed<   dZee   ed<   y)	WorkflowStepiddescriptionactionpendingstatusNresulterror)	__name__
__module____qualname__str__annotations__r   r   r	   r        2/mnt/e/genesis-system/core/antigravity_workflow.pyr   r       s8    GKFC FHSM E8C=r   r   c                   J    e Zd ZU eed<   eed<   ee   ed<   eed<   dZeed<   y)WorkflowPlanplan_idgoalsteps
created_atplannedr   N)r   r   r   r   r   r   r   r   r   r   r   r   r   )   s&    L
IOFCr   r   c                   <    e Zd ZdZd Zd
dededefdZdedefd	Z	y)AntigravityWorkflowz<
    Orchestrates goal-to-execution cycles for Genesis.
    c                     t        d      | _        t        dz  dz  dz  | _        t        dz  dz  dz  | _        | j                  j                  dd       | j                  j                  dd       y )NT)use_rate_maximizerdatazantigravity-syncplans
executions)parentsexist_ok)r   executorGENESIS_ROOT	plans_direxec_dirmkdir)selfs    r   __init__zAntigravityWorkflow.__init__6   sj    &$?%.1CCgM$v-0BB\QTD9D48r   Nr!   contextreturnc                    d| dt        j                  |xs i        d}| j                  j                  |d      }|j                  st        d|j                         	 |j                  j                         }d|v r5|j                  d      d   j                  d	      d
   j                         }n8d	|v r4|j                  d	      d   j                  d	      d
   j                         }t        j                  |      }|D cg c]  }t        di | }}dt        t        j                                }	t        |	||t        j                   t"        j$                        j'                               }
|
S c c}w # t(        $ rM}t*        j                  d|        t*        j                  d|j                          t        d|       d}~ww xY w)zH
        Synthesize a goal into a multi-step plan using Gemini.
        z# GOAL: z
# CONTEXT: az  

Act as the Genesis System Architect. Break this goal down into exact, executable technical steps.
Return ONLY a JSON list of steps. Each step MUST have:
- "id": unique string ID
- "description": clear explanation of what to do
- "action": the specific technical implementation instruction

Response Format:
[
  {"id": "step_1", "description": "...", "action": "..."},
  ...
]
architecture	task_typezFailed to create plan: z```json   z```r   plan_)r    r!   r"   r#   zError parsing plan: zRaw response: zPlan synthesis failed: Nr   )jsondumpsr.   execute_optimizedsuccessRuntimeErrorr   responsestripsplitloadsr   inttimer   r   nowr   utc	isoformat	Exceptionlogger)r3   r!   r5   promptrB   cleaned_resp
steps_datasr"   r    planes               r   create_planzAntigravityWorkflow.create_plan=   s    dV $JJw}"%& ' ==226^2T!88HIJJ	>#,,224LL(+11)<Q?EEeLQOUUW,&+11%8;AA%HKQQSL1J0:;1\&A&;E;c$))+./0G#<<5??A	D K <  	>LL/s34LL>(*;*;)<=>!8<==	>s-   #B$F  E;A!F  ;F   	G	AGG	plan_datac                    |j                  d      }|j                  dg       }g }t        d|        |D ]  }t        di |}t        d|j                   d|j                          d|j                  d       d|j                   d	|j
                   d
}| j                  j                  |d      }|j                  rd|_	        |j                  |_        nd|_	        |j                  |_        |j                  t        |             |j                  dk(  st        d|j                   d|j                           n |t        d |D              rd|dS d|dS )z-
        Execute a synthesized plan.
        r    r"   z[Workflow] Executing plan: z	  > Step z: z	## GOAL: r!   z
## STEP DESCRIPTION: z
## ACTION: z

Implement this step. If it involves code, provide the code. 
Verify the implementation.
If complete, end with TASK_COMPLETE.
code_generationr9   	completedfailedz	  X Step z	 FAILED: c              3   ,   K   | ]  }|d    dk(    yw)r   rW   Nr   ).0rs     r   	<genexpr>z3AntigravityWorkflow.execute_plan.<locals>.<genexpr>   s     (U8)C(Us   partially_failed)r    r   resultsr   )getprintr   r   r   r   r.   r?   r@   r   rB   r   r   appendr   all)	r3   rT   r    r"   r^   	step_datastepexec_promptresps	            r   execute_planz AntigravityWorkflow.execute_planm   sf    --	*gr*+G956 	I,),DIdggYb)9)9(:;< (	f(='> ?&&' (KK= K ==22;J[2\D||)"mm&!ZZ
NN6$<({{h&	$'')DJJ<@A5	: %((UW(U%Uk
 	
[m
 	
r   )N)
r   r   r   __doc__r4   r   dictr   rS   rg   r   r   r   r&   r&   1   s:    9.> .>d .>l .>`*
d *
t *
r   r&   __main__)rh   r=   loggingossysrG   r   r   pathlibr   typingr   r   r   r	   dataclassesr
   r   r/   pathinsertr   core.gemini_executorr   	getLoggerrL   r   r   r&   r   workflowr   r   r   <module>rv      s   
   	 
  '  , , ) +, 3|$ % /			6	7
        f
 f
P z"$H r   