hermes_state_profile_repair.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. """Crossed-profile durable state in ONE ``state.db`` — finders and fixers for
  2. ``hermes sessions repair-profiles`` (#88715 PR-6).
  3. Every ``state.db`` belongs to exactly one profile (``<root>/state.db`` → ``default``,
  4. ``<root>/profiles/<name>/state.db`` → ``name``) and every gateway session key encodes the profile
  5. that owns the conversation (``agent:main:…`` for the default, ``agent:<name>:…`` for a named one).
  6. The per-profile store model (#88734) is forward-only: it routes NEW writes to the right store but
  7. never touched rows that had already landed in the wrong one, and the identity fences
  8. (``_INHERIT_PARENT_META_SQL``, ``_recovered_row_allowed_for_active_profile``) only refuse to
  9. *widen* damage that already exists. This module is the backward-looking half: it names each crossed
  10. row and can settle it.
  11. Store-level only. Which profiles exist, which store owns which home, whether a live gateway holds the
  12. routing index in memory, and the JSON files outside ``state.db`` are the orchestrator's concern
  13. (``hermes_cli/sessions_repair_profiles.py``).
  14. Moving a session between two stores is two single-store transactions — copy into the target, then
  15. delete from the source — because a crash between them leaves a *duplicate*, which the next run
  16. settles idempotently (:meth:`import_moved_session` reports ``present``; the delete only proceeds when
  17. the target holds at least as many messages). The reverse order would lose the row.
  18. """
  19. from __future__ import annotations
  20. import logging
  21. from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
  22. from hermes_state_common import (
  23. _id_chunks,
  24. _placeholders as _session_ids_placeholders,
  25. emit_session_deleted,
  26. )
  27. logger = logging.getLogger(__name__)
  28. def session_key_profile(session_key: Any) -> Optional[str]:
  29. """Profile encoded in an ``agent:<ns>:…`` gateway key (``default`` for ``main``), or None for a
  30. keyless row (CLI/subagent lineage) or a key another producer minted (hosted rooms, tests)."""
  31. if not isinstance(session_key, str):
  32. return None
  33. parts = session_key.split(":")
  34. if len(parts) < 3 or parts[0] != "agent" or not parts[1]:
  35. return None
  36. from hermes_state_keys import profile_from_session_key_namespace
  37. return profile_from_session_key_namespace(parts[1])
  38. def _stored_profile(value: Any) -> Optional[str]:
  39. """``sessions.profile_name`` as a comparable label; NULL/blank is "unowned", not a crossing."""
  40. name = str(value or "").strip()
  41. return name or None
  42. def _table_columns(conn, table: str) -> list[str]:
  43. return [row[1] for row in conn.execute(f"PRAGMA table_info('{table}')")]
  44. # Columns a moved message must NOT carry over verbatim: ``id`` is reassigned by the target's
  45. # AUTOINCREMENT and ``display_order`` points at a message id of the SOURCE store — left NULL, the
  46. # ``messages_display_order_insert`` trigger recomputes it from ``display_identity`` (a content hash,
  47. # store-independent) or the new id.
  48. _MESSAGE_MOVE_SKIP = frozenset({"id", "display_order"})
  49. class SessionLedgeredError(RuntimeError):
  50. """A session with gateway admission/worker history cannot be moved between stores."""
  51. def __init__(self, session_id: str) -> None:
  52. super().__init__(f"{session_id} has gateway admission/worker history; not moved")
  53. class SessionProfileRepairMixin:
  54. """Per-store crossed-profile identity: find, relabel, sever, move, and settle routing/topic rows."""
  55. # ── finders ────────────────────────────────────────────────────────────────
  56. def find_crossed_profile_sessions(self, owner: str) -> dict[str, list[dict[str, Any]]]:
  57. """Keyed session rows whose identity disagrees with itself or with this store.
  58. ``mislabelled``: ``profile_name`` names a profile other than the one in the row's own key
  59. (the key is what routing, the agent cache and ``_db_for_key`` consult; the label is stamped
  60. after the fact). ``foreign``: the key's profile is not *owner* — the row sits in a store that
  61. the routed profile never reads. ``crossed_parents``: child and parent are both keyed and name
  62. different profiles, so the NULL-fill inheritance fence would have been the only thing standing
  63. between them. A row can appear in more than one list.
  64. """
  65. rows = self._read_all(
  66. "SELECT s.id, s.session_key, s.profile_name, s.parent_session_id, s.message_count, "
  67. " p.session_key AS parent_session_key "
  68. "FROM sessions s LEFT JOIN sessions p ON p.id = s.parent_session_id "
  69. "WHERE s.session_key LIKE 'agent:%' ORDER BY s.started_at, s.id")
  70. ledgered = self._ledgered_session_ids()
  71. found: dict[str, list[dict[str, Any]]] = {"mislabelled": [], "foreign": [], "crossed_parents": []}
  72. for row in rows:
  73. key_profile = session_key_profile(row["session_key"])
  74. if key_profile is None:
  75. continue
  76. label = _stored_profile(row["profile_name"])
  77. if label is not None and label != key_profile:
  78. found["mislabelled"].append({
  79. "id": row["id"], "session_key": row["session_key"],
  80. "profile_name": label, "key_profile": key_profile})
  81. if key_profile != owner:
  82. found["foreign"].append({
  83. "id": row["id"], "session_key": row["session_key"], "key_profile": key_profile,
  84. "message_count": int(row["message_count"] or 0),
  85. "parent_session_id": row["parent_session_id"], "ledgered": row["id"] in ledgered})
  86. parent_profile = session_key_profile(row["parent_session_key"])
  87. if parent_profile is not None and parent_profile != key_profile:
  88. found["crossed_parents"].append({
  89. "id": row["id"], "key_profile": key_profile,
  90. "parent_session_id": row["parent_session_id"], "parent_profile": parent_profile})
  91. return found
  92. def _ledgered_session_ids(self) -> set[str]:
  93. """Sessions the gateway's durable ledger references (``session_admissions`` /
  94. ``worker_executions``, both ``ON DELETE RESTRICT``). A move copies only the transcript, so
  95. such a row could never leave the source: report it instead of copying it. A store whose
  96. schema predates the ledger has none."""
  97. def _read(conn):
  98. tables = {row[0] for row in conn.execute(
  99. "SELECT name FROM sqlite_master WHERE type='table' "
  100. "AND name IN ('session_admissions', 'worker_executions')")}
  101. ids: set[str] = set()
  102. if "session_admissions" in tables:
  103. ids.update(r[0] for r in conn.execute("SELECT DISTINCT target_session_id FROM session_admissions"))
  104. if "worker_executions" in tables:
  105. ids.update(r[0] for r in conn.execute("SELECT DISTINCT session_id FROM worker_executions"))
  106. return ids
  107. return self._read_retrying_ioerr(_read)
  108. def list_gateway_routing_rows(self) -> list[dict[str, Any]]:
  109. return [dict(row) for row in self._read_all(
  110. "SELECT scope, session_key, entry_json, updated_at FROM gateway_routing "
  111. "ORDER BY scope, session_key")]
  112. def find_profile_less_telegram_topic_rows(self) -> list[dict[str, Any]]:
  113. """``telegram_dm_topic_bindings`` rows labelled ``default`` whose ``session_key`` names a named
  114. profile: written by a multiplexer that had not yet learned to stamp the routed profile
  115. (#76423). Stores whose topic tables predate ``profile_name`` have nothing to relabel."""
  116. def _read(conn):
  117. existing = {row[0] for row in conn.execute(
  118. "SELECT name FROM sqlite_master WHERE type='table' AND name = 'telegram_dm_topic_bindings'")}
  119. if not existing or "profile_name" not in _table_columns(conn, "telegram_dm_topic_bindings"):
  120. return []
  121. return [dict(row) for row in conn.execute(
  122. "SELECT chat_id, thread_id, session_key FROM telegram_dm_topic_bindings "
  123. "WHERE profile_name = 'default' ORDER BY chat_id, thread_id")]
  124. out = []
  125. for row in self._read_retrying_ioerr(_read):
  126. key_profile = session_key_profile(row["session_key"])
  127. if key_profile not in (None, "default"):
  128. out.append({**row, "key_profile": key_profile})
  129. return out
  130. def count_messages_all(self, session_id: str) -> int:
  131. row = self._read_one("SELECT COUNT(*) FROM messages WHERE session_id = ?", (session_id,))
  132. return int(row[0]) if row else 0
  133. # ── in-store fixers ───────────────────────────────────────────────────────
  134. def relabel_sessions_to_key_profile(self, ids: Iterable[str]) -> int:
  135. """Set ``profile_name`` to the profile in each row's own key. Re-derives the target inside the
  136. transaction (never trusts a stale report)."""
  137. wanted = list(ids)
  138. if not wanted:
  139. return 0
  140. def _do(conn) -> int:
  141. changed = 0
  142. for chunk in _id_chunks(wanted):
  143. rows = conn.execute(
  144. f"SELECT id, session_key FROM sessions WHERE id IN ({_session_ids_placeholders(chunk)})",
  145. chunk).fetchall()
  146. for session_id, session_key in rows:
  147. target = session_key_profile(session_key)
  148. if target is None:
  149. continue
  150. changed += conn.execute(
  151. "UPDATE sessions SET profile_name = ? WHERE id = ? AND profile_name IS NOT ?",
  152. (target, session_id, target)).rowcount
  153. return changed
  154. return self._execute_write(_do)
  155. def sever_crossed_parents(self, ids: Iterable[str]) -> int:
  156. """Detach children from a parent keyed under another profile. Only ``parent_session_id`` is
  157. cleared — ``profile_name`` stays the row's own (relabelled separately when it is wrong) — and
  158. only while the crossing still holds."""
  159. wanted = list(ids)
  160. if not wanted:
  161. return 0
  162. def _do(conn) -> int:
  163. severed = 0
  164. for chunk in _id_chunks(wanted):
  165. rows = conn.execute(
  166. "SELECT s.id, s.session_key, p.session_key FROM sessions s "
  167. "JOIN sessions p ON p.id = s.parent_session_id "
  168. f"WHERE s.id IN ({_session_ids_placeholders(chunk)})", chunk).fetchall()
  169. for session_id, key, parent_key in rows:
  170. mine, theirs = session_key_profile(key), session_key_profile(parent_key)
  171. if mine is not None and theirs is not None and mine != theirs:
  172. severed += conn.execute(
  173. "UPDATE sessions SET parent_session_id = NULL WHERE id = ?", (session_id,)).rowcount
  174. return severed
  175. return self._execute_write(_do)
  176. # ── cross-store move ──────────────────────────────────────────────────────
  177. def export_session_for_move(self, session_id: str) -> Optional[dict[str, Any]]:
  178. """Everything the target store needs to hold *session_id* as its own: the row (every column),
  179. the resolved system prompt, EVERY message row (inactive and compacted generations included —
  180. a move is not an export) and its usage rows."""
  181. def _read(conn):
  182. session = conn.execute("SELECT * FROM sessions WHERE id = ?", (session_id,)).fetchone()
  183. if session is None:
  184. return None
  185. def _stored(prompt_hash):
  186. row = conn.execute("SELECT prompt FROM system_prompts WHERE hash = ?", (prompt_hash,)).fetchone()
  187. return row[0] if row else None
  188. prompt = _stored(session["system_prompt_hash"]) if session["system_prompt_hash"] else None
  189. # The tools[] pin is content-addressed the same way; a legacy inline list resolves to None.
  190. tool_pin = _stored(session["tool_names"]) if session["tool_names"] else None
  191. messages = [dict(r) for r in conn.execute(
  192. "SELECT * FROM messages WHERE session_id = ? ORDER BY id", (session_id,))]
  193. usage = [dict(r) for r in conn.execute(
  194. "SELECT * FROM session_model_usage WHERE session_id = ?", (session_id,))]
  195. return {"session": dict(session), "system_prompt": prompt, "tool_pin": tool_pin, "messages": messages,
  196. "usage": usage}
  197. payload = self._read_retrying_ioerr(_read)
  198. if payload is not None and session_id in self._ledgered_session_ids():
  199. # Re-checked at apply time: the scan may predate a turn, and copying first would leave
  200. # the session in both stores once the ledger's RESTRICT FK refuses the source delete.
  201. raise SessionLedgeredError(session_id)
  202. return payload
  203. def import_moved_session(self, payload: dict[str, Any], *, profile_name: str) -> str:
  204. """Insert a moved session into THIS store as *profile_name*'s. ``present`` when the id already
  205. exists (an earlier run copied but did not delete), else ``imported``. The parent link survives
  206. only when the parent is already here — a moved row must never point across stores or at a
  207. row of another profile. Columns the target schema lacks are dropped, never invented. Titles
  208. are unique per store only, so a title an unrelated row here already holds gets the moved
  209. row's id tail (the :meth:`import_foreign_history` convention); the resident row keeps its
  210. name, since it is the one this profile's clients resolve by title."""
  211. session = dict(payload["session"])
  212. session_id = session["id"]
  213. def _do(conn) -> str:
  214. if conn.execute("SELECT 1 FROM sessions WHERE id = ?", (session_id,)).fetchone():
  215. return "present"
  216. session["profile_name"] = profile_name
  217. parent_id = session.get("parent_session_id")
  218. if parent_id and conn.execute("SELECT 1 FROM sessions WHERE id = ?", (parent_id,)).fetchone() is None:
  219. session["parent_session_id"] = None
  220. title = session.get("title")
  221. if title is not None and conn.execute("SELECT 1 FROM sessions WHERE title = ?", (title,)).fetchone():
  222. suffix = f" ({session_id[-12:]})"
  223. session["title"] = title[:self.MAX_TITLE_LENGTH - len(suffix)] + suffix
  224. session["system_prompt_hash"] = self._store_system_prompt(conn, payload.get("system_prompt"))
  225. if payload.get("tool_pin") is not None or len(session.get("tool_names") or "") == 64:
  226. # A pin hash means nothing in this store: re-store the pin, or drop an unresolvable ref.
  227. session["tool_names"] = self._store_system_prompt(conn, payload.get("tool_pin"))
  228. self._insert_row(conn, "sessions", session, skip=frozenset())
  229. for message in payload.get("messages") or []:
  230. self._insert_row(conn, "messages", {**message, "session_id": session_id}, skip=_MESSAGE_MOVE_SKIP)
  231. for usage in payload.get("usage") or []:
  232. self._insert_row(conn, "session_model_usage", {**usage, "session_id": session_id}, skip=frozenset())
  233. return "imported"
  234. return self._execute_write(_do)
  235. @staticmethod
  236. def _insert_row(conn, table: str, values: dict[str, Any], *, skip: frozenset) -> None:
  237. columns = [c for c in _table_columns(conn, table) if c in values and c not in skip]
  238. conn.execute(
  239. f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({', '.join('?' for _ in columns)})",
  240. [values[c] for c in columns])
  241. def delete_moved_session(self, session_id: str) -> bool:
  242. """Remove a session this store no longer owns after the target confirmed it. Children left
  243. behind are detached (same FK rule as :meth:`delete_session`); topic bindings on the row
  244. cascade with it."""
  245. def _do(conn) -> bool:
  246. if conn.execute("SELECT 1 FROM sessions WHERE id = ?", (session_id,)).fetchone() is None:
  247. return False
  248. conn.execute("UPDATE sessions SET parent_session_id = NULL WHERE parent_session_id = ?", (session_id,))
  249. conn.execute("DELETE FROM messages WHERE session_id = ?", (session_id,))
  250. conn.execute("DELETE FROM session_model_usage WHERE session_id = ?", (session_id,))
  251. conn.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
  252. self._delete_unreferenced_system_prompts(conn)
  253. return True
  254. deleted = self._execute_write(_do)
  255. if deleted:
  256. emit_session_deleted(self, [session_id], reason="profile_repair")
  257. return bool(deleted)
  258. # ── routing index ─────────────────────────────────────────────────────────
  259. def rekey_legacy_main_sessions(self, ids: Iterable[str], profile: str) -> int:
  260. """``agent:main:…`` → ``agent:<profile>:…`` (+ ``profile_name``) for rows a standalone
  261. gateway wrote before this store's profile was multiplexed (#113884). Only rows still under
  262. the legacy namespace change; a ``main``-named profile is written in its marked form."""
  263. from gateway.session import _session_key_namespace
  264. wanted = list(ids)
  265. if not wanted:
  266. return 0
  267. new_ns = _session_key_namespace(profile) + ":"
  268. def _do(conn) -> int:
  269. changed = 0
  270. for chunk in _id_chunks(wanted):
  271. changed += conn.execute(
  272. "UPDATE sessions SET session_key = ? || substr(session_key, 12), profile_name = ? "
  273. f"WHERE id IN ({_session_ids_placeholders(chunk)}) AND substr(session_key, 1, 11) = 'agent:main:'",
  274. (new_ns, profile, *chunk)).rowcount
  275. return changed
  276. return self._execute_write(_do)
  277. def delete_gateway_routing_rows(self, rows: Iterable[tuple[str, str]]) -> int:
  278. wanted = list(rows)
  279. if not wanted:
  280. return 0
  281. def _do(conn) -> int:
  282. return sum(conn.execute(
  283. "DELETE FROM gateway_routing WHERE scope = ? AND session_key = ?", (scope, key)).rowcount
  284. for scope, key in wanted)
  285. return self._execute_write(_do)
  286. def insert_gateway_routing_rows_if_absent(self, rows: Iterable[tuple[str, str, str, float]]) -> int:
  287. """Adopt ``(scope, session_key, entry_json, updated_at)`` rows another store held for this
  288. one's routing index. An existing key wins — the row the gateway actually loads stays."""
  289. wanted = list(rows)
  290. if not wanted:
  291. return 0
  292. def _do(conn) -> int:
  293. return sum(conn.execute(
  294. "INSERT OR IGNORE INTO gateway_routing (scope, session_key, entry_json, updated_at) "
  295. "VALUES (?, ?, ?, ?)", row).rowcount for row in wanted)
  296. return self._execute_write(_do)
  297. # ── telegram topic tables ─────────────────────────────────────────────────
  298. def relabel_telegram_topic_rows(self, rows: Iterable[dict[str, Any]]) -> dict[str, int]:
  299. """Stamp the key's profile onto ``default``-labelled bindings (and the chat's mode row when
  300. only the default one exists). A binding that would collide with one the named profile already
  301. wrote is the stale duplicate and is removed."""
  302. wanted = list(rows)
  303. counts = {"bindings_relabelled": 0, "bindings_duplicates_removed": 0, "mode_rows_relabelled": 0}
  304. if not wanted:
  305. return counts
  306. def _do(conn) -> dict[str, int]:
  307. mode_has_profile = "profile_name" in _table_columns(conn, "telegram_dm_topic_mode")
  308. for row in wanted:
  309. target = session_key_profile(row["session_key"])
  310. if target in (None, "default"):
  311. continue
  312. chat_id, thread_id = row["chat_id"], row["thread_id"]
  313. collides = conn.execute(
  314. "SELECT 1 FROM telegram_dm_topic_bindings WHERE profile_name = ? AND chat_id = ? "
  315. "AND thread_id = ?", (target, chat_id, thread_id)).fetchone()
  316. if collides:
  317. counts["bindings_duplicates_removed"] += conn.execute(
  318. "DELETE FROM telegram_dm_topic_bindings WHERE profile_name = 'default' "
  319. "AND chat_id = ? AND thread_id = ?", (chat_id, thread_id)).rowcount
  320. else:
  321. counts["bindings_relabelled"] += conn.execute(
  322. "UPDATE telegram_dm_topic_bindings SET profile_name = ? WHERE profile_name = 'default' "
  323. "AND chat_id = ? AND thread_id = ?", (target, chat_id, thread_id)).rowcount
  324. if mode_has_profile and conn.execute(
  325. "SELECT 1 FROM telegram_dm_topic_mode WHERE profile_name = ? AND chat_id = ?",
  326. (target, chat_id)).fetchone() is None:
  327. counts["mode_rows_relabelled"] += conn.execute(
  328. "UPDATE telegram_dm_topic_mode SET profile_name = ? WHERE profile_name = 'default' "
  329. "AND chat_id = ?", (target, chat_id)).rowcount
  330. return counts
  331. return self._execute_write(_do)
  332. # ── evidence for JSON-file repairs ────────────────────────────────────────
  333. def key_profiles_for_chat(self, platform: str, chat_id: str) -> set[str]:
  334. """Profiles whose keyed sessions hold *(platform, chat_id)* in this store — the evidence for
  335. who owns a profile-less ``gateway_voice_mode.json`` entry."""
  336. rows = self._read_all(
  337. "SELECT DISTINCT session_key FROM sessions WHERE source = ? AND chat_id = ? "
  338. "AND session_key LIKE 'agent:%'", (platform, str(chat_id)))
  339. return {p for p in (session_key_profile(r["session_key"]) for r in rows) if p is not None}