hermes_state.py becomes the stand-in plugins import, the rest of the legacy store and its test folder go, and the per-process modules the backend imported from main move under persistence/sqlite/_state/. The backend checks PID namespaces and lease holders through process_identity and its WAL setup through sqlite_support, and its error module takes the shared predicates and classification from storage_errors, so neither helper set has a second copy. The backend, sqlite_support.py and the tests that pin those modules reach their final imports, and the backend-import guard allows no file outside persistence/. The modules no caller imports any more go with it: the agent's onboarding rules (now in session_rules) and gateway/session_admission.py. The stand-in's SessionDB releases its store on close() and, used again afterwards, opens it again with a warning, as the old class reopened its connection, unless the file at its path was replaced or the store is latched corrupt; a read-only one raises. A close during a call releases the store when the call returns. A named source wins over a source list in its session listing and count. An agent given a writable stand-in SessionDB as session_db= and no persistence= takes the store handle that SessionDB forwards to as its persistence, so its turn flush, token counts, compression and session row writes go through the storage interface, as an agent given the old SessionDB wrote through it. The backend-import guard also holds the backend's remaining reads of the process or task home, each with its reason, and refuses any other and any import of the stand-in inside persistence/sqlite/. Plugin context engines get the agent's store as persistence= beside the deprecated session_db=, in each hook whose signature accepts that keyword, including the rebind after /new, /resume and /branch, and the context engine guide and the plugin guide's replacement table name it. A test fails once the running release reaches the stand-in's removal release, listing what goes with the stand-in. release_unheld_media and collect_unheld_api_images take a store only: their SessionDB branches, which no caller reaches, go with the gateway's own copies of the admission and transcript queries.
406 lines
22 KiB
Python
406 lines
22 KiB
Python
"""ProfileRepairAdmin on one SQLite state.db.
|
|
|
|
Every ``state.db`` belongs to exactly one profile (``<root>/state.db`` is ``default``,
|
|
``<root>/profiles/<name>/state.db`` is ``name``) and every gateway session key encodes the profile
|
|
that owns the conversation (``agent:main:...`` for the default, ``agent:<name>:...`` for a named one).
|
|
The finders name each row whose identity disagrees with itself or with this store; the fixers settle it.
|
|
|
|
Moving a session between two stores is two single-store transactions, copy into the target, then
|
|
delete from the source, because a crash between them leaves a *duplicate*, which the next run
|
|
settles idempotently (:meth:`import_moved_session` reports ``present``; the delete only proceeds when
|
|
the target holds at least as many messages). The reverse order would lose the row.
|
|
|
|
A read-only handle never creates a file: when its ``state.db`` does not exist every read answers
|
|
empty (no rows, no messages, no payload) without opening anything.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Iterable, Literal, Mapping, Optional
|
|
|
|
from persistence.sqlite._state.common import _id_chunks, _placeholders as _session_ids_placeholders
|
|
from persistence import SessionLedgered
|
|
from persistence.conversation.sessions import MAX_TITLE_LENGTH
|
|
from persistence.core.keys import session_key_profile
|
|
from session_retention import emit_session_deleted
|
|
from session_rules.route_entries import _session_key_namespace
|
|
|
|
from .._holder import StoreHolder
|
|
from ..conversation._store.core import _delete_unreferenced_system_prompts
|
|
from ._base import AdminFile
|
|
|
|
__all__ = ["SQLiteProfileRepairAdmin"]
|
|
|
|
|
|
def _stored_profile(value: Any) -> Optional[str]:
|
|
"""``sessions.profile_name`` as a comparable label; NULL/blank is "unowned", not a crossing."""
|
|
name = str(value or "").strip()
|
|
return name or None
|
|
|
|
|
|
def _table_columns(conn, table: str) -> list[str]:
|
|
return [row[1] for row in conn.execute(f"PRAGMA table_info('{table}')")]
|
|
|
|
|
|
# Columns a moved message must NOT carry over verbatim: ``id`` is reassigned by the target's
|
|
# AUTOINCREMENT and ``display_order`` points at a message id of the SOURCE store; left NULL, the
|
|
# ``messages_display_order_insert`` trigger recomputes it from ``display_identity`` (a content hash,
|
|
# store-independent) or the new id.
|
|
_MESSAGE_MOVE_SKIP = frozenset({"id", "display_order"})
|
|
|
|
|
|
def _ledgered_session_ids(holder: StoreHolder) -> set[str]:
|
|
"""Sessions the gateway's durable ledger references (``session_admissions`` /
|
|
``worker_executions``, both ``ON DELETE RESTRICT``). A move copies only the transcript, so
|
|
such a row could never leave the source: report it instead of copying it. A store whose
|
|
schema predates the ledger has none."""
|
|
def _read(conn):
|
|
tables = {row[0] for row in conn.execute(
|
|
"SELECT name FROM sqlite_master WHERE type='table' "
|
|
"AND name IN ('session_admissions', 'worker_executions')")}
|
|
ids: set[str] = set()
|
|
if "session_admissions" in tables:
|
|
ids.update(r[0] for r in conn.execute("SELECT DISTINCT target_session_id FROM session_admissions"))
|
|
if "worker_executions" in tables:
|
|
ids.update(r[0] for r in conn.execute("SELECT DISTINCT session_id FROM worker_executions"))
|
|
return ids
|
|
return holder._read_retrying_ioerr(_read)
|
|
|
|
|
|
def _insert_row(conn, table: str, values: dict[str, Any], *, skip: frozenset) -> None:
|
|
columns = [c for c in _table_columns(conn, table) if c in values and c not in skip]
|
|
conn.execute(
|
|
f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({', '.join('?' for _ in columns)})",
|
|
[values[c] for c in columns])
|
|
|
|
|
|
class SQLiteProfileRepairAdmin(AdminFile):
|
|
"""Per-store crossed-profile identity: find, relabel, sever, move, and settle routing/topic rows."""
|
|
|
|
def _absent(self) -> bool:
|
|
"""A read-only handle on a file that does not exist: reads answer empty, nothing is opened."""
|
|
return self.read_only and not self.db_path.exists()
|
|
|
|
# finders
|
|
|
|
def find_crossed_profile_sessions(self, owner: str) -> dict[str, list[dict[str, Any]]]:
|
|
"""Keyed session rows whose identity disagrees with itself or with this store.
|
|
|
|
``mislabelled``: ``profile_name`` names a profile other than the one in the row's own key
|
|
(the key is what routing, the agent cache and ``_db_for_key`` consult; the label is stamped
|
|
after the fact). ``foreign``: the key's profile is not *owner*, so the row sits in a store that
|
|
the routed profile never reads. ``crossed_parents``: child and parent are both keyed and name
|
|
different profiles, so the NULL-fill inheritance fence would have been the only thing standing
|
|
between them. A row can appear in more than one list.
|
|
"""
|
|
found: dict[str, list[dict[str, Any]]] = {"mislabelled": [], "foreign": [], "crossed_parents": []}
|
|
if self._absent():
|
|
return found
|
|
with self._reader() as holder:
|
|
rows = holder._read_all(
|
|
"SELECT s.id, s.session_key, s.profile_name, s.parent_session_id, s.message_count, "
|
|
" p.session_key AS parent_session_key "
|
|
"FROM sessions s LEFT JOIN sessions p ON p.id = s.parent_session_id "
|
|
"WHERE s.session_key LIKE 'agent:%' ORDER BY s.started_at, s.id")
|
|
ledgered = _ledgered_session_ids(holder)
|
|
for row in rows:
|
|
key_profile = session_key_profile(row["session_key"])
|
|
if key_profile is None:
|
|
continue
|
|
label = _stored_profile(row["profile_name"])
|
|
if label is not None and label != key_profile:
|
|
found["mislabelled"].append({
|
|
"id": row["id"], "session_key": row["session_key"],
|
|
"profile_name": label, "key_profile": key_profile})
|
|
if key_profile != owner:
|
|
found["foreign"].append({
|
|
"id": row["id"], "session_key": row["session_key"], "key_profile": key_profile,
|
|
"message_count": int(row["message_count"] or 0),
|
|
"parent_session_id": row["parent_session_id"], "ledgered": row["id"] in ledgered})
|
|
parent_profile = session_key_profile(row["parent_session_key"])
|
|
if parent_profile is not None and parent_profile != key_profile:
|
|
found["crossed_parents"].append({
|
|
"id": row["id"], "key_profile": key_profile,
|
|
"parent_session_id": row["parent_session_id"], "parent_profile": parent_profile})
|
|
return found
|
|
|
|
def list_gateway_routing_rows(self) -> list[dict[str, Any]]:
|
|
if self._absent():
|
|
return []
|
|
with self._reader() as holder:
|
|
return [dict(row) for row in holder._read_all(
|
|
"SELECT scope, session_key, entry_json, updated_at FROM gateway_routing "
|
|
"ORDER BY scope, session_key")]
|
|
|
|
def find_profile_less_telegram_topic_rows(self) -> list[dict[str, Any]]:
|
|
"""``telegram_dm_topic_bindings`` rows labelled ``default`` whose ``session_key`` names a named
|
|
profile: written by a multiplexer that had not yet learned to stamp the routed profile.
|
|
Stores whose topic tables predate ``profile_name`` have nothing to relabel."""
|
|
if self._absent():
|
|
return []
|
|
|
|
def _read(conn):
|
|
existing = {row[0] for row in conn.execute(
|
|
"SELECT name FROM sqlite_master WHERE type='table' AND name = 'telegram_dm_topic_bindings'")}
|
|
if not existing or "profile_name" not in _table_columns(conn, "telegram_dm_topic_bindings"):
|
|
return []
|
|
return [dict(row) for row in conn.execute(
|
|
"SELECT chat_id, thread_id, session_key FROM telegram_dm_topic_bindings "
|
|
"WHERE profile_name = 'default' ORDER BY chat_id, thread_id")]
|
|
out = []
|
|
with self._reader() as holder:
|
|
rows = holder._read_retrying_ioerr(_read)
|
|
for row in rows:
|
|
key_profile = session_key_profile(row["session_key"])
|
|
if key_profile not in (None, "default"):
|
|
out.append({**row, "key_profile": key_profile})
|
|
return out
|
|
|
|
def count_messages_all(self, session_id: str) -> int:
|
|
if self._absent():
|
|
return 0
|
|
with self._reader() as holder:
|
|
row = holder._read_one("SELECT COUNT(*) FROM messages WHERE session_id = ?", (session_id,))
|
|
return int(row[0]) if row else 0
|
|
|
|
# in-store fixers
|
|
|
|
def relabel_sessions_to_key_profile(self, ids: Iterable[str]) -> int:
|
|
"""Set ``profile_name`` to the profile in each row's own key. Re-derives the target inside the
|
|
transaction (never trusts a stale report)."""
|
|
wanted = list(ids)
|
|
if not wanted:
|
|
return 0
|
|
|
|
def _do(conn) -> int:
|
|
changed = 0
|
|
for chunk in _id_chunks(wanted):
|
|
rows = conn.execute(
|
|
f"SELECT id, session_key FROM sessions WHERE id IN ({_session_ids_placeholders(chunk)})",
|
|
chunk).fetchall()
|
|
for session_id, session_key in rows:
|
|
target = session_key_profile(session_key)
|
|
if target is None:
|
|
continue
|
|
changed += conn.execute(
|
|
"UPDATE sessions SET profile_name = ? WHERE id = ? AND profile_name IS NOT ?",
|
|
(target, session_id, target)).rowcount
|
|
return changed
|
|
with self._writer("relabel_sessions_to_key_profile") as holder:
|
|
return holder._execute_write(_do)
|
|
|
|
def sever_crossed_parents(self, ids: Iterable[str]) -> int:
|
|
"""Detach children from a parent keyed under another profile. Only ``parent_session_id`` is
|
|
cleared (``profile_name`` stays the row's own, relabelled separately when it is wrong), and
|
|
only while the crossing still holds."""
|
|
wanted = list(ids)
|
|
if not wanted:
|
|
return 0
|
|
|
|
def _do(conn) -> int:
|
|
severed = 0
|
|
for chunk in _id_chunks(wanted):
|
|
rows = conn.execute(
|
|
"SELECT s.id, s.session_key, p.session_key FROM sessions s "
|
|
"JOIN sessions p ON p.id = s.parent_session_id "
|
|
f"WHERE s.id IN ({_session_ids_placeholders(chunk)})", chunk).fetchall()
|
|
for session_id, key, parent_key in rows:
|
|
mine, theirs = session_key_profile(key), session_key_profile(parent_key)
|
|
if mine is not None and theirs is not None and mine != theirs:
|
|
severed += conn.execute(
|
|
"UPDATE sessions SET parent_session_id = NULL WHERE id = ?", (session_id,)).rowcount
|
|
return severed
|
|
with self._writer("sever_crossed_parents") as holder:
|
|
return holder._execute_write(_do)
|
|
|
|
# cross-store move
|
|
|
|
def export_session_for_move(self, session_id: str) -> dict[str, Any] | None:
|
|
"""Everything the target store needs to hold *session_id* as its own: the row (every column),
|
|
the resolved system prompt, EVERY message row (inactive and compacted generations included;
|
|
a move is not an export) and its usage rows. ``SessionLedgered`` when the gateway's ledger
|
|
references the session."""
|
|
if self._absent():
|
|
return None
|
|
|
|
def _read(conn):
|
|
session = conn.execute("SELECT * FROM sessions WHERE id = ?", (session_id,)).fetchone()
|
|
if session is None:
|
|
return None
|
|
def _stored(prompt_hash):
|
|
row = conn.execute("SELECT prompt FROM system_prompts WHERE hash = ?", (prompt_hash,)).fetchone()
|
|
return row[0] if row else None
|
|
prompt = _stored(session["system_prompt_hash"]) if session["system_prompt_hash"] else None
|
|
# The tools[] pin is content-addressed the same way; a legacy inline list resolves to None.
|
|
tool_pin = _stored(session["tool_names"]) if session["tool_names"] else None
|
|
messages = [dict(r) for r in conn.execute(
|
|
"SELECT * FROM messages WHERE session_id = ? ORDER BY id", (session_id,))]
|
|
usage = [dict(r) for r in conn.execute(
|
|
"SELECT * FROM session_model_usage WHERE session_id = ?", (session_id,))]
|
|
return {"session": dict(session), "system_prompt": prompt, "tool_pin": tool_pin, "messages": messages,
|
|
"usage": usage}
|
|
with self._reader() as holder:
|
|
payload = holder._read_retrying_ioerr(_read)
|
|
ledgered = payload is not None and session_id in _ledgered_session_ids(holder)
|
|
if ledgered:
|
|
# Re-checked at apply time: the scan may predate a turn, and copying first would leave
|
|
# the session in both stores once the ledger's RESTRICT FK refuses the source delete.
|
|
raise SessionLedgered(session_id)
|
|
return payload
|
|
|
|
def import_moved_session(
|
|
self, payload: Mapping[str, Any], *, profile_name: str,
|
|
) -> Literal["present", "imported"]:
|
|
"""Insert a moved session into THIS store as *profile_name*'s. ``present`` when the id already
|
|
exists (an earlier run copied but did not delete), else ``imported``. The parent link survives
|
|
only when the parent is already here: a moved row must never point across stores or at a
|
|
row of another profile. Columns the target schema lacks are dropped, never invented. Titles
|
|
are unique per store only, so a title an unrelated row here already holds gets the moved
|
|
row's id tail; the resident row keeps its name, since it is the one this profile's clients
|
|
resolve by title."""
|
|
session = dict(payload["session"])
|
|
session_id = session["id"]
|
|
|
|
def _do(conn) -> str:
|
|
if conn.execute("SELECT 1 FROM sessions WHERE id = ?", (session_id,)).fetchone():
|
|
return "present"
|
|
session["profile_name"] = profile_name
|
|
parent_id = session.get("parent_session_id")
|
|
if parent_id and conn.execute("SELECT 1 FROM sessions WHERE id = ?", (parent_id,)).fetchone() is None:
|
|
session["parent_session_id"] = None
|
|
title = session.get("title")
|
|
if title is not None and conn.execute("SELECT 1 FROM sessions WHERE title = ?", (title,)).fetchone():
|
|
suffix = f" ({session_id[-12:]})"
|
|
session["title"] = title[:MAX_TITLE_LENGTH - len(suffix)] + suffix
|
|
session["system_prompt_hash"] = StoreHolder._store_system_prompt(conn, payload.get("system_prompt"))
|
|
if payload.get("tool_pin") is not None or len(session.get("tool_names") or "") == 64:
|
|
# A pin hash means nothing in this store: re-store the pin, or drop an unresolvable ref.
|
|
session["tool_names"] = StoreHolder._store_system_prompt(conn, payload.get("tool_pin"))
|
|
_insert_row(conn, "sessions", session, skip=frozenset())
|
|
for message in payload.get("messages") or []:
|
|
_insert_row(conn, "messages", {**message, "session_id": session_id}, skip=_MESSAGE_MOVE_SKIP)
|
|
for usage in payload.get("usage") or []:
|
|
_insert_row(conn, "session_model_usage", {**usage, "session_id": session_id}, skip=frozenset())
|
|
return "imported"
|
|
with self._writer("import_moved_session") as holder:
|
|
return holder._execute_write(_do)
|
|
|
|
def delete_moved_session(self, session_id: str) -> bool:
|
|
"""Remove a session this store no longer owns after the target confirmed it. Children left
|
|
behind are detached (the same FK rule as a session delete); topic bindings on the row
|
|
cascade with it. Fires ``on_session_delete`` (``reason="profile_repair"``) after the commit."""
|
|
def _do(conn) -> bool:
|
|
if conn.execute("SELECT 1 FROM sessions WHERE id = ?", (session_id,)).fetchone() is None:
|
|
return False
|
|
conn.execute("UPDATE sessions SET parent_session_id = NULL WHERE parent_session_id = ?", (session_id,))
|
|
conn.execute("DELETE FROM messages WHERE session_id = ?", (session_id,))
|
|
conn.execute("DELETE FROM session_model_usage WHERE session_id = ?", (session_id,))
|
|
conn.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
|
|
_delete_unreferenced_system_prompts(conn)
|
|
return True
|
|
with self._writer("delete_moved_session") as holder:
|
|
deleted = holder._execute_write(_do)
|
|
if deleted:
|
|
emit_session_deleted(self.db_path.parent, [session_id], reason="profile_repair")
|
|
return bool(deleted)
|
|
|
|
# routing index
|
|
|
|
def rekey_legacy_main_sessions(self, ids: Iterable[str], profile: str) -> int:
|
|
"""``agent:main:...`` to ``agent:<profile>:...`` (+ ``profile_name``) for rows a standalone
|
|
gateway wrote before this store's profile was multiplexed. Only rows still under the legacy
|
|
namespace change; a ``main``-named profile is written in its marked form."""
|
|
wanted = list(ids)
|
|
if not wanted:
|
|
return 0
|
|
new_ns = _session_key_namespace(profile) + ":"
|
|
|
|
def _do(conn) -> int:
|
|
changed = 0
|
|
for chunk in _id_chunks(wanted):
|
|
changed += conn.execute(
|
|
"UPDATE sessions SET session_key = ? || substr(session_key, 12), profile_name = ? "
|
|
f"WHERE id IN ({_session_ids_placeholders(chunk)}) AND substr(session_key, 1, 11) = 'agent:main:'",
|
|
(new_ns, profile, *chunk)).rowcount
|
|
return changed
|
|
with self._writer("rekey_legacy_main_sessions") as holder:
|
|
return holder._execute_write(_do)
|
|
|
|
def delete_gateway_routing_rows(self, rows: Iterable[tuple[str, str]]) -> int:
|
|
wanted = list(rows)
|
|
if not wanted:
|
|
return 0
|
|
|
|
def _do(conn) -> int:
|
|
return sum(conn.execute(
|
|
"DELETE FROM gateway_routing WHERE scope = ? AND session_key = ?", (scope, key)).rowcount
|
|
for scope, key in wanted)
|
|
with self._writer("delete_gateway_routing_rows") as holder:
|
|
return holder._execute_write(_do)
|
|
|
|
def insert_gateway_routing_rows_if_absent(self, rows: Iterable[tuple[str, str, str, float]]) -> int:
|
|
"""Adopt ``(scope, session_key, entry_json, updated_at)`` rows another store held for this
|
|
one's routing index. An existing key wins: the row the gateway actually loads stays."""
|
|
wanted = list(rows)
|
|
if not wanted:
|
|
return 0
|
|
|
|
def _do(conn) -> int:
|
|
return sum(conn.execute(
|
|
"INSERT OR IGNORE INTO gateway_routing (scope, session_key, entry_json, updated_at) "
|
|
"VALUES (?, ?, ?, ?)", row).rowcount for row in wanted)
|
|
with self._writer("insert_gateway_routing_rows_if_absent") as holder:
|
|
return holder._execute_write(_do)
|
|
|
|
# telegram topic tables
|
|
|
|
def relabel_telegram_topic_rows(self, rows: Iterable[Mapping[str, Any]]) -> dict[str, int]:
|
|
"""Stamp the key's profile onto ``default``-labelled bindings (and the chat's mode row when
|
|
only the default one exists). A binding that would collide with one the named profile already
|
|
wrote is the stale duplicate and is removed."""
|
|
wanted = list(rows)
|
|
counts = {"bindings_relabelled": 0, "bindings_duplicates_removed": 0, "mode_rows_relabelled": 0}
|
|
if not wanted:
|
|
return counts
|
|
|
|
def _do(conn) -> dict[str, int]:
|
|
mode_has_profile = "profile_name" in _table_columns(conn, "telegram_dm_topic_mode")
|
|
for row in wanted:
|
|
target = session_key_profile(row["session_key"])
|
|
if target in (None, "default"):
|
|
continue
|
|
chat_id, thread_id = row["chat_id"], row["thread_id"]
|
|
collides = conn.execute(
|
|
"SELECT 1 FROM telegram_dm_topic_bindings WHERE profile_name = ? AND chat_id = ? "
|
|
"AND thread_id = ?", (target, chat_id, thread_id)).fetchone()
|
|
if collides:
|
|
counts["bindings_duplicates_removed"] += conn.execute(
|
|
"DELETE FROM telegram_dm_topic_bindings WHERE profile_name = 'default' "
|
|
"AND chat_id = ? AND thread_id = ?", (chat_id, thread_id)).rowcount
|
|
else:
|
|
counts["bindings_relabelled"] += conn.execute(
|
|
"UPDATE telegram_dm_topic_bindings SET profile_name = ? WHERE profile_name = 'default' "
|
|
"AND chat_id = ? AND thread_id = ?", (target, chat_id, thread_id)).rowcount
|
|
if mode_has_profile and conn.execute(
|
|
"SELECT 1 FROM telegram_dm_topic_mode WHERE profile_name = ? AND chat_id = ?",
|
|
(target, chat_id)).fetchone() is None:
|
|
counts["mode_rows_relabelled"] += conn.execute(
|
|
"UPDATE telegram_dm_topic_mode SET profile_name = ? WHERE profile_name = 'default' "
|
|
"AND chat_id = ?", (target, chat_id)).rowcount
|
|
return counts
|
|
with self._writer("relabel_telegram_topic_rows") as holder:
|
|
return holder._execute_write(_do)
|
|
|
|
# evidence for JSON-file repairs
|
|
|
|
def key_profiles_for_chat(self, platform: str, chat_id: str) -> set[str]:
|
|
"""Profiles whose keyed sessions hold *(platform, chat_id)* in this store: the evidence for
|
|
who owns a profile-less ``gateway_voice_mode.json`` entry."""
|
|
if self._absent():
|
|
return set()
|
|
with self._reader() as holder:
|
|
rows = holder._read_all(
|
|
"SELECT DISTINCT session_key FROM sessions WHERE source = ? AND chat_id = ? "
|
|
"AND session_key LIKE 'agent:%'", (platform, str(chat_id)))
|
|
return {p for p in (session_key_profile(r["session_key"]) for r in rows) if p is not None}
|