The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): single durable-shape authority for async_delegations
liuhao1024a6d65cdd09d7
diff --git a/hermes_state_schema.py b/hermes_state_schema.pyindex 877636d87a0..996c5163f52 100644--- a/hermes_state_schema.py+++ b/hermes_state_schema.py@@ -1243,3 +1243,25 @@ class SessionSchemaMixin:1 if entry.get("expiry_finalized") or entry.get("memory_flushed") else 0, str(session_id),),)+++def reconcile_state_schema(conn: sqlite3.Connection) -> None:+ """Bring a raw ``state.db`` connection to the canonical SCHEMA_SQL shape.++ Single durable-shape authority for callers that open ``state.db``+ outside SessionDB. The async-delegation tool used to carry its own+ CREATE TABLE and ALTER column list for ``async_delegations``; it drifted+ from SCHEMA_SQL (same-name columns with different nullability/defaults+ depending on which authority touched the database first, #94691). This+ helper 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.+ """+ 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())diff --git a/tests/test_hermes_state.py b/tests/test_hermes_state.pyindex ca3e2ab7e2e..c0e3abf2015 100644--- a/tests/test_hermes_state.py+++ b/tests/test_hermes_state.py@@ -1888,26 +1888,84 @@ class TestSchemaInit:class TestAsyncDelegationsSchemaAgreement:- """SCHEMA_SQL and the delegation tool's own DDL must declare the same- shape (#94691).-- The delegation tool carries its own CREATE TABLE for async_delegations- (plus a lazy ALTER for pre-existing tables). When its column list drifts- ahead of the canonical SCHEMA_SQL, two databases at the same- schema_version end up with different async_delegations shapes depending- only on whether the delegation tool ever ran — breaking rebuild/replay- pipelines that reconstruct state.db from the canonical schema.+ """One durable-shape authority for async_delegations (#94691).++ The delegation tool used to carry its own CREATE TABLE + ALTER column+ list; it drifted from SCHEMA_SQL (a same-name column with a different+ shape depending on which authority touched the database first). The+ tool's initializer now routes through the canonical reconciler, so+ every opening order must land on the same canonical shape — compared+ over FULL PRAGMA table_info metadata (type, notnull, dflt_value, pk),+ not just column names, and with the canonical index set pinned."""- def _async_delegations_columns(self, conn):+ def _table_info(self, conn):+ return {+ row[1]: (row[2], row[3], row[4], row[5])+ for row in conn.execute(+ "PRAGMA table_info(async_delegations)"+ ).fetchall()+ }++ def _canonical_shape(self):+ ref = __import__("sqlite3").connect(":memory:")+ try:+ from hermes_state_common import SCHEMA_SQL++ ref.executescript(SCHEMA_SQL)+ return self._table_info(ref), {+ row[0]+ for row in ref.execute(+ "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='async_delegations' AND sql IS NOT NULL"+ ).fetchall()+ }+ finally:+ ref.close()++ def _legacy_db(self, db_path):+ """A database created before origin_session_id existed, carrying a+ pre-existing delegation row that must survive every opening order."""import sqlite3- rows = conn.execute(- "PRAGMA table_info(async_delegations)"- ).fetchall()- return {row[1] for row in rows}+ from hermes_state_common import SCHEMA_SQL++ legacy_sql = SCHEMA_SQL.replace(+ " origin_session_id TEXT NOT NULL DEFAULT ''\n", ""+ ).replace(+ " delivery_claimed_at REAL,\n",+ " delivery_claimed_at REAL\n",+ )+ conn = sqlite3.connect(db_path)+ try:+ conn.executescript(legacy_sql)+ conn.execute(+ "INSERT INTO async_delegations (delegation_id, origin_session, origin_ui_session_id, state, dispatched_at, updated_at) VALUES ('legacy-1', 'sess-a', '', 'completed', 1.0, 1.0)"+ )+ conn.commit()+ finally:+ conn.close()++ def _assert_canonical(self, conn):+ expected_cols, expected_indexes = self._canonical_shape()+ live_cols = self._table_info(conn)+ assert live_cols == expected_cols+ live_indexes = {+ row[0]+ for row in conn.execute(+ "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='async_delegations' AND sql IS NOT NULL"+ ).fetchall()+ }+ assert live_indexes == expected_indexes+ row = conn.execute(+ "SELECT delegation_id, IFNULL(origin_session_id, '<null>') FROM async_delegations WHERE delegation_id='legacy-1'"+ ).fetchone()+ if row is not None:+ # The legacy row survived and the canonical '' default+ # backfilled the added column (SQLite ADD COLUMN ... DEFAULT+ # populates existing rows with the default).+ assert row[1] == ""- def test_fresh_db_and_tool_schema_agree(self, tmp_path):+ def test_fresh_session_db_then_tool(self, tmp_path):import sqlite3from tools.async_delegation import _initialize_schema@@ -1918,46 +1976,42 @@ class TestAsyncDelegationsSchemaAgreement:conn = sqlite3.connect(db_path)try:- canonical_cols = self._async_delegations_columns(conn)- # The drift column from the tool's DDL is present from the- # canonical fresh-install shape (#94691).- assert "origin_session_id" in canonical_cols+ shape_before = self._table_info(conn)_initialize_schema(conn)conn.commit()- after_cols = self._async_delegations_columns(conn)+ assert self._table_info(conn) == shape_before+ self._assert_canonical(conn)finally:conn.close()- # Running the tool's schema initializer over a canonical database- # must not change the table's shape — the two authorities agree.- assert after_cols == canonical_cols+ def test_legacy_store_then_session_db(self, tmp_path):+ db_path = tmp_path / "legacy-state.db"+ self._legacy_db(db_path)++ db = SessionDB(db_path=db_path)+ try:+ self._assert_canonical(db._conn)+ finally:+ db.close()- def test_reconcile_backfills_drift_column_on_pre_tool_db(self, tmp_path):- """A database created before the column existed (canonical schema- without it, delegation tool never run) gets it backfilled by the- declarative reconciliation on the next writable open."""+ def test_legacy_store_then_tool_then_session_db(self, tmp_path):import sqlite3- from hermes_state_common import SCHEMA_SQL+ from tools.async_delegation import _initialize_schema++ db_path = tmp_path / "legacy-tool-state.db"+ self._legacy_db(db_path)- db_path = tmp_path / "legacy-state.db"- legacy_sql = SCHEMA_SQL.replace(- " origin_session_id TEXT NOT NULL DEFAULT ''\n", ""- ).replace(- " delivery_claimed_at REAL,\n",- " delivery_claimed_at REAL\n",- )conn = sqlite3.connect(db_path)try:- conn.executescript(legacy_sql)- assert "origin_session_id" not in self._async_delegations_columns(conn)+ _initialize_schema(conn)+ conn.commit()finally:conn.close()db = SessionDB(db_path=db_path)try:- cols = self._async_delegations_columns(db._conn)- assert "origin_session_id" in cols+ self._assert_canonical(db._conn)finally:db.close()diff --git a/tools/async_delegation.py b/tools/async_delegation.pyindex 27fd4523fd4..25d1f7176f4 100644--- a/tools/async_delegation.py+++ b/tools/async_delegation.py@@ -96,37 +96,18 @@ def _connect() -> sqlite3.Connection:def _initialize_schema(conn: sqlite3.Connection) -> None:from hermes_state_repair import apply_durability_barriers+ from hermes_state_schema import reconcile_state_schema# Preserve the journal mode SessionDB configured on state.db: forcing WAL from# every short-lived connection collides with live transcript/FTS writers.apply_durability_barriers(conn)- conn.execute("""CREATE TABLE IF NOT EXISTS async_delegations (- delegation_id TEXT PRIMARY KEY,- origin_session TEXT NOT NULL,- origin_ui_session_id TEXT NOT NULL DEFAULT '',- parent_session_id TEXT,- state TEXT NOT NULL,- dispatched_at REAL NOT NULL,- completed_at REAL,- updated_at REAL NOT NULL,- event_json TEXT,- result_json TEXT,- delivery_state TEXT NOT NULL DEFAULT 'pending',- delivery_attempts INTEGER NOT NULL DEFAULT 0,- delivered_at REAL,- owner_pid INTEGER,- owner_started_at INTEGER,- task_json TEXT,- delivery_claim TEXT,- delivery_claimed_at REAL,- origin_session_id TEXT NOT NULL DEFAULT ''- )""")- columns = {row[1] for row in conn.execute("PRAGMA table_info(async_delegations)")}- # origin_session_id: raw api_server session id of the ORIGINATING request- # (wake target); without it restart-recovered completions are unroutable there.- for name, sql_type in (("owner_pid", "INTEGER"), ("owner_started_at", "INTEGER"), ("task_json", "TEXT"),- ("delivery_claim", "TEXT"), ("delivery_claimed_at", "REAL"), ("origin_session_id", "TEXT")):- if name not in columns:- conn.execute(f"ALTER TABLE async_delegations ADD COLUMN {name} {sql_type}")+ # Single durable-shape authority: the canonical SCHEMA_SQL drives both+ # table creation and column backfill (reconcile_state_schema replays the+ # canonical DDL and reuses SessionDB's declarative reconciliation). This+ # module previously carried its own CREATE TABLE + ALTER column list,+ # which drifted from SCHEMA_SQL — same-name columns with different+ # nullability/defaults depending on which authority touched the database+ # first (#94691).+ reconcile_state_schema(conn)@contextmanager