
    UHjF                    r   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 ddlmZmZmZ  e	e      j!                         j0                  d   Zed	z  Zd
ZdZdZdZdZdZ dZ!dZ"dZ#dZ$dg dddg ddgZ%g dZ&g dZ'd*dZ(d+dZ)d,dZ*d-dZ+	 	 	 	 	 	 	 	 	 	 	 	 d.dZ,d/d Z-	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d0d!Z.	 	 	 	 	 	 	 	 	 	 	 	 d1d"Z/d2d#Z0d3d$Z1d4d%Z2d4d&Z3d5d'Z4d6d(Z5e6d)k(  r e5        yy)7zBCompare save-selector leaf-table windows across all selector rows.    )annotationsN)Path)Any)collect_linked_cns)find_cns_stringsread_sectionsva_to_offset   outmap1_01amap2_02dz2:0iT i)T i*T i+T ?   i	   zout/save_scene_selectors.json)groupslotfirstDwords)pathfieldsHwanse2.exe)zselector leaf-table dwordsz descriptor marker/child pointerszCNS linked field maps)z"runtime-negative-wrapper-selectionz)nonnegative-route-pair-entry-reader-proofzselected-root-executionzstrict-hotspot)z;runtime selection of the current negative -12 wrapper entryz[normal-route proof that a non-negative current route-pair entry reaches the frontier readerz2selected-root/current-root runtime execution proofz,strict map1_01a source coordinate or hotspotc                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     N/home/exedev/hwanse/tools/summarize_save_selector_leaf_table_global_context.py	load_jsonr    A   s*    ;;=::dnngn677    c                2    t        | t              rd| dS d S )N0x08x)
isinstanceint)values    r   hex32r(   G   s    )%5Rc{?4?r!   c                    t        |t              sy t        ||      }||dz   t        |       kD  ry t	        j
                  d| |      d   S )N   z<Ir   )r%   r&   r	   lenstructunpack_from)exesectionsvaoffsets       r   dword_atr2   K   sL    b#(B'F~!c#h.dC033r!   c                    | j                  d      xs g }t        |      |k  ry ||   }|j                  d      r|j                  d      S d S )Nr   pointerr'   )getr+   )rowindexfirstitems       r   first_pointerr:   T   sL    GGM"(bE
5zU<D $ 3488G==r!   c                    t        |t              rt        ||      g S ||vr$t        | |||      j	                  d      xs g ||<   ||   S )N	fieldMaps)r%   r&   r	   r   r5   )r.   r/   stringscacher4   s        r   field_maps_for_pointerr?   \   sW     gs#|Hg'F'N	e+C7GLPPQ\]cacg>r!   c                &    t         | v xr t        | v S N)SOURCETARGET)mapss    r   has_route_pairrE   j   s    T>,fn,r!   c                   ||dz  z   }t        | ||      }t        | ||      }	|	t        k(  rt        | ||dz         nd }
t        | ||||      }t        | ||||
      }t        |      }t        |      }i d|d|dt	        |      d|dk\  dt	        |      dt	        |	      d	|	t        k(  d
t	        |
      d|d|d|d|dt        |xs |      d|xs |d|t        k(  d|
t        k(  d|t        k(  S )Nr*   selector
entryIndex
entryVaHexentryIsNonNegativer   descriptorHexdescriptorMarkerHexdescriptorIsMarkerShapechildPointerHexdescriptorFieldMapschildFieldMapsdescriptorHasRoutePairchildHasRoutePairhasAnyFieldMapEvidencehasRoutePairisCurrentSelectorisFrontierLeafChildisFrontierLeafDescriptor)r2   DESCRIPTOR_MARKERr?   rE   r(   boolCURRENT_SELECTORFRONTIER_LEAF)r.   r/   r=   r>   rG   table_pointerr7   entry_va
descriptormarkerchilddescriptor_maps
child_mapsroute_pair_descriptorroute_pair_childs                  r   	entry_rowre   n   s    uqy(H#x2Jc8Z0F7=AR7RHS(JN3X\E,S(GUJWO'XwuMJ*?;%j1He 	eHo 	eqj	
 	z* 	uV} 	"6->#> 	5< 	 	* 	!"7 	- 	!$'D*"E 	-A1A 	X)99  	u5!" 	#J-$?# r!   c           	          g }|D ]j  }t        |d      |j                  d       d|j                  d       |j                   fdt        t        t
        dz         D               l |S )N   r   :r   c           
   3  @   K   | ]  }t        |        y wrA   )re   ).0r7   r>   r.   r/   rG   r=   r\   s     r   	<genexpr>z'selector_window_rows.<locals>.<genexpr>   s*      
 c8WeX}eT
s   r
   )r:   r5   extendrangeINDEX_START	INDEX_END)	r.   r/   r=   r>   selector_rowsrowsselector_rowrG   r\   s	   ````   @@r   selector_window_rowsrs      s     D% 
%lA6 "&&w/0,2B2B62J1KL 
 
{IM:
 	

 Kr!   c                   t        |       }t        | |      }i }t        | ||||      }t        |D ch c]  }|d   	 c}      }|D cg c]  }|j	                  d      s| }}|D cg c]  }|j	                  dd      dk  s| }	}|D cg c]  }|j	                  dd      dk\  s| }
}t        |	D ch c]  }|d   	 c}      }t        |
D ch c]  }|d   	 c}      }|D cg c]  }|d   t
        k(  s| }}|D cg c]  }|j	                  d      s| }}|D cg c]  }|j	                  dd      dk  s| }}|D cg c]  }|j	                  dd      dk\  s| }}|D cg c]&  }|j	                  d      s|j	                  d      r|( }}|D cg c]  }|j	                  d      s| }}|D cg c]  }|j	                  dd      dk  s| }}|D cg c]  }|j	                  dd      dk\  s| }}d}i d	t        d
t        dt
        dt        t              dt        t              dt        t              dt        t              dt        dt        t              dt        t         gdt        |      dt        |      dt        |	      dt        |
      dt        |      dt        |      dt        |      i dt        |      dt        |      dt        |      dt        |      dt        |      d|D cg c]  }|d   	 c}d |d!t#        |      xr t%        d" |D              d#|d$|D cg c]$  }|d   d%d&t
        d'hv r|j	                  d      r|& c}d d( d)d*d+d*d,t&        d-t(        d.d*d/d*d0d1t(        |d2S c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )3NrG   rS   rH   r   rT   rV   rW   a  Across selector rows, field-map-bearing entries commonly appear on both sides of each selector's root table pointer. The current reader-bearing frontier leaf 0x00542ae8 appears only as the child of the current selector's negative root-relative entry -12, while the non-negative current entries that contain the map1_01a/map2_02d route pair do not by themselves prove the reader path. This keeps the wrapper as sliding-window context evidence rather than normal-transition proof.sourcetargetcurrentSelectorcurrentRootHexcurrentTablePointerHexfrontierLeafHexfrontierReaderHexevidenceRefsevidenceRefCountwindowIndexRangeselectorTableCountfieldEntryRowCountnegativeFieldEntryRowCountnonNegativeFieldEntryRowCount!selectorsWithNegativeFieldEntries$selectorsWithNonNegativeFieldEntriesroutePairRowCountroutePairNegativeRowCountroutePairNonNegativeRowCount currentSelectorRoutePairRowCount(currentSelectorNegativeRoutePairRowCount+currentSelectorNonNegativeRoutePairRowCountcurrentSelectorRoutePairIndicescurrentSelectorFrontierLeafRowscurrentFrontierLeafOnlyNegativec              3  F   K   | ]  }|j                  d d      dk    yw)rH   r   N)r5   )rj   r6   s     r   rk   z build_summary.<locals>.<genexpr>   s      Na(1,Ns   !currentSelectorRouteRowssampleSelectorRowsz0:0z1:0z10:0   
proofFoundFleafTableGlobalProofFoundfailedLeafTableGlobalGateIdsmissingEvidenceruntimeSelectionProvenstrictHotspotFoundpromotionStatusblocked)remainingProofs
conclusion)r   r   rs   sortedr5   rZ   rB   rC   r(   CURRENT_ROOTCURRENT_TABLE_POINTERr[   FRONTIER_READEREVIDENCE_REFSr+   rn   ro   rY   all!FAILED_LEAF_TABLE_GLOBAL_GATE_IDSMISSING_EVIDENCE)r.   rp   r/   r=   r>   rq   r6   selectors_with_table
field_rowsnegative_field_rowscurrent_field_rowsselectors_with_negativeselectors_with_currentcurrent_selector_rowscurrent_route_rowscurrent_negative_route_rowscurrent_nonnegative_route_rowscurrent_frontier_rowsroute_pair_rowsroute_pair_negative_rowsroute_pair_nonnegative_rowsr   s                         r   build_summaryr      s   S!HsH-G"$EXw}MD!d"Cs3z?"CD!%K#1I)J#KJK*4U3a8PST8T3UU)3U#sww|Q7OST7T#UU$AT%U#c*o%UV#@R$SS_$ST,0XSC
OGW4WSXX)>Z##''.BY#ZZ2D"e3P\^_H`cdHd3"e"e5G%ic377S_abKcghKhc%i"%i,77()SWW5O-P 	  '+Fscggn.EsFOF/>_#'',XYBZ]^B^__2A"c3SWW\[\E]abEb3"c"c	d )&)&) 	+) 	%-	)
 	!%(=">) 	5/) 	U?3) 	) 	C.) 	[)4) 	c"67) 	c*o) 	%c*=&>) 	(-?)@) 	,S1H-I)  	/4J0K!)" 	S1#)$ 	$S)A%B%)& 	',G(H')( 	+C0B,C))* 	3C8S4T+), 	6s;Y7Z-). 	*I[+\#C,=+\/)0 	*+@1)2 	*40E+F ,ON8MNN5)6 	#$67)8 	%
:5%1A6"JJswwWeOf 
 29)@ 	eA)B 	$UC)D 	'(IE)F 	+G)H 	!%I)J 	eK)L 	9M)N , Q) )1 #DKUU%U$SXZ"e%i G_"c> ,]

s   N.	N3 N3*N8N8N=*N=9OO,O>OOO)OOO)O3+O $O%;O%O* O**O/O/O4
)O9
c                R    dj                  | j                  |      xs g       xs dS )N, -)joinr5   )r6   keys     r   	maps_textr      s#    99SWWS\'R(/C/r!   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&    dd'| d(    dd)| d*    dd+| d,    dd| d-   dd.dg}| j                  d/      xs g D ]J  }|j                  d0|j                  d1       d2d3j                  |j                  d4      xs g               L |j                  g d5       | d6   D ]  }|j                  d7|d8    d9|d:    d;|j                  d<       d;|j                  d=      xs d> d?|d@    dAt	        |dB       dAt	        |dC       dA|j                  dD       dA|j                  dE      xs |j                  dF       dG        |j                  g dH       | dI   D ]n  }|j                  dJ|dK    d?|d8    d9|d:    d;|j                  d<       d;|j                  d=      xs d> d?|d@    dAt	        |dB       dAt	        |dC       dG       p |j                  g dL       |j                  dM | dN   D               |j                  g dO       |j                  dP | dQ   D               |j                  g dR       |j                  dS | dT   D               |j                  d       dUj                  |      S )VNz)# Save Selector Leaf Table Global Context z
- route: `ru   z -> rv   `z- current selector: `rw   z` root `rx   z- current table pointer: `ry   z- frontier leaf: `rz   z- frontier reader: `r{   z- selector tables scanned: r   z- field-map entry rows: r   z (r   z negative, r   z non-negative)z)- selectors with negative field entries: r   z-- selectors with non-negative field entries: r   z- current route-pair rows: r    at r   z'- current frontier leaf only negative: r   z- proof found: `r   z"- leaf table global proof found: `r   z- promotion status: `r   z- evidence refs: `r}   r   z## Evidence Refsr|   z- `r   z`: r   r   )r   z## Current Selector Route Rowsr   zk| index | entry | descriptor | child | non-negative | desc maps | child maps | route pair | frontier leaf |z8| ---: | --- | --- | --- | --- | --- | --- | --- | --- |r   z| rH   z | `rI   z` | `rK   rN   r   z` | rJ   z | rO   rP   rT   rV   rW   z |)r   z### Sample Cross-Selector Route Rowsr   zY| selector | index | entry | descriptor | child | non-negative | desc maps | child maps |z2| --- | ---: | --- | --- | --- | --- | --- | --- |r   z| `rG   )r   z## Remaining Proofsr   c              3  &   K   | ]	  }d |   ywz- N rj   r9   s     r   rk   zmarkdown.<locals>.<genexpr>/       D2dVD   r   )r   z!## Failed Leaf-Table Global Gatesr   c              3  &   K   | ]	  }d |   ywr   r   r   s     r   rk   zmarkdown.<locals>.<genexpr>1  s     Q2dVQr   r   )r   z## Missing Evidencer   c              3  &   K   | ]	  }d |   ywr   r   r   s     r   rk   zmarkdown.<locals>.<genexpr>3  r   r   r   
)r5   appendr   rl   r   )summarylinesrefr6   s       r   markdownr      s"   3

WX&'tGH,=+>a@
(9 :;8GL\D]C^^_`
$W-E%F$GqI
W%678:
w':;<A>
%g.B&C%DE
"7+?#@"AGLhDiCjjuv}  ^  w_  v`  `n  	o
3G<_4`3ab
7@f8g7hi
%g.P&Q%RRVW^  `A  XB  WC  	D
1':[2\1]^
7<013
,W5P-Q,RRST
(9 :;1=
W%789;


-E0 {{>*0b 
#''&/"#dii0A0GR&H%IJ	

 
LL   12 
\"#4L(9':%@X?Y Z)*1c2$s;O7P6QQT345S3HX9Y8ZZ]ww~&'s3773H+I+pSWWUoMp*qqsu	

 
LL   +, 
#j/"$s<'8&9c,>O=P Q()sww7H/I/PS.QQU'()Ys<Q-R,SSV./04	

 
LL01	LLD1B)CDD	LL>?	LLQ1O)PQQ	LL01	LLD1B)CDD	LL99Ur!   c           #     ~   dj                  d | j                  d      xs g D              }dEd}dj                  d | d   D              }dj                  d | d   D              }dj                  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+    d,d-t        j                  | d.          d/d0| d1    d2d3| d4d5d6d7 || d8   d9       d:d;d<d7 || d=   d>       d:d?d@| d4dAd@| d4dBd@| d4dCdDdg      S )FNr   c              3     K   | ]m  }d t        j                  t        |j                  d                   dt        j                  dj	                  |j                  d      xs g              d o yw)z
<li><code>r   z	</code>: r   r   </li>N)htmlescapestrr5   r   )rj   r   s     r   rk   zhtml_page.<locals>.<genexpr>9  sf      
 		S1239;;tyy!2!8b9:
;	s   A3A5r|   c                   g }| D ]=  }|rdt        j                  |d          dnd}|j                  d| d|d    dt        j                  t        |d	                d
t        j                  t        |j	                  d                   d
t        j                  t        |j	                  d      xs d             d|d    dt        j                  t        |d             dt        j                  t        |d             d|j	                  d       d|j	                  d      xs |j	                  d       d       @ dj                  |      S )Nz
<td><code>rG   z</code></td>r   z<tr>z<td>rH   z</td><td><code>rI   z</code></td><td><code>rK   rN   r   z</code></td><td>rJ   z	</td><td>rO   rP   rT   rV   rW   z
</td></tr>)r   r   r   r   r5   r   r   )rq   include_selectorcellsr6   selector_cells        r   row_htmlzhtml_page.<locals>.row_htmlA  sa    	CWgjS_)E(FlSmoMLL /s<() *![[S->)?@A B![[SWW_-E)FGH I![[SWW5F-G-N3)OPQ R/01 2{{9S2G#HIJ K{{9S2B#CDE Fww~./ 0ww45\A[9\] ^	  wwu~r!   c              3  N   K   | ]  }d t        j                  |       d  ywz<li>r   Nr   r   r   s     r   rk   zhtml_page.<locals>.<genexpr>U  s#     \tDKK-.e4\   #%r   c              3  N   K   | ]  }d t        j                  |       d  ywr   r   r   s     r   rk   zhtml_page.<locals>.<genexpr>V  s+       t{{4 !'r   r   c              3  N   K   | ]  }d t        j                  |       d  ywr   r   r   s     r   rk   zhtml_page.<locals>.<genexpr>Z  s#     ]$T[[./u5]r   r   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">z8  <title>Save Selector Leaf Table Global Context</title>a    <style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;margin:24px}table{border-collapse:collapse;width:100%;max-width:1280px}td,th{border:1px solid #333;padding:6px 8px;text-align:left;vertical-align:top}th{background:#1f1f1f}code{color:#9bd4ff}</style>z</head>z<body>z2  <h1>Save Selector Leaf Table Global Context</h1>z  <p>route <code>ru   z -&gt; rv   z </code>; current selector <code>rw   z!</code>; selector tables scanned r   z; field rows r   z; current route rows r   r   r   z; frontier only negative r   z; proof found <code>r   z-</code>; leaf table global proof found <code>r   z</code>; promotion <code>r   z</code>.</p>z  <p>r   z</p>z  <p><b>Evidence refs:</b> r}   z.</p>z  <h2>Evidence Refs</h2><ul>z</ul>z&  <h2>Current Selector Route Rows</h2>z  <table><thead><tr><th>index</th><th>entry</th><th>descriptor</th><th>child</th><th>non-negative</th><th>desc maps</th><th>child maps</th><th>route pair</th><th>frontier leaf</th></tr></thead>z	  <tbody>r   Fz</tbody></table>z+  <h2>Sample Cross-Selector Route Rows</h2>z  <table><thead><tr><th>selector</th><th>index</th><th>entry</th><th>descriptor</th><th>child</th><th>non-negative</th><th>desc maps</th><th>child maps</th><th>route pair</th><th>frontier leaf</th></tr></thead>r   Tz  <h2>Remaining Proofs</h2>z  <ul>z)  <h2>Failed Leaf-Table Global Gates</h2>z  <h2>Missing Evidence</h2>z</body>z</html>)rq   
list[dict]r   rY   returnr   )r   r5   r   r   )r   evidence_refsr   proofsfailed_gatesmissings         r   	html_pager   8  s   GG 
 ;;~.4" M( WW\IZA[\\F77 :; L gg]'J[B\]]G99"PB 	\<
GH-.ggh6G5HHhip  rC  jD  iE  Ef  gn  oC  gD  fE  ER  SZ  [o  Sp  Rq  qF  GN  Oq  Gr  Fs  sw  x  @a  xb  wc  c|  }D  Ef  }g  |h  h|  }D  EQ  }R  |S  S@  AH  Id  Ae  @f  f  @	G	  H	Y	  @	Z	  [	  [	g	  	h	
GL12348
%g.@&A%B%H
&}oU;0 	L
HW%?@%HIIYZ5 	]
HW%9:DABBRS%
3
e$%
	
;  r!   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       y )
NT)parentsexist_ok,save_selector_leaf_table_global_context.jsonFrg   )ensure_asciiindentr   r   r   )mkdir
write_textr   dumps)r   out_dirs     r   write_outputsr   |  sD    MM$M.==II

7q9D@ J r!   c                    t        j                  t              } | j                  dt        t
        dz         | j                  dt        t        dz         | j                  dt        t               | j                         }t        |j                  j                         t        |j                  g             }t        ||j                         t        d|j                  d	z          y )
N)descriptionz--exer   )typedefaultz--selectorszsave_scene_selectors.jsonz	--out-dirz#wrote leaf table global context -> r   )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsr   r.   
read_bytesr    	selectorsr   r   print)parserargsr   s      r   mainr     s    $$9F
dD=4HI
D#@[:[\
$<D$.."%G '4<<(	/?m0m/n
opr!   __main__)r   r   r   r   r   r   )r'   
int | Noner   z
str | None)r.   bytesr/   r   r0   r   r   r   )r6   dictr7   r&   r   r   )r.   r  r/   r   r=   dict[int, str]r>   dict[int, list[str]]r4   r   r   	list[str])rD   r  r   rY   )r.   r  r/   r   r=   r  r>   r  rG   r   r\   r&   r7   r&   r   r  )r.   r  r/   r   r=   r  r>   r  rp   r   r   r   )r.   r  rp   r   r   r  )r6   r  r   r   r   r   )r   r  r   r   )r   r  r   r   r   None)r   r  )7r   
__future__r   r   r   r   r,   syspathlibr   typingr   r   insertr   __file__resolveparentparse_savedatar   probe_exe_scene_tablesr   r   r	   r   r   r   rB   rC   rZ   r   r   r[   r   rX   rn   ro   r   r   r   r    r(   r2   r:   r?   rE   re   rs   r   r   r   r   r   r   __name__r   r!   r   <module>r     s   H "     
   3tH~--/667 8 - P P H~''*
Ul		 "  	 0
 
$% ! 8@4>	   	
  -#	## #  	#
 # # # 
#L	   	
  (FR0@FAHq zF r!   