Skip to content

The agent that grows with you

fix(cli): skip created_source on read-only pre-column stores

David Dudok de Witbd7903d0b2d4
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index 89b07bdf1c3..58255bd6d1b 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -19,6 +19,7 @@ import argparse
import logging
import os
import re
+import sqlite3
import uuid
from hermes_cli.gateway_client import GatewayClientError
@@ -352,18 +353,43 @@ class SurfaceLabelReader:
The surface predicate runs in SQL before LIMIT/OFFSET. Filtering a fixed over-fetch of every
``local`` row dropped CLI sessions once newer Desktop/TUI rows filled that window.
+
+ 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.
"""
def __init__(self, db):
self._wrapped = db
+ self._created_source_column: bool | None = None
def __getattr__(self, name):
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
+ 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
+ return present
+
def _labelled(self, requested, limit, offset, call):
wanted = {requested} if isinstance(requested, str) else set(requested or ())
surfaces = wanted & _LOCAL_LABELLED
- if not surfaces:
+ # No column: naming ``created_source`` raises, and the read-only caller swallows that
+ # into an empty page. Match the requested labels only, as the old filter did when the
+ # key was absent (a ``local`` row could not prove its surface).
+ if not surfaces or not self._has_created_source_column():
return call(requested, limit, offset, None)
# ``local`` is the gateway label; ``created_source`` is the surface. Page the rows this
# surface can see, not a window of every local row.
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 22f9ff95475..d3810de1e55 100644
--- a/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py
+++ b/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py
@@ -258,3 +258,71 @@ def test_sessions_list_stays_a_recency_page_across_surfaces(tmp_path, capsys):
assert "cli-session" in full
assert "desk-059" in full
db.close()
+
+
+def _column_names(db: SessionDB) -> set[str]:
+ return {row[1] for row in db._conn.execute("PRAGMA table_info(sessions)")}
+
+
+def _pre_column_readonly(tmp_path) -> SessionDB:
+ """A store last written before ``created_source``, reopened read-only.
+
+ Writable open adds the column. Bare ``-c``, ``--resume latest``, ``/sessions``
+ and ``hermes sessions list`` never do that.
+ """
+ path = tmp_path / "state.db"
+ writer = SessionDB(db_path=path)
+ writer.create_session("legacy-cli", source="cli")
+ writer.append_message("legacy-cli", role="user", content="hello from cli")
+ with writer._lock:
+ writer._conn.execute("DROP INDEX IF EXISTS idx_sessions_local_created_started")
+ writer._conn.execute("ALTER TABLE sessions DROP COLUMN created_source")
+ writer._conn.commit()
+ writer.close()
+ reader = SessionDB(db_path=path, read_only=True)
+ assert "created_source" not in _column_names(reader)
+ return reader
+
+
+def test_readonly_pre_column_continue_finds_legacy_cli_session(tmp_path, monkeypatch):
+ """Bare ``-c`` and ``--resume latest`` share ``_resolve_last_session`` and open read-only.
+
+ A missing ``created_source`` column must not become "no previous session".
+ The lookup must not migrate the file.
+ """
+ _pre_column_readonly(tmp_path).close()
+
+ assert _resolve_cli(monkeypatch, tmp_path) == "legacy-cli"
+ healed = SessionDB(db_path=tmp_path / "state.db", read_only=True)
+ try:
+ assert "created_source" not in _column_names(healed)
+ finally:
+ healed.close()
+
+
+def test_readonly_pre_column_session_listing_finds_legacy_cli_session(tmp_path):
+ """In-chat ``/sessions`` and ``/resume`` list the legacy CLI row on a pre-column store."""
+ db = _pre_column_readonly(tmp_path)
+ try:
+ listed, via_command = _recent_cli_ids(db)
+ assert listed == ["legacy-cli"]
+ assert via_command == ["legacy-cli"]
+ assert "created_source" not in _column_names(db)
+ finally:
+ db.close()
+
+
+def test_readonly_sessions_list_shows_pre_column_cli_row_without_migrating(tmp_path, capsys):
+ """Control: ``hermes sessions list`` is read-only and does not use the surface predicate.
+
+ It still prints the legacy row, and it does not add ``created_source``.
+ """
+ from hermes_cli.sessions_cmd import _cmd_list
+
+ db = _pre_column_readonly(tmp_path)
+ try:
+ _cmd_list(db, SimpleNamespace(limit=20, source=None, workspace=None))
+ assert "legacy-cli" in capsys.readouterr().out
+ assert "created_source" not in _column_names(db)
+ finally:
+ db.close()