
    &`ir/                        d dl Z d dlZd dlZd dlZd dlZd dlZd dl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 d dlmZ d dlZd dlmZmZ d dlmZ d dlmZmZ d dlmZ  ej        e          Zej        fdZ G d	 d
e           Z! G d de          Z" G d de          Z#	 	 	 	 	 	 ddZ$edd            Z%d Z&d Z'd Z(d Z)dS )    N)Pdb)Callable)build_addressis_ipv6)ray_constants)_internal_kv_del_internal_kv_put)DeveloperAPIc                 P    t          | |           |                                 d S )N)file)printflush)messagestderrs     a/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/rpdb.py_cryr      s&    	'
LLNNNNN    c                   |    e Zd Zd Zed             Zd Z ej        d          fdZ	 ej        d          fdZ
dS )_LF2CRLF_FileWrapperc                 2   | _                             d          x| _        j        | _        j        | _        j        | _        j        | _        j        | _        j        | _        t          d          rfd| _
        d S j        | _
        d S )Nrwencodingc                 b                         |                     j        d                    S )Nreplace)errors)sendallencoder   )data
connectionfhs    r   <lambda>z/_LF2CRLF_FileWrapper.__init__.<locals>.<lambda>-   s,    j&8&8BK	::' ' r   )r   makefilestreamreadreadline	readlinescloser   filenohasattr_sendr   )selfr   r    s    `@r   __init__z_LF2CRLF_FileWrapper.__init__#   s    $%..t444bG	X
X
i2z"" 	,    DJJJ $+DJJJr   c                     | j         j        S N)r#   r   r+   s    r   r   z_LF2CRLF_FileWrapper.encoding3   s    {##r   c                 4    | j                                         S r.   )r#   __iter__r/   s    r   r1   z_LF2CRLF_FileWrapper.__iter__7   s    {##%%%r   z?
c                 \    |                     d|          }|                     |           d S )Nz
)subr*   )r+   r   nl_rexs      r   writez_LF2CRLF_FileWrapper.write:   s-    zz&$''

4r   c                 <    |D ]}|                      ||           d S r.   )r5   )r+   linesr4   lines       r   
writelinesz_LF2CRLF_FileWrapper.writelines>   s2     	% 	%DJJtV$$$$	% 	%r   N)__name__
__module____qualname__r,   propertyr   r1   recompiler5   r9    r   r   r   r   "   s        , , ,  $ $ X$& & & ",G!4!4     (2rz'':': % % % % % %r   r   c                   <    e Zd ZdZdeg df         fdZd ZexZZdS )_PdbWrapz/Wrap PDB to run a custom exit hook on continue.	exit_hookNc                 <    || _         t          j        |            d S r.   )
_exit_hookr   r,   )r+   rC   s     r   r,   z_PdbWrap.__init__F   s    #Tr   c                 T    |                                   t          j        | |          S r.   )rE   r   do_continuer+   args     r   rG   z_PdbWrap.do_continueJ   s$    tS)))r   )	r:   r;   r<   __doc__r   r,   rG   do_cdo_contr@   r   r   rB   rB   C   sS        99(2t8"4    * * * ! D777r   rB   c                   f    e Zd ZdZdZ	 	 ddZd Zd Zd ZexZ	Z
d ZexZZdd	Zdd
Zd Zd ZdS )
_RemotePdbac  
    This will run pdb as a ephemeral telnet service. Once you connect no one
    else can connect. On construction this object will block execution till a
    client has connected.
    Based on https://github.com/tamentis/rpdb I think ...
    To use this::
        RemotePdb(host="0.0.0.0", port=4444).set_trace()
    Then run: telnet 127.0.0.1 4444
    NFc                 h   || _         || _        || _        t          j        t	          |          rt          j        nt          j        t          j                  | _        | j        	                    t          j
        t          j        d           | j                            ||f           || _        d S )NT)_breakpoint_uuid_quiet_patch_stdstreamssocketr   AF_INET6AF_INETSOCK_STREAM_listen_socket
setsockopt
SOL_SOCKETSO_REUSEADDRbind_ip_address)r+   breakpoint_uuidhostport
ip_addresspatch_stdstreamsquiets          r   r,   z_RemotePdb.__init__^   s     !0!1$m&t}}@FOO&.&BT
 
 	&&v'8&:MtTTT  $...%r   c                 x   | j         sBt          dt          | j        | j                                        d                   z             | j                            d           | j                                        \  }}| j         st          d|            t          |          | _	        t          j        | d| j	        | j	        dg           g | _        | j        rOdD ]L}| j                            |t          t           |          f           t#          t           || j	                   M| t$          _        d S )Nz;RemotePdb session open at %s, use 'ray debug' to connect...   z#RemotePdb accepted connection from tabzray.*)completekeystdinstdoutskip)r   rh   
__stderr__
__stdout__rg   	__stdin__)rQ   r   r   r\   rW   getsocknamelistenacceptr   handler   r,   backuprR   appendgetattrsyssetattrrN   active_instance)r+   r   addressnames       r   rn   z_RemotePdb.listenq   sP   { 	0 0$2E2Q2Q2S2STU2VWWX  
 	""1%%%"188::
G{ 	B@w@@AAA*:66+;	
 	
 	
 	
 ! 
	0 	0 	0 ""D'#t*<*<#=>>>T4;////%)
"""r   c                     | j         r| j        st          d| j         z             | j         D ]\  }}t          t          ||           | j                                         d t          _        d S )NzRestoring streams: %s ...)	rq   rQ   r   ru   rt   rp   r'   rN   rv   )r+   rx   r    s      r   	__restorez_RemotePdb.__restore   sw    ; 	<t{ 	<,t{:;;; 	# 	#HD"Cr""""%)
"""r   c                 T    |                                   t          j        | |          S r.   )_RemotePdb__restorer   do_quitrH   s     r   r}   z_RemotePdb.do_quit   s$    {4%%%r   c                     |                                   | j        j                                         t	          j        | |          S r.   )r|   rp   r   r'   r   rG   rH   s     r   rG   z_RemotePdb.do_continue   s;    $$&&&tS)))r   c                     |t          j                    j        }	 t          j        | |           d S # t
          $ r!}|j        t          j        k    r Y d }~d S d }~ww xY wr.   )rt   	_getframef_backr   	set_traceIOErrorerrno
ECONNRESET)r+   frameexcs      r   r   z_RemotePdb.set_trace   sx    =MOO*E	M$&&&&& 	 	 	yE,,, -,,,,,	s   3 
AAAc                     	 t          j                    d         }|                                  t          j        | d |           d S # t
          $ r!}|j        t          j        k    r Y d }~d S d }~ww xY w)N   )rt   exc_inforesetr   interactionr   r   r   )r+   	tracebacktr   s       r   post_mortemz_RemotePdb.post_mortem   s    	q!AJJLLLOD$***** 	 	 	yE,,, -,,,,,	s   AA 
A2A--A2c                    | j         t          j        j        j        _        t          j        dt          j                    	                                i          }t          d                    | j                   |t          j                   |                                  | j        j                                         t%          j        | |          S )z7remote
        Skip into the next remote call.
        job_idzRAY_PDB_CONTINUE_{}	namespace)rP   ray_privateworkerglobal_workerdebugger_breakpointjsondumpsget_runtime_context
get_job_idr	   formatr   KV_NAMESPACE_PDBr|   rp   r   r'   r   rG   )r+   rI   r   s      r   	do_remotez_RemotePdb.do_remote   s    
 AE@U)=z#133>>@@
 

 	!(()>??#4	
 	
 	
 	

 	$$&&&tS)))r   c                     | j         t          j        j        j        _        |                                  | j        j        	                                 t          j        | |          S )z?get
        Skip to where the current task returns to.
        )rP   r   r   r   r   debugger_get_breakpointr|   rp   r   r'   r   rG   rH   s     r   do_getz_RemotePdb.do_get   sU    
 ! 	)A 	$$&&&tS)))r   )FFr.   )r:   r;   r<   rJ   rv   r,   rn   r|   r}   do_qdo_exitrG   rK   rL   r   r   r   r   r@   r   r   rN   rN   Q   s          O & & & &&* * *B* * *& & & D7* * *
 ! D7   	 	 	 	* * **	* 	* 	* 	* 	*r   rN   Fc                    |r| r
J d            d} n"|  t           j                            dd          } |-t          t           j                            dd                    }|-t	          t           j                            dd	                    }|st          j                    j        }|rt          j	        j
        j        j        }nd
}t          || ||||          }|j                                        }t!          ||d                   }	t#          j        t#          j                              d         }
t          j                                        |	|
j        |
j        d                    t3          j        t7          j                               t;          j                    t          j                                                    t          j                                                     t          j                    !                                t          j                    "                                t          j                    #                                d}tI          d%                    |          tM          j'        |          dtP          j)                   |*                                 tW          d%                    |          tP          j)                   |S )z5
    Opens a remote PDB on first available port.
    z.Cannot specify both host and debugger_externalz0.0.0.0NREMOTE_PDB_HOSTz	127.0.0.1REMOTE_PDB_PORT0REMOTE_PDB_QUIET 	localhost)r]   r^   r_   r`   ra   rb   rd   r   
)	proctitlepdb_addressfilenamelinenor   	timestampr   node_id	worker_idactor_idtask_idz
RAY_PDB_{}T)	overwriter   r   ),osenvirongetintbooluuiduuid4hexr   r   r   r   node_ip_addressrN   rW   rm   r   inspectgetouterframescurrentframe_rayletgetproctitler   r   joinr   format_exceptionrt   r   timer   r   get_node_idget_worker_idget_actor_idget_task_idr	   r   r   r   r   r   rn   r   )r^   r_   ra   rb   r]   debugger_externalr`   rdbsocknamer   parentframeinfor   s               r   _connect_ray_pdbr      sw     >IIIIIx	z~~/==|2:>>"3S99::}RZ^^$6;;<< +*,,* !\(6F

 

')  C !--//H
HQK88K,W-A-C-CDDQGO[--//"#,!(YYy93<>>JKKY[[)++6688*,,88::,..<<>>+--::<<*,,88:: D O,,
40	    JJLLLO,,8V    Jr   c                    t           j                            dd          dk    r$t          j        j                            |           S t           j                            dd          dk    rt          j        j        j	        j
        dk    rvt          j                    j        }t          dddd| r|                                 ndt          j        j        j	        j                  }|                    |           dS dS dS )	zsInterrupt the flow of the program and drop into the Ray debugger.

    Can be used within a Ray task or actor.
    	RAY_DEBUG1legacyr   NF)r^   r_   ra   rb   r]   r   )r   )r   r   r   r   utilray_debugpyr   r   r   r   r   rt   r   r   r   decoderay_debugger_external)r]   r   r   s      r   r   r     s     
z~~k3''3..x#--o>>>	z~~k3''833 <,@CGGMOO*E"!&<K U 6 6 8 8 8QU"%,"5"C"Y  C MMM&&&&& 43 HGr   c                     t           j                            dd          dk    r#t          j        j                                        S t           j                            dd          dk    rft          d           dt          j        j	        _
        d } t          |           }t          j                    j        }|                    |           dS dS )zThe breakpoint hook to use for the driver.

    This disables Ray driver logs temporarily so that the PDB console is not
    spammed: https://github.com/ray-project/ray/issues/18172
    r   r   r   z-*** Temporarily disabling Ray worker logs ***Fc                  P    t          d           dt          j        j        _        d S )Nz#*** Re-enabling Ray worker logs ***T)r   r   r   r   _worker_logs_enabledr@   r   r   enable_loggingz)_driver_set_trace.<locals>.enable_logging@  s$    78887;CL444r   N)r   r   r   r   r   r   r   r   r   r   r   rB   rt   r   r   )r   pdbr   s      r   _driver_set_tracer   4  s     
z~~k3''3..x#--///	z~~k3''833=>>>380	< 	< 	< ~&&&e 43r   c                  J    t           j                            dd          dk    S )NRAY_DEBUG_POST_MORTEMr   r   )r   r   r   r@   r   r   $_is_ray_debugger_post_mortem_enabledr   I  s    :>>13773>>r   c                     t           j                            dd          dk    r#t          j        j                                        S t          d d dd t          j        j	        j
        j                  } |                                  d S )Nr   r   F)r^   r_   ra   rb   r   )r   r   r   r   r   r   _post_mortemr   r   r   r   r   r   )r   s    r   r   r   M  sz    	z~~k3''3..x#00222
,-;Q  C OOr   c                    t           j        dk    rdd l}t          j        t	          |           rt          j        nt          j        t          j                  }|                    | |f           	 t           j        dk    rt          j	        |gg g d          d         }|
                                r|                    t           j                   |s=t           j                                        s|                    t           j                   n&t          j	        t           j        |gg g           \  }}}|D ]}||k    rj|                    d          }|s d S t           j                            |                                           t           j                                         rt           j                                        }	|                    |	                                           )Nwin32r   Trd   i   )rt   platformmsvcrtrS   r   rT   rU   rV   connectselectkbhitrr   rg   isattyrecvrh   r5   r   r   r%   sendr   )
r^   r_   r   sready_to_readwrite_socketserror_socketssockr   msgs
             r   _connect_pdb_clientr   [  s   
|w"4==<fnf>P	 	A IItTl%<7"""M1#r2q99!<M||~~ 0$$SY///  0)9)9);); 0 $$SY///:@-AB; ;7M=- " 	% 	%Dqyyyy 'FFJ$$T[[]]333J$$&&&& i((**szz||$$$$9%r   )NNFNNFr.   )*r   r   r   loggingr   r>   r   rS   rt   r   r   r   r   r   typingr   r   ray._common.network_utilsr   r   ray._privater   ray.experimental.internal_kvr   r	   ray.util.annotationsr
   	getLoggerr:   logrj   r   objectr   rB   rN   r   r   r   r   r   r   r@   r   r   <module>r      s;  
     				 				   



                   



 < < < < < < < < & & & & & & K K K K K K K K - - - - - -g!!     
% % % % %6 % % %B! ! ! ! !s ! ! !H* H* H* H* H* H* H* H*X 
	
= = = =@ ' ' ' '.  *? ? ?  %% %% %% %% %%r   