
    ʁ\js                        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Zd dl	m
Z
  e
e                                          j        d         Zedz  Z e
d          Zd$dZd%dZd&dZd'dZd(dZd)dZedk    r	  ej         e                       dS # e$ rZdd ee          j         de ej                            ej        j                                                  dZ 	  ej!        e"                    d                     Z# e ee#d!          e            n# e$ r Y nw xY w e$ ej%        e 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.jsonzN/home/hermes/.hermes/skills/content-creation/tao-anh/scripts/generate_image.py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/run-artwork-revision.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'manifestdict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)r   r   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$r&   objectlabelc                |   t          | t                    r| st          d|           t          |           }|                                s
t
          |z  }	 |                                                    t
                                                     n%# t          $ r}t          d|           |d }~ww xY w|S )Nzmissing project path: zproject path escapes root: )	r   r   r    r   r!   r"   r#   r$   r%   )r&   r*   r   r'   s       r   project_pathr,   (   s    eS!! = =;E;;<<<;;D d{K""4<<>>2222 K K K@@@AAsJKKs   >B 
B9!B44B9Nonec                   | j                             dd           t          j        |dd          dz                       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_okF   )ensure_asciiindent
utf-8.)prefixdirwb)parentmkdirjsondumpsencodetempfilemkstempnameosfdopenwriteflushfsyncfilenoreplacer   existsunlink)r   r&   datafdrB   r   s         r   atomic_jsonrN   5   s   KdT222Ju5;;;dBJJ7SSD'749'7'7'7T[IIIHBYr4   	&FLLLLNNNHV]]__%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	
47>>$ 	IdOOOOO	 	27>>$ 	IdOOOO	s1   2D: AC#D: #C''D: *C'+D: :6E0boolc                    |                                  o<|                                 j        dk    o|                                 d d         dk    S )Ni      s   PNG

)rJ   statst_size
read_bytes)r   s    r   	valid_pngrU   D   sC    ;;==iTYY[[047iDOO<M<Mbqb<QUi<iir   intc                 n   t          j        t                              d                    } |                     d          pi }|                    d          }|                    d          d| dk    s|dvrt          d	          t          | d
          }t          | d          }t          | d          }t          | d          }t          | d          }t          | d          }t          |                    d          d          }	t          |                    d          d          }
t          |                    d          d          }t          j        |                    d                    }t          j        |                    d                    }t          j        |	                    d                    }t          j        |
                    d                    }|                    d          }|                    d          dk    s|                    d          durt          d          |                    d          pi }|                    d          |k    s,|                    d          |                    d          k    rt          d          |                    d          d| dk    s2|                    d          dk    s|                    d          |k    rt          d           |                    d          d!k    s,|                    d          d"us|                    d#          st          d$          |                    d          dk    s|                    d          durt          d%          |                    d&          pi }t          |          t          |          d'}||k    rt          d(          |                    d)          pi }|                    d*          |d*         k    s|                    d+          |d+         k    rt          d,          |                    d          t          |          k    s|                    d)          |k    rt          d-          |	                                s|	                                rt          d.          t                                          st          d/          |j                            dd0           t          j        t!          t                    d1|d2         d         d3t!          |          d4d5d6d7d8d9d:d;g}t#          j        |ddd<=          }|j        d>k    st)          |          s*t          d?| d@|j         dA|j        dBd                     dCd"d||t!          |          t          |          t!          |          t          |          dDt!          |          t          |          dDt!          |          t          |          dDdEt!          |	          t          |	          dDt!          |
          t          |
          dDdFdt!          |          t          |          |                                j        ddGgddt0          j                            t0          j        j                                                  dH}t;          ||           d| dI|d<   t!          |          |d         dJ<   t          |          |d         dK<   |dL         |d         dM<   t;          ||           t=          t          j        |d         |dNt!          |          t          |          t!          |          dOd"P                     d>S )QNr6   encodingartwork_revisionversionstatusintro_	_prepared>   v2v3v4z.active intro revision is absent or unsupportedpromotion_receiptimage_promptsintro_masterartwork_generation_receiptleft_panel_masterright_panel_masterprior_visual_receiptprior_orientation_receiptprior_intro_preservedcanon_sha256	completedverifiedTzpromotion is not verifiedrevisionsource_canon_sha256source_candidate_sha256candidate_sha256zrevision prompt is stalerevision_intro_assetintroz7prompt revision contract does not match active revisionfailedFblockersz0prior visual failure does not authorize revisionz$prior orientation evidence is absentaccepted_panel_hashes)
left_panelright_panelzaccepted panel drift detectedartifact_sha256rx   ry   z3prior visual receipt does not match accepted panelsz2prior intro or orientation evidence drift detectedz9revision output/receipt already exists and requires auditz%configured image generator is missingr/   z--promptpromptsz--outputz--n1z--detailhighz--formatpngz	--timeout600i  )textcapture_outputtimeoutr   zintro z generation failed: exit=z	; stderr=i/completed_pending_visual_and_orientation_review)r   r   )rx   ry   prior_intro)visualorientation)kindr   r   bytespng_signature_verified)r\   rm   revision_assetrevision_versionro   prompt_manifestprompt_manifest_sha256preserved_assetsprior_evidence	generatedvisual_review_requiredorientation_gate_requiredcompleted_at_generated_pending_reviewoutput_pathoutput_sha256r   generated_atr   )r\   r[   generated_countr   r   receiptr3   ) r=   loadsPROJECT_MANIFEST	read_textr   r    r(   r,   r   rJ   	GENERATORis_filer;   r<   sys
executabler   
subprocessrun
returncoderU   stderrrR   rS   datetimenowtimezoneutc	isoformatrN   printr>   )projectrn   r[   promotion_pathprompts_pathoutputr   leftrightprior_visual_pathprior_orientation_pathr   	promotionr{   prior_visualprior_orientation
canon_hashprompt_revisionexpected_panelscurrent_panelsprior_hashescommandrl   results                           r   mainr   H   s   j)33W3EEFFG{{-..4"Hll9%%G||H!<'!<!<!<<<Oa@a@aKLLL#G-@AAN!'?;;LG^44FW&BCCG'#677D7$899E$X\\2H%I%IKabb)(,,7R*S*SUpqqx||,CDDF]^^K
>33W3EEFFIj///AABBG:/9979KKLLL
#9#C#CW#C#U#UVV~..J}}X+--z1J1JRV1V1V6777kk*--3O{{())Z777;;G`;a;aenerer  tF  fG  fG  <G  <G5666{{8 D' D D DDDH[H[\cHdHdhoHoHo  tC  tG  tG  HQ  tR  tR  V]  t]  t]TUUU!!X--1A1A*1M1MUZ1Z1Zbnbrbrs}b~b~1ZMNNNX&&+559J9N9Nz9Z9Zbf9f9fABBBll#:;;ArO$*4LLOON.((:;;;##$566<"L%%)EEEIYIYZgIhIhlz  |I  mJ  JJ  JJPQQQ  F;$7$777;L;P;PQb;c;cgs;s;sOPPP}} X'..** XVWWW DBCCC
Mt444I
GI4Fw4OCKKZE;G
 wT$PSTTTIq  	&(9(9 GiFZeneuv{v|v|e}  A  A  	ACQV!w)|,,|H\H\#&t99tEE$'JJ&--HH$'$4$4{@S@STT
 
  ##455HYAZAZ[[$'(>$?$?6RhKiKijj
 
  'Fvf~~`f`k`k`m`m`u  RV  W  W  X"&T )--h.?.CDDNNPP! F$    L'LLLGH),VGJ&+1&>>GJ(*0*@GJ'g&&&	$*x 0WYZdghndodo  |B  CI  |J  |J  WZ  [b  Wc  Wc  d  d  sx  y  y  y  z  z  z1r   __main__ru   Fz: )r\   rm   error	failed_atr6   rX   re   r   )file)r   r   r   r   )r   r   r   r   r   r   )r&   r)   r*   r   r   r   )r   r   r&   r   r   r-   )r   r   r   rO   )r   rV   )'
__future__r   r   r   r=   rC   r   r   r@   pathlibr   __file__r#   r0   r"   r   r   r   r(   r,   rN   rU   r   __name__exit	Exceptionr'   typer   r   r   r   failurer   r   r   r   r>   r    r   r   <module>r      s   " " " " " "    				     



       tH~~'*88 Dabb	      
 
 
 
   j j j jR R R Rj z
   %5dd3iiFXCaCa\_CaCapx  qB  qF  qF  GO  GX  G\  q]  q]  qg  qg  qi  qi  j  j	 dj!1!;!;W!;!M!MNNGKw0LMMwWWWW 	 	 	D	jdju555CJGGGG s=    B E1AE,.:D)(E,)D1.E,0D115E,,E1