Files
hermes-agent/persistence/sqlite/conversation/_store/mutation_transcript.py
yoniebans 99c44c927d refactor(persistence/sqlite): break the conversation store's import cycles
The conversation store modules imported one another at their ends, and the package preloaded compression so that the order happened to resolve: ten modules formed one module-level cycle. Each module now imports its peers at its head along one order (compaction_display, core, messages, compression, coverage, portability, write_guards, sessions, prune_rewrite, titles), and an import that runs against that order happens inside the function that uses it. The preload is gone, and every store module imports alone in a fresh interpreter.
2026-11-17 17:49:47 +01:00

43 lines
2.7 KiB
Python

"""The rewind body run inside a write transaction."""
from persistence.sqlite.conversation._store.sql_common import _placeholders
from persistence.sqlite.conversation._store.messages import _active_transcript_counts, _check_transcript_write_guards, _insert_message_rows, _split_rewind_target
from persistence.sqlite.conversation._store.messages_sql import _ACTIVE_IDS_SQL, _SET_COUNTERS_SQL
def rewind_in_transaction(holder, conn, session_id, target_message_id, *,
preserve_compaction_handoff=False, expected_active_ids=None,
expected_target_content=None):
_check_transcript_write_guards(holder,
conn, session_id, None, reject_active_turn_lease=True, reject_active_compression_lock=True)
if expected_active_ids is not None:
active_rows = conn.execute(_ACTIVE_IDS_SQL, (session_id,)).fetchall()
if [int(r[0]) for r in active_rows] != expected_active_ids:
raise RuntimeError("active transcript changed before the rewind could be persisted")
row = conn.execute(
"SELECT * FROM messages WHERE id = ? AND session_id = ?", (target_message_id, session_id)).fetchone()
if row is None:
raise ValueError(f"message {target_message_id} not found in session {session_id}")
target_row = dict(row)
if target_row.get("role") != "user":
raise ValueError(
f"rewind target must be a 'user' message (got role={target_row.get('role')!r}, id={target_message_id})")
replacement_message_id = replacement = None
if preserve_compaction_handoff or expected_target_content is not None:
replacement = _split_rewind_target(holder, target_row, expected_target_content, preserve_compaction_handoff)
ids = [r[0] for r in conn.execute("SELECT id FROM messages WHERE session_id = ? AND id >= ? AND active = 1",
(session_id, target_message_id)).fetchall()]
if ids:
conn.execute(f"UPDATE messages SET active = 0 WHERE id IN ({_placeholders(ids)})", ids)
if replacement is not None:
_insert_message_rows(holder, conn, session_id, [replacement])
replacement_message_id = int(conn.execute("SELECT last_insert_rowid()").fetchone()[0])
conn.execute(
"UPDATE sessions SET rewind_count = COALESCE(rewind_count, 0) + 1 WHERE id = ?", (session_id,))
message_count, tool_call_count = _active_transcript_counts(conn, session_id)
conn.execute(f"{_SET_COUNTERS_SQL} WHERE id = ?", (message_count, tool_call_count, session_id))
head_id = conn.execute(
"SELECT MAX(id) FROM messages WHERE session_id = ? AND active = 1", (session_id,)).fetchone()[0]
return target_row, ids, head_id, replacement_message_id