
    7q]j[#              
         d dl 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m	Z	  e	e
                                          j        d         Zedz  Zg dZg dZd!dZd"dZd#dZd$dZd%dZd&dZedk    rw	  ej         e                       dS # e$ rTZ e ej        dd ee          j         de dd          ej                     ej        d           Y dZ[dS dZ[ww xY wdS )'    )annotationsNPath   zscript/project-manifest.json)story_brief
charactersledgeroutlineidentity_registryreveal_ledger))	authorityauthority_receiptF)	candidatecandidate_receiptT)
voice_testvoice_test_receiptT)semanticsemantic_receiptT)originalityoriginality_receiptTpathr   returnstrc                   t          j                    }|                     d          5 t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                S )Nrbc                 .                          d          S )Ni   )read)handles   script/promote-story.py<lambda>zsha256.<locals>.<lambda>   s    &++o">">     r!   )hashlibsha256openiterupdate	hexdigest)r   digestchunkr   s      @r   r#   r#      s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'dictc                R    t          j        |                     d                    S )Nutf-8)encoding)jsonloads	read_text)r   s    r   loadr1       s     :dnngn66777r!   manifestkeyc                   |                      d          pi                      |          }t          |t                    r|st          d| d          t	          |          }|                                s
t          |z  }	 |                                                    t                                                     n%# t          $ r}t          d|           |d }~ww xY w|S )Nactive_pathszactive_paths.z is missingzactive path escapes project: )
get
isinstancer   RuntimeErrorr   is_absoluteROOTresolverelative_to
ValueError)r2   r3   valuer   excs        r   resolve_activer@   $   s    \\.))/R44S99EeS!! = =;3;;;<<<;;D d{K""4<<>>2222 K K K@3@@AAsJKKs   >C 
C$CC$databytesNonec                   | j                             dd           t          j        d| j         d| j                   \  }}	 t          j        |d          5 }|                    |           |                                 t          j	        |
                                           d d d            n# 1 swxY w Y   t          j        ||            t
          j                            |          rt          j        |           d S d S # t
          j                            |          rt          j        |           w w xY w)NT)parentsexist_ok.)prefixdirwb)parentmkdirtempfilemkstempnameosfdopenwriteflushfsyncfilenoreplacer   existsunlink)r   rA   fdrO   r   s        r   atomic_bytesrZ   2   sX   KdT222'749'7'7'7T[IIIHBYr4   	&FLLLLNNNHV]]__%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	
47>>$ 	IdOOOOO	 	27>>$ 	IdOOOO	s1   D AB6*D 6B::D =B:>D 6Er>   c                |    t          | t          j        |dd          dz                       d                     d S )NF   )ensure_asciiindent
r,   )rZ   r.   dumpsencode)r   r>   s     r   atomic_jsonrb   @   s=    
5uQGGG$NVVW^__`````r!   intc                    t          t                    fdddddddgt          d t          D             D             t	                                                    t          gz   } d	 | D             }d         d         d         hfd
|D             }|rt          d|           dD ]4}|                                         rt          d|                    5t          d                   }t          d                   }|	                    d          dk    s|	                    d          durt          d          |	                    d          |k    sJ|	                    d|	                    d                    d         
                                j        k    rt          d          i }t          D ]\  }}}t          |                   }	|	||<   |		                    d          dk    s|		                    d          durt          d|           |r+|		                    d          |k    rt          d|           fdt          D             }
|d         	                    d          |
k    rt          d          d                                         }|                    dd           }t          |                                          }t          t!          d!                    }|	                    d"          }d# d         d         d         d         t          d$                                D             }t%          d         |           t%          d         |           t          d                   }t          d                   }||k    s||k    rt          d%          t&          j                            t&          j        j                                                  }i ddddd"|d&	                    d&          d'	                    d(          d)t1          d                   d|dt1          d                   d*t          d                   d+t1          d                   d,|d-t1          d                   d.|d/dd0|dt          |          d1|
fd2t          D             ||d3d4}t3          d         |           t          d                   }|                    d5t1          d                             t8                              |t1          d                             t8                              |t1          d                             t8                              ||ddt1          d6                             t8                              ||d7         d8         d9ddt1          d                             t8                              d:d;|d<           t3          d         |           t          t                    d=d<   |d,<   |d.<   dd>         d?<   dd>         d7<   d6         j        j        |t1          d6                             t8                              dd|d7         d8         ||d7         d8         z  d@dA<   |dB<   t3          t                     t          d                   	                    d.          |k    rt          dC          t          d                   	                    d.          |k    rt          dD          t          t                    	                    d.          |k    rt          dE          t?          tA          j!        dd|||t1          d                   dFdGH                     dIS )JNc                2    i | ]}|t          |          S  )r@   ).0r3   r2   s     r   
<dictcomp>zmain.<locals>.<dictcomp>F   s5        	^Hc**  r!   r   producer_close_receiptstory_canonspoken_narrationstory_manifestpromotion_receiptc                    g | ]\  }}}|	S rf   rf   )rg   _r3   s      r   
<listcomp>zmain.<locals>.<listcomp>K   s    ---iaac---r!   c                T    g | ]%}|                                 t          |          &S rf   )rW   r   )rg   r   s     r   rp   zmain.<locals>.<listcomp>O   s+    CCCTT[[]]Cs4yyCCCr!   c                6    g | ]}t          |          v|S rf   r   )rg   r   allowed_missings     r   rp   zmain.<locals>.<listcomp>R   s)    VVVDJJo4U4U4U4U4Ur!   z#required promotion inputs missing: )rj   rk   rm   zApromotion output already exists and requires audit/invalidation: status	completedwriter_closedTzcandidate writer is not closedcandidate_sha256rB   
byte_countz,candidate differs from producer-close markerverifiedz&promotion gate not terminal/verified: z$promotion gate stale for candidate: c                    i | ]C}t          |                             t                              t          |                   DS rf   )r   r<   r:   r#   )rg   r3   pathss     r   rh   zmain.<locals>.<dictcomp>i   sB    hhhRUE#J22488996%*;M;Mhhhr!   r   hashesz)authority drift detected after validationr,   strict)errorscandidate_run_manifestrun_idc                    i | ]N\  }}|t          |          |                                |                                rt          |          nd dOS )N)r   rW   r#   )r   rW   r#   )rg   r3   r   s      r   rh   zmain.<locals>.<dictcomp>r   sc       C 	c$ii4;;==TXT_T_TaTaDkF4LLLgkll  r!   )canon	narration	promotionrl   project_manifestz*post-write promotion equality check failed
project_idcanonical_titlestory_titlecandidate_pathproducer_close_sha256
canon_pathcanon_sha256spoken_narration_pathspoken_narration_sha256assembly_equalitywords_whitespaceauthority_hashesc                p    i | ]2\  }}}|t          |                   t          |                   d 3S ))r   r#   )r   r#   )rg   rO   r3   ro   r{   s       r   rh   zmain.<locals>.<dictcomp>   sO     
 
 
c1 3uSz??fU3Z6H6HII
 
 
r!   zsha256 streaming 8 MiB)gate_receiptsprecondition_fingerprintspromoted_athash_methodpromotedr   r   wpm)rt   ry   receiptrw   r   )rt   ry   r   r$   )rt   r   rw   r   r   r   r   r   r   r   
media_gater   story_promotedstepsstory)r   rw   r   rt   ry   r   predicted_full_minutesvoice_test_run
updated_atz!promotion receipt readback failedzstory manifest readback failedz project manifest readback failed)rt   ry   r   r   r   rm   Fr]   r   )"r1   MANIFEST_PATHAUTHORITY_KEYS	GATE_KEYSlistvaluesr8   rW   r#   r6   statst_size
read_bytesdecodelensplitr@   itemsrZ   datetimenowtimezoneutc	isoformatr   rb   r&   r<   r:   rK   rO   printr.   r`   )requiredmissingblocking_missingr3   candidate_hashclosereportsrO   candidate_boundreportr   rA   textwordsrun_manifestr   before
canon_hashnarration_hashr   r   rl   rs   r2   r{   s                         @@@r   mainr   D   s   M""H    1=BT1
4B
 .-9---
  E ELLNN##}o5HCCXCCCG]+U3E-FNaHbcOVVVVVVV USAQSSTTTG q q: 	qochilcmooppp	q E+.//N/011Eyyk))UYY-G-Gt-S-S;<<<yy#$$66%))GUYYWcMdMd:e:einozi{  jA  jA  jC  jC  jK  ;K  ;KIJJJ!G&/ N N"c?eCj!!::h;..&**Z2H2HPT2T2TNNNOOO 	Nvzz*<==OOLdLLMMMhhhhYghhh{))-===FGGG((**D;;wx;00D

Ex1IJJKKLh''F  =)>P8Q23uM]G^ -
 
 %''  F }%t,,,)*D111m,--JE"4566N^##~'G'GGHHH



 1 5
6
6
@
@
B
BC+)4196hll<002CX\\R_E`E` 	#eK011 4F~ 	!#e,D&E"F"F	
 	 .F(G!H!H 	c%.// 2@ 	 U+=%>!?!? 	"> 4G 	E $+CII 8JK[
 
 
 
 )
 
 
 &,C/  I" )*I666% 0122NE+4F4R4RSW4X4X0Y0Y*#eM>R>^>^_c>d>d:e:e"!$U+=%>%J%J4%P%P!Q!Q#1u!,$5!56BB4HHII .w|7LU7SU U !,5!45AA$GGHHJ JS     &'888M""H)HX)H^*8H&'!,HWg&1HWl#,-49*s5AU;V;b;bcg;h;h7i7i48Me8T"'',*?*F"F	" "H !H\x(((E%&''++,EFF.XX>???E"#$$(()BCC~UU;<<<M899^KK=>>>	$*4#1u ':!;<<  	     
 1r!   __main__failedFz: )rt   ry   errorr   )file)r   r   r   r   )r   r   r   r*   )r2   r*   r3   r   r   r   )r   r   rA   rB   r   rC   )r   r   r>   r*   r   rC   )r   rc   )
__future__r   r   r"   r.   rP   sysrM   pathlibr   __file__r;   rE   r:   r   r   r   r#   r1   r@   rZ   rb   r   __name__exit	Exceptionr?   r   r`   typestderrrf   r!   r   <module>r      s   " " " " " "    				 



       tH~~'*55iii  	   8 8 8 8      a a a a} } } }@ z   jdjH%S		*33c335 5CHJ J JPSPZ	\ 	\ 	\ 	\ s   9B C,A	C''C,