The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): rebuild /save json timings after dropping undone rows
kshitijk4poor3b6589108a45
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.pyindex e98bbfa602e..6a6ccdcf791 100644--- a/gateway/slash_commands_session.py+++ b/gateway/slash_commands_session.py@@ -716,8 +716,8 @@ class GatewaySessionCommandsMixin:"""Handle /save — export the current session and send it as a document."""import tempfilefrom hermes_cli.session_export import (- SAVE_TRANSCRIPT_FORMATS, SAVE_USAGE, default_save_filename, export_projection, normalize_save_format,- render_session_for_save)+ SAVE_TRANSCRIPT_FORMATS, SAVE_USAGE, default_save_filename, drop_undone_rows, export_projection,+ normalize_save_format, render_session_for_save)parts = event.get_command_args().split()redact = bool(parts) and parts[-1].lower() in ("redact", "--redact")@@ -748,8 +748,7 @@ class GatewaySessionCommandsMixin:if not export_data:return t("gateway.save.no_messages", session_id=session_id)if fmt not in SAVE_TRANSCRIPT_FORMATS:- # Turns removed by /undo, rewind or edit stay out of a file sent into the chat (archived turns are kept).- export_data["messages"] = [m for m in export_data["messages"] if m["active"] or m["compacted"]]+ drop_undone_rows(export_data)if redact:from hermes_cli.session_export_md import redact_session_dataexport_data = redact_session_data(export_data)diff --git a/hermes_cli/cli_session_mixin.py b/hermes_cli/cli_session_mixin.pyindex 088fdedc16d..dc6faf17be0 100644--- a/hermes_cli/cli_session_mixin.py+++ b/hermes_cli/cli_session_mixin.py@@ -631,7 +631,8 @@ class CLISessionMixin:"""from cli import datetimefrom hermes_cli.session_export import (- SAVE_TRANSCRIPT_FORMATS, SAVE_USAGE, export_projection, normalize_save_format, render_session_for_save)+ SAVE_TRANSCRIPT_FORMATS, SAVE_USAGE, drop_undone_rows, export_projection, normalize_save_format,+ render_session_for_save)parts = cmd.split()[1:]redact = bool(parts) and parts[-1].lower() in ("redact", "--redact")@@ -666,8 +667,7 @@ class CLISessionMixin:except Exception:session_data = Noneif session_data and fmt not in SAVE_TRANSCRIPT_FORMATS:- # Turns removed by /undo, rewind or edit stay out of a /save file (archived turns are kept).- session_data["messages"] = [m for m in session_data["messages"] if m["active"] or m["compacted"]]+ drop_undone_rows(session_data)if not session_data:if not self.conversation_history:print(t("cli.session.save_nothing"))diff --git a/hermes_cli/session_export.py b/hermes_cli/session_export.pyindex 8453260b22d..19c2585462a 100644--- a/hermes_cli/session_export.py+++ b/hermes_cli/session_export.py@@ -218,6 +218,15 @@ def export_projection(transcript: bool) -> Dict[str, bool]:return {"include_compacted": True} if transcript else {"include_inactive": True}+def drop_undone_rows(export: Dict[str, Any]) -> Dict[str, Any]:+ """Keep live and compaction-archived rows in a ``/save`` JSON snapshot; rows removed by /undo, rewind+ or edit stay out. ``timings`` is rebuilt from the kept rows so it names no dropped row."""+ from hermes_state_portability import _export_timings+ export["messages"] = [m for m in export["messages"] if m["active"] or m["compacted"]]+ export["timings"] = _export_timings(export["messages"], export.get("id"))+ return export++SAVE_USAGE = """/save — export the current session to a fileUsage: /save <format> [filename] [redact]diff --git a/tests/hermes_cli/test_save_transcript_history.py b/tests/hermes_cli/test_save_transcript_history.pyindex 6440c0ffb25..259e031913d 100644--- a/tests/hermes_cli/test_save_transcript_history.py+++ b/tests/hermes_cli/test_save_transcript_history.py@@ -95,6 +95,7 @@ def test_save_json_restores_compacted_history_as_archived(tmp_path, monkeypatch,snapshot = json.loads(save(db, "json", tmp_path / "saved.json"))kept = [m["id"] for m in db.get_messages("s1", include_inactive=True) if m["active"] or m["compacted"]]assert [m["id"] for m in snapshot["messages"]] == kept # every kept row, not the deduped display read+ assert sum(snapshot["timings"]["role_counts"].values()) == len(kept) # timings name no undone row# 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")