
    UHjD                    j   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  ee
      j                         j                  d   Zedz  ZdZd	Zd
ZdZdZdZg dZg dZddddddgZd#dZd$dZd%dZd%dZd&dZd'dZd(dZd)d*dZd+dZ d,dZ!d-d.dZ"d/dZ#d/dZ$efd0d Z%d1d!Z&e'd"k(  r e&        yy)2zFApply public savedat byte samples to the current gate-base candidates.    )annotationsN)Path)Any   outmap1_01amap2_02d      )zcurrent-selector-2-0-samplezruntime-pointer-mode-proofzparty-slot-index-evidencezcontrol-path-gate-byte-proof)zBpublic or captured sample covering current selector 2:0 gate bytesz:runtime pointer-mode proof selecting the correct gate basezVparty-slot byte evidence that yields a valid branch-state index rather than stat byteszNcontrol-path proof that sampled gate bytes apply to the current frontier gatesz%data/savedata_sample_gate_values.jsonz:public savedat byte samples used to calibrate gate offsets)pathdescriptionz+out/save_selector_gate_base_candidates.jsonz;candidate gate-base mappings and save offsets being sampledc                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     G/home/exedev/hwanse/tools/summarize_save_selector_gate_sample_values.py	load_jsonr   ,   s*    ;;=::dnngn677    c                    d| dS )N0x02x values    r   hex8r    2   s    c{r   c                L    | j                  d       d| j                  d       S )NselectorGroup:selectorSlotgetsamples    r   sample_selectorr)   6   s'    jj)*!FJJ~,F+GHHr   c                L    | j                  d       d| j                  d       S )NtileX,tileYr%   r'   s    r   sample_tiler.   :   s'    jj!"!FJJw$7#899r   c                F    | j                  d      }|sy|d    d|d    dS )N
knownField-field (byteRole)r%   )rowr2   s     r   
field_textr7   >   s4    GGL!EGnRj 12!44r   c                    | j                  d      xs i }|j                  |      }||j                  |j                               }t        |t              r|S d S )Nvalues)r&   lower
isinstanceint)r(   
offset_hexr9   r   s       r   value_atr>   E   sS    ZZ!'RFJJz"E}

:++-.uc*544r   c                V    t        | t              xr t        | cxk  xr	 t        k  S c S N)r;   r<   EXPECTED_INDEX_MINEXPECTED_INDEX_MAXr   s    r   in_expected_rangerC   M   s&    eS!W&8E&WEW&WW&WWr   c                    | j                  d      xs g D cg c])  }|j                  d      |k(  r|j                  d      r|+ }}|$|D cg c]  }|j                  d      |k(  s| }}|S c c}w c c}w )NbaseCandidatesbaseKindsaveOffsetHexclassificationr%   )gate_base_candidates	base_kindrH   r6   rowss        r   candidate_rowsrL   Q   s    +//0@AGR77:)+0H 	D  !#Ssww/?'@N'RSSK
 Ts   .A0A5(A5c           	        g }| D ]M  }t        |      }|j                  d      t        k(  xr |j                  d      t        k(  }|D ]  }|j                  d      }t	        ||      }|j                  i d|j                  d      d|j                  d      d|d|j                  d      d|j                  d      d	t        |      d
|d|j                  d      d|j                  d      d|j                  d      d|d|j                  d      d|d|t        |      nd dt        |      d|j                  d             
 P |S )Nr"   r$   rG   sampleIdid
samplePathr   selectortilecoversCurrentFrontierSelectorrF   slotgateOffsetHexr0   r   valueHexinExpectedIndexRangecandidateClassificationrH   )	r)   r&   CURRENT_FRONTIER_GROUPCURRENT_FRONTIER_SLOTr>   appendr.   r    rC   )	samples
candidatesrK   r(   rQ   covers_current	candidater=   r   s	            r   sample_value_rowsr`   [   s   D "6*JJ'+AA D

>*.CC 	 $ 	I"7JVZ0EKK FJJt,fjj0 H  O!<	
 

> : F+ 0 IMM*5 	f-  !?   immL9  5+<DK$ '(9%(@  *9==9I+J! 	6 Kr   c                h    t        | D ch c]  }|j                  d      s|d    c}      S c c}w )NrV   )sortedr&   )rK   r6   s     r   distinct_valuesrc   {   s*    dJscggj6I3z?JKKJs   //c                   | | nt        t        dz  dz  i       } ||nt        t        dz  i       }| j                  d      xs g }t	        |d      }t	        |dd      }t        ||      }t        ||      }t        d	 |D              }g }|D ]h  }	|	j                  d
      xs | j                  d
      }
|	j                  d      xs | j                  d      }|
|d}|
sS||vsX|j                  |       j t        |D 	ch c]  }	t        |	       c}	      }d}i d
t        dt        ddj                  d |D              ddj                  d |D              d|d|j                  d      xs g dt        t        gdt         dt          dt#        |      dt#        |      d|d|dt#        |      dt#        |      d|d |d!t%        d" |D              t%        d# |D              t'        |      t%        d$ |D              t%        d% |D              t'        |      d&d'd(d(t(        t*        t,        t#        t,              d'|d)S c c}	w )*Ndata savedata_sample_gate_values.json'save_selector_gate_base_candidates.jsonr\   zsave/runtime block basezparty slot basezimplausible-stat-byte-index)rH   c              3     K   | ]6  }|j                  d       t        k(  xr |j                  d      t        k(   8 yw)r"   r$   N)r&   rY   rZ   ).0r(   s     r   	<genexpr>z build_summary.<locals>.<genexpr>   sG      #  	

?#'== 	@JJ~&*??	@#s   <>source	sourceUrl)rk   rl   a  The public savedat bytes keep the save/runtime block base plausible only as calibration: loader-aware mapping puts 0x004576d8+0xe8/0xea at save offsets 0x00e2/0x00e4 after the six-byte runtime gap, and all public samples read 0x00 there. Those values are in the expected 0..11 branch-index range, but the samples cover selectors 0:0, 1:0, and 22:0, not the current frontier selector 2:0. The same samples make the party-slot-base mapping weaker: the corresponding stat-byte candidates are 0xe7, 0xa6, or 0x7a, outside the expected selection-index range. This rejects the stat-byte interpretation as promotion evidence while leaving global/save-runtime buffer or object pointer table runtime proof still required.targetsampleSource, c              3  &   K   | ]	  }|d      yw)rk   Nr   ri   r6   s     r   rj   z build_summary.<locals>.<genexpr>   s     !JC#h-!J   sampleSourceUrlc              3  J   K   | ]  }|j                  d       s|d      yw)rl   Nr%   rq   s     r   rj   z build_summary.<locals>.<genexpr>   s$     $h#SVSZSZ[fSgS%5$hs   #
#sampleSourcesgateOffsetsHexexpectedIndexRangecurrentFrontierSelectorr#   sampleCountuniqueSelectorCountsampleSelectorscurrentFrontierSampleCoveredsaveRuntimeGateCandidateCountpartySlotStatCandidateCountsaveRuntimeGateSampleRowspartySlotStatSampleRowssaveRuntimeGateSampleValueCountc              3  D   K   | ]  }|j                  d       d  ywr   Nr   r%   rq   s     r   rj   z build_summary.<locals>.<genexpr>   s"     .nSQTQXQXY`QaQmq.n     c              3  ,   K   | ]  }|d    s	d  ywrW   r   Nr   rq   s     r   rj   z build_summary.<locals>.<genexpr>   s     5tCX[\rXsa5t   
c              3  D   K   | ]  }|j                  d       d  ywr   r%   rq   s     r   rj   z build_summary.<locals>.<genexpr>   s"     ,o3RURYRYZaRbRnQ,or   c              3  ,   K   | ]  }|d    s	d  ywr   r   rq   s     r   rj   z build_summary.<locals>.<genexpr>   s     3u#Y\]sYtA3ur   TblockedF)&saveRuntimeGateInRangeSampleValueCount saveRuntimeGateDistinctValuesHexpartySlotStatSampleValueCount$partySlotStatInRangeSampleValueCountpartySlotStatDistinctValuesHexruntimePointerModeStillRequiredcontrolPathProofStatus
proofFoundgateSampleValueProofFoundfailedGateSampleValueGateIdsmissingEvidenceevidenceRefsevidenceRefCountpromotionStatus
conclusion)r   ROOTOUTr&   rL   r`   anyr[   rb   r)   SOURCETARGETjoinrA   rB   rY   rZ   lensumrc   !FAILED_GATE_SAMPLE_VALUE_GATE_IDS"GATE_SAMPLE_VALUE_MISSING_EVIDENCEGATE_SAMPLE_VALUE_EVIDENCE_REFS)r\   rI   sample_itemssave_runtime_candidatesparty_slot_stat_candidatessave_runtime_rowsparty_slot_stat_rowscurrent_frontier_coveredsample_sourcesr(   rk   
source_urlitem	selectorsr   s                  r   build_summaryr      s    ,g)D6MLn<npr2sG3G3S/Yb77
Z ;;y)/RL,-AC\]!/4"
 *,8OP,\;UV" # ##  
 N (H%>X)>ZZ,HK0H
 z:d.0!!$'( lKF/KLI	c !&!&! 			!J>!JJ! 	499$h$hh	!
 	! 	.223CDJ! 	13EF! 	"&<%=Q?T>U#V! 	s<(! 	s9~! 	9! 	'(@! 	(-D)E! 	&s+E'F! 	$%6!  	"#7!!" 	*3.n<M.n+n#!$ 365tCT5t2t,;<M,N),,o:N,o)o033uAU3u0u*9:N*O+/"+%*(I=7 ?@$ A! ! Ls   Ic                R   ddd| d    d| d    d| d    d	| d
    dddj                  d | d   D               d| d   d    d| d   d    d| d    ddj                  d | d   D               d| d    dd| d    d| d     d!| d"    d#| d$    d!| d%    d&| d'    d(| d)    dd*| d+    dd,| d-    dd| d.   dd/dg}|j                  d0 | d1   D               |j                  g d2       |j                  d3 | d4   D               |j                  g d5       |j                  d6 | d7   D               |j                  g d8       | d9   D ]H  }|j                  d:|d;    d<|d=    d>|d?    d>|d@    d>|dA    d>|dB   xs dC dD|dE    dF|dG    dH       J |j                  g dI       | dJ   D ]N  }|j                  d:|d;    d<|d=    dD|dK    d<|d@    d>|dA    dDt        |       d<|dB   xs dC dD|dE    dH       P |j                  d       dLj                  |      S )MNz"# Save Selector Gate Sample Values z	- route: rk   z -> rm   z- sample source: rn   r3   rs   r5   z- gate offsets: ro   c              3  (   K   | ]
  }d | d   yw`Nr   ri   r   s     r   rj   zmarkdown.<locals>.<genexpr>   s     $WTqa[$W   rv   z"- expected selection index range: rw   r   z..r   z- sample count: ry   z- sample selectors: c              3  (   K   | ]
  }d | d   ywr   r   r   s     r   rj   zmarkdown.<locals>.<genexpr>   s     (\1TF!(\r   r{   z- current frontier selector: `rx   r   z#- current frontier sample covered: r|   z - save/runtime in-range values: r    / r   z#- party-slot stat in-range values: r   r   z'- runtime pointer mode still required: r   z- proofFound: `r   z- gateSampleValueProofFound: `r   z- promotion status: `r   r   z## Failed Gatesc              3  (   K   | ]
  }d | d  yw)- `r   Nr   r   s     r   rj   zmarkdown.<locals>.<genexpr>   s     S43tfASr   r   )r   z## Missing Evidencer   c              3  &   K   | ]	  }d |   yw)z- Nr   r   s     r   rj   zmarkdown.<locals>.<genexpr>   s     D2dVDrr   r   )r   z## Evidence Refsr   c              3  8   K   | ]  }d |d    d|d      yw)r   r   z`: r   Nr   rq   s     r   rj   zmarkdown.<locals>.<genexpr>   s/       c&k]#c-012s   r   )r   z## Save/Runtime Gate Bytesr   z^| sample | selector | tile | gate | save offset | value | in range | covers current selector |z2| --- | --- | --- | --- | --- | ---: | --- | --- |r   z| rN   z | `rQ   z` | `rR   rU   rG   rV   r1   z` | rW   z | rS   z |)r   z## Party Slot Stat Bytesr   zW| sample | selector | slot | gate | save offset | known save field | value | in range |z3| --- | --- | ---: | --- | --- | --- | ---: | --- |r   rT   
)r   extendr[   r7   )summarylinesr6   s      r   markdownr      s   ,

GH%&d78+<*=>
GN34Bw?P7Q6RRST
499$WWEU=V$WWXY
,W5I-J1-M,NbQXYmQnopQqPrs
7=123
tyy(\IZA[(\\]^
(1J)K(LAN
-g6T.U-VW
*73[+\*]]`ah  jK  bL  aM  	N
-g6\.]-^^abi  kJ  cK  bL  	M
1':[2\1]^
',/02
(1L)M(NaP
(9 :;1=


+E. 
LLS73Q+RSS	LL  
 
LLD1B)CDD	LL  
 
LL >*  
LL   23 
Z!c*o%6eCK=cRaNbMc dO$%U3z?+Ac*B$sKaGbFccf234B8	

 
LL   01 
Z!c*o%6d3v;-tCP_L`Ka bO$%T*S/):$s:?URU>VVZ)*+2/	

 
LL99Ur   c                   dj                  d | d   D              }dj                  d | d   D              }dj                  d | d   D              }g }| d   D ]  }|j                  d	t        j                  |d
   xs d       dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d   xs d       d|d    d|d    d        g }| d   D ]  }|j                  d	t        j                  |d
   xs d       dt        j                  |d          d|d    dt        j                  |d          dt        j                  |d          dt        j                  t	        |             dt        j                  |d   xs d       d|d    d        dj                  g dddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/t        j                  | d0          d1t        j                  | d2          d3t        j                  | j                  d4      xs d       d5t        j                  d6j                  | d7                d8t        j                  | d9          d:| d;    d<| d=    d>| d?    d<| d@    dA| dB    dCt        j                  | dD          dEdFt        j                  | dG          dHdIdJ| dKdLdJ| dKdMdJ| dKdNdO|dPdQdR|dPdSdTd      S )UNr   c              3  N   K   | ]  }d t        j                  |       d  yw)
<li><code>z</code></li>Nhtmlescaper   s     r   rj   zhtml_page.<locals>.<genexpr>  s+       T[[&'|4   #%r   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li></li>Nr   r   s     r   rj   zhtml_page.<locals>.<genexpr>  s+       t{{4 !'r   r   c              3     K   | ]9  }d t        j                  |d          dt        j                  |d          d ; yw)r   r   z	</code>: r   r   Nr   rq   s     r   rj   zhtml_page.<locals>.<genexpr>  sE       T[[V-.iCDV8W7XX]^s   ?Ar   r   z<tr><td>rN   r1   z</td><td><code>rQ   z</code></td><td><code>rR   rU   rG   rV   z</code></td><td>rW   z	</td><td>rS   z
</td></tr>r   rT   r   z<!doctype html>z<html lang="en">z<head>z  <meta charset="utf-8">zF  <meta name="viewport" content="width=device-width, initial-scale=1">z1  <title>Save Selector Gate Sample Values</title>z	  <style>zg    :root { color-scheme: dark; font-family: system-ui, sans-serif; background: #101010; color: #eee; }z&    body { margin: 0; padding: 24px; }z,    h1 { margin: 0 0 8px; font-size: 24px; }z/    h2 { margin: 24px 0 8px; font-size: 18px; }z<    p { max-width: 1120px; color: #bbb; line-height: 1.45; }z[    table { width: 100%; border-collapse: collapse; margin: 12px 0 20px; font-size: 13px; }zi    th, td { border-bottom: 1px solid #303030; padding: 7px 8px; text-align: left; vertical-align: top; }zF    th { position: sticky; top: 0; background: #181818; color: #ddd; }z    code { color: #d7f0ff; }z
  </style>z</head>z<body>z+  <h1>Save Selector Gate Sample Values</h1>z  <p>route rk   z -&gt; rm   z; sample source rn   z; sample selectors ro   r{   z"; current frontier selector <code>rx   z&</code>; save/runtime in-range values r   r   r   z"; party-slot stat in-range values r   r   z; proofFound <code>r   z </code>; promotion status <code>r   z</code>.</p>z  <p>r   z</p>z  <h2>Failed Gates</h2>z  <ul>z</ul>z  <h2>Missing Evidence</h2>z  <h2>Evidence Refs</h2>z"  <h2>Save/Runtime Gate Bytes</h2>z  <table><thead><tr><th>sample</th><th>selector</th><th>tile</th><th>gate</th><th>save offset</th><th>value</th><th>in range</th><th>covers current selector</th></tr></thead><tbody>z  </tbody></table>z   <h2>Party Slot Stat Bytes</h2>z  <table><thead><tr><th>sample</th><th>selector</th><th>slot</th><th>gate</th><th>save offset</th><th>known save field</th><th>value</th><th>in range</th></tr></thead><tbody>z</body>z</html>)r   r[   r   r   r7   r&   )r   failed_gatesmissing_evidenceevidence_refsr   r6   party_slot_rowss          r   	html_pager     sX   77 :; L ww -.  GG >* M 23 
  ;;s:5#67 8S_56 7S[12 3S%9:; <S%9:; <S_%;<= >-./ 0678 9		

 O01 
;;s:5#67 8S_56 7v;-  S%9:; <S%9:; <;;z#/0 1S_%;<= >-./ 0		

 99 /// 	/ 	#	/
 	Q/ 	</ 	/ 	r/ 	1/ 	7/ 	:/ 	G/ 	f/ 	t/ 	Q/  	'!/" 	#/$ 	%/& 	'/( 	6)/* dkk'("345WT[[QYIZ=[<\ ]W[[%@%GCHI J KK		':K2L(MNO P++/;;w?X7Y+Z*[ \((/0X(Y'ZZ]^e  gH  _I  ^J J++23Y+Z*[[^_f  hG  `H  _I I#L12 3""&++g6G.H"I!J,	X+/: GL12348;/< 	"=/> e$?/@ 	&A/B !"%(C/D 	#E/F u%G/H 	-I/J 	@K/L 
M/N 	O/P 	+Q/R 	yS/T 
U/V 	W/X 	Y/Z 	[/\ 	]/ / /r   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       y )NT)parentsexist_okz%save_selector_gate_sample_values.jsonFr
   )ensure_asciiindentr   r   r   %save_selector_gate_sample_values.html)mkdir
write_textr   dumpsr   )r   out_dirs     r   write_outputsr   i  se    MM$M.66BB

7q9D@ C  66BB9WCU`gBhr   c                    t        j                  t              } | j                  dt        t
        dz  dz         | j                  dt        t        dz         | j                  dt        t               | j                         }t        t        |j                  i       t        |j                  i             }t        ||j                         t        d	|j                  d
z          y )N)r   z	--samplesre   rf   )typedefaultz--gate-base-candidatesrg   z	--out-dirz*wrote save selector gate sample values -> r   )argparseArgumentParser__doc__add_argumentr   r   r   
parse_argsr   r   r\   rI   r   r   print)parserargsr   s      r   mainr   r  s    $$9F
$vHj8jk
0tSKtEtu
$<D$,,#$++R0G '4<<(	6t||Fm7m6n
opr   __main__)r   r   r   r   returnr   )r   r<   r   str)r(   dictr   r   )r6   r   r   r   )r(   r   r=   r   r   
int | None)r   r   r   boolr@   )rI   r   rJ   r   rH   z
str | Noner   
list[dict])r\   r   r]   r   r   r   )rK   r   r   z	list[str])NN)r\   dict | NonerI   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )(r   
__future__r   r   r   r   pathlibr   typingr   __file__resolver   r   r   r   r   rY   rZ   rA   rB   r   r   r   r   r    r)   r.   r7   r>   rC   rL   r`   rc   r   r   r   r   r   __name__r   r   r   <module>r      s   L "      H~''*
Ul		    % !& " 8S
 >T	# 8I:55X@LFRCLXv 25 iq zF r   