Skip to content

The agent that grows with you

fix(cli): do not cache a failed or negative schema probe

David Dudok de Wit33c5256897df
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index 58255bd6d1b..79c1400474b 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -19,7 +19,6 @@ import argparse
import logging
import os
import re
-import sqlite3
import uuid
from hermes_cli.gateway_client import GatewayClientError
@@ -356,7 +355,8 @@ class SurfaceLabelReader:
Read-only opens do not migrate. A store that has no ``created_source`` column is matched on
``source`` alone (there are no gateway ``local`` rows yet). ``created_source='unknown'`` stays
- unmatched once the column exists.
+ unmatched once the column exists. Only a successful "column present" probe is cached: a lock
+ error is not a missing column, and another connection can add the column later.
"""
def __init__(self, db):
@@ -367,20 +367,23 @@ class SurfaceLabelReader:
return getattr(self._wrapped, name)
def _has_created_source_column(self) -> bool:
- """Cached ``PRAGMA table_info`` read. A connection does not gain the column after open."""
- if self._created_source_column is not None:
- return self._created_source_column
- present = False
+ """Whether ``sessions.created_source`` is on this connection.
+
+ A positive answer is cached. An absent answer is re-probed: a later
+ ``ALTER TABLE`` from another connection is visible to the next statement.
+ ``sqlite3.Error`` (lock, I/O) propagates and is not stored — it is not
+ evidence the column is missing. This read does not write.
+ """
+ if self._created_source_column is True:
+ return True
conn = getattr(self._wrapped, "_conn", None)
- if conn is not None:
- try:
- present = any(
- row[1] == "created_source"
- for row in conn.execute("PRAGMA table_info(sessions)")
- )
- except sqlite3.Error:
- present = False
- self._created_source_column = present
+ if conn is None:
+ return False
+ present = any(
+ row[1] == "created_source" for row in conn.execute("PRAGMA table_info(sessions)")
+ )
+ if present:
+ self._created_source_column = True
return present
def _labelled(self, requested, limit, offset, call):
diff --git a/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py b/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py
index d3810de1e55..b9b3124cc96 100644
--- a/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py
+++ b/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py
@@ -12,6 +12,7 @@ placeholder on upsert; this reader does not admit it.
from __future__ import annotations
+import sqlite3
from types import SimpleNamespace
from hermes_state import SessionDB
@@ -326,3 +327,70 @@ def test_readonly_sessions_list_shows_pre_column_cli_row_without_migrating(tmp_p
assert "created_source" not in _column_names(db)
finally:
db.close()
+
+
+def test_locked_schema_probe_is_not_remembered_as_a_missing_column(tmp_path):
+ """A ``PRAGMA table_info`` failure is not proof the column is missing.
+
+ Caching that failure makes the next lookup, after the lock is gone, match
+ ``source`` only and drop the newer ``source='local'`` / ``created_source='cli'``
+ row. The error has to surface, and the following call has to see the column.
+ """
+ db = SessionDB(db_path=tmp_path / "state.db")
+ _populate(db, [
+ _cli_row("legacy-cli", 1_000_000, source="cli", created="cli"),
+ _cli_row("local-cli", 4_000_000_000),
+ ])
+ from hermes_cli.gateway_session_store import SurfaceLabelReader
+
+ reader = SurfaceLabelReader(db)
+ real_execute = db._conn.execute
+ raised = {"done": False}
+
+ def execute(sql, *args, **kwargs):
+ if isinstance(sql, str) and "table_info" in sql and not raised["done"]:
+ raised["done"] = True
+ raise sqlite3.OperationalError("database is locked")
+ return real_execute(sql, *args, **kwargs)
+
+ db._conn.execute = execute
+ caught = None
+ try:
+ first = [row["id"] for row in reader.search_sessions(source=["cli", "oneshot"], limit=1)]
+ except sqlite3.OperationalError as exc:
+ caught = str(exc)
+ first = None
+ assert caught and "database is locked" in caught, first
+ assert reader._created_source_column is None
+ assert [row["id"] for row in reader.search_sessions(source=["cli", "oneshot"], limit=1)] == ["local-cli"]
+ db.close()
+
+
+def test_absent_schema_probe_sees_a_column_added_later(tmp_path):
+ """A successful "column absent" answer is not permanent.
+
+ In-chat ``/sessions`` keeps one reader. After another connection adds
+ ``created_source`` and a newer local CLI row, that same reader must return it.
+ """
+ db = _pre_column_readonly(tmp_path)
+ from hermes_cli.gateway_session_store import SurfaceLabelReader
+
+ reader = SurfaceLabelReader(db)
+ try:
+ assert [row["id"] for row in reader.search_sessions(source="cli", limit=5)] == ["legacy-cli"]
+ other = sqlite3.connect(tmp_path / "state.db")
+ try:
+ other.execute("ALTER TABLE sessions ADD COLUMN created_source TEXT")
+ other.execute(
+ "INSERT INTO sessions (id, source, created_source, started_at) VALUES (?, ?, ?, ?)",
+ ("local-new", "local", "cli", 4_000_000_000),
+ )
+ other.commit()
+ finally:
+ other.close()
+ found = [row["id"] for row in reader.search_sessions(source="cli", limit=5)]
+ assert found[0] == "local-new"
+ listed = [row["id"] for row in reader.list_sessions_rich(source="cli", limit=5)]
+ assert "local-new" in listed
+ finally:
+ db.close()