
    i                         d Z ddlZddl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	 ddl
mZ ddlmZmZmZmZmZmZ  ej        e          Z ed          Z e            dz  ZdZd	Zd
Z G d d          ZdS )a}  
SQLite State Store for Hermes Agent.

Provides persistent session storage with FTS5 full-text search, replacing
the per-session JSONL file approach. Stores session metadata, full message
history, and model configuration for CLI and gateway sessions.

Key design decisions:
- WAL mode for concurrent readers + one writer (gateway multi-platform)
- FTS5 virtual table for fast text search across all session messages
- Compression-triggered session splitting via parent_session_id chains
- Batch runner and RL trajectories are NOT stored here (separate systems)
- Session source tagging ('cli', 'telegram', 'discord', etc.) for filtering
    N)Path)get_hermes_home)AnyCallableDictListOptionalTypeVarTzstate.db   aJ  
CREATE TABLE IF NOT EXISTS schema_version (
    version INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS sessions (
    id TEXT PRIMARY KEY,
    source TEXT NOT NULL,
    user_id TEXT,
    model TEXT,
    model_config TEXT,
    system_prompt TEXT,
    parent_session_id TEXT,
    started_at REAL NOT NULL,
    ended_at REAL,
    end_reason TEXT,
    message_count INTEGER DEFAULT 0,
    tool_call_count INTEGER DEFAULT 0,
    input_tokens INTEGER DEFAULT 0,
    output_tokens INTEGER DEFAULT 0,
    cache_read_tokens INTEGER DEFAULT 0,
    cache_write_tokens INTEGER DEFAULT 0,
    reasoning_tokens INTEGER DEFAULT 0,
    billing_provider TEXT,
    billing_base_url TEXT,
    billing_mode TEXT,
    estimated_cost_usd REAL,
    actual_cost_usd REAL,
    cost_status TEXT,
    cost_source TEXT,
    pricing_version TEXT,
    title TEXT,
    FOREIGN KEY (parent_session_id) REFERENCES sessions(id)
);

CREATE TABLE IF NOT EXISTS messages (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id TEXT NOT NULL REFERENCES sessions(id),
    role TEXT NOT NULL,
    content TEXT,
    tool_call_id TEXT,
    tool_calls TEXT,
    tool_name TEXT,
    timestamp REAL NOT NULL,
    token_count INTEGER,
    finish_reason TEXT,
    reasoning TEXT,
    reasoning_details TEXT,
    codex_reasoning_items TEXT
);

CREATE INDEX IF NOT EXISTS idx_sessions_source ON sessions(source);
CREATE INDEX IF NOT EXISTS idx_sessions_parent ON sessions(parent_session_id);
CREATE INDEX IF NOT EXISTS idx_sessions_started ON sessions(started_at DESC);
CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id, timestamp);
a  
CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5(
    content,
    content=messages,
    content_rowid=id
);

CREATE TRIGGER IF NOT EXISTS messages_fts_insert AFTER INSERT ON messages BEGIN
    INSERT INTO messages_fts(rowid, content) VALUES (new.id, new.content);
END;

CREATE TRIGGER IF NOT EXISTS messages_fts_delete AFTER DELETE ON messages BEGIN
    INSERT INTO messages_fts(messages_fts, rowid, content) VALUES('delete', old.id, old.content);
END;

CREATE TRIGGER IF NOT EXISTS messages_fts_update AFTER UPDATE ON messages BEGIN
    INSERT INTO messages_fts(messages_fts, rowid, content) VALUES('delete', old.id, old.content);
    INSERT INTO messages_fts(rowid, content) VALUES (new.id, new.content);
END;
c            #       Z   e Zd ZdZdZdZdZdZd\defdZ	d	e
ej        gef         d
efdZd]dZd Zd Z	 	 	 	 	 d^dedededeeef         dededed
efdZdeded
dfdZded
dfdZdeded
dfdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d_dededededed ed!ed"ee         d#ee         d$ee         d%ee         d&ee         d'ee         d(ee         d)ee         d*ed
df"d+Z	 	 d`dededed
dfd-Zded
eeeef                  fd.Zd/ed
ee         fd0Z d1Z!e"d2ee         d
ee         fd3            Z#ded2ed
efd4Z$ded
ee         fd5Z%d2ed
eeeef                  fd6Z&d2ed
ee         fd7Z'd8ed
efd9Z(	 	 	 	 	 daded;e)e         d<ed=ed>ed
e)eeef                  fd?Z*	 	 	 	 	 	 	 	 	 dbded@edAedBedCedDedEedFedGedHedIed
efdJZ+ded
e)eeef                  fdKZ,ded
e)eeef                  fdLZ-e"dMed
efdN            Z.	 	 	 	 	 dcdMedOe)e         d;e)e         dPe)e         d<ed=ed
e)eeef                  fdQZ/	 	 	 ddded<ed=ed
e)eeef                  fdRZ0d\ded
efdSZ1d\ded
efdTZ2ded
eeeef                  fdUZ3d\ded
e)eeef                  fdVZ4ded
dfdWZ5ded
efdXZ6dedZeded
efd[Z7dS )f	SessionDBz
    SQLite-backed session storage with FTS5 search.

    Thread-safe for the common gateway pattern (multiple reader threads,
    single writer via WAL mode). Each method opens its own cursor.
       g{Gz?g333333?2   Ndb_pathc                    |pt           | _        | j        j                            dd           t	          j                    | _        d| _        t          j	        t          | j                  ddd           | _        t          j        | j        _        | j                            d           | j                            d           |                                  d S )	NT)parentsexist_okr   Fg      ?)check_same_threadtimeoutisolation_levelzPRAGMA journal_mode=WALzPRAGMA foreign_keys=ON)DEFAULT_DB_PATHr   parentmkdir	threadingLock_lock_write_countsqlite3connectstr_connRowrow_factoryexecute_init_schema)selfr   s     4/home/agentuser/.hermes/hermes-agent/hermes_state.py__init__zSessionDB.__init__   s    1/!!$!>>>^%%
_#  !
 
 

 ")

4555
3444    fnreturnc                 *   d}t          | j                  D ]f}	 | j        5  | j                            d           	  || j                  }| j                                         n:# t          $ r- 	 | j                                         n# t          $ r Y nw xY w w xY w	 ddd           n# 1 swxY w Y   | xj	        dz  c_	        | j	        | j
        z  dk    r|                                  |c S # t          j        $ rx}t          |                                          }d|v sd|v rI|}|| j        dz
  k     r9t!          j        | j        | j                  }t)          j        |           Y d}~_ d}~ww xY w|pt          j        d          )u  Execute a write transaction with BEGIN IMMEDIATE and jitter retry.

        *fn* receives the connection and should perform INSERT/UPDATE/DELETE
        statements.  The caller must NOT call ``commit()`` — that's handled
        here after *fn* returns.

        BEGIN IMMEDIATE acquires the WAL write lock at transaction start
        (not at commit time), so lock contention surfaces immediately.
        On ``database is locked``, we release the Python lock, sleep a
        random 20-150ms, and retry — breaking the convoy pattern that
        SQLite's built-in deterministic backoff creates.

        Returns whatever *fn* returns.
        NzBEGIN IMMEDIATE   r   lockedbusyz$database is locked after max retries)range_WRITE_MAX_RETRIESr   r"   r%   commitBaseExceptionrollback	Exceptionr   _CHECKPOINT_EVERY_N_WRITES_try_wal_checkpointr   OperationalErrorr!   lowerrandomuniform_WRITE_RETRY_MIN_S_WRITE_RETRY_MAX_Stimesleep)r'   r+   last_errattemptresultexcerr_msgjitters           r(   _execute_writezSessionDB._execute_write   s/    )-T455 	 	GZ 
 
J&&'8999!#DJ
))++++(   ! J//1111( ! ! ! D! ,	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 !!Q&!!$t'FF!KK,,...+   c((..**w&&&G*;*;"H!81!<<<!' 3 3" " 
6***   
'22
 
 	
s|   C5B/)A('B/(
B3BB
B	BB	BB/#C5/B3	3C56B3	7;C55E<A,E76E77E<c                 .   	 | j         5  | j                            d                                          }|r4|d         dk    r(t                              d|d         |d                    ddd           dS # 1 swxY w Y   dS # t          $ r Y dS w xY w)a2  Best-effort PASSIVE WAL checkpoint.  Never blocks, never raises.

        Flushes committed WAL frames back into the main DB file for any
        frames that no other connection currently needs.  Keeps the WAL
        from growing unbounded when many processes hold persistent
        connections.
        PRAGMA wal_checkpoint(PASSIVE)r.   r   z(WAL checkpoint: %d/%d pages checkpointed   N)r   r"   r%   fetchoneloggerdebugr6   )r'   rC   s     r(   r8   zSessionDB._try_wal_checkpoint   s    	  ++4 (**   fQi!mmLLBq	6!9                     	 	 	DD	s5   B A#A9,B 9A==B  A=B 
BBc                     | j         5  | j        rL	 | j                            d           n# t          $ r Y nw xY w| j                                         d| _        ddd           dS # 1 swxY w Y   dS )zClose the database connection.

        Attempts a PASSIVE WAL checkpoint first so that exiting processes
        help keep the WAL file from growing unbounded.
        rI   N)r   r"   r%   r6   close)r'   s    r(   rO   zSessionDB.close   s     Z 	" 	"z "J&&'GHHHH    D
  """!
	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s,   A),A)
9A)9#A))A-0A-c                    | j                                         }|                    t                     |                    d           |                                }||                    dt          f           nt          |t          j	                  r|d         n|d         }|dk     rA	 |                    d           n# t          j
        $ r Y nw xY w|                    d           |d	k     rA	 |                    d
           n# t          j
        $ r Y nw xY w|                    d           |dk     rA	 |                    d           n# t          j
        $ r Y nw xY w|                    d           |dk     rhg d}|D ]L\  }}	 |                    dd          }|                    d| d|            8# t          j
        $ r Y Iw xY w|                    d           |dk     rddD ]L\  }}		 |                    dd          }
|                    d|
 d|	            8# t          j
        $ r Y Iw xY w|                    d           	 |                    d           n# t          j
        $ r Y nw xY w	 |                    d           n/# t          j
        $ r |                    t                     Y nw xY w| j                                          dS )z:Create tables and FTS if they don't exist, run migrations.z*SELECT version FROM schema_version LIMIT 1Nz/INSERT INTO schema_version (version) VALUES (?)versionr   rJ   z2ALTER TABLE messages ADD COLUMN finish_reason TEXTz%UPDATE schema_version SET version = 2   z*ALTER TABLE sessions ADD COLUMN title TEXTz%UPDATE schema_version SET version = 3   zfCREATE UNIQUE INDEX IF NOT EXISTS idx_sessions_title_unique ON sessions(title) WHERE title IS NOT NULLz%UPDATE schema_version SET version = 4   ))cache_read_tokensINTEGER DEFAULT 0)cache_write_tokensrV   )reasoning_tokensrV   )billing_providerTEXT)billing_base_urlrZ   )billing_moderZ   )estimated_cost_usdREAL)actual_cost_usdr^   )cost_statusrZ   )cost_sourcerZ   )pricing_versionrZ   "z""z!ALTER TABLE sessions ADD COLUMN "z" z%UPDATE schema_version SET version = 5r   ))	reasoningrZ   )reasoning_detailsrZ   )codex_reasoning_itemsrZ   z!ALTER TABLE messages ADD COLUMN "z%UPDATE schema_version SET version = 6z"SELECT * FROM messages_fts LIMIT 0)r"   cursorexecutescript
SCHEMA_SQLr%   rK   SCHEMA_VERSION
isinstancer   r#   r9   replaceFTS_SQLr3   )r'   rg   rowcurrent_versionnew_columnsnamecolumn_type	safe_namecol_namecol_typesafes              r(   r&   zSessionDB._init_schema   s   ""$$Z((( 	CDDDoo;NNL~N_````0:30L0LXc)nnRUVWRXO""NN#WXXXX/   DFGGG""NN#OPPPP/   DFGGG""NNE    /   DFGGG""   *5  %D+ %)LLd$;$;	'e9'e'eXc'e'effff"3   FGGG""+  &Hh
'//T::RRRRR    #3   FGGG	NN=    ' 	 	 	D		*NN?@@@@' 	* 	* 	*  )))))	* 	
s~   .C CC5D DD<E E$#E$1G  GG81H**H<;H<I+ +I=<I=J )KK
session_idsourcemodelmodel_configsystem_promptuser_idparent_session_idc                 T    fd}|                      |           S )z4Create a new session record. Returns the session_id.c                     |                      drt          j                  nd t          j                    f           d S )NzINSERT OR IGNORE INTO sessions (id, source, user_id, model, model_config,
                   system_prompt, parent_session_id, started_at)
                   VALUES (?, ?, ?, ?, ?, ?, ?, ?))r%   jsondumpsr?   )connry   rz   r}   rw   rx   r{   r|   s    r(   _doz%SessionDB.create_session.<locals>._don  sa    LL6 0<FDJ|,,,$!%IKK		    r*   rG   )	r'   rw   rx   ry   rz   r{   r|   r}   r   s	    ``````` r(   create_sessionzSessionDB.create_sessionc  s\    	 	 	 	 	 	 	 	 	 	 	  	C   r*   
end_reasonc                 @    fd}|                      |           dS )zMark a session as ended.c                 \    |                      dt          j                    f           d S )Nz=UPDATE sessions SET ended_at = ?, end_reason = ? WHERE id = ?r%   r?   )r   r   rw   s    r(   r   z"SessionDB.end_session.<locals>._do  s6    LLOj*5    r*   Nr   )r'   rw   r   r   s    `` r(   end_sessionzSessionDB.end_session  >    	 	 	 	 	 	
 	C     r*   c                 <    fd}|                      |           dS )z6Clear ended_at/end_reason so a session can be resumed.c                 6    |                      df           d S )NzCUPDATE sessions SET ended_at = NULL, end_reason = NULL WHERE id = ?r%   r   rw   s    r(   r   z%SessionDB.reopen_session.<locals>._do  s+    LLU    r*   Nr   r'   rw   r   s    ` r(   reopen_sessionzSessionDB.reopen_session  s8    	 	 	 	 	
 	C     r*   c                 @    fd}|                      |           dS )z0Store the full assembled system prompt snapshot.c                 8    |                      df           d S )Nz2UPDATE sessions SET system_prompt = ? WHERE id = ?r   )r   rw   r{   s    r(   r   z+SessionDB.update_system_prompt.<locals>._do  s.    LLD
+    r*   Nr   )r'   rw   r{   r   s    `` r(   update_system_promptzSessionDB.update_system_prompt  r   r*   r   Finput_tokensoutput_tokensrU   rW   rX   r]   r_   r`   ra   rb   rY   r[   r\   absolutec                 r    |rdnd|||||||	|	|
|||||||ffd}|                      |           dS )u  Update token counters and backfill model if not already set.

        When *absolute* is False (default), values are **incremented** — use
        this for per-API-call deltas (CLI path).

        When *absolute* is True, values are **set directly** — use this when
        the caller already holds cumulative totals (gateway path, where the
        cached agent accumulates across messages).
        a}  UPDATE sessions SET
                   input_tokens = ?,
                   output_tokens = ?,
                   cache_read_tokens = ?,
                   cache_write_tokens = ?,
                   reasoning_tokens = ?,
                   estimated_cost_usd = COALESCE(?, 0),
                   actual_cost_usd = CASE
                       WHEN ? IS NULL THEN actual_cost_usd
                       ELSE ?
                   END,
                   cost_status = COALESCE(?, cost_status),
                   cost_source = COALESCE(?, cost_source),
                   pricing_version = COALESCE(?, pricing_version),
                   billing_provider = COALESCE(billing_provider, ?),
                   billing_base_url = COALESCE(billing_base_url, ?),
                   billing_mode = COALESCE(billing_mode, ?),
                   model = COALESCE(model, ?)
                   WHERE id = ?a  UPDATE sessions SET
                   input_tokens = input_tokens + ?,
                   output_tokens = output_tokens + ?,
                   cache_read_tokens = cache_read_tokens + ?,
                   cache_write_tokens = cache_write_tokens + ?,
                   reasoning_tokens = reasoning_tokens + ?,
                   estimated_cost_usd = COALESCE(estimated_cost_usd, 0) + COALESCE(?, 0),
                   actual_cost_usd = CASE
                       WHEN ? IS NULL THEN actual_cost_usd
                       ELSE COALESCE(actual_cost_usd, 0) + ?
                   END,
                   cost_status = COALESCE(?, cost_status),
                   cost_source = COALESCE(?, cost_source),
                   pricing_version = COALESCE(?, pricing_version),
                   billing_provider = COALESCE(billing_provider, ?),
                   billing_base_url = COALESCE(billing_base_url, ?),
                   billing_mode = COALESCE(billing_mode, ?),
                   model = COALESCE(model, ?)
                   WHERE id = ?c                 4    |                                 d S Nr   )r   paramssqls    r(   r   z*SessionDB.update_token_counts.<locals>._do  s    LLf%%%%%r*   Nr   )r'   rw   r   r   ry   rU   rW   rX   r]   r_   r`   ra   rb   rY   r[   r\   r   r   r   r   s                     @@r(   update_token_countszSessionDB.update_token_counts  s    8  '	##CC(#C( !
$	& 	& 	& 	& 	& 	&C     r*   unknownc                 D    fd}|                      |           dS )a2  Ensure a session row exists, creating it with minimal metadata if absent.

        Used by _flush_messages_to_session_db to recover from a failed
        create_session() call (e.g. transient SQLite lock at agent startup).
        INSERT OR IGNORE is safe to call even when the row already exists.
        c                 ^    |                      dt          j                    f           d S )NzxINSERT OR IGNORE INTO sessions
                   (id, source, model, started_at)
                   VALUES (?, ?, ?, ?)r   )r   ry   rw   rx   s    r(   r   z%SessionDB.ensure_session.<locals>._do  s<    LL* VUDIKK8	    r*   Nr   )r'   rw   rx   ry   r   s    ``` r(   ensure_sessionzSessionDB.ensure_session  sD    	 	 	 	 	 	 	 	C     r*   c                     | j         5  | j                            d|f          }|                                }ddd           n# 1 swxY w Y   |rt	          |          ndS )zGet a session by ID.z#SELECT * FROM sessions WHERE id = ?Nr   r"   r%   rK   dictr'   rw   rg   rn   s       r(   get_sessionzSessionDB.get_session  s    Z 	$ 	$Z''5
} F //##C		$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$
  )tCyyyT)   1AA	A	session_id_or_prefixc                    |                      |          }|r|d         S |                    dd                              dd                              dd          }| j        5  | j                            d| df          }d	 |                                D             }d
d
d
           n# 1 swxY w Y   t          |          dk    r|d         S d
S )a*  Resolve an exact or uniquely prefixed session ID to the full ID.

        Returns the exact ID when it exists. Otherwise treats the input as a
        prefix and returns the single matching session ID if the prefix is
        unambiguous. Returns None for no matches or ambiguous prefixes.
        id\\\%\%_\_zSSELECT id FROM sessions WHERE id LIKE ? ESCAPE '\' ORDER BY started_at DESC LIMIT 2c                     g | ]
}|d          S )r    .0rn   s     r(   
<listcomp>z0SessionDB.resolve_session_id.<locals>.<listcomp>*  s    >>>Ss4y>>>r*   Nr.   r   )r   rl   r   r"   r%   fetchalllen)r'   r   exactescapedrg   matchess         r(   resolve_session_idzSessionDB.resolve_session_id  s'      !566 	; !WT6""WS%  WS%  	 	 Z 	? 	?Z''f  F ?>FOO,=,=>>>G	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? w<<11:ts   %>B//B36B3d   titlec                 R   | sdS t          j        dd|           }t          j        dd|          }t          j        dd|                                          }|sdS t          |          t          j        k    r-t          dt          |           dt          j         d	          |S )
a  Validate and sanitize a session title.

        - Strips leading/trailing whitespace
        - Removes ASCII control characters (0x00-0x1F, 0x7F) and problematic
          Unicode control chars (zero-width, RTL/LTR overrides, etc.)
        - Collapses internal whitespace runs to single spaces
        - Normalizes empty/whitespace-only strings to None
        - Enforces MAX_TITLE_LENGTH

        Returns the cleaned title string or None.
        Raises ValueError if the title exceeds MAX_TITLE_LENGTH after cleaning.
        Nz [\x00-\x08\x0b\x0c\x0e-\x1f\x7f] zB[\u200b-\u200f\u2028-\u202e\u2060-\u2069\ufeff\ufffc\ufff9-\ufffb]z\s+ zTitle too long (z chars, max ))resubstripr   r   MAX_TITLE_LENGTH
ValueError)r   cleaneds     r(   sanitize_titlezSessionDB.sanitize_title2  s      	4
 &<b%HH &Q
 
 &g..4466 	4w<<)444Z3w<<ZZY=WZZZ   r*   c                 r    |                                fd}|                     |          }|dk    S )aL  Set or update a session's title.

        Returns True if session was found and title was set.
        Raises ValueError if title is already in use by another session,
        or if the title fails validation (too long, invalid characters).
        Empty/whitespace-only strings are normalized to None (clearing the title).
        c                     rI|                      df          }|                                }|rt          d d|d                    |                      df          }|j        S )Nz3SELECT id FROM sessions WHERE title = ? AND id != ?zTitle 'z' is already in use by session r   z*UPDATE sessions SET title = ? WHERE id = ?)r%   rK   r   rowcount)r   rg   conflictrw   r   s      r(   r   z(SessionDB.set_session_title.<locals>._dog  s     
IJ'  "??,, $X%XXQUXX   \\<
# F ?"r*   r   )r   rG   )r'   rw   r   r   r   s    ``  r(   set_session_titlezSessionDB.set_session_title^  sV     ##E**	# 	# 	# 	# 	# 	#" &&s++!|r*   c                     | j         5  | j                            d|f          }|                                }ddd           n# 1 swxY w Y   |r|d         ndS )z%Get the title for a session, or None.z'SELECT title FROM sessions WHERE id = ?Nr   r   r"   r%   rK   r   s       r(   get_session_titlezSessionDB.get_session_title{  s    Z 	$ 	$Z''9J= F //##C		$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$
  #,s7||,r   c                     | j         5  | j                            d|f          }|                                }ddd           n# 1 swxY w Y   |rt	          |          ndS )z?Look up a session by exact title. Returns session dict or None.z&SELECT * FROM sessions WHERE title = ?Nr   )r'   r   rg   rn   s       r(   get_session_by_titlezSessionDB.get_session_by_title  s    Z 	$ 	$Z''85( F //##C		$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$
  )tCyyyT)r   c                    |                      |          }|                    dd                              dd                              dd          }| j        5  | j                            d| df          }|                                }d	d	d	           n# 1 swxY w Y   |r|d
         d         S |r|d         S d	S )ad  Resolve a title to a session ID, preferring the latest in a lineage.

        If the exact title exists, returns that session's ID.
        If not, searches for "title #N" variants and returns the latest one.
        If the exact title exists AND numbered variants exist, returns the
        latest numbered variant (the most recent continuation).
        r   r   r   r   r   r   zaSELECT id, title, started_at FROM sessions WHERE title LIKE ? ESCAPE '\' ORDER BY started_at DESC #%Nr   r   )r   rl   r   r"   r%   r   )r'   r   r   r   rg   numbereds         r(   resolve_session_by_titlez"SessionDB.resolve_session_by_title  s    ))%00 --f--55c5AAII#uUUZ 	) 	)Z''J" F
 ((H	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)  	A;t$$ 	;ts   4BB"B
base_titlec           	      N   t          j        d|          }|r|                    d          }n|}|                    dd                              dd                              dd          }| j        5  | j                            d	|| d
f          }d |                                D             }ddd           n# 1 swxY w Y   |s|S d}|D ]I}t          j        d|          }	|	r0t          |t          |	                    d                              }J| d|dz    S )u   Generate the next title in a lineage (e.g., "my session" → "my session #2").

        Strips any existing " #N" suffix to find the base name, then finds
        the highest existing number and increments.
        z^(.*?) #(\d+)$r.   r   r   r   r   r   r   zESELECT title FROM sessions WHERE title = ? OR title LIKE ? ESCAPE '\'r   c                     g | ]
}|d          S )r   r   r   s     r(   r   z7SessionDB.get_next_title_in_lineage.<locals>.<listcomp>  s    BBBGBBBr*   Nz^.* #(\d+)$z #)
r   matchgrouprl   r   r"   r%   r   maxint)
r'   r   r   baser   rg   existingmax_numtms
             r(   get_next_title_in_lineagez#SessionDB.get_next_title_in_lineage  s    *J77 	;;q>>DDD ,,tV,,44S%@@HHeTTZ 	C 	CZ''X'' F CB0A0ABBBH	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C  	K  	8 	8A++A 8gs1771::77'''A+'''s   5?C  CC   exclude_sourceslimitoffsetinclude_childrenc                 D   g }g }|s|                     d           |r*|                     d           |                     |           |rMd                    d |D                       }|                     d| d           |                    |           |rdd                    |           nd	}	d
|	 d}
|                    ||g           | j        5  | j                            |
|          }|                                }ddd           n# 1 swxY w Y   g }|D ]}}t          |          }|                    dd	          	                                }|r(|dd         }|t          |          dk    rdnd	z   |d<   nd	|d<   |                     |           ~|S )a  List sessions with preview (first user message) and last active timestamp.

        Returns dicts with keys: id, source, model, title, started_at, ended_at,
        message_count, preview (first 60 chars of first user message),
        last_active (timestamp of last message).

        Uses a single query with correlated subqueries instead of N+2 queries.

        By default, child sessions (subagent runs, compression continuations)
        are excluded.  Pass ``include_children=True`` to include them.
        zs.parent_session_id IS NULLzs.source = ?,c              3      K   | ]}d V  dS ?Nr   r   r   s     r(   	<genexpr>z/SessionDB.list_sessions_rich.<locals>.<genexpr>  s"      #A#AAC#A#A#A#A#A#Ar*   s.source NOT IN (r   zWHERE  AND r   ah  
            SELECT s.*,
                COALESCE(
                    (SELECT SUBSTR(REPLACE(REPLACE(m.content, X'0A', ' '), X'0D', ' '), 1, 63)
                     FROM messages m
                     WHERE m.session_id = s.id AND m.role = 'user' AND m.content IS NOT NULL
                     ORDER BY m.timestamp, m.id LIMIT 1),
                    ''
                ) AS _preview_raw,
                COALESCE(
                    (SELECT MAX(m2.timestamp) FROM messages m2 WHERE m2.session_id = s.id),
                    s.started_at
                ) AS last_active
            FROM sessions s
            zM
            ORDER BY s.started_at DESC
            LIMIT ? OFFSET ?
        N_preview_raw<   z...preview)appendjoinextendr   r"   r%   r   r   popr   r   )r'   rx   r   r   r   r   where_clausesr   placeholders	where_sqlqueryrg   rowssessionsrn   srawtexts                     r(   list_sessions_richzSessionDB.list_sessions_rich  s)   &  	@  !>??? 	"  000MM&!!! 	+88#A#A#A#A#AAAL  !D\!D!D!DEEEMM/***>KS:W\\-88:::QS	   $ 	ufo&&&Z 	% 	%Z''v66F??$$D	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  		 		CS		A%%++1133C "3B3x#C2uu2F)!)OOAs   0DDDrolecontent	tool_name
tool_callstool_call_idtoken_countfinish_reasonrd   re   rf   c                 H  	 |
rt          j        |
          nd|rt          j        |          nd|rt          j        |          ndd|&t          |t                    rt	          |          nd	fd}|                     |          S )z
        Append a message to a session. Returns the message row ID.

        Also increments the session's message_count (and tool_call_count
        if role is 'tool' or tool_calls is present).
        Nr   r.   c                     |                      d
	t          j                    f          }|j        }dk    r|                      d
f           n|                      d
f           |S )Na  INSERT INTO messages (session_id, role, content, tool_call_id,
                   tool_calls, tool_name, timestamp, token_count, finish_reason,
                   reasoning, reasoning_details, codex_reasoning_items)
                   VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)r   zUPDATE sessions SET message_count = message_count + 1,
                       tool_call_count = tool_call_count + ? WHERE id = ?zBUPDATE sessions SET message_count = message_count + 1 WHERE id = ?)r%   r?   	lastrowid)r   rg   msg_idcodex_items_jsonr  r	  num_tool_callsrd   reasoning_details_jsonr  rw   r  r  tool_calls_jsonr  s      r(   r   z%SessionDB.append_message.<locals>._do;  s    \\B
  #IKK!*$ F( %F !!M#Z0    XM   Mr*   )r   r   rk   listr   rG   )r'   rw   r  r  r  r  r  r  r	  rd   re   rf   r   r  r  r  r  s    ```` ````   @@@@r(   append_messagezSessionDB.append_message  s    . !+DJ()))&* 	 %/DJ,---*. 	 5?H$*Z000D !0::t0L0LSS___RSN#	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	 #	J ""3'''r*   c                    | j         5  | j                            d|f          }|                                }ddd           n# 1 swxY w Y   g }|D ]}t	          |          }|                    d          rZ	 t          j        |d                   |d<   n;# t          j        t          f$ r" t                              d           g |d<   Y nw xY w|                    |           |S )z6Load all messages for a session, ordered by timestamp.zBSELECT * FROM messages WHERE session_id = ? ORDER BY timestamp, idNr  zDFailed to deserialize tool_calls in get_messages, falling back to [])r   r"   r%   r   r   getr   loadsJSONDecodeError	TypeErrorrL   warningr   )r'   rw   rg   r   rC   rn   msgs          r(   get_messageszSessionDB.get_messagesb  sG   Z 	% 	%Z''T F ??$$D	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  	 	Cs))Cww|$$ ++(,
3|3D(E(EC%%,i8 + + +NN#ijjj(*C%%%+ MM#s#   1AA	A	;B5CCc                    | j         5  | j                            d|f          }|                                }ddd           n# 1 swxY w Y   g }|D ]}|d         |d         d}|d         r|d         |d<   |d         r|d         |d<   |d         rZ	 t	          j        |d                   |d<   n;# t          j        t          f$ r" t          	                    d	           g |d<   Y nw xY w|d         d
k    r|d         r|d         |d<   |d         rZ	 t	          j        |d                   |d<   n;# t          j        t          f$ r" t          	                    d           d|d<   Y nw xY w|d         rZ	 t	          j        |d                   |d<   n;# t          j        t          f$ r" t          	                    d           d|d<   Y nw xY w|
                    |           |S )z
        Load messages in the OpenAI conversation format (role + content dicts).
        Used by the gateway to restore conversation history.
        zSELECT role, content, tool_call_id, tool_calls, tool_name, reasoning, reasoning_details, codex_reasoning_items FROM messages WHERE session_id = ? ORDER BY timestamp, idNr  r  r  r  r  r  r  zKFailed to deserialize tool_calls in conversation replay, falling back to []	assistantrd   re   z=Failed to deserialize reasoning_details, falling back to Nonerf   zAFailed to deserialize codex_reasoning_items, falling back to None)r   r"   r%   r   r   r  r  r  rL   r  r   )r'   rw   rg   r   messagesrn   r  s          r(   get_messages_as_conversationz&SessionDB.get_messages_as_conversationv  s   
 Z 	% 	%Z''L 	 F ??$$D	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  	! 	!Cv;3y>BBC>" :&).&9N#; 4#&{#3K <  ++(,
3|3D(E(EC%%,i8 + + +NN#pqqq(*C%%%+ 6{k)){# 8'*;'7C$*+ 8837:cBU>V3W3W/00 0)< 8 8 8'fggg37/0008 ./ <<7;z#F]B^7_7_344 0)< < < <'jkkk7;3444< OOC    sG   1AA	A	B555C-,C-D665E.-E.:F5GGr   c                 <   g dt           j        dt          ffd}t          j        d||           }t          j        dd|          }t          j        dd|          }t          j        d	d
|          }t          j        dd|                                          }t          j        dd|                                          }t          j        dd|          }t                    D ]\  }}|                    d| d|          } |                                S )a  Sanitize user input for safe use in FTS5 MATCH queries.

        FTS5 has its own query syntax where characters like ``"``, ``(``, ``)``,
        ``+``, ``*``, ``{``, ``}`` and bare boolean operators (``AND``, ``OR``,
        ``NOT``) have special meaning.  Passing raw user input directly to
        MATCH can cause ``sqlite3.OperationalError``.

        Strategy:
        - Preserve properly paired quoted phrases (``"exact phrase"``)
        - Strip unmatched FTS5-special characters that would cause errors
        - Wrap unquoted hyphenated and dotted terms in quotes so FTS5
          matches them as exact phrases instead of splitting on the
          hyphen/dot (e.g. ``chat-send``, ``P2.2``, ``my-app.config.ts``)
        r   r,   c                                          |                     d                     dt                    dz
   dS )Nr    Qr.    )r   r   r   )r   _quoted_partss    r(   _preserve_quotedz8SessionDB._sanitize_fts5_query.<locals>._preserve_quoted  s?      ,,,73}--17777r*   z"[^"]*"z
[+{}()\"^]r   z\*+*z(^|\s)\*z\1z(?i)^(AND|OR|NOT)\b\s*r   z(?i)\s+(AND|OR|NOT)\s*$z\b(\w+(?:[.-]\w+)+)\bz"\1"r#  r$  )r   Matchr!   r   r   	enumeraterl   )r   r&  	sanitizediquotedr%  s        @r(   _sanitize_fts5_queryzSessionDB._sanitize_fts5_query  s-   $ !	8 	8S 	8 	8 	8 	8 	8 	8 F:'7??	 F=#y99	 F63	22	F;y99	 F4b)//:K:KLL	F5r9??;L;LMM	 F3WiHH	 #=11 	C 	CIAv!))/!///6BBII   r*   source_filterrole_filterc           	         |r|                                 sg S |                     |          }|sg S dg}|g}|Md                    d |D                       }	|                    d|	 d           |                    |           |Md                    d |D                       }
|                    d|
 d           |                    |           |rMd                    d	 |D                       }|                    d
| d           |                    |           d                    |          }|                    ||g           d| d}| j        5  	 | j                            ||          }n## t          j	        $ r g cY cddd           S w xY wd |
                                D             }ddd           n# 1 swxY w Y   |D ]}	 | j        5  | j                            d|d         |d         |d         f          }d |
                                D             }ddd           n# 1 swxY w Y   ||d<   v# t          $ r g |d<   Y w xY w|D ]}|                    dd           |S )a  
        Full-text search across session messages using FTS5.

        Supports FTS5 query syntax:
          - Simple keywords: "docker deployment"
          - Phrases: '"exact phrase"'
          - Boolean: "docker OR kubernetes", "python NOT java"
          - Prefix: "deploy*"

        Returns matching messages with session metadata, content snippet,
        and surrounding context (1 message before and after the match).
        zmessages_fts MATCH ?Nr   c              3      K   | ]}d V  dS r   r   r   s     r(   r   z,SessionDB.search_messages.<locals>.<genexpr>  s"      *F*F13*F*F*F*F*F*Fr*   zs.source IN (r   c              3      K   | ]}d V  dS r   r   r   s     r(   r   z,SessionDB.search_messages.<locals>.<genexpr>  s"      +I+IAC+I+I+I+I+I+Ir*   r   c              3      K   | ]}d V  dS r   r   r   s     r(   r   z,SessionDB.search_messages.<locals>.<genexpr>	  s"      (B(B(B(B(B(B(B(Br*   zm.role IN (r   a  
            SELECT
                m.id,
                m.session_id,
                m.role,
                snippet(messages_fts, 0, '>>>', '<<<', '...', 40) AS snippet,
                m.content,
                m.timestamp,
                m.tool_name,
                s.source,
                s.model,
                s.started_at AS session_started
            FROM messages_fts
            JOIN messages m ON m.id = messages_fts.rowid
            JOIN sessions s ON s.id = m.session_id
            WHERE z@
            ORDER BY rank
            LIMIT ? OFFSET ?
        c                 ,    g | ]}t          |          S r   r   r   s     r(   r   z-SessionDB.search_messages.<locals>.<listcomp>*  s    >>>StCyy>>>r*   zSELECT role, content FROM messages
                           WHERE session_id = ? AND id >= ? - 1 AND id <= ? + 1
                           ORDER BY idrw   r   c                 D    g | ]}|d          |d         pddd         dS )r  r  r   N   r  r   )r   rs     r(   r   z-SessionDB.search_messages.<locals>.<listcomp>7  sF     $ $ $ "#6)8JDSD7QRR$ $ $r*   contextr  )r   r-  r   r   r   r   r"   r%   r   r9   r   r6   r   )r'   r   r.  r   r/  r   r   r   r   source_placeholdersexclude_placeholdersrole_placeholdersr   r   rg   r   r   
ctx_cursorcontext_msgss                      r(   search_messageszSessionDB.search_messages  s   *  	EKKMM 	I))%00 	I 00w$"%((*F*F*F*F*F"F"F  !G1D!G!G!GHHHMM-(((&#&88+I+I+I+I+I#I#I   !L5I!L!L!LMMMMM/*** 	' #(B(Bk(B(B(B B B  !C/@!C!C!CDDDMM+&&&LL//	ufo&&&   ( Z 	? 	?++C88+   			? 	? 	? 	? 	? 	? 	? 	? ?>FOO,=,=>>>G	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?  	& 	&E&Z 
 
!%!3!3* |,eDk5;G	" "J$ $!+!4!4!6!6$ $ $L
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 $0i   & & &#%i   &  	' 	'EIIi&&&&sm   G	 E<;G	<FG	F!G		GGI AH;/I;H?	?IH?	IIIc                     | j         5  |r| j                            d|||f          }n| j                            d||f          }d |                                D             cddd           S # 1 swxY w Y   dS )z-List sessions, optionally filtered by source.zQSELECT * FROM sessions WHERE source = ? ORDER BY started_at DESC LIMIT ? OFFSET ?z@SELECT * FROM sessions ORDER BY started_at DESC LIMIT ? OFFSET ?c                 ,    g | ]}t          |          S r   r5  r   s     r(   r   z-SessionDB.search_sessions.<locals>.<listcomp>W  s    ;;;#DII;;;r*   N)r   r"   r%   r   )r'   rx   r   r   rg   s        r(   search_sessionszSessionDB.search_sessionsE  s     Z 	< 	< 	++gUF+ 
 ++VFO  <;):):;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   AA11A58A5c                     | j         5  |r| j                            d|f          }n| j                            d          }|                                d         cddd           S # 1 swxY w Y   dS )z.Count sessions, optionally filtered by source.z.SELECT COUNT(*) FROM sessions WHERE source = ?zSELECT COUNT(*) FROM sessionsr   Nr   )r'   rx   rg   s      r(   session_countzSessionDB.session_count]  s    Z 	( 	( M++Dvi  ++,KLL??$$Q'	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(   AA((A,/A,c                     | j         5  |r| j                            d|f          }n| j                            d          }|                                d         cddd           S # 1 swxY w Y   dS )z2Count messages, optionally for a specific session.z2SELECT COUNT(*) FROM messages WHERE session_id = ?zSELECT COUNT(*) FROM messagesr   Nr   )r'   rw   rg   s      r(   message_countzSessionDB.message_counth  s    Z 	( 	( M++H:-  ++,KLL??$$Q'	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(rE  c                 n    |                      |          }|sdS |                     |          }i |d|iS )z8Export a single session with all its messages as a dict.Nr  )r   r  )r'   rw   sessionr  s       r(   export_sessionzSessionDB.export_sessionw  sJ    "":.. 	4$$Z000'0:x000r*   c                     |                      |d          }g }|D ]8}|                     |d                   }|                    i |d|i           9|S )z
        Export all sessions (with messages) as a list of dicts.
        Suitable for writing to a JSONL file for backup/analysis.
        i )rx   r   r   r  )rB  r  r   )r'   rx   r   resultsrI  r  s         r(   
export_allzSessionDB.export_all  sq    
 ''vV'DD 	> 	>G((77HNN<g<z8<<====r*   c                 <    fd}|                      |           dS )z9Delete all messages for a session and reset its counters.c                 d    |                      df           |                      df           d S )N)DELETE FROM messages WHERE session_id = ?zGUPDATE sessions SET message_count = 0, tool_call_count = 0 WHERE id = ?r   r   s    r(   r   z%SessionDB.clear_messages.<locals>._do  sJ    LL;j]   LLY    r*   Nr   r   s    ` r(   clear_messageszSessionDB.clear_messages  s8    	 	 	 	 	 	C     r*   c                 8    fd}|                      |          S )zDelete a session and all its messages.

        Child sessions are orphaned (parent_session_id set to NULL) rather
        than cascade-deleted, so they remain accessible independently.
        Returns True if the session was found and deleted.
        c                     |                      df          }|                                d         dk    rdS |                      df           |                      df           |                      df           dS )Nz*SELECT COUNT(*) FROM sessions WHERE id = ?r   FzHUPDATE sessions SET parent_session_id = NULL WHERE parent_session_id = ?rP  !DELETE FROM sessions WHERE id = ?T)r%   rK   )r   rg   rw   s     r(   r   z%SessionDB.delete_session.<locals>._do  s    \\<zm F   #q((uLL.  
 LLDzmTTTLL<zmLLL4r*   r   r   s    ` r(   delete_sessionzSessionDB.delete_session  s2    	 	 	 	 	 ""3'''r*   Z   older_than_daysc                 n    t          j                     |dz  z
  fd}|                     |          S )a  Delete sessions older than N days. Returns count of deleted sessions.

        Only prunes ended sessions (not active ones).  Child sessions outside
        the prune window are orphaned (parent_session_id set to NULL) rather
        than cascade-deleted.
        iQ c                    r|                      df          }n|                      df          }t          d |                                D                       }|sdS d                    dt	          |          z            }|                      d| dt          |                     |D ]0}|                      d	|f           |                      d
|f           1t	          |          S )NzkSELECT id FROM sessions
                       WHERE started_at < ? AND ended_at IS NOT NULL AND source = ?zESELECT id FROM sessions WHERE started_at < ? AND ended_at IS NOT NULLc              3   &   K   | ]}|d          V  dS )r   Nr   r   s     r(   r   z8SessionDB.prune_sessions.<locals>._do.<locals>.<genexpr>  s&      EECc$iEEEEEEr*   r   r   r   zIUPDATE sessions SET parent_session_id = NULL WHERE parent_session_id IN (r   rP  rT  )r%   setr   r   r   r  )r   rg   session_idsr   sidcutoffrx   s        r(   r   z%SessionDB.prune_sessions.<locals>._do  s*    
WV$  [I  EE6??3D3DEEEEEK q 88C#k*:*:$:;;LLL?/;? ? ?[!!   # J JH3&QQQ@3&IIII{###r*   )r?   rG   )r'   rW  rx   r   r^  s     ` @r(   prune_sessionszSessionDB.prune_sessions  sO     % 78	$ 	$ 	$ 	$ 	$ 	$< ""3'''r*   r   )r,   N)NNNNN)r   r   Nr   r   r   NNNNNNNNF)r   N)NNr   r   F)	NNNNNNNNN)NNNr   r   )Nr   r   )rV  N)8__name__
__module____qualname____doc__r2   r=   r>   r7   r   r)   r   r   
Connectionr   rG   r8   rO   r&   r!   r   r   r   r   r   r   r   r	   floatboolr   r   r   r   r   staticmethodr   r   r   r   r   r   r   r  r  r  r   r-  r?  rB  rD  rG  rJ  rM  rQ  rU  r_  r   r*   r(   r   r   s   sW          !#     42
7+=*>*A!B 2
q 2
 2
 2
 2
h   *" " "a a aV '+!!%   	
 38n    
   <!c !s !t ! ! ! !! ! ! ! ! !!s !3 !4 ! ! ! ! !""# !.2+/%)%))-*.*.&*#X! X!X! X! 	X!
 X! X!  X! X! %UOX! "%X! c]X! c]X! "#X! #3-X! #3-X!  sm!X!" #X!$ 
%X! X! X! X!z  	! !! ! 	!
 
! ! ! !**c *htCH~.F * * * *s x}    8 )hsm ) ) ) ) \)VC      :-C -HSM - - - -*# *(4S>2J * * * *c hsm    :!(C !(C !( !( !( !(J %)!&D DD cD 	D
 D D 
d38n	D D D D\  !!%%)I( I(I( I( 	I(
 I( I( I( I( I( I( I(  #I( 
I( I( I( I(Vs tDcN/C    (-s -tDcN?S - - - -f 2!C 2!C 2! 2! 2! \2!n $(%)!%e ee Cye c	e
 #Ye e e 
d38n	e e e eR 	< << < 	<
 
d38n	< < < <0	( 	(C 	(3 	( 	( 	( 	(	( 	( 	(s 	( 	( 	( 	(1 1$sCx.1I 1 1 1 1
 
 
T#s(^0D 
 
 
 

! 
! 
! 
! 
! 
!( ( ( ( ( (0'( '(c '( '(s '( '( '( '( '( '(r*   r   )rc  r   loggingr;   r   r   r   r?   pathlibr   hermes_constantsr   typingr   r   r   r   r	   r
   	getLoggerr`  rL   r   r   rj   ri   rm   r   r   r*   r(   <module>rm     s(       				             , , , , , , ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?		8	$	$GCLL!/##j07
r,c( c( c( c( c( c( c( c( c( c(r*   