
     j                       d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
mZmZmZ ddlmZ dZd	Z ej$                  e      Z	 	 d	 	 	 	 	 dd
Zdddd	 	 	 	 	 	 	 	 	 ddZddddddddded
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)u>  utils/normal_completion_callback_collector_entrypoint.py
task-2728 — normal completion callback collector entrypoint (registry-based idempotent prune wiring).
normal callback/result 수집/envelope CONFIRMED/terminal marker 중 하나라도 성립 시 동일 (task_id·round·head) pending fallback을 즉시 prune.
    )annotationsN)Path)CallableOptional)prune_fallbacks_for_keydetect_unwired_fallbacksummarize_live_prune_now_utcCANONICAL_ROOT_DEFAULTz2normal_completion_callback_collector_entrypoint.v1*RESULT_JSON_MISSING_RECOVERED_BY_COLLECTORc                *    ||dfS | | dfS t         dfS )un   canonical_root/worktree_path 주입 우선, autoset 추측 금지.

    Returns (root: str, source: str).
    worktree_pathcanonical_root_injecteddefaultr   )canonical_rootr   s     8utils/normal_completion_callback_collector_entrypoint.pyresolve_canonical_rootr      s2      //		# 9::&	22    r   r   result_json_pathc               h    |t        |      S t        ||      \  }}t        |      dz  dz  |  dz  S )u   result_json_path 우선, 아니면 resolve_canonical_root로 root 구한 뒤
    Path(root)/"memory"/"results"/f"{task_id}.result.json".
    autoset 추측 금지.
    memoryresultsz.result.json)r   r   )task_idr   r   r   root_s         r   resolve_result_json_pathr   )   sF     #$%%$^]CGD!: 9,',/GGGr   T)
r   r   r   registry_pathremoverdry_runnormal_callback_collecteddispatch_firedrequire_successnow_fnc                   t        ||      \  }}t        | |||      }|j                         }t        |
      xs |xs |dv }|r8|s6t        | |||t        |
      ||t        |      t
        dg dddddd |       dS d}|rd	}n&t
        }d}t        j                  d
| |||t
               t        | |||||	|||	      }d}|st        | ||||||      }dt        |      ddt        |      }t        | |||t        |
      ||t        |      |||D cg c]  }|j                          c}|r|j                         nd| |       dS c c}w )u+  normal callback/result 수집 후 동일 (task_id·round·head) pending fallback을 즉시 prune.

    trigger ∈ {"normal_callback_collected","result_collected","envelope_confirmed","terminal_marker"}
    (검증은 느슨하게 — 그냥 기록).

    require_success=True(기본)면 success signal이 없을 때 prune을 건너뜀(success gating).
    success signal = normal_callback_collected OR result_json 존재 OR trigger가 성공 계열.
    기존 호출 시그니처/반환 키 하위호환 유지(기존 키 삭제 금지, 추가만).
    r   >   terminal_markerresult_collectedenvelope_confirmedTNFNO_SUCCESS_SIGNAL)wiredsuccess_gatedfiredreason)schemar   roundhead_shatriggerr"   r   canonical_root_sourceresult_json_existsresult_json_statusfollow_up_requiredprune_outcomesunwired
live_prunets_utcOKuC   RESULT_JSON_MISSING: task_id=%s round=%s head_sha=%s path=%s — %s)	r   r0   r1   r2   r    r!   r   r   r%   )r   r0   r1   r#   r   r   r%   )r+   r,   r-   )r   r   existsboolCOLLECTOR_ENTRYPOINT_SCHEMARESULT_JSON_MISSING_RECOVEREDloggerwarningr   r   r	   to_dict)r   r0   r1   r2   r   r   r   r   r    r!   r"   r#   r$   r%   r   root_sourcerjpr4   successr6   r5   outcomesr8   r9   os                            r   collect_and_prunerH   :   s   4 /~}MD+ #%#)	C 
 	&' 	T	TSS  w 2 )-.G)H"%0"&'9":"?"& !%-	 h)
 	
0 !:!QUHc+H	
 '%#
H G)))'
 o. x
(	J .%)*C%D!,"#56000891199;9)0GOO%d (  :s   E
)NN)r   Optional[str]r   rI   returntuple)
r   strr   rI   r   rI   r   rI   rJ   r   )r   rL   r0   intr1   rL   r2   rL   r   rI   r   rI   r   rI   r   rI   r!   r=   r"   r=   r#   r=   r$   r=   r%   zCallable[[], str]rJ   dict)__doc__
__future__r   loggingpathlibr   typingr   r   )utils.completion_callback_fallback_cancelr   r   r	   r
   utils.callback_envelope_schemar   r>   r?   	getLogger__name__r@   r   r   rH    r   r   <module>rY      sq   #   %  BR  L 			8	$ %)#'3!3 3 3& %)#'&*HH "H !	H
 $H 
H. %)#'&*#'&*  (DD D 	D
 D "D !D $D !D D  $D D D D  
!Dr   