
    iv                        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 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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)sanitize_context)get_hermes_home)AnyCallableDictListOptionalTypeVarTzstate.db   a  
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,
    api_call_count INTEGER DEFAULT 0,
    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_content TEXT,
    reasoning_details TEXT,
    codex_reasoning_items TEXT,
    codex_message_items TEXT
);

CREATE TABLE IF NOT EXISTS state_meta (
    key TEXT PRIMARY KEY,
    value 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
);

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

CREATE TRIGGER IF NOT EXISTS messages_fts_delete AFTER DELETE ON messages BEGIN
    DELETE FROM messages_fts WHERE rowid = old.id;
END;

CREATE TRIGGER IF NOT EXISTS messages_fts_update AFTER UPDATE ON messages BEGIN
    DELETE FROM messages_fts WHERE rowid = old.id;
    INSERT INTO messages_fts(rowid, content) VALUES (
        new.id,
        COALESCE(new.content, '') || ' ' || COALESCE(new.tool_name, '') || ' ' || COALESCE(new.tool_calls, '')
    );
END;
a  
CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts_trigram USING fts5(
    content,
    tokenize='trigram'
);

CREATE TRIGGER IF NOT EXISTS messages_fts_trigram_insert AFTER INSERT ON messages BEGIN
    INSERT INTO messages_fts_trigram(rowid, content) VALUES (
        new.id,
        COALESCE(new.content, '') || ' ' || COALESCE(new.tool_name, '') || ' ' || COALESCE(new.tool_calls, '')
    );
END;

CREATE TRIGGER IF NOT EXISTS messages_fts_trigram_delete AFTER DELETE ON messages BEGIN
    DELETE FROM messages_fts_trigram WHERE rowid = old.id;
END;

CREATE TRIGGER IF NOT EXISTS messages_fts_trigram_update AFTER UPDATE ON messages BEGIN
    DELETE FROM messages_fts_trigram WHERE rowid = old.id;
    INSERT INTO messages_fts_trigram(rowid, content) VALUES (
        new.id,
        COALESCE(new.content, '') || ' ' || COALESCE(new.tool_name, '') || ' ' || COALESCE(new.tool_calls, '')
    );
END;
c            %       l   e Zd ZdZdZdZdZdZddefdZ	d	e
ej        gef         d
efdZddZd Zeded
eeeeef         f         fd            Zdej        d
dfdZd Z	 	 	 	 	 ddedededeeef         dededed
dfdZ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	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd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d0e!d
df$d1Z"	 	 ddededed
efd3Z#dd4d5d
efd6Z$ded
eeeef                  fd7Z%d8ed
ee         fd9Z&d:Z'ed;ee         d
ee         fd<            Z(ded;ed
e!fd=Z)ded
ee         fd>Z*d;ed
eeeef                  fd?Z+d;ed
ee         fd@Z,dAed
efdBZ-ded
ee         fdCZ.	 	 	 	 	 	 	 ddedFe/e         dGedHedIe!dJe!dKe!d
e/eeef                  fdLZ0ded
eeeef                  fdMZ1dNZ2e3dOed
efdP            Z4e3dOed
efdQ            Z5	 	 	 	 	 	 	 	 	 	 	 ddedRedOedSedTedUedVedWedXedYedZed[ed\ed
efd]Z6ded^e/eeef                  d
dfd_Z7ded
e/eeef                  fd`Z8ded
efdaZ9	 ddedbe!d
e/eeef                  fdcZ:ded
e/e         fddZ;ed^e/eeef                  deeeef         d
e!fdf            Z<edged
efdh            Z=edied
e!fdj            Z>edked
e!fdl            Z?e3dked
efdm            Z@	 	 	 	 	 ddgedne/e         dFe/e         doe/e         dGedHed
e/eeef                  fdpZA	 	 	 ddedGedHed
e/eeef                  fdqZBdded
efdrZCdded
efdsZDded
eeeef                  fdtZEdded
e/eeef                  fduZFded
dfdvZGed4ee         ded
dfdw            ZH	 dded4ee         d
e!fdxZI	 	 	 ddzeded4ee         d
efd{ZJd|ed
ee         fd}ZKd|ed~ed
dfdZLddZM	 	 	 	 ddedede!d4ee         d
eeef         f
dZNdS )	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     1/home/ubuntu/.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debugr7   )r(   rD   s     r)   r9   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.
        rJ   N)r   r#   r&   r7   closer(   s    r)   rP   zSessionDB.close  s     Z 	" 	"z "J&&'GHHHH    D
  """!
	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s,   A),A)
9A)9#A))A-0A-
schema_sqlc                 r   t          j        d          }	 |                    |            i }|                    d                                          D ]\  }i }|                    d| d                                          D ]~}|d         }|d         pd}|d         }|d	         }	|d
         }
|r|gng }|r|
s|                    d           |	|                    d|	            d                    |          ||<   |||<   ||                                 S # |                                 w xY w)u  Extract expected columns per table from SCHEMA_SQL.

        Uses an in-memory SQLite database to parse the SQL — SQLite itself
        handles all syntax (DEFAULT expressions with commas, inline
        REFERENCES, CHECK constraints, etc.) so there are zero regex
        edge cases.  The in-memory DB is opened, the schema DDL is
        executed, and PRAGMA table_info extracts the column metadata.

        Adding a column to SCHEMA_SQL is all that's needed; the
        reconciliation loop picks it up automatically.
        z:memory:zNSELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'PRAGMA table_info("")r/   rK             zNOT NULLNzDEFAULT  )r    r!   executescriptr&   fetchallappendjoinrP   )rR   reftable_columnstblcolsrowcol_namecol_typenotnulldefaultpkpartss               r)   _parse_schema_columnszSessionDB._parse_schema_columns(  sg    oj))	j)))79M++B  hjj* * (*;;1#111 (**5 5C  #1vH"1v|H!!fG!!fGQB*2:XJJE 1r 1Z000*%9%9%9:::%(XXe__DNN%)c"" IIKKKKCIIKKKKs   C5D   D6cursorc           
         |                      t                    }|                                D ])\  }}	 |                    d| d                                          }n# t
          j        $ r Y Dw xY wt                      }|D ]C}t          |t          t          f          r|d         n|d         }|                    |           D|                                D ]x\  }	}
|	|vro|	                    dd          }	 |                    d| d| d	|
            ?# t
          j        $ r'}t                              d
||	|           Y d}~pd}~ww xY wy+dS )uZ  Ensure live tables have every column declared in SCHEMA_SQL.

        Follows the Beets/sqlite-utils pattern: the CREATE TABLE definition
        in SCHEMA_SQL is the single source of truth for the desired schema.
        On every startup this method diffs the live columns (via PRAGMA
        table_info) against the declared columns, and ADDs any that are
        missing.

        This makes column additions a declarative operation — just add
        the column to SCHEMA_SQL and it appears on the next startup.
        Version-gated migration blocks are no longer needed for ADD COLUMN.
        rT   rU   r/   name"""zALTER TABLE "z" ADD COLUMN "z" zreconcile %s.%s: %sN)rj   
SCHEMA_SQLitemsr&   r\   r    r:   set
isinstancetuplelistaddreplacerM   rN   )r(   rk   expected
table_namedeclared_colsrows	live_colsrc   rm   rd   re   	safe_namerE   s                r)   _reconcile_columnszSessionDB._reconcile_columnsS  s    --j99)1)9)9 	 	%J~~8*888 (**  +   I $ $!+C%!?!?Ps1vvS[d####&3&9&9&;&;  "(9,, ( 0 0d ; ;I]J]]i]]S[]]    #3   
 1:x        -	 	s)   +A""A43A4?DE-EEc                    | j                                         }|                    t                     |                     |           |                    d           |                                }||                    dt          f           nt          |t          j
                  r|d         n|d         }|dk     ra	 |                    d           d}n# t          j        $ r d	}Y nw xY w|s/|                    t                     |                    d
           |dk     rdD ]0}	 |                    d|            # t          j        $ r Y -w xY wdD ]0}	 |                    d|            # t          j        $ r Y -w xY w|                    t                     |                    t                     |                    d           |                    d           |t          k     r|                    dt          f           	 |                    d           n# t          j        $ r Y nw xY w	 |                    d           n/# t          j        $ r |                    t                     Y nw xY w	 |                    d           n/# t          j        $ r |                    t                     Y nw xY w| j                                          dS )a  Create tables and FTS if they don't exist, reconcile columns.

        Schema management follows the declarative reconciliation pattern
        (Beets, sqlite-utils): SCHEMA_SQL is the single source of truth.
        On existing databases, _reconcile_columns() diffs live columns
        against SCHEMA_SQL and ADDs any missing ones.  This eliminates
        the version-gated migration chain for column additions, making
        it impossible for reordered or inserted migrations to skip columns.

        The schema_version table is retained for future data migrations
        (transforming existing rows) which cannot be handled declaratively.
        z*SELECT version FROM schema_version LIMIT 1Nz/INSERT INTO schema_version (version) VALUES (?)versionr   
   z*SELECT * FROM messages_fts_trigram LIMIT 0TFzkINSERT INTO messages_fts_trigram(rowid, content) SELECT id, content FROM messages WHERE content IS NOT NULLr   )messages_fts_insertmessages_fts_deletemessages_fts_updatemessages_fts_trigram_insertmessages_fts_trigram_deletemessages_fts_trigram_updatezDROP TRIGGER IF EXISTS )messages_ftsmessages_fts_trigramzDROP TABLE IF EXISTS zINSERT INTO messages_fts(rowid, content) SELECT id, COALESCE(content, '') || ' ' || COALESCE(tool_name, '') || ' ' || COALESCE(tool_calls, '') FROM messageszINSERT INTO messages_fts_trigram(rowid, content) SELECT id, COALESCE(content, '') || ' ' || COALESCE(tool_name, '') || ' ' || COALESCE(tool_calls, '') FROM messagesz%UPDATE schema_version SET version = ?zfCREATE UNIQUE INDEX IF NOT EXISTS idx_sessions_title_unique ON sessions(title) WHERE title IS NOT NULLz"SELECT * FROM messages_fts LIMIT 0)r#   rk   r[   rp   r~   r&   rL   SCHEMA_VERSIONrs   r    r$   r:   FTS_TRIGRAM_SQLFTS_SQLr4   )r(   rk   rc   current_version_fts_trigram_exists_trig_tbls          r)   r'   zSessionDB._init_schema  s    ""$$Z((( 	'''
 	CDDDoo;NNA!   
 1;30L0LXc)nnRUVWRXO
 ##
0NN#OPPP*.''/ 0 0 0*/'''0* ((999NNU   ##  E'H'H'HIIII"3   D  D'Et'E'EFFFF"3    $$W---$$_555$   $   //;#%  	NN=    ' 	 	 	D		*NN?@@@@' 	* 	* 	*  )))))	*	2NNGHHHH' 	2 	2 	2  11111	2 	
sl   C C/.C/.EEE!E::FFH+ +H=<H=I )JJJ )K	K	
session_idsourcemodelmodel_configsystem_promptuser_idparent_session_idc                 T    fd}|                      |           dS )z)Shared INSERT OR IGNORE for session rows.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@   )connr   r   r   r   r   r   r   s    r)   _doz*SessionDB._insert_session_row.<locals>._do  sa    LL6 0<FDJ|,,,$!%IKK		    r+   NrH   )	r(   r   r   r   r   r   r   r   r   s	    ``````` r)   _insert_session_rowzSessionDB._insert_session_row  s\    	 	 	 	 	 	 	 	 	 	 	  	C     r+   c                 $     | j         ||fi | |S )z4Create a new session record. Returns the session_id.r   )r(   r   r   kwargss       r)   create_sessionzSessionDB.create_session"  s%      V>>v>>>r+   
end_reasonc                 @    fd}|                      |           dS )a  Mark a session as ended.

        No-ops when the session is already ended. The first end_reason wins:
        compression-split sessions must keep their ``end_reason = 'compression'``
        record even if a later stale ``end_session()`` call (e.g. from a
        desynced CLI session_id after ``/resume`` or ``/branch``) targets them
        with a different reason. Use ``reopen_session()`` first if you
        intentionally need to re-end a closed session with a new reason.
        c                 \    |                      dt          j                    f           d S )NzRUPDATE sessions SET ended_at = ?, end_reason = ? WHERE id = ? AND ended_at IS NULL)r&   r@   )r   r   r   s    r)   r   z"SessionDB.end_session.<locals>._do0  s8    LL4j*5    r+   Nr   )r(   r   r   r   s    `` r)   end_sessionzSessionDB.end_session&  s>    	 	 	 	 	 	 	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   r   s    r)   r   z%SessionDB.reopen_session.<locals>._do:  s+    LLU    r+   Nr   r(   r   r   s    ` r)   reopen_sessionzSessionDB.reopen_session8  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   r   r   s    r)   r   z+SessionDB.update_system_prompt.<locals>._doC  s.    LLD
+    r+   Nr   )r(   r   r   r   s    `` r)   update_system_promptzSessionDB.update_system_promptA  s>    	 	 	 	 	 	
 	C     r+   r   Finput_tokensoutput_tokenscache_read_tokenscache_write_tokensreasoning_tokensestimated_cost_usdactual_cost_usdcost_statuscost_sourcepricing_versionbilling_providerbilling_base_urlbilling_modeapi_call_countabsolutec                 t    |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, ?),
                   api_call_count = ?
                   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, ?),
                   api_call_count = COALESCE(api_call_count, 0) + ?
                   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(   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                      @@r)   update_token_countszSessionDB.update_token_countsJ  s    :  )	##CC*#C* #
&	& 	& 	& 	& 	& 	&C     r+   unknownc                 (     | j         ||fd|i| |S )zHEnsure a session row exists (INSERT OR IGNORE). Accepts optional kwargs.r   r   )r(   r   r   r   r   s        r)   ensure_sessionzSessionDB.ensure_session  s,     	! VKK5KFKKKr+   sessions_dirzOptional[Path]c                     t          j                     dz
  fd}|                     |          pg }|r|r|D ]}|                     ||           t          |          S )zCRemove empty TUI ghost sessions (no messages, no title, >24hr old).Q c                     |                      df                                          }d |D             }|r?d                    dt          |          z            }|                      d| d|           |S )NaZ  
                SELECT id FROM sessions
                WHERE source = 'tui'
                  AND title IS NULL
                  AND ended_at IS NOT NULL
                  AND started_at < ?
                  AND NOT EXISTS (
                      SELECT 1 FROM messages WHERE messages.session_id = sessions.id
                  )
            c                 f    g | ].}t          |t          t          f          r|d          n|d         /S )r   id)rs   rt   ru   ).0rs     r)   
<listcomp>zESessionDB.prune_empty_ghost_sessions.<locals>._do.<locals>.<listcomp>  s7    SSS:a%77D1Q44QtWSSSr+   ,?z"DELETE FROM sessions WHERE id IN ())r&   r\   r^   len)r   r{   idsplaceholderscutoffs       r)   r   z1SessionDB.prune_empty_ghost_sessions.<locals>._do  s    << 	! 	 	 %HJJ  TSdSSSC "xxc#hh77HHHH#   Jr+   )r@   rH   _remove_session_filesr   )r(   r   r   removed_idssidr   s        @r)   prune_empty_ghost_sessionsz$SessionDB.prune_empty_ghost_sessions  s    u$	 	 	 	 	& ))#..4" 	>K 	>" > >**<====;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&   rL   dictr(   r   rk   rc   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.
        r   \\\%\%_\_zSSELECT id FROM sessions WHERE id LIKE ? ESCAPE '\' ORDER BY started_at DESC LIMIT 2c                     g | ]
}|d          S )r    r   rc   s     r)   r   z0SessionDB.resolve_session_id.<locals>.<listcomp>  s    >>>Ss4y>>>r+   Nr/   r   )r   rw   r   r#   r&   r\   r   )r(   r   exactescapedrk   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]rV   zB[\u200b-\u200f\u2028-\u202e\u2060-\u2069\ufeff\ufffc\ufff9-\ufffb]z\s+rZ   zTitle too long (z chars, max r   )resubstripr   r   MAX_TITLE_LENGTH
ValueError)r   cleaneds     r)   sanitize_titlezSessionDB.sanitize_title  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&   rL   r   rowcount)r   rk   conflictr   r   s      r)   r   z(SessionDB.set_session_title.<locals>._do-  s     
IJ'  "??,, $X%XXQUXX   \\<
# F ?"r+   r   )r   rH   )r(   r   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&   rL   r   s       r)   get_session_titlezSessionDB.get_session_titleA  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   rk   rc   s       r)   get_session_by_titlezSessionDB.get_session_by_titleJ  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  rw   r   r#   r&   r\   )r(   r   r   r   rk   numbereds         r)   resolve_session_by_titlez"SessionDB.resolve_session_by_titleS  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grouprw   r   r#   r&   r\   maxint)
r(   r
  r  baser   rk   existingmax_numtms
             r)   get_next_title_in_lineagez#SessionDB.get_next_title_in_lineagep  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c                     |}t          d          D ]`}| j        5  | j                            d||f          }|                                }ddd           n# 1 swxY w Y   ||c S |d         }a|S )a  Walk the compression-continuation chain forward and return the tip.

        A compression continuation is a child session where:
        1. The parent's ``end_reason = 'compression'``
        2. The child was created AFTER the parent was ended (started_at >= ended_at)

        The second condition distinguishes compression continuations from
        delegate subagents or branch children, which can also have a
        ``parent_session_id`` but were created while the parent was still live.

        Returns the session_id of the latest continuation in the chain, or the
        input ``session_id`` if it isn't part of a compression chain (or if the
        input itself doesn't exist).
        r   zSELECT id FROM sessions WHERE parent_session_id = ?   AND started_at >= (      SELECT ended_at FROM sessions       WHERE id = ? AND end_reason = 'compression'  ) ORDER BY started_at DESC LIMIT 1Nr   )r2   r   r#   r&   rL   )r(   r   currentr   rk   rc   s         r)   get_compression_tipzSessionDB.get_compression_tip  s      s 	  	 A ( (++7 g&	 	 oo''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( {$iGGs   2AA	 A	   Texclude_sourceslimitoffsetinclude_childrenproject_compression_tipsorder_by_last_activec                 n   g }g }	|s|                     d           |r*|                     d           |	                     |           |rMd                    d |D                       }
|                     d|
 d           |	                    |           |rdd                    |           nd	}|rd
| d| d}|	|	z   ||gz   }	n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<   |                    dd           |                     |           |r|sg }|D ]}|                    d          dk    r|                     |           1|                     |d                   }||d         k    r|                     |           n|                     |          }|s|                     |           t          |          }dD ]}||v r||         ||<   |d         |d<   |                     |           |}|S )u  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.

        With ``project_compression_tips=True`` (default), sessions that are
        roots of compression chains are projected forward to their latest
        continuation — one logical conversation = one list entry, showing the
        live continuation's id/message_count/title/last_active. This prevents
        compressed continuations from being invisible to users while keeping
        delegate subagents and branches hidden. Pass ``False`` to return the
        raw root rows (useful for admin/debug UIs).

        Pass ``order_by_last_active=True`` to sort by most-recent activity
        instead of original conversation start time. For compression chains,
        the "most-recent activity" is taken from the live tip (not the root),
        so an old conversation that was compressed and continued recently
        surfaces in the correct slot. Ordering is computed at SQL level via
        a recursive CTE that walks compression-continuation edges, so LIMIT
        and OFFSET still apply efficiently.
        z(s.parent_session_id IS NULL OR EXISTS (SELECT 1 FROM sessions p            WHERE p.id = s.parent_session_id            AND p.end_reason = 'branched'            AND s.started_at >= p.ended_at))zs.source = ?r   c              3      K   | ]}d V  dS r   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 rV   zr
                WITH RECURSIVE chain(root_id, cur_id) AS (
                    SELECT s.id, s.id FROM sessions s a  
                    UNION ALL
                    SELECT c.root_id, child.id
                    FROM chain c
                    JOIN sessions parent ON parent.id = c.cur_id
                    JOIN sessions child ON child.parent_session_id = c.cur_id
                    WHERE parent.end_reason = 'compression'
                      AND child.started_at >= parent.ended_at
                ),
                chain_max AS (
                    SELECT
                        root_id,
                        MAX(COALESCE(
                            (SELECT MAX(m.timestamp) FROM messages m WHERE m.session_id = cur_id),
                            (SELECT started_at FROM sessions ss WHERE ss.id = cur_id)
                        )) AS effective_last_active
                    FROM chain
                    GROUP BY root_id
                )
                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,
                    COALESCE(cm.effective_last_active, s.started_at) AS _effective_last_active
                FROM sessions s
                LEFT JOIN chain_max cm ON cm.root_id = s.id
                z
                ORDER BY _effective_last_active DESC, s.started_at DESC, s.id DESC
                LIMIT ? OFFSET ?
            a  
                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
                zY
                ORDER BY s.started_at DESC
                LIMIT ? OFFSET ?
            N_preview_raw<   ...preview_effective_last_activer   compressionr   )
r   ended_atr   message_counttool_call_countr   last_activer+  r   r   _lineage_root_id)r]   r^   extendr   r#   r&   r\   r   popr   r   getr  _get_session_rich_row)r(   r   r  r  r  r  r  r   where_clausesr   r   	where_sqlqueryrk   r{   sessionsrc   srawtext	projectedtip_idtip_rowmergedkeys                            r)   list_sessions_richzSessionDB.list_sessions_rich  s   J  	   ?    	"  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	 I	+'7@' 'H I' ' 'ER f_v6FF   E$ MM5&/***Z 	% 	%Z''v66F??$$D	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  	 	CS		A%%++1133C "3B3x#C2uu2F)!)EE*D111OOA $ 	!,< 	!I ) )55&&-77$$Q'''11!D'::QtW$$$$Q'''44V<< $$Q''' a 3 3C
 g~~&-cls-.tW)*  (((( Hs   -0D))D-0D-c                 |   d}| j         5  | j                            ||f          }|                                }ddd           n# 1 swxY w Y   |sdS t	          |          }|                    dd                                          }|r(|dd         }|t          |          dk    rdndz   |d<   nd|d<   |S )zFetch a single session with the same enriched columns as
        ``list_sessions_rich`` (preview + last_active). Returns None if the
        session doesn't exist.
        a  
            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
            WHERE s.id = ?
        Nr(  rV   r)  r*  r+  )r   r#   r&   rL   r   r4  r   r   )r(   r   r9  rk   rc   r;  r<  r=  s           r)   r6  zSessionDB._get_session_rich_rowu  s   
  Z 	$ 	$Z''
}==F//##C	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$  	4IIeeNB''--// 	ss8DCHHrMM55rBAiLLAiLs   1AAAz json:contentc                     |(t          |t          t          t          t          f          r|S 	 | j        t          j        |          z   S # t          t          f$ r t          |          cY S w xY w)ah  Serialize structured (list/dict) message content for sqlite.

        sqlite3 can only bind ``str``, ``bytes``, ``int``, ``float``, and ``None``
        to query parameters. Multimodal messages have ``content`` as a list of
        parts (``[{"type": "text", ...}, {"type": "image_url", ...}]``), which
        raises ``ProgrammingError: Error binding parameter N: type 'list' is
        not supported`` when bound directly.

        Returns the value unchanged when it's already a safe scalar, or a
        sentinel-prefixed JSON string for lists/dicts. Paired with
        :meth:`_decode_content` on read.
        )
rs   r"   bytesr  float_CONTENT_JSON_PREFIXr   r   	TypeErrorr   clsrE  s     r)   _encode_contentzSessionDB._encode_content  st     ?j3sE2JKK?N	 +dj.A.AAA:& 	  	  	 w<<	 s   A
 
 A-,A-c                 2   t          |t                    r|                    | j                  rg	 t	          j        |t          | j                  d                   S # t          j        t          f$ r t          
                    d           |cY S w xY w|S )z;Reverse :meth:`_encode_content`; returns scalars unchanged.NzCFailed to decode JSON-encoded message content; returning raw string)rs   r"   
startswithrI  r   loadsr   JSONDecodeErrorrJ  rM   warningrK  s     r)   _decode_contentzSessionDB._decode_content  s     gs## 	(:(:3;S(T(T 	z'#c.F*G*G*H*H"IJJJ()4   +    s   -A 2BBrole	tool_name
tool_callstool_call_idtoken_countfinish_reason	reasoningreasoning_contentreasoning_detailscodex_reasoning_itemscodex_message_itemsc                   	
 |rt          j        |          nd|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 )NaW  INSERT INTO messages (session_id, role, content, tool_call_id,
                   tool_calls, tool_name, timestamp, token_count, finish_reason,
                   reasoning, reasoning_content, reasoning_details, codex_reasoning_items,
                   codex_message_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   rk   msg_idcodex_items_jsoncodex_message_items_jsonrY  num_tool_callsrZ  r[  reasoning_details_jsonrT  r   stored_contentrX  rW  tool_calls_jsonrU  s      r)   r   z%SessionDB.append_message.<locals>._do  s    \\H " #IKK!%*$, F. %F !!M#Z0    XM   Mr+   )r   r   rM  rs   ru   r   rH   )r(   r   rT  rE  rU  rV  rW  rX  rY  rZ  r[  r\  r]  r^  r   rc  rd  re  rf  rg  rh  s    `` ` `````    @@@@@@r)   append_messagezSessionDB.append_message  sC   2 !+DJ()))&* 	 %/DJ,---*. 	 #-DJ*+++(, 	! 5?H$*Z000D --g66 !0::t0L0LSS___RSN&	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	 &	P ""3'''r+   messagesc                 D      fd}                      |           dS )a   Atomically replace every message for a session.

        Used by transcript-rewrite flows such as /retry, /undo, and /compress.
        The delete + reinsert sequence must commit as one transaction so a
        mid-rewrite failure does not leave SQLite with a partial transcript.
        c                    |                      df           |                      df           t          j                    }d}d}D ]}|                    dd          }|                    d          }|dk    r|                    d          nd }|dk    r|                    d	          nd }|dk    r|                    d
          nd }	|rt          j        |          nd }
|rt          j        |          nd }|	rt          j        |	          nd }|rt          j        |          nd }|                      d|                    |                    d                    |                    d          ||                    d          ||                    d          |                    d          |dk    r|                    d          nd |dk    r|                    d          nd |
||f           |dz  }|)|t          |t                    rt          |          ndz  }|dz  }|                      d||f           d S )N)DELETE FROM messages WHERE session_id = ?GUPDATE sessions SET message_count = 0, tool_call_count = 0 WHERE id = ?r   rT  r   rV  	assistantr\  r]  r^  ag  INSERT INTO messages (session_id, role, content, tool_call_id,
                       tool_calls, tool_name, timestamp, token_count, finish_reason,
                       reasoning, reasoning_content, reasoning_details, codex_reasoning_items,
                       codex_message_items)
                       VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)rE  rW  rU  rX  rY  rZ  r[  r/   gư>zGUPDATE sessions SET message_count = ?, tool_call_count = ? WHERE id = ?)	r&   r@   r5  r   r   rM  rs   ru   r   )r   now_tstotal_messagestotal_tool_callsmsgrT  rV  r\  r]  r^  rf  rc  rd  rh  rj  r(   r   s                 r)   r   z'SessionDB.replace_messages.<locals>._do%  s   LL;j]   LLY  
 Y[[FN  2 2wwvy11 WW\22
DHKDWDWCGG,?$@$@$@]a!8<8K8KCGG3444QU & 7;k6I6ICGG1222t $
 6GPDJ0111D ' :OXDJ4555TX ! 8KTDJ2333PT ) =G"P$*Z"8"8"8DL #,,SWWY-?-?@@//',,..00040C0C,,,8<8K8K 3444QU.(0  . !#)$+5j$+G+GNJQ$ $LLY!1:>    r+   Nr   )r(   r   rj  r   s   ``` r)   replace_messageszSessionDB.replace_messages  sL    C	 C	 C	 C	 C	 C	 C	J 	C     r+   c                    | j         5  | j                            d|f          }|                                }ddd           n# 1 swxY w Y   g }|D ]}t	          |          }d|v 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|                    |           |S )z6Load all messages for a session, ordered by timestamp.zBSELECT * FROM messages WHERE session_id = ? ORDER BY timestamp, idNrE  rV  zDFailed to deserialize tool_calls in get_messages, falling back to [])r   r#   r&   r\   r   rS  r5  r   rP  rQ  rJ  rM   rR  r]   )r(   r   rk   r{   rD   rc   rs  s          r)   get_messageszSessionDB.get_messagesl  sm   Z 	% 	%Z''T F ??$$D	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  
	 
	Cs))CC!%!5!5c)n!E!EIww|$$ ++(,
3|3D(E(EC%%,i8 + + +NN#ijjj(*C%%%+ MM#s#   1AA	A	B;;5C32C3c                 ^   |s|S | j         5  	 | j                            d|f                                          }n# t          $ r |cY cddd           S w xY w||cddd           S |}|h}t          d          D ]}	 | j                            d|f                                          }n # t          $ r |cY c cddd           S w xY w||c cddd           S t          |d          r|d         n|d         }|r||v r|c cddd           S |                    |           	 | j                            d|f                                          }n # t          $ r |cY c cddd           S w xY w||c cddd           S |}	 ddd           n# 1 swxY w Y   |S )u  Redirect a resume target to the descendant session that holds the messages.

        Context compression ends the current session and forks a new child session
        (linked via ``parent_session_id``). The flush cursor is reset, so the
        child is where new messages actually land — the parent ends up with
        ``message_count = 0`` rows unless messages had already been flushed to
        it before compression. See #15000.

        This helper walks ``parent_session_id`` forward from ``session_id`` and
        returns the first descendant in the chain that has at least one message
        row. If the original session already has messages, or no descendant
        has any, the original ``session_id`` is returned unchanged.

        The chain is always walked via the child whose ``started_at`` is
        latest; that matches the single-chain shape that compression creates.
        A depth cap (32) guards against accidental loops in malformed data.
        z3SELECT 1 FROM messages WHERE session_id = ? LIMIT 1N    z]SELECT id FROM sessions WHERE parent_session_id = ? ORDER BY started_at DESC, id DESC LIMIT 1keysr   r   )r   r#   r&   rL   r7   r2   hasattrrv   )	r(   r   rc   r  seenr   	child_rowchild_idmsg_rows	            r)   resolve_resume_session_idz#SessionDB.resolve_resume_session_id  sZ   $  	Z )	# )	#"j((IM  (**   " " "!!!)	# )	# )	# )	# )	# )	# )	# )	#"!)	# )	# )	# )	# )	# )	# )	# )	# !G9D2YY # #& $
 2 2D !
	! !
 hjj I ! & & &%%%%%3)	# )	# )	# )	# )	# )	# )	# )	#0&$%%%7)	# )	# )	# )	# )	# )	# )	# )	#8 /6i.H.HZ9T??iXYl &8t#3#3%%%=)	# )	# )	# )	# )	# )	# )	# )	#> """&"j00M!  hjj G ! & & &%%%%%M)	# )	# )	# )	# )	# )	# )	# )	#J&&#OOQ)	# )	# )	# )	# )	# )	# )	# )	#R #3#!)	# )	# )	# )	# )	# )	# )	# )	# )	# )	# )	# )	# )	# )	# )	#T s   F".=F"A	F"AF"+F".B21F"2C>F"CF"$)F"F"0.EF"E<+F";E<<F"F""F&)F&include_ancestorsc                 V   |g}|r|                      |          }| j        5  d                    d |D                       }| j                            d| dt          |                                                    }ddd           n# 1 swxY w Y   g }|D ]}|                     |d                   }|d         dv r6t          |t                    r!t          |                                          }|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         r|d         |	d<   |d         |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|d         rZ	 t          j        |d                   |	d<   n;# t          j        t          f$ r" t                               d           d|	d<   Y nw xY w|r|                     ||	          rw|                    |	           |S )z
        Load messages in the OpenAI conversation format (role + content dicts).
        Used by the gateway to restore conversation history.
        r   c              3      K   | ]}d V  dS r#  r   r$  s     r)   r%  z9SessionDB.get_messages_as_conversation.<locals>.<genexpr>  s"      #=#=AC#=#=#=#=#=#=r+   zSELECT role, content, tool_call_id, tool_calls, tool_name, finish_reason, reasoning, reasoning_content, reasoning_details, codex_reasoning_items, codex_message_items FROM messages WHERE session_id IN (z) ORDER BY timestamp, idNrE  rT  >   userro  rT  rE  rW  rU  rV  zKFailed to deserialize tool_calls in conversation replay, falling back to []ro  rY  rZ  r[  r\  z=Failed to deserialize reasoning_details, falling back to Noner]  zAFailed to deserialize codex_reasoning_items, falling back to Noner^  z?Failed to deserialize codex_message_items, falling back to None)_session_lineage_root_to_tipr   r^   r#   r&   rt   r\   rS  rs   r"   r   r   r   rP  rQ  rJ  rM   rR  #_is_duplicate_replayed_user_messager]   )
r(   r   r  session_idsr   r{   rj  rc   rE  rs  s
             r)   get_messages_as_conversationz&SessionDB.get_messages_as_conversation  s(    "l 	H;;JGGKZ 	 	88#=#=#=#=#===L:%%] 7C] ] ] k""  hjj 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  -	! -	!C**3y>::G6{333
7C8P8P3*73399;;v;7;;C>" :&).&9N#; 4#&{#3K <  ++(,
3|3D(E(EC%%,i8 + + +NN#pqqq(*C%%%+ 6{k))' @+.+?C({# 8'*;'7C$*+7/23F/GC+,*+ 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< ,- ::59ZDY@Z5[5[122 0)< : : :'hiii591222: ! T%M%MhX[%\%\ OOC    sZ   ABBB3E5F	F	G995H10H1=I5JJJ==5K54K5c                    |s|gS g }|}t                      }| j        5  t          d          D ]}|r||v r n}|                    |           |                    |           | j                            d|f                                          }| n!t          |d          r|d         n|d         }d d d            n# 1 swxY w Y   t          t          |                    p|gS )Nr   z3SELECT parent_session_id FROM sessions WHERE id = ?ry  r   r   )rr   r   r2   rv   r]   r#   r&   rL   rz  ru   reversed)r(   r   chainr  r{  r   rc   s          r)   r  z&SessionDB._session_lineage_root_to_tip	  sa    	 <uuZ 	W 	W3ZZ W W 'T//E!!!W%%%j((IJ  (**  ;E6=c66J6JV#122PSTUPV	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W HUOO$$44s   BCC	Crs  c                    |                     d          dk    rdS |                     d          }t          |t                    r|sdS t          |           D ]}}|                     d          dk    r|                     d          |k    r dS |                     d          dk    r-|                     d          s|                     d          r dS ~dS )NrT  r  FrE  Tro  rV  )r5  rs   r"   r  )rj  rs  rE  prevs       r)   r  z-SessionDB._is_duplicate_replayed_user_message  s    776??f$$5'')$$'3'' 	w 	5X&& 	 	Dxx6))dhhy.A.AW.L.Lttxx;..DHHY4G4G.488T`KaKa.uuur+   r9  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_quotedE  s?      ,,,73}--17777r+   z"[^"]*"z
[+{}()\"^]rZ   z\*+*z(^|\s)\*z\1z(?i)^(AND|OR|NOT)\b\s*rV   z(?i)\s+(AND|OR|NOT)\s*$z\b(\w+(?:[._-]\w+)+)\bz"\1"r  r  )r   Matchr"   r   r   	enumeraterw   )r9  r  	sanitizediquotedr  s        @r)   _sanitize_fts5_queryzSessionDB._sanitize_fts5_query1  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4gyII	 #=11 	C 	CIAv!))/!///6BBII   r+   cpc                     d| cxk    odk    nc p_d| cxk    odk    nc pOd| cxk    odk    nc p?d| cxk    odk    nc p/d	| cxk    od
k    nc pd| cxk    odk    nc pd| cxk    odk    nc S )N N     4  M     ߦ  0  ?0  @0  0  0  0       r   )r  s    r)   _is_cjk_codepointzSessionDB._is_cjk_codepointg  s   "&&&&&&&& '"&&&&&&&&'2((((((((' "&&&&&&&&' "&&&&&&&&	'
 "&&&&&&&&' "&&&&&&&&	(r+   r=  c                    | D ]~}t          |          }d|cxk    rdk    s]n d|cxk    rdk    sNn d|cxk    rdk    s?n d|cxk    rdk    s0n d	|cxk    rd
k    s!n d|cxk    rdk    sn d|cxk    rdk    rn { dS dS )zBCheck if text contains CJK (Chinese, Japanese, Korean) characters.r  r  r  r  r  r  r  r  r  r  r  r  r  r  TF)ord)r=  chr  s      r)   _contains_cjkzSessionDB._contains_cjkq  s     		 		BRB"&&&&&&&&"&&&&&&&&2(((((((("&&&&&&&&"&&&&&&&&"&&&&&&&&"&&&&&&&&&tt 'ur+   c                 :     t           fd|D                       S )zCount CJK characters in text.c              3   `   K   | ](}                     t          |                    $d V  )dS )r/   N)r  r  )r   r  rL  s     r)   r%  z'SessionDB._count_cjk.<locals>.<genexpr>  s<      FFs'<'<SWW'E'EF1FFFFFFr+   )sum)rL  r=  s   ` r)   
_count_cjkzSessionDB._count_cjk  s(     FFFFtFFFFFFr+   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}|                     |          }|r&|                     d                                           }|                     |          }|dk    r|                                }g }|D ]]}|                                dv r|                    |           .|                    d|	                    dd          z   dz              ^d                    |          }dg}|g}|K|                    dd                    d |D                        d           |                    |           |K|                    dd                    d |D                        d           |                    |           |rK|                    d
d                    d |D                        d           |                    |           dd                    |           d}|                    ||g           | j
        5  	 | j                            ||          }d |                                D             }n# t          j        $ r g }Y nw xY wddd           n# 1 swxY w Y   nF|	                    dd          	                    dd          	                    dd          }d g}d| dd| dd| dg}|K|                    dd                    d! |D                        d           |                    |           |K|                    dd                    d" |D                        d           |                    |           |rK|                    d
d                    d# |D                        d           |                    |           d$d                    |           d%}|                    ||g           |g|z   }| j
        5  | j                            ||          }d& |                                D             }ddd           n# 1 swxY w Y   n~| j
        5  	 | j                            ||          }d' |                                D             }n## t          j        $ r g cY cddd           S w xY w	 ddd           n# 1 swxY w Y   |D ]5} 	 | j
        5  | j                            d(| d)         | d)         f          }!g }"|!                                D ]}#|#d*         }$|                     |$          }%t#          |%t$                    rBd+ |%D             }&d                    d, |&D                                                        }'|'pd-}(nt#          |%t&                    r|%}(nd.}(|"                    |#d/         |(dd0         d1           	 ddd           n# 1 swxY w Y   |"| d2<   !# t(          $ r	 g | d2<   Y 3w xY w|D ]} |                     d*d           |S )3a  
        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 ?
        rn   rW   )ANDORNOTro   rZ   zmessages_fts_trigram MATCH ?c              3      K   | ]}d V  dS r#  r   r$  s     r)   r%  z,SessionDB.search_messages.<locals>.<genexpr>  s"      =Y=Yac=Y=Y=Y=Y=Y=Yr+   c              3      K   | ]}d V  dS r#  r   r$  s     r)   r%  z,SessionDB.search_messages.<locals>.<genexpr>  s"      A_A_!#A_A_A_A_A_A_r+   c              3      K   | ]}d V  dS r#  r   r$  s     r)   r%  z,SessionDB.search_messages.<locals>.<genexpr>  s"      ;U;UAC;U;U;U;U;U;Ur+   a  
                    SELECT
                        m.id,
                        m.session_id,
                        m.role,
                        snippet(messages_fts_trigram, 0, '>>>', '<<<', '...', 40) AS snippet,
                        m.content,
                        m.timestamp,
                        m.tool_name,
                        s.source,
                        s.model,
                        s.started_at AS session_started
                    FROM messages_fts_trigram
                    JOIN messages m ON m.id = messages_fts_trigram.rowid
                    JOIN sessions s ON s.id = m.session_id
                    WHERE zX
                    ORDER BY rank
                    LIMIT ? OFFSET ?
                c                 ,    g | ]}t          |          S r   r   r   s     r)   r   z-SessionDB.search_messages.<locals>.<listcomp>
  s    "N"N"N499"N"N"Nr+   r   r   r   r   r   r   z`(m.content LIKE ? ESCAPE '\' OR m.tool_name LIKE ? ESCAPE '\' OR m.tool_calls LIKE ? ESCAPE '\')c              3      K   | ]}d V  dS r#  r   r$  s     r)   r%  z,SessionDB.search_messages.<locals>.<genexpr>  s"      >Z>Zqs>Z>Z>Z>Z>Z>Zr+   c              3      K   | ]}d V  dS r#  r   r$  s     r)   r%  z,SessionDB.search_messages.<locals>.<genexpr>  s"      B`B`13B`B`B`B`B`B`r+   c              3      K   | ]}d V  dS r#  r   r$  s     r)   r%  z,SessionDB.search_messages.<locals>.<genexpr>  s"      <V<VQS<V<V<V<V<V<Vr+   a  
                    SELECT m.id, m.session_id, m.role,
                           substr(m.content,
                                  max(1, instr(m.content, ?) - 40),
                                  120) AS snippet,
                           m.content, m.timestamp, m.tool_name,
                           s.source, s.model, s.started_at AS session_started
                    FROM messages m
                    JOIN sessions s ON s.id = m.session_id
                    WHERE zd
                    ORDER BY m.timestamp DESC
                    LIMIT ? OFFSET ?
                c                 ,    g | ]}t          |          S r   r  r   s     r)   r   z-SessionDB.search_messages.<locals>.<listcomp>,  s    KKKStCyyKKKr+   c                 ,    g | ]}t          |          S r   r  r   s     r)   r   z-SessionDB.search_messages.<locals>.<listcomp>5  s    FFFStCyyFFFr+   a  WITH target AS (
                               SELECT session_id, timestamp, id
                               FROM messages
                               WHERE id = ?
                           )
                           SELECT role, content
                           FROM (
                               SELECT m.id, m.timestamp, m.role, m.content
                               FROM messages m
                               JOIN target t ON t.session_id = m.session_id
                               WHERE (m.timestamp < t.timestamp)
                                  OR (m.timestamp = t.timestamp AND m.id < t.id)
                               ORDER BY m.timestamp DESC, m.id DESC
                               LIMIT 1
                           )
                           UNION ALL
                           SELECT role, content
                           FROM messages
                           WHERE id = ?
                           UNION ALL
                           SELECT role, content
                           FROM (
                               SELECT m.id, m.timestamp, m.role, m.content
                               FROM messages m
                               JOIN target t ON t.session_id = m.session_id
                               WHERE (m.timestamp > t.timestamp)
                                  OR (m.timestamp = t.timestamp AND m.id > t.id)
                               ORDER BY m.timestamp ASC, m.id ASC
                               LIMIT 1
                           )r   rE  c                     g | ]F}t          |t                    r/|                    d           dk    0|                    dd          GS )typer=  rV   )rs   r   r5  )r   ps     r)   r   z-SessionDB.search_messages.<locals>.<listcomp>d  sW     * * *67#-a#6#6*;<55==F;R;R !"fb 1 1;R;R;Rr+   c              3      K   | ]}||V  	d S r   r   )r   r  s     r)   r%  z,SessionDB.search_messages.<locals>.<genexpr>h  s'      +G+G!Q+GA+G+G+G+G+G+Gr+   z[multimodal content]rV   rT     r  context)r   r  r^   r]   r3  r  r  splitupperrw   r   r#   r&   r\   r    r:   rS  rs   ru   r"   r7   r4  ))r(   r9  r  r  r  r  r  r7  r   source_placeholdersexclude_placeholdersrole_placeholdersr8  r   is_cjk	raw_query	cjk_counttokensri   toktrigram_query	tri_where
tri_paramstri_sql
tri_cursorr   r   
like_wherelike_paramslike_sqllike_cursorrk   r  
ctx_cursorcontext_msgsr   r<  decoded
text_partsr=  r+  s)                                            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&&&   : ##E** `	GC((..00I	22IA~~ #**! I ICyy{{&:::S))))S3;;sD+A+A%AC%GHHHH #;<	$1?
 ,$$%\SXX=Y=Y==Y=Y=Y5Y5Y%\%\%\]]]%%m444".$$%bA_A_A_A_A_9_9_%b%b%bccc%%o666 3$$%X388;U;U;U;U;U3U3U%X%X%XYYY%%k222 #<<	22  & !!5&/222Z O OO%)Z%7%7%L%L
 #O"N
8K8K8M8M"N"N"N #3 % % %"$%O O O O O O O O O O O O O O O $++D&99AA#uMMUUVY[`aa D  E
%3^^^^^^^^^^^$T ,%%&]chh>Z>ZM>Z>Z>Z6Z6Z&]&]&]^^^&&}555".%%&c#((B`B`P_B`B`B`:`:`&c&c&cddd&&777 4%%&YCHH<V<V+<V<V<V4V4V&Y&Y&YZZZ&&{333 #<<
33   ""E6?333(kK7Z L L"&*"4"4X{"K"KKKKK4H4H4J4JKKKGL L L L L L L L L L L L L L L  G GG!Z//V<<F
 GFFOO4E4EFFFGG	 /   IIG G G G G G G G G G G G G G G G G G G G G G G  :	& :	&E9&Z 5 5!%!3!3 < teDk2? "  "JB $&L'0022  	l"&"6"6s";"; &gt44 
)* *;B* * *J $'88+G+Gz+G+G+G#G#G#M#M#O#OD&*&D.DGG'55 )&-GG&(G$++%&vY74C4=II   !G5 5 5 5 5 5 5 5 5 5 5 5 5 5 5l $0i   & & &#%i   &  	' 	'EIIi&&&&s   !O #N>O N1.O 0N11O  OO:V		VVXW9XW8)X7W88XXX\6 C7\$\6$\(	(\6+\(	,\66]	]	c                    d}| 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 )zList sessions, optionally filtered by source.

        Returns rows enriched with a computed ``last_active`` column (latest
        message timestamp for the session, falling back to ``started_at``),
        ordered by most-recently-used first.
        zSELECT s.*, COALESCE(m.last_active, s.started_at) AS last_active FROM sessions s LEFT JOIN (SELECT session_id, MAX(timestamp) AS last_active FROM messages GROUP BY session_id) m ON m.session_id = s.id z[WHERE s.source = ? ORDER BY last_active DESC, s.started_at DESC, s.id DESC LIMIT ? OFFSET ?zHORDER BY last_active DESC, s.started_at DESC, s.id DESC LIMIT ? OFFSET ?c                 ,    g | ]}t          |          S r   r  r   s     r)   r   z-SessionDB.search_sessions.<locals>.<listcomp>  s    ;;;#DII;;;r+   N)r   r#   r&   r\   )r(   r   r  r  select_with_last_activerk   s         r)   search_sessionszSessionDB.search_sessions{  s   * 	  Z 	< 	< ++. _ _ _ UF+	  ++. _ _ _FO 
 <;):):;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   A"A99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 )z.Count sessions, optionally filtered by source.z.SELECT COUNT(*) FROM sessions WHERE source = ?zSELECT COUNT(*) FROM sessionsr   Nr  )r(   r   rk   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(   r   rk   s      r)   r/  zSessionDB.message_count  s    Z 	( 	( M++H:-  ++,KLL??$$Q'	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(r  c                 n    |                      |          }|sdS |                     |          }i |d|iS )z8Export a single session with all its messages as a dict.Nrj  )r   rv  )r(   r   sessionrj  s       r)   export_sessionzSessionDB.export_session  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 )r   r  r   rj  )r  rv  r]   )r(   r   r:  resultsr  rj  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 )Nrm  rn  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                 "   | dS dD ]2}| | | z  }	 |                     d           ## t          $ r Y /w xY w	 |                     d| d          D ])}	 |                     d           # t          $ r Y &w xY wdS # t          $ r Y dS w xY w)aH  Remove on-disk transcript files for a session.

        Cleans up ``{session_id}.json``, ``{session_id}.jsonl``, and any
        ``request_dump_{session_id}_*.json`` files left by the gateway.
        Silently skips files that don't exist and swallows OSError so a
        filesystem hiccup never blocks a DB operation.
        N)z.jsonz.jsonlT)
missing_okrequest_dump_z_*.json)unlinkOSErrorglob)r   r   suffixr  s       r)   r   zSessionDB._remove_session_files  s    F) 	 	F*6f666AD))))   	!&&'Jz'J'J'JKK  HHH----   D 
  	 	 	DD	sA   *
77B  A.-B  .
A;8B  :A;;B   
BBc                 l    fd}|                      |          }|r|                     |           |S )a  Delete 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.
        When *sessions_dir* is provided, also removes on-disk transcript
        files (``.json`` / ``.jsonl`` / ``request_dump_*``) for the deleted
        session. 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 = ?rm  !DELETE FROM sessions WHERE id = ?T)r&   rL   )r   rk   r   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+   )rH   r   )r(   r   r   r   deleteds    `   r)   delete_sessionzSessionDB.delete_session  sU    	 	 	 	 	  %%c** 	A&&|Z@@@r+   Z   older_than_daysc                     t          j                     |dz  z
  g fd}|                     |          }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.  When *sessions_dir* is provided, also removes
        on-disk transcript files (``.json`` / ``.jsonl`` /
        ``request_dump_*``) for every pruned session, outside the DB
        transaction.
        r   c                    r|                      df          }n|                      df          }t          d |                                D                       }|sdS d                    dt	          |          z            }|                      d| dt          |                     |D ]E}|                      d	|f           |                      d
|f                               |           Ft	          |          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>8  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   rm  r  )r&   rr   r\   r^   r   ru   r]   )r   rk   r  r   r   r   r   r   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?/;? ? ?[!!   # ( (H3&QQQ@3&III""3''''{###r+   )r@   rH   r   )	r(   r  r   r   r   countr   r   r   s	     `    @@r)   prune_sessionszSessionDB.prune_sessions  s     % 78!#	$ 	$ 	$ 	$ 	$ 	$ 	$> ##C(( 	: 	:C&&|S9999r+   rB  c                     | j         5  | j                            d|f                                          }ddd           n# 1 swxY w Y   |dS t	          |t
          j                  r|d         n|d         S )z1Read a value from the state_meta key/value store.z*SELECT value FROM state_meta WHERE key = ?Nvaluer   )r   r#   r&   rL   rs   r    r$   )r(   rB  rc   s      r)   get_metazSessionDB.get_metaS  s    Z 	 	*$$<sf hjj 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ;4)#w{;;Gs7||QGs   /AA
Ar
  c                 @    fd}|                      |           dS )z0Write a value to the state_meta key/value store.c                 8    |                      df           d S )NzgINSERT INTO state_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.valuer   )r   rB  r
  s    r)   r   zSessionDB.set_meta.<locals>._do_  s0    LLHe    r+   Nr   )r(   rB  r
  r   s    `` r)   set_metazSessionDB.set_meta]  s>    	 	 	 	 	 	 	C     r+   c                     | j         5  	 | 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 )ui  Run VACUUM to reclaim disk space after large deletes.

        SQLite does not shrink the database file when rows are deleted —
        freed pages just get reused on the next insert. After a prune that
        removed hundreds of sessions, the file stays bloated unless we
        explicitly VACUUM.

        VACUUM rewrites the entire DB, so it's expensive (seconds per
        100MB) and cannot run inside a transaction. It also acquires an
        exclusive lock, so callers must ensure no other writers are
        active. Safe to call at startup before the gateway/CLI starts
        serving traffic.
        zPRAGMA wal_checkpoint(TRUNCATE)VACUUMN)r   r#   r&   r7   rQ   s    r)   vacuumzSessionDB.vacuumi  s     Z 	) 	)
""#DEEEE   Jx(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s,   A%A
2A2AA #A    retention_daysmin_interval_hoursr  c                    dddd}	 |                      d          }t          j                    }|r;	 t          |          }||z
  |dz  k     rd|d<   |S n# t          t          f$ r Y nw xY w|                     ||          }	|	|d	<   |rS|	dk    rM	 |                                  d|d
<   n2# t          $ r%}
t          	                    d|
           Y d}
~
nd}
~
ww xY w| 
                    dt          |                     |	dk    r't                              d|	||d
         rdnd           nD# t          $ r7}
t          	                    d|
           t          |
          |d<   Y d}
~
nd}
~
ww xY w|S )u  Idempotent auto-maintenance: prune old sessions + optional VACUUM.

        Records the last run timestamp in state_meta so subsequent calls
        within ``min_interval_hours`` no-op. Designed to be called once at
        startup from long-lived entrypoints (CLI, gateway, cron scheduler).

        When *sessions_dir* is provided, on-disk transcript files
        (``.json`` / ``.jsonl`` / ``request_dump_*``) for pruned sessions
        are removed as part of the same sweep (issue #3015).

        Never raises. On any failure, logs a warning and returns a dict
        with ``"error"`` set.

        Returns a dict with keys:
          - ``"skipped"`` (bool) — true if within min_interval_hours of last run
          - ``"pruned"`` (int)   — number of sessions deleted
          - ``"vacuumed"`` (bool) — true if VACUUM ran
          - ``"error"`` (str, optional) — present only on failure
        Fr   )skippedprunedvacuumedlast_auto_prunei  Tr  )r  r   r  r  zstate.db VACUUM failed: %sNzDstate.db auto-maintenance: pruned %d session(s) older than %d days%sz	 + VACUUMrV   z$state.db auto-maintenance failed: %serror)r  r@   rH  rJ  r   r  r  r7   rM   rR  r  r"   info)r(   r  r  r  r   rD   last_rawnowlast_tsr  rE   s              r)   maybe_auto_prune_and_vacuumz%SessionDB.maybe_auto_prune_and_vacuum  s   4 .3aU!S!S*	'}}%677H)++C #HooGW}'9D'@@@,0y)% A ":.   D (( .) )  F  &F8  F&1**FKKMMM)-F:&&  F F FNN#?EEEEEEEEF
 MM+SXX666zzZ"#)*#5=KK2	    	' 	' 	'NNA3GGG!#hhF7OOOOOO	'
 se   *D/ !A D/ A*'D/ )A**'D/ B, +D/ ,
C6CD/ CAD/ /
E09-E++E0r   )r-   N)NNNNN)r   r   Nr   r   r   NNNNNNNNr   F)r   N)NNr  r   FTF)NNNNNNNNNNN)F)NNNr  r   )Nr  r   )r  NN)r  r  TN)O__name__
__module____qualname____doc__r3   r>   r?   r8   r   r*   r   r    
Connectionr   rH   r9   rP   staticmethodr"   r   rj   Cursorr~   r'   r   r   r   r   r   r   r  r
   rH  boolr   r   r   r   r   r   r   r   r  r  r	  r  r  r	   rC  r6  rI  classmethodrM  rS  ri  rt  rv  r  r  r  r  r  r  r  r  r  r  r  r/  r  r  r  r   r  r  r  r  r  r  r   r+   r)   r   r      s          !#     42
7+=*>*A!B 2
q 2
 2
 2
 2
h   *" " " (# ($sDcN7J2K ( ( ( \(T* *D * * * *X@ @ @T '+!!%! !! ! 	!
 38n! ! ! ! 
! ! ! !: c     !c !s !t ! ! ! !$! ! ! ! ! !!s !3 !4 ! ! ! ! !""# !.2+/%)%))-*.*.&*%\! \!\! \! 	\!
 \! \!  \! \! %UO\! "%\! c]\! c]\! "#\! #3-\! #3-\!  sm!\!" #\!$ %\!& 
'\! \! \! \!B  		 		 	 		 
	 	 	 	   7G  SV        <*c *htCH~.F * * * *s x}    8 )hsm ) ) ) ) \)VC      :-C -HSM - - - -*# *(4S>2J * * * *c hsm    :!(C !(C !( !( !( !(F"c "hsm " " " "L %)!&)-%*| || c| 	|
 | | #'| #| 
d38n	| | | ||! !c3h8P ! ! ! !V ' c  c       [ * c c    ["  !!%!%%)#'U( U(U( U( 	U(
 U( U( U( U( U( U( U( U(  #U( !U( 
U( U( U( U(nM!3 M!$tCH~:N M!SW M! M! M! M!^s tDcN/C    ,?C ?C ? ? ? ?D :?D DD26D	d38n	D D D DL5s 5tCy 5 5 5 5, d4S>6J QUVY[^V^Q_ dh    \" 2!C 2!C 2! 2! 2! \2!j (c (d ( ( ( \( C D    \ Gc Gc G G G [G $(%)!%t tt Cyt c	t
 #Yt t t 
d38n	t t t tp 	"< "<"< "< 	"<
 
d38n	"< "< "< "<P	( 	(C 	(3 	( 	( 	( 	(	( 	( 	(s 	( 	( 	( 	(1 1$sCx.1I 1 1 1 1
 
 
T#s(^0D 
 
 
 

! 
! 
! 
! 
! 
! HTN  PT    \: (,     tn  
	       H  "'+	5 55 5 tn	5
 
5 5 5 5rHC HHSM H H H H!C ! ! ! ! ! !) ) ) )2 !"$'+G GG  G 	G
 tnG 
c3hG G G G G Gr+   r   )r#  r   loggingr<   r   r    r   r@   pathlibr   agent.memory_managerr   hermes_constantsr   typingr   r   r   r	   r
   r   	getLoggerr   rM   r   r   r   rp   r   r   r   r   r+   r)   <module>r/     sA       				             1 1 1 1 1 1 , , , , , , ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?		8	$	$GCLL!/##j0?
B:6h  h  h  h  h  h  h  h  h  h r+   