
    ejQ             	      ^   U d Z 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
Z
ddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZmZmZmZ ddlmZmZ dd	lmZmZ dd
lm Z m!Z!m"Z" ddl#m$Z$ dg dg dddg ddddZ%de&d<   dg g dZ'de&d<   ddie'd ejP                   ejR                  e'dd      jU                               jW                         iZ,de&d<    ejZ                  d       Z. ejZ                  d!      Z/ G d" d#e0      Z1	 	 	 	 dUd$Z2dVd%Z3dWd&Z4dd'dXd(Z5dYd)Z6dZd*Z7d[d+Z8	 	 	 	 d\d,Z9	 	 	 	 d]d-Z:	 	 	 	 	 	 d^d.Z;	 	 	 	 	 	 	 	 d_d/Z<	 	 	 	 	 	 d`d0Z=dad1Z>dbd2Z?	 	 	 	 	 	 	 	 	 	 dcd3Z@	 	 	 	 	 	 	 	 	 	 	 	 ddd4ZA	 	 	 	 ded5ZB	 	 	 	 	 	 dfd6ZC	 	 	 	 	 	 dgd7ZD	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dhd8ZE	 	 	 	 	 	 	 	 did9ZF ed:      ZG ed;      ZHd<g d=d>d?ZId@ZJdAZKh dBZL	 	 	 	 	 	 	 	 djdCZM	 	 	 	 	 	 	 	 	 	 dkdDZN	 	 	 	 	 	 	 	 	 	 dldEZOdmdFZP	 	 	 	 	 	 	 	 dndGZQ	 	 	 	 	 	 	 	 	 	 dodHZRddI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dpdJZSddK	 	 	 	 	 	 	 	 	 dqdLZTdrdMZUdsdNZV	 	 	 	 	 	 	 	 dtdOZW	 	 	 	 	 	 	 	 dudPZXdddQ	 	 	 	 	 	 	 	 	 	 	 	 	 dvdRZYdwdSZZe[dTk(  r e\ eZ             y)xzHOffline rehydration and sealed expected-state checks for Task26 bundles.    )annotationsN)BytesParser)compat32)Path)AnyMappingSequencecast)InvalidRequirementRequirement)InvalidVersionVersion)qualification_tool_hashestrust_boundary_digestvalidate_product_binding)validate_snapshotz!task26-evidence-trust-boundary-v1)trusted_operator_accounthost_kernelselected_python_pip_toolchainsealed_wheel_bytes)content_identitydeterministic_tamper_detectionreproducibility*revocation_and_current_authority_selectionfail_closed_workflowzThese guarantees apply only while the trusted operator account, host kernel, selected Python/pip toolchain, and sealed wheel bytes remain trustworthy.zCoherent compromise of the trusted operator account or host administrator, including coherent modification of the repository, ledger, or verifier, is outside the Task26 threat model.)external_authenticitynon_repudiationowner_tamper_proof_or_worm+resistance_to_compromised_operator_or_adminFnone)schematrust_anchorsguarantees_within_boundaryscope_statementoutside_scopenot_claimedexternal_authenticity_claimedsignature_claimdict[str, object]TRUST_BOUNDARY	root_modedirectoriesentries_EMPTY_WHEELHOUSE_PAYLOADr!   %task26-sealed-wheelhouse-inventory-v1inventory_sha256T,:	sort_keys
separators_EMPTY_WHEELHOUSEz[-_.]+z^[0-9a-f]{64}$c                        e Zd Zd fdZ xZS )InvariantMismatchc                T    t         |   d|        || _        || _        || _        y )Nzinvariant differs: )super__init__keyexpectedactual)selfr>   r?   r@   	__class__s       /home/cube/projects/richard/traning coach/.omo/evidence/task26/task26-final-state-v7-prefreeze-st_01a01574-v30/gateway/platforms/task26_final_state.pyr=   zInvariantMismatch.__init__U   s-    .se45     )r>   strr?   objectr@   rF   returnNone)__name__
__module____qualname__r=   __classcell__)rB   s   @rC   r:   r:   T   s     rD   r:   c                >    | D ]  \  }}}||k7  st        |||       y N)r:   )checksr>   r?   r@   s       rC   _require_invariantsrP   \   s,     "(XvX#C6:: "(rD   c                R    t        j                  | dddd      j                         S )NFTr2   )ensure_asciir6   r7   	allow_nan)jsondumpsencodevalues    rC   	canonicalrY   d   s+    :: fhrD   c                   t        j                         }| j                  d      5 t        fdd      D ]  }|j	                  |        	 d d d        |j                         S # 1 sw Y   |j                         S xY w)Nrbc                 &     j                  d      S )Ni   )read)streams   rC   <lambda>zsha256_file.<locals>.<lambda>q   s    &++k":rD   rD   )hashlibsha256openiterupdate	hexdigest)pathdigestchunkr^   s      @rC   sha256_fileri   n   sh    ^^F	4F:C@EMM%  A 
  
 s   &A&&A>	directoryc                  | j                         }|rt        j                  nt        j                  }|rddhnddh}t        j                  |j
                        se ||j
                        rS|j                  t        j                         k7  s2|s|j                  dk7  s!t        j                  |j
                        |vrt        d|        y )N  i@          z sealed private path is invalid: )lstatstatS_ISDIRS_ISREGS_ISLNKst_modest_uidosgeteuidst_nlinkS_IMODE
ValueError)rf   rk   infor?   modess        rC   _privater   v   s    ::<D(t||dllH'UENeU^ET\\"%;;"**,&dmmq0<<%U2;D6BCC 3rD   c                    t        | t              rt        d | D              rt        | d      t	        t        t
        t        f   |       S )Nc              3  >   K   | ]  }t        |t                 y wrN   )
isinstancerE   ).0r>   s     rC   	<genexpr>z_object.<locals>.<genexpr>   s     )Tesjc.B*Be   z is invalid)r   dictanyr|   r
   rE   r   )rX   labels     rC   _objectr      s@    eT"c)Te)T&TE7+.//S#X&&rD   c                J    t         j                  d|       j                         S )N-)_NORMALIZE_PROJECTsublowerrW   s    rC   _normal_projectr      s    !!#u-3355rD   c           	     d   t        |        	 t        j                  |       5 }|j                         }t	        |      t	        t        |            k7  st        d |D              rt        d      |D cg c]  }|j                  d      s| }}t	        |      dk7  rt        d      t        t              j                  |j                  |d               }d d d        t        j                  d	d
            j!                         }t        |j                  dd
            j!                         }|r|st        d      	 t        t#        |            }	t'        d |j)                  d      xs g D              }
t+        |      |	|
t-        |       | j/                         j0                  t/        j2                  | j/                         j4                        dS c c}w # 1 sw Y   xY w# t        j                  $ r}t        d      |d }~ww xY w# t$        $ r}t        d      |d }~ww xY w)Nc              3  p   K   | ].  }|j                  d       xs dt        |      j                  v  0 yw)/..N)
startswithr   parts)r   names     rC   r   z"_wheel_identity.<locals>.<genexpr>   s3     ZTYDts+GttDz7G7G/GGTYs   46zwheel members are invalidz.dist-info/METADATArp   z%wheel METADATA cardinality is invalid)policyr   zdependency wheel is invalidName r   z$wheel project/version is unavailablezwheel version is invalidc              3  2   K   | ]  }t        |        y wrN   rE   )r   rX   s     rC   r   z"_wheel_identity.<locals>.<genexpr>   s     [3Y#e*3Y   zRequires-Dist)projectversionrequires_distra   sizemode)r   zipfileZipFilenamelistlensetr   r|   endswithr   r   
parsebytesr]   
BadZipFilerE   getstripr   r   sortedget_allr   ri   rr   st_sizer{   rv   )rf   archivenamesr   metadata_namesmessageexcr   version_textr   requirementss              rC   _wheel_identityr      s   TNA__T"g$$&EE
c#e*o-ZTYZZ !<==!&!&$--8M*N   >"a' !HII!2==^A./G #" '++fb)*002Gw{{9b1288:L,?@@>gl+, [7???3S3YWY3Y[[L"7+%d#		##TYY[001 ' #"  A67S@A  >34#=>s`   G. AG!4GGAG!G. H G!!G+&G. .HHH	H/H**H/c                `    t         d       g }g } g}|ry|j                         }t        |d       t        t        j                  |      d       D ]3  }t        |j                        }|j                         }t        j                  |j                        rt        d      t        j                  |j                        rmt        |d       |j                  |j                         j                         t        j                   |j                        d       |j                  |       t        j"                  |j                        r-|j$                  dk(  rt        |       |j                  |       +t        d       |ryt        | fd	      t        |d
       fS )NTrj   c                    | j                   S rN   )r   )items    rC   r_   z#_wheelhouse_files.<locals>.<lambda>   s    DIIrD   )r>   zwheelhouse symlink is forbidden)relative_pathr   z.whlz.wheelhouse contains an unbound non-wheel entryc                B    | j                        j                         S rN   )relative_toas_posix)rf   roots    rC   r_   z#_wheelhouse_files.<locals>.<lambda>   s    (8(8(>(G(G(IrD   c                    t        | d         S )Nr   r   )rows    rC   r_   z#_wheelhouse_files.<locals>.<lambda>   s    CO0D,ErD   )r   popr   rx   scandirr   rf   rq   rr   ru   rv   r|   rs   appendr   r   r{   rt   suffix)r   wheelsr-   pendingrk   entryrf   r}   s   `       rC   _wheelhouse_filesr      sO    TT"F+-KfG
KKM	d+BJJy17MNNE

#D::<D||DLL) !BCC||DLL)."")-)9)9$)?)H)H)J $T\\ : t$dll+v0Ed# !QRR% O . 	vIJ{ EF rD   c                   i }t               }| D ]j  \  }}t        |j                  dd            }t        |j                  dd            }||f}||v s||v rt        d      |j	                  |       ||f||<   l | D ]*  \  }}|j                  d      }	t        |	t              st        d      |	D ]  }
	 t        t        |
            }|j                  t        d      |j                  |j                  j                         sV|j                  t        |j                              }|t        d	|j                         t        |d
   j                  dd            }|j                  s|j                  j                  |d      rt        d|j                          - y # t        $ r}t        d      |d }~ww xY w)Nr   r   r   z/duplicate project/version in explicit wheel setr   z)wheel dependency declarations are invalidz'wheel dependency declaration is invalidz%direct URL dependencies are forbiddenzmissing dependency: rp   T)prereleaseszdependency version mismatch: )r   rE   r   r|   addr   listr   r   urlmarkerevaluater   r   	specifiercontains)r   projects
identitiesrf   identityr   r   r>   _pathr   rawrequirementr   
dependencyselected_versions                  rC   _validate_dependency_closurer      s    >@H'*uJ hhll9b12hll9b12 *8 3NOOs!8, ! "x||O4,-HIICU)#c(3 * !HII!!-k6H6H6Q6Q6S!ok6F6F&GHJ! #78H8H7I!JKK":a=#4#4Y#CD$$[-B-B-K-K d .L . !#@AQAQ@R!STT!  	 " & U !JKQTTUs   7F--	G6GGc                  |D cg c]  }|t        |      f }}| g }g }n]t        |       \  }}|D cg c]  }|t        |      f }}|D cg c](  \  }}d|j                  |       j                         i|* }}}| d}	g }n-t	        j
                  | j	                         j                        }	t        g ||       |	|d}
ddi|
dt        j                  t        |
            j                         iS c c}w c c}w c c}}w )zMBuild an exact recursive inventory and prove the explicit dependency closure.Nr   r+   r!   r0   r1   )r   r   r   r   rr   r{   rv   r   r`   ra   rY   re   )
wheelhouserequired_wheelsrf   root_wheelsdependency_wheelsr.   pathsr-   r   r,   payloads              rC   build_wheelhouse_inventoryr      sN    >MM_TD/$/0_KMEG+-.z:{GLMutdOD$9:uM #4

 #4h  !1!1*!=!F!F!H #4 	 
  $	LL!2!:!:;	 !C;!C1B!CD"G 	9
 	GNN9W+=>HHJ 3 N N
s   C<D-Dc                  t        |d      }t        |      h dk7  s|j                  d      dk7  rt        d      |j                  d      }|j                  d      }|j                  d      ||d	}t	        |t
              rNt	        |t
              r>|j                  d
      t        j                  t        |            j                         k7  rt        d      t        | |      }||k7  rt        d      |S )Nsealed wheelhouse inventory>   r!   r.   r,   r-   r1   r!   r0   -sealed wheelhouse inventory schema is invalidr.   r-   r,   r+   r1   z-sealed wheelhouse inventory digest is invalidr   z4wheelhouse inventory differs from sealed expectation)r   r   r   r|   r   r   r`   ra   rY   re   r   )r   r?   r   documentr.   r-   r   r@   s           rC   verify_wheelhouse_inventoryr   "  s     x!>?H
8}   

	0
1 HIIll9%G,,}-K\\+."G w%+t,<<*+>>)G,-779: HII'OF OPPMrD   c                   |j                  d      }t        |t              st        d      | |rt        d      g S g }|D ]~  }t	        |d      }|j                  d      }t        |t
              r0t        |      j                         sdt        |      j                  v rt        d      |j                  | |z          |S )Nr.   %sealed wheelhouse entries are invalidz sealed wheelhouse is unavailablezsealed wheelhouse rowr   r   z!sealed wheelhouse path is invalid)
r   r   r   r|   r   rE   r   is_absoluter   r   )r   	inventoryr.   r   r   r   relatives          rC   _bound_wheel_pathsr   I  s     mmI&Ggt$@AA?@@	Ec2377?+8S)H~))+tH~+++@AAZ(*+  LrD   c                    t        | t              r t        d | j                         D              S t        | t              rt        d | D              S y)Nc              3  D   K   | ]  \  }}|d v xs t        |        yw)>   record_pathraw_installed_record_sha256raw_installed_record_portable!raw_installed_records_nonportableN%_contains_nonportable_record_material)r   r>   r   s      rC   r   z8_contains_nonportable_record_material.<locals>.<genexpr>c  s>      

 +	T  ; 5T:; +s    c              3  2   K   | ]  }t        |        y wrN   r   r   r   s     rC   r   z8_contains_nonportable_record_material.<locals>.<genexpr>o  s     Q548>5r   F)r   r   r   itemsr   rW   s    rC   r   r   a  sN    % 

 #[[]

 

 
	
 %Q5QQQrD   c           	        t        | d      }t        |j                  d      d      }t        |j                  d      d      }|j                  d      }|j                  d      dk7  sFt        |      s;|j                  d      d	k7  s'|d
dddk7  st        |t              rt        |      dk7  rt        d      ddg}t        ||d      D ]  \  }\  }}t        |d      }	|	j                  d      }
t        |	      h dk7  s|	j                  d      |k7  s|	j                  d      |k7  st        |
t              rt        |
      j                         sndt        |
      j                  v sWt        j                  t        |	j                  dd                  )t        |	j                  d            t         us	|	d   dk  st        d       y )Nportable installed runtimeorigin_proofzportable origin proof	executionzportable origin executionmodulesr!   $installed-golden-runtime-portable-v2 task26-installed-origin-proof-v1private_empty_controlledTFcwd_authoritypython_isolatedpythonpath_inherited   z*portable installed origin proof is invalidgatewayhermescheckin_cliprofilestrictzportable origin moduleinstalled_relative_path>   r   modulera   distributionr  r  r  r   ra   r   r   rp   z1portable installed origin module proof is invalid)r   r   r   r   r   r   r|   zipr   rE   r   r   r   _HEX_DIGEST	fullmatchtypeint)runtime_portableruntimeproofr   r   expected_pairsr   r  r  r   r   s              rC   "_validate_portable_origin_contractr  s  s   &(DEGGKK/1HIE		+.0KLIii	"GH!GG0999X"DD7#$)

 '4(w<1EFF+-GHN'*7N4'P##flc347745H wwx F*ww~&,6h,H~))+tH~+++$$S2)>%?@HCGGFO$C/6{QPQQ+ (QrD   c          	        t        | d      }t        |      h dk7  rt        d      |j                         D ci c]  \  }}|dk7  s|| }}}t        |j	                  d      d      }t        |j	                  d      d      }	|j	                  d      d	k7  sv|j	                  d
      |k7  sb|j	                  d      |k7  sN|j	                  d      t        j                  t        |            j                         k7  st        |      ddhk7  rt        d      dD ]c  }
t        |j	                  |
      |
 d      }t        |	j	                  |
      |
 d      }|j	                  d      }t        |      h dk7  s|j	                  d      |j	                  d      k7  s|j	                  d      |j	                  d      k7  s|j	                  d      |j	                  d      k7  st        j                  t        |j	                  dd                  	 b|j	                  d      dusO|j	                  d      dk7  s;t        |t              r+|t        t        |            k7  st        d |D              s[t        d        |S c c}}w )!Nz"nonportable installed RECORD audit>   r!   audit_sha256install_roledistributionscandidate_digestz4nonportable installed RECORD audit schema is invalidr  r  znonportable audit distributionszportable runtime distributionsr!   z,task26-nonportable-installed-record-audit-v1r  r   r  r	  z5nonportable installed RECORD audit binding is invalidr  r	  z raw RECORD auditz portable distributionpath_dependent_classifications>   reasonr  wheel_sha256portable_projection_sha256r   r   r"  r  distribution_namer$  r%  !portable_record_projection_sha256r   r   r   Fr#  Avalidated_path_dependent_installer_material_may_change_raw_recordc              3  $   K   | ]  }|d v 
 yw)>   path_dependent_launcherconsole_entry_point_launcherN r   s     rC   r   z5_validate_nonportable_record_audit.<locals>.<genexpr>  s&       ,D 
 ,s   z1nonportable installed RECORD audit row is invalid)r   r   r|   r   r   r`   ra   rY   re   r  r  rE   r   r   r   r   )rX   r  r   r  auditr>   r   unsignedaudit_distributionsportable_distributionsroler   portableclassificationss                 rC   "_validate_nonportable_record_auditr4    s~    E?@E
5z   OPP+0;;=R=ic4C><QT	=HR!		/"$E %_-/O 			(MM99^$499'(,<<99^$>>)H-.88:;"#	8'<<PQQ%)--d3v=N5OP"&&t,6L.M
 ''"BCH	 ww~&(,,7J*KKww~&(,,~*FFww34||?@A$$CGG92>?  ww67uDwwx RSot4&_)=">>  ,  PQQO &P Lq Ss   J	Jc                  t        | d||      }t        |d||      }t        |j                  d      d      }t        |j                  d      d      }i }	dD ]  }
t        |j                  |
      d|
 d	      }t        |j                  |
      d
|
 d	      }|j                  d      }||j                  d      k7  s#|j                  d      |j                  d      k7  rt        d      ||	|
<    d||d|	dS )zDValidate both raw audits without comparing their nonportable hashes.originalr  r   r  
rehydratedr  zoriginal portable distributionsz!rehydrated portable distributionsr!  z	original  distributionzrehydrated r'  r$  z3original and rehydrated portable projection differsz.task26-frozen-installed-record-verification-v2F)r!   !original_nonportable_record_audit#rehydrated_nonportable_record_audit,raw_record_hash_equality_required_or_claimedr%  )r4  r   r   r|   )r6  r8  r   original_runtime_portablerehydrated_runtime_portableoriginal_auditrehydrated_auditoriginal_distributionsrehydrated_distributionsprojection_digestsr1  original_distributionrehydrated_distribution
projections                 rC   $verify_nonportable_record_audit_pairrG    sP    8)2	N :!)4	 %!%%o6)  '#''8+  -/% '"&&t,	$}.M!
 #*$((.+dV=0Q#
 +../

 &**+NOP$((8&**>:; RSS#-4 # && C-;/?8=&8 rD   c                    t        t        |             }d|d   |d}t        j                  t	        |            j                         |d<   |S )zIBind the complete canonical product derivation, never a digest tautology."task26-outer-candidate-manifest-v2r   )r!   r   product_bindingmanifest_sha256)r   r   r`   ra   rY   re   )rJ  	validatedr   s      rC   build_outer_candidate_manifestrM  #  sR     )o)>?I6%&89$#H
 #*..81D"E"O"O"QHOrD   c                   t        |      }|j                         sd|j                  v rt        d      | |z  }t	        |       t        t        j                  |j                  d            d      }t        |      h dk7  rt        d      |j                         D ci c]  \  }}|dk7  s|| }}}t        |j                  d	            }t        |j                  d
d            }	|j                  d      dk7  sF|	|d
   k7  s>|j                  d      t        j                  t!        |            j#                         k7  rt        d      |	t%        |      |fS c c}}w )Nr   (outer candidate manifest path is invalidutf-8encodingzouter candidate manifest>   r!   rK  rJ  r   z*outer candidate manifest schema is invalidrK  rJ  r   r   r!   rI  z+outer candidate manifest binding is invalid)r   r   r   r|   r   r   rT   loads	read_textr   r   r   r   rE   r`   ra   rY   re   ri   )
bundler   r   rf   r   r>   rX   r.  rJ  	candidates
             rC    _verify_outer_candidate_manifestrW  1  sX    M"H!7CDDHDTN

4>>7>346PH 8}   EFF%-^^%5%5zsE@Q9QU
%5   /x||<M/NOOHLL!3R89IX"FF(:;;<<)*>>)H-.88:; FGGk$'88s   "E 0E c                  t        | j                  d      | d      }t        | j                  d      | d      }t        |j                  d      | d      }t        | j                  d      | d      }| j                  d	      }| j                  d
      }t        | j                  d      | d      }t        | j                  d            }	|j                         D 
ci c]  \  }
}|
dk7  r|
| }}
}| j                  d      |k7  st        j                  t        |            t        j                  t        |            |j                  d      |k7  st        j                  t        |j                  dd                  	 lt        j                  t        |j                  dd                  	 <t        j                  t        |j                  dd                  |j                  d      dk7  s|j                  d      |k7  s|j                  d      d| dk7  st        j                  t        |j                  dd                  	 |j                  d      dk7  s|j                  d	      |k7  spt        j                  t        |j                  dd                  	 @t        j                  t        |j                  dd                  	 t        j                  t        |j                  dd                  	 |j                  d      |j                  d      k(  s||j                  d      |j                  d      hv s|d||j                  d      |j                  d      |j                  d      |j                  d      d k7  sR|j                  d      t        j                  t        |            j                         k7  s|	j                  d	      |k7  rt        | d!      |||d   |d   |j                  d      |j                  d      |||	|d"
S c c}}
w )#Ncandidate_authorityz
 authoritytask26_contractz Task26 contractlocal_socket_transcriptz local socket transcriptdeployment_wheelsz deployment wheelsr   source_tree_digestcapability_identity_receiptz capability identity receiptruntime_authority_snapshotreceipt_sha256runtime_modecurrent_qualified_candidateregistry_head_sha256r   ledger_head_sha256
chain_headr!   z4task26-local-http-telegram-api-transcript-binding-v1r   z'data/task26-local-http-telegram-api-qa-.jsontranscript_sha256z"task26-deployment-wheel-binding-v1 candidate_product_binding_sha256hermes_wheel_sha256profile_wheel_sha256z.task26-delivery-capability-identity-receipt-v1)r!   r   rh  ri  rj  wheel_digestz- candidate or authority projection is invalid)
r   r]  task26_chain_headauthority_current_candidateauthority_registry_headauthority_ledger_headr\  r^  r_  r[  )r   r   r   r   r  r  rE   r`   ra   rY   re   r|   )resultr   	authoritycontract
transcript
deploymentrV  source_treecapability_identityr_  r>   rX   capability_unsigneds                rC   _candidate_result_projectionrx  R  s>    

#89dV:;NOIvzz"34>N6OPH./&()J $78TFBT:UVJ

-.I**12K!

01&,- "3

/0"
 .3355JC"" 	U
5   	

>"d*  Y08  [!12:==679D  	4b9:
    	2B78
    X\\,%C!DEM>>(#AB>>.)T1>>/*4TF%@A  
2B78
  >>(#'KK>>,-:  
A2FG
    
4b9:
    
5r:;
  >>/0>>012NN01NN12


 F )0:21 $.>>2G#H$.NN3I$J&NN+@A	


 ""#34>>)$789CCEF%))*<=JD6!NOPP%)%l3'01N'O#,==1G#H!*/C!D'':&@#- Cs   &P>c               L   t        |d      }t        |d      }t        | |      \  }	}
}t        |j                  d      d      }|d   |d   k7  rt	        d      t        |d   d	      }t        |d   d
      }|j                  d      dk7  sZ|j                  d      dk7  sF|j                  d      |j                  d      k(  s#|j                  d      |j                  d      k(  rt	        d      |d   |d   k7  slt        |d   d      j                  d      |d   k7  sHt        |d   d      j                  d      |d   k7  s$t        |d   d      j                  d      |d   k7  rt	        d      t        fd|d   |d   |d   |d   |	fD              rt	        d      ||fD ]6  }t        |      }|j                         sd|j                  v s-t	        d       d||||||
|	|d   dd}t        j                  t        |            j                         |d<   |S ) Nsourcer   	installedderivation_inputsouter product derivation inputsr]  z3source and installed tree provenance digests differr[  zsource local socket transcriptz!installed local socket transcriptra  r   rg  z8source and installed socket transcripts were substitutedr\  source deployment wheelsrh  binding_sha256ri  rj  z5source and installed deployment wheel receipts differc              3  (   K   | ]	  }|k7    y wrN   r,  )r   rX   r   s     rC   r   z)build_candidate_parity.<locals>.<genexpr>  s#      	
E 	!!
   r   rm  9source, installed, authority, and outer candidates differr   z'candidate parity bundle path is invalid%task26-cross-mode-candidate-parity-v1)r   ra   r   product_binding_sha256)r!   r   source_bundle_relative_pathinstalled_bundle_relative_pathrz  r|  outer_manifestparity_sha256)rx  rW  r   r   r|   r   r   r   r   r`   ra   rY   re   )rU  r   source_resultinstalled_resultr  r  outer_manifest_relative_pathrz  r|  outer_candidate	outer_sharJ  product_inputssource_transcriptinstalled_transcriptr   rf   r   s    `                rC   build_candidate_parityr    s    *-hGF,-=KPI2R,3/OY /02SN "#y1E'FFNOO()+K #+,.Q 	n-9##N3{B  1##O45  !45##$789 STT"#y1D'EE6-.0JKOO.
 +,- 6-.0JKOO!
 /01 6-.0JKOO"
 012
 PQQ
 	 %&01()34
	 	 TUU02PQH~!3FGG R
 :,'B*H9 /&56F&G	
#H !(y/B C M M OH_OrD   c          
        t        |d      }t        |      h dk7  rt        d      |j                         D ci c]  \  }}|dk7  s|| }}}t        |j	                  d      d      }|j	                  d      }|j	                  d      d	k7  sb|j	                  d
      k7  sN|j	                  d      t        j                  t        |            j                         k7  st        |t              st        d      t        | |      \  }	}
}t        |j	                  d      d      }t        |j	                  d      d      }t        |j	                  d      d      }t        |j	                  d      d      }|j	                  d      |j	                  d      k7  s-|j	                  d      |j	                  d      k7  s	|j	                  d      |j	                  d      k7  s|j	                  d      |j	                  d      k7  s|j	                  d      |j	                  d      k7  s|j	                  d      |
k7  s|j	                  d
      |	k7  sx|j	                  d      |j	                  d      k7  sUt        fd|	|j	                  d
      |j	                  d      |j	                  d
      |j	                  d      fD              rt        d      t        t        t        t        f   |      S c c}}w )Ncandidate parity>   r!   rz  r|  r  r  r   r  r  z"candidate parity schema is invalidr  r  outer manifest parityr   r!   r  r   z"candidate parity digest is invalidrz  zsource candidate parityr|  zinstalled candidate parityr}  r~  r\  r  r]  rh  r  ri  rj  ra   r  c              3  (   K   | ]	  }|k7    y wrN   r,  )r   rX   expected_candidates     rC   r   z*verify_candidate_parity.<locals>.<genexpr><  s#      	
 ''r  rm  r  )r   r   r|   r   r   r`   ra   rY   re   r   rE   rW  r   r
   r   rF   )rU  parityr  r   r>   rX   r.  outerr   r  r  rJ  rz  r|  r  source_deployments     `             rC   verify_candidate_parityr    s    v12H
8} 	 	 =>>%-^^%5%5zsE9OU
%5   HLL!124KLEyy)HX"II<<*+/AA<<(>>)H-.88:;(C(=>>2R3/OY X\\(+-FGF[13OPI/02SN  

&')C 	

'(IMM:N,OO::)*imm<O.PP  !CD/01  !67345  !7845699X)+99'(O;99-./01 	
  

-.

8901;<	
 	
 TUUS&[!8,,cs   K4
K4z0qualification/profile-qualification-receipt.jsonz'qualification/profile-qualification.logselected_qualification_python)-I-mpytestz-qprofile_source_root)executable_roleargvworking_directory_role&supplied_successor_profile_source_treez
749 passed>   r!   status
log_sha256
test_countr`  command_identitysource_root_rolelog_relative_pathrj  recorded_result_textprofile_source_tree_digestc               l   t         j                  |      t        d      t        |       | t        j
                  z  }|j                  d       | t        z  }|j                  t        dz   j                                |j                  d       d|t        |      t        t        ddt        j                         t        |      t        d	
}t!        j"                  t%        |            j'                         |d
<   | t        z  }|j                  t%        |      dz          |j                  d       |S )zJPersist the canonical successful profile qualification before final state.z%profile source-tree digest is invalidrm   r{  
rn   'task26-profile-qualification-receipt-v1  PASS)
r!   r  rj  r  r  r  r  r  r  r  r`     
)r  r  r|   r   #_PROFILE_QUALIFICATION_RECEIPT_PATHparentmkdir_PROFILE_QUALIFICATION_LOG_PATHwrite_bytes_PROFILE_QUALIFICATION_RESULTrV   chmodri   _PROFILE_QUALIFICATION_COMMAND_PROFILE_SOURCE_ROOT_ROLEr   r`   ra   rY   re   )rU  r  profile_wheelqualificationlog_pathreceiptreceipt_paths          rC   #write_profile_qualification_receiptr  e  s    78@@AA]@GGGMU#77H7$>FFHINN5;&@ +M ::5<EEG!(+ ="G !(y/A B L L NG??LYw/%78uNrD   c                  t        |d      }|j                         D ci c]  \  }}|dk7  s|| }}}| t        z  }| t        z  }	t	        |       t	        |	       t        t        j                  |j                  d            d      }
t        |      t        k7  s|j                  d      dk7  s|j                  d      |k7  st        j                  t        |j                  dd	                  	 U|j                  d
      |k7  s@|j                  d      t        k7  s'|j                  d      t        k7  s|j                  d      dk7  st!        |j                  d            t"        us|j                  d      dk7  s|j                  d      t        j%                         k7  s|j                  d      t&        k7  s|j                  d      t)        |	      k7  sk|	j+                         t&        dz   j-                         k7  sC|j                  d      t/        j0                  t3        |            j5                         k7  s|
|k7  rt7        d      |S c c}}w )Nzprofile qualification receiptr`  rP  rQ  z'persisted profile qualification receiptr!   r  r  r   rj  r  r  r  r  r  r  r  r  r  r  z%profile qualification receipt differs)r   r   r  r  r   rT   rS  rT  r   _PROFILE_QUALIFICATION_KEYSr   r  r  rE   r  r  r  r  r   r  ri   
read_bytesrV   r`   ra   rY   re   r|   )rU  rX   r  rj  r  r>   r   r.  r  r  	persisteds              rC   _verify_profile_qualificationr    s    e<=G#*==?#2ic4c=M6MT	?   ??L77H\X

<))7);<1I
 	G33;;x 45;;34%&  8"=>
  ;;-.2FF;;)*.LL;;)*.GG;;|$+L)*#5;;x F*;;*+*3356;;-.();;|$H(== )D088:;;;'(>>)H-.88:;@AANOs
   IIc          
     z   t        | d       t        |       t        |       t        |       t        t        j                  |j	                  d            d      }t        |      h dk7  s|j                  d      dk7  rt        d	      |j                  d
      }|j                         D ci c]  \  }}|d
k7  s|| }}}|t        j                  t        |            j                         k7  rt        d      |j                  d      t        k7  rt        d      t        |j                  d             t        |j                  d      dt!        |j                  dd            t        |j                  d      d             t#        | |j                  d      t!        |j                  dd                  }	t        |j                  d      d      }
t        |	j                  d      d      }t        |	j                  d      d      }t        |j                  d      d      }t%        | |j                  d      |j                  d       t'        |      !       |j                  d"      t'        |      k7  rt        d#      |
|j                  d$      |j                  d$      d%k7  rt        d&      d'|fd|ffD ]J  \  }}t        |j                  |      |      }||j(                  t'        |      d(k7  s=t        d)| d*       t        |j                  d+      d,      }t        |      h d-k7  rt        d.      |j                  d/      }t+        |t,              st        d0      t        |j                  d      d1      }t        |j                  d2      d3      }|j                  d4      }t+        |t               st        d5      t/        | |      \  }}}t1        |t3        | d6z  t5        t6              j9                         j:                  d7   8      t=        t              t!        |d9         t?        |      ||:       tA        |j                  d;      t!        |j                  dd            |<       d=D ]  }t        |j                  |      |      }|j                  d4      }t+        |t               r0t5        |      jC                         sd>t5        |      jD                  v rt        d)| d?      | |z  }t        |       |j                  d"      t'        |      k7  st        d)| d*       t        |j                  d@      dA      }dBD ]O  }| dCz  |z  }t        |       |j                  |jG                  dD      dEz         t'        |      k7  sFt        dF       |S c c}}w )GzGValidate the hash-bound portable expectation before creating a runtime.Trj   rP  rQ  zsealed expected state>   r!   verifierrq  r   hermes_wheelr  trust_boundarydocument_sha256expected_statusr   candidate_parityfrozen_bootstrapr  provenance_helperprofile_qualificationexpected_result_sha256hermetic_ty_attestationlocal_socket_transcriptsindependent_candidate_verifierr:  r!   task26-sealed-final-state-v7z'sealed expected-state schema is invalidr  z'sealed expected-state digest is invalidr  z)sealed evidence trust boundary is invalidr  r:  r6  r   r   r   r7  r  r  r  zsealed local socket transcriptsrz  source candidate projectionr|  installed candidate projectionr  zprofile wheelr  r]  r  rj  ra   z+profile qualification wheel binding differsr[  rz  r|  z.sealed local socket transcript bindings differr  filenamera   sealed  digest differsr   r   >   r!   r.   r,   r-   r1   r   r.   r   r  r  r  r   rO  verification-toolsr  scripts_dirpackage_rootr1   expected_tool_hashesexpected_trust_boundary_sha256$expected_wheelhouse_inventory_sha256expected_wheelhouse_entry_countactual_hermes_wheelactual_profile_wheelr  r   rJ  )r  r  r  r  r   z path is invalidrq  zsealed authority)zregistry.jsonzqualification-ledger.jsonzcandidate-authorityrf  _sha256zsealed authority digest differs)$r   r   rT   rS  rT  r   r   r|   r   r`   ra   rY   re   r*   r  r4  rE   r  r  ri   r   r   r   rW  r   r   r   __file__resolveparentsr   r   !_validate_hermetic_ty_attestationr   r   removesuffix)rU  expected_pathr  r  r?   claimedr>   rX   r.  verified_paritytranscript_bindingssource_projectioninstalled_projectionprofile_bindingr   rf   bindingr   r.   r  r  r   _rJ  targetrq  r   s                              rC   verify_expected_stater    sd    Vt$]\]

=**G*<=H 8}  * 
h	#A	ABCCll,-G-5^^-=Z-=zsEHYAYU
-=HZ'..8!45??AABCC||$%7DEE&x||4F'GH&89X\\*<bAB LL+,.J
	 .'(x||,>CDO
 "/0)  H%'D #K(*J hll?;_MO!,-#4#8#89M#N(7	 8$M(BBFGG#''(AB)--.GH  IJJ	&	-(t (,,u-u5499D8IJJwug_=>> l35RSJ
:   HIInnY'Ggt$@AAX\\"457IJFFJJ/02IJEyy)Hh$CDD<VXNAq/6!55h//199!<
 (=^'L-0<N1O-P(+G(* &./X\\*<bAB'
 (,,u-u5;;/8S)H~))+tH~+++wug-=>??(";;x K$77wug_=>>#$ [13EFI>//$6==**73i?@KE
 
 >?? ? O_ [s   1V7?V7c                ^    | j                         D ci c]  \  }}|dvr|| c}}S c c}}w )N>   package_authorityrehydration_origin_proof)r   )rp  r>   rX   s      rC   _portable_verifier_resultr  S  s@     !,,.(JCGG 	U
(  s   )c               L   t        | d      t              h dk7  rt        d      t        |j                  d      d      }t        |j                  d      d      }j	                         D ci c]  \  }}|dk7  s|| }}}j                  d	      d
k7  sj                  d      |k7  sj                  d      |j                  d      k7  sj                  d      |j                  d      k7  sj                  d      |j                  d      k7  szt        fddD              sft        j                  d            t        usFd   dk  s>j                  d      t        j                  t        |            j                         k7  rt        d      S c c}}w )Nzsealed hermetic Ty attestation>
   r!   
ty_versionr`  r   attestation_sha256binary_member_sha256raw_diagnostics_countraw_diagnostics_sha256corrected_ty_wheel_sha256execution_provenance_sha256z0sealed hermetic Ty attestation schema is invalidr}  zproduct inputshermetic_tyzhermetic Ty bindingr  r!   z(task26-sealed-hermetic-ty-attestation-v1r   r  r  r  c           	   3  ~   K   | ]4  }t         j                  t        j                  |d                   du  6 yw)r   N)r  r  rE   r   )r   fieldattestations     rC   r   z4_validate_hermetic_ty_attestation.<locals>.<genexpr>|  s=      
 !!#kooeR&@"ABdJs   :=)r  r  r`  r  rp   z1sealed hermetic Ty attestation binding is invalid)r   r   r|   r   r   r   r  r  r`   ra   rY   re   )	rX   r   rJ  inputsr  r>   r   r.  r  s	           @rC   r  r  [  s    %!ABK
;   KLL_(()<=?OPF&**]35JKK#.#4#4#6#6ic4#AU:UT	#6   	!%OO??-.2BB??67??678??<(KOOL,II??12??123 

 
  789D./!3??/0>>)H-.88:; LMM3s   2F  F c                   t        d| j                  d      |j                  d      fd| j                  d      |j                  d      fd| j                  d      |j                  d      fd	| j                  d
      |j                  d
      fd| j                  d      |j                  d      fd| j                  d
      |j                  d
      fdd|j                  d      fdd|j                  d      ff       y)zKCompare portable Ty behavior, never path-dependent installed RECORD hashes.z1hermetic_ty_attestation.corrected_ty_wheel_sha256r  ty_wheel_sha256z,hermetic_ty_attestation.binary_member_sha256r  installed_executable_sha256z.hermetic_ty_attestation.raw_diagnostics_sha256r  z-hermetic_ty_attestation.raw_diagnostics_countr  z6hermetic_ty_attestation.receipt.raw_diagnostics_sha256z5hermetic_ty_attestation.receipt.raw_diagnostics_countz;hermetic_ty_attestation.receipt.capability_diagnostic_countr   capability_diagnostic_countz&hermetic_ty_attestation.receipt.status?PASS_CHANGED_SURFACE_WITH_PREEXISTING_OUTSIDE_SCOPE_DIAGNOSTICSr  N)rP   r   )	sealed_ty	ty_resultty_provenance
ty_receipts       rC   _verify_rehydrated_ty_resultr    s%     D9:/0 ?45;< A6767 @56!!"9: I6778 H5667 N<= 9Qx(I)	
+rD   )r   c          	     T   t        |	j                  d      d      }t        |       |t        t              nt        |||f      }t        | |      \  }}}|j                  d      }t        |t              st        d      t        |t        | dz  t        t              j                         j                  d         t!        t"              t%        |d	         t'        |      ||
       t        |      }t)        t        |      d||      }t+        t        |      ||      }t-        | ||
|	|||      }t        |j                  d      d      }t        |j                  d      d      }| t.        z  }t1        |       t3        | t5        j6                  |j9                  d            |j                  d      t;        |            }i ddd|d|d|d|d|j<                  t;        |      dd|j<                  t;        |      dd |d!|t;        | |z        d"d#|t;        | |z        d"d$d%t;        | d%z        d"d&d't;        | d'z        d"d(|	j                  d)      d*t?        j@                  tC        tE        |	                  jG                         d+t;        | d,z        t;        | d-z        |j                  d.      |j                  d/      d0d1|d2|d3   |d3   d4|t"        d5}t?        j@                  tC        |            jG                         |d6<   |S )7zIBuild, but do not write or freeze, one successor expected-state document.rY  zcandidate authority resultr   r.   zwheelhouse entries are invalidr  r  r  r1   r  r6  r7  r  )r   r  r  r  r  r  rz  r  r|  r  rP  rQ  r]  r  r!   r  r   r  r:  r  r  r  r  r   r  )r   ra   r  r  z-verification-tools/task26_frozen_bootstrap.pyr  z2verification-tools/independent_verify_candidate.pyr  r  r  rq  z!candidate-authority/registry.jsonz-candidate-authority/qualification-ledger.jsonrc  rd  )registry_sha256zqualification-ledger_sha256rc  rd  r  r  r[  r  )r  r  r  )$r   r   r  r   r8   r   rW  r   r   r|   r   r   r   r  r  r  r   r*   rE   r   r4  r  r  r  r   r  rT   rS  rT  ri   r   r`   ra   rY   r  re   )rU  r   r  r:  r  r  r  verifier_relative_pathprovenance_relative_pathverifier_resultsource_verifier_resultr  r  r  r   authority_resultwheelhouse_inventoryr  outer_product_bindingwheelhouse_entriesruntime_expectationr?  sealed_ty_attestationr  r  r  profile_qualification_pathr  r   s                                rC   build_expected_stater$    s   & 124P ''78  	'(E
  #C,#Aq
 .11)<($/9::6!55h//199!<
 (=^'L-0 !34.
 ),,>(?(* /07./),	N >$%)-
 .),($?'E%A  X&(E #[)+K "(*M!M'(9

-777IJ#4#8#89M#N(7	:#0:#,:# 	/:# 	,^	:#
 	"#8:# 	$))!,/
:# 	%**!-0
:# 	*:# 	3!&+A"AB
:#& 	5!&+C"CD
':#. 	L!HH
/:#: 	)D!MM	+
;:#J 	?..x8K:#L 	!'../@A#

)+Q:#R 	*<<  ,7HH, %5$8$89O$P"2"6"67K"L	
S:#f 	,g:#h 	#'(AB-.GH%
i:#p "7(s:#Hv #*..81D"E"O"O"QHOrD   controlled_cwdc          	        t        |d      }t        |j                  d      d      }|j                  d      dk7  s|j                  d      dk7  s~|j                  d      d	uskt        |      d
dhk7  s[t        |j                  d      t              r<|Et        t	        |d               j                  d      |j                  d      k7  rt        d      t        | j                  d      d      }g }dD ]  \  }}	|j                  |      }
t        |
t              st        d      t        |
      }	 |j                  d      }|j                  |j                  d            j                         }|j                         }|j                         s.t        j                  |j                        r|j                  dk7  rt        d      t        |j                  |	      |	 d      }|j                  d      }t        |t               st        d      |D cg c]2  }t        |t"              r |j                  d      |k(  rt        |d      4 }}t%        |      }t'        |      dk7  s8|d   j                  d      |k7  s!|d   j                  d      |j(                  k7  rt        d      |j+                  |	||||j(                  d         d!d"dd	d#|d$S # t        t        f$ r}t        d      |d}~ww xY wc c}w )%z@Bind imported package origins to exact installed inventory rows.zrehydrated origin prober   zrehydrated module originsr!   z!task26-rehydrated-origin-probe-v1isolatedrp   pythonpath_presentFr  r  cwdNTr  z,rehydrated Python isolation proof is invalidr  runtime distributions)r  r
  z#rehydrated module origin is invalidz1module origin is outside rehydrated site-packagesz.rehydrated module origin is not a regular filer9  installed_inventoryzinstalled inventory is invalidrf   zinstalled inventory rowr   ra   r   z5module origin differs from installed RECORD inventory)r  r  r  ra   r   r   r  r  )r!   r   r   )r   r   r   r   rE   r   r  r|   r   r   OSErrorrr   
is_symlinkrt   rv   rz   r   r   ri   r   r   r   )r  site_packagesprober&  evidencer   r  
proof_rowsr  r1  raw_pathrf   resolvedr   r   metadatar  r   r   matchesrg   s                        rC   verify_runtime_origin_proofr7  V  s    u78Hhll9-/JKGX"EE<<
#q(<<,-U:w<I}55(,,u-s3&S%)*22$2?%%T%23 GHHGKK8:QRM*,JK;;v&(C(BCCH~	||4|0H++M,A,A,A,NOXXZH
 ==?!<< 0 01  A%MNN}0064&8NO $$%:;	)T*=>> !
 #t$H)D C23  	 

 X&LAqz~~h'61qz~~f%)9)99TUU $ +3  ((	
G LZ 57#$)

  I $ 	C	
s   7AK"7L"L1K==Lc                   t        | j                  d      d      }i }dD ]U  }t        |j                  |      | d      }|j                  d      }t        |t              rt	        d |D              rt        | d      |D cg c]^  }dt        t        |j                  d	d
                  j                  vr,t        |j                  d	d
            j                  d      s|` }}dD ci c]  }||j                  |       }	}t        |j                  d      | d      }
|
j                  d      |	d<   ||	d<   t        j                  t        |            j                         |	d<   |	||<   X t        | j                  d      d      }d|j                  d      |j                  d      d|| j                  d      dS c c}w c c}w )Nr  r+  r!  r9  r,  c              3  >   K   | ]  }t        |t                 y wrN   )r   r   )r   r   s     rC   r   z$_portable_runtime.<locals>.<genexpr>  s      6
1>#
3%%r   z installed inventory is invalidr   rf   r   z.dist-info/RECORD)r&  distribution_versionmetadata_sha256wheel_filenamer$  wheel_record_sha256portable_record_projectionz portable RECORD projectionprojection_sha256r'  installed_inventory_sha256interpreterzruntime interpreterr   ra   r   )ra   r   r   )r!   rA  r  r   )r   r   r   r   r   r|   r   rE   r   r   r`   ra   rY   re   )r  r  r0  r1  r  raw_inventoryr   r   r>   portable_distributionrF  rA  s               rC   _portable_runtimerD    s   GKK8:QRM02%}0064&8NO$(()>?-.# 6
1>6
 3
 v%DEFF %
$4CGGFB$7 89???+,556IJ $ 	 

4
 !!#&& 	 
4
 9:f/0

 FP^^F
AB 8A34>Enni ?

)+ 	:; (=t$G &H '++m46KLK8!ooh/"y1
 0N3 =

4
s   A#G0Gc                     t         j                  j                         D  ci c]*  \  } }| j                  d      s| j                  d      s| |, }} }|j	                  dt         j
                  ddd       |S c c}} w )NPYTHONPIP1)PYTHONNOUSERSITEPIP_CONFIG_FILEPIP_NO_INDEXPIP_DISABLE_PIP_VERSION_CHECK)rx   environr   r   rd   devnull)r>   rX   environments      rC   _clean_environmentrP    s     ****,,JC~~h'u0E 	U
,  
  #!zz-0		
 s   /A:c               
   t        |d       t        |j                               rt        d      t	        j
                  t        |       d|t        |      dd      }t        |j                               rt        d      |S )NTrj   z"controlled Python cwd is not empty)checkr*  envcapture_outputtextz)Python subprocess wrote to controlled cwd)r   r   iterdirr|   
subprocessrunr   r   )commandr&  rO  	completeds       rC   _run_pythonr[    sy     ^t,
>!!#$=>>WI >!!#$DEErD   c                    d|fd|ffD ]J  \  }}t        | j                  |      |      }||j                  t        |      dk7  s=t	        d| d       y )Nr  r  r  r  r  )r   r   r   ri   r|   )r?   r  r  r   rf   r  s         rC   _verify_bound_wheel_hashesr]     se     
&	-(t (,,u-u5499D8IJJwug_=>>rD   )r   temp_parentc                  '()*+, t         |||      }t        ||j                  d      ||f      }t        t	        j
                  d|            }|j                  d       |dz  ''j                  d       t               (	 |d	z  }	t        t        j                  d
dd	t        |	      g'(       |	j                  d       |	t        j                  dk(  rdndz  *t        ||||f      }t        |||       t!        ||      }
t        *      d
ddddddgd |
D        t        |      t        |      }t        |'(       t        t        *      d
ddg'(      j"                  j%                         }t        |      }|dz  + t        t'        |d   d      d         z  }d}t        t        *      d
d|t        |      t        |	      t        |      t        |      t        |      t        |j                  dd            g
'(      j"                  }t'        t)        j*                  |      d      }t'        |j                  d       d!      }t-        |||j                  d"      '#      }||d$<   t/        |      }|j                  d%      }||j                  d&      k7  rt1        d'      t3        |j                  d(      t        |j                  dd            t'        t5         t        t'        t'        |j                  d)      d*      j                  d+      d,      j                  dd                  d-   d.      /      }|t1        d0      t7        t8        t:           |j                  d1g             D cg c]V  }t'        |d2      j                  d3      |j                  d4      k(  r'|t        t'        |d2      j                  d            z  X }}t=        |      d5k7  rt1        d6      |d7z  } d8z  } d9z  }t        t        *      d
t        |      d:t        |d;z        d<t        |d=         d>t        |d4         d?t        |d@         dAt        |      dBt        |      g'(      }t'        t)        j*                  |j"                        dC      }t'        |j                  dD      dE      }t'        |j                  dF      dG      }t?        ||||       +jA                  tC        |      dHz          +j                  dI        t        t'        |dJ   dJ      d         z  ,tE         |j                  d)      t        |j                  dd            K      )dk '()*+,fdL}  | dMdN      }! | dOdP      }"tG        |!dQ      )j                  dQ      k7  stG        |"dR      )j                  dR      k7  rt1        dS      tI         |j                  dT      |!j                  dU      tK        |      V      }#|"j                  dU      |#j                  dW      k7  rt1        dX      t'        |"j                  dY      dZ      }$t'        |j                  d[      d\      }%|"j                  d]      |j                  d^      k7  s|"j                  d      |j                  d      k7  s|$j                  d_      |%j                  d_      k7  sj|$j                  d`      |%j                  d`      k7  sGtM        jN                  tC        tQ        |"                  jS                         |j                  da      k7  rt1        db      tU        |j                  dc      |t        |j                  dd            t'        |j                  d&      dd      |e      |"df<   ||"d(<   |#|"dT<   ||"dg<   |"	 t        ||||f       t        |||       |jW                         rc|jY                  dh      D ]7  }&	 |&j[                         r|&j                  |&j]                         rdndI       9 ta        jb                  |dij       S S c c}w # t^        $ r Y cw xY w# |jW                         rr|jY                  dh      D ]F  }&	 |&j[                         r|&j                  |&j]                         rdndI       9# t^        $ r Y Dw xY w ta        jb                  |dij       w w xY w# 	 t        ||||f       t        |||       |jW                         rr|jY                  dh      D ]F  }&	 |&j[                         r|&j                  |&j]                         rdndI       9# t^        $ r Y Dw xY w ta        jb                  |dij       w w # |jW                         rr|jY                  dh      D ]F  }&	 |&j[                         r|&j                  |&j]                         rdndI       9# t^        $ r Y Dw xY w ta        jb                  |dij       w w xY wxY w)lzECreate an isolated offline runtime, rerun verification, and scrub it.)r  r  r   r   ztask26-rehydrate-)prefixdirrm   zcontrolled-cwdr{  venvr  r  r&  rO  ntzScripts/python.exez
bin/pythonpipinstallz
--no-indexz	--no-depsz--no-compilec              3  2   K   | ]  }t        |        y wrN   r   )r   rf   s     rC   r   z'rehydrate_and_verify.<locals>.<genexpr>@  s     5$4Dc$i$4r   z-cz9import sysconfig; print(sysconfig.get_paths()['purelib'])zruntime.jsonr  zprovenance helperr   a=  import hashlib,importlib.util,json,os,pathlib,sys;p=pathlib.Path(sys.argv[1]);s=importlib.util.spec_from_file_location('sealed_provenance',p);m=importlib.util.module_from_spec(s);s.loader.exec_module(m);r=m.collect_installed_runtime(venv=pathlib.Path(sys.argv[2]),site_packages=pathlib.Path(sys.argv[3]),profile_wheel=pathlib.Path(sys.argv[4]),hermes_wheel=pathlib.Path(sys.argv[5]));a=m.nonportable_installed_record_audit(r,install_role='rehydrated',candidate_digest=sys.argv[6]);import checkin_cli,gateway;o={'schema':'task26-rehydrated-origin-probe-v1','isolated':sys.flags.isolated,'pythonpath_present':'PYTHONPATH' in os.environ,'cwd':os.getcwd(),'modules':{'gateway':gateway.__file__,'checkin_cli':checkin_cli.__file__}};print(json.dumps({'runtime':r,'record_audit':a,'origin_probe':o},sort_keys=True,separators=(',',':')))r   r   zrehydrated probe documentr  zrehydrated runtimeorigin_prober%  r   record_auditr  z5rehydrated runtime differs from sealed portable bytesr  r  r  r  r  r  zouter product bindingr  z-sealed corrected Ty wheelhouse is unavailabler.   zwheelhouse rowra   r  rp   z(sealed corrected Ty wheel is unavailablezty-attestationz/verification-tools/task26_hermetic_ty_runner.pyz,verification-tools/task26_ty_surface_gate.pyz--sourcez'gateway/platforms/nutrition_coaching.pyz
--ty-wheelr   z--expected-wheel-sha256z--expected-executable-sha256r  z--gate-toolz--output-directoryzrehydrated hermetic Ty resultexecution_provenancez"rehydrated Ty execution provenancesurface_receiptzrehydrated Ty surface receiptr  rn   r  r  c           
        t        t        j                  | d                  }|z  }t        t        	      dt              t        |      dt        
      g      }t	        t        j                  |j                        |      S )Nr   r  z--rehydrated-runtimerc  )r   rE   r   r[  r   rT   rS  stdout)relative_keyr   r   target_bundlerZ  rU  r&  rO  r  pythonruntime_pathr  s        rC   run_verifierz*rehydrate_and_verify.<locals>.run_verifier  s    C

< <=>H"X-M#KM&*%  .'I 4::i&6&67??rD   r  z!rehydrated source verifier resultr  z$rehydrated installed verifier resultrz  r|  z4rehydrated source/installed candidate parity differsr  r]  r  r  z/rehydrated profile qualification source differsrY  zrehydrated authorityrq  zexpected authorityr  r  rc  rd  r  z=rehydrated verifier result differs from sealed expected stater:  zoriginal portable runtime)r   r=  r>  installed_record_provenancer  *F)ignore_errors)rn  rE   r   rE   rG   dict[str, Any])2r  r   r   r   tempfilemkdtempr  r  rP  r[  sys
executablerE   rx   r   r]  r   rm  r   r   rT   rS  r7  rD  r|   r  rW  r
   r   rF   r   r  r  rY   r  rx  r  ri   r`   ra   r  re   rG  existsrglobr.  is_dirr-  shutilrmtree)-rU  r  r  r  r   r^  r?   r   	temporaryrb  dependency_pathsrf  site_packages_textr/  helperr0  probe_outputprobe_documentruntime_documentr   rehydrated_portabler@  r  r   ty_wheel_matches	ty_output	ty_runnerty_gatety_completedr  r  r  rr  r  rp  r  rq  expected_authorityrf   r&  rO  r  rp  rq  r  s-   `                                      @@@@@@rC   rehydrate_and_verifyr    s>
    %!#	H ,\"&5I
 X%%-@kRSIOOE!11Ne$$&KU>6!^^T4T;)#	

 	

5D-lS/*L9
	
 	#8\=I-j)DK
 6$45
 
 
 	N	
 )FK	 *#	
 & 	 /0 >1#H013FG
 
4 	& #FFD	M"M"L!HLL!3R89 *#
 & 	  !JJ|$&A
 #y)+?
 3~.)	
 ,8(/0@A)--n=(,,/A"BBTUU5LL23 .@"!EF#0# (-? @ 2 "c"233 #or2  (
	& LMM DL)--	2*FG
Gs,-11(;}}89: WS*:;??PQQG 	 
  A%GHH 00	NN	II"FIM$MMN$Q'()I9:;.I456G$I" *#'
* JJ|**+-L
	  MM010
 MM+,.M

 	%		
 	  +;!<u!DE5!CHZ(*5oF
 
 )LL+,"8<<0BB#GH
	@ 	@" %)+N
 ,.T
 )XFzz(#$+FEzz+&' STT =LL01'4'8'89M'N!,]!;	!
 ::*+/D/H/H(0
 
 NOOJJ,-/E
	 %LL%';
 JJx HLL1B$CCzz,->P1QQ}}34!%%&<=>}}12!%%&:;<~~3F;<ik||456
 O  1@A !$X\\2Db%I!J*1LL!34/+ -@	 	,- -6()*?&'-9)*	>'!. =
 'x}M!%OOC0D??,$

DKKM5uE	 1 iu= "e
p #  !%OOC0D??,$

DKKM5uE"  1 iu= "	>'!. =
 'x}M!%OOC0D??,$

DKKM5uE"  1 iu= "y!%OOC0D??,$

DKKM5uE"  1 iu= "s   	K=c Aa !Oc /a1a#a c 	aa%c:b/
c#b/.c/	b;8c:b;;che=:$he/h0#eh	e 	he 	 h=%h#g
3h4#g
h	g$!h#g$$hhc                    t        j                         } | j                  dt               | j                  dt               | j                  dt        d       | j                  dt        d       | j                  dt               | j                  d	t               | j	                         }	 t        |j                  |j                  |j                  |j                  |j                  |j                  
      }t3        t!        j4                  |dd             y# t        t        t        j                  t         j"                  f$ r}dt%        |      d}t'        |t(              r>|j+                  |j,                  |j,                  |j.                  |j0                  d       t3        t!        j4                  |d      t6        j8                         Y d }~yd }~ww xY w)NrU  )r  expected_statez--hermes-wheelT)r  requiredz--profile-wheelz--wheelhousez--temp-parent)r  r  r   r^  TASK26_REHYDRATION_FAIL)r  r#  )first_invariant_keyrf   r?   r@   )r6   )filerp   r2   r5   r   )argparseArgumentParseradd_argumentr   
parse_argsr  rU  r  r  r  r   r^  r-  r|   rW  SubprocessErrorrT   JSONDecodeErrorrE   r   r:   rd   r>   r?   r@   printrU   ry  stderr)parserargsrp  r   failures        rC   mainr  >  s   $$&F
t,
(t4
(tdC
)tD
T2
d3D#%KK**,,((
F 
$**Vt

CD7 	""	  0#h&
 c,-NN+.77GG #!jj	 	JJ 	
 5s   2AD! !-G$BGG$__main__)rO   z$Sequence[tuple[str, object, object]]rG   rH   )rX   rF   rG   bytes)rf   r   rG   rE   )rf   r   rk   boolrG   rH   )rX   rF   r   rE   rG   rv  )rX   rE   rG   rE   )rf   r   rG   r)   )r   r   rG   z*tuple[list[Path], list[dict[str, object]]])r   z+Sequence[tuple[Path, Mapping[str, object]]]rG   rH   )r   Path | Noner   Sequence[Path]rG   r)   )r   r  r?   rF   r   r  rG   r)   )r   r  r   Mapping[str, object]rG   z
list[Path])rX   rF   rG   r  )r  rF   rG   rH   )
rX   rF   r  rE   r   rE   r  r  rG   r)   )r6  rF   r8  rF   r   rE   r=  r  r>  r  rG   r)   )rJ  r  rG   r)   )rU  r   r   rE   rG   z"tuple[str, str, dict[str, object]])rp  r  r   rE   rG   r)   )rU  r   r   rE   r  r  r  r  r  rE   r  rE   r  rE   rG   r)   )rU  r   r  rF   r  rE   rG   r)   )rU  r   r  rE   r  r   rG   r)   )
rU  r   rX   rF   r  rF   rj  rE   rG   r)   )
rU  r   r  r   r  r   r  r   rG   rv  )rp  r  rG   r)   )rX   rF   r   rE   rJ  r  rG   r)   )
r  r  r  r  r  r  r  r  rG   rH   ) rU  r   r   rE   r  r  r:  r  r  r  r  r   r  r   r  rE   r  rE   r  r  r  r  r  rE   r  rE   r  rE   r   r  rG   r)   )
r  r  r/  r   r0  rF   r&  r  rG   r)   )r  r  rG   r)   )rG   zdict[str, str])rY  zSequence[str]r&  r   rO  zMapping[str, str]rG   z subprocess.CompletedProcess[str])r?   r  r  r   r  r   rG   rH   )rU  r   r  r   r  r   r  r   r   r  r^  r  rG   r)   )rG   r  )]__doc__
__future__r   r  r`   rT   rx   rer~  rr   rW  ry  rw  r   email.parserr   email.policyr   pathlibr   typingr   r   r	   r
   packaging.requirementsr   r   packaging.versionr   r   -gateway.platforms.task26_candidate_derivationr   r   r   *gateway.platforms.task26_runtime_authorityr   r*   __annotations__r/   ra   rU   rV   re   r8   compiler   r  r|   r:   rP   rY   ri   r   r   r   r   r   r   r   r   r   r   r  r4  rG  rM  rW  rx  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r$  r7  rD  rP  r[  r]  r  r  rI   
SystemExitr,  rD   rC   <module>r     s{   N "    	 	    
   $ !  / / B 5 
 I 2#	
	+ &+A!%! !F 0 ,  5
(
( 

%!	
 &( ik
( $ 
  RZZ	* bjj*+
 ;0;	; /4 D'6$N 
 / F!U7!U	!UH## $# 	#L$$$ $	$
 $N(<0$)RXHH H 	H
 +H HV666 	6
  46 "66 6r)99!$9'9B_ _+.__DQQ Q (	Q
 +Q "%Q %(Q #&Q Qh@-@- @-9<@-@-F '+6' # #''P"Q 6(3" 
 E  ,   !$ 	
 D/// !'	/
 / /dWWW 	W
 W Wt// / *	/
 /d2#2#2 (2 %	2
 
2J #!QQ Q +	Q
 (<Q 2Q Q Q  Q "Q *Q 1Q "%Q %(Q #&Q  !Q" #Qr #'M!MM M
  M M`0f"  #	
 &,	?"	?26	?GK	?		?$ ##o>o>o> 	o>
 o> o> o> o>d	.b z
TV
 rD   