
    JjN<                       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  e	e      j!                         j*                  d   Zedz  Zd	Zd
ZdZg dZg dZdg dddg 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Z%d*dZ&d+d,dZ'd-dZ(d-dZ)edfd.dZ*d/d Z+e,d!k(  r e+        yy)0zISummarize route selector root references for the current merge/order gap.    )annotationsN)Path)Any)offset_to_varead_sections   outmap1_01amap2_02d)z0:0z1:0z2:0)zselector-root-code-refzpredecessor-current-root-linkzroute-order-execution-proofz%strict-hotspot-or-real-selector-proof)z;code or runtime reference executing route selector root 2:0zCpredecessor 1:0 root/control-flow link to current selector root 2:0zIroute-order proof from source/predecessor selectors into current selectorzDstrict source hotspot or captured selector 2:0 save/runtime evidenceHwanse2.exe)zselector row-pointer dword refszselected-root dword refsz.text reference classification)pathfieldszout/save_scene_selectors.json)groupslotrowPointerHexselectedPointerHex	fieldMapsc                    d| dS )N0x08x values    K/home/exedev/hwanse/tools/summarize_save_selector_route_root_ref_context.pyhex32r   3   s    c{    c                "    | rt        | d      S d S )N   )intr   s    r   	parse_hexr    7   s    "3ub>,,r   c                L    | j                  d       d| j                  d       S )Nr   :r   get)rows    r   selector_keyr&   ;   s&    gggq 122r   c                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     r   	load_jsonr0   ?   s*    ;;=::dnngn677r   c                    | D ]O  }t        |d         }|t        |d         z   }||cxk  r|k  s.n 1t        |j                  d      xs d      c S  y )Nvaraw_sizename )r   strr$   )sectionsr2   sectionstartends        r   section_for_var;   E   s^     2GDM"c'*-..Bw{{6*0b11	2
 r   c           	         t        j                  d|      }g }d}	 | j                  ||      }|dk  r	 |S t        ||      }|2|j	                  |t        |      t        |      t        ||      d       |dz   }`)Nz<Ir   )refVarefVaHextargetVaHexr8   r   )structpackfindr   appendr   r;   )exer7   targetpatternrefsr9   indexr2   s           r   
dword_refsrI   N   s    kk$'GDE
%(19 K (E*>KK!"I$V})(B7	  	 r   c                R    | D ]  }t        |      |k(  s|c S  t        d| d      )N	selector z
 not found)r&   
ValueError)	selectorskeyr%   s      r   selector_rowrO   b   s9     #J yZ0
11r   c           
        t        | j                  d            }d}|dk(  r&| j                  d      |j                  d      k(  rd}n*|dk(  r%| j                  d      |j                  d      k(  rd}i | ||| j                  d	      d
k(  |:t        |j                  d             |t        |j                  d            z
  dS d dS )Nr>   other
rowPointerrowPointerVaHex selector-table-row-pointer-entryselectedRootselectedPointerVaHex selector-row-selected-root-entryr8   z.text)refKindrole	isTextRefoffsetFromRowPointerVa)r    r$   )r%   selectorref_kindref_varY   s        r   classify_refr_   i   s    swwz*+FD<CGGJ$78<<HY;Z$Z1	^	#
(;x||Lb?c(c1


WWY'72 !i=N0O&P&\ Yx||,=>??
 
 
 
r   c                j   t        |j                  d            }t        |j                  d            }||t        dt        |       d      t	        | ||      D cg c]  }t        ||d       }}t	        | ||      D cg c]  }t        ||d       }}||z   D cg c]  }|j                  d      s| }}t        |      dk(  xrE |d	   j                  d
      dk(  xr, t        |      dk(  xr |d	   j                  d
      dk(  xr | }	|j                  d      xs g }
t        |      |j                  d      |j                  d      |j                  d      |j                  d      |
t        |
v t        |
v t        |      t        |      t        |      |||	dS c c}w c c}w c c}w )Nr   r   rK   z missing pointer fieldsrR   rU   rZ   r   r   rY   rT   rW   r   rS   rV   )r\   rowPointerTableEntryHexr   selectedRootEntryHexselectedRootHexr   containsSourcecontainsTargetrowPointerRefCountselectedRootRefCounttextRefCountrowPointerRefsselectedRootRefstableOnlyChain)	r    r$   rL   r&   rI   r_   lenSOURCETARGET)rD   r7   r\   row_pointerselected_rootrefrow_pointer_refsselected_root_refs	text_refstable_only_chain
field_mapss              r   build_selector_summaryrw   }   s   HLL9:Khll+?@AMm39\(%;$<<STUU c8[9 	S(L1  c8]; 	S(N3  !13E E^Q\I]^I^" 	Q##F+/QQ	"#q(	 q!%%f-1SS	 M  k*0bJ *#+<<0A#B!o6 (-C D#<<(<= J. J.!"23 #$6 7I*.* # _s    F&F+"F09F0c                V   ||nt        t        dz  g       }t        |       }t        D cg c]  }t	        | |t        ||             }}|d   }|d   }|d   t        d |D              }t        d |D              }t        fd|d   |d	   z   D              }	d
   xr d   }
|d
   xr |d    xr |d   xr
 |d
    xr |
}d}i dt        dt        d|d|d|d|d|
d|	dddddddt        dt        dt        dt        t              dddg d d!|iS c c}w )"Nsave_scene_selectors.jsonr   r      c              3  &   K   | ]	  }|d      yw)rk   Nr   .0r%   s     r   	<genexpr>z build_summary.<locals>.<genexpr>   s     ?3-.?   c              3  &   K   | ]	  }|d      yw)rh   Nr   r|   s     r   r~   z build_summary.<locals>.<genexpr>   s     <N+<r   c              3  L   K   | ]  }|j                  d       d   k(    yw)r?   rc   Nr#   )r}   rq   current_rows     r   r~   z build_summary.<locals>.<genexpr>   s-      * 	+.?"@@*s   !$ri   rj   rd   re   a  The route-relevant selector roots 0:0, 1:0, and 2:0 each have a selector-table-only pointer chain: one table entry points to a selector row, and that row points to the selected root. No .text dword ref targets these row pointers or roots, and the predecessor root has no direct pointer to the current root. This reinforces the selector-merge gap: 0:0 is source-side, 1:0 is target-side, and 2:0 carries both maps, but the static root references are table membership, not execution order proof.sourcerE   rM   allRouteSelectorRootsTableOnlyanyRouteSelectorRootTextRefs(sourceTargetSplitAcrossPreviousSelectors currentSelectorContainsRoutePair predecessorToCurrentRootRefFoundrouteOrderProvenF
proofFoundrouteRootRefProofFoundfailedRouteRootRefGateIdsmissingEvidenceevidenceRefsevidenceRefCountpromotionStatusblockedremainingProofs)zXfind runtime/control-flow proof that target-side selector 1:0 state reaches selector 2:0zCfind a strict map1_01a source hotspot or equivalent runtime triggerzQreplace selector table adjacency with captured selector 2:0 save/runtime evidence
conclusion)r0   OUTr   ROUTE_SELECTORSrw   rO   allanyrm   rn   FAILED_ROUTE_ROOT_REF_GATE_IDSROUTE_ROOT_REF_MISSING_EVIDENCEROUTE_ROOT_REF_EVIDENCE_REFSrl   )rD   rM   r7   rN   rows
source_rowpredecessor_rowall_table_onlyany_text_refspredecessor_to_current_root_refcurrent_contains_pairsource_target_splitr   r   s                @r   build_summaryr      s   &2		#HcBceg8hIS!H # 	sHl9c.JKD  aJ1gOq'K?$??N<t<<M&) *"#34GY7ZZ* '# ((89[kJZ>[#$ 	"+,,	",-	"   011	" " 	Z && 	T 	).	
 	' 	34G 	+,A 	+,K 	E 	e 	!% 	$%C 	: 	4 	C <=  	9!" 	 
#, 	j- 9s   D&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j                  | j                  d      xs g       xs d dt        | j                  d      xs g        d| j                  d       d | d!    dd| d"   dd#dg}|j                  d$ | d   D               |j                  g d%       | d&   D ]a  }dj                  |d'         }|j	                  d(|d)    d*|d+    d*|d,    d*|d-    d*|d.    d/| d0|d1    d2|d3    d0|d4    d0|d5    d6       c |j                  g d7       |j                  d8 | d9   D               |j	                  d       d:j                  |      S );Nz,# Save Selector Route Root Reference Contextr5   z
- route: `r   z -> rE   `z'- all route selector roots table-only: r   z&- any route selector root .text refs: r   z1- source/target split across previous selectors: r   z(- current selector contains route pair: r   z)- predecessor -> current root ref found: r   z- route order proven: r   z- proof found: r   z- route root-ref proof found: r   z- failed route root-ref gates: , r   -z- missing evidence count: r   z- evidence refs: r   z- promotion status: `r   r   z## Missing Evidencec              3  &   K   | ]	  }d |   ywz- Nr   r}   items     r   r~   zmarkdown.<locals>.<genexpr>        D2dVDr   )r5   z## Selector Rootsr5   z| selector | row pointer entry | row pointer | root entry | selected root | field maps | refs row/root | text refs | table-only |z9| --- | --- | --- | --- | --- | --- | ---: | ---: | --- |rM   r   z| `r\   z` | `ra   r   rb   rc   z` | z | rf   /rg   rh   rk   z |)r5   z## Remaining Proofsr5   c              3  &   K   | ]	  }d |   ywr   r   r   s     r   r~   zmarkdown.<locals>.<genexpr>  r   r   r   
)joinr$   rl   extendrC   )summarylinesr%   mapss       r   markdownr      sd   6

WX&'tGH,=+>a@
1':Z2[1\]
09W1X0YZ
;GDn<o;pq
27;]3^2_`
3G<^4_3`a
 );!< =>
',/01
(1I)J(KL
)$))GKK@[4\4b`b*c*jgj)kl
$S5F)G)M2%N$OP
GKK(:;<=
(9 :;1=


)E, 
LLD1B)CDD	LL   {# 
yy[)*#j/"%,E(F'GuSQ`MaLb c*+,E#6G2H1IdVSV'()3/E+F*Gs3~K^J__b#$%R)	

 
LL01	LLD1B)CDD	LL99Ur   c                   g }| d   D ]  }|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                d|d    d|d    d|d    d|d    d        dj                  d | j	                  d      xs g D              }dj                  d | j	                  d      xs g D              }dj                  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          d3| d4    d5| d6    d7| d8    d9| d:    d;| d<    d=t        | j	                  d      xs g        d>| j	                  d?       d@t        j                  | dA          dBdCt        j                  | dD          dEdF| dGdH| dGdIdJ|dKdLdM| dGdNdOd      S )PNrM   z<tr><td><code>r\   z</code></td><td><code>ra   r   rb   rc   z</code></td><td>r   r   z	</td><td>rf   r   rg   rh   rk   z
</td></tr>r5   c              3  N   K   | ]  }d t        j                  |       d  ywz<li>z</li>Nhtmlescaper   s     r   r~   zhtml_page.<locals>.<genexpr>  s(      ,0$t{{4 !'   #%r   c              3  N   K   | ]  }d t        j                  |       d  ywr   r   r   s     r   r~   zhtml_page.<locals>.<genexpr>  s#     g$T[[./u5gr   r   c              3  N   K   | ]  }d t        j                  |       d  ywr   r   r   s     r   r~   zhtml_page.<locals>.<genexpr>  s#     \tDKK-.e4\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">z;  <title>Save Selector Route Root Reference Context</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: 1180px; 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>z5  <h1>Save Selector Route Root Reference Context</h1>z  <p>route <code>r   z -&gt; rE   z.</code>; all route selector roots table-only: r   z&; any route selector root .text refs: r   z1; source/target split across previous selectors: r   z); predecessor -> current root ref found: r   z; proof found: r   z; missing evidence count: z; evidence refs: r   z; promotion status <code>r   z</code>.</p>z  <p>r   z</p>z*  <h2>Failed Route Root-Ref Gates</h2><ul>z</ul>z  <h2>Missing Evidence</h2><ul>z  <h2>Selector Roots</h2>z  <table><thead><tr><th>selector</th><th>row pointer entry</th><th>row pointer</th><th>root entry</th><th>selected root</th><th>field maps</th><th>refs row/root</th><th>text refs</th><th>table-only</th></tr></thead><tbody>z  </tbody></table>z  <h2>Remaining Proofs</h2>z  <ul>z</body>z</html>)rC   r   r   r   r$   rl   )r   selector_rowsr%   failed_gatesmissingproofss         r   	html_pager     s   M{# 
S_56 7S)B%CDE FS%9:; <S)?%@AB CS):%;<= >;;tyy[)9:;< =+,-Qs3I/J.K L~&' ('() *
	

 77 4;KK@[4\4b`b L ggg'++N_B`BfdfggGWW\IZA[\\F99 *** 	* 	#	*
 	Q* 	F* 	* 	r* 	1* 	7* 	:* 	G* 	f* 	t* 	Q*  	'!*" 	#*$ 	%*& 	'*( 	@)** DKK(9:;74;;wW_O`CaBb c0078X0Y/Z [//67U/V.W X::ABl:m9n o229:\2]1^ _-. /##&w{{3D'E'K#L"M N!++&89: ;""&++g6G.H"I!J,	X+*< GL12348=*> 5\N%H?*@ *'%8A*B 	$C*D 	iE*F 
G*H 	I*J 	&K*L M*N 	O*P 	Q*R 	S* * *r   c                   |j                  dd       |dz  }|j                  t        j                  | dd      dz   d	       |9|j                  j                  dd       |j                  t        |       d	       |S )
NT)parentsexist_okz)save_selector_route_root_ref_context.jsonFrz   )ensure_asciiindentr   r(   r)   )mkdir
write_textr,   dumpsparentr   )r   out_dirhtml_outjson_outs       r   write_outputsr   M  s    MM$M.DDH

7q9D@   dT:Ig.AOr   c                    t        j                  t              } | j                  dt        t
        dz         | j                  dt        t        dz         | j                  dt        t               | j                  dt        	       | j                         }t        |j                  j                         t        |j                  g             }t        ||j                  |j                        }t!        d
|        y )N)descriptionz--exer   )typedefaultz--selectorsry   z	--out-dirz
--html-out)r   z4wrote save selector route root reference context -> )argparseArgumentParser__doc__add_argumentr   ROOTr   
parse_argsr   rD   
read_bytesr0   rM   r   r   r   print)parserargsr   r   s       r   mainr   Z  s    $$9F
dD=4HI
D#@[:[\
$<
40DDHH//19T^^R3PQGWdllDMMBH	@

KLr   __main__)r   r   returnr6   )r   
str | Noner   z
int | None)r%   dictr   r6   )r   r   r/   r   r   r   )r7   
list[dict]r2   r   r   r   )rD   bytesr7   r   rE   r   r   r   )rM   r   rN   r6   r   r   )r%   r   r\   r   r]   r6   r   r   )rD   r   r7   r   r\   r   r   r   )N)rD   r   rM   zlist[dict] | Noner   r   )r   r   r   r6   )r   r   r   r   r   zPath | Noner   r   )r   None)-r   
__future__r   r   r   r,   r@   syspathlibr   typingr   r   insertr6   __file__resolver   probe_exe_scene_tablesr   r   r   r   r   rm   rn   r   r   r   r   r   r    r&   r0   r;   rI   rO   r_   rw   r   r   r   r   r   __name__r   r   r   <module>r      s   O "     
   3tH~--/667 8 > H~''*
Ul		'" #  
 0W   -38(2(%P6r*Z?D 25d 
	M zF r   