
    yj5                         d Z ddlmZ ddlmZ ddlmZ  G d de          Z G d de          Z G d	 d
e          Z	 G d de          Z
 G d de          Z G d de          Zg dZdS )af  
Agent events for Honcho telemetry.

Agent events track per-iteration metrics and state-changing tool executions
within agentic loops (dream specialists, dialectic agents). These events
correlate with parent events via run_id for detailed analytics.

Events in this module:
- AgentIterationEvent: Per-LLM-call metrics within an agent run
- AgentToolConclusionsCreatedEvent: Conclusions created by agent tool
- AgentToolConclusionsDeletedEvent: Conclusions deleted by agent tool
- AgentToolPeerCardUpdatedEvent: Peer card updated by agent tool
- AgentToolSummaryCreatedEvent: Summary created by agent tool (future)
    )ClassVar)Field)	BaseEventc                   z   e Zd ZU dZdZee         ed<   dZee	         ed<   dZ
ee         ed<   dZee         ed	<    ed
d          Zeed<    ed
d          Zeed<    ed
d          Zeed<    ed
d          Zeed<    edd          Zedz  ed<    edd          Zedz  ed<    edd          Zedz  ed<    ed
d          Ze	ed<    eed          Zee         ed <    ed
d!          Ze	ed"<    ed
d#          Ze	ed$<    ed%d&          Ze	ed'<    ed%d(          Ze	ed)<   d*efd+ZdS ),AgentIterationEventzEmitted after each LLM call within an agentic loop.

    Tracks per-iteration token usage and tool calls for detailed cost analysis.
    Correlates with parent events (dream.run, dream.specialist, dialectic.completed)
    via run_id.
    zagent.iteration_event_type   _schema_versionagent	_categoryhigh_volume_volume_class.Nanoid for run correlationdescriptionrun_id'Parent category: 'dream' or 'dialectic'parent_category4Agent type: 'deduction', 'induction', or 'dialectic'
agent_typeWorkspace nameworkspace_nameNObserver peer namedefaultr   observerObserved peer nameobservedzPeer name being queried	peer_nameIteration number (1-indexed)	iterationz>Tool names called in this iteration (can be empty or multiple)default_factoryr   
tool_callszInput tokens for this iterationinput_tokensz Output tokens for this iterationoutput_tokensr   zTokens read from prompt cachecache_read_tokenszTokens written to prompt cachecache_creation_tokensreturnc                 $    | j          d| j         S )9Resource ID includes run_id and iteration for uniqueness.:r   r!   selfs    B/DATA/AppData/hermes/projects/honcho/src/telemetry/events/agent.pyget_resource_idz#AgentIterationEvent.get_resource_idJ   s    +00000    )__name__
__module____qualname____doc__r   r   str__annotations__r
   intr   r   r   r   r   r   r   r   r   r   r!   listr$   r%   r&   r'   r(   r1    r2   r0   r   r      s          "3K#222%&OXc]&&&&Ix}&&&#0M8C=000 %)EFFFFCFFF !5B  OS    eO  J     %1ABBBNCBBB !5;OPPPHcDjPPP 5;OPPPHcDjPPP "E$<UVVVIsTzVVV U3,JKKKIsKKK "ET  JS	    c/PQQQL#QQQs0RSSSM3SSS"U>  s    "'?" " "3   1 1 1 1 1 1 1r2   r   c                      e Zd ZU dZdZee         ed<   dZee	         ed<   dZ
ee         ed<    edd	
          Zeed<    edd
          Ze	ed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Ze	ed<    eed          Zee         ed<   defdZdS )  AgentToolConclusionsCreatedEventz~Emitted when the create_conclusions tool is executed.

    Tracks conclusion creation with level breakdown for analytics.
    zagent.tool.conclusions.createdr   r	   r
   r   r   .r   r   r   #Iteration number when this occurredr!   r   r   r   r   r   r   r   r   r   r   zNumber of conclusions createdconclusion_countzGLevel of each conclusion (e.g., ['explicit', 'deductive', 'deductive'])r"   levelsr)   c                 &    | j          d| j         dS )r+   r,   z:conclusions_createdr-   r.   s    r0   r1   z0AgentToolConclusionsCreatedEvent.get_resource_idq       +DDDDDDr2   Nr3   r4   r5   r6   r   r   r7   r8   r
   r9   r   r   r   r!   r   r   r   r   r   r?   r:   r@   r1   r;   r2   r0   r=   r=   O   s         
 "BK#AAA%&OXc]&&&&Ix}&&& %)EFFFFCFFFU3,QRRRIsRRR !5B  OS    eO  J     %1ABBBNCBBB E#+?@@@Hc@@@E#+?@@@Hc@@@ "E#3RSSScSSS]  FDI   
E E E E E E Er2   r=   c                      e Zd ZU dZdZee         ed<   dZee	         ed<   dZ
ee         ed<    edd	
          Zeed<    edd
          Ze	ed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Ze	ed<    eed          Zee         ed<   defdZdS )  AgentToolConclusionsDeletedEventz~Emitted when the delete_conclusions tool is executed.

    Tracks conclusion deletion for memory consolidation analytics.
    zagent.tool.conclusions.deletedr      r
   r   r   .r   r   r   r>   r!   #Parent category (typically 'dream')r   z0Agent type (typically 'deduction' for deletions)r   r   r   r   r   r   r   zNumber of conclusions deletedr?   zOLevel of each deleted conclusion (e.g., ['explicit', 'deductive', 'deductive'])r"   r@   r)   c                 &    | j          d| j         dS )r+   r,   z:conclusions_deletedr-   r.   s    r0   r1   z0AgentToolConclusionsDeletedEvent.get_resource_id   rB   r2   NrC   r;   r2   r0   rE   rE   v   s         
 "BK#AAA%&OXc]&&&&Ix}&&& %)EFFFFCFFFU3,QRRRIsRRR !52WXXXOSXXXeK  J     %1ABBBNCBBB E#+?@@@Hc@@@E#+?@@@Hc@@@ "E#3RSSScSSSe  FDI   
E E E E E E Er2   rE   c                      e Zd ZU dZdZee         ed<   dZee	         ed<   dZ
ee         ed<    edd	
          Zeed<    edd
          Ze	ed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Ze	ed<   defdZdS )AgentToolPeerCardUpdatedEventzrEmitted when the update_peer_card tool is executed.

    Tracks peer card updates during dream consolidation.
    zagent.tool.peer_card.updatedr   r	   r
   r   r   .r   r   r   r>   r!   rG   r   z&Agent type: 'deduction' or 'induction'r   r   r   r   r   r   r   z Number of facts in the peer cardfacts_countr)   c                 &    | j          d| j         dS )r+   r,   z:peer_card_updatedr-   r.   s    r0   r1   z-AgentToolPeerCardUpdatedEvent.get_resource_id   s    +BBBBBBr2   N)r3   r4   r5   r6   r   r   r7   r8   r
   r9   r   r   r   r!   r   r   r   r   r   rK   r1   r;   r2   r0   rJ   rJ      s         
 "@K#???%&OXc]&&&&Ix}&&& %)EFFFFCFFFU3,QRRRIsRRR !52WXXXOSXXXeC-UVVVJVVV%1ABBBNCBBB E#+?@@@Hc@@@E#+?@@@Hc@@@ uS.PQQQKQQQC C C C C C Cr2   rJ   c                      e Zd ZU dZdZee         ed<   dZee	         ed<   dZ
ee         ed<    edd	
          Zeed<    edd
          Ze	ed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Zeed<    edd
          Ze	ed<    edd
          Ze	ed<    edd
          Zeed<    edd
          Ze	ed<    edd 
          Ze	ed!<    ed"d#$          Ze	ed%<    ed"d&$          Ze	ed'<    ed"d($          Ze	ed)<   d*efd+Zd,S )-AgentToolSummaryCreatedEventzEmitted when a summary is created.

    Tracks summary creation with full context about what was summarized
    and the resources consumed.
    zagent.tool.summary.createdr   r	   r
   r   r   .r   r   r   r>   r!   zParent categoryr   z
Agent typer   r   r   zSession namesession_namez#Message ID the summary covers up to
message_idz&Number of messages included in summarymessage_countz.Sequence number of the base message in sessionmessage_seq_in_sessionzSummary type: 'short' or 'long'summary_typeu   Provider-side input tokens for the summary LLM call (equivalent to HonchoLLMCallResponse.input_tokens). keeps this field unchanged — adding a duplicate `provider_input_tokens` would only churn analytics queries.r%   z#Output tokens (summary token count)r&   r   zrToken count of the previous summary text fed back in as context. 0 when this is the first summary for the session.r   previous_summary_tokenszkSum of `Message.token_count` across the messages being summarized (excludes scaffold and previous_summary).message_tokenszpEstimated tokens for the static scaffold portion of the prompt (from estimate_short/long_summary_prompt_tokens).prompt_scaffold_tokensr)   c                 &    | j          d| j         dS )r+   r,   z:summary_createdr-   r.   s    r0   r1   z,AgentToolSummaryCreatedEvent.get_resource_id  s    +@@@@@@r2   N)r3   r4   r5   r6   r   r   r7   r8   r
   r9   r   r   r   r!   r   r   r   rO   rP   rQ   rR   rS   r%   r&   rT   rU   rV   r1   r;   r2   r0   rN   rN      s          ">K#===%&OXc]&&&&Ix}&&& %)EFFFFCFFFU3,QRRRIsRRR !52CDDDOSDDDeC\:::J:::%1ABBBNCBBB c~>>>L#>>> eC-RSSSJSSSA  M3    #(%I# # #C   
 c/PQQQL#QQQ J  L#    s0UVVVM3VVV $)5@$ $ $S     %C  NC    #(%@# # #C   A A A A A A Ar2   rN   c                   X   e Zd ZU dZdZee         ed<   dZee	         ed<   dZ
ee         ed<   dZee         ed	<    ed
d          Zeed<    ed
d          Ze	ed<    ed
d          Ze	ed<    edd          Zedz  ed<    ed
d          Zeed<    ed
d          Zeed<    ed
d          Zeed<    ed
d          Zeed<    ed
d          Zeed<    ed d!          Zeed"<    ed
d#          Ze	ed$<    edd%          Ze	dz  ed&<    ed'd(          Ze	ed)<    ed d*          Zeed+<    edd,          Ze	dz  ed-<    edd.          Ze	dz  ed/<    edd0          Ze	dz  ed1<    edd2          Z edz  ed3<    ed'd4          Z!e	ed5<   d6efd7Z"dS )8AgentToolCallCompletedEventu  generic tool-call event: fires once per tool invocation.

    Complements the four state-changer events (conclusions_created/deleted,
    peer_card_updated, summary_created), which carry semantic information
    about specific tools, with a lightweight per-call telemetry record that
    covers every tool — including read-only tools (`search_memory`,
    `get_recent_history`, etc.) that have no dedicated event today.

    Resource id includes `tool_call_seq` so the model can legitimately call
    the same tool twice in one iteration (it does) without colliding event
    ids — without seq, both calls would deterministically hash to the same
    id and dedupe would drop one.
    zagent.tool.call.completedr      r
   r   r   r   r   .zRun id for correlationr   r   r    r!   zp0-indexed position within the iteration's tool batch. Disambiguates two calls to the same tool in one iteration.tool_call_seqNzvProvider-supplied tool call id (e.g. Anthropic's toolu_*) when available; lets analytics cross-reference provider logsr   provider_tool_call_idr   r   r   r   r   r   zTool name as invoked	tool_namez"Wall-clock duration of the handlerduration_msFzTrue if the handler raisedis_errorz/Length of the result string returned to the LLMresult_charszyOriginal result size when the handler truncated; None when no truncation occurred. Pair with was_truncated for the delta.result_chars_before_truncationr   z>tiktoken-based size proxy for the result string; estimate onlyresult_tokens_estimatez=True when the handler clamped the result to fit a size budgetwas_truncatedz*tiktoken estimate of the search query textquery_tokensz$Caller-supplied top_k for the searchtop_kz(Number of results returned by the searchresults_countzCTrue when the search ran a vector lookup (vs. metadata-only filter)used_embeddingzBNumber of embedding API calls the handler made for this invocationembedding_query_countr)   c                 4    | j          d| j         d| j         S )z}{run_id}:{iteration}:{tool_call_seq} so duplicate tool calls within
        one iteration produce distinct deterministic ids.r,   )r   r!   r[   r.   s    r0   r1   z+AgentToolCallCompletedEvent.get_resource_id\  s'     +EEEE1CEEEr2   )#r3   r4   r5   r6   r   r   r7   r8   r
   r9   r   r   r   r   r!   r[   r\   r   r   r   r]   r^   floatr_   boolr`   ra   rb   rc   rd   re   rf   rg   rh   r1   r;   r2   r0   rY   rY   	  s          "=K#<<<%&OXc]&&&&Ix}&&&#0M8C=000 %)ABBBFCBBBU3,JKKKIsKKK G  M3    ). M) ) )3:    !5B  OS    eO  J     %1ABBBNCBBB U3,BCCCIsCCCs0TUUUKUUUU56RSSSHdSSS J  L#    27 P2 2 2"C$J    #(%T# # #C     %S  M4     %u"N     L#*    "H  E3:    !&"L! ! !M3:    #(%Y# # #ND4K    "'X" " "3   
F F F F F F Fr2   rY   )r   rY   r=   rE   rJ   rN   N)r6   typingr   pydanticr   src.telemetry.events.baser   r   r=   rE   rJ   rN   rY   __all__r;   r2   r0   <module>rp      s                / / / / / /51 51 51 51 51) 51 51 51p$E $E $E $E $Ey $E $E $EN"E "E "E "E "Ey "E "E "EJC C C C CI C C C>LA LA LA LA LA9 LA LA LA^VF VF VF VF VF) VF VF VFr  r2   