fix(pheby): migrate bridge to the new persistence repository API

The Hermes update replaced SessionDB with the persistence layer; raw SQL
(_read_all), create_session/append_message/delete_sessions,
get_messages_as_conversation and set_session_yolo are gone. Use
sessions.search_sessions (client-side session_key filter), messages
.read_conversation(ConversationRequest) with as_openai_message rows,
sessions.create/append/delete/get/set_yolo instead. Update test fakes
and open the storage interface directly in the cron delivery tests.
This commit is contained in:
2026-11-20 16:01:01 +00:00
parent 7e0e510bac
commit 3bfb300a2f
3 changed files with 179 additions and 68 deletions
+12 -6
View File
@@ -2,7 +2,8 @@
import pytest
from gateway.config import PlatformConfig
from hermes_state import SessionDB
from persistence.conversation.messages import NewMessage
from persistence.conversation.sessions import NewSession, SessionId
from pheby import hermes_bridge as hb, protocol as proto
from pheby.adapter import PhebyAdapter
from pheby.server import PhebyServer
@@ -10,7 +11,11 @@ from pheby.server import PhebyServer
@pytest.fixture
def delivery_stack(tmp_path, monkeypatch):
db = SessionDB(tmp_path / "state.db")
# SessionDB is deprecated; open the storage interface directly for the
# test profile (context-local HERMES_HOME drives profile_scope()).
from persistence_assembly import open_persistence, profile_scope
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
db = open_persistence(profile_scope(), eager=True)
monkeypatch.setattr(hb, "_session_db", lambda: db)
monkeypatch.setattr(hb, "_session_store", lambda: None)
adapter = PhebyAdapter(PlatformConfig(enabled=True, extra={"secret": "test-secret"}))
@@ -30,8 +35,9 @@ async def test_cron_delivery_survives_reopen_without_touching_active_turn(delive
adapter, server, db = delivery_stack
cid = "home"
key = hb._session_key_for(cid)
db.create_session("active-session", "pheby", session_key=key)
db.append_message("active-session", "user", "My ongoing question")
db.sessions.create(NewSession(
session_id=SessionId("active-session"), source="pheby", session_key=key))
db.messages.append("active-session", NewMessage(role="user", content="My ongoing question"))
adapter._drafts[cid] = {"message_id": "draft-active", "text": "Still thinking"}
hb._ACTIVE_RUNS[cid] = {"run_id": "active-run", "started": 0}
@@ -44,7 +50,7 @@ async def test_cron_delivery_survives_reopen_without_touching_active_turn(delive
assert found and not has_more
assert hb.active_run_id(cid) == "active-run"
assert adapter._drafts[cid]["message_id"] == "draft-active"
assert [m["content"] for m in db.get_messages_as_conversation("active-session")] == ["My ongoing question"]
assert [m.content for m in db.messages.read_conversation("active-session")] == ["My ongoing question"]
assert await server.router.get_name(cid) is not None
conversations = await hb.list_conversations(server)
assert conversations[0]["conversation_id"] == cid
@@ -67,7 +73,7 @@ async def test_cron_delivery_does_not_claim_success_when_save_fails(delivery_sta
raise OSError("disk unavailable")
monkeypatch.setattr(server, "broadcast", broadcast)
monkeypatch.setattr(db, "append_message", fail)
monkeypatch.setattr(db.messages, "append", fail)
result = await adapter.send("home", "A report", metadata={"job_id": "daily"})
assert not result.success
assert broadcasts == []
+82 -29
View File
@@ -452,18 +452,31 @@ class TestBridge:
def peek_session_id(self, _key):
return "session-1"
class DB:
def _read_all(self, sql, params):
return [{"id": "session-1"}]
class _Msg:
def __init__(self, row):
self._row = row
def get_messages_as_conversation(self, _session_id, include_row_ids=False, include_compacted=False):
assert include_row_ids is True
assert include_compacted is True
def as_openai_message(self):
return self._row
class Sessions:
def search_sessions(self, *, sources, window):
return []
class Messages:
def read_conversation(self, _session_id, request):
from persistence.conversation.messages import TranscriptView
assert request.include_row_ids is True
assert request.view is TranscriptView.DISPLAY
return [
{"_row_id": 10, "role": "user", "content": "hello", "timestamp": 1_789_000_000.25},
{"_row_id": 11, "role": "assistant", "content": "reply", "timestamp": 1_789_000_001.5},
_Msg({"_row_id": 10, "role": "user", "content": "hello", "timestamp": 1_789_000_000.25}),
_Msg({"_row_id": 11, "role": "assistant", "content": "reply", "timestamp": 1_789_000_001.5}),
]
class DB:
sessions = Sessions()
messages = Messages()
monkeypatch.setattr(hb, "_session_store", lambda: Store())
monkeypatch.setattr(hb, "_session_db", lambda: DB())
@@ -483,17 +496,35 @@ class TestBridge:
def peek_session_id(self, _key):
return "new"
class DB:
def _read_all(self, sql, params):
assert "session_key" in sql and params[1] == "pheby"
return [{"id": "old"}, {"id": "new"}]
class _Msg:
def __init__(self, row):
self._row = row
def get_messages_as_conversation(self, sid, **kwargs):
assert kwargs == {"include_row_ids": True, "include_compacted": True}
def as_openai_message(self):
return self._row
key = hb._session_key_for("conv")
def _entry(session_id):
from types import SimpleNamespace
return SimpleNamespace(session=SimpleNamespace(
session_id=session_id, session_key=key))
class Sessions:
def search_sessions(self, *, sources, window):
assert "pheby" in sources
return [_entry("old"), _entry("new")]
class Messages:
def read_conversation(self, sid, _request):
start = 1 if sid == "old" else 4
return [{"_row_id": n, "role": "user", "content": f"turn {n}", "timestamp": float(n)}
return [_Msg({"_row_id": n, "role": "user", "content": f"turn {n}", "timestamp": float(n)})
for n in range(start, start + 3)]
class DB:
sessions = Sessions()
messages = Messages()
monkeypatch.setattr(hb, "_session_store", lambda: Store())
monkeypatch.setattr(hb, "_session_db", lambda: DB())
latest, found, older = await hb.conversation_history("conv", 2)
@@ -510,10 +541,13 @@ class TestBridge:
async def test_history_read_failure_is_not_reported_as_empty_chat(self, monkeypatch):
from pheby import hermes_bridge as hb
class DB:
def _read_all(self, _sql, _params):
class Sessions:
def search_sessions(self, *, sources, window):
raise OSError("database offline")
class DB:
sessions = Sessions()
monkeypatch.setattr(hb, "_session_store", lambda: None)
monkeypatch.setattr(hb, "_session_db", lambda: DB())
with pytest.raises(RuntimeError, match="transcript"):
@@ -735,9 +769,15 @@ class TestBridge:
assert key == session_key
return "session-1"
class Sessions:
def get(self, _session_id):
return None
def set_yolo(self, session_id, enabled):
persisted.append((str(session_id), enabled))
class DB:
def set_session_yolo(self, session_id, enabled):
persisted.append((session_id, enabled))
sessions = Sessions()
monkeypatch.setattr(hb, "_session_store", lambda: Store())
monkeypatch.setattr(hb, "_session_db", lambda: DB())
@@ -909,16 +949,29 @@ class TestBridge:
def peek_session_id(self, key):
return "session-delete" if key == session_key else None
class Sessions:
def __init__(self):
self.deleted = []
def search_sessions(self, *, sources, window):
assert "pheby" in sources and "pheby_delivery" in sources
from types import SimpleNamespace
return [
SimpleNamespace(session=SimpleNamespace(
session_id=sid, session_key=session_key, started_at=float(i)))
for i, sid in enumerate(("session-old", "session-delete"))
]
def delete(self, session_id, *, media_root):
self.deleted.append(str(session_id))
return (session_id,)
def get(self, _session_id):
return None
class DB:
deleted = None
def _read_all(self, sql, params):
assert "session_key" in sql and params == (session_key, "pheby", "pheby_delivery")
return [{"id": "session-old"}, {"id": "session-delete"}]
def delete_sessions(self, session_ids):
self.deleted = list(session_ids)
return len(session_ids)
def __init__(self):
self.sessions = Sessions()
store, db = Store(), DB()
from pheby import hermes_bridge as hb
@@ -926,7 +979,7 @@ class TestBridge:
monkeypatch.setattr(hb, "_session_db", lambda: db)
assert await hb.delete_conversation(cid) is True
assert db.deleted == ["session-old", "session-delete"]
assert db.sessions.deleted == ["session-old", "session-delete"]
assert session_key not in store._entries
assert store.saved is True
assert await server.router.get_name(cid) is None