
    4j7                    j   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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$ edz  dz  Z%e%d	z  Z&e%d
z  Z'e%dz  Z(e%dz  Z)edz  dz  Z*e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  edz  dz  dZ,e&e'e(e)gZ-ddZ.d Z/ G d dej`                        Z1e2dk(  r ejf                  d       yy)u  Regression — task-2553+39 TRACK B: batch coordinator ← profile decision output 소비.

§3.3 verbatim 매핑 + invariant extras (byte-0 / zero-import-coupling /
NO-CRON / emission-guard / no-mutation / git-ref / schema-conformance):

  1  정상 소비: valid decision.v1 → coordinator-consumable binding 산출
  2  engine 부재 → fail-closed safe (DECISION_UNAVAILABLE, 자동확정 0)
  3  engine schema mismatch → fail-closed safe (자동확정 0, settle 불가)
  4  engine status HOLD_FOR_CHAIR → HOLD 전파 (자동확정 0)
  5  런타임 HOLD trigger 관측 → RUNTIME_HOLD_OBSERVED (자동확정 0)
  6  coordinator closeout/merge 자동확정 0 — 모든 입력에서 hard-pinned False
  7  engine/coordinator(+29/+30/frozen) byte-0 — binding 소비로 무변
  8  +29/+30 public API 무회귀
  9  zero import coupling — binding 모듈에 anu_v3 import 0 (순수 stdlib)
  10 NO-CRON — cron/cokacdir 토큰 0
  11 emission hard-guard — frozen/git-tracked/무관 untracked clobber 0
  12 소비한 decision 입력 무mutation
  13 decision-logic 파일 I/O side effect 0
  14 binding output schema 적합
  15 live /home/jay/workspace git tracked HEAD/branch/ref 전후 assertEqual
    )annotationsN)Path   )ACCEPTED_DECISION_SCHEMABINDING_SCHEMACONSUME_HOLD_ENGINECONSUME_HOLD_RUNTIME
CONSUME_OKCONSUME_UNAVAILABLECoordinatorProfileBindingCoordinatorProfileBindingErrorFrozenWriteRefusedconsume_for_coordinatoremit_bindingload_binding_fixtureload_profile_decisionmemoryfixturesz+task-2553+39.profile-decision-resolved.jsonz'task-2553+39.profile-decision-hold.jsonz'task-2553+39.runtime-hold-observed.jsonz*task-2553+39.decision-schema-mismatch.jsonschemasz'coordinator_profile_binding.schema.jsonanu_v3zcoordinator_profile_binding.pypolicy_profile_engine.pyzparallel_batch_coordinator.pyzgeneric_batch_coordinator.pyzparallel_runtime_registry.pyutilsz%anu_delegation_completion_callback.py)engineparallel_batch_coordinatorgeneric_batch_coordinatorparallel_runtime_registrycallback_anchorc                d    t        j                  | j                               j                         S N)hashlibsha256
read_bytes	hexdigest)ps    S/home/jay/workspace/tests/regression/test_coordinator_profile_binding_2553plus39.py_shar&   L   s     >>!,,.)3355    c                 R   t         dz  } | dz  j                  d      j                         }|j                  d      r|j	                  dd      d   n|}|j                  d      r| |z  nd }|r3|j                         r#| |z  j                  d      j                         n|}|||fS )Nz.gitHEADutf-8encodingzref:zref:    )_ROOT	read_textstrip
startswithsplitexists)git_dirhead_txtbranchref_pathshas        r%   _git_refr9   P   s    fnG& ++W+=CCEH v& 	w"1% 
 $,#6#6v#>wDH ) 
6	$$g$6<<> 
 fc""r'   c                  r    e 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)#CoordinatorProfileBindingRegressionc                ,   t         j                         D ci c]!  \  }}|j                         r|t        |      # c}}| _        t
        D ci c]  }|j                         s|t        |      ! c}| _        t               | _        y c c}}w c c}w r   )	FROZENitemsis_filer&   _frozen_preRO_ARTIFACTS_ro_prer9   _git_preselfkr$   s      r%   setUpz)CoordinatorProfileBindingRegression.setUpb   sj    39<<> ,41a yy{ tAwJ ,,8HqAIIK47
H 
,Hs   &BB%Bc           
        | j                  t        j                         D ci c]"  \  }}|j                         s|t	        |      $ c}}| j
                  d       | j                  t        D ci c]  }|j                         s|t	        |      ! c}| j                  d       | j                  t               | j                  d       y c c}}w c c}w )Nu-   engine/coordinator/anchor byte-0 위반 (§5)u%   read-only fixture mutated (§3.2/§5)u@   live /home/jay/workspace git HEAD/branch/ref 변경 (§5 위반))
assertEqualr=   r>   r?   r&   r@   rA   rB   r9   rC   rD   s      r%   tearDownz,CoordinatorProfileBindingRegression.tearDownh   s    $*LLNBDAqaiikQQZB;	

 	!-=AQQZ=LL3	

 	JN	
 C
 >s   C
C
1CCc                "   t        t              }t        |j                  |j                        }| j                  |d   t               | j                  |d   t               |d   }| j                  t        |d         d       | j                  t        |d         d       | j                  |d          | j                  |d	          | j                  |d
          | j                  |d          | j                  |d   d   t               y )Nruntime_signalsbinding_schemaconsumed_decision_schematrack_consumption_viewgate_condition_names   hold_trigger_conditionsallowed_actionsforbidden_actionscompletion_packet_meta_refevidence_meta_refconsumption_decisionsignal)r   FX_RESOLVEDr   decisionrM   rI   r   r   len
assertTrueassertIsNotNoner
   )rE   fxbindingviews       r%   test_01_normal_consumptionz>CoordinatorProfileBindingRegression.test_01_normal_consumptiony   s   !+.)KK););
 	!12NC!;<1	3/0T"89:A>T";<=qA./0012T">?@T"567!78B#	%r'   c                j   t         dz  dz  dz  }| j                  |j                                t        |      }| j	                  |d          | j                  |d   d   t               | j                  |d   d       | j                  t              5  t        |       d d d        y # 1 sw Y   y xY w)	Nr   r   ztask-2553+39.__nope__.jsonfail_closedrX   rY   
error_codedecision_source_unreadable)
r.   assertFalser3   r   r]   rI   r   assertRaisesr   r   )rE   missingr`   s      r%   !test_02_engine_absent_fail_closedzECoordinatorProfileBindingRegression.test_02_engine_absent_fail_closed   s    ("Z/2NN)*)'2./!78B,	..0LM=> 	+!'*	+ 	+ 	+s   B))B2c                X   t        t              }t        |j                        }| j	                  |d          | j                  |d   d       | j                  |d   d   t               |d   }| j                  |d          | j                  |d          | j                  |d          y )	Nrd   re   decision_schema_mismatchrX   rY   auto_confirmcloseout_authoritymerge_authority)r   FX_MISMATCHr   r[   r]   rI   r   rg   rE   r_   r`   cds       r%   #test_03_schema_mismatch_fail_closedzGCoordinatorProfileBindingRegression.test_03_schema_mismatch_fail_closed   s    !+.)"++6./.0JK!78B,	. +,N+,012-./r'   c                   t        t              }t        |j                        }|d   }| j	                  |d   t
               | j                  |d   d          | j                  |d   d          | j                  |d          y )NrX   rY   holdengine_status_holdrm   )r   FX_HOLDr   r[   rI   r   r]   rg   rq   s       r%   test_04_engine_hold_propagatedzBCoordinatorProfileBindingRegression.test_04_engine_hold_propagated   sx    !'*)"++6+,H':;6
6*+6
#789N+,r'   c                    t        t              }t        |j                  |j                        }|d   }| j                  |d   t               | j                  |d   d          | j                  |d          y )NrL   rX   rY   ru   runtime_fired_triggersrm   )	r   FX_RUNTIME_HOLDr   r[   rM   rI   r	   r]   rg   rq   s       r%   test_05_runtime_hold_observedzACoordinatorProfileBindingRegression.test_05_runtime_hold_observed   sp    !/2)KK););
 +,H';<6
#;<=N+,r'   c                H   t         t        t        t        fD ]  }t	        |      }t        |j                  |j                        }|d   }|d   }| j                  |d   d|j                         ||fD ]_  }| j                  |d   |j                         | j                  |d   |j                         | j                  |d   |j                         a | j                  |d	   d
        t	        t               }t        |j                        }| j                  |j                         d       |j                         }|j                         }| j                  |d   t               dD ]4  }| j                  ||   d| d       | j                  ||   d| d       6 y )NrL   rP   rX   coordinator_roledecision_consumer_onlyrn   ro   rm   	authorityjudgment_assist_onlyRESOLVEDrY   )rn   ro   rm   zview.z must be hard-pinned Falsez	decision.)rZ   rw   r{   rp   r   r   r[   rM   rI   namerg   r   engine_statusrP   rX   r
   )	rE   srcr_   bra   rr   flag_holdercpbrF   s	            r%   !test_06_no_auto_confirm_any_inputzECoordinatorProfileBindingRegression.test_06_no_auto_confirm_any_input   s   /;G 	FC%c*B'R-?-?A -.D)*BT"455sxxA $bz H  -A!BCHHM  ->!?J  ^!<chhGH R_.DE	F  "+.'4**,j9))+%%'Hz2J 	OAT!WaS0J&KLRUis2L$MN	Or'   c                r    t         D ].  }t        |      }t        |j                  |j                         0 y )NrL   )rA   r   r   r[   rM   )rE   r   r_   s      r%   &test_07_consume_does_not_mutate_frozenzJCoordinatorProfileBindingRegression.test_07_consume_does_not_mutate_frozen   s2     	C%c*B#R-?-?	r'   c                   dd l m} dd lm} dD ]!  }| j	                  t        ||      d|        # dD ]!  }| j	                  t        ||      d|        # |j                  j                  }| j	                  t        |             y )Nr   )ParallelRuntimeRegistryTaskRuntimeRecordu   +29 API 회귀: )GenericBatchCoordinatorGenericBatchPlanGenericTrackPlanemit_generic_batch_stateload_plan_from_fixtureu   +30 API 회귀: )	 anu_v3.parallel_runtime_registryr    anu_v3.generic_batch_coordinatorr   r]   hasattrr   closeout_proposalcallable)rE   reggbcsymprop_defaults        r%   test_08_2930_api_intactz;CoordinatorProfileBindingRegression.test_08_2930_api_intact   s    66C 	ICOOGC-1A#/GH	I. 	IC OOGC-1A#/GH	I
 22DD./r'   c                    t         j                  d      }| j                  d|d       | j                  d|d       dd lm} | j                  |j                  d       y )Nr*   r+   zimport anu_v3u"   import 결합 0 위반 (§3.1/§5)zfrom anu_v3r   z"anu_v3.coordinator_profile_binding)BINDING_MODULE_SRCr/   assertNotIn"anu_v3.coordinator_profile_bindingcoordinator_profile_bindingrI   __name__)rE   r   ms      r%   test_09_zero_import_couplingz@CoordinatorProfileBindingRegression.test_09_zero_import_coupling   sY     **G*<#=	?=	? 	7%IJr'   c                j    t         j                  d      }dD ]  }| j                  ||d|        y )Nr*   r+   )z--cronzcron-removezcron-registerz--cron-removecokacdiru   NO-CRON 위반: )r   r/   r   )rE   r   toks      r%   test_10_no_cron_tokensz:CoordinatorProfileBindingRegression.test_10_no_cron_tokens   sB     **G*<1 	CCS#)9#'AB	Cr'   c                "   t        t              }t        |j                        }| j	                  t
              5  t        |t        dz  dz  dz         d d d        | j	                  t
              5  t        |t        dz  dz         d d d        t        j                         5 }t        |      dz  }|j                  dd	       | j	                  t
              5  t        ||       d d d        | j                  |j                  d	      d       t        |      d
z  }t        ||      }| j                  |j                                t        ||       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   eventsz#task-2553.parallel-batch-state.jsonr   r   zunrelated.txtORIGINALr*   r+   z"x.coordinator-profile-binding.json)r   rZ   r   r[   rh   r   r   r.   tempfileTemporaryDirectoryr   
write_textrI   r/   r]   r?   )rE   r_   r`   dvictimoutr$   s          r%   test_11_emission_guardz:CoordinatorProfileBindingRegression.test_11_emission_guard  so   !+.)"++612 	B%("2X"=@#A B	B 12 	7%("25#6 7	7 ((* 
	'a!W.Fj7;""#56 .Wf-.V--w-?Lq'@@CWc*AOOAIIK(#&
	' 
	'	B 	B	7 	7. .
	' 
	's<   E 1E-$7FE9(A/F E*-E69F	>FFc                |   t        t              }t        j                  |j                  d      }t        |j                        }|j                  |j                         |j                          |j                  |j                         | j                  t        j                  |j                  d      |       y )NT)	sort_keys)r   rZ   jsondumpsr[   r   build_bindingrM   rP   rX   rI   )rE   r_   snapr   s       r%   "test_12_input_decision_not_mutatedzFCoordinatorProfileBindingRegression.test_12_input_decision_not_mutated  s    !+.zz"++6'4",,-""$  !3!34BKK4@$Gr'   c                   t        t              }t        j                         5 }t	        t        |      j                               }t        |j                        }|j                          |j                          |j                          |j                          t        |j                         | j                  t	        t        |      j                               |       d d d        y # 1 sw Y   y xY wr   )r   rZ   r   r   setr   iterdirr   r[   r   rP   rX   evaluate_holdr   rI   )rE   r_   r   beforer   s        r%   test_13_decision_logic_no_ioz@CoordinatorProfileBindingRegression.test_13_decision_logic_no_io%  s    !+.((* 	=aa*+F+BKK8C&&($$&#BKK0Sa!23V<	= 	= 	=s   B?C,,C5c                   dd l }t        j                  t        j	                  d            }t
        t        t        t        fD ]@  }t        |      }t        |j                  |j                        }|j                  ||       B y )Nr   r*   r+   rL   )
jsonschemar   loadsSCHEMA_FILEr/   rZ   rw   r{   rp   r   r   r[   rM   validate)rE   r   schemar   r_   r   s         r%   test_14_schema_conformancez>CoordinatorProfileBindingRegression.test_14_schema_conformance2  sn    K1171CD/;G 	+C%c*B'R-?-?A 6*	+r'   c                    t        t              }t        |j                        }t	        j
                         5 }t        |t        |      dz         d d d        y # 1 sw Y   y xY w)Nz"t.coordinator-profile-binding.json)r   rZ   r   r[   r   r   r   r   )rE   r_   r   r   s       r%   $test_15_git_ref_invariant_under_emitzHCoordinatorProfileBindingRegression.test_15_git_ref_invariant_under_emit=  sU    !+.#BKK0((* 	LaDG&JJK	L 	L 	Ls   AA$N)r   
__module____qualname__rG   rJ   rb   rj   rs   rx   r|   r   r   r   r   r   r   r   r   r   r    r'   r%   r;   r;   a   s\    #
"%(
+0--O80KC'2H
=+Lr'   r;   __main__)	verbosity)r$   r   returnstr)4__doc__
__future__r   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr    r   sysr   unittestpathlibr   __file__resolveparentsr.   r   pathinsertr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   FXrZ   rw   r{   rp   r   r   r=   rA   r&   r9   TestCaser;   r   mainr   r'   r%   <module>r      s~  * #     
   X ((+u:SXXHHOOAs5z"     X
"@@
8
8@@??i"KKX%(HH  h!;;"'("2%#&!&!1$"%!&!1$"%w-.

 Wo{C6#"`L(*;*; `LH zHMMA r'   