
    4j7                        d Z ddlZddlmc mZ ddl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edefdZ edd	      Zeej2                  d
<   ej4                  Zej6                  Zej8                  Zej:                  Zej<                  Zej>                  Zej@                  Z ejB                  Z!ejD                  Z"ejF                  Z#ejH                  Z$ejJ                  Z%ejL                  Z& edd      Z'e'jP                  Z(e'jR                  Z)e'jT                  Z*e'jV                  Z+e'jX                  Z,e'jZ                  Z-ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z:m;Z;m<Z< edz  dz  dz  Z=edz  dz  edz  dz  edz  dz  edz  dz  gZ>edz  dz  Z?edz  dz  Z@edz  dz  dz  ZAdZBdZCdZDd ZEd!ZFd"ed#efd$ZGd%ed#efd&ZH G d' d(ej                        ZJeKd)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   modnamerelpathc                     t         j                  j                  | t        |z        }t         j                  j	                  |      }|t
        j                  | <   |j                  j                  |       |S )uE  Hermetic file-path import.

    pytest places tests/ on sys.path and a shadow package tests/dispatch/
    exists, so a bare `import dispatch.*` can resolve to the wrong package
    depending on collection order. Loading the real workspace files by
    explicit path is collision-proof and touches no shared config (§8).
    )		importlibutilspec_from_file_location_ROOTmodule_from_specsysmodulesloaderexec_module)r   r   specmods       ^/home/jay/workspace/tests/regression/test_executor_completion_callback_mandatory_2553plus32.py_loadr      sW     >>11'57?KD
..
)
)$
/CCKKKKC J    !_p32_executor_completion_contractz(dispatch/executor_completion_contract.pyz%dispatch.executor_completion_contract_p32_spec_template_validatorz#dispatch/spec_template_validator.py)
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    r   _shar7   y   s     >>!,,.)3355r   argsc                      t        j                  ddt        t              g| ddd      j                  j                         S )Ngit-CT)capture_outputtextcheck)
subprocessrunstrr   stdoutstrip)r8   s    r   _gitrD   }   s:    >>	c%j(4($d fUUWr   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    r   
setUpClassz.ExecutorCompletionCallbackMandatory.setUpClass   s     G--w-?@r   c                     d}| j                  t        |             | j                  t        |      j                  t
               y )Nux   executor 는 작업 완료 직후 ANU 에 normal completion callback cron 을 반드시 발사해야 한다 (mandatory).)
assertTruespec_has_normal_callback_clauseassertEqualvalidate_specverdictPASSselfr   s     r   test_01_spec_with_callback_passzCExecutorCompletionCallbackMandatory.test_01_spec_with_callback_pass   s<    B 	 	7=>t,44d;r   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     r   	<genexpr>zYExecutorCompletionCallbackMandatory.test_02_spec_missing_callback_fail.<locals>.<genexpr>   s     >qIN>   )	assertFalserT   rV   rU   rW   FAILrS   anyreasons)rZ   r   rs      r   "test_02_spec_missing_callback_failzFExecutorCompletionCallbackMandatory.test_02_spec_missing_callback_fail   sT    Y8>?$D)>AII>>?r   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).)assertInNO_CRON_CORRECTED_DEFINITIONrU   rV   rW   rX   rY   s     r   (test_03_no_cron_does_not_strip_mandatoryzLExecutorCompletionCallbackMandatory.test_03_no_cron_does_not_strip_mandatory   sH    k#?@+-IJ? 	 	t,44d;r   c           
          | j                  t                      | j                  t                      d}t        D ]>  }|j	                  d      }|D ]%  }| j                  |||j                   d|d       ' @ y )N)z--cronz--cron-removecokacdirr?   z	os.systemPopenzos.popenrH   rI   z  must not execute cron/process ())rd   r!   r    NEW_MODULESrN   assertNotInname)rZ   exec_vectorsmtxttoks        r   %test_04_registry_checkpoint_zero_cronzIExecutorCompletionCallbackMandatory.test_04_registry_checkpoint_zero_cron   s    
 	ACD9;<
  	A++w+/C#   vvh>sgQG	r   c                 h    | j                  t                      | j                  t                      y r2   )rd   0is_executor_completion_callback_a_cron_violationr   rZ   s    r   #test_05_callback_not_cron_violationzGExecutorCompletionCallbackMandatory.test_05_callback_not_cron_violation   s&    IKLKMNr   c                 h    | j                  t                      | j                  t                      y r2   )rS   r   rd   r#   r}   s    r   test_06_fallback_safety_pathz@ExecutorCompletionCallbackMandatory.test_06_fallback_safety_path   s$    /12ACDr   c                 h    | j                  t                      | j                  t                      y r2   )rS   r   rd   r    r}   s    r   #test_07_cancel_on_success_preservedzGExecutorCompletionCallbackMandatory.test_07_cancel_on_success_preserved   s$    FHI9;<r   c                 N    t        dddd      }| j                  |t               y )NTFdispatch_okresult_presentdone_presentnormal_callback_registered)classify_completionrU   RESULT_READY_NO_NORMAL_CALLBACK)rZ   cs     r   'test_08_result_ready_no_normal_callbackzKExecutorCompletionCallbackMandatory.test_08_result_ready_no_normal_callback   s)    T%
 	;<r   c                     | j                  t        t                     | j                  t	        t                     | j                  t	        t
                     y r2   )rS   is_recovery_stater   rd   is_accepted_normal_lifecycleNORMAL_COLLECTOR_COMPLETEDr}   s    r   test_09_recovery_not_completezAExecutorCompletionCallbackMandatory.test_09_recovery_not_complete   sC    )*IJK()HI	
 	()CD	
r   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)
rO   rd   rT   rU   rV   rW   re   r   tuple_is_validCallback4Tuple)rZ   ecr   obsts        r   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 	
r   c                     d}| j                  t        |             t        |      }| j                  t        |             | j	                  t        |      |       y )Nu   task-XXXX 를 수행하라.)rd   rT   !inject_completion_callback_clauserS   rU   )rZ   bareinjecteds      r   #test_11_prompt_auto_includes_clausezGExecutorCompletionCallbackMandatory.test_11_prompt_auto_includes_clause   sO    ,8>?4T:7AB-h7	
r   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_   r`   rh   s     r   rb   z_ExecutorCompletionCallbackMandatory.test_12_4tuple_requires_normal_collector.<locals>.<genexpr>	  s     IA2a7Irc   N)r   validate_4tuplerS   rf   r   )rZ   badrg   goods       r   (test_12_4tuple_requires_normal_collectorzLExecutorCompletionCallbackMandatory.test_12_4tuple_requires_normal_collector  sY    S#tS1!#&III	
 c3S1t,-r   c                     | j                  t                      | j                  t                      | j                  t	                      | j                  t               g        y r2   )rS   r   r   rd   r$   rU   r"   r}   s    r   test_13_checkpoint_not_primaryzBExecutorCompletionCallbackMandatory.test_13_checkpoint_not_primary  sH    +-.467BDEBDbIr   c                 6    | j                  t                      y r2   )rd   r#   r}   s    r   !test_14_checkpoint_keeps_fallbackzEExecutorCompletionCallbackMandatory.test_14_checkpoint_keeps_fallback  s    ACDr   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     r   rb   z_ExecutorCompletionCallbackMandatory.test_15_finalization_authority_forbidden.<locals>.<genexpr>   s     =ANa/=rc   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     r   rb   z_ExecutorCompletionCallbackMandatory.test_15_finalization_authority_forbidden.<locals>.<genexpr>/  s      9 @1D 9rc   )	r   r   rS   rf   rU   ExecutorCloseoutEvidencerd   closeout_is_validvalidate_closeout_evidence)rZ   r   okevs       r   (test_15_finalization_authority_forbiddenzLExecutorCompletionCallbackMandatory.test_15_finalization_authority_forbidden  s    *+A-148,
  	===>)*@-248+
  	R % $$27%)	
 	*2./ 93B79 9	
r   c                 ^    t        dd      }| j                  |j                  t               y )N F)spec_location_known)rV   rU   rW   HOLD)rZ   rh   s     r   $test_inv_spec_location_unknown_holdszHExecutorCompletionCallbackMandatory.test_inv_spec_location_unknown_holds4  s"    "%8D)r   c                     | j                  t        dd      t               | j                  t        ddd      t               y )Nz	rev-parseHEADz--abbrev-ref)rU   rD   GIT_HEAD_PREGIT_BRANCH_PREr}   s    r   test_inv_git_head_branch_equalzBExecutorCompletionCallbackMandatory.test_inv_git_head_branch_equal8  s5    k62LAnf5~	
r   c                     | j                  t        t              t               | j                  t        t              t
               | j                  t        t              t               y r2   )rU   r7   FROZEN_ANCHORFROZEN_ANCHOR_SHA
DURABLE_V1DURABLE_V1_SHAPLUS31_CKPTPLUS31_CKPT_SHAr}   s    r   -test_inv_frozen_anchor_durable_ckpt_unmutatedzQExecutorCompletionCallbackMandatory.test_inv_frozen_anchor_durable_ckpt_unmutated>  sB    m,.?@j)>:k*O<r   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))
rs   rM   r?   r@   rA   r   relative_to
returncodeassertNotEqualru   )rZ   rw   rcs      r   "test_inv_new_modules_git_untrackedzFExecutorCompletionCallbackMandatory.test_inv_new_modules_git_untrackedD  s~    y( 	Ac%j*6GQ]]5)*,#$ j	 
 A!&&!GH	r   c                 `    t               }| j                  d|       | j                  d|       y )Nrk   zNO-CRON)r   rl   )rZ   vs     r   test_inv_doctrine_verdictz=ExecutorCompletionCallbackMandatory.test_inv_doctrine_verdictO  s&    k1%i#r   N)__name__
__module____qualname__classmethodrQ   r[   ri   rn   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r_   r   r   rF   rF      s    A A<@<2O
E
==

0
.JE

2*
=	$r   rF   __main__)	verbosity)M__doc__builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr3   importlib.utilr   rK   r?   r   unittestpathlibr   __file__resolveparentsr   rA   pathinsertr   _eccr   r   r   rm   r   r   r   r   r   r|   r   r   r   r   _stvre   r   rX   r   rT   rV   !anu_v3.executor_callback_contractr   r   r   r   r   r   r   r   r    r!   2anu_v3.runtime_reconcile_checkpoint_recovery_layerr"   r#   r$   rM   rs   r   r   r   r   r   r   r   r   r7   rD   TestCaserF   r   mainr_   r   r   <module>r      s  
      
  X ((+u:SXXHHOOAs5z"3   '. 8<3 4$$88 #@@ "&"F"F !<< .. ** #@@ 99 1 ** $$&&!<< ") yyyyyy$($J$J !"&"F"F ""    
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  N$(*;*; N$b zHMMA r   