Skip to content

The agent that grows with you

fix(sessions): `hermes sessions export` JSONL backups keep compaction-archived turns

Froraut92a6b252f50d
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.py
index efa49497db1..fd22b0386df 100644
--- a/hermes_cli/sessions_cmd.py
+++ b/hermes_cli/sessions_cmd.py
@@ -341,10 +341,16 @@ def _cmd_export(db, args):
# --only is a transcript view too (md/jsonl of what the user saw); md/qmd without --only go to _export_markdown.
shown = args.format in SAVE_TRANSCRIPT_FORMATS or bool(getattr(args, "only", None))
+ # A shown transcript is the display history; the json/jsonl backup is the TRANSFER projection —
+ # every row with its active/compacted flags, so an import restores a compacted session's whole
+ # history (live-only dropped every turn in-place compaction archived; #122679 fixed only the
+ # console export).
+ projection = {"include_compacted": True} if shown else {"include_inactive": True}
+
def _collect_sessions():
"""--session-id / filters / bare export -> redacted session dicts, or None after printing an error."""
def _one(session_id):
- return _redact(db.export_session(session_id, include_compacted=shown))
+ return _redact(db.export_session(session_id, **projection))
if args.session_id:
resolved = db.resolve_session_id(args.session_id)
data = _one(resolved) if resolved else None
@@ -359,7 +365,7 @@ def _cmd_export(db, args):
return [s for s in (_one(row["id"]) for row in candidates) if s]
if args.dry_run:
return print("--dry-run requires at least one filter.")
- return [_redact(s) for s in db.export_all(source=None, include_compacted=shown)]
+ return [_redact(s) for s in db.export_all(source=None, **projection)]
if getattr(args, "only", None):
return _export_flat("only", args, _collect_sessions)
if args.format == "trace":
diff --git a/tests/hermes_cli/test_sessions_export_backup_round_trip.py b/tests/hermes_cli/test_sessions_export_backup_round_trip.py
new file mode 100644
index 00000000000..4bb236b934b
--- /dev/null
+++ b/tests/hermes_cli/test_sessions_export_backup_round_trip.py
@@ -0,0 +1,42 @@
+"""`hermes sessions export` JSONL — the documented backup format — must round-trip a compacted
+session: import restores the turns in-place compaction archived, still archived (#122679 fixed
+only the console export; the primary CLI kept the live-only projection)."""
+
+import json
+import sys
+
+import hermes_cli.main as main_mod
+from hermes_state import SessionDB
+
+SID = "20260929_120000_abcdef"
+
+
+def _shape(db, **flags):
+ return [(m["role"], m["content"], m.get("active", 1), m.get("compacted", 0))
+ for m in db.get_messages(SID, **flags)]
+
+
+def test_jsonl_backup_round_trips_compaction_archived_turns(tmp_path, monkeypatch):
+ src = SessionDB()
+ src.create_session(SID, source="cli")
+ for i in range(1, 5):
+ src.append_message(SID, "user", f"question {i}")
+ src.append_message(SID, "assistant", f"answer {i}")
+ tail = src.get_messages(SID)[-2:]
+ src.archive_and_compact(SID, [{"role": "user", "content": "[summary]"}, *tail], tail_count=2)
+ shown, live = _shape(src, include_compacted=True), _shape(src)
+ src.close()
+ assert len(shown) > len(live)
+
+ backup = tmp_path / "backup.jsonl"
+ monkeypatch.setattr(sys, "argv", ["hermes", "sessions", "export", str(backup)])
+ main_mod.main()
+
+ dst = SessionDB(db_path=tmp_path / "restored.db")
+ try:
+ payload = [json.loads(line) for line in backup.read_text(encoding="utf-8").splitlines() if line]
+ assert dst.import_sessions(payload)["ok"]
+ assert _shape(dst, include_compacted=True) == shown, "display history must survive the backup"
+ assert _shape(dst) == live, "archived turns must not come back as live context"
+ finally:
+ dst.close()
diff --git a/tests/hermes_cli/test_sessions_export_output_dir.py b/tests/hermes_cli/test_sessions_export_output_dir.py
index 42f1f965d6c..cd7aa5b102c 100644
--- a/tests/hermes_cli/test_sessions_export_output_dir.py
+++ b/tests/hermes_cli/test_sessions_export_output_dir.py
@@ -11,7 +11,7 @@ class _FakeDB:
def resolve_session_id(self, session_id):
return "sess-123"
- def export_session(self, session_id, include_compacted=False):
+ def export_session(self, session_id, **_projection):
return {"id": "sess-123", "source": "cli", "messages": [{"role": "user", "content": "hi"}]}
def close(self):