
    j                        d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 dZdZdZ eh d	      Z G d
 de      ZddZddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZy)u  anu_v3.runtime_batch_state_updater — versioned/additive batch_state PROPOSAL.

task-2553+31 ANU_RUNTIME_RECONCILE_CHECKPOINT leaf module (구현목표 14·15).

9-R.1 HARD BOUNDARY:
  "batch_state 자동 갱신" == a single NEW versioned/additive batch_state
  PROPOSAL artifact only:
      memory/events/task-2553.runtime-reconcile-checkpoint.batch-state.json
  The frozen durable v1 (memory/events/task-2553.parallel-batch-state.json)
  and ANY pre-existing batch-state-class file are read-only inputs:
  write/overwrite is ABSOLUTELY ZERO.

The decision/derivation surface (build_proposal) is pure and writes nothing.
Emission (emit_runtime_batch_state) is a SEPARATE, hard-guarded I/O function
outside the decision boundary that refuses every forbidden path.
    )annotationsN)Path)DictListOptionalz2anu_v3.runtime_reconcile_checkpoint.batch_state.v1z4batch-task-2553-runtime-reconcile-checkpoint-2553p31zEmemory/events/task-2553.runtime-reconcile-checkpoint.batch-state.json>   0memory/events/task-2553.generic-batch-state.json1memory/events/task-2553.parallel-batch-state.jsonBmemory/events/task-2553.parallel-runtime-registry.batch-state.jsonc                      e Zd ZdZy)FrozenWriteRefusedzERaised when emission targets a frozen / non-allowlisted path (9-R.1).N)__name__
__module____qualname____doc__     9/home/jay/workspace/anu_v3/runtime_batch_state_updater.pyr   r   )   s    Or   r   c                v    t        j                  t        |       j                               j	                         S N)hashlibsha256r   
read_bytes	hexdigest)paths    r   _sha256r   -   s'    >>$t*//12<<>>r   )contaminationprior_versionc               X   t        |xs g       }h d| j                         D 	ci c]  \  }}	||	j                  d       }
}}	t        |       xr" t	        fd|
j                         D              }t        d |
j                         D              }t        d | j                         D              }|s|rd}n	|r|sd}nd}|xr | xr | }|d	d
d|rdndd}i dt        dddt        d|dz   dd
ddd|d|d|d|ddd|d| d |d!|d"|d#d$d%d&d'd(d)iS c c}	}w )*u   Pure builder — derives the additive PROPOSAL doc. Writes NOTHING.

    `prior_version` lets callers chain versions additively (regression 13);
    the emitted doc is versioned and never overwrites durable v1.
    >   TRACK_MISMATCHNO_CRON_TASK_DONEDUPLICATE_CALLBACK_IGNOREDNORMAL_COLLECTOR_COMPLETEDRESULT_READY_NO_NORMAL_CALLBACKclassificationc              3  &   K   | ]  }|v  
 y wr   r   ).0cterminal_statess     r   	<genexpr>z!build_proposal.<locals>.<genexpr>K   s      /!"_/s   c              3  &   K   | ]	  }|d k(    yw)r   Nr   )r&   r'   s     r   r)   z!build_proposal.<locals>.<genexpr>N   s      "#s   c              3  `   K   | ]&  }|j                  d i       j                  dd       ( yw)next_actionblockingFN)get)r&   recs     r   r)   z!build_proposal.<locals>.<genexpr>Q   s0       	r"&&z59s   ,.RECOMMEND_HOLD_FOR_CHAIRRECOMMEND_BATCH_ACCEPTRECOMMEND_CONTINUE_LOOPz4runtime_reconcile_checkpoint.batch_state (read-only)Fu&   CHAIR (§7/§9 closeout 확정 금지)ut   all tracks terminal & no mismatch/contamination — closeout PROPOSED only; chair confirmation still required (§7).uT   not all tracks terminal or mismatch/contamination present — closeout NOT eligible.)eligiblederived_from	confirmedauthority_requiredreasonschemaartifact_kindADDITIVE_VERSIONED_PROPOSALbatch_idversion   supersedes_durable_v1durable_v1_writez(ZERO (read-only provenance input, 9-R.1)source_fixturesource_fixture_sha256frozen_batch_state_v1_reffrozen_batch_state_v1_sha256frozen_couplingz9read-only provenance only; zero mutation/coupling (9-R.1)generated_ts_ksttracksr   batch_next_actioncloseout_proposalwrite_authorityu{   PROPOSAL artifact only — checkpoint performs zero write to durable v1 / coordinator / any existing batch-state (§7/§10)callback_paths_statusu'   documented & NOT disabled (§2, reg 17)u'   documented & NOT disabled (§2, reg 18)u$   compatible & unchanged (§2, reg 19))primary_callback_pathfallback_safety_pathcancel_on_success)	listitemsr.   boolallvaluesanySCHEMABATCH_ID)track_recordsr@   rA   frozen_v1_reffrozen_v1_sha256rE   r   r   tidr/   classificationsall_terminalany_mismatchblocking_anyrG   closeout_eligiblerH   r(   s                    @r   build_proposalr_   1   s!     ,"-MO 8E7J7J7L+33SWW%&&O  & 3 /&5&<&<&>/ ,L  '6'='='? L   '') L
 }6	l45 %O\)9O->O%NF !F%&6 	H 	=1$	
 	  	F 	. 	 !6 	$] 	'(8 	G 	, 	-  	!" 	.#$ 	.%& 	M). 	 %N$M!G"
/ Ks   D&c                   t        |      j                         }t        |      j                         }	 |j                  |      j                         }|t        v rt        d| d      |t        k7  rt        d| dt         d      |j                  j                  dd       t        j                  | d	d
      }|j                  |dz   d       t        |      S # t        $ r}t        d|       |d}~ww xY w)ui  Hard-guarded emitter — the ONLY function here that touches disk.

    Refuses (FrozenWriteRefused) unless `out_path` resolves to exactly
    ``<repo_root>/memory/events/task-2553.runtime-reconcile-checkpoint
    .batch-state.json`` (9-R.1 allowlist). Never overwrites durable v1 or
    any other batch-state-class file. Returns the written file's sha256.
    zemit path escapes repo_root: Nz	refused: z- is a frozen/durable batch-state file (9-R.1)z$ not the allowlisted PROPOSAL path (z	) (9-R.1)T)parentsexist_okF   )ensure_asciiindent
zutf-8)encoding)r   resolverelative_toas_posix
ValueErrorr   FORBIDDEN_EMIT_RELPATHSALLOWED_EMIT_RELPATHparentmkdirjsondumps
write_textr   )docout_path	repo_rootrelexcpayloads         r   emit_runtime_batch_statery      s    Y'')IH~%%'H""9-668 %% uIJ
 	
 "" u $%Y0
 	

 OO$6jj5;G$98%   +H:6
	s   C 	C<(C77C<)r   r   returnstr)rV   zDict[str, dict]r@   r{   rA   r{   rW   r{   rX   r{   rE   r{   r   zOptional[List[dict]]r   intrz   Dict[str, object])rs   r}   rt   r   ru   r   rz   r{   )r   
__future__r   r   rp   pathlibr   typingr   r   r   rT   rU   rm   	frozensetrl   RuntimeErrorr   r   r_   ry   r   r   r   <module>r      s     #    ' '	=A L 
 $ %  P P? +/X"X X 	X
 X X X (X X Xv"	"" " 		"r   