
    yjC                       d Z ddlmZ ddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddlZddlmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) ddl*m+Z+  ej,        e-          Z.e G d d                      Z/	 d.ddddddd/d%Z0d0d)Z1ej2        d1d-            Z3dS )2an  
Dream orchestrator for the specialist-based architecture.

This module coordinates the full dream cycle:
0. [Optional] Surprisal sampling: Pre-filter observations by geometric surprisal
1. Run deduction specialist (self-directed exploration, creates deductive observations)
2. Run induction specialist (self-directed exploration, creates inductive observations)

Specialists are self-directed agents that explore the observation space and create
higher-level observations. When surprisal sampling finds interesting observations,
they're passed as hints, but specialists are free to follow the evidence wherever it leads.
    )annotationsN)	dataclass)datetimetimezone)Any)generate)funcselect)crudmodels)settings)
tracked_db)SPECIALISTSSpecialistResult)SurprisalScore)SurprisalError)	DreamType)DreamRunEventemit)accumulate_metriclog_performance_metrics)get_configuration)DreamPayloadc                  x    e Zd ZU dZded<   ded<   ded<   ded<   ded	<   d
ed<   d
ed<   ded<   d
ed<   d
ed<   dS )DreamResultz0Result of a dream cycle for telemetry reporting.strrun_id	list[str]specialists_runbooldeduction_successinduction_successsurprisal_enabledintsurprisal_conclusion_counttotal_iterationsfloattotal_duration_msinput_tokensoutput_tokensN)__name__
__module____qualname____doc____annotations__     @/DATA/AppData/hermes/projects/honcho/src/dreamer/orchestrator.pyr   r   ,   s         :: KKK  #### r1   r   )
dream_typetrigger_reasondelay_reason&documents_since_last_dream_at_scheduledocument_thresholdworkspace_namer   observerobservedsession_name
str | Noner3   r4   r5   r6   
int | Noner7   returnDreamResult | Nonec                 K   t           j        j        sdS t                      }	d|	 }
t	          j                    }t                              d|	 d|  d| d|            t          d          4 d{V }|t          j
        || |           d{V }nd}t          j        ||            d{V }t          d||          }ddd          d{V  n# 1 d{V swxY w Y   |j        j        s&t                              d|	 d	|  d| d
           dS d}d}d}d}d}d}t           j        j        j        rt                              d|	 d           	 ddlm}  || ||           d{V }t                              d|	 dt%          |           d           t%          |          }t'          |
dt%          |          d           t%          |          dk    rt)          |          }t                              d|	 dt%          |           ddz              t                              d|	 d|d         j        dd|d         j        dz              nt                              d|	 d           nX# t,          $ rK}t                              d|	 d| d           t'          |
dt1          |          d            Y d}~nd}~ww xY wd!}d}d}d}	 t                              d|	 d"           t2          d#         }	 |                    | ||||||	$           d{V }t                              d|	 d%|j        dd&          d'           t'          |
d(|j        d            |j        }nX# t:          $ rK}t                              d|	 d)| d           t'          |
d*t1          |          d            Y d}~nd}~ww xY wt                              d|	 d+           t2          d,         }	 |                    | ||||||	$           d{V }t                              d|	 d-|j        dd&          d'           t'          |
d.|j        d            |j        }nX# t:          $ rK}t                              d|	 d/| d           t'          |
d0t1          |          d            Y d}~nd}~ww xY wt	          j                    |z
  d1z  }t'          |
d2|d3           t                              d|	 d4|d5d3           t=          d6|	           |d!k    rt	          j                    |z
  d1z  }	 |r|j        nd|r|j        ndz   }|r|j         nd|r|j         ndz   }|r|j!        nd|r|j!        ndz   }tE          tG          dMi d7|	d8| d9|d:|d;|d<d#d,gd=|d>|d?t           j        j        j        d@|dA|dB|dC|dD|dE|dFt%          t           j        j$                  dG|dH|dI|dJ|           nH# t:          $ r  t          %                    dKd           Y nw xY w# |d!k    rt	          j                    |z
  d1z  }	 |r|j        nd|r|j        ndz   }|r|j         nd|r|j         ndz   }|r|j!        nd|r|j!        ndz   }tE          tG          dMi d7|	d8| d9|d:|d;|d<d#d,gd=|d>|d?t           j        j        j        d@|dA|dB|dC|dD|dE|dFt%          t           j        j$                  dG|dH|dI|dJ|           w # t:          $ r t          %                    dKd           Y w w xY wxY wtM          |	d#d,g||t           j        j        j        |||||L
  
        S )Nat  
    Run a full dream cycle with optional surprisal-based sampling.

    The dream cycle runs specialists sequentially:
    0. [Optional] Surprisal sampling: Pre-filter observations by geometric surprisal
    1. Deduction specialist: Creates deductive observations from explicit facts
    2. Induction specialist: Creates inductive observations from patterns

    Uses short-lived DB sessions to avoid holding connections during LLM calls.

    Args:
        workspace_name: Workspace identifier
        observer: Observer peer name
        observed: Observed peer name
        session_name: Session identifier if specified
    Ndream_orchestrator_[z] Starting dream cycle for /zdream.config)r8   r;   )r8   z] Dreams disabled for z, skipping dreamFr   z] Computing surprisal scores)"sample_observations_with_surprisal)r8   r9   r:   z] Surprisal: Found z high-surprisal observationssurprisal_observationscountu"   ] ✨ SURPRISAL HINTS: Suggesting  z4high-surprisal topics for specialists to investigatez/] Targeting observations with surprisal range: z.3fz to zB] No high-surprisal observations - specialists will explore freelyz] Surprisal sampling failed: Texc_infosurprisal_errorblobg        z] Running deduction specialist	deduction)r8   r9   r:   r;   hintsconfigurationparent_run_idz] Deduction completed:    ...deduction_resultz] Deduction specialist failed: deduction_errorz] Running induction specialist	inductionz] Induction completed: induction_resultz] Induction specialist failed: induction_errori  total_durationmsz] Dream cycle completed in .0fdream_orchestratorr   r8   r;   r9   r:   r   r!   r"   r#   r%   r&   total_input_tokenstotal_output_tokensr(   r3   enabled_types_countr4   r5   r6   r7   zFailed to emit DreamRunEvent)
r   r   r!   r"   r#   r%   r&   r(   r)   r*   r0   )'r   DREAMENABLEDgenerate_nanoidtimeperf_counterloggerinfor   r   get_sessionget_workspacer   dreamenabled	SURPRISALsrc.dreamer.surprisalrD   lenr   _create_queries_from_surprisal	surprisalr   errorr   r   runcontentsuccess	Exceptionr   
iterationsr)   r*   r   r   ENABLED_TYPESdebugr   )r8   r9   r:   r;   r3   r4   r5   r6   r7   r   	task_name
start_timedbsession	workspacerO   r!   r"   surprisal_observation_countrS   rV   exploration_hintsrD   high_surprisal_obseduration_msr&   r\   r]   deduction_specialistinduction_specialists                                  r2   	run_dreamr   C   s     8 >! tF.f..I"$$J
KKUFUU~UUUU8UU  
 .)) 	D 	D 	D 	D 	D 	D 	DR# ,>        GG G,ROOOOOOOOO	)$C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 & ]]]n]]|]]]	
 	
 	
 t "#0404 +/~' %L<<<<===#	LPPPPPP'I'I-!!( ( ( " " " " " " KKdFdds3E/F/Fddd   +..@*A*A'3S9K5L5Lg   %&&**$BCU$V$V![[[#FWBXBX[[[LM   OOOO+B/9hhhCUVWCXCbhhhi   
 bbbb    	L 	L 	LLLEVEE!EEPTLUUUi):CFFFKKKKKKKK	L" KhH>>>>???*;7	L%9%=%=-!!)'+$ &> & &             KKVFVV3C3KDSD3QVVV   -/?/G   !1 8 	L 	L 	L
 LLGVGGAGGRVLWWWi):CFFFKKKKKKKK	L 	>>>>???*;7	L%9%=%=-!!)'+$ &> & &             KKVFVV3C3KDSD3QVVV   -/?/G   !1 8 	L 	L 	LLLGVGGAGGRVLWWWi):CFFFKKKKKKKK	L
 (**Z74?)%5{DIIINNN;NNNNOOO 4f=== #,..;tCK%	H/?F ++Q0@G!,,a I 2BH --q2BI!.."K 3CI ..3CJ!//#L    !6#1> ". &X	
 &X &1+$>$> '8&7 '8&7 '/n&>&F&F 0K/J &6%5 (:'9 )<(; '2k"  *z#$ ),HN,H(I(I(I%& $2>'( ".)* <b;a+, (:'9-   4  	H 	H 	HLL7$LGGGGG	HM #,..;tCK%	H/?F ++Q0@G!,,a I 2BH --q2BI!.."K 3CI ..3CJ!//#L    !6#1> ". &X	
 &X &1+$>$> '8&7 '8&7 '/n&>&F&F 0K/J &6%5 (:'9 )<(; '2k"  *z#$ ),HN,H(I(I(I%& $2>'( ".)* <b;a+, (:'9-   4  	H 	H 	HLL7$LGGGGG	H $k2++".2:#>)%')   s   7AC
C#&C#D&J 
KAKK&+Y A.N  Y 
OAOY O.Y A.Q4 3Y 4
S	>AS?Y S		A Y 	C
X &X?>X? ]#C
\.-].&]]]]r~   list[SurprisalScore]r   c                    g }| D ]C}|j         j        }t          |          dk    r|dd         dz   }|                    |           D|dd         S )a&  
    Create search queries from high-surprisal observations.

    Strategy: Use observation content as semantic search queries.
    Truncate if too long (>200 chars).

    Args:
        high_surprisal_obs: List of SurprisalScore objects

    Returns:
        List of query strings (max 10)
    rQ   NrR   
   )observationrq   rl   append)r~   queriesscorerq   s       r2   rm   rm   6  si     G#    #+w<<#dsdme+Gw3B3<r1   payloadr   Nonec                <  K   t                               d| j         d| d| j         d| j         d	           	 | j        t
          j        k    rEt          || j        | j        | j        | j        j	        | j
        | j        | j        | j        	  	         d{V }|t                               d|j         dd	|j         dz   d
|j        ddz              t#          j        t&          j                                                  }t-          d          4 d{V }t/          j        ||| j        | j        d           d{V }t3          t5          j        t8          j        j                                                t8          j        j         |k    t8          j        j        | j        k    t8          j        j        | j        k    t8          j        j!        dk              }tE          |#                    |           d{V pd          }tI          |j%        &                    di                     }||d<   ||d<   t/          j'        ||| j        | j        d|i           d{V  ddd          d{V  n# 1 d{V swxY w Y   dS dS dS dS # tP          $ rz}	t           )                    d| j         d| j         d| j         dtU          |	           d           tV          j,        j-        rt]          j/        |	           Y d}	~	dS Y d}	~	dS d}	~	ww xY w)z
    Process a dream task by performing collection maintenance operations.

    Args:
        payload: The dream task payload containing workspace, peer, and dream type information
    u9   
(っ- ‸ - ς)ᶻ z 𐰁 ᶻ z 𐰁 ᶻ z 𐰁

DREAM: z documents for rC   u1   

𐰁 z ᶻ 𐰁 z ᶻ 𐰁 z ᶻ(っ- ‸ - ς))	r8   r9   r:   r;   r3   r4   r5   r6   r7   NzDream completed: run_id=z, ziterations=z	duration=rZ   rY   zdream.guard_pair_writeT)r9   r:   with_for_updateexplicitr   rh   last_dream_atlast_dream_document_count)update_datazError processing dream task z for z: rI   )0rd   re   r3   r9   r:   r   OMNIr   r;   valuer4   r5   r6   r7   r   r&   r(   r   nowr   utc	isoformatr   r   get_collectionr
   r	   rF   r   Documentidwherer8   levelr$   scalardictinternal_metadataget#update_collection_internal_metadatars   ro   r   r   SENTRYr`   
sentry_sdkcapture_exception)
r   r8   resultnow_isory   
collection
count_stmtcurrent_explicit_count
dream_metar   s
             r2   process_dreamr   N  sr      KK	3	3 	3+9	3 	3<C<L	3 	3OVO_	3 	3 	3  :, (#1$-$-!(!5&17#*#9!(!5;B;i'.'A            %KKD6=DDDC(?CCCDFf&>FFFFG   'l8<88BBDDG)*BCC       r+/+>*%,%5%,%5,0, , , & & & & & &
 &,DJv7I,J,J%K%K%Q%Q"O:nL"O48HH"O48HH"O1Z?	& &
 25299Z;P;P5P5P5P5P5P5P5UTU1V1V.%)**F*J*J7TV*W*W%X%X
6=
?3BX
#>?"F*#,#,)0*(=         %                             &% 1  d  , , ,s7+=ssGDTssW^Wgssknopkqkqss 	 	
 	
 	
 ?" 	,(+++++++++	, 	, 	, 	, 	, 	,,s>   C%J EI>,J >
JJ JJ 
L!A)LL)N)r8   r   r9   r   r:   r   r;   r<   r3   r<   r4   r<   r5   r<   r6   r=   r7   r=   r>   r?   )r~   r   r>   r   )r   r   r8   r   r>   r   )4r.   
__future__r   loggingrb   dataclassesr   r   r   typingr   r   nanoidr   ra   
sqlalchemyr	   r
   srcr   r   
src.configr   src.dependenciesr   src.dreamer.specialistsr   r   rk   r   src.exceptionsr   src.schemasr   src.telemetry.eventsr   r   src.telemetry.loggingr   r   src.utils.config_helpersr   src.utils.queue_payloadr   	getLoggerr+   rd   r   r   rm   tracer   r0   r1   r2   <module>r      sf    # " " " " "   ! ! ! ! ! ! ' ' ' ' ' ' ' '           . . . . . . # # # # # # # #               ' ' ' ' ' ' A A A A A A A A 0 0 0 0 0 0 ) ) ) ) ) ) ! ! ! ! ! ! 4 4 4 4 4 4 4 4        7 6 6 6 6 6 0 0 0 0 0 0		8	$	$        4  $	p "!%#9=%)p p p p p pf   0 K, K, K, K, K, K,r1   