
    4j/                    f   d Z ddlm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mZ ddlmZ ddlmZ ddlmZ ddlmZ  ee      j9                         j:                  d   Zedz  dz  dz  Zed	z  d
z  Z g dZ!dZ"ddZ# G d dejH                        Z%e&dk(  r ejN                  d       yy)u)  Regression — task-2553+42 STEP 2 profile engine operational adoption planner.

증명 대상 (회장 §3.4 / 9-R.1 Layer A):
  - plan 산출 정상 (expected_files allowlist / conflict / risk tier)
  - seam(+38) · binding(+39) · engine(C1) byte-0 (planner 사용 전후 hash EQUAL)
  - callback / collector 경로 무수정 (frozen 83b3e307 byte-0, source 무접촉)
  - 실 write · merge · cron · PR 0 / dry-run 부작용 0 (applied=0)
  - frozen anchor byte-0 / git tracked HEAD·branch 전후 EQUAL
  - emit hard-guard: frozen/tracked write REFUSE

Layer A 한정 (9-R.1): executor 의 §8 normal completion callback 발사는
검증 대상 아님 (별개 lifecycle 신호). 본 테스트는 산출 모듈의 callback/
collector 소스 무수정 · 실 adoption 0 · dry-run 부작용 0 만 증명.
    )annotationsN)Path   memoryfixturesztask-2553+42.jsonschemasz!profile_adoption_plan.schema.json)zanu_v3/policy_profile_engine.pyz$anu_v3/dispatch_profile_selection.pyz%anu_v3/coordinator_profile_binding.pyz$anu_v3/parallel_batch_coordinator.py+utils/anu_delegation_completion_callback.py@83b3e307c8207c76a3e311c408aab4951373bd317896e51687d3007907b0c3d4c                d    t        j                  | j                               j                         S N)hashlibsha256
read_bytes	hexdigest)ps    P/home/jay/workspace/tests/regression/test_profile_adoption_planner_2553plus42.py_shar   .   s     >>!,,.)3355    c                      e Zd ZddZddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZddZddZddZddZddZddZddZddZy) ProfileAdoptionPlanner2553Plus42c                   t        j                  t        j                  d            | _        | j                  d   | _        t        D ci c],  }t        |z  j                         r|t        t        |z        . c}| _
        y c c}w )Nutf-8encodingexpected_invariants)jsonloadsFIXTURE	read_textfixtureinv_BYTE0REPOexistsr   _base_hashes)selfrels     r   setUpz&ProfileAdoptionPlanner2553Plus42.setUp3   sq    zz'"3"3W"3"EF<< 56 
s
""$ dSj!!
 
s   1Bc                   t        j                         }| j                  |d   t         j                         | j                  |d   d       | j                  |d   d       | j                  |d   d       | j	                  t        |d         | j                  d	          |d   D cg c]  }|d
   	 }}| j                  || j                  d          y c c}w )Nplan_schemataskztask-2553+42adoption_lifecycle_effectnonereal_in_place_adoption_countr   touchpointsmin_touchpointstouchpoint_idexpected_touchpoint_ids)plannerbuild_adoption_planassertEqualPLAN_SCHEMAassertGreaterEquallenr!   )r&   plantidss       r   test_plan_built_and_well_formedz@ProfileAdoptionPlanner2553Plus42.test_plan_built_and_well_formed=   s    **,m,g.A.ABf~69:FC<=qA]#$dhh/@&A	
 ,0+>?aq!??dhh'@AB @s   )Cc                h    t        j                         }|d   }dD ]  }| j                  ||        y )Nexpected_files_allowlist)"anu_v3/profile_adoption_planner.pyz)schemas/profile_adoption_plan.schema.jsonz<tests/regression/test_profile_adoption_planner_2553plus42.pyz-memory/events/task-2553+42.adoption-plan.jsonzmemory/reports/task-2553+42.md)r3   r4   assertIn)r&   r9   allowrequireds       r   *test_expected_files_allowlist_matches_speczKProfileAdoptionPlanner2553Plus42.test_expected_files_allowlist_matches_specI   s<    **,/0
 	+H MM(E*	+r   c                    t        j                         }|d   }| j                  |d   | j                  d   d   d          |d   D cg c]  }|d   	 }}| j	                  | j
                  d   |       y c c}w )	Nconflictconflict_countcasesr   expect_conflict_count_minfrozen_anchor_conflicts	candidateexpected_blocked_frozen_route)r3   r4   r7   r    r@   r!   )r&   r9   confccandss        r   3test_conflict_set_flags_frozen_parallel_coordinatorzTProfileAdoptionPlanner2553Plus42.test_conflict_set_flags_frozen_parallel_coordinatorU   s    **,J!"DLL$9!$<=X$Y	
 *..G)HIA;IIdhh>?G Js   	A7c                   t        j                         }| j                  |d   d   | j                  d          |d   d   }| j                  |d   d       | j                  |d   d       | j                  |d	   d
       y )Nriskoverall_tieroverall_risk_tierper_touchpointzTE.engine_decision_emitLOWzTA.dispatch_selection_wireMEDzTB.coordinator_binding_consumeHIGH)r3   r4   r5   r!   )r&   r9   pers      r   test_risk_tier_overall_highz<ProfileAdoptionPlanner2553Plus42.test_risk_tier_overall_high^   s    **,L($((3F*G	
 6l+,67?9:EB=>Gr   c                L   t        j                  t        j                  d            }t	        j
                         }| j                  t        j                  ||      g        t	        j                  |      }| j                  t        j                  ||d   d         g        y )Nr   r   definitionsdry_run)
r   r   SCHEMAr   r3   r4   r5   ppevalidate_against_metadry_run_adoption)r&   metar9   drys       r   &test_schema_validates_plan_and_dry_runzGProfileAdoptionPlanner2553Plus42.test_schema_validates_plan_and_dry_runh   s    zz&**G*<=**,224>C&&t,%%c4+>y+IJB	
r   c                &   t        j                         }| j                  |d   d       | j                  |d   d       | j                  |d   d       | j                  |d   d       | j                  |d   d       | j                  |d   d       | j                  |d	          | j                  |d
          |d   D ]  }| j                  |d           |d   D cg c]  }|d   	 }}| j	                  | j
                  d   |       y c c}w )NmodeREAD_ONLY_SIMULATIONapplied_countr   writesmergescron_opspr_opscallback_collector_touchedfrozen_anchor_touchedsimulated_diffsappliedblocked_frozen_routesrJ   rK   )r3   r`   r5   assertFalser@   r!   )r&   rb   dbblockeds        r   test_dry_run_zero_side_effectsz?ProfileAdoptionPlanner2553Plus42.test_dry_run_zero_side_effectsr   s   &&(V&<=_-q1X*X*Z!,X*9:;456&' 	+AQy\*	++./F+GHa1[>HHdhh>?I Is    Dc                |    t        j                         }| j                  |d          | j                  |d          y )Nreal_in_place_adoptioncallback_collector_untouched)r3   run_adoption_plannerrq   
assertTrue)r&   rs   s     r   test_bundle_entrypoint_purez<ProfileAdoptionPlanner2553Plus42.test_bundle_entrypoint_pure   s6    ((*34589:r   c                    t        j                          t        j                          | j                  j	                         D ]+  \  }}| j                  t        t        |z        |d|        - y )Nu   byte-0 깨짐: )r3   ry   introspect_seamsr%   itemsr5   r   r#   )r&   r'   bases      r   .test_byte0_anchors_unchanged_after_planner_usezOProfileAdoptionPlanner2553Plus42.test_byte0_anchors_unchanged_after_planner_use   s`    $$&  "**002 	NICT$*-tse5LM	Nr   c                Z    t         dz  dz  }| j                  t        |      t               y )Nutilsz%anu_delegation_completion_callback.py)r#   r5   r   _CALLBACK_FROZEN_SHA)r&   cbs     r   test_callback_frozen_anchor_shaz@ProfileAdoptionPlanner2553Plus42.test_callback_frozen_anchor_sha   s&    G^EEb#78r   c                    t         dz  dz  j                  d      }| j                  d|       | j                  d|       | j                  d|j                  d      d	   j                  d
      d	          y )Nanu_v3zprofile_adoption_planner.pyr   r   z/import utils.anu_delegation_completion_callbackz)anu_delegation_completion_callback importr   zdef emit_adoption_planr   zdef load_planner_fixture)r#   r   assertNotInsplit)r&   srcs     r   9test_planner_does_not_reference_callback_collector_sourcezZProfileAdoptionPlanner2553Plus42.test_planner_does_not_reference_callback_collector_source   s}    h!>>II J 
 	JCPDcJcii0H&I!&L% :;A'? 	@r   c                   t        j                  ddt        t              ddgdd      j                  j                         }t        j                  ddt        t              ddgdd      j                  j                         }| j                  |d	       | j                  |d
       t        j                  ddt        t              ddgdd      j                  j                         }| j                  |d       y )Ngit-Cz	rev-parseHEADTcapture_outputtextbranchz--show-current(20456b5f83fc039f2fd6f50f4b94095c29b41bfbz%task/task-2553p1-f1-clean-replacementls-filesr?    )
subprocessrunstrr#   stdoutstripr5   )r&   headr   trackeds       r   "test_git_head_and_branch_unchangedzCProfileAdoptionPlanner2553Plus42.test_git_head_and_branch_unchanged   s    ~~D#d)[&9d
 & 	 D#d)X/?@d
 & 	 	IJ!HI..D#d)Z13d
 &	 	
 	"%r   c                    | j                  t        j                        5  t        j                  i d       d d d        y # 1 sw Y   y xY w)Nz#task-2553.parallel-batch-state.jsonassertRaisesr3   FrozenWriteRefusedemit_adoption_planr&   s    r   #test_emit_refuses_frozen_durable_v1zDProfileAdoptionPlanner2553Plus42.test_emit_refuses_frozen_durable_v1   s@    w99: 	R&&r+PQ	R 	R 	R   A  A	c                    | j                  t        j                        5  t        j                  i d       d d d        y # 1 sw Y   y xY w)Nzpolicy_profile_engine.pyr   r   s    r   (test_emit_refuses_frozen_anchor_basenamezIProfileAdoptionPlanner2553Plus42.test_emit_refuses_frozen_anchor_basename   s@    w99: 	G&&r+EF	G 	G 	Gr   c                4   t        j                  ddt        t              dgdd      j                  j                         }|rI| j                  t        j                        5  t        j                  i t        |d   z         d d d        y y # 1 sw Y   y xY w)Nr   r   r   Tr   r   )
r   r   r   r#   r   
splitlinesr   r3   r   r   )r&   r   s     r   "test_emit_refuses_git_tracked_pathzCProfileAdoptionPlanner2553Plus42.test_emit_refuses_git_tracked_path   s    ..D#d)Z0d
 & 	 ""7#=#=> B**2tgaj/@AB B B Bs   #!BBc                d   t         j                  D ]  }| j                  t         j                        5  t        j                  i |       d d d        | j                  t         j                        5  t        j                  i t
        |z         d d d         y # 1 sw Y   QxY w# 1 sw Y   xY wr   )r3   CALLBACK_COLLECTOR_PATHSr   r   r   r#   )r&   ccs     r   )test_emit_refuses_callback_collector_pathzJProfileAdoptionPlanner2553Plus42.test_emit_refuses_callback_collector_path   s    22 	:B""7#=#=> 3**2r23""7#=#=> :**2tby9: :	:3 3: :s   B1B&B#	&B/	c                    t        j                  ddddddgddddt         j                  d	
      }t        j                  |g      }| j	                  |d   d       y )NzSYN.dupXmerM   r	   kTrr1   trackseam_moduleseam_entrypointconsumeslive_target_candidatesadoption_kindin_place_edit_requiredtouches_frozen_anchortouches_callback_collector	risk_tier	rationalerF      )r3   AdoptionTouchpoint	RISK_HIGHclassify_conflictsr5   )r&   tprL   s      r   +test_conflict_count_deduped_no_double_countzLProfileAdoptionPlanner2553Plus42.test_conflict_count_deduped_no_double_count   so     ''#=$ #'"&'+''
  ))2$/./3r   c                   t        j                  ddddddgddd	dt         j                  d
      }t        j                         }|j	                         g|d<   t        j
                  |      }| j                  |d          |d   D cg c]  }|d   	 }}| j                  d|       |d   D ]+  }| j                  d|d          | j                  |d          - y c c}w )NzSYN.cbr   r   r   rM   z$anu_v3/executor_callback_contract.pyr   TFr   r   r/   rl   blocked_callback_routesrJ   rn   executor_callback_contractchangero   )	r3   r   r   r4   to_dictr`   rq   r@   r   )r&   r   r9   rb   rs   
cb_blockedrr   s          r   1test_dry_run_callback_route_blocked_not_simulatedzRProfileAdoptionPlanner2553Plus42.test_dry_run_callback_route_blocked_not_simulated   s    ''"$J#K#'"''+''
 **,!zz|n]&&t,9:;.12K.LManM
M<jI&' 	+A91X;GQy\*	+ Ns   	Cc                \   t        j                         }| j                  |d   d   t        j                         | j                  |d   d   t        j
                         | j                  |d   d   t        j                         | j                  |d   d   t        j                         y )Ndispatch_seammodulelifecycle_effectcoordinator_binding_seambinding_schemaengineversion)
r3   r}   r5   dpsSEAM_MODULEDISPATCH_LIFECYCLE_EFFECTcpbBINDING_SCHEMAr^   ENGINE_VERSION)r&   intros     r   )test_introspection_mirrors_seam_contractszJProfileAdoptionPlanner2553Plus42.test_introspection_mirrors_seam_contracts   s    ((*/93??K/"#56))	
 	,-.>?	
 	x3S5G5GHr   N)returnNone)__name__
__module____qualname__r(   r<   rC   rO   rY   rc   ru   r{   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   2   sk    

C
+HH
J;N9@&(RGB:4,+6Ir   r   __main__)	verbosity)r   r   r   r   )(__doc__
__future__r   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr   r   r   unittestpathlibr   "anu_v3.coordinator_profile_bindingcoordinator_profile_bindingr   !anu_v3.dispatch_profile_selectiondispatch_profile_selectionr   anu_v3.policy_profile_enginepolicy_profile_enginer^   anu_v3.profile_adoption_plannerprofile_adoption_plannerr3   __file__resolveparentsr#   r   r]   r"   r   r   TestCaser   r   mainr   r   r   <module>r      s    #        0 / * 1H~''*
/J
&)<
<			?	?
 G 
6TIx'8'8 TIn zHMMA r   