d15c61b5dc
hermes_state.py 17,220 -> 6,442 LOC. Behavior-neutral: every moved body is AST-identical to the original, verified per extraction. SessionDB core - _write_sql / _write_rowcount / _read_one / _read_all replace ~120 copies of the `def _do(conn): conn.execute(...)` + `_execute_write(_do)` and `with self._read_ctx() as conn: row = conn.execute(...).fetchone()` shapes. - _set_lineage_column replaces four copies of the recursive compression-lineage UPDATE (archived / pinned / hidden / last_read_at). - _read_session_number unifies the three compression counter readers. - Dead (zero refs repo-wide): restore_rewound, delete_gateway_routing_entries, _is_duplicate_replayed_user_message, SessionPortabilityMixin.get_first_assistant_text. New mixins bound onto SessionDB via the MRO (logger name stays "hermes_state"): hermes_state_messages SessionMessagesMixin 48 methods hermes_state_compression SessionCompressionMixin 30 hermes_state_gateway SessionGatewayMixin 26 hermes_state_maintenance SessionMaintenanceMixin 13 hermes_state_usage SessionUsageMixin 12 hermes_state_titles SessionTitlesMixin 13 hermes_state_telegram SessionTelegramTopicsMixin 11 Origin-internal symbols resolve through a lazy `from hermes_state import ...` inside the few methods that need them (no import cycle). New free-function modules, every name re-imported into hermes_state so `hermes_state.<name>` (and test monkeypatches on it) keep working; intra-module calls to patched helpers go through the lazy origin import: hermes_state_repair repair/backup/preflight (43 defs) hermes_state_wal journal-mode / PRAGMA policy (33 defs) hermes_state_dbfile header probes, zeroed-db quarantine, stats, holders (21 defs) Existing mixins: search — shared FTS MATCH/LIKE builders, unified rebuild status/step/finish engines, state_meta helpers; schema — one legacy/v23 FTS init branch, shared _live_pk_columns, Row/tuple dual access dropped; portability — shared _PREVIEW_RAW_SUBQUERY_SQL and _rich_row; common — single stat_db_file_identity (was 3 copies), AUTO_VACUUM_MIN_FREELIST_RATIO. Docstrings/comments hand-compacted (AST-identical) keeping every invariant, ordering rule, failure mode and WHY. Schema SQL, migration order and PRAGMAs untouched. test_repair_path_has_no_bare_connects repointed to hermes_state_repair.
306 lines
12 KiB
Python
306 lines
12 KiB
Python
"""Session title mixin for SessionDB: sanitizing, auto/user provenance
|
|
ranking, and lineage-aware lookups."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import re
|
|
from typing import Any, Dict, Optional
|
|
|
|
from agent.message_sanitization import _sanitize_surrogates
|
|
from hermes_state_common import _COMPRESSION_CHILD_SQL, escape_like as _escape_like
|
|
|
|
# caplog tests pin the "hermes_state" logger name.
|
|
logger = logging.getLogger("hermes_state")
|
|
|
|
|
|
class SessionTitlesMixin:
|
|
"""Sanitizing, ranking auto/user titles, lineage-aware lookups."""
|
|
|
|
@classmethod
|
|
def _title_rank(cls, source: Optional[str]) -> int:
|
|
"""Rank a stored title_source.
|
|
|
|
NULL (pre-provenance rows) is indistinguishable from a manual ``/title``
|
|
of that era, so it ranks as ``user``: auto-titling only ever fills
|
|
genuinely empty legacy titles.
|
|
"""
|
|
if source is None:
|
|
return cls._TITLE_SOURCE_RANK[cls.TITLE_SOURCE_USER]
|
|
return cls._TITLE_SOURCE_RANK.get(str(source), 0)
|
|
|
|
@staticmethod
|
|
def sanitize_title(title: Optional[str]) -> Optional[str]:
|
|
"""Strip control/zero-width/bidi chars, collapse whitespace, normalize
|
|
empty to None. Raises ValueError if longer than MAX_TITLE_LENGTH
|
|
after cleaning."""
|
|
from hermes_state import SessionDB
|
|
if not title:
|
|
return None
|
|
|
|
# Lone surrogates cannot be bound by sqlite3 (UnicodeEncodeError).
|
|
title = _sanitize_surrogates(title)
|
|
|
|
# ASCII controls, keeping \t \n \r so the whitespace collapse below
|
|
# turns them into spaces.
|
|
cleaned = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '', title)
|
|
|
|
# Zero-width, bidi override, object-replacement, interlinear annotation.
|
|
cleaned = re.sub(
|
|
r'[\u200b-\u200f\u2028-\u202e\u2060-\u2069\ufeff\ufffc\ufff9-\ufffb]',
|
|
'', cleaned,
|
|
)
|
|
|
|
cleaned = re.sub(r'\s+', ' ', cleaned).strip()
|
|
|
|
if not cleaned:
|
|
return None
|
|
|
|
if len(cleaned) > SessionDB.MAX_TITLE_LENGTH:
|
|
raise ValueError(
|
|
f"Title too long ({len(cleaned)} chars, max {SessionDB.MAX_TITLE_LENGTH})"
|
|
)
|
|
|
|
return cleaned
|
|
|
|
def _is_compression_ancestor(
|
|
self, conn, *, ancestor_id: str, descendant_id: str
|
|
) -> bool:
|
|
"""True if *ancestor_id* is a compression predecessor of *descendant_id*.
|
|
|
|
Uses the canonical continuation edge ``_COMPRESSION_CHILD_SQL`` (parent
|
|
ended with ``end_reason = 'compression'`` and child started at/after its
|
|
``ended_at``), which excludes delegate/branch children that also carry
|
|
``parent_session_id``. One recursive CTE so the edge is defined once.
|
|
"""
|
|
if not ancestor_id or not descendant_id or ancestor_id == descendant_id:
|
|
return False
|
|
edge = _COMPRESSION_CHILD_SQL.format(a="child")
|
|
row = conn.execute(
|
|
f"""
|
|
WITH RECURSIVE ancestors(id) AS (
|
|
SELECT ?
|
|
UNION
|
|
SELECT parent.id
|
|
FROM ancestors a
|
|
JOIN sessions child ON child.id = a.id
|
|
JOIN sessions parent ON parent.id = child.parent_session_id
|
|
WHERE {edge}
|
|
)
|
|
SELECT 1 FROM ancestors WHERE id = ? AND id != ? LIMIT 1
|
|
""",
|
|
(descendant_id, ancestor_id, descendant_id),
|
|
).fetchone()
|
|
return row is not None
|
|
|
|
def _set_session_title(
|
|
self,
|
|
session_id: str,
|
|
title: str,
|
|
*,
|
|
source: str,
|
|
) -> bool:
|
|
"""Write a title, enforcing provenance precedence.
|
|
|
|
A ``user`` write always lands. ``derived``/``llm`` land only when the
|
|
row is untitled or holds strictly lower authority, so derived upgrades
|
|
to llm exactly once, nothing overwrites a user name, and re-running the
|
|
titler on an llm row is a no-op (stops sessions renaming themselves).
|
|
No writer may move a hidden canonical Bot Chat off its title.
|
|
|
|
Read and write are one compare-and-swap in a single transaction, so a
|
|
manual ``/title`` racing an in-flight generation is not clobbered.
|
|
"""
|
|
title = self.sanitize_title(title)
|
|
is_user = source == self.TITLE_SOURCE_USER
|
|
new_rank = self._title_rank(source) if not is_user else None
|
|
|
|
def _do(conn):
|
|
current = conn.execute(
|
|
"SELECT title, title_source, hidden FROM sessions WHERE id = ?",
|
|
(session_id,),
|
|
).fetchone()
|
|
if current is None:
|
|
return 0
|
|
# The canonical Bot Chat's NAME is its identity: Bot Mode resolves it
|
|
# by exact-title lookup on every open, so a rename orphans the whole
|
|
# conversation (next open mints an empty replacement and UNIQUE(title)
|
|
# blocks renaming back). Refuse here, the single write path every
|
|
# surface funnels through. Hidden is the discriminator: canonical
|
|
# chats are born hidden; a visible session merely named "Bot Chat"
|
|
# stays renameable. Provenance-blind so the auto-titler no-ops too.
|
|
if (
|
|
(current["title"] or "") == self.CANONICAL_BOT_CHAT_TITLE
|
|
and bool(current["hidden"])
|
|
and title != self.CANONICAL_BOT_CHAT_TITLE
|
|
):
|
|
if is_user:
|
|
raise ValueError(
|
|
"This is the bot's canonical Bot Chat — its name is its "
|
|
"identity, and renaming it would orphan the conversation. "
|
|
"To start fresh, create a new bot instead."
|
|
)
|
|
return 0
|
|
if not is_user and current["title"] is not None:
|
|
if self._title_rank(current["title_source"]) >= new_rank:
|
|
return 0
|
|
|
|
if title:
|
|
cursor = conn.execute(
|
|
"SELECT id FROM sessions WHERE title = ? AND id != ?",
|
|
(title, session_id),
|
|
)
|
|
conflict = cursor.fetchone()
|
|
if conflict:
|
|
conflict_id = conflict["id"]
|
|
# If the conflicting holder is a hidden compressed ancestor
|
|
# of this continuation, the user cannot free the title, so
|
|
# transfer it onto the tip. Uniqueness and lineage are kept.
|
|
if self._is_compression_ancestor(
|
|
conn, ancestor_id=conflict_id, descendant_id=session_id
|
|
):
|
|
conn.execute(
|
|
"UPDATE sessions SET title = NULL WHERE id = ?",
|
|
(conflict_id,),
|
|
)
|
|
else:
|
|
raise ValueError(
|
|
f"Title '{title}' is already in use by session {conflict_id}"
|
|
)
|
|
# CAS on the values just read (``IS`` is NULL-safe): a concurrent
|
|
# write between the SELECT and here loses instead of being overwritten.
|
|
cursor = conn.execute(
|
|
"UPDATE sessions SET title = ?, title_source = ? "
|
|
"WHERE id = ? AND title IS ? AND title_source IS ?",
|
|
(
|
|
title,
|
|
source if title else None,
|
|
session_id,
|
|
current["title"],
|
|
current["title_source"],
|
|
),
|
|
)
|
|
return cursor.rowcount
|
|
|
|
rowcount = self._execute_write(_do)
|
|
return rowcount > 0
|
|
|
|
def set_session_title(self, session_id: str, title: str) -> bool:
|
|
"""Set a title on the user's behalf (``user`` provenance; auto-titling
|
|
never replaces it). Empty clears the title. Raises ValueError on a
|
|
title conflict or validation failure. Automatic callers must use
|
|
:meth:`set_auto_title`."""
|
|
return self._set_session_title(
|
|
session_id, title, source=self.TITLE_SOURCE_USER
|
|
)
|
|
|
|
def set_auto_title(self, session_id: str, title: str, *, source: str) -> bool:
|
|
"""Set an automatic title; False (untouched) when a higher-authority
|
|
title already holds the row."""
|
|
if source not in (self.TITLE_SOURCE_DERIVED, self.TITLE_SOURCE_LLM):
|
|
raise ValueError(f"invalid automatic title source: {source!r}")
|
|
return self._set_session_title(session_id, title, source=source)
|
|
|
|
def set_auto_title_if_empty(self, session_id: str, title: str) -> bool:
|
|
"""Back-compat shim (third-party plugins reference it by name); new
|
|
code calls :meth:`set_auto_title` with an explicit source."""
|
|
return self.set_auto_title(
|
|
session_id, title, source=self.TITLE_SOURCE_LLM
|
|
)
|
|
|
|
def get_session_title(self, session_id: str) -> Optional[str]:
|
|
"""Get the title for a session, or None."""
|
|
with self._read_ctx() as conn:
|
|
cursor = conn.execute(
|
|
"SELECT title FROM sessions WHERE id = ?", (session_id,)
|
|
)
|
|
row = cursor.fetchone()
|
|
return row["title"] if row else None
|
|
|
|
def get_session_title_source(self, session_id: str) -> Optional[str]:
|
|
"""Get the provenance of a session's title, or None when untitled."""
|
|
with self._read_ctx() as conn:
|
|
cursor = conn.execute(
|
|
"SELECT title, title_source FROM sessions WHERE id = ?",
|
|
(session_id,),
|
|
)
|
|
row = cursor.fetchone()
|
|
if not row or row["title"] is None:
|
|
return None
|
|
return row["title_source"]
|
|
|
|
def set_session_title_source(self, session_id: str, source: str) -> bool:
|
|
"""Overwrite a title's provenance without touching the text: a title
|
|
copied across a compression rotation keeps the original's authority."""
|
|
if source not in self._TITLE_SOURCE_RANK:
|
|
raise ValueError(f"invalid title source: {source!r}")
|
|
|
|
return self._write_rowcount(
|
|
"UPDATE sessions SET title_source = ? "
|
|
"WHERE id = ? AND title IS NOT NULL",
|
|
(source, session_id),
|
|
) > 0
|
|
|
|
def get_session_by_title(self, title: str) -> Optional[Dict[str, Any]]:
|
|
"""Look up a session by exact title. Returns session dict or None."""
|
|
with self._read_ctx() as conn:
|
|
cursor = conn.execute(
|
|
"SELECT s.*, "
|
|
"COALESCE(sp.prompt, s.system_prompt) AS _system_prompt_resolved "
|
|
"FROM sessions s "
|
|
"LEFT JOIN system_prompts sp ON sp.hash = s.system_prompt_hash "
|
|
"WHERE s.title = ?",
|
|
(title,),
|
|
)
|
|
row = cursor.fetchone()
|
|
return self._session_row_dict(row) if row else None
|
|
|
|
def resolve_session_by_title(self, title: str) -> Optional[str]:
|
|
"""Resolve a title to a session ID, preferring the latest "title #N"
|
|
continuation over the exact match."""
|
|
exact = self.get_session_by_title(title)
|
|
|
|
# Escape LIKE wildcards so "%"/"_" in titles cannot false-match.
|
|
escaped = _escape_like(title)
|
|
with self._read_ctx() as conn:
|
|
cursor = conn.execute(
|
|
"SELECT id, title, started_at FROM sessions "
|
|
"WHERE title LIKE ? ESCAPE '\\' ORDER BY started_at DESC",
|
|
(f"{escaped} #%",),
|
|
)
|
|
numbered = cursor.fetchall()
|
|
|
|
if numbered:
|
|
return numbered[0]["id"]
|
|
elif exact:
|
|
return exact["id"]
|
|
return None
|
|
|
|
def get_next_title_in_lineage(self, base_title: str) -> str:
|
|
"""Next title in a lineage ("my session" → "my session #2"): strip any
|
|
" #N" suffix, then increment the highest existing number."""
|
|
match = re.match(r'^(.*?) #(\d+)$', base_title)
|
|
if match:
|
|
base = match.group(1)
|
|
else:
|
|
base = base_title
|
|
|
|
escaped = _escape_like(base)
|
|
with self._read_ctx() as conn:
|
|
cursor = conn.execute(
|
|
"SELECT title FROM sessions WHERE title = ? OR title LIKE ? ESCAPE '\\'",
|
|
(base, f"{escaped} #%"),
|
|
)
|
|
existing = [row["title"] for row in cursor.fetchall()]
|
|
|
|
if not existing:
|
|
return base
|
|
|
|
max_num = 1 # The unnumbered original counts as #1
|
|
for t in existing:
|
|
m = re.match(r'^.* #(\d+)$', t)
|
|
if m:
|
|
max_num = max(max_num, int(m.group(1)))
|
|
|
|
return f"{base} #{max_num + 1}"
|