Ë
    A²XjÔ+  ã                  óš  — d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
Z
ddlmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZ g d¢ZdZdZ ej:                  e«      Zdd„Z dd„Z!dd„Z"dd„Z#d d„Z$dedddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!d„Z%ddœd"d„Z&dedddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#d„Z'ddœ	 	 	 	 	 	 	 	 	 d$d„Z(y)%é    )ÚannotationsN)Úuuid4)ÚIteratorÚAsyncIteratoré   )ÚTRANSIENT_ERRORSÚjitterÚbackoffÚis_fatal_status_erroré   )ÚHeadersÚomit)ÚAPIStatusError)ÚBetaSelfHostedWork)ÚWorkÚ	AsyncWork)Ú	iter_workÚ
aiter_workÚPOLL_BLOCK_MSiç  g      N@c                ó$   — t        | t        ¬«      S )N)Úcap)r
   Ú_POLL_BACKOFF_CAP)Úattempts    úh/var/www/html/content_generation/venv/lib/python3.12/site-packages/anthropic/lib/environments/_poller.pyÚ_backoffr      s   € Ü�7Ô 1Ô2Ð2ó    c                ó   — t        | |«      S ©N)r	   )ÚlowÚhighs     r   Ú_jitterr!   "   s   € Ü�#�tÓÐr   c                ó   — t        | «      S r   )r   )Úerrs    r   Ú_is_fatal_4xxr$   &   s   € Ü  Ó%Ð%r   c                óD   — t        | t        «      xr | j                  |k(  S r   )Ú
isinstancer   Ústatus_code)r#   Úcodes     r   Ú
_is_statusr)   *   s   € Ü�cœ>Ó*ÒF¨s¯©À$Ñ/FÐFr   c                 ó^   — t        j                  «       › dt        «       j                  d d › �S )NÚ-é   )ÚsocketÚgethostnamer   Úhex© r   r   Ú_default_worker_idr1   .   s-   € ô × Ñ Ó"Ð# 1¤U£W§[¡[°°"Ð%5Ð$6Ð7Ð7r   FT)Ú	worker_idÚblock_msÚreclaim_older_than_msÚdrainÚ	auto_stopÚextra_headersc          	   #  ó  K  — |xs
 t        «       }t        j                  d|||«       d}d}		 	 | j                  ||�|nt        |�|nt        ||¬«      }
d}|
€9|rt        j                  d
|«       yt        j                  t        dd«      «       Œet        j                  d|
j                  t        |
j                  dd«      «       	 | j!                  |
j                  ||¬«       d}	|s|
–— ŒÃ	 |
–— 	 | j%                  |
j                  ||¬«       Œç# t
        $ rw}t        |«      rt        j                  d|«       ‚ |dz  }t        |«      t        dd«      z   }t        j                  d	|||«       t        j                  |«       Y d}~�Œcd}~ww xY w# t
        $ r«}t        |«      r@t        j                  d|
j                  |«       t#        | |
j                  ||¬«       Y d}~�Œ¿|	dz  }	t        |	«      t        dd«      z   }t        j                  d|
j                  |	||«       t        j                  |«       Y d}~�Œd}~ww xY w# t&        $ r8}t)        |d«      s!t        j                  d|
j                  |«       Y d}~�Œwd}~ww xY w# 	 | j%                  |
j                  ||¬«       w # t&        $ r<}t)        |d«      s&t        j                  d|
j                  |«       Y d}~w Y d}~w d}~ww xY wxY w­w)uã	  Iterate work items claimed from a self-hosted environment.

    Each yielded :class:`BetaSelfHostedWork` has already been ack'd. The ``work``
    resource must be bound to a client authenticated for the environment â€” the
    poller itself does not handle credentials. Use
    ``client.beta.environments.work.poller(...)`` for the user-facing entry
    point that constructs a scoped sub-client for you.

    Two consumption shapes are supported:

    - **Long-running runner** (``drain=False, auto_stop=True``, the default):
      loops forever, sleeps with jitter on empty polls, and calls ``work.stop``
      when the consuming for-loop body returns or raises. The poller owns the
      whole work-item lifecycle.
    - **Drain-and-dispatch** (``drain=True, auto_stop=False``): returns as soon
      as the queue is empty and never calls ``work.stop`` â€” use this when each
      yielded item is handed off to another process (e.g. a webhook handler
      that spawns a sandbox per work item) and that process owns ``stop``.

    Args:
      block_ms: How long the server holds an empty poll open (long-poll).
        Pass ``None`` to omit the param for a non-blocking poll â€” the server
        rejects ``0``. Drain callers usually want ``None`` so the final empty
        poll returns immediately.
      drain: When True, return after the first empty poll instead of sleeping
        and re-polling. Lets a webhook-driven dispatcher drain the queue and
        respond.
      auto_stop: When True (default), call ``work.stop`` after the consumer's
        loop body completes. Set False when the work item is handed off to
        another process that owns the stop call â€” otherwise the lease is
        terminated out from under it.
      reclaim_older_than_ms: Forwarded to ``work.poll``. Reclaim un-ack'd work
        older than this many ms. Useful in drain mode so a dead runner's
        work re-surfaces on the next webhook delivery.
      extra_headers: Optional headers passed through per request on every
        poll / ack / stop call (including the force-stop of an unprocessable
        item). They are threaded into each call's ``extra_headers=`` and are
        never assigned onto the client, so client state is not mutated.
        Credentials and ``x-stainless-helper`` come from the bound client,
        not this argument; a header given here overrides the bound client's
        same-named default for that one request, so use it for caller
        passthrough (e.g. trace ids), not to set auth.
    ú7poller starting environment_id=%s drain=%s auto_stop=%sr   N©r3   r4   Úanthropic_worker_idr7   ú poll failed permanently error=%sé   ç        ç      ð?ú-poll failed attempt=%d backoff=%.1fs error=%súqueue drained environment_id=%sç      @ú$claimed work work_id=%s work_type=%sÚtype©Úenvironment_idr7   ú:ack failed permanently; force-stopping work_id=%s error=%súDack failed, backing off work_id=%s attempt=%d backoff=%.1fs error=%sé™  ústop failed work_id=%s error=%s)r1   ÚlogÚinfoÚpollr   r   r$   Úerrorr   r!   ÚwarningÚtimeÚsleepÚidÚgetattrÚdataÚackÚ_force_stop_quietlyÚstopÚ	Exceptionr)   ©ÚworkrF   r2   r3   r4   r5   r6   r7   Úpoll_attemptÚack_attemptÚitemÚeÚwaits                r   r   r   5   sÒ  è ø€ ðl Ò1Ô/Ó1€IÜ‡H�HÐFÈÐX]Ð_hÔið €LØ€KØ
ð	Ø—9‘9ØØ%-Ð%9™¼tØ?TÐ?`Ñ&;ÔfjØ$-Ø+ð ó ˆDð  ˆØˆ<ÙÜ—‘Ð:¸NÔKØÜ�J‰J”w˜s CÓ(Ô)ØÜ�‰Ð7¸¿¹Ä'È$Ï)É)ÐU[Ð]aÓBbÔcð	Ø�H‰HØ—‘Ø-Ø+ð ô ð" ˆÙØŠJØð	OØŠJðOØ—	‘	Ø—G‘GØ#1Ø"/ð ô ði øô  ò 	Ü˜QÔÜ—	‘	Ð<¸aÔ@ØØ˜AÑˆLÜ˜LÓ)¬G°C¸Ó,=Ñ=ˆDÜ�K‰KÐGÈÐW[Ð]^Ô_Ü�J‰J�tÔÝûð	ûô.  ò 	Ü˜QÔÜ—	‘	ÐVÐX\×X_ÑX_ÐabÔcÜ# D¨$¯'©'À.Ð`mÕnÝØ˜1ÑˆKÜ˜KÓ(¬7°3¸Ó+<Ñ<ˆDÜ�K‰KØVÐX\×X_ÑX_ÐalÐnrÐtuôô �J‰J�tÔÝûð	ûô2 ò OÜ! ! SÔ)Ü—K‘KÐ AÀ4Ç7Á7ÈAÔNÿùðOûðOØ—	‘	Ø—G‘GØ#1Ø"/ð õ øô
 ò OÜ! ! SÔ)Ü—K‘KÐ AÀ4Ç7Á7ÈA×NÑNô *ûðOÿsÎ   ‚,K?¯&D ÁA3K?Ã	F Ã'	K?Ã1J Ã6I ÄK?Ä	FÄA,FÆ
K?ÆFÆK?Æ	IÆ!AIÇ&K?Ç,AIÉK?ÉIÉK?É	JÉ-JÊK?ÊJÊK?ÊK<ÊJ4Ê3K<Ê4	K9Ê=-K4Ë*
K<Ë4K9Ë9K<Ë<K?)r7   c               ó¬   — 	 | j                  ||d|¬«       y# t        $ r2}t        |d«      st        j	                  d||«       Y d}~yY d}~yd}~ww xY w)zÛBest-effort ``work.stop(force=True)`` for an item that can't be processed.

    A 409 just means the work already stopped; anything else is logged but not
    raised, since the poll loop must keep going regardless.
    T©rF   Úforcer7   rI   ú;force-stop of unprocessable work failed work_id=%s error=%sN©rW   rX   r)   rK   rN   ©rZ   Úwork_idrF   r7   r^   s        r   rV   rV   °   sT   € ðaØ�	‰	�'¨.ÀÐTaˆ	ÕbøÜò aÜ˜!˜SÔ!Ü�I‰IÐSÐU\Ð^_×`Ñ`ô "ûðaús   ‚ ˜	A¡#AÁAc          	    ó¤  K  — |xs
 t        «       }t        j                  d|||«       d}d}		 	 | j                  ||�|nt        |�|nt        ||¬«      ƒ d{  –—† }
d}|
€A|rt        j                  d
|«       yt        j                  t        dd«      «      ƒ d{  –—†  Œut        j                  d|
j                  t        |
j                  dd«      «       	 | j!                  |
j                  ||¬«      ƒ d{  –—†  d}	|s|
­–— ŒÜ	 |
­–— 	 | j%                  |
j                  ||¬«      ƒ d{  –—†  �Œ
7 Œß# t
        $ r€}t        |«      rt        j                  d|«       ‚ |dz  }t        |«      t        dd«      z   }t        j                  d	|||«       t        j                  |«      ƒ d{  –—†7   Y d}~�Œ‘d}~ww xY w7 �Œ*7 ŒÎ# t
        $ r½}t        |«      rIt        j                  d|
j                  |«       t#        | |
j                  ||¬«      ƒ d{  –—†7   Y d}~�Œû|	dz  }	t        |	«      t        dd«      z   }t        j                  d|
j                  |	||«       t        j                  |«      ƒ d{  –—†7   Y d}~�Œ_d}~ww xY w7 �Œc# t&        $ r8}t)        |d«      s!t        j                  d|
j                  |«       Y d}~�Œ�d}~ww xY w# 	 | j%                  |
j                  ||¬«      ƒ d{  –—†7   w # t&        $ r<}t)        |d«      s&t        j                  d|
j                  |«       Y d}~w Y d}~w d}~ww xY wxY w­w)zŒAsync version of :func:`iter_work`. See its docstring for semantics,
    including how ``extra_headers`` is passed through per request.
    r9   r   Nr:   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rG   rH   rI   rJ   )r1   rK   rL   rM   r   r   r$   rN   r   r!   rO   ÚanyiorQ   rR   rS   rT   rU   Ú_aforce_stop_quietlyrW   rX   r)   rY   s                r   r   r   ½   s!  è ø€ ð Ò1Ô/Ó1€IÜ‡H�HÐFÈÐX]Ð_hÔiØ€LØ€KØ
ð	ØŸ™ØØ%-Ð%9™¼tØ?TÐ?`Ñ&;ÔfjØ$-Ø+ð #ó ÷ ˆDð  ˆØˆ<ÙÜ—‘Ð:¸NÔKØÜ—+‘+œg c¨3Ó/Ó0×0Ð0ØÜ�‰Ð7¸¿¹Ä'È$Ï)É)ÐU[Ð]aÓBbÔcð	Ø—(‘(Ø—‘Ø-Ø+ð ó ÷ ð ð" ˆÙØ‹JØð	OØ‹JðOØ—i‘iØ—G‘GØ#1Ø"/ð  ó ÷ ð ñi ðùô  ò 	Ü˜QÔÜ—	‘	Ð<¸aÔ@ØØ˜AÑˆLÜ˜LÓ)¬G°C¸Ó,=Ñ=ˆDÜ�K‰KÐGÈÐW[Ð]^Ô_Ü—+‘+˜dÓ#×#Ñ#Ýûð	úð 1ùðùô
  ò 	Ü˜QÔÜ—	‘	ÐVÐX\×X_ÑX_ÐabÔcÜ*¨4°·±ÈÐgtÔu×uÑuÝØ˜1ÑˆKÜ˜KÓ(¬7°3¸Ó+<Ñ<ˆDÜ�K‰KØVÐX\×X_ÑX_ÐalÐnrÐtuôô —+‘+˜dÓ#×#Ñ#Ýûð	úð(úô
 ò OÜ! ! SÔ)Ü—K‘KÐ AÀ4Ç7Á7ÈAÔNÿùðOûðOØ—i‘iØ—G‘GØ#1Ø"/ð  ó ÷ ò øô
 ò OÜ! ! SÔ)Ü—K‘KÐ AÀ4Ç7Á7ÈA×NÑNô *ûðOÿs?  ‚,M¯)D: ÁD8ÁD: Á?MÂGÂ;MÃ!G Ã:G	Ã;G Ã?
MÄ
K Ä!J Ä1JÄ2J Ä6MÄ8D: Ä:	GÅA/F>Æ2F5Æ3F>Æ8MÆ>GÇMÇ	G Ç	JÇAJÈHÈJÈ"MÈ(AJÊ JÊJÊMÊJÊMÊJ Ê	KÊ -KËMËKËMËMË!LË>LË?LÌMÌ	M
Ì-MÌ;
MÍM
Í
MÍMc             ƒ  óÈ   K  — 	 | j                  ||d|¬«      ƒ d{  –—†  y7 Œ# t        $ r2}t        |d«      st        j	                  d||«       Y d}~yY d}~yd}~ww xY w­w)z-Async version of :func:`_force_stop_quietly`.Tra   NrI   rc   rd   re   s        r   ri   ri     s]   è ø€ ðaØ�i‰i˜°ÀdÐZgˆiÓh×hÒhùÜò aÜ˜!˜SÔ!Ü�I‰IÐSÐU\Ð^_×`Ñ`ô "ûðaüs6   ‚A"„$ œ"�$ ¡A"¢$ ¤	A­#AÁ
A"ÁAÁA")r   ÚintÚreturnÚfloat)r   rm   r    rm   rl   rm   )r#   rX   rl   Úbool)r#   rX   r(   rk   rl   rn   )rl   Ústr)rZ   r   rF   ro   r2   ú
str | Noner3   ú
int | Noner4   rq   r5   rn   r6   rn   r7   úHeaders | Nonerl   zIterator[BetaSelfHostedWork])
rZ   r   rf   ro   rF   ro   r7   rr   rl   ÚNone)rZ   r   rF   ro   r2   rp   r3   rq   r4   rq   r5   rn   r6   rn   r7   rr   rl   z!AsyncIterator[BetaSelfHostedWork])
rZ   r   rf   ro   rF   ro   r7   rr   rl   rs   ))Ú
__future__r   rP   r-   ÚloggingÚuuidr   Úcollections.abcr   r   rh   Ú_retryr   r	   r
   r   Ú_typesr   r   Ú_exceptionsr   Útypes.beta.environmentsr   Ú resources.beta.environments.workr   r   Ú__all__r   r   Ú	getLoggerÚ__name__rK   r   r!   r$   r)   r1   r   rV   r   ri   r0   r   r   Ú<module>r€      s¿  ðÝ "ã Û Û Ý ß 3ã ç MÓ Mß #Ý )Ý 9ß ?ò€ð €ØÐ à€g×Ñ˜Ó!€ó3óó&óGó8ð !Ø(Ø(,ØØØ$(ñxOØ
ðxOð ðxOð ð	xOð
 ðxOð &ðxOð ðxOð ðxOð "ðxOð "óxOðv koõ 
að" !Ø(Ø(,ØØØ$(ñMOØ
ðMOð ðMOð ð	MOð
 ðMOð &ðMOð ðMOð ðMOð "ðMOð 'óMOðb \`ñaØ
ðaØ!ðaØ69ðaØJXðaà	ôar   