
    4j`)                        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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j4                  d
<    edd      Zeej4                  d<    edd      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$ddl%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+m,Z,m-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  dz  Z0edz  dz  Z1edz  dz  Z2dZ3d Z4d!Z5d"Z6d#Z7d$Z8edz  d%z  d&z  Z9edz  dz  d'z  Z:d(ed)efd*Z;d+ed)efd,Z< G d- d.ejz                        Z>e?d/k(  r ej                  d0       yy)1uG  Regression — task-2553+46 canonical artifact-root resolver + collector
lookup.

Covers §5 verbatim items 10~12, 16, 19, 20 + §5/§8/9-R invariants. The
normal/fallback/dead-man collector no longer judges artifact-missing on an
autoset-cwd false negative: the canonical ANU workspace root
(/home/jay/workspace) is hard-coded and re-checked FIRST (회장 §2.2/§3.C).

9-R.1 Layer A: artifact_root_resolver / collector_artifact_lookup are
read-only — ZERO write/cron/dispatch. 9-R.2/§5.20: the runtime checkpoint
stays a recovery layer, never the primary callback replacement.
    N)Path   modnamerelpathc                     t         j                  j                  | t        |z        }t         j                  j	                  |      }|t
        j                  | <   |j                  j                  |       |S N)		importlibutilspec_from_file_location_ROOTmodule_from_specsysmodulesloaderexec_module)r   r   specmods       N/home/jay/workspace/tests/regression/test_artifact_root_resolver_2553plus46.py_loadr      sU    >>11'57?KD
..
)
)$
/CCKKKKC J    _p46_arrz anu_v3/artifact_root_resolver.pyzanu_v3.artifact_root_resolver_p46_regz"anu_v3/callback_4tuple_registry.pyzanu_v3.callback_4tuple_registry_p46_calz#anu_v3/collector_artifact_lookup.py))assert_checkpoint_is_recovery_not_primary(checkpoint_discards_fallback_safety_path)checkpoint_replaces_callback_primary_path)callback_is_primary.cancel_on_success_applies_after_normal_successcheckpoint_is_recovery_layerfallback_is_safety_pathanu_v3artifact_root_resolver.pycollector_artifact_lookup.pyzcallback_4tuple_registry.pydispatchzcron_dispatch_guard.pyutilsz%anu_delegation_completion_callback.pymemoryeventsz#task-2553.parallel-batch-state.jsonzpolicy_profile_engine.pyzparallel_batch_coordinator.py(20456b5f83fc039f2fd6f50f4b94095c29b41bfbz%task/task-2553p1-f1-clean-replacement@83b3e307c8207c76a3e311c408aab4951373bd317896e51687d3007907b0c3d4@fe705d84274e8ae367aaa88c77df763b46bdf4c936efaa1dae78458aedd2a3bc@2363e291a0a43884892f5e554f115481a077322bd5caa3000fb75bf5b72bc6be@10529421110b3d2765785b6cf911527c8f5e964b5078fcfa6190fcb86d0f2c0ffixturesz0task-2553plus39.artifact-root-false-missing.jsontask-2553+39.result.jsonpreturnc                 d    t        j                  | j                               j                         S r   )hashlibsha256
read_bytes	hexdigest)r/   s    r   _shar6   ^   s     >>!,,.)3355r   ac                      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)r7   s    r   _gitrC   b   s:    >>	c%j%1%$d fUUWr   c                   d    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y)ArtifactRootResolverc                 `    t        j                  t        j                  d            | _        y )Nutf-8encoding)jsonloadsFX39	read_textfx39)clss    r   
setUpClasszArtifactRootResolver.setUpClassj   s    ::dnngn>?r   c                     | j                  t        t                     d       | j                  t        t              d       y )N/home/jay/workspace)assertEqualr@   canonical_rootCANONICAL_ANU_WORKSPACE_ROOTselfs    r   test_00_canonical_root_fixedz1ArtifactRootResolver.test_00_canonical_root_fixedo   s5    ^-.0EF,-/D	
r   c                    | j                  t        j                         d       t        j                         5 }t        ddt        |      d      }d d d        | j                  j                  | j                  d   d          | j                  d   d   D ]  }| j                  |j                  |         | j                  |j                  d	       | j                  |j                         y # 1 sw Y   xY w)
Nz-precondition: canonical +39 result must existtask-2553+39
6937032012r.   task_idchat_idautoset_cwdresult_basenamecaseverdict_expected_one_ofverdict_forbiddenrR   )
assertTruePLUS39_RESULTis_filetempfileTemporaryDirectoryclassifyr   assertInverdictrN   assertNotEqualrS   rT   autoset_only_miss_blocked)rW   autosetrforbs       r    test_10_plus39_canonical_presentz5ArtifactRootResolver.test_10_plus39_canonical_presentw   s    !!#;	
 ((* 	g&$ M :	A	 	IIIIf78	
 IIf%&9: 	1D		40	1))+@A334	 	s   C11C:c                    t        j                         5 }t        ddt        |      d      }d d d        | j	                  j
                  t               | j                  |j                         t        t        t        j                                     }| j                  |j                  d   d       y # 1 sw Y   xY w)NrZ   r[   r.   r\   )r_   r   rR   )rg   rh   ri   r   assertNotInrk    FORBIDDEN_WHEN_CANONICAL_PRESENTrd   rm   resolve_roots
gettempdirrS   search_order)rW   rn   ro   rootss       r    test_11_autoset_only_not_missingz5ArtifactRootResolver.test_11_autoset_only_not_missing   s    ((* 	g&$ M :	A	 	$DE334$x/B/B/D*EF++A.0EF	 	s   B88Cc                    | j                   d   }t        j                         5 }t        j                         5 }t        |d   dt	        |      |d   t	        |            }d d d        d d d        | j                  j                  t               | j                  |j                         | j                  |j                         y # 1 sw Y   hxY w# 1 sw Y   lxY w)Ntrue_missing_caser]   r[   dispatch_staler]   r^   r_   r|   schedule_history_dir)rN   rg   rh   ri   r   rS   rk   RESULT_MISSINGassertFalseschedule_history_seenrd   r|   )rW   tmrn   empty_shro   s        r   test_12_true_missing_bot_stalez3ArtifactRootResolver.test_12_true_missing_bot_stale   s    YY*+((* 	g++-	199$ M!"23%)(^A	 	 	N3001(()	 	 	 	s"   C)C	"C	C	CCc                 d   t        j                         5 }t        j                         5 }t        ddt        |      dt        |            }d d d        d d d        | j	                  j
                  t               | j                  |j
                  d       y # 1 sw Y   NxY w# 1 sw Y   RxY w)Nztask-2553+NONEXISTENT-YYYr[   Fr}   PENDING_FAILSAFE)rg   rh   ri   r   rl   rk   r   rS   rW   rn   r   ro   s       r   "test_12b_failsafe_no_false_missingz7ArtifactRootResolver.test_12b_failsafe_no_false_missing   s    ((* 	g++-	193$ M$%)(^A	 	 	AII~6$67	 	 	 	"   B&#BB&B#	B&&B/c                 d   t        j                         5 }t        j                         5 }t        ddt        |      dt        |            }d d d        d d d        | j	                  j
                  t               | j                  |j                  d       y # 1 sw Y   NxY w# 1 sw Y   RxY w)Nztask-2553+UNRELATEDr[   Fr}   NO_LEDGER_RECORD)	rg   rh   ri   r   rl   rk   NORMAL_COLLECTOR_COMPLETEDrS   registry_verdictr   s       r   $test_16_unrelated_callback_not_citedz9ArtifactRootResolver.test_16_unrelated_callback_not_cited   s    ((* 	g++-	19-$ M$%)(^A	 	 	AII'AB++-?@	 	 	 	r   c                     | j                  t                      | j                  t                      | j                  t                      y r   )rd   r   r    r   rV   s    r   test_19_structure_preservedz0ArtifactRootResolver.test_19_structure_preserved   s2    +-./12FHIr   c                     | j                  t                      | j                  t                      | j                  t	                      | j                  t               g        y r   )rd   r   r   r   r   rS   r   rV   s    r   'test_20_checkpoint_recovery_not_primaryz<ArtifactRootResolver.test_20_checkpoint_recovery_not_primary   sJ    467BDEACDBDbIr   c                     | j                  t        dd      t               | j                  t        ddd      t               y )Nz	rev-parseHEADz--abbrev-ref)rS   rC   GIT_HEAD_PREGIT_BRANCH_PRErV   s    r   test_inv_git_head_branch_equalz3ArtifactRootResolver.test_inv_git_head_branch_equal   s5    k62LAnf5~	
r   c                    | j                  t        t              t               | j                  t        t              t
               | j                  t        t              t               | j                  t        t              t               y r   )
rS   r6   FROZEN_ANCHORFROZEN_ANCHOR_SHA
DURABLE_V1DURABLE_V1_SHAPPEPPE_SHAPBCPBC_SHArV   s    r   (test_inv_frozen_anchor_durable_unmutatedz=ArtifactRootResolver.test_inv_frozen_anchor_durable_unmutated   sU    m,.?@j)>:cG,cG,r   c                     t         D ]s  }t        j                  ddt        t              ddt        |j                  t                    gdd      j                  }| j                  |d|j                   d       u y )	Nr9   r:   zls-filesz--error-unmatchT)r;   r<   r   z must be git-untracked)	NEW_MODULESr>   r?   r@   r   relative_to
returncoderl   name)rW   mrcs      r   "test_inv_new_modules_git_untrackedz7ArtifactRootResolver.test_inv_new_modules_git_untracked   sv     	JAc%j*6GQ]]5)*,#$ j	 
 A!&&1G'HI	Jr   c           	          d}t         dz  dz  t         dz  dz  fD ]=  }|j                  d      }|D ]$  }| j                  |||j                   d|       & ? y )N)zimport subprocesszsubprocess.runzsubprocess.Popenz
os.system(z	os.popen(z.Popen(z"--cronz'--cronzopen(z.write(z.write_text(z.mkdir(z"a"z'a'z"w"z'w'r!   r"   r#   rG   rH   z (Layer A read-only) must not )r   rM   rs   r   )rW   forbidden_syntaxr   txttoks        r   "test_inv_layer_a_read_only_no_cronz7ArtifactRootResolver.test_inv_layer_a_read_only_no_cron   s    
 H::H==
 		A ++w+/C'   vvh<SGD		r   N)__name__
__module____qualname__classmethodrP   rX   rq   ry   r   r   r   r   r   r   r   r   r    r   r   rE   rE   i   sW    @ @
5.G* 8AJJ
-Jr   rE   __main__)	verbosity)A__doc__builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr2   importlib.utilr	   rJ   r>   r   rg   unittestpathlibr   __file__resolveparentsr   r@   pathinsertr   _arrr   _reg_calru   rT   rU   ri   RESULT_PRESENTr   r   rt   2anu_v3.runtime_reconcile_checkpoint_recovery_layerr   r   r   !anu_v3.executor_callback_contractr   r   r   r    r   r   r   r   r   r   r   r   r   r   r   rL   re   r6   rC   TestCaserE   r   mainr   r   r   <module>r      sn        
   X ((+u:SXXHHOOAs5z"3   Z;</3+ ,Z=>15- .Z>?""$$#@@ ==$$$$!<< #'#H#H   
  
H22	H55	H44	J11	 "IIX(+PP
h33h8898F  G  M
L 
Hz!89   8+.HH6D 6S 6S S U8,, Up zHMMA r   