The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(state): batch message hydration in export_all
Xipong78f85112d9b4
diff --git a/hermes_state_portability.py b/hermes_state_portability.pyindex 3858ccd9f5e..a9f39874a31 100644--- a/hermes_state_portability.py+++ b/hermes_state_portability.py@@ -258,7 +258,22 @@ class SessionPortabilityMixin:def export_all(self, source: str = None) -> List[Dict[str, Any]]:"""Export all sessions (with messages) as dicts, e.g. for JSONL backup."""- return [self._with_messages(s) for s in self.search_sessions(source=source, limit=100000)]+ sessions = self.search_sessions(source=source, limit=100000)+ messages_by_session = {session["id"]: [] for session in sessions}+ session_ids = list(messages_by_session)+ # Stay below SQLite's legacy 999-variable limit while replacing the per-session N+1 reads.+ for start in range(0, len(session_ids), 900):+ chunk = session_ids[start:start + 900]+ rows = self._read_all(+ f"SELECT * FROM messages WHERE session_id IN ({','.join('?' for _ in chunk)}) "+ "AND active = 1 ORDER BY session_id, id",+ chunk,+ )+ for row in rows:+ messages_by_session[row["session_id"]].append(+ self._row_to_message_dict(row, warn_context="get_messages", summary_flag=True)+ )+ return [{**session, "messages": messages_by_session[session["id"]]} for session in sessions]def adopt_session_lineage_from(self, donor_db: Any, session_id: str, *, retire_donor: bool = True) -> Dict[str, Any]:"""Adopt *session_id*'s full compression lineage from *donor_db* (stranded-bot-sessiondiff --git a/tests/test_session_export_batch.py b/tests/test_session_export_batch.pynew file mode 100644index 00000000000..9bb76061999--- /dev/null+++ b/tests/test_session_export_batch.py@@ -0,0 +1,41 @@+from hermes_state import SessionDB+++def test_export_all_batches_message_reads_without_changing_export_rows(tmp_path, monkeypatch):+ db = SessionDB(tmp_path / "state.db")+ try:+ for index, source in enumerate(("cli", "telegram", "cli", "cli")):+ session_id = f"session-{index}"+ db.create_session(session_id=session_id, source=source)+ db.append_messages_batch(+ session_id,+ [+ {"role": "user", "content": f"question {index}"},+ {+ "role": "assistant",+ "content": f"answer {index}",+ "tool_calls": [{"id": f"call-{index}", "type": "function"}],+ },+ ],+ )++ sessions = db.search_sessions(source="cli", limit=100000)+ expected = [+ {**session, "messages": db.get_messages(session["id"])}+ for session in sessions+ ]++ original_read_all = db._read_all+ read_calls = 0++ def counted_read_all(*args, **kwargs):+ nonlocal read_calls+ read_calls += 1+ return original_read_all(*args, **kwargs)++ monkeypatch.setattr(db, "_read_all", counted_read_all)++ assert db.export_all(source="cli") == expected+ assert read_calls <= 2+ finally:+ db.close()