
    b\j+                       S SK Jr  S SKrS SKrS SKrS SKJr  S SKJrJ	r	  S SK
r
SSKJrJrJrJr  SSKJrJr  SS	KJr  SS
KJr  SSKJrJr  / SQrSrSr\R:                  " \5      rSS jr SS jr!SS jr"SS jr#S S jr$S\SSSSS.                 S!S jjr%SS.S"S jjr&S\SSSSS.                 S#S jjr'SS.         S$S jjr(g)%    )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                    [        U [        S9$ )N)cap)r
   _POLL_BACKOFF_CAP)attempts    X/var/www/website/venv/lib/python3.13/site-packages/anthropic/lib/environments/_poller.py_backoffr      s    7 122    c                    [        X5      $ N)r	   )lowhighs     r   _jitterr!   "   s    #r   c                    [        U 5      $ r   )r   )errs    r   _is_fatal_4xxr$   &   s     %%r   c                N    [        U [        5      =(       a    U R                  U:H  $ r   )
isinstancer   status_code)r#   codes     r   
_is_statusr)   *   s    c>*Fs$/FFr   c                 `    [         R                  " 5        S[        5       R                  S S  3$ )N-   )socketgethostnamer   hex r   r   _default_worker_idr1   .   s-       "#1UW[["%5$677r   FT)	worker_idblock_msreclaim_older_than_msdrain	auto_stopextra_headersc          	   #  @  #    U=(       d
    [        5       n[        R                  SXU5        SnSn	  U R                  UUb  UO[        Ub  UO[        UUS9n
SnU
c@  U(       a  [        R                  S
U5        g[        R                  " [        SS5      5        Ml  [        R                  SU
R                  [        U
R                  SS5      5         U R!                  U
R                  UUS9  Sn	U(       d  U
v   M   U
v    U R%                  U
R                  UUS9  M  ! [
         a}  n[        U5      (       a  [        R                  SU5        e US-  n[        U5      [        SS5      -   n[        R                  S	XU5        [        R                  " U5         SnAGMr  SnAff = f! [
         a  n[        U5      (       a<  [        R                  SU
R                  U5        [#        X
R                  XS9   SnAGM  U	S-  n	[        U	5      [        SS5      -   n[        R                  SU
R                  XU5        [        R                  " U5         SnAGM,  SnAff = f! [&         a=  n[)        US5      (       d!  [        R                  SU
R                  U5         SnAGNSnAff = f!  U R%                  U
R                  UUS9  f ! [&         aA  n[)        US5      (       d&  [        R                  SU
R                  U5         SnAf  SnAf SnAff = f= f7f)uK	  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=%sDack 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/1IHHF_hi LK
	99%-%9t?T?`&;fj$-+  D  <:NKJJwsC()7'$))U[]aBbc	HH-+  " J	OJO		GG#1"/  i    	Q		<a@ALL)GC,==DKKG]^_JJt	.   	Q		VX\X_X_abc#D''.n1KK(73+<<DKKVX\X_X_altu JJt	2  O!!S))KK A477ANOO		GG#1"/  
  O!!S))KK A477ANN *Os   0L%D! A;LF+ /L?J/ I% L!
F(+A1F#L#F((L+
I"5AI:LAILI""L%
J,/2J'!L'J,,L/L1KL
L2L	
LLLL)r7   c                    U R                  XSUS9  g! [         a6  n[        US5      (       d  [        R	                  SX5         SnAg SnAgSnAff = f)zBest-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      sR    a		'Ta	b a!S!!IISU\`` "as    
A'AAc          	      #    U=(       d
    [        5       n[        R                  SXU5        SnSn	  U R                  UUb  UO[        Ub  UO[        UUS9I Sh  vN n
SnU
cH  U(       a  [        R                  S
U5        g[        R                  " [        SS5      5      I Sh  vN   M|  [        R                  SU
R                  [        U
R                  SS5      5         U R!                  U
R                  UUS9I Sh  vN   Sn	U(       d  U
7v   M   U
7v    U R%                  U
R                  UUS9I Sh  vN   GM   N! [
         a  n[        U5      (       a  [        R                  SU5        e US-  n[        U5      [        SS5      -   n[        R                  S	XU5        [        R                  " U5      I Sh  vN     SnAGM  SnAff = f GN3 N! [
         a  n[        U5      (       aE  [        R                  SU
R                  U5        [#        X
R                  XS9I Sh  vN     SnAGM  U	S-  n	[        U	5      [        SS5      -   n[        R                  SU
R                  XU5        [        R                  " U5      I Sh  vN     SnAGMq  SnAff = f GNn! [&         a=  n[)        US5      (       d!  [        R                  SU
R                  U5         SnAGNSnAff = f!  U R%                  U
R                  UUS9I Sh  vN    f ! [&         aA  n[)        US5      (       d&  [        R                  SU
R                  U5         SnAf  SnAf SnAff = f= f7f)zAsync 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/1IHHF_hiLK
	%-%9t?T?`&;fj$-+ #  D  <:NK++gc3/0007'$))U[]aBbc	((-+    " J	OJOiiGG#1"/     i    	Q		<a@ALL)GC,==DKKG]^_++d###	 1
   	Q		VX\X_X_abc*4uuu1KK(73+<<DKKVX\X_X_altu ++d###	(
  O!!S))KK A477ANOOiiGG#1"/     
  O!!S))KK A477ANN *Os@  0M/(E EE  AM/&G'<M/$G GG M/K7 J- <J*=J- M/E 
GA4GGG
M/GM/G 
J'(AJ"0H31J"6M/=AJ"JJ"M/"J''M/*J- -
K472K/)M//K44M/7M,9LLLM,
M)(2M$
M,$M))M,,M/c                  #     U R                  XSUS9I Sh  vN   g N! [         a6  n[        US5      (       d  [        R	                  SX5         SnAg SnAgSnAff = f7f)z-Async version of :func:`_force_stop_quietly`.Tra   NrI   rc   rd   re   s        r   ri   ri     sZ     aiidZgihhh a!S!!IISU\`` "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 ?  !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   