From 0bb27d37cfbad99ea05365510c431d2fe5a5a1ea Mon Sep 17 00:00:00 2001 From: liuhao1024 Date: Tue, 25 Aug 2026 19:31:08 +0800 Subject: [PATCH] fix(state): declare origin_session_id in the canonical async_delegations schema The delegation tool carries its own CREATE TABLE for async_delegations (tools/async_delegation.py _initialize_schema) plus a lazy ALTER TABLE ADD COLUMN for the tables it finds already existing. Its column list had drifted ahead of the canonical SCHEMA_SQL: origin_session_id (raw api_server session id of the originating request, the wake self-post target) existed only through the tool's lazy path, so two databases at the same schema_version had different async_delegations shapes depending solely on whether the delegation tool had ever run. Rebuild/replay pipelines that reconstruct state.db from the canonical schema then hit the column with no version gate to explain it (#94691). Declare the column in SCHEMA_SQL with the same TEXT NOT NULL DEFAULT '' shape the tool uses. Fresh installs now carry it canonically; the declarative _reconcile_columns backfills it into legacy databases on the next writable open (same pattern as earlier additive columns); the tool's lazy ALTER keeps serving pre-reconciliation databases. The two schema authorities now agree, pinned by a test that runs the tool's initializer over a canonical database and asserts the shape is unchanged. Fixes #94691 --- hermes_state_common.py | 11 +++++- tests/test_hermes_state.py | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/hermes_state_common.py b/hermes_state_common.py index fab7a1b205..103920c8ee 100644 --- a/hermes_state_common.py +++ b/hermes_state_common.py @@ -502,7 +502,16 @@ CREATE TABLE IF NOT EXISTS async_delegations ( owner_started_at INTEGER, task_json TEXT, delivery_claim TEXT, - delivery_claimed_at REAL + delivery_claimed_at REAL, + -- Mirrors the delegation tool's own CREATE TABLE (tools/async_delegation.py + -- _initialize_schema). Keeping the canonical fresh-install shape identical + -- to the tool's avoids a silent schema drift: the tool's lazy + -- ALTER TABLE ADD COLUMN used to be the only source of this column, so two + -- databases at the same schema_version had different + -- async_delegations shapes depending on whether the delegation tool had + -- ever run, breaking rebuild/replay pipelines that reconstruct state.db + -- from the canonical schema (#94691). + origin_session_id TEXT NOT NULL DEFAULT '' ); CREATE INDEX IF NOT EXISTS idx_sessions_source ON sessions(source); diff --git a/tests/test_hermes_state.py b/tests/test_hermes_state.py index e0180a6c37..ca3e2ab7e2 100644 --- a/tests/test_hermes_state.py +++ b/tests/test_hermes_state.py @@ -1887,6 +1887,81 @@ 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. + """ + + def _async_delegations_columns(self, conn): + import sqlite3 + + rows = conn.execute( + "PRAGMA table_info(async_delegations)" + ).fetchall() + return {row[1] for row in rows} + + def test_fresh_db_and_tool_schema_agree(self, tmp_path): + import sqlite3 + + from tools.async_delegation import _initialize_schema + + db_path = tmp_path / "state.db" + db = SessionDB(db_path=db_path) + db.close() + + 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 + _initialize_schema(conn) + conn.commit() + after_cols = self._async_delegations_columns(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_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.""" + import sqlite3 + + from hermes_state_common import SCHEMA_SQL + + 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) + finally: + conn.close() + + db = SessionDB(db_path=db_path) + try: + cols = self._async_delegations_columns(db._conn) + assert "origin_session_id" in cols + finally: + db.close() + + class TestReconcileColumnsErrorHandling: """_reconcile_columns must not bury migration failures (#79531/#80037).