Skip to content

The agent that grows with you

fix(state): walk the message_uid backfill by rowid so TEXT-id legacy stores open

liuhao1024ac33a01627c9
diff --git a/hermes_state_schema.py b/hermes_state_schema.py
index 36c4b1db5a6..edde611e980 100644
--- a/hermes_state_schema.py
+++ b/hermes_state_schema.py
@@ -1119,21 +1119,26 @@ class SessionSchemaMixin:
"""Mint ``message_uid`` onto rows written before the column existed, one bounded slice per open.
A single full-table UPDATE held the write lock for 261 s on an 835k-row / 4.8 GB store, so every
- sibling process timed out with "database is locked". Id-range chunks commit one at a time (the
+ sibling process timed out with "database is locked". Rowid-range chunks commit one at a time (the
writer connection is autocommit) and an open spends at most ``_MESSAGE_UID_BACKFILL_BUDGET_S``
after its first chunk, so a large store converges over later opens. Until then a legacy row
- simply has no uid, which every reader already treats as "no identity yet"."""
+ simply has no uid, which every reader already treats as "no identity yet".
+
+ The walk is keyed on ``rowid``, not ``id``: on the declared table ``id INTEGER PRIMARY KEY`` is the
+ rowid alias (same plan), but a store converted from JSONL still carries ``id TEXT`` with no PRIMARY
+ KEY (``_reconcile_columns`` adds columns, never rebuilds the key), where ``MAX(id)`` is a str and an
+ ``id`` range compares lexicographically (#141467)."""
if _meta_row(cursor, _MESSAGE_UID_BACKFILL_DONE) is not None:
return
row = _meta_row(cursor, _MESSAGE_UID_BACKFILL_CURSOR)
done_through = int(row[0]) if row else 0
- high = cursor.execute("SELECT MAX(id) FROM messages").fetchone()[0] or 0
+ high = cursor.execute("SELECT MAX(rowid) FROM messages").fetchone()[0] or 0
deadline = time.monotonic() + _MESSAGE_UID_BACKFILL_BUDGET_S
while done_through < high:
upper = done_through + _MESSAGE_UID_BACKFILL_CHUNK
cursor.execute(
"UPDATE messages SET message_uid = lower(hex(randomblob(16))) "
- "WHERE id > ? AND id <= ? AND message_uid IS NULL", (done_through, upper))
+ "WHERE rowid > ? AND rowid <= ? AND message_uid IS NULL", (done_through, upper))
done_through = upper
if done_through < high and time.monotonic() >= deadline:
self.set_meta(_MESSAGE_UID_BACKFILL_CURSOR, str(done_through), cursor=cursor)
diff --git a/tests/hermes_state/test_message_uid.py b/tests/hermes_state/test_message_uid.py
index 6dd2cd85dca..8819ab0270c 100644
--- a/tests/hermes_state/test_message_uid.py
+++ b/tests/hermes_state/test_message_uid.py
@@ -20,6 +20,25 @@ from hermes_state import SessionDB
UID_RE = re.compile(r"^[0-9a-f]{32}$")
+# What a JSONL→SQLite conversion left behind once column reconciliation added the declared columns:
+# ``id TEXT`` without a PRIMARY KEY, every column nullable, schema_version short of the uid backfill.
+_JSONL_CONVERTED_STORE_SQL = """
+CREATE TABLE schema_version (version INTEGER NOT NULL);
+INSERT INTO schema_version VALUES (30);
+CREATE TABLE messages (id TEXT, session_id TEXT, role TEXT, content TEXT, tool_call_id TEXT,
+ tool_calls TEXT, tool_name TEXT, timestamp TEXT, token_count TEXT, finish_reason TEXT,
+ reasoning TEXT, reasoning_content TEXT, reasoning_details TEXT, codex_reasoning_items TEXT,
+ codex_message_items TEXT, platform_message_id TEXT, observed TEXT, active TEXT,
+ compacted INTEGER NOT NULL DEFAULT 0, effect_disposition TEXT, api_content TEXT,
+ display_kind TEXT, display_metadata TEXT, _compressed_summary INTEGER NOT NULL DEFAULT 0,
+ display_identity BLOB, display_order INTEGER, message_uid TEXT,
+ absorbed_message_uids TEXT, tool_call_uids TEXT, tool_call_uid TEXT);
+INSERT INTO messages (id, session_id, role, content, timestamp) VALUES
+ ('11022', 's', 'user', 'hi', '1779587405.0'),
+ ('11023', 's', 'assistant', 'hello', '1779587406.0'),
+ ('5078', 's', 'user', 'older row whose text id sorts last', '1779587300.0');
+"""
+
@pytest.fixture()
def db(tmp_path):
@@ -101,6 +120,30 @@ class TestMintAndRestore:
assert all(UID_RE.match(u) for u in seen[2]) and len(set(seen[2])) == 5
assert done == "1"
+ def test_a_jsonl_converted_store_with_text_ids_opens_and_backfills(self, tmp_path, monkeypatch):
+ """``_reconcile_columns`` adds the declared columns but never rebuilds the key, so a converted store
+ keeps ``id TEXT`` with no PRIMARY KEY: ``MAX(id)`` is a str and an ``id`` range compares as text.
+ The walk keys on rowid, so the open does not raise and every row is minted, whatever its id sorts
+ like (#141467)."""
+ import sqlite3
+
+ import hermes_state_schema
+
+ monkeypatch.setattr(hermes_state_schema, "_MESSAGE_UID_BACKFILL_CHUNK", 2)
+ path = tmp_path / "legacy.db"
+ conn = sqlite3.connect(path)
+ conn.executescript(_JSONL_CONVERTED_STORE_SQL)
+ conn.close()
+ handle = SessionDB(db_path=path)
+ try:
+ rows = handle._conn.execute("SELECT id, message_uid FROM messages ORDER BY rowid").fetchall()
+ done = handle.get_meta("message_uid_backfill")
+ finally:
+ handle.close()
+ assert [r[0] for r in rows] == ["11022", "11023", "5078"]
+ assert all(UID_RE.match(u) for _, u in rows) and len({u for _, u in rows}) == 3
+ assert done == "1"
+
def test_a_row_inserted_without_a_uid_by_an_older_writer_gets_one(self, db):
"""The store mints for a build that predates the column (its INSERT binds no uid)."""
db.create_session("s", "cli", model="test/model")