
    0j	                        d dl Z d dlZd dlmZmZmZ d dlZddlmZ ddl	m
Z
 ddlmZmZ ddlmZ deeej        def         d	efd
Zdedeedf         d	efdZded	efdZej        deded	ed         fd            ZdS )    N)UnionIteratorNoReturn   )NotGiven)DEFAULT_EXECUTE_TIMEOUT_SECONDS)APIStatusErrorAPITimeoutError)is_giventimeoutreturnc                 x    t          | t          j                  r| j        } t	          |           r| t
          S | S )zCreate a simple float timeout for server responses from the provided timeout.

    For httpx.Timeout, we only use the read timeout.
    )
isinstancehttpxTimeoutreadr   r   )r   s    Z/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/parallel/lib/_time.pyprepare_timeout_floatr      s>    
 '5=)) ,G /..N    run_idexcc                 *    t          d|  d          |)Nz$Fetching task run result for run id z timed out.)TimeoutError)r   r   s     r   _raise_timeoutr      s    
QfQQQ
R
RX[[r   status_codec                 
    | dv S )a  Determine if an error is retryable.

    We retry the following HTTP status codes within the SDK:
    - 408 (Request Timeout): The server timed out waiting for the request
    - 503 (Service Unavailable): The server is temporarily unable to handle the request
    - 504 (Gateway Timeout): The gateway server timed out

    These errors typically indicate temporary issues with the server or network
    that may resolve upon retry. We don't include 429 (Too Many Requests) as this
    indicates rate limiting, which requires backing off rather than immediate retries.

    Note: This is a low-level retry mechanism within the SDK. Customers may want to
    implement their own retry logic at the application level for other error types.
    )i  i  i   )r   s    r   _is_retryable_errorr      s     /))r   deadlinec              #      K   d}t          j                    |k     rI	 dV  dS # t          $ r}|}Y d}~/d}~wt          $ r!}t	          |j                  r|}Y d}~W d}~ww xY wt          | |           dS )aN  Context manager for handling timeouts and retries when fetching task run results.

    Args:
        run_id: The ID of the task run
        deadline: The absolute time (monotonic) by which the operation must complete

    Raises:
        TimeoutError: If the deadline is reached
        APIStatusError: For non-timeout API errors
    N)time	monotonicr
   r	   r   r   r   )r   r   r   es       r   timeout_retry_contextr$   1   s       #'C
.

X
%
%
	EEEF 	 	 	CHHHH 	 	 	"1=11 		
 63s$   # 
A"4A"AAA")r!   
contextlibtypingr   r   r   r   _typesr   
_constantsr   _exceptionsr	   r
   _utils._utilsr   floatr   r   str	Exceptionr   intboolr   contextmanagerr$   r   r   r   <module>r1      sd        , , , , , , , , , ,        8 8 8 8 8 8 9 9 9 9 9 9 9 9 $ $ $ $ $ $5tX)M#N SX    \3 \U9d?%; \ \ \ \ \*S *T * * * *$  #    8D>            r   