
    |xj@             	      |   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.d!Z/d"Z0d#Z1d$d%d&d'd(d)e/gZ2 ejZ                  d*      Z3 G d+ d,e4      Z5	 	 	 	 d^d-Z6d_d.Z7d`d/Z8dd0dad1Z9dbd2Z:dcd3Z;ddd4Z<	 	 	 	 ded5Z=	 	 	 	 dfd6Z>	 	 	 	 	 	 dgd7Z?	 	 	 	 	 	 	 	 dhd8Z@	 	 	 	 	 	 did9ZAdjd:ZBdkd;ZC	 	 	 	 	 	 	 	 	 	 dld<ZD	 	 	 	 	 	 	 	 	 	 	 	 dmd=ZE	 	 	 	 dnd>ZF	 	 	 	 	 	 dod?ZG	 	 	 	 	 	 dpd@ZH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dqdAZI	 	 	 	 	 	 	 	 drdBZJ edC      ZK edD      ZLdEg dFdGdHZMdIZNdJZOh dKZP	 	 	 	 	 	 	 	 dsdLZQ	 	 	 	 	 	 	 	 	 	 dtdMZR	 	 	 	 	 	 	 	 	 	 dudNZSdvdOZT	 	 	 	 	 	 	 	 dwdPZU	 	 	 	 	 	 	 	 	 	 dxdQZVddR	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dydSZWddT	 	 	 	 	 	 	 	 	 dzdUZXd{dVZYd|dWZZ	 	 	 	 	 	 	 	 d}dXZ[	 	 	 	 	 	 	 	 d~dYZ\dddZ	 	 	 	 	 	 	 	 	 	 	 	 	 dd[Z]dd\Z^e_d]k(  r e` e^             y)zHOffline 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3.12@f4a2265141525dc8005430e8afdf61145ff88c24a7fc00aad2d7609b3f2ce86a@4b24118c95c05fa392f976a69034da7e7d24f63d098b32ef5c252537ab13da61z	sealed-tycheck'gateway/platforms/nutrition_coaching.pyz--output-formatgitlab--python-versionz^[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)selfrD   rE   rF   	__class__s       /home/cube/projects/richard/traning coach/.omo/evidence/task26/task26-ty-python312-prefreeze-st_01a0195c-v33/gateway/platforms/task26_final_state.pyrC   zInvariantMismatch.__init__a   s-    .se45     )rD   strrE   objectrF   rL   returnNone)__name__
__module____qualname__rC   __classcell__)rH   s   @rI   r@   r@   `   s     rJ   r@   c                >    | D ]  \  }}}||k7  st        |||       y N)r@   )checksrD   rE   rF   s       rI   _require_invariantsrV   h   s,     "(XvX#C6:: "(rJ   c                R    t        j                  | dddd      j                         S )NFTr2   )ensure_asciir6   r7   	allow_nan)jsondumpsencodevalues    rI   	canonicalr_   p   s+    :: fhrJ   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   rI   <lambda>zsha256_file.<locals>.<lambda>}   s    &++k":rJ   rJ   )hashlibsha256openiterupdate	hexdigest)pathdigestchunkrd   s      @rI   sha256_filero   z   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)rl   rq   inforE   modess        rI   _privater      s    ::<D(t||dllH'UENeU^ET\\"%;;"**,&dmmq0<<%U2;D6BCC 3rJ   c                    t        | t              rt        d | D              rt        | d      t	        t        t
        t        f   |       S )Nc              3  >   K   | ]  }t        |t                 y wrT   )
isinstancerK   ).0rD   s     rI   	<genexpr>z_object.<locals>.<genexpr>   s     )Tesjc.B*Be   z is invalid)r   dictanyr   r
   rK   r   )r^   labels     rI   _objectr      s@    eT"c)Te)T&TE7+.//S#X&&rJ   c                J    t         j                  d|       j                         S )N-)_NORMALIZE_PROJECTsublowerr]   s    rI   _normal_projectr      s    !!#u-3355rJ   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     rI   r   z"_wheel_identity.<locals>.<genexpr>   s3     ZTYDts+GttDz7G7G/GGTYs   46zwheel members are invalidz.dist-info/METADATArv   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rT   rK   )r   r^   s     rI   r   z"_wheel_identity.<locals>.<genexpr>   s     [3Y#e*3Y   zRequires-Dist)projectversionrequires_distrg   sizemode)r   zipfileZipFilenamelistlensetr   r   endswithr   r   
parsebytesrc   
BadZipFilerK   getstripr   r   sortedget_allr   ro   rx   st_sizer   r|   )rl   archivenamesr   metadata_namesmessageexcr   version_textr   requirementss              rI   _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 )NTrp   c                    | j                   S rT   )r   )items    rI   re   z#_wheelhouse_files.<locals>.<lambda>   s    DIIrJ   )rD   zwheelhouse symlink is forbidden)relative_pathr   z.whlz.wheelhouse contains an unbound non-wheel entryc                B    | j                        j                         S rT   )relative_toas_posix)rl   roots    rI   re   z#_wheelhouse_files.<locals>.<lambda>   s    (8(8(>(G(G(IrJ   c                    t        | d         S )Nr   r   )rows    rI   re   z#_wheelhouse_files.<locals>.<lambda>   s    CO0D,ErJ   )r   popr   r~   scandirr   rl   rw   rx   r{   r|   r   ry   appendr   r   r   rz   suffix)r   wheelsr-   pendingrq   entryrl   r   s   `       rI   _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 rJ   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: rv   T)prereleaseszdependency version mismatch: )r   rK   r   r   addr   listr   r   urlmarkerevaluater   r   	specifiercontains)r   projects
identitiesrl   identityr   r   rD   _pathr   rawrequirementr   
dependencyselected_versions                  rI   _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   rx   r   r|   r   rf   rg   r_   rk   )
wheelhouserequired_wheelsrl   root_wheelsdependency_wheelsr.   pathsr-   r   r,   payloads              rI   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   rf   rg   r_   rk   r   )r   rE   r   documentr.   r-   r   rF   s           rI   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MrJ   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   rK   r   is_absoluter   r   )r   	inventoryr.   r   r   r   relatives          rI   _bound_wheel_pathsr   U  s     mmI&Ggt$@AA?@@	Ec2377?+8S)H~))+tH~+++@AAZ(*+  LrJ   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_direct_urlraw_direct_url_sha256raw_installed_record_sha256raw_installed_record_portable raw_direct_url_record_row_sha256!raw_installed_records_nonportableN%_contains_nonportable_record_material)r   rD   r   s      rI   r   z8_contains_nonportable_record_material.<locals>.<genexpr>o  s>      
 +	T 	 
; 5T:
; +s    c              3  2   K   | ]  }t        |        y wrT   r   r   r   s     rI   r   z8_contains_nonportable_record_material.<locals>.<genexpr>~  s     Q548>5r   F)r   r   r   itemsr   r]   s    rI   r   r   m  sN    % 
 #[[]
 
 	
 %Q5QQQrJ   c           	     |   t        | d      }t        |j                  d      d      }t        |j                  d      d      }t        |j                  d      d      }|j                  d      }|j                  d	      }|j                  d
      dk7  s`t        |t              rP|d d ddgk7  sFt	        |      s;|j                  d
      dk7  s'|ddddk7  st        |t              rt        |      dk7  rt        d      t        |j                  d      d      }t        |      ddhk7  rt        d      dD ]  }t        |j                  |      d| d      }	|	j                  d      }
t        |
t              st        d      |
D cg c]2  }t        |t              r |j                  d      d k(  rt        |d!      4 }}t        |      d"k7  rt        d#      |d$   }t        |j                  d%      d&      }|	j                  d'      }|j                  d(      }t        |      h d)k7  s|j                  d*      |k7  st        |t              r|j                  d+      rt        |      h d,k7  s|j                  d
      d-k7  s|j                  d.      |	j                  d/      k7  so|j                  d0      |	j                  d1      k7  sL|j                  d2      |k7  s8|j                  d3      |k7  s$t        j                  t        |xs d4            t        d5       d6d7g}t        ||d8      D ]  \  }\  }}	t        |d9      }|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>d4                  )t#        |j                  d?            t$        us	|d?   d"k  st        d@       y c c}w )ANportable installed runtimeorigin_proofzportable origin proof	executionzportable origin executioninterpreterzportable interpreterr   modulesr!   $installed-golden-runtime-portable-v2          task26-installed-origin-proof-v1private_empty_controlledTFcwd_authoritypython_isolatedpythonpath_inheritedz*portable installed origin proof is invaliddistributionsportable runtime distributionsprofilehermesz,portable installed distributions are invalidr  r  z	portable  distributioninstalled_inventoryz'portable installed inventory is invalidkind'canonical_direct_url_archive_provenancez!portable direct URL inventory rowrv   z2portable direct URL provenance cardinality differsr   
provenancezportable direct URL provenancewheel_sha256dist_info_member_path>   r  rl   r  rl   z.dist-info/direct_url.json>   r!   distributionarchive_sha256sealed_wheel_sha256r  sealed_wheel_basenamez(task26-portable-direct-url-provenance-v1r  distribution_namer"  wheel_filenamer!  r   r   z)portable direct URL provenance is invalidgatewayr  checkin_clir  strictzportable origin moduleinstalled_relative_path>   r   modulerg   r  r+  r,  r   rg   r   z1portable installed origin module proof is invalid)r   r   r   r   r   r   r   r   r   rK   r   _HEX_DIGEST	fullmatchzipr   r   r   typeint)runtime_portableruntimeproofr  r  interpreter_versionr  r  roler  r   r   direct_rows
direct_rowr  r  member_pathexpected_pairsr   r,  r   s                        rI   "_validate_portable_origin_contractr;    s   &(DEGGKK/1HIE		+.0KLI'++m46LMK%//)4ii	"GH!GG-t4r"q"g-0999X"DD7#$)

 '4(w<1EFFO$&FM =i22GHH%d#ym%D
 !$$%:;	)T*FGG !
 #t$89 C<=  	 
 {q QRR ^
NN<(*J

 $''7 nn%<=
O==~~f%4k3/''(DE: ~~h'9:~~n- 345~~56 012~~34D~~./<?$$S);%<=EHII] &^ ,-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S
s   
7P9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  s|j	                  d
      |k7  su|j	                  d      |k7  sa|j	                  d      dusN|j	                  d      t        j                  t        |            j                         k7  st        |      ddhk7  rt        d      dD ]  }
t        |j	                  |
      |
 d      }t        |	j	                  |
      |
 d      }|j	                  d      }t        |      h dk7  sa|j	                  d      |j	                  d      k7  s=|j	                  d      |j	                  d      k7  s|j	                  d      |j	                  d      k7  st        j                  t        |j	                  dd                  	 |j	                  d      dust        j                  t        |j	                  dd                  	 |j	                  d      |j	                  d      k7  sb|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_roler  candidate_digest0raw_direct_url_hash_equality_required_or_claimedz4nonportable installed RECORD audit schema is invalidr=  r  znonportable audit distributionsr  r!   z,task26-nonportable-installed-record-audit-v1r>  r?  r@  Fr  r  z5nonportable installed RECORD audit binding is invalidr  z raw RECORD auditz portable distributionpath_dependent_classifications>
   reasonr  r  r   portable_projection_sha256r   r   rA  r   +raw_direct_url_equality_required_or_claimedr  r#  r  rC  !portable_record_projection_sha256r   r   r   r   r   rD  rB  Avalidated_path_dependent_installer_material_may_change_raw_recordc              3  $   K   | ]  }|d v 
 yw)>   path_dependent_launcherdirect_url_raw_record_hashconsole_entry_point_launcherdirect_url_absolute_file_urlN r  s     rI   r   z5_validate_nonportable_record_audit.<locals>.<genexpr>0  s&      	 ,D  ,s   z1nonportable installed RECORD audit row is invalid)r   r   r   r  r   rf   rg   r_   rk   r-  r.  rK   r   r   r   r   )r^   r>  r?  r2  auditrD   r   unsignedaudit_distributionsportable_distributionsr6  r   portableclassificationss                 rI   "_validate_nonportable_record_auditrS    s    E?@E
5z   OPP+0;;=R=ic4C><QT	=HR!		/"$E %_-/O 			(MM99^$499'(,<<99GHPUU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$$CGG3R89  ww9:ww./0wwDEURwwx RSot4&_)=">> 	 ,	 	 PQQg &h LK Ss   L	L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|	dS )zDValidate both raw audits without comparing their nonportable hashes.originalr>  r?  r2  
rehydratedr  zoriginal portable distributionsz!rehydrated portable distributionsr  z	original r  zrehydrated rE  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@  rC  )rS  r   r   r   )rU  rW  r?  original_runtime_portablerehydrated_runtime_portableoriginal_auditrehydrated_auditoriginal_distributionsrehydrated_distributionsprojection_digestsr6  original_distributionrehydrated_distribution
projections                 rI   $verify_nonportable_record_audit_pairre  ?  sS    8)2	N :!)4	 %!%%o6)  '#''8+  -/% '"&&t,	$}.M!
 #*$((.+dV=0Q#
 +../

 &**+NOP$((8&**>:; RSS#-4 # && C-;/?8=<A&8 rJ   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   rf   rg   r_   rk   )rh  	validatedr   s      rI   build_outer_candidate_manifestrk  y  sR     )o)>?I6%&89$#H
 #*..81D"E"O"O"QHOrJ   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!   ri  rh  r?  z*outer candidate manifest schema is invalidri  rh  r?  r   r!   rg  z+outer candidate manifest binding is invalid)r   r   r   r   r   r   rZ   loads	read_textr   r  r   r   rK   rf   rg   r_   rk   ro   )
bundler   r   rl   r   rD   r^   rN  rh  	candidates
             rI    _verify_outer_candidate_manifestru    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?  r  r  r  wheel_digestz- candidate or authority projection is invalid)
r?  r{  task26_chain_headauthority_current_candidateauthority_registry_headauthority_ledger_headrz  r|  r}  ry  )r   r   r   r  r-  r.  rK   rf   rg   r_   rk   r   )resultr   	authoritycontract
transcript
deploymentrt  source_treecapability_identityr}  rD   r^   capability_unsigneds                rI   _candidate_result_projectionr    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 differry  zsource local socket transcriptz!installed local socket transcriptr  r   r  z8source and installed socket transcripts were substitutedrz  source deployment wheelsr  binding_sha256r  r  z5source and installed deployment wheel receipts differc              3  (   K   | ]	  }|k7    y wrT   rL  )r   r^   r?  s     rI   r   z)build_candidate_parity.<locals>.<genexpr>=  s#      	
E 	!!
   r?  r  9source, installed, authority, and outer candidates differr   z'candidate parity bundle path is invalid%task26-cross-mode-candidate-parity-v1)r   rg   r?  product_binding_sha256)r!   r?  source_bundle_relative_pathinstalled_bundle_relative_pathr  r  outer_manifestparity_sha256)r  ru  r   r   r   r   r   r   r   rf   rg   r_   rk   )rs  r?  source_resultinstalled_resultr  r  outer_manifest_relative_pathr  r  outer_candidate	outer_sharh  product_inputssource_transcriptinstalled_transcriptr   rl   r   s    `                rI   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_OrJ   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!   r  r  r  r  r?  r  r  z"candidate parity schema is invalidr  r  outer manifest parityr   r!   r  r?  z"candidate parity digest is invalidr  zsource candidate parityr  zinstalled candidate parityr  r  rz  r  r{  r  r  r  r  rg   r  c              3  (   K   | ]	  }|k7    y wrT   rL  )r   r^   expected_candidates     rI   r   z*verify_candidate_parity.<locals>.<genexpr>  s#      	
 ''r  r  r  )r   r   r   r  r   rf   rg   r_   rk   r   rK   ru  r   r
   r   rL   )rs  parityr  r   rD   r^   rN  outerr   r  r  rh  r  r  r  source_deployments     `             rI   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_pathr  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 invalidrs   r  
rt   'task26-profile-qualification-receipt-v1  PASS)
r!   r  r  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_RESULTr\   chmodro   _PROFILE_QUALIFICATION_COMMAND_PROFILE_SOURCE_ROOT_ROLEr   rf   rg   r_   rk   )rs  r  profile_wheelqualificationlog_pathreceiptreceipt_paths          rI   #write_profile_qualification_receiptr    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NrJ   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~  rn  ro  z'persisted profile qualification receiptr!   r  r  r   r  r  r  r  r  r  r  r  r  r  r  z%profile qualification receipt differs)r   r  r  r  r   rZ   rq  rr  r   _PROFILE_QUALIFICATION_KEYSr   r-  r.  rK   r  r  r0  r1  r   r  ro   
read_bytesr\   rf   rg   r_   rk   r   )rs  r^   r  r  r  rD   r   rN  r  r  	persisteds              rI   _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.Trp   rn  ro  zsealed expected state>   r!   verifierr  r   hermes_wheelr  trust_boundarydocument_sha256expected_statusr?  candidate_parityfrozen_bootstrapr2  provenance_helperprofile_qualificationexpected_result_sha256hermetic_ty_attestationlocal_socket_transcriptsindependent_candidate_verifierrX  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 invalidr2  rX  rU  r?  r   r  rV  r  r  r  zsealed local socket transcriptsr  source candidate projectionr  installed candidate projectionr  zprofile wheelr  r{  r  r  rg   z+profile qualification wheel binding differsry  r  r  z.sealed local socket transcript bindings differr  filenamerg   sealed  digest differsr   r   >   r!   r.   r,   r-   r1   r   r.   r   r  r  r  r   rm  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?  rh  )r  r  r  r  r   z path is invalidr  zsealed authority)zregistry.jsonzqualification-ledger.jsonzcandidate-authorityr  _sha256zsealed authority digest differs)$r   r   rZ   rq  rr  r   r   r   r  rf   rg   r_   rk   r*   r;  rS  rK   r  r  ro   r   r   r   ru  r   r   r   __file__resolveparentsr   r   !_validate_hermetic_ty_attestationr   r   removesuffix)rs  expected_pathr  r  rE   claimedrD   r^   rN  verified_paritytranscript_bindingssource_projectioninstalled_projectionprofile_bindingr   rl   bindingr   r.   r  r  r   _rh  targetr  r   s                              rI   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  )r  rD   r^   s      rI   _portable_verifier_resultr    s@     !,,.(JCGG 	U
(  s   )c               h   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  soj                  d      |j                  d      k7  sKj                  d      |j                  d      k7  s'j                  d      t
        k7  sj                  d      |j                  d      k7  sj                  d      |j                  d      k7  st        j                  d      t              rt        t        t           d         d d ddgk7  sj                  d      t        k7  sjj                  d      |j                  d      k7  sFj                  d      t        |j                  d      d      j                  d      k7  s	j                  d      t        |j                  d      d      j                  d      k7  s͉j                  d      t        k7  sj                  d      t        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~  ty_command_argvr?  gate_tool_sha256attestation_sha256runner_tool_sha256binary_member_sha256raw_diagnostics_counttarget_python_versionraw_diagnostics_sha256corrected_ty_wheel_sha256execution_provenance_sha256qualification_interpreter_role!qualification_interpreter_version'diagnostics_semantic_fingerprint_sha256z0sealed hermetic Ty attestation schema is invalidr  zproduct inputshermetic_tyzhermetic Ty bindingr"  r!   z(task26-sealed-hermetic-ty-attestation-v2r?  r(  r  r&  r*  r+  r
  r  r  r   r#  qualification_tool_sha256zqualification toolsty_hermetic_runnerr!  ty_surface_gater'  r,  r$  c           	   3  ~   K   | ]4  }t         j                  t        j                  |d                   du  6 yw)r   N)r-  r.  rK   r   )r   fieldattestations     rI   r   z4_validate_hermetic_ty_attestation.<locals>.<genexpr>  s=      

 !!#kooeR&@"ABdJs   :=)r'  r,  r#  r!  r)  r~  r%  rv   z1sealed hermetic Ty attestation binding is invalid)r   r   r   r   r   _TASK26_TY_TARGET_PYTHON_VERSIONr   r   r
   rL   _TASK26_TY_COMMAND_ARGV!_TASK26_TY_RAW_DIAGNOSTICS_SHA256)_TASK26_TY_DIAGNOSTICS_FINGERPRINT_SHA256r   r0  r1  rf   rg   r_   rk   )	r^   r?  rh  inputsr-  rD   r   rN  r3  s	           @rI   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??23+,??23??234??;<??;<=+//*MNPTUVk*MNOPRQRSr7??,-1HH??,-AR1SS??/06::9:<QRVV 

 ??-.6::9:<QRVV

 ??34,-??DE45??12??123 



 

  789D./!3??/0>>)H-.88:; LMMgs   2L. L.c                (   t        d| j                  d      |j                  d      fd| j                  d      |j                  d      fd| j                  d      t        |j                  d      d      j                  d	      fd
d|j                  d      fdt        |j                  d      fdt        |j                  d      fdd|j                  d      fdt        |j                  d      fdt        |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| 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       |j                  d      }t        |t              st        d(      t        t        t           |      }|t        k7  s|j                  d)      d*k7  rt        d(      t        |j                  d      d      }|j                  d	      }|j                  d+      d,k7  st        |t              r
|d-d. d/d0gk7  rt        d1      y-)2zKCompare portable Ty behavior, never path-dependent installed RECORD hashes.z-hermetic_ty_attestation.target_python_versionr&  z'hermetic_ty_attestation.ty_command_argvr   z9hermetic_ty_attestation.qualification_interpreter_versionr+  qualification_interpreterz'rehydrated Ty qualification interpreterr   z)hermetic_ty_attestation.provenance.schemaztask26-hermetic-ty-execution-v2r!   z8hermetic_ty_attestation.provenance.target_python_versionz2hermetic_ty_attestation.provenance.ty_command_argvz&hermetic_ty_attestation.receipt.schemaz'task26-ty-capability-surface-receipt-v3z5hermetic_ty_attestation.receipt.target_python_versionz/hermetic_ty_attestation.receipt.ty_command_argvz1hermetic_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%  z?hermetic_ty_attestation.diagnostics_semantic_fingerprint_sha256r,  zGhermetic_ty_attestation.receipt.diagnostics_semantic_fingerprint_sha256z6hermetic_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  z0rehydrated Ty semantic target command is invalidr>   rv   r  r  Nr
  r  r  z2rehydrated Ty qualification interpreter is invalid)rV   r   r   r4  r5  r   r   r   r
   rL   count)	sealed_ty	ty_resultty_provenance
ty_receiptcommand_valuecommandr  r   s           rI   _verify_rehydrated_ty_resultrF    sf     @5656 :/0/0 LABMM"=>= #i. <1!!(+ K0!!"9: E'!!"34 99x( H067 B'01 D9:/0 ?45;< A6767 @56!!"9: RGHGH ZGHHI I6778 H5667 N<= 9Qx(}c	
eL "%%&78MmT*KLL4</G**==+,1KLL561K ooi(G)*.MM'4(2A;1b'!MNN "rJ   )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.rw  zcandidate authority resultr   r.   zwheelhouse entries are invalidr  r
  r  r1   r  rU  rV  r  )r?  r  r  r  r  r  r  r  r  r  rn  ro  r{  r  r!   r  r?  r2  rX  r  r  r  r  r   r  )r   rg   r  r  z-verification-tools/task26_frozen_bootstrap.pyr  z2verification-tools/independent_verify_candidate.pyr  r  r  r  z!candidate-authority/registry.jsonz-candidate-authority/qualification-ledger.jsonr  r  )registry_sha256zqualification-ledger_sha256r  r  r  r  ry  r  )r  r  r  )$r   r   r;  r   r8   r   ru  r   r   r   r   r   r   r
  r  r  r   r*   rK   r   rS  r  r  r  r   r  rZ   rq  rr  ro   r   rf   rg   r_   r  rk   )rs  r?  r2  rX  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                                rI   build_expected_staterT    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OrJ   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isolatedrv   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 filer  r  zinstalled inventory is invalidrl   zinstalled inventory rowr   rg   r   z5module origin differs from installed RECORD inventory)r  r,  r+  rg   r   r  r  r  )r!   r  r  )r   r   r   r   rK   r   r  r   r   r   OSErrorrx   
is_symlinkrz   r|   r   r   r   ro   r   r   r   )r3  site_packagesproberV  evidencer  r  
proof_rowsr,  r6  raw_pathrl   resolvedr   r   metadatar  r   r   matchesrm   s                        rI   verify_runtime_origin_proofrf    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  r  r  c              3  >   K   | ]  }t        |t                 y wrT   )r   r   )r   r   s     rI   r   z$_portable_runtime.<locals>.<genexpr>q  s      6
1>#
3%%r   z installed inventory is invalidr   rl   r   z.dist-info/RECORD)r#  distribution_versionmetadata_sha256r$  r  wheel_record_sha256portable_record_projectionz portable RECORD projectionprojection_sha256rE  installed_inventory_sha256r  zruntime interpreterr	  rg   r   )rg   r   r  )r!   r  r  r  )r   r   r   r   r   r   r   rK   r   r   rf   rg   r_   rk   )r3  r  rP  r6  r  raw_inventoryr   r   rD   portable_distributionrd  r  s               rI   _portable_runtimerq  k  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)r~   environr  r   rj   devnull)rD   r^   environments      rI   _clean_environmentr}    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 )NTrp   z"controlled Python cwd is not empty)r;   rZ  envcapture_outputtextz)Python subprocess wrote to controlled cwd)r   r   iterdirr   
subprocessrunr   r   )rE  rV  r|  	completeds       rI   _run_pythonr    sy     ^t,
>!!#$=>>WI >!!#$DEErJ   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   ro   r   )rE   r  r  r   rl   r  s         rI   _verify_bound_wheel_hashesr    se     
&	-(t (,,u-u5499D8IJJwug_=>>rJ   )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dirrs   zcontrolled-cwdr  venvr  r  rV  r|  ntzScripts/python.exez
bin/pythonpipinstallz
--no-indexz	--no-depsz--no-compilec              3  2   K   | ]  }t        |        y wrT   r   )r   rl   s     rI   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 documentr3  zrehydrated runtimeorigin_proberU  r  record_auditr2  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 rowrg   r(  rv   z(sealed corrected Ty wheel is unavailablezty-attestationz/verification-tools/task26_hermetic_ty_runner.pyz,verification-tools/task26_ty_surface_gate.pyz--sourcer<   z
--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  rt   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-runtimer  )r   rK   r   r  r   rZ   rq  stdout)relative_keyr   r   target_bundler  rs  rV  r|  r  pythonruntime_pathr  s        rI   run_verifierz*rehydrate_and_verify.<locals>.run_verifier  s    C

< <=>H"X-M#KM&*%  .'I 4::i&6&67??rJ   r  z!rehydrated source verifier resultr  z$rehydrated installed verifier resultr  r  z4rehydrated source/installed candidate parity differsr  r{  r  r  z/rehydrated profile qualification source differsrw  zrehydrated authorityr  zexpected authorityr  r  r  r  r  z=rehydrated verifier result differs from sealed expected staterX  zoriginal portable runtime)r?  r[  r\  installed_record_provenancer  *F)ignore_errors)r  rK   r   rK   rM   dict[str, Any])2r  r   r   r   tempfilemkdtempr  r  r}  r  sys
executablerK   r~   r   r  r   r  r   r   rZ   rq  rf  rq  r   r  ru  r
   r   rL   r   rF  r  r_   r  r  r  ro   rf   rg   r  rk   re  existsrglobr]  is_dirr\  shutilrmtree)-rs  r  r  r  r   r  rE   r   	temporaryr  dependency_pathsr  site_packages_textr^  helperr_  probe_outputprobe_documentruntime_documentr  rehydrated_portabler^  r@  r   ty_wheel_matches	ty_output	ty_runnerty_gatety_completedrA  rB  rC  r  r  r  r  r  expected_authorityrl   rV  r|  r  r  r  r  s-   `                                      @@@@@@rI   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)Nrs  )r0  expected_statez--hermes-wheelT)r0  requiredz--profile-wheelz--wheelhousez--temp-parent)r  r  r   r  TASK26_REHYDRATION_FAIL)r  rB  )first_invariant_keyrl   rE   rF   )r6   )filerv   r2   r5   r   )argparseArgumentParseradd_argumentr   
parse_argsr  rs  r  r  r  r   r  r\  r   r  SubprocessErrorrZ   JSONDecodeErrorrK   r   r@   rj   rD   rE   rF   printr[   r  stderr)parserargsr  r   failures        rI   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__)rU   z$Sequence[tuple[str, object, object]]rM   rN   )r^   rL   rM   bytes)rl   r   rM   rK   )rl   r   rq   boolrM   rN   )r^   rL   r   rK   rM   r  )r^   rK   rM   rK   )rl   r   rM   r)   )r   r   rM   z*tuple[list[Path], list[dict[str, object]]])r   z+Sequence[tuple[Path, Mapping[str, object]]]rM   rN   )r   Path | Noner   Sequence[Path]rM   r)   )r   r  rE   rL   r   r  rM   r)   )r   r  r   Mapping[str, object]rM   z
list[Path])r^   rL   rM   r  )r2  rL   rM   rN   )
r^   rL   r>  rK   r?  rK   r2  r  rM   r)   )rU  rL   rW  rL   r?  rK   r[  r  r\  r  rM   r)   )rh  r  rM   r)   )rs  r   r   rK   rM   z"tuple[str, str, dict[str, object]])r  r  r   rK   rM   r)   )rs  r   r?  rK   r  r  r  r  r  rK   r  rK   r  rK   rM   r)   )rs  r   r  rL   r  rK   rM   r)   )rs  r   r  rK   r  r   rM   r)   )
rs  r   r^   rL   r  rL   r  rK   rM   r)   )
rs  r   r  r   r  r   r  r   rM   r  )r  r  rM   r)   )r^   rL   r?  rK   rh  r  rM   r)   )
r@  r  rA  r  rB  r  rC  r  rM   rN   ) rs  r   r?  rK   r2  r  rX  r  r  r  r  r   r  r   rI  rK   rJ  rK   rK  r  rL  r  r  rK   r  rK   r  rK   r   r  rM   r)   )
r3  r  r^  r   r_  rL   rV  r  rM   r)   )r3  r  rM   r)   )rM   zdict[str, str])rE  zSequence[str]rV  r   r|  zMapping[str, str]rM   z subprocess.CompletedProcess[str])rE   r  r  r   r  r   rM   rN   )rs  r   r  r   r  r   r  r   r   r  r  r  rM   r)   )rM   r1  )a__doc__
__future__r   r  rf   rZ   r~   rer  rx   r  r  r  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/   rg   r[   r\   rk   r8   compiler   r4  r6  r7  r5  r-  r   r@   rV   r_   ro   r   r   r   r   r   r   r   r   r   r   r;  rS  re  rk  ru  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rF  rT  rf  rq  r}  r  r  r  r  rO   
SystemExitrL  rJ   rI   <module>r     s   N "    	 	    
   $ !  / / B 5 
 I 2#	
	+ &+A!%! !F 0 ,  5
(
( 

%!	
 &( ik
( $ 
  RZZ	* #)  $f !,n )-$  bjj*+
 ;0;	; /4 D'6$N 
 / F!U7!U	!UH## $# 	#L$$$ $	$
 $N(<0*aRHVV V 	V
 +V Vr777 	7
  47 "67 7t)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PP P *	P
 Pf@O#@O#@O (@O %	@O
 
@Of #!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
 rJ   