The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): reconcile columns before dependent indexes on repair snapshots
funky-xamarinaf586e5aca35
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.pyindex 27a9ffb88d3..c10f2b423ab 100644--- a/hermes_cli/sessions_cmd.py+++ b/hermes_cli/sessions_cmd.py@@ -106,6 +106,8 @@ def _cmd_repair(args):print(f"No session database at {db_path} (nothing to repair).")returnfrom hermes_state_reconcile import repair_declarative_schema, schema_drift_reason+ # A missing canonical sessions table is not additive upgrade drift: creating+ # an empty one could hide lost history. Preserve corruption/recovery routing.reason = _db_opens_cleanly(db_path)repair = repair_state_db_schemaif reason is None:diff --git a/hermes_state_reconcile.py b/hermes_state_reconcile.pyindex 88bfe0ce5e8..feb1b3cdbf9 100644--- a/hermes_state_reconcile.py+++ b/hermes_state_reconcile.py@@ -30,7 +30,7 @@ def _reconcile_snapshot(path, guard):_copy_database_snapshot, _db_opens_cleanly, _repair_conn,_repair_scratch_space_error, _unlink_db_triple,)- from hermes_state_schema import reconcile_state_schema+ from hermes_state_schema import SessionSchemaMixin, reconcile_state_schemascratch = path.with_name(path.name + '.reconcile-scratch')if error := _repair_scratch_space_error(path):@@ -40,6 +40,10 @@ def _reconcile_snapshot(path, guard):try:_copy_database_snapshot(path, scratch, source_connection=guard)with _repair_conn(scratch) as 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.+ object.__new__(SessionSchemaMixin)._reconcile_columns(conn.cursor())reconcile_state_schema(conn)conn.commit()if reason := schema_drift_reason(scratch):@@ -61,6 +65,11 @@ def _reconcile_locked(path, backup, report):if _live_writer_holds_db(path):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')if schema_drift_reason(path) is None:report.update(repaired=True, strategy='already_healthy')returndiff --git a/tests/hermes_cli/test_sessions_repair_declarative.py b/tests/hermes_cli/test_sessions_repair_declarative.pyindex 563e29826fe..c14d79a5d1c 100644--- a/tests/hermes_cli/test_sessions_repair_declarative.py+++ b/tests/hermes_cli/test_sessions_repair_declarative.py@@ -81,6 +81,38 @@ def test_canonical_store_is_unchanged(tmp_path, monkeypatch, with_history):assert path.read_bytes() == before[email protected]('missing', ['sessions', 'messages'])+def test_missing_history_table_is_not_recreated_as_empty(tmp_path, monkeypatch, missing):+ from hermes_state_reconcile import repair_declarative_schema+ path = tmp_path / 'state.db'+ kept = 'messages' if missing == 'sessions' else 'sessions'+ with closing(sqlite3.connect(path)) as conn:+ conn.execute(f'CREATE TABLE {kept} (id TEXT)')+ conn.execute(f"INSERT INTO {kept} VALUES ('retain-me')")+ conn.commit()+ before = path.read_bytes()+ 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:+ db.create_session('retained', source='cli')+ db.append_message('retained', role='user', content='history survives')+ monkeypatch.setattr('hermes_state.DEFAULT_DB_PATH', path)+ with closing(sqlite3.connect(path)) as conn:+ conn.execute('DROP INDEX idx_async_delegations_delivery')+ conn.execute('ALTER TABLE async_delegations DROP COLUMN completed_at')+ conn.commit()+ assert sessions_cmd._cmd_repair(SimpleNamespace(check_only=False, no_backup=False)) in (None, 0)+ with _open_session_db_at_path(path, read_only=True) as db:+ assert [row['content'] for row in db.get_messages('retained')] == ['history survives']+ assert db._conn.execute("SELECT name FROM sqlite_master WHERE name='idx_async_delegations_delivery'").fetchone()++@pytest.mark.parametrize('backup', [True, False])def test_explicit_repair_restores_readonly_history(tmp_path, monkeypatch, backup):path = _legacy_store(tmp_path, monkeypatch)