
    Ki.                       U d Z ddlmZ ddlZddlmZ ddlmZ h dh dh dh d	d
Zde	d<   dddddddddgdddddddddgdddddddddgddddd ddd!dgdd"dd#d$ddd%dgd#d&ddd'ddd(dgdd)ddd*dd+d,dd-d.dgdd/ddd0dd1d2dgdd3ddd4dd+d5dgd6	Z
d7e	d8<   d9Zd:Zd;Z G d< d=      ZdFd>Z	 	 	 	 	 	 	 	 dGd?ZdHd@Z	 	 	 	 	 	 dIdAZdJdBZ	 	 	 	 	 	 	 	 dKdCZ e       adLdDZdMdEZy)Na4  Context-aware hints system for MCP tool responses.

Tracks session state (in-memory only) and generates intelligent
next-step suggestions after each tool call.  Hints are appended as
``_hints`` to new tool responses so that Claude Code can propose
follow-up actions without the user having to discover them.
    )annotationsN)deque)Any>   detect_changesget_impact_radiusget_affected_flowsget_review_context>   get_flowquery_graphsemantic_search_nodes>   refactorfind_dead_codesuggest_refactorings>   
list_flowslist_communitieslist_graph_statsget_architecture_overview)	reviewing	debuggingrefactoring	exploringzdict[str, set[str]]_INTENT_TOOLSr
   z3Drill into a specific flow for step-by-step details)tool
suggestionr   z0Check which flows are affected by recent changesr   zSee the high-level architecturer   z.Inspect callers/callees of a step in this flowz!Check if changes affect this flowr   zBrowse other execution flowsr   zGet risk-scored change analysisz Inspect a specific affected flowr	   z+Build a full review context for the changesget_communityz&Inspect a specific community's membersz)See cross-community coupling and warningsz&See execution flows across communitiesz,Explore callers/callees of community membersr   zBrowse other communitiesz,See how this community fits the architecturez!Drill into individual communitiesz.See how recent changes affect the architecturezExplore execution flowsz0Build a full review context with source snippetsz&See which execution flows are affectedr   z Expand the blast radius analysisr   z2Look for refactoring opportunities in changed codez*Verify call sites before applying a renamez!Check risk of the refactored coder   z#Find related symbols to also renamez*Inspect callers/callees of a search resultz-See the execution flow through a matched nodez)Check the blast radius from matched nodes)	r   r
   r   r   r   r   r   r   r   zdict[str, list[dict[str, str]]]	_WORKFLOW   d   i  c                  0    e Zd ZdZddZddZd	dZd
dZy)SessionStatez4In-memory session state for a single MCP connection.c                    t        t              | _        t               | _        t               | _        d | _        d| _        y )N)maxleng        )r   _MAX_TOOLS_HISTORYtools_calledsetnodes_queriedfiles_touchedinferred_intentlast_tool_time)selfs    c/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/code_review_graph/hints.py__init__zSessionState.__init__   s4    (-5G(H'*u'*u+/%(    c                l    | j                   j                  |       t        j                         | _        y)z/Record a tool invocation (FIFO, capped at 100).N)r$   appendtimer)   )r*   	tool_names     r+   record_tool_callzSessionState.record_tool_call   s$      +"iikr-   c                    |D ];  }t        | j                        t        k\  r y| j                  j                  |       = y)z1Record queried node identifiers (capped at 1000).N)lenr&   _MAX_NODES_TRACKEDadd)r*   node_idsnids      r+   record_nodeszSessionState.record_nodes   s<     	(C4%%&*<<""3'	(r-   c                :    | j                   j                  |       y)zRecord touched file paths.N)r'   update)r*   filess     r+   record_fileszSessionState.record_files   s    !!%(r-   NreturnNone)r1   strr?   r@   )r7   	list[str]r?   r@   )r<   rB   r?   r@   )__name__
__module____qualname____doc__r,   r2   r9   r=    r-   r+   r    r       s    >)*
()r-   r    c                "   | j                   syt        | j                         dd }t        D ci c]  }|d c}|D ]0  }t        j                         D ]  \  }}||v s|xx   dz  cc<    2 t	        fd      }|   dk(  ry|S c c}w )zClassify the user's likely intent from their tool-call history.

    Returns one of: ``"reviewing"``, ``"debugging"``, ``"refactoring"``,
    ``"exploring"`` (default).
    r   iNr      c                    |    S )NrG   )kscoress    r+   <lambda>zinfer_intent.<locals>.<lambda>   s    VAY r-   )key)r$   listr   itemsmax)sessionrecentintentr   toolsbestrL   s         @r+   infer_intentrW      s      '&&'-F6CDFfaiDF $*002 	$MFEu}v!#	$$
 v./Dd|qK Es   
Bc                    |j                  |        t        |      |_        t        | |      }t	        |      }t        | ||      }t        ||       |dt         |dt         |dt         dS )aX  Build context-aware hints for a tool response.

    Returns::

        {
            "next_steps": [{"tool": ..., "suggestion": ...}, ...],
            "related": [...],
            "warnings": [...],
        }

    At most ``_MAX_PER_CATEGORY`` items per list.  Tools already called
    in this session are suppressed from ``next_steps``.
    N)
next_stepsrelatedwarnings)r2   rW   r(   _build_next_steps_extract_warnings_build_related_track_result_MAX_PER_CATEGORY)r1   resultrR   rY   r[   rZ   s         r+   generate_hintsrb      s    & Y'*73G"9g6J (H Y8G &'" !!3"34--.//0 r-   c           
        dD ]T  }| j                  |      }t        |t              s%|j                  |D cg c]  }t        |t              s| c}       V g }dD ]a  }| j                  |      }t        |t              s%|D ]8  }t        |t
              s|j                  d      }|s(|j                  |       : c |r|j                  |       yyc c}w )zCExtract node IDs and file paths from a tool result and record them.)changed_filesimpacted_files)resultschanged_nodesimpacted_nodesqualified_nameN)get
isinstancerO   r=   rA   dictr/   r9   )	ra   rR   rN   r<   fr7   rP   itemqns	            r+   r_   r_     s     3 K

3eT"  U!IjC6H!!IJK H= ,

3eT" ,dD)"23B +	,, X&  "Js   C
C
c                    t        |j                        }t        j                  | g       }g }|D ]  }|d   |vs|j	                  |        |S )z=Return next-step suggestions, filtering already-called tools.r   )r%   r$   r   rj   r/   )r1   rR   called
candidatesoutcs         r+   r\   r\   1  sV     %%&Fy"-J "C V9F"JJqM Jr-   c                   g }| j                  d      }t        |t              rb|r`|dd D cg c]/  }t        |t              r|j                  d|      n
t	        |      1 }}|j                  ddj                  |              | j                  d      }t        |t        t        f      r|dkD  r|j                  d	|d
d       | j                  d      }t        |t              rV|dd D ]N  }t        |t              r|j                  |       %t        |t              s6d|v s;|j                  |d          P |S c c}w )z(Pull warning signals from a tool result.	test_gapsN   namezTest coverage gaps: z, 
risk_scoregffffff?zHigh risk score (z.2fu   ) — review carefullyr[   r   message)	rj   rk   rO   rl   rA   r/   joinintfloat)ra   r[   rv   gnamesriskarch_warningsws           r+   r]   r]   >  s+   H 

;'I)T"yNWXZYZm\Z4%8vq!c!fD\\"499U#3"45	

 ::l#D$e%$*+D:5KLM JJz*M-&r" 	.A!S!"At$a)-		. O' ]s   4Ec                &   g }t               }|j                  d      }t        |t              rc|D ]^  }t        |t              s||j
                  vs#||vs(|j                  |       |j                  |       t        |      t        k\  s] |S  |S )z6Suggest related node/file identifiers from the result.re   )
r%   rj   rk   rO   rA   r'   r/   r6   r4   r`   )r1   ra   rR   rZ   seenimpactedrm   s          r+   r^   r^   [  s     GUD zz*+H(D! 	A!S!aw/D/D&DRVq!w<#44N	 Nr-   c                     t         S )z*Return the global in-memory session state.)_sessionrG   r-   r+   get_sessionr   x  s    Or-   c                     t               ay)z.Reset the global session (useful for testing).N)r    r   rG   r-   r+   reset_sessionr   }  s     ~Hr-   )rR   r    r?   rA   )r1   rA   ra   dict[str, Any]rR   r    r?   r   )ra   r   rR   r    r?   r@   )r1   rA   rR   r    r?   zlist[dict[str, str]])ra   r   r?   rB   )r1   rA   ra   r   rR   r    r?   rB   )r?   r    r>   )rF   
__future__r   r0   collectionsr   typingr   r   __annotations__r   r`   r#   r5   r    rW   rb   r_   r\   r]   r^   r   r   r   rG   r-   r+   <module>r      s   #   
&" ( O	

 )L	

 0;	
  "J	

 )=	

 !8	
  %;	

 <	

 )G	
  $B	

 0E	

 !B	
  "H	

 '4	

 0H	
  '=	

 %J	

 !3	
"  )L	

 )B	

 (<	

 N	
( "F	

 %=	

 ,?	
  "F	

 I	

 (E	
kC.	* CL     ) )@8### # 	#V',

)

:  	4 >
r-   