
    UHjF                    H   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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efd)dZ d*dZ!e"dk(  r e!        yy)+zLApply public savedat active-order samples to opcode 0x20 descriptor effects.    )annotationsN)Path)Any   outmap1_01amap2_02d   )zcurrent-selector-2-0-samplezruntime-active-orderznon-pointer-context-a8-effectzroute-promotion-proof)zDpublic or captured sample covering current selector 2:0 active orderz4runtime active order/count on the current route pathz5non-pointer context+0xa8 effect in active descriptorszBroute-promotion proof linking sample order to map1_01a -> map2_02dz%data/savedata_sample_party_order.jsonz.public savedat active count/order byte samples)pathdescriptionz2out/save_selector_opcode20_descriptor_scripts.jsonz;descriptor+4 static effects applied to public sample ordersc                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     R/home/exedev/hwanse/tools/summarize_save_selector_opcode20_sample_order_effects.py	load_jsonr   *   s*    ;;=::dnngn677    c                    d| dS )N0x04x )values    r   hex16r   0   s    c{r   c                    | j                  d      xs g D ci c]'  }t        |j                  d      t              r|d   |) c}S c c}w )NdescriptorRowsindexget
isinstanceint)descriptor_scriptsrows     r   descriptor_rows_by_indexr(   4   sQ     &))*:;Arcggg&, 	Gc  s   ,Ac                    | j                  d      }| j                  d      xs g }t        |t              sg S |d | D cg c]  }t        |t              s| c}S c c}w )NactiveSlotCountactiveOrderBytesr"   )samplecountorderr   s       r   active_indicesr/   <   sW    JJ()EJJ)*0bEeS!	$Ve}Ge
5#0FEGGGs    AAc                    dj                  d t        |       D              }| j                  d       d| j                  d       d| j                  d       d| d	S )
N,c              3  2   K   | ]  }t        |        y wNstr.0r   s     r   	<genexpr>z#sample_signature.<locals>.<genexpr>E   s     EUc%jE   selectorGroup:selectorSlotz count=r*   z order=[])joinr/   r#   )r,   actives     r   sample_signaturer@   D   s]    XXEnV.DEEFjj)*!FJJ~,F+GwvzzZkOlNmmuv|u}}~r   c                \   g }d }d }g }t        t        |             D ]7  \  }}|j                  |      }|'|j                  |       |j                  ||dd       A|j                  d      }	|j                  d      }
|	r|	}|
r|
}|j                  |||j                  d      |j                  d      xs g |j                  d      |j                  d      |j                  d	      |	xs i j                  d
      |	xs i j                  d      |	xs i j                  d      |
xs i j                  d
      |
xs i j                  d      dd       : i d| j                  d      d| j                  d      d| j                  d      d| j                  d      d| j                  d       d| j                  d       d| j                  d      d| j                  d      d| j                  d       d| j                  d       d| j                  d      d| j                  d      xs g dt        |       d| j                  d      dt	        |       d |d!|d"|xs i j                  d
      d#|xs i j                  d      |xs i j                  d$      |xs i j                  d      |xs i j                  d
      |xs i j                  d      |xs i j                  d$      | j                  d      t
        k(  xr | j                  d      t        k(  d%S )&NT)
activeSlotdescriptorIndexmissingDescriptorscript4LastContextA8SetterRow'script4LastNonPointerContextA8SetterRowdescriptorVaHexscript0LinkedCnsscript4VaHexscript4ContextA8SetterRowCount(script4ContextA8NonPointerSetterRowCountbaseExpression	opcodeHexisPointerDwordF)rB   rC   rG   rH   rI   rJ   rK   "script4LastContextA8BaseExpressionscript4LastContextA8OpcodeHex"script4LastContextA8IsPointerDword,script4LastNonPointerContextA8BaseExpression'script4LastNonPointerContextA8OpcodeHexrD   idr   source	sourceUrlselectorr:   r;   r<   tiletileXr1   tileYr*   r+   activeDescriptorIndicesactiveFlagByte	signaturedescriptorEffectsmissingDescriptorIndices"finalShapedContextA8BaseExpressionfinalShapedContextA8OpcodeHexvaHex)finalShapedContextA8SetterVaHex"finalShapedContextA8IsPointerDword&finalNonPointerContextA8BaseExpression!finalNonPointerContextA8OpcodeHex#finalNonPointerContextA8SetterVaHexcoversCurrentFrontierSelector)	enumerater/   r#   appendr@   CURRENT_FRONTIER_GROUPCURRENT_FRONTIER_SLOT)r,   descriptorseffectsfinal_shaped_contextfinal_non_pointer_contextmissing_descriptor_indicesslotdescriptor_index
descriptorlast_shaped_contextlast_non_pointer_contexts              r   sample_effectrw   I   s   G $!#"+N6,B"C  __%56
&--.>?NN"#3%) 
 (nn-LM#->>2[#\ #6 #(@%/)~~.?@ */A B Hb&NN>:.8nn=].^8BGq8r3F3L"2Q2QRb2c.A.GR-L-L[-Y3F3L"2Q2QRb2c=U=[Y[<`<`aq<r8P8VTV7[7[\g7h!&
 	!>fjj

6" 	&**X& 	VZZ,	
 	vzz/231VZZ5O4PQ 	O4 	

>2 	6::g&'qG)<(=> 	6::&78 	FJJ'9:@b 	">&#9 	&**%56 	%f- 	W 	#$>  	-/C/Ir.N.NO_.`!" 	(*>*D")I)I+)V#$ -A,FB+K+KG+T/C/Ir.N.NO_.`3L3RPR2W2WXh2i.G.M2-R-RS^-_0I0OR/T/TU\/]JJ'+AA D

>*.CC3 r   c                    i }| D ]   }|xs d}|j                  |d      dz   ||<   " t        |j                               D cg c]
  \  }}||d c}}S c c}}w )Nnoner   r   )r   r-   )r#   sorteditems)valuescountsr   keyr-   s        r   	histogramr      sn    F -ovjja(1,s-
 #6<<>2E5 %(  s   Ac           
        | | nt        t        dz  dz  i       } ||nt        t        dz  i       }t        |      }| j	                  d      xs g D cg c]  }t        ||       }}g }| j	                  d      xs 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]  }	|	d   	 c}	      }
t        d	 |D              }d
}i dt        dt        ddj                  d |D              ddj                  d |D              d|d| j	                  d      xs i j	                  d      xs t        d      d| j	                  d      xs i j	                  d      xs t        d      dt        |      dt        |
      d|
dt         dt         d|d|dt!        |D 	cg c]  }	|	d   	 c}	      d t!        |D 	cg c]  }	|	d!   	 c}	      d"t#        d# |D              d$d%d&d&t$        t&        t(        t        t(              d%|d'S c c}w c c}	w c c}	w c c}	w )(Ndata savedata_sample_party_order.json.save_selector_opcode20_descriptor_scripts.jsonsamplesrU   rV   )rU   rV   r]   c              3  &   K   | ]	  }|d      yw)rh   Nr   r7   r'   s     r   r8   z build_summary.<locals>.<genexpr>   s     "_C3'F#G"_   a`  The public sample saves make the opcode 0x20 count/order bytes concrete: selector 0:0 and 1:0 samples use count 1 with descriptor order [0], and progressed selector 22:0 samples use count 3 with [0,1,2]. Applying those orders to descriptor+4 static effects leaves only pointer-dword low-byte collisions in the last shaped context+0xa8 setter rows; there is no non-pointer context+0xa8 setter in the active descriptors covered by the public samples. None of the public samples covers the current frontier selector 2:0, so this remains calibration evidence rather than promotion proof for map1_01a -> map2_02d.targetsampleSourcez, c              3  &   K   | ]	  }|d      yw)rU   Nr   r   s     r   r8   z build_summary.<locals>.<genexpr>   s     !JC#h-!Jr   sampleSourceUrlc              3  J   K   | ]  }|j                  d       s|d      yw)rV   N)r#   r   s     r   r8   z build_summary.<locals>.<genexpr>   s$     $h#SVSZSZ[fSgS%5$hs   #
#sampleSourcesactiveSlotCountOffsetHexfields   activeOrderOffsetHex   sampleCountuniqueSampleStateCountuniqueSampleSignaturescurrentFrontierSelectorr;   currentFrontierSampleCovered
sampleRows'sampleFinalShapedContextA8BaseHistogramr`   +sampleFinalNonPointerContextA8BaseHistogramre   $allSamplesHaveKnownDescriptorIndicesc              3  (   K   | ]
  }|d       yw)r_   Nr   r   s     r   r8   z build_summary.<locals>.<genexpr>   s     3o\_<V8W4W3o   controlPathProofStatusblockedF)
proofFoundopcode20SampleOrderProofFound failedOpcode20SampleOrderGateIdsmissingEvidenceevidenceRefsevidenceRefCountpromotionStatus
conclusion)r   ROOTOUTr(   r#   rw   rj   rz   anySOURCETARGETr>   r   lenrk   rl   r   all%FAILED_OPCODE20_SAMPLE_ORDER_GATE_IDS&OPCODE20_SAMPLE_ORDER_MISSING_EVIDENCE#OPCODE20_SAMPLE_ORDER_EVIDENCE_REFS)r   r&   rm   r,   sample_rowssample_sourcesrU   
source_urlitemr'   unique_signaturescurrent_frontier_coveredr   s                r   build_summaryr      s    ,g)D6MLn<npr2sG/A/M+S\>>
T ++=>K kk),2 	fk*K  N++i(.B (H%>X)>ZZ,HK0H
 z:d.0!!$'( KHSK 0HI""_S^"__	] && 			!J>!JJ 	499$h$hh	
 	 	#gkk(&;&Ar%F%FGa%b%sfklrfs 	'++h"7"=2!B!BCY!Z!k^cdj^k 	s;' 	!#&7"8 	!"3 	"&<%=Q?T>U#V 	'(@ 	k 	29AL>
:=C45>
 4" 	6yEPB
>AC89B
 8#( 	/3ocn3o0o)* 	!)+, ).,QA; CD$ ; -  I2>
B
s   I)4I.>I3I8c                   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| d    dd| d     dd| d!   dd"dg}|j                  d# | d$   D               |j                  g d%       |j                  d& | d'   D               |j                  g d(       |j                  d) | d*   D               |j                  g d+       | d,   D ]   }|j                  d-|d.    d/|d0    d1       " | d2   D ]   }|j                  d3|d.    d/|d0    d1       " |j                  g d4       | d5   D ]d  }d6j                  d7 |d8   D              }|j                  d9|d:    d;|d<    d=|d>    d/|d?    d@| dA|dB   xs dC d=|dD   xs dC d/|dE    d1       f |j                  d       dFj                  |      S )GNz0# Save Selector Opcode 0x20 Sample Order Effects z	- route: rU   z -> r   z- sample source: r   z (r   )z- active slot count offset: `r   `z- active order offset: `r   z- sample count: r   z- unique sample states: r   z- current frontier selector: `r   z#- current frontier sample covered: r   z-- all samples have known descriptor indices: r   z- proofFound: `r   z"- opcode20SampleOrderProofFound: `r   z- promotion status: `r   r   z## Failed Gatesc              3  (   K   | ]
  }d | d  yw)- `r   Nr   r7   r   s     r   r8   zmarkdown.<locals>.<genexpr>   s     W43tfAWr   r   )r   z## Missing Evidencer   c              3  &   K   | ]	  }d |   yw)z- Nr   r   s     r   r8   zmarkdown.<locals>.<genexpr>   s     D2dVDr   r   )r   z## Evidence Refsr   c              3  8   K   | ]  }d |d    d|d      yw)r   r   z`: r   Nr   r   s     r   r8   zmarkdown.<locals>.<genexpr>   s/       c&k]#c-012s   r   )r   z$## Final Context+0xa8 Base Histogramr   z)| kind | base expression | sample count |z| --- | --- | ---: |r   z| shaped | `r   z` | r-   z |r   z| non-pointer | `)r   z
## Samplesr   z| sample | selector | tile | count | active descriptors | final shaped context+0xa8 base | final non-pointer context+0xa8 base | covers current selector |z,| --- | --- | --- | ---: | --- | --- | --- |r   r1   c              3  2   K   | ]  }t        |        y wr3   r4   r6   s     r   r8   zmarkdown.<locals>.<genexpr>       Q#e*Qr9   r[   z| rT   z | `rW   z` | `rX   r*   z | `[z]` | `r`   -re   rh   
)extendrj   r>   )summarylinesr'   r?   s       r   markdownr      s   :

GH%&d78+<*=>
GN34Bw?P7Q6RRST
'0J(K'LAN
"7+A#B"C1E
7=123
"7+C#D"EF
(1J)K(LAN
-g6T.U-VW
7@f8g7hi
',/02
,W5T-U,VVWX
(9 :;1=


'E* 
LLW73U+VWW	LL  
 
LLD1B)CDD	LL  
 
LL >*  
LL   @A H|CL>c'l^2FGHDE M(Wd3w<.KLM	LL   |$ 
Q#6O2PQQT4J0c&k]$sK\G]F^ _s#GHOCP Q<=DET#NmJnIooqs	

 
LL99Ur   c                   dj                  d | d   D              }dj                  d | d   D              }dj                  d | d   D              }| d   D cg c]$  }d	t        j                  |d
          d|d    d& c}| d   D cg c]$  }dt        j                  |d
          d|d    d& c}z   }g }| d   D ]  }dj                  d |d   D              }|j                  dt        j                  |d   xs d       dt        j                  |d          dt        j                  |d          d|d    dt        j                  |       dt        j                  |d   xs d       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/d0d1d2d3d4d5d6t        j                  | d7          d8t        j                  | d9          d:t        j                  | d;   xs d       d<t        j                  | d=          d>t        j                  | d?          d@t        j                  | dA          dB| dC    dD| dE    dFt        j                  | dG          dHdIt        j                  | dJ          dKdLdM| dNdOdM| dNdPdM| dNdQdR|dSdTdU|dSdVdWd      S c c}w c c}w )XNr   c              3  N   K   | ]  }d t        j                  |       d  yw)
<li><code>z</code></li>Nhtmlescaper   s     r   r8   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   r8   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   r   s     r   r8   zhtml_page.<locals>.<genexpr>   sE       T[[V-.iCDV8W7XX]^s   ?Ar   r   z<tr><td>shaped</td><td><code>r   z</code></td><td>r-   z
</td></tr>r   z"<tr><td>non-pointer</td><td><code>r   r1   c              3  2   K   | ]  }t        |        y wr3   r4   r6   s     r   r8   zhtml_page.<locals>.<genexpr>-  r   r9   r[   z<tr><td>rT   r   z</td><td><code>rW   z</code></td><td><code>rX   r*   z</td><td><code>[z]</code></td><td><code>r`   re   rh   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">z?  <title>Save Selector Opcode 0x20 Sample Order Effects</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>z9  <h1>Save Selector Opcode 0x20 Sample Order Effects</h1>z  <p>route rU   z -&gt; r   z; sample source r   z!; active slot count offset <code>r   z#</code>; active order offset <code>r   z)</code>; current frontier selector <code>r   z)</code>; current frontier sample covered 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>Final Context+0xa8 Base Histogram</h2>zb  <table><thead><tr><th>kind</th><th>base expression</th><th>sample count</th></tr></thead><tbody>z  </tbody></table>z  <h2>Samples</h2>z  <table><thead><tr><th>sample</th><th>selector</th><th>tile</th><th>count</th><th>active descriptors</th><th>final shaped context+0xa8 base</th><th>final non-pointer context+0xa8 base</th><th>covers current selector</th></tr></thead><tbody>z</body>z</html>)r>   r   r   rj   )r   failed_gatesmissing_evidenceevidence_refsr'   histogram_rowsr   r?   s           r   	html_pager     s   77 >? L ww -.  GG >* M DE (CL(A'BBRSVW^S_R``jk
 HI	 -T[[W-F,GGWX[\cXdWeeop	N K|$ 
Q#6O2PQQ;;s4y/C01 2S_56 7S[12 3()* +++f-. /S)M%N%URUVW XS)Q%R%YVYZ[ \678 9		

 99 ((( 	( 	#	(
 	Q( 	J( 	( 	r( 	1( 	7( 	:( 	G( 	f( 	t( 	Q(  	'!(" 	#($ 	%(& 	'(( 	D)(* dkk'("345WT[[QYIZ=[<\\lmqmxmx  zA  BP  zQ  zX  UX  nY  mZ  Z{  |@  |G  |G  HO  Pj  Hk  |l  {m  mP  QU  Q\  Q\  ]d  e{  ]|  Q}  P~  ~g  hl  hs  hs  t{  |U  tV  hW  gX  XA  BI  Jh  Bi  Aj  j}  ~E  FR  ~S  }T  Tt  uy  u@	  u@	  A	H	  I	Z	  A	[	  u\	  t]	  ]	i	  	j	+(, GL12348-(. 	"/(0 e$1(2 	&3(4 !"%(5(6 	#7(8 u%9(: 	7;(< 	m=(> 
?(@ 	A(B 	C(D 	|E(F 
G(H 	I(J 	K(L 	M(N 	O( ( (-	s   )K")K'c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       y )NT)parentsexist_okz0save_selector_opcode20_sample_order_effects.jsonFr
   )ensure_asciiindentr   r   r   0save_selector_opcode20_sample_order_effects.html)mkdir
write_textr   dumpsr   )r   out_dirs     r   write_outputsr   e  sf    MM$M.AAMM

7q9D@ N  AAMMiX_N`krMsr   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	--samplesr   r   )typedefaultz--descriptor-scriptsr   z	--out-dirz8wrote save selector opcode 0x20 sample order effects -> r   )argparseArgumentParser__doc__add_argumentr   r   r   
parse_argsr   r   r   r&   r   r   print)parserargsr   s      r   mainr   n  s    $$9F
$vHj8jk
.T3IyCyz
$<D$,,#$))2.G '4<<(	DT\\  UG  FG  EH  I  Jr   __main__)r   r   r   r   returnr   )r   r%   r   r5   )r&   dictr   dict[int, dict])r,   r   r   z	list[int])r,   r   r   r5   )r,   r   rm   r   r   r   )r|   zlist[str | None]r   z
list[dict])NN)r   dict | Noner&   r   r   r   )r   r   r   r5   )r   r   r   r   r   None)r   r   )#r   
__future__r   r   r   r   pathlibr   typingr   __file__resolver   r   r   r   r   rk   rl   r   r   r   r   r   r(   r/   r@   rw   r   r   r   r   r   r   __name__r   r   r   <module>r      s    R "      H~''*
Ul		  ) %* & 8G
 ET	' #8H@
?D;|@FK\ 25 tJ zF r   