The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(state): one apply_declared_schema for every raw schema catch-up
Teknium48b512cb25f7
diff --git a/hermes_state_reconcile.py b/hermes_state_reconcile.pyindex 32be34edc54..4beac40299a 100644--- a/hermes_state_reconcile.py+++ b/hermes_state_reconcile.py@@ -36,7 +36,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 SessionSchemaMixin, reconcile_state_schema+ from hermes_state_schema import apply_declared_schemascratch = path.with_name(path.name + '.reconcile-scratch')if error := _repair_scratch_space_error(path):@@ -47,11 +47,8 @@ def _reconcile_snapshot(path, guard):_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.- object.__new__(SessionSchemaMixin)._reconcile_columns(conn.cursor())- reconcile_state_schema(conn)+ # The final probe fails closed if an additive change is not possible.+ apply_declared_schema(conn.cursor())conn.commit()if reason := schema_drift_reason(scratch):raise RuntimeError(f'schema reconciliation incomplete: {reason}')@@ -228,9 +225,8 @@ def _apply_additive_schema(path: Path, budget: _Budget | None = None) -> None:the shared reconciler could not add (it logs and continues) or an expired budget rolls thestore back to exactly what it was, never a partial upgrade."""from hermes_cli.sqlite_safe_read import connect_tracked- from hermes_state_common import SCHEMA_SQLfrom hermes_state_repair import _reapply_durability_barriers, _schema_not_built- from hermes_state_schema import SessionSchemaMixin, schema_read_probe_statements+ from hermes_state_schema import apply_declared_schema, schema_read_probe_statementsbudget = budget or _Budget(BROWSE_RECONCILE_BUDGET_S)budget.check()@@ -241,11 +237,7 @@ def _apply_additive_schema(path: Path, budget: _Budget | None = None) -> None:conn.execute('BEGIN IMMEDIATE')try:cursor = conn.cursor()- shim = object.__new__(SessionSchemaMixin)- # Columns first: SCHEMA_SQL indexes may name columns an old table lacks.- shim._reconcile_columns(cursor)- SessionSchemaMixin._execute_ddl_script_transactional(cursor, SCHEMA_SQL)- shim._reconcile_columns(cursor) # tables the script just created are complete; no-op+ apply_declared_schema(cursor)# Postcondition, still inside the transaction: every declared table/column resolves.for statement in schema_read_probe_statements():try:diff --git a/hermes_state_schema.py b/hermes_state_schema.pyindex c10288a9731..36c4b1db5a6 100644--- a/hermes_state_schema.py+++ b/hermes_state_schema.py@@ -1350,8 +1350,21 @@ class SessionSchemaMixin:)+def apply_declared_schema(cursor: sqlite3.Cursor) -> None:+ """The one additive catch-up order for raw ``state.db`` connections: columns (SCHEMA_SQL indexes may+ name columns an old table lacks), then the canonical DDL statement by statement (no+ ``executescript`` implicit COMMIT, so callers keep their own transaction), then columns again+ (a no-op for tables the script just created whole). Locking, snapshots and commit stay with the+ callers. ``_reconcile_columns`` only touches the staticmethod ``_parse_schema_columns``, so a bare+ instance is enough and the column reconciler keeps one implementation."""+ shim = object.__new__(SessionSchemaMixin)+ shim._reconcile_columns(cursor)+ SessionSchemaMixin._execute_ddl_script_transactional(cursor, SCHEMA_SQL)+ shim._reconcile_columns(cursor)++def reconcile_state_schema(conn: sqlite3.Connection) -> None:- """Bring a raw ``state.db`` connection to the canonical SCHEMA_SQL shape.+ """Bring a raw ``state.db`` connection to the canonical SCHEMA_SQL shape and commit.Single durable-shape authority for callers that open ``state.db``outside SessionDB. The async-delegation tool used to carry its own@@ -1359,14 +1372,8 @@ def reconcile_state_schema(conn: sqlite3.Connection) -> None:from SCHEMA_SQL (same-name columns with different nullability/defaultsdepending on which authority touched the database first, #94691). Thishelper instead replays the canonical DDL (every statement is- IF NOT EXISTS/idempotent) and reuses SessionDB's declarative column- reconciliation, so out-of-band openers can never grow a second- hand-maintained shape for the same durable tables.+ IF NOT EXISTS/idempotent) through :func:`apply_declared_schema`."""- conn.executescript(SCHEMA_SQL)- # _reconcile_columns only touches the staticmethod _parse_schema_columns,- # so a bare instance works; reusing it keeps one reconciliation- # implementation (one authority) instead of a near-copy on raw- # connections.- shim = object.__new__(SessionSchemaMixin)- shim._reconcile_columns(conn.cursor())+ apply_declared_schema(conn.cursor())+ if conn.in_transaction:+ conn.commit()diff --git a/tests/hermes_cli/test_browse_schema_drift.py b/tests/hermes_cli/test_browse_schema_drift.pyindex e5e34bad0bb..6a2c5244a67 100644--- a/tests/hermes_cli/test_browse_schema_drift.py+++ b/tests/hermes_cli/test_browse_schema_drift.py@@ -994,3 +994,18 @@ def test_read_only_lagging_store_gets_a_permissions_message(tmp_path):assert "nothing was changed" in detailassert "restart" not in detail.lower()assert _file_state(path) == before+++def test_every_raw_schema_catch_up_handles_an_old_indexed_column(tmp_path):+ """alt-glitch S1: three hand-ordered catch-ups (reconcile_state_schema, the repair snapshot and the+ browse DDL) each encoded the columns -> SCHEMA_SQL -> columns order. One apply_declared_schema+ now owns it; every raw caller catches an old store (indexed column missing) up the same way."""+ import hermes_state_schema+ from hermes_state_schema import reconcile_state_schema++ path = _lagging_store_with_history(_profile_home(tmp_path), rows=5)+ with closing(sqlite3.connect(path)) as conn:+ reconcile_state_schema(conn)+ assert not conn.in_transaction+ assert "last_read_at" in _session_columns(path)+ assert hasattr(hermes_state_schema, "apply_declared_schema")diff --git a/tests/hermes_cli/test_sessions_repair_declarative.py b/tests/hermes_cli/test_sessions_repair_declarative.pyindex abe51d5b916..b937dc0d1bc 100644--- a/tests/hermes_cli/test_sessions_repair_declarative.py+++ b/tests/hermes_cli/test_sessions_repair_declarative.py@@ -59,12 +59,12 @@ def test_refused_reconciliation_preserves_original(tmp_path, monkeypatch, blockeif blocker == 'owner':owner.reserve([tmp_path])elif blocker == 'ddl':- def fail(conn):- conn.execute('CREATE TABLE only_in_scratch (id INTEGER)')+ def fail(cursor):+ cursor.execute('CREATE TABLE only_in_scratch (id INTEGER)')raise sqlite3.OperationalError('injected DDL refusal')- monkeypatch.setattr(schema, 'reconcile_state_schema', fail)+ monkeypatch.setattr(schema, 'apply_declared_schema', fail)elif blocker == 'incomplete':- monkeypatch.setattr(schema, 'reconcile_state_schema', lambda conn: None)+ monkeypatch.setattr(schema, 'apply_declared_schema', lambda cursor: None)else:monkeypatch.setattr(repair, '_backup_db_file', lambda path: (None, 'injected backup refusal'))try: