
    #/in                          d Z ddlZddlZddlZddlmZ ddlmZ  ej                  e      Z	 ed      Z
e
dz  Ze
dz  Ze
dz  Zdad	 Zd
ededefdZdedefdZdedededefdZddedefdZy)a7  
Handoff MCP Tools
==================
Structured auto-handoff when an agent finishes a block of work.
Enables seamless context transfer between Claude and Antigravity agents.

Tools:
- handoff_push: Write structured handoff + publish event
- handoff_pull: Read latest handoff(s) + recent events since last sync
    N)datetime)Pathz+/mnt/e/genesis-system/data/antigravity-synchandoffszevents.jsonlzcurrent_state.jsonc                  v   t         {	 t        j                  j                  dd       t        j                  j                  dd       ddlm}  ddlm}  | |j                  d      a t         j                          t         S t         S # t        $ r'}t        j                  d	|        Y d}~t         S d}~ww xY w)
zLazy load AgentBus.Nr   z/mnt/e/genesis-systemz)/mnt/e/genesis-system/data/genesis-memory)AgentBus)	AgentTypeF)
agent_typeenable_auditz#AgentBus unavailable for handoffs: )
_agent_bussyspathinsertAIVA.agents.agent_busr   AIVA.agents.message_schemasr   CLAUDEconnect	Exceptionloggerwarning)r   r   es      5/mnt/e/genesis-system/genesis_v2/mcp/handoff_tools.py_get_agent_busr      s     	FHHOOA67HHOOAJK6=!Y-=-=ERJ  :  	FNN@DEE	Fs   A4B 	B8B33B8source
event_typedatac                    	 t        j                         j                         | ||d}t        j                  j                  dd       t        t        d      5 }|j                  t        j                  |      dz          ddd       y# 1 sw Y   yxY w# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)zAppend event to events.jsonl.)	timestampr   typer   Tparentsexist_oka
NzFailed to log event: )r   utcnow	isoformatEVENTS_FILEparentmkdiropenwritejsondumpsr   r   error)r   r   r   eventfr   s         r   
_log_eventr0   .   s    2!*446	
 	   =+s# 	.qGGDJJu%,-	. 	. 	. 2,QC0112s6   AB (BB BB B 	C B==Cagenthandoff_summaryc                 6   	 i }t         j                         r.t        t         d      5 }t        j                  |      }ddd       t        j                         j                         |d<   d|vri |d<   | |dd t        j                         j                         d|d   d<   t        t         d      5 }t        j                  ||d	
       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z3Update current_state.json with latest handoff info.rNlast_updatedcoordinationi  )r1   summaryr   last_handoffw   indentz Failed to update current state: )
STATE_FILEexistsr)   r+   loadr   r$   r%   dumpr   r   r-   )r1   r2   stater/   r   s        r   _update_current_staterB   >   s   =j#& %!		!% !) 1 ; ; =n&$&E.! &t,!*4461
nn- *c" 	*aIIeQq)	* 	*% %	* 	* =7s;<<=sL   &C- CA5C- 3C!C- CC- !C*&C- *C- -	D6DDr7   files_changed_jsonnext_actions_jsonreturnc                    	 |rt        j                  |      ng }	 |rt        j                  |      ng }d}t        j                         j                         }||| |g g |dddd}t        j                  dd       t        d| dz  }t        | d	|j                  d
d       dz  }		 t        |d      5 }
t        j                  ||
d       ddd       t        |	d      5 }
t        j                  ||
d       ddd       t               }|rL	 ddlm}m}  ||j$                  dd|| dd t'        |      t'        |      dd      }|j)                  d|       t-        ||        t/        |d| dd t'        |      t'        |      d       t        j0                  | d	| dt3        |      t'        |      t'        |      d      S # t         j                  $ r g }Y w xY w# t         j                  $ r g }Y w xY w# 1 sw Y   4xY w# 1 sw Y   xY w# t        $ r#}t        j                  d|        Y d}~>d}~ww xY w# t        $ r#}t        j+                  d|        Y d}~d}~ww xY w)aG  
    Push a structured handoff when an agent finishes a block of work.

    Args:
        summary: What was done (text description)
        files_changed_json: JSON array of file paths that were changed
        next_actions_json: JSON array of recommended next actions

    Returns:
        JSON with handoff ID and status
    claudeN)
session_idtoken_usage)r1   r   r7   files_changedtasks_completedtasks_creatednext_actionscontext_snapshotTr   LATEST_.json_:-r9   r:   r;   zFailed to write handoff file: r   )EventMessager   zaiva:eventshandoff_pushed   )r1   r7   files_countactions_countinfo)senderchannelr   payloadseverityzRedis handoff event failed: )r7   rJ   rM   pushed)
handoff_idstatusfilerJ   rM   )r+   loadsJSONDecodeErrorr   r$   r%   HANDOFFS_DIRr(   replacer)   r@   r   r   r-   r   r   rT   r   r   lenpublishdebugrB   r0   r,   str)r7   rC   rD   rJ   rM   r1   nowhandofflatest_filearchive_filer/   r   busrT   r   msgs                   r   handoff_pushrp   V   s   :L

#56RT8Itzz"34r E
//

%
%
'C &$
G td375'!77KUG1S[[c-B,C5"IIL;+s# 	,qIIgq+	,,$ 	,IIgq+	, 
C
	=K ''%+"&t}#&}#5%(%6	  C KKs+
 %) u&4C=]+L))  ::q&K ]+L)  K  
  4	, 	,	, 	, ;5aS9::;*  	=LL7s;<<	=s   G
 G& +H 7HH $H=H AI 
G#"G#&G?>G?HH HH 	I%II	I7I22I7c                    g }t         j                  dd       | r| g}nddg}|D ]\  }t         d| dz  }|j                         s!	 t        |d      5 }t	        j
                  |      }|j                  |       ddd       ^ g }t        | xs d dz  }	d}
|	j                         r<	 t        |	d      5 }t	        j
                  |      }|j                  d      }
ddd       t        j                         r	 t        t        d      5 }|D ]e  }|j                         }|s	 t	        j                  |      }|
r'|j                  dd      |
kD  r#|j                  |       n|j                  |       g 	 ddd       |dd }t!        j"                         j%                         }	 |	j&                  j                  dd       t        |	d      5 }t	        j(                  d|i|       ddd       t	        j*                  ||t-        |      ||
d      S # 1 sw Y   xY w# t        $ r&}t        j                  d	| d
|        Y d}~%d}~ww xY w# 1 sw Y   xxY w# t        $ r Y w xY w# t        j                  $ r Y yw xY w# 1 sw Y   xY w# t        $ r#}t        j                  d|        Y d}~@d}~ww xY w# 1 sw Y   xY w# t        $ r#}t        j                  d|        Y d}~	d}~ww xY w)a  
    Pull latest handoff(s) and recent events since last sync.

    Args:
        agent: Filter by agent name (empty = all agents). Common values:
               "claude", "antigravity"

    Returns:
        JSON with latest handoffs, recent events, and sync timestamp
    Tr   rG   antigravityrO   rP   r4   NzFailed to read handoff for z: z_last_sync.json	last_syncr    zFailed to read events: ir9   z!Failed to update sync timestamp: )r   recent_eventsevents_count	synced_atrs   )rd   r(   r>   r)   r+   r?   appendr   r   r-   SYNC_DIRgetr&   striprb   rc   r   r$   r%   r'   r@   r,   rf   )r1   r   agents_to_checkr"   rl   r/   handoff_datar   ru   	sync_filers   	sync_dataliner.   rj   s                  r   handoff_pullr      s    Htd3  '#]3 E"wqc%77E+s+ 2q#'99Q<LOOL12	E Me/x0@@II	i% 7 IIaL	%MM+6	7 	8k3' !1 !D::<D ! $

4 0$$yyb9IE - 4 4U ;)007!!$ "#$'M //

%
%
'C>td;)S! 	-QII{C(!,	-
 ::&M*  e2 2  E:1#RsCDDE7 7  		"  // ! !! !  	8LL21#677	8	- 	- >8<==>s   H!'H;H!*I  6'II  :J 
J
$AI03J
6J &)K K(K H	H!!	I*IIII   	I-,I-0JJ
JJ

JJ 	K J>>KKK 	K>K99K>)rt   )__doc__r+   r   loggingr   pathlibr   	getLogger__name__r   ry   rd   r&   r=   r   r   ri   dictr0   rB   rp   r        r   <module>r      s   	  
   			8	$ =>*$',,
 
 2s 2 24 2 = =s =0Y# Y3 Y3 YSV YxP PS Pr   