Skip to content

The agent that grows with you

fix(sessions): cap /save json by sessions.max_export_messages

kshitijk4poorc5ff25a50ac7
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index a4cd3111943..10f5c8308f3 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -738,6 +738,12 @@ class GatewaySessionCommandsMixin:
# Never trust path separators from chat input; the filename is only echoed to the platform.
filename = parts[1] if len(parts) > 1 else default_save_filename(session_id, fmt)
filename = os.path.basename(filename) or default_save_filename(session_id, fmt)
+ if fmt not in SAVE_TRANSCRIPT_FORMATS:
+ from hermes_state import SessionExportTooLargeError
+ try:
+ await self._session_db.assert_export_safe(session_id) # the JSON backup loads every stored row
+ except SessionExportTooLargeError as e:
+ return str(e)
export_data = await self._session_db.export_session(session_id, **export_projection(fmt in SAVE_TRANSCRIPT_FORMATS))
if not export_data:
return t("gateway.save.no_messages", session_id=session_id)
diff --git a/hermes_cli/cli_session_mixin.py b/hermes_cli/cli_session_mixin.py
index e20ffe743e0..3624085bd21 100644
--- a/hermes_cli/cli_session_mixin.py
+++ b/hermes_cli/cli_session_mixin.py
@@ -654,6 +654,13 @@ class CLISessionMixin:
_db = getattr(self, "_session_db", None)
_sid = getattr(self, "session_id", None)
if _db and _sid:
+ if fmt not in SAVE_TRANSCRIPT_FORMATS:
+ from hermes_state import SessionExportTooLargeError
+ try:
+ _db.assert_export_safe(_sid) # the JSON backup loads every stored row, like `sessions export`
+ except SessionExportTooLargeError as e:
+ print(f"(._.) {e}")
+ return
try:
session_data = _db.export_session(_sid, **export_projection(fmt in SAVE_TRANSCRIPT_FORMATS))
except Exception:
diff --git a/tests/hermes_cli/test_save_transcript_history.py b/tests/hermes_cli/test_save_transcript_history.py
index 00a07cf0416..17f3d04a1cc 100644
--- a/tests/hermes_cli/test_save_transcript_history.py
+++ b/tests/hermes_cli/test_save_transcript_history.py
@@ -1,5 +1,7 @@
"""/save md (CLI and gateway) carries the display history, compaction-archived turns included."""
import asyncio
+import contextlib
+import io
from datetime import datetime
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
@@ -28,8 +30,10 @@ def _cli_save(db, fmt, out):
stub = SimpleNamespace(_session_db=db, session_id="s1", conversation_history=[], model="m",
session_start=datetime(2026, 1, 1))
- cli.HermesCLI.save_conversation(stub, f"/save {fmt} {out}")
- return out.read_text(encoding="utf-8")
+ printed = io.StringIO()
+ with contextlib.redirect_stdout(printed):
+ cli.HermesCLI.save_conversation(stub, f"/save {fmt} {out}")
+ return out.read_text(encoding="utf-8") if out.exists() else printed.getvalue()
def _gateway_save(db, fmt, out):
@@ -52,8 +56,8 @@ def _gateway_save(db, fmt, out):
updated_at=datetime.now(), platform=Platform.TELEGRAM, chat_type="dm")
runner._session_db = AsyncSessionDB(db)
event = MessageEvent(text=f"/save {fmt} {out.name}", source=source, message_id="m1")
- assert asyncio.run(runner._handle_save_command(event)) == "Export complete."
- return delivered["text"]
+ reply = asyncio.run(runner._handle_save_command(event))
+ return delivered["text"] if reply == "Export complete." else reply
@pytest.mark.parametrize("save", [_cli_save, _gateway_save], ids=["cli", "gateway"])
@@ -84,6 +88,9 @@ def test_save_json_restores_compacted_history_as_archived(tmp_path, monkeypatch,
try:
shown, live = shape(db, include_compacted=True), shape(db)
snapshot = json.loads(save(db, "json", tmp_path / "saved.json"))
+ # Like `hermes sessions export`, the in-memory backup is capped per session (sessions.max_export_messages).
+ monkeypatch.setattr("hermes_state.resolved_max_export_messages", lambda: 5)
+ assert "max_export_messages" in save(db, "json", tmp_path / "capped.json")
finally:
db.close()
restored = SessionDB(db_path=tmp_path / "restored.db")
diff --git a/website/docs/user-guide/sessions.md b/website/docs/user-guide/sessions.md
index b8241322895..5f8e9fbca13 100644
--- a/website/docs/user-guide/sessions.md
+++ b/website/docs/user-guide/sessions.md
@@ -1127,8 +1127,8 @@ sessions:
max_export_messages: 20000 # one-shot in-memory export of a single session
```
-`max_export_messages` applies per session to the JSON/JSONL backup (`hermes sessions export`, and
-`sessions export` in `hermes console`). It counts every stored row, archived included, because
+`max_export_messages` applies per session to the JSON/JSONL backup (`hermes sessions export`,
+`sessions export` in `hermes console`, and CLI/messaging `/save json`). It counts every stored row, archived included, because
the backup holds all of them. A heavily compacted session with a small live tail can still exceed it.
The dashboard Sessions page's Export action streams the rows instead and is not capped.