Ë
    òÏjÔ  ã                  óÌ   — d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZ ddlmZ d	Z eeeeef«      Z	  ed
d
¬«       G d„ d«      «       Z	 	 	 	 dd„Zy)aÁ	  Read the status of each task from the writes recorded for a superstep.

While a superstep is open, the checkpointer keeps a log of writes for each
task in that step. Entries are added as tasks run and are only discarded when
the whole superstep finishes and a new checkpoint is saved. When a task runs
again, for example after being resumed, its earlier entries stay in the log.

This module is the single place that turns that log into task status. Code that
needs to know whether a task finished, which interrupts it raised, which of them
are still waiting for an answer, or which output it produced must use
`read_task_statuses` instead of inspecting the writes directly.

The log uses two kinds of writes:

- Control writes describe what happened to a task: `INTERRUPT` (the task asked
  a question), `RESUME` (answers the task has received), `ERROR`, and
  `ERROR_SOURCE_NODE`. `INTERRUPT`, `RESUME` and `ERROR` each have a fixed slot
  per task (`WRITES_IDX_MAP`), so a newer write of the same kind can replace an
  older one.
- Every other write is output: channel writes, `RETURN` for functional tasks,
  and the `NO_WRITES` marker.

The rules are:

1. When a task that ran finishes successfully, `PregelRunner.commit` records at
   least one output write, adding `NO_WRITES` if the task produced no other
   output.
2. A task that pauses at an interrupt records only control writes.
3. A task is therefore treated as finished if and only if it has an output
   write.
4. Because `INTERRUPT` is stored in a fixed slot, its recorded value is the most
   recent question the task asked. That question is waiting for an answer only
   while the task is unfinished.

A `RESUME` write never means a task is finished: it can hold the answer to an
earlier question while the task waits on a later one.

What these rules cannot see:

- A task whose result came from the cache does not go through
  `PregelRunner.commit`, so nothing is recorded for it. It reads as not
  finished.
- A task that fails can record partial output writes along with its error. It
  reads as finished, which is how the executor has always treated it.
- Writes recorded before rule 1 existed may describe a finished task with no
  output using only control writes. Those tasks read as unfinished. The
  executor treated them the same way before and runs them again. Their last
  interrupt now counts as pending, so they appear in `next` and a resume
  without an interrupt id raises if another interrupt is also pending.
é    )Úannotations)ÚIterableÚSequence)Ú	dataclass)ÚAny)ÚPendingWrite)ÚERRORÚERROR_SOURCE_NODEÚ	INTERRUPTÚNULL_TASK_IDÚRESUME)Ú	Interrupt)ÚCONTROL_WRITESÚ
TaskStatusÚread_task_statusesT)ÚfrozenÚslotsc                  óf   — e Zd ZU dZdZded<   	 dZded<   	 dZded	<   	 edd
„«       Z	edd„«       Z
y)r   zHThe status of one task, read from the writes recorded for its superstep.© ztuple[tuple[str, Any], ...]Úoutputútuple[Interrupt, ...]Ú
interruptsNzBaseException | NoneÚerrorc                ó,   — t        | j                  «      S )z#Whether the task ran to completion.)Úboolr   ©Úselfs    úZ/var/www/html/gaurav-v2/venv/lib/python3.12/site-packages/langgraph/pregel/_task_status.pyÚfinishedzTaskStatus.finishedX   s   € ô �D—K‘KÓ Ð ó    c                ó6   — | j                   rdS | j                  S )zCInterrupts waiting for an answer. Always empty for a finished task.r   )r   r   r   s    r   Úpending_interruptszTaskStatus.pending_interrupts]   s   € ð —]’]ˆrÐ7¨¯©Ð7r    )Úreturnr   )r#   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   Úpropertyr   r"   r   r    r   r   r   K   sU   … áRà*,€FÐ'Ó,ØNà(*€JÐ%Ó*ØXà"&€EÐÓ&Ø%àò!ó ð!ð ò8ó ñ8r    r   c                ó  — i }i }i }| D ]›  \  }}}|t         k(  rŒ|j                  |g «       |t        k(  r5|j                  |g «      j                  t	        |t
        «      r|n|g«       Œa|t        k(  r|j                  ||«       Œ}|t        vsŒ†||   j                  ||f«       Œ� |j                  «       D ��ci c]E  \  }}|t        t        |«      t        |j                  |d«      «      |j                  |«      ¬«      “ŒG c}}S c c}}w )z½Return the status of every task that has recorded writes, keyed by task id.

    Writes from `NULL_TASK_ID` are input to the superstep, not task activity, so
    they are not included.
    r   )r   r   r   )r   Ú
setdefaultr   ÚextendÚ
isinstancer   r	   r   ÚappendÚitemsr   ÚtupleÚget)Úpending_writesr   r   ÚerrorsÚtask_idÚchannelÚvalueÚtask_outputs           r   r   r   c   s  € ð 02€FØ-/€JØ')€FÛ#1Ñˆ�˜%Ø”lÒ"ØØ×Ñ˜' 2Ô&Ø”iÒØ×!Ñ! '¨2Ó.×5Ñ5Ü# E¬8Ô4‘¸5¸'õð œÒØ×Ñ˜g uÕ-ØœNÒ*Ø�7‰O×"Ñ" G¨UÐ#3Õ4ð $2ð$ %+§L¡L¤Nôñ %3Ñ ˆG�[ð 	”Ü˜Ó%Ü˜ZŸ^™^¨G°RÓ8Ó9Ø—*‘*˜WÓ%ô
ñ 	
ð
 %3òð ùó s   Â:A
DN)r2   zIterable[PendingWrite]r#   zdict[str, TaskStatus])r'   Ú
__future__r   Úcollections.abcr   r   Údataclassesr   Útypingr   Úlanggraph.checkpoint.baser   Úlanggraph._internal._constantsr	   r
   r   r   r   Úlanggraph.typesr   Ú__all__Ú	frozensetr   r   r   r   r    r   Ú<module>rA      s|   ðñ1õf #ç .Ý !Ý å 2÷õ õ &à
@€á˜EÐ#4°iÀÐHÓI€Ø Rñ �$˜dÔ#÷8ð 8ó $ð8ð.Ø*ðàôr    