
    UHjAD                    *   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m	Z	 ddl
mZ ej                  j                  d e e	e      j!                         j"                               ddlmZmZmZ  e	e      j!                         j,                  d   Zedz  Zd	Zd
Zg dZg dZdddddddddgZdZdZdZ dZ!dZ"dZ#dZ$d*dZ%d*dZ&d+dZ'd,dZ(d,dZ)d-d Z*d.d!Z+d/d"Z,d0d#Z-	 	 d1	 	 	 	 	 	 	 d2d$Z.d3d%Z/d3d&Z0efd4d'Z1d5d(Z2e3d)k(  r e2        yy)6zFSummarize runtime sources for opcode 0x20 slot count and slot objects.    )annotationsN)Path)Any)offset_to_varead_sectionsva_to_offset   outmap1_01amap2_02d)zruntime-slot-countz runtime-slot-descriptor-pointerszactive-order-runtime-statez selected-root-execution-or-trace)z?runtime slot count byte at 0x004576e8 on the current route pathzBruntime descriptor pointers materialized at 0x00457750 + slot*0xd8z2active order/count values for current selector 2:0zHselected-root execution or equivalent runtime trace reaching opcode 0x20Hwanse2.exez;static slot/count byte locations and direct-reference scans)pathdescriptionzout/save_loader_trace.jsonzAsave block mapping for count byte and slot base runtime addressesz1out/save_selector_opcode20_nested_base_modes.jsonz>current opcode 0x20 mode and nested script path classificationi(T ivE ivE iPwE i        c                    d| dS )N0x08x values    J/home/exedev/hwanse/tools/summarize_save_selector_opcode20_slot_sources.pyhex32r   :       c{    c                    d| dS )Nr   04xr   r   s    r   hex16r   >   r   r   c                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     r   	load_jsonr(   B   s*    ;;=::dnngn677r   c                F    t        ||      }||t        |       k\  ry | |   S )N)r   lenexesectionsvaoffsets       r   byte_atr0   H   s+    (B'F~3s8+v;r   c                v    t        ||      }||dz   t        |       kD  ry t        j                  d| |      d   S )N   <Ir   )r   r*   structunpack_fromr+   s       r   dword_atr6   O   s?    (B'F~!c#h.dC033r   c                    g }d}	 | j                  ||      }|dk  r|S t        ||      }||j                  |       |dz   }>)Nr   r	   )findr   append)r,   r-   patternrowsr/   hitr.   s          r   pattern_hitsr=   V   sU    DF
hhw'7K(C(>KKOq r   c                    t        | |t        j                  d|            }t        |      t	        |      |d d D cg c]  }t        |       c}dS c c}w )Nr3      )targetVaHexcount
sampleRefs)r=   r4   packr   r*   )r,   r-   r   refsrefs        r   
dword_refsrF   c   sP    Xv{{4'?@DU|T-1#2Y7cuSz7  8s   Ac           
        | j                  d      xs g D ]r  }t        |d   d      }t        |d   d      }||cxk  r	||z   k  s0n 3t        |d   d      |z   |z
  }|d   |d   |d   |d   t        |      t        ||z
        dc S  y )	NsaveReadBlocksr@      sizeHexsaveOffsetHex	callVaHexr   )rL   r@   rJ   r   rK   offsetWithinBlockHex)getintr   )save_loader_trace
runtime_varowtargetsizesave_offsets         r   save_block_for_runtime_varV   l   s     $$%56<" S',3y>2&Z/&4-/c/2B7*DvMK -"=1y>"=1!&{!3(-j6.A(B  r   c                f   g }t        t              D ]  }t        |t        z  z   }t	        | ||      }t	        | ||dz         }t        ||      }|j                  |t        |      |r|d   nd |d u|t        |      nd |t        |      nd t        |xr d|cxk  xr dk  nc       dd        |S )Nr2   rK   i  @ i  ` T)slotslotBaseHexrK   insideLoadedSecondBlockstaticFirstDwordHexstaticPlus4DwordHexstaticFirstDwordLooksPointer$runtimeSlotDescriptorPointerRequired)	rangeSLOT_SCAN_COUNTSECOND_SAVE_BLOCK_BASESLOT_STRIDEr6   rV   r9   r   bool)	r,   r-   rP   r;   rX   basefirst_dwordplus4_dword
save_blocks	            r   	slot_rowsrh   }   s    Do& %{(::sHd3sHdQh7./@$G
 ;<FZ8D'1'=9D9P5#5VZ9D9P5#5VZ,01h{Ah^hAh,i48	
 		 Kr   c                z   t        |       }||nt        t        dz  i       }||nt        t        dz  i       }t        |t              }t        |t
              }t        | ||      }t        t        z  }t        t        z  }t        d |D              }	d}
i dt        dt        dt        t              d|j                  d      d	|j                  d	      d
|j                  d
      dt        t              d|dt        | |t              ddt        | |t              ddt!        | |t              dt        t
              dt#        t              d|dt#        t              d|dt#        |      t!        | |t
              |t%        |      |	dddddt&        t(        t*        t%        t*              d|
dS )Nzsave_loader_trace.jsonz-save_selector_opcode20_nested_base_modes.jsonc              3  2   K   | ]  }|d    dk7  sd  yw)r[   
0x00000000r	   Nr   .0rR   s     r   	<genexpr>z build_summary.<locals>.<genexpr>   s     $f33?T;UYe;eQ$fs   a:  Opcode 0x20 mode 0 is controlled by runtime state, not by a static leaf table. The loop count byte 0x004576e8 sits inside the first save-read block at save offset 0x0010, while the 0x00457750 slot area is the second save-read block and contains only three complete 0xd8-byte slots. The static first dwords of the scanned slots are zero, so slot descriptor pointers must be materialized at runtime before descriptor+4 nested scripts can run. This keeps the current map1_01a -> map2_02d gate proof blocked until runtime slot count and descriptor pointer values are proven.sourcerS   currentOpcode20VaHexcurrentOpcode20ValueHexcurrentModecurrentModeHexcountRuntimeVaHexcountSaveSourcecountStaticInitialBytecountStaticInitialByteHexr   02xcountDirectRefSummaryrY   slotStrideHexslotSaveSourceloadedSecondBlockSizeHexloadedCompleteSlotCapacityloadedSlotRemainderHexTblockedF)slotBaseDirectRefSummaryslotRowsscannedSlotCountstaticFirstDwordNonzeroCountruntimeSlotCountRequired%runtimeSlotDescriptorPointersRequiredcontrolPathProofStatus
proofFoundopcode20SlotSourceProofFoundfailedOpcode20SlotSourceGateIdsmissingEvidenceevidenceRefsevidenceRefCountpromotionStatus
conclusion)r   r(   OUTrV   COUNT_RUNTIME_VAra   rh   SECOND_SAVE_BLOCK_SIZErb   sumSOURCETARGETr   CURRENT_OPCODE20_VArN   r0   rF   r   r*   $FAILED_OPCODE20_SLOT_SOURCE_GATE_IDS%OPCODE20_SLOT_SOURCE_MISSING_EVIDENCE"OPCODE20_SLOT_SOURCE_EVIDENCE_REFS)r,   rP   opcode20_nested_base_modesr-   count_blocksecond_blockslotsloaded_slot_capacityloaded_slot_remainderstatic_first_dword_nonzeror   s              r   build_summaryr      s>   
 S!H->-J)PY&&
Q &1 	#sLLbQ  ,,=?OPK,->@VWLc8%67E1[@2[@!$$f%$f!f	u !&!&! 	&9 :! 	"#=#A#AB[#\	!
 	155mD! 	4889IJ! 	U#34! 	;! 	!'#x9I"J! 	$r'#xAQ*RSV)W%X! 	 C;K!L! 	u34! 	{+! 	,! 	#E*@$A!  	%&:!!" 	!%(=">#!$ %/sH>T$UJ(B$(15"+(-+O@: BC$ A! !r   c                   | d   xs i }| d   xs i }ddd| d    d| d    d	| d
    d| d    d| d    dd| d    d|j                  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g}|j                  d. | d/   D               |j                  g d0       |j                  d1 | d2   D               |j                  g d3       |j                  d4 | d5   D               |j                  dd6dd7d8d9| d    d:|j                  dd       d:|j                  d;d       d:|j                  d<d       d=|j                  d>d       d?| d@   dA    dBdC| d    d:|j                  dd       d:|j                  d;d       d:|j                  d<d       d=|j                  d>d       d?| dD   dA    dBddEddFdGg       | dH   D ]J  }|j                  dI|dJ    dK|d    d:|d   xs d d=|dL    dK|dM   xs d d:|dN   xs d d=|dO    dB       L |j                  d       dPj                  |      S )QNru   r{   z(# Save Selector Opcode 0x20 Slot Sources z	- route: ro   z -> rS   z- opcode 0x20 row: `rp   z	` value `rq   z` (rr   )z- count byte: `rt   z` save offset `rK   -`z- count static initial byte: `rw   z- slot base: `rY   z- slot stride: `rz   z!- loaded complete slot capacity: r}   z1- scanned slots with nonzero static first dword: r   z- runtime slot count required: r   z-- runtime slot descriptor pointers required: r   z- control path proof status: `r   z- proofFound: `r   z!- opcode20SlotSourceProofFound: `r   z- promotion status: `r   r   z## Failed Gatesc              3  (   K   | ]
  }d | d  yw)- `r   Nr   rm   items     r   rn   zmarkdown.<locals>.<genexpr>   s     V43tfAVs   r   )r   z## Missing Evidencer   c              3  &   K   | ]	  }d |   yw)z- Nr   r   s     r   rn   zmarkdown.<locals>.<genexpr>   s     D2dVDs   r   )r   z## Evidence Refsr   c              3  8   K   | ]  }d |d    d|d      yw)r   r   z`: r   Nr   rl   s     r   rn   zmarkdown.<locals>.<genexpr>   s/       c&k]#c-012s   r   z## Save SourceszY| field | runtime VA | save offset | read call | block size | description | direct refs |z,| --- | --- | --- | --- | --- | --- | ---: |z| count byte | `z` | `rL   rJ   z` | r   z | ry   rA   z |z| slot base | `r   z## Scanned Slotszi| slot | base | save offset | inside loaded block | static first dword | static +4 dword | pointer-like |z,| ---: | --- | --- | --- | --- | --- | --- |r   z| rX   z | `rZ   r[   r\   r]   
)rN   extendr9   join)summarycount_sourceslot_sourcelinesrR   s        r   markdownr      s   ,-3L*+1rK2

GH%&d78+<*=>
w'=>?yQjIkHllopw  yF  qG  pH  HI  	J
'"567|GWGWXgilGmFnnop
(1L)M(NaP
/0P_ad@e?ffgh
7?34A6
+G4P,Q+RS
;GDb<c;de
)'2L*M)NO
7@g8h7ij
(1I)J(K1M
',/02
+G4R,S+TTUV
(9 :;1=


+E. 
LLV73T+UVV	LL  
 
LLD1B)CDD	LL  
 
LL >*  
LL

c6w':;<E,BRBRSbdgBhAi j  c2359I9I)UX9Y8ZZ^s34C@W8XY`8a7bbdf
 gm45U;???\_;`:a bS12%	SV8W7XX\}c233w?Y7Z[b7c6ddfh 	
s6) , z" 
VT#m"4!5U3;O;VSV:WW[,-.d37L3M3TQT2U V)*1c2$s;Y7Z6[[]_	

 
LL99Ur   c                   dj                  d | d   D              }dj                  d | d   D              }dj                  d | d   D              }| d   xs i }| d	   xs i }d
| d   |j                  dd      |j                  dd      |j                  dd      |j                  dd      t        | d   d         gd| d   |j                  dd      |j                  dd      |j                  dd      |j                  dd      t        | d   d         gg}|D cg c]   }ddj                  d |D              z   dz   " }}g }	| d   D ]  }|	j                  d|d    dt	        j
                  |d          dt	        j
                  |d   xs d       d|d    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/d0d1d2d3d4d5d6d7d8d9t	        j
                  | d:          d;t	        j
                  | d<          d=t	        j
                  | d>          d?t	        j
                  | d          d@t	        j
                  |j                  dd             dAt	        j
                  | d          dBt	        j
                  | dC          dD| dE    dF| dG    dH| dI    dJ| dK    dLt	        j
                  | dM          dNdOt	        j
                  | dP          dQdRdS| dTdUdS| dTdVdS| dTdWdX|dYdZd[|	dYd\d]d      S c c}w )^Nr   c              3  N   K   | ]  }d t        j                  |       d  yw)
<li><code>z</code></li>Nhtmlescaper   s     r   rn   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   rn   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   rl   s     r   rn   zhtml_page.<locals>.<genexpr>&  sE       T[[V-.iCDV8W7XX]^s   ?Ar   ru   r{   z
count bytert   rK   r   rL   rJ   r   ry   rA   z	slot baserY   r   z<tr>c              3  N   K   | ]  }d t        j                  |       d  yw)z<td>z</td>Nr   )rm   r   s     r   rn   zhtml_page.<locals>.<genexpr>A  s#     Ke4E 2359Kr   z</tr>r   z<tr><td>rX   z</td><td><code>z</code></td><td><code>z</code></td><td>rZ   r[   r\   r]   z
</td></tr>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">z7  <title>Save Selector Opcode 0x20 Slot Sources</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>z1  <h1>Save Selector Opcode 0x20 Slot Sources</h1>z  <p>route ro   z -&gt; rS   z; opcode row <code>rp   z</code>; count byte <code>z</code> save offset <code>z</code>; slot base <code>z</code>; slot stride <code>rz   z(</code>; loaded complete slot capacity: r}   z; runtime slot count required: r   z-; runtime slot descriptor pointers required: 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 Sources</h2>z  <table><thead><tr><th>field</th><th>runtime VA</th><th>save offset</th><th>read call</th><th>block size</th><th>description</th><th>direct refs</th></tr></thead><tbody>z  </tbody></table>z  <h2>Scanned Slots</h2>z  <table><thead><tr><th>slot</th><th>base</th><th>save offset</th><th>inside loaded block</th><th>static first dword</th><th>static +4 dword</th><th>pointer-like</th></tr></thead><tbody>z</body>z</html>)r   rN   strr9   r   r   )
r   failed_gatesmissing_evidenceevidence_refsr   r   	save_rowsrR   save_rows_htmlslot_rows_htmls
             r   	html_pager     s   77 => L ww -.  GG >* M ,-3L*+1rK '(_c2[#.Y,]C0/09:	
 M"OOOS1OOK-OOIs+OOM3/23G<=	
I,  	KsKKKgUN  Nz" 
v;-  S%789 :S%9%@SAB C012 3S)>%?%F3GH IS)>%?%F3GH I567 8
	

 99 ((( 	( 	#	(
 	Q( 	B( 	( 	r( 	1( 	7( 	:( 	G( 	f( 	t( 	Q(  	'!(" 	#($ 	%(& 	'(( 	<)(* dkk'("345WT[[QYIZ=[<\\optp{p{  }D  E[  }\  q]  p^  ^x  y}  yD  yD  EL  M`  Ea  yb  xc  c}  ~B  ~I  ~I  JV  JZ  JZ  [j  lo  Jp  ~q  }r  rK  LP  LW  LW  X_  `m  Xn  Lo  Kp  pK  LP  LW  LW  X_  `o  Xp  Lq  Kr  rZ  [b  c  [@  ZA  A`  ah  iC	  aD	  `E	  E	r	  s	z	  {	b
  s	c
  r	d
  d
w
  x

  @L  x
M  w
N  Nn  os  oz  oz  {B  CT  {U  oV  nW  Wc  	d+(, GL12348-(. 	"/(0 e$1(2 	&3(4 !"%(5(6 	#7(8 u%9(: 	";(< 	u=(> 
?(@ 	A(B 	#C(D 	EE(F 
G(H 	I(J 	K(L 	M(N 	O( ( (#s   %M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_opcode20_slot_sources.jsonF   )ensure_asciiindentr   r    r!   (save_selector_opcode20_slot_sources.html)mkdir
write_textr$   dumpsr   )r   out_dirs     r   write_outputsr   |  sf    MM$M.99EE

7q9D@ F  99EEiPWFXcjEkr   c                 v   t        j                  t              } | j                  dt        t
        dz         | j                  dt        t               | j                         }t        |j                  j                               }t        ||j                         t        d|j                  dz          y )N)r   z--exer   )typedefaultz	--out-dirz0wrote save selector opcode 0x20 slot sources -> r   )argparseArgumentParser__doc__add_argumentr   ROOTr   
parse_argsr   r,   
read_bytesr   r   print)parserargsr   s      r   mainr     s    $$9F
dD=4HI
$<DDHH//12G'4<<(	<T\\Lv=v<w
xyr   __main__)r   rO   returnr   )r   r   r'   r   r   r   )r,   bytesr-   
list[dict]r.   rO   r   z
int | None)r,   r   r-   r   r:   r   r   z	list[int])r,   r   r-   r   r   rO   r   dict)rP   r   rQ   rO   r   dict | None)r,   r   r-   r   rP   r   r   r   )NN)r,   r   rP   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )4r   
__future__r   r   r   r$   r4   syspathlibr   typingr   r   insertr   __file__resolveparentprobe_exe_scene_tablesr   r   r   r   r   r   r   r   r   r   r   r   r   FIRST_SAVE_BLOCK_BASEra   r   rb   r`   r   r   r(   r0   r6   r=   rF   rV   rh   r   r   r   r   r   __name__r   r   r   <module>r      s~   L "     
   3tH~--/667 8 L L H~''*
Ul		( $) % T
 -Z
 DW& " !  " #  84
", &*.2?	?"? !,? 
	?DGT\~ 25 lz zF r   