
    i                       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	m
Z
mZmZ ddlmZ ddlmZ ddlmZ  ej        e          Zdd	d
ddddddidddg ddZddd
dddddddddddgddZddd
dddddg d d!dddd"dgddZd#d$d
dd%ddddd&g ddZd'd(d
dd)ddd*ddddd+g ddZeeeeegZ G d, d-e          Zd1d0ZdS )2uL  Honcho memory plugin — MemoryProvider for Honcho AI-native memory.

Provides cross-session user modeling with dialectic Q&A, semantic search,
peer cards, and persistent conclusions via the Honcho SDK. Honcho provides AI-native cross-session user
modeling with dialectic Q&A, semantic search, peer cards, and conclusions.

The 4 tools (profile, search, context, conclude) are exposed through
the MemoryProvider interface.

Config: Uses the existing Honcho config chain:
  1. $HERMES_HOME/honcho.json (profile-scoped)
  2. ~/.honcho/config.json (legacy global)
  3. Environment variables
    )annotationsN)AnyDictListOptional)sanitize_context)MemoryProvider)
tool_errorhoncho_profileu  Retrieve or update a peer card from Honcho — a curated list of key facts about that peer (name, role, preferences, communication style, patterns). Pass `card` to update; omit `card` to read.  If the card is empty, the result includes a `hint` field explaining why (observation disabled, fresh peer, dialectic layer still warming up, etc.) — this is NOT an error.  Peer cards accumulate over time from observed conversation.objectstringzaPeer to query. Built-in aliases: 'user' (default), 'ai'. Or pass any peer ID from this workspace.)typedescriptionarrayr   zGNew peer card as a list of fact strings. Omit to read the current card.)r   itemsr   )peercard)r   
propertiesrequired)namer   
parametershoncho_searchu   Semantic search over Honcho's stored context about a peer. Returns raw excerpts ranked by relevance — no LLM synthesis. Cheaper and faster than honcho_reasoning. Good when you want to find specific past facts and reason over them yourself.z&What to search for in Honcho's memory.integerz:Token budget for returned context (default 800, max 2000).)query
max_tokensr   r   honcho_reasoningug  Ask Honcho a natural language question and get a synthesized answer. Uses Honcho's LLM (dialectic reasoning) — higher cost than honcho_profile or honcho_search. Can query about any peer via alias or explicit peer ID. Pass reasoning_level to control depth: minimal (fast/cheap), low (default), medium, high, max (deep/expensive). Omit for configured default.zA natural language question.a  Override the default reasoning depth. Omit to use the configured default (typically low). Guide:
- minimal: quick factual lookups (name, role, simple preference)
- low: straightforward questions with clear answers
- medium: multi-aspect questions requiring synthesis across observations
- high: complex behavioral patterns, contradictions, deep analysis
- max: thorough audit-level analysis, leave no stone unturnedminimallowmediumhighmax)r   r   enum)r   reasoning_levelr   honcho_contextu   Retrieve full session context from Honcho — summary, peer representation, peer card, and recent messages. No LLM synthesis. Cheaper than honcho_reasoning. Use this to see what Honcho knows about the current conversation and the specified peer.zOOptional focus query to filter context. Omit for full session context snapshot.)r   r   honcho_concludeuC  Write or delete a conclusion about a peer in Honcho's memory. Conclusions are persistent facts that build a peer's profile. You MUST pass exactly one of: `conclusion` (to create) or `delete_id` (to delete). Passing neither is an error. Deletion is only for PII removal — Honcho self-heals incorrect conclusions over time.zpA factual statement to persist. Provide this when creating a conclusion. Do not send it together with delete_id.zzConclusion ID to delete for PII removal. Provide this when deleting a conclusion. Do not send it together with conclusion.)
conclusion	delete_idr   c                     e Zd ZU dZd ZedSd            ZdTdZd Zd	 Z	dUdZ
dVdZdVdZdTdZdWdZdSdZdddXdZdYdZdddZdZddddddd Zd!ed"<   d#Zd$Zd%Zd&Zd'Zd(ZdTd)Zd[d+Zd\d,Zd]d-Zd^d_d/Zd`d3Z e!dad5            Z"dbd6Z# e$j%        d7e$j&                  Z'e(dcd8            Z)ddd;Z*e!ded>            Z+dfdAZ,dddgdDZ-	 dhdidJZ.djdMZ/dkdNZ0dldQZ1dmdRZ2dES )nHonchoMemoryProviderzHHoncho AI-native memory with dialectic Q&A and persistent user modeling.c                   d | _         d | _        d| _        d| _        t	          j                    | _        d | _        d | _        d| _	        d | _
        t	          j                    | _        d| _        d| _        d| _        d| _        d| _        d | _        d| _        d| _        d| _        d| _        d	| _        d| _        d| _        d
| _        d | _        d | _        d
| _        d S )N hybridr   
every-turn   Tr!   g        F)_manager_config_session_key_prefetch_result	threadingLock_prefetch_lock_prefetch_thread_sync_thread_recall_mode_base_context_cache_base_context_lock_turn_count_injection_frequency_context_cadence_dialectic_cadence_dialectic_depth_dialectic_depth_levels_reasoning_heuristic_reasoning_level_cap_last_context_turn_last_dialectic_turn_prefetch_thread_started_at_prefetch_result_fired_at_dialectic_empty_streak_session_initialized_lazy_init_kwargs_lazy_init_session_id_cron_skippedselfs    C/home/ubuntu/.hermes/hermes-agent/plugins/memory/honcho/__init__.py__init__zHonchoMemoryProvider.__init__   s     "'n..<@8< % 37 "+."2"2 $0! !"# !9=$*.!)/!"&$(! 36(.2&,-$ %*!1548" #    returnstrc                    dS )Nhoncho rN   s    rP   r   zHonchoMemoryProvider.name   s    xrR   boolc                    	 ddl m} |                                }|j        ot	          |j        p|j                  S # t          $ r Y dS w xY w)z0Check if Honcho is configured. No network calls.r   )HonchoClientConfigF)plugins.memory.honcho.clientrZ   from_global_configenabledrX   api_keybase_url	Exception)rO   rZ   cfgs      rP   is_availablez!HonchoMemoryProvider.is_available   sj    	GGGGGG$7799C;D4(Cs|#D#DD 	 	 	55	s   ;> 
AAc                >   ddl }ddlm}  ||          dz  }i }|                                r4	  |j        |                                          }n# t          $ r Y nw xY w|                    |           |                     |j	        |d                     dS )zDWrite config to $HERMES_HOME/honcho.json (Honcho SDK native format).r   N)Pathzhoncho.json   )indent)
jsonpathlibrd   existsloads	read_textr`   update
write_textdumps)rO   valueshermes_homerg   rd   config_pathexistings          rP   save_configz HonchoMemoryProvider.save_config   s          d;''-7 	%4:k&;&;&=&=>>   ztz(1===>>>>>s   "A 
A A c                    ddddddddd	gS )
Nr^   zHoncho API keyTHONCHO_API_KEYzhttps://app.honcho.dev)keyr   secretenv_varurlbaseUrlz!Honcho base URL (for self-hosted))rv   r   rW   rN   s    rP   get_config_schemaz&HonchoMemoryProvider.get_config_schema  s6    .>$[k  uM  N  N.QRR
 	
rR   rp   configdictNonec                T    ddl }ddlm}  ||                                           dS )z:Run the full Honcho setup wizard after provider selection.r   N)	cmd_setup)typesplugins.memory.honcho.clir   SimpleNamespace)rO   rp   r|   r   r   s        rP   
post_setupzHonchoMemoryProvider.post_setup  s?    777777	%''))*****rR   
session_idc                T   	 |                     dd          }|                     dd          }|dv s|dk    r%t                              d||           d| _        d	S d
dlm}m} d
dlm} |	                                }|j
        r|j        s#|j        st                              d           d	S || _        |j        | _        t                              d| j                   	 |j        pi }	|	                     dd          | _        t%          |	                     dd                    | _        t%          |	                     dd                    | _        t+          dt-          |j        d                    | _        |j        | _        |j        | _        |j        | j        v r|j        | _        n2# t@          $ r%}
t                              d|
           Y d	}
~
nd	}
~
ww xY w| j        dk    rI|j!        r | j"        ||fi | d	S || _#        || _$        || _        t                              d           d	S  | j"        ||fi | d	S # tJ          $ r t                              d           Y d	S t@          $ r-}
t          &                    d|
           d	| _'        Y d	}
~
d	S d	}
~
ww xY w)zInitialize Honcho session manager.

        Handles: cron guard, recall_mode, session name resolution,
        peer memory mode, SOUL.md ai_peer sync, memory file migration,
        and pre-warming context at init.
        agent_contextr,   platformcli)cronflushr   zBHoncho skipped: cron/flush context (agent_context=%s, platform=%s)TNr   )rZ   get_honcho_clientHonchoSessionManageru)   Honcho not configured — plugin inactivezHoncho recall_mode: %sinjectionFrequencyr.   contextCadencer/   dialecticCadence   z,Honcho cost-awareness config parse error: %stoolsuG   Honcho tools-only mode — deferring session init until first tool callu3   honcho-ai package not installed — plugin inactivezHoncho init failed: %s)(getloggerdebugrM   r[   rZ   r   plugins.memory.honcho.sessionr   r\   r]   r^   r_   r2   recall_moder:   rawr>   intr?   r@   r"   mindialectic_depthrA   dialectic_depth_levelsrB   reasoning_heuristicrC   reasoning_level_cap_LEVEL_ORDERrD   r`   init_on_session_start_do_session_initrK   rL   ImportErrorwarningr1   )rO   r   kwargsr   r   rZ   r   r   ra   r   es              rP   
initializezHonchoMemoryProvider.initialize  s   D	!"JJ;;Mzz*e44H 111X5G5Ga*H6 6 6%)"ZZZZZZZZJJJJJJ$7799C; s{ cl HIIIDL !$DLL143DEEEPgm,/GG4H,,W,W)(+CGG4Da,H,H(I(I%
 +.cgg6H!.L.L*M*M'(+As33F/J/J(K(K%/2/I,,/,C)*d.???030GD- P P PKQOOOOOOOOP  G++, )D)#zDDVDDDF)/&-7*"fggg "D!#z<<V<<<<< 	P 	P 	PLLNOOOOOO 	! 	! 	!NN3Q777 DMMMMMMM	!sb   AI
 AI
 03I
 $CF3 2I
 3
G"=GI
 G""$I
 /I
 9I
 
$J'1	J':"J""J'c                    ddl m} ddlm}  ||          } ||||j        |                    d          pd           _        |                    d          }|                    d          }|                    |||	          p|pd
 _        t          
                    d j                    j                             j                  }	d _        	 |	j        sl|j        dk    raddlm}
 t#           |
            dz            } j                             j        |           t          
                    d j                   n+|j        dk    r t          
                    d j                   n2# t&          $ r%}t          
                    d|           Y d}~nd}~ww xY w j        dv r	  j                             j                   n2# t&          $ r%}t          
                    d|           Y d}~nd}~ww xY wdd fd}t-          j                     _        t3          j        |dd           _         j                                         t          
                    d j                   dS dS )zBShared session initialization logic for both eager and lazy paths.r   )r   r   user_idN)rV   r|   context_tokensruntime_user_peer_namesession_titlegateway_session_key)r   r   r   hermes-defaultzHoncho session key resolved: %sTzper-session)get_hermes_homememoriesz:Honcho memory file migration attempted for new session: %sz_Honcho memory file migration skipped: per-session strategy creates a fresh session per run (%s)z(Honcho memory file migration skipped: %s)contextr-   z!Honcho context prewarm failed: %szcSummarize what you know about this user. Focus on preferences, current projects, and working style.rS   r~   c                    	                                } nC# t          $ r6}t                              d|           xj        dz  c_        Y d }~d S d }~ww xY w| rQ|                                 r=j        5  | _        d_        d d d            n# 1 swxY w Y   d_	        d_        d S xj        dz  c_        d S )Nz#Honcho dialectic prewarm failed: %sr/   r   
_run_dialectic_depthr`   r   r   rI   stripr7   r4   rH   rF   )rexc_prewarm_queryrO   s     rP   _prewarm_dialecticzAHonchoMemoryProvider._do_session_init.<locals>._prewarm_dialectic  s+   11.AAAA    LL!FLLL00A500FFFFF  6 6, ; ;01-9:6; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 12D-34D00000A50000&    
A+AA9BBBzhoncho-prewarm-dialectictargetdaemonr   z'Honcho pre-warm started for session: %srS   r~   )r[   r   r   r   r   r   r1   resolve_session_namer3   r   r   get_or_createrJ   messagessession_strategyhermes_constantsr   rT   migrate_memory_filesr`   r:   prefetch_contexttime	monotonicrG   r5   Threadr8   start)rO   ra   r   r   r   r   clientr   r   sessionr   mem_dirr   r   r   s   `             @rP   r   z%HonchoMemoryProvider._do_session_init`  s"   BBBBBBFFFFFF""3'',,-#)::i#8#8#@D	
 
 
 

?33$jj)>??$$+%$7 %    
     	 	68IJJJ ---d.?@@$(!	H# 	(<(M(M<<<<<<oo//*<==2243DgNNNY[_[lmmmm%66u%    	H 	H 	HLLCQGGGGGGGG	H  555E..t/@AAAA E E E@!DDDDDDDDEM 
6 6 6 6 6 6 6" 04~/?/?D,$-$4)$=W% % %D! !'')))LLBDDUVVVVVC 65s1   BE9 9
F(F##F(5G 
HG??Hc                *   | j         r	| j        rdS | j        rdS | j        r| j        sdS 	  | j        | j        | j        pdfi | j         d| _        d| _        | j         duS # t          $ r&}t          	                    d|           Y d}~dS d}~ww xY w)zLazily initialize the Honcho session (for tools-only mode).

        Returns True if the manager is ready, False otherwise.
        TFr   Nz#Honcho lazy session init failed: %s)
r1   rJ   rM   r2   rK   r   rL   r`   r   r   )rO   r   s     rP   _ensure_sessionz$HonchoMemoryProvider._ensure_session  s    
 = 	T6 	4 	5| 	4#9 	5	!D!*>.>  (   &*D")-D&=,, 	 	 	NN@!DDD55555	s   6A" "
B,BBctxc                   g }|                     dd          }|r|                    d|            |                     dd          }|r|                    d|            |                     dd          }|r|                    d|            |                     dd          }|r|                    d	|            |                     d
d          }|r|                    d|            |sdS d                    |          S )zEFormat the prefetch context dict into a readable system prompt block.summaryr,   z## Session Summary
representationz## User Representation
r   z## User Peer Card
ai_representationz## AI Self-Representation
ai_cardz## AI Identity Card


)r   appendjoin)rO   r   partsr   repr   ai_repr   s           rP   _format_first_turn_contextz/HonchoMemoryProvider._format_first_turn_context  s9    '')R(( 	;LL999:::gg&++ 	;LL9C99:::wwvr"" 	7LL5t55666,b11 	ALL?v??@@@'')R(( 	<LL:::;;; 	2{{5!!!rR   c                    | j         rdS | j        r| j        s| j        dk    r
| j        r	 dS dS | j        dk    rd}n| j        dk    rd}nd}|S )u  Return system prompt text, adapted by recall_mode.

        Returns only the mode header and tool instructions — static text
        that doesn't change between turns (prompt-cache friendly).
        Live context (representation, card) is injected via prefetch().
        r,   r   z# Honcho Memory
Active (tools-only mode). Use honcho_profile, honcho_search, honcho_reasoning, honcho_context, and honcho_conclude tools to access user memory.r   u   # Honcho Memory
Active (context-injection mode). Relevant user context is automatically injected before each turn. No memory tools are available — context is managed automatically.u  # Honcho Memory
Active (tools-only mode). Use honcho_profile for a quick factual snapshot, honcho_search for raw excerpts, honcho_context for raw peer context, honcho_reasoning for synthesized answers (pass reasoning_level minimal/low/medium/high/max — you pick the depth per call), honcho_conclude to save facts about the user. No automatic context injection — you must use tools to access memory.u  # Honcho Memory
Active (hybrid mode). Relevant context is auto-injected AND memory tools are available. Use honcho_profile for a quick factual snapshot, honcho_search for raw excerpts, honcho_context for raw peer context, honcho_reasoning for synthesized answers (pass reasoning_level minimal/low/medium/high/max — you pick the depth per call), honcho_conclude to save facts about the user.)rM   r1   r3   r:   r2   )rO   headers     rP   system_prompt_blockz(HonchoMemoryProvider.system_prompt_block  s      	2} 	D$5 	 G+++i 
 2 	))) F '))Z F@  rR   r,   )r   r   c               r     j         rdS  j        dk    rdS  j        dk    r j        dk    rdS                                rdS g } j        5   j        	  j                             j	                  }|r 
                    |          nd _         j         _        n9# t          $ r,}t                              d|           d _        Y d}~nd}~ww xY w j        }ddd           n# 1 swxY w Y    j        r` j                             j	                  }|r? 
                    |          }|r( j        5  | _        ddd           n# 1 swxY w Y   |}|r|                    |            j        5  t%           j                  }	ddd           n# 1 swxY w Y   |	r j        dk    r j         _         j        dk    rΉr̉ j        r j        j        r j        j        nd}
 j        d fd}t/          j                     _        t5          j        |dd           _         j                                          j                            |
            j                                        rt                              d|
            j        r4 j                                        r j                            d            j        5   j        } j         }d _        d _         ddd           n# 1 swxY w Y    j!         j"        z  }|r8|dk    r2 j        |z
  |k    r$t                              d| j        |           d}|r)|#                                r|                    |           |sdS d                    |          } $                    |          }|S )u  Return base context (representation + card) plus dialectic supplement.

        Assembles two layers:
        1. Base context from peer.context() — cached, refreshed on context_cadence
        2. Dialectic supplement — cached, refreshed on dialectic_cadence

        B1: Returns empty when recall_mode is "tools" (no injection).
        B5: Respects injection_frequency — "first-turn" returns cached/empty after turn 0.
        Port #3265: Truncates to context_tokens budget.
        r,   r   z
first-turnr/   Nz$Honcho base context fetch failed: %sr0          @rS   r~   c                    	                                } nC# t          $ r6}t                              d|           xj        dz  c_        Y d }~d S d }~ww xY w| rQ|                                 r=j        5  | _        _        d d d            n# 1 swxY w Y   _	        d_        d S xj        dz  c_        d S )Nz&Honcho first-turn dialectic failed: %sr/   r   r   )r   r   	_fired_atr   rO   s     rP   _run_first_turnz6HonchoMemoryProvider.prefetch.<locals>._run_first_turnp  s<   11%88AA    LL!I3OOO00A500FFFFF  	6 	6, C C01-9B6C C C C C C C C C C C C C C C
 1:D-34D00000A50000r   Tzhoncho-prefetch-firstr   timeoutuS   Honcho first-turn dialectic still running after %.1fs — will surface on next turng      @r   zKHoncho pending dialectic discarded as stale: fired_at=%d, turn=%d, limit=%dr   r   )%rM   r:   r>   r=   _is_trivial_promptr<   r;   r1   get_prefetch_contextr3   r   rE   r`   r   r   pop_context_resultr   r7   rX   r4   rF   r2   r   r   r   rG   r5   r   r8   r   r   is_aliverH   r@   _STALE_RESULT_MULTIPLIERr   _truncate_to_budget)rO   r   r   r   r   r   base_context	fresh_ctx	formatted_prewarm_landed_first_turn_timeoutr   dialectic_resultfired_atstale_limitresultr   s   ``              @rP   prefetchzHonchoMemoryProvider.prefetch#  s_     	2 ''2 $449IA9M9M2 ""5)) 	2
 $ 
	4 
	4'/2-<<T=NOOCWZ/bt/N/Ns/S/S/S`bD,.2.>D++  2 2 2LL!GKKK/1D,,,,,,2  3L
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 
	4 = 	-889JKKI - ;;IFF	 -0 = =3<0= = = = = = = = = = = = = = =#,L 	'LL&&&   	: 	:"4#899O	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	9t8D@@(,(8D%$,,,(,V9MV$$SV   (I6 6 6 6 6 6 6 6$ 04~/?/?D,$-$4&t:Q% % %D! !'')))!&&/B&CCC$--// 0'     	4T%:%C%C%E%E 	4!&&s&333  	2 	2#45H$&D!-1D*		2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 -0MM 	"A43Ch3NR]2]2]LL$%-t/?    " 	+ 0 6 6 8 8 	+LL)*** 	2U## ))&11ss   C1A	B%$C1%
C/"CC1C
C11C58C5EEEF""F&)F&!L

LLtextc                    | j         r| j         j        s|S | j         j        dz  }t          |          |k    r|S |d|         }|                    d          }||dz  k    r
|d|         }|dz   S )z9Truncate text to fit within context_tokens budget if set.   N g?u    …)r2   r   lenrfind)rO   r   budget_chars	truncated
last_spaces        rP   r   z(HonchoMemoryProvider._truncate_to_budget  s    | 	4<#> 	K|2Q6t99$$K,'	__S))
s***!+:+.I6!!rR   c               b     j         rdS  j        r	 j        rsdS  j        dk    rdS                                rdS  j        dk    s j         j        z
   j        k    r` j         _        	  j                             j                   n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w                                 rt                              d           dS                                  } j         j        z
  |k     r7t                              d| j         j         j         j        z
             dS  j         fd}t#          j                     _        t)          j        |dd	
           _         j                                         dS )a  Fire background prefetch threads for the upcoming turn.

        B5: Checks cadence independently for dialectic and context refresh.
        Context refresh updates the base layer (representation + card).
        Dialectic fires the LLM reasoning supplement.
        Nr   r/   z"Honcho context prefetch failed: %sz=Honcho dialectic prefetch skipped: prior thread still runningzhHoncho dialectic prefetch skipped: effective cadence %d (base %d, empty streak %d), turns since last: %dc                    	                                } nC# t          $ r6}t                              d|           xj        dz  c_        Y d }~d S d }~ww xY w| rQ|                                 r=j        5  | _        _        d d d            n# 1 swxY w Y   _	        d_        d S xj        dz  c_        d S )NzHoncho prefetch failed: %sr/   r   r   )r   r   r   r   rO   s     rP   _runz1HonchoMemoryProvider.queue_prefetch.<locals>._run  s+   22599   91===,,1,,  2&,,.. 2( ? ?,2D)5>D2? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -6)/0,,,,,1,,,,r   Tzhoncho-prefetchr   )rM   r1   r3   r:   r   r?   r=   rE   r   r`   r   r   _thread_is_live_effective_cadencerF   r@   rI   r   r   rG   r5   r   r8   r   )rO   r   r   r   	effectiver  r   s   ``    @rP   queue_prefetchz#HonchoMemoryProvider.queue_prefetch  s     	F} 	D$5 	U 	F ''F ""5)) 	F  A%%$*:T=T*TY]Yn)n)n&*&6D#F..t/@%HHHH F F FA1EEEEEEEEF !! 	LLXYYYF ++--	t88IEELLC42D4P 4#<<	   F $		2 	2 	2 	2 	2 	2 	2  ,0>+;+;( ) 0+<!
 !
 !
 	##%%%%%s   3 B 
CB>>Cbaser   r   ))r/   r   )re   r   )re   r/   )r   r   )r   r/   )r   re   zdict[tuple[int, int], str]_PROPORTIONAL_LEVELSr   x   i  g       @re      c                *   | j         r| j                                         sdS | j        r| j        j        r| j        j        nd}t	          j                    | j        z
  }||| j        z  k    r&t          	                    d||| j        z             dS dS )zThread-alive guard that treats threads older than the stale
        threshold as dead, so a hung Honcho request can't block new fires.Fr   uS   Honcho prefetch thread age %.1fs exceeds stale threshold %.1fs — treating as deadT)
r8   r   r2   r   r   r   rG   _STALE_THREAD_MULTIPLIERr   r   )rO   r   ages      rP   r  z$HonchoMemoryProvider._thread_is_live*  s     $ 	D,A,J,J,L,L 	5+/<YDL<PY4<''VYn!AA48888LL-.17T=Z3Z   5trR   r   c                    | j         dk    r| j        S | j        | j         z   }| j        | j        z  }t          ||          S )uB   Cadence plus empty-streak backoff, capped at _BACKOFF_MAX × base.r   )rI   r@   _BACKOFF_MAXr   )rO   widenedceilings      rP   r  z'HonchoMemoryProvider._effective_cadence9  sI    '1,,**)D,HH)D,==7G$$$rR   c                    d}| j         r4| j                                         rt          j                    | j        z
  }| j        | j        | j        | j        | 	                                |du|dS )zIn-process snapshot of dialectic liveness state for diagnostics.

        Returns current turn, last successful dialectic turn, pending-result
        fire turn, empty streak, effective cadence, and thread status.
        N)
turn_countlast_dialectic_turnpending_result_fired_atempty_streakeffective_cadencethread_alivethread_age_seconds)
r8   r   r   r   rG   r=   rF   rH   rI   r  )rO   
thread_ages     rP   liveness_snapshotz&HonchoMemoryProvider.liveness_snapshotA  s     
  	MT%:%C%C%E%E 	M))D,LLJ*#'#<'+'E 8!%!8!8!:!:&d2",
 
 	
rR   c                6   | j         r|s|S || j        vr|S t          |          }|| j        k     rd}n|| j        k     rd}nd}| j                            |          }| j                            | j                  }| j        t          ||z   |                   S )zScale `base` up by query length, clamped at reasoning_level_cap.

        Char-count heuristic: +1 at >=120 chars, +2 at >=400.
        r   r/   re   )rC   r   r   _HEURISTIC_LENGTH_MEDIUM_HEURISTIC_LENGTH_HIGHindexrD   r   )rO   r  r   nbumpbase_idxcap_idxs          rP   _apply_reasoning_heuristicz/HonchoMemoryProvider._apply_reasoning_heuristicT  s    
 ( 	 	Kt(((KJJt,,,DD,,,DDD$**400#))$*CDD X_g!>!>??rR   pass_idxc                   | j         r%|t          | j                   k     r| j         |         S | j        r| j        j        nd}| j                            | j        |f          }||dk    r|                     ||          S |S )ut  Resolve reasoning level for a given pass index.

        Precedence:
          1. dialecticDepthLevels (explicit per-pass) — wins absolutely
          2. _PROPORTIONAL_LEVELS table (depth>1 lighter-early passes)
          3. Base level = dialecticReasoningLevel, optionally scaled by the
             reasoning heuristic when the mapping falls through to 'base'
        r   Nr  )rB   r   r2   dialectic_reasoning_levelr  r   rA   r#  )rO   r$  r   r  mappings        rP   _resolve_pass_levelz(HonchoMemoryProvider._resolve_pass_levelh  s     ' 	:Hs4;W7X7X,X,X/99:>,Q66E+//1F0QRR?g//224???rR   prior_results	list[str]is_coldc                    |dk    r|r	 dS 	 dS |dk    r|r|d         nd}d| dS d	t          |          dk    r|d         nd
 dt          |          dk    r|d         nd
 dS )a  Build the prompt for a given dialectic pass.

        Pass 0: cold start (general user query) or warm (session-scoped).
        Pass 1: self-audit / targeted synthesis against gaps from pass 0.
        Pass 2: reconciliation / contradiction check across prior passes.
        r   zWho is this person? What are their preferences, goals, and working style? Focus on facts that would help an AI assistant be immediately useful.zGiven what's been discussed in this session so far, what context about this user is most relevant to the current conversation? Prioritize active context over biographical facts.r/   r,   z Given this initial assessment:

z

What gaps remain in your understanding that would help going forward? Synthesize what you actually know about the user's current state and immediate needs, grounded in evidence from recent sessions.z Prior passes produced:

Pass 1:
z(empty)z


Pass 2:
z

Do these assessments cohere? Reconcile any contradictions and produce a final, concise synthesis of what matters most for the current conversation.)r   )rO   r$  r)  r+  priors        rP   _build_dialectic_promptz,HonchoMemoryProvider._build_dialectic_promptz  s     q== 7 S 
 ]])6>M"%%BE4U 4 4 4003M0B0BQ0F0FM!,,I0 003M0B0BQ0F0FM!,,I0 0 0rR   r   c                :   | r%t          |                                           dk     rdS d| v rJd| v sDd| v s@t          j        d| t          j                  s t          j        d| t          j                  rdS t          |                                           d	k    S )
zCheck if a dialectic pass returned enough signal to skip further passes.

        Heuristic: a response longer than 100 chars with some structure
        (section headers, bullets, or an ordered list) is considered sufficient.
        d   F
z##u   •z^[*-] z
^\s*\d+\. Ti,  )r   r   research	MULTILINE)r   s    rP   _signal_sufficientz'HonchoMemoryProvider._signal_sufficient  s      	V\\^^,,s2256>>FNNyFBL99 y==  46<<>>""S((rR   c                   | j         r| j        sdS | j         }g }t          | j                  D ]}|dk    r|                     d||          }nW|r>|                     |d                   r#t                              d| j        |            n|                     |||          }| 	                    ||          }t                              d| j        |||           | j         
                    | j        ||d          }|                    |pd           t          |          D ]}|r|                                r|c S dS )	u[  Execute up to dialecticDepth .chat() calls with conditional bail-out.

        Cold start (no base context): general user-oriented query.
        Warm session (base context exists): session-scoped query.
        Each pass is conditional — bails early if prior pass returned strong signal.
        Returns the best (usually last) result.
        r,   r   r-  zCHoncho dialectic depth %d: pass %d skipped, prior signal sufficient)r   z5Honcho dialectic depth %d: pass %d, level=%s, cold=%suserr$   r   )r1   r3   r;   rangerA   r/  r6  r   r   r(  dialectic_queryr   reversedr   )	rO   r   r+  resultsipromptlevelr   r   s	            rP   r   z)HonchoMemoryProvider._run_dialectic_depth  s    } 	D$5 	2..t,-- 	) 	)AAvv55a'JJ  t66wr{CC LL!f!%!6; ; ;E55a'JJ,,Qe,<<ELLP.5'C C C ]22!6 % 3  F
 NN6<R(((( '"" 	 	A QWWYY rrR   z^(yes|no|ok|okay|sure|thanks|thank you|y|n|yep|nope|yeah|nah|continue|go ahead|do it|proceed|got it|cool|nice|great|done|next|lgtm|k)$c                    |sdS |                                 }|sdS |                    d          rdS | j                            |          rdS dS )zFReturn True if the prompt is too trivial to warrant context injection.T/F)r   
startswith_TRIVIAL_PROMPT_REmatch)clsr   strippeds      rP   r   z'HonchoMemoryProvider._is_trivial_prompt  si      	4::<< 	4s## 	4!''11 	4urR   turn_numbermessagec                    || _         dS )z;Track turn count for cadence and injection_frequency logic.N)r=   )rO   rH  rI  r   s       rP   on_turn_startz"HonchoMemoryProvider.on_turn_start  s    &rR   contentlimitc                l   t          |           |k    r| gS d}t          |          }g }| }d}|r|r|n||z
  }t          |          |k    r|                    |r|n||z              n|d|         }|                    d          }	|	|dz  k     r |                    d          }	|	dk    r|	dz  }	|	|dz  k     r|                    d	          }	|	|dz  k     r|}	|d|	                                         }
||	d                                         }|s||
z   }
|                    |
           d
}||S )aR  Split content into chunks that fit within the Honcho message limit.

        Splits at paragraph boundaries when possible, falling back to
        sentence boundaries, then word boundaries. Each continuation
        chunk is prefixed with "[continued] " so Honcho's representation
        engine can reconstruct the full message.
        z[continued] TNr   g333333?z. r   re   r   F)r   r   r   rstriplstrip)rL  rM  prefix
prefix_lenchunks	remainingfirstr  segmentcutchunks              rP   _chunk_messagez#HonchoMemoryProvider._chunk_message  s}    w<<5  9[[
	 	!&>EJ,>I9~~**5Hiify6HIII

+G --''CY_$$mmD))!881HCY_$$mmC((Y_$$dsdO**,,E!#$$..00I 'MM%   E1  	4 rR   r   Dict[str, Any]c                ^   | j         }g }||dk    r=t          t          |dd                    }t          t          |dd                    }n<t          t          |dd                    }t          t          |dd                    }|s|s|                    d| d	           t          | d
d          }t          | dd          }|t	          d|          k     r|                    d| d| d           |s|                    d           ddd                    |          z   dz   dS )u  Build a diagnostic hint when honcho_profile returns an empty card.

        A literal "No profile facts available yet." tells the model nothing
        about WHY.  The model then often surfaces it to the user as a cryptic
        error.  This hint enumerates the likely causes so the model can
        explain the situation (or retry with a different peer).

        Ordered by likelihood for a typical deployment:
          1. Observation is disabled for this peer
          2. Card hasn't accumulated yet (fresh peer, not enough dialectic
             cycles — dialectic cadence runs every N turns)
          3. Self-hosted Honcho backend doesn't support peer cards
             (honcho-ai server < 3.x)
        Nr8  user_observe_meTuser_observe_othersai_observe_meai_observe_othersz"observation is disabled for peer 'z+' (user_observe_me/ai_observe_me in config)r@   r/   r=   r   re   zthis session has only zh turn(s); peer cards accumulate as the dialectic layer reasons over conversation history (cadence every z	 turn(s))u   peer card has no facts yet — Honcho's dialectic layer builds this over time from observed turns; self-hosted Honcho < 3.x does not support peer cards at allzNo profile facts available yet.zThis is not an error.  z; zf.  Try honcho_reasoning for a synthesized answer, or honcho_search to query raw conversation excerpts.)r   hint)r2   rX   getattrr   r"   r   )rO   r   ra   reasons
observe_meobserve_otherscadenceturns           rP   _empty_profile_hintz(HonchoMemoryProvider._empty_profile_hint'  s    l?v~~!'#/@$"G"GHH
!%gc3H$&O&O!P!P!'#"E"EFF
!%gc3F&M&M!N!N . A A A A  
 $ 4a88t]A..#a//!!NN5 5 5")5 5 5    	NN5   8)))G$$%DD
 
 	
rR   user_contentassistant_contentc                    j         rdS  j        r j        sdS  j        r j        j        ndt          |pd                                          t          |pd                                           fd} j        r4 j                                        r j        	                    d           t          j        |dd	           _         j                                         dS )
zRecord the conversation turn in Honcho (non-blocking).

        Messages exceeding the Honcho API limit (default 25k chars) are
        split into multiple messages with continuation markers.
        Nia  r,   c                    	 j                             j                  }                               D ]}|                     d|                                         D ]}|                     d|           j                             |            d S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)Nr8  	assistantzHoncho sync_turn failed: %s)	r1   r   r3   rY  add_message_flush_sessionr`   r   r   )r   rX  r   clean_assistant_contentclean_user_content	msg_limitrO   s      rP   _syncz-HonchoMemoryProvider.sync_turn.<locals>._synco  s    ?-55d6GHH!001CYOO 7 7E''6666!001H)TT < <E''U;;;;,,W55555 ? ? ?:A>>>>>>>>>?s   BB 
C&CC      @r   Tzhoncho-syncr   )rM   r1   r3   r2   message_max_charsr   r   r9   r   r   r5   r   r   )rO   rh  ri  r   rr  ro  rp  rq  s   `    @@@rP   	sync_turnzHonchoMemoryProvider.sync_turn`  s)     	F} 	D$5 	F6:lMDL22	-l.@bAAGGII"23D3J"K"K"Q"Q"S"S		? 		? 		? 		? 		? 		? 		? 		?  	0!2!;!;!=!= 	0""3"///%,M
 
 
 	!!!!!rR   Nactionr   metadataOptional[Dict[str, Any]]c                     |dk    s|dk    ssdS  j         rdS  j        r j        sdS  fd}t          j        |dd          }|                                 dS )ai  Mirror built-in user profile writes as Honcho conclusions.

        ``metadata`` is accepted for compatibility with the write-origin
        work landed in main (commit 6a957a74); it's not yet threaded into
        the Honcho conclusion payload.  Left as a follow-up so this PR
        stays focused on the 7-PR consolidation and its review follow-ups.
        addr8  Nc                     	 j                             j                   d S # t          $ r&} t                              d|            Y d } ~ d S d } ~ ww xY w)NzHoncho memory mirror failed: %s)r1   create_conclusionr3   r`   r   r   )r   rL  rO   s    rP   _writez4HonchoMemoryProvider.on_memory_write.<locals>._write  st    C//0A7KKKKK C C C>BBBBBBBBBCs    % 
AAATzhoncho-memwriter   )rM   r1   r3   r5   r   r   )rO   rv  r   rL  rw  r}  ts   `  `   rP   on_memory_writez$HonchoMemoryProvider.on_memory_write  s     U??f..g.F 	F} 	D$5 	F	C 	C 	C 	C 	C 	C F4>OPPP						rR   r   List[Dict[str, Any]]c                :   | j         rdS | j        sdS | j        r4| j                                        r| j                            d           	 | j                                         dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)z4Flush all pending messages to Honcho on session end.Ng      $@r   z#Honcho session-end flush failed: %s)	rM   r1   r9   r   r   	flush_allr`   r   r   )rO   r   r   s      rP   on_session_endz#HonchoMemoryProvider.on_session_end  s     	F} 	F 	1!2!;!;!=!= 	1""4"000	CM##%%%%% 	C 	C 	CLL>BBBBBBBBB	Cs   A* *
B4BBc                V    | j         rg S | j        dk    rg S t          t                    S )zeReturn tool schemas, respecting recall_mode.

        B1: context-only mode hides all tools.
        r   )rM   r:   listALL_TOOL_SCHEMASrN   s    rP   get_tool_schemasz%HonchoMemoryProvider.get_tool_schemas  s6    
  	I	))I$%%%rR   	tool_nameargsc                   | j         rt          d          S | j        s#|                                 st          d          S | j        r| j        st          d          S 	 |dk    r|                    dd          }|                    d          }|r[| j                            | j        ||          }|t          d
          S t          j	        dt          |           d|d          S | j                            | j        |          }|s't          j	        |                     |                    S t          j	        d|i          S |dk    r|                    dd          }|st          d          S t          t          |                    dd                    d          }	|                    dd          }| j                            | j        ||	|          }|st          j	        ddi          S t          j	        d|i          S |dk    r|                    dd          }|st          d          S |                    dd          }|                    d          }
| j                            | j        ||
|          }| j        | _        t          j	        d|pdi          S |dk    rq|                    dd          }| j                            | j        |          }|st          j	        ddi          S g }|                    d          r|                    d|d                     |                    d           r|                    d!|d                      |                    d          r|                    d"|d                     |                    d#          rG|d#         }d$                    d% |d&d	         D                       }|                    d'|            t          j	        dd(                    |          pd)i          S |d*k    r<|                    d+          pd                                }|                    d,d                                          }|                    dd          }t/          |          }t/          |          }||k    rt          d-          S |rQ| j                            | j        ||          }|rt          j	        dd.| d/i          S t          d0| d1          S | j                            | j        ||          }|rt          j	        dd2| d3| i          S t          d4          S t          d5|           S # t4          $ r;}t6                              d6||           t          d7| d8|           cY d	}~S d	}~ww xY w)9zFHandle a Honcho tool call, with lazy session init for tools-only mode.z$Honcho is not active (cron context).z(Honcho session could not be initialized.z&Honcho is not active for this session.r   r   r8  r   )r   NzFailed to update peer card.zPeer card updated (z facts).)r   r   r   r   r   r,   z!Missing required parameter: queryr   i   i  )r   r   zNo relevant context found.r   r$   r9  zNo result from Honcho.r%   zNo context available yet.r   z## Summary
r   z## Representation
z## Card
recent_messagesr2  c              3  N   K   | ] }d |d          d|d         dd          V  !dS )z  [rolez] rL  N   rW   ).0ms     rP   	<genexpr>z8HonchoMemoryProvider.handle_tool_call.<locals>.<genexpr>  sX       ( ( @ai??1Y<+=??( ( ( ( ( (rR   z## Recent messages
r   zNo context available.r&   r(   r'   z8Exactly one of conclusion or delete_id must be provided.zConclusion z	 deleted.zFailed to delete conclusion .zConclusion saved for z: zFailed to save conclusion.zUnknown tool: zHoncho tool %s failed: %szHoncho z	 failed: )rM   r
   rJ   r   r1   r3   r   set_peer_cardrg   rn   r   get_peer_cardrg  r   r   search_contextr;  r=   rF   get_session_contextr   r   r   rX   delete_conclusionr|  r`   r   error)rO   r  r  r   r   card_updater   r   r   r   r$   r   r   msgsmsg_strr(   r'   has_delete_idhas_conclusionokr   s                        rP   handle_tool_callz%HonchoMemoryProvider.handle_tool_call  s\    	FDEEE ( 	N'')) N!"LMMM} 	HD$5 	HFGGGW	A,,,xx//"hhv.. o!]889JK^b8ccF~)*GHHH:1\s6{{1\1\1\fl&m&mnnn}2243D42PP F:d&>&>t&D&DEEEz8T"2333o--"-- K%&IJJJ TXXlC%@%@!A!A4HH
xx//55%u$ 6    P:x1M&NOOOz8V"4555000"-- K%&IJJJxx//"&((+<"="=66%u$3 7   -1,<)z8V-O7O"PQQQ...xx//m778IPT7UU O:x1L&MNNN779%% BLL!@I!@!@AAA77+,, PLL!Ns;K7L!N!NOOO776?? <LL!:S[!:!:;;;77,-- C01D"ii ( (!%bcc( ( (  G LL!A!A!ABBBz8V[[-?-?-ZCZ"[\\\///!XXk228b??AA	!XXlB77==??
xx// $Y!%j!1!1 N22%&`aaa  S889JI\`8aaB Z#z85W95W5W5W*XYYY%&QY&Q&Q&QRRR]44T5F
Y]4^^ `:x1]1]1]Q[1]1]&^___!">???:y::;;; 	A 	A 	ALL4iCCC?	??A??@@@@@@@@	As   A%U> 'U> -A	U> 7U> ,U> :BU> <U> ,U> ?A1U> 1AU> D!U> )B U> 
?U> 
U> ?U> U> ,U> >
W0V>8W>Wc                    | j         | j        fD ].}|r*|                                r|                    d           /| j        r-	 | j                                         d S # t          $ r Y d S w xY wd S )Nrs  r   )r8   r9   r   r   r1   r  r`   )rO   r~  s     rP   shutdownzHonchoMemoryProvider.shutdown  s    '):; 	$ 	$A $QZZ\\ $s###= 	'')))))   	 	s   A! !
A/.A/)rS   rT   )rS   rX   )rp   rT   r|   r}   rS   r~   )r   rT   rS   r~   )r   r}   rS   rT   )r   rT   r   rT   rS   rT   )r   rT   rS   rT   )r   rT   r   rT   rS   r~   )rS   r   )rS   r}   )r  rT   r   rT   rS   rT   )r,   )r$  r   r   rT   rS   rT   )r$  r   r)  r*  r+  rX   rS   rT   )r   rT   rS   rX   )r   rT   rS   rT   )r   rT   rS   rX   )rH  r   rI  rT   rS   r~   )rL  rT   rM  r   rS   r*  )r   rT   rS   rZ  )rh  rT   ri  rT   r   rT   rS   r~   )N)
rv  rT   r   rT   rL  rT   rw  rx  rS   r~   )r   r  rS   r~   )rS   r  )r  rT   r  r}   rS   rT   r   )3__name__
__module____qualname____doc__rQ   propertyr   rb   rs   r{   r   r   r   r   r   r   r   r   r  r  __annotations__r   r  r  r  r   r  r  r  r  r#  r(  r/  staticmethodr6  r   r3  compile
IGNORECASErD  classmethodr   rK  rY  rg  ru  r  r  r  r  r  rW   rR   rP   r*   r*      s        RR'# '# '#R    X   ? ? ?
 
 
+ + + +K! K! K! K!ZXW XW XW XWt   4" " " ":0 0 0 0d 9; K K K K K KZ" " " " ?A H& H& H& H& H& H&b 
8 
8 
 
 
 
 ?L  #   #  ! L   % % % %
 
 
 
&@ @ @ @(    $% % % %N ) ) ) \)&( ( ( (Z $	U
     [' ' ' ' * * * \*X7
 7
 7
 7
r Y[ " " " " " "L .2    <C C C C	& 	& 	& 	&dA dA dA dAL	 	 	 	 	 	rR   r*   rS   r~   c                H    |                      t                                 dS )z,Register Honcho as a memory provider plugin.N)register_memory_providerr*   )r   s    rP   registerr  .  s#      !5!7!788888rR   r   )r  
__future__r   rg   loggingr3  r5   r   typingr   r   r   r   agent.memory_managerr   agent.memory_providerr	   tools.registryr
   	getLoggerr  r   PROFILE_SCHEMASEARCH_SCHEMAREASONING_SCHEMACONTEXT_SCHEMACONCLUDE_SCHEMAr  r*   r  rW   rR   rP   <module>r     sI    # " " " " "   				      , , , , , , , , , , , , 1 1 1 1 1 1 0 0 0 0 0 0 % % % % % %		8	$	$ 	N  !  C 
   (+h 

 

   8 	X  !G 
 "[ 
 !  C 
 
 I!  : 	K  != 
 !T DCC    !  C '
 
0 I5 % % P 	;  !p 
 !  C 	
 	
   2 	b  !  R 
 !  \ 
 !  C 
 
 !  < #M3C^Ude h h h h h> h h h^#9 9 9 9 9 9rR   