
    Swj                        d Z ddlmZ ddlZddlmZmZmZmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZ dZdZd	Zd
ZdZdZdZdZdZdZdZ  e!ee
eeh      Z"	 	 	 	 ddZ#e fddZ$	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ%y)u  v3.6 Runtime Harness — Layer 0: Task MD SHA Contract.

chair_authorization_id=CHAIR-AUTH-TASK-2705PLUS1-V36-TASK-MD-SHA-BOOTSTRAP-260528

Implements the 3-sha decision matrix from
``memory/specs/v36_task_md_sha_contract_design_draft_260528.md`` (sections 2, 4, 6).

Public API
----------
- ``measure_sha_file(path)`` — read a file and return (sha256, size, content_bytes).
- ``classify(...)`` — given three sha values and (optionally) the underlying bytes,
  return a dict with ``patch_type``, ``content_verbatim_match``, ``continue_allowed``,
  ``decision_class``, ``reason_code``, ``mismatch_location``.

Safe-fail: every public function catches all exceptions and returns a HOLD result
with ``reason_code=runtime_internal_error_hold`` — dispatch.py is never affected.
    )annotationsN)AnyDictOptionalTuple   ) PATCH_TYPE_DISPATCH_META_SIDECAR$PATCH_TYPE_FORBIDDEN_SEMANTIC_CHANGEPATCH_TYPE_NO_PATCHPATCH_TYPE_RETRY_HEADER_PREPENDPATCH_TYPE_UNKNOWN#PATCH_TYPE_WHITESPACE_NORMALIZATIONVERBATIM_FALSEVERBATIM_TRUEVERBATIM_UNVERIFIABLEcompute_sha256content_verbatim_matchdetect_patch_typeALLOWHOLD_FOR_CHAIRDENYtruefalseholdDISPATCH_ENTRY_TO_EXITDISPATCH_EXIT_TO_BOT_READANU_PRE_DISPATCH_TO_BOT_READBOT_READ_TO_BOT_WORKUNKNOWNc                    	 | rt         j                  j                  |       syt        | d      5 }|j	                         }ddd       t              t        |      |fS # 1 sw Y    xY w# t        $ r Y yw xY w)z<Read ``path`` and return (sha256, byte_size, content_bytes).NNNrbN)ospathexistsopenreadr   len	Exception)r$   fhcontents      ?/home/jay/workspace/scripts/harness/v36/task_md_sha_contract.pymeasure_sha_filer-   <   sq     277>>$/#$ 	 ggiG	 g&Gg==	  	     s-   !A, A, A A,  A)%A, ,	A87A8c                4    t         t        t        t        | |dS )N
patch_typer   continue_alloweddecision_classreason_codemismatch_location)r   r   CONTINUE_HOLDDECISION_HOLD)reasonr4   s     r,   _holdr8   J   s    ("7)'.     c                ^   	 | ||t        d      S | r/|r-|r+| |cxk(  r|k(  r n nt        t        t        t        dt
        dS |t        dt              S |{|y||k7  rtt        ||      }t        ||      }|t        v r"|t        k(  r|t        t        t        dt        dS |t        k(  rt        |t        t        dt        dS t        dt              S | || |k7  rt        ||      }t        ||      }	|t        v r"|	t        k(  r|t        t        t        dt        dS |t        k(  rt        |	t        t        d	t        dS ||t        d
t              S t        dt              S |$|"||k(  rt        t        t        t        dt
        dS t        d      S # t        $ r t        d      cY S w xY w)u=   Compute the layer-0 decision dict per Bootstrap 전략 4–9.!all_sha_missing_unverifiable_holdall_three_sha_identical_allowr/   "executor_observed_sha_missing_hold#post_observed_normalize_match_allow"post_observed_semantic_change_denypost_observed_unverifiable_holdpre_post_normalize_match_allowpre_post_semantic_change_denypre_post_content_missing_holdpre_post_unverifiable_holdpost_observed_match_allowuncovered_state_holdruntime_internal_error_hold)r8   r   r   CONTINUE_TRUEDECISION_ALLOWLOCATION_UNKNOWNLOCATION_BOT_READ_TO_BOT_WORKr   r   _ALLOWED_PATCH_TYPES"LOCATION_DISPATCH_EXIT_TO_BOT_READr
   CONTINUE_FALSEDECISION_DENYLOCATION_DISPATCH_ENTRY_TO_EXITr)   )
pre_shapost_shaobserved_shapre_contentpost_contentobserved_contentpatch_type_post_vs_observedverbatim_post_vs_observedpatch_type_pre_vs_postverbatim_pre_vs_posts
             r,   classifyr[   U   s   _4?x/L4H<==xLW5XL5X1*7$1"0>%5  4- 
 L$<\AY*;LJZ*['(>|M](^%+/CC-> #>.;(5&4#H)K  +.RR"F.G(6&3#G)K  12 
 8#7Gx<O%6{L%Q"#9+|#T &*>>(M9 #9.;(5&4#C)H  &)MM"F.B(6&3#B)H  "l&:33  ,/ 
 L$<\AY1*7$1"0:%5  +,, 42334sT   F 0F F A
F !!F F A
F !F  F F $%F 

F F,+F,)r$   Optional[str]returnz4Tuple[Optional[str], Optional[int], Optional[bytes]])r7   strr4   r^   r]   Dict[str, Any]r!   )rQ   r\   rR   r\   rS   r\   rT   Optional[bytes]rU   r`   rV   r`   r]   r_   )&__doc__
__future__r   r#   typingr   r   r   r   task_md_sha_normalizer	   r
   r   r   r   r   r   r   r   r   r   r   rI   r6   rO   rH   rN   r5   rP   rM   %LOCATION_ANU_PRE_DISPATCH_TO_BOT_READrK   rJ   	frozensetrL   r-   r8   r[    r9   r,   <module>rh      s   " # 	 - -     ": %@ "(F % 6   $#'	"   
 9  1A  $($((,h4h4h4  h4 !	h4
 "h4 &h4 h4r9   