Skip to content

The agent that grows with you

refactor(cli): trim the surface-label reader and its tests

kshitijk4poorf37e164850dd
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index b0db618504a..d8f05dc3764 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -348,51 +348,32 @@ _LOCAL_LABELLED = frozenset({"cli", "tui", "gui"})
class SurfaceLabelReader:
"""A read-only ``SessionDB`` whose source-filtered listings read main's labels: ``source='cli'``
also matches a gateway row labelled ``local`` that the CLI created (``created_source='cli'``), so
- main's /resume picker, /sessions, /history and bare ``-c`` find the sessions the CLI made.
-
- 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. Only a successful "column present" probe is cached: a lock
- error is not a missing column, and another connection can add the column later.
+ main's /resume picker, /sessions, /history and bare ``-c`` find the sessions the CLI made. The
+ surface predicate runs in SQL, before LIMIT/OFFSET.
"""
def __init__(self, db):
self._wrapped = db
- self._created_source_column: bool | None = None
+ self._has_column = False
def __getattr__(self, name):
return getattr(self._wrapped, name)
def _has_created_source_column(self) -> bool:
- """Whether ``sessions.created_source`` is on this connection.
-
- The probe is a read, so it uses the same disk-I/O retry as every other
- read (``_read_retrying_ioerr``). A positive answer is cached. An absent
- answer is re-probed: a later ``ALTER TABLE`` from another connection is
- visible to the next statement. A lock or an I/O error that survives the
- retry budget propagates and is not stored. This read does not write.
- """
- if self._created_source_column is True:
- return True
- rows = self._wrapped._read_all("PRAGMA table_info(sessions)")
- present = any(row[1] == "created_source" for row in rows)
- if present:
- self._created_source_column = True
- return present
+ """Read-only opens don't migrate, so ``created_source`` may be missing. Only "present" is
+ cached: a lock or I/O error is not proof of absence, and another connection may add it."""
+ if not self._has_column:
+ rows = self._wrapped._read_all("PRAGMA table_info(sessions)")
+ self._has_column = any(row[1] == "created_source" for row in rows)
+ return self._has_column
def _labelled(self, requested, limit, offset, call):
wanted = {requested} if isinstance(requested, str) else set(requested or ())
surfaces = wanted & _LOCAL_LABELLED
- # 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).
+ # No column (and so no gateway ``local`` rows yet): naming it would raise, and the caller
+ # swallows that into an empty page.
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.
return call(sorted(wanted | {"local"}), limit, offset, sorted(surfaces))
def search_sessions(self, source=None, limit=20, offset=0, **kw):
diff --git a/hermes_state_common.py b/hermes_state_common.py
index 6ddf8e75acc..44ca96f6d6b 100644
--- a/hermes_state_common.py
+++ b/hermes_state_common.py
@@ -355,18 +355,13 @@ def _placeholders(items) -> str:
def _source_match_clause(sources, local_created_sources=None) -> tuple[str, list]:
- """``sessions s`` source predicate, applied before LIMIT/OFFSET.
-
- With ``local_created_sources``, a ``source='local'`` row matches only when
- ``created_source`` is one of those surfaces. Every other requested source
- matches on its own label, whatever ``created_source`` says. A placeholder
- ``created_source='unknown'`` is not a surface: it stays unmatched until an
- upsert stamps the real creator (#138838, #139058).
- """
+ """``sessions s`` source predicate. With ``local_created_sources``, a ``source='local'`` row
+ matches only when ``created_source`` is one of those surfaces; the placeholder ``'unknown'``
+ is not a surface and stays unmatched until an upsert stamps the real creator."""
labels = list(sources or [])
if not labels:
return "", []
- surfaces = [item for item in (local_created_sources or ()) if item]
+ surfaces = list(local_created_sources or ())
if not surfaces:
return f"s.source IN ({_placeholders(labels)})", labels
non_local = [item for item in labels if item != "local"]
diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py
index 0df19d18d3c..5fc8f6b1497 100644
--- a/hermes_state_sessions.py
+++ b/hermes_state_sessions.py
@@ -1565,14 +1565,12 @@ class SessionSessionsMixin:
self, source: str | None = None, sources: list[str] | None = None, cwd_prefix: str | None = None,
min_message_count: int = 0, include_archived: bool = False, archived_only: bool = False,
exclude_children: bool = False, exclude_sources: list[str] | None = None, include_subagents: bool = False,
- local_created_sources: Sequence[str] | None = None,
) -> int:
"""Count sessions with list_sessions_rich's filters so a paired "load more" total matches."""
where_clauses, params = _session_filter_where(
exclude_children=exclude_children, source=source, sources=sources,
exclude_sources=exclude_sources, cwd_prefix=cwd_prefix, min_message_count=min_message_count,
archived_only=archived_only, include_archived=include_archived, include_subagents=include_subagents,
- local_created_sources=local_created_sources,
)
if exclude_children:
from hermes_state_local import exclude_superseded_segments
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 27141c599f2..6210932f493 100644
--- a/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py
+++ b/tests/hermes_cli/test_cli_sessions_behind_newer_surfaces.py
@@ -3,18 +3,19 @@
Gateway rows for Desktop, TUI and the classic CLI are stored as ``source='local'``
with the real surface in ``created_source``. ``hermes -c``, in-chat ``/sessions``
and ``/resume`` read those rows through ``SurfaceLabelReader``. A fixed over-fetch
-of every local row, filtered afterwards, drops the CLI session once enough newer
-Desktop/TUI rows fill the window. ``hermes sessions list`` does not use that reader.
-
-``created_source='unknown'`` stays unmatched. #138838 and #139058 repair that
-placeholder on upsert; this reader does not admit it.
+of every local row, filtered afterwards, dropped the CLI session once enough newer
+Desktop/TUI rows filled the window.
"""
from __future__ import annotations
import sqlite3
+from contextlib import contextmanager
from types import SimpleNamespace
+import pytest
+
+from hermes_cli.gateway_session_store import SurfaceLabelReader
from hermes_state import SessionDB
@@ -31,17 +32,12 @@ def _populate(db: SessionDB, rows: list[tuple[str, str, str, float]]) -> None:
def _newer_locals(count: int, *, start: float, prefix: str) -> list[tuple[str, str, str, float]]:
"""``count`` gateway rows newer than a CLI session, alternating Desktop and TUI."""
- surfaces = ("desktop", "tui")
return [
- (f"{prefix}-{index:03d}", "local", surfaces[index % 2], start + index)
+ (f"{prefix}-{index:03d}", "local", ("desktop", "tui")[index % 2], start + index)
for index in range(count)
]
-def _cli_row(sid: str, started: float, *, source: str = "local", created: str = "cli"):
- return (sid, source, created, started)
-
-
def _resolve_cli(monkeypatch, home) -> str | None:
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setattr("hermes_cli.main._resolve_workspace_key", lambda: None)
@@ -53,394 +49,148 @@ def _resolve_cli(monkeypatch, home) -> str | None:
def _recent_cli_ids(db: SessionDB) -> tuple[list[str], list[str]]:
"""Ids from the shared listing helper and from ``/sessions`` / ``/resume`` (limit 10)."""
from hermes_cli.cli_session_mixin import CLISessionMixin
- from hermes_cli.gateway_session_store import SurfaceLabelReader
from hermes_cli.session_listing import query_session_listing
from hermes_state_sessions import INTERNAL_LISTING_SOURCES
reader = SurfaceLabelReader(db)
listed = query_session_listing(
- reader,
- source="cli",
- current_session_id="other-session",
- include_all_sources=False,
- include_unnamed=True,
- limit=10,
- exclude_sources=list(INTERNAL_LISTING_SOURCES),
+ reader, source="cli", current_session_id="other-session", include_all_sources=False,
+ include_unnamed=True, limit=10, exclude_sources=list(INTERNAL_LISTING_SOURCES),
)
via_command = CLISessionMixin._list_recent_sessions(
- SimpleNamespace(_session_db=reader, session_id="other-session"),
- limit=10,
+ SimpleNamespace(_session_db=reader, session_id="other-session"), limit=10,
)
return [row["id"] for row in listed], [row["id"] for row in via_command]
-def _reader_ids(db: SessionDB, method: str, **kwargs) -> list[str]:
- from hermes_cli.gateway_session_store import SurfaceLabelReader
-
- rows = getattr(SurfaceLabelReader(db), method)(**kwargs)
- return [row["id"] for row in rows]
+def _ids(reader, method: str, **kwargs) -> list[str]:
+ return [row["id"] for row in getattr(reader, method)(**kwargs)]
-def test_continue_finds_cli_session_behind_sixty_newer_desktop_sessions(tmp_path, monkeypatch):
- """Bare ``hermes -c`` resolves the CLI session even when 60 newer Desktop/TUI rows exist."""
+def test_continue_and_sessions_find_cli_session_behind_newer_desktop_rows(tmp_path, monkeypatch):
+ """Bare ``hermes -c``, ``/sessions`` and ``/resume`` reach the CLI session behind 200 newer rows."""
db = SessionDB(db_path=tmp_path / "state.db")
- _populate(db, [
- _cli_row("cli-session", 1_000_000),
- *_newer_locals(60, start=2_000_000, prefix="desk"),
- ])
+ _populate(db, [("cli-session", "local", "cli", 1_000_000), *_newer_locals(200, start=2_000_000, prefix="d")])
+ assert _recent_cli_ids(db) == (["cli-session"], ["cli-session"])
db.close()
assert _resolve_cli(monkeypatch, tmp_path) == "cli-session"
-def test_sessions_and_resume_list_cli_session_behind_two_hundred_newer_rows(tmp_path):
- """In-chat ``/sessions`` and ``/resume`` still list the CLI session behind 200 newer rows."""
- db = SessionDB(db_path=tmp_path / "state.db")
- _populate(db, [
- _cli_row("cli-session", 1_000_000),
- *_newer_locals(200, start=3_000_000, prefix="newer"),
- ])
-
- listed, via_command = _recent_cli_ids(db)
- assert listed == ["cli-session"]
- assert via_command == ["cli-session"]
- db.close()
-
-
def test_surface_pages_are_exact_across_newer_local_rows(tmp_path):
- """LIMIT/OFFSET walk matching rows, including across Desktop/TUI rows that sit between them.
+ """LIMIT/OFFSET walk matching rows only.
- A non-local ``source='cli'`` row still matches when ``created_source`` names another
- surface. ``unknown``, Desktop, TUI and an unrelated platform do not.
+ A non-local ``source='cli'`` row matches whatever its ``created_source``. ``unknown``,
+ Desktop, TUI and an unrelated platform do not.
"""
- rows = [
+ db = SessionDB(db_path=tmp_path / "state.db")
+ _populate(db, [
("telegram-new", "telegram", "telegram", 2_100_000),
("unknown-new", "local", "unknown", 2_000_000),
*_newer_locals(60, start=1_900_000, prefix="top"),
- _cli_row("legacy-cli", 1_800_000, source="cli", created="desktop"),
+ ("legacy-cli", "cli", "desktop", 1_800_000),
*_newer_locals(10, start=1_700_000, prefix="gap-a"),
- _cli_row("cli-a", 1_600_000),
+ ("cli-a", "local", "cli", 1_600_000),
*_newer_locals(10, start=1_500_000, prefix="gap-b"),
- _cli_row("cli-b", 1_400_000),
+ ("cli-b", "local", "cli", 1_400_000),
*_newer_locals(10, start=1_300_000, prefix="gap-c"),
- _cli_row("cli-c", 1_200_000),
- ]
- db = SessionDB(db_path=tmp_path / "state.db")
- _populate(db, rows)
- expected = ["legacy-cli", "cli-a", "cli-b", "cli-c"]
-
+ ("cli-c", "local", "cli", 1_200_000),
+ ])
+ reader = SurfaceLabelReader(db)
for method in ("search_sessions", "list_sessions_rich"):
- kwargs = {"source": "cli"}
- assert _reader_ids(db, method, limit=10, **kwargs) == expected
- assert _reader_ids(db, method, limit=2, offset=0, **kwargs) == ["legacy-cli", "cli-a"]
- assert _reader_ids(db, method, limit=1, offset=1, **kwargs) == ["cli-a"]
- assert _reader_ids(db, method, limit=2, offset=2, **kwargs) == ["cli-b", "cli-c"]
+ assert _ids(reader, method, source="cli", limit=10) == ["legacy-cli", "cli-a", "cli-b", "cli-c"]
+ assert _ids(reader, method, source="cli", limit=1, offset=1) == ["cli-a"]
+ assert _ids(reader, method, source="cli", limit=2, offset=2) == ["cli-b", "cli-c"]
# ``hermes -c`` asks for the CLI family (cli + oneshot), still paged in SQL.
- assert _reader_ids(db, "search_sessions", source=["cli", "oneshot"], limit=1) == ["legacy-cli"]
- db.close()
-
-
-def test_continue_and_sessions_find_a_lone_cli_session(tmp_path, monkeypatch):
- """Control: with no newer Desktop/TUI rows, both surfaces already find the CLI session."""
- db = SessionDB(db_path=tmp_path / "state.db")
- _populate(db, [_cli_row("cli-session", 1_000_000)])
- listed, via_command = _recent_cli_ids(db)
- db.close()
-
- assert listed == ["cli-session"]
- assert via_command == ["cli-session"]
- assert _resolve_cli(monkeypatch, tmp_path) == "cli-session"
-
-
-def test_unknown_placeholder_does_not_become_the_cli_mru(tmp_path, monkeypatch):
- """Control: a newer ``created_source='unknown'`` local row is not CLI history.
-
- #138838 and #139058 repair that placeholder on the creator upsert. Widening this
- reader to admit it would duplicate those fixes and hide the real CLI session.
- """
- db = SessionDB(db_path=tmp_path / "state.db")
- _populate(db, [
- _cli_row("cli-session", 1_000_000),
- ("unknown-new", "local", "unknown", 2_000_000),
- ])
+ assert _ids(reader, "search_sessions", source=["cli", "oneshot"], limit=1) == ["legacy-cli"]
db.close()
- assert _resolve_cli(monkeypatch, tmp_path) == "cli-session"
-
-def test_oneshot_remains_cli_family_mru(tmp_path, monkeypatch):
- """Control: a finite CLI run (``source='oneshot'``) is still the ``-c`` MRU when it is newest."""
- db = SessionDB(db_path=tmp_path / "state.db")
- _populate(db, [
- _cli_row("cli-session", 1_000_000),
- ("shot", "oneshot", "oneshot", 2_000_000),
- ])
+def test_surface_filter_seeks_the_local_created_source_index(tmp_path):
+ """Startup creates the partial index on a store that predates it, and the local branch
+ seeks it instead of walking every Desktop/TUI row."""
+ path = tmp_path / "state.db"
+ db = SessionDB(db_path=path)
+ db._conn.execute("DROP INDEX IF EXISTS idx_sessions_local_created_started")
+ db._conn.commit()
db.close()
- assert _resolve_cli(monkeypatch, tmp_path) == "shot"
-
-
-def _plans_for_surface_reads(db: SessionDB) -> list[str]:
- """EXPLAIN QUERY PLAN text for the surface-filtered SELECTs the reader actually runs."""
- from hermes_cli.gateway_session_store import SurfaceLabelReader
-
+ db = SessionDB(db_path=path)
+ _populate(db, [("cli-session", "local", "cli", 1_000_000), *_newer_locals(30, start=2_000_000, prefix="d")])
captured: list[tuple[str, tuple]] = []
original = db._read_all
- def _capture(sql, params=()):
+ def capture(sql, params=()):
captured.append((sql, tuple(params)))
return original(sql, params)
- db._read_all = _capture
+ db._read_all = capture
reader = SurfaceLabelReader(db)
reader.search_sessions(source=["cli", "oneshot"], limit=1)
reader.list_sessions_rich(source="cli", limit=10, offset=2)
- plans = []
- for sql, params in captured:
- if "created_source" not in sql:
- continue
- rows = db._conn.execute("EXPLAIN QUERY PLAN " + sql, params).fetchall()
- plans.append("\n".join(row["detail"] if hasattr(row, "keys") else row[3] for row in rows))
- return plans
-
-
-def test_surface_filter_seeks_the_local_created_source_index(tmp_path):
- """The local branch seeks ``created_source`` instead of scanning every local row.
-
- Startup recreates the partial index on a store that predates it (the index is
- declared with the other post-reconcile indexes).
- """
- path = tmp_path / "state.db"
- db = SessionDB(db_path=path)
- db._conn.execute("DROP INDEX IF EXISTS idx_sessions_local_created_started")
- db._conn.commit()
- db.close()
-
- db = SessionDB(db_path=path)
- present = db._conn.execute(
- "SELECT 1 FROM sqlite_master WHERE type = 'index' AND name = ?",
- ("idx_sessions_local_created_started",),
- ).fetchone()
- assert present is not None
- _populate(db, [
- _cli_row("cli-session", 1_000_000),
- *_newer_locals(30, start=2_000_000, prefix="desk"),
- ])
- plans = _plans_for_surface_reads(db)
- assert plans, "surface-filtered reads did not reach SQL"
+ plans = [
+ " ".join(row[3] for row in db._conn.execute("EXPLAIN QUERY PLAN " + sql, params))
+ for sql, params in captured if "created_source" in sql
+ ]
+ assert len(plans) == 2
for plan in plans:
assert "idx_sessions_local_created_started" in plan
- assert "SCAN s" not in plan
- assert "SCAN sessions" not in plan
- db.close()
-
-
-def test_sessions_list_stays_a_recency_page_across_surfaces(tmp_path, capsys):
- """Control: ``hermes sessions list`` does not apply the CLI surface window.
-
- The default page is the newest rows of every source. A limit that reaches the older
- CLI row includes it together with the Desktop rows, the same as an unfiltered read.
- """
- from hermes_cli.sessions_cmd import _cmd_list
-
- db = SessionDB(db_path=tmp_path / "state.db")
- _populate(db, [
- _cli_row("cli-session", 1_000_000),
- *_newer_locals(60, start=2_000_000, prefix="desk"),
- ])
-
- _cmd_list(db, SimpleNamespace(limit=20, source=None, workspace=None))
- short = capsys.readouterr().out
- assert "cli-session" not in short
- assert "desk-059" in short
-
- _cmd_list(db, SimpleNamespace(limit=80, source=None, workspace=None))
- full = capsys.readouterr().out
- 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.
- """
+ """A store last written before ``created_source``, reopened read-only (which never migrates)."""
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
-
+ return SessionDB(db_path=path, read_only=True)
-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 _columns(db: SessionDB) -> set[str]:
+ return {row[1] for row in db._conn.execute("PRAGMA table_info(sessions)")}
-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
+def test_readonly_pre_column_store_finds_legacy_cli_session(tmp_path, monkeypatch):
+ """A missing ``created_source`` column must not read as "no previous session", and the
+ lookups must not migrate the file."""
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)
+ assert _recent_cli_ids(db) == (["legacy-cli"], ["legacy-cli"])
finally:
db.close()
-
-
-def test_schema_probe_retries_one_disk_io_error(tmp_path, monkeypatch):
- """One ``disk I/O error`` on ``PRAGMA table_info`` still resolves the CLI session.
-
- The session SELECT already replays that blip (``_read_retrying_ioerr``). The
- schema probe is the same read. An unretried error is swallowed by bare ``-c``
- and in-chat ``/sessions``, which then look empty.
- """
- path = tmp_path / "state.db"
- writer = SessionDB(db_path=path)
- _populate(writer, [_cli_row("local-cli", 4_000_000_000)])
- writer.close()
-
- probes: list[dict[str, int]] = []
-
- def fail_first_table_info(conn):
- real = conn.execute
- seen = {"n": 0}
-
- def execute(sql, *args, **kwargs):
- if isinstance(sql, str) and "table_info(sessions)" in sql:
- seen["n"] += 1
- if seen["n"] == 1:
- raise sqlite3.OperationalError("disk I/O error")
- return real(sql, *args, **kwargs)
-
- conn.execute = execute
- probes.append(seen)
-
- import hermes_state
-
- real_open = hermes_state.SessionDB._open_read_only
-
- def open_read_only(self):
- real_open(self)
- fail_first_table_info(self._conn)
-
- monkeypatch.setattr(hermes_state.SessionDB, "_open_read_only", open_read_only)
- assert _resolve_cli(monkeypatch, tmp_path) == "local-cli"
- assert probes[0]["n"] >= 2
-
- ro = SessionDB(db_path=path, read_only=True)
- try:
- listed, via_command = _recent_cli_ids(ro)
- finally:
- ro.close()
- assert listed == ["local-cli"]
- assert via_command == ["local-cli"]
- assert probes[-1]["n"] >= 2
-
-
-def test_persistent_disk_io_on_schema_probe_is_not_cached(tmp_path):
- """A disk I/O error that survives the retry budget still propagates and is not stored."""
- path = tmp_path / "state.db"
- writer = SessionDB(db_path=path)
- _populate(writer, [_cli_row("local-cli", 4_000_000_000)])
- writer.close()
- ro = SessionDB(db_path=path, read_only=True)
- real = ro._conn.execute
-
- def execute(sql, *args, **kwargs):
- if isinstance(sql, str) and "table_info(sessions)" in sql:
- raise sqlite3.OperationalError("disk I/O error")
- return real(sql, *args, **kwargs)
-
- ro._conn.execute = execute
- from hermes_cli.gateway_session_store import SurfaceLabelReader
-
- reader = SurfaceLabelReader(ro)
+ assert _resolve_cli(monkeypatch, tmp_path) == "legacy-cli"
+ ro = SessionDB(db_path=tmp_path / "state.db", read_only=True)
try:
- caught = None
- try:
- reader.search_sessions(source=["cli", "oneshot"], limit=1)
- except sqlite3.OperationalError as exc:
- caught = str(exc)
- assert caught and "disk I/O error" in caught
- assert reader._created_source_column is None
- ro._conn.execute = real
- found = [row["id"] for row in reader.search_sessions(source=["cli", "oneshot"], limit=1)]
- assert found == ["local-cli"]
+ assert "created_source" not in _columns(ro)
finally:
ro.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.
- """
[email protected](("error", "failures", "first_call_raises"), [
+ ("disk I/O error", 1, False), # transient: replayed like every other read
+ ("disk I/O error", 99, True), # persistent: propagates, not remembered
+ ("database is locked", 1, True), # a lock is not a missing column
+])
+def test_schema_probe_error_is_never_cached_as_absent(tmp_path, error, failures, first_call_raises):
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)
- raised = {"done": False}
+ _populate(db, [("legacy-cli", "cli", "cli", 1_000_000), ("local-cli", "local", "cli", 4_000_000_000)])
+ left = {"n": failures}
real_ctx = db._read_ctx
- from contextlib import contextmanager
-
@contextmanager
def read_ctx():
with real_ctx() as conn:
real_execute = conn.execute
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")
+ if "table_info" in sql and left["n"] > 0:
+ left["n"] -= 1
+ raise sqlite3.OperationalError(error)
return real_execute(sql, *args, **kwargs)
conn.execute = execute
@@ -450,30 +200,23 @@ def test_locked_schema_probe_is_not_remembered_as_a_missing_column(tmp_path):
conn.execute = real_execute
db._read_ctx = read_ctx
- 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"]
+ reader = SurfaceLabelReader(db)
+ if first_call_raises:
+ with pytest.raises(sqlite3.OperationalError, match=error):
+ reader.search_sessions(source=["cli", "oneshot"], limit=1)
+ left["n"] = 0
+ assert _ids(reader, "search_sessions", source=["cli", "oneshot"], limit=1) == ["local-cli"]
+ assert left["n"] == 0 # the probe went through the retrying reader
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.
- """
+ """In-chat ``/sessions`` keeps one reader; after another connection adds ``created_source``
+ and a newer local CLI row, that same reader returns 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"]
+ assert _ids(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")
@@ -484,9 +227,7 @@ def test_absent_schema_probe_sees_a_column_added_later(tmp_path):
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
+ assert _ids(reader, "search_sessions", source="cli", limit=5)[0] == "local-new"
+ assert "local-new" in _ids(reader, "list_sessions_rich", source="cli", limit=5)
finally:
db.close()