
    !Jju?                    6   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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Zd'dZd#d(dZ d)dZ!e"dk(  r e!        yy)*zSClassify the nearest local selected-pointer producer before opcode 0x08 activators.    )annotationsN)Path)Any   outmap1_01amap2_02dz0:0z1:0z2:0c                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)pathfallbacks     P/home/exedev/hwanse/tools/summarize_save_selector_opcode08_activation_windows.py	load_jsonr      s*    ;;=::dnngn677    c                @    t        | j                  d      xs dd      S )NvaHex0   )intgetrows    r   va_intr      s    swww&3++r   c                t   | j                  d      }|dk(  r| j                  d      xs i }| j                  d      || j                  d      | j                  d      ||j                  d      |j                  d      | j                  d	      d
u | j                  d      d
u d	S |dk(  r| j                  d      xs i }| j                  d      || j                  d      | j                  d      ||j                  d      |j                  d      | j                  d      d
u | j                  d      d
u d	S i S )N	opcodeHex0x07selectedValueClassr   valueHexselectedValueHexkindselectorselectedCurrentRootTselectedCurrentRange)	producerVaHexproducerOpcodeHexproducerValueHexproducerPointerHexproducerPointerClassproducerPointerKindproducerPointerSelectorproducesCurrentRootproducesCurrentRange0x09storedPointerClassstoredPointerHexstoresCurrentRootstoresCurrentRanger   )r   opcodepointer_classs      r   producer_pointerr;       s;   WW[!F 45; WWW-!' #
 3"%''*<"=$1#0#4#4V#<'4'8'8'D#&77+@#AT#I$'GG,B$Ct$K

 
	
  45; WWW-!' #
 3"%''*<"=$1#0#4#4V#<'4'8'8'D#&77+>#?4#G$'GG,@$AT$I

 
	
 Ir   c                    |sy|j                  d      ry|j                  d      ry|j                  d      | k(  ry|j                  d      }|rt        |      S y	)
Nno-local-producerr1   zcurrent-rootr2   zcurrent-ranger0   z	own-ranger/   unknown)r   str)r'   producerr&   s      r   producer_bucketrA   ?   s\    "||)*||*+||-.(:<<-.D4yr   c                   |xs i }d }|j                  d      rt        |      t        |d   d      z
  }| |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d	      |j                  d
      du |j                  d      du |j                  d	      | k(  t        | |      |dS )Nr*   r   r   r$   r+   r,   r-   r/   r0   r1   Tr2   )r'   activationVaHexactivationValueHexnearestProducerVaHexnearestProducerOpcodeHexnearestProducerValueHexnearestProducerPointerHexnearestProducerPointerKindnearestProducerPointerSelector"nearestProducerProducesCurrentRoot#nearestProducerProducesCurrentRangenearestProducerOwnRangenearestProducerBucketdistanceBytes)r   r   r   rA   )r'   r   r@   distances       r   activation_rowrQ   N   s    ~2HH||O$#;Xo%>!CC777+!ggj1 (_ =$,LL1D$E#+<<0B#C%-\\2F%G&.ll3H&I*2,,7P*Q.6ll;P.QUY.Y/7||<R/SW[/[#+<<0I#Jh#V!08!D! r   c                    | j                  d      }t        | j                  d      xs g t              }d }g }|D ]E  }|j                  d      }|dv rt        |      }$|dk(  s*|j	                  t        |||             G t        | |      S )Nr'   rows)keyr!   >   r"   r3   0x08)r   sortedr   r;   appendrQ   summarize_context)scanr'   rS   last_produceractivationsr   r9   s          r   scan_contextr\   e   s    xx
#H$((6"(bf5D!%MK M%%%,S1Mv~h]KLM T;//r   c                    ddfd}| j                  d      | j                  d      | j                  d      t               |d       |d       |d       |dd	       |dd
       |dd      dS )Nc                2     t         fdD              S )Nc              3  L   K   | ]  }|j                        k(  sd   yw)r   Nr8   ).0r   rT   values     r   	<genexpr>z3summarize_context.<locals>.count.<locals>.<genexpr>u   s      Eswws|u/D1Es   $$)sum)rT   ra   r[   s   ``r   countz summarize_context.<locals>.countt   s    EEEEr   r'   rolerangeHexrK   rL   rM   rN   zscript-scalar-or-pair
unreadabler=   )r'   re   rf   activationCountnearestCurrentRootProducerCount nearestCurrentRangeProducerCountnearestOwnRangeProducerCount nearestScriptScalarProducerCountnearestUnreadableProducerCountnearestNoLocalProducerCountr[   )T)rT   r?   ra   r   returnr   )r   len)rY   r[   rd   s    ` r   rX   rX   s   s    F HHZ( HHZ({++01U+V,12W,X(-.G(H,12IKb,c*/0G*V',-DFY'Z" r   c                   | xs t        t        dz  i       } | j                  d      xs g D cg c]  }t        |       }}|D cg c]!  }|j                  d      t        t
        hv r|# }}t        d |D        i       }t        d |D              }t        d |D              }t        d |D              }t        d |D              }t        d	 |D              }	t        d
 |D              }
|j                  dd      }d}i dt        dt        dt        dt
        dt        d|d|d|d|d|d|	d|
d|dddddg d d!|S c c}w c c}w )"N0save_selector_selected_pointer_opcode_paths.jsoncontextScansr'   c              3  R   K   | ]  }|j                  d       t        k(  s| ! yw)r'   N)r   CURRENT_SELECTOR)r`   rY   s     r   rb   z build_summary.<locals>.<genexpr>   s!     WTdhhz.BFV.VDWs   ''c              3  &   K   | ]	  }|d      yw)rh   N r`   r   s     r   rb   z build_summary.<locals>.<genexpr>   s     ,eS1B-C,e   c              3  &   K   | ]	  }|d      yw)ri   Nrw   rx   s     r   rb   z build_summary.<locals>.<genexpr>   s     /x[^4U0V/xry   c              3  &   K   | ]	  }|d      yw)rj   Nrw   rx   s     r   rb   z build_summary.<locals>.<genexpr>        0z]`5W1X0zry   c              3  &   K   | ]	  }|d      yw)rk   Nrw   rx   s     r   rb   z build_summary.<locals>.<genexpr>   s     ,rUXS1O-P,rry   c              3  &   K   | ]	  }|d      yw)rl   Nrw   rx   s     r   rb   z build_summary.<locals>.<genexpr>   r|   ry   c              3  &   K   | ]	  }|d      yw)rm   Nrw   rx   s     r   rb   z build_summary.<locals>.<genexpr>   s     -uX[c2R.S-ury   rj   r   a  Nearest local producer windows before source/predecessor opcode 0x08 activators still do not produce the current selector 2:0 root/range. The windows are dominated by own-range continuation stores, script-scalar table selections, and unreadable pointer-like operands; only current 2:0 has nearest current-range producers, and those are internal current-root continuations. Therefore opcode 0x08 activation still requires a runtime selected-pointer trace or a real selector 2:0 save before route promotion.sourcetargetsourceSelectorpredecessorSelectorcurrentSelectorcontextWindows)sourceOrPredecessorOpcode08ActivatorCount+sourceOrPredecessorCurrentRootProducerCount,sourceOrPredecessorCurrentRangeProducerCount(sourceOrPredecessorOwnRangeProducerCount,sourceOrPredecessorScriptScalarProducerCount*sourceOrPredecessorUnreadableProducerCount(currentInternalCurrentRangeProducerCountopcode08ActivationPromotesRouteFpromotionStatusblockedremainingProofs)z7capture a selected-pointer runtime trace at opcode 0x08zOreplace the synthetic selector 2:0 savedat vector with a captured gameplay savez3find a strict map1_01a source coordinate or hotspot
conclusion)r   OUTr   r\   SOURCE_SELECTORPREDECESSOR_SELECTORnextrc   SOURCETARGETru   )selected_pointer_opcode_pathsrY   scanssource_or_predecessorcurrent"source_predecessor_activator_count%source_predecessor_current_root_count&source_predecessor_current_range_count"source_predecessor_own_range_count&source_predecessor_script_scalar_count#source_predecessor_unreadable_count$current_internal_current_range_countr   s                r   build_summaryr      s	   $A %Y@@
F! -J,M,Mn,],cacdD\$dEd88JO5I#JJ 	  WUWY[\G),,eOd,e)e&,//xbw/x,x)-00zdy0z-z*),,r\q,r)r&-00zdy0z-z**--u_t-u*u'+2;;7Y[\+](	 && 	/ 	3	
 	+ 	% 	45W 	67\ 	78^ 	34V 	78^ 	56Y 	34X 	*5 	9  	 
!* 	j+ + es   E&Ec                    d| j                  d       d| j                  d       d| j                  d       d| j                  d       d	| j                  d
       
S )Nzop8=rh   z currentRange=rj   z
 ownRange=rk   z scriptScalar=rl   z unreadable=rm   r8   r   s    r   context_briefr      su    
sww()* + BCD EGG:;< = BCD Egg>?@		Br   c                    g }| j                  d      xs g D ]E  }|j                  d      r|j                  |       &t        |      dk  s5|j                  |       G |d d S )Nr[   rL   
       )r   rW   rp   )contextrS   r   s      r   interesting_activationsr      sb    D{{=)/R 7789KKY^KK	
 9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g}| d   D ]2  }|j                  d|d     d!|d"    d#|d$    d!t        |       d%	       4 |j                  g d&       | d   D ]  }t        |      D ]  }|j                  d|d     d'|d(    d'|j	                  d)      xs d* d'|j	                  d+      xs d* d'|j	                  d,      xs d* d!|j	                  d-      xs d* d.|j	                  d/      |j	                  d/      nd* d%         |j                  g d0       |j                  d1 | d2   D               |j                  d       d3j                  |      S )4Nz.# Save Selector Opcode 0x08 Activation Windows z	- route: r   z -> r   z!- source/predecessor activators: r   z-- source/predecessor current-root producers: r   z.- source/predecessor current-range producers: r   z- own-range producers: r   z- script-scalar producers: r   z- unreadable producers: r   z,- current internal current-range producers: r   z&- opcode08 activation promotes route: r   z- promotion status: `r   `r   z## Context Windowsz$| selector | role | range | counts |z| --- | --- | --- | --- |r   z| `r'   z` | re   z | `rf   z |)r   z## Activation Samplesr   zS| selector | activation | nearest producer | opcode | pointer | bucket | distance |z,| --- | --- | --- | --- | --- | --- | ---: |z` | `rC   rE   -rF   rH   rN   z | rO   )r   z## Remaining Proofsr   c              3  &   K   | ]	  }d |   yw)z- Nrw   r`   items     r   rb   zmarkdown.<locals>.<genexpr>   s     D2dVDry   r   
)rW   r   extendr   r   join)summarylinesr   r   s       r   markdownr      s{   8

GH%&d78+<*=>
+G4_,`+ab
7@m8n7op
8Ao9p8qr
!'*T"U!VW
%g.\&]%^_
"7+W#X"YZ
6w?i7j6kl
09Z1[0\]
(9 :;1=


.#'E* +, 
'*%&d76?*;4
@S?TTXYfgnYoXpprs	

 
LL   +, *73 	CLLc*o&eC0A,B+C5QgIhIoloHp qGG67>3?uSWWMhEiEpmpDqqu7723:s;3[^[b[bcr[s[sww?W  FI  ?J  JLM	 
LL01	LLD1B)CDD	LL99Ur   c           !        dj                  d | d   D              }g }| d   D ]t  }t        |      D ]b  }|j                  dt        j                  t        |d                dt        j                  t        |d                dt        j                  t        |j                  d      xs d	             dt        j                  t        |j                  d
      xs d	             dt        j                  t        |j                  d      xs d	             dt        j                  t        |j                  d      xs d	             dt        j                  t        |j                  d      |j                  d      nd	             d       e w 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%t        j                  | d&          d't        j                  | d(          d)| d*    d+| d,    d-| d.    d/d0| d1    d2| d3    d4| d5    d6| d7    d/	d8t        j                  | d9          d:d;d<|d=d>d?dj                  |      d=d@| dAdBg      S )CNr   c              3  ,  K   | ]  }d t        j                  t        |d                dt        j                  t        |d                dt        j                  t        |d                dt        j                  t        |             d	  yw)<tr><td><code>r'   </code></td><td>re   z</td><td><code>rf   
</td></tr>N)htmlescaper?   r   )r`   r   s     r   rb   zhtml_page.<locals>.<genexpr>   s       	[[WZ%8!9:; <{{3wv/01 2[[WZ%8!9:; <{{=123 4		s   BBr   r   r'   z</code></td><td><code>rC   rE   r   rF   rH   r   rN   z	</td><td>rO   r   r   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li>z</li>N)r   r   r   s     r   rb   zhtml_page.<locals>.<genexpr>  s#     \tDKK-.e4\s   #%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 0x08 Activation Windows</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[    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; }z,    th { background: #181818; color: #ddd; }z    code { color: #d7f0ff; }z
  </style>z</head>z<body>z7  <h1>Save Selector Opcode 0x08 Activation Windows</h1>z  <p>route r   z -&gt; r   z ; source/predecessor activators r   z-; source/predecessor current-range producers r   z%; opcode08 activation promotes route r   z.</p>z  <p>own-range producers r   z; script-scalar producers r   z; unreadable producers r   z+; current internal current-range producers r   z  <p>r   z</p>z  <h2>Context Windows</h2>zc  <table><thead><tr><th>selector</th><th>role</th><th>range</th><th>counts</th></tr></thead><tbody>z  </tbody></table>z  <h2>Activation Samples</h2>z  <table><thead><tr><th>selector</th><th>activation</th><th>nearest producer</th><th>opcode</th><th>pointer</th><th>bucket</th><th>distance</th></tr></thead><tbody>z  <h2>Remaining Proofs</h2><ul>z</ul>z</body></html>)r   r   rW   r   r   r?   r   )r   context_rowsactivation_rowsr   r   proofss         r   	html_pager      sS   99  /0 L O+, *73 	C""![[S_)=>? @![[S1B-C)DEF G![[SWW5K-L-SPS)TUV W![[SWW5O-P-WTW)XYZ [![[SWW5P-Q-XUX)YZ[ \{{3sww/F'G'N3#OPQ R{{3377?C[Cgsww'?mp#qrs t
	 WW\IZA[\\F99"PGq0es6&A
dkk'("345WT[[QYIZ=[<\\|  ~E  Fq  ~r  }s  s`  ah  iW  aX  `Y  Y~  F  Gh  i  ~j  jo  	p
#G,V$W#XXrsz  |j  tk  sl  lC  DK  Lx  Dy  Cz  ze  fm  nX  fY  eZ  Z_  	`
GL12348$m' 	o		/"
)&7=  r   c                    |j                  dd       |dz  j                  t        j                  | dd      d       |:|j                  j                  dd       |j                  t        |       d       y y )	NT)parentsexist_ok.save_selector_opcode08_activation_windows.jsonF),:)ensure_ascii
separatorsr   r   )mkdir
write_textr   dumpsparentr   )r   out_dirhtml_outs      r   write_outputsr   6  s{    MM$M.??KK

7:F L  dT:Ig.A r   c                    t        j                         } | j                  dt        t        dz         | j                  dt        t               | j                  dt        d d       | j                         }t        t        |j                  i             }t        ||j                  |j                         t        d|j                  d	z          y )
Nz--selected-pointerrr   )typedefaultz	--out-dirz
--html-outz!Optional HTML report output path.)r   r   helpz6wrote save selector opcode 0x08 activation windows -> r   )argparseArgumentParseradd_argumentr   r   
parse_argsr   r   selected_pointerr   r   r   print)parserargsr   s      r   mainr   A  s    $$&F
,4GyAyz
$<
4DghDId&;&;R@AG'4<<7	B4<<  SC  DC  CD  E  Fr   __main__)r   r   r   r   ro   r   )r   dictro   r   )r   r   ro   r   )r'   r?   r@   dict | Nonero   r?   )r'   r?   r   r   r@   r   ro   r   )rY   r   ro   r   )rY   r   r[   
list[dict]ro   r   )N)r   r   ro   r   )r   r   ro   r?   )r   r   ro   r   )r   r   ro   r?   )r   r   r   r   r   zPath | Nonero   None)ro   r   )#__doc__
__future__r   r   r   r   pathlibr   typingr   __file__resolver   ROOTr   r   r   r   r   ru   r   r   r;   rA   rQ   r\   rX   r   r   r   r   r   r   r   __name__rw   r   r   <module>r      s    Y "      H~''*
Ul		  8,>.0&0f+\8vBF zF r   