
    3jL                       d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZmZ ddlmZmZ ddlmZ dZd	Zd
ZdZdZdZdZdZdZdZdZ e h d      Z!dZ"e G d d             Z#d dZ$d!dZ%	 	 	 	 d"dZ&ddd	 	 	 	 	 	 	 	 	 d#dZ'g dZ(y)$u	  anu_v3.proposal_authorization_gate — classify a +54 runtime-event-loop
proposal candidate and decide the single bounded action the enactor is
permitted to take (task-2553+55 §2.2~2.7 / §8).

회장 §2 verbatim 2~7:

  2. proposal type 구분: consolidated_summary · closeout_candidate ·
     next_dispatch_candidate · hold_packet.
  3. 권한 있는 action만 enact.
  4. 권한 없는 action은 proposal 상태로 유지.
  5. closeout은 additive artifact로만 enact.
  6. next dispatch는 ANU key + owner/key guard + callback mandatory contract
     통과 시에만 candidate->actual dispatch 승격.
  7. merge/PR/credential/branch write 기본 금지.

This gate is the *authority boundary*. It NEVER enacts; it only classifies
and authorizes. Every decision routes through the +49 single sources of
truth (no rule re-implementation):

  * ``dispatch.callback_owner_enforcer.is_anu_key`` — ANU-key ownership.
  * ``anu_v3.callback_owner_validator.validate_callback_owner_runtime`` —
    callback owner/key/role fail-closed contract (§2.6).
  * ``anu_v3.self_collector_guard.guard_self_collector_session`` /
    ``anu_v3.authoritative_verdict_selector.select_authoritative_verdict``
    — self-chain / non-ANU collector permanent quarantine (§8).
  * ``anu_v3.executor_callback_contract`` — the callback-mandatory doctrine
    (a next-dispatch candidate with no mandatory ANU callback is NOT
    promotable; §2.6).

Track boundary (회장 §3 / §8):

  * consolidated_summary / closeout_candidate -> ``ENACT_ADDITIVE`` (Track
    A: additive artifact ONLY — zero merge/PR/branch/write to any existing
    file).
  * next_dispatch_candidate -> ``DISPATCH_READY`` *iff* the §2.6 triple
    passes — but Track B is **verify-only**: dispatch-ready is NOT
    execution; the actual candidate->dispatch promotion requires an
    explicit chair grant (``allow_actual_dispatch``) which this task never
    sets. Otherwise ``PROPOSAL_ONLY``.
  * hold_packet (self-chain / non-ANU / mismatch) -> ``HOLD_ROUTED`` (Track
    C: routing/safety-gate verification only).
  * any merge / PR / credential / OWNER_PAT / branch-write target ->
    ``BLOCKED`` (stays a proposal; 회장 §6/§7).

Layer A / NO-CRON: pure classification. ZERO cron / dispatch / subprocess /
cokacdir / merge / PR / branch / credential.
    )annotations)	dataclassfield)DictListOptionalSequence)AUTHORITATIVE_PASSCOLLECTOR_ROLE_ANUVerdictRecordselect_authoritative_verdict)
is_anu_keyvalidate_callback_owner_runtime)guard_self_collector_sessionz%anu_v3.proposal_authorization_gate.v1consolidated_summarycloseout_candidatenext_dispatch_candidatehold_packetunknown_proposalENACT_ADDITIVEDISPATCH_READYPROPOSAL_ONLYBLOCKEDHOLD_ROUTED>   branch-writeprpatpushmergesecretgit_push	owner_pat
credential
main_writepullrequestbranch_writepull_request)requires_credentialrequires_branch_writerequires_mergerequires_prrequires_owner_patrequires_main_writec                      e Zd ZU ded<   ded<   ded<   ded<   ded<   ded<   ded	<   ded
<   ded<   ded<   ded<   ded<   ded<    ee      Zded<   ddZy)AuthorizationDecisionstrschemaproposal_typeactionbool
authorizedenact_as_additivedispatch_readyblockedsource	source_idzOptional[Dict[str, object]]owner_validationselector
self_guardcallback_contract_ok)default_factoryz	List[str]reasonsc                N   | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  t        | j                        dS )Nr1   r2   r3   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r@   )r1   r2   r3   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   listr@   )selfs    9/home/jay/workspace/anu_v3/proposal_authorization_gate.pyto_jsonzAuthorizationDecision.to_json   s}    kk!//kk//!%!7!7"11||kk $ 5 5//$($=$=DLL)
 	
    N)returnDict[str, object])__name__
__module____qualname____annotations__r   rC   r@   rF    rG   rE   r/   r/   s   s_    KKMKN11))++t4GY4
rG   r/   c                   t        | j                  d      xs d      }t        | j                  d      xs d      }d|v s|dk(  rt        S | j                  d      s| j                  d      rt        S d|v s| j                  d	      r]| j                  d	      t	        t
              r6d
j                  fddD              j                         }d|v sd|v rt        S t        S t        S )u  Map a +54 candidate dict onto one of the §2.2 proposal types.

    * a consolidated-summary candidate schema / status -> consolidated_summary
    * a quarantined self-chain / non-ANU / mismatch packet -> hold_packet
    * a dispatch candidate whose declared next_phase is a *closeout* ->
      closeout_candidate (Track A target)
    * any other dispatch candidate -> next_dispatch_candidate
    r1    statusr   $CONSOLIDATED_SUMMARY_CANDIDATE_READY
event_kindr   dispatch_candidate
next_phase c              3  Z   K   | ]"  }t        j                  |      xs d        $ yw)rP   N)r0   get).0knps     rE   	<genexpr>z$classify_proposal.<locals>.<genexpr>   s*       BFF1IO$s   (+)phasekindintentr3   closeoutzconsolidated closeout)r0   rX   PROPOSAL_CONSOLIDATED_SUMMARYPROPOSAL_HOLD_PACKET
isinstancedictjoinlowerPROPOSAL_CLOSEOUT_CANDIDATE PROPOSAL_NEXT_DISPATCH_CANDIDATEPROPOSAL_UNKNOWN)	candidater1   rQ   blobr[   s       @rE   classify_proposalrl      s     x(.B/Fx(.B/F'6., -,}}\"immM&B##v%|)D]]<(b$88 >  eg  T!%<%D22//rG   c           	     x   | j                  d      }t        |t              r|ni }t        D ]:  }t	        | j                  |            st	        |j                  |            s8|c S  | j                  d      }t        |t
        t        f      r|rddj                  d |D               S dD ]  }t        |j                  |      xs d      j                         }t        |j                  dd	      j                  d
d	      j                  d	            }|t        z  }|sud| d|dt        |       c S  y)uv   Detect a structurally forbidden enact target (§2.7/§6).

    Only structured fields are inspected (never note text).rU   forbidden_actionszforbidden_actions:,c              3  2   K   | ]  }t        |        y w)N)r0   )rY   xs     rE   r\   z$_forbidden_target.<locals>.<genexpr>   s     ,@SV,@s   )r^   r3   targetr_   	operationrP   /_-znext_phase.=z -> N)rX   rc   rd   FORBIDDEN_REQUIREMENT_FLAGSr4   rC   tuplere   r0   rf   setreplacesplitFORBIDDEN_ACTION_TOKENSsorted)rj   r[   flagfakeyvaltokenshits           rE   _forbidden_targetr      s"    
|	$B"d#B+ 	d#$RVVD\(:K 
*	+B"tUm$#CHH,@R,@$@#ABBB @"&&+#$**,S[[c*223<BB3GH.. QsgT&+??@ rG   c                0   t        t        t        t        ddddt	        | j                  d      xs | j                  d      xs d      t	        | j                  d      xs( | j                  d      xs | j                  d      xs d      d d d dd	g
      S )NFschema_kindr9   
quarantinetask_idr:   event_idrP   u   hold_packet (self-chain / non-ANU / registry-mismatch) — Track C routing/safety-gate verification ONLY; never enacted, never dispatched, never auto-resolved (회장 §3.C/§8). Routed to the independent-ANU HOLD lane.rB   )r/   GATE_SCHEMArb   ACTION_HOLD_ROUTEDr0   rX   )rj   s    rE   _hold_packet_decisionr      s     !*!9==/Z9==3JZl[MM)$ }}[)}}Z( 	
 ")
' rG   rP   F)executor_self_keyallow_actual_dispatchc               
   t        |       }t        | j                  d      xs | j                  d      xs d      }t        | j                  d      xs( | j                  d      xs | j                  d      xs d      }|t        k(  rt	        |       S t        |       }|&t        t        |t        dddd	||ddddd
| dg      S |t        t        fv rt        | j                  d            }t        | j                  d            }	| j                  d      }
t        |
t              r<|xs t        |
j                  d            }|	xs t        |
j                  d            }	|r$|	r"t        t        |t        d	d	dd||dddd	dg      S t        t        |t        dddd||dddddg      S | j                  d      }t        |t              r|ni }| j                  d      }t        |t              r|ni }t        |j                  d      xs( |j                  d      xs |j                  d      xs d      }| j                  d      }t        |t         t"        f      r|r|D cg c]  }t        |       c}n
t!        |      }|rt%        ||      nd}t        |j                  d      xs d      }t        |j                  d      xs |      }t        |j                  d      xs t&              }t        |      xr |||fv }t)        |r|n|xs d|xs d||rd	nd      }t+        t-        dd||xs d|xs ||t        |      xr ||k(  dd 	      g||!      }t        |j                  d"      xr |j                  d#      d	u       }d}|r|rt/        |xs d$||xs |||t        |j                  d"      xs d      xs dt        |j                  d%      xs d      xs dt        |j                  d&      xs d'      t        |j                  d(      xs d      |t        |j                  d)            *      j1                         }t        |xr |j                  d+            }t        |j2                        }|j4                  t6        k(  xr |j8                  d,k\  }|xr |xr
 |xr |xr |}|sRt        t        t:        t        dddd||||j1                         |j1                         |d-| d.| d/| d0| d1| d2g      S t        t        t:        t<        d	dd	d||||j1                         |j1                         d	d3| d4g      S c c}w )5u  Decide the single bounded action for one proposal candidate.

    ``allow_actual_dispatch`` is the *explicit chair grant* for an actual
    candidate->dispatch promotion. task-2553+55 NEVER sets it (Track B is
    verify-only); even when a dispatch candidate passes the full §2.6
    triple, the action is DISPATCH_READY (proposal stays a proposal until a
    future chair-granted session).
    r9   r1   rP   r:   batch_idr   NFTz!forbidden enact target detected (u   ) — merge / PR / credential / OWNER_PAT / branch-write is BLOCKED by default; the proposal is retained as a proposal and never enacted (회장 §2.7/§6/§7).rB   all_settledall_authoritative_passgate_evidenceu   all-settled & all-AUTHORITATIVE_PASS consolidated/closeout proposal — AUTHORIZED for an ADDITIVE artifact enact ONLY (Track A, 회장 §2.5/§3.A/§8: zero merge/PR/branch, zero modification of any existing artifact).u   consolidated/closeout proposal is NOT all-settled & all-AUTHORITATIVE_PASS — unauthorized for enact; retained as a proposal (회장 §2.4).rU   callback_contractcollector_owner_keycollector_key	owner_keyanu_keysexecutor_keycollector_roleunknown_executor)r   r   r   is_executor_self_sessioncollector_resultPASSindependent_anunext_dispatch_candidate_gate)	r^   verdictr   r   r   r   session_is_executor_selfclaimed_origindetail)r   r   normal_collector_cron_idcallback_mandatoryznext-dispatch-candidatefallback_callback_cron_iddispatch_cron_idzDG-nextchat_idno_fallback)r   r   r   r   r   r   r   r   r   r   r   registration_allowed   uE   next_dispatch_candidate did NOT pass the §2.6 triple (anu_key_owner=z, owner_validation=z, self_guard=z, authoritative_selector=z, callback_mandatory_contract=u   ) — UNAUTHORIZED; retained as a proposal, zero dispatch (회장 §2.4/§2.6/§8). self-chain / non-ANU collector is permanently rejected via the +49 selector/guard.u'  next_dispatch_candidate PASSED the §2.6 triple (ANU-key owner + +49 owner/key guard + callback-mandatory contract) — DISPATCH-READY. Track B is verify-only: NO actual dispatch is executed in this task; the candidate->dispatch promotion requires an explicit chair grant (allow_actual_dispatch=uI   ); not set here (회장 §3.B/§8/§10 — 자동 실 dispatch 승격 0).)rl   r0   rX   rb   r   r   r/   r   ACTION_BLOCKEDra   rg   r4   rc   rd   ACTION_ENACT_ADDITIVEACTION_PROPOSAL_ONLYrC   ry   r   r   r   r   r   r   rF   okclassificationr
   independent_anu_countrh   ACTION_DISPATCH_READY)rj   r   r   r   ptyper9   r:   forbr   all_apgate_evr[   ccr   raw_anurZ   declared_anu_keysanu_owner_okr   r   r   self_key_taintguardselcontract_presentovowner_okguard_oksel_ok	triple_oks                                 rE   authorize_proposalr      s!    i(Ex(IIMM(,CIrJFk" 	==$	==#	 	I $$$Y// Y'D$!# !!&3D6 :2 2
 	
, %#  9==78imm$<=>--0gt$%Igkk-.H)IKJtGKK0H$IJF6("#,"&$#!%%)K * %'# !!&0
 	
. 
|	$B"d#B	*	+B"d#B
$% 	66/"	66+	 	I mmJ'G ge}-' !!AQ!(^ 
 @I:i):;eLrvvn-34L/<9=M 01G5GHN
 +, 1BG 2N )  4"4#+t%)7T	E ''!)?-?+8y-)-m)< *2!\105	
 "!C$ 
)* 	1FF'(D0 
B\,:!:%'49 ))%(128b& & &)239r' '  (:!;!HyIy)/R0&RVVM23#
$ ')% 	( B9266"89:HEHH~H00 	+%%* 
 	 			 	   $:'# [[]}}!1""./B*M( 489:J9K LCC
 	
8 !6$==?!&
 '<%< =HH
 e 	"s   *U)r   ra   rg   rh   rb   ri   r   r   r   r   r   r}   r/   rl   r   N)rj   rI   rH   r0   )rj   rI   rH   zOptional[str])rj   rI   rH   r/   )
rj   rI   r   zSequence[str]r   r0   r   r4   rH   r/   ))__doc__
__future__r   dataclassesr   r   typingr   r   r   r	   %anu_v3.authoritative_verdict_selectorr
   r   r   r   anu_v3.callback_owner_validatorr   r   anu_v3.self_collector_guardr   r   ra   rg   rh   rb   ri   r   r   r   r   r   	frozensetr}   rx   r/   rl   r   r   r   __all__rN   rG   rE   <module>r      s  .^ # ( 1 1  6 !7 2 #<  $ %  ) ( & "  $ "   
  
  
H<* F  "'@ @ @ 	@
  @ @FrG   