
    Śi                         S SK r S SKrS SKrS SKJrJrJr  S SKr\R                  " \	5      r
 " S S5      r\	S:X  aB  \R                  " \R                  S9  \" 5       r\" S5        \R!                  5       r\" \5        gg)    N)ListDictOptionalc                       \ rS rSrSrSS\S\4S jjrSS\\   S\\   S	\	R                  4S
 jjrSS\S\\   S	\4S jjrS	\4S jrSS\S\S	\4S jjrSrg)JulesBridge	   z
Programmatic bridge to interact with the @google/jules CLI.
Allows Genesis to autonomously spin up Deep Think sessions and pull insights.
repo_dir	repo_namec                 ^    Xl         X l        [        R                  S:X  a  SOSnUS/U l        g )Nntznpx.cmdnpxz@google/jules@latest)r	   r
   osnamebase_cmd)selfr	   r
   npx_execs       1E:\genesis-system\genesis_v2\core\jules_bridge.py__init__JulesBridge.__init__   s*     " "49U!#9:    Nargs
input_datareturnc           
      $   U R                   U-   n[        R                  SSR                  U5       35         [        R
                  " UU R                  U(       a  UOSSSSSS9nU$ ! [         a  n[        R                  SU 35        e SnAff = f)	z4Helper to run Jules CLI commands and capture output.zRunning Jules command:  NTutf-8F)cwdinputcapture_outputtextencodingcheckz!Failed to execute Jules command: )	r   loggerinfojoin
subprocessrunr	   	Exceptionerror)r   r   r   cmdresultes         r   _run_commandJulesBridge._run_command   s    mmd"-chhsm_=>	  ^^MM$.jD# F M 	LL<QC@A	s   .A' '
B1B

Bpromptcontext_filec                 4   [         R                  S5        UnU(       ai  [        R                  R	                  U5      (       aE  [         R                  SU 35        [        USSS9 nUR                  5       nSSS5        SW SU S	3nS
SU R                  /nU R                  XcS9nUR                  S:w  a0  [         R                  SUR                   SUR                   35        g[         R                  SUR                   35        g! , (       d  f       N= f)z
Spins up a new Jules session.
If a context_file is provided, its contents are prepended to the input stream 
to ensure Jules has absolute zero-hallucination context before acting.
z'Creating new Jules Strategic Session...zInjecting context file: rr   )r!   Nz
<CONTEXT>
z
</CONTEXT>

<TASK>
z
</TASK>newz--repo)r   r   z"Failed to create session:
STDOUT:
z	
STDERR:
FzSession creation output:
T)r#   r$   r   pathexistsopenreadr
   r-   
returncoder)   stdoutstderr)r   r/   r0   final_inputfcontext_datar   r+   s           r   create_sessionJulesBridge.create_session+   s     	=>BGGNN<88KK2<.ABlC':a vvx ;'~5MfXU^_Kx0""4"@!LL?kZ`ZgZgYhij0@A ;:s   &D		
Dc                     / SQnU R                  U5      nUR                  S:w  a#  [        R                  SUR                   35        gUR
                  $ )z
Returns the raw output of `jules remote list --session`.
Parsing the table output is tricky without JSON output, so returning string for now.
)remotelist	--sessionr   zFailed to list sessions:
 )r-   r8   r#   r)   r:   r9   )r   r   r+   s      r   list_sessionsJulesBridge.list_sessionsD   sL    
 /""4(!LL5fmm_EF}}r   
session_idapply_patchc                     SSS[        U5      /nU(       a  UR                  S5        U R                  U5      nUR                  S:w  a&  [        R                  SU SUR                   35        gUR                  $ )	z[
Pulls the output of a given session.
Can optionally apply the patch directly to the repo.
rA   pullrC   z--applyr   zFailed to pull session z:
rD   )strappendr-   r8   r#   r)   r:   r9   )r   rG   rH   r   r+   s        r   pull_sessionJulesBridge.pull_sessionP   sn    
 &+s:?KK	"""4(!LL2:,c&--QR}}r   )r   r	   r
   )ze:/genesis-systemzKinan27/genesis-system)N)F)__name__
__module____qualname____firstlineno____doc__rK   r   r   r   r&   CompletedProcessr-   boolr>   rE   rM   __static_attributes__ r   r   r   r   	   s    ; ;s ;c  Q[QlQl ,S  QU 2
s 
s  #  r   r   __main__)levelzListing current sessions:)r&   jsonloggingtypingr   r   r   r   	getLoggerrO   r#   r   basicConfigINFObridgeprintrE   sessionsrW   r   r   <module>rc      sz       ' ' 				8	$T Tl zgll+]F	
%&##%H	(O r   