Files
hermes-agent/tests/hermes_cli/test_sessions_pin.py
T
KoNit-K b4e22481a8 fix(cli): open observational session stores read-only
## What does this PR do?

Makes observational CLI commands open `state.db` in read-only mode, so they can inspect a live Hermes installation without participating in writable WAL lifecycle handling.

### Symptom

Running `hermes status`, `hermes doctor` without `--fix`, `hermes sessions list`, `hermes sessions stats`, or `hermes insights` while a gateway owns the store could open another writable session handle. The live turn could then lose its WAL generation and stop.

### Impact

Users inspecting status or session history during an active turn could lose that in-flight turn and leave the gateway halted until recovery.

### Bug Cause

**Trigger:** observational CLI helpers constructed `SessionDB()` with its writable default.

**Causal chain:**

1. A live gateway holds the `state.db` WAL generation.
2. A nested observational CLI command opens a second writable handle.
3. Writable-handle close behavior can participate in WAL lifecycle work and retire the generation used by the live writer.

**Why it is wrong:** these commands only query state and should not have writer privileges.

**Working sibling / contrast:** repair and mutating session commands still use writable access intentionally.

**Ruled out:** no state schema, migration, or WAL checkpoint implementation changes are included.

### Fix

Routes status, non-fixing doctor state inspection, sessions list/stats, and both insights entrypoints through `SessionDB(read_only=True)`. Repair and mutating paths remain writable, and regression tests cover WAL preservation with a live writer.

## Related Issue

Fixes #110173

## Type of Change

- ✅ Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `hermes_cli/status.py`, `hermes_cli/doctor_state.py`, and insights helpers — open observational state readers read-only.
- `hermes_cli/sessions_cmd.py` — make only `list` and `stats` read-only; retain writable access for mutations.
- `tests/hermes_cli/test_observational_sessiondb_modes.py` — verify access modes and a live writer's WAL remains usable.

## How to Test

- ✅ `scripts/run_tests.sh tests/hermes_cli/test_observational_sessiondb_modes.py tests/hermes_cli/test_cli_insights_command.py` — 9 passed.
- ✅ `scripts/run_tests.sh tests/hermes_cli/test_doctor.py tests/hermes_cli/test_doctor_structural_corruption.py tests/hermes_cli/test_sessions_error_exit_codes.py` — 75 passed; two sandbox-only failures came from blocked host process/symlink operations.
- ✅ A live `SessionDB` writer remains able to create and retrieve a session after `sessions stats` reads the store.

## Checklist

### Code

- ✅ I've read the Contributing Guide
- ✅ My commit messages follow Conventional Commits
- ✅ I searched for existing PRs to make sure this isn't a duplicate
- ✅ My PR contains only changes related to this fix
- ✅ I've run relevant tests locally (see How to Test)
- ✅ I've added tests for my changes
- ✅ I've tested on my platform: macOS

### Documentation & Housekeeping

- ✅ Documentation update: N/A
- ✅ `cli-config.yaml.example`: N/A
- ✅ `CONTRIBUTING.md` or `AGENTS.md`: N/A
- ✅ Cross-platform impact considered
- ✅ Tool descriptions/schemas: N/A
2026-09-15 12:51:27 +05:30

139 lines
4.1 KiB
Python

"""CLI pin / unpin / pinned subcommands (issue #52955).
Pin state is the durable "keep" flag in state.db that the Desktop sidebar
writes; these tests pin the CLI's read/write access to the SAME store
(SessionDB.set_session_pinned / list_sessions_rich(include_pinned=True)),
not a client-local list.
"""
import json
import sys
class _FakeDB:
def __init__(self, rows=None, known=("20260315_092437_c9a6ff",)):
self.rows = rows or []
self.known = set(known)
self.pin_calls = []
self.list_kwargs = None
self.closed = False
def resolve_session_id(self, session_id):
for k in self.known:
if k.startswith(session_id):
return k
return None
def set_session_pinned(self, session_id, pinned):
self.pin_calls.append((session_id, pinned))
return session_id in self.known
def get_session_title(self, session_id):
return "Alpha Work" if session_id in self.known else None
def list_sessions_rich(self, **kwargs):
self.list_kwargs = kwargs
return self.rows
def close(self):
self.closed = True
def _run(monkeypatch, capsys, argv_tail, db):
import hermes_cli.main as main_mod
import hermes_state
monkeypatch.setattr(hermes_state, "SessionDB", lambda *args, **kwargs: db)
monkeypatch.setattr(sys, "argv", ["hermes", "sessions", *argv_tail])
try:
main_mod.main()
code = 0
except SystemExit as e: # non-zero exits propagate through main()
code = e.code or 0
return code, capsys.readouterr().out
def test_pin_accepts_unique_prefix(monkeypatch, capsys):
db = _FakeDB()
code, out = _run(monkeypatch, capsys, ["pin", "20260315_092437"], db)
assert db.pin_calls == [("20260315_092437_c9a6ff", True)]
assert "Pinned session '20260315_092437_c9a6ff'." in out
assert "(Alpha Work)" in out
assert code == 0
def test_unpin_writes_false(monkeypatch, capsys):
db = _FakeDB()
_code, out = _run(monkeypatch, capsys, ["unpin", "20260315_092437_c9a6ff"], db)
assert db.pin_calls == [("20260315_092437_c9a6ff", False)]
assert "Unpinned session" in out
def test_pin_multiple_ids_one_missing(monkeypatch, capsys):
db = _FakeDB(known=("aaa111", "bbb222"))
code, out = _run(monkeypatch, capsys, ["pin", "aaa", "nope", "bbb"], db)
assert ("aaa111", True) in db.pin_calls
assert ("bbb222", True) in db.pin_calls
assert "Session 'nope' not found." in out
assert code == 1
def test_pinned_lists_only_pinned_rows(monkeypatch, capsys):
rows = [
{
"id": "pinned_one",
"title": "Keep Me",
"source": "cli",
"last_active": 1_700_000_000.0,
"message_count": 3,
"pinned": 1,
},
{
"id": "recent_unpinned",
"title": "Recent",
"source": "cli",
"last_active": 1_700_000_100.0,
"message_count": 5,
"pinned": 0,
},
]
db = _FakeDB(rows=rows)
_code, out = _run(monkeypatch, capsys, ["pinned"], db)
# include_pinned back-fill is what guarantees old pins surface
assert db.list_kwargs["include_pinned"] is True
assert "pinned_one" in out
assert "Keep Me" in out
assert "recent_unpinned" not in out
def test_pinned_json_output(monkeypatch, capsys):
rows = [
{
"id": "pinned_one",
"title": "Keep Me",
"source": "cli",
"last_active": 1_700_000_000.0,
"message_count": 3,
"pinned": 1,
},
]
db = _FakeDB(rows=rows)
_code, out = _run(monkeypatch, capsys, ["pinned", "--json"], db)
payload = json.loads(out)
assert payload == [
{
"id": "pinned_one",
"title": "Keep Me",
"source": "cli",
"last_active": 1_700_000_000.0,
"message_count": 3,
}
]
def test_pinned_empty_hint(monkeypatch, capsys):
db = _FakeDB(rows=[])
_code, out = _run(monkeypatch, capsys, ["pinned"], db)
assert "No pinned sessions" in out
assert "hermes sessions pin" in out