Skip to content

The agent that grows with you

fix(sessions): verify history tables on the guarded repair snapshot

funky-xamarinf4c559e38314
diff --git a/hermes_state_reconcile.py b/hermes_state_reconcile.py
index feb1b3cdbf9..6dc3754367a 100644
--- a/hermes_state_reconcile.py
+++ b/hermes_state_reconcile.py
@@ -40,6 +40,7 @@ def _reconcile_snapshot(path, guard):
try:
_copy_database_snapshot(path, scratch, source_connection=guard)
with _repair_conn(scratch) as conn:
+ _require_history_tables(conn)
# Old indexed columns must exist before the canonical DDL can create
# their indexes. Reuse the column owner; the final probe fails closed
# if an additive change is not possible. Missing tables are created below.
@@ -57,6 +58,12 @@ def _reconcile_snapshot(path, guard):
logging.getLogger(__name__).warning('Could not remove reconciliation snapshot: %s', error)
+def _require_history_tables(conn):
+ tables = {row[0] for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
+ if {'sessions', 'messages'} - tables:
+ raise RuntimeError('canonical history tables are missing; use sessions recover, not additive reconciliation')
+
+
def _reconcile_locked(path, backup, report):
from hermes_state_repair import (
_backup_db_file, _db_opens_cleanly, _exclusive_repair_db_guard,
@@ -67,9 +74,7 @@ def _reconcile_locked(path, backup, report):
raise RuntimeError('a live writer still holds state.db; stop it and retry')
from hermes_cli.sqlite_safe_read import connect_tracked
with closing(connect_tracked(path.resolve().as_uri() + '?mode=ro', uri=True, tracking_path=path)) as conn:
- tables = {row[0] for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
- if {'sessions', 'messages'} - tables:
- raise RuntimeError('canonical history tables are missing; use sessions recover, not additive reconciliation')
+ _require_history_tables(conn)
if schema_drift_reason(path) is None:
report.update(repaired=True, strategy='already_healthy')
return
diff --git a/tests/hermes_cli/test_sessions_repair_declarative.py b/tests/hermes_cli/test_sessions_repair_declarative.py
index c14d79a5d1c..88ddcd5c01d 100644
--- a/tests/hermes_cli/test_sessions_repair_declarative.py
+++ b/tests/hermes_cli/test_sessions_repair_declarative.py
@@ -97,6 +97,27 @@ def test_missing_history_table_is_not_recreated_as_empty(tmp_path, monkeypatch,
assert path.read_bytes() == before
+def test_snapshot_history_loss_is_not_promoted(tmp_path, monkeypatch):
+ import hermes_state_repair as repair
+ from hermes_state_reconcile import repair_declarative_schema
+ path = _legacy_store(tmp_path, monkeypatch)
+ before = path.read_bytes()
+ copy = repair._copy_database_snapshot
+
+ def changed_snapshot(source, destination, **kwargs):
+ copy(source, destination, **kwargs)
+ if kwargs.get('source_connection') is not None:
+ with closing(sqlite3.connect(destination)) as conn:
+ conn.execute('DROP TABLE messages')
+ conn.commit()
+
+ monkeypatch.setattr(repair, '_copy_database_snapshot', changed_snapshot)
+ report = repair_declarative_schema(path)
+ assert report['repaired'] is False
+ assert 'canonical history tables are missing' in report['error']
+ assert path.read_bytes() == before
+
+
def test_repair_restores_missing_indexed_column(tmp_path, monkeypatch):
path = tmp_path / 'state.db'
with SessionDB(db_path=path) as db: