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
This commit is contained in:
liuhao1024
2026-08-25 19:31:08 +08:00
committed by Teknium
parent a239c4f811
commit 0bb27d37cf
2 changed files with 85 additions and 1 deletions
+10 -1
View File
@@ -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);
+75
View File
@@ -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).