
    ;_jT                     :   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mZ d dlm	Z	  e	e
                                          j        d         Zedz  Zedz  Zedz  Z ej        d          Zg d	g d
g dg ddZd Zd Zd Zd Zedk    r e             dS dS )    N)datetimetimezone)Path   zwork/story/run-002zauthority-validation.jsonzlog/run-002/draft-progress.jsonzu(?i)(?<!\w)(teaser|cold open|catch-up|beat|scene|POV|outline|candidate|canon|narration|prompt|receipt|metadata)(?!\w))r                  )      	   
         )                  )                  )r   r   r   r	   c                 r    t          j        |                                                                           S N)hashlibsha256
read_bytes	hexdigest)paths    script/close-run002-part.pyr!   r!      s(    >$//++,,66888    c                    | j                             dd           |                     | j        dz             }t	          j        |dd          dz                       d          }t          |d	          5 }|                    |           |	                                 t          j        |                                           d d d            n# 1 swxY w Y   t          j        ||            d S )
NTparentsexist_okz.partFr   )ensure_asciiindent
utf-8xb)parentmkdirwith_suffixsuffixjsondumpsencodeopenwriteflushosfsyncfilenoreplace)r$   valuepartdatahandles        r%   atomic_jsonrB      s   KdT222DK'122DJu5;;;dBJJ7SSD	dD		 "VT
!!!" " " " " " " " " " " " " " " JtTs   7ACCCc                      t          |           r   )RuntimeError)messages    r%   failrF   )   s    
w

r&   c                  *   t          j                    } |                     dt          t          d           |                     dt
          dd           |                     dd           |                                 }t          j        t          d	z  
                    d
                    }|                    d          dk    s|                    d          dk    rt          d           |                    d          dk    rt          d           t          j        t          
                    d
                    }|                    d          dk    s0|                    d          dus|                    d          dk    rt          d           |d                                         D ]@\  }}t          |d         |         z  }t          |          |k    rt          d|            At           d|j        ddz  }|                                s'|                    d                                          rt          d|            |j                                        s&t          |j        z                                  |_        |j                                        st          d           t           |j        j        vs|j        j        t           k    rt          d            |j                                        }|                    d!          sd"|v rt          d#           |                    d$          r|                    d%          rt          d&           	 |                    d
          }	n)# t:          $ r}
t          d'|
            Y d }
~
nd }
~
ww xY wt=          j        d(|	          st          d)           tA          d* |	!                                D                       rt          d+           tE          j#        d,|	          stE          j#        d-|	          rt          d.           d/ tI          |	!                                d0          D             }|rt          d1|            d2 tI          |	!                                d0          D             }|rt          d3|            g }tJ                                          r}t          j        tJ          
                    d
                    }|                    d4          t          t                    k    rt          d5           |                    d6g           }tM          tO          d0|j                            }d7 |D             |k    rt          d8           |j        (                    dd9           |                    d          }tS          |d:          5 }|*                    |           |+                                 tY          j-        |.                                           d d d            n# 1 swxY w Y   tY          j/        ||           |                                |k    rt          d;           ta          j1        td          j3                  4                                }|j        d<t          |j                 tk          |6                    t                              tk          |j        6                    t                              to          j        |          8                                t          |          ts          |          ts          |	:                                          |j;        |d=}|j        d>k     rd?nd@ddddAt          t                    |d         ||gz   ty          dB ||gz   D                       |dC
}t{          tJ          |           t}          t          j?        |dDE                     d S )FNz--partT)typechoicesrequiredz--sourcez6Reviewed temp draft to promote as official source part)rH   rJ   helpz--review-note)rJ   zscript/project-manifest.jsonr.   )encoding
active_runzrun-002authority_revisionv2z&active run/authority revision mismatchstatusz&run-002_authority_locked_prose_allowedz)official source-part close is not enabledPASSverifiedz)authority validation v2 not terminal PASSauthority_hashesactive_pathsz#authority drift before part close: zpart-02dz.txtz	.txt.partzofficial part already exists: zreviewed temp source missingzHtemp source must be under a run-002 subdirectory, not official namespaces   ﻿   zBOM/CR forbidden   
s   

z!part must end with exactly one LFzinvalid UTF-8: NFCzpart is not NFCc              3   @   K   | ]}|                     d           V  dS )) 	N)endswith).0lines     r%   	<genexpr>zmain.<locals>.<genexpr>V   s.      
D
D$4==%%
D
D
D
D
D
Dr&   ztrailing whitespacez(?m)^\s*#{1,6}\s+uQ   (?im)^\s*(chương|phần|cảnh|hồi|chapter|part|scene)\s*(\d+|[IVXLC]+|[:\-])z heading/section marker forbiddenc                 L    g | ]!\  }}t                               |          |"S  )PRODUCTION_TERMSsearchr]   ir^   s      r%   
<listcomp>zmain.<locals>.<listcomp>Z   s1    iiiWQK[KbKbcgKhKhiqiiir&   r   z+production terminology in narration lines: c                 B    g | ]\  }}t          j        d |          |S )uH   (?i)(đăng ký kênh|subscribe|nhấn chuông|hãy like|hãy chia sẻ))rerc   rd   s      r%   rf   zmain.<locals>.<listcomp>]   sT      b  b  bgaBI  OZ  \`  Ea  Ea  b  b  b  br&   zCTA in narration lines: authority_validation_sha256z>draft progress belongs to another authority validation receiptpartsc                     g | ]
}|d          S )r?   ra   )r]   rows     r%   rf   zmain.<locals>.<listcomp>h   s    %%%F%%%r&   z*parts must close sequentially without gapsr(   r/   zofficial part readback mismatchclosed)r?   rP   beatsr$   source_temp_pathsource_temp_sha256r!   byteswordsreview_note	closed_atr	   in_progressall_parts_closedz,work/story/run-002/authority-validation.jsonc              3   &   K   | ]}|d          V  dS )rr   Nra   )r]   items     r%   r_   zmain.<locals>.<genexpr>   s&      CCT4=CCCCCCr&   )
rP   rR   run_idrN   authority_validation_pathri   rS   rj   total_words
updated_atF)r+   )@argparseArgumentParseradd_argumentintEXPECTED_BEATSr   
parse_argsr4   loadsROOT	read_textgetrF   AUTH_RECEIPTitemsr!   RUNr?   existsr2   sourceis_absoluteresolver)   r0   r"   
startswithr\   decodeUnicodeDecodeErrorunicodedatais_normalizedany
splitlinesrh   rc   	enumeratePROGRESSlistranger1   r7   r8   r9   r:   r;   r<   r=   r   nowr   utc	isoformatstrrelative_tor    r#   lensplitrs   sumrB   printr5   )parserargsmanifestreceiptkeyexpectedr$   officialrawtextexcproduction_hitscta_hitspriorstateexpected_priortemprA   r   rl   s                       r%   mainr   -   s   $&&F
sNTRRR

C{|||
$777Dz4"@@KKU\K]]^^H||L!!Y..(,,?S2T2TX\2\2\5666||H!III8999j///AABBG{{8&&'++j*A*A*M*MQXQ\Q\]qQrQrvzQzQz8999 !34::<< > >Xh~.s33$<<8##<s<<===0TY00000H :H00==DDFF :8h88999;""$$ 5dk)2244; -+,,,
$+%%%);s)B)BWXXX
+
 
 
"
"C
~~o&& !%3,,   << 2#,,w"7"7 20111&zz'"" & & &$s$$%%%%%%%%&$UD11  

D
D$//2C2C
D
D
DDD $"###	y%t,, 1	  ;O  QU  1V  1V 1/000ii	$//2C2CQ(G(GiiiO NL?LLMMM b  b4??+<+<a!@!@  b  b  bH 4222333E '
8--w-??@@99233vl7K7KKKQRRR		'2&&%49--..N%%u%%%779:::O$666,,D	dD		 "VS
!!!" " " " " " " " " " " " " " " JtX##.///
,x|
$
$
.
.
0
0C		*H((..// 7 7 = =>>%nS11;;==""STZZ\\""' C $(9q==--6H"%S'-l';';#$67#CCUcU]CCCCC E %   	$*Su
-
-
-.....s+   3N	 	
N/N**N/#AX??YY__main__)r}   r    r4   r:   rh   r   r   r   pathlibr   __file__r   r)   r   r   r   r   compilerb   r   r!   rB   rF   r   __name__ra   r&   r%   <module>r      sW      				 				     ' ' ' ' ' ' ' '      tH~~'*
!!00332:|   	 9 9 9       c/ c/ c/L zDFFFFF r&   