
    	j;0                        d Z ddlZddlZddlZddlZddlZddlmZ  ee      j                         j                  d   Z ee      ej                  vr"ej                  j                  d ee             ddlmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2 ed	z  d
z  dz  Z3edz  dz  edz  dz  edz  dz  edz  dz  gZ4edz  dz  Z5edz  dz  Z6ed	z  dz  dz  Z7dZ8dZ9dZ:dZ;dZ<dedefdZ=dedefd Z> G d! d"ej~                        Z@eAd#k(  r ej                  d$       yy)%uB  Regression — task-2553+32 EXECUTOR COMPLETION CALLBACK MANDATORY RULE 복원.

Covers §6 verbatim (1~15) + invariants (git HEAD/branch equal, frozen anchor /
durable v1 / +31 checkpoint original sha equal, NO-CRON token scan, additive
no-mutation). The executor normal completion callback is restored as a
MANDATORY lifecycle signal in executable code (§1/§4).

9-R.1: zero cron register/remove anywhere; cancel-on-success path only
asserted preserved (execute 0). 9-R.2: finalization-authority escalation
forbidden while executor self-task lifecycle outputs are REQUIRED.
    N)Path   )Callback4TupleExecutorCloseoutEvidenceNO_CRON_CORRECTED_DEFINITIONRESULT_READY_NO_NORMAL_CALLBACKNORMAL_COLLECTOR_COMPLETEDclassify_completioncloseout_is_validis_accepted_normal_lifecycle0is_executor_completion_callback_a_cron_violationis_recovery_statetuple_is_validvalidate_4tuplevalidate_closeout_evidence)FAILHOLDPASS!inject_completion_callback_clausespec_has_normal_callback_clausevalidate_spec)
callback_is_primary.cancel_on_success_applies_after_normal_successcheckpoint_is_recovery_layercloseout_authority_violationCloseoutAuthorityCheckdoctrine_verdict2executor_completion_callback_is_cron_add_violationfallback_is_safety_path plus32_executor_removes_any_cron(registry_is_primary_completion_mechanism))assert_checkpoint_is_recovery_not_primary(checkpoint_discards_fallback_safety_path)checkpoint_replaces_callback_primary_pathmemoryfixturesz2task-2553plus31.no-normal-callback-regression.jsondispatchzexecutor_completion_contract.pyzspec_template_validator.pyanu_v3zexecutor_callback_contract.pyz.runtime_reconcile_checkpoint_recovery_layer.pyzruntime_reconcile_checkpoint.pyutilsz%anu_delegation_completion_callback.pyeventsz#task-2553.parallel-batch-state.json(20456b5f83fc039f2fd6f50f4b94095c29b41bfbz%task/task-2553p1-f1-clean-replacement@83b3e307c8207c76a3e311c408aab4951373bd317896e51687d3007907b0c3d4@fe705d84274e8ae367aaa88c77df763b46bdf4c936efaa1dae78458aedd2a3bc@6018730f60bccd3b165b5b83b1f18a3281c71d4e06f3657a1ae6cbe29d1ef853preturnc                 d    t        j                  | j                               j                         S N)hashlibsha256
read_bytes	hexdigest)r/   s    ^/home/jay/workspace/tests/regression/test_executor_completion_callback_mandatory_2553plus32.py_shar8   ]   s     >>!,,.)3355    argsc                      t        j                  ddt        t              g| ddd      j                  j                         S )Ngit-CT)capture_outputtextcheck)
subprocessrunstr_ROOTstdoutstrip)r:   s    r7   _gitrG   a   s:    >>	c%j(4($d fUUWr9   c                       e Zd Zed        Zd Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)#ExecutorCompletionCallbackMandatoryc                 `    t        j                  t        j                  d            | _        y )Nutf-8encoding)jsonloadsFIXTURE	read_textfx)clss    r7   
setUpClassz.ExecutorCompletionCallbackMandatory.setUpClassi   s     G--w-?@r9   c                     d}| j                  t        |             | j                  t        |      j                  t
               y )Nux   executor 는 작업 완료 직후 ANU 에 normal completion callback cron 을 반드시 발사해야 한다 (mandatory).)
assertTruer   assertEqualr   verdictr   selfspecs     r7   test_01_spec_with_callback_passzCExecutorCompletionCallbackMandatory.test_01_spec_with_callback_passn   s<    B 	 	7=>t,44d;r9   c                     d}| j                  t        |             t        |      }| j                  |j                  t
               | j                  t        d |j                  D                     y )NuH   executor 는 작업을 수행하고 result.json 과 .done 을 남긴다.c              3   $   K   | ]  }d |v  
 yw)MISSINGN ).0xs     r7   	<genexpr>zYExecutorCompletionCallbackMandatory.test_02_spec_missing_callback_fail.<locals>.<genexpr>|   s     >qIN>   )	assertFalser   r   rW   rX   r   rV   anyreasons)rZ   r[   rs      r7   "test_02_spec_missing_callback_failzFExecutorCompletionCallbackMandatory.test_02_spec_missing_callback_failw   sT    Y8>?$D)>AII>>?r9   c                     | j                  dt               | j                  dt               d}| j                  t        |      j                  t
               y )N	MANDATORYzregistry/checkpointux   NO-CRON variant. 그래도 executor 는 완료 직후 normal completion callback 을 반드시 발사한다 (mandatory).)assertInr   rW   r   rX   r   rY   s     r7   (test_03_no_cron_does_not_strip_mandatoryzLExecutorCompletionCallbackMandatory.test_03_no_cron_does_not_strip_mandatory   sH    k#?@+-IJ? 	 	t,44d;r9   c                     | j                  t                      | j                  t                      t        D ];  }|j	                  d      }dD ]"  }| j                  |||j                   d       $ = y )NrK   rL   )z--cron zcron-removezcron-registercokacdirzsubprocess.run(["/usr/localz must not register/remove cron)re   r!   r    NEW_MODULESrQ   assertNotInname)rZ   mtxttoks       r7   %test_04_registry_checkpoint_zero_cronzIExecutorCompletionCallbackMandatory.test_04_registry_checkpoint_zero_cron   sx    ACD9;< 	A++w+/CD   (FG	r9   c                 h    | j                  t                      | j                  t                      y r2   )re   r   r   rZ   s    r7   #test_05_callback_not_cron_violationzGExecutorCompletionCallbackMandatory.test_05_callback_not_cron_violation   s&    IKLKMNr9   c                 h    | j                  t                      | j                  t                      y r2   )rV   r   re   r#   rx   s    r7   test_06_fallback_safety_pathz@ExecutorCompletionCallbackMandatory.test_06_fallback_safety_path   s$    /12ACDr9   c                 h    | j                  t                      | j                  t                      y r2   )rV   r   re   r    rx   s    r7   #test_07_cancel_on_success_preservedzGExecutorCompletionCallbackMandatory.test_07_cancel_on_success_preserved   s$    FHI9;<r9   c                 N    t        dddd      }| j                  |t               y )NTFdispatch_okresult_presentdone_presentnormal_callback_registered)r
   rW   r   )rZ   cs     r7   'test_08_result_ready_no_normal_callbackzKExecutorCompletionCallbackMandatory.test_08_result_ready_no_normal_callback   s)    T%
 	;<r9   c                     | j                  t        t                     | j                  t	        t                     | j                  t	        t
                     y r2   )rV   r   r   re   r   r	   rx   s    r7   test_09_recovery_not_completezAExecutorCompletionCallbackMandatory.test_09_recovery_not_complete   sC    )*IJK()HI	
 	()CD	
r9   c                    | j                   d   }|d   }| j                  t        |             | j                  t	        |      j
                  t               |d   }| j                  t        |d   |d   |d   |d         |d	          |d
   }| j                  t        t        |d   |d   |d   |d                      y )N
error_casespec_excerptobserved_runtimer   r   r   r   r   classification_expectedcallback_4tupletask_iddispatch_cron_idnormal_collector_cron_idfallback_callback_cron_id)
rR   re   r   rW   r   rX   r   r
   r   r   )rZ   ecr[   obsts        r7   test_10_plus31_error_reproducedzCExecutorCompletionCallbackMandatory.test_10_plus31_error_reproduced   s    WW\".!8>?t,44d;#$."#34 0+./K+L	 ()	
  !>)a 23,-q1L/M 	
r9   c                     d}| j                  t        |             t        |      }| j                  t        |             | j	                  t        |      |       y )Nu   task-XXXX 를 수행하라.)re   r   r   rV   rW   )rZ   bareinjecteds      r7   #test_11_prompt_auto_includes_clausezGExecutorCompletionCallbackMandatory.test_11_prompt_auto_includes_clause   sO    ,8>?4T:7AB-h7	
r9   c                     t        ddd d      }t        |      }| j                  t        d |D                     t        dddd      }| j                  t	        |             y )Nr   DFc              3   $   K   | ]  }d |v  
 yw)z normal_collector_cron_id missingNr`   ra   rh   s     r7   rc   z_ExecutorCompletionCallbackMandatory.test_12_4tuple_requires_normal_collector.<locals>.<genexpr>   s     IA2a7Ird   N)r   r   rV   rf   r   )rZ   badrg   goods       r7   (test_12_4tuple_requires_normal_collectorzLExecutorCompletionCallbackMandatory.test_12_4tuple_requires_normal_collector   sY    S#tS1!#&III	
 c3S1t,-r9   c                     | j                  t                      | j                  t                      | j                  t	                      | j                  t               g        y r2   )rV   r   r   re   r$   rW   r"   rx   s    r7   test_13_checkpoint_not_primaryzBExecutorCompletionCallbackMandatory.test_13_checkpoint_not_primary   sH    +-.467BDEBDbIr9   c                 6    | j                  t                      y r2   )re   r#   rx   s    r7   !test_14_checkpoint_keeps_fallbackzEExecutorCompletionCallbackMandatory.test_14_checkpoint_keeps_fallback   s    ACDr9   c                 j   t        t        dd            }| j                  t        d |D                     t        t        dd            }| j	                  |g        t        ddddd       }| j                  t        |             | j                  t        d t        |      D                     y )NT) finalization_authority_escalated'executor_self_lifecycle_outputs_presentc              3   $   K   | ]  }d |v  
 yw)finalizationNr`   r   s     r7   rc   z_ExecutorCompletionCallbackMandatory.test_15_finalization_authority_forbidden.<locals>.<genexpr>   s     =ANa/=rd   F)result_json_presentdone_marker_presentreport_present%normal_callback_registration_evidencer   c              3   $   K   | ]  }d |v  
 yw)z-normal callback registration evidence missingNr`   r   s     r7   rc   z_ExecutorCompletionCallbackMandatory.test_15_finalization_authority_forbidden.<locals>.<genexpr>  s      9 @1D 9rd   )	r   r   rV   rf   rW   r   re   r   r   )rZ   r   okevs       r7   (test_15_finalization_authority_forbiddenzLExecutorCompletionCallbackMandatory.test_15_finalization_authority_forbidden   s    *+A-148,
  	===>)*@-248+
  	R % $$27%)	
 	*2./ 93B79 9	
r9   c                 ^    t        dd      }| j                  |j                  t               y )N F)spec_location_known)r   rW   rX   r   )rZ   rh   s     r7   $test_inv_spec_location_unknown_holdszHExecutorCompletionCallbackMandatory.test_inv_spec_location_unknown_holds  s"    "%8D)r9   c                     | j                  t        dd      t               | j                  t        ddd      t               y )Nz	rev-parseHEADz--abbrev-ref)rW   rG   GIT_HEAD_PREGIT_BRANCH_PRErx   s    r7   test_inv_git_head_branch_equalzBExecutorCompletionCallbackMandatory.test_inv_git_head_branch_equal  s5    k62LAnf5~	
r9   c                     | j                  t        t              t               | j                  t        t              t
               | j                  t        t              t               y r2   )rW   r8   FROZEN_ANCHORFROZEN_ANCHOR_SHA
DURABLE_V1DURABLE_V1_SHAPLUS31_CKPTPLUS31_CKPT_SHArx   s    r7   -test_inv_frozen_anchor_durable_ckpt_unmutatedzQExecutorCompletionCallbackMandatory.test_inv_frozen_anchor_durable_ckpt_unmutated  sB    m,.?@j)>:k*O<r9   c                    t         t        gz   D ]s  }t        j                  ddt	        t
              ddt	        |j                  t
                    gdd      j                  }| j                  |d|j                   d       u y )	Nr<   r=   zls-filesz--error-unmatchT)r>   r?   r   u&    must be git-untracked (§8 invariant))
rp   rP   rA   rB   rC   rD   relative_to
returncodeassertNotEqualrr   )rZ   rs   rcs      r7   "test_inv_new_modules_git_untrackedzFExecutorCompletionCallbackMandatory.test_inv_new_modules_git_untracked  s~    y( 	Ac%j*6GQ]]5)*,#$ j	 
 A!&&!GH	r9   c                 `    t               }| j                  d|       | j                  d|       y )Nrk   zNO-CRON)r   rl   )rZ   vs     r7   test_inv_doctrine_verdictz=ExecutorCompletionCallbackMandatory.test_inv_doctrine_verdict&  s&    k1%i#r9   N)__name__
__module____qualname__classmethodrT   r\   ri   rm   rv   ry   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r`   r9   r7   rI   rI   h   s    A A<@<	O
E
==

0
.JE

2*
=	$r9   rI   __main__)	verbosity)C__doc__r3   rN   rA   sysunittestpathlibr   __file__resolveparentsrD   rC   pathinsert%dispatch.executor_completion_contractr   r   r   r   r	   r
   r   r   r   r   r   r   r    dispatch.spec_template_validatorr   r   r   r   r   r   !anu_v3.executor_callback_contractr   r   r   r   r   r   r   r   r    r!   2anu_v3.runtime_reconcile_checkpoint_recovery_layerr"   r#   r$   rP   rp   r   r   r   r   r   r   r   r   r8   rG   TestCaserI   r   mainr`   r9   r7   <module>r      s  
    
  X ((+u:SXXHHOOAs5z"        
Hz!:; 
 
J::	J55	H66	HGG	 h!BB"IIX(+PP
 :8F  G  G 
6D 6S 6  A$(*;*; A$H zHMMA r9   