
    o\j\              
      b   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edz  Zedz  Zedz  Zed	z  Zed
z  Zedz  Zedz  Zedz  Zedz  Zedz  Zedz  edz  edz  edz  edz  edz  gZd,dZd-dZd.d Zd/d"Zd0d$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 )1    )annotationsN)Path   z&work/story/run-001/story-candidate.txtzstory/story-canon.txtzstory/spoken-narration.txtzlog/authority-validation.jsonzlog/candidate-validation.jsonz$work/voice-test/run-001/receipt.jsonzlog/semantic-review.jsonzlog/originality-validation.jsonzlog/promotion.jsonzscript/story-manifest.jsonzscript/project-manifest.jsonzstory/story-brief.jsonzstory/characters.jsonzstory/ledger.jsonzscript/outline.jsonzscript/identity-registry.jsonzscript/reveal-ledger.json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   loadr    *   s     :dnngn66777r   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   r!   fdr/   r   s        r   atomic_bytesr:   .   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valuec                |    t          | t          j        |dd          dz                       d                     d S )NF   )ensure_asciiindent
r   )r:   r   dumpsencode)r   r;   s     r   atomic_jsonrC   <   s=    
5uQGGG$NVVW^__`````r   intc                    t           t          t          t          t          t
          t          t          gt          } d | D             }|rt          d|           t          t                     }t          t                    }t          t                    }t          t                    }t          t                    }t          t
                    }|||||d}d |                                D             }	|	rt          d|	           d|fd|fd|fd	|ffD ]0\  }
}|                    d
          |k    rt          |
 d          1d t          D             }|                    di           }||k    rt          d          t                                           }t          |                    dd                                                    }d t$          t&          t(          t          t          fD             }t+          t$          |           t+          t&          |           t          t$                    }t          t&                    }||k    s||k    rt          d          t,          j                            t,          j        j                                                  }i dddddddddddt7          t                     d
|dt7          t$                    d |d!t7          t&                    d"|d#dd$|d%t          |          d&|d't7          t                    t7          t                    t7          t                    t7          t                    t7          t
                    dd(||d)d*}t9          t(          |           t          t                    }|                    d+t7          t                               t>                              |t7          t$                              t>                              |t7          t&                              t>                              ||ddt7          t                              t>                              ||d,         d-ddt7          t(                              t>                              d.d/|d0           t9          t          |           t          t                    }d1|d<   ||d <   ||d"<   d|d2         d3<   d|d2         d<   ||d4<   t9          t          |           t          t(                                        d          durt          d5          t          t                                        d"          |k    rt          d6          t          t                                        d"          |k    rt          d7          tA          tC          j"        dd|||t7          t(                    d8d9:                     d;S )<Nc                T    g | ]%}|                                 t          |          &S  )r7   r   .0r   s     r   
<listcomp>zmain.<locals>.<listcomp>F   s+    CCCTT[[]]Cs4yyCCCr   z#required promotion inputs missing: )	authority	candidate
voice_testsemanticoriginalityc                F    g | ]\  }}|                     d           du|S )verifiedT)get)rI   r/   reports      r   rJ   zmain.<locals>.<listcomp>W   s3    bbb|tVvzz*?U?U]a?a?ad?a?a?ar   zpromotion gates not verified: rL   rM   rN   rO   candidate_sha256z report is stale for candidatec                x    i | ]7}t          |                    t                              t          |          8S rG   )r   relative_toROOTr   rH   s     r   
<dictcomp>zmain.<locals>.<dictcomp>c   s6    ___tT--d3344fTll___r   hashesz)authority drift detected after validationr   strict)errorsc                    i | ]M}t          |                    t                              |                                rt	          |          nd NS )N)r   rV   rW   r7   r   rH   s     r   rX   zmain.<locals>.<dictcomp>j   sW        	DT""##T[[]]%LVD\\\  r   z*post-write promotion equality check failedstatus	completedrQ   Trun_idzrun-001
project_idz001-Mui-Chi-Giau-Trong-Mua-Muacanonical_titleu!   Mũi Chỉ Giấu Trong Mùa Mưacandidate_path
canon_pathcanon_sha256spoken_narration_pathspoken_narration_sha256assembly_equalitywords_whitespacer"   authority_hashesgate_receiptsprecondition_fingerprintszsha256 streaming 8 MiB)promoted_athash_methodpromotedwpm)r]   rQ   receiptrT   ro   )r]   rQ   rp   r   )r]   rb   rT   rc   rd   re   rf   rh   rM   	promotion
media_gaterl   story_promotedstepsstory
updated_atz!promotion receipt readback failedzstory manifest readback failedz project manifest readback failed)r]   rQ   rd   rf   rh   promotion_receiptFr>   r   )#	CANDIDATEAUTHORITY_REPORTCANDIDATE_REPORTVOICE_REPORTSEMANTIC_REPORTORIGINALITY_REPORTSTORY_MANIFESTPROJECT_MANIFESTAUTHORITY_FILESRuntimeErrorr   r    itemsrR   
read_byteslendecodesplitCANON	NARRATION	PROMOTIONr:   datetimenowtimezoneutc	isoformatr   rC   r   rV   rW   printr   rA   )requiredmissingcandidate_hashrK   rL   voicerN   rO   gate_reportsfailedr/   rS   current_authorityrecorded_authorityr!   wordsbefore
canon_hashnarration_hashr   rq   story_manifestproject_manifests                          r   mainr   @   sT   #%5|+^=M 
H
 DCXCCCG LJJJKKKI&&N%&&I%&&IEO$$H)**K" L cb|'9'9';';bbbF FDFDDEEE	i 	u	X	$	 H Hf ::())^;;$FFFGGG < `____"x44...FGGG!!DGH55;;==>>E Iy.BRS  F D!!!JI&&N^##~'G'GGHHH



 1 5
6
6
@
@
B
BC+D 	) 	6	
 	> 	#i.. 	N 	c%jj 	
 	 Y 	"> 	T 	E 	T 	-  	-..-..l++O,,122
 
!. 	$V/0 /3  I8 	9%%%.))Ni33D99::*%++D1122"!$Y%:%:4%@%@!A!A#1!!<33D99:: .<
 
 "9006677
 

 -    0 ///,--!1X'1^$2@./)4Wg&.9Wl+%(\" "2333 I:&&d22>???N 9::nLL;<<<!!";<<NN=>>>	$*"#1! ^^        1r   __main__r   Fz: )r]   rQ   errorrx   )file)r   r   r   r   )r   r   r   r   )r   r   r!   r"   r   r#   )r   r   r;   r   r   r#   )r   rD   )'
__future__r   r   r   r   r0   sysr-   pathlibr   __file__resolver%   rW   ry   r   r   rz   r{   r|   r}   r~   r   r   r   r   r   r    r:   rC   r   __name__exit	Exceptionexcr   rA   typestderrrG   r   r   <module>r      s   " " " " " "    				 



       tH~~'*;;	&&//	99 99 <<33== ''	4488 ##""  **&&   8 8 8 8   a a a aI I I IX z   jdjS		*33c33
 
 	   &)Z		1 	1 	1 	1
 	 s   9C D,A	D''D,